codeStream.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-2024 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::codeStream
26 
27 Description
28  Dictionary entry that contains C++ OpenFOAM code that is compiled to
29  generate the entry itself.
30 
31  \c \#codeStream reads three entries: \c code, \c codeInclude (optional),
32  \c codeOptions (optional) to generate the library source code stored in the
33  local \c dynamicCode directory with a subdirectory name corresponding to the
34  SHA1 of the code. The code is then compiled into a dynamically loaded
35  library libcodeStream_<SHA1>.so stored in the
36  \c dynamicCode/platforms/$WM_OPTIONS/lib directory using 'wmake libso'.
37  The resulting library is loaded in executed with arguments
38  \code
39  (const dictionary& dict, Ostream& os)
40  \endcode
41  where the dictionary is the current dictionary. The code writes results to
42  the current entry via the \c Ostream \c os.
43 
44  The verbatim string format \c \#{ ... \c \#} is used to allow multi-line
45  input without the need to escape the newlines.
46 
47  Dictionary entries constructed with \c \#codeStream can conveniently access
48  and use typed variables. This means calculations involving vectors and
49  tensors and list etc. are possible. To access a variable and construct it
50  as a given type within a \c \#codeStream entry, put the type immediately
51  after the $ symbol inside angled brackets <>. So, $<vector>var or
52  $<vector>{var} 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 \#codeStream independent
56  of the values in the dictionary and of each other. Hence the \c
57  \#codeStream code does not need to be recompiled when the values in the
58  dictionary are changed, only if the code is changed.
59 
60 Usage
61  Example to set the internal field of a field:
62  \verbatim
63  internalField #codeStream
64  {
65  code
66  #{
67  const IOdictionary& d = static_cast<const IOdictionary&>(dict);
68  const fvMesh& mesh = refCast<const fvMesh>(d.db());
69  scalarField fld(mesh.nCells(), 12.34);
70  writeEntry(os, "", fld);
71  #};
72 
73  //- Optional:
74  codeInclude
75  #{
76  #include "volFields.H"
77  #};
78 
79  //- Optional:
80  codeOptions
81  #{
82  -I$(LIB_SRC)/finiteVolume/lnInclude
83  #};
84  };
85  \endverbatim
86 
87  Example to rotate a list of points around an axis by a given angle
88  \verbatim
89  points ((3 0 0) (2 1 1) (1 2 2) (0 3 3));
90  rotation
91  {
92  axis (0 1 1);
93  angle 45;
94  }
95 
96  #codeStream
97  {
98  codeInclude
99  #{
100  #include "pointField.H"
101  #include "transform.H"
102  #};
103 
104  code
105  #{
106  const pointField points($<List<point>>points);
107  const vector axis = $<vector>!rotation/axis;
108  const scalar angle = degToRad($!rotation/angle);
109  os << "pointsRotated" << nl
110  << (Ra(axis, angle) & points)() << ";";
111  #};
112  };
113  \endverbatim
114 
115  Example Compute the centre and trianglation of a polygon
116  \verbatim
117  polygon ((0 0 0) (1 0 0) (2 1 0) (0 2 0) (-1 1 0));
118 
119  #codeStream
120  {
121  codeInclude
122  #{
123  #include "polygonTriangulate.H"
124  #};
125 
126  code
127  #{
128  const List<point> polygon($<List<point>>polygon);
129  writeEntry(os, "polygonCentre", face::centre(polygon));
130 
131  polygonTriangulate triEngine;
132  triEngine.triangulate(polygon);
133  os << "polygonTris" << ' ' << triEngine.triPoints() << ";";
134  #};
135  };
136  \endverbatim
137 
138  Example to generate a single block blockMeshDict for use with snappyHexMesh
139  with no redundant information
140  \verbatim
141  min (-2.5 -1.2 -3.0); // Minimum coordinates of the block
142  max (2.5 1.2 3.0); // Maximum coordinates of the block
143  nCellsByL 33.3333; // Number of cells per unit length
144 
145  // Calculate the number of cells in each block direction
146  nCells #calc
147  "Vector<label>($nCellsByL*($<vector>max - $<vector>min) + vector::one/2)";
148 
149  // Generate the vertices using a boundBox
150  vertices #codeStream
151  {
152  codeInclude
153  #{
154  #include "boundBox.H"
155  #};
156 
157  code
158  #{
159  os << boundBox($<vector>min, $<vector>max).points();
160  #};
161  };
162 
163  blocks
164  (
165  hex (0 1 2 3 4 5 6 7) $nCells simpleGrading (1 1 1)
166  );
167 
168  defaultPatch
169  {
170  type patch;
171  }
172 
173  boundary
174  ();
175  \endverbatim
176 
177 See also
178  Foam::functionEntries::calcEntry
179 
180 SourceFiles
181  codeStream.C
182 
183 \*---------------------------------------------------------------------------*/
184 
185 #ifndef codeStream_H
186 #define codeStream_H
187 
188 #include "functionEntry.H"
189 
190 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
191 
192 namespace Foam
193 {
194 class dlLibraryTable;
195 
196 namespace functionEntries
197 {
198 
199 // Forward declaration of friend classes
200 class calcEntry;
201 
202 /*---------------------------------------------------------------------------*\
203  Class codeStream Declaration
204 \*---------------------------------------------------------------------------*/
205 
206 class codeStream
207 :
208  public functionEntry
209 {
210  //- Interpreter function type
211  typedef void (*streamingFunctionType)(Ostream&, const dictionary&);
212 
213 
214  // Private Member Functions
215 
216  //- Helper function: parent (of parent etc.) of dictionary up to the top
217  static const dictionary& topDict(const dictionary&);
218 
219  //- Return true for master only reading of global dictionary
220  static bool masterOnlyRead(const dictionary& dict);
221 
222  //- Construct, compile, load and return streaming function
223  static streamingFunctionType getFunction
224  (
225  const dictionary& contextDict,
226  const dictionary& codeDict
227  );
228 
229  //- Compile and execute the code and return the resulting string
230  static string run
231  (
232  const dictionary& contextDict,
233  Istream& is
234  );
235 
236 
237 public:
238 
239  // Static Data Members
240 
241  //- Keywords associated with source code
242  static const wordList codeKeys;
243 
244  //- Name of the dictionary variables in the source code
245  static const wordList codeDictVars;
246 
247  //- Name of the C code template to be used
248  static const word codeTemplateC;
249 
250 
251  // Related types
252 
253  //- Declare friendship with the calcEntry class
254  friend class calcEntry;
255 
256 
257  //- Runtime type information
258  ClassName("codeStream");
259 
260 
261  // Constructors
262 
263  //- Disallow default bitwise copy construction
264  codeStream(const codeStream&) = delete;
265 
266 
267  // Member Functions
268 
269  //- Execute the functionEntry in a sub-dict context
270  static bool execute(dictionary& contextDict, Istream&);
271 
272  //- Execute the functionEntry in a primitiveEntry context
273  static bool execute
274  (
275  const dictionary& contextDict,
277  Istream&
278  );
279 
280 
281  // Member Operators
282 
283  //- Disallow default bitwise assignment
284  void operator=(const codeStream&) = delete;
285 };
286 
287 
288 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
289 
290 } // End namespace functionEntries
291 } // End namespace Foam
292 
293 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
294 
295 #endif
296 
297 // ************************************************************************* //
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
Compiles and executes code string expressions, returning the result to the dictionary entry.
Definition: calcEntry.H:191
Dictionary entry that contains C++ OpenFOAM code that is compiled to generate the entry itself.
Definition: codeStream.H:208
ClassName("codeStream")
Runtime type information.
static const wordList codeKeys
Keywords associated with source code.
Definition: codeStream.H:241
static const word codeTemplateC
Name of the C code template to be used.
Definition: codeStream.H:247
static const wordList codeDictVars
Name of the dictionary variables in the source code.
Definition: codeStream.H:244
codeStream(const codeStream &)=delete
Disallow default bitwise copy construction.
static bool execute(dictionary &contextDict, Istream &)
Execute the functionEntry in a sub-dict context.
Definition: codeStream.C:367
void operator=(const codeStream &)=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,.
A class for handling words, derived from string.
Definition: word.H:62
Namespace for OpenFOAM.