From 26a653305363b2f6e095b4cf64ec74c33d25246e Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 28 May 2019 14:48:18 +0200 Subject: [PATCH] ENH: FieldFunctions reduce on returnType, not input type (#1327) - for most functions the input type and return type are identical, but MinMax, sumMag are not. --- .../DimensionedFieldFunctions.C | 3 +- .../DimensionedFieldFunctions.H | 5 +- .../FieldField/FieldFieldFunctions.C | 10 +-- .../FieldField/FieldFieldFunctions.H | 8 ++- .../fields/Fields/Field/FieldFunctions.C | 62 ++++++++----------- .../fields/Fields/Field/FieldFunctions.H | 10 +-- .../GeometricField/GeometricFieldFunctions.C | 4 +- .../GeometricField/GeometricFieldFunctions.H | 8 +-- 8 files changed, 52 insertions(+), 58 deletions(-) diff --git a/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedFieldFunctions.C b/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedFieldFunctions.C index 603c78c574..2573a39afb 100644 --- a/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedFieldFunctions.C +++ b/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedFieldFunctions.C @@ -324,12 +324,13 @@ dimensioned func \ UNARY_REDUCTION_FUNCTION(Type, max, gMax) UNARY_REDUCTION_FUNCTION(Type, min, gMin) UNARY_REDUCTION_FUNCTION(Type, sum, gSum) -UNARY_REDUCTION_FUNCTION(scalar, sumMag, gSumMag) UNARY_REDUCTION_FUNCTION(Type, average, gAverage) UNARY_REDUCTION_FUNCTION(MinMax, minMax, gMinMax) UNARY_REDUCTION_FUNCTION(scalarMinMax, minMaxMag, gMinMaxMag) +UNARY_REDUCTION_FUNCTION(scalar, sumMag, gSumMag) + #undef UNARY_REDUCTION_FUNCTION diff --git a/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedFieldFunctions.H b/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedFieldFunctions.H index cebb4ada3e..198aa981eb 100644 --- a/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedFieldFunctions.H +++ b/src/OpenFOAM/fields/DimensionedFields/DimensionedField/DimensionedFieldFunctions.H @@ -102,6 +102,7 @@ tmp cmptAv(const tmp>& tdf); +// Forward to FieldFunction via dfunc() #define UNARY_REDUCTION_FUNCTION(returnType, func, dfunc) \ \ template \ @@ -118,12 +119,12 @@ dimensioned func \ UNARY_REDUCTION_FUNCTION(Type, max, gMax) UNARY_REDUCTION_FUNCTION(Type, min, gMin) UNARY_REDUCTION_FUNCTION(Type, sum, gSum) -UNARY_REDUCTION_FUNCTION(scalar, sumMag, gSumMag) UNARY_REDUCTION_FUNCTION(Type, average, gAverage) - UNARY_REDUCTION_FUNCTION(MinMax, minMax, gMinMax) UNARY_REDUCTION_FUNCTION(scalarMinMax, minMaxMag, gMinMaxMag) +UNARY_REDUCTION_FUNCTION(scalar, sumMag, gSumMag) + #undef UNARY_REDUCTION_FUNCTION diff --git a/src/OpenFOAM/fields/FieldFields/FieldField/FieldFieldFunctions.C b/src/OpenFOAM/fields/FieldFields/FieldField/FieldFieldFunctions.C index 146ae1193c..159c223198 100644 --- a/src/OpenFOAM/fields/FieldFields/FieldField/FieldFieldFunctions.C +++ b/src/OpenFOAM/fields/FieldFields/FieldField/FieldFieldFunctions.C @@ -554,13 +554,14 @@ scalarMinMax minMaxMag(const FieldField& f) TMP_UNARY_FUNCTION(scalarMinMax, minMaxMag) +// With reduction on ReturnType #define G_UNARY_FUNCTION(returnType, gFunc, func, rFunc) \ \ template class Field, class Type> \ returnType gFunc(const FieldField& f) \ { \ returnType res = func(f); \ - reduce(res, rFunc##Op()); \ + reduce(res, rFunc##Op()); \ return res; \ } \ TMP_UNARY_FUNCTION(returnType, gFunc) @@ -568,10 +569,11 @@ 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(scalar, gSumMag, sumMag, sum) -G_UNARY_FUNCTION(MinMax, gMinMax, minMax, minMax) -G_UNARY_FUNCTION(scalarMinMax, gMinMaxMag, minMaxMag, minMaxMag) +G_UNARY_FUNCTION(MinMax, gMinMax, minMax, sum) +G_UNARY_FUNCTION(scalarMinMax, gMinMaxMag, minMaxMag, sum) + +G_UNARY_FUNCTION(scalar, gSumMag, sumMag, sum) #undef G_UNARY_FUNCTION diff --git a/src/OpenFOAM/fields/FieldFields/FieldField/FieldFieldFunctions.H b/src/OpenFOAM/fields/FieldFields/FieldField/FieldFieldFunctions.H index 206b2fbeab..18be310352 100644 --- a/src/OpenFOAM/fields/FieldFields/FieldField/FieldFieldFunctions.H +++ b/src/OpenFOAM/fields/FieldFields/FieldField/FieldFieldFunctions.H @@ -238,6 +238,7 @@ scalarMinMax minMaxMag(const FieldField& f); TMP_UNARY_FUNCTION(scalarMinMax, minMaxMag) +// With reduction on ReturnType #define G_UNARY_FUNCTION(returnType, gFunc, func, rFunc) \ \ template class Field, class Type> \ @@ -247,10 +248,11 @@ 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(scalar, gSumMag, sumMag, sum) -G_UNARY_FUNCTION(MinMax, gMinMax, minMax, minMax) -G_UNARY_FUNCTION(scalarMinMax, gMinMaxMag, minMaxMag, minMaxMag) +G_UNARY_FUNCTION(MinMax, gMinMax, minMax, sum) +G_UNARY_FUNCTION(scalarMinMax, gMinMaxMag, minMaxMag, sum) + +G_UNARY_FUNCTION(scalar, gSumMag, sumMag, sum) #undef G_UNARY_FUNCTION diff --git a/src/OpenFOAM/fields/Fields/Field/FieldFunctions.C b/src/OpenFOAM/fields/Fields/Field/FieldFunctions.C index a5b9663992..9404946e07 100644 --- a/src/OpenFOAM/fields/Fields/Field/FieldFunctions.C +++ b/src/OpenFOAM/fields/Fields/Field/FieldFunctions.C @@ -317,10 +317,8 @@ Type max(const UList& f) TFOR_ALL_S_OP_FUNC_F_S(Type, Max, =, max, Type, f, Type, Max) return Max; } - else - { - return pTraits::min; - } + + return pTraits::min; } TMP_UNARY_FUNCTION(Type, max) @@ -334,10 +332,8 @@ Type min(const UList& f) TFOR_ALL_S_OP_FUNC_F_S(Type, Min, =, min, Type, f, Type, Min) return Min; } - else - { - return pTraits::max; - } + + return pTraits::max; } TMP_UNARY_FUNCTION(Type, min) @@ -351,10 +347,8 @@ Type sum(const UList& f) TFOR_ALL_S_OP_F(Type, Sum, +=, Type, f) return Sum; } - else - { - return Zero; - } + + return Zero; } TMP_UNARY_FUNCTION(Type, sum) @@ -387,10 +381,8 @@ Type maxMagSqr(const UList& f) ) return Max; } - else - { - return Zero; - } + + return Zero; } TMP_UNARY_FUNCTION(Type, maxMagSqr) @@ -414,10 +406,8 @@ Type minMagSqr(const UList& f) ) return Min; } - else - { - return pTraits::rootMax; - } + + return pTraits::rootMax; } TMP_UNARY_FUNCTION(Type, minMagSqr) @@ -508,25 +498,24 @@ Type average(const UList& f) return avrg; } - else - { - WarningInFunction - << "empty field, returning zero" << endl; - return Zero; - } + WarningInFunction + << "empty field, returning zero" << endl; + + return Zero; } TMP_UNARY_FUNCTION(Type, average) +// With reduction on ReturnType #define G_UNARY_FUNCTION(ReturnType, gFunc, Func, rFunc) \ \ template \ ReturnType gFunc(const UList& f, const label comm) \ { \ ReturnType res = Func(f); \ - reduce(res, rFunc##Op(), Pstream::msgType(), comm); \ + reduce(res, rFunc##Op(), Pstream::msgType(), comm); \ return res; \ } \ TMP_UNARY_FUNCTION(ReturnType, gFunc) @@ -536,12 +525,13 @@ 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) -G_UNARY_FUNCTION(MinMax, gMinMax, minMax, minMax) -G_UNARY_FUNCTION(scalarMinMax, gMinMaxMag, minMaxMag, minMaxMag) +G_UNARY_FUNCTION(MinMax, gMinMax, minMax, sum) +G_UNARY_FUNCTION(scalarMinMax, gMinMaxMag, minMaxMag, sum) + +G_UNARY_FUNCTION(scalar, gSumSqr, sumSqr, sum) +G_UNARY_FUNCTION(scalar, gSumMag, sumMag, sum) #undef G_UNARY_FUNCTION @@ -591,13 +581,11 @@ Type gAverage return avrg; } - else - { - WarningInFunction - << "empty field, returning zero." << endl; - return Zero; - } + WarningInFunction + << "empty field, returning zero." << endl; + + return Zero; } TMP_UNARY_FUNCTION(Type, gAverage) diff --git a/src/OpenFOAM/fields/Fields/Field/FieldFunctions.H b/src/OpenFOAM/fields/Fields/Field/FieldFunctions.H index f99c53d057..c64aa4798a 100644 --- a/src/OpenFOAM/fields/Fields/Field/FieldFunctions.H +++ b/src/OpenFOAM/fields/Fields/Field/FieldFunctions.H @@ -227,6 +227,7 @@ Type average(const UList& f); TMP_UNARY_FUNCTION(Type, average) +// With reduction on ReturnType #define G_UNARY_FUNCTION(ReturnType, gFunc, Func, rFunc) \ \ template \ @@ -238,12 +239,13 @@ 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) -G_UNARY_FUNCTION(MinMax, gMinMax, minMax, minMax) -G_UNARY_FUNCTION(scalarMinMax, gMinMaxMag, minMaxMag, minMaxMag) +G_UNARY_FUNCTION(MinMax, gMinMax, minMax, sum) +G_UNARY_FUNCTION(scalarMinMax, gMinMaxMag, minMaxMag, sum) + +G_UNARY_FUNCTION(scalar, gSumSqr, sumSqr, sum) +G_UNARY_FUNCTION(scalar, gSumMag, sumMag, sum) #undef G_UNARY_FUNCTION diff --git a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricFieldFunctions.C b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricFieldFunctions.C index 24349196d4..6c2a4d3b0c 100644 --- a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricFieldFunctions.C +++ b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricFieldFunctions.C @@ -506,7 +506,7 @@ dimensioned func \ Foam::func(gf.primitiveField()), \ Foam::func(gf.boundaryField()) \ ), \ - binaryOp() \ + binaryOp() \ ) \ ); \ } \ @@ -558,8 +558,8 @@ dimensioned func \ } UNARY_REDUCTION_FUNCTION(Type, sum, gSum) -UNARY_REDUCTION_FUNCTION(scalar, sumMag, gSumMag) UNARY_REDUCTION_FUNCTION(Type, average, gAverage) +UNARY_REDUCTION_FUNCTION(scalar, sumMag, gSumMag) #undef UNARY_REDUCTION_FUNCTION diff --git a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricFieldFunctions.H b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricFieldFunctions.H index 24b701326b..142ffbee1f 100644 --- a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricFieldFunctions.H +++ b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricFieldFunctions.H @@ -230,10 +230,8 @@ dimensioned func \ UNARY_REDUCTION_FUNCTION_WITH_BOUNDARY(Type, max, maxOp) UNARY_REDUCTION_FUNCTION_WITH_BOUNDARY(Type, min, minOp) - -// Same signature, but different implementation -UNARY_REDUCTION_FUNCTION_WITH_BOUNDARY(MinMax, minMax, unused) -UNARY_REDUCTION_FUNCTION_WITH_BOUNDARY(scalarMinMax, minMaxMag, unused) +UNARY_REDUCTION_FUNCTION_WITH_BOUNDARY(MinMax, minMax, minMaxOp) +UNARY_REDUCTION_FUNCTION_WITH_BOUNDARY(scalarMinMax, minMaxMag, minMaxMagOp) #undef UNARY_REDUCTION_FUNCTION_WITH_BOUNDARY @@ -253,8 +251,8 @@ dimensioned func \ ); UNARY_REDUCTION_FUNCTION(Type, sum, gSum) -UNARY_REDUCTION_FUNCTION(scalar, sumMag, gSumMag) UNARY_REDUCTION_FUNCTION(Type, average, gAverage) +UNARY_REDUCTION_FUNCTION(scalar, sumMag, gSumMag) #undef UNARY_REDUCTION_FUNCTION