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-2023 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  Example usage:
72  - Print the list of available configured functionObjects:
73  \verbatim
74  foamPostProcess -list
75  \endverbatim
76 
77  - Execute the functionObjects specified in the controlDict of the
78  fluid region for all the available times:
79  \verbatim
80  foamPostProcess -region fluid
81  \endverbatim
82 
83  - Execute the functionObjects specified in the controlDict
84  for the 'fluid' solver in the 'cooling' region for the latest time only:
85  \verbatim
86  foamPostProcess -solver fluid -region cooling -latestTime
87  \endverbatim
88 
89 \*---------------------------------------------------------------------------*/
90 
91 #include "argList.H"
92 #include "timeSelector.H"
93 #include "solver.H"
94 #include "ReadFields.H"
95 #include "volFields.H"
96 #include "surfaceFields.H"
97 #include "pointFields.H"
99 
100 using namespace Foam;
101 
102 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
103 
104 #define ReadFields(GeoFieldType) \
105  readFields<GeoFieldType>(mesh, objects, requiredFields, storedObjects);
106 
107 #define ReadPointFields(GeoFieldType) \
108  readFields<GeoFieldType>(pMesh, objects, requiredFields, storedObjects);
109 
110 #define ReadUniformFields(FieldType) \
111  readUniformFields<FieldType> \
112  (constantObjects, requiredFields, storedObjects);
113 
114 void executeFunctionObjects
115 (
116  const argList& args,
117  const Time& runTime,
118  fvMesh& mesh,
119  const HashSet<word>& requiredFields0,
120  functionObjectList& functions,
121  bool lastTime
122 )
123 {
124  Info<< nl << "Reading fields:" << endl;
125 
126  // Maintain a stack of the stored objects to clear after executing
127  // the functionObjects
128  LIFOStack<regIOobject*> storedObjects;
129 
130  // Read objects in time directory
131  IOobjectList objects(mesh, runTime.name());
132 
133  HashSet<word> requiredFields(requiredFields0);
134  forAll(functions, i)
135  {
136  requiredFields.insert(functions[i].fields());
137  }
138 
139  // Read volFields
145 
146  // Read internal fields
152 
153  // Read surface fields
159 
160  // Read point fields.
161  const pointMesh& pMesh = pointMesh::New(mesh);
162 
163  ReadPointFields(pointScalarField)
164  ReadPointFields(pointVectorField);
165  ReadPointFields(pointSphericalTensorField);
166  ReadPointFields(pointSymmTensorField);
167  ReadPointFields(pointTensorField);
168 
169  // Read uniform dimensioned fields
170  IOobjectList constantObjects(mesh, runTime.constant());
171 
172  ReadUniformFields(uniformDimensionedScalarField);
173  ReadUniformFields(uniformDimensionedVectorField);
174  ReadUniformFields(uniformDimensionedSphericalTensorField);
175  ReadUniformFields(uniformDimensionedSymmTensorField);
176  ReadUniformFields(uniformDimensionedTensorField);
177 
178  Info<< nl << "Executing functionObjects" << endl;
179 
180  // Execute the functionObjects in post-processing mode
181  functions.execute();
182 
183  // Execute the functionObject 'end()' function for the last time
184  if (lastTime)
185  {
186  functions.end();
187  }
188 
189  while (!storedObjects.empty())
190  {
191  storedObjects.pop()->checkOut();
192  }
193 }
194 
195 
196 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
197 
198 int main(int argc, char *argv[])
199 {
201  (
202  "solver",
203  "name",
204  "Solver name"
205  );
206 
208  #include "addRegionOption.H"
209  #include "addFunctionObjectOptions.H"
210 
211  // Set functionObject post-processing mode
213 
214  #include "setRootCase.H"
215 
216  if (args.optionFound("list"))
217  {
218  Info<< nl
219  << "Available configured functionObjects:"
221  (
223  )
224  << endl;
225  return 0;
226  }
227 
228  #include "createTime.H"
229 
231 
233 
234  if (args.optionReadIfPresent("region", regionName))
235  {
236  Info
237  << "Create mesh " << regionName << " for time = "
238  << runTime.name() << nl << endl;
239  }
240  else
241  {
242  Info
243  << "Create mesh for time = "
244  << runTime.name() << nl << endl;
245  }
246 
247  fvMesh mesh
248  (
249  IOobject
250  (
251  regionName,
252  runTime.name(),
253  runTime,
255  )
256  );
257 
258  // Either the solver name is specified...
259  word solverName;
260 
261  // ...or the fields are specified on the command-line
262  // or later inferred from the function arguments
263  HashSet<word> requiredFields;
264 
265  if (args.optionReadIfPresent("solver", solverName))
266  {
267  libs.open("lib" + solverName + ".so");
268  }
269  else
270  {
271  // Initialise the set of selected fields from the command-line options
272  if (args.optionFound("fields"))
273  {
274  args.optionLookup("fields")() >> requiredFields;
275  }
276  if (args.optionFound("field"))
277  {
278  requiredFields.insert(args.optionLookup("field")());
279  }
280  }
281 
282  // Externally stored dictionary for functionObjectList
283  // if not constructed from runTime
284  dictionary functionsControlDict("controlDict");
285 
286  // Construct functionObjectList
287  autoPtr<functionObjectList> functionsPtr
288  (
290  (
291  args,
292  runTime,
293  functionsControlDict
294  )
295  );
296 
297  forAll(timeDirs, timei)
298  {
299  runTime.setTime(timeDirs[timei], timei);
300 
301  Info<< "Time = " << runTime.userTimeName() << endl;
302 
303  if (mesh.readUpdate() != polyMesh::UNCHANGED)
304  {
305  // Update functionObjectList if mesh changes
306  functionsPtr = functionObjectList::New
307  (
308  args,
309  runTime,
310  functionsControlDict
311  );
312  }
313 
315 
316  try
317  {
318  if (solverName != word::null)
319  {
320  // Optionally instantiate the selected solver
321  autoPtr<solver> solverPtr;
322 
323  solverPtr = solver::New(solverName, mesh);
324 
325  functionsPtr->execute();
326 
327  // Clear the objects owned by the mesh
328  mesh.objectRegistry::clear();
329  }
330  else
331  {
332  executeFunctionObjects
333  (
334  args,
335  runTime,
336  mesh,
337  requiredFields,
338  functionsPtr(),
339  timei == timeDirs.size()-1
340  );
341  }
342  }
343  catch (IOerror& err)
344  {
345  Warning<< err << endl;
346  }
347 
348  Info<< endl;
349  }
350 
351  Info<< "End\n" << endl;
352 
353  return 0;
354 }
355 
356 
357 // ************************************************************************* //
Field reading functions for post-processing utilities.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
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:111
Report an I/O error.
Definition: error.H:190
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:939
word userTimeName() const
Return current user time name with units.
Definition: Time.C:836
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
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
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:160
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 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, dictionary &controlDict)
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:101
readUpdateState readUpdate(const stitchType stitch=stitchType::geometric)
Update the mesh based on the mesh files saved in time.
Definition: fvMesh.C:808
Mesh representing a set of points created from polyMesh.
Definition: pointMesh.H:52
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:268
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
Foam::word regionName
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:230
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:251
wordList listAllConfigFiles(const fileName &configFilesPath)
Return the list of configuration files in.
Definition: dictionaryIO.C:413
messageStream Info
IOerror FatalIOError
static const char nl
Definition: Ostream.H:260
messageStream Warning
objects
Foam::argList args(argc, argv)
Foam::surfaceFields.