diff --git a/applications/test/hexRef8/Test-hexRef8.C b/applications/test/hexRef8/Test-hexRef8.C index a9459148a7..cb74d8fb29 100644 --- a/applications/test/hexRef8/Test-hexRef8.C +++ b/applications/test/hexRef8/Test-hexRef8.C @@ -48,12 +48,6 @@ using namespace Foam; // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -bool notEqual(const scalar s1, const scalar s2, const scalar tol) -{ - return mag(s1-s2) > tol; -} - - // Main program: int main(int argc, char *argv[]) { @@ -180,6 +174,8 @@ int main(int argc, char *argv[]) // Construct refiner. Read initial cell and point levels. hexRef8 meshCutter(mesh); + // Comparison for inequality + const auto isNotEqual = notEqualOp(1e-10); while (runTime.loop()) { @@ -345,7 +341,7 @@ int main(int argc, char *argv[]) Info<< "Uniform one field min = " << min << " max = " << max << endl; - if (notEqual(max, 1.0, 1e-10) || notEqual(min, 1.0, 1e-10)) + if (isNotEqual(min, 1) || isNotEqual(max, 1)) { FatalErrorInFunction << "Uniform volVectorField not preserved." @@ -369,7 +365,7 @@ int main(int argc, char *argv[]) Info<< "Linear profile field min = " << min << " max = " << max << endl; - if (notEqual(max, 0.0, 1e-10) || notEqual(min, 0.0, 1e-10)) + if (isNotEqual(min, 0) || isNotEqual(max, 0)) { Info<< "Linear profile not preserved." << " Min and max should both be 0.0. min:" << min @@ -390,7 +386,7 @@ int main(int argc, char *argv[]) Info<< "Uniform surface field min = " << min << " max = " << max << endl; - if (notEqual(max, 1.0, 1e-10) || notEqual(min, 1.0, 1e-10)) + if (isNotEqual(min, 1) || isNotEqual(max, 1)) { FatalErrorInFunction << "Uniform surfaceScalarField not preserved." diff --git a/src/OpenFOAM/primitives/Scalar/Scalar.H b/src/OpenFOAM/primitives/Scalar/Scalar.H index 647d9ce923..ffb97436b8 100644 --- a/src/OpenFOAM/primitives/Scalar/Scalar.H +++ b/src/OpenFOAM/primitives/Scalar/Scalar.H @@ -39,7 +39,7 @@ namespace Foam // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -// template specialisation for pTraits +// Template specialisation for pTraits template<> class pTraits { @@ -403,6 +403,52 @@ inline Scalar stabilise(const Scalar s, const Scalar tol) } +// Specializations + +// Default definition in ops.H +template struct equalOp; + +//- Compare scalar values for equality +template<> +struct equalOp +{ + const Scalar tolerance; + + //- Construct with specified tolerance (non-negative value) + equalOp(Scalar tol = ScalarVSMALL) + : + tolerance(tol) + {} + + bool operator()(const Scalar& a, const Scalar& b) const + { + return Foam::mag(a - b) <= tolerance; + } +}; + + +// Default definition in ops.H +template struct notEqualOp; + +//- Compare scalar values for inequality +template<> +struct notEqualOp +{ + const Scalar tolerance; + + //- Construct with specified tolerance (non-negative value) + notEqualOp(Scalar tol = ScalarVSMALL) + : + tolerance(tol) + {} + + bool operator()(const Scalar& a, const Scalar& b) const + { + return Foam::mag(a - b) > tolerance; + } +}; + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam diff --git a/src/OpenFOAM/primitives/ops/ops.H b/src/OpenFOAM/primitives/ops/ops.H index 8f86cdd8d1..c179d716b9 100644 --- a/src/OpenFOAM/primitives/ops/ops.H +++ b/src/OpenFOAM/primitives/ops/ops.H @@ -25,12 +25,9 @@ InNamespace Foam Description - Various binary and unary operations, which can be used for example, - for parallel combine-reduce operations. - - The information from all nodes is collected on the master node, - combined using the given combination function and the result is - broadcast to all nodes + Various function objects for unary and binary operations. + Can be used for parallel combine-reduce operations or other places + requiring a function object. \*---------------------------------------------------------------------------*/ @@ -79,10 +76,10 @@ EqOp(eqMag, x = mag(y)) EqOp(plusEqMagSqr, x += magSqr(y)) EqOp(maxEq, x = max(x, y)) EqOp(minEq, x = min(x, y)) -EqOp(minMagSqrEq, x = (magSqr(x)<=magSqr(y) ? x : y)) -EqOp(maxMagSqrEq, x = (magSqr(x)>=magSqr(y) ? x : y)) +EqOp(minMagSqrEq, x = (magSqr(x) <= magSqr(y) ? x : y)) +EqOp(maxMagSqrEq, x = (magSqr(x) >= magSqr(y) ? x : y)) EqOp(andEq, x = (x && y)) -EqOp(orEq, x = (x || y)) +EqOp(orEq, x = (x || y)) EqOp(eqMinus, x = -y) @@ -134,9 +131,32 @@ EqOp(nopEq, (void)x) }; -// ... +// Operations taking two parameters (unaltered), returning bool -#define weightedOp(opName, op) \ +#define BoolOp(opName, op) \ + \ + template \ + struct opName##Op2 \ + { \ + bool operator()(const T1& x, const T2& y) const WARNRETURN \ + { \ + return op; \ + } \ + }; \ + \ + template \ + struct opName##Op \ + { \ + bool operator()(const T& x, const T& y) const WARNRETURN \ + { \ + return op; \ + } \ + }; + + +// Weighting operations + +#define WeightedOp(opName, op) \ \ template \ class opName##WeightedOp \ @@ -178,18 +198,21 @@ Op(min, min(x, y)) Op(minMagSqr, (magSqr(x)<=magSqr(y) ? x : y)) Op(maxMagSqr, (magSqr(x)>=magSqr(y) ? x : y)) Op(minMod, minMod(x, y)) -Op(and, x && y) -Op(or, x || y) -Op(eqEq, x == y) -Op(less, x < y) -Op(lessEq, x <= y) -Op(greater, x > y) -Op(greaterEq, x >= y) -weightedOp(multiply, (weight*y)) +BoolOp(and, x && y) +BoolOp(or, x || y) +BoolOp(equal, x == y) +BoolOp(notEqual, x != y) +BoolOp(less, x < y) +BoolOp(lessEq, x <= y) +BoolOp(greater, x > y) +BoolOp(greaterEq, x >= y) + +WeightedOp(multiply, (weight*y)) #undef Op -#undef weightedOp +#undef BoolOp +#undef WeightedOp // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //