diff --git a/opm/input/eclipse/EclipseState/Tables/FlatTable.hpp b/opm/input/eclipse/EclipseState/Tables/FlatTable.hpp index 81e372983..49943dc27 100644 --- a/opm/input/eclipse/EclipseState/Tables/FlatTable.hpp +++ b/opm/input/eclipse/EclipseState/Tables/FlatTable.hpp @@ -3,6 +3,7 @@ #include #include +#include #include namespace Opm { @@ -24,6 +25,45 @@ struct FlatTable : public std::vector< T > { } }; +template +class FlatTableWithCopy +{ +public: + FlatTableWithCopy() = default; + explicit FlatTableWithCopy(const DeckKeyword& kw, + std::string_view expect = ""); + explicit FlatTableWithCopy(std::initializer_list records); + + auto size() const { return this->table_.size(); } + bool empty() const { return this->table_.empty(); } + auto begin() const { return this->table_.begin(); } + auto end() const { return this->table_.end(); } + + const RecordType& operator[](const std::size_t tableID) const + { + return this->table_[tableID]; + } + + const RecordType& at(const std::size_t tableID) const + { + return this->table_.at(tableID); + } + + bool operator==(const FlatTableWithCopy& other) const + { + return this->table_ == other.table_; + } + + template + void serializeOp(Serializer& serializer) + { + serializer.vector(this->table_); + } + +protected: + std::vector table_{}; +}; + struct GRAVITYRecord { static constexpr std::size_t size = 3; @@ -46,33 +86,12 @@ struct GRAVITYRecord { } }; -class GravityTable +struct GravityTable : public FlatTableWithCopy { -public: GravityTable() = default; explicit GravityTable(const DeckKeyword& kw); explicit GravityTable(std::initializer_list records); - auto size() const { return this->table_.size(); } - bool empty() const { return this->table_.empty(); } - auto begin() const { return this->table_.begin(); } - auto end() const { return this->table_.end(); } - - const GRAVITYRecord& operator[](const std::size_t tableID) const - { - return this->table_[tableID]; - } - - const GRAVITYRecord& at(const std::size_t tableID) const - { - return this->table_.at(tableID); - } - - bool operator==(const GravityTable& other) const - { - return this->table_ == other.table_; - } - static GravityTable serializeObject() { return GravityTable({{1.0, 2.0, 3.0}}); @@ -81,11 +100,8 @@ public: template void serializeOp(Serializer& serializer) { - serializer.vector(this->table_); + FlatTableWithCopy::serializeOp(serializer); } - -private: - std::vector table_{}; }; struct DENSITYRecord { @@ -110,33 +126,12 @@ struct DENSITYRecord { } }; -class DensityTable +struct DensityTable : public FlatTableWithCopy { -public: DensityTable() = default; explicit DensityTable(const DeckKeyword& kw); - explicit DensityTable(std::initializer_list records); explicit DensityTable(const GravityTable& gravity); - - auto size() const { return this->table_.size(); } - bool empty() const { return this->table_.empty(); } - auto begin() const { return this->table_.begin(); } - auto end() const { return this->table_.end(); } - - const DENSITYRecord& operator[](const std::size_t tableID) const - { - return this->table_[tableID]; - } - - const DENSITYRecord& at(const std::size_t tableID) const - { - return this->table_.at(tableID); - } - - bool operator==(const DensityTable& other) const - { - return this->table_ == other.table_; - } + explicit DensityTable(std::initializer_list records); static DensityTable serializeObject() { @@ -146,11 +141,8 @@ public: template void serializeOp(Serializer& serializer) { - serializer.vector(this->table_); + FlatTableWithCopy::serializeOp(serializer); } - -private: - std::vector table_{}; }; struct DiffCoeffRecord { @@ -227,31 +219,12 @@ struct PVTWRecord { } }; -class PvtwTable +struct PvtwTable : public FlatTableWithCopy { -public: PvtwTable() = default; explicit PvtwTable(const DeckKeyword& kw); explicit PvtwTable(std::initializer_list records); - auto size() const { return this->table_.size(); } - bool empty() const { return this->table_.empty(); } - - const PVTWRecord& operator[](const std::size_t tableID) const - { - return this->table_[tableID]; - } - - const PVTWRecord& at(const std::size_t tableID) const - { - return this->table_.at(tableID); - } - - bool operator==(const PvtwTable& other) const - { - return this->table_ == other.table_; - } - static PvtwTable serializeObject() { return PvtwTable({{1.0, 2.0, 3.0, 4.0, 5.0}}); @@ -260,11 +233,8 @@ public: template void serializeOp(Serializer& serializer) { - serializer.vector(this->table_); + FlatTableWithCopy::serializeOp(serializer); } - -private: - std::vector table_{}; }; struct ROCKRecord { diff --git a/src/opm/input/eclipse/EclipseState/Tables/Tables.cpp b/src/opm/input/eclipse/EclipseState/Tables/Tables.cpp index 0a9a77c30..f14901795 100644 --- a/src/opm/input/eclipse/EclipseState/Tables/Tables.cpp +++ b/src/opm/input/eclipse/EclipseState/Tables/Tables.cpp @@ -94,6 +94,7 @@ #include #include #include +#include #include @@ -1574,13 +1575,14 @@ bool all_defaulted(const DeckRecord& record) // ------------------------------------------------------------------------ -PvtwTable::PvtwTable(const DeckKeyword& kw) +template +FlatTableWithCopy::FlatTableWithCopy(const DeckKeyword& kw, + std::string_view expect) { - if (kw.name() != ParserKeywords::PVTW::keywordName) { + if (!expect.empty() && (kw.name() != expect)) { throw std::invalid_argument { fmt::format("Keyword {} cannot be used to " - "initialise {} table structures", kw.name(), - ParserKeywords::PVTW::keywordName) + "initialise {} table structures", kw.name(), expect) }; } @@ -1588,9 +1590,9 @@ PvtwTable::PvtwTable(const DeckKeyword& kw) for (const auto& record : kw) { if (all_defaulted(record)) { - // All-defaulted records imply PVTW in region R is equal to PVTW - // in region R-1. PVTW must not be defaulted in region 1 (i.e., - // when PVTNUM=1). + // All-defaulted records imply table in region R is equal to + // table in region R-1. Table must not be defaulted in region 1 + // (i.e., when PVTNUM=1). if (this->table_.empty()) { throw OpmInputError { "First record cannot be defaulted", @@ -1601,89 +1603,34 @@ PvtwTable::PvtwTable(const DeckKeyword& kw) this->table_.push_back(this->table_.back()); } else { - this->table_.push_back(flat_get(record, mkseq{})); + this->table_.push_back(flat_get(record, mkseq{})); } } } -PvtwTable::PvtwTable(std::initializer_list records) - : table_(records) +template +FlatTableWithCopy::FlatTableWithCopy(std::initializer_list records) + : table_{ records } {} // ------------------------------------------------------------------------ GravityTable::GravityTable(const DeckKeyword& kw) -{ - if (kw.name() != ParserKeywords::GRAVITY::keywordName) { - throw std::invalid_argument { - fmt::format("Keyword {} cannot be used to " - "initialise {} table structures", kw.name(), - ParserKeywords::GRAVITY::keywordName) - }; - } - - this->table_.reserve(kw.size()); - - for (const auto& record : kw) { - if (all_defaulted(record)) { - // All-defaulted records imply GRAVITY in region R is equal to - // GRAVITY in region R-1. GRAVITY must not be defaulted in - // region 1 (i.e., when PVTNUM=1). - if (this->table_.empty()) { - throw OpmInputError { - "First record cannot be defaulted", - kw.location() - }; - } - - this->table_.push_back(this->table_.back()); - } - else { - this->table_.push_back(flat_get(record, mkseq{})); - } - } -} + : FlatTableWithCopy(kw, ParserKeywords::GRAVITY::keywordName) +{} GravityTable::GravityTable(std::initializer_list records) - : table_(records) + : FlatTableWithCopy(records) {} // ------------------------------------------------------------------------ DensityTable::DensityTable(const DeckKeyword& kw) -{ - if (kw.name() != ParserKeywords::DENSITY::keywordName) { - throw std::invalid_argument { - fmt::format("Keyword {} cannot be used to " - "initialise {} table structures", kw.name(), - ParserKeywords::DENSITY::keywordName) - }; - } - - this->table_.reserve(kw.size()); - - for (const auto& record : kw) { - if (all_defaulted(record)) { - // All-defaulted records imply DENSITY in region R is equal to - // DENSITY in region R-1. DENSITY must not be defaulted in - // region 1 (i.e., when PVTNUM=1). - if (this->table_.empty()) { - throw OpmInputError { - "First record cannot be defaulted", - kw.location() - }; - } - - this->table_.push_back(this->table_.back()); - } - else { - this->table_.push_back(flat_get(record, mkseq{})); - } - } -} + : FlatTableWithCopy(kw, ParserKeywords::DENSITY::keywordName) +{} DensityTable::DensityTable(std::initializer_list records) - : table_(records) + : FlatTableWithCopy(records) {} DensityTable::DensityTable(const GravityTable& gravity) @@ -1716,6 +1663,16 @@ DensityTable::DensityTable(const GravityTable& gravity) // ------------------------------------------------------------------------ +PvtwTable::PvtwTable(const DeckKeyword& kw) + : FlatTableWithCopy(kw, ParserKeywords::PVTW::keywordName) +{} + +PvtwTable::PvtwTable(std::initializer_list records) + : FlatTableWithCopy(records) +{} + +// ------------------------------------------------------------------------ + template< typename T > FlatTable< T >::FlatTable( const DeckKeyword& kw ) : std::vector< T >( flat_records< T >( kw, mkseq< T::size >{} ) )