diff --git a/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSV.C b/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSV.C index ea992c10f68d743d0881f05663ed40e0b8636d56..3fe17d24a01e1cbf6c973d3ce7e9ca448d171059 100644 --- a/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSV.C +++ b/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSV.C @@ -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" diff --git a/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSV.H b/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSV.H index 66679a21f3daeeb88e07cf53b95b99016406154d..c95406350b3b461546266e6cad4ee0badb3dffb4 100644 --- a/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSV.H +++ b/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSV.H @@ -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 diff --git a/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSVIO.C b/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSVIO.C index 98d784ac5bbe6c9cf7a9321fabf4bc213249f2cf..d16ca23ab89696b9a8a5169962c0c649bc180c61 100644 --- a/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSVIO.C +++ b/src/OpenFOAM/primitives/functions/DataEntry/CSV/CSVIO.C @@ -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 diff --git a/src/OpenFOAM/primitives/functions/DataEntry/Table/Table.C b/src/OpenFOAM/primitives/functions/DataEntry/Table/Table.C index 2ee8180be65ec9ea37f3b63ad5062a0c99920a10..dd60ac90fb55af2d46f848aa8e2184c90d317765 100644 --- a/src/OpenFOAM/primitives/functions/DataEntry/Table/Table.C +++ b/src/OpenFOAM/primitives/functions/DataEntry/Table/Table.C @@ -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" diff --git a/src/OpenFOAM/primitives/functions/DataEntry/Table/Table.H b/src/OpenFOAM/primitives/functions/DataEntry/Table/Table.H index fc849488e547f62db67046cc7f741cb11e7e5067..c1ba2121e8903cfd99292919c740b19e3c79bc6d 100644 --- a/src/OpenFOAM/primitives/functions/DataEntry/Table/Table.H +++ b/src/OpenFOAM/primitives/functions/DataEntry/Table/Table.H @@ -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 diff --git a/src/OpenFOAM/primitives/functions/DataEntry/Table/TableBase.H b/src/OpenFOAM/primitives/functions/DataEntry/Table/TableBase.H index 82ee7e697a93ab59927ddf3737ee1ed23a93f0ba..f4d20028bcc0616cecb5d26ed9b2dab7d992cf60 100644 --- a/src/OpenFOAM/primitives/functions/DataEntry/Table/TableBase.H +++ b/src/OpenFOAM/primitives/functions/DataEntry/Table/TableBase.H @@ -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 diff --git a/src/OpenFOAM/primitives/functions/DataEntry/Table/TableIO.C b/src/OpenFOAM/primitives/functions/DataEntry/Table/TableIO.C index 6fd26f353f38d59545050ef7a2b8c4fdcb21280f..ec43d46dd9c37f91a96ca2015d5d4ba5ba6fcd6e 100644 --- a/src/OpenFOAM/primitives/functions/DataEntry/Table/TableIO.C +++ b/src/OpenFOAM/primitives/functions/DataEntry/Table/TableIO.C @@ -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); }