mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: add dimensionSet provisioning for a dimensioned clip() method
- use file-local function to reduce some code clutter
This commit is contained in:
@ -27,7 +27,9 @@ Description
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#include "dimensionSet.H"
|
||||
#include "dimensionedScalar.H"
|
||||
#include "IOmanip.H"
|
||||
#include <tuple>
|
||||
|
||||
using namespace Foam;
|
||||
|
||||
@ -39,6 +41,96 @@ using namespace Foam;
|
||||
Info<< STRING_QUOTE(arg) << " " << arg << nl
|
||||
|
||||
|
||||
bool hadDimensionError
|
||||
(
|
||||
const std::tuple<bool, dimensionSet, dimensionSet>& input,
|
||||
bool dimsOk,
|
||||
std::string errMsg
|
||||
)
|
||||
{
|
||||
if (dimsOk)
|
||||
{
|
||||
if (std::get<0>(input))
|
||||
{
|
||||
Info<< "(pass) dimension check ok ";
|
||||
}
|
||||
else
|
||||
{
|
||||
Info<< "(fail) unexpected success for dimension check ";
|
||||
}
|
||||
Info<< std::get<1>(input) << " == " << std::get<2>(input) << nl;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (std::get<0>(input))
|
||||
{
|
||||
Info<< "(fail) unexpected";
|
||||
}
|
||||
else
|
||||
{
|
||||
Info<< "(pass) expected";
|
||||
}
|
||||
|
||||
Info<< " failure" << nl << errMsg.c_str() << nl;
|
||||
}
|
||||
|
||||
return (std::get<0>(input) != dimsOk);
|
||||
}
|
||||
|
||||
|
||||
unsigned checkDimensions
|
||||
(
|
||||
std::initializer_list
|
||||
<
|
||||
std::tuple<bool, dimensionSet, dimensionSet>
|
||||
> tests
|
||||
)
|
||||
{
|
||||
Info<< nl << "Verify dimension checks" << nl << nl;
|
||||
|
||||
unsigned nFail = 0;
|
||||
std::string errMsg;
|
||||
|
||||
// Expect some failures
|
||||
const bool prev = FatalError.throwExceptions();
|
||||
|
||||
for
|
||||
(
|
||||
const std::tuple<bool, dimensionSet, dimensionSet>& test
|
||||
: tests
|
||||
)
|
||||
{
|
||||
const bool expected = std::get<0>(test);
|
||||
const dimensionSet& a = std::get<1>(test);
|
||||
const dimensionSet& b = std::get<2>(test);
|
||||
|
||||
bool dimsOk = false;
|
||||
|
||||
try
|
||||
{
|
||||
// min(a, b);
|
||||
clip(a, b);
|
||||
dimsOk = true;
|
||||
}
|
||||
catch (Foam::error& err)
|
||||
{
|
||||
errMsg = err.message();
|
||||
}
|
||||
|
||||
if (expected != dimsOk)
|
||||
{
|
||||
++nFail;
|
||||
}
|
||||
|
||||
hadDimensionError(test, dimsOk, errMsg);
|
||||
}
|
||||
|
||||
FatalError.throwExceptions(prev);
|
||||
|
||||
return nFail;
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
// Main program:
|
||||
|
||||
@ -92,6 +184,21 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
|
||||
unsigned nFail = 0;
|
||||
|
||||
nFail += checkDimensions
|
||||
({
|
||||
{ true, dimless, dimless },
|
||||
{ false, dimless, dimPressure }
|
||||
});
|
||||
|
||||
|
||||
if (nFail)
|
||||
{
|
||||
Info<< nl << "failed " << nFail << " tests" << nl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
Info<< nl << "End\n" << endl;
|
||||
|
||||
return 0;
|
||||
|
||||
@ -47,14 +47,49 @@ inline scalar readNasScalar(const std::string& str)
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
template<class TYPE>
|
||||
template<class T>
|
||||
bool hadParsingError
|
||||
(
|
||||
const std::pair<bool, std::string>& input,
|
||||
const std::pair<bool, T>& result,
|
||||
std::string errMsg
|
||||
)
|
||||
{
|
||||
if (result.first)
|
||||
{
|
||||
if (input.first)
|
||||
{
|
||||
Info<< "(pass) parsed "
|
||||
<< input.second << " = " << result.second << nl;
|
||||
}
|
||||
else
|
||||
{
|
||||
Info<< "(fail) unexpected success for " << input.second << nl;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (input.first)
|
||||
{
|
||||
Info<< "(fail) unexpected";
|
||||
}
|
||||
else
|
||||
{
|
||||
Info<< "(pass) expected";
|
||||
}
|
||||
|
||||
Info<< " failure " << input.second << " >> " << errMsg.c_str() << nl;
|
||||
}
|
||||
|
||||
return (input.first != result.first);
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
unsigned testParsing
|
||||
(
|
||||
TYPE (*function)(const std::string&),
|
||||
std::initializer_list
|
||||
<
|
||||
Tuple2<bool, std::string>
|
||||
> tests
|
||||
T (*function)(const std::string&),
|
||||
std::initializer_list<std::pair<bool, std::string>> tests
|
||||
)
|
||||
{
|
||||
unsigned nFail = 0;
|
||||
@ -63,51 +98,26 @@ unsigned testParsing
|
||||
// Expect some failures
|
||||
const bool prev = FatalIOError.throwExceptions();
|
||||
|
||||
for (const Tuple2<bool, std::string>& test : tests)
|
||||
for (const std::pair<bool, std::string>& test : tests)
|
||||
{
|
||||
const bool expected = test.first();
|
||||
const std::string& str = test.second();
|
||||
std::pair<bool, T> result(false, T());
|
||||
|
||||
bool parsed = true;
|
||||
|
||||
TYPE val;
|
||||
try
|
||||
{
|
||||
val = function (str);
|
||||
result.second = function (test.second);
|
||||
result.first = true;
|
||||
}
|
||||
catch (Foam::error& err)
|
||||
{
|
||||
parsed = false;
|
||||
errMsg = err.message();
|
||||
}
|
||||
|
||||
if (parsed)
|
||||
if (test.first != result.first)
|
||||
{
|
||||
if (expected)
|
||||
{
|
||||
Info<< "(pass) parsed " << str << " = " << val << nl;
|
||||
}
|
||||
else
|
||||
{
|
||||
++nFail;
|
||||
Info<< "(fail) unexpected success for " << str << nl;
|
||||
}
|
||||
++nFail;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (expected)
|
||||
{
|
||||
++nFail;
|
||||
Info<< "(fail) unexpected";
|
||||
}
|
||||
else
|
||||
{
|
||||
Info<< "(pass) expected";
|
||||
}
|
||||
|
||||
Info<< " failure " << str
|
||||
<< " >> " << errMsg.c_str() << nl;
|
||||
}
|
||||
hadParsingError(test, result, errMsg);
|
||||
}
|
||||
|
||||
FatalIOError.throwExceptions(prev);
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation
|
||||
\\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd.
|
||||
\\/ M anipulation | Copyright (C) 2017-2019 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -36,6 +36,33 @@ namespace Foam
|
||||
const Foam::scalar Foam::dimensionSet::smallExponent = SMALL;
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
|
||||
|
||||
namespace Foam
|
||||
{
|
||||
|
||||
static inline bool checkDims
|
||||
(
|
||||
const char* what,
|
||||
const dimensionSet& a,
|
||||
const dimensionSet& b
|
||||
)
|
||||
{
|
||||
if (a != b)
|
||||
{
|
||||
FatalErrorInFunction
|
||||
<< "Different dimensions for '" << what
|
||||
<< "'\n dimensions : " << a << " != " << b << nl
|
||||
<< abort(FatalError);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // End namespace Foam
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||
|
||||
Foam::dimensionSet::dimensionSet()
|
||||
@ -166,12 +193,9 @@ bool Foam::dimensionSet::operator!=(const dimensionSet& ds) const
|
||||
|
||||
bool Foam::dimensionSet::operator=(const dimensionSet& ds) const
|
||||
{
|
||||
if (dimensionSet::debug && *this != ds)
|
||||
if (dimensionSet::debug)
|
||||
{
|
||||
FatalErrorInFunction
|
||||
<< "Different dimensions for =" << nl
|
||||
<< " dimensions : " << *this << " = " << ds << endl
|
||||
<< abort(FatalError);
|
||||
checkDims("(a = b)", *this, ds);
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -180,12 +204,9 @@ bool Foam::dimensionSet::operator=(const dimensionSet& ds) const
|
||||
|
||||
bool Foam::dimensionSet::operator+=(const dimensionSet& ds) const
|
||||
{
|
||||
if (dimensionSet::debug && *this != ds)
|
||||
if (dimensionSet::debug)
|
||||
{
|
||||
FatalErrorInFunction
|
||||
<< "Different dimensions for +=" << nl
|
||||
<< " dimensions : " << *this << " = " << ds << endl
|
||||
<< abort(FatalError);
|
||||
checkDims("(a += b)", *this, ds);
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -194,12 +215,9 @@ bool Foam::dimensionSet::operator+=(const dimensionSet& ds) const
|
||||
|
||||
bool Foam::dimensionSet::operator-=(const dimensionSet& ds) const
|
||||
{
|
||||
if (dimensionSet::debug && *this != ds)
|
||||
if (dimensionSet::debug)
|
||||
{
|
||||
FatalErrorInFunction
|
||||
<< "Different dimensions for -=" << nl
|
||||
<< " dimensions : " << *this << " = " << ds << endl
|
||||
<< abort(FatalError);
|
||||
checkDims("(a -= b)", *this, ds);
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -226,12 +244,9 @@ bool Foam::dimensionSet::operator/=(const dimensionSet& ds)
|
||||
|
||||
Foam::dimensionSet Foam::min(const dimensionSet& ds1, const dimensionSet& ds2)
|
||||
{
|
||||
if (dimensionSet::debug && ds1 != ds2)
|
||||
if (dimensionSet::debug)
|
||||
{
|
||||
FatalErrorInFunction
|
||||
<< "Arguments of min have different dimensions" << nl
|
||||
<< " dimensions : " << ds1 << " and " << ds2 << endl
|
||||
<< abort(FatalError);
|
||||
checkDims("min(a, b)", ds1, ds2);
|
||||
}
|
||||
|
||||
return ds1;
|
||||
@ -240,12 +255,20 @@ Foam::dimensionSet Foam::min(const dimensionSet& ds1, const dimensionSet& ds2)
|
||||
|
||||
Foam::dimensionSet Foam::max(const dimensionSet& ds1, const dimensionSet& ds2)
|
||||
{
|
||||
if (dimensionSet::debug && ds1 != ds2)
|
||||
if (dimensionSet::debug)
|
||||
{
|
||||
FatalErrorInFunction
|
||||
<< "Arguments of max have different dimensions" << nl
|
||||
<< " dimensions : " << ds1 << " and " << ds2 << endl
|
||||
<< abort(FatalError);
|
||||
checkDims("max(a, b)", ds1, ds2);
|
||||
}
|
||||
|
||||
return ds1;
|
||||
}
|
||||
|
||||
|
||||
Foam::dimensionSet Foam::clip(const dimensionSet& ds1, const dimensionSet& ds2)
|
||||
{
|
||||
if (dimensionSet::debug)
|
||||
{
|
||||
checkDims("clip(a, b)", ds1, ds2);
|
||||
}
|
||||
|
||||
return ds1;
|
||||
@ -464,12 +487,9 @@ Foam::dimensionSet Foam::trans(const dimensionSet& ds)
|
||||
|
||||
Foam::dimensionSet Foam::atan2(const dimensionSet& ds1, const dimensionSet& ds2)
|
||||
{
|
||||
if (dimensionSet::debug && ds1 != ds2)
|
||||
if (dimensionSet::debug)
|
||||
{
|
||||
FatalErrorInFunction
|
||||
<< "Arguments of atan2 have different dimensions" << nl
|
||||
<< " dimensions : " << ds1 << " and " << ds2 << endl
|
||||
<< abort(FatalError);
|
||||
checkDims("atan2(a, b)", ds1, ds2);
|
||||
}
|
||||
|
||||
return dimless;
|
||||
@ -508,12 +528,9 @@ Foam::dimensionSet Foam::operator+
|
||||
const dimensionSet& ds2
|
||||
)
|
||||
{
|
||||
if (dimensionSet::debug && ds1 != ds2)
|
||||
if (dimensionSet::debug)
|
||||
{
|
||||
FatalErrorInFunction
|
||||
<< "LHS and RHS of '+' have different dimensions" << nl
|
||||
<< " dimensions : " << ds1 << " + " << ds2 << endl
|
||||
<< abort(FatalError);
|
||||
checkDims("(a + b)", ds1, ds2);
|
||||
}
|
||||
|
||||
return ds1;
|
||||
@ -526,12 +543,9 @@ Foam::dimensionSet Foam::operator-
|
||||
const dimensionSet& ds2
|
||||
)
|
||||
{
|
||||
if (dimensionSet::debug && ds1 != ds2)
|
||||
if (dimensionSet::debug)
|
||||
{
|
||||
FatalErrorInFunction
|
||||
<< "LHS and RHS of '-' have different dimensions" << nl
|
||||
<< " dimensions : " << ds1 << " - " << ds2 << endl
|
||||
<< abort(FatalError);
|
||||
checkDims("(a - b)", ds1, ds2);
|
||||
}
|
||||
|
||||
return ds1;
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation
|
||||
\\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd.
|
||||
\\/ M anipulation | Copyright (C) 2017-2019 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -70,10 +70,8 @@ public:
|
||||
|
||||
// Member Constants
|
||||
|
||||
enum
|
||||
{
|
||||
nDimensions = 7 //!< 7 base dimensions
|
||||
};
|
||||
//- There are 7 base dimensions
|
||||
static constexpr int nDimensions = 7;
|
||||
|
||||
//- Enumeration for the dimension exponents
|
||||
enum dimensionType
|
||||
@ -88,8 +86,9 @@ public:
|
||||
};
|
||||
|
||||
|
||||
// Static data members
|
||||
// Static Data Members
|
||||
|
||||
//- Tolerance for 'small' exponents, for near-zero rounding
|
||||
static const scalar smallExponent;
|
||||
|
||||
|
||||
@ -216,6 +215,7 @@ public:
|
||||
//- Return non-const access to the exponents as a list
|
||||
FixedList<scalar,7>& values();
|
||||
|
||||
//- Copy assign the exponents from the dimensionSet
|
||||
void reset(const dimensionSet& ds);
|
||||
|
||||
|
||||
@ -291,6 +291,7 @@ Ostream& operator<<(Ostream& os, const dimensionSet& ds);
|
||||
|
||||
dimensionSet min(const dimensionSet& ds1, const dimensionSet& ds2);
|
||||
dimensionSet max(const dimensionSet& ds1, const dimensionSet& ds2);
|
||||
dimensionSet clip(const dimensionSet& ds1, const dimensionSet& ds2);
|
||||
|
||||
dimensionSet cmptMultiply(const dimensionSet& ds1, const dimensionSet& ds2);
|
||||
dimensionSet cmptDivide(const dimensionSet& ds1, const dimensionSet& ds2);
|
||||
|
||||
@ -421,8 +421,8 @@ Foam::Istream& Foam::dimensionSet::read
|
||||
if (startToken != token::BEGIN_SQR)
|
||||
{
|
||||
FatalIOErrorInFunction(is)
|
||||
<< "Expected a " << token::BEGIN_SQR << " in dimensionSet"
|
||||
<< endl << "in stream " << is.info()
|
||||
<< "Expected a '" << token::BEGIN_SQR << "' in dimensionSet\n"
|
||||
<< "in stream " << is.info() << nl
|
||||
<< exit(FatalIOError);
|
||||
}
|
||||
|
||||
@ -444,7 +444,7 @@ Foam::Istream& Foam::dimensionSet::read
|
||||
{
|
||||
// Read first five dimensions
|
||||
exponents_[dimensionSet::MASS] = nextToken.number();
|
||||
for (int d=1; d<dimensionSet::CURRENT; ++d)
|
||||
for (int d=1; d < dimensionSet::CURRENT; ++d)
|
||||
{
|
||||
is >> exponents_[d];
|
||||
}
|
||||
@ -472,8 +472,8 @@ Foam::Istream& Foam::dimensionSet::read
|
||||
if (nextToken != token::END_SQR)
|
||||
{
|
||||
FatalIOErrorInFunction(is)
|
||||
<< "Expected a " << token::END_SQR << " in dimensionSet "
|
||||
<< endl << "in stream " << is.info()
|
||||
<< "Expected a '" << token::END_SQR << "' in dimensionSet\n"
|
||||
<< "in stream " << is.info() << nl
|
||||
<< exit(FatalIOError);
|
||||
}
|
||||
}
|
||||
@ -508,8 +508,8 @@ Foam::Istream& Foam::dimensionSet::read
|
||||
if (startToken != token::BEGIN_SQR)
|
||||
{
|
||||
FatalIOErrorInFunction(is)
|
||||
<< "Expected a " << token::BEGIN_SQR << " in dimensionSet"
|
||||
<< endl << "in stream " << is.info()
|
||||
<< "Expected a '" << token::BEGIN_SQR << "' in dimensionSet\n"
|
||||
<< "in stream " << is.info() << nl
|
||||
<< exit(FatalIOError);
|
||||
}
|
||||
|
||||
@ -606,8 +606,8 @@ Foam::Istream& Foam::dimensionSet::read
|
||||
if (nextToken != token::END_SQR)
|
||||
{
|
||||
FatalIOErrorInFunction(is)
|
||||
<< "Expected a " << token::END_SQR << " in dimensionSet " << nl
|
||||
<< "in stream " << is.info() << endl
|
||||
<< "Expected a '" << token::END_SQR << "' in dimensionSet\n"
|
||||
<< "in stream " << is.info() << nl
|
||||
<< exit(FatalIOError);
|
||||
}
|
||||
}
|
||||
@ -631,7 +631,7 @@ Foam::Ostream& Foam::dimensionSet::write
|
||||
if (writeUnits.valid() && os.format() == IOstream::ASCII)
|
||||
{
|
||||
scalarField exponents(dimensionSet::nDimensions);
|
||||
for (int d=0; d<dimensionSet::nDimensions; ++d)
|
||||
for (int d=0; d < dimensionSet::nDimensions; ++d)
|
||||
{
|
||||
exponents[d] = exponents_[d];
|
||||
}
|
||||
@ -675,7 +675,7 @@ Foam::Ostream& Foam::dimensionSet::write
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int d=0; d<dimensionSet::nDimensions; ++d)
|
||||
for (int d=0; d < dimensionSet::nDimensions; ++d)
|
||||
{
|
||||
if (d) os << token::SPACE;
|
||||
os << exponents_[d];
|
||||
|
||||
Reference in New Issue
Block a user