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-2021 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 expressions_exprResult_H
58#define expressions_exprResult_H
59
60#include "exprTraits.H"
61#include "dimensionedType.H"
63
64// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
65
66namespace Foam
67{
68namespace expressions
69{
70
71/*---------------------------------------------------------------------------*\
72 Class exprResult Declaration
73\*---------------------------------------------------------------------------*/
74
75class exprResult
76:
77 public refCount
78{
79 // Private Data
80
81 //- The value type as string,
82 //- normally corresponds to pTraits or typeName
83 word valType_;
84
85 //- Is single, uniform value (can be a non-field)
86 bool isUniform_;
87
88 //- Represents point data
89 bool isPointData_;
90
91 //- Whether or not the variable will be reset
92 bool noReset_;
93
94 //- Allow override of noReset_, but only accessible for subclasses
95 bool needsReset_;
96
97 //- Size (length) of field or object
98 label size_;
99
100 //- A %union of single values, including standard VectorSpace types
101 union singleValue
102 {
103 bool bool_;
104 label label_;
105 scalar scalar_;
106 vector vector_;
107 tensor tensor_;
108 symmTensor symmTensor_;
109 sphericalTensor sphTensor_;
110
111 //- Default construct, zero-initialized
112 singleValue();
113
114 //- Copy construct
115 singleValue(const singleValue& val);
116
117 //- Copy assignment
118 void operator=(const singleValue& val);
119
120 //- Return current value for specified type.
121 template<class T>
122 inline const T& get() const
123 {
125 << "Not implemented for type "
127 return pTraits<T>::zero;
128 }
129
130 //- Set new value for specified type.
131 // \return updated value
132 template<class T>
133 inline const T& set(const T& val)
134 {
136 << "Not implemented for type "
138 return val;
139 }
140 };
141
142 //- The single value representation
143 singleValue single_;
144
145 //- Allocated plain field (eg, scalarField)
146 void *fieldPtr_;
147
148
149 // Private Member Functions
150
151 //- Type-checked deletion of the value pointer.
152 // \return True if the type check was satisfied
153 template<class Type>
154 inline bool deleteChecked();
155
156 //- Dispatch to type-checked pointer deletion
157 void uglyDelete();
158
159 //- Type-checked creation of field from dictionary
160 // \return True if the type check was satisfied
161 template<class Type>
162 inline bool readChecked
163 (
164 const word& key,
165 const dictionary& dict,
166 const label len,
167 const bool uniform
168 );
169
170 //- Type-checked retrieval of uniform field from current results
171 // \return True if the type check was satisfied
172 template<class Type>
173 bool getUniformChecked
174 (
175 exprResult& result,
176 const label size,
177 const bool noWarn,
178 const bool parRun
179 ) const;
180
181 //- Type-checked retrieval of \c bool uniform field from current result
182 // \return True if the type check was satisfied
183 bool getUniformCheckedBool
184 (
185 exprResult& result,
186 const label size,
187 const bool noWarn,
188 const bool parRun
189 ) const;
190
191 //- Type-checked determination of centre value (min/max)
192 // \return True if the type check was satisfied
193 template<class Type>
194 bool setAverageValueChecked(const bool parRun = Pstream::parRun());
195
196 //- Type-checked determination of average bool value
197 // \return True if the type check was satisfied
198 bool setAverageValueCheckedBool(const bool parRun = Pstream::parRun());
199
200 //- Type-checked copy of field
201 // \return True if the type check was satisfied
202 template<class Type>
203 bool duplicateFieldChecked(const void* ptr);
204
205 //- Type-checked writing of the single value (uniform) entry
206 // \return True if the type check was satisfied
207 template<class Type>
208 bool writeSingleValueChecked(Ostream& os) const;
209
210 //- Type-checked writing field as entry (if keyword is non-empty)
211 //- or as plain field (if keyword is empty)
212 // \return True if the type check was satisfied
213 template<class Type>
214 bool writeFieldChecked(const word& keyword, Ostream& os) const;
215
216 //- Type-checked forwarding to Field::writeEntry
217 // \return True if the type check was satisfied
218 template<class Type>
219 bool writeEntryChecked(const word& keyword, Ostream& os) const;
220
221 //- Type-checked field addition with another expression field
222 // \return True if the type check was satisfied
223 template<class Type>
224 bool plusEqChecked(const exprResult& b);
225
226 //- Type-checked field multiplication with a scalar
227 // \return True if the type check was satisfied
228 template<class Type>
229 bool multiplyEqChecked(const scalar& b);
230
231
232 template<class Type>
233 inline void setResultImpl(Field<Type>*, bool wantPointData=false);
234
235 template<class Type>
236 inline void setResultImpl(const Field<Type>&, bool wantPointData=false);
237
238 template<class Type>
239 inline void setResultImpl(Field<Type>&&, bool wantPointData=false);
240
241 template<class Type>
242 inline void setResultImpl(const Type& val, const label len);
243
244 template<class Type>
245 inline void setSingleValueImpl(const Type& val);
246
247
248protected:
249
250 // Protected Member Functions
251
252 //- Simulate virtual templated methods
253 inline virtual expressions::exprResult& target() { return *this; }
254
255 //- Reset at new timestep according to the derived class type
256 virtual void resetImpl();
257
258 //- Reset at new timestep according to type
259 // \return true if it was actually reset
260 bool reset(bool force=false);
261
262 //- Adjusts the internal needsReset value
263 void needsReset(bool val) { needsReset_ = val; }
264
265
266public:
267
268 //- An empty result
269 static const exprResult null;
270
271 //- Friendship with globals
272 friend class exprResultGlobals;
273
274
275 //- Runtime type information
276 TypeName("exprResult");
277
279 (
280 autoPtr,
282 dictionary,
283 (
284 const dictionary& dict
285 ),
286 (dict)
287 );
289 (
290 autoPtr,
292 empty,
293 (),
294 ()
295 );
296
297
298 // Constructors
299
300 //- Default construct
301 exprResult();
303 //- Copy construct
304 exprResult(const exprResult& expr);
305
306 //- Move construct
307 exprResult(exprResult&& expr);
308
309 //- Construct from a dictionary
310 explicit exprResult
311 (
313 const bool uniform = false,
314 const bool needsValue = false
315 );
316
317 //- Construct from Istream as dictionary content
318 explicit exprResult(Istream& is);
319
320 //- Construct by copying a field
321 template<class Type>
322 explicit exprResult(const Field<Type>& fld);
323
324 //- Construct by moving a field
325 template<class Type>
326 explicit exprResult(Field<Type>&& fld);
328 //- Construct for an IOobject
329 template<class Type>
330 explicit exprResult(autoPtr<Type>&& obj);
331
332 //- Construct from a dimensioned value
333 template<class Type>
334 explicit exprResult(const dimensioned<Type>& dt);
335
336 #undef exprResult_Construct
337 #define exprResult_Construct(Type) \
338 \
339 explicit exprResult(const Type& val) : exprResult() \
340 { \
341 setSingleValue(val); \
342 }
343
345 exprResult_Construct(scalar);
350
351 #undef exprResult_Construct
352
353
354 // Selectors
355
356 //- Return a reference to the selected value driver
357 static autoPtr<exprResult> New(const dictionary& dict);
358
359 //- Construct from Istream as dictionary content
360 static autoPtr<exprResult> New(Istream& is);
361
362 //- Clone
363 virtual autoPtr<exprResult> clone() const
364 {
365 return autoPtr<exprResult>::New(*this);
366 }
368
369 //- Destructor
370 virtual ~exprResult();
371
372
373 // Member Functions
374
375 // Access
376
377 //- Has a value?
378 inline bool hasValue() const;
380 //- Basic type for the field or single value
381 inline const word& valueType() const noexcept;
382
383 //- True if representing point data,
384 //- or test for same value as wantPointData argument
385 inline bool isPointData(const bool wantPointData=true) const;
387 //- True if single, uniform value
388 inline bool isUniform() const;
389
390 //- True if valueType corresponds to the given Type
391 template<class Type>
392 inline bool isType() const;
394 //- Return a single value when isUniform() is true,
395 //- or Zero when it is non-uniform or if the type mismatches,
396 //- which means that it can generally be considered as failsafe.
397 template<class Type>
398 inline Type getValue() const;
399
400 //- True if valueType is a bool
401 inline bool is_bool() const;
402
403 //- The field or object size
404 inline label size() const;
405
406 //- The address of the field data content.
407 // Fatal for unknown types.
408 // Used, for example, for python integration
409 const void* dataAddress() const;
410
411
412 // Edit
413
414 //- Clear (zero) the result
415 void clear();
416
417 //- Change reset behaviour
418 void noReset() noexcept { noReset_ = true; }
419
420 //- Change reset behaviour
421 void allowReset() noexcept { noReset_ = false; }
422
423 //- Test if field corresponds to a single-value and thus uniform.
424 // Uses field min/max to establish uniformity.
425 // Test afterwards with isUniform()
426 void testIfSingleValue(const bool parRun = Pstream::parRun());
427
428
429 // Set results
430
431 //- Set result field, taking ownership of the pointer
432 template<class Type>
433 inline void setResult(Field<Type>*, bool wantPointData=false);
434
435 //- Set result field, taking copy of the field contents
436 template<class Type>
437 inline void setResult(const Field<Type>&, bool wantPointData=false);
438
439 //- Set result field, moving field contents
440 template<class Type>
441 inline void setResult(Field<Type>&&, bool wantPointData=false);
442
443 //- Set uniform result field of given size
444 template<class Type>
445 inline void setResult(const Type& val, const label size);
446
447 //- Set single-value uniform result
448 template<class Type>
449 inline void setSingleValue(const Type& val);
450
451
452 // Access/Get results
453
454 //- Return const reference to the field
455 template<class Type>
456 inline const Field<Type>& cref() const;
457
458 //- Return non-const reference to the field
459 template<class Type>
460 inline Field<Type>& ref();
461
462 //- Return non-const reference to the field, casting away constness
463 template<class Type>
464 inline Field<Type>& constCast() const;
465
466 //- Return tmp field of the contents,
467 //- optionally keeping a copy in cache
468 template<class Type>
469 inline tmp<Field<Type>> getResult(bool cacheCopy=false);
471 //- Construct a uniform field from the current results
472 // Uses the field average. Optionally warning if the min/max
473 // deviation is larger than SMALL.
475 (
476 const label size,
477 const bool noWarn,
478 const bool parRun = Pstream::parRun()
479 ) const;
480
481 //- Get a reduced result
482 template<template<class> class BinaryOp, class Type>
483 inline Type getReduced
484 (
485 const BinaryOp<Type>& bop,
486 const Type& initial = pTraits<Type>::zero
487 );
488
489
490 // Write
491
492 //- Forwarding to Field::writeEntry
493 void writeEntry(const word& keyword, Ostream& os) const;
494
495 //- Write entry as dictionary contents
496 void writeDict(Ostream& os, const bool subDict=true) const;
497
498 //- Write the field, optionally as an entry
499 void writeField(Ostream& os, const word& keyword = "") const;
500
501 //- Write the single value, or the first value from field
502 void writeValue(Ostream& os) const;
503
504
505 // Member Operators
506
507 //- Copy assignment
508 virtual void operator=(const exprResult& rhs);
510 //- Move assignment
511 virtual void operator=(exprResult&& rhs);
512
514 //- Scalar multiplication
515 exprResult& operator*=(const scalar& b);
516
517 //- Addition of results
519};
520
521
522// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
523
524} // End namespace expressions
525
526// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
527
528// Operators
529
531(
532 const scalar& a,
534);
536(
538 const scalar& b
539);
541(
544);
545
546
547// IO Operator
550
551
552// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
553
554} // End namespace Foam
555
556// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
557
558#include "exprResultI.H"
559
560// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
561
562#endif
563
564// ************************************************************************* //
Info<< nl<< "Wrote faMesh in vtk format: "<< writer.output().name()<< nl;}{ vtk::lineWriter writer(aMesh.points(), aMesh.edges(), fileName(aMesh.mesh().time().globalPath()/"finiteArea-edges"));writer.writeGeometry();writer.beginCellData(4);writer.writeProcIDs();{ Field< scalar > fld(faMeshTools::flattenEdgeField(aMesh.magLe(), true))
Generic templated field type.
Definition: Field.H:82
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
static autoPtr< Time > New()
Construct (dummy) Time - no functionObjects or libraries.
Definition: Time.C:717
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:433
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
Database for solution data, solver performance and other reduced data.
Definition: data.H:58
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
Generic dimensioned Type class.
A globally available registry of expression results. These are currently registered on Time (may chan...
A polymorphic field/result from evaluating an expression.
Definition: exprResult.H:127
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.
Definition: exprResultI.H:235
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.
Definition: exprResultI.H:728
void writeDict(Ostream &os, const bool subDict=true) const
Write entry as dictionary contents.
Definition: exprResult.C:585
exprResult & operator+=(const exprResult &b)
Addition of results.
Definition: exprResult.C:729
tmp< Field< Type > > getResult(bool cacheCopy=false)
exprResult getUniform(const label size, const bool noWarn, const bool parRun=Pstream::parRun()) const
Construct a uniform field from the current results.
Definition: exprResult.C:436
static const exprResult null
An empty result.
Definition: exprResult.H:318
virtual void resetImpl()
Reset at new timestep according to the derived class type.
Definition: exprResult.C:378
virtual expressions::exprResult & target()
Simulate virtual templated methods.
Definition: exprResult.H:302
label size() const
The field or object size.
Definition: exprResultI.H:281
bool isType() const
True if valueType corresponds to the given Type.
Definition: exprResultI.H:257
Field< Type > & ref()
Return non-const reference to the field.
declareRunTimeSelectionTable(autoPtr, exprResult, empty,(),())
void allowReset() noexcept
Change reset behaviour.
Definition: exprResult.H:470
void testIfSingleValue(const bool parRun=Pstream::parRun())
Test if field corresponds to a single-value and thus uniform.
Definition: exprResult.C:471
void writeValue(Ostream &os) const
Write the single value, or the first value from field.
Definition: exprResult.C:661
void noReset() noexcept
Change reset behaviour.
Definition: exprResult.H:467
bool is_bool() const
True if valueType is a bool.
Definition: exprResultI.H:275
void writeEntry(const word &keyword, Ostream &os) const
Forwarding to Field::writeEntry.
Definition: exprResult.C:561
virtual autoPtr< exprResult > clone() const
Clone.
Definition: exprResult.H:412
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.
Definition: exprResultI.H:423
TypeName("exprResult")
Runtime type information.
declareRunTimeSelectionTable(autoPtr, exprResult, dictionary,(const dictionary &dict),(dict))
virtual void operator=(const exprResult &rhs)
Copy assignment.
Definition: exprResult.C:498
void needsReset(bool val)
Adjusts the internal needsReset value.
Definition: exprResult.H:312
void setResult(Field< Type > *, bool wantPointData=false)
Set result field, taking ownership of the pointer.
Definition: exprResultI.H:359
exprResult()
Default construct.
Definition: exprResult.C:206
static autoPtr< exprResult > New(const dictionary &dict)
Return a reference to the selected value driver.
Definition: exprResult.C:304
void clear()
Clear (zero) the result.
Definition: exprResult.C:396
friend class exprResultGlobals
Friendship with globals.
Definition: exprResult.H:321
virtual ~exprResult()
Destructor.
Definition: exprResult.C:368
bool isUniform() const
True if single, uniform value.
Definition: exprResultI.H:250
bool isPointData(const bool wantPointData=true) const
Definition: exprResultI.H:242
const void * dataAddress() const
The address of the field data content.
Definition: exprResult.C:833
exprResult & operator*=(const scalar &b)
Scalar multiplication.
Definition: exprResult.C:695
bool reset(bool force=false)
Reset at new timestep according to type.
Definition: exprResult.C:384
bool hasValue() const
Has a value?
Definition: exprResultI.H:228
void writeField(Ostream &os, const word &keyword="") const
Write the field, optionally as an entry.
Definition: exprResult.C:629
A traits class, which is primarily used for primitives.
Definition: pTraits.H:59
Reference counter for various OpenFOAM components.
Definition: refCount.H:51
Tensor of scalars, i.e. Tensor<scalar>.
A class for managing temporary objects.
Definition: tmp.H:65
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
A class for handling words, derived from Foam::string.
Definition: word.H:68
const volScalarField & T
#define exprResult_Construct(Type)
Definition: exprResult.H:386
OBJstream os(runTime.globalPath()/outputName)
#define WarningInFunction
Report a warning using Foam::Warning.
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
Namespace for OpenFOAM.
SphericalTensor< scalar > sphericalTensor
SphericalTensor of scalars, i.e. SphericalTensor<scalar>.
SymmTensor< scalar > symmTensor
SymmTensor of scalars, i.e. SymmTensor<scalar>.
Definition: symmTensor.H:59
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Istream & operator>>(Istream &, directionInfo &)
const direction noexcept
Definition: Scalar.H:223
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
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
Definition: createFields.H:27
A non-counting (dummy) refCount.
Definition: refCount.H:59
#define TypeName(TypeNameString)
Declare a ClassName() with extra virtual type info.
Definition: typeInfo.H:73