Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
11License
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\*---------------------------------------------------------------------------*/
29#include "objectRegistry.H"
30
31// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
32
33template<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
52template<class Type>
53const 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
78template<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
103template<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);
151
152 return pTraits<Type>::zero;
153}
154
155
156template<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;
222
223 result = Zero;
224}
225
226
227template<class Type>
229(
230 const word& name,
231 bool wantPointData,
232 label expectedSize
233) const
234{
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
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;
267 return good;
268}
269
270
271template<class Type>
274(
275 const Type& val
276) const
278 return tmp<Field<Type>>::New(size(), val);
279}
280
281
282template<class Type>
285(
286 const Type& val
287) const
288{
289 return tmp<Field<Type>>::New(pointSize(), val);
290}
291
292
293// ************************************************************************* //
Generic templated field type that is much like a Foam::List except that it is expected to hold numeri...
Definition Field.H:172
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition Function1.H:92
virtual Type value(const scalar x) const
Return value as a function of (scalar) independent variable.
Definition Function1.C:62
A HashTable similar to std::unordered_map.
Definition HashTable.H:124
bool empty() const noexcept
True if List is empty (ie, size() is zero).
Definition UList.H:701
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
bool isLocalVariable(const word &name, bool wantPointData=false, label expectedSize=-1) const
Test existence of a local variable.
virtual label size() const
The natural field size for the expression.
Definition exprDriver.H:423
exprResult result_
The result.
Definition exprDriver.H:201
virtual exprResult & variable(const word &name)
Non-const access to the named variable (sub-classes only).
Definition exprDriverI.H:57
virtual label pointSize() const
The point field size for the expression.
Definition exprDriver.H:431
tmp< Field< Type > > newField(const Type &val=pTraits< Type >::zero) const
Return a new field with the size().
HashTable< refPtr< Function1< scalar > > > scalarFuncs_
Function1 mappings/timelines (scalar), evaluated at the simulation time or with arbitrary scalars.
Definition exprDriver.H:217
HashTable< refPtr< Function1< vector > > > vectorFuncs_
Function1 mappings/timelines (vector), evaluated at the simulation time or with arbitrary scalars.
Definition exprDriver.H:223
Type getFunctionValue(const word &name, const scalar x) const
Evaluate named mapping for the given time/value. Zero for undefined/unknown.
const exprResult & result() const noexcept
Const access to expression result.
Definition exprDriver.H:463
bool isFunction(const word &name) const
Named mapping with given type exists.
virtual bool hasVariable(const word &name) const
True if named variable exists.
Definition exprDriverI.H:37
void fillFunctionValues(Field< Type > &result, const word &name, const scalarField &input) const
Fill result with values remapped according to the named Function1.
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().
A polymorphic field/result from evaluating an expression.
Definition exprResult.H:122
const word & valueType() const noexcept
Basic type for the field or single value.
label size() const
The field or object size.
bool isType() const
True if valueType corresponds to the given Type.
bool isPointData(const bool wantPointData=true) const
True if representing point data, or test for same value as wantPointData argument.
A traits class, which is primarily used for primitives and vector-space.
Definition pTraits.H:64
A class for managing references or pointers (no reference counting).
Definition refPtr.H:54
A class for managing temporary objects.
Definition tmp.H:75
A class for handling words, derived from Foam::string.
Definition word.H:66
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
#define doLocalCode(FieldType, Variable)
auto & name
#define DebugInfo
Report an information message using Foam::Info.
Namespace for handling debugging switches.
Definition debug.C:45
List< word > wordList
List of word.
Definition fileName.H:60
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.
messageStream Info
Information stream (stdout output on master, null elsewhere).
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
bool returnReduceAnd(const bool value, const int communicator=UPstream::worldComm)
Perform logical (and) MPI Allreduce on a copy. Uses UPstream::reduceAnd.
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition hashSets.C:26
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition FlatOutput.H:217
static constexpr const zero Zero
Global zero (0).
Definition zero.H:127
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
Vector< scalar > vector
Definition vector.H:57
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50