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 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 
44  (
45  functionObject,
46  derivedFields,
47  dictionary
48  );
49 }
50 }
51 
52 
53 const Foam::Enum
54 <
56 >
58 ({
59  { derivedType::NONE , "none" },
60  { derivedType::MASS_FLUX , "rhoU" },
61  { derivedType::TOTAL_PRESSURE , "pTotal" },
62 });
63 
64 
65 
66 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
67 
68 namespace Foam
69 {
70 
71 static bool calc_rhoU
72 (
73  const fvMesh& mesh,
74  const word& derivedName,
75  const scalar rhoRef
76 )
77 {
78  // rhoU = rho * U
79 
80  const auto* rhoPtr = mesh.findObject<volScalarField>("rho");
82 
83  volVectorField* result = mesh.getObjectPtr<volVectorField>(derivedName);
84 
85  const bool isNew = !result;
86 
87  if (!result)
88  {
89  result = new volVectorField
90  (
91  IOobject
92  (
93  derivedName,
94  mesh.time().timeName(),
95  mesh,
98  true
99  ),
100  mesh,
102  );
103 
104  result->store();
105  }
106 
107  if (rhoPtr)
108  {
109  const auto& rho = *rhoPtr;
110 
111  *result = (rho * U);
112  }
113  else
114  {
115  const dimensionedScalar rho("rho", dimDensity, rhoRef);
116 
117  *result = (rho * U);
118  }
119 
120  return isNew;
121 }
122 
123 
124 static bool calc_pTotal
125 (
126  const fvMesh& mesh,
127  const word& derivedName,
128  const scalar rhoRef
129 )
130 {
131  // pTotal = p + rho * U^2 / 2
132 
133  const auto* rhoPtr = mesh.findObject<volScalarField>("rho");
136 
137  volScalarField* result = mesh.getObjectPtr<volScalarField>(derivedName);
138 
139  const bool isNew = !result;
140 
141  if (!result)
142  {
143  result = new volScalarField
144  (
145  IOobject
146  (
147  derivedName,
148  mesh.time().timeName(),
149  mesh,
152  true
153  ),
154  mesh,
156  );
157 
158  result->store();
159  }
160 
161  if (rhoPtr)
162  {
163  const auto& rho = *rhoPtr;
164 
165  *result = (p + 0.5 * rho * magSqr(U));
166  }
167  else
168  {
169  const dimensionedScalar rho("rho", dimDensity, rhoRef);
170 
171  *result = (rho * (p + 0.5 * magSqr(U)));
172  }
173 
174  return isNew;
175 }
176 } // End namespace Foam
177 
178 
179 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
180 
182 (
183  const word& name,
184  const Time& runTime,
185  const dictionary& dict
186 )
187 :
189  derivedTypes_(),
190  rhoRef_(1.0)
191 {
192  read(dict);
193 }
194 
195 
196 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
197 
199 {
201 
202  rhoRef_ = dict.lookupOrDefault<scalar>("rhoRef", 1);
203 
204  wordList derivedNames(dict.get<wordList>("derived"));
205 
206  derivedTypes_.resize(derivedNames.size());
207 
208  label nbad = 0, ngood = 0;
209 
210  for (const word& key : derivedNames)
211  {
212  derivedTypes_[ngood] = knownNames.get(key, derivedType::UNKNOWN);
213 
214  switch (derivedTypes_[ngood])
215  {
216  case derivedType::NONE:
217  break;
218 
219  case derivedType::UNKNOWN:
220  {
221  derivedNames[nbad++] = key;
222  break;
223  }
224 
225  default:
226  {
227  ++ngood;
228  break;
229  }
230  }
231  }
232 
233  if (nbad)
234  {
236  << "Ignoring unknown derived names: "
237  << SubList<word>(derivedNames, nbad) << nl;
238  }
239 
240  derivedTypes_.resize(ngood);
241 
242  // Output the good names
244  {
245  derivedNames[i] = knownNames[derivedTypes_[i]];
246  }
247 
248  Info<< type() << " derived: "
249  << flatOutput(SubList<word>(derivedNames, ngood)) << nl;
250 
251  return true;
252 }
253 
254 
256 {
257  Log << type() << " calculating:";
258 
259  for (const derivedType category : derivedTypes_)
260  {
261  bool isNew = false;
262 
263  switch (category)
264  {
265  case derivedType::MASS_FLUX:
266  {
267  isNew = calc_rhoU(mesh_, knownNames[category], rhoRef_);
268 
269  Log << " " << knownNames[category];
270  if (isNew) Log << " (new)";
271  break;
272  }
273 
274  case derivedType::TOTAL_PRESSURE:
275  {
276  isNew = calc_pTotal(mesh_, knownNames[category], rhoRef_);
277 
278  Log << " " << knownNames[category];
279  if (isNew) Log << " (new)";
280  break;
281  }
282 
283  default:
284  break;
285  }
286  }
287 
288  Log << nl << endl;
289 
290  return true;
291 }
292 
293 
295 {
296  for (const derivedType category : derivedTypes_)
297  {
298  switch (category)
299  {
300  case derivedType::NONE:
301  case derivedType::UNKNOWN:
302  break;
303 
304  default:
305  {
306  const auto* ioptr =
307  mesh_.cfindObject<regIOobject>(knownNames[category]);
308 
309  if (ioptr)
310  {
311  Log << type() << " " << name() << " write:" << nl
312  << " writing field " << ioptr->name() << endl;
313 
314  ioptr->write();
315  }
316  break;
317  }
318  }
319  }
320 
321  return true;
322 }
323 
324 
326 {
327  for (const derivedType category : derivedTypes_)
328  {
329  mesh_.thisDb().checkOut(knownNames[category]);
330  }
331 }
332 
333 
335 {
336  if (&mpm.mesh() == &mesh_)
337  {
338  removeDerivedFields();
339  }
340 }
341 
342 
344 {
345  if (&m == &mesh_)
346  {
347  removeDerivedFields();
348  }
349 }
350 
351 
352 // ************************************************************************* //
Foam::IOobject::NO_WRITE
Definition: IOobject.H:130
volFields.H
Foam::objectRegistry::getObjectPtr
Type * getObjectPtr(const word &name, const bool recursive=false) const
Definition: objectRegistryTemplates.C:423
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::dimPressure
const dimensionSet dimPressure
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
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
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::dimVelocity
const dimensionSet dimVelocity
Foam::dimDensity
const dimensionSet dimDensity
Foam::SubList
A List obtained as a section of another List.
Definition: SubList.H:53
mapPolyMesh.H
Foam::Time::timeName
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:764
Foam::functionObjects::derivedFields::updateMesh
virtual void updateMesh(const mapPolyMesh &mpm)
Update for changes of mesh.
Definition: derivedFields.C:334
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::calc_rhoU
static bool calc_rhoU(const fvMesh &mesh, const word &derivedName, const scalar rhoRef)
Definition: derivedFields.C:72
Foam::Enum::get
EnumType get(const word &enumName) const
The enumeration corresponding to the given name.
Definition: Enum.C:86
Foam::functionObjects::fvMeshFunctionObject
Specialization of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
Definition: fvMeshFunctionObject.H:64
rho
rho
Definition: readInitialConditions.H:96
Foam::functionObjects::addToRunTimeSelectionTable
addToRunTimeSelectionTable(functionObject, add, dictionary)
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
Foam::magSqr
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
rhoPtr
Info<< "Reading mechanical properties\n"<< 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
Definition: readMechanicalProperties.H:18
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::functionObjects::derivedFields::execute
virtual bool execute()
Calculate the derived fields.
Definition: derivedFields.C:255
derivedFields.H
Foam::functionObjects::derivedFields::knownNames
static const Enum< derivedType > knownNames
Known derived field types.
Definition: derivedFields.H:129
Foam::volScalarField
GeometricField< scalar, fvPatchField, volMesh > volScalarField
Definition: volFieldsFwd.H:57
Foam::objectRegistry::lookupObject
const Type & lookupObject(const word &name, const bool recursive=false) const
Definition: objectRegistryTemplates.C:434
Foam::List::resize
void resize(const label newSize)
Adjust allocated size of list.
Definition: ListI.H:139
Foam::functionObjects::derivedFields::write
virtual bool write()
Write derived fields.
Definition: derivedFields.C:294
Foam::blockMeshTools::read
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:33
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::functionObjects::regionFunctionObject::read
virtual bool read(const dictionary &dict)
Read optional controls.
Definition: regionFunctionObject.C:166
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::dimensioned< scalar >
Foam::Ostream::write
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:84
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::functionObjects::derivedFields::read
virtual bool read(const dictionary &dict)
Read the data.
Definition: derivedFields.C:198
Foam::volVectorField
GeometricField< vector, fvPatchField, volMesh > volVectorField
Definition: volFieldsFwd.H:60
Foam::functionObjects::defineTypeNameAndDebug
defineTypeNameAndDebug(fvMeshFunctionObject, 0)
Foam::functionObjects::derivedFields::removeDerivedFields
void removeDerivedFields()
Remove (checkOut) derived fields from the object registry.
Definition: derivedFields.C:325
Time.H
U
U
Definition: pEqn.H:72
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:67
Foam::functionObjects::derivedFields::derivedFields
derivedFields(const word &name, const Time &runTime, const dictionary &dict)
Construct from Time and dictionary.
Definition: derivedFields.C:182
Foam::objectRegistry::findObject
const Type * findObject(const word &name, const bool recursive=false) const
Return const pointer to the object of the given Type.
Definition: objectRegistryTemplates.C:401
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::flatOutput
FlatOutput< Container > flatOutput(const Container &obj, label len=0)
Global flatOutput function.
Definition: FlatOutput.H:85
Foam::functionObject::type
virtual const word & type() const =0
Runtime type information.
Foam::calc_pTotal
static bool calc_pTotal(const fvMesh &mesh, const word &derivedName, const scalar rhoRef)
Definition: derivedFields.C:125
Foam::functionObjects::derivedFields::derivedType
derivedType
Derived/calculated field type.
Definition: derivedFields.H:120
Foam::List< word >
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
dictionary.H
Foam::mapPolyMesh
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:160
Foam::fvMesh::time
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:246
Foam::GeometricField< scalar, fvPatchField, volMesh >
Foam::mapPolyMesh::mesh
const polyMesh & mesh() const
Return polyMesh.
Definition: mapPolyMesh.H:362
Foam::IOobject::NO_READ
Definition: IOobject.H:123
Foam::functionObjects::derivedFields::movePoints
virtual void movePoints(const polyMesh &m)
Update for mesh point-motion.
Definition: derivedFields.C:343
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:294
Foam::functionObjects::derivedFields::derivedTypes_
List< derivedType > derivedTypes_
List of derived field (types) to create.
Definition: derivedFields.H:137
Log
#define Log
Report write to Foam::Info if the local log switch is true.
Definition: messageStream.H:332
Foam::functionObjects::derivedFields::rhoRef_
scalar rhoRef_
Reference density (to convert from kinematic to static pressure)
Definition: derivedFields.H:140