/*---------------------------------------------------------------------------*\ ========= | \\ / 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 . 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) \ FUNC(Func, Op, const X&, tmp& x, x) \ FUNC(Func, Op, const X&, tmp&& x, x) #define UnaryFunctionTemplate(Func, Op, XPar, XArg, XVar) \ template \ < \ class X, \ class = EnableIfExpressionable> \ > \ UnaryExpression Func(XArg) \ { \ return UnaryExpression(XVar); \ } \ #define UnaryEagerFunctionTemplate(Func, Op, XPar, XArg, XVar) \ \ UnaryFunctionTemplate(Func##_, Op, XPar, XArg, XVar) \ \ template \ < \ class X, \ class = EnableIfExpressionable> \ > \ tmp>> Func(XArg) \ { \ return eval(Func##_(Eval::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::precedence; \ \ template \ 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> \ ElementType Func \ ( \ const Expression& e, \ const bool globalReduce = ReduceType::globalReduce, \ const label comm = UPstream::worldComm \ ) \ { \ return ReduceType::template value \ ( \ e, globalReduce, comm \ ); \ } \ \ template> \ ElementType Func(const tmp& te) \ { \ auto x = Func(te()); \ te.clear(); \ return x; \ } \ \ template> \ ElementType gFunc \ ( \ const Expression& e, \ const label comm = UPstream::worldComm \ ) \ { \ return Func(e, true, comm); \ } \ \ template> \ ElementType gFunc(const tmp& 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> ElementType average ( const Expression& e, const bool globalReduce = ReduceType::globalReduce, const label comm = UPstream::worldComm ) { return ReduceType::average(e, globalReduce, comm); } template> ElementType average(const tmp& te) { auto x = average(te()); te.clear(); return x; } template> ElementType gAverage ( const Expression& e, const label comm = UPstream::worldComm ) { return average(e, true, comm); } template> ElementType gAverage ( const tmp& te, const label comm = UPstream::worldComm ) { auto x = gAverage(te()); te.clear(); return x; } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // Weighted average template < class Expression, class WeightExpression, class = EnableIfExpressionable > ElementType weightedAverage ( const Expression& e, const WeightExpression& we, const bool globalReduce = ReduceType::globalReduce, const label comm = UPstream::worldComm ) { return ReduceType::weightedAverage ( e, we, globalReduce, comm ); } template < class Expression, class WeightExpression, class = EnableIfExpressionable > ElementType weightedAverage ( const tmp& te, const WeightExpression& we ) { auto x = weightedAverage(te(), we); te.clear(); return x; } template < class Expression, class WeightExpression, class = EnableIfExpressionable > ElementType 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 > ElementType gWeightedAverage ( const tmp& 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) \ FUNC(Func, Op, APar, AArg, AVar, const B&, tmp& b, b) \ FUNC(Func, Op, APar, AArg, AVar, const B&, tmp&& 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) \ BinaryFunction__(FUNC, Func, Op, const A&, tmp& a, a) \ BinaryFunction__(FUNC, Func, Op, const A&, tmp&& a, a) #define BinaryFunctionTemplate(Func, Op, APar, AArg, AVar, BPar, BArg, BVar) \ template \ < \ class A, \ class B, \ class = EnableIfExpressionable> \ > \ BinaryExpression Func(AArg, BArg) \ { \ return BinaryExpression(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> \ > \ tmp>> Func(AArg, BArg) \ { \ return eval(Func##_(Eval::eval(AVar), Eval::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 // ************************************************************************* //