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-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
27InNamespace
28 Foam
29
30Description
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
42namespace 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
71EqOp(eq, x = y)
72EqOp(plusEq, x += y)
73EqOp(minusEq, x -= y)
74EqOp(multiplyEq, x *= y)
75EqOp(divideEq, x /= y)
76EqOp(eqSqr, x = sqr(y))
77EqOp(eqMag, x = mag(y))
78EqOp(eqMagSqr, x = magSqr(y))
79EqOp(plusEqMagSqr, x += magSqr(y))
80EqOp(maxEq, x = max(x, y))
81EqOp(minEq, x = min(x, y))
82EqOp(minMagSqrEq, x = (magSqr(x) <= magSqr(y) ? x : y))
83EqOp(maxMagSqrEq, x = (magSqr(x) >= magSqr(y) ? x : y))
84
85EqOp(andEq, x = (x && y))
86EqOp(orEq, x = (x || y))
87EqOp(xorEq, x = (x != y))
88EqOp(bitAndEq, x = (x & y))
89EqOp(bitOrEq, x = (x | y))
90EqOp(bitXorEq, x = (x ^ y))
91
92EqOp(eqMinus, x = -y)
93
94EqOp(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
214
215Op(plus, x + y)
216Op(minus, x - y)
228
229Op(bitAnd, (x & y))
230Op(bitOr, (x | y))
231Op(bitXor, (x ^ y))
232
233BoolOp(and, x && y)
234BoolOp(or, x || y)
235BoolOp(xor, (!x) != (!y)) // With forced bool context
239BoolOp(lessEqual, x <= y)
240BoolOp(greater, x > y)
241BoolOp(greaterEqual, x >= y)
242
243BoolOp(lessEq, x <= y) // OpenFOAM-v2112 and earlier
244BoolOp(greaterEq, x >= y) // OpenFOAM-v2112 and earlier
245
246Bool1Op(equal, x == value)
248Bool1Op(less, x < value)
249Bool1Op(lessEqual, x <= value)
250Bool1Op(greater, x > value)
251Bool1Op(greaterEqual, x >= value)
252
254
255#undef Op
256#undef BoolOp
257#undef Bool1Op
258#undef WeightedOp
259
260// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
261
262//- Three-way comparison operation of two parameters,
263// similar to the \c <=> operator in C++20.
264//
265// \return a negative value for less, a positive value for greater,
266// and zero for equal value.
267template<class T>
269{
270 int operator()(const T& a, const T& b) const WARNRETURN
271 {
272 return (a < b) ? -1 : (b < a) ? 1 : 0;
273 }
274};
275
276
277//- Deprecated(2020-11) use nameOp (word.H)
278// \deprecated(2020-11) use nameOp
279template<class T> struct getNameOp : nameOp<T> {};
280
281//- Deprecated(2020-11) use typeOp (word.H)
282// \deprecated(2020-11) use typeOp
283template<class T> struct getTypeOp : typeOp<T> {};
284
285
286// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
287
288#undef WARNRETURN
289
290// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
291
292} // End namespace Foam
293
294// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
295
296#endif
297
298// ************************************************************************* //
scalar y
const volScalarField & T
Namespace for OpenFOAM.
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
bool notEqual(const Scalar s1, const Scalar s2)
Definition: Scalar.H:291
dimensionedSymmTensor sqr(const dimensionedVector &dv)
Scalar minMod(const Scalar s1, const Scalar s2)
Definition: Scalar.H:303
Scalar cmptPow(const Scalar s1, const Scalar s2)
Definition: Scalar.H:369
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
Type maxMagSqr(const UList< Type > &f)
bool equal(const T &s1, const T &s2)
Compare two values for equality.
Definition: doubleFloat.H:46
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
static bool less(const vector &x, const vector &y)
To compare normals.
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
void multiply(FieldField< Field, Type > &f, const FieldField< Field, Type > &f1, const FieldField< Field, scalar > &f2)
dimensionedScalar stabilise(const dimensionedScalar &x, const dimensionedScalar &y)
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)
Type minMagSqr(const UList< Type > &f)
void divide(FieldField< Field, Type > &f, const FieldField< Field, Type > &f1, const FieldField< Field, scalar > &f2)
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
#define WARNRETURN
Definition: ops.H:105
#define WeightedOp(opName, op)
Definition: ops.H:186
#define Op(opName, op)
Definition: ops.H:112
#define BoolOp(opName, op)
Definition: ops.H:144
#define EqOp(opName, op)
Definition: ops.H:50
#define Bool1Op(opName, op)
Definition: ops.H:168
volScalarField & b
Definition: createFields.H:27
Three-way comparison operation of two parameters,.
Definition: ops.H:269
int operator()(const T &a, const T &b) const
Definition: ops.H:270
Deprecated(2020-11) use nameOp (word.H)
Definition: ops.H:279
Deprecated(2020-11) use typeOp (word.H)
Definition: ops.H:283
Extract name (as a word) from an object, typically using its name() method.
Definition: word.H:238
Extract type (as a word) from an object, typically using its type() method.
Definition: word.H:255