calcEntry.H
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) 2011-2025 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 Class
25  Foam::functionEntries::calcEntry
26 
27 Description
28  Compiles and executes code string expressions,
29  returning the result to the dictionary entry.
30 
31  \c \#calc reads the following code string to generate the library source
32  code stored in the local \c dynamicCode directory with a subdirectory name
33  corresponding to the SHA1 of the code. The code is then compiled into a
34  dynamically loaded library libcodeStream_<SHA1>.so stored in the \c
35  dynamicCode/platforms/$WM_OPTIONS/lib directory using 'wmake libso'. The
36  resulting library is loaded in executed with arguments
37  \code
38  (const dictionary& dict, Ostream& os)
39  \endcode
40  where the dictionary is the current dictionary. The code writes results to
41  the current entry via the \c Ostream \c os.
42 
43  The verbatim string format \c \#{ ... \c \#} can optionally be used to allow
44  string/word comparison without the need to escape the quotes and multi-line
45  input without the need to escape the newlines.
46 
47  Dictionary entries constructed \c \#calc can conveniently access and
48  use typed variables. This means calculations involving vectors and tensors
49  and list etc. are possible. To access a variable and construct it as a
50  given type within a \c \#calc entry, put the type immediately after the $
51  symbol inside angled brackets <>. So, $<vector>var or $<vector>{var}
52  substitutes a variable named var as a vector.
53 
54  The variable values are no longer embedded into the code but looked-up at
55  run-time making the code corresponding to each \c \#calc independent of the
56  values in the dictionary and of each other. Hence the \c \#calc code does
57  not need to be recompiled when the values in the dictionary are changed,
58  only if the code is changed.
59 
60 Usage
61  Simple variable multiplication:
62  \verbatim
63  a 1.1;
64  b 3.2;
65  c #calc "$a*$b";
66  \endverbatim
67 
68  Special care is required for calc entries that include a division since
69  "/" is also used as the scoping operator to identify keywords in
70  sub-dictionaries. For example, "$a/b" expects a keyword "b" within a
71  sub-dictionary named "a". A division can be correctly executed by using a
72  space between a variables and "/", e.g.
73  \verbatim
74  c #calc "$a / $b";
75  \endverbatim
76 
77  or bracketing the variable, e.g.
78  \verbatim
79  c #calc "($a)/$b";
80  \endverbatim
81 
82  Example using the string type:
83  \verbatim
84  s "field";
85  fieldName #calc "$<string>s + \"Name\" ";
86  \endverbatim
87 
88  Additional include files for the \c \#calc code compilation can be specified
89  using the \c \#codeInclude entry, e.g. if functions from transform.H are
90  used:
91  \verbatim
92  angleOfAttack 5; // degs
93 
94  angle #calc "-degToRad($angleOfAttack)";
95 
96  #codeInclude "transform.H"
97  liftDir #calc "transform(Ry($angle), vector(0, 0, 1))";
98  dragDir #calc "transform(Ry($angle), vector(1, 0, 0))";
99  \endverbatim
100 
101  Calculate the magnitude of the velocity and turbulent kinetic energy
102  where the velocity is looked-up from testCalc2:
103  \verbatim
104  magU #calc "mag($<vector>testCalc2!U)";
105  k #calc "1.5*magSqr(0.05*$<vector>{${FOAM_CASE}/testCalc2!U})";
106  \endverbatim
107 
108  Example to generate a single block blockMeshDict for use with snappyHexMesh
109  with no redundant information:
110  \verbatim
111  min (-2.5 -1.2 -3.0); // Minimum coordinates of the block
112  max (2.5 1.2 3.0); // Maximum coordinates of the block
113  nCellsByL 33.3333; // Number of cells per unit length
114 
115  // Calculate the number of cells in each block direction
116  nCells #calc
117  "Vector<label>($nCellsByL*($<vector>max - $<vector>min) + vector::one/2)";
118 
119  // Generate the vertices using a boundBox
120  #codeInclude "boundBox.H"
121  vertices #calc "boundBox($<vector>min, $<vector>max).points()";
122 
123  blocks
124  (
125  hex #calc "identityMap(8)" $nCells simpleGrading (1 1 1)
126  );
127 
128  defaultPatch
129  {
130  type patch;
131  }
132 
133  boundary
134  ();
135  \endverbatim
136 
137  Example of conditional with \c #calc: showing the benefit of using verbatim
138  string:
139  \verbatim
140  #if (#calc "$fluid == \"waterAndSludge\" || $fluid == \"waterAndSand\"")
141  mixture multicomponentMixture;
142  #else
143  mixture pureMixture;
144  #endif
145  \endverbatim
146  same again but showing the benefit of using verbatim string:
147  \verbatim
148  #if (#calc #{$fluid == "waterAndSludge" || $fluid == "waterAndSand"#})
149  mixture multicomponentMixture;
150  #else
151  mixture pureMixture;
152  #endif
153  \endverbatim
154 
155 See also
156  Foam::functionEntries::codeIncludeEntry
157  Foam::functionEntries::streamEntry
158  Foam::functionEntries::codeStream
159 
160 SourceFiles
161  calcEntry.C
162 
163 \*---------------------------------------------------------------------------*/
164 
165 #ifndef calcEntry_H
166 #define calcEntry_H
167 
168 #include "streamEntry.H"
169 
170 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
171 
172 namespace Foam
173 {
174 namespace functionEntries
175 {
176 
177 /*---------------------------------------------------------------------------*\
178  Class calcEntry Declaration
179 \*---------------------------------------------------------------------------*/
180 
181 class calcEntry
182 :
183  public streamEntry
184 {
185  // Private Member Functions
186 
187  //- Return the code string
188  static string codeString
189  (
190  const label index,
191  const dictionary& codeDict,
192  Istream&
193  );
194 
195 
196 public:
197 
198  // Related types
199 
200  //- Declare friendship with the calcEntry class
201  friend class codeBlockEntry;
202 
203 
204  //- Runtime type information
205  FunctionTypeName("#calc");
206 
207 
208  // Constructors
209 
210  //- Copy construct
211  calcEntry(const calcEntry&) = default;
212 
213  //- Clone
214  virtual autoPtr<entry> clone(const dictionary&) const
215  {
216  return autoPtr<entry>(new calcEntry(*this));
217  }
218 
219 
220  // Member Functions
221 
222  //- Expand the functionEntry into the contextDict
223  virtual bool execute(dictionary& contextDict, Istream&)
224  {
226  return false;
227  }
228 
229  //- Expand the functionEntry into the contextEntry
230  static bool execute
231  (
232  const dictionary& contextDict,
233  primitiveEntry& contextEntry,
234  Istream&
235  );
236 
237 
238  // Member Operators
239 
240  //- Disallow default bitwise assignment
241  void operator=(const calcEntry&) = delete;
242 };
243 
244 
245 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
246 
247 } // End namespace functionEntries
248 } // End namespace Foam
249 
250 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
251 
252 #endif
253 
254 // ************************************************************************* //
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
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 keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
virtual autoPtr< entry > clone() const
Construct on freestore as copy.
Definition: entry.C:56
Compiles and executes code string expressions, returning the result to the dictionary entry.
Definition: calcEntry.H:183
virtual bool execute(dictionary &contextDict, Istream &)
Expand the functionEntry into the contextDict.
Definition: calcEntry.H:222
calcEntry(const calcEntry &)=default
Copy construct.
FunctionTypeName("#calc")
Runtime type information.
void operator=(const calcEntry &)=delete
Disallow default bitwise assignment.
Part of the #codeBlock...#codeBlock clause.
Compiles and executes C++ OpenFOAM code string expressions.
Definition: codeDict.H:131
Compiles and executes C++ OpenFOAM code string expressions.
Definition: streamEntry.H:112
A keyword and a list of tokens is a 'primitiveEntry'. An primitiveEntry can be read,...
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:381
Namespace for OpenFOAM.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59