derivedFields.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) 2019-2020 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "derivedFields.H"
29 #include "volFields.H"
30 #include "dictionary.H"
31 #include "Time.H"
32 #include "mapPolyMesh.H"
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
39 namespace functionObjects
40 {
41  defineTypeNameAndDebug(derivedFields, 0);
42  addToRunTimeSelectionTable(functionObject, derivedFields, dictionary);
43 }
44 }
45 
46 
47 const Foam::Enum
48 <
50 >
52 ({
53  { derivedType::NONE , "none" },
54  { derivedType::MASS_FLUX , "rhoU" },
55  { derivedType::TOTAL_PRESSURE , "pTotal" },
56 });
57 
58 
59 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
60 
61 namespace Foam
62 {
63 
64 static bool calc_rhoU
65 (
66  const fvMesh& mesh,
67  const word& derivedName,
68  const scalar rhoRef
69 )
70 {
71  // rhoU = rho * U
72 
73  const auto* rhoPtr = mesh.findObject<volScalarField>("rho");
75 
76  volVectorField* result = mesh.getObjectPtr<volVectorField>(derivedName);
77 
78  const bool isNew = !result;
79 
80  if (!result)
81  {
82  result = new volVectorField
83  (
84  IOobject
85  (
86  derivedName,
87  mesh.time().timeName(),
88  mesh,
92  ),
93  mesh,
95  );
96 
97  result->store();
98  }
99 
100  if (rhoPtr)
101  {
102  const auto& rho = *rhoPtr;
103 
104  *result = (rho * U);
105  }
106  else
107  {
108  const dimensionedScalar rho("rho", dimDensity, rhoRef);
109 
110  *result = (rho * U);
111  }
112 
113  return isNew;
114 }
115 
116 
117 static bool calc_pTotal
118 (
119  const fvMesh& mesh,
120  const word& derivedName,
121  const scalar rhoRef
122 )
123 {
124  // pTotal = p + rho * U^2 / 2
125 
126  const auto* rhoPtr = mesh.findObject<volScalarField>("rho");
129 
130  volScalarField* result = mesh.getObjectPtr<volScalarField>(derivedName);
131 
132  const bool isNew = !result;
133 
134  if (!result)
135  {
136  result = new volScalarField
137  (
138  IOobject
139  (
140  derivedName,
141  mesh.time().timeName(),
142  mesh,
146  ),
147  mesh,
149  );
150 
151  result->store();
152  }
153 
154  if (rhoPtr)
155  {
156  const auto& rho = *rhoPtr;
157 
158  *result = (p + 0.5 * rho * magSqr(U));
159  }
160  else
161  {
162  const dimensionedScalar rho("rho", dimDensity, rhoRef);
163 
164  *result = (rho * (p + 0.5 * magSqr(U)));
165  }
166 
167  return isNew;
168 }
169 } // End namespace Foam
170 
171 
172 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
173 
175 (
176  const word& name,
177  const Time& runTime,
178  const dictionary& dict
179 )
180 :
181  fvMeshFunctionObject(name, runTime, dict),
182  derivedTypes_(),
183  rhoRef_(1.0)
184 {
185  read(dict);
186 }
187 
188 
189 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
190 
192 {
194 
195  rhoRef_ = dict.getOrDefault<scalar>("rhoRef", 1);
196 
197  wordList derivedNames(dict.get<wordList>("derived"));
198 
199  derivedTypes_.resize(derivedNames.size());
200 
201  label nbad = 0, ngood = 0;
202 
203  for (const word& key : derivedNames)
204  {
205  derivedTypes_[ngood] = knownNames.lookup(key, derivedType::UNKNOWN);
206 
207  switch (derivedTypes_[ngood])
208  {
209  case derivedType::NONE:
210  break;
211 
212  case derivedType::UNKNOWN:
213  {
214  derivedNames[nbad++] = key;
215  break;
216  }
217 
218  default:
219  {
220  ++ngood;
221  break;
222  }
223  }
224  }
225 
226  if (nbad)
227  {
229  << "Ignoring unknown derived names: "
230  << SubList<word>(derivedNames, nbad) << nl;
231  }
232 
233  derivedTypes_.resize(ngood);
234 
235  // Output the good names
237  {
238  derivedNames[i] = knownNames[derivedTypes_[i]];
239  }
240 
241  Info<< type() << " derived: "
242  << flatOutput(SubList<word>(derivedNames, ngood)) << nl;
243 
244  return true;
245 }
246 
247 
249 {
250  Log << type() << " calculating:";
251 
252  for (const derivedType category : derivedTypes_)
253  {
254  bool isNew = false;
255 
256  switch (category)
257  {
258  case derivedType::MASS_FLUX:
259  {
260  isNew = calc_rhoU(mesh_, knownNames[category], rhoRef_);
261 
262  Log << " " << knownNames[category];
263  if (isNew) Log << " (new)";
264  break;
265  }
266 
267  case derivedType::TOTAL_PRESSURE:
268  {
269  isNew = calc_pTotal(mesh_, knownNames[category], rhoRef_);
270 
271  Log << " " << knownNames[category];
272  if (isNew) Log << " (new)";
273  break;
274  }
275 
276  default:
277  break;
278  }
279  }
281  Log << nl << endl;
282 
283  return true;
284 }
285 
286 
288 {
289  for (const derivedType category : derivedTypes_)
290  {
291  switch (category)
292  {
293  case derivedType::NONE:
294  case derivedType::UNKNOWN:
295  break;
296 
297  default:
298  {
299  const auto* ioptr =
300  mesh_.cfindObject<regIOobject>(knownNames[category]);
301 
302  if (ioptr)
303  {
304  Log << type() << " " << name() << " write:" << nl
305  << " writing field " << ioptr->name() << endl;
306 
307  ioptr->write();
308  }
309  break;
310  }
311  }
312  }
313 
314  return true;
315 }
316 
317 
319 {
320  for (const derivedType category : derivedTypes_)
321  {
322  mesh_.thisDb().checkOut(knownNames[category]);
323  }
324 }
325 
326 
328 {
329  if (&mpm.mesh() == &mesh_)
330  {
331  removeDerivedFields();
332  }
333 }
334 
335 
337 {
338  if (&m == &mesh_)
339  {
340  removeDerivedFields();
341  }
342 }
343 
344 
345 // ************************************************************************* //
dictionary dict
defineTypeNameAndDebug(ObukhovLength, 0)
const Type & lookupObject(const word &name, const bool recursive=false) const
Lookup and return const reference to the object of the given Type. Fatal if not found or the wrong ty...
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:160
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
static bool calc_pTotal(const fvMesh &mesh, const word &derivedName, const scalar rhoRef)
void removeDerivedFields()
Remove (checkOut) derived fields from the object registry.
derivedFields(const word &name, const Time &runTime, const dictionary &dict)
Construct from Time and dictionary.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
virtual bool read(const dictionary &dict)
Read the data.
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
derivedType
Options for the derived/calculated field type.
bool store()
Register object with its registry and transfer ownership to the registry.
Definition: regIOobjectI.H:36
const Type * findObject(const word &name, const bool recursive=false) const
Return const pointer to the object of the given Type.
Ignore writing from objectRegistry::writeObject()
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:360
GeometricField< vector, fvPatchField, volMesh > volVectorField
Definition: volFieldsFwd.H:82
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:157
Macros for easy insertion into run-time selection tables.
virtual void movePoints(const polyMesh &m)
Update for mesh point-motion.
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
GeometricField< scalar, fvPatchField, volMesh > volScalarField
Definition: volFieldsFwd.H:81
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
A List obtained as a section of another List.
Definition: SubList.H:50
dynamicFvMesh & mesh
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
Type * getObjectPtr(const word &name, const bool recursive=false) const
Return non-const pointer to the object of the given Type, using a const-cast to have it behave like a...
List< derivedType > derivedTypes_
List of derived field (types) to create.
virtual const word & type() const =0
Runtime type information.
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
scalar rhoRef_
Reference density (to convert from kinematic to static pressure)
const dimensionSet dimPressure
static word timeName(const scalar t, const int precision=precision_)
Return a time name for the given scalar time value formatted with the given precision.
Definition: Time.C:714
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
const dimensionSet dimDensity
U
Definition: pEqn.H:72
virtual void updateMesh(const mapPolyMesh &mpm)
Update for changes of mesh.
Info<< "Reading mechanical properties\"<< endl;IOdictionary mechanicalProperties(IOobject("mechanicalProperties", runTime.constant(), mesh, IOobject::MUST_READ_IF_MODIFIED, IOobject::NO_WRITE));const dictionary &rhoDict(mechanicalProperties.subDict("rho"));word rhoType(rhoDict.get< word >"type"));autoPtr< volScalarField > rhoPtr
#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
dimensioned< scalar > dimensionedScalar
Dimensioned scalar obtained from generic dimensioned type.
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:78
const polyMesh & mesh() const
Return polyMesh.
Definition: mapPolyMesh.H:437
Nothing to be read.
#define Log
Definition: PDRblock.C:28
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:66
messageStream Info
Information stream (stdout output on master, null elsewhere)
virtual bool read(const dictionary &dict)
Read optional controls.
static bool calc_rhoU(const fvMesh &mesh, const word &derivedName, const scalar rhoRef)
Definition: derivedFields.C:58
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
volScalarField & p
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:172
virtual bool execute()
Calculate the derived fields.
Request registration (bool: true)
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
Namespace for OpenFOAM.
static const Enum< derivedType > knownNames
Names for derivedType.
EnumType lookup(const word &enumName, const EnumType deflt) const
The enumeration corresponding to the given name.
Definition: Enum.C:85
virtual bool write()
Write derived fields.
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:225
const dimensionSet dimVelocity