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
30 
31 Usage
32  \verbatim
33  a 1.1;
34  b 3.2;
35  c #calc "$a*$b";
36  \endverbatim
37 
38  Special care is required for calc entries that include a division since
39  "/" is also used as the scoping operator to identify keywords in
40  sub-dictionaries. For example, "$a/b" expects a keyword "b" within a
41  sub-dictionary named "a". A division can be correctly executed by using a
42  space between a variables and "/", e.g.
43 
44  \verbatim
45  c #calc "$a / $b";
46  \endverbatim
47 
48  or bracketing the variable, e.g.
49 
50  \verbatim
51  c #calc "($a)/$b";
52  \endverbatim
53 
54  \verbatim
55  s "field";
56  fieldName #calc "$<string>s + \"Name\" ";
57  \endverbatim
58 
59  Additional include files for the #calc code compilation can be specified
60  using the #calcInclude entry, e.g. if functions from transform.H are used
61  \verbatim
62  angleOfAttack 5; // degs
63 
64  angle #calc "-degToRad($angleOfAttack)";
65 
66  #calcInclude "transform.H"
67  liftDir #calc "transform(Ry($angle), vector(0, 0, 1))";
68  dragDir #calc "transform(Ry($angle), vector(1, 0, 0))";
69  \endverbatim
70 
71  If the code string is delimited by '#{...#}' multiple lines and multiple
72  code statements can be used to generate the entry using 'os << ...;'.
73  This is equivalent to #codeStream but with a more compact syntax.
74  \verbatim
75  #calcInclude "transform.H"
76  maxAngle 30;
77  nAngles 7;
78  Us #calc
79  #{
80  const vector U($<vector>testCalc2!U);
81  const int nAngles = $nAngles;
82  const scalar angleStep = ($<scalar>maxAngle)/(nAngles - 1);
83  List<vector> Us(nAngles);
84  for(int i=0; i<nAngles; i++)
85  {
86  const scalar angle = degToRad(i*angleStep);
87  Us[i] = transform(Ry(angle), U);
88  }
89  os << Us;
90  #};
91  \endverbatim
92 
93 See also
94  Foam::functionEntries::calcIncludeEntry
95  Foam::functionEntries::codeStream
96 
97 SourceFiles
98  calcEntry.C
99 
100 \*---------------------------------------------------------------------------*/
101 
102 #ifndef calcEntry_H
103 #define calcEntry_H
104 
105 #include "functionEntry.H"
106 
107 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
108 
109 namespace Foam
110 {
111 
112 class dlLibraryTable;
113 
114 namespace functionEntries
115 {
116 
117 /*---------------------------------------------------------------------------*\
118  Class calcEntry Declaration
119 \*---------------------------------------------------------------------------*/
120 
121 class calcEntry
122 :
123  public functionEntry
124 {
125  // Private Member Functions
126 
127  //- Perform the calculation and return the resulting string
128  static string calc
129  (
130  const dictionary& dict,
131  Istream& is
132  );
133 
134 
135 public:
136 
137  //- Runtime type information
138  ClassName("calc");
139 
140 
141  // Constructors
142 
143  //- Disallow default bitwise copy construction
144  calcEntry(const calcEntry&) = delete;
145 
146 
147  // Member Functions
148 
149  //- Execute the functionEntry in a sub-dict context
150  static bool execute(dictionary& dict, Istream&);
151 
152  //- Execute the functionEntry in a primitiveEntry context
153  static bool execute
154  (
155  const dictionary& dict,
157  Istream&
158  );
159 
160 
161  // Member Operators
162 
163  //- Disallow default bitwise assignment
164  void operator=(const calcEntry&) = delete;
165 };
166 
167 
168 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
169 
170 } // End namespace functionEntries
171 } // End namespace Foam
172 
173 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
174 
175 #endif
176 
177 // ************************************************************************* //
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:160
Compiles and executes code string expressions, returning the result to the dictionary.
Definition: calcEntry.H:123
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.