diff --git a/applications/test/complex/Test-complex.C b/applications/test/complex/Test-complex.C index 9f26407fd18ae6a34898cdda7524977c29c17030..da200a28645f49e03c815dee1e61de075467d973 100644 --- a/applications/test/complex/Test-complex.C +++ b/applications/test/complex/Test-complex.C @@ -97,6 +97,7 @@ int main(int argc, char *argv[]) c.Im() *= 5; } + Info<< "sumProd: " << sumProd(fld1, fld2) << nl; fld1 *= 10; Info<< "scalar multiply: " << flatOutput(fld1) << nl; diff --git a/src/OpenFOAM/fields/Fields/Field/FieldFunctions.C b/src/OpenFOAM/fields/Fields/Field/FieldFunctions.C index 51a1817f6c41b5e4c11753db1c9d9eb55b710e9c..e5f1df698084d2cdd6498ca83e31ce4c01f28b64 100644 --- a/src/OpenFOAM/fields/Fields/Field/FieldFunctions.C +++ b/src/OpenFOAM/fields/Fields/Field/FieldFunctions.C @@ -423,14 +423,17 @@ Type minMagSqr(const UList<Type>& f) TMP_UNARY_FUNCTION(Type, minMagSqr) template<class Type> -scalar sumProd(const UList<Type>& f1, const UList<Type>& f2) +typename pTraits<Type>::cmptType +sumProd(const UList<Type>& f1, const UList<Type>& f2) { - scalar SumProd = 0; + typedef typename pTraits<Type>::cmptType outType; + + outType result = Zero; if (f1.size() && (f1.size() == f2.size())) { - TFOR_ALL_S_OP_F_OP_F(scalar, SumProd, +=, Type, f1, &&, Type, f2) + TFOR_ALL_S_OP_F_OP_F(outType, result, +=, Type, f1, &&, Type, f2) } - return SumProd; + return result; } @@ -544,16 +547,18 @@ G_UNARY_FUNCTION(scalarMinMax, gMinMaxMag, minMaxMag, minMaxMag) template<class Type> -scalar gSumProd +typename pTraits<Type>::cmptType gSumProd ( const UList<Type>& f1, const UList<Type>& f2, const label comm ) { - scalar SumProd = sumProd(f1, f2); - reduce(SumProd, sumOp<scalar>(), Pstream::msgType(), comm); - return SumProd; + typedef typename pTraits<Type>::cmptType outType; + + outType result = sumProd(f1, f2); + reduce(result, sumOp<outType>(), Pstream::msgType(), comm); + return result; } template<class Type> @@ -658,7 +663,7 @@ tmp<Field<typename product<Type1, Type2>::type>> \ operator Op(const UList<Type1>& f1, const tmp<Field<Type2>>& tf2) \ { \ typedef typename product<Type1, Type2>::type productType; \ - auto tres = reuseTmp<productType, Type2>::New(tf2); \ + auto tres = reuseTmp<productType, Type2>::New(tf2); \ OpFunc(tres.ref(), f1, tf2()); \ tf2.clear(); \ return tres; \ @@ -669,7 +674,7 @@ tmp<Field<typename product<Type1, Type2>::type>> \ operator Op(const tmp<Field<Type1>>& tf1, const UList<Type2>& f2) \ { \ typedef typename product<Type1, Type2>::type productType; \ - auto tres = reuseTmp<productType, Type1>::New(tf1); \ + auto tres = reuseTmp<productType, Type1>::New(tf1); \ OpFunc(tres.ref(), tf1(), f2); \ tf1.clear(); \ return tres; \ diff --git a/src/OpenFOAM/fields/Fields/Field/FieldFunctions.H b/src/OpenFOAM/fields/Fields/Field/FieldFunctions.H index 246ad438a51de7d8f7f9bd5834a6b3f89c370560..3d917f28210672987b276a62cb94c991cd58a4ab 100644 --- a/src/OpenFOAM/fields/Fields/Field/FieldFunctions.H +++ b/src/OpenFOAM/fields/Fields/Field/FieldFunctions.H @@ -197,7 +197,11 @@ TMP_UNARY_FUNCTION(Type, minMagSqr) template<class Type> -scalar sumProd(const UList<Type>& f1, const UList<Type>& f2); +typename pTraits<Type>::cmptType sumProd +( + const UList<Type>& f1, + const UList<Type>& f2 +); template<class Type> Type sumCmptProd(const UList<Type>& f1, const UList<Type>& f2); @@ -244,7 +248,7 @@ G_UNARY_FUNCTION(scalarMinMax, gMinMaxMag, minMaxMag, minMaxMag) #undef G_UNARY_FUNCTION template<class Type> -scalar gSumProd +typename pTraits<Type>::cmptType gSumProd ( const UList<Type>& f1, const UList<Type>& f2, diff --git a/src/OpenFOAM/fields/Fields/complex/complexField.C b/src/OpenFOAM/fields/Fields/complex/complexField.C index 5662ab40763d26425db2041d6bf1eb9b5f9c857f..881745b4abf358b00ac798b280922cf6a87b08b6 100644 --- a/src/OpenFOAM/fields/Fields/complex/complexField.C +++ b/src/OpenFOAM/fields/Fields/complex/complexField.C @@ -132,6 +132,18 @@ Foam::scalarField Foam::Im(const UList<complex>& cf) namespace Foam { +template<> +complex sumProd(const UList<complex>& f1, const UList<complex>& f2) +{ + complex result = Zero; + if (f1.size() && (f1.size() == f2.size())) + { + TFOR_ALL_S_OP_F_OP_F(complex, result, +=, complex, f1, *, complex, f2) + } + return result; +} + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // UNARY_FUNCTION(complex, complex, pow3) diff --git a/src/OpenFOAM/fields/Fields/complex/complexField.H b/src/OpenFOAM/fields/Fields/complex/complexField.H index c9b9351364f672d9a133159619638b9a4a408978..51b147263c1345775751214e7a08f0155fc6ef18 100644 --- a/src/OpenFOAM/fields/Fields/complex/complexField.H +++ b/src/OpenFOAM/fields/Fields/complex/complexField.H @@ -74,6 +74,12 @@ scalarField Im(const UList<complex>& cf); //- Sum real and imag components scalarField ReImSum(const UList<complex>& cf); +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +//- Sum product +template<> +complex sumProd(const UList<complex>& f1, const UList<complex>& f2); + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/fields/Fields/scalarField/scalarField.C b/src/OpenFOAM/fields/Fields/scalarField/scalarField.C index 96b0e4a98215eedce4c6b076843acad2e72a0302..31c3dcd354d887cf3c5f7e0d08f6c54d98ad82cf 100644 --- a/src/OpenFOAM/fields/Fields/scalarField/scalarField.C +++ b/src/OpenFOAM/fields/Fields/scalarField/scalarField.C @@ -92,14 +92,26 @@ tmp<scalarField> stabilise(const tmp<scalarField>& tsf, const scalar s) // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // template<> -scalar sumProd(const UList<scalar>& f1, const UList<scalar>& f2) +float sumProd(const UList<float>& f1, const UList<float>& f2) { - scalar SumProd = 0.0; + float result = 0.0; if (f1.size() && (f1.size() == f2.size())) { - TFOR_ALL_S_OP_F_OP_F(scalar, SumProd, +=, scalar, f1, *, scalar, f2) + TFOR_ALL_S_OP_F_OP_F(float, result, +=, float, f1, *, float, f2) } - return SumProd; + return result; +} + + +template<> +double sumProd(const UList<double>& f1, const UList<double>& f2) +{ + double result = 0.0; + if (f1.size() && (f1.size() == f2.size())) + { + TFOR_ALL_S_OP_F_OP_F(double, result, +=, double, f1, *, double, f2) + } + return result; } diff --git a/src/OpenFOAM/fields/Fields/scalarField/scalarField.H b/src/OpenFOAM/fields/Fields/scalarField/scalarField.H index 4d029b4be9d1b51f74dd0a2880d71bcd76a77f22..1ea17a658b68a59388e3cf2d7c6ff0f6f3192432 100644 --- a/src/OpenFOAM/fields/Fields/scalarField/scalarField.H +++ b/src/OpenFOAM/fields/Fields/scalarField/scalarField.H @@ -76,8 +76,13 @@ tmp<scalarField> stabilise(const tmp<scalarField>&, const scalar s); // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +//- Sum product for float template<> -scalar sumProd(const UList<scalar>& f1, const UList<scalar>& f2); +float sumProd(const UList<float>& f1, const UList<float>& f2); + +//- Sum product for double +template<> +double sumProd(const UList<double>& f1, const UList<double>& f2); // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //