exprDriverTemplates.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2010-2018 Bernhard Gschaider
9  Copyright (C) 2019-2025 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "objectRegistry.H"
30 
31 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
32 
33 template<class Type>
36 {
37  if (!result_.isPointData(wantPointData))
38  {
40  << "Expected a" << (wantPointData ? " point" : "")
41  << " field, but found a" << (!wantPointData ? " point" : "")
42  << " field" << nl
43  << exit(FatalError);
44  }
45 
46  return result_.getResult<Type>();
47 }
48 
49 
50 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
51 
52 template<class Type>
53 const Foam::Function1<Type>* Foam::expressions::exprDriver::getFunction1Ptr
54 (
55  const word& name,
56  const HashTable<refPtr<Function1<Type>>>& tbl,
57  wordList* listFailure
58 )
59 {
60  const Function1<Type>* func = nullptr;
61 
62  const auto iter = tbl.cfind(name);
63 
64  if (iter.good())
65  {
66  func = iter.val().get();
67  }
68 
69  if (!func && listFailure)
70  {
71  *listFailure = tbl.sortedToc();
72  }
73 
74  return func;
75 }
76 
77 
78 template<class Type>
80 {
81  // Currently only scalar, vector
82  #undef doLocalCode
83  #define doLocalCode(WhichType, MapperMember) \
84  if constexpr (std::is_same_v<Type, WhichType>) \
85  { \
86  return bool \
87  ( \
88  this->template getFunction1Ptr<WhichType> \
89  ( \
90  name, MapperMember \
91  ) \
92  ); \
93  }
94 
95  doLocalCode(scalar, scalarFuncs_);
96  doLocalCode(vector, vectorFuncs_);
97  #undef doLocalCode
98 
99  return false;
100 }
101 
102 
103 template<class Type>
105 (
106  const word& name,
107  const scalar x
108 ) const
109 {
110  const Function1<Type>* func = nullptr;
111 
112  wordList failed;
113 
114  do
115  {
116  // Currently only scalar, vector
117  #undef doLocalCode
118  #define doLocalCode(WhichType, MapperMember) \
119  if constexpr (std::is_same_v<Type, WhichType>) \
120  { \
121  const Function1<WhichType>* ptr = \
122  this->template getFunction1Ptr<WhichType> \
123  ( \
124  name, MapperMember, &failed \
125  ); \
126  func = reinterpret_cast<const Function1<Type>*>(ptr); \
127  break; \
128  }
129 
130  doLocalCode(scalar, scalarFuncs_);
131  doLocalCode(vector, vectorFuncs_);
132  #undef doLocalCode
133  }
134  while (false);
135 
136  // Error handling
137  if (!failed.empty())
138  {
140  << "No mapping '" << name << " (" << pTraits<Type>::typeName
141  << ") found." << nl
142  << "Valid entries: "
143  << flatOutput(failed) << nl
144  << exit(FatalError);
145  }
146 
147  if (func)
148  {
149  return func->value(x);
150  }
152  return pTraits<Type>::zero;
153 }
154 
155 
156 template<class Type>
158 (
159  Field<Type>& result,
160  const word& name,
161  const scalarField& input
162 ) const
163 {
164  // #ifdef FULLDEBUG
165  // checkSize(result, input);
166  // #endif
167 
168  const Function1<Type>* func = nullptr;
169 
170  wordList failed;
171 
172  do
173  {
174  // Currently only scalar, vector
175  #undef doLocalCode
176  #define doLocalCode(WhichType, MapperMember) \
177  if constexpr (std::is_same_v<Type, WhichType>) \
178  { \
179  const Function1<WhichType>* ptr = \
180  this->template getFunction1Ptr<WhichType> \
181  ( \
182  name, MapperMember, &failed \
183  ); \
184  func = reinterpret_cast<const Function1<Type>*>(ptr); \
185  break; \
186  }
187 
188  doLocalCode(scalar, scalarFuncs_);
189  doLocalCode(vector, vectorFuncs_);
190  #undef doLocalCode
191  }
192  while (false);
193 
194  // Error handling
195  if (!failed.empty())
196  {
198  << "No mapping '" << name << " (" << pTraits<Type>::typeName
199  << ") found." << nl
200  << "Valid entries: "
201  << flatOutput(failed) << nl
202  << exit(FatalError);
203  }
204 
205  if (func)
206  {
207  const label len = min(result.size(), input.size());
208 
209  for (label i = 0; i < len; ++i)
210  {
211  result[i] = func->value(input[i]);
212  }
213 
214  // Safety
215  for (label i = len; i < result.size(); ++i)
216  {
217  result[i] = Zero;
218  }
219 
220  return;
221  }
223  result = Zero;
224 }
225 
226 
227 template<class Type>
229 (
230  const word& name,
231  bool wantPointData,
232  label expectedSize
233 ) const
234 {
235  DebugInfo
236  << "Looking for local" << (wantPointData ? " point" : "")
237  << " field name:" << name << " type:"
238  << pTraits<Type>::typeName << " size:" << expectedSize;
239 
240 
241  bool good = hasVariable(name);
242 
243  if (good)
244  {
245  const exprResult& var = variable(name);
246 
247  DebugInfo
248  << " - found (" << var.valueType()
249  << (var.isPointData() ? " point" : "") << ')';
250 
251  good = (var.isType<Type>() && var.isPointData(wantPointData));
252 
253  // Do size checking if requested
254  if (good && expectedSize >= 0)
255  {
256  good = returnReduceAnd(var.size() == expectedSize);
257 
258  if (debug && !good)
259  {
260  Info<< " size is";
261  }
262  }
263  }
264 
265  DebugInfo << (good ? " good" : " bad") << endl;
266 
267  return good;
268 }
269 
270 
271 template<class Type>
274 (
275  const Type& val
276 ) const
277 {
278  return tmp<Field<Type>>::New(size(), val);
279 }
280 
281 
282 template<class Type>
285 (
286  const Type& val
287 ) const
288 {
289  return tmp<Field<Type>>::New(pointSize(), val);
290 }
291 
292 
293 // ************************************************************************* //
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
tmp< Field< Type > > getResult(bool wantPointData=false)
Return the expression result as a tmp field.
tmp< Field< Type > > newPointField(const Type &val=pTraits< Type >::zero) const
Return a new field with the pointSize()
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:600
A polymorphic field/result from evaluating an expression.
Definition: exprResult.H:121
tmp< Field< Type > > getResult(bool cacheCopy=false)
Return tmp field of the contents, optionally keeping a copy in cache.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
bool empty() const noexcept
True if List is empty (ie, size() is zero)
Definition: UList.H:697
bool isType() const
True if valueType corresponds to the given Type.
Definition: exprResultI.H:214
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:529
A traits class, which is primarily used for primitives and vector-space.
Definition: pTraits.H:61
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tf1, const word &name, const dimensionSet &dimensions, const bool initCopy=false)
Global function forwards to reuseTmpDimensionedField::New.
const word & valueType() const noexcept
Basic type for the field or single value.
Definition: exprResultI.H:192
tmp< Field< Type > > newField(const Type &val=pTraits< Type >::zero) const
Return a new field with the size()
A class for managing references or pointers (no reference counting)
Definition: HashPtrTable.H:49
bool isFunction(const word &name) const
Named mapping with given type exists.
virtual Type value(const scalar x) const
Return value as a function of (scalar) independent variable.
Definition: Function1.C:62
void fillFunctionValues(Field< Type > &result, const word &name, const scalarField &input) const
Fill result with values remapped according to the named Function1.
Type getFunctionValue(const word &name, const scalar x) const
Evaluate named mapping for the given time/value. Zero for undefined/unknown.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
Generic templated field type.
Definition: Field.H:63
A class for handling words, derived from Foam::string.
Definition: word.H:63
bool returnReduceAnd(const bool value, const int communicator=UPstream::worldComm)
Perform logical (and) MPI Allreduce on a copy. Uses UPstream::reduceAnd.
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:33
A HashTable similar to std::unordered_map.
Definition: HashTable.H:108
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:26
#define DebugInfo
Report an information message using Foam::Info.
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
int debug
Static debugging option.
label size() const
The field or object size.
Definition: exprResultI.H:252
#define doLocalCode(WhichType, MapperMember)
bool isLocalVariable(const word &name, bool wantPointData=false, label expectedSize=-1) const
Test existence of a local variable.
messageStream Info
Information stream (stdout output on master, null elsewhere)
exprResult result_
The result.
Definition: exprDriver.H:201
bool isPointData(const bool wantPointData=true) const
True if representing point data, or test for same value as wantPointData argument.
Definition: exprResultI.H:199
A class for managing temporary objects.
Definition: HashPtrTable.H:50
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:225
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127