ENH: add static set/get dimensionSet::checking() method

- encapsulates toggling

STYLE: noexcept for some Time methods
This commit is contained in:
Mark Olesen
2021-10-26 22:39:37 +02:00
parent 33790ca972
commit f5058bca90
14 changed files with 98 additions and 66 deletions

View File

@ -678,7 +678,7 @@ int main(int argc, char *argv[])
} }
// Disable dimension checking during operations // Disable dimension checking during operations
dimensionSet::debug = false; dimensionSet::checking(false);
#include "createNamedMesh.H" #include "createNamedMesh.H"

View File

@ -197,7 +197,6 @@ public:
//- Search the case for the time closest to the given time //- Search the case for the time closest to the given time
instant findClosestTime(const scalar t) const; instant findClosestTime(const scalar t) const;
}; };

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2018 OpenCFD Ltd. Copyright (C) 2018-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -92,13 +92,13 @@ public:
inline scalar timeOutputValue() const; inline scalar timeOutputValue() const;
//- Return current time index //- Return current time index
inline label timeIndex() const; inline label timeIndex() const noexcept;
//- Return time step value //- Return time step value
inline scalar deltaTValue() const; inline scalar deltaTValue() const noexcept;
//- Return old time step value //- Return old time step value
inline scalar deltaT0Value() const; inline scalar deltaT0Value() const noexcept;
//- Return time step //- Return time step
inline dimensionedScalar deltaT() const; inline dimensionedScalar deltaT() const;
@ -109,12 +109,12 @@ public:
// Check // Check
//- Return true if this is a write time //- True if this is a write time
inline bool writeTime() const; inline bool writeTime() const noexcept;
//- Deprecated(2016-05) return true if this is a write time. //- Deprecated(2016-05) return true if this is a write time.
// \deprecated(2016-05) - use writeTime() method // \deprecated(2016-05) - use writeTime() method
inline bool outputTime() const { return this->writeTime(); } bool outputTime() const noexcept { return this->writeTime(); }
}; };

View File

@ -6,6 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2016 OpenFOAM Foundation Copyright (C) 2016 OpenFOAM Foundation
Copyright (C) 2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -33,19 +34,19 @@ inline Foam::scalar Foam::TimeState::timeOutputValue() const
} }
inline Foam::label Foam::TimeState::timeIndex() const inline Foam::label Foam::TimeState::timeIndex() const noexcept
{ {
return timeIndex_; return timeIndex_;
} }
inline Foam::scalar Foam::TimeState::deltaTValue() const inline Foam::scalar Foam::TimeState::deltaTValue() const noexcept
{ {
return deltaT_; return deltaT_;
} }
inline Foam::scalar Foam::TimeState::deltaT0Value() const inline Foam::scalar Foam::TimeState::deltaT0Value() const noexcept
{ {
return deltaT0_; return deltaT0_;
} }
@ -63,7 +64,7 @@ inline Foam::dimensionedScalar Foam::TimeState::deltaT0() const
} }
inline bool Foam::TimeState::writeTime() const inline bool Foam::TimeState::writeTime() const noexcept
{ {
return writeTime_; return writeTime_;
} }

View File

@ -33,12 +33,6 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::timeSelector::timeSelector()
:
scalarRanges()
{}
Foam::timeSelector::timeSelector(const std::string& str) Foam::timeSelector::timeSelector(const std::string& str)
: :
scalarRanges(str) scalarRanges(str)

View File

@ -82,7 +82,7 @@ SourceFiles
namespace Foam namespace Foam
{ {
// Forward declarations // Forward Declarations
class argList; class argList;
class Time; class Time;
@ -98,11 +98,11 @@ public:
// Constructors // Constructors
//- Construct null //- Default construct
timeSelector(); timeSelector() = default;
//- Construct by parsing string for time ranges //- Construct by parsing string for time ranges
timeSelector(const std::string& str); explicit timeSelector(const std::string& str);
// Member Functions // Member Functions
@ -165,7 +165,6 @@ public:
Time& runTime, Time& runTime,
const argList& args const argList& args
); );
}; };

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2017-2019 OpenCFD Ltd. Copyright (C) 2017-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -202,7 +202,7 @@ bool Foam::dimensionSet::operator!=(const dimensionSet& ds) const
bool Foam::dimensionSet::operator=(const dimensionSet& ds) const bool Foam::dimensionSet::operator=(const dimensionSet& ds) const
{ {
if (dimensionSet::debug) if (dimensionSet::checking())
{ {
checkDims("(a = b)", *this, ds); checkDims("(a = b)", *this, ds);
} }
@ -213,7 +213,7 @@ bool Foam::dimensionSet::operator=(const dimensionSet& ds) const
bool Foam::dimensionSet::operator+=(const dimensionSet& ds) const bool Foam::dimensionSet::operator+=(const dimensionSet& ds) const
{ {
if (dimensionSet::debug) if (dimensionSet::checking())
{ {
checkDims("(a += b)", *this, ds); checkDims("(a += b)", *this, ds);
} }
@ -224,7 +224,7 @@ bool Foam::dimensionSet::operator+=(const dimensionSet& ds) const
bool Foam::dimensionSet::operator-=(const dimensionSet& ds) const bool Foam::dimensionSet::operator-=(const dimensionSet& ds) const
{ {
if (dimensionSet::debug) if (dimensionSet::checking())
{ {
checkDims("(a -= b)", *this, ds); checkDims("(a -= b)", *this, ds);
} }
@ -253,7 +253,7 @@ bool Foam::dimensionSet::operator/=(const dimensionSet& ds)
Foam::dimensionSet Foam::min(const dimensionSet& ds1, const dimensionSet& ds2) Foam::dimensionSet Foam::min(const dimensionSet& ds1, const dimensionSet& ds2)
{ {
if (dimensionSet::debug) if (dimensionSet::checking())
{ {
checkDims("min(a, b)", ds1, ds2); checkDims("min(a, b)", ds1, ds2);
} }
@ -264,7 +264,7 @@ Foam::dimensionSet Foam::min(const dimensionSet& ds1, const dimensionSet& ds2)
Foam::dimensionSet Foam::max(const dimensionSet& ds1, const dimensionSet& ds2) Foam::dimensionSet Foam::max(const dimensionSet& ds1, const dimensionSet& ds2)
{ {
if (dimensionSet::debug) if (dimensionSet::checking())
{ {
checkDims("max(a, b)", ds1, ds2); checkDims("max(a, b)", ds1, ds2);
} }
@ -275,7 +275,7 @@ Foam::dimensionSet Foam::max(const dimensionSet& ds1, const dimensionSet& ds2)
Foam::dimensionSet Foam::clip(const dimensionSet& ds1, const dimensionSet& ds2) Foam::dimensionSet Foam::clip(const dimensionSet& ds1, const dimensionSet& ds2)
{ {
if (dimensionSet::debug) if (dimensionSet::checking())
{ {
checkDims("clip(a, b)", ds1, ds2); checkDims("clip(a, b)", ds1, ds2);
} }
@ -325,7 +325,7 @@ Foam::dimensionSet Foam::pow
const dimensionedScalar& dS const dimensionedScalar& dS
) )
{ {
if (dimensionSet::debug && !dS.dimensions().dimensionless()) if (dimensionSet::checking() && !dS.dimensions().dimensionless())
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Exponent of pow is not dimensionless" << endl << "Exponent of pow is not dimensionless" << endl
@ -344,7 +344,7 @@ Foam::dimensionSet Foam::pow
{ {
if if
( (
dimensionSet::debug dimensionSet::checking()
&& !dS.dimensions().dimensionless() && !dS.dimensions().dimensionless()
&& !ds.dimensionless() && !ds.dimensionless()
) )
@ -483,7 +483,7 @@ Foam::dimensionSet Foam::inv(const dimensionSet& ds)
Foam::dimensionSet Foam::trans(const dimensionSet& ds) Foam::dimensionSet Foam::trans(const dimensionSet& ds)
{ {
if (dimensionSet::debug && !ds.dimensionless()) if (dimensionSet::checking() && !ds.dimensionless())
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Argument of trancendental function not dimensionless" << nl << "Argument of trancendental function not dimensionless" << nl
@ -496,7 +496,7 @@ Foam::dimensionSet Foam::trans(const dimensionSet& ds)
Foam::dimensionSet Foam::atan2(const dimensionSet& ds1, const dimensionSet& ds2) Foam::dimensionSet Foam::atan2(const dimensionSet& ds1, const dimensionSet& ds2)
{ {
if (dimensionSet::debug) if (dimensionSet::checking())
{ {
checkDims("atan2(a, b)", ds1, ds2); checkDims("atan2(a, b)", ds1, ds2);
} }
@ -507,7 +507,7 @@ Foam::dimensionSet Foam::atan2(const dimensionSet& ds1, const dimensionSet& ds2)
Foam::dimensionSet Foam::hypot(const dimensionSet& ds1, const dimensionSet& ds2) Foam::dimensionSet Foam::hypot(const dimensionSet& ds1, const dimensionSet& ds2)
{ {
if (dimensionSet::debug) if (dimensionSet::checking())
{ {
checkDims("hypot(a, b)", ds1, ds2); checkDims("hypot(a, b)", ds1, ds2);
} }
@ -548,7 +548,7 @@ Foam::dimensionSet Foam::operator+
const dimensionSet& ds2 const dimensionSet& ds2
) )
{ {
if (dimensionSet::debug) if (dimensionSet::checking())
{ {
checkDims("(a + b)", ds1, ds2); checkDims("(a + b)", ds1, ds2);
} }
@ -563,7 +563,7 @@ Foam::dimensionSet Foam::operator-
const dimensionSet& ds2 const dimensionSet& ds2
) )
{ {
if (dimensionSet::debug) if (dimensionSet::checking())
{ {
checkDims("(a - b)", ds1, ds2); checkDims("(a - b)", ds1, ds2);
} }

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2017-2019 OpenCFD Ltd. Copyright (C) 2017-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -54,7 +54,7 @@ SourceFiles
namespace Foam namespace Foam
{ {
// Forward declarations // Forward Declarations
class dimensionSet; class dimensionSet;
class dimensionSets; class dimensionSets;
@ -97,17 +97,17 @@ public:
private: private:
// Private data // Private Data
//- The array of dimension exponents //- The array of dimension exponents
list_type exponents_; list_type exponents_;
// Private classes // Private Classes
class tokeniser class tokeniser
{ {
// Private data // Private Data
Istream& is_; Istream& is_;
@ -171,6 +171,24 @@ public:
ClassName("dimensionSet"); ClassName("dimensionSet");
// Static Functions
//- Turn dimension checking on/off.
// \return the previous value
static bool checking(const bool on) noexcept
{
bool old(debug);
debug = static_cast<int>(on);
return old;
}
//- True if dimension checking is enabled (the usual default)
static bool checking() noexcept
{
return static_cast<bool>(debug);
}
// Constructors // Constructors
//- Default construct (dimensionless). //- Default construct (dimensionless).

View File

@ -100,7 +100,7 @@ inline dimensioned<MinMax<T>> makeDimensionedMinMax
const dimensioned<T>& maxVal const dimensioned<T>& maxVal
) )
{ {
// Dimension check when (dimensionSet::debug) // Dimension check when dimensionSet::checking()
return dimensioned<MinMax<T>> return dimensioned<MinMax<T>>
( (
name, name,

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2016-2017 Wikki Ltd Copyright (C) 2016-2017 Wikki Ltd
Copyright (C) 2019-2020 OpenCFD Ltd. Copyright (C) 2019-2021 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -1049,7 +1049,11 @@ void Foam::checkMethod
<< abort(FatalError); << abort(FatalError);
} }
if (dimensionSet::debug && fam1.dimensions() != fam2.dimensions()) if
(
dimensionSet::checking()
&& fam1.dimensions() != fam2.dimensions()
)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "incompatible dimensions for operation " << "incompatible dimensions for operation "
@ -1070,7 +1074,11 @@ void Foam::checkMethod
const char* op const char* op
) )
{ {
if (dimensionSet::debug && fam.dimensions()/dimArea != vf.dimensions()) if
(
dimensionSet::checking()
&& fam.dimensions()/dimArea != vf.dimensions()
)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "incompatible dimensions for operation " << "incompatible dimensions for operation "
@ -1091,7 +1099,11 @@ void Foam::checkMethod
const char* op const char* op
) )
{ {
if (dimensionSet::debug && fam.dimensions()/dimArea != dt.dimensions()) if
(
dimensionSet::checking()
&& fam.dimensions()/dimArea != dt.dimensions()
)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "incompatible dimensions for operation " << "incompatible dimensions for operation "

View File

@ -405,8 +405,7 @@ Foam::tmp<GeomField> Foam::expressions::fvExprDriver::getOrReadFieldImpl
} }
// Switch dimension checking off // Switch dimension checking off
const int oldDebug = dimensionSet::debug; const bool oldDimChecking = dimensionSet::checking(false);
dimensionSet::debug = 0;
// go through ALL old times // go through ALL old times
GeomField* fp = &(fld); GeomField* fp = &(fld);
@ -418,7 +417,7 @@ Foam::tmp<GeomField> Foam::expressions::fvExprDriver::getOrReadFieldImpl
} }
// Restore old value of dimension checking // Restore old value of dimension checking
dimensionSet::debug = oldDebug; dimensionSet::checking(oldDimChecking);
} }
} }
else if (mandatory) else if (mandatory)

View File

@ -1992,7 +1992,11 @@ void Foam::checkMethod
<< abort(FatalError); << abort(FatalError);
} }
if (dimensionSet::debug && fvm1.dimensions() != fvm2.dimensions()) if
(
dimensionSet::checking()
&& fvm1.dimensions() != fvm2.dimensions()
)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "incompatible dimensions for operation " << "incompatible dimensions for operation "
@ -2013,7 +2017,11 @@ void Foam::checkMethod
const char* op const char* op
) )
{ {
if (dimensionSet::debug && fvm.dimensions()/dimVolume != df.dimensions()) if
(
dimensionSet::checking()
&& fvm.dimensions()/dimVolume != df.dimensions()
)
{ {
FatalErrorInFunction FatalErrorInFunction
<< endl << " " << endl << " "
@ -2033,7 +2041,11 @@ void Foam::checkMethod
const char* op const char* op
) )
{ {
if (dimensionSet::debug && fvm.dimensions()/dimVolume != dt.dimensions()) if
(
dimensionSet::checking()
&& fvm.dimensions()/dimVolume != dt.dimensions()
)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "incompatible dimensions for operation " << "incompatible dimensions for operation "

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2018-2019 OpenFOAM Foundation Copyright (C) 2018-2019 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd. Copyright (C) 2020-2021 OpenCFD Ltd.
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -50,13 +50,12 @@ bool Foam::functionObjects::log::calc()
{ {
const volScalarField& x = lookupObject<volScalarField>(fieldName_); const volScalarField& x = lookupObject<volScalarField>(fieldName_);
// Cache the current debug setting for dimensionSet
const bool dimensionSetDebug = dimensionSet::debug;
// Switch-off dimension checking if requested // Switch-off dimension checking if requested
const bool oldDimChecking = dimensionSet::checking();
if (!checkDimensions_) if (!checkDimensions_)
{ {
dimensionSet::debug = 0; dimensionSet::checking(false);
} }
bool stored = store bool stored = store
@ -68,7 +67,7 @@ bool Foam::functionObjects::log::calc()
// Reinstate dimension checking // Reinstate dimension checking
if (!checkDimensions_) if (!checkDimensions_)
{ {
dimensionSet::debug = dimensionSetDebug; dimensionSet::checking(oldDimChecking);
} }
return stored; return stored;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2020 OpenCFD Ltd. Copyright (C) 2020-2021 OpenCFD Ltd.
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -49,13 +49,12 @@ bool Foam::functionObjects::pow::calc()
{ {
const volScalarField& x = lookupObject<volScalarField>(fieldName_); const volScalarField& x = lookupObject<volScalarField>(fieldName_);
// Cache the current debug setting for dimensionSet
const bool dimensionSetDebug = dimensionSet::debug;
// Switch-off dimension checking if requested // Switch-off dimension checking if requested
const bool oldDimChecking = dimensionSet::checking();
if (!checkDimensions_) if (!checkDimensions_)
{ {
dimensionSet::debug = 0; dimensionSet::checking(false);
} }
bool stored = store bool stored = store
@ -67,7 +66,7 @@ bool Foam::functionObjects::pow::calc()
// Reinstate dimension checking // Reinstate dimension checking
if (!checkDimensions_) if (!checkDimensions_)
{ {
dimensionSet::debug = dimensionSetDebug; dimensionSet::checking(oldDimChecking);
} }
return stored; return stored;