STYLE: logical ops return bool (issue #1043)

- these currently only with bool parameters, but the return value should
  nonetheless always be a bool value:

      andOp(), orOp(), lessOp(), lessEqOp(), greaterOp(), greaterEqOp()

- renamed the unused eqEqOp() to equalOp() for naming consistency with
  the equal() global function.

ENH: equalOp() specialization for scalars

- function object version of the equal() function.
  The default constructor uses the same tolerance (VSMALL),
  but can also supply an alternative tolerance on construction.
This commit is contained in:
Mark Olesen
2018-10-17 07:59:26 +02:00
parent 0a0fee88a0
commit 8b569b16d1
3 changed files with 95 additions and 30 deletions

View File

@ -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: // Main program:
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -180,6 +174,8 @@ int main(int argc, char *argv[])
// Construct refiner. Read initial cell and point levels. // Construct refiner. Read initial cell and point levels.
hexRef8 meshCutter(mesh); hexRef8 meshCutter(mesh);
// Comparison for inequality
const auto isNotEqual = notEqualOp<scalar>(1e-10);
while (runTime.loop()) while (runTime.loop())
{ {
@ -345,7 +341,7 @@ int main(int argc, char *argv[])
Info<< "Uniform one field min = " << min Info<< "Uniform one field min = " << min
<< " max = " << max << endl; << " max = " << max << endl;
if (notEqual(max, 1.0, 1e-10) || notEqual(min, 1.0, 1e-10)) if (isNotEqual(min, 1) || isNotEqual(max, 1))
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Uniform volVectorField not preserved." << "Uniform volVectorField not preserved."
@ -369,7 +365,7 @@ int main(int argc, char *argv[])
Info<< "Linear profile field min = " << min Info<< "Linear profile field min = " << min
<< " max = " << max << endl; << " 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." Info<< "Linear profile not preserved."
<< " Min and max should both be 0.0. min:" << min << " 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 Info<< "Uniform surface field min = " << min
<< " max = " << max << endl; << " max = " << max << endl;
if (notEqual(max, 1.0, 1e-10) || notEqual(min, 1.0, 1e-10)) if (isNotEqual(min, 1) || isNotEqual(max, 1))
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Uniform surfaceScalarField not preserved." << "Uniform surfaceScalarField not preserved."

View File

@ -39,7 +39,7 @@ namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// template specialisation for pTraits<Scalar> // Template specialisation for pTraits<Scalar>
template<> template<>
class pTraits<Scalar> class pTraits<Scalar>
{ {
@ -403,6 +403,52 @@ inline Scalar stabilise(const Scalar s, const Scalar tol)
} }
// Specializations
// Default definition in ops.H
template<class T> struct equalOp;
//- Compare scalar values for equality
template<>
struct equalOp<Scalar>
{
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<class T> struct notEqualOp;
//- Compare scalar values for inequality
template<>
struct notEqualOp<Scalar>
{
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 } // End namespace Foam

View File

@ -25,12 +25,9 @@ InNamespace
Foam Foam
Description Description
Various binary and unary operations, which can be used for example, Various function objects for unary and binary operations.
for parallel combine-reduce operations. Can be used for parallel combine-reduce operations or other places
requiring a function object.
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
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
@ -79,8 +76,8 @@ EqOp(eqMag, x = mag(y))
EqOp(plusEqMagSqr, x += magSqr(y)) EqOp(plusEqMagSqr, x += magSqr(y))
EqOp(maxEq, x = max(x, y)) EqOp(maxEq, x = max(x, y))
EqOp(minEq, x = min(x, y)) EqOp(minEq, x = min(x, y))
EqOp(minMagSqrEq, 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(maxMagSqrEq, x = (magSqr(x) >= magSqr(y) ? x : y))
EqOp(andEq, x = (x && y)) EqOp(andEq, x = (x && y))
EqOp(orEq, x = (x || y)) EqOp(orEq, x = (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<class T1, class T2> \
struct opName##Op2 \
{ \
bool operator()(const T1& x, const T2& y) const WARNRETURN \
{ \
return op; \
} \
}; \
\
template<class T> \
struct opName##Op \
{ \
bool operator()(const T& x, const T& y) const WARNRETURN \
{ \
return op; \
} \
};
// Weighting operations
#define WeightedOp(opName, op) \
\ \
template<class T, class CombineOp> \ template<class T, class CombineOp> \
class opName##WeightedOp \ class opName##WeightedOp \
@ -178,18 +198,21 @@ Op(min, min(x, y))
Op(minMagSqr, (magSqr(x)<=magSqr(y) ? x : y)) Op(minMagSqr, (magSqr(x)<=magSqr(y) ? x : y))
Op(maxMagSqr, (magSqr(x)>=magSqr(y) ? x : y)) Op(maxMagSqr, (magSqr(x)>=magSqr(y) ? x : y))
Op(minMod, minMod(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 Op
#undef weightedOp #undef BoolOp
#undef WeightedOp
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //