ENH: internal checkTypes method for orientedType

- reuse dimensionSet checking for dimensionedType

STYLE: unfriend some functions for complex
This commit is contained in:
Mark Olesen 2023-01-13 18:15:13 +01:00
parent ecaa55295b
commit 1f5a75c3c2
5 changed files with 145 additions and 212 deletions

View File

@ -709,22 +709,15 @@ Foam::dimensioned<Type> Foam::cmptDivide
template<class Type>
Foam::dimensioned<Type> Foam::max
(
const dimensioned<Type>& dt1,
const dimensioned<Type>& dt2
const dimensioned<Type>& a,
const dimensioned<Type>& b
)
{
if (dt1.dimensions() != dt2.dimensions())
{
FatalErrorInFunction
<< "dimensions of arguments are not equal"
<< abort(FatalError);
}
return dimensioned<Type>
(
"max(" + dt1.name() + ',' + dt2.name() + ')',
dt1.dimensions(),
max(dt1.value(), dt2.value())
"max(" + a.name() + ',' + b.name() + ')',
max(a.dimensions(), b.dimensions()),
max(a.value(), b.value())
);
}
@ -732,16 +725,18 @@ Foam::dimensioned<Type> Foam::max
template<class Type>
Foam::dimensioned<Type> Foam::min
(
const dimensioned<Type>& dt1,
const dimensioned<Type>& dt2
const dimensioned<Type>& a,
const dimensioned<Type>& b
)
{
if (dt1.dimensions() != dt2.dimensions())
{
FatalErrorInFunction
<< "dimensions of arguments are not equal"
<< abort(FatalError);
}
return dimensioned<Type>
(
"min(" + a.name() + ',' + b.name() + ')',
min(a.dimensions(), b.dimensions()),
min(a.value(), b.value())
);
}
return dimensioned<Type>
(

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2022 OpenCFD Ltd.
Copyright (C) 2017-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -48,19 +48,54 @@ Foam::orientedType::orientedOptionNames
bool Foam::orientedType::checkType
(
const orientedType& ot1,
const orientedType& ot2
const orientedType& a,
const orientedType& b
) noexcept
{
return
(
(ot1.oriented() == ot2.oriented())
|| (ot1.oriented() == orientedType::UNKNOWN)
|| (ot2.oriented() == orientedType::UNKNOWN)
(a.oriented() == b.oriented())
|| (a.oriented() == orientedOption::UNKNOWN)
|| (b.oriented() == orientedOption::UNKNOWN)
);
}
// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
namespace Foam
{
static inline bool checkTypes
(
const char* what,
const orientedType& a,
const orientedType& b
)
{
// ie, checkType(a,b)
const bool ok
(
(a.oriented() == b.oriented())
|| (a.oriented() == orientedType::orientedOption::UNKNOWN)
|| (b.oriented() == orientedType::orientedOption::UNKNOWN)
);
if (!ok)
{
FatalErrorInFunction
<< what << " : undefined for "
<< orientedType::orientedOptionNames[a.oriented()] << " and "
<< orientedType::orientedOptionNames[b.oriented()] << " types"
<< abort(FatalError);
}
return ok;
}
} // End namespace Foam
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::orientedType::orientedType(Istream& is)
@ -108,33 +143,19 @@ void Foam::orientedType::operator+=(const orientedType& ot)
oriented_ = ot.oriented();
}
if (!checkType(*this, ot))
{
FatalErrorInFunction
<< "Operator += is undefined for "
<< orientedOptionNames[oriented_] << " and "
<< orientedOptionNames[ot.oriented()] << " types"
<< abort(FatalError);
}
(void) checkTypes("Operator +=", *this, ot);
}
void Foam::orientedType::operator-=(const orientedType& ot)
{
// Set the oriented status if it was unknown
if (oriented_ == UNKNOWN)
if (oriented_ == orientedOption::UNKNOWN)
{
oriented_ = ot.oriented();
}
if (!checkType(*this, ot))
{
FatalErrorInFunction
<< "Operator -= is undefined for "
<< orientedOptionNames[oriented_] << " and "
<< orientedOptionNames[ot.oriented()] << " types"
<< abort(FatalError);
}
(void) checkTypes("Operator -=", *this, ot);
}
@ -164,33 +185,17 @@ void Foam::orientedType::operator/=(const scalar s)
// * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
Foam::orientedType Foam::max(const orientedType& ot1, const orientedType& ot2)
Foam::orientedType Foam::min(const orientedType& a, const orientedType& b)
{
if (!orientedType::checkType(ot1, ot2))
{
FatalErrorInFunction
<< "Operator max is undefined for "
<< orientedType::orientedOptionNames[ot1.oriented()] << " and "
<< orientedType::orientedOptionNames[ot2.oriented()] << " types"
<< abort(FatalError);
}
return ot1;
(void) checkTypes("Function min", a, b);
return a;
}
Foam::orientedType Foam::min(const orientedType& ot1, const orientedType& ot2)
Foam::orientedType Foam::max(const orientedType& a, const orientedType& b)
{
if (!orientedType::checkType(ot1, ot2))
{
FatalErrorInFunction
<< "Operator min is undefined for "
<< orientedType::orientedOptionNames[ot1.oriented()] << " and "
<< orientedType::orientedOptionNames[ot2.oriented()] << "types"
<< abort(FatalError);
}
return ot1;
(void) checkTypes("Function max", a, b);
return a;
}
@ -220,7 +225,7 @@ Foam::orientedType Foam::cmptAv(const orientedType& ot)
}
Foam::orientedType Foam::pow(const orientedType& ot, const scalar r)
Foam::orientedType Foam::pow(const orientedType& ot, const scalar p)
{
// Undefined???
// - only defined for integers where:
@ -236,6 +241,12 @@ Foam::orientedType Foam::sqr(const orientedType& ot)
}
Foam::orientedType Foam::pow2(const orientedType& ot)
{
return orientedType(false);
}
Foam::orientedType Foam::pow3(const orientedType& ot)
{
return ot;
@ -350,14 +361,7 @@ Foam::orientedType Foam::atan2
const orientedType& ot2
)
{
if (!orientedType::checkType(ot1, ot2))
{
FatalErrorInFunction
<< "Operator atan2 is undefined for "
<< orientedType::orientedOptionNames[ot1.oriented()] << " and "
<< orientedType::orientedOptionNames[ot2.oriented()] << "types"
<< abort(FatalError);
}
(void) checkTypes("Function atan2", ot1, ot2);
return ot1;
}
@ -369,14 +373,7 @@ Foam::orientedType Foam::hypot
const orientedType& ot2
)
{
if (!orientedType::checkType(ot1, ot2))
{
FatalErrorInFunction
<< "Operator hypot is undefined for "
<< orientedType::orientedOptionNames[ot1.oriented()] << " and "
<< orientedType::orientedOptionNames[ot2.oriented()] << "types"
<< abort(FatalError);
}
(void) checkTypes("Function hypot", ot1, ot2);
return ot1;
}
@ -418,14 +415,7 @@ Foam::orientedType Foam::operator+
const orientedType& ot2
)
{
if (!orientedType::checkType(ot1, ot2))
{
FatalErrorInFunction
<< "Operator + is undefined for "
<< orientedType::orientedOptionNames[ot1.oriented()] << " and "
<< orientedType::orientedOptionNames[ot2.oriented()] << " types"
<< abort(FatalError);
}
(void) checkTypes("Operator +", ot1, ot2);
return orientedType(ot1.is_oriented() || ot2.is_oriented());
}
@ -443,14 +433,7 @@ Foam::orientedType Foam::operator-
const orientedType& ot2
)
{
if (!orientedType::checkType(ot1, ot2))
{
FatalErrorInFunction
<< "Operator - is undefined for "
<< orientedType::orientedOptionNames[ot1.oriented()] << " and "
<< orientedType::orientedOptionNames[ot2.oriented()] << " types"
<< abort(FatalError);
}
(void) checkTypes("Operator -", ot1, ot2);
return orientedType(ot1.is_oriented() || ot2.is_oriented());
}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2022 OpenCFD Ltd.
Copyright (C) 2017-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -63,11 +63,14 @@ public:
//- Enumeration defining oriented flags
enum orientedOption : unsigned char
{
UNKNOWN = 0,
ORIENTED = 1,
UNORIENTED = 2
UNKNOWN = 0, //!< Unknown/undefined orientation
ORIENTED = 1, //!< Is oriented
UNORIENTED = 2 //!< Is unoriented
};
// Static Data
//- Named enumerations for oriented flags
static const Enum<orientedOption> orientedOptionNames;
@ -117,11 +120,11 @@ public:
// Member Functions
//- Return true if can operate on this pair of oriented types
//- True if can operate on this combination of oriented types
static bool checkType
(
const orientedType& ot1,
const orientedType& ot2
const orientedType& a,
const orientedType& b
) noexcept;
//- Return the oriented flag
@ -167,17 +170,18 @@ public:
};
// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
orientedType min(const orientedType& a, const orientedType& b);
orientedType max(const orientedType& a, const orientedType& b);
orientedType max(const orientedType& ot1, const orientedType& ot2);
orientedType min(const orientedType& ot1, const orientedType& ot2);
orientedType cmptMultiply(const orientedType& ot1, const orientedType& ot2);
orientedType cmptDivide(const orientedType& ot1, const orientedType& ot);
orientedType cmptAv(const orientedType& ot);
orientedType pow(const orientedType& ot, const scalar r);
orientedType pow(const orientedType& ot, const scalar p);
orientedType sqr(const orientedType& ot);
orientedType pow2(const orientedType& ot);
orientedType pow3(const orientedType& ot);
orientedType pow4(const orientedType& ot);
orientedType pow5(const orientedType& ot);
@ -204,6 +208,9 @@ orientedType atan2(const orientedType& ot1, const orientedType& ot2);
orientedType hypot(const orientedType& ot1, const orientedType& ot2);
orientedType transform(const orientedType& ot);
// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
orientedType operator-(const orientedType& ot);
orientedType operator*(const scalar s, const orientedType& ot);
orientedType operator/(const orientedType& ot, const scalar s);

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2014 OpenFOAM Foundation
Copyright (C) 2019-2020 OpenCFD Ltd.
Copyright (C) 2019-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -54,13 +54,6 @@ namespace Foam
// Forward Declarations
class complex;
inline scalar magSqr(const complex&);
inline scalar mag(const complex&);
inline complex sqr(const complex&);
inline const complex& min(const complex&, const complex&);
inline const complex& max(const complex&, const complex&);
inline complex limit(const complex&, const complex&);
inline const complex& sum(const complex&);
inline complex operator-(const complex&);
inline complex operator+(const complex&, const complex&);
inline complex operator+(const complex&, const scalar);
@ -93,16 +86,16 @@ public:
// Generated Methods
//- Copy construct
complex(const complex&) = default;
complex(const complex&) noexcept = default;
//- Copy assignment
complex& operator=(const complex&) = default;
complex& operator=(const complex&) noexcept = default;
//- Move construct
complex(complex&&) = default;
complex(complex&&) noexcept = default;
//- Move assignment
complex& operator=(complex&&) = default;
complex& operator=(complex&&) noexcept = default;
// Constructors
@ -134,47 +127,48 @@ public:
// STL getter/setter
//- Real part of complex number - STL naming
constexpr scalar real() const
{
return re;
}
constexpr scalar real() const noexcept { return re; }
//- Imaginary part of complex number - STL naming
constexpr scalar imag() const
{
return im;
}
constexpr scalar imag() const noexcept { return im; }
//- Set real part of complex number - STL naming
inline void real(scalar val);
void real(scalar val) noexcept { re = val; }
//- Set imaginary part of complex number - STL naming
inline void imag(scalar val);
void imag(scalar val) noexcept { im = val; }
// Access
//- Real part of complex number
inline scalar Re() const;
scalar Re() const noexcept { return re; }
//- Imaginary part of complex number
inline scalar Im() const;
scalar Im() const noexcept { return im; }
// Edit
//- Real part of complex number
inline scalar& Re();
scalar& Re() noexcept { return re; }
//- Imaginary part of complex number
inline scalar& Im();
scalar& Im() noexcept { return im; }
// Operations
// Methods
//- Complex conjugate
inline complex conjugate() const;
//- The magnitude (L2-norm) of complex.
//- Called magnitude() instead mag(), which looks too much like imag()
inline scalar magnitude() const;
//- The L2-norm squared of complex
inline scalar magSqr() const;
// Member Operators
@ -207,24 +201,6 @@ public:
inline bool operator!=(const complex& c) const;
// Friend Functions
friend scalar magSqr(const complex& c);
friend scalar mag(const complex& c);
friend complex sqr(const complex& c);
//- sgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum
friend complex sign(const complex& c);
//- csgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum
friend scalar csign(const complex& c);
friend const complex& min(const complex& c1, const complex& c2);
friend const complex& max(const complex& c1, const complex& c2);
friend complex limit(const complex& c1, const complex& c2);
friend const complex& sum(const complex& c);
// Friend Operators
friend complex operator-(const complex& c);

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2014 OpenFOAM Foundation
Copyright (C) 2019-2021 OpenCFD Ltd.
Copyright (C) 2019-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -72,48 +72,24 @@ inline Foam::complex::complex(const std::complex<double>& c)
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
inline void Foam::complex::real(scalar val)
{
re = val;
}
inline void Foam::complex::imag(scalar val)
{
im = val;
}
inline Foam::scalar Foam::complex::Re() const
{
return re;
}
inline Foam::scalar Foam::complex::Im() const
{
return im;
}
inline Foam::scalar& Foam::complex::Re()
{
return re;
}
inline Foam::scalar& Foam::complex::Im()
{
return im;
}
inline Foam::complex Foam::complex::conjugate() const
{
return complex(re, -im);
}
inline Foam::scalar Foam::complex::magnitude() const
{
return std::hypot(re, im);
}
inline Foam::scalar Foam::complex::magSqr() const
{
return (re*re + im*im);
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
inline void Foam::complex::operator=(const Foam::zero)
@ -202,20 +178,20 @@ inline Foam::complex Foam::operator~(const complex& c)
}
// * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
namespace Foam
{
inline scalar magSqr(const complex& c)
inline scalar mag(const complex& c)
{
return (c.re*c.re + c.im*c.im);
return c.magnitude();
}
inline scalar mag(const complex& c)
inline scalar magSqr(const complex& c)
{
return std::hypot(c.re, c.im);
return c.magSqr();
}
@ -225,6 +201,7 @@ inline complex sqr(const complex& c)
}
//- sgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum
inline complex sign(const complex& c)
{
const scalar s(mag(c));
@ -232,37 +209,32 @@ inline complex sign(const complex& c)
}
//- csgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum
inline scalar csign(const complex& c)
{
return equal(c.Re(), 0) ? sign(c.Im()) : sign(c.Re());
return equal(c.real(), 0) ? sign(c.imag()) : sign(c.real());
}
inline const complex& min(const complex& c1, const complex& c2)
{
if (magSqr(c1) < magSqr(c2))
{
return c1;
}
return c2;
return (c1.magSqr() < c2.magSqr()) ? c1 : c2;
}
inline const complex& max(const complex& c1, const complex& c2)
{
if (magSqr(c1) < magSqr(c2))
{
return c2;
}
return c1;
return (c1.magSqr() < c2.magSqr()) ? c2 : c1;
}
inline complex limit(const complex& c1, const complex& c2)
{
return complex(limit(c1.re, c2.re), limit(c1.im, c2.im));
return complex
(
limit(c1.real(), c2.real()),
limit(c1.imag(), c2.imag())
);
}
@ -357,7 +329,7 @@ inline complex operator*(const scalar s, const complex& c)
inline complex operator/(const complex& c1, const complex& c2)
{
const scalar sqrC2 = magSqr(c2);
const scalar sqrC2 = c2.magSqr();
return complex
(