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:
@ -121,7 +121,7 @@ void Foam::CSV<Type>::read()
|
||||
values.append(Tuple2<scalar,Type>(x, value));
|
||||
}
|
||||
|
||||
table_.transfer(values);
|
||||
this->table_.transfer(values);
|
||||
}
|
||||
|
||||
|
||||
@ -131,36 +131,25 @@ template<class Type>
|
||||
Foam::CSV<Type>::CSV(const word& entryName, const dictionary& dict)
|
||||
:
|
||||
DataEntry<Type>(entryName),
|
||||
headerLine_(false),
|
||||
refColumn_(0),
|
||||
componentColumns_(),
|
||||
separator_(string(",")[0]),
|
||||
fName_("none"),
|
||||
table_()
|
||||
TableBase<Type>(entryName, dict.subDict(type() + "Coeffs")),
|
||||
coeffs_(dict.subDict(type() + "Coeffs")),
|
||||
headerLine_(readBool(coeffs_.lookup("hasHeaderLine"))),
|
||||
refColumn_(readLabel(coeffs_.lookup("refColumn"))),
|
||||
componentColumns_(coeffs_.lookup("componentColumns")),
|
||||
separator_(coeffs_.lookupOrDefault<string>("separator", string(","))[0]),
|
||||
fName_(coeffs_.lookup("fileName"))
|
||||
{
|
||||
const dictionary coeffs(dict.subDict(type() + "Coeffs"));
|
||||
coeffs.lookup("hasHeaderLine") >> headerLine_;
|
||||
coeffs.lookup("refColumn") >> refColumn_;
|
||||
coeffs.lookup("componentColumns") >> componentColumns_;
|
||||
coeffs.readIfPresent("separator", string(separator_)[0]);
|
||||
coeffs.lookup("fileName") >> fName_;
|
||||
|
||||
if (componentColumns_.size() != pTraits<Type>::nComponents)
|
||||
{
|
||||
FatalErrorIn("Foam::CSV<Type>::CSV(const word&, Istream&)")
|
||||
<< componentColumns_ << " does not have the expected length "
|
||||
<< componentColumns_ << " does not have the expected length of "
|
||||
<< pTraits<Type>::nComponents << endl
|
||||
<< exit(FatalError);
|
||||
}
|
||||
|
||||
read();
|
||||
|
||||
if (!table_.size())
|
||||
{
|
||||
FatalErrorIn("Foam::CSV<Type>::CSV(const Istream&)")
|
||||
<< "CSV for entry " << this->name_ << " is invalid (empty)"
|
||||
<< nl << exit(FatalError);
|
||||
}
|
||||
TableBase<Type>::check();
|
||||
}
|
||||
|
||||
|
||||
@ -168,12 +157,12 @@ template<class Type>
|
||||
Foam::CSV<Type>::CSV(const CSV<Type>& tbl)
|
||||
:
|
||||
DataEntry<Type>(tbl),
|
||||
TableBase<Type>(tbl),
|
||||
headerLine_(tbl.headerLine_),
|
||||
refColumn_(tbl.refColumn_),
|
||||
componentColumns_(tbl.componentColumns_),
|
||||
separator_(tbl.separator_),
|
||||
fName_(tbl.fName_),
|
||||
table_(tbl.table_)
|
||||
fName_(tbl.fName_)
|
||||
{}
|
||||
|
||||
|
||||
@ -184,98 +173,6 @@ Foam::CSV<Type>::~CSV()
|
||||
{}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||
|
||||
template<class Type>
|
||||
Type Foam::CSV<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
|
||||
// CSV<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::CSV<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 "CSVIO.C"
|
||||
|
||||
@ -29,13 +29,14 @@ Description
|
||||
e.g. time
|
||||
|
||||
\verbatim
|
||||
<entryName> CSV
|
||||
<entryName> csvFile
|
||||
{
|
||||
hasHeaderLine true;
|
||||
refColumn 0; // reference column index
|
||||
componentColumns (0 1 2); // component column indices
|
||||
separator ","; // optional (defaults to ",")
|
||||
fileName fileXYZ; // name of csv data file
|
||||
refColumn 0; // reference column index
|
||||
componentColumns (0 1 2); // component column indices
|
||||
separator ","; // optional (defaults to ",")
|
||||
fileName fileXYZ; // name of csv data file
|
||||
outOfBounds clamp; // optional out-of-bounds handling
|
||||
}
|
||||
\endverbatim
|
||||
|
||||
@ -48,6 +49,7 @@ SourceFiles
|
||||
#define CSV_H
|
||||
|
||||
#include "DataEntry.H"
|
||||
#include "TableBase.H"
|
||||
#include "Tuple2.H"
|
||||
#include "labelList.H"
|
||||
#include "ISstream.H"
|
||||
@ -74,10 +76,14 @@ Ostream& operator<<
|
||||
template<class Type>
|
||||
class CSV
|
||||
:
|
||||
public DataEntry<Type>
|
||||
public DataEntry<Type>,
|
||||
public TableBase<Type>
|
||||
{
|
||||
// Private data
|
||||
|
||||
//- Coefficients dictionary (for convenience on reading)
|
||||
dictionary coeffs_;
|
||||
|
||||
//- Does the file have a header line?
|
||||
bool headerLine_;
|
||||
|
||||
@ -93,9 +99,6 @@ class CSV
|
||||
//- File name for csv table (optional)
|
||||
fileName fName_;
|
||||
|
||||
//- CSV data
|
||||
List<Tuple2<scalar, Type> > table_;
|
||||
|
||||
|
||||
// Private Member Functions
|
||||
|
||||
@ -112,7 +115,7 @@ class CSV
|
||||
public:
|
||||
|
||||
//- Runtime type information
|
||||
TypeName("csv");
|
||||
TypeName("csvFile");
|
||||
|
||||
|
||||
// Constructors
|
||||
@ -136,11 +139,17 @@ public:
|
||||
|
||||
// Member Functions
|
||||
|
||||
//- Return CSV value
|
||||
Type value(const scalar x) const;
|
||||
//- Return Table value
|
||||
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
|
||||
|
||||
@ -46,11 +46,6 @@ Foam::Ostream& Foam::operator<<
|
||||
else
|
||||
{
|
||||
os << static_cast<const DataEntry<Type>& >(tbl);
|
||||
os.write
|
||||
(
|
||||
reinterpret_cast<const char*>(&tbl.table_),
|
||||
sizeof(tbl.table_)
|
||||
);
|
||||
}
|
||||
|
||||
// Check state of Ostream
|
||||
|
||||
@ -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