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-2019 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 const Foam::Enum
47 <
49 >
51 ({
52  { STATIC, "static" },
53  { TOTAL, "total" },
54  { ISENTROPIC, "isentropic" },
55  { STATIC_COEFF, "staticCoeff" },
56  { TOTAL_COEFF, "totalCoeff" },
57 });
58 
59 const Foam::Enum
60 <
62 >
64 ({
65  { NONE, "none" },
66  { ADD, "add" },
67  { SUBTRACT, "subtract" },
68 });
69 
70 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
71 
72 Foam::word Foam::functionObjects::pressure::resultName() const
73 {
74  word rName;
75 
76  if (mode_ & STATIC)
77  {
78  rName = "static(" + fieldName_ + ")";
79  }
80  else if (mode_ & TOTAL)
81  {
82  rName = "total(" + fieldName_ + ")";
83  }
84  else if (mode_ & ISENTROPIC)
85  {
86  rName = "isentropic(" + fieldName_ + ")";
87  }
88  else
89  {
91  << "Unhandled calculation mode " << modeNames[mode_]
92  << abort(FatalError);
93  }
94 
95  switch (hydrostaticMode_)
96  {
97  case NONE:
98  {
99  break;
100  }
101  case ADD:
102  {
103  rName = rName + "+rgh";
104 
105  break;
106  }
107  case SUBTRACT:
108  {
109  rName = rName + "-rgh";
110 
111  break;
112  }
113  }
114 
115  if (mode_ & COEFF)
116  {
117  rName += "_coeff";
118  }
119 
120  return rName;
121 }
122 
123 
124 Foam::tmp<Foam::volScalarField> Foam::functionObjects::pressure::rhoScale
125 (
126  const volScalarField& p
127 ) const
128 {
129  if (p.dimensions() == dimPressure)
130  {
132  (
133  IOobject
134  (
135  "rhoScale",
136  p.mesh().time().timeName(),
137  p.mesh(),
140  false
141  ),
142  p,
144  );
145  }
146 
147  if (!rhoInfInitialised_)
148  {
150  << type() << " " << name() << ": "
151  << "pressure identified as incompressible, but reference "
152  << "density is not set. Please set 'rho' to 'rhoInf', and "
153  << "set an appropriate value for 'rhoInf'"
154  << exit(FatalError);
155  }
156 
157  return dimensionedScalar("rhoInf", dimDensity, rhoInf_)*p;
158 }
159 
160 
161 Foam::tmp<Foam::volScalarField> Foam::functionObjects::pressure::rhoScale
162 (
163  const volScalarField& p,
164  const tmp<volScalarField>& tsf
165 ) const
166 {
167  if (p.dimensions() == dimPressure)
168  {
169  return lookupObject<volScalarField>(rhoName_)*tsf;
170  }
171 
172  return dimensionedScalar("rhoInf", dimDensity, rhoInf_)*tsf;
173 }
174 
175 
176 void Foam::functionObjects::pressure::addHydrostaticContribution
177 (
179 ) const
180 {
181  // Add/subtract hydrostatic contribution
182 
183  if (hydrostaticMode_ == NONE)
184  {
185  return;
186  }
187 
188  if (!gInitialised_)
189  {
190  g_ = mesh_.time().lookupObject<uniformDimensionedVectorField>("g");
191  }
192 
193  if (!hRefInitialised_)
194  {
195  hRef_ = mesh_.lookupObject<uniformDimensionedScalarField>("hRef");
196  }
197 
198  const dimensionedScalar ghRef
199  (
200  (g_ & (cmptMag(g_.value())/mag(g_.value())))*hRef_
201  );
202 
203  tmp<volScalarField> rgh = rhoScale(p, (g_ & mesh_.C()) - ghRef);
204 
205  switch (hydrostaticMode_)
206  {
207  case ADD:
208  {
209  p += rgh;
210  break;
211  }
212  case SUBTRACT:
213  {
214  p -= rgh;
215  break;
216  }
217  default:
218  {}
219  }
220 }
221 
222 
223 Foam::tmp<Foam::volScalarField> Foam::functionObjects::pressure::calcPressure
224 (
225  const volScalarField& p,
226  const tmp<volScalarField>& tp
227 ) const
228 {
229  // Initialise to the pressure reference level
230  auto tresult =
232  (
233  IOobject
234  (
235  name() + ":p",
236  mesh_.time().timeName(),
237  mesh_,
239  ),
240  mesh_,
241  dimensionedScalar("p", dimPressure, pRef_)
242  );
243 
244  volScalarField& result = tresult.ref();
245 
246  addHydrostaticContribution(result);
247 
248  if (mode_ & STATIC)
249  {
250  result += tp;
251  return tresult;
252  }
253 
254  if (mode_ & TOTAL)
255  {
256  result +=
257  tp
258  + rhoScale(p, 0.5*magSqr(lookupObject<volVectorField>(UName_)));
259  return tresult;
260  }
261 
262  if (mode_ & ISENTROPIC)
263  {
264  const basicThermo* thermoPtr =
265  p.mesh().lookupObjectPtr<basicThermo>(basicThermo::dictName);
266 
267  if (!thermoPtr)
268  {
270  << "Isentropic pressure calculation requires a "
271  << "thermodynamics package"
272  << exit(FatalError);
273  }
274 
275  const volScalarField gamma(thermoPtr->gamma());
276  const volScalarField Mb
277  (
278  mag(lookupObject<volVectorField>(UName_))
279  /sqrt(gamma*tp.ref()/thermoPtr->rho())
280  );
281 
282  result += tp*(pow(1 + (gamma - 1)/2*sqr(Mb), gamma/(gamma - 1)));
283  return tresult;
284  }
285 
286 
287  return tresult;
288 }
289 
290 
291 Foam::tmp<Foam::volScalarField> Foam::functionObjects::pressure::coeff
292 (
293  const tmp<volScalarField>& tp
294 ) const
295 {
296  if (mode_ & COEFF)
297  {
298  tmp<volScalarField> tpCoeff(tp.ptr());
299  volScalarField& pCoeff = tpCoeff.ref();
300 
301  pCoeff -= dimensionedScalar("pInf", dimPressure, pInf_);
302 
303  const dimensionedScalar pSmall("pSmall", dimPressure, SMALL);
304  const dimensionedVector U("U", dimVelocity, UInf_);
305  const dimensionedScalar rho("rho", dimDensity, rhoInf_);
306 
307  pCoeff /= 0.5*rho*magSqr(U) + pSmall;
308 
309  return tpCoeff;
310  }
311 
312  return std::move(tp);
313 }
314 
315 
316 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
317 
318 bool Foam::functionObjects::pressure::calc()
319 {
320  if (foundObject<volScalarField>(fieldName_))
321  {
322  const volScalarField& p = lookupObject<volScalarField>(fieldName_);
323 
324  auto tp = tmp<volScalarField>::New
325  (
326  IOobject
327  (
328  resultName_,
329  p.mesh().time().timeName(),
330  p.mesh(),
333  ),
334  coeff(calcPressure(p, rhoScale(p)))
335  );
336 
337  return store(resultName_, tp);
338  }
339 
340  return false;
341 }
342 
343 
344 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
345 
347 (
348  const word& name,
349  const Time& runTime,
350  const dictionary& dict
351 )
352 :
354  mode_(STATIC),
355  hydrostaticMode_(NONE),
356  UName_("U"),
357  rhoName_("rho"),
358  pRef_(0),
359  pInf_(0),
360  UInf_(Zero),
361  rhoInf_(1),
362  rhoInfInitialised_(false),
363  g_(dimAcceleration),
364  gInitialised_(false),
365  hRef_(dimLength),
366  hRefInitialised_(false)
367 {
368  read(dict);
369 }
370 
371 
372 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
373 
375 {
376  Info<< type() << " " << name() << ":" << nl;
377 
379 
380  UName_ = dict.lookupOrDefault<word>("U", "U");
381  rhoName_ = dict.lookupOrDefault<word>("rho", "rho");
382 
383  if (rhoName_ == "rhoInf")
384  {
385  dict.readEntry("rhoInf", rhoInf_);
386  rhoInfInitialised_ = true;
387  }
388 
389  if (!modeNames.readIfPresent("mode", dict, mode_))
390  {
391  // Backwards compatibility
392  // - check for the presence of 'calcTotal' and 'calcCoeff'
393 
394  bool calcTotal =
395  dict.getOrDefaultCompat<bool>("mode", {{"calcTotal", 1812}}, false);
396  bool calcCoeff =
397  dict.getOrDefaultCompat<bool>("mode", {{"calcCoeff", 1812}}, false);
398 
399  if (calcTotal)
400  {
401  mode_ = TOTAL;
402  }
403  else
404  {
405  mode_ = STATIC;
406  }
407 
408  if (calcCoeff)
409  {
410  mode_ = static_cast<mode>(COEFF | mode_);
411  }
412  }
413 
414  Info<< " Operating mode: " << modeNames[mode_] << nl;
415 
416  pRef_ = dict.lookupOrDefault<scalar>("pRef", 0);
417 
418  if
419  (
420  hydrostaticModeNames.readIfPresent
421  (
422  "hydrostaticMode",
423  dict,
424  hydrostaticMode_
425  )
426  && hydrostaticMode_
427  )
428  {
429  Info<< " Hydrostatic mode: "
430  << hydrostaticModeNames[hydrostaticMode_]
431  << nl;
432  gInitialised_ = dict.readIfPresent("g", g_);
433  hRefInitialised_ = dict.readIfPresent("hRef", hRef_);
434  }
435  else
436  {
437  Info<< " Not including hydrostatic effects" << nl;
438  }
439 
440 
441 
442  if (mode_ & COEFF)
443  {
444  dict.readEntry("pInf", pInf_);
445  dict.readEntry("UInf", UInf_);
446  dict.readEntry("rhoInf", rhoInf_);
447 
448  scalar zeroCheck = 0.5*rhoInf_*magSqr(UInf_) + pInf_;
449 
450  if (mag(zeroCheck) < ROOTVSMALL)
451  {
453  << type() << " " << name() << ": "
454  << "Coefficient calculation requested, but reference "
455  << "pressure level is zero. Please check the supplied "
456  << "values of pInf, UInf and rhoInf" << endl;
457  }
458 
459  rhoInfInitialised_ = true;
460  }
461 
462  resultName_ = dict.lookupOrDefault<word>("result", resultName());
463 
464  Info<< endl;
465 
466  return true;
467 }
468 
469 
470 // ************************************************************************* //
Foam::functionObjects::pressure::STATIC
Static pressure.
Definition: pressure.H:267
Foam::IOobject::NO_WRITE
Definition: IOobject.H:130
volFields.H
Foam::functionObjects::pressure::mode
mode
Enumeration for pressure calculation mode.
Definition: pressure.H:265
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::dimPressure
const dimensionSet dimPressure
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::Enum
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: IOstreamOption.H:51
basicThermo.H
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::dimLength
const dimensionSet dimLength(0, 1, 0, 0, 0, 0, 0)
Definition: dimensionSets.H:53
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:59
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::dimVelocity
const dimensionSet dimVelocity
Foam::dimDensity
const dimensionSet dimDensity
Foam::read
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
Foam::functionObjects::pressure::ADD
Definition: pressure.H:281
Foam::functionObjects::fieldExpression::read
virtual bool read(const dictionary &dict)
Read the fieldExpression data.
Definition: fieldExpression.C:91
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::functionObjects::pressure::hydrostaticModeNames
static const Enum< hydrostaticMode > hydrostaticModeNames
Definition: pressure.H:285
Foam::fvPatchField< scalar >::calculatedType
static const word & calculatedType()
Return the type of the calculated for of fvPatchField.
Definition: calculatedFvPatchField.C:34
rho
rho
Definition: readInitialConditions.H:96
Foam::functionObjects::addToRunTimeSelectionTable
addToRunTimeSelectionTable(functionObject, add, dictionary)
Foam::functionObjects::pressure::SUBTRACT
Definition: pressure.H:282
Foam::functionObjects::pressure::ISENTROPIC
Isentropic pressure.
Definition: pressure.H:269
Foam::magSqr
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
Foam::dimensionedVector
dimensioned< vector > dimensionedVector
Dimensioned vector obtained from generic dimensioned type.
Definition: dimensionedVector.H:50
Foam::functionObjects::pressure::read
virtual bool read(const dictionary &)
Read the pressure data.
Definition: pressure.C:374
Foam::cmptMag
void cmptMag(FieldField< Field, Type > &cf, const FieldField< Field, Type > &f)
Definition: FieldFieldFunctions.C:400
Foam::parsing::errorType::NONE
No error encountered.
Foam::functionObjects::pressure::TOTAL
Total pressure.
Definition: pressure.H:268
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::functionObjects::pressure::pressure
pressure(const word &name, const Time &runTime, const dictionary &)
Construct from Time and dictionary.
Definition: pressure.C:347
Foam::functionObjects::pressure::NONE
Definition: pressure.H:280
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::uniformDimensionedVectorField
UniformDimensionedField< vector > uniformDimensionedVectorField
Definition: uniformDimensionedFields.H:50
Foam::uniformDimensionedScalarField
UniformDimensionedField< scalar > uniformDimensionedScalarField
Definition: uniformDimensionedFields.H:49
Foam::dimensionedScalar
dimensioned< scalar > dimensionedScalar
Dimensioned scalar obtained from generic dimensioned type.
Definition: dimensionedScalarFwd.H:43
Foam::volScalarField
GeometricField< scalar, fvPatchField, volMesh > volScalarField
Definition: volFieldsFwd.H:57
Foam::pow
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
Definition: dimensionedScalar.C:75
Foam::dictionary::dictName
word dictName() const
The local dictionary name (final part of scoped name)
Definition: dictionary.H:458
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::functionObjects::fieldExpression::fieldName_
word fieldName_
Name of field to process.
Definition: fieldExpression.H:69
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::functionObjects::pressure::hydrostaticMode
hydrostaticMode
Enumeration for hydrostatic contributions.
Definition: pressure.H:278
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::functionObjects::pressure::COEFF
Coefficient manipulator.
Definition: pressure.H:270
Foam::dimAcceleration
const dimensionSet dimAcceleration
Foam::functionObjects::defineTypeNameAndDebug
defineTypeNameAndDebug(fvMeshFunctionObject, 0)
pressure.H
uniformDimensionedFields.H
U
U
Definition: pEqn.H:72
Foam::functionObjects::pressure::modeNames
static const Enum< mode > modeNames
Definition: pressure.H:275
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::sqr
dimensionedSymmTensor sqr(const dimensionedVector &dv)
Definition: dimensionedSymmTensor.C:51
Foam::functionObjects::fieldExpression
Base class for field expression function objects.
Definition: fieldExpression.H:60
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::GeometricField::ref
Internal & ref(const bool updateAccessTime=true)
Return a reference to the dimensioned internal field.
Definition: GeometricField.C:718
Foam::sqrt
dimensionedScalar sqrt(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:144
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::type
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:590
gamma
const scalar gamma
Definition: EEqn.H:9
Foam::tmp::New
static tmp< T > New(Args &&... args)
Construct tmp of T with forwarding arguments.
Foam::IOobject::NO_READ
Definition: IOobject.H:123
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:294