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 <bgschaid@hfd-research.com>
9  Copyright (C) 2019 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 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
30 
31 template<class TableType>
33 (
34  const word& name,
35  const dictionary& dict,
37  bool clear
38 )
39 {
40  if (clear)
41  {
42  tbl.clear();
43  }
44 
45  if (!dict.found(name))
46  {
47  return false;
48  }
49 
50  ITstream& is = dict.lookup(name);
51  List<dictionary> input(is);
52 
53  for (const dictionary& d : input)
54  {
55  tbl.insert(dict.get<word>("name"), TableType(d));
56  }
57 
58  return true;
59 }
60 
61 
62 template<class TableType>
64 (
65  Ostream& os,
66  const word& name,
67  const HashTable<TableType>& tbl
68 )
69 {
70  if (tbl.size())
71  {
72  os.writeKeyword(name);
73  os << token::BEGIN_LIST << nl;
74 
75  forAllConstIters(tbl, iter)
76  {
77  os.beginBlock();
78  os.writeEntry("name", iter.key());
79  (*iter).write(os);
80  os.endBlock();
81  }
82  os << token::END_LIST
83  << token::END_STATEMENT << nl;
84  }
85 }
86 
87 
88 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
89 
90 template<class Type>
91 Type Foam::expressions::exprDriver::exprDriver::weightedAverage
92 (
93  const scalarField& wfield,
94  const Field<Type>& fld
95 )
96 {
97  if (isNull(wfield))
98  {
99  const label n = returnReduce(fld.size(), sumOp<label>());
100 
101  // stabilize
102  if (!n)
103  {
104  return Zero;
105  }
106 
107  return gSum(fld) / scalar(n);
108  }
109 
110  // #ifdef FULLDEBUG
111  // checkSize(wfield, fld);
112  // #endif
113 
114  const scalar s = gSum(wfield);
115 
116  // stabilize
117  if (mag(s) < ROOTVSMALL)
118  {
119  return Zero;
120  }
121 
122  return gSum(wfield*fld) / s;
123 }
124 
125 
126 template<class Type>
127 Type Foam::expressions::exprDriver::exprDriver::weightedSum
128 (
129  const scalarField& wfield,
130  const Field<Type>& fld
131 )
132 {
133  if (isNull(wfield))
134  {
135  return gSum(fld);
136  }
137 
138  // #ifdef FULLDEBUG
139  // checkSize(wfield, fld);
140  // #endif
141 
142  return gSum(wfield*fld);
143 }
144 
145 
146 template<class Type>
149 {
150  if (!result_.isPointValue(isPointVal))
151  {
153  << "Expected a" << (isPointVal ? " point" : "")
154  << " field, but found a" << (!isPointVal ? " point" : "")
155  << " field" << nl
156  << exit(FatalError);
157  }
158 
159  return result_.getResult<Type>();
160 }
161 
162 
163 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
164 
165 template<class Type>
167 (
168  const word& name,
169  bool isPointVal,
170  label expectedSize
171 ) const
172 {
173  DebugInfo
174  << "Looking for local" << (isPointVal ? " point" : "")
175  << " field name:" << name << " type:"
176  << pTraits<Type>::typeName << " size:" << expectedSize;
177 
178  bool good = hasVariable(name);
179 
180  if (good)
181  {
182  const exprResult& var = variable(name);
183 
184  DebugInfo
185  << " - found (" << var.valueType() << ' ' << var.isPointValue() << ')';
186 
187 
188  good = (var.isType<Type>() && var.isPointValue(isPointVal));
189 
190  // Do size checking if requested
191  if (good && expectedSize >= 0)
192  {
193  good = (var.size() == expectedSize);
194  reduce(good, andOp<bool>());
195 
196  if (debug && !good)
197  {
198  Info<< " size is";
199  }
200  }
201  }
202 
203  DebugInfo << (good ? " good" : " bad") << endl;
204 
205  return good;
206 }
207 
208 
209 template<class Type>
212 (
213  const Type& val
214 ) const
215 {
216  return tmp<Field<Type>>::New(size(), val);
217 }
218 
219 
220 template<class Type>
223 (
224  const Type& val
225 ) const
226 {
227  return tmp<Field<Type>>::New(pointSize(), val);
228 }
229 
230 
231 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::HashTable::size
label size() const noexcept
The number of elements in table.
Definition: HashTableI.H:52
Foam::scalarField
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Definition: primitiveFieldsFwd.H:52
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::expressions::exprResult::size
label size() const
The field or object size.
Definition: exprResultI.H:290
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::expressions::exprDriver::isLocalVariable
bool isLocalVariable(const word &name, bool isPointVal, label expectedSize=-1) const
Test existence of a local variable.
Definition: exprDriverTemplates.C:167
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:59
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::expressions::exprResult::valueType
const word & valueType() const
Basic type for the field or single value.
Definition: exprResultI.H:250
Foam::expressions::exprResult::getResult
tmp< Field< Type > > getResult(bool cacheCopy=false)
Foam::expressions::exprDriver::newField
tmp< Field< Type > > newField(const Type &val=pTraits< Type >::zero) const
Return a new field with the size()
Foam::HashTable::insert
bool insert(const Key &key, const T &obj)
Copy insert a new entry, not overwriting existing entries.
Definition: HashTableI.H:168
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::gSum
Type gSum(const FieldField< Field, Type > &f)
Definition: FieldFieldFunctions.C:594
Foam::Ostream::beginBlock
virtual Ostream & beginBlock(const keyType &kw)
Write begin block group with the given name.
Definition: Ostream.C:91
Foam::sumOp
Definition: ops.H:213
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::exprDriver::getResult
tmp< Field< Type > > getResult(bool isPointVal=false)
Return the expression result as a tmp field.
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::expressions::exprResult
A polymorphic field/result from evaluating an expression.
Definition: exprResult.H:128
Foam::Field< scalar >
Foam::ITstream
An input stream of tokens.
Definition: ITstream.H:55
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::andOp
Definition: ops.H:233
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
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::Ostream::endBlock
virtual Ostream & endBlock()
Write end block group.
Definition: Ostream.C:109
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::Ostream::write
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
Foam::expressions::exprDriver::writeTable
static void writeTable(Ostream &os, const word &name, const HashTable< TableType > &tbl)
Write an interpolation table.
Definition: exprDriverTemplates.C:64
Foam::Ostream::writeKeyword
virtual Ostream & writeKeyword(const keyType &kw)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:57
Foam::expressions::exprResult::isType
bool isType() const
True if valueType corresponds to the given Type.
Definition: exprResultI.H:272
Foam::expressions::exprDriver::readTable
static bool readTable(const word &name, const dictionary &dict, HashTable< TableType > &tbl, bool clear=true)
Read an interpolation table.
Definition: exprDriverTemplates.C:33
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
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
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
DebugInfo
#define DebugInfo
Report an information message using Foam::Info.
Definition: messageStream.H:350
clear
patchWriters clear()
Foam::nl
constexpr char nl
Definition: Ostream.H:372
forAllConstIters
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
Foam::HashTable::clear
void clear()
Clear all entries from table.
Definition: HashTable.C:630
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:102
Foam::pTraits
Traits class for primitives.
Definition: pTraits.H:52
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::Ostream::writeEntry
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition: Ostream.H:219
Foam::expressions::exprResult::isPointValue
bool isPointValue(const bool isPointVal=true) const
True if representing point values, or test if same as isPointVal.
Definition: exprResultI.H:257
Foam::isNull
bool isNull(const T *ptr)
True if ptr is a pointer (of type T) to the nullObject.
Definition: nullObject.H:180
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::expressions::exprDriver::result_
exprResult result_
The result.
Definition: exprDriver.H:124