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-2021 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>
34 Type Foam::expressions::exprDriver::exprDriver::weightedAverage
35 (
36  const scalarField& wfield,
37  const Field<Type>& fld
38 )
39 {
40  if (isNull(wfield))
41  {
42  const label n = returnReduce(fld.size(), sumOp<label>());
43 
44  // stabilize
45  if (!n)
46  {
47  return Zero;
48  }
49 
50  return gSum(fld) / scalar(n);
51  }
52 
53  // #ifdef FULLDEBUG
54  // checkSize(wfield, fld);
55  // #endif
56 
57  const scalar s = gSum(wfield);
58 
59  // stabilize
60  if (mag(s) < ROOTVSMALL)
61  {
62  return Zero;
63  }
64 
65  return gSum(wfield*fld) / s;
66 }
67 
68 
69 template<class Type>
70 Type Foam::expressions::exprDriver::exprDriver::weightedSum
71 (
72  const scalarField& wfield,
73  const Field<Type>& fld
74 )
75 {
76  if (isNull(wfield))
77  {
78  return gSum(fld);
79  }
80 
81  // #ifdef FULLDEBUG
82  // checkSize(wfield, fld);
83  // #endif
84 
85  return gSum(wfield*fld);
86 }
87 
88 
89 template<class Type>
92 {
93  if (!result_.isPointData(wantPointData))
94  {
96  << "Expected a" << (wantPointData ? " point" : "")
97  << " field, but found a" << (!wantPointData ? " point" : "")
98  << " field" << nl
99  << exit(FatalError);
100  }
101 
102  return result_.getResult<Type>();
103 }
104 
105 
106 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
107 
108 template<class Type>
109 const Foam::Function1<Type>* Foam::expressions::exprDriver::getFunction1Ptr
110 (
111  const word& name,
112  const HashTable<refPtr<Function1<Type>>>& tbl,
113  wordList* listFailure
114 )
115 {
116  const Function1<Type>* func = nullptr;
117 
118  const auto iter = tbl.cfind(name);
119 
120  if (iter.found())
121  {
122  func = iter.val().get();
123  }
124 
125  if (!func && listFailure)
126  {
127  *listFailure = tbl.sortedToc();
128  }
129 
130  return func;
131 }
132 
133 
134 template<class Type>
136 {
137  // Currently only scalar, vector
138  #undef doLocalCode
139  #define doLocalCode(WhichType, MapperMember) \
140  if (std::is_same<Type, WhichType>::value) \
141  { \
142  return bool \
143  ( \
144  this->template getFunction1Ptr<WhichType> \
145  ( \
146  name, MapperMember \
147  ) \
148  ); \
149  }
150 
151  doLocalCode(scalar, scalarFuncs_);
152  doLocalCode(vector, vectorFuncs_);
153  #undef doLocalCode
154 
155  return false;
156 }
157 
158 
159 template<class Type>
161 (
162  const word& name,
163  const scalar x
164 ) const
165 {
166  const Function1<Type>* func = nullptr;
167 
168  wordList failed;
169 
170  do
171  {
172  // Currently only scalar, vector
173  #undef doLocalCode
174  #define doLocalCode(WhichType, MapperMember) \
175  if (std::is_same<Type, WhichType>::value) \
176  { \
177  const Function1<WhichType>* ptr = \
178  this->template getFunction1Ptr<WhichType> \
179  ( \
180  name, MapperMember, &failed \
181  ); \
182  func = reinterpret_cast<const Function1<Type>*>(ptr); \
183  break; \
184  }
185 
186  doLocalCode(scalar, scalarFuncs_);
187  doLocalCode(vector, vectorFuncs_);
188  #undef doLocalCode
189  }
190  while (false);
191 
192  // Error handling
193  if (!failed.empty())
194  {
196  << "No mapping '" << name << " (" << pTraits<Type>::typeName
197  << ") found." << nl
198  << "Valid entries: "
199  << flatOutput(failed) << nl
200  << exit(FatalError);
201  }
202 
203  if (func)
204  {
205  return func->value(x);
206  }
207 
208  return pTraits<Type>::zero;
209 }
210 
211 
212 template<class Type>
214 (
215  Field<Type>& result,
216  const word& name,
217  const scalarField& input
218 ) const
219 {
220  // #ifdef FULLDEBUG
221  // checkSize(result, input);
222  // #endif
223 
224  const Function1<Type>* func = nullptr;
225 
226  wordList failed;
227 
228  do
229  {
230  // Currently only scalar, vector
231  #undef doLocalCode
232  #define doLocalCode(WhichType, MapperMember) \
233  if (std::is_same<Type, WhichType>::value) \
234  { \
235  const Function1<WhichType>* ptr = \
236  this->template getFunction1Ptr<WhichType> \
237  ( \
238  name, MapperMember, &failed \
239  ); \
240  func = reinterpret_cast<const Function1<Type>*>(ptr); \
241  break; \
242  }
243 
244  doLocalCode(scalar, scalarFuncs_);
245  doLocalCode(vector, vectorFuncs_);
246  #undef doLocalCode
247  }
248  while (false);
249 
250  // Error handling
251  if (!failed.empty())
252  {
254  << "No mapping '" << name << " (" << pTraits<Type>::typeName
255  << ") found." << nl
256  << "Valid entries: "
257  << flatOutput(failed) << nl
258  << exit(FatalError);
259  }
260 
261  if (func)
262  {
263  const label len = min(result.size(), input.size());
264 
265  for (label i = 0; i < len; ++i)
266  {
267  result[i] = func->value(input[i]);
268  }
269 
270  // Safety
271  for (label i = len; i < result.size(); ++i)
272  {
273  result[i] = Zero;
274  }
275 
276  return;
277  }
278 
279  result = Zero;
280 }
281 
282 
283 template<class Type>
285 (
286  const word& name,
287  bool wantPointData,
288  label expectedSize
289 ) const
290 {
291  DebugInfo
292  << "Looking for local" << (wantPointData ? " point" : "")
293  << " field name:" << name << " type:"
294  << pTraits<Type>::typeName << " size:" << expectedSize;
295 
296 
297  bool good = hasVariable(name);
298 
299  if (good)
300  {
301  const exprResult& var = variable(name);
302 
303  DebugInfo
304  << " - found (" << var.valueType()
305  << (var.isPointData() ? " point" : "") << ')';
306 
307  good = (var.isType<Type>() && var.isPointData(wantPointData));
308 
309  // Do size checking if requested
310  if (good && expectedSize >= 0)
311  {
312  good = (var.size() == expectedSize);
313  reduce(good, andOp<bool>());
314 
315  if (debug && !good)
316  {
317  Info<< " size is";
318  }
319  }
320  }
321 
322  DebugInfo << (good ? " good" : " bad") << endl;
323 
324  return good;
325 }
326 
327 
328 template<class Type>
331 (
332  const Type& val
333 ) const
334 {
335  return tmp<Field<Type>>::New(size(), val);
336 }
337 
338 
339 template<class Type>
342 (
343  const Type& val
344 ) const
345 {
346  return tmp<Field<Type>>::New(pointSize(), val);
347 }
348 
349 
350 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::scalarField
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Definition: primitiveFieldsFwd.H:52
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::expressions::exprResult::size
label size() const
The field or object size.
Definition: exprResultI.H:281
Foam::returnReduce
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Definition: PstreamReduceOps.H:94
s
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Definition: gmvOutputSpray.H:25
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:61
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::expressions::exprResult::isPointData
bool isPointData(const bool wantPointData=true) const
Definition: exprResultI.H:242
Foam::expressions::exprResult::getResult
tmp< Field< Type > > getResult(bool cacheCopy=false)
Foam::expressions::exprDriver::getResult
tmp< Field< Type > > getResult(bool wantPointData=false)
Return the expression result as a tmp field.
Foam::expressions::exprDriver::newField
tmp< Field< Type > > newField(const Type &val=pTraits< Type >::zero) const
Return a new field with the size()
objectRegistry.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::gSum
Type gSum(const FieldField< Field, Type > &f)
Definition: FieldFieldFunctions.C:594
Foam::min
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
Foam::Function1
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: propellerInfo.H:291
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::reduce
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
Definition: PstreamReduceOps.H:51
Foam::expressions::exprDriver::newPointField
tmp< Field< Type > > newPointField(const Type &val=pTraits< Type >::zero) const
Return a new field with the pointSize()
Foam::expressions::exprResult
A polymorphic field/result from evaluating an expression.
Definition: exprResult.H:124
Foam::expressions::exprResult::valueType
const word & valueType() const noexcept
Basic type for the field or single value.
Definition: exprResultI.H:235
Foam::Field
Generic templated field type.
Definition: Field.H:63
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::andOp
Definition: ops.H:233
Foam::expressions::exprDriver::fillFunctionValues
void fillFunctionValues(Field< Type > &result, const word &name, const scalarField &input) const
Fill result with values remapped according to the named Function1.
Definition: exprDriverTemplates.C:214
Foam::func
void func(FieldField< Field, Type > &f, const FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
fld
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< ' ';}gmvFile<< nl;for(const word &name :lagrangianScalarNames){ IOField< scalar > fld(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Definition: gmvOutputLagrangian.H:23
Foam::expressions::exprDriver::isLocalVariable
bool isLocalVariable(const word &name, bool wantPointData=false, label expectedSize=-1) const
Test existence of a local variable.
Definition: exprDriverTemplates.C:285
Foam::FatalError
error FatalError
Foam::flatOutput
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:216
Foam::expressions::exprResult::isType
bool isType() const
True if valueType corresponds to the given Type.
Definition: exprResultI.H:257
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
doLocalCode
#define doLocalCode(WhichType, MapperMember)
Foam::HashTable
A HashTable similar to std::unordered_map.
Definition: HashTable.H:105
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
Foam::expressions::exprDriver::getFunctionValue
Type getFunctionValue(const word &name, const scalar x) const
Definition: exprDriverTemplates.C:161
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
DebugInfo
#define DebugInfo
Report an information message using Foam::Info.
Definition: messageStream.H:382
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::Vector< scalar >
Foam::List< word >
Foam::pTraits
A traits class, which is primarily used for primitives.
Definition: pTraits.H:56
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::input
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:55
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::isNull
bool isNull(const T *ptr)
True if ptr is a pointer (of type T) to the nullObject.
Definition: nullObject.H:192
Foam::expressions::exprDriver::isFunction
bool isFunction(const word &name) const
Named mapping with given type exists.
Definition: exprDriverTemplates.C:135
Foam::expressions::exprDriver::result_
exprResult result_
The result.
Definition: exprDriver.H:190
Foam::refPtr
A class for managing references or pointers (no reference counting)
Definition: PtrList.H:60