Loading...
Searching...
No Matches
exprResult.H
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) 2012-2018 Bernhard Gschaider
9 Copyright (C) 2019-2023 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
27Class
28 Foam::expressions::exprResult
29
30Description
31 A polymorphic field/result from evaluating an expression
32
33 \heading Dictionary parameters
34 \table
35 Property | Description | Required | Default
36 resultType | The type of result | no | exprResult
37 unsetValue | Create without reading the dictionary | no | false
38 noReset | Suppress reset on time | no | false
39 \endtable
40
41 When creating with values
42 \table
43 Property | Description | Required | Default
44 valueType | Result value type (scalar, vector,..) | yes |
45 isSingleValue | A single field value | no | false
46 isPointValue | Interpret values as point values | no | false
47 value | The field values | yes |
48 fieldSize | The size of the field (when not single-value) | no |
49 \endtable
50
51SourceFiles
52 exprResult.C
53 exprResultI.H
54
55\*---------------------------------------------------------------------------*/
56
57#ifndef Foam_expressions_exprResult_H
58#define Foam_expressions_exprResult_H
59
60#include "exprTraits.H"
61#include "exprValue.H"
62#include "dimensionedType.H"
64
65// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
66
67namespace Foam
68{
69namespace expressions
70{
71
72/*---------------------------------------------------------------------------*\
73 Class exprResult Declaration
74\*---------------------------------------------------------------------------*/
75
76class exprResult
77{
78 // Private Data
79
80 //- The value type as string,
81 //- normally corresponds to pTraits or typeName
82 word valType_;
83
84 //- Represents point data
85 bool isPointData_;
86
87 //- Whether or not the variable will be reset
88 bool noReset_;
89
90 //- Allow override of noReset_, but only accessible for subclasses
91 bool needsReset_;
92
93 //- Size (length) of field or object
94 label size_;
95
96 //- The single value representation
97 expressions::exprValue value_;
98
99 //- Allocated plain field (eg, scalarField)
100 void *fieldPtr_;
101
102
103 // Private Member Functions
104
105 //- Type-checked deletion of the value pointer.
106 // \return True if the type check was satisfied
107 template<class Type>
108 inline bool deleteChecked();
109
110 //- Invoke type-checked pointer deletion
111 void destroy();
112
113 //- Type-checked creation of field from dictionary (primitive) entry
114 // \return True if the type check was satisfied
115 template<class Type>
116 inline bool readChecked
117 (
118 const entry& e,
120 const label len,
121 //! Read a single entry, not a field
122 const bool singleValueOnly
123 );
124
125 //- Type-checked retrieval of uniform field from current results
126 // \return True if the type check was satisfied
127 template<class Type>
128 bool getUniformChecked
129 (
130 exprResult& result,
131 const label size,
132 const bool noWarn,
133 const bool parRun
134 ) const;
135
136 //- Type-checked retrieval of \c bool uniform field from current result
137 // \return True if the type check was satisfied
138 bool getUniformCheckedBool
139 (
140 exprResult& result,
141 const label size,
142 const bool noWarn,
143 const bool parRun
144 ) const;
145
146 //- Type-checked determination of centre value (min/max)
147 // \return True if the type check was satisfied
148 template<class Type>
149 bool setAverageValueChecked(const bool parRun = UPstream::parRun());
150
151 //- Type-checked determination of average bool value
152 // \return True if the type check was satisfied
153 bool setAverageValueCheckedBool(const bool parRun = UPstream::parRun());
154
155 //- Type-checked copy of field
156 // \return True if the type check was satisfied
157 template<class Type>
158 bool duplicateFieldChecked(const void* ptr);
159
160 //- Type-checked writing of the single value (uniform) entry
161 // \return True if the type check was satisfied
162 template<class Type>
163 bool writeSingleValueChecked(Ostream& os) const;
164
165 //- Type-checked writing field as entry (if keyword is non-empty)
166 //- or as plain field (if keyword is empty)
167 // \return True if the type check was satisfied
168 template<class Type>
169 bool writeFieldChecked(const word& keyword, Ostream& os) const;
170
171 //- Type-checked forwarding to Field::writeEntry
172 // \return True if the type check was satisfied
173 template<class Type>
174 bool writeEntryChecked(const word& keyword, Ostream& os) const;
175
176 //- Type-checked field addition with another expression field
177 // \return True if the type check was satisfied
178 template<class Type>
179 bool plusEqChecked(const exprResult& b);
180
181 //- Type-checked field multiplication with a scalar
182 // \return True if the type check was satisfied
183 template<class Type>
184 bool multiplyEqChecked(const scalar& b);
185
186
187 template<class Type>
188 inline void setResultImpl(Field<Type>*, bool wantPointData=false);
189
190 template<class Type>
191 inline void setResultImpl(const Field<Type>&, bool wantPointData=false);
192
193 template<class Type>
194 inline void setResultImpl(Field<Type>&&, bool wantPointData=false);
195
196 template<class Type>
197 inline void setResultImpl(const Type& val, const label len);
198
199 template<class Type>
200 inline void setSingleValueImpl(const Type& val);
201
202
203protected:
204
205 // Protected Member Functions
206
207 //- Simulate virtual templated methods
208 inline virtual expressions::exprResult& target() { return *this; }
209
210 //- Reset at new timestep according to the derived class type
211 virtual void resetImpl();
212
213 //- Reset at new timestep according to type
214 // \return true if it was actually reset
215 bool reset(bool force=false);
216
217 //- Adjusts the internal needsReset value
218 void needsReset(bool val) { needsReset_ = val; }
219
220
221public:
222
223 //- An empty result
224 static const exprResult null;
225
226 //- Friendship with globals
227 friend class exprResultGlobals;
228
229
230 //- Runtime type information
231 TypeName("exprResult");
232
234 (
235 autoPtr,
238 (
239 const dictionary& dict
240 ),
241 (dict)
242 );
244 (
245 autoPtr,
247 empty,
248 (),
249 ()
250 );
251
252
253 // Constructors
254
255 //- Default construct
256 exprResult();
257
258 //- Copy construct
259 exprResult(const exprResult& expr);
260
261 //- Move construct
262 exprResult(exprResult&& expr);
263
264 //- Construct from a dictionary
265 explicit exprResult
266 (
267 const dictionary& dict,
269 const bool singleValueOnly = false,
271 const bool valueReqd = false
272 );
273
274 //- Construct from Istream as dictionary content
275 explicit exprResult(Istream& is);
276
277 //- Construct by copying a field
278 template<class Type>
279 explicit exprResult(const Field<Type>& fld);
280
281 //- Construct by moving a field
282 template<class Type>
283 explicit exprResult(Field<Type>&& fld);
284
285 //- Construct for an IOobject
286 template<class Type>
287 explicit exprResult(autoPtr<Type>&& obj);
288
289 //- Construct from a dimensioned value
290 template<class Type>
291 explicit exprResult(const dimensioned<Type>& dt);
292
293 #undef exprResult_Construct
294 #define exprResult_Construct(Type) \
295 \
296 explicit exprResult(const Type& val) : exprResult() \
297 { \
298 setSingleValue(val); \
299 }
300
302 exprResult_Construct(scalar);
308 #undef exprResult_Construct
309
310
311 // Selectors
312
313 //- Return a reference to the selected value driver
314 static autoPtr<exprResult> New(const dictionary& dict);
315
316 //- Construct from Istream as dictionary content
317 static autoPtr<exprResult> New(Istream& is);
318
319 //- Clone
320 virtual autoPtr<exprResult> clone() const
321 {
322 return autoPtr<exprResult>::New(*this);
323 }
325
326 //- Destructor
327 virtual ~exprResult();
328
329
330 // Member Functions
331
332 // Access
333
334 //- Has a value?
335 inline bool hasValue() const;
336
337 //- Basic type for the field or single value
338 inline const word& valueType() const noexcept;
339
340 //- True if representing point data,
341 //- or test for same value as wantPointData argument
342 inline bool isPointData(const bool wantPointData=true) const;
344 //- True if single, uniform value
345 inline bool isUniform() const;
346
347 //- True if valueType corresponds to the given Type
348 template<class Type>
349 inline bool isType() const;
350
351 //- Return a single value when isUniform() is true,
352 //- or Zero when it is non-uniform or if the type mismatches,
353 //- which means that it can generally be considered as failsafe.
354 template<class Type>
355 inline Type getValue() const;
356
357 //- Return a read pointer to the field data if the type matches,
358 //- nullptr otherwise. Can generally be considered as failsafe.
359 template<class Type>
360 inline const Field<Type>* getField() const;
361
362 //- True if valueType is a bool
363 inline bool is_bool() const;
364
365 //- The field or object size
366 inline label size() const;
367
368 //- The address of the field data content.
369 // Fatal for unknown types.
370 // Used, for example, for python integration
371 const void* dataAddress() const;
372
373
374 // Edit
375
376 //- Clear (zero) the result
377 void clear();
378
379 //- Change reset behaviour
380 void noReset() noexcept { noReset_ = true; }
381
382 //- Change reset behaviour
383 void allowReset() noexcept { noReset_ = false; }
384
385 //- Test if field corresponds to a single-value and thus uniform.
386 // Uses field min/max to establish uniformity.
387 // Test afterwards with isUniform()
388 void testIfSingleValue(const bool parRun = UPstream::parRun());
389
390
391 // Set results
392
393 //- Set result field, taking ownership of the pointer
394 template<class Type>
395 inline void setResult(Field<Type>*, bool wantPointData=false);
396
397 //- Set result field, taking copy of the field contents
398 template<class Type>
399 inline void setResult(const Field<Type>&, bool wantPointData=false);
400
401 //- Set result field, moving field contents
402 template<class Type>
403 inline void setResult(Field<Type>&&, bool wantPointData=false);
404
405 //- Set uniform result field of given size
406 template<class Type>
407 inline void setResult(const Type& val, const label size);
408
409 //- Set single-value uniform result
410 template<class Type>
411 inline void setSingleValue(const Type& val);
412
413
414 // Access/Get results
416 //- Return const reference to the field
417 template<class Type>
418 inline const Field<Type>& cref() const;
419
420 //- Return non-const reference to the field
421 template<class Type>
422 inline Field<Type>& ref();
423
424 //- Return non-const reference to the field, casting away constness
425 template<class Type>
426 inline Field<Type>& constCast() const;
427
428 //- Return tmp field of the contents,
429 //- optionally keeping a copy in cache
430 template<class Type>
431 inline tmp<Field<Type>> getResult(bool cacheCopy=false);
433 //- Construct a uniform field from the current results
434 // Uses the field average. Optionally warning if the min/max
435 // deviation is larger than SMALL.
437 (
438 const label size,
439 const bool noWarn,
440 const bool parRun = UPstream::parRun()
441 ) const;
442
443 //- Get a reduced result
444 template<template<class> class BinaryOp, class Type>
445 inline Type getReduced
446 (
447 const BinaryOp<Type>& bop,
448 const Type& initial = pTraits<Type>::zero
449 );
450
451
452 // Write
453
454 //- Forwarding to Field::writeEntry
455 void writeEntry(const word& keyword, Ostream& os) const;
457 //- Write entry as dictionary contents
458 void writeDict(Ostream& os, const bool subDict=true) const;
459
460 //- Write the field, optionally as an entry
461 void writeField(Ostream& os, const word& keyword = "") const;
462
463 //- Write the single value, or the first value from field
464 void writeValue(Ostream& os) const;
465
466
467 // Member Operators
468
469 //- Copy assignment
470 virtual void operator=(const exprResult& rhs);
471
472 //- Move assignment
473 virtual void operator=(exprResult&& rhs);
474
475
476 //- Scalar multiplication
477 exprResult& operator*=(const scalar& b);
478
479 //- Addition of results
481};
482
483
484// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
485
486} // End namespace expressions
487
488// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
489
490// Operators
491
493(
494 const scalar& a,
496);
498(
500 const scalar& b
501);
503(
506);
507
508
509// IO Operator
513
514// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
515
516} // End namespace Foam
517
518// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
519
520#include "exprResultI.H"
521
522// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
523
524#endif
525
526// ************************************************************************* //
Info<< nl;Info<< "Write faMesh in vtk format:"<< nl;{ vtk::uindirectPatchWriter writer(aMesh.patch(), fileName(aMesh.time().globalPath()/vtkBaseFileName));writer.writeGeometry();globalIndex procAddr(aMesh.nFaces());labelList cellIDs;if(UPstream::master()) { cellIDs.resize(procAddr.totalSize());for(const labelRange &range :procAddr.ranges()) { auto slice=cellIDs.slice(range);slice=identity(range);} } writer.beginCellData(4);writer.writeProcIDs();writer.write("cellID", cellIDs);writer.write("area", aMesh.S().field());writer.write("normal", aMesh.faceAreaNormals());writer.beginPointData(1);writer.write("normal", aMesh.pointAreaNormals());Info<< " "<< writer.output().name()<< nl;}{ vtk::lineWriter writer(aMesh.points(), aMesh.edges(), fileName(aMesh.time().globalPath()/(vtkBaseFileName+"-edges")));writer.writeGeometry();writer.beginCellData(4);writer.writeProcIDs();{ Field< scalar > fld(faMeshTools::flattenEdgeField(aMesh.magLe(), true))
Generic templated field type that is much like a Foam::List except that it is expected to hold numeri...
Definition Field.H:172
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
static bool parRun(const bool on) noexcept
Set as parallel run on/off.
Definition UPstream.H:1669
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
static autoPtr< T > New(Args &&... args)
Construct autoPtr with forwarding arguments.
Definition autoPtr.H:178
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
Generic dimensioned Type class.
A keyword and a list of tokens is an 'entry'.
Definition entry.H:66
A polymorphic field/result from evaluating an expression.
Definition exprResult.H:122
Field< Type > & constCast() const
Return non-const reference to the field, casting away constness.
const word & valueType() const noexcept
Basic type for the field or single value.
void testIfSingleValue(const bool parRun=UPstream::parRun())
Test if field corresponds to a single-value and thus uniform.
Definition exprResult.C:440
exprResult(Istream &is)
Construct from Istream as dictionary content.
Type getReduced(const BinaryOp< Type > &bop, const Type &initial=pTraits< Type >::zero)
Get a reduced result.
Type getValue() const
Return a single value when isUniform() is true, or Zero when it is non-uniform or if the type mismatc...
void writeDict(Ostream &os, const bool subDict=true) const
Write entry as dictionary contents.
Definition exprResult.C:552
exprResult & operator+=(const exprResult &b)
Addition of results.
Definition exprResult.C:697
tmp< Field< Type > > getResult(bool cacheCopy=false)
Return tmp field of the contents, optionally keeping a copy in cache.
static const exprResult null
An empty result.
Definition exprResult.H:332
virtual void resetImpl()
Reset at new timestep according to the derived class type.
Definition exprResult.C:346
virtual expressions::exprResult & target()
Simulate virtual templated methods.
Definition exprResult.H:307
label size() const
The field or object size.
bool isType() const
True if valueType corresponds to the given Type.
Field< Type > & ref()
Return non-const reference to the field.
declareRunTimeSelectionTable(autoPtr, exprResult, empty,(),())
void allowReset() noexcept
Change reset behaviour.
Definition exprResult.H:548
exprResult getUniform(const label size, const bool noWarn, const bool parRun=UPstream::parRun()) const
Construct a uniform field from the current results.
Definition exprResult.C:404
void writeValue(Ostream &os) const
Write the single value, or the first value from field.
Definition exprResult.C:628
void noReset() noexcept
Change reset behaviour.
Definition exprResult.H:543
bool is_bool() const
True if valueType is a bool.
void writeEntry(const word &keyword, Ostream &os) const
Forwarding to Field::writeEntry.
Definition exprResult.C:528
virtual autoPtr< exprResult > clone() const
Clone.
Definition exprResult.H:456
exprResult(autoPtr< Type > &&obj)
Construct for an IOobject.
const Field< Type > & cref() const
Return const reference to the field.
void setSingleValue(const Type &val)
Set single-value uniform result.
const Field< Type > * getField() const
Return a read pointer to the field data if the type matches, nullptr otherwise. Can generally be cons...
TypeName("exprResult")
Runtime type information.
declareRunTimeSelectionTable(autoPtr, exprResult, dictionary,(const dictionary &dict),(dict))
virtual void operator=(const exprResult &rhs)
Copy assignment.
Definition exprResult.C:467
void needsReset(bool val)
Adjusts the internal needsReset value.
Definition exprResult.H:324
void setResult(Field< Type > *, bool wantPointData=false)
Set result field, taking ownership of the pointer.
exprResult()
Default construct.
Definition exprResult.C:173
static autoPtr< exprResult > New(const dictionary &dict)
Return a reference to the selected value driver.
Definition exprResult.C:272
void clear()
Clear (zero) the result.
Definition exprResult.C:364
friend class exprResultGlobals
Friendship with globals.
Definition exprResult.H:337
virtual ~exprResult()
Destructor.
Definition exprResult.C:336
bool isUniform() const
True if single, uniform value.
bool isPointData(const bool wantPointData=true) const
True if representing point data, or test for same value as wantPointData argument.
const void * dataAddress() const
The address of the field data content.
Definition exprResult.C:810
exprResult & operator*=(const scalar &b)
Scalar multiplication.
Definition exprResult.C:662
bool reset(bool force=false)
Reset at new timestep according to type.
Definition exprResult.C:352
bool hasValue() const
Has a value?
void writeField(Ostream &os, const word &keyword="") const
Write the field, optionally as an entry.
Definition exprResult.C:596
A polymorphic typed union of simple primitive and VectorSpace types. It uses a 'fatter' representatio...
Definition exprValue.H:158
A traits class, which is primarily used for primitives and vector-space.
Definition pTraits.H:64
A class for managing temporary objects.
Definition tmp.H:75
A class for handling words, derived from Foam::string.
Definition word.H:66
#define exprResult_Construct(Type)
Definition exprResult.H:424
OBJstream os(runTime.globalPath()/outputName)
A namespace for expression-related classes/traits etc.
Namespace for OpenFOAM.
Tensor< scalar > tensor
Definition symmTensor.H:57
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
Istream & operator>>(Istream &, directionInfo &)
void rhs(fvMatrix< typename Expr::value_type > &m, const Expr &expression)
const direction noexcept
Definition scalarImpl.H:265
bool isType(const U &obj)
Check if typeid of the object and Type are identical.
Definition typeInfo.H:112
Vector< scalar > vector
Definition vector.H:57
SphericalTensor< scalar > sphericalTensor
SphericalTensor of scalars, i.e. SphericalTensor<scalar>.
SymmTensor< scalar > symmTensor
SymmTensor of scalars, i.e. SymmTensor<scalar>.
Definition symmTensor.H:55
Macros to ease declaration of run-time selection tables.
#define declareRunTimeSelectionTable(ptrWrapper, baseType, argNames, argList, parList)
Declare a run-time selection (variables and adder classes).
dictionary dict
volScalarField & b
volScalarField & e
#define TypeName(TypeNameString)
Declare a ClassName() with extra virtual type info.
Definition typeInfo.H:68