introvoyz042's picture
Migrated from GitHub
5a600cf verified
Raw History Blame Contribute Delete
17.2 kB
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2026 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
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/>.
Description
Unary and binary functions and operators used by the expression
templating system
\*---------------------------------------------------------------------------*/
#ifndef expressions_H
#define expressions_H
#include "expressionFunctor.H"
#include "expressionReduce.H"
#include "PstreamReduceOps.H"
#include "expressionEval.H"
#include "UnaryExpression.H"
#include "BinaryExpression.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Define a unary function or operator given the global function or operator
// name and the operator class that it uses
#define UnaryFunction_(FUNC, Func, Op) \
FUNC(Func, Op, const X&, const X& x, x) \
FUNC(Func, Op, const X&, X& x, x) \
FUNC(Func, Op, X, X&& x, std::move(x)) \
FUNC(Func, Op, const X&, const tmp<X>& x, x) \
FUNC(Func, Op, const X&, tmp<X>& x, x) \
FUNC(Func, Op, const X&, tmp<X>&& x, x)
#define UnaryFunctionTemplate(Func, Op, XPar, XArg, XVar) \
template \
< \
class X, \
class = EnableIfExpressionable<UnaryExpression<X, Op>> \
> \
UnaryExpression<XPar, Op> Func(XArg) \
{ \
return UnaryExpression<XPar, Op>(XVar); \
} \
#define UnaryEagerFunctionTemplate(Func, Op, XPar, XArg, XVar) \
\
UnaryFunctionTemplate(Func##_, Op, XPar, XArg, XVar) \
\
template \
< \
class X, \
class = EnableIfExpressionable<UnaryExpression<X, Op>> \
> \
tmp<ResultType<UnaryExpression<XPar, Op>>> Func(XArg) \
{ \
return eval(Func##_(Eval<XPar>::eval(XVar))); \
} \
#define UnaryOperator(OpSym, Func) \
UnaryFunction_(UnaryFunctionTemplate, operator OpSym, Func##Op)
#define UnaryFunction(Func) \
UnaryFunction_(UnaryFunctionTemplate, Func, Func##Op)
#define UnaryEagerFunction(Func) \
UnaryFunction_(UnaryEagerFunctionTemplate, Func, Func##Op)
#define UnaryOp(Func) \
struct Func##Op \
{ \
static inline const char* prefix = #Func"("; \
static inline const char postfix = ')'; \
\
static const signed char precedence = \
expression::Operation<void>::precedence; \
\
template<class Type> \
static auto value(const Type& x) \
{ \
return Func(x); \
} \
};
#define UnaryOpFunction(Func) \
UnaryOp(Func) \
UnaryFunction(Func)
#define UnaryOpEagerFunction(Func) \
UnaryOp(Func) \
UnaryEagerFunction(Func)
// Promotion operator (i.e., unary plus). Has no effect. Just facilitates
// expressions with plus signs added for clarity.
UnaryOperator(+, promote)
// Negation operator (i.e., unary minus). Changes the sign of the operand.
UnaryOperator(-, negate)
UnaryFunction(sqr)
UnaryFunction(mag)
UnaryFunction(magSqr)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Define unary reduction functions
#define UnaryReduction_(Func, gFunc, Op) \
\
template<class Expression, class = EnableIfExpressionable<Expression>> \
ElementType<Expression> Func \
( \
const Expression& e, \
const bool globalReduce = ReduceType<Expression>::globalReduce, \
const label comm = UPstream::worldComm \
) \
{ \
return ReduceType<Expression>::template value<Op> \
( \
e, globalReduce, comm \
); \
} \
\
template<class Expression, class = EnableIfExpressionable<Expression>> \
ElementType<Expression> Func(const tmp<Expression>& te) \
{ \
auto x = Func(te()); \
te.clear(); \
return x; \
} \
\
template<class Expression, class = EnableIfExpressionable<Expression>> \
ElementType<Expression> gFunc \
( \
const Expression& e, \
const label comm = UPstream::worldComm \
) \
{ \
return Func(e, true, comm); \
} \
\
template<class Expression, class = EnableIfExpressionable<Expression>> \
ElementType<Expression> gFunc(const tmp<Expression>& te) \
{ \
auto x = gFunc(te()); \
te.clear(); \
return x; \
}
#define UnaryReduction(Func, gFunc) \
UnaryReduction_(Func, gFunc, Func##Op)
UnaryReduction(sum, gSum);
UnaryReduction(max, gMax);
UnaryReduction(min, gMin);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Define unary average and gAverage functions
template<class Expression, class = EnableIfExpressionable<Expression>>
ElementType<Expression> average
(
const Expression& e,
const bool globalReduce = ReduceType<Expression>::globalReduce,
const label comm = UPstream::worldComm
)
{
return ReduceType<Expression>::average(e, globalReduce, comm);
}
template<class Expression, class = EnableIfExpressionable<Expression>>
ElementType<Expression> average(const tmp<Expression>& te)
{
auto x = average(te());
te.clear();
return x;
}
template<class Expression, class = EnableIfExpressionable<Expression>>
ElementType<Expression> gAverage
(
const Expression& e,
const label comm = UPstream::worldComm
)
{
return average(e, true, comm);
}
template<class Expression, class = EnableIfExpressionable<Expression>>
ElementType<Expression> gAverage
(
const tmp<Expression>& te,
const label comm = UPstream::worldComm
)
{
auto x = gAverage(te());
te.clear();
return x;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Weighted average
template
<
class Expression,
class WeightExpression,
class = EnableIfExpressionable<Expression>
>
ElementType<Expression> weightedAverage
(
const Expression& e,
const WeightExpression& we,
const bool globalReduce = ReduceType<Expression>::globalReduce,
const label comm = UPstream::worldComm
)
{
return ReduceType<Expression>::weightedAverage
(
e,
we,
globalReduce,
comm
);
}
template
<
class Expression,
class WeightExpression,
class = EnableIfExpressionable<Expression>
>
ElementType<Expression> weightedAverage
(
const tmp<Expression>& te,
const WeightExpression& we
)
{
auto x = weightedAverage(te(), we);
te.clear();
return x;
}
template
<
class Expression,
class WeightExpression,
class = EnableIfExpressionable<Expression>
>
ElementType<Expression> gWeightedAverage
(
const Expression& e,
const WeightExpression& we,
const label comm = UPstream::worldComm
)
{
return weightedAverage(e, we, true, comm);
}
template
<
class Expression,
class WeightExpression,
class = EnableIfExpressionable<Expression>
>
ElementType<Expression> gWeightedAverage
(
const tmp<Expression>& te,
const WeightExpression& we,
const label comm = UPstream::worldComm
)
{
auto x = gWeightedAverage(te(), we);
te.clear();
return x;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Define a binary function or operator given the global function or operator
// name and the operator class that it uses
#define BinaryFunction__(FUNC, Func, Op, APar, AArg, AVar) \
FUNC(Func, Op, APar, AArg, AVar, const B&, const B& b, b) \
FUNC(Func, Op, APar, AArg, AVar, const B&, B& b, b) \
FUNC(Func, Op, APar, AArg, AVar, B, B&& b, std::move(b)) \
FUNC(Func, Op, APar, AArg, AVar, const B&, const tmp<B>& b, b) \
FUNC(Func, Op, APar, AArg, AVar, const B&, tmp<B>& b, b) \
FUNC(Func, Op, APar, AArg, AVar, const B&, tmp<B>&& b, b)
#define BinaryFunction_(FUNC, Func, Op) \
BinaryFunction__(FUNC, Func, Op, const A&, const A& a, a) \
BinaryFunction__(FUNC, Func, Op, const A&, A& a, a) \
BinaryFunction__(FUNC, Func, Op, A, A&& a, std::move(a)) \
BinaryFunction__(FUNC, Func, Op, const A&, const tmp<A>& a, a) \
BinaryFunction__(FUNC, Func, Op, const A&, tmp<A>& a, a) \
BinaryFunction__(FUNC, Func, Op, const A&, tmp<A>&& a, a)
#define BinaryFunctionTemplate(Func, Op, APar, AArg, AVar, BPar, BArg, BVar) \
template \
< \
class A, \
class B, \
class = EnableIfExpressionable<BinaryExpression<A, B, Op>> \
> \
BinaryExpression<APar, BPar, Op> Func(AArg, BArg) \
{ \
return BinaryExpression<APar, BPar, Op>(AVar, BVar); \
} \
#define BinaryEagerFunctionTemplate( \
Func, Op, APar, AArg, AVar, BPar, BArg, BVar) \
\
BinaryFunctionTemplate(Func##_, Op, APar, AArg, AVar, BPar, BArg, BVar) \
\
template \
< \
class A, \
class B, \
class = EnableIfExpressionable<BinaryExpression<A, B, Op>> \
> \
tmp<ResultType<BinaryExpression<APar, BPar, Op>>> Func(AArg, BArg) \
{ \
return eval(Func##_(Eval<APar>::eval(AVar), Eval<BPar>::eval(BVar))); \
} \
#define BinaryOperator(OpSym, Func) \
BinaryFunction_(BinaryFunctionTemplate, operator OpSym, Func##Op)
#define BinaryFunction(Func) \
BinaryFunction_(BinaryFunctionTemplate, Func, Func##Op)
#define BinaryEagerFunction(Func) \
BinaryFunction_(BinaryEagerFunctionTemplate, Func, Func##Op)
// Addition operator and sum function
BinaryOperator(+, add);
// Subtraction operator
BinaryOperator(-, subtract);
// Multiplication and/or outer-product operator
BinaryOperator(*, multiply);
// Division operator
BinaryOperator(/, divide);
// Inner product (dot) operator
BinaryOperator(&, inner);
// Cross product operator
BinaryOperator(^, cross);
// Maximum functions
BinaryFunction(max);
// Minimum function
BinaryFunction(min);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //