ops.H 11.7 KB
Newer Older
Henry's avatar
Henry committed
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
Henry Weller's avatar
Henry Weller committed
5
    \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
Mark Olesen's avatar
Mark Olesen committed
6
     \\/     M anipulation  | Copyright (C) 2018-2019 OpenCFD Ltd.
Henry's avatar
Henry committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

24 25
InNamespace
    Foam
Henry's avatar
Henry committed
26 27

Description
28 29 30
    Various function objects for unary and binary operations.
    Can be used for parallel combine-reduce operations or other places
    requiring a function object.
Henry's avatar
Henry committed
31 32 33 34 35 36 37 38 39 40 41 42 43

\*---------------------------------------------------------------------------*/

#ifndef ops_H
#define ops_H

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

namespace Foam
{

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

44 45 46 47
// Assignment operation taking two parameters, returning void.
// Alters the value of the first parameter.
//     Eg, plusEqOp for (x += y)

48 49
#define EqOp(opName, op)                                                       \
                                                                               \
50 51
    template<class T1, class T2>                                               \
    struct opName##Op2                                                         \
52
    {                                                                          \
53 54 55 56 57
        void operator()(T1& x, const T2& y) const                              \
        {                                                                      \
            op;                                                                \
        }                                                                      \
    };                                                                         \
58
                                                                               \
59 60
    template<class T>                                                          \
    struct opName##Op                                                          \
61
    {                                                                          \
62 63 64 65 66 67
        void operator()(T& x, const T& y) const                                \
        {                                                                      \
            op;                                                                \
        }                                                                      \
    };

Henry's avatar
Henry committed
68 69 70 71 72 73

EqOp(eq, x = y)
EqOp(plusEq, x += y)
EqOp(minusEq, x -= y)
EqOp(multiplyEq, x *= y)
EqOp(divideEq, x /= y)
Henry Weller's avatar
Henry Weller committed
74
EqOp(eqSqr, x = sqr(y))
Henry's avatar
Henry committed
75 76 77 78
EqOp(eqMag, x = mag(y))
EqOp(plusEqMagSqr, x += magSqr(y))
EqOp(maxEq, x = max(x, y))
EqOp(minEq, x = min(x, y))
79 80
EqOp(minMagSqrEq, x = (magSqr(x) <= magSqr(y) ? x : y))
EqOp(maxMagSqrEq, x = (magSqr(x) >= magSqr(y) ? x : y))
Mark Olesen's avatar
Mark Olesen committed
81

Henry's avatar
Henry committed
82
EqOp(andEq, x = (x && y))
83
EqOp(orEq,  x = (x || y))
Mark Olesen's avatar
Mark Olesen committed
84 85
EqOp(bitAndEq, x = (x & y))
EqOp(bitOrEq,  x = (x | y))
Henry's avatar
Henry committed
86 87 88 89 90 91 92 93 94 95

EqOp(eqMinus, x = -y)

EqOp(nopEq, (void)x)

#undef EqOp


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

96
// Warning about unused result
Henry's avatar
Henry committed
97 98 99 100 101 102
#if __GNUC__
#define WARNRETURN __attribute__((warn_unused_result))
#else
#define WARNRETURN
#endif

103 104 105 106
// Operation taking two parameters, returning the first type.
// Neither parameter is altered.
//     Eg, plusOp for (x + y)

107 108 109
#define Op(opName, op)                                                         \
                                                                               \
    template<class T, class T1, class T2>                                      \
110
    struct opName##Op3                                                         \
111 112 113 114 115 116 117 118
    {                                                                          \
        T operator()(const T1& x, const T2& y) const WARNRETURN                \
        {                                                                      \
            return op;                                                         \
        }                                                                      \
    };                                                                         \
                                                                               \
    template<class T1, class T2>                                               \
119
    struct opName##Op2                                                         \
120 121 122 123 124 125 126 127
    {                                                                          \
        T1 operator()(const T1& x, const T2& y) const WARNRETURN               \
        {                                                                      \
            return op;                                                         \
        }                                                                      \
    };                                                                         \
                                                                               \
    template<class T>                                                          \
128
    struct opName##Op                                                          \
129 130 131 132 133
    {                                                                          \
        T operator()(const T& x, const T& y) const WARNRETURN                  \
        {                                                                      \
            return op;                                                         \
        }                                                                      \
Henry's avatar
Henry committed
134 135 136
    };


137
// Operations taking two parameters (unaltered), returning bool
138

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
#define BoolOp(opName, op)                                                     \
                                                                               \
    template<class T1, class T2>                                               \
    struct opName##Op2                                                         \
    {                                                                          \
        bool operator()(const T1& x, const T2& y) const WARNRETURN             \
        {                                                                      \
            return op;                                                         \
        }                                                                      \
    };                                                                         \
                                                                               \
    template<class T>                                                          \
    struct opName##Op                                                          \
    {                                                                          \
        bool operator()(const T& x, const T& y) const WARNRETURN               \
        {                                                                      \
            return op;                                                         \
        }                                                                      \
    };


// Weighting operations

#define WeightedOp(opName, op)                                                 \
163
                                                                               \
164
    template<class T, class CombineOp>                                         \
165 166 167 168
    class opName##WeightedOp                                                   \
    {                                                                          \
        const CombineOp& cop_;                                                 \
                                                                               \
169
    public:                                                                    \
170
                                                                               \
171 172 173 174
        opName##WeightedOp(const CombineOp& cop)                               \
        :                                                                      \
            cop_(cop)                                                          \
        {}                                                                     \
175
                                                                               \
176 177 178 179 180 181 182 183 184 185
        void operator()                                                        \
        (                                                                      \
            T& x,                                                              \
            const label index,                                                 \
            const T& y,                                                        \
            const scalar weight                                                \
        ) const                                                                \
        {                                                                      \
            cop_(x, op);                                                       \
        }                                                                      \
186
    };                                                                         \
Henry's avatar
Henry committed
187 188 189 190 191 192 193 194 195


Op(sum, x + y)

Op(plus, x + y)
Op(minus, x - y)
Op(multiply, x * y)
Op(divide, x / y)
Op(cmptMultiply, cmptMultiply(x, y))
196
Op(cmptPow, cmptPow(x, y))
Henry's avatar
Henry committed
197 198 199 200 201 202 203 204
Op(cmptDivide, cmptDivide(x, y))
Op(stabilise, stabilise(x, y))
Op(max, max(x, y))
Op(min, min(x, y))
Op(minMagSqr, (magSqr(x)<=magSqr(y) ? x : y))
Op(maxMagSqr, (magSqr(x)>=magSqr(y) ? x : y))
Op(minMod, minMod(x, y))

Mark Olesen's avatar
Mark Olesen committed
205 206 207
Op(bitOr, (x | y))
Op(bitAnd, (x & y))

208 209 210 211 212 213 214 215 216 217
BoolOp(and, x && y)
BoolOp(or, x || y)
BoolOp(equal, x == y)
BoolOp(notEqual, x != y)
BoolOp(less, x < y)
BoolOp(lessEq, x <= y)
BoolOp(greater, x > y)
BoolOp(greaterEq, x >= y)

WeightedOp(multiply, (weight*y))
Henry's avatar
Henry committed
218 219

#undef Op
220 221
#undef BoolOp
#undef WeightedOp
222 223 224

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
//- Three-way comparison operation of two parameters,
//  similar to the \c <=> operator in C++20.
//
//  \return a negative value for less, a positive value for greater,
//      and zero for equal value.
template<class T>
struct compareOp
{
    int operator()(const T& a, const T& b) const WARNRETURN
    {
        return (a < b) ? -1 : (b < a) ? 1 : 0;
    }
};


240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
//- General get operation to extract the 'name' from an object as a word.
//  The default implementation uses the 'name()' method commonly used within
//  OpenFOAM.
template<class T>
struct getNameOp
{
    word operator()(const T& x) const WARNRETURN
    {
        return x.name();
    }
};


//- General get operation to extract the 'type' from an object as a word.
//  The default implementation uses the 'type()' method commonly used within
//  OpenFOAM.
template<class T>
struct getTypeOp
{
    word operator()(const T& x) const WARNRETURN
    {
        return x.type();
    }
};


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

Henry's avatar
Henry committed
268 269 270 271 272 273 274 275 276 277 278
#undef WARNRETURN

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

} // End namespace Foam

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

#endif

// ************************************************************************* //