diff --git a/opm/parser/eclipse/EclipseState/Grid/GridProperties.cpp b/opm/parser/eclipse/EclipseState/Grid/GridProperties.cpp index 44cca2071..7e3879934 100644 --- a/opm/parser/eclipse/EclipseState/Grid/GridProperties.cpp +++ b/opm/parser/eclipse/EclipseState/Grid/GridProperties.cpp @@ -170,7 +170,7 @@ namespace Opm { template< typename T > size_t GridProperties::size() const { - return m_property_list.size(); + return m_properties.size(); } @@ -181,7 +181,7 @@ namespace Opm { if (!hasKeyword(kw)) addAutoGeneratedKeyword_(kw); - GridProperty& property = (*m_properties.at( kw )); + GridProperty& property = m_properties.at( kw ); property.runPostProcessor( ); return property; } @@ -193,7 +193,7 @@ namespace Opm { const std::string kw = normalize(keyword); if (hasKeyword(kw)) - return *m_properties.at( kw ); + return m_properties.at( kw ); else { if (supportsKeyword(kw)) throw std::invalid_argument("Keyword: " + kw + " is supported - but not initialized."); @@ -203,6 +203,16 @@ namespace Opm { } + template< typename T > + void GridProperties::insertKeyword(const SupportedKeywordInfo& supportedKeyword) const { + int nx = m_eclipseGrid.getNX(); + int ny = m_eclipseGrid.getNY(); + int nz = m_eclipseGrid.getNZ(); + + m_properties.emplace( supportedKeyword.getKeywordName() , GridProperty( nx, ny , nz , supportedKeyword )); + } + + template< typename T > bool GridProperties::addKeyword(const std::string& keywordName) { @@ -225,14 +235,7 @@ namespace Opm { return true; } - auto supportedKeyword = m_supportedKeywords.at( kw ); - int nx = m_eclipseGrid.getNX(); - int ny = m_eclipseGrid.getNY(); - int nz = m_eclipseGrid.getNZ(); - std::shared_ptr > newProperty(new GridProperty(nx , ny , nz , supportedKeyword)); - - m_properties.insert( std::pair > > ( kw , newProperty )); - m_property_list.push_back( newProperty ); + insertKeyword( m_supportedKeywords.at( kw ) ); return true; } } @@ -252,7 +255,7 @@ namespace Opm { template< typename T > - GridProperty& GridProperties::getOrCreateProperty(const std::string name) { + GridProperty& GridProperties::getOrCreateProperty(const std::string& name) { if (!hasKeyword(name)) addKeyword(name); @@ -413,9 +416,13 @@ namespace Opm { if (!hasKeyword(kw)) addAutoGeneratedKeyword_(kw); - return *m_properties.at( kw ); + return m_properties.at( kw ); } + + /* + This is const because of the auto-generation of keywords on get(). + */ template< typename T > bool GridProperties::addAutoGeneratedKeyword_(const std::string& keywordName) const { if (!supportsKeyword( keywordName )) @@ -424,16 +431,8 @@ namespace Opm { if (m_properties.count( keywordName ) > 0) return false; // property already exists (if it is auto generated or not doesn't matter) else { - auto& supportedKeyword = m_supportedKeywords.at( keywordName ); - int nx = m_eclipseGrid.getNX(); - int ny = m_eclipseGrid.getNY(); - int nz = m_eclipseGrid.getNZ(); - std::shared_ptr > newProperty(new GridProperty(nx , ny , nz , supportedKeyword)); - m_autoGeneratedProperties.insert(keywordName); - - m_properties.insert( std::pair > > ( keywordName , newProperty )); - m_property_list.push_back( newProperty ); + insertKeyword( m_supportedKeywords.at( keywordName ) ); return true; } } diff --git a/opm/parser/eclipse/EclipseState/Grid/GridProperties.hpp b/opm/parser/eclipse/EclipseState/Grid/GridProperties.hpp index 685752eb9..bc1e18d27 100644 --- a/opm/parser/eclipse/EclipseState/Grid/GridProperties.hpp +++ b/opm/parser/eclipse/EclipseState/Grid/GridProperties.hpp @@ -58,13 +58,15 @@ namespace Opm { void setKeywordBox( const DeckRecord& deckRecord, BoxManager& boxManager); - class Eclipse3DProperties; template class GridProperties { public: typedef typename GridProperty::SupportedKeywordInfo SupportedKeywordInfo; + + struct const_iterator; + GridProperties(const EclipseGrid& eclipseGrid, const UnitSystem* deckUnitSystem, std::vector< SupportedKeywordInfo >&& supportedKeywords); @@ -80,6 +82,8 @@ namespace Opm { size_t size() const; const GridProperty& getKeyword(const std::string& keyword) const; const GridProperty& getInitializedKeyword(const std::string& keyword) const; + + bool addKeyword(const std::string& keywordName); void copyKeyword(const std::string& srcField , const std::string& targetField , @@ -105,7 +109,7 @@ namespace Opm { return getInitializedKeyword( Keyword::keywordName ); } - GridProperty& getOrCreateProperty(const std::string name); + GridProperty& getOrCreateProperty(const std::string& name); /** The fine print of the manual says the ADD keyword should support @@ -123,6 +127,31 @@ namespace Opm { void handleMULTIREGRecord( const DeckRecord& record, const GridProperty& regionProperty ); void handleCOPYREGRecord( const DeckRecord& record, const GridProperty& regionProperty ); + /* + Iterators over initialized properties. The overloaded + operator*() opens the pair which comes natively from the + std::map iterator. + */ + const_iterator begin() const { + return const_iterator( m_properties.begin() ); + } + + const_iterator end() const { + return const_iterator( m_properties.end() ); + } + + typedef typename std::map > storage; + typedef typename storage::const_iterator storage_iterator; + + struct const_iterator : public storage_iterator { + const_iterator( storage_iterator iter ) : storage_iterator( iter ) { } + + const GridProperty& operator*( ) { + const auto& pair = storage_iterator::operator*( ); + return pair.second; + } + }; + private: /// this method exists for (friend) Eclipse3DProperties to be allowed initializing PORV and ACTNUM keyword @@ -132,6 +161,7 @@ namespace Opm { const std::string& dimString ); GridProperty& getKeyword(const std::string& keyword); bool addAutoGeneratedKeyword_(const std::string& keywordName) const; + void insertKeyword(const SupportedKeywordInfo& supportedKeyword) const; bool isAutoGenerated_(const std::string& keyword) const; friend class Eclipse3DProperties; // needed for PORV keyword entanglement @@ -139,10 +169,11 @@ namespace Opm { const UnitSystem * m_deckUnitSystem = nullptr; MessageContainer m_messages; std::unordered_map m_supportedKeywords; - mutable std::map > > m_properties; + + mutable storage m_properties; mutable std::set m_autoGeneratedProperties; - mutable std::vector > > m_property_list; }; + } #endif // ECLIPSE_GRIDPROPERTIES_HPP_ diff --git a/opm/parser/eclipse/EclipseState/tests/Eclipse3DPropertiesTests.cpp b/opm/parser/eclipse/EclipseState/tests/Eclipse3DPropertiesTests.cpp index 010b1119b..95b1ba431 100644 --- a/opm/parser/eclipse/EclipseState/tests/Eclipse3DPropertiesTests.cpp +++ b/opm/parser/eclipse/EclipseState/tests/Eclipse3DPropertiesTests.cpp @@ -80,6 +80,8 @@ static Opm::DeckPtr createDeck() { return parser->parseString(deckData, Opm::ParseContext()); } + + static Opm::DeckPtr createValidIntDeck() { const char *deckData = "RUNSPEC\n" "GRIDOPTS\n" @@ -232,7 +234,6 @@ BOOST_AUTO_TEST_CASE(IntGridProperty) { BOOST_AUTO_TEST_CASE(AddregIntSetCorrectly) { Opm::DeckPtr deck = createValidIntDeck(); Setup s(deck); - const auto& property = s.props.getIntGridProperty("SATNUM"); for (size_t j = 0; j < 5; j++) for (size_t i = 0; i < 5; i++) { @@ -258,6 +259,33 @@ BOOST_AUTO_TEST_CASE(PermxUnitAppliedCorrectly) { } } +BOOST_AUTO_TEST_CASE(DoubleIterator) { + Opm::DeckPtr deck = createValidPERMXDeck(); + Setup s(deck); + const auto& doubleProperties = s.props.getDoubleProperties(); + std::vector kw_list; + for (const auto& prop : doubleProperties ) + kw_list.push_back( prop.getKeywordName() ); + + BOOST_CHECK_EQUAL( 2 , kw_list.size() ); + BOOST_CHECK( std::find( kw_list.begin() , kw_list.end() , "PERMX") != kw_list.end()); + BOOST_CHECK( std::find( kw_list.begin() , kw_list.end() , "PERMZ") != kw_list.end()); +} + + +BOOST_AUTO_TEST_CASE(IntIterator) { + Opm::DeckPtr deck = createValidPERMXDeck(); + Setup s(deck); + const auto& intProperties = s.props.getIntProperties(); + std::vector kw_list; + for (const auto& prop : intProperties ) + kw_list.push_back( prop.getKeywordName() ); + + BOOST_CHECK_EQUAL( 1 , kw_list.size() ); + BOOST_CHECK_EQUAL( kw_list[0] , "MULTNUM" ); +} + + BOOST_AUTO_TEST_CASE(getRegions) { const char* input = "START -- 0 \n"