ops.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) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2018-2025 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 InNamespace
28  Foam
29 
30 Description
31  Various functors for unary and binary operations.
32  Can be used for parallel combine-reduce operations or other places
33  requiring a functor.
34 
35 \*---------------------------------------------------------------------------*/
36 
37 #ifndef Foam_ops_H
38 #define Foam_ops_H
39 
40 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
41 
42 namespace Foam
43 {
44 
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 
47 // Assignment operation taking two parameters, returning void.
48 // Alters the value of the first parameter.
49 // Eg, plusEqOp for (x += y)
50 #define EqOp(opName, op) \
51  \
52  template<class T1, class T2> \
53  struct opName##Op2 \
54  { \
55  void operator()(T1& x, const T2& y) const \
56  { \
57  op; \
58  } \
59  }; \
60  \
61  template<class T> \
62  struct opName##Op \
63  { \
64  void operator()(T& x, const T& y) const \
65  { \
66  op; \
67  } \
68  };
69 
70 
71 EqOp(eq, x = y)
72 EqOp(plusEq, x += y)
73 EqOp(minusEq, x -= y)
74 EqOp(multiplyEq, x *= y)
75 EqOp(divideEq, x /= y)
76 EqOp(eqMag, x = mag(y))
77 EqOp(eqSqr, x = sqr(y))
78 EqOp(eqMagSqr, x = magSqr(y))
79 EqOp(plusEqMagSqr, x += magSqr(y))
80 EqOp(minEq, x = min(x, y))
81 EqOp(maxEq, x = max(x, y))
82 EqOp(minMagSqrEq, x = (magSqr(x) <= magSqr(y) ? x : y))
83 EqOp(maxMagSqrEq, x = (magSqr(x) >= magSqr(y) ? x : y))
84 
85 EqOp(andEq, x = (x && y))
86 EqOp(orEq, x = (x || y))
87 EqOp(xorEq, x = (x != y))
88 EqOp(bitAndEq, x &= y)
89 EqOp(bitOrEq, x |= y)
90 EqOp(bitXorEq, x ^= y)
91 
92 EqOp(eqMinus, x = -y)
93 
94 EqOp(nopEq, (void)x)
95 
96 #undef EqOp
97 
98 
99 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
100 
101 // Operation taking two parameters, returning the first type.
102 // Neither parameter is altered.
103 // Eg, plusOp for (x + y)
104 
105 #define Op(opName, op) \
106  \
107  template<class T, class T1, class T2> \
108  struct opName##Op3 \
109  { \
110  [[nodiscard]] T operator()(const T1& x, const T2& y) const \
111  { \
112  return op; \
113  } \
114  }; \
115  \
116  template<class T1, class T2> \
117  struct opName##Op2 \
118  { \
119  [[nodiscard]] T1 operator()(const T1& x, const T2& y) const \
120  { \
121  return op; \
122  } \
123  }; \
124  \
125  template<class T> \
126  struct opName##Op \
127  { \
128  [[nodiscard]] T operator()(const T& x, const T& y) const \
129  { \
130  return op; \
131  } \
132  };
133 
134 
135 // Operations taking two parameters (unaltered), returning bool
136 
137 #define BoolOp(opName, op) \
138  \
139  template<class T1, class T2> \
140  struct opName##Op2 \
141  { \
142  [[nodiscard]] bool operator()(const T1& x, const T2& y) const \
143  { \
144  return op; \
145  } \
146  }; \
147  \
148  template<class T> \
149  struct opName##Op \
150  { \
151  [[nodiscard]] bool operator()(const T& x, const T& y) const \
152  { \
153  return op; \
154  } \
155  };
156 
157 
158 // Operations taking one parameter, returning bool.
159 // The comparison value is defined during construction
160 
161 #define Bool1Op(opName, op) \
162  \
163  template<class T> \
164  struct opName##Op1 \
165  { \
166  const T& value; \
167  \
168  opName##Op1(const T& v) : value(v) {} \
169  \
170  [[nodiscard]] bool operator()(const T& x) const \
171  { \
172  return op; \
173  } \
174  };
175 
176 
177 // Weighting operations
178 
179 #define WeightedOp(opName, op) \
180  \
181  template<class T, class CombineOp> \
182  class opName##WeightedOp \
183  { \
184  const CombineOp& cop_; \
185  \
186  public: \
187  \
188  opName##WeightedOp(const CombineOp& cop) \
189  : \
190  cop_(cop) \
191  {} \
192  \
193  void operator() \
194  ( \
195  T& x, \
196  const label index, \
197  const T& y, \
198  const scalar weight \
199  ) const \
200  { \
201  cop_(x, op); \
202  } \
203  }; \
206 Op(sum, x + y)
208 Op(plus, x + y)
209 Op(minus, x - y)
216 Op(min, min(x, y))
217 Op(max, max(x, y))
218 Op(minMagSqr, (magSqr(x) <= magSqr(y) ? x : y))
219 Op(maxMagSqr, (magSqr(x) >= magSqr(y) ? x : y))
222 Op(bitAnd, (x & y))
223 Op(bitOr, (x | y))
224 Op(bitXor, (x ^ y))
226 BoolOp(and, x && y)
227 BoolOp(or, x || y)
228 BoolOp(xor, (!x) != (!y)) // With forced bool context
229 BoolOp(equal, x == y)
232 BoolOp(lessEqual, x <= y)
233 BoolOp(greater, x > y)
234 BoolOp(greaterEqual, x >= y)
236 BoolOp(lessEq, x <= y) // OpenFOAM-v2112 and earlier
237 BoolOp(greaterEq, x >= y) // OpenFOAM-v2112 and earlier
239 Bool1Op(equal, x == value)
240 Bool1Op(notEqual, x != value)
241 Bool1Op(less, x < value)
242 Bool1Op(lessEqual, x <= value)
243 Bool1Op(greater, x > value)
244 Bool1Op(greaterEqual, x >= value)
245 
246 WeightedOp(multiply, (weight*y))
247 
248 #undef Op
249 #undef BoolOp
250 #undef Bool1Op
251 #undef WeightedOp
252 
253 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
254 
255 //- Three-way comparison operation of two parameters,
256 // similar to the \c <=> operator in C++20.
257 //
258 // \return a negative value for less, a positive value for greater,
259 // and zero for equal value.
260 template<class T>
261 struct compareOp
262 {
263  [[nodiscard]] int operator()(const T& a, const T& b) const
264  {
265  return (a < b) ? -1 : (b < a) ? 1 : 0;
266  }
267 };
268 
269 
270 //- Linear interpolation (cf. std::lerp)
271 template<class T>
272 struct lerpOp
273 {
274  [[nodiscard]] T operator()(const T& a, const T& b, const scalar t) const
275  {
276  return lerp(a, b, t);
277  }
278 };
279 
280 
281 //- Linear interpolation (lerp) with interpolation value defined on construct
282 template<class T>
283 struct lerpOp1
284 {
285  const scalar value;
287  lerpOp1(scalar v) : value(v) {}
289  [[nodiscard]] T operator()(const T& a, const T& b) const
290  {
291  return lerp(a, b, value);
292  }
293 };
294 
295 
296 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
297 
298 //- Deprecated(2020-11) use nameOp (word.H)
299 // \deprecated(2020-11) use nameOp
300 template<class T> struct getNameOp : nameOp<T> {};
301 
302 //- Deprecated(2020-11) use typeOp (word.H)
303 // \deprecated(2020-11) use typeOp
304 template<class T> struct getTypeOp : typeOp<T> {};
305 
306 
307 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
308 
309 } // End namespace Foam
310 
311 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
312 
313 #endif
314 
315 // ************************************************************************* //
const scalar value
Definition: ops.H:286
Deprecated(2020-11) use typeOp (word.H)
Definition: ops.H:311
void divide(DimensionedField< Type, GeoMesh > &result, const DimensionedField< Type, GeoMesh > &f1, const DimensionedField< scalar, GeoMesh > &f2)
T operator()(const T &a, const T &b, const scalar t) const
Definition: ops.H:273
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
bool equal(const T &a, const T &b)
Compare two values for equality.
Definition: label.H:164
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:40
dimensionedSymmTensor sqr(const dimensionedVector &dv)
#define BoolOp(opName, op)
Definition: ops.H:131
Type minMagSqr(const UList< Type > &f1)
Three-way comparison operation of two parameters,.
Definition: ops.H:258
#define Op(opName, op)
Definition: ops.H:99
dimensionedScalar stabilise(const dimensionedScalar &x, const dimensionedScalar &y)
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &f1, const label comm)
Type maxMagSqr(const UList< Type > &f1)
static bool less(const vector &x, const vector &y)
To compare normals.
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
bool notEqual(const Scalar a, const Scalar b)
Definition: scalarImpl.H:340
scalar y
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
#define EqOp(opName, op)
Definition: ops.H:44
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:26
#define Bool1Op(opName, op)
Definition: ops.H:155
Scalar cmptPow(const Scalar s1, const Scalar s2)
Definition: scalarImpl.H:446
Linear interpolation (cf. std::lerp)
Definition: ops.H:271
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)
dimensioned< Type > lerp(const dimensioned< Type > &a, const dimensioned< Type > &b, const scalar t)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
#define WeightedOp(opName, op)
Definition: ops.H:173
Scalar minMod(const Scalar s1, const Scalar s2)
Definition: scalarImpl.H:368
T operator()(const T &a, const T &b) const
Definition: ops.H:290
Linear interpolation (lerp) with interpolation value defined on construct.
Definition: ops.H:284
void multiply(DimensionedField< Type, GeoMesh > &result, const DimensionedField< Type, GeoMesh > &f1, const DimensionedField< scalar, GeoMesh > &f2)
int operator()(const T &a, const T &b) const
Definition: ops.H:260
lerpOp1(scalar v)
Definition: ops.H:288
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
Extract type (as a word) from an object, typically using its type() method.
Definition: word.H:361
Namespace for OpenFOAM.