mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: Refactored DataEntry table-types to make use of new table base class
This commit is contained in:
@ -31,19 +31,14 @@ template<class Type>
|
||||
Foam::Table<Type>::Table(const word& entryName, const dictionary& dict)
|
||||
:
|
||||
DataEntry<Type>(entryName),
|
||||
table_()
|
||||
TableBase<Type>(entryName, dictionary::null)
|
||||
{
|
||||
Istream& is(dict.lookup(entryName));
|
||||
word entryType(is);
|
||||
|
||||
is >> table_;
|
||||
is >> this->table_;
|
||||
|
||||
if (!table_.size())
|
||||
{
|
||||
FatalErrorIn("Foam::Table<Type>::Table(const Istream&)")
|
||||
<< "Table for entry " << this->name_ << " is invalid (empty)"
|
||||
<< nl << exit(FatalError);
|
||||
}
|
||||
TableBase<Type>::check();
|
||||
}
|
||||
|
||||
|
||||
@ -51,7 +46,7 @@ template<class Type>
|
||||
Foam::Table<Type>::Table(const Table<Type>& tbl)
|
||||
:
|
||||
DataEntry<Type>(tbl),
|
||||
table_(tbl.table_)
|
||||
TableBase<Type>(tbl)
|
||||
{}
|
||||
|
||||
|
||||
@ -62,98 +57,6 @@ Foam::Table<Type>::~Table()
|
||||
{}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||
|
||||
template<class Type>
|
||||
Type Foam::Table<Type>::value(const scalar x) const
|
||||
{
|
||||
// Return zero if out of bounds
|
||||
if (x < table_[0].first() || x > table_.last().first())
|
||||
{
|
||||
return pTraits<Type>::zero;
|
||||
}
|
||||
|
||||
// Find i such that x(i) < x < x(i+1)
|
||||
label i = 0;
|
||||
while ((table_[i+1].first() < x) && (i+1 < table_.size()))
|
||||
{
|
||||
i++;
|
||||
}
|
||||
|
||||
// Linear interpolation to find value. Note constructor needed for
|
||||
// Table<label> to convert intermediate scalar back to label.
|
||||
return Type
|
||||
(
|
||||
(x - table_[i].first())/(table_[i+1].first() - table_[i].first())
|
||||
* (table_[i+1].second() - table_[i].second())
|
||||
+ table_[i].second()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
Type Foam::Table<Type>::integrate(const scalar x1, const scalar x2) const
|
||||
{
|
||||
// Initialise return value
|
||||
Type sum = pTraits<Type>::zero;
|
||||
|
||||
// Return zero if out of bounds
|
||||
if ((x1 > table_.last().first()) || (x2 < table_[0].first()))
|
||||
{
|
||||
return sum;
|
||||
}
|
||||
|
||||
// Find next index greater than x1
|
||||
label id1 = 0;
|
||||
while ((table_[id1].first() < x1) && (id1 < table_.size()))
|
||||
{
|
||||
id1++;
|
||||
}
|
||||
|
||||
// Find next index less than x2
|
||||
label id2 = table_.size() - 1;
|
||||
while ((table_[id2].first() > x2) && (id2 >= 1))
|
||||
{
|
||||
id2--;
|
||||
}
|
||||
|
||||
if ((id1 - id2) == 1)
|
||||
{
|
||||
// x1 and x2 lie within 1 interval
|
||||
sum = 0.5*(value(x1) + value(x2))*(x2 - x1);
|
||||
}
|
||||
else
|
||||
{
|
||||
// x1 and x2 cross multiple intervals
|
||||
|
||||
// Integrate table body
|
||||
for (label i=id1; i<id2; i++)
|
||||
{
|
||||
sum +=
|
||||
(table_[i].second() + table_[i+1].second())
|
||||
* (table_[i+1].first() - table_[i].first());
|
||||
}
|
||||
sum *= 0.5;
|
||||
|
||||
// Add table ends (partial segments)
|
||||
if (id1 > 0)
|
||||
{
|
||||
sum += 0.5
|
||||
* (value(x1) + table_[id1].second())
|
||||
* (table_[id1].first() - x1);
|
||||
}
|
||||
if (id2 < table_.size() - 1)
|
||||
{
|
||||
sum += 0.5
|
||||
* (table_[id2].second() + value(x2))
|
||||
* (x2 - table_[id2].first());
|
||||
}
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * * * //
|
||||
|
||||
#include "TableIO.C"
|
||||
|
||||
@ -70,14 +70,9 @@ Ostream& operator<<
|
||||
template<class Type>
|
||||
class Table
|
||||
:
|
||||
public DataEntry<Type>
|
||||
public DataEntry<Type>,
|
||||
public TableBase<Type>
|
||||
{
|
||||
// Private data
|
||||
|
||||
//- Table data
|
||||
List<Tuple2<scalar, Type> > table_;
|
||||
|
||||
|
||||
// Private Member Functions
|
||||
|
||||
//- Disallow default bitwise assignment
|
||||
@ -112,10 +107,16 @@ public:
|
||||
// Member Functions
|
||||
|
||||
//- Return Table value
|
||||
Type value(const scalar x) const;
|
||||
virtual Type value(const scalar x) const
|
||||
{
|
||||
return TableBase<Type>::value(x);
|
||||
}
|
||||
|
||||
//- Integrate between two (scalar) values
|
||||
Type integrate(const scalar x1, const scalar x2) const;
|
||||
virtual Type integrate(const scalar x1, const scalar x2) const
|
||||
{
|
||||
return TableBase<Type>::integrate(x1, x2);
|
||||
}
|
||||
|
||||
|
||||
// I/O
|
||||
@ -124,7 +125,7 @@ public:
|
||||
friend Ostream& operator<< <Type>
|
||||
(
|
||||
Ostream& os,
|
||||
const Table<Type>& cnst
|
||||
const Table<Type>& tbl
|
||||
);
|
||||
|
||||
//- Write in dictionary format
|
||||
|
||||
@ -130,10 +130,10 @@ public:
|
||||
bool checkMaxBounds(const scalar x, scalar& xDash) const;
|
||||
|
||||
//- Return Table value
|
||||
Type value(const scalar x) const;
|
||||
virtual Type value(const scalar x) const;
|
||||
|
||||
//- Integrate between two (scalar) values
|
||||
Type integrate(const scalar x1, const scalar x2) const;
|
||||
virtual Type integrate(const scalar x1, const scalar x2) const;
|
||||
|
||||
|
||||
// I/O
|
||||
|
||||
@ -34,20 +34,8 @@ Foam::Ostream& Foam::operator<<
|
||||
const Table<Type>& tbl
|
||||
)
|
||||
{
|
||||
if (os.format() == IOstream::ASCII)
|
||||
{
|
||||
os << static_cast<const DataEntry<Type>& >(tbl)
|
||||
<< token::SPACE << tbl.table_;
|
||||
}
|
||||
else
|
||||
{
|
||||
os << static_cast<const DataEntry<Type>& >(tbl);
|
||||
os.write
|
||||
(
|
||||
reinterpret_cast<const char*>(&tbl.table_),
|
||||
sizeof(tbl.table_)
|
||||
);
|
||||
}
|
||||
os << static_cast<const DataEntry<Type>&>(tbl)
|
||||
<< static_cast<const TableBase<Type>&>(tbl);
|
||||
|
||||
// Check state of Ostream
|
||||
os.check
|
||||
@ -63,8 +51,7 @@ template<class Type>
|
||||
void Foam::Table<Type>::writeData(Ostream& os) const
|
||||
{
|
||||
DataEntry<Type>::writeData(os);
|
||||
|
||||
os << nl << indent << table_ << token::END_STATEMENT << nl;
|
||||
TableBase<Type>::writeData(os);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user