fvExpressionField.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) 2021 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 Class
27  Foam::functionObjects::fvExpressionField
28 
29 Group
30  grpFieldFunctionObjects
31 
32 Description
33  Function object that generates or modifies a field based on expressions.
34 
35 Usage
36  A minimal example:
37  \verbatim
38  <name1>
39  {
40  type exprField;
41  libs (fieldFunctionObjects);
42  field pTotal;
43 
44  expression "p + 0.5*(rho*magSqr(U))";
45  dimensions [ Pa ];
46  }
47 
48  // Modify an existing field
49  <name2>
50  {
51  type exprField;
52  libs (fieldFunctionObjects);
53  field pTotal;
54  action modify;
55 
56  // Static pressure only in these regions
57  fieldMask "(mag(pos()) < 0.05) && (pos().y() > 0)";
58  expression "p";
59  }
60  \endverbatim
61 
62  where the entries mean:
63  \table
64  Property | Description | Type | Reqd | Dflt
65  type | Type name: exprField | word | yes |
66  libs | Libraries: fieldFunctionObjects | wordList | yes |
67  field | Name of input or output field | word | yes |
68  expression | Field evaluation expression | string | yes |
69  action | Type of operation: see below | word | no | new
70  autowrite | Add AUTO_WRITE to created field | bool | no | false
71  store | Store calculated field | bool | no | true
72  fieldMask | Masking as logical expression | string | no | ""
73  dimensions | Apply specified dimensions to created field | dim-spec | no |
74  readFields | Preload named fields (post-process mode) | wordList | no |
75  useNamePrefix | Add prefix scoping to output name | bool | no | false
76  \endtable
77 
78  Options for the \c action entry:
79  \plaintable
80  none | No operation
81  new | Define field based on expression (default)
82  modify | Adjust field according to expression and fieldMask
83  \endplaintable
84 
85 Note
86  The \c useNamePrefix entry is always ignored for the \c modify action.
87 
88 SourceFiles
89  fvExpressionField.C
90 
91 \*---------------------------------------------------------------------------*/
92 
93 #ifndef functionObjects_fvExpressionField_H
94 #define functionObjects_fvExpressionField_H
95 
96 #include "fvMeshFunctionObject.H"
97 #include "volumeExprDriver.H"
98 #include "Enum.H"
99 #include "dimensionSet.H"
100 
101 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
102 
103 namespace Foam
104 {
105 namespace functionObjects
106 {
107 
108 /*---------------------------------------------------------------------------*\
109  Class fvExpressionField Declaration
110 \*---------------------------------------------------------------------------*/
111 
112 class fvExpressionField
113 :
114  public fvMeshFunctionObject
115 {
116 public:
117 
118  // Public Data Types
119 
120  //- Action type enumeration
121  enum actionType : unsigned char
122  {
123  opNone = 0,
124  opNew,
125  opModify
126  };
127 
128  //- Action type names
129  static const Enum<actionType> actionNames_;
130 
131 
132 protected:
133 
134  // Private Data
135 
136  //- The context dictionary
137  dictionary dict_;
138 
139  //- Name of the field
140  word fieldName_;
141 
142  //- Names fields to preload
144 
145  //- The field-mask expression (modify mode)
146  expressions::exprString maskExpr_;
147 
148  //- Expression to evaluate
149  expressions::exprString valueExpr_;
150 
151  //- Dimensions for new field
152  dimensionSet dimensions_;
153 
154  //- Operation mode
156 
157  //- Set AUTO_WRITE for new field
158  bool autowrite_;
159 
160  //- Store calculated field
161  bool store_;
162 
163  //- True if dimensions_ should be used (creation)
164  bool hasDimensions_;
165 
166  //- Load fields from files (not from objectRegistry)
167  bool loadFromFiles_;
168 
169  autoPtr<expressions::volumeExprDriver> driver_;
170 
171 
172  // Private Member Functions
173 
174  //- Attempt load from io, store on database if successful
175  template<class FieldType>
176  bool loadAndStore(const IOobject& io);
177 
178  //- Forward to loadAndStore for supported types
179  template<class Type>
180  bool loadField(const IOobject& io);
181 
182  //- Attempt to load specified fields
183  label loadFields(const UList<word>& fieldSet_);
184 
185  template<class GeoField>
186  bool setField
187  (
188  GeoField& output,
189  const GeoField& evaluated,
190  const boolField& cond
191  );
192 
193  bool performAction(bool doWrite);
194 
195 public:
196 
197  //- Runtime type information
198  TypeName("exprField");
199 
200 
201  // Constructors
202 
203  //- Construct from Time and dictionary
205  (
206  const word& name,
207  const Time& runTime,
208  const dictionary& dict,
209  const bool loadFromFiles = false
210  );
211 
212  //- No copy construct
213  fvExpressionField(const fvExpressionField&) = delete;
214 
215  //- No copy assignment
216  void operator=(const fvExpressionField&) = delete;
217 
218 
219  //- Destructor
221 
222 
223  // Member Functions
224 
225  //- Qualified/unqualified field name (depends on action)
226  virtual word fieldName() const;
227 
228  //- Read the data
229  virtual bool read(const dictionary& dict);
230 
231  //- Execute
232  virtual bool execute();
233 
234  //- Write
235  virtual bool write();
236 };
237 
238 
239 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
240 
241 } // End namespace functionObjects
242 } // End namespace Foam
243 
244 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
245 
246 #endif
247 
248 // ************************************************************************* //
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::functionObjects::fvExpressionField::valueExpr_
expressions::exprString valueExpr_
Expression to evaluate.
Definition: fvExpressionField.H:232
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
Foam::functionObjects::fvExpressionField::read
virtual bool read(const dictionary &dict)
Read the data.
Definition: fvExpressionField.C:355
Foam::functionObjects::fvExpressionField::preloadFields_
wordList preloadFields_
Names fields to preload.
Definition: fvExpressionField.H:226
Foam::functionObjects::fvExpressionField::opNew
Create/overwrite field (default)
Definition: fvExpressionField.H:207
Foam::Enum< actionType >
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::output
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:66
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
fvMeshFunctionObject.H
Foam::functionObjects::fvExpressionField::~fvExpressionField
virtual ~fvExpressionField()
Destructor.
Definition: fvExpressionField.C:327
Foam::functionObjects::fvExpressionField::driver_
autoPtr< expressions::volumeExprDriver > driver_
Definition: fvExpressionField.H:252
Foam::functionObjects::fvExpressionField::opModify
Modify existing field.
Definition: fvExpressionField.H:208
Foam::functionObjects::fvExpressionField::loadAndStore
bool loadAndStore(const IOobject &io)
Attempt load from io, store on database if successful.
Definition: fvExpressionField.C:132
Foam::functionObjects::fvExpressionField::loadField
bool loadField(const IOobject &io)
Forward to loadAndStore for supported types.
Definition: fvExpressionField.C:149
Foam::dimensionSet
Dimension set for the base types, which can be used to implement rigorous dimension checking for alge...
Definition: dimensionSet.H:108
Foam::functionObjects::fvExpressionField::TypeName
TypeName("exprField")
Runtime type information.
Foam::functionObjects::fvMeshFunctionObject
Specialization of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
Definition: fvMeshFunctionObject.H:64
Foam::functionObjects::fvExpressionField::fieldName_
word fieldName_
Name of the field.
Definition: fvExpressionField.H:223
Foam::wordList
List< word > wordList
A List of words.
Definition: fileName.H:62
Foam::functionObjects::fvExpressionField::actionNames_
static const Enum< actionType > actionNames_
Action type names.
Definition: fvExpressionField.H:212
Foam::Field< bool >
Foam::functionObjects::fvExpressionField::dimensions_
dimensionSet dimensions_
Dimensions for new field.
Definition: fvExpressionField.H:235
Foam::functionObjects::fvExpressionField::operator=
void operator=(const fvExpressionField &)=delete
No copy assignment.
volumeExprDriver.H
Foam::functionObjects::fvExpressionField::dict_
dictionary dict_
The context dictionary.
Definition: fvExpressionField.H:220
dimensionSet.H
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:123
Foam::functionObjects::fvExpressionField::store_
bool store_
Store calculated field.
Definition: fvExpressionField.H:244
Foam::functionObjects::fvExpressionField::fieldName
virtual word fieldName() const
Qualified/unqualified field name (depends on action)
Definition: fvExpressionField.C:333
Foam::functionObjects::fvExpressionField::loadFromFiles_
bool loadFromFiles_
Load fields from files (not from objectRegistry)
Definition: fvExpressionField.H:250
Foam::functionObjects::fvExpressionField::performAction
bool performAction(bool doWrite)
Definition: fvExpressionField.C:448
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::functionObjects::fvExpressionField::setField
bool setField(GeoField &output, const GeoField &evaluated, const boolField &cond)
Definition: fvExpressionField.C:227
Foam::functionObjects::fvExpressionField::loadFields
label loadFields(const UList< word > &fieldSet_)
Attempt to load specified fields.
Definition: fvExpressionField.C:165
Foam::functionObjects::fvExpressionField::fvExpressionField
fvExpressionField(const word &name, const Time &runTime, const dictionary &dict, const bool loadFromFiles=false)
Construct from Time and dictionary.
Definition: fvExpressionField.C:301
Foam::functionObjects::fvExpressionField::action_
actionType action_
Operation mode.
Definition: fvExpressionField.H:238
Foam::boolField
Field< bool > boolField
Specialisation of Field<T> for bool.
Definition: boolField.H:51
Foam::functionObjects::fvExpressionField::autowrite_
bool autowrite_
Set AUTO_WRITE for new field.
Definition: fvExpressionField.H:241
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::functionObject::name
const word & name() const noexcept
Return the name of this functionObject.
Definition: functionObject.C:143
Foam::functionObjects::fvExpressionField::write
virtual bool write()
Write.
Definition: fvExpressionField.C:690
Foam::List< word >
Foam::expressions::exprString
Definition: exprString.H:60
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
Foam::functionObjects::fvExpressionField::execute
virtual bool execute()
Execute.
Definition: fvExpressionField.C:684
Foam::functionObjects::fvExpressionField::opNone
No operation.
Definition: fvExpressionField.H:206
Foam::functionObjects::fvExpressionField::actionType
actionType
Action type enumeration.
Definition: fvExpressionField.H:204
Foam::functionObjects::fvExpressionField::maskExpr_
expressions::exprString maskExpr_
The field-mask expression (modify mode)
Definition: fvExpressionField.H:229
Foam::functionObjects::fvExpressionField::hasDimensions_
bool hasDimensions_
True if dimensions_ should be used (creation)
Definition: fvExpressionField.H:247
Foam::functionObjects::fvExpressionField
Function object that generates or modifies a field based on expressions.
Definition: fvExpressionField.H:195
Enum.H