mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
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:
@ -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."
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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
|
|
||||||
|
|
||||||
\*---------------------------------------------------------------------------*/
|
\*---------------------------------------------------------------------------*/
|
||||||
|
|
||||||
@ -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
|
||||||
|
|
||||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user