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
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
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
220  virtual ~fvExpressionField();
221 
223  // Member Functions
224 
225  //- Qualified/unqualified field name (depends on action)
226  virtual word fieldName() const;
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 };
238 
239 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
240 
241 } // End namespace functionObjects
242 } // End namespace Foam
243 
244 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
245 
246 #endif
248 // ************************************************************************* //
bool setField(GeoField &output, const GeoField &evaluated, const boolField &cond)
dictionary dict
virtual word fieldName() const
Qualified/unqualified field name (depends on action)
autoPtr< expressions::volumeExprDriver > driver_
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
bool loadField(const IOobject &io)
Forward to loadAndStore for supported types.
engineTime & runTime
label loadFields(const UList< word > &fieldSet_)
Attempt to load specified fields.
bool autowrite_
Set AUTO_WRITE for new field.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
const word & name() const noexcept
Return the name of this functionObject.
expressions::exprString maskExpr_
The field-mask expression (modify mode)
Function object that generates or modifies a field based on expressions.
Field< bool > boolField
Specialisation of Field<T> for bool.
Definition: boolField.H:47
A class for handling words, derived from Foam::string.
Definition: word.H:63
bool hasDimensions_
True if dimensions_ should be used (creation)
bool loadFromFiles_
Load fields from files (not from objectRegistry)
bool loadAndStore(const IOobject &io)
Attempt load from io, store on database if successful.
expressions::exprString valueExpr_
Expression to evaluate.
List< word > wordList
List of word.
Definition: fileName.H:59
wordList preloadFields_
Names fields to preload.
TypeName("exprField")
Runtime type information.
dictionary dict_
The context dictionary.
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:44
void operator=(const fvExpressionField &)=delete
No copy assignment.
virtual bool read(const dictionary &dict)
Read the data.
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
dimensionSet dimensions_
Dimensions for new field.
static const Enum< actionType > actionNames_
Action type names.
fvExpressionField(const word &name, const Time &runTime, const dictionary &dict, const bool loadFromFiles=false)
Construct from Time and dictionary.
Namespace for OpenFOAM.