fvExprDriver.H
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) 2010-2018 Bernhard Gschaider
9 Copyright (C) 2019-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::expressions::fvExprDriver
29
30Description
31 Base driver for parsing value expressions associated with an fvMesh.
32
33 Largely based on code and ideas from swak4foam
34
35 Properties
36 \table
37 Property | Description | Required | Default
38 variables | List of variables for expressions | no | ()
39 delayedVariables | List of delayed variables | no | ()
40 storedVariables | List of stored variables | no | ()
41 globalScopes | Scopes for global variables | no | ()
42 allowShadowing | Allow variables to shadow field names | no | false
43 \endtable
44
45 Debug Properties
46 \table
47 Property | Description | Required | Default
48 debug.driver | Debug level (int) for base driver | no |
49 debug.scanner | Add debug for scanner | no | false
50 debug.parser | Add debug for parser | no | false
51 \endtable
52
53SourceFiles
54 fvExprDriverI.H
55 fvExprDriver.C
56 fvExprDriverIO.C
57 fvExprDriverNew.C
58 fvExprDriverTemplates.C
59
60\*---------------------------------------------------------------------------*/
61
62#ifndef expressions_fvExprDriver_H
63#define expressions_fvExprDriver_H
64
65#include "exprDriver.H"
66#include "exprResultDelayed.H"
67#include "exprResultStored.H"
68#include "pointMesh.H"
69#include "volFields.H"
70#include "topoSetSource.H"
72
73// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
74
75namespace Foam
76{
77namespace expressions
78{
79
80// Forward Declarations
81class fvExprDriverWriter;
82
83/*---------------------------------------------------------------------------*\
84 Class fvExprDriver Declaration
85\*---------------------------------------------------------------------------*/
86
87class fvExprDriver
88:
89 public expressions::exprDriver
90{
91 // Static Data
92
93 //- Pointer to the "default" mesh
94 static const fvMesh *defaultMeshPtr_;
95
96 //- Cache cellSets, faceSets instead of reading from disc each time
97 static bool cacheSets_;
98
99
100 // Private Data
101
102 // Stored Data
103
104 //- The scopes for global variables
105 List<word> globalScopes_;
106
107 //- The (delayed) variables table
108 HashTable<exprResultDelayed> delayedVariables_;
109
110 //- Stored expressions. Read from dictionary and updated as required
111 List<exprResultStored> storedVariables_;
112
113 //- Time index when handling special variables
114 label specialVariablesIndex_;
115
116 //- The name of the other mesh (if it is to be required)
117 word otherMeshName_;
118
119 //- Writing and restoring
120 autoPtr<fvExprDriverWriter> writer_;
121
122
123 // Private Member Functions
124
125 //- Read the IOobject and return the headerClassName
126 static word getHeaderClassName
127 (
128 const polyMesh& mesh,
129 const word& name
130 );
131
132 //- Read the set IOobject and return its headerClassName
133 static word getSetClassName
134 (
135 const polyMesh& mesh,
136 const word& name
137 );
138
139
140 //- No copy assignment
141 void operator=(const fvExprDriver&) = delete;
142
143
144protected:
145
146 // Static Member Functions
147
148 //- Determine mesh or region mesh as specified in the dictionary
149 //- with the keyword "region"
150 static const fvMesh& regionMesh
151 (
152 const dictionary& dict,
153 const fvMesh& mesh,
154 bool readIfNecessary
155 );
156
157 //- Default boundary type is calculated
158 template<class T>
159 static inline word defaultBoundaryType(const T&)
160 {
161 return "calculated";
162 }
163
164 //- Default boundary type for volume fields is zeroGradient
165 //- since they are essentially just internal fields.
166 template<class Type>
167 static inline word defaultBoundaryType
168 (
169 const VolumeField<Type>&
170 )
171 {
172 return "zeroGradient";
173 }
174
175
176 //- Apply correctBoundaryConditions (volume fields only)
177 template<class T>
178 static inline void correctField(T&) {}
179
180 template<class Type>
181 static inline void correctField
182 (
183 VolumeField<Type>& fld
184 )
185 {
186 fld.correctBoundaryConditions();
187 }
188
189
190 // Protected Member Functions
191
192 // Mesh related
193
194 //- The mesh we are attached to
195 virtual const fvMesh& mesh() const = 0;
196
197
198 // Variables
199
200 //- Define scopes for global variables
201 void setGlobalScopes(const wordUList& scopes)
202 {
203 globalScopes_ = scopes;
204 }
205
206 //- Non-const access to the named variable (sub-classes only)
207 inline virtual exprResult& variable(const word& name);
209 //- Test existence of a global variable
210 template<class T>
212 (
213 const word& name,
214 const bool wantPointData = false,
215 const label expectedSize = -1
216 ) const;
217
218 //- Return the global variable if available or a null result
219 const exprResult& lookupGlobal(const word& name) const;
220
221
222 // Fields
223
224 //- Test for the existence of a mesh field
225 template<class Type>
226 bool isField
228 const word& name,
229 const bool wantPointData = false,
230 const label expectSize = -1
231 ) const;
232
233
234 //- Retrieve field from memory or disk
235 template<class GeomField>
237 (
238 const word& name,
239 const bool mandatory = true,
240 const bool getOldTime = false
241 );
242
243 //- Retrieve point field from memory or disk
244 template<class GeomField>
246 (
247 const word& name,
248 const bool mandatory = true,
249 const bool getOldTime = false
250 );
251
252 //- Retrieve field from memory or disk (implementation)
253 template<class GeomField, class MeshRef>
255 (
256 const word& name,
257 const MeshRef& meshRef,
258 const bool mandatory = true,
259 const bool getOldTime = false
260 );
261
262 //- Helper function for getOrReadField
263 template<class GeomField, class MeshRef>
265 (
266 const word& name,
267 const MeshRef& meshRef
268 );
269
270
271 // Sets
272
273 //- The origin of the topoSet
274 enum SetOrigin { INVALID = 0, NEW, FILE, MEMORY, CACHE };
275
276 //- Get topoSet
277 template<class T>
279 (
280 const fvMesh& mesh,
281 const word& setName,
282 SetOrigin& origin
283 ) const;
284
285 //- Update topoSet
286 template<class T>
287 inline bool updateSet
288 (
289 autoPtr<T>& setPtr,
290 const word& setName,
291 SetOrigin origin
292 ) const;
293
295 // Updating
296
297 //- Examine current variable values and update stored variables
298 virtual void updateSpecialVariables(bool force=false);
299
300 //- Do we need a data file to be written
301 virtual bool hasDataToWrite() const;
302
303 //- Prepare/update special variables and add to dictionary,
304 //- normally via the reader/writer
305 virtual void prepareData(dictionary& dict) const;
306
307 //- Read data from dictionary, normally via the reader/writer
308 virtual void getData(const dictionary& dict);
309
310
311public:
312
313 // Friends
314 friend class fvExprDriverWriter;
315
316
317 // Static Member Functions
318
319 //- Get the default mesh, if one is defined
320 static const fvMesh& defaultMesh();
321
322 //- Set the default mesh (if not already set)
324 (
325 const fvMesh& mesh,
326 const bool force = false
327 );
328
329
330 //- Runtime type information
331 TypeName("fvExprDriver");
332
333
334 // Run-time selection
335
337 (
338 autoPtr,
341 (
342 const dictionary& dict,
343 const fvMesh& mesh
344 ),
345 (dict,mesh)
346 );
347
349 (
350 autoPtr,
352 idName,
353 (
354 const word& ident,
355 const fvMesh& mesh
356 ),
357 (ident, mesh)
358 );
359
360
361 // Constructors
362
363 //- Default construct, and default construct with search preferences
364 explicit fvExprDriver
365 (
369 );
370
371 //- Copy construct with dictionary reference
372 explicit fvExprDriver
373 (
374 const fvExprDriver& rhs,
375 const dictionary& dict
376 );
377
378 //- Construct from a dictionary
379 explicit fvExprDriver(const dictionary& dict);
381
382 //- Return a reference to the selected value driver
384 (
386 const fvMesh& mesh
387 );
388
389 //- Return a reference to the selected value driver
391 (
392 const dictionary& dict
393 );
394
395 //- Return a reference to the selected value driver
398 const word& type,
399 const word& id,
400 const fvMesh& mesh
401 );
402
403 //- Not generally clonable
404 virtual autoPtr<fvExprDriver> clone() = delete;
405
406
407 //- Destructor
408 virtual ~fvExprDriver();
409
410
411 // Public Member Functions
412
413 //- The natural field size for the expression
414 virtual label size() const = 0;
415
416 //- The point field size for the expression
417 virtual label pointSize() const = 0;
418
419
420 // General Controls
421
422 //- Status of cache-sets (static variable)
423 bool cacheSets() const { return cacheSets_; }
424
425
426 // Variables
427
428 //- Clear temporary variables, reset from expression strings
429 virtual void clearVariables();
430
431 //- True if named variable exists
432 inline virtual bool hasVariable(const word& name) const;
433
434 //- Return const-access to the named variable
435 inline virtual const exprResult& variable(const word& name) const;
436
437 //- Test for existence of a local/global variable or a field
438 template<class Type>
439 inline bool isVariableOrField
440 (
441 const word& name,
442 const bool wantPointData = false,
443 const label expectSize = -1
444 ) const;
445
446 //- Retrieve local/global variable as a tmp field
447 //
448 // \param name The name of the local/global field
449 // \param expectSize The size check on the variable, -1 to ignore
450 // \param mandatory A missing variable is Fatal, or return
451 // an invalid tmp
452 template<class Type>
454 (
455 const word& name,
456 const label expectSize,
457 const bool mandatory = true
458 ) const;
459
460
461 //- Lookup the field class name (memory or read from disk)
462 //
463 // Return empty if the name cannot be resolved.
464 word getFieldClassName(const word& name) const;
465
467 // Types
468
469 //- Return cell/face/point set type or unknown
471
472 //- Return cell/face/point zone type or unknown
474
475 //- Return cell/face/point zone/set type or unknown
477
478 //- Get the labels associated with the topo set
480 (
481 const word& name,
482 enum topoSetSource::sourceType setType
483 ) const;
484
485 //- Test if name is a known cellZone
486 bool isCellZone(const word& name) const;
487
488 //- Test if name is a known faceZone
489 bool isFaceZone(const word& name) const;
490
491 //- Test if name is a known pointZone
492 bool isPointZone(const word& name) const;
493
494 //- Test if name is a known cellSet
495 bool isCellSet(const word& name) const;
496
497 //- Test if name is a known faceSet
498 bool isFaceSet(const word& name) const;
499
500 //- Test if name is a known pointSet
501 bool isPointSet(const word& name) const;
503
504 // Evaluation
505
506 //- Evaluate the expression
507 //- and save as the specified named variable
508 virtual void evaluateVariable
509 (
510 const word& varName,
511 const expressions::exprString& expr
512 );
513
514 //- Evaluate an expression on a remote
515 //- and save as the specified named variable
516 //
517 // The fully qualified form of the remote is given as follows
518 // \verbatim
519 // type'name/region
520 // \endverbatim
521 //
522 // If not specified, the default type is "patch", which means the
523 // following are equivalent
524 // \verbatim
525 // patch'name/region
526 // name/region
527 // \endverbatim
528 //
529 // If region is identical to the current region, it can be omitted:
530 // \verbatim
531 // patch'name
532 // name (default is patch)
533 // \endverbatim
534 virtual void evaluateVariableRemote
535 (
536 string remote,
537 const word& varName,
538 const expressions::exprString& expr
539 );
540
541
542 // Fields
543
544 //- Test existence of a local/global variable
545 template<class Type>
546 inline bool isVariable
547 (
548 const word& name,
549 bool wantPointData = false,
550 label expectSize = -1
551 ) const;
552
553 //- Test if specified field can be found in memory or disk
554 template<class Type>
555 bool foundField(const word& name) const;
556
557 //- Read the IOobject for fieldName and return its headerClassName
558 // Empty if the field could not be found.
559 word getTypeOfField(const word& fieldName) const;
560
561
562 // Handling remote data (future)
563
564 // //- Access the other mesh name (future)
565 // const word& otherMeshName() const { return otherMeshName_; }
566 //
567 // //- Access the other mesh name (future)
568 // word& otherMeshName() { return otherMeshName_; }
569
570
571 // Reading
572
573 //- Read variables, tables etc.
574 // Also usable for objects not constructed from a dictionary.
575 virtual bool readDict(const dictionary& dict);
576
577
578 // Writing
579
580 //- Write "variables", "storedVariables", "delayedVariables",
581 //- "globalScopes" if they are present.
582 Ostream& writeCommon(Ostream& os, bool debug=false) const;
583
584 //- Create a writer for this object
585 void createWriterAndRead(const word& name);
586
587 //- Write data if appropriate
588 //- Should enable exact restarts
589 void tryWrite() const;
590
591
592 // Plugins (future)
593
600};
601
602
603// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
604
605} // End namespace expressions
606} // End namespace Foam
607
608// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
609
610#include "fvExprDriverI.H"
611
612#ifdef NoRepository
613 #include "fvExprDriverTemplates.C"
614#endif
615
616// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
617
618#endif
619
620// ************************************************************************* //
Info<< nl<< "Wrote faMesh in vtk format: "<< writer.output().name()<< nl;}{ vtk::lineWriter writer(aMesh.points(), aMesh.edges(), fileName(aMesh.mesh().time().globalPath()/"finiteArea-edges"));writer.writeGeometry();writer.beginCellData(4);writer.writeProcIDs();{ Field< scalar > fld(faMeshTools::flattenEdgeField(aMesh.magLe(), true))
Generic GeometricField class.
A HashTable similar to std::unordered_map.
Definition: HashTable.H:123
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
Base driver for parsing (field) values.
Definition: exprDriver.H:141
searchControls
Search/caching controls.
Definition: exprDriver.H:148
const dictionary & dict() const noexcept
The dictionary with all input data/specification.
Definition: exprDriver.H:385
A polymorphic field/result from evaluating an expression.
Definition: exprResult.H:127
Registered input/output for an expressions::fvExprDriver.
Base driver for parsing value expressions associated with an fvMesh.
Definition: fvExprDriver.H:139
TypeName("fvExprDriver")
Runtime type information.
static autoPtr< fvExprDriver > New(const dictionary &dict, const fvMesh &mesh)
Return a reference to the selected value driver.
bool isPointSet(const word &name) const
Test if name is a known pointSet.
Definition: fvExprDriver.C:670
bool isFaceSet(const word &name) const
Test if name is a known faceSet.
Definition: fvExprDriver.C:660
virtual exprResult & variable(const word &name)
Non-const access to the named variable (sub-classes only)
Definition: fvExprDriverI.H:59
virtual void updateSpecialVariables(bool force=false)
Examine current variable values and update stored variables.
Definition: fvExprDriver.C:240
tmp< GeomField > getOrReadPointField(const word &name, const bool mandatory=true, const bool getOldTime=false)
Retrieve point field from memory or disk.
static void correctField(VolumeField< Type > &fld)
Definition: fvExprDriver.H:231
bool foundField(const word &name) const
Test if specified field can be found in memory or disk.
void setGlobalScopes(const wordUList &scopes)
Define scopes for global variables.
Definition: fvExprDriver.H:250
virtual void prepareData(dictionary &dict) const
Definition: fvExprDriver.C:724
tmp< Field< Type > > getVariable(const word &name, const label expectSize, const bool mandatory=true) const
Retrieve local/global variable as a tmp field.
static const fvMesh & regionMesh(const dictionary &dict, const fvMesh &mesh, bool readIfNecessary)
Definition: fvExprDriver.C:509
tmp< GeomField > readAndRegister(const word &name, const MeshRef &meshRef)
Helper function for getOrReadField.
virtual bool hasDataToWrite() const
Do we need a data file to be written.
Definition: fvExprDriver.C:708
const exprResult & lookupGlobal(const word &name) const
Return the global variable if available or a null result.
Definition: fvExprDriver.C:700
static const fvMesh & defaultMesh()
Get the default mesh, if one is defined.
Definition: fvExprDriver.C:61
bool isCellSet(const word &name) const
Test if name is a known cellSet.
Definition: fvExprDriver.C:650
declareRunTimeSelectionTable(autoPtr, fvExprDriver, idName,(const word &ident, const fvMesh &mesh),(ident, mesh))
tmp< GeomField > getOrReadFieldImpl(const word &name, const MeshRef &meshRef, const bool mandatory=true, const bool getOldTime=false)
Retrieve field from memory or disk (implementation)
virtual label pointSize() const =0
The point field size for the expression.
bool isVariableOrField(const word &name, const bool wantPointData=false, const label expectSize=-1) const
Test for existence of a local/global variable or a field.
Definition: fvExprDriverI.H:90
bool isPointZone(const word &name) const
Test if name is a known pointZone.
Definition: fvExprDriver.C:692
virtual const fvMesh & mesh() const =0
The mesh we are attached to.
virtual void evaluateVariableRemote(string remote, const word &varName, const expressions::exprString &expr)
Definition: fvExprDriver.C:405
topoSetSource::sourceType topoZoneType(const word &name) const
Return cell/face/point zone type or unknown.
Definition: fvExprDriver.C:614
virtual autoPtr< fvExprDriver > clone()=delete
Not generally clonable.
static word defaultBoundaryType(const T &)
Default boundary type is calculated.
Definition: fvExprDriver.H:208
bool isField(const word &name, const bool wantPointData=false, const label expectSize=-1) const
Test for the existence of a mesh field.
virtual bool readDict(const dictionary &dict)
Read variables, tables etc.
Definition: fvExprDriver.C:153
static void correctField(T &)
Apply correctBoundaryConditions (volume fields only)
Definition: fvExprDriver.H:227
word getFieldClassName(const word &name) const
Lookup the field class name (memory or read from disk)
Definition: fvExprDriver.C:568
declareRunTimeSelectionTable(autoPtr, fvExprDriver, dictionary,(const dictionary &dict, const fvMesh &mesh),(dict, mesh))
autoPtr< T > getTopoSet(const fvMesh &mesh, const word &setName, SetOrigin &origin) const
Get topoSet.
bool isCellZone(const word &name) const
Test if name is a known cellZone.
Definition: fvExprDriver.C:680
void createWriterAndRead(const word &name)
Create a writer for this object.
bool isVariable(const word &name, bool wantPointData=false, label expectSize=-1) const
Test existence of a local/global variable.
Definition: fvExprDriverI.H:74
static word defaultBoundaryType(const VolumeField< Type > &)
Definition: fvExprDriver.H:217
virtual label size() const =0
The natural field size for the expression.
bool isFaceZone(const word &name) const
Test if name is a known faceZone.
Definition: fvExprDriver.C:686
virtual void evaluateVariable(const word &varName, const expressions::exprString &expr)
Definition: fvExprDriver.C:357
topoSetSource::sourceType topoSetType(const word &name) const
Return cell/face/point set type or unknown.
Definition: fvExprDriver.C:592
virtual ~fvExprDriver()
Destructor.
Definition: fvExprDriver.C:146
virtual void clearVariables()
Clear temporary variables, reset from expression strings.
Definition: fvExprDriver.C:331
static const fvMesh * resetDefaultMesh(const fvMesh &mesh, const bool force=false)
Set the default mesh (if not already set)
Definition: fvExprDriver.C:77
virtual bool hasVariable(const word &name) const
True if named variable exists.
Definition: fvExprDriverI.H:34
bool cacheSets() const
Status of cache-sets (static variable)
Definition: fvExprDriver.H:472
Ostream & writeCommon(Ostream &os, bool debug=false) const
bool updateSet(autoPtr< T > &setPtr, const word &setName, SetOrigin origin) const
Update topoSet.
tmp< GeomField > getOrReadField(const word &name, const bool mandatory=true, const bool getOldTime=false)
Retrieve field from memory or disk.
topoSetSource::sourceType topoSourceType(const word &name) const
Return cell/face/point zone/set type or unknown.
Definition: fvExprDriver.C:636
virtual void getData(const dictionary &dict)
Read data from dictionary, normally via the reader/writer.
Definition: fvExprDriver.C:715
refPtr< labelList > getTopoSetLabels(const word &name, enum topoSetSource::sourceType setType) const
Get the labels associated with the topo set.
word getTypeOfField(const word &fieldName) const
Read the IOobject for fieldName and return its headerClassName.
Definition: fvExprDriver.C:559
SetOrigin
The origin of the topoSet.
Definition: fvExprDriver.H:323
bool isGlobalVariable(const word &name, const bool wantPointData=false, const label expectedSize=-1) const
Test existence of a global variable.
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:91
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:81
A class for managing references or pointers (no reference counting)
Definition: refPtr.H:58
A class for managing temporary objects.
Definition: tmp.H:65
sourceType
Enumeration defining the types of sources.
Definition: topoSetSource.H:75
A class for handling words, derived from Foam::string.
Definition: word.H:68
const volScalarField & T
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:598
UList< word > wordUList
A UList of words.
Definition: wordList.H:57
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
fileName search(const word &file, const fileName &directory)
Recursively search the given directory for the file.
Definition: fileName.C:624
Macros to ease declaration of run-time selection tables.
#define declareRunTimeSelectionTable(ptrWrapper, baseType, argNames, argList, parList)
Declare a run-time selection (variables and adder classes)
#define TypeName(TypeNameString)
Declare a ClassName() with extra virtual type info.
Definition: typeInfo.H:73