/*---------------------------------------------------------------------------*\ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2013 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 . \*---------------------------------------------------------------------------*/ #include "PstreamReduceOps.H" #include "FieldReuseFunctions.H" #define TEMPLATE template #include "FieldFunctionsM.C" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam { /* * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * */ template void component ( Field::cmptType>& res, const UList& f, const direction d ) { typedef typename Field::cmptType cmptType; TFOR_ALL_F_OP_F_FUNC_S ( cmptType, res, =, Type, f, .component, const direction, d ) } template void T(Field& res, const UList& f) { TFOR_ALL_F_OP_F_FUNC(Type, res, =, Type, f, T) } template void pow ( Field::type>& res, const UList& vf ) { typedef typename powProduct::type powProductType; TFOR_ALL_F_OP_FUNC_F_S ( powProductType, res, =, pow, Type, vf, powProductType, pTraits::zero ) } template tmp::type> > pow ( const UList& f, typename powProduct::type ) { typedef typename powProduct::type powProductType; tmp > tRes ( new Field(f.size()) ); pow(tRes(), f); return tRes; } template tmp::type> > pow ( const tmp >& tf, typename powProduct::type ) { typedef typename powProduct::type powProductType; tmp > tRes = reuseTmp::New(tf); pow(tRes(), tf()); reuseTmp::clear(tf); return tRes; } template void sqr ( Field::type>& res, const UList& vf ) { typedef typename outerProduct::type outerProductType; TFOR_ALL_F_OP_FUNC_F(outerProductType, res, =, sqr, Type, vf) } template tmp::type> > sqr(const UList& f) { typedef typename outerProduct::type outerProductType; tmp > tRes ( new Field(f.size()) ); sqr(tRes(), f); return tRes; } template tmp::type> > sqr(const tmp >& tf) { typedef typename outerProduct::type outerProductType; tmp > tRes = reuseTmp::New(tf); sqr(tRes(), tf()); reuseTmp::clear(tf); return tRes; } template void magSqr(Field& res, const UList& f) { TFOR_ALL_F_OP_FUNC_F(scalar, res, =, magSqr, Type, f) } template tmp > magSqr(const UList& f) { tmp > tRes(new Field(f.size())); magSqr(tRes(), f); return tRes; } template tmp > magSqr(const tmp >& tf) { tmp > tRes = reuseTmp::New(tf); magSqr(tRes(), tf()); reuseTmp::clear(tf); return tRes; } template void mag(Field& res, const UList& f) { TFOR_ALL_F_OP_FUNC_F(scalar, res, =, mag, Type, f) } template tmp > mag(const UList& f) { tmp > tRes(new Field(f.size())); mag(tRes(), f); return tRes; } template tmp > mag(const tmp >& tf) { tmp > tRes = reuseTmp::New(tf); mag(tRes(), tf()); reuseTmp::clear(tf); return tRes; } template void cmptMax(Field::cmptType>& res, const UList& f) { typedef typename Field::cmptType cmptType; TFOR_ALL_F_OP_FUNC_F(cmptType, res, =, cmptMax, Type, f) } template tmp::cmptType> > cmptMax(const UList& f) { typedef typename Field::cmptType cmptType; tmp > tRes(new Field(f.size())); cmptMax(tRes(), f); return tRes; } template tmp::cmptType> > cmptMax(const tmp >& tf) { typedef typename Field::cmptType cmptType; tmp > tRes = reuseTmp::New(tf); cmptMax(tRes(), tf()); reuseTmp::clear(tf); return tRes; } template void cmptMin(Field::cmptType>& res, const UList& f) { typedef typename Field::cmptType cmptType; TFOR_ALL_F_OP_FUNC_F(cmptType, res, =, cmptMin, Type, f) } template tmp::cmptType> > cmptMin(const UList& f) { typedef typename Field::cmptType cmptType; tmp > tRes(new Field(f.size())); cmptMin(tRes(), f); return tRes; } template tmp::cmptType> > cmptMin(const tmp >& tf) { typedef typename Field::cmptType cmptType; tmp > tRes = reuseTmp::New(tf); cmptMin(tRes(), tf()); reuseTmp::clear(tf); return tRes; } template void cmptAv(Field::cmptType>& res, const UList& f) { typedef typename Field::cmptType cmptType; TFOR_ALL_F_OP_FUNC_F(cmptType, res, =, cmptAv, Type, f) } template tmp::cmptType> > cmptAv(const UList& f) { typedef typename Field::cmptType cmptType; tmp > tRes(new Field(f.size())); cmptAv(tRes(), f); return tRes; } template tmp::cmptType> > cmptAv(const tmp >& tf) { typedef typename Field::cmptType cmptType; tmp > tRes = reuseTmp::New(tf); cmptAv(tRes(), tf()); reuseTmp::clear(tf); return tRes; } template void cmptMag(Field& res, const UList& f) { TFOR_ALL_F_OP_FUNC_F(Type, res, =, cmptMag, Type, f) } template tmp > cmptMag(const UList& f) { tmp > tRes(new Field(f.size())); cmptMag(tRes(), f); return tRes; } template tmp > cmptMag(const tmp >& tf) { tmp > tRes = reuseTmp::New(tf); cmptMag(tRes(), tf()); reuseTmp::clear(tf); return tRes; } #define TMP_UNARY_FUNCTION(ReturnType, Func) \ \ template \ ReturnType Func(const tmp >& tf1) \ { \ ReturnType res = Func(tf1()); \ tf1.clear(); \ return res; \ } template Type max(const UList& f) { if (f.size()) { Type Max(f[0]); TFOR_ALL_S_OP_FUNC_F_S(Type, Max, =, max, Type, f, Type, Max) return Max; } else { return pTraits::min; } } TMP_UNARY_FUNCTION(Type, max) template Type min(const UList& f) { if (f.size()) { Type Min(f[0]); TFOR_ALL_S_OP_FUNC_F_S(Type, Min, =, min, Type, f, Type, Min) return Min; } else { return pTraits::max; } } TMP_UNARY_FUNCTION(Type, min) template Type sum(const UList& f) { if (f.size()) { Type Sum = pTraits::zero; TFOR_ALL_S_OP_F(Type, Sum, +=, Type, f) return Sum; } else { return pTraits::zero; } } TMP_UNARY_FUNCTION(Type, sum) template Type maxMagSqr(const UList& f) { if (f.size()) { Type Max(f[0]); TFOR_ALL_S_OP_FUNC_F_S ( Type, Max, =, maxMagSqrOp(), Type, f, Type, Max ) return Max; } else { return pTraits::min; } } TMP_UNARY_FUNCTION(Type, maxMagSqr) template Type minMagSqr(const UList& f) { if (f.size()) { Type Min(f[0]); TFOR_ALL_S_OP_FUNC_F_S ( Type, Min, =, minMagSqrOp(), Type, f, Type, Min ) return Min; } else { return pTraits::max; } } TMP_UNARY_FUNCTION(Type, minMagSqr) template scalar sumProd(const UList& f1, const UList& f2) { if (f1.size() && (f1.size() == f2.size())) { scalar SumProd = 0.0; TFOR_ALL_S_OP_F_OP_F(scalar, SumProd, +=, Type, f1, &&, Type, f2) return SumProd; } else { return 0.0; } } template Type sumCmptProd(const UList& f1, const UList& f2) { if (f1.size() && (f1.size() == f2.size())) { Type SumProd = pTraits::zero; TFOR_ALL_S_OP_FUNC_F_F ( Type, SumProd, +=, cmptMultiply, Type, f1, Type, f2 ) return SumProd; } else { return pTraits::zero; } } template scalar sumSqr(const UList& f) { if (f.size()) { scalar SumSqr = 0.0; TFOR_ALL_S_OP_FUNC_F(scalar, SumSqr, +=, sqr, Type, f) return SumSqr; } else { return 0.0; } } TMP_UNARY_FUNCTION(scalar, sumSqr) template scalar sumMag(const UList& f) { if (f.size()) { scalar SumMag = 0.0; TFOR_ALL_S_OP_FUNC_F(scalar, SumMag, +=, mag, Type, f) return SumMag; } else { return 0.0; } } TMP_UNARY_FUNCTION(scalar, sumMag) template Type sumCmptMag(const UList& f) { if (f.size()) { Type SumMag = pTraits::zero; TFOR_ALL_S_OP_FUNC_F(scalar, SumMag, +=, cmptMag, Type, f) return SumMag; } else { return pTraits::zero; } } TMP_UNARY_FUNCTION(Type, sumCmptMag) template Type average(const UList& f) { if (f.size()) { Type avrg = sum(f)/f.size(); return avrg; } else { WarningIn("average(const UList&)") << "empty field, returning zero" << endl; return pTraits::zero; } } TMP_UNARY_FUNCTION(Type, average) #define G_UNARY_FUNCTION(ReturnType, gFunc, Func, rFunc) \ \ template \ ReturnType gFunc(const UList& f, const int comm) \ { \ ReturnType res = Func(f); \ reduce(res, rFunc##Op(), Pstream::msgType(), comm); \ return res; \ } \ TMP_UNARY_FUNCTION(ReturnType, gFunc) G_UNARY_FUNCTION(Type, gMax, max, max) G_UNARY_FUNCTION(Type, gMin, min, min) G_UNARY_FUNCTION(Type, gSum, sum, sum) G_UNARY_FUNCTION(Type, gMaxMagSqr, maxMagSqr, maxMagSqr) G_UNARY_FUNCTION(Type, gMinMagSqr, minMagSqr, minMagSqr) G_UNARY_FUNCTION(scalar, gSumSqr, sumSqr, sum) G_UNARY_FUNCTION(scalar, gSumMag, sumMag, sum) G_UNARY_FUNCTION(Type, gSumCmptMag, sumCmptMag, sum) #undef G_UNARY_FUNCTION template scalar gSumProd ( const UList& f1, const UList& f2, const int comm ) { scalar SumProd = sumProd(f1, f2); reduce(SumProd, sumOp(), Pstream::msgType(), comm); return SumProd; } template Type gSumCmptProd ( const UList& f1, const UList& f2, const int comm ) { Type SumProd = sumCmptProd(f1, f2); reduce(SumProd, sumOp(), Pstream::msgType(), comm); return SumProd; } template Type gAverage ( const UList& f, const int comm ) { label n = f.size(); Type s = sum(f); sumReduce(s, n, Pstream::msgType(), comm); if (n > 0) { Type avrg = s/n; return avrg; } else { WarningIn("gAverage(const UList&)") << "empty field, returning zero." << endl; return pTraits::zero; } } TMP_UNARY_FUNCTION(Type, gAverage) #undef TMP_UNARY_FUNCTION BINARY_FUNCTION(Type, Type, Type, max) BINARY_FUNCTION(Type, Type, Type, min) BINARY_FUNCTION(Type, Type, Type, cmptMultiply) BINARY_FUNCTION(Type, Type, Type, cmptDivide) BINARY_TYPE_FUNCTION(Type, Type, Type, max) BINARY_TYPE_FUNCTION(Type, Type, Type, min) BINARY_TYPE_FUNCTION(Type, Type, Type, cmptMultiply) BINARY_TYPE_FUNCTION(Type, Type, Type, cmptDivide) /* * * * * * * * * * * * * * * * Global operators * * * * * * * * * * * * * */ UNARY_OPERATOR(Type, Type, -, negate) BINARY_OPERATOR(Type, Type, scalar, *, multiply) BINARY_OPERATOR(Type, scalar, Type, *, multiply) BINARY_OPERATOR(Type, Type, scalar, /, divide) BINARY_TYPE_OPERATOR_SF(Type, scalar, Type, *, multiply) BINARY_TYPE_OPERATOR_FS(Type, Type, scalar, *, multiply) BINARY_TYPE_OPERATOR_FS(Type, Type, scalar, /, divide) // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // #define PRODUCT_OPERATOR(product, Op, OpFunc) \ \ template \ void OpFunc \ ( \ Field::type>& res, \ const UList& f1, \ const UList& f2 \ ) \ { \ typedef typename product::type productType; \ TFOR_ALL_F_OP_F_OP_F(productType, res, =, Type1, f1, Op, Type2, f2) \ } \ \ template \ tmp::type> > \ operator Op(const UList& f1, const UList& f2) \ { \ typedef typename product::type productType; \ tmp > tRes(new Field(f1.size())); \ OpFunc(tRes(), f1, f2); \ return tRes; \ } \ \ template \ tmp::type> > \ operator Op(const UList& f1, const tmp >& tf2) \ { \ typedef typename product::type productType; \ tmp > tRes = reuseTmp::New(tf2); \ OpFunc(tRes(), f1, tf2()); \ reuseTmp::clear(tf2); \ return tRes; \ } \ \ template \ tmp::type> > \ operator Op(const tmp >& tf1, const UList& f2) \ { \ typedef typename product::type productType; \ tmp > tRes = reuseTmp::New(tf1); \ OpFunc(tRes(), tf1(), f2); \ reuseTmp::clear(tf1); \ return tRes; \ } \ \ template \ tmp::type> > \ operator Op(const tmp >& tf1, const tmp >& tf2) \ { \ typedef typename product::type productType; \ tmp > tRes = \ reuseTmpTmp::New(tf1, tf2); \ OpFunc(tRes(), tf1(), tf2()); \ reuseTmpTmp::clear(tf1, tf2); \ return tRes; \ } \ \ template \ void OpFunc \ ( \ Field::type>& res, \ const UList& f1, \ const VectorSpace& vs \ ) \ { \ typedef typename product::type productType; \ TFOR_ALL_F_OP_F_OP_S \ (productType, res, =,Type, f1, Op, Form, static_cast(vs))\ } \ \ template \ tmp::type> > \ operator Op(const UList& f1, const VectorSpace& vs) \ { \ typedef typename product::type productType; \ tmp > tRes(new Field(f1.size())); \ OpFunc(tRes(), f1, static_cast(vs)); \ return tRes; \ } \ \ template \ tmp::type> > \ operator Op \ ( \ const tmp >& tf1, \ const VectorSpace& vs \ ) \ { \ typedef typename product::type productType; \ tmp > tRes = reuseTmp::New(tf1); \ OpFunc(tRes(), tf1(), static_cast(vs)); \ reuseTmp::clear(tf1); \ return tRes; \ } \ \ template \ void OpFunc \ ( \ Field::type>& res, \ const VectorSpace& vs, \ const UList& f1 \ ) \ { \ typedef typename product::type productType; \ TFOR_ALL_F_OP_S_OP_F \ (productType, res, =,Form,static_cast(vs), Op, Type, f1) \ } \ \ template \ tmp::type> > \ operator Op(const VectorSpace& vs, const UList& f1) \ { \ typedef typename product::type productType; \ tmp > tRes(new Field(f1.size())); \ OpFunc(tRes(), static_cast(vs), f1); \ return tRes; \ } \ \ template \ tmp::type> > \ operator Op \ ( \ const VectorSpace& vs, const tmp >& tf1 \ ) \ { \ typedef typename product::type productType; \ tmp > tRes = reuseTmp::New(tf1); \ OpFunc(tRes(), static_cast(vs), tf1()); \ reuseTmp::clear(tf1); \ return tRes; \ } PRODUCT_OPERATOR(typeOfSum, +, add) PRODUCT_OPERATOR(typeOfSum, -, subtract) PRODUCT_OPERATOR(outerProduct, *, outer) PRODUCT_OPERATOR(crossProduct, ^, cross) PRODUCT_OPERATOR(innerProduct, &, dot) PRODUCT_OPERATOR(scalarProduct, &&, dotdot) #undef PRODUCT_OPERATOR // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // #include "undefFieldFunctionsM.H" // ************************************************************************* //