functionEntries::calcEntry, codeStream: Added documentation for typed variable lookup
This commit is contained in:
@ -26,9 +26,39 @@ Class
|
|||||||
|
|
||||||
Description
|
Description
|
||||||
Compiles and executes code string expressions,
|
Compiles and executes code string expressions,
|
||||||
returning the result to the dictionary
|
returning the result to the dictionary entry.
|
||||||
|
|
||||||
|
\c \#calc reads the following code string to generate the library source
|
||||||
|
code stored in the local \c dynamicCode directory with a subdirectory name
|
||||||
|
corresponding to the SHA1 of the code. The code is then compiled into a
|
||||||
|
dynamically loaded library libcodeStream_<SHA1>.so stored in the \c
|
||||||
|
dynamicCode/platforms/$WM_OPTIONS/lib directory using 'wmake libso'. The
|
||||||
|
resulting library is loaded in executed with arguments
|
||||||
|
\code
|
||||||
|
(const dictionary& dict, Ostream& os)
|
||||||
|
\endcode
|
||||||
|
where the dictionary is the current dictionary. The code writes results to
|
||||||
|
the current entry via the \c Ostream \c os.
|
||||||
|
|
||||||
|
The verbatim string format \c \#{ ... \c \#} can optionally be used to allow
|
||||||
|
multi-line input without the need to escape the newlines but in this case
|
||||||
|
the result must be written to the \c Ostream \c os explicitly.
|
||||||
|
|
||||||
|
Dictionary entries constructed \c \#calc can conveniently access and
|
||||||
|
use typed variables. This means calculations involving vectors and tensors
|
||||||
|
and list etc. are possible. To access a variable and construct it as a
|
||||||
|
given type within a \c \#calc entry, put the type immediately after the $
|
||||||
|
symbol inside angled brackets <>. So, $<vector>var or $<vector>{var}
|
||||||
|
substitutes a variable named var as a vector.
|
||||||
|
|
||||||
|
The variable values are no longer embedded into the code but looked-up at
|
||||||
|
run-time making the code corresponding to each \c \#calc independent of the
|
||||||
|
values in the dictionary and of each other. Hence the \c \#calc code does
|
||||||
|
not need to be recompiled when the values in the dictionary are changed,
|
||||||
|
only if the code is changed.
|
||||||
|
|
||||||
Usage
|
Usage
|
||||||
|
Simple variable multiplication
|
||||||
\verbatim
|
\verbatim
|
||||||
a 1.1;
|
a 1.1;
|
||||||
b 3.2;
|
b 3.2;
|
||||||
@ -40,24 +70,24 @@ Usage
|
|||||||
sub-dictionaries. For example, "$a/b" expects a keyword "b" within a
|
sub-dictionaries. For example, "$a/b" expects a keyword "b" within a
|
||||||
sub-dictionary named "a". A division can be correctly executed by using a
|
sub-dictionary named "a". A division can be correctly executed by using a
|
||||||
space between a variables and "/", e.g.
|
space between a variables and "/", e.g.
|
||||||
|
|
||||||
\verbatim
|
\verbatim
|
||||||
c #calc "$a / $b";
|
c #calc "$a / $b";
|
||||||
\endverbatim
|
\endverbatim
|
||||||
|
|
||||||
or bracketing the variable, e.g.
|
or bracketing the variable, e.g.
|
||||||
|
|
||||||
\verbatim
|
\verbatim
|
||||||
c #calc "($a)/$b";
|
c #calc "($a)/$b";
|
||||||
\endverbatim
|
\endverbatim
|
||||||
|
|
||||||
|
// Example using the string type
|
||||||
\verbatim
|
\verbatim
|
||||||
s "field";
|
s "field";
|
||||||
fieldName #calc "$<string>s + \"Name\" ";
|
fieldName #calc "$<string>s + \"Name\" ";
|
||||||
\endverbatim
|
\endverbatim
|
||||||
|
|
||||||
Additional include files for the #calc code compilation can be specified
|
Additional include files for the \c \#calc code compilation can be specified
|
||||||
using the #calcInclude entry, e.g. if functions from transform.H are used
|
using the \c \#calcInclude entry, e.g. if functions from transform.H are
|
||||||
|
used
|
||||||
\verbatim
|
\verbatim
|
||||||
angleOfAttack 5; // degs
|
angleOfAttack 5; // degs
|
||||||
|
|
||||||
@ -68,9 +98,18 @@ Usage
|
|||||||
dragDir #calc "transform(Ry($angle), vector(1, 0, 0))";
|
dragDir #calc "transform(Ry($angle), vector(1, 0, 0))";
|
||||||
\endverbatim
|
\endverbatim
|
||||||
|
|
||||||
If the code string is delimited by '#{...#}' multiple lines and multiple
|
Calculate the magnitude of the velocity and turbulent kinetic energy
|
||||||
code statements can be used to generate the entry using 'os << ...;'.
|
where the velocity is looked-up from testCalc2
|
||||||
This is equivalent to #codeStream but with a more compact syntax.
|
\verbatim
|
||||||
|
magU #calc "mag($<vector>testCalc2!U)";
|
||||||
|
k #calc "1.5*magSqr(0.05*$<vector>{${FOAM_CASE}/testCalc2!U})";
|
||||||
|
\endverbatim
|
||||||
|
|
||||||
|
If the code string is delimited by \#{ ... \c \#} multiple lines
|
||||||
|
and multiple code statements can be used to generate the entry using
|
||||||
|
'os << ...;'. This is equivalent to \#codeStream but with a more
|
||||||
|
compact syntax.
|
||||||
|
|
||||||
\verbatim
|
\verbatim
|
||||||
#calcInclude "transform.H"
|
#calcInclude "transform.H"
|
||||||
maxAngle 30;
|
maxAngle 30;
|
||||||
@ -90,6 +129,35 @@ Usage
|
|||||||
#};
|
#};
|
||||||
\endverbatim
|
\endverbatim
|
||||||
|
|
||||||
|
Example to generate a single block blockMeshDict for use with snappyHexMesh
|
||||||
|
with no redundant information
|
||||||
|
\verbatim
|
||||||
|
min (-2.5 -1.2 -3.0); // Minimum coordinates of the block
|
||||||
|
max (2.5 1.2 3.0); // Maximum coordinates of the block
|
||||||
|
nCellsByL 33.3333; // Number of cells per unit length
|
||||||
|
|
||||||
|
// Calculate the number of cells in each block direction
|
||||||
|
nCells #calc
|
||||||
|
"Vector<label>($nCellsByL*($<vector>max - $<vector>min) + vector::one/2)";
|
||||||
|
|
||||||
|
// Generate the vertices using a boundBox
|
||||||
|
#calcInclude "boundBox.H"
|
||||||
|
vertices #calc "boundBox($<vector>min, $<vector>max).points()";
|
||||||
|
|
||||||
|
blocks
|
||||||
|
(
|
||||||
|
hex #calc "identityMap(8)" $nCells simpleGrading (1 1 1)
|
||||||
|
);
|
||||||
|
|
||||||
|
defaultPatch
|
||||||
|
{
|
||||||
|
type patch;
|
||||||
|
}
|
||||||
|
|
||||||
|
boundary
|
||||||
|
();
|
||||||
|
\endverbatim
|
||||||
|
|
||||||
See also
|
See also
|
||||||
Foam::functionEntries::calcIncludeEntry
|
Foam::functionEntries::calcIncludeEntry
|
||||||
Foam::functionEntries::codeStream
|
Foam::functionEntries::codeStream
|
||||||
|
|||||||
@ -26,22 +26,39 @@ Class
|
|||||||
|
|
||||||
Description
|
Description
|
||||||
Dictionary entry that contains C++ OpenFOAM code that is compiled to
|
Dictionary entry that contains C++ OpenFOAM code that is compiled to
|
||||||
generate the entry itself. So
|
generate the entry itself.
|
||||||
- codeStream reads three entries: 'code', 'codeInclude' (optional),
|
|
||||||
'codeOptions' (optional)
|
\c \#codeStream reads three entries: \c code, \c codeInclude (optional),
|
||||||
and uses those to generate library sources inside \c codeStream/
|
\c codeOptions (optional) to generate the library source code stored in the
|
||||||
- these get compiled using 'wmake libso'
|
local \c dynamicCode directory with a subdirectory name corresponding to the
|
||||||
- the resulting library is loaded in executed with as arguments
|
SHA1 of the code. The code is then compiled into a dynamically loaded
|
||||||
|
library libcodeStream_<SHA1>.so stored in the
|
||||||
|
\c dynamicCode/platforms/$WM_OPTIONS/lib directory using 'wmake libso'.
|
||||||
|
The resulting library is loaded in executed with arguments
|
||||||
\code
|
\code
|
||||||
(const dictionary& dict, Ostream& os)
|
(const dictionary& dict, Ostream& os)
|
||||||
\endcode
|
\endcode
|
||||||
where the dictionary is the current dictionary.
|
where the dictionary is the current dictionary. The code writes results to
|
||||||
- the code has to write into Ostream which is then used to construct
|
the current entry via the \c Ostream \c os.
|
||||||
the actual dictionary entry.
|
|
||||||
|
|
||||||
|
The verbatim string format \c \#{ ... \c \#} is used to allow multi-line
|
||||||
|
input without the need to escape the newlines.
|
||||||
|
|
||||||
E.g. to set the internal field of a field:
|
Dictionary entries constructed with \c \#codeStream can conveniently access
|
||||||
|
and use typed variables. This means calculations involving vectors and
|
||||||
|
tensors and list etc. are possible. To access a variable and construct it
|
||||||
|
as a given type within a \c \#codeStream entry, put the type immediately
|
||||||
|
after the $ symbol inside angled brackets <>. So, $<vector>var or
|
||||||
|
$<vector>{var} substitutes a variable named var as a vector.
|
||||||
|
|
||||||
|
The variable values are no longer embedded into the code but looked-up at
|
||||||
|
run-time making the code corresponding to each \c \#codeStream independent
|
||||||
|
of the values in the dictionary and of each other. Hence the \c
|
||||||
|
\#codeStream code does not need to be recompiled when the values in the
|
||||||
|
dictionary are changed, only if the code is changed.
|
||||||
|
|
||||||
|
Usage
|
||||||
|
Example to set the internal field of a field:
|
||||||
\verbatim
|
\verbatim
|
||||||
internalField #codeStream
|
internalField #codeStream
|
||||||
{
|
{
|
||||||
@ -67,23 +84,98 @@ Description
|
|||||||
};
|
};
|
||||||
\endverbatim
|
\endverbatim
|
||||||
|
|
||||||
|
Example to rotate a list of points around an axis by a given angle
|
||||||
|
\verbatim
|
||||||
|
points ((3 0 0) (2 1 1) (1 2 2) (0 3 3));
|
||||||
|
rotation
|
||||||
|
{
|
||||||
|
axis (0 1 1);
|
||||||
|
angle 45;
|
||||||
|
}
|
||||||
|
|
||||||
Note the \c \#{ ... \c \#} syntax is a 'verbatim' input mode that allows
|
#codeStream
|
||||||
inputting strings with embedded newlines.
|
{
|
||||||
|
codeInclude
|
||||||
|
#{
|
||||||
|
#include "pointField.H"
|
||||||
|
#include "transform.H"
|
||||||
|
#};
|
||||||
|
|
||||||
Limitations:
|
code
|
||||||
- '~' symbol not allowed inside the code sections.
|
#{
|
||||||
- probably some other limitations (uses string::expand which expands
|
const pointField points($<List<point>>points);
|
||||||
\c \$ and \c ~ sequences)
|
const vector axis = $<vector>!rotation/axis;
|
||||||
|
const scalar angle = degToRad($!rotation/angle);
|
||||||
|
os << "pointsRotated" << nl
|
||||||
|
<< (Ra(axis, angle) & points)() << ";";
|
||||||
|
#};
|
||||||
|
};
|
||||||
|
\endverbatim
|
||||||
|
|
||||||
Note:
|
Example Compute the centre and trianglation of a polygon
|
||||||
The code to be compiled is stored under the local \c codeStream
|
\verbatim
|
||||||
directory with a subdirectory name corresponding to the SHA1 of the
|
polygon ((0 0 0) (1 0 0) (2 1 0) (0 2 0) (-1 1 0));
|
||||||
contents.
|
|
||||||
|
|
||||||
The corresponding library code is located under the local
|
#codeStream
|
||||||
\c codeStream/platforms/$WM_OPTIONS/lib directory in a library
|
{
|
||||||
\c libcodeStream_SHA1.so
|
codeInclude
|
||||||
|
#{
|
||||||
|
#include "polygonTriangulate.H"
|
||||||
|
#};
|
||||||
|
|
||||||
|
code
|
||||||
|
#{
|
||||||
|
const List<point> polygon($<List<point>>polygon);
|
||||||
|
writeEntry(os, "polygonCentre", face::centre(polygon));
|
||||||
|
|
||||||
|
polygonTriangulate triEngine;
|
||||||
|
triEngine.triangulate(polygon);
|
||||||
|
os << "polygonTris" << ' ' << triEngine.triPoints() << ";";
|
||||||
|
#};
|
||||||
|
};
|
||||||
|
\endverbatim
|
||||||
|
|
||||||
|
Example to generate a single block blockMeshDict for use with snappyHexMesh
|
||||||
|
with no redundant information
|
||||||
|
\verbatim
|
||||||
|
min (-2.5 -1.2 -3.0); // Minimum coordinates of the block
|
||||||
|
max (2.5 1.2 3.0); // Maximum coordinates of the block
|
||||||
|
nCellsByL 33.3333; // Number of cells per unit length
|
||||||
|
|
||||||
|
// Calculate the number of cells in each block direction
|
||||||
|
nCells #calc
|
||||||
|
"Vector<label>($nCellsByL*($<vector>max - $<vector>min) + vector::one/2)";
|
||||||
|
|
||||||
|
// Generate the vertices using a boundBox
|
||||||
|
vertices #codeStream
|
||||||
|
{
|
||||||
|
codeInclude
|
||||||
|
#{
|
||||||
|
#include "boundBox.H"
|
||||||
|
#};
|
||||||
|
|
||||||
|
code
|
||||||
|
#{
|
||||||
|
os << boundBox($<vector>min, $<vector>max).points();
|
||||||
|
#};
|
||||||
|
};
|
||||||
|
|
||||||
|
blocks
|
||||||
|
(
|
||||||
|
hex (0 1 2 3 4 5 6 7) $nCells simpleGrading (1 1 1)
|
||||||
|
);
|
||||||
|
|
||||||
|
defaultPatch
|
||||||
|
{
|
||||||
|
type patch;
|
||||||
|
}
|
||||||
|
|
||||||
|
boundary
|
||||||
|
();
|
||||||
|
\endverbatim
|
||||||
|
|
||||||
|
See also
|
||||||
|
Foam::functionEntries::calcEntry
|
||||||
|
|
||||||
SourceFiles
|
SourceFiles
|
||||||
codeStream.C
|
codeStream.C
|
||||||
|
|||||||
Reference in New Issue
Block a user