exprValueFieldTag.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) 2023-2024 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Class
27  Foam::expressions::exprValueFieldTag
28 
29 Description
30  An expressions::exprValue (polymorphic typed union) with an additional
31  flag for tracking Field content as uniform etc.
32 
33 SourceFiles
34  exprValueFieldTag.C
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef Foam_expressions_exprValueFieldTag_H
39 #define Foam_expressions_exprValueFieldTag_H
40 
41 #include "exprValue.H"
42 #include "UList.H" // For ListPolicy
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
48 namespace expressions
49 {
50 
51 /*---------------------------------------------------------------------------*\
52  Class exprValueFieldTag Declaration
53 \*---------------------------------------------------------------------------*/
54 
56 {
57  // Private Data
58 
59  //- Uniformity of field (0: empty, 1: uniform, 2: non-uniform, ...)
60  // Values as per internal enum Foam::ListPolicy::uniformity
61  int uniformity_{};
62 
63  //- Representative (uniform) type/value for the field
64  expressions::exprValue value_{};
65 
66 
67 public:
68 
69  // Generated Methods
70 
71  //- Default construct. Uniformity = empty, type = none
72  exprValueFieldTag() = default;
73 
74  //- Copy construct
75  exprValueFieldTag(const exprValueFieldTag&) = default;
76 
77  //- Copy assignment
79 
80 
81  // Constructors
82 
83  //- Construct as uniform with the specified type/value
84  template<class Type>
85  explicit exprValueFieldTag(const Type& val)
86  {
87  set_value(val);
88  }
89 
90  //- Construct from a range of values
91  template<class Type>
92  explicit exprValueFieldTag(const Type* first, const Type* last)
93  {
94  set(first, last);
95  }
96 
97 
98  // Factory Methods
99 
100  //- Make an empty field tag with the specified type (zero-value)
101  template<class Type>
103  {
104  exprValueFieldTag tag; // construct empty, no type
105  tag.set_zero<Type>(); // set type and zero value
106  return tag;
107  }
108 
109 
110  // Member Functions
111 
112  //- True if the uniformity is "empty"
113  bool empty() const noexcept;
115  //- True if the uniformity is "uniform"
116  bool is_uniform() const noexcept;
117 
118  //- True if the uniformity is "non-uniform"
119  bool is_nonuniform() const noexcept;
120 
121  //- Representative (uniform) value for the field
122  const expressions::exprValue& value() const noexcept;
123 
124  //- Compare (uniformity, type, value)
125  int compare(const exprValueFieldTag& rhs) const;
126 
127  //- Test for equality of the values
128  bool equal(const exprValueFieldTag& rhs) const;
129 
130 
131  // Setters
132 
133  //- Set value and uniformity from range of data
134  template<class Type>
135  void set(const Type* first, const Type* last)
136  {
137  uniformity_ = Foam::ListPolicy::check_uniformity(first, last);
138 
139  if (first != last) // or uniformity_ != EMPTY
140  {
141  value_.set<Type>(*first);
142  }
143  else
144  {
145  // Is empty, set zero value
146  value_.set<Type>(Foam::zero{});
147  }
148  }
149 
150  //- Set as uniform, with specified value
151  template<class Type>
152  void set_value(const Type& val)
153  {
155  value_.set<Type>(val);
156  }
157 
158  //- Set type and zero value (does not affect uniformity)
159  template<class Type>
160  void set_zero()
161  {
162  value_.set<Type>(Foam::zero{});
163  }
164 
165  //- Set as empty with zero value, leave type unchanged
166  void set_empty();
167 
168  //- Set as non-uniform with zero value, leave type unchanged
169  void set_nouniform();
170 
171 
172  // Parallel
173 
174  //- Inplace parallel reduction, uses worldComm
175  void reduce();
176 
177  //- Perform a reduction on a copy and return the result
179 
181  // Reduction operations
182 
183  //- Inplace combine - eg, for global uniformity
184  void combine(const exprValueFieldTag& b);
185 
186  //- Binary combine operator, e.g. for global reduction
187  struct combineOp
188  {
189  exprValueFieldTag operator()
190  (
191  const exprValueFieldTag& a,
192  const exprValueFieldTag& b
193  ) const
194  {
195  exprValueFieldTag result(a);
196  result.combine(b);
197  return result;
198  }
199  };
200 
201 
202  // IO Operations
203 
204  //- Read uniformity label and the value as pair
205  void read(Istream& is);
206 
207  //- Write uniformity label and the value as pair
208  void write(Ostream& os) const;
209 
210  //- Print description to Ostream
211  void print(Ostream& os) const;
212 
213 
214  // Member Operators
215 
216  //- Assign from zero. Changes value but not type
217  void operator=(Foam::zero) { value_ = Foam::zero{}; }
218 
219  //- Compare (uniformity,value) for equality
220  bool operator==(const exprValueFieldTag&) const;
221 
222  //- Compare (uniformity,value) for inequality
223  bool operator!=(const exprValueFieldTag& rhs) const
224  {
225  return !(*this == rhs);
226  }
227 
228  //- Compare (uniformity,value)
229  bool operator<(const exprValueFieldTag&) const;
230 };
231 
232 } // End namespace expressions
233 
234 
235 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
236 
237 // IO
240 
241 
242 } // End namespace Foam
243 
244 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
245 
246 #endif
247 
248 // ************************************************************************* //
void set_empty()
Set as empty with zero value, leave type unchanged.
void write(Ostream &os) const
Write uniformity label and the value as pair.
exprValueFieldTag()=default
Default construct. Uniformity = empty, type = none.
A polymorphic typed union of simple primitive and VectorSpace types. It uses a &#39;fatter&#39; representatio...
Definition: exprValue.H:157
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
Binary combine operator, e.g. for global reduction.
int compare(const exprValueFieldTag &rhs) const
Compare (uniformity, type, value)
void set_nouniform()
Set as non-uniform with zero value, leave type unchanged.
static exprValueFieldTag make_empty()
Make an empty field tag with the specified type (zero-value)
enum uniformity check_uniformity(InputIt first, InputIt last)
Algorithm to determine list/container uniformity.
Definition: ListPolicy.H:267
bool set(const Type &val)
Assign from type. Returns false and sets to &#39;none&#39; for unsupported types.
Definition: exprValueI.H:169
void read(Istream &is)
Read uniformity label and the value as pair.
void set_value(const Type &val)
Set as uniform, with specified value.
void combine(const exprValueFieldTag &b)
Inplace combine - eg, for global uniformity.
static exprValueFieldTag returnReduce(const exprValueFieldTag &tag)
Perform a reduction on a copy and return the result.
const expressions::exprValue & value() const noexcept
Representative (uniform) value for the field.
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Istream & operator>>(Istream &, directionInfo &)
Container (non-empty) with identical values.
Definition: ListPolicy.H:258
exprValueFieldTag & operator=(const exprValueFieldTag &)=default
Copy assignment.
bool is_uniform() const noexcept
True if the uniformity is "uniform".
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: scalarImpl.H:255
bool empty() const noexcept
True if the uniformity is "empty".
void reduce()
Inplace parallel reduction, uses worldComm.
OBJstream os(runTime.globalPath()/outputName)
bool operator==(const exprValueFieldTag &) const
Compare (uniformity,value) for equality.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
bool is_nonuniform() const noexcept
True if the uniformity is "non-uniform".
bool operator!=(const exprValueFieldTag &rhs) const
Compare (uniformity,value) for inequality.
An expressions::exprValue (polymorphic typed union) with an additional flag for tracking Field conten...
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
bool operator<(const exprValueFieldTag &) const
Compare (uniformity,value)
void print(Ostream &os) const
Print description to Ostream.
bool equal(const exprValueFieldTag &rhs) const
Test for equality of the values.
void set(const Type *first, const Type *last)
Set value and uniformity from range of data.
Namespace for OpenFOAM.
void set_zero()
Set type and zero value (does not affect uniformity)