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-2021 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 ops_H
38 #define 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(eqSqr, x = sqr(y))
77 EqOp(eqMag, x = mag(y))
78 EqOp(eqMagSqr, x = magSqr(y))
79 EqOp(plusEqMagSqr, x += magSqr(y))
80 EqOp(maxEq, x = max(x, y))
81 EqOp(minEq, x = min(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 = (x & y))
89 EqOp(bitOrEq, x = (x | y))
90 EqOp(bitXorEq, x = (x ^ y))
91 
92 EqOp(eqMinus, x = -y)
93 
94 EqOp(nopEq, (void)x)
95 
96 #undef EqOp
97 
98 
99 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
100 
101 // Warning about unused result
102 #if __GNUC__
103 #define WARNRETURN __attribute__((warn_unused_result))
104 #else
105 #define WARNRETURN
106 #endif
107 
108 // Operation taking two parameters, returning the first type.
109 // Neither parameter is altered.
110 // Eg, plusOp for (x + y)
111 
112 #define Op(opName, op) \
113  \
114  template<class T, class T1, class T2> \
115  struct opName##Op3 \
116  { \
117  T operator()(const T1& x, const T2& y) const WARNRETURN \
118  { \
119  return op; \
120  } \
121  }; \
122  \
123  template<class T1, class T2> \
124  struct opName##Op2 \
125  { \
126  T1 operator()(const T1& x, const T2& y) const WARNRETURN \
127  { \
128  return op; \
129  } \
130  }; \
131  \
132  template<class T> \
133  struct opName##Op \
134  { \
135  T operator()(const T& x, const T& y) const WARNRETURN \
136  { \
137  return op; \
138  } \
139  };
140 
141 
142 // Operations taking two parameters (unaltered), returning bool
143 
144 #define BoolOp(opName, op) \
145  \
146  template<class T1, class T2> \
147  struct opName##Op2 \
148  { \
149  bool operator()(const T1& x, const T2& y) const WARNRETURN \
150  { \
151  return op; \
152  } \
153  }; \
154  \
155  template<class T> \
156  struct opName##Op \
157  { \
158  bool operator()(const T& x, const T& y) const WARNRETURN \
159  { \
160  return op; \
161  } \
162  };
163 
164 
165 // Operations taking one parameter, returning bool.
166 // The comparison value is defined during construction
167 
168 #define Bool1Op(opName, op) \
169  \
170  template<class T> \
171  struct opName##Op1 \
172  { \
173  const T& value; \
174  \
175  opName##Op1(const T& v) : value(v) {} \
176  \
177  bool operator()(const T& x) const WARNRETURN \
178  { \
179  return op; \
180  } \
181  };
182 
183 
184 // Weighting operations
185 
186 #define WeightedOp(opName, op) \
187  \
188  template<class T, class CombineOp> \
189  class opName##WeightedOp \
190  { \
191  const CombineOp& cop_; \
192  \
193  public: \
194  \
195  opName##WeightedOp(const CombineOp& cop) \
196  : \
197  cop_(cop) \
198  {} \
199  \
200  void operator() \
201  ( \
202  T& x, \
203  const label index, \
204  const T& y, \
205  const scalar weight \
206  ) const \
207  { \
208  cop_(x, op); \
209  } \
210  }; \
211 
212 
213 Op(sum, x + y)
214 
215 Op(plus, x + y)
216 Op(minus, x - y)
223 Op(max, max(x, y))
224 Op(min, min(x, y))
225 Op(minMagSqr, (magSqr(x) <= magSqr(y) ? x : y))
226 Op(maxMagSqr, (magSqr(x) >= magSqr(y) ? x : y))
228 
229 Op(bitAnd, (x & y))
230 Op(bitOr, (x | y))
231 Op(bitXor, (x ^ y))
232 
233 BoolOp(and, x && y)
234 BoolOp(or, x || y)
235 BoolOp(xor, (!x) != (!y)) // With forced bool context
239 BoolOp(lessEq, x <= y)
240 BoolOp(greater, x > y)
241 BoolOp(greaterEq, x >= y)
242 
243 Bool1Op(equal, x == value)
244 Bool1Op(notEqual, x != value)
245 Bool1Op(less, x < value)
246 Bool1Op(lessEq, x <= value)
247 Bool1Op(greater, x > value)
248 Bool1Op(greaterEq, x >= value)
249 
251 
252 #undef Op
253 #undef BoolOp
254 #undef Bool1Op
255 #undef WeightedOp
256 
257 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
258 
259 //- Three-way comparison operation of two parameters,
260 // similar to the \c <=> operator in C++20.
261 //
262 // \return a negative value for less, a positive value for greater,
263 // and zero for equal value.
264 template<class T>
265 struct compareOp
266 {
267  int operator()(const T& a, const T& b) const WARNRETURN
268  {
269  return (a < b) ? -1 : (b < a) ? 1 : 0;
270  }
271 };
272 
273 
274 //- Deprecated(2020-11) use nameOp (word.H)
275 // \deprecated(2020-11) use nameOp
276 template<class T> struct getNameOp : nameOp<T> {};
277 
278 //- Deprecated(2020-11) use typeOp (word.H)
279 // \deprecated(2020-11) use typeOp
280 template<class T> struct getTypeOp : typeOp<T> {};
281 
282 
283 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
284 
285 #undef WARNRETURN
286 
287 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
288 
289 } // End namespace Foam
290 
291 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
292 
293 #endif
294 
295 // ************************************************************************* //
Foam::getTypeOp
Deprecated(2020-11) use typeOp (word.H)
Definition: ops.H:280
EqOp
#define EqOp(opName, op)
Definition: ops.H:50
Foam::cmptPow
Scalar cmptPow(const Scalar s1, const Scalar s2)
Definition: Scalar.H:364
WeightedOp
#define WeightedOp(opName, op)
Definition: ops.H:186
Foam::cmptMultiply
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)
Foam::less
static bool less(const vector &x, const vector &y)
To compare normals.
Definition: meshRefinementRefine.C:57
Foam::compareOp
Three-way comparison operation of two parameters,.
Definition: ops.H:265
Bool1Op
#define Bool1Op(opName, op)
Definition: ops.H:168
Foam::min
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
Foam::magSqr
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
Foam::divide
void divide(FieldField< Field, Type > &f, const FieldField< Field, Type > &f1, const FieldField< Field, scalar > &f2)
Foam::maxMagSqr
Type maxMagSqr(const UList< Type > &f)
Definition: FieldFunctions.C:417
Foam::compareOp::operator()
int operator()(const T &a, const T &b) const
Definition: ops.H:267
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::stabilise
tmp< DimensionedField< scalar, GeoMesh > > stabilise(const DimensionedField< scalar, GeoMesh > &dsf, const dimensioned< scalar > &ds)
Definition: DimensionedScalarField.C:43
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::nameOp
Extract name (as a word) from an object, typically using its name() method.
Definition: word.H:237
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
BoolOp
#define BoolOp(opName, op)
Definition: ops.H:144
WARNRETURN
#define WARNRETURN
Definition: ops.H:105
Foam::getNameOp
Deprecated(2020-11) use nameOp (word.H)
Definition: ops.H:276
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::minMagSqr
Type minMagSqr(const UList< Type > &f)
Definition: FieldFunctions.C:442
Foam::cmptDivide
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
Foam::sqr
dimensionedSymmTensor sqr(const dimensionedVector &dv)
Definition: dimensionedSymmTensor.C:51
Foam::minMod
Scalar minMod(const Scalar s1, const Scalar s2)
Definition: Scalar.H:298
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::sum
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
Definition: DimensionedFieldFunctions.C:327
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::multiply
void multiply(FieldField< Field, Type > &f, const FieldField< Field, Type > &f1, const FieldField< Field, scalar > &f2)
Op
#define Op(opName, op)
Definition: ops.H:112
Foam::equal
bool equal(const T &s1, const T &s2)
Compare two values for equality.
Definition: doubleFloat.H:46
Foam::notEqual
bool notEqual(const Scalar s1, const Scalar s2)
Definition: Scalar.H:286
Foam::typeOp
Extract type (as a word) from an object, typically using its type() method.
Definition: word.H:248
y
scalar y
Definition: LISASMDCalcMethod1.H:14