Added iterator support to GridProperties<T>

- Removed std::vector<> storage of GridProperty<T> objects.
 - Completely removed std::shared_ptr<> from GridProperties<T>.
This commit is contained in:
Joakim Hove
2016-07-13 11:47:34 +02:00
parent c3e292399b
commit 4a22657e76
3 changed files with 85 additions and 27 deletions

View File

@@ -170,7 +170,7 @@ namespace Opm {
template< typename T >
size_t GridProperties<T>::size() const {
return m_property_list.size();
return m_properties.size();
}
@@ -181,7 +181,7 @@ namespace Opm {
if (!hasKeyword(kw))
addAutoGeneratedKeyword_(kw);
GridProperty<T>& property = (*m_properties.at( kw ));
GridProperty<T>& 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<T>::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<T>( nx, ny , nz , supportedKeyword ));
}
template< typename T >
bool GridProperties<T>::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<GridProperty<T> > newProperty(new GridProperty<T>(nx , ny , nz , supportedKeyword));
m_properties.insert( std::pair<std::string , std::shared_ptr<GridProperty<T> > > ( 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<T>& GridProperties<T>::getOrCreateProperty(const std::string name) {
GridProperty<T>& GridProperties<T>::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<T>::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<GridProperty<T> > newProperty(new GridProperty<T>(nx , ny , nz , supportedKeyword));
m_autoGeneratedProperties.insert(keywordName);
m_properties.insert( std::pair<std::string , std::shared_ptr<GridProperty<T> > > ( keywordName , newProperty ));
m_property_list.push_back( newProperty );
insertKeyword( m_supportedKeywords.at( keywordName ) );
return true;
}
}

View File

@@ -58,13 +58,15 @@ namespace Opm {
void setKeywordBox( const DeckRecord& deckRecord,
BoxManager& boxManager);
class Eclipse3DProperties;
template <typename T>
class GridProperties {
public:
typedef typename GridProperty<T>::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<T>& getKeyword(const std::string& keyword) const;
const GridProperty<T>& 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<T>& getOrCreateProperty(const std::string name);
GridProperty<T>& 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<int>& regionProperty );
void handleCOPYREGRecord( const DeckRecord& record, const GridProperty<int>& 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<std::string , GridProperty<T> > storage;
typedef typename storage::const_iterator storage_iterator;
struct const_iterator : public storage_iterator {
const_iterator( storage_iterator iter ) : storage_iterator( iter ) { }
const GridProperty<T>& 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<T>& 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<std::string, SupportedKeywordInfo> m_supportedKeywords;
mutable std::map<std::string , std::shared_ptr<GridProperty<T> > > m_properties;
mutable storage m_properties;
mutable std::set<std::string> m_autoGeneratedProperties;
mutable std::vector<std::shared_ptr<GridProperty<T> > > m_property_list;
};
}
#endif // ECLIPSE_GRIDPROPERTIES_HPP_

View File

@@ -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<std::string> 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<std::string> 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"