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-2022 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 "dimensionedType.H"
62 #include "runTimeSelectionTables.H"
63 
64 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
65 
66 namespace Foam
67 {
68 namespace expressions
69 {
70 
71 /*---------------------------------------------------------------------------*\
72  Class exprResult Declaration
73 \*---------------------------------------------------------------------------*/
74 
75 class 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 "
126  << pTraits<T>::typeName << nl;
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 "
137  << pTraits<T>::typeName << nl;
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 (primitive) entry
160  // \return True if the type check was satisfied
161  template<class Type>
162  inline bool readChecked
163  (
164  const entry& e,
165  const label len,
166  const bool uniform
167  );
168 
169  //- Type-checked retrieval of uniform field from current results
170  // \return True if the type check was satisfied
171  template<class Type>
172  bool getUniformChecked
173  (
174  exprResult& result,
175  const label size,
176  const bool noWarn,
177  const bool parRun
178  ) const;
179 
180  //- Type-checked retrieval of \c bool uniform field from current result
181  // \return True if the type check was satisfied
182  bool getUniformCheckedBool
183  (
184  exprResult& result,
185  const label size,
186  const bool noWarn,
187  const bool parRun
188  ) const;
189 
190  //- Type-checked determination of centre value (min/max)
191  // \return True if the type check was satisfied
192  template<class Type>
193  bool setAverageValueChecked(const bool parRun = Pstream::parRun());
194 
195  //- Type-checked determination of average bool value
196  // \return True if the type check was satisfied
197  bool setAverageValueCheckedBool(const bool parRun = Pstream::parRun());
198 
199  //- Type-checked copy of field
200  // \return True if the type check was satisfied
201  template<class Type>
202  bool duplicateFieldChecked(const void* ptr);
203 
204  //- Type-checked writing of the single value (uniform) entry
205  // \return True if the type check was satisfied
206  template<class Type>
207  bool writeSingleValueChecked(Ostream& os) const;
208 
209  //- Type-checked writing field as entry (if keyword is non-empty)
210  //- or as plain field (if keyword is empty)
211  // \return True if the type check was satisfied
212  template<class Type>
213  bool writeFieldChecked(const word& keyword, Ostream& os) const;
214 
215  //- Type-checked forwarding to Field::writeEntry
216  // \return True if the type check was satisfied
217  template<class Type>
218  bool writeEntryChecked(const word& keyword, Ostream& os) const;
219 
220  //- Type-checked field addition with another expression field
221  // \return True if the type check was satisfied
222  template<class Type>
223  bool plusEqChecked(const exprResult& b);
224 
225  //- Type-checked field multiplication with a scalar
226  // \return True if the type check was satisfied
227  template<class Type>
228  bool multiplyEqChecked(const scalar& b);
229 
230 
231  template<class Type>
232  inline void setResultImpl(Field<Type>*, bool wantPointData=false);
233 
234  template<class Type>
235  inline void setResultImpl(const Field<Type>&, bool wantPointData=false);
236 
237  template<class Type>
238  inline void setResultImpl(Field<Type>&&, bool wantPointData=false);
239 
240  template<class Type>
241  inline void setResultImpl(const Type& val, const label len);
242 
243  template<class Type>
244  inline void setSingleValueImpl(const Type& val);
245 
246 
247 protected:
248 
249  // Protected Member Functions
250 
251  //- Simulate virtual templated methods
252  inline virtual expressions::exprResult& target() { return *this; }
253 
254  //- Reset at new timestep according to the derived class type
255  virtual void resetImpl();
256 
257  //- Reset at new timestep according to type
258  // \return true if it was actually reset
259  bool reset(bool force=false);
260 
261  //- Adjusts the internal needsReset value
262  void needsReset(bool val) { needsReset_ = val; }
263 
264 
265 public:
266 
267  //- An empty result
268  static const exprResult null;
269 
270  //- Friendship with globals
271  friend class exprResultGlobals;
272 
273 
274  //- Runtime type information
275  TypeName("exprResult");
276 
278  (
279  autoPtr,
280  exprResult,
281  dictionary,
282  (
283  const dictionary& dict
284  ),
285  (dict)
286  );
288  (
289  autoPtr,
290  exprResult,
291  empty,
292  (),
293  ()
294  );
295 
296 
297  // Constructors
298 
299  //- Default construct
300  exprResult();
301 
302  //- Copy construct
303  exprResult(const exprResult& expr);
304 
305  //- Move construct
306  exprResult(exprResult&& expr);
307 
308  //- Construct from a dictionary
309  explicit exprResult
310  (
311  const dictionary& dict,
312  const bool uniform = false,
313  const bool needsValue = false
314  );
315 
316  //- Construct from Istream as dictionary content
317  explicit exprResult(Istream& is);
318 
319  //- Construct by copying a field
320  template<class Type>
321  explicit exprResult(const Field<Type>& fld);
322 
323  //- Construct by moving a field
324  template<class Type>
325  explicit exprResult(Field<Type>&& fld);
326 
327  //- Construct for an IOobject
328  template<class Type>
329  explicit exprResult(autoPtr<Type>&& obj);
330 
331  //- Construct from a dimensioned value
332  template<class Type>
333  explicit exprResult(const dimensioned<Type>& dt);
334 
335  #undef exprResult_Construct
336  #define exprResult_Construct(Type) \
337  \
338  explicit exprResult(const Type& val) : exprResult() \
339  { \
340  setSingleValue(val); \
341  }
342 
343  exprResult_Construct(bool);
344  exprResult_Construct(scalar);
349 
350  #undef exprResult_Construct
351 
352 
353  // Selectors
354 
355  //- Return a reference to the selected value driver
356  static autoPtr<exprResult> New(const dictionary& dict);
357 
358  //- Construct from Istream as dictionary content
359  static autoPtr<exprResult> New(Istream& is);
360 
361  //- Clone
362  virtual autoPtr<exprResult> clone() const
363  {
364  return autoPtr<exprResult>::New(*this);
365  }
367 
368  //- Destructor
369  virtual ~exprResult();
370 
371 
372  // Member Functions
373 
374  // Access
375 
376  //- Has a value?
377  inline bool hasValue() const;
378 
379  //- Basic type for the field or single value
380  inline const word& valueType() const noexcept;
381 
382  //- True if representing point data,
383  //- or test for same value as wantPointData argument
384  inline bool isPointData(const bool wantPointData=true) const;
385 
386  //- True if single, uniform value
387  inline bool isUniform() const;
388 
389  //- True if valueType corresponds to the given Type
390  template<class Type>
391  inline bool isType() const;
392 
393  //- Return a single value when isUniform() is true,
394  //- or Zero when it is non-uniform or if the type mismatches,
395  //- which means that it can generally be considered as failsafe.
396  template<class Type>
397  inline Type getValue() const;
398 
399  //- True if valueType is a bool
400  inline bool is_bool() const;
401 
402  //- The field or object size
403  inline label size() const;
404 
405  //- The address of the field data content.
406  // Fatal for unknown types.
407  // Used, for example, for python integration
408  const void* dataAddress() const;
409 
410 
411  // Edit
412 
413  //- Clear (zero) the result
414  void clear();
415 
416  //- Change reset behaviour
417  void noReset() noexcept { noReset_ = true; }
418 
419  //- Change reset behaviour
420  void allowReset() noexcept { noReset_ = false; }
421 
422  //- Test if field corresponds to a single-value and thus uniform.
423  // Uses field min/max to establish uniformity.
424  // Test afterwards with isUniform()
425  void testIfSingleValue(const bool parRun = Pstream::parRun());
426 
427 
428  // Set results
429 
430  //- Set result field, taking ownership of the pointer
431  template<class Type>
432  inline void setResult(Field<Type>*, bool wantPointData=false);
433 
434  //- Set result field, taking copy of the field contents
435  template<class Type>
436  inline void setResult(const Field<Type>&, bool wantPointData=false);
437 
438  //- Set result field, moving field contents
439  template<class Type>
440  inline void setResult(Field<Type>&&, bool wantPointData=false);
441 
442  //- Set uniform result field of given size
443  template<class Type>
444  inline void setResult(const Type& val, const label size);
445 
446  //- Set single-value uniform result
447  template<class Type>
448  inline void setSingleValue(const Type& val);
449 
450 
451  // Access/Get results
452 
453  //- Return const reference to the field
454  template<class Type>
455  inline const Field<Type>& cref() const;
456 
457  //- Return non-const reference to the field
458  template<class Type>
459  inline Field<Type>& ref();
460 
461  //- Return non-const reference to the field, casting away constness
462  template<class Type>
463  inline Field<Type>& constCast() const;
464 
465  //- Return tmp field of the contents,
466  //- optionally keeping a copy in cache
467  template<class Type>
468  inline tmp<Field<Type>> getResult(bool cacheCopy=false);
469 
470  //- Construct a uniform field from the current results
471  // Uses the field average. Optionally warning if the min/max
472  // deviation is larger than SMALL.
474  (
475  const label size,
476  const bool noWarn,
477  const bool parRun = Pstream::parRun()
478  ) const;
479 
480  //- Get a reduced result
481  template<template<class> class BinaryOp, class Type>
482  inline Type getReduced
483  (
484  const BinaryOp<Type>& bop,
485  const Type& initial = pTraits<Type>::zero
486  );
487 
489  // Write
491  //- Forwarding to Field::writeEntry
492  void writeEntry(const word& keyword, Ostream& os) const;
494  //- Write entry as dictionary contents
495  void writeDict(Ostream& os, const bool subDict=true) const;
496 
497  //- Write the field, optionally as an entry
498  void writeField(Ostream& os, const word& keyword = "") const;
499 
500  //- Write the single value, or the first value from field
501  void writeValue(Ostream& os) const;
502 
503 
504  // Member Operators
505 
506  //- Copy assignment
507  virtual void operator=(const exprResult& rhs);
508 
509  //- Move assignment
510  virtual void operator=(exprResult&& rhs);
511 
512 
513  //- Scalar multiplication
514  exprResult& operator*=(const scalar& b);
515 
516  //- Addition of results
518 };
519 
520 
521 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
522 
523 } // End namespace expressions
524 
525 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
526 
527 // Operators
528 
529 expressions::exprResult operator*
530 (
531  const scalar& a,
533 );
534 expressions::exprResult operator*
535 (
536  const expressions::exprResult& a,
537  const scalar& b
538 );
539 expressions::exprResult operator+
540 (
541  const expressions::exprResult& a,
543 );
544 
545 
546 // IO Operator
549 
550 
551 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
552 
553 } // End namespace Foam
554 
555 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
556 
557 #include "exprResultI.H"
558 
559 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
560 
561 #endif
562 
563 // ************************************************************************* //
bool hasValue() const
Has a value?
Definition: exprResultI.H:220
Type getReduced(const BinaryOp< Type > &bop, const Type &initial=pTraits< Type >::zero)
Get a reduced result.
Definition: exprResultI.H:720
bool is_bool() const
True if valueType is a bool.
Definition: exprResultI.H:267
void writeValue(Ostream &os) const
Write the single value, or the first value from field.
Definition: exprResult.C:657
dictionary dict
exprResult & operator+=(const exprResult &b)
Addition of results.
Definition: exprResult.C:725
Field< Type > & ref()
Return non-const reference to the field.
void allowReset() noexcept
Change reset behaviour.
Definition: exprResult.H:598
#define exprResult_Construct(Type)
Definition: exprResult.H:481
bool isUniform() const
True if single, uniform value.
Definition: exprResultI.H:242
A polymorphic field/result from evaluating an expression.
Definition: exprResult.H:120
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
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
void clear()
Clear (zero) the result.
Definition: exprResult.C:392
bool isType() const
True if valueType corresponds to the given Type.
Definition: exprResultI.H:249
Tensor< scalar > tensor
Definition: symmTensor.H:57
A traits class, which is primarily used for primitives.
Definition: pTraits.H:51
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:1004
const word & valueType() const noexcept
Basic type for the field or single value.
Definition: exprResultI.H:227
virtual ~exprResult()
Destructor.
Definition: exprResult.C:364
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:829
static autoPtr< exprResult > New(const dictionary &dict)
Return a reference to the selected value driver.
Definition: exprResult.C:300
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:625
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:432
exprResult()
Default construct.
Definition: exprResult.C:199
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:467
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
const direction noexcept
Definition: Scalar.H:258
OBJstream os(runTime.globalPath()/outputName)
Database for solution data, solver performance and other reduced data.
Definition: data.H:52
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
void needsReset(bool val)
Adjusts the internal needsReset value.
Definition: exprResult.H:383
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:76
label size() const
The field or object size.
Definition: exprResultI.H:273
TypeName("exprResult")
Runtime type information.
#define WarningInFunction
Report a warning using Foam::Warning.
bool reset(bool force=false)
Reset at new timestep according to type.
Definition: exprResult.C:380
void setSingleValue(const Type &val)
Set single-value uniform result.
Definition: exprResultI.H:415
void setResult(Field< Type > *, bool wantPointData=false)
Set result field, taking ownership of the pointer.
Definition: exprResultI.H:351
exprResult & operator*=(const scalar &b)
Scalar multiplication.
Definition: exprResult.C:691
virtual void operator=(const exprResult &rhs)
Copy assignment.
Definition: exprResult.C:494
static const exprResult null
An empty result.
Definition: exprResult.H:391
virtual autoPtr< exprResult > clone() const
Clone.
Definition: exprResult.H:513
void writeEntry(const word &keyword, Ostream &os) const
Forwarding to Field::writeEntry.
Definition: exprResult.C:557
virtual expressions::exprResult & target()
Simulate virtual templated methods.
Definition: exprResult.H:366
bool isPointData(const bool wantPointData=true) const
True if representing point data, or test for same value as wantPointData argument.
Definition: exprResultI.H:234
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:396
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:581
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:256
void noReset() noexcept
Change reset behaviour.
Definition: exprResult.H:593
virtual void resetImpl()
Reset at new timestep according to the derived class type.
Definition: exprResult.C:374
Namespace for OpenFOAM.