src/OpenFOAM: Rationalised use of enumerations by using the C++11 scoped form

for polynomialEqns
This commit is contained in:
Henry Weller
2018-08-30 13:02:18 +01:00
parent 5377d637f7
commit dee5b8e4eb
15 changed files with 86 additions and 75 deletions

View File

@ -39,7 +39,7 @@ void test(const Type& polynomialEqn, const scalar tol)
const scalar nan = std::numeric_limits<scalar>::quiet_NaN(); const scalar nan = std::numeric_limits<scalar>::quiet_NaN();
const scalar inf = std::numeric_limits<scalar>::infinity(); const scalar inf = std::numeric_limits<scalar>::infinity();
FixedList<label, Type::nComponents - 1> t; FixedList<rootType, Type::nComponents - 1> t;
FixedList<scalar, Type::nComponents - 1> v(nan); FixedList<scalar, Type::nComponents - 1> v(nan);
FixedList<scalar, Type::nComponents - 1> e(nan); FixedList<scalar, Type::nComponents - 1> e(nan);
bool ok = true; bool ok = true;
@ -48,16 +48,16 @@ void test(const Type& polynomialEqn, const scalar tol)
t[i] = r.type(i); t[i] = r.type(i);
switch (t[i]) switch (t[i])
{ {
case roots::real: case rootType::real:
v[i] = polynomialEqn.value(r[i]); v[i] = polynomialEqn.value(r[i]);
e[i] = polynomialEqn.error(r[i]); e[i] = polynomialEqn.error(r[i]);
ok = ok && mag(v[i]) <= tol*mag(e[i]); ok = ok && mag(v[i]) <= tol*mag(e[i]);
break; break;
case roots::posInf: case rootType::posInf:
v[i] = + inf; v[i] = + inf;
e[i] = nan; e[i] = nan;
break; break;
case roots::negInf: case rootType::negInf:
v[i] = - inf; v[i] = - inf;
e[i] = nan; e[i] = nan;
break; break;
@ -70,7 +70,7 @@ void test(const Type& polynomialEqn, const scalar tol)
if (!ok) if (!ok)
{ {
Info<< "Coeffs: " << polynomialEqn << endl Info<< "Coeffs: " << polynomialEqn << endl
<< " Types: " << t << endl // << " Types: " << t << endl
<< " Roots: " << r << endl << " Roots: " << r << endl
<< "Values: " << v << endl << "Values: " << v << endl
<< "Errors: " << e << endl << endl; << "Errors: " << e << endl << endl;

View File

@ -93,22 +93,22 @@ Foam::vector Foam::eigenValues(const tensor& t)
{ {
switch (roots.type(i)) switch (roots.type(i))
{ {
case roots::real: case rootType::real:
lambda[i] = roots[i]; lambda[i] = roots[i];
break; break;
case roots::complex: case rootType::complex:
WarningInFunction WarningInFunction
<< "Complex eigenvalues detected for tensor: " << t << "Complex eigenvalues detected for tensor: " << t
<< endl; << endl;
lambda[i] = 0; lambda[i] = 0;
break; break;
case roots::posInf: case rootType::posInf:
lambda[i] = vGreat; lambda[i] = vGreat;
break; break;
case roots::negInf: case rootType::negInf:
lambda[i] = - vGreat; lambda[i] = - vGreat;
break; break;
case roots::nan: case rootType::nan:
FatalErrorInFunction FatalErrorInFunction
<< "Eigenvalue calculation failed for tensor: " << t << "Eigenvalue calculation failed for tensor: " << t
<< exit(FatalError); << exit(FatalError);

View File

@ -99,22 +99,22 @@ Foam::vector2D Foam::eigenValues(const tensor2D& t)
{ {
switch (roots.type(i)) switch (roots.type(i))
{ {
case roots::real: case rootType::real:
lambda[i] = roots[i]; lambda[i] = roots[i];
break; break;
case roots::complex: case rootType::complex:
WarningInFunction WarningInFunction
<< "Complex eigenvalues detected for tensor: " << t << "Complex eigenvalues detected for tensor: " << t
<< endl; << endl;
lambda[i] = 0; lambda[i] = 0;
break; break;
case roots::posInf: case rootType::posInf:
lambda[i] = vGreat; lambda[i] = vGreat;
break; break;
case roots::negInf: case rootType::negInf:
lambda[i] = - vGreat; lambda[i] = - vGreat;
break; break;
case roots::nan: case rootType::nan:
FatalErrorInFunction FatalErrorInFunction
<< "Eigenvalue calculation failed for tensor: " << t << "Eigenvalue calculation failed for tensor: " << t
<< exit(FatalError); << exit(FatalError);

View File

@ -46,11 +46,8 @@ namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace roots
{
//- Types of root //- Types of root
enum type enum class rootType
{ {
real = 0, real = 0,
complex, complex,
@ -59,7 +56,6 @@ enum type
nan nan
}; };
}
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class Roots Declaration Class Roots Declaration
@ -83,12 +79,12 @@ public:
inline Roots(); inline Roots();
//- Construct with a uniform value //- Construct with a uniform value
inline Roots(const roots::type t, const scalar x); inline Roots(const rootType t, const scalar x);
//- Construct by concatenation //- Construct by concatenation
inline Roots inline Roots
( (
const roots::type t, const rootType t,
const scalar x, const scalar x,
const Roots<N - 1>& xs const Roots<N - 1>& xs
); );
@ -97,7 +93,7 @@ public:
inline Roots inline Roots
( (
const Roots<N - 1>& xs, const Roots<N - 1>& xs,
const roots::type t, const rootType t,
const scalar x const scalar x
); );
@ -109,10 +105,10 @@ public:
// Member Functions // Member Functions
//- Set the type of the i-th root //- Set the type of the i-th root
inline void type(const direction i, const roots::type t); inline void type(const direction i, const rootType t);
//- Return the type of the i-th root //- Return the type of the i-th root
inline roots::type type(const direction i) const; inline rootType type(const direction i) const;
}; };

View File

@ -32,13 +32,13 @@ inline Foam::Roots<N>::Roots()
{ {
forAll(*this, i) forAll(*this, i)
{ {
type(i, roots::nan); type(i, rootType::nan);
} }
} }
template <Foam::direction N> template <Foam::direction N>
inline Foam::Roots<N>::Roots(const roots::type t, const scalar x) inline Foam::Roots<N>::Roots(const rootType t, const scalar x)
: :
types_(0) types_(0)
{ {
@ -53,7 +53,7 @@ inline Foam::Roots<N>::Roots(const roots::type t, const scalar x)
template <Foam::direction N> template <Foam::direction N>
inline Foam::Roots<N>::Roots inline Foam::Roots<N>::Roots
( (
const roots::type t, const rootType t,
const scalar x, const scalar x,
const Roots<N - 1>& xs const Roots<N - 1>& xs
) )
@ -74,7 +74,7 @@ template <Foam::direction N>
inline Foam::Roots<N>::Roots inline Foam::Roots<N>::Roots
( (
const Roots<N - 1>& xs, const Roots<N - 1>& xs,
const roots::type t, const rootType t,
const scalar x const scalar x
) )
: :
@ -119,17 +119,17 @@ template <Foam::direction N>
inline void Foam::Roots<N>::type inline void Foam::Roots<N>::type
( (
const direction i, const direction i,
const roots::type t const rootType t
) )
{ {
types_ += (t - type(i)) << 3*i; types_ += (static_cast<int>(t) - static_cast<int>(type(i))) << 3*i;
} }
template <Foam::direction N> template <Foam::direction N>
inline Foam::roots::type Foam::Roots<N>::type(const direction i) const inline Foam::rootType Foam::Roots<N>::type(const direction i) const
{ {
return static_cast<roots::type>((types_ >> 3*i) & 7); return static_cast<rootType>((types_ >> 3*i) & 7);
} }

View File

@ -76,7 +76,7 @@ Foam::Roots<3> Foam::cubicEqn::roots() const
if (a == 0) if (a == 0)
{ {
return Roots<3>(quadraticEqn(b, c, d).roots(), roots::nan, 0); return Roots<3>(quadraticEqn(b, c, d).roots(), rootType::nan, 0);
} }
// This is assumed not to over- or under-flow. If it does, all bets are off. // This is assumed not to over- or under-flow. If it does, all bets are off.

View File

@ -53,8 +53,11 @@ class cubicEqn
{ {
public: public:
//- Component labeling enumeration //- Coefficient indexing enumeration
enum components { A, B, C, D }; struct coefficient
{
enum {a, b, c, d};
};
// Constructors // Constructors

View File

@ -43,10 +43,10 @@ inline Foam::cubicEqn::cubicEqn
const scalar d const scalar d
) )
{ {
this->v_[A] = a; this->v_[coefficient::a] = a;
this->v_[B] = b; this->v_[coefficient::b] = b;
this->v_[C] = c; this->v_[coefficient::c] = c;
this->v_[D] = d; this->v_[coefficient::d] = d;
} }
@ -54,49 +54,49 @@ inline Foam::cubicEqn::cubicEqn
inline Foam::scalar Foam::cubicEqn::a() const inline Foam::scalar Foam::cubicEqn::a() const
{ {
return this->v_[A]; return this->v_[coefficient::a];
} }
inline Foam::scalar Foam::cubicEqn::b() const inline Foam::scalar Foam::cubicEqn::b() const
{ {
return this->v_[B]; return this->v_[coefficient::b];
} }
inline Foam::scalar Foam::cubicEqn::c() const inline Foam::scalar Foam::cubicEqn::c() const
{ {
return this->v_[C]; return this->v_[coefficient::c];
} }
inline Foam::scalar Foam::cubicEqn::d() const inline Foam::scalar Foam::cubicEqn::d() const
{ {
return this->v_[D]; return this->v_[coefficient::d];
} }
inline Foam::scalar& Foam::cubicEqn::a() inline Foam::scalar& Foam::cubicEqn::a()
{ {
return this->v_[A]; return this->v_[coefficient::a];
} }
inline Foam::scalar& Foam::cubicEqn::b() inline Foam::scalar& Foam::cubicEqn::b()
{ {
return this->v_[B]; return this->v_[coefficient::b];
} }
inline Foam::scalar& Foam::cubicEqn::c() inline Foam::scalar& Foam::cubicEqn::c()
{ {
return this->v_[C]; return this->v_[coefficient::c];
} }
inline Foam::scalar& Foam::cubicEqn::d() inline Foam::scalar& Foam::cubicEqn::d()
{ {
return this->v_[D]; return this->v_[coefficient::d];
} }

View File

@ -52,8 +52,11 @@ class linearEqn
{ {
public: public:
//- Component labeling enumeration //- Coefficient indexing enumeration
enum components { A, B }; struct coefficient
{
enum {a, b};
};
// Constructors // Constructors

View File

@ -37,8 +37,8 @@ inline Foam::linearEqn::linearEqn(const Foam::zero)
inline Foam::linearEqn::linearEqn(const scalar a, const scalar b) inline Foam::linearEqn::linearEqn(const scalar a, const scalar b)
{ {
this->v_[A] = a; this->v_[coefficient::a] = a;
this->v_[B] = b; this->v_[coefficient::b] = b;
} }
@ -46,25 +46,25 @@ inline Foam::linearEqn::linearEqn(const scalar a, const scalar b)
inline Foam::scalar Foam::linearEqn::a() const inline Foam::scalar Foam::linearEqn::a() const
{ {
return this->v_[A]; return this->v_[coefficient::a];
} }
inline Foam::scalar Foam::linearEqn::b() const inline Foam::scalar Foam::linearEqn::b() const
{ {
return this->v_[B]; return this->v_[coefficient::b];
} }
inline Foam::scalar& Foam::linearEqn::a() inline Foam::scalar& Foam::linearEqn::a()
{ {
return this->v_[A]; return this->v_[coefficient::a];
} }
inline Foam::scalar& Foam::linearEqn::b() inline Foam::scalar& Foam::linearEqn::b()
{ {
return this->v_[B]; return this->v_[coefficient::b];
} }
@ -93,7 +93,7 @@ inline Foam::Roots<1> Foam::linearEqn::roots() const
This function solves a linear equation of the following form: This function solves a linear equation of the following form:
a*x + b = 0 a*x + b = 0
x + B = 0 x + b = 0
*/ */
@ -102,15 +102,21 @@ inline Foam::Roots<1> Foam::linearEqn::roots() const
if (a == 0) if (a == 0)
{ {
return Roots<1>(roots::nan, 0); return Roots<1>(rootType::nan, 0);
} }
if (mag(b/vGreat) >= mag(a)) if (mag(b/vGreat) >= mag(a))
{ {
return Roots<1>(sign(a) == sign(b) ? roots::negInf : roots::posInf, 0); return Roots<1>
(
sign(a) == sign(b)
? rootType::negInf
: rootType::posInf,
0
);
} }
return Roots<1>(roots::real, - b/a); return Roots<1>(rootType::real, -b/a);
} }

View File

@ -58,7 +58,7 @@ Foam::Roots<2> Foam::quadraticEqn::roots() const
if (a == 0) if (a == 0)
{ {
return Roots<2>(linearEqn(b, c).roots(), roots::nan, 0); return Roots<2>(linearEqn(b, c).roots(), rootType::nan, 0);
} }
// This is assumed not to over- or under-flow. If it does, all bets are off. // This is assumed not to over- or under-flow. If it does, all bets are off.
@ -81,7 +81,7 @@ Foam::Roots<2> Foam::quadraticEqn::roots() const
} }
else // if (twoComplex) else // if (twoComplex)
{ {
return Roots<2>(roots::complex, 0); return Roots<2>(rootType::complex, 0);
} }
} }

View File

@ -53,8 +53,11 @@ class quadraticEqn
{ {
public: public:
//- Component labeling enumeration //- Coefficient indexing enumeration
enum components { A, B, C }; struct coefficient
{
enum {a, b, c};
};
// Constructors // Constructors

View File

@ -42,9 +42,9 @@ inline Foam::quadraticEqn::quadraticEqn
const scalar c const scalar c
) )
{ {
this->v_[A] = a; this->v_[coefficient::a] = a;
this->v_[B] = b; this->v_[coefficient::b] = b;
this->v_[C] = c; this->v_[coefficient::c] = c;
} }
@ -52,37 +52,37 @@ inline Foam::quadraticEqn::quadraticEqn
inline Foam::scalar Foam::quadraticEqn::a() const inline Foam::scalar Foam::quadraticEqn::a() const
{ {
return this->v_[A]; return this->v_[coefficient::a];
} }
inline Foam::scalar Foam::quadraticEqn::b() const inline Foam::scalar Foam::quadraticEqn::b() const
{ {
return this->v_[B]; return this->v_[coefficient::b];
} }
inline Foam::scalar Foam::quadraticEqn::c() const inline Foam::scalar Foam::quadraticEqn::c() const
{ {
return this->v_[C]; return this->v_[coefficient::c];
} }
inline Foam::scalar& Foam::quadraticEqn::a() inline Foam::scalar& Foam::quadraticEqn::a()
{ {
return this->v_[A]; return this->v_[coefficient::a];
} }
inline Foam::scalar& Foam::quadraticEqn::b() inline Foam::scalar& Foam::quadraticEqn::b()
{ {
return this->v_[B]; return this->v_[coefficient::b];
} }
inline Foam::scalar& Foam::quadraticEqn::c() inline Foam::scalar& Foam::quadraticEqn::c()
{ {
return this->v_[C]; return this->v_[coefficient::c];
} }

View File

@ -858,7 +858,7 @@ Foam::scalar Foam::particle::trackToMovingTri
for (label j = 0; j < 3; ++ j) for (label j = 0; j < 3; ++ j)
{ {
if (mu.type(j) == roots::real && hitEqn[i].derivative(mu[j]) < 0) if (mu.type(j) == rootType::real && hitEqn[i].derivative(mu[j]) < 0)
{ {
if (debug) if (debug)
{ {

View File

@ -314,7 +314,7 @@ Foam::plane Foam::sweptFaceAreaWeightAMI::getCutPlane
Pair<bool> cuts(false, false); Pair<bool> cuts(false, false);
for (label j = 0; j < 2; ++ j) for (label j = 0; j < 2; ++ j)
{ {
if (us.type(j) == roots::real) if (us.type(j) == rootType::real)
{ {
const vector den = ca + da*us[j]; const vector den = ca + da*us[j];
if (magSqr(den) > vSmall) if (magSqr(den) > vSmall)