ENH: Refactored DataEntry table-types to make use of new table base class

This commit is contained in:
andy
2011-11-22 13:14:48 +00:00
parent 1df679963e
commit e9e9dcbd08
7 changed files with 54 additions and 262 deletions

View File

@ -121,7 +121,7 @@ void Foam::CSV<Type>::read()
values.append(Tuple2<scalar,Type>(x, value)); 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) Foam::CSV<Type>::CSV(const word& entryName, const dictionary& dict)
: :
DataEntry<Type>(entryName), DataEntry<Type>(entryName),
headerLine_(false), TableBase<Type>(entryName, dict.subDict(type() + "Coeffs")),
refColumn_(0), coeffs_(dict.subDict(type() + "Coeffs")),
componentColumns_(), headerLine_(readBool(coeffs_.lookup("hasHeaderLine"))),
separator_(string(",")[0]), refColumn_(readLabel(coeffs_.lookup("refColumn"))),
fName_("none"), componentColumns_(coeffs_.lookup("componentColumns")),
table_() 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) if (componentColumns_.size() != pTraits<Type>::nComponents)
{ {
FatalErrorIn("Foam::CSV<Type>::CSV(const word&, Istream&)") 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 << pTraits<Type>::nComponents << endl
<< exit(FatalError); << exit(FatalError);
} }
read(); read();
if (!table_.size()) TableBase<Type>::check();
{
FatalErrorIn("Foam::CSV<Type>::CSV(const Istream&)")
<< "CSV for entry " << this->name_ << " is invalid (empty)"
<< nl << exit(FatalError);
}
} }
@ -168,12 +157,12 @@ template<class Type>
Foam::CSV<Type>::CSV(const CSV<Type>& tbl) Foam::CSV<Type>::CSV(const CSV<Type>& tbl)
: :
DataEntry<Type>(tbl), DataEntry<Type>(tbl),
TableBase<Type>(tbl),
headerLine_(tbl.headerLine_), headerLine_(tbl.headerLine_),
refColumn_(tbl.refColumn_), refColumn_(tbl.refColumn_),
componentColumns_(tbl.componentColumns_), componentColumns_(tbl.componentColumns_),
separator_(tbl.separator_), separator_(tbl.separator_),
fName_(tbl.fName_), fName_(tbl.fName_)
table_(tbl.table_)
{} {}
@ -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 * * * * * * * * * * * * * // // * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * * * //
#include "CSVIO.C" #include "CSVIO.C"

View File

@ -29,13 +29,14 @@ Description
e.g. time e.g. time
\verbatim \verbatim
<entryName> CSV <entryName> csvFile
{ {
hasHeaderLine true; hasHeaderLine true;
refColumn 0; // reference column index refColumn 0; // reference column index
componentColumns (0 1 2); // component column indices componentColumns (0 1 2); // component column indices
separator ","; // optional (defaults to ",") separator ","; // optional (defaults to ",")
fileName fileXYZ; // name of csv data file fileName fileXYZ; // name of csv data file
outOfBounds clamp; // optional out-of-bounds handling
} }
\endverbatim \endverbatim
@ -48,6 +49,7 @@ SourceFiles
#define CSV_H #define CSV_H
#include "DataEntry.H" #include "DataEntry.H"
#include "TableBase.H"
#include "Tuple2.H" #include "Tuple2.H"
#include "labelList.H" #include "labelList.H"
#include "ISstream.H" #include "ISstream.H"
@ -74,10 +76,14 @@ Ostream& operator<<
template<class Type> template<class Type>
class CSV class CSV
: :
public DataEntry<Type> public DataEntry<Type>,
public TableBase<Type>
{ {
// Private data // Private data
//- Coefficients dictionary (for convenience on reading)
dictionary coeffs_;
//- Does the file have a header line? //- Does the file have a header line?
bool headerLine_; bool headerLine_;
@ -93,9 +99,6 @@ class CSV
//- File name for csv table (optional) //- File name for csv table (optional)
fileName fName_; fileName fName_;
//- CSV data
List<Tuple2<scalar, Type> > table_;
// Private Member Functions // Private Member Functions
@ -112,7 +115,7 @@ class CSV
public: public:
//- Runtime type information //- Runtime type information
TypeName("csv"); TypeName("csvFile");
// Constructors // Constructors
@ -136,11 +139,17 @@ public:
// Member Functions // Member Functions
//- Return CSV value //- 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 //- 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 // I/O

View File

@ -46,11 +46,6 @@ Foam::Ostream& Foam::operator<<
else else
{ {
os << static_cast<const DataEntry<Type>& >(tbl); os << static_cast<const DataEntry<Type>& >(tbl);
os.write
(
reinterpret_cast<const char*>(&tbl.table_),
sizeof(tbl.table_)
);
} }
// Check state of Ostream // Check state of Ostream

View File

@ -31,19 +31,14 @@ template<class Type>
Foam::Table<Type>::Table(const word& entryName, const dictionary& dict) Foam::Table<Type>::Table(const word& entryName, const dictionary& dict)
: :
DataEntry<Type>(entryName), DataEntry<Type>(entryName),
table_() TableBase<Type>(entryName, dictionary::null)
{ {
Istream& is(dict.lookup(entryName)); Istream& is(dict.lookup(entryName));
word entryType(is); word entryType(is);
is >> table_; is >> this->table_;
if (!table_.size()) TableBase<Type>::check();
{
FatalErrorIn("Foam::Table<Type>::Table(const Istream&)")
<< "Table for entry " << this->name_ << " is invalid (empty)"
<< nl << exit(FatalError);
}
} }
@ -51,7 +46,7 @@ template<class Type>
Foam::Table<Type>::Table(const Table<Type>& tbl) Foam::Table<Type>::Table(const Table<Type>& tbl)
: :
DataEntry<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 * * * * * * * * * * * * * // // * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * * * //
#include "TableIO.C" #include "TableIO.C"

View File

@ -70,14 +70,9 @@ Ostream& operator<<
template<class Type> template<class Type>
class Table class Table
: :
public DataEntry<Type> public DataEntry<Type>,
public TableBase<Type>
{ {
// Private data
//- Table data
List<Tuple2<scalar, Type> > table_;
// Private Member Functions // Private Member Functions
//- Disallow default bitwise assignment //- Disallow default bitwise assignment
@ -112,10 +107,16 @@ public:
// Member Functions // Member Functions
//- Return Table value //- 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 //- 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 // I/O
@ -124,7 +125,7 @@ public:
friend Ostream& operator<< <Type> friend Ostream& operator<< <Type>
( (
Ostream& os, Ostream& os,
const Table<Type>& cnst const Table<Type>& tbl
); );
//- Write in dictionary format //- Write in dictionary format

View File

@ -130,10 +130,10 @@ public:
bool checkMaxBounds(const scalar x, scalar& xDash) const; bool checkMaxBounds(const scalar x, scalar& xDash) const;
//- Return Table value //- Return Table value
Type value(const scalar x) const; virtual Type value(const scalar x) const;
//- Integrate between two (scalar) values //- 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 // I/O

View File

@ -34,20 +34,8 @@ Foam::Ostream& Foam::operator<<
const Table<Type>& tbl const Table<Type>& tbl
) )
{ {
if (os.format() == IOstream::ASCII) os << static_cast<const DataEntry<Type>&>(tbl)
{ << static_cast<const TableBase<Type>&>(tbl);
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_)
);
}
// Check state of Ostream // Check state of Ostream
os.check os.check
@ -63,8 +51,7 @@ template<class Type>
void Foam::Table<Type>::writeData(Ostream& os) const void Foam::Table<Type>::writeData(Ostream& os) const
{ {
DataEntry<Type>::writeData(os); DataEntry<Type>::writeData(os);
TableBase<Type>::writeData(os);
os << nl << indent << table_ << token::END_STATEMENT << nl;
} }