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-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 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  multi-line input without the need to escape the newlines but in this case
45  the result must be written to the \c Ostream \c os explicitly.
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 \#calcInclude 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  #calcInclude "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  If the code string is delimited by \#{ ... \c \#} multiple lines
109  and multiple code statements can be used to generate the entry using
110  'os << ...;'. This is equivalent to \#codeStream but with a more
111  compact syntax.
112 
113  \verbatim
114  #calcInclude "transform.H"
115  maxAngle 30;
116  nAngles 7;
117  Us #calc
118  #{
119  const vector U($<vector>testCalc2!U);
120  const int nAngles = $nAngles;
121  const scalar angleStep = ($<scalar>maxAngle)/(nAngles - 1);
122  List<vector> Us(nAngles);
123  for(int i=0; i<nAngles; i++)
124  {
125  const scalar angle = degToRad(i*angleStep);
126  Us[i] = transform(Ry(angle), U);
127  }
128  os << Us;
129  #};
130  \endverbatim
131 
132  Example to generate a single block blockMeshDict for use with snappyHexMesh
133  with no redundant information
134  \verbatim
135  min (-2.5 -1.2 -3.0); // Minimum coordinates of the block
136  max (2.5 1.2 3.0); // Maximum coordinates of the block
137  nCellsByL 33.3333; // Number of cells per unit length
138 
139  // Calculate the number of cells in each block direction
140  nCells #calc
141  "Vector<label>($nCellsByL*($<vector>max - $<vector>min) + vector::one/2)";
142 
143  // Generate the vertices using a boundBox
144  #calcInclude "boundBox.H"
145  vertices #calc "boundBox($<vector>min, $<vector>max).points()";
146 
147  blocks
148  (
149  hex #calc "identityMap(8)" $nCells simpleGrading (1 1 1)
150  );
151 
152  defaultPatch
153  {
154  type patch;
155  }
156 
157  boundary
158  ();
159  \endverbatim
160 
161 See also
162  Foam::functionEntries::calcIncludeEntry
163  Foam::functionEntries::codeStream
164 
165 SourceFiles
166  calcEntry.C
167 
168 \*---------------------------------------------------------------------------*/
169 
170 #ifndef calcEntry_H
171 #define calcEntry_H
172 
173 #include "functionEntry.H"
174 
175 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
176 
177 namespace Foam
178 {
179 
180 class dlLibraryTable;
181 
182 namespace functionEntries
183 {
184 
185 /*---------------------------------------------------------------------------*\
186  Class calcEntry Declaration
187 \*---------------------------------------------------------------------------*/
188 
189 class calcEntry
190 :
191  public functionEntry
192 {
193  // Private Member Functions
194 
195  //- Perform the calculation and return the resulting string
196  static string calc
197  (
198  const dictionary& dict,
199  Istream& is
200  );
201 
202 
203 public:
204 
205  //- Runtime type information
206  ClassName("calc");
207 
208 
209  // Constructors
210 
211  //- Disallow default bitwise copy construction
212  calcEntry(const calcEntry&) = delete;
213 
214 
215  // Member Functions
216 
217  //- Execute the functionEntry in a sub-dict context
218  static bool execute(dictionary& dict, Istream&);
219 
220  //- Execute the functionEntry in a primitiveEntry context
221  static bool execute
222  (
223  const dictionary& dict,
225  Istream&
226  );
227 
228 
229  // Member Operators
230 
231  //- Disallow default bitwise assignment
232  void operator=(const calcEntry&) = delete;
233 };
234 
235 
236 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
237 
238 } // End namespace functionEntries
239 } // End namespace Foam
240 
241 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
242 
243 #endif
244 
245 // ************************************************************************* //
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
Compiles and executes code string expressions, returning the result to the dictionary entry.
Definition: calcEntry.H:191
ClassName("calc")
Runtime type information.
static bool execute(dictionary &dict, Istream &)
Execute the functionEntry in a sub-dict context.
Definition: calcEntry.C:127
calcEntry(const calcEntry &)=delete
Disallow default bitwise copy construction.
void operator=(const calcEntry &)=delete
Disallow default bitwise assignment.
A functionEntry causes entries to be added/manipulated on the specified dictionary given an input str...
Definition: functionEntry.H:66
A keyword and a list of tokens is a 'primitiveEntry'. An primitiveEntry can be read,...
const dictionary & dict() const
This entry is not a dictionary,.
Namespace for OpenFOAM.