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-2026 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  Compiles and executes C++ OpenFOAM code string expressions
29 
30  inserting the result into the dictionary or dictionary entry.
31 
32  \c \#codeStream reads three entries: \c code, \c codeInclude (optional),
33  \c codeOptions (optional) to generate the library source code stored in the
34  local \c dynamicCode directory with a subdirectory name corresponding to the
35  SHA1 of the code. The code is then compiled into a dynamically loaded
36  library libcodeStream_<SHA1>.so stored in the
37  \c dynamicCode/platforms/$WM_OPTIONS/lib directory using 'wmake libso'.
38  The resulting library is loaded in executed with arguments
39  \code
40  (const dictionary& dict, Ostream& os)
41  \endcode
42  where the dictionary is the current dictionary. The code writes results to
43  the current entry via the \c Ostream \c os.
44 
45  The verbatim string format \c \#{ ... \c \#} is used to allow multi-line
46  input without the need to escape the newlines.
47 
48  Dictionary entries constructed with \c \#codeStream can conveniently access
49  and use typed variables. This means calculations involving vectors and
50  tensors and list etc. are possible. To access a variable and construct it
51  as a given type within a \c \#codeStream entry, put the type immediately
52  after the $ symbol inside angled brackets <>. So, $<vector>var or
53  $<vector>{var} substitutes a variable named var as a vector.
54 
55  The variable values are looked-up at run-time making the code corresponding
56  to each \c \#codeStream independent of the values in the dictionary and of
57  each other. Hence the \c \#codeStream code does not need to be recompiled
58  when the values in the 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 #include "OTstream.H"
190 
191 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
192 
193 namespace Foam
194 {
195 
196 class dlLibraryTable;
197 
198 namespace functionEntries
199 {
200 
201 // Forward declaration of friend classes
202 class codeDict;
203 class streamEntry;
204 class calcEntry;
205 class codeBlockEntry;
206 class codeBlockStreamEntry;
207 
208 /*---------------------------------------------------------------------------*\
209  Class codeStream Declaration
210 \*---------------------------------------------------------------------------*/
211 
212 class codeStream
213 :
214  public functionEntry
215 {
216  //- Interpreter function type
217  typedef void (*streamingFunctionType)(Ostream&, const dictionary&);
218 
219 
220  // Private Member Functions
221 
222  //- Helper function: parent (of parent etc.) of dictionary up to the top
223  static const dictionary& topDict(const dictionary&);
224 
225  //- Return true for master only reading of global dictionary
226  static bool masterOnlyRead
227  (
228  const word& typeName,
229  const dictionary& dict
230  );
231 
232  //- Return the code string
233  static string codeString
234  (
235  const word& typeName,
236  const word& templateFunctionName,
237  const label index,
238  const dictionary& codeDict,
239  Istream&
240  );
241 
242  //- Return the code string
243  static string codeString
244  (
245  const label index,
246  const dictionary& codeDict,
247  Istream&
248  );
249 
250  //- Construct, compile, load and return streaming function
251  static void* compile
252  (
253  const word& typeName,
254  const dictionary& contextDict,
255  const dictionary& codeDict,
256  const word& codeOptions,
257  const wordList& compileFiles,
258  word& codeName
259  );
260 
261  //- Construct, compile, load and return streaming function
262  static streamingFunctionType getFunction
263  (
264  const dictionary& contextDict,
265  const dictionary& codeDict
266  );
267 
268  //- Compile and execute the code and return the result in an OTstream
269  static OTstream resultStream
270  (
271  const dictionary& contextDict,
272  Istream& is
273  );
274 
275 
276 public:
277 
278  // Static Data Members
279 
280  //- Keywords associated with source code
281  static const wordList codeKeys;
282 
283  //- Name of the dictionary variables in the source code
284  static const wordList codeDictVars;
285 
286  //- Name of the code options file to be used
287  static const word codeOptions;
288 
289  //- Name of the C code template to be used
290  static const wordList compileFiles;
291 
292 
293  // Related types
294 
295  //- Declare friendship with the calcEntry class
296  friend class codeDict;
297  friend class streamEntry;
298  friend class calcEntry;
299  friend class codeBlockEntry;
300  friend class codeBlockStreamEntry;
301 
302 
303  //- Runtime type information
304  FunctionTypeName("#codeStream");
305 
306 
307  // Constructors
308 
309  //- Construct from function type and parent dictionary
310  codeStream
311  (
312  const functionName&,
313  const label lineNumber,
314  const dictionary&
315  );
316 
317  //- Construct from line number, dictionary and Istream
318  codeStream
319  (
320  const label lineNumber,
321  const dictionary& parentDict,
322  Istream& is
323  );
324 
325  //- Copy construct
326  codeStream(const codeStream&) = default;
327 
328  //- Clone
329  virtual autoPtr<entry> clone(const dictionary&) const
330  {
331  return autoPtr<entry>(new codeStream(*this));
332  }
333 
334 
335  // Member Functions
336 
337  //- Expand the functionEntry into the contextDict
338  virtual bool execute(dictionary& contextDict, Istream&);
339 
340  //- Expand the functionEntry into the contextEntry
341  static bool execute
342  (
343  const dictionary& contextDict,
344  primitiveEntry& contextEntry,
345  Istream&
346  );
347 
348 
349  // Member Operators
350 
351  //- Disallow default bitwise assignment
352  void operator=(const codeStream&) = delete;
353 };
354 
355 
356 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
357 
358 } // End namespace functionEntries
359 } // End namespace Foam
360 
361 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
362 
363 #endif
364 
365 // ************************************************************************* //
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:450
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
Output token stream.
Definition: OTstream.H:56
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
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
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: codeStream.H:214
static const word codeOptions
Name of the code options file to be used.
Definition: codeStream.H:286
static const wordList codeKeys
Keywords associated with source code.
Definition: codeStream.H:280
codeStream(const functionName &, const label lineNumber, const dictionary &)
Construct from function type and parent dictionary.
Definition: codeStream.C:302
static const wordList compileFiles
Name of the C code template to be used.
Definition: codeStream.H:289
static const wordList codeDictVars
Name of the dictionary variables in the source code.
Definition: codeStream.H:283
virtual bool execute(dictionary &contextDict, Istream &)
Expand the functionEntry into the contextDict.
Definition: codeStream.C:326
void operator=(const codeStream &)=delete
Disallow default bitwise assignment.
FunctionTypeName("#codeStream")
Runtime type information.
Compiles and executes C++ OpenFOAM code string expressions.
Definition: streamEntry.H:112
A functionEntry causes entries to be added/manipulated on the specified dictionary given an input str...
Definition: functionEntry.H:66
A functionName is a word starting with '#'.
Definition: functionName.H:60
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,.
Template function which returns the un-mangled name of a given type. Useful for types which do not ha...
A class for handling words, derived from string.
Definition: word.H:63
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