ENH: consolidate table bounding (issue #520)

- replace duplicate code with global bounds enums and Enum
This commit is contained in:
Mark Olesen
2017-07-07 12:37:25 +02:00
parent 45dabbfe2b
commit 51d46079df
10 changed files with 258 additions and 402 deletions

View File

@ -123,6 +123,7 @@ $(ranges)/labelRange/labelRange.C
$(ranges)/labelRange/labelRanges.C $(ranges)/labelRange/labelRanges.C
$(ranges)/scalarRange/scalarRange.C $(ranges)/scalarRange/scalarRange.C
$(ranges)/scalarRange/scalarRanges.C $(ranges)/scalarRange/scalarRanges.C
$(ranges)/tableBounds/tableBounds.C
spatialVectorAlgebra = primitives/spatialVectorAlgebra spatialVectorAlgebra = primitives/spatialVectorAlgebra
$(spatialVectorAlgebra)/SpatialVector/spatialVector/spatialVector.C $(spatialVectorAlgebra)/SpatialVector/spatialVector/spatialVector.C

View File

@ -55,7 +55,7 @@ template<class Type>
Foam::interpolation2DTable<Type>::interpolation2DTable() Foam::interpolation2DTable<Type>::interpolation2DTable()
: :
List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>(), List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>(),
boundsHandling_(interpolation2DTable::WARN), bounding_(bounds::normalBounding::WARN),
fileName_("fileNameIsUndefined"), fileName_("fileNameIsUndefined"),
reader_(nullptr) reader_(nullptr)
{} {}
@ -65,12 +65,12 @@ template<class Type>
Foam::interpolation2DTable<Type>::interpolation2DTable Foam::interpolation2DTable<Type>::interpolation2DTable
( (
const List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>& values, const List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>& values,
const boundsHandling bounds, const bounds::normalBounding bounding,
const fileName& fName const fileName& fName
) )
: :
List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>(values), List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>(values),
boundsHandling_(bounds), bounding_(bounding),
fileName_(fName), fileName_(fName),
reader_(nullptr) reader_(nullptr)
{} {}
@ -80,7 +80,7 @@ template<class Type>
Foam::interpolation2DTable<Type>::interpolation2DTable(const fileName& fName) Foam::interpolation2DTable<Type>::interpolation2DTable(const fileName& fName)
: :
List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>(), List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>(),
boundsHandling_(interpolation2DTable::WARN), bounding_(bounds::normalBounding::WARN),
fileName_(fName), fileName_(fName),
reader_(new openFoamTableReader<Type>(dictionary())) reader_(new openFoamTableReader<Type>(dictionary()))
{ {
@ -92,7 +92,15 @@ template<class Type>
Foam::interpolation2DTable<Type>::interpolation2DTable(const dictionary& dict) Foam::interpolation2DTable<Type>::interpolation2DTable(const dictionary& dict)
: :
List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>(), List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>(),
boundsHandling_(wordToBoundsHandling(dict.lookup("outOfBounds"))), bounding_
(
bounds::normalBoundingNames.lookupOrFailsafe
(
"outOfBounds",
dict,
bounds::normalBounding::WARN
)
),
fileName_(dict.lookup("file")), fileName_(dict.lookup("file")),
reader_(tableReader<Type>::New(dict)) reader_(tableReader<Type>::New(dict))
{ {
@ -107,7 +115,7 @@ Foam::interpolation2DTable<Type>::interpolation2DTable
) )
: :
List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>(interpTable), List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>(interpTable),
boundsHandling_(interpTable.boundsHandling_), bounding_(interpTable.bounding_),
fileName_(interpTable.fileName_), fileName_(interpTable.fileName_),
reader_(interpTable.reader_) // note: steals reader. Used in write(). reader_(interpTable.reader_) // note: steals reader. Used in write().
{} {}
@ -130,9 +138,9 @@ Type Foam::interpolation2DTable<Type>::interpolateValue
if (lookupValue < minLimit) if (lookupValue < minLimit)
{ {
switch (boundsHandling_) switch (bounding_)
{ {
case interpolation2DTable::ERROR: case bounds::normalBounding::ERROR:
{ {
FatalErrorInFunction FatalErrorInFunction
<< "value (" << lookupValue << ") less than lower " << "value (" << lookupValue << ") less than lower "
@ -140,18 +148,18 @@ Type Foam::interpolation2DTable<Type>::interpolateValue
<< exit(FatalError); << exit(FatalError);
break; break;
} }
case interpolation2DTable::WARN: case bounds::normalBounding::WARN:
{ {
WarningInFunction WarningInFunction
<< "value (" << lookupValue << ") less than lower " << "value (" << lookupValue << ") less than lower "
<< "bound (" << minLimit << ")" << nl << "bound (" << minLimit << ")" << nl
<< " Continuing with the first entry" << " Continuing with the first entry"
<< endl; << endl;
// Behaviour as per 'CLAMP' // Behaviour as per CLAMP
return data.first().second(); return data.first().second();
break; break;
} }
case interpolation2DTable::CLAMP: case bounds::normalBounding::CLAMP:
{ {
return data.first().second(); return data.first().second();
break; break;
@ -160,9 +168,9 @@ Type Foam::interpolation2DTable<Type>::interpolateValue
} }
else if (lookupValue >= maxLimit) else if (lookupValue >= maxLimit)
{ {
switch (boundsHandling_) switch (bounding_)
{ {
case interpolation2DTable::ERROR: case bounds::normalBounding::ERROR:
{ {
FatalErrorInFunction FatalErrorInFunction
<< "value (" << lookupValue << ") greater than upper " << "value (" << lookupValue << ") greater than upper "
@ -170,18 +178,18 @@ Type Foam::interpolation2DTable<Type>::interpolateValue
<< exit(FatalError); << exit(FatalError);
break; break;
} }
case interpolation2DTable::WARN: case bounds::normalBounding::WARN:
{ {
WarningInFunction WarningInFunction
<< "value (" << lookupValue << ") greater than upper " << "value (" << lookupValue << ") greater than upper "
<< "bound (" << maxLimit << ")" << nl << "bound (" << maxLimit << ")" << nl
<< " Continuing with the last entry" << " Continuing with the last entry"
<< endl; << endl;
// Behaviour as per 'CLAMP' // Behaviour as per CLAMP
return data.last().second(); return data.last().second();
break; break;
} }
case interpolation2DTable::CLAMP: case bounds::normalBounding::CLAMP:
{ {
return data.last().second(); return data.last().second();
break; break;
@ -241,25 +249,25 @@ Foam::label Foam::interpolation2DTable<Type>::Xi
if (bop(valueX, t[limitI].first())) if (bop(valueX, t[limitI].first()))
{ {
switch (boundsHandling_) switch (bounding_)
{ {
case interpolation2DTable::ERROR: case bounds::normalBounding::ERROR:
{ {
FatalErrorInFunction FatalErrorInFunction
<< "value (" << valueX << ") out of bounds" << "value (" << valueX << ") out of bounds"
<< exit(FatalError); << exit(FatalError);
break; break;
} }
case interpolation2DTable::WARN: case bounds::normalBounding::WARN:
{ {
WarningInFunction WarningInFunction
<< "value (" << valueX << ") out of bounds" << "value (" << valueX << ") out of bounds"
<< endl; << endl;
// Behaviour as per 'CLAMP' // Behaviour as per CLAMP
return limitI; return limitI;
break; break;
} }
case interpolation2DTable::CLAMP: case bounds::normalBounding::CLAMP:
{ {
return limitI; return limitI;
break; break;
@ -267,7 +275,7 @@ Foam::label Foam::interpolation2DTable<Type>::Xi
default: default:
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Unhandled enumeration " << boundsHandling_ << "Unhandled bounding type " << int(bounding_)
<< abort(FatalError); << abort(FatalError);
} }
} }
@ -351,79 +359,6 @@ Type Foam::interpolation2DTable<Type>::operator()
} }
template<class Type>
Foam::word Foam::interpolation2DTable<Type>::boundsHandlingToWord
(
const boundsHandling& bound
) const
{
word enumName("warn");
switch (bound)
{
case interpolation2DTable::ERROR:
{
enumName = "error";
break;
}
case interpolation2DTable::WARN:
{
enumName = "warn";
break;
}
case interpolation2DTable::CLAMP:
{
enumName = "clamp";
break;
}
}
return enumName;
}
template<class Type>
typename Foam::interpolation2DTable<Type>::boundsHandling
Foam::interpolation2DTable<Type>::wordToBoundsHandling
(
const word& bound
) const
{
if (bound == "error")
{
return interpolation2DTable::ERROR;
}
else if (bound == "warn")
{
return interpolation2DTable::WARN;
}
else if (bound == "clamp")
{
return interpolation2DTable::CLAMP;
}
else
{
WarningInFunction
<< "bad outOfBounds specifier " << bound << " using 'warn'" << endl;
return interpolation2DTable::WARN;
}
}
template<class Type>
typename Foam::interpolation2DTable<Type>::boundsHandling
Foam::interpolation2DTable<Type>::outOfBounds
(
const boundsHandling& bound
)
{
boundsHandling prev = boundsHandling_;
boundsHandling_ = bound;
return prev;
}
template<class Type> template<class Type>
void Foam::interpolation2DTable<Type>::checkOrder() const void Foam::interpolation2DTable<Type>::checkOrder() const
{ {
@ -453,7 +388,7 @@ template<class Type>
void Foam::interpolation2DTable<Type>::write(Ostream& os) const void Foam::interpolation2DTable<Type>::write(Ostream& os) const
{ {
os.writeEntry("file", fileName_); os.writeEntry("file", fileName_);
os.writeEntry("outOfBounds", boundsHandlingToWord(boundsHandling_)); os.writeEntry("outOfBounds", bounds::normalBoundingNames[bounding_]);
os << *this; os << *this;
} }

View File

@ -38,6 +38,7 @@ SourceFiles
#include "List.H" #include "List.H"
#include "Tuple2.H" #include "Tuple2.H"
#include "tableBounds.H"
#include "tableReader.H" #include "tableReader.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -58,14 +59,6 @@ public:
// Public data types // Public data types
//- Enumeration for handling out-of-bound values
enum boundsHandling
{
ERROR, //!< Exit with a FatalError
WARN, //!< Issue warning and clamp value (default)
CLAMP //!< Clamp value to the start/end value
};
//- Convenience typedef //- Convenience typedef
typedef List<Tuple2<scalar, List<Tuple2<scalar, Type>>>> table; typedef List<Tuple2<scalar, List<Tuple2<scalar, Type>>>> table;
@ -74,8 +67,8 @@ private:
// Private data // Private data
//- Enumeration for handling out-of-bound values //- Handling for out-of-bound values
boundsHandling boundsHandling_; bounds::normalBounding bounding_;
//- File name //- File name
fileName fileName_; fileName fileName_;
@ -117,14 +110,14 @@ public:
interpolation2DTable interpolation2DTable
( (
const List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>& values, const List<Tuple2<scalar, List<Tuple2<scalar, Type>>>>& values,
const boundsHandling bounds, const bounds::normalBounding bounding,
const fileName& fName const fileName& fName
); );
//- Construct given the name of the file containing the table of data //- Construct given the name of the file containing the table of data
interpolation2DTable(const fileName& fName); interpolation2DTable(const fileName& fName);
//- Construct by reading the fileName and boundsHandling from dictionary //- Construct by reading file name and outOfBounds from dictionary
interpolation2DTable(const dictionary& dict); interpolation2DTable(const dictionary& dict);
//- Construct copy //- Construct copy
@ -133,15 +126,6 @@ public:
// Member Functions // Member Functions
//- Return the out-of-bounds handling as a word
word boundsHandlingToWord(const boundsHandling& bound) const;
//- Return the out-of-bounds handling as an enumeration
boundsHandling wordToBoundsHandling(const word& bound) const;
//- Set the out-of-bounds handling from enum, return previous setting
boundsHandling outOfBounds(const boundsHandling& bound);
//- Check that list is monotonically increasing //- Check that list is monotonically increasing
// Exit with a FatalError if there is a problem // Exit with a FatalError if there is a problem
void checkOrder() const; void checkOrder() const;
@ -152,7 +136,7 @@ public:
// Member Operators // Member Operators
//- Return an element of constant Tuple2<scalar, Type> //- Return an element of constant List<Tuple2<scalar, Type>>
const List<Tuple2<scalar, Type>>& operator[](const label) const; const List<Tuple2<scalar, Type>>& operator[](const label) const;
//- Return an interpolated value //- Return an interpolated value

View File

@ -59,7 +59,7 @@ template<class Type>
Foam::interpolationTable<Type>::interpolationTable() Foam::interpolationTable<Type>::interpolationTable()
: :
List<Tuple2<scalar, Type>>(), List<Tuple2<scalar, Type>>(),
boundsHandling_(interpolationTable::WARN), bounding_(bounds::repeatableBounding::WARN),
fileName_("fileNameIsUndefined"), fileName_("fileNameIsUndefined"),
reader_(nullptr) reader_(nullptr)
{} {}
@ -69,12 +69,12 @@ template<class Type>
Foam::interpolationTable<Type>::interpolationTable Foam::interpolationTable<Type>::interpolationTable
( (
const List<Tuple2<scalar, Type>>& values, const List<Tuple2<scalar, Type>>& values,
const boundsHandling bounds, const bounds::repeatableBounding bounding,
const fileName& fName const fileName& fName
) )
: :
List<Tuple2<scalar, Type>>(values), List<Tuple2<scalar, Type>>(values),
boundsHandling_(bounds), bounding_(bounding),
fileName_(fName), fileName_(fName),
reader_(nullptr) reader_(nullptr)
{} {}
@ -84,7 +84,7 @@ template<class Type>
Foam::interpolationTable<Type>::interpolationTable(const fileName& fName) Foam::interpolationTable<Type>::interpolationTable(const fileName& fName)
: :
List<Tuple2<scalar, Type>>(), List<Tuple2<scalar, Type>>(),
boundsHandling_(interpolationTable::WARN), bounding_(bounds::repeatableBounding::WARN),
fileName_(fName), fileName_(fName),
reader_(new openFoamTableReader<Type>(dictionary())) reader_(new openFoamTableReader<Type>(dictionary()))
{ {
@ -96,7 +96,15 @@ template<class Type>
Foam::interpolationTable<Type>::interpolationTable(const dictionary& dict) Foam::interpolationTable<Type>::interpolationTable(const dictionary& dict)
: :
List<Tuple2<scalar, Type>>(), List<Tuple2<scalar, Type>>(),
boundsHandling_(wordToBoundsHandling(dict.lookup("outOfBounds"))), bounding_
(
bounds::repeatableBoundingNames.lookupOrFailsafe
(
"outOfBounds",
dict,
bounds::repeatableBounding::WARN
)
),
fileName_(dict.lookup("file")), fileName_(dict.lookup("file")),
reader_(tableReader<Type>::New(dict)) reader_(tableReader<Type>::New(dict))
{ {
@ -111,7 +119,7 @@ Foam::interpolationTable<Type>::interpolationTable
) )
: :
List<Tuple2<scalar, Type>>(interpTable), List<Tuple2<scalar, Type>>(interpTable),
boundsHandling_(interpTable.boundsHandling_), bounding_(interpTable.bounding_),
fileName_(interpTable.fileName_), fileName_(interpTable.fileName_),
reader_(interpTable.reader_) // note: steals reader. Used in write(). reader_(interpTable.reader_) // note: steals reader. Used in write().
{} {}
@ -120,88 +128,6 @@ Foam::interpolationTable<Type>::interpolationTable
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
Foam::word Foam::interpolationTable<Type>::boundsHandlingToWord
(
const boundsHandling& bound
) const
{
word enumName("warn");
switch (bound)
{
case interpolationTable::ERROR:
{
enumName = "error";
break;
}
case interpolationTable::WARN:
{
enumName = "warn";
break;
}
case interpolationTable::CLAMP:
{
enumName = "clamp";
break;
}
case interpolationTable::REPEAT:
{
enumName = "repeat";
break;
}
}
return enumName;
}
template<class Type>
typename Foam::interpolationTable<Type>::boundsHandling
Foam::interpolationTable<Type>::wordToBoundsHandling
(
const word& bound
) const
{
if (bound == "error")
{
return interpolationTable::ERROR;
}
else if (bound == "warn")
{
return interpolationTable::WARN;
}
else if (bound == "clamp")
{
return interpolationTable::CLAMP;
}
else if (bound == "repeat")
{
return interpolationTable::REPEAT;
}
else
{
WarningInFunction
<< "bad outOfBounds specifier " << bound << " using 'warn'" << endl;
return interpolationTable::WARN;
}
}
template<class Type>
typename Foam::interpolationTable<Type>::boundsHandling
Foam::interpolationTable<Type>::outOfBounds
(
const boundsHandling& bound
)
{
boundsHandling prev = boundsHandling_;
boundsHandling_ = bound;
return prev;
}
template<class Type> template<class Type>
void Foam::interpolationTable<Type>::check() const void Foam::interpolationTable<Type>::check() const
{ {
@ -230,7 +156,7 @@ template<class Type>
void Foam::interpolationTable<Type>::write(Ostream& os) const void Foam::interpolationTable<Type>::write(Ostream& os) const
{ {
os.writeEntry("file", fileName_); os.writeEntry("file", fileName_);
os.writeEntry("outOfBounds", boundsHandlingToWord(boundsHandling_)); os.writeEntry("outOfBounds", bounds::repeatableBoundingNames[bounding_]);
if (reader_.valid()) if (reader_.valid())
{ {
reader_->write(os); reader_->write(os);
@ -255,33 +181,33 @@ Type Foam::interpolationTable<Type>::rateOfChange(const scalar value) const
if (lookupValue < minLimit) if (lookupValue < minLimit)
{ {
switch (boundsHandling_) switch (bounding_)
{ {
case interpolationTable::ERROR: case bounds::repeatableBounding::ERROR:
{ {
FatalErrorInFunction FatalErrorInFunction
<< "value (" << lookupValue << ") underflow" << nl << "value (" << lookupValue << ") underflow" << nl
<< exit(FatalError); << exit(FatalError);
break; break;
} }
case interpolationTable::WARN: case bounds::repeatableBounding::WARN:
{ {
WarningInFunction WarningInFunction
<< "value (" << lookupValue << ") underflow" << nl << "value (" << lookupValue << ") underflow" << nl
<< " Zero rate of change." << " Zero rate of change."
<< endl; << endl;
// behaviour as per 'CLAMP' // Behaviour as per CLAMP
return 0; return 0;
break; break;
} }
case interpolationTable::CLAMP: case bounds::repeatableBounding::CLAMP:
{ {
return 0; return 0;
break; break;
} }
case interpolationTable::REPEAT: case bounds::repeatableBounding::REPEAT:
{ {
// adjust lookupValue to >= minLimit // Adjust lookupValue to >= minLimit
scalar span = maxLimit-minLimit; scalar span = maxLimit-minLimit;
lookupValue = fmod(lookupValue-minLimit, span) + minLimit; lookupValue = fmod(lookupValue-minLimit, span) + minLimit;
break; break;
@ -290,33 +216,33 @@ Type Foam::interpolationTable<Type>::rateOfChange(const scalar value) const
} }
else if (lookupValue >= maxLimit) else if (lookupValue >= maxLimit)
{ {
switch (boundsHandling_) switch (bounding_)
{ {
case interpolationTable::ERROR: case bounds::repeatableBounding::ERROR:
{ {
FatalErrorInFunction FatalErrorInFunction
<< "value (" << lookupValue << ") overflow" << nl << "value (" << lookupValue << ") overflow" << nl
<< exit(FatalError); << exit(FatalError);
break; break;
} }
case interpolationTable::WARN: case bounds::repeatableBounding::WARN:
{ {
WarningInFunction WarningInFunction
<< "value (" << lookupValue << ") overflow" << nl << "value (" << lookupValue << ") overflow" << nl
<< " Zero rate of change." << " Zero rate of change."
<< endl; << endl;
// Behaviour as per 'CLAMP' // Behaviour as per CLAMP
return 0; return 0;
break; break;
} }
case interpolationTable::CLAMP: case bounds::repeatableBounding::CLAMP:
{ {
return 0; return 0;
break; break;
} }
case interpolationTable::REPEAT: case bounds::repeatableBounding::REPEAT:
{ {
// adjust lookupValue <= maxLimit // Adjust lookupValue <= maxLimit
scalar span = maxLimit-minLimit; scalar span = maxLimit-minLimit;
lookupValue = fmod(lookupValue-minLimit, span) + minLimit; lookupValue = fmod(lookupValue-minLimit, span) + minLimit;
break; break;
@ -401,16 +327,16 @@ Foam::interpolationTable<Type>::operator[](const label i) const
} }
else if (ii < 0) else if (ii < 0)
{ {
switch (boundsHandling_) switch (bounding_)
{ {
case interpolationTable::ERROR: case bounds::repeatableBounding::ERROR:
{ {
FatalErrorInFunction FatalErrorInFunction
<< "index (" << ii << ") underflow" << nl << "index (" << ii << ") underflow" << nl
<< exit(FatalError); << exit(FatalError);
break; break;
} }
case interpolationTable::WARN: case bounds::repeatableBounding::WARN:
{ {
WarningInFunction WarningInFunction
<< "index (" << ii << ") underflow" << nl << "index (" << ii << ") underflow" << nl
@ -420,12 +346,12 @@ Foam::interpolationTable<Type>::operator[](const label i) const
ii = 0; ii = 0;
break; break;
} }
case interpolationTable::CLAMP: case bounds::repeatableBounding::CLAMP:
{ {
ii = 0; ii = 0;
break; break;
} }
case interpolationTable::REPEAT: case bounds::repeatableBounding::REPEAT:
{ {
while (ii < 0) while (ii < 0)
{ {
@ -437,16 +363,16 @@ Foam::interpolationTable<Type>::operator[](const label i) const
} }
else if (ii >= n) else if (ii >= n)
{ {
switch (boundsHandling_) switch (bounding_)
{ {
case interpolationTable::ERROR: case bounds::repeatableBounding::ERROR:
{ {
FatalErrorInFunction FatalErrorInFunction
<< "index (" << ii << ") overflow" << nl << "index (" << ii << ") overflow" << nl
<< exit(FatalError); << exit(FatalError);
break; break;
} }
case interpolationTable::WARN: case bounds::repeatableBounding::WARN:
{ {
WarningInFunction WarningInFunction
<< "index (" << ii << ") overflow" << nl << "index (" << ii << ") overflow" << nl
@ -456,12 +382,12 @@ Foam::interpolationTable<Type>::operator[](const label i) const
ii = n - 1; ii = n - 1;
break; break;
} }
case interpolationTable::CLAMP: case bounds::repeatableBounding::CLAMP:
{ {
ii = n - 1; ii = n - 1;
break; break;
} }
case interpolationTable::REPEAT: case bounds::repeatableBounding::REPEAT:
{ {
while (ii >= n) while (ii >= n)
{ {
@ -492,31 +418,31 @@ Type Foam::interpolationTable<Type>::operator()(const scalar value) const
if (lookupValue < minLimit) if (lookupValue < minLimit)
{ {
switch (boundsHandling_) switch (bounding_)
{ {
case interpolationTable::ERROR: case bounds::repeatableBounding::ERROR:
{ {
FatalErrorInFunction FatalErrorInFunction
<< "value (" << lookupValue << ") underflow" << nl << "value (" << lookupValue << ") underflow" << nl
<< exit(FatalError); << exit(FatalError);
break; break;
} }
case interpolationTable::WARN: case bounds::repeatableBounding::WARN:
{ {
WarningInFunction WarningInFunction
<< "value (" << lookupValue << ") underflow" << nl << "value (" << lookupValue << ") underflow" << nl
<< " Continuing with the first entry" << " Continuing with the first entry"
<< endl; << endl;
// Behaviour as per 'CLAMP' // Behaviour as per CLAMP
return this->first().second(); return this->first().second();
break; break;
} }
case interpolationTable::CLAMP: case bounds::repeatableBounding::CLAMP:
{ {
return this->first().second(); return this->first().second();
break; break;
} }
case interpolationTable::REPEAT: case bounds::repeatableBounding::REPEAT:
{ {
// adjust lookupValue to >= minLimit // adjust lookupValue to >= minLimit
const scalar span = maxLimit-minLimit; const scalar span = maxLimit-minLimit;
@ -527,16 +453,16 @@ Type Foam::interpolationTable<Type>::operator()(const scalar value) const
} }
else if (lookupValue >= maxLimit) else if (lookupValue >= maxLimit)
{ {
switch (boundsHandling_) switch (bounding_)
{ {
case interpolationTable::ERROR: case bounds::repeatableBounding::ERROR:
{ {
FatalErrorInFunction FatalErrorInFunction
<< "value (" << lookupValue << ") overflow" << nl << "value (" << lookupValue << ") overflow" << nl
<< exit(FatalError); << exit(FatalError);
break; break;
} }
case interpolationTable::WARN: case bounds::repeatableBounding::WARN:
{ {
WarningInFunction WarningInFunction
<< "value (" << lookupValue << ") overflow" << nl << "value (" << lookupValue << ") overflow" << nl
@ -546,12 +472,12 @@ Type Foam::interpolationTable<Type>::operator()(const scalar value) const
return this->last().second(); return this->last().second();
break; break;
} }
case interpolationTable::CLAMP: case bounds::repeatableBounding::CLAMP:
{ {
return this->last().second(); return this->last().second();
break; break;
} }
case interpolationTable::REPEAT: case bounds::repeatableBounding::REPEAT:
{ {
// adjust lookupValue <= maxLimit // adjust lookupValue <= maxLimit
const scalar span = maxLimit-minLimit; const scalar span = maxLimit-minLimit;

View File

@ -64,6 +64,7 @@ SourceFiles
#include "List.H" #include "List.H"
#include "Tuple2.H" #include "Tuple2.H"
#include "tableBounds.H"
#include "tableReader.H" #include "tableReader.H"
#include "autoPtr.H" #include "autoPtr.H"
@ -81,26 +82,10 @@ class interpolationTable
: :
public List<Tuple2<scalar, Type>> public List<Tuple2<scalar, Type>>
{ {
public:
// Public data types
//- Enumeration for handling out-of-bound values
enum boundsHandling
{
ERROR, //!< Exit with a FatalError
WARN, //!< Issue warning and clamp value (default)
CLAMP, //!< Clamp value to the start/end value
REPEAT //!< Treat as a repeating list
};
private:
// Private data // Private data
//- Enumeration for handling out-of-bound values //- Handling for out-of-bound values
boundsHandling boundsHandling_; bounds::repeatableBounding bounding_;
//- File name //- File name
fileName fileName_; fileName fileName_;
@ -108,6 +93,7 @@ private:
//- The actual reader //- The actual reader
autoPtr<tableReader<Type>> reader_; autoPtr<tableReader<Type>> reader_;
// Private Member Functions // Private Member Functions
//- Read the table of data from file //- Read the table of data from file
@ -125,14 +111,14 @@ public:
interpolationTable interpolationTable
( (
const List<Tuple2<scalar, Type>>& values, const List<Tuple2<scalar, Type>>& values,
const boundsHandling bounds, const bounds::repeatableBounding bounding,
const fileName& fName const fileName& fName
); );
//- Construct given the name of the file containing the table of data //- Construct given the name of the file containing the table of data
interpolationTable(const fileName& fName); interpolationTable(const fileName& fName);
//- Construct by reading the fileName and boundsHandling from dictionary //- Construct by reading file name and outOfBounds from dictionary
// and read the table from that file. // and read the table from that file.
// This is a specialised constructor used by patchFields // This is a specialised constructor used by patchFields
interpolationTable(const dictionary& dict); interpolationTable(const dictionary& dict);
@ -143,15 +129,6 @@ public:
// Member Functions // Member Functions
//- Return the out-of-bounds handling as a word
word boundsHandlingToWord(const boundsHandling& bound) const;
//- Return the out-of-bounds handling as an enumeration
boundsHandling wordToBoundsHandling(const word& bound) const;
//- Set the out-of-bounds handling from enum, return previous setting
boundsHandling outOfBounds(const boundsHandling& bound);
//- Check that list is monotonically increasing //- Check that list is monotonically increasing
// Exit with a FatalError if there is a problem // Exit with a FatalError if there is a problem
void check() const; void check() const;

View File

@ -64,11 +64,13 @@ Foam::Function1Types::TableBase<Type>::TableBase
: :
Function1<Type>(name), Function1<Type>(name),
name_(name), name_(name),
boundsHandling_ bounding_
( (
wordToBoundsHandling bounds::repeatableBoundingNames.lookupOrFailsafe
( (
dict.lookupOrDefault<word>("outOfBounds", "clamp") "outOfBounds",
dict,
bounds::repeatableBounding::CLAMP
) )
), ),
interpolationScheme_ interpolationScheme_
@ -84,7 +86,7 @@ Foam::Function1Types::TableBase<Type>::TableBase(const TableBase<Type>& tbl)
: :
Function1<Type>(tbl), Function1<Type>(tbl),
name_(tbl.name_), name_(tbl.name_),
boundsHandling_(tbl.boundsHandling_), bounding_(tbl.bounding_),
interpolationScheme_(tbl.interpolationScheme_), interpolationScheme_(tbl.interpolationScheme_),
table_(tbl.table_), table_(tbl.table_),
tableSamplesPtr_(tbl.tableSamplesPtr_), tableSamplesPtr_(tbl.tableSamplesPtr_),
@ -101,90 +103,6 @@ Foam::Function1Types::TableBase<Type>::~TableBase()
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
Foam::word Foam::Function1Types::TableBase<Type>::boundsHandlingToWord
(
const boundsHandling& bound
) const
{
word enumName("warn");
switch (bound)
{
case ERROR:
{
enumName = "error";
break;
}
case WARN:
{
enumName = "warn";
break;
}
case CLAMP:
{
enumName = "clamp";
break;
}
case REPEAT:
{
enumName = "repeat";
break;
}
}
return enumName;
}
template<class Type>
typename Foam::Function1Types::TableBase<Type>::boundsHandling
Foam::Function1Types::TableBase<Type>::wordToBoundsHandling
(
const word& bound
) const
{
if (bound == "error")
{
return ERROR;
}
else if (bound == "warn")
{
return WARN;
}
else if (bound == "clamp")
{
return CLAMP;
}
else if (bound == "repeat")
{
return REPEAT;
}
else
{
WarningInFunction
<< "bad outOfBounds specifier " << bound << " using 'warn'"
<< endl;
return WARN;
}
}
template<class Type>
typename Foam::Function1Types::TableBase<Type>::boundsHandling
Foam::Function1Types::TableBase<Type>::outOfBounds
(
const boundsHandling& bound
)
{
boundsHandling prev = boundsHandling_;
boundsHandling_ = bound;
return prev;
}
template<class Type> template<class Type>
void Foam::Function1Types::TableBase<Type>::check() const void Foam::Function1Types::TableBase<Type>::check() const
{ {
@ -223,33 +141,33 @@ bool Foam::Function1Types::TableBase<Type>::checkMinBounds
{ {
if (x < table_.first().first()) if (x < table_.first().first())
{ {
switch (boundsHandling_) switch (bounding_)
{ {
case ERROR: case bounds::repeatableBounding::ERROR:
{ {
FatalErrorInFunction FatalErrorInFunction
<< "value (" << x << ") underflow" << "value (" << x << ") underflow"
<< exit(FatalError); << exit(FatalError);
break; break;
} }
case WARN: case bounds::repeatableBounding::WARN:
{ {
WarningInFunction WarningInFunction
<< "value (" << x << ") underflow" << nl << "value (" << x << ") underflow" << nl
<< endl; << endl;
// Behaviour as per 'CLAMP' // Behaviour as per CLAMP
xDash = table_.first().first(); xDash = table_.first().first();
return true; return true;
break; break;
} }
case CLAMP: case bounds::repeatableBounding::CLAMP:
{ {
xDash = table_.first().first(); xDash = table_.first().first();
return true; return true;
break; break;
} }
case REPEAT: case bounds::repeatableBounding::REPEAT:
{ {
// adjust x to >= minX // adjust x to >= minX
const scalar span = const scalar span =
@ -282,33 +200,33 @@ bool Foam::Function1Types::TableBase<Type>::checkMaxBounds
{ {
if (x > table_.last().first()) if (x > table_.last().first())
{ {
switch (boundsHandling_) switch (bounding_)
{ {
case ERROR: case bounds::repeatableBounding::ERROR:
{ {
FatalErrorInFunction FatalErrorInFunction
<< "value (" << x << ") overflow" << "value (" << x << ") overflow"
<< exit(FatalError); << exit(FatalError);
break; break;
} }
case WARN: case bounds::repeatableBounding::WARN:
{ {
WarningInFunction WarningInFunction
<< "value (" << x << ") overflow" << nl << "value (" << x << ") overflow" << nl
<< endl; << endl;
// Behaviour as per 'CLAMP' // Behaviour as per CLAMP
xDash = table_.last().first(); xDash = table_.last().first();
return true; return true;
break; break;
} }
case CLAMP: case bounds::repeatableBounding::CLAMP:
{ {
xDash = table_.last().first(); xDash = table_.last().first();
return true; return true;
break; break;
} }
case REPEAT: case bounds::repeatableBounding::REPEAT:
{ {
// adjust x to >= minX // adjust x to >= minX
const scalar span = const scalar span =
@ -427,9 +345,13 @@ Foam::tmp<Foam::Field<Type>> Foam::Function1Types::TableBase<Type>::y() const
template<class Type> template<class Type>
void Foam::Function1Types::TableBase<Type>::writeEntries(Ostream& os) const void Foam::Function1Types::TableBase<Type>::writeEntries(Ostream& os) const
{ {
if (boundsHandling_ != CLAMP) if (bounding_ != bounds::repeatableBounding::CLAMP)
{ {
os.writeEntry("outOfBounds", boundsHandlingToWord(boundsHandling_)); os.writeEntry
(
"outOfBounds",
bounds::repeatableBoundingNames[bounding_]
);
} }
if (interpolationScheme_ != "linear") if (interpolationScheme_ != "linear")
{ {

View File

@ -35,6 +35,7 @@ SourceFiles
#ifndef TableBase_H #ifndef TableBase_H
#define TableBase_H #define TableBase_H
#include "tableBounds.H"
#include "Function1.H" #include "Function1.H"
#include "Tuple2.H" #include "Tuple2.H"
@ -57,20 +58,6 @@ class TableBase
: :
public Function1<Type> public Function1<Type>
{ {
public:
// Public data types
//- Enumeration for handling out-of-bound values
enum boundsHandling
{
ERROR, //!< Exit with a FatalError
WARN, //!< Issue warning and clamp value (default)
CLAMP, //!< Clamp value to the start/end value
REPEAT //!< Treat as a repeating list
};
protected: protected:
// Protected data // Protected data
@ -78,8 +65,8 @@ protected:
//- Table name //- Table name
const word name_; const word name_;
//- Enumeration for handling out-of-bound values //- Handling for out-of-bound values
const boundsHandling boundsHandling_; const bounds::repeatableBounding bounding_;
//- Interpolation type //- Interpolation type
const word interpolationScheme_; const word interpolationScheme_;
@ -108,7 +95,7 @@ protected:
private: private:
//- Disallow default bitwise assignment //- Disallow default bitwise assignment
void operator=(const TableBase<Type>&); void operator=(const TableBase<Type>&) = delete;
public: public:
@ -128,15 +115,6 @@ public:
// Member Functions // Member Functions
//- Return the out-of-bounds handling as a word
word boundsHandlingToWord(const boundsHandling& bound) const;
//- Return the out-of-bounds handling as an enumeration
boundsHandling wordToBoundsHandling(const word& bound) const;
//- Set the out-of-bounds handling from enum, return previous setting
boundsHandling outOfBounds(const boundsHandling& bound);
//- Check the table for size and consistency //- Check the table for size and consistency
void check() const; void check() const;
@ -164,8 +142,8 @@ public:
//- Write all table data in dictionary format //- Write all table data in dictionary format
virtual void writeData(Ostream& os) const; virtual void writeData(Ostream& os) const;
//- Write keywords only in dictionary format. Used for non-inline //- Write keywords only in dictionary format.
// table types // Used for non-inline table types
virtual void writeEntries(Ostream& os) const; virtual void writeEntries(Ostream& os) const;
}; };

View File

@ -0,0 +1,49 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "tableBounds.H"
// * * * * * * * * * * * * * * * * Global Data * * * * * * * * * * * * * * * //
const Foam::Enum<Foam::bounds::normalBounding>
Foam::bounds::normalBoundingNames
{
{ normalBounding::ERROR, "error" },
{ normalBounding::WARN, "warn" },
{ normalBounding::CLAMP, "clamp" },
};
const Foam::Enum<Foam::bounds::repeatableBounding>
Foam::bounds::repeatableBoundingNames
{
{ repeatableBounding::ERROR, "error" },
{ repeatableBounding::WARN, "warn" },
{ repeatableBounding::CLAMP, "clamp" },
{ repeatableBounding::REPEAT, "repeat" },
};
// ************************************************************************* //

View File

@ -0,0 +1,84 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Namespace
Foam::bounds
Description
Namespace for bounding specifications.
At the moment, mostly for tables.
SourceFiles
tableBounds.C
\*---------------------------------------------------------------------------*/
#ifndef tableBounds_H
#define tableBounds_H
#include "Enum.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
namespace bounds
{
// Enumerations
//- Enumeration for handling out-of-bound values
enum class normalBounding
{
ERROR, //!< Exit with a FatalError
WARN, //!< Issue warning and clamp value (this is a good default)
CLAMP //!< Clamp value to the start/end value
};
//- Enumeration for handling out-of-bound values that are repeatable
enum class repeatableBounding
{
ERROR, //!< Exit with a FatalError
WARN, //!< Issue warning and clamp value (this is a good default)
CLAMP, //!< Clamp value to the start/end value
REPEAT //!< Treat as a repeating list
};
//- Strings corresponding to the normalBounding
extern const Foam::Enum<normalBounding> normalBoundingNames;
//- Strings corresponding to the repeatableBounding
extern const Foam::Enum<repeatableBounding> repeatableBoundingNames;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace bounds
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -149,7 +149,7 @@ Foam::radiation::blackBodyEmission::blackBodyEmission
table_ table_
( (
emissivePowerTable, emissivePowerTable,
interpolationTable<scalar>::CLAMP, bounds::repeatableBounding::CLAMP,
"blackBodyEmissivePower" "blackBodyEmissivePower"
), ),
C1_("C1", dimensionSet(1, 4, 3, 0, 0, 0, 0), 3.7419e-16), C1_("C1", dimensionSet(1, 4, 3, 0, 0, 0, 0), 3.7419e-16),