Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
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{
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 };
71EqOp(eq, x = y)
72EqOp(plusEq, x += y)
73EqOp(minusEq, x -= y)
74EqOp(multiplyEq, x *= y)
75EqOp(divideEq, x /= y)
76EqOp(eqMag, x = mag(y))
77EqOp(eqSqr, x = sqr(y))
78EqOp(eqMagSqr, x = magSqr(y))
79EqOp(plusEqMagSqr, x += magSqr(y))
80EqOp(minEq, x = min(x, y))
81EqOp(maxEq, x = max(x, y))
82EqOp(minMagSqrEq, x = (magSqr(x) <= magSqr(y) ? x : y))
83EqOp(maxMagSqrEq, x = (magSqr(x) >= magSqr(y) ? x : y))
85EqOp(andEq, x = (x && y))
86EqOp(orEq, x = (x || y))
87EqOp(xorEq, x = (x != y))
88EqOp(bitAndEq, x &= y)
89EqOp(bitOrEq, x |= y)
90EqOp(bitXorEq, x ^= y)
91
92EqOp(eqMinus, x = -y)
93
94EqOp(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 }; \
208Op(plus, x + y)
209Op(minus, x - y)
219Op(maxMagSqr, (magSqr(x) >= magSqr(y) ? x : y))
222Op(bitAnd, (x & y))
223Op(bitOr, (x | y))
224Op(bitXor, (x ^ y))
226BoolOp(and, x && y)
227BoolOp(or, x || y)
228BoolOp(xor, (!x) != (!y)) // With forced bool context
229BoolOp(equal, x == y)
232BoolOp(lessEqual, x <= y)
233BoolOp(greater, x > y)
234BoolOp(greaterEqual, x >= y)
236BoolOp(lessEq, x <= y) // OpenFOAM-v2112 and earlier
237BoolOp(greaterEq, x >= y) // OpenFOAM-v2112 and earlier
239Bool1Op(equal, x == value)
241Bool1Op(less, x < value)
242Bool1Op(lessEqual, x <= value)
243Bool1Op(greater, x > value)
244Bool1Op(greaterEqual, x >= value)
245
246WeightedOp(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.
260template<class T>
261struct 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)
271template<class T>
272struct lerpOp
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
282template<class T>
283struct lerpOp1
285 const scalar value;
287 lerpOp1(scalar v) : value(v) {}
289 [[nodiscard]] T operator()(const T& a, const T& b) const
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
300template<class T> struct getNameOp : nameOp<T> {};
301
302//- Deprecated(2020-11) use typeOp (word.H)
303// \deprecated(2020-11) use typeOp
304template<class T> struct getTypeOp : typeOp<T> {};
305
306
307// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
308
309} // End namespace Foam
310
311// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
312
313#endif
314
315// ************************************************************************* //
scalar y
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:40
dimensionedSymmTensor sqr(const dimensionedVector &dv)
Scalar minMod(const Scalar s1, const Scalar s2)
Definition scalarImpl.H:378
Scalar cmptPow(const Scalar s1, const Scalar s2)
Definition scalarImpl.H:456
bool equal(const T &a, const T &b)
Compare two values for equality.
Definition label.H:180
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
void divide(DimensionedField< Type, GeoMesh > &result, const DimensionedField< Type, GeoMesh > &f1, const DimensionedField< scalar, GeoMesh > &f2)
Type minMagSqr(const UList< Type > &f1)
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:26
dimensionedScalar stabilise(const dimensionedScalar &x, const dimensionedScalar &y)
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &f1, const label comm)
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)
bool notEqual(const Scalar a, const Scalar b)
Definition scalarImpl.H:350
Type maxMagSqr(const UList< Type > &f1)
void multiply(DimensionedField< Type, GeoMesh > &result, const DimensionedField< Type, GeoMesh > &f1, const DimensionedField< scalar, GeoMesh > &f2)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
dimensioned< Type > lerp(const dimensioned< Type > &a, const dimensioned< Type > &b, const scalar t)
#define WeightedOp(opName, op)
Definition ops.H:173
#define Op(opName, op)
Definition ops.H:99
#define BoolOp(opName, op)
Definition ops.H:131
#define EqOp(opName, op)
Definition ops.H:44
#define Bool1Op(opName, op)
Definition ops.H:155
volScalarField & b
Three-way comparison operation of two parameters,.
Definition ops.H:259
int operator()(const T &a, const T &b) const
Definition ops.H:260
const T & value
Definition ops.H:233
Deprecated(2020-11) use nameOp (word.H).
Definition ops.H:304
Deprecated(2020-11) use typeOp (word.H).
Definition ops.H:311
const T & value
Definition ops.H:238
const T & value
Definition ops.H:237
Linear interpolation (lerp) with interpolation value defined on construct.
Definition ops.H:285
const scalar value
Definition ops.H:286
lerpOp1(scalar v)
Definition ops.H:288
T operator()(const T &a, const T &b) const
Definition ops.H:290
Linear interpolation (cf. std::lerp).
Definition ops.H:272
T operator()(const T &a, const T &b, const scalar t) const
Definition ops.H:273
const T & value
Definition ops.H:236
const T & value
Definition ops.H:235
Extract name (as a word) from an object, typically using its name() method.
Definition word.H:341
const T & value
Definition ops.H:234
Extract type (as a word) from an object, typically using its type() method.
Definition word.H:362