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 -------------------------------------------------------------------------------
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 Class
28  Foam::expressions::exprResult
29 
30 Description
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 
51 SourceFiles
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"
63 #include "runTimeSelectionTables.H"
64 
65 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
66 
67 namespace Foam
68 {
69 namespace expressions
70 {
71 
72 /*---------------------------------------------------------------------------*\
73  Class exprResult Declaration
74 \*---------------------------------------------------------------------------*/
75 
76 class 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,
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 = Pstream::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 = Pstream::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 
203 protected:
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 
221 public:
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,
236  exprResult,
237  dictionary,
238  (
239  const dictionary& dict
240  ),
241  (dict)
242  );
244  (
245  autoPtr,
246  exprResult,
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 
301  exprResult_Construct(bool);
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;
343 
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 = Pstream::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
415 
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 = Pstream::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 
492 expressions::exprResult operator*
493 (
494  const scalar& a,
496 );
497 expressions::exprResult operator*
498 (
499  const expressions::exprResult& a,
500  const scalar& b
501 );
502 expressions::exprResult operator+
503 (
504  const expressions::exprResult& a,
506 );
507 
508 
509 // IO Operator
512 
513 
514 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
515 
516 } // End namespace Foam
517 
518 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
519 
520 #include "exprResultI.H"
521 
522 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
523 
524 #endif
525 
526 // ************************************************************************* //
bool hasValue() const
Has a value?
Definition: exprResultI.H:185
Type getReduced(const BinaryOp< Type > &bop, const Type &initial=pTraits< Type >::zero)
Get a reduced result.
Definition: exprResultI.H:699
bool is_bool() const
True if valueType is a bool.
Definition: exprResultI.H:246
void writeValue(Ostream &os) const
Write the single value, or the first value from field.
Definition: exprResult.C:628
dictionary dict
exprResult & operator+=(const exprResult &b)
Addition of results.
Definition: exprResult.C:697
Field< Type > & ref()
Return non-const reference to the field.
void allowReset() noexcept
Change reset behaviour.
Definition: exprResult.H:548
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
#define exprResult_Construct(Type)
Definition: exprResult.H:424
bool isUniform() const
True if single, uniform value.
Definition: exprResultI.H:207
A polymorphic field/result from evaluating an expression.
Definition: exprResult.H:121
tmp< Field< Type > > getResult(bool cacheCopy=false)
Return tmp field of the contents, optionally keeping a copy in cache.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void clear()
Clear (zero) the result.
Definition: exprResult.C:364
bool isType() const
True if valueType corresponds to the given Type.
Definition: exprResultI.H:214
Tensor< scalar > tensor
Definition: symmTensor.H:57
A traits class, which is primarily used for primitives and vector-space.
Definition: pTraits.H:75
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:1049
const word & valueType() const noexcept
Basic type for the field or single value.
Definition: exprResultI.H:192
virtual ~exprResult()
Destructor.
Definition: exprResult.C:336
Field< Type > & constCast() const
Return non-const reference to the field, casting away constness.
const void * dataAddress() const
The address of the field data content.
Definition: exprResult.C:810
static autoPtr< exprResult > New(const dictionary &dict)
Return a reference to the selected value driver.
Definition: exprResult.C:272
const Field< Type > & cref() const
Return const reference to the field.
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
void writeField(Ostream &os, const word &keyword="") const
Write the field, optionally as an entry.
Definition: exprResult.C:596
Generic templated field type.
Definition: Field.H:62
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
SymmTensor< scalar > symmTensor
SymmTensor of scalars, i.e. SymmTensor<scalar>.
Definition: symmTensor.H:55
A class for handling words, derived from Foam::string.
Definition: word.H:63
Istream & operator>>(Istream &, directionInfo &)
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:404
exprResult()
Default construct.
Definition: exprResult.C:173
const Field< Type > * getField() const
Return a read pointer to the field data if the type matches, nullptr otherwise. Can generally be cons...
Vector< scalar > vector
Definition: vector.H:57
void testIfSingleValue(const bool parRun=Pstream::parRun())
Test if field corresponds to a single-value and thus uniform.
Definition: exprResult.C:440
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
OBJstream os(runTime.globalPath()/outputName)
void needsReset(bool val)
Adjusts the internal needsReset value.
Definition: exprResult.H:324
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))
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
label size() const
The field or object size.
Definition: exprResultI.H:252
TypeName("exprResult")
Runtime type information.
bool reset(bool force=false)
Reset at new timestep according to type.
Definition: exprResult.C:352
void setSingleValue(const Type &val)
Set single-value uniform result.
Definition: exprResultI.H:392
void setResult(Field< Type > *, bool wantPointData=false)
Set result field, taking ownership of the pointer.
Definition: exprResultI.H:330
exprResult & operator*=(const scalar &b)
Scalar multiplication.
Definition: exprResult.C:662
virtual void operator=(const exprResult &rhs)
Copy assignment.
Definition: exprResult.C:467
static const exprResult null
An empty result.
Definition: exprResult.H:332
virtual autoPtr< exprResult > clone() const
Clone.
Definition: exprResult.H:456
void writeEntry(const word &keyword, Ostream &os) const
Forwarding to Field::writeEntry.
Definition: exprResult.C:528
virtual expressions::exprResult & target()
Simulate virtual templated methods.
Definition: exprResult.H:307
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
bool isPointData(const bool wantPointData=true) const
True if representing point data, or test for same value as wantPointData argument.
Definition: exprResultI.H:199
Macros to ease declaration of run-time selection tables.
SphericalTensor< scalar > sphericalTensor
SphericalTensor of scalars, i.e. SphericalTensor<scalar>.
declareRunTimeSelectionTable(autoPtr, exprResult, dictionary,(const dictionary &dict),(dict))
friend class exprResultGlobals
Friendship with globals.
Definition: exprResult.H:337
A class for managing temporary objects.
Definition: HashPtrTable.H:50
static autoPtr< T > New(Args &&... args)
Construct autoPtr with forwarding arguments.
Definition: autoPtr.H:178
void writeDict(Ostream &os, const bool subDict=true) const
Write entry as dictionary contents.
Definition: exprResult.C:552
Type getValue() const
Return a single value when isUniform() is true, or Zero when it is non-uniform or if the type mismatc...
Definition: exprResultI.H:221
void noReset() noexcept
Change reset behaviour.
Definition: exprResult.H:543
virtual void resetImpl()
Reset at new timestep according to the derived class type.
Definition: exprResult.C:346
Namespace for OpenFOAM.