From 0d9786651b51d4df19779de676877a15e48d6aac Mon Sep 17 00:00:00 2001 From: Will Bainbridge Date: Thu, 14 Nov 2019 16:50:02 +0000 Subject: [PATCH] Function1: Made Table and Constant readable in multiple formats A typical Function1 entry can be read in one of three ways; with the parameters in the current dictionary, in a separate sub-dict, or with the entry itself as the dictionary; e.g., // Current-dictionary form sine; amplitude 0.1; frequency 10; level 0.2; // Coeff-dictionary form sine; Coeffs { amplitude 0.1; frequency 10; level 0.2; } // Entry-as-dictionary form { type sine; amplitude 0.1; frequency 10; level 0.2; } The latter two sub-dictionary forms are needed when there are multiple Function1 entries of the same type in a dictionary. Enclosing the parameters in a separate sub-dictionary prevents the keywords (in this case "amplitude", "frequency" and "level") from being duplicated. Table and constant Function1 entries are different in that they have special formats which allow the data to be appended directly to the entry name; e.g; table ((0 (1 0 0)) (1 (2 0 0))); constant (1 0 0); // (constant can even have the "constant" keyword omitted) (1 0 0); Table also has two optional additional controls; "outOfBounds" and "interpolationScheme". In order for these to be written out in such a way that the entries are not duplicated, table needs to be written out (and therefore also read in) as one of the sub-dictionary forms. To that effect, Table has been extended to additionally permit reading in the three forms described previously, and to write in the coeff-dictionary form. // Current-dictionary form table; values ((0 (1 0 0)) (1 (2 0 0))); outOfBounds repeat; interpolationScheme linear; // Coeff-dictionary form sine; Coeffs { values ((0 (1 0 0)) (1 (2 0 0))); outOfBounds repeat; interpolationScheme linear; } // Entry-as-dictionary form { type table; values ((0 (1 0 0)) (1 (2 0 0))); outOfBounds repeat; interpolationScheme linear; } For completeness and consistency, constant has also been modified so that it can read in these forms. However, constant has no additional control entries, which means writing a coeff-dictionary is unecessary, so the output has not been changed. --- .../functions/Function1/Constant/Constant.C | 20 +++++++++++-- .../functions/Function1/Table/Table.C | 30 ++++++++++++++----- .../functions/Function1/Table/Table.H | 4 +-- .../functions/Function1/Table/TableBase.C | 25 ++++------------ .../functions/Function1/Table/TableBase.H | 3 -- .../functions/Function1/TableFile/TableFile.C | 1 + 6 files changed, 48 insertions(+), 35 deletions(-) diff --git a/src/OpenFOAM/primitives/functions/Function1/Constant/Constant.C b/src/OpenFOAM/primitives/functions/Function1/Constant/Constant.C index 0d633a7788..2be8eb08c1 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Constant/Constant.C +++ b/src/OpenFOAM/primitives/functions/Function1/Constant/Constant.C @@ -49,9 +49,23 @@ Foam::Function1s::Constant::Constant FieldFunction1>(entryName), value_(Zero) { - Istream& is(dict.lookup(entryName)); - word entryType(is); - is >> value_; + if (!dict.found(entryName)) + { + dict.lookup("value") >> value_; + } + else + { + Istream& is(dict.lookup(entryName)); + word entryType(is); + if (is.eof()) + { + dict.lookup("value") >> value_; + } + else + { + is >> value_; + } + } } diff --git a/src/OpenFOAM/primitives/functions/Function1/Table/Table.C b/src/OpenFOAM/primitives/functions/Function1/Table/Table.C index b3530a917d..2931c8ae5b 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Table/Table.C +++ b/src/OpenFOAM/primitives/functions/Function1/Table/Table.C @@ -36,9 +36,24 @@ Foam::Function1s::Table::Table : TableBase>(entryName, dict) { - Istream& is(dict.lookup(entryName)); - word entryType(is); - is >> this->table_; + if (!dict.found(entryName)) + { + dict.lookup("values") >> this->table_; + } + else + { + Istream& is(dict.lookup(entryName)); + word entryType(is); + if (is.eof()) + { + dict.lookup("values") >> this->table_; + } + else + { + is >> this->table_; + } + } + TableBase>::check(); } @@ -79,11 +94,12 @@ Foam::Function1s::Table::~Table() // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template -void Foam::Function1s::Table::writeData(Ostream& os) const +void Foam::Function1s::Table::writeEntries(Ostream& os) const { - Function1::writeData(os); - os << nl << indent << this->table_ << token::END_STATEMENT << nl; - TableBase>::writeCoeffDict(os); + TableBase>::writeEntries(os); + + os << indent << "values" << this->table_ + << token::END_STATEMENT << endl; } diff --git a/src/OpenFOAM/primitives/functions/Function1/Table/Table.H b/src/OpenFOAM/primitives/functions/Function1/Table/Table.H index 0159f3a2b7..db783bb620 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Table/Table.H +++ b/src/OpenFOAM/primitives/functions/Function1/Table/Table.H @@ -96,8 +96,8 @@ public: // Member Functions - //- Write all table data in dictionary format - virtual void writeData(Ostream& os) const; + //- Write entries only in dictionary format + virtual void writeEntries(Ostream& os) const; // Member Operators diff --git a/src/OpenFOAM/primitives/functions/Function1/Table/TableBase.C b/src/OpenFOAM/primitives/functions/Function1/Table/TableBase.C index bbb667e5a4..8b3725573b 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Table/TableBase.C +++ b/src/OpenFOAM/primitives/functions/Function1/Table/TableBase.C @@ -323,25 +323,6 @@ void Foam::Function1s::TableBase::writeEntries } -template -void Foam::Function1s::TableBase::writeCoeffDict -( - Ostream& os -) const -{ - OStringStream oss; - writeEntries(oss); - - if (oss.str().size()) - { - os << indent << word(this->name() + "Coeffs") << nl; - os << indent << token::BEGIN_BLOCK << incrIndent << nl; - writeEntries(os); - os << decrIndent << indent << token::END_BLOCK << endl; - } -} - - template void Foam::Function1s::TableBase::writeData ( @@ -350,7 +331,11 @@ void Foam::Function1s::TableBase::writeData { Function1::writeData(os); os << token::END_STATEMENT << nl; - writeCoeffDict(os); + + os << indent << word(this->name() + "Coeffs") << nl; + os << indent << token::BEGIN_BLOCK << incrIndent << nl; + writeEntries(os); + os << decrIndent << indent << token::END_BLOCK << endl; } diff --git a/src/OpenFOAM/primitives/functions/Function1/Table/TableBase.H b/src/OpenFOAM/primitives/functions/Function1/Table/TableBase.H index 0d2e9b0c25..0008f58ab4 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Table/TableBase.H +++ b/src/OpenFOAM/primitives/functions/Function1/Table/TableBase.H @@ -143,9 +143,6 @@ public: //- Write entries only in dictionary format virtual void writeEntries(Ostream& os) const; - //- Write the coeff dict in dictionary format - virtual void writeCoeffDict(Ostream& os) const; - //- Write all table data in dictionary format virtual void writeData(Ostream& os) const; diff --git a/src/OpenFOAM/primitives/functions/Function1/TableFile/TableFile.C b/src/OpenFOAM/primitives/functions/Function1/TableFile/TableFile.C index 8bc2afdc26..df8966dc05 100644 --- a/src/OpenFOAM/primitives/functions/Function1/TableFile/TableFile.C +++ b/src/OpenFOAM/primitives/functions/Function1/TableFile/TableFile.C @@ -77,6 +77,7 @@ template void Foam::Function1s::TableFile::writeEntries(Ostream& os) const { TableBase>::writeEntries(os); + writeEntry(os, "file", fName_); writeEntry(os, "format", reader_->type()); reader_->write(os);