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-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 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 #include "stdFoam.H" // For FOAM_NODISCARD
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 namespace Foam
45 {
46 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 // Assignment operation taking two parameters, returning void.
50 // Alters the value of the first parameter.
51 // Eg, plusEqOp for (x += y)
52 #define EqOp(opName, op) \
53  \
54  template<class T1, class T2> \
55  struct opName##Op2 \
56  { \
57  void operator()(T1& x, const T2& y) const \
58  { \
59  op; \
60  } \
61  }; \
62  \
63  template<class T> \
64  struct opName##Op \
65  { \
66  void operator()(T& x, const T& y) const \
67  { \
68  op; \
69  } \
70  };
71 
72 
73 EqOp(eq, x = y)
74 EqOp(plusEq, x += y)
75 EqOp(minusEq, x -= y)
76 EqOp(multiplyEq, x *= y)
77 EqOp(divideEq, x /= y)
78 EqOp(eqMag, x = mag(y))
79 EqOp(eqSqr, x = sqr(y))
80 EqOp(eqMagSqr, x = magSqr(y))
81 EqOp(plusEqMagSqr, x += magSqr(y))
82 EqOp(minEq, x = min(x, y))
83 EqOp(maxEq, x = max(x, y))
84 EqOp(minMagSqrEq, x = (magSqr(x) <= magSqr(y) ? x : y))
85 EqOp(maxMagSqrEq, x = (magSqr(x) >= magSqr(y) ? x : y))
86 
87 EqOp(andEq, x = (x && y))
88 EqOp(orEq, x = (x || y))
89 EqOp(xorEq, x = (x != y))
90 EqOp(bitAndEq, x &= y)
91 EqOp(bitOrEq, x |= y)
92 EqOp(bitXorEq, x ^= y)
93 
94 EqOp(eqMinus, x = -y)
95 
96 EqOp(nopEq, (void)x)
97 
98 #undef EqOp
99 
100 
101 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
102 
103 // Operation taking two parameters, returning the first type.
104 // Neither parameter is altered.
105 // Eg, plusOp for (x + y)
106 
107 #define Op(opName, op) \
108  \
109  template<class T, class T1, class T2> \
110  struct opName##Op3 \
111  { \
112  FOAM_NODISCARD T operator()(const T1& x, const T2& y) const \
113  { \
114  return op; \
115  } \
116  }; \
117  \
118  template<class T1, class T2> \
119  struct opName##Op2 \
120  { \
121  FOAM_NODISCARD T1 operator()(const T1& x, const T2& y) const \
122  { \
123  return op; \
124  } \
125  }; \
126  \
127  template<class T> \
128  struct opName##Op \
129  { \
130  FOAM_NODISCARD T operator()(const T& x, const T& y) const \
131  { \
132  return op; \
133  } \
134  };
135 
136 
137 // Operations taking two parameters (unaltered), returning bool
138 
139 #define BoolOp(opName, op) \
140  \
141  template<class T1, class T2> \
142  struct opName##Op2 \
143  { \
144  FOAM_NODISCARD bool operator()(const T1& x, const T2& y) const \
145  { \
146  return op; \
147  } \
148  }; \
149  \
150  template<class T> \
151  struct opName##Op \
152  { \
153  FOAM_NODISCARD bool operator()(const T& x, const T& y) const \
154  { \
155  return op; \
156  } \
157  };
158 
159 
160 // Operations taking one parameter, returning bool.
161 // The comparison value is defined during construction
162 
163 #define Bool1Op(opName, op) \
164  \
165  template<class T> \
166  struct opName##Op1 \
167  { \
168  const T& value; \
169  \
170  opName##Op1(const T& v) : value(v) {} \
171  \
172  FOAM_NODISCARD bool operator()(const T& x) const \
173  { \
174  return op; \
175  } \
176  };
177 
178 
179 // Weighting operations
180 
181 #define WeightedOp(opName, op) \
182  \
183  template<class T, class CombineOp> \
184  class opName##WeightedOp \
185  { \
186  const CombineOp& cop_; \
187  \
188  public: \
189  \
190  opName##WeightedOp(const CombineOp& cop) \
191  : \
192  cop_(cop) \
193  {} \
194  \
195  void operator() \
196  ( \
197  T& x, \
198  const label index, \
199  const T& y, \
200  const scalar weight \
201  ) const \
202  { \
203  cop_(x, op); \
204  } \
205  }; \
208 Op(sum, x + y)
210 Op(plus, x + y)
211 Op(minus, x - y)
218 Op(min, min(x, y))
219 Op(max, max(x, y))
220 Op(minMagSqr, (magSqr(x) <= magSqr(y) ? x : y))
221 Op(maxMagSqr, (magSqr(x) >= magSqr(y) ? x : y))
224 Op(bitAnd, (x & y))
225 Op(bitOr, (x | y))
226 Op(bitXor, (x ^ y))
228 BoolOp(and, x && y)
229 BoolOp(or, x || y)
230 BoolOp(xor, (!x) != (!y)) // With forced bool context
231 BoolOp(equal, x == y)
234 BoolOp(lessEqual, x <= y)
235 BoolOp(greater, x > y)
236 BoolOp(greaterEqual, x >= y)
238 BoolOp(lessEq, x <= y) // OpenFOAM-v2112 and earlier
239 BoolOp(greaterEq, x >= y) // OpenFOAM-v2112 and earlier
241 Bool1Op(equal, x == value)
242 Bool1Op(notEqual, x != value)
243 Bool1Op(less, x < value)
244 Bool1Op(lessEqual, x <= value)
245 Bool1Op(greater, x > value)
246 Bool1Op(greaterEqual, x >= value)
247 
248 WeightedOp(multiply, (weight*y))
249 
250 #undef Op
251 #undef BoolOp
252 #undef Bool1Op
253 #undef WeightedOp
254 
255 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
256 
257 //- Three-way comparison operation of two parameters,
258 // similar to the \c <=> operator in C++20.
259 //
260 // \return a negative value for less, a positive value for greater,
261 // and zero for equal value.
262 template<class T>
263 struct compareOp
264 {
265  FOAM_NODISCARD int operator()(const T& a, const T& b) const
266  {
267  return (a < b) ? -1 : (b < a) ? 1 : 0;
268  }
269 };
270 
271 
272 //- Linear interpolation (cf. std::lerp)
273 template<class T>
274 struct lerpOp
275 {
276  FOAM_NODISCARD T operator()(const T& a, const T& b, const scalar t) const
277  {
278  return lerp(a, b, t);
279  }
280 };
281 
282 
283 //- Linear interpolation (lerp) with interpolation value defined on construct
284 template<class T>
285 struct lerpOp1
286 {
287  const scalar value;
289  lerpOp1(scalar v) : value(v) {}
291  FOAM_NODISCARD T operator()(const T& a, const T& b) const
292  {
293  return lerp(a, b, value);
294  }
295 };
296 
297 
298 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
299 
300 //- Deprecated(2020-11) use nameOp (word.H)
301 // \deprecated(2020-11) use nameOp
302 template<class T> struct getNameOp : nameOp<T> {};
303 
304 //- Deprecated(2020-11) use typeOp (word.H)
305 // \deprecated(2020-11) use typeOp
306 template<class T> struct getTypeOp : typeOp<T> {};
307 
308 
309 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
310 
311 } // End namespace Foam
312 
313 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
314 
315 #endif
316 
317 // ************************************************************************* //
const scalar value
Definition: ops.H:288
Deprecated(2020-11) use typeOp (word.H)
Definition: ops.H:313
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &f1)
FOAM_NODISCARD T operator()(const T &a, const T &b, const scalar t) const
Definition: ops.H:275
void divide(DimensionedField< Type, GeoMesh > &result, const DimensionedField< Type, GeoMesh > &f1, const DimensionedField< scalar, GeoMesh > &f2)
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
#define FOAM_NODISCARD
Definition: stdFoam.H:63
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:133
Type minMagSqr(const UList< Type > &f1)
Three-way comparison operation of two parameters,.
Definition: ops.H:260
#define Op(opName, op)
Definition: ops.H:101
dimensionedScalar stabilise(const dimensionedScalar &x, const dimensionedScalar &y)
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: Scalar.H:343
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:46
FOAM_NODISCARD int operator()(const T &a, const T &b) const
Definition: ops.H:262
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:157
Scalar cmptPow(const Scalar s1, const Scalar s2)
Definition: Scalar.H:449
Linear interpolation (cf. std::lerp)
Definition: ops.H:273
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:175
FOAM_NODISCARD T operator()(const T &a, const T &b) const
Definition: ops.H:292
Scalar minMod(const Scalar s1, const Scalar s2)
Definition: Scalar.H:371
Includes some standard C++ headers, defines global macros and templates used in multiple places by Op...
Linear interpolation (lerp) with interpolation value defined on construct.
Definition: ops.H:286
void multiply(DimensionedField< Type, GeoMesh > &result, const DimensionedField< Type, GeoMesh > &f1, const DimensionedField< scalar, GeoMesh > &f2)
lerpOp1(scalar v)
Definition: ops.H:290
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.