GridProperties.hpp
Go to the documentation of this file.
1 /*
2  Copyright 2014 Statoil ASA.
3 
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #ifndef ECLIPSE_GRIDPROPERTIES_HPP_
20 #define ECLIPSE_GRIDPROPERTIES_HPP_
21 
22 
23 #include <string>
24 #include <vector>
25 #include <tuple>
26 #include <unordered_map>
27 
30 
31 /*
32  This class implements a container (std::unordered_map<std::string ,
33  Gridproperty<T>>) of Gridproperties. Usage is as follows:
34 
35  1. Instantiate the class; passing the number of grid cells and the
36  supported keywords as a list of strings to the constructor.
37 
38  2. Query the container with the supportsKeyword() and hasKeyword()
39  methods.
40 
41  3. When you ask the container to get a keyword with the
42  getKeyword() method it will automatically create a new
43  GridProperty object if the container does not have this
44  property.
45 */
46 
47 
48 namespace Opm {
49 
50 template <typename T>
52 public:
54 
55  GridProperties(std::shared_ptr<const EclipseGrid> eclipseGrid , std::shared_ptr<const std::vector<SupportedKeywordInfo> > supportedKeywords) {
56  m_eclipseGrid = eclipseGrid;
57 
58  for (auto iter = supportedKeywords->begin(); iter != supportedKeywords->end(); ++iter)
59  m_supportedKeywords[iter->getKeywordName()] = *iter;
60  }
61 
62 
63  bool supportsKeyword(const std::string& keyword) const {
64  return m_supportedKeywords.count( keyword ) > 0;
65  }
66 
67  bool hasKeyword(const std::string& keyword) const {
68  return m_properties.count( keyword ) > 0;
69  }
70 
71  size_t size() const {
72  return m_property_list.size();
73  }
74 
75 
76  std::shared_ptr<GridProperty<T> > getKeyword(const std::string& keyword) {
77  if (!hasKeyword(keyword))
78  addKeyword(keyword);
79 
80  return m_properties.at( keyword );
81  }
82 
83  std::shared_ptr<GridProperty<T> > getKeyword(size_t index) {
84  if (index < size())
85  return m_property_list[index];
86  else
87  throw std::invalid_argument("Invalid index");
88  }
89 
90 
91  std::shared_ptr<GridProperty<T> > getInitializedKeyword(const std::string& keyword) const {
92  if (hasKeyword(keyword))
93  return m_properties.at( keyword );
94  else {
95  if (supportsKeyword(keyword))
96  throw std::invalid_argument("Keyword: " + keyword + " is supported - but not initialized.");
97  else
98  throw std::invalid_argument("Keyword: " + keyword + " is not supported.");
99  }
100  }
101 
102 
103  bool addKeyword(const std::string& keywordName) {
104  if (!supportsKeyword( keywordName ))
105  throw std::invalid_argument("The keyword: " + keywordName + " is not supported in this container");
106 
107  if (hasKeyword(keywordName))
108  return false;
109  else {
110  auto supportedKeyword = m_supportedKeywords.at( keywordName );
111  int nx = m_eclipseGrid->getNX();
112  int ny = m_eclipseGrid->getNY();
113  int nz = m_eclipseGrid->getNZ();
114  std::shared_ptr<GridProperty<T> > newProperty(new GridProperty<T>(nx , ny , nz , supportedKeyword));
115 
116  m_properties.insert( std::pair<std::string , std::shared_ptr<GridProperty<T> > > ( keywordName , newProperty ));
117  m_property_list.push_back( newProperty );
118  return true;
119  }
120  }
121 
122 
123  template <class Keyword>
124  bool hasKeyword() const {
125  return hasKeyword( Keyword::keywordName );
126  }
127 
128  template <class Keyword>
129  std::shared_ptr<GridProperty<T> > getKeyword() {
130  return getKeyword( Keyword::keywordName );
131  }
132 
133  template <class Keyword>
134  std::shared_ptr<GridProperty<T> > getInitializedKeyword() const {
135  return getInitializedKeyword( Keyword::keywordName );
136  }
137 
138 
139 
140 private:
141  std::shared_ptr<const EclipseGrid> m_eclipseGrid;
142  std::unordered_map<std::string, SupportedKeywordInfo> m_supportedKeywords;
143  std::map<std::string , std::shared_ptr<GridProperty<T> > > m_properties;
144  std::vector<std::shared_ptr<GridProperty<T> > > m_property_list;
145 };
146 
147 }
148 
149 #endif
Definition: GridProperty.hpp:64
Definition: Deck.hpp:29
GridProperties(std::shared_ptr< const EclipseGrid > eclipseGrid, std::shared_ptr< const std::vector< SupportedKeywordInfo > > supportedKeywords)
Definition: GridProperties.hpp:55
Definition: GridProperties.hpp:51
std::shared_ptr< GridProperty< T > > getKeyword(size_t index)
Definition: GridProperties.hpp:83
bool hasKeyword() const
Definition: GridProperties.hpp:124
std::shared_ptr< GridProperty< T > > getInitializedKeyword(const std::string &keyword) const
Definition: GridProperties.hpp:91
GridProperty< T >::SupportedKeywordInfo SupportedKeywordInfo
Definition: GridProperties.hpp:53
bool addKeyword(const std::string &keywordName)
Definition: GridProperties.hpp:103
std::shared_ptr< GridProperty< T > > getKeyword(const std::string &keyword)
Definition: GridProperties.hpp:76
size_t size() const
Definition: GridProperties.hpp:71
std::shared_ptr< GridProperty< T > > getInitializedKeyword() const
Definition: GridProperties.hpp:134
bool supportsKeyword(const std::string &keyword) const
Definition: GridProperties.hpp:63
bool hasKeyword(const std::string &keyword) const
Definition: GridProperties.hpp:67
std::shared_ptr< GridProperty< T > > getKeyword()
Definition: GridProperties.hpp:129
Definition: GridProperty.hpp:153