My Project
Deck.hpp
1 /*
2  Copyright 2013 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 
20 #ifndef DECK_HPP
21 #define DECK_HPP
22 
23 #include <map>
24 #include <memory>
25 #include <ostream>
26 #include <optional>
27 #include <vector>
28 #include <string>
29 
30 #include <opm/parser/eclipse/Deck/DeckTree.hpp>
31 #include <opm/parser/eclipse/Deck/DeckKeyword.hpp>
32 #include <opm/parser/eclipse/Units/UnitSystem.hpp>
33 
34 #ifdef OPM_PARSER_DECK_API_WARNING
35 #ifndef OPM_PARSER_DECK_API
36 #pragma message "\n\n" \
37 " ----------------------------------------------------------------------------------\n" \
38 " The current compilation unit includes the header Deck.hpp. Outside of opm-parser \n" \
39 " you are encouraged to use the EclipseState API instead of the low level Deck API. \n" \
40 " If use of the Deck API is absolutely necessary you can silence this warning with \n" \
41 " #define OPM_PARSER_DECK_API before including the Deck.hpp header. \n" \
42 " ----------------------------------------------------------------------------------\n" \
43 ""
44 #endif
45 #endif
46 
47 
48 
49 namespace Opm {
50 
51  /*
52  * The Deck (container) class owns all memory given to it via .addX(), as
53  * do all inner objects. This means that the Deck object itself must stay
54  * alive as long as DeckItem (and friends) are needed, to avoid
55  * use-after-free.
56  */
57  class DeckOutput;
58 
59  class DeckView {
60  public:
61  typedef std::vector< DeckKeyword >::const_iterator const_iterator;
62 
63  bool hasKeyword( const DeckKeyword& keyword ) const;
64  bool hasKeyword( const std::string& keyword ) const;
65  template< class Keyword >
66  bool hasKeyword() const {
67  return hasKeyword( Keyword::keywordName );
68  }
69 
70  const DeckKeyword& getKeyword( const std::string& keyword, size_t index ) const;
71  const DeckKeyword& getKeyword( const std::string& keyword ) const;
72  const DeckKeyword& getKeyword( size_t index ) const;
73 
74  const DeckKeyword& operator[](std::size_t index) const;
75  DeckKeyword& getKeyword( size_t index );
76  template< class Keyword >
77  const DeckKeyword& getKeyword() const {
78  return getKeyword( Keyword::keywordName );
79  }
80  template< class Keyword >
81  const DeckKeyword& getKeyword( size_t index ) const {
82  return getKeyword( Keyword::keywordName, index );
83  }
84  template< class Keyword >
85  std::size_t count() const {
86  return count( Keyword::keywordName );
87  }
88 
89  const std::vector< const DeckKeyword* > getKeywordList( const std::string& keyword ) const;
90  template< class Keyword >
91  const std::vector< const DeckKeyword* > getKeywordList() const {
92  return getKeywordList( Keyword::keywordName );
93  }
94 
95  size_t count(const std::string& keyword) const;
96  size_t size() const;
97 
98  const_iterator begin() const;
99  const_iterator end() const;
100 
101 
102  protected:
103  void add( const DeckKeyword*, const_iterator, const_iterator );
104 
105  const std::vector< size_t >& offsets( const std::string& ) const;
106 
107  DeckView( const_iterator first, const_iterator last );
108  explicit DeckView( std::pair< const_iterator, const_iterator > );
109  DeckView() = default;
110  void init( const_iterator, const_iterator );
111 
112  private:
113  const_iterator first;
114  const_iterator last;
115  std::map< std::string, std::vector< size_t > > keywordMap;
116 
117  };
118 
119  class Deck : private DeckView {
120  public:
121  using DeckView::const_iterator;
122  using DeckView::hasKeyword;
123  using DeckView::getKeyword;
124  using DeckView::getKeywordList;
125  using DeckView::count;
126  using DeckView::size;
127  using DeckView::begin;
128  using DeckView::end;
129  using DeckView::operator[];
130 
131  using iterator = std::vector< DeckKeyword >::iterator;
132 
133  Deck();
134  Deck( const Deck& );
135  Deck( Deck&& );
136 
137  static Deck serializeObject();
138 
139  Deck& operator=(const Deck& rhs);
140  bool operator==(const Deck& data) const;
141 
142  void addKeyword( DeckKeyword&& keyword );
143  void addKeyword( const DeckKeyword& keyword );
144 
145  DeckKeyword& getKeyword( size_t );
146 
147  const UnitSystem& getDefaultUnitSystem() const;
148  const UnitSystem& getActiveUnitSystem() const;
149  UnitSystem& getActiveUnitSystem();
150  UnitSystem& getDefaultUnitSystem();
151  void selectActiveUnitSystem( UnitSystem::UnitType unit_type );
152 
153  const std::string& getInputPath() const;
154  std::string getDataFile() const;
155  void setDataFile(const std::string& dataFile);
156  std::string makeDeckPath(const std::string& path) const;
157  DeckTree& tree();
158  DeckTree tree() const;
159 
160  std::size_t size() const;
161  bool empty() const;
162  iterator begin();
163  iterator end();
164  void write( DeckOutput& output ) const ;
165  friend std::ostream& operator<<(std::ostream& os, const Deck& deck);
166 
167  template<class Serializer>
168  void serializeOp(Serializer& serializer)
169  {
170  serializer.vector(keywordList);
171  defaultUnits.serializeOp(serializer);
172  serializer(activeUnits);
173  serializer(m_dataFile);
174  serializer(input_path);
175  serializer(unit_system_access_count);
176  if (!serializer.isSerializing())
177  this->init(this->keywordList.begin(), this->keywordList.end());
178  }
179 
180  private:
181  Deck(std::vector<DeckKeyword>&& keywordList);
182 
183  std::vector< DeckKeyword > keywordList;
184  UnitSystem defaultUnits;
185  std::unique_ptr<UnitSystem> activeUnits;
186 
187  std::optional<std::string> m_dataFile;
188  std::string input_path;
189  DeckTree file_tree;
190  mutable std::size_t unit_system_access_count = 0;
191  };
192 }
193 #endif /* DECK_HPP */
Definition: DeckKeyword.hpp:36
Definition: DeckOutput.hpp:29
Definition: DeckTree.hpp:38
Definition: Deck.hpp:59
Definition: Deck.hpp:119
Definition: Serializer.hpp:38
Definition: UnitSystem.hpp:34
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:29