pressure.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2012-2016 OpenFOAM Foundation
9  Copyright (C) 2016-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "pressure.H"
30 #include "volFields.H"
31 #include "basicThermo.H"
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
39 namespace functionObjects
40 {
41  defineTypeNameAndDebug(pressure, 0);
42  addToRunTimeSelectionTable(functionObject, pressure, dictionary);
43 }
44 }
45 
46 
47 const Foam::Enum
48 <
50 >
52 ({
53  { STATIC, "static" },
54  { TOTAL, "total" },
55  { ISENTROPIC, "isentropic" },
56  { STATIC_COEFF, "staticCoeff" },
57  { TOTAL_COEFF, "totalCoeff" },
58 });
59 
60 
61 const Foam::Enum
62 <
64 >
66 ({
67  { NONE, "none" },
68  { ADD, "add" },
69  { SUBTRACT, "subtract" },
70 });
71 
72 
73 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
74 
75 Foam::word Foam::functionObjects::pressure::resultName() const
76 {
77  word rName;
78 
79  if (mode_ & STATIC)
80  {
81  rName = "static(" + fieldName_ + ")";
82  }
83  else if (mode_ & TOTAL)
84  {
85  rName = "total(" + fieldName_ + ")";
86  }
87  else if (mode_ & ISENTROPIC)
88  {
89  rName = "isentropic(" + fieldName_ + ")";
90  }
91  else
92  {
94  << "Unhandled calculation mode " << modeNames[mode_]
95  << abort(FatalError);
96  }
97 
98  switch (hydrostaticMode_)
99  {
100  case NONE:
101  {
102  break;
103  }
104  case ADD:
105  {
106  rName = rName + "+rgh";
107 
108  break;
109  }
110  case SUBTRACT:
111  {
112  rName = rName + "-rgh";
113 
114  break;
115  }
116  }
117 
118  if (mode_ & COEFF)
119  {
120  rName += "_coeff";
121  }
122 
123  return rName;
124 }
125 
126 
127 Foam::tmp<Foam::volScalarField> Foam::functionObjects::pressure::rhoScale
128 (
129  const volScalarField& p
130 ) const
131 {
132  if (p.dimensions() == dimPressure)
133  {
134  return volScalarField::New
135  (
136  "rhoScale",
138  p,
140  );
141  }
142 
143  if (!rhoInfInitialised_)
144  {
146  << type() << " " << name() << ": "
147  << "pressure identified as incompressible, but reference "
148  << "density is not set. Please set 'rho' to 'rhoInf', and "
149  << "set an appropriate value for 'rhoInf'"
150  << exit(FatalError);
151  }
152 
153  return dimensionedScalar("rhoInf", dimDensity, rhoInf_)*p;
154 }
155 
156 
157 Foam::tmp<Foam::volScalarField> Foam::functionObjects::pressure::rhoScale
158 (
159  const volScalarField& p,
160  const tmp<volScalarField>& tsf
161 ) const
162 {
163  if (p.dimensions() == dimPressure)
164  {
165  return lookupObject<volScalarField>(rhoName_)*tsf;
166  }
167 
168  return dimensionedScalar("rhoInf", dimDensity, rhoInf_)*tsf;
169 }
170 
171 
172 void Foam::functionObjects::pressure::addHydrostaticContribution
173 (
174  const volScalarField& p,
175  volScalarField& prgh
176 ) const
177 {
178  // Add/subtract hydrostatic contribution
179 
180  if (hydrostaticMode_ == NONE)
181  {
182  return;
183  }
184 
185  if (!gInitialised_)
186  {
187  g_ = mesh_.time().lookupObject<uniformDimensionedVectorField>("g");
188  }
189 
190  if (!hRefInitialised_)
191  {
192  hRef_ = mesh_.lookupObject<uniformDimensionedScalarField>("hRef");
193  }
194 
195  const dimensionedScalar ghRef
196  (
197  (g_ & (cmptMag(g_.value())/mag(g_.value())))*hRef_
198  );
199 
200  const auto oldConsistency = FieldBase::localBoundaryConsistency(0);
201 
202  tmp<volScalarField> rgh = rhoScale(p, (g_ & mesh_.C()) - ghRef);
203 
204  switch (hydrostaticMode_)
205  {
206  case ADD:
207  {
208  prgh += rgh;
209  break;
210  }
211  case SUBTRACT:
212  {
213  prgh -= rgh;
214  break;
215  }
216  default:
217  {}
218  }
219 
220  FieldBase::localBoundaryConsistency(oldConsistency);
221 }
222 
223 
224 Foam::tmp<Foam::volScalarField> Foam::functionObjects::pressure::calcPressure
225 (
226  const volScalarField& p,
227  const tmp<volScalarField>& tp
228 ) const
229 {
230  // Initialise to the pressure reference level
231  auto tresult = volScalarField::New
232  (
233  scopedName("p"),
235  mesh_,
236  dimensionedScalar("p", dimPressure, pRef_)
237  );
238  auto& result = tresult.ref();
239 
240  addHydrostaticContribution(p, result);
241 
242  if (mode_ & STATIC)
243  {
244  result += tp;
245  return tresult;
246  }
247 
248  if (mode_ & TOTAL)
249  {
250  result +=
251  tp
252  + rhoScale(p, 0.5*magSqr(lookupObject<volVectorField>(UName_)));
253  return tresult;
254  }
255 
256  if (mode_ & ISENTROPIC)
257  {
258  const basicThermo* thermoPtr =
259  p.mesh().cfindObject<basicThermo>(basicThermo::dictName);
260 
261  if (!thermoPtr)
262  {
264  << "Isentropic pressure calculation requires a "
265  << "thermodynamics package"
266  << exit(FatalError);
267  }
268 
269  const volScalarField gamma(thermoPtr->gamma());
270  const volScalarField Mb
271  (
272  mag(lookupObject<volVectorField>(UName_))
273  /sqrt(gamma*tp.ref()/thermoPtr->rho())
274  );
275 
276  result += tp*(pow(1 + (gamma - 1)/2*sqr(Mb), gamma/(gamma - 1)));
277  return tresult;
278  }
279 
280  return tresult;
281 }
282 
283 
284 Foam::tmp<Foam::volScalarField> Foam::functionObjects::pressure::coeff
285 (
286  const tmp<volScalarField>& tp
287 ) const
288 {
289  if (mode_ & COEFF)
290  {
291  tmp<volScalarField> tpCoeff(tp.ptr());
292  auto& pCoeff = tpCoeff.ref();
293 
294  pCoeff -= dimensionedScalar("pInf", dimPressure, pInf_);
295 
296  const dimensionedScalar pSmall("pSmall", dimPressure, SMALL);
297  const dimensionedVector U("U", dimVelocity, UInf_);
298  const dimensionedScalar rho("rho", dimDensity, rhoInf_);
299 
300  pCoeff /= 0.5*rho*magSqr(U) + pSmall;
301 
302  return tpCoeff;
303  }
304 
305  return std::move(tp);
306 }
307 
308 
309 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
310 
311 bool Foam::functionObjects::pressure::calc()
312 {
313  const auto* pptr = cfindObject<volScalarField>(fieldName_);
314 
315  if (pptr)
316  {
317  const auto& p = *pptr;
318 
319  auto tp = volScalarField::New
320  (
321  resultName_,
323  coeff(calcPressure(p, rhoScale(p)))
324  );
325 
326  return store(resultName_, tp);
327  }
328 
329  return false;
330 }
331 
332 
333 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
334 
336 (
337  const word& name,
338  const Time& runTime,
339  const dictionary& dict
340 )
341 :
343  mode_(STATIC),
344  hydrostaticMode_(NONE),
345  UName_("U"),
346  rhoName_("rho"),
347  pRef_(0),
348  pInf_(0),
349  UInf_(Zero),
350  rhoInf_(1),
351  rhoInfInitialised_(false),
352  g_(dimAcceleration),
353  gInitialised_(false),
354  hRef_(dimLength),
355  hRefInitialised_(false)
356 {
357  read(dict);
358 }
359 
360 
361 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
362 
364 {
365  Info<< type() << " " << name() << ":" << nl;
366 
368 
369  UName_ = dict.getOrDefault<word>("U", "U");
370  rhoName_ = dict.getOrDefault<word>("rho", "rho");
371 
372  if (rhoName_ == "rhoInf")
373  {
374  dict.readEntry("rhoInf", rhoInf_);
375  rhoInfInitialised_ = true;
376  }
377 
378  if (!modeNames.readIfPresent("mode", dict, mode_))
379  {
380  // Backwards compatibility
381  // - check for the presence of 'calcTotal' and 'calcCoeff'
382 
383  bool calcTotal =
384  dict.getOrDefaultCompat<bool>("mode", {{"calcTotal", 1812}}, false);
385  bool calcCoeff =
386  dict.getOrDefaultCompat<bool>("mode", {{"calcCoeff", 1812}}, false);
387 
388  if (calcTotal)
389  {
390  mode_ = TOTAL;
391  }
392  else
393  {
394  mode_ = STATIC;
395  }
396 
397  if (calcCoeff)
398  {
399  mode_ = static_cast<mode>(COEFF | mode_);
400  }
401  }
402 
403  Info<< " Operating mode: " << modeNames[mode_] << nl;
404 
405  pRef_ = dict.getOrDefault<scalar>("pRef", 0);
406 
407  if
408  (
409  hydrostaticModeNames.readIfPresent
410  (
411  "hydrostaticMode",
412  dict,
413  hydrostaticMode_
414  )
415  && hydrostaticMode_
416  )
417  {
418  Info<< " Hydrostatic mode: "
419  << hydrostaticModeNames[hydrostaticMode_]
420  << nl;
421  gInitialised_ = dict.readIfPresent("g", g_);
422  hRefInitialised_ = dict.readIfPresent("hRef", hRef_);
423  }
424  else
425  {
426  Info<< " Not including hydrostatic effects" << nl;
427  }
428 
429 
430  if (mode_ & COEFF)
431  {
432  dict.readEntry("pInf", pInf_);
433  dict.readEntry("UInf", UInf_);
434  dict.readEntry("rhoInf", rhoInf_);
435 
436  const scalar zeroCheck = 0.5*rhoInf_*magSqr(UInf_) + pInf_;
437 
438  if (mag(zeroCheck) < ROOTVSMALL)
439  {
441  << type() << " " << name() << ": "
442  << "Coefficient calculation requested, but reference "
443  << "pressure level is zero. Please check the supplied "
444  << "values of pInf, UInf and rhoInf" << endl;
445  }
446 
447  rhoInfInitialised_ = true;
448  }
449 
450  resultName_ = dict.getOrDefault<word>("result", resultName());
451 
452  Info<< endl;
453 
454  return true;
455 }
456 
457 
458 // ************************************************************************* //
word dictName() const
The local dictionary name (final part of scoped name)
Definition: dictionaryI.H:53
dictionary dict
defineTypeNameAndDebug(ObukhovLength, 0)
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
static const Enum< hydrostaticMode > hydrostaticModeNames
Definition: pressure.H:378
UniformDimensionedField< vector > uniformDimensionedVectorField
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:600
word fieldName_
Name of field to process.
dimensionedSymmTensor sqr(const dimensionedVector &dv)
mode
Enumeration for pressure calculation mode.
Definition: pressure.H:356
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Various UniformDimensionedField types.
dimensioned< vector > dimensionedVector
Dimensioned vector obtained from generic dimensioned type.
engineTime & runTime
dimensionedScalar sqrt(const dimensionedScalar &ds)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:529
virtual bool read(const dictionary &dict)
Read the fieldExpression data.
No type, or default initialized type.
pressure(const word &name, const Time &runTime, const dictionary &)
Construct from Time and dictionary.
Definition: pressure.C:329
UniformDimensionedField< scalar > uniformDimensionedScalarField
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
Macros for easy insertion into run-time selection tables.
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:127
GeometricField< scalar, fvPatchField, volMesh > volScalarField
Definition: volFieldsFwd.H:72
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:801
const dimensionSet dimAcceleration
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
hydrostaticMode
Enumeration for hydrostatic contributions.
Definition: pressure.H:371
A class for handling words, derived from Foam::string.
Definition: word.H:63
virtual bool read(const dictionary &)
Read the pressure data.
Definition: pressure.C:356
Coefficient manipulator.
Definition: pressure.H:361
const dimensionSet dimPressure
errorManip< error > abort(error &err)
Definition: errorManip.H:139
static tmp< GeometricField< scalar, fvPatchField, volMesh > > New(const word &name, IOobjectOption::registerOption regOpt, const Mesh &mesh, const dimensionSet &dims, const word &patchFieldType=fvPatchField< scalar >::calculatedType())
Return tmp field (NO_READ, NO_WRITE) from name, mesh, dimensions and patch type. [Takes current timeN...
void cmptMag(FieldField< Field, Type > &cf, const FieldField< Field, Type > &f)
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
const dimensionSet dimDensity
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
U
Definition: pEqn.H:72
#define WarningInFunction
Report a warning using Foam::Warning.
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: error.H:64
const dimensionSet dimLength(0, 1, 0, 0, 0, 0, 0)
Definition: dimensionSets.H:50
dimensioned< scalar > dimensionedScalar
Dimensioned scalar obtained from generic dimensioned type.
Intermediate class for handling field expression function objects (e.g. blendingFactor etc...
static const word & calculatedType() noexcept
The type name for calculated patch fields.
Definition: fvPatchField.H:185
messageStream Info
Information stream (stdout output on master, null elsewhere)
const scalar gamma
Definition: EEqn.H:9
volScalarField & p
A class for managing temporary objects.
Definition: HashPtrTable.H:50
static int localBoundaryConsistency() noexcept
Get flag for local boundary consistency checks.
Definition: Field.H:127
mode_t mode(const fileName &name, const bool followLink=true)
Return the file mode, normally following symbolic links.
Definition: POSIX.C:775
Request registration (bool: true)
Do not request registration (bool: false)
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
Namespace for OpenFOAM.
static const Enum< mode > modeNames
Definition: pressure.H:366
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127
const dimensionSet dimVelocity