foamPostProcess.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2022-2024 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Application
25  foamPostProcess
26 
27 Description
28  Execute the set of functionObjects specified in the selected dictionary
29  (which defaults to system/controlDict) or on the command-line for the
30  selected set of times on the selected set of fields.
31 
32  The functionObjects are either executed directly or for the solver
33  optionally specified as a command-line argument.
34 
35 Usage
36  \b foamPostProcess [OPTION]
37  - \par -dict <file>
38  Read control dictionary from specified location
39 
40  - \par -solver <name>
41  Solver name
42 
43  - \par -libs '(\"lib1.so\" ... \"libN.so\")'
44  Specify the additional libraries loaded
45 
46  -\par -region <name>
47  Specify the region
48 
49  - \par -func <name>
50  Specify the name of the functionObject to execute, e.g. Q
51 
52  - \par -funcs <list>
53  Specify the names of the functionObjects to execute, e.g. '(Q div(U))'
54 
55  - \par -field <name>
56  Specify the name of the field to be processed, e.g. U
57 
58  - \par -fields <list>
59  Specify a list of fields to be processed,
60  e.g. '(U T p)' - regular expressions not currently supported
61 
62  - \par -time <ranges>
63  comma-separated time ranges - eg, ':10,20,40:70,1000:'
64 
65  - \par -latestTime
66  Select the latest time
67 
68  - \par -list
69  List the available configured functionObjects
70 
71  - \par -list
72  List the available functionObject templates
73 
74  Example usage:
75  - Print the list of available configured functionObjects:
76  \verbatim
77  foamPostProcess -list
78  \endverbatim
79 
80  - Execute the functionObjects specified in the controlDict of the
81  fluid region for all the available times:
82  \verbatim
83  foamPostProcess -region fluid
84  \endverbatim
85 
86  - Execute the functionObjects specified in the controlDict
87  for the 'fluid' solver in the 'cooling' region for the latest time only:
88  \verbatim
89  foamPostProcess -solver fluid -region cooling -latestTime
90  \endverbatim
91 
92 \*---------------------------------------------------------------------------*/
93 
94 #include "argList.H"
95 #include "timeSelector.H"
96 #include "solver.H"
97 #include "ReadFields.H"
98 #include "volFields.H"
99 #include "surfaceFields.H"
100 #include "pointFields.H"
102 
103 using namespace Foam;
104 
105 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
106 
107 #define ReadFields(GeoFieldType) \
108  readFields<GeoFieldType>(mesh, objects, requiredFields, storedObjects);
109 
110 #define ReadPointFields(GeoFieldType) \
111  readFields<GeoFieldType>(pMesh, objects, requiredFields, storedObjects);
112 
113 #define ReadUniformFields(FieldType) \
114  readUniformFields<FieldType> \
115  (constantObjects, requiredFields, storedObjects);
116 
117 void executeFunctionObjects
118 (
119  const argList& args,
120  const Time& runTime,
121  fvMesh& mesh,
122  const HashSet<word>& requiredFields0,
123  functionObjectList& functions,
124  bool lastTime
125 )
126 {
127  Info<< nl << "Reading fields:" << endl;
128 
129  // Maintain a stack of the stored objects to clear after executing
130  // the functionObjects
131  LIFOStack<regIOobject*> storedObjects;
132 
133  // Read objects in time directory
134  IOobjectList objects(mesh, runTime.name());
135 
136  HashSet<word> requiredFields(requiredFields0);
137  forAll(functions, i)
138  {
139  requiredFields.insert(functions[i].fields());
140  }
141 
142  // Read volFields
148 
149  // Read internal fields
155 
156  // Read surface fields
162 
163  // Read point fields.
164  const pointMesh& pMesh = pointMesh::New(mesh);
165 
166  ReadPointFields(pointScalarField)
167  ReadPointFields(pointVectorField);
168  ReadPointFields(pointSphericalTensorField);
169  ReadPointFields(pointSymmTensorField);
170  ReadPointFields(pointTensorField);
171 
172  // Read uniform dimensioned fields
173  IOobjectList constantObjects(mesh, runTime.constant());
174 
175  ReadUniformFields(uniformDimensionedScalarField);
176  ReadUniformFields(uniformDimensionedVectorField);
177  ReadUniformFields(uniformDimensionedSphericalTensorField);
178  ReadUniformFields(uniformDimensionedSymmTensorField);
179  ReadUniformFields(uniformDimensionedTensorField);
180 
181  Info<< nl << "Executing functionObjects" << endl;
182 
183  // Execute the functionObjects in post-processing mode
184  functions.execute();
185 
186  // Execute the functionObject 'end()' function for the last time
187  if (lastTime)
188  {
189  functions.end();
190  }
191 
192  while (!storedObjects.empty())
193  {
194  storedObjects.pop()->checkOut();
195  }
196 }
197 
198 
199 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
200 
201 int main(int argc, char *argv[])
202 {
204  (
205  "solver",
206  "name",
207  "Solver name"
208  );
209 
211  #include "addRegionOption.H"
212  #include "addFunctionObjectOptions.H"
213 
215  (
216  "listTemplates",
217  "List the available functionObjects templates"
218  );
219 
220  // Set functionObject post-processing mode
222 
223  #include "setRootCase.H"
224 
225  bool printedList = false;
226 
227  if (args.optionFound("list"))
228  {
229  Info<< nl
230  << "Available configured functionObjects:"
232  (
234  )
235  << endl;
236 
237  printedList = true;
238  }
239 
240  if (args.optionFound("listTemplates"))
241  {
242  Info<< nl
243  << "Available functionObject templates:"
245  (
247  )
248  << endl;
249 
250  printedList = true;
251  }
252 
253  if (printedList)
254  {
255  return 0;
256  }
257 
258  #include "createTime.H"
259 
261 
262  // Either the solver name is specified...
263  word solverName;
264 
265  // ...or the fields are specified on the command-line
266  // or later inferred from the function arguments
267  HashSet<word> requiredFields;
268 
269  if (args.optionReadIfPresent("solver", solverName))
270  {
271  libs.open("lib" + solverName + ".so");
272  }
273  else
274  {
275  // Initialise the set of selected fields from the command-line options
276  if (args.optionFound("fields"))
277  {
278  args.optionLookup("fields")() >> requiredFields;
279  }
280  if (args.optionFound("field"))
281  {
282  requiredFields.insert(word(args.optionLookup("field")()));
283  }
284  }
285 
287 
288  if (args.optionReadIfPresent("region", regionName))
289  {
290  Info
291  << "Create mesh " << regionName << " for time = "
292  << runTime.name() << nl << endl;
293  }
294  else
295  {
296  Info
297  << "Create mesh for time = "
298  << runTime.name() << nl << endl;
299  }
300 
301  fvMesh mesh
302  (
303  IOobject
304  (
305  regionName,
306  runTime.name(),
307  runTime,
309  )
310  );
311 
312  // Construct functionObjectList
313  autoPtr<functionObjectList> functionsPtr
314  (
315  functionObjectList::New(args, runTime)
316  );
317 
318  forAll(timeDirs, timei)
319  {
320  runTime.setTime(timeDirs[timei], timei);
321 
322  Info<< "Time = " << runTime.userTimeName() << endl;
323 
324  if (mesh.readUpdate() != fvMesh::UNCHANGED)
325  {
326  // Update functionObjectList if mesh changes
327  functionsPtr = functionObjectList::New(args, runTime);
328  }
329 
331 
332  try
333  {
334  if (solverName != word::null)
335  {
336  // Optionally instantiate the selected solver
337  autoPtr<solver> solverPtr;
338 
339  solverPtr = solver::New(solverName, mesh);
340 
341  functionsPtr->execute();
342 
343  // Clear the objects owned by the mesh
344  mesh.objectRegistry::clear();
345  }
346  else
347  {
348  executeFunctionObjects
349  (
350  args,
351  runTime,
352  mesh,
353  requiredFields,
354  functionsPtr(),
355  timei == timeDirs.size()-1
356  );
357  }
358  }
359  catch (IOerror& err)
360  {
361  Warning<< err << endl;
362  }
363 
364  Info<< endl;
365  }
366 
367  Info<< "End\n" << endl;
368 
369  return 0;
370 }
371 
372 
373 // ************************************************************************* //
Field reading functions for post-processing utilities.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
static pointMesh & New(const word &name, const polyMesh &mesh)
Construct and return the named DemandDrivenMeshObject.
Field with dimensions and associated with geometry type GeoMesh which is used to size the field and a...
Generic GeometricField class.
A HashTable with keys but without contents.
Definition: HashSet.H:62
bool insert(const Key &key)
Insert a new entry.
Definition: HashSet.H:109
List of IOobjects with searching and retrieving facilities.
Definition: IOobjectList.H:53
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
A LIFO stack based on a singly-linked list.
Definition: LIFOStack.H:54
T pop()
Pop the top element off the stack.
Definition: LIFOStack.H:90
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
static const word & constant()
Return constant name.
Definition: TimePaths.H:122
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
virtual void setTime(const Time &)
Reset the time and time-index to those of the given time.
Definition: Time.C:985
word userTimeName() const
Return current user time name with units.
Definition: Time.C:856
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:103
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:128
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:118
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:114
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:204
IStringStream optionLookup(const word &opt) const
Return an IStringStream from the named option.
Definition: argListI.H:120
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
const word & name() const
Return const reference to name.
bool open(const fileName &libName, const bool verbose=true)
Open the named library, optionally with warnings if problems occur.
void throwExceptions()
Definition: error.H:115
static fileName functionObjectTemplatePath
Default relative path to the directory structure.
static fileName functionObjectDictPath
Default relative path to the directory structure.
List of function objects with start(), execute() and end() functions that is called for each object.
static autoPtr< functionObjectList > New(const argList &args, const Time &runTime)
Construct and return a functionObjectList for an application.
bool execute()
Called at each ++ or += of the time-loop.
bool end()
Called when Time::run() determines that the time-loop exits.
static bool postProcess
Global post-processing mode switch.
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:99
readUpdateState readUpdate(const stitchType stitch=stitchType::geometric)
Update the mesh based on the mesh files saved in time.
Definition: fvMesh.C:761
Mesh representing a set of points created from polyMesh.
Definition: pointMesh.H:53
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:267
static autoPtr< solver > New(const word &solverName, fvMesh &mesh)
Select, construct and return the solver.
Definition: solverNew.C:37
static void addOptions(const bool constant=true, const bool withZero=false)
Add the options handled by timeSelector to argList::validOptions.
Definition: timeSelector.C:114
static instantList select0(Time &runTime, const argList &args)
Return the set of times selected based on the argList options.
Definition: timeSelector.C:252
A class for handling words, derived from string.
Definition: word.H:62
static const word null
An empty word.
Definition: word.H:77
int main(int argc, char *argv[])
Definition: financialFoam.C:44
static instantList timeDirs
Definition: globalFoam.H:44
Info<< "Calculating turbulent flame speed field St\n"<< endl;volScalarField St(IOobject("St", runTime.name(), mesh, IOobject::NO_READ, IOobject::AUTO_WRITE), flameWrinkling->Xi() *Su);multivariateSurfaceInterpolationScheme< scalar >::fieldTable fields
Definition: createFields.H:228
Namespace for OpenFOAM.
dlLibraryTable libs
Table of loaded dynamic libraries.
wordList ReadFields(const Mesh &mesh, const IOobjectList &objects, PtrList< GeoField > &fields, const bool syncPar=true)
Read all fields of the specified type.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
const word & regionName(const solver &region)
Definition: solver.H:209
wordList listAllConfigFiles(const fileName &configFilesPath)
Return the list of configuration files in.
Definition: dictionaryIO.C:415
messageStream Info
IOerror FatalIOError
static const char nl
Definition: Ostream.H:266
messageStream Warning
objects
Foam::argList args(argc, argv)
Foam::surfaceFields.