My Project
DeckItem.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 DECKITEM_HPP
21 #define DECKITEM_HPP
22 
23 #include <string>
24 #include <vector>
25 #include <memory>
26 #include <ostream>
27 
28 #include <opm/parser/eclipse/Units/Dimension.hpp>
29 #include <opm/parser/eclipse/Utility/Typetools.hpp>
30 #include <opm/parser/eclipse/Deck/UDAValue.hpp>
31 #include <opm/parser/eclipse/Deck/value_status.hpp>
32 
33 
34 namespace Opm {
35  class DeckOutput;
36 
37  class DeckItem {
38  public:
39 
40  DeckItem() = default;
41  DeckItem( const std::string&, int);
42  DeckItem( const std::string&, RawString);
43  DeckItem( const std::string&, std::string);
44  DeckItem( const std::string&, double) = delete;
45  DeckItem( const std::string&, UDAValue) = delete;
46  DeckItem( const std::string&, UDAValue, const std::vector<Dimension>& active_dim, const std::vector<Dimension>& default_dim);
47  DeckItem( const std::string&, double, const std::vector<Dimension>& active_dim, const std::vector<Dimension>& default_dim);
48 
49  static DeckItem serializeObject();
50 
51  const std::string& name() const;
52 
53  // return true if the default value was used for a given data point
54  bool defaultApplied( size_t ) const;
55 
56  // Return true if the item has a value for the current index;
57  // does not differentiate between default values from the
58  // config and values which have been set in the deck.
59  bool hasValue( size_t ) const;
60 
61  // if the number returned by this method is less than what is semantically
62  // expected (e.g. size() is less than the number of cells in the grid for
63  // keywords like e.g. SGL), then the remaining values are defaulted. The deck
64  // creates the defaulted items if all their sizes are fully specified by the
65  // keyword, though...
66 
67  size_t data_size() const;
68 
69  template<typename T>
70  T get( size_t index ) const;
71 
72 
73  double getSIDouble( size_t ) const;
74  std::string getTrimmedString( size_t ) const;
75 
76  template< typename T > const std::vector< T >& getData() const;
77  const std::vector< double >& getSIDoubleData() const;
78  const std::vector<value::status>& getValueStatus() const;
79 
80  template< typename T>
81  void shrink_to_fit();
82 
83 
84  void push_back( UDAValue );
85  void push_back( int );
86  void push_back( double );
87  void push_back( std::string );
88  void push_back( RawString );
89  void push_back( UDAValue, size_t );
90  void push_back( int, size_t );
91  void push_back( double, size_t );
92  void push_back( std::string, size_t );
93  void push_backDefault( UDAValue, std::size_t n = 1 );
94  void push_backDefault( int, std::size_t n = 1 );
95  void push_backDefault( double, std::size_t n = 1 );
96  void push_backDefault( std::string, std::size_t n = 1 );
97  void push_backDefault( RawString, std::size_t n = 1 );
98  // trying to access the data of a "dummy default item" will raise an exception
99 
100  template <typename T>
101  void push_backDummyDefault( std::size_t n = 1 );
102 
103  type_tag getType() const;
104 
105  void write(DeckOutput& writer) const;
106  friend std::ostream& operator<<(std::ostream& os, const DeckItem& item);
107 
108 
109  /*
110  The comparison can be adjusted with the cmp_default and
111  cmp_numeric flags. If cmp_default is set to true the
112  comparison will take the defaulted status of the items into
113  account, i.e. two items will compare differently if one is
114  defaulted and the other has the default value explicitly
115  set. The default behaviour is cmp_default == false -
116  itrespective of whether they have been set explicitly or
117  have been defaulted.
118  */
119  bool equal(const DeckItem& other, bool cmp_default, bool cmp_numeric) const;
120 
121  /*
122  The operator== is implemented based on the equal( ) method,
123  with the arguments cmp_default=false and cmp_numeric=true.
124  */
125  bool operator==(const DeckItem& other) const;
126  bool operator!=(const DeckItem& other) const;
127  static bool to_bool(std::string string_value);
128 
129  bool is_uda() { return (type == get_type< UDAValue >()); };
130  bool is_double() { return type == get_type< double >(); };
131  bool is_int() { return type == get_type< int >() ; };
132  bool is_string() { return type == get_type< std::string >(); };
133 
134  UDAValue& get_uda() { return uval[0]; };
135 
136  template<class Serializer>
137  void serializeOp(Serializer& serializer)
138  {
139  serializer(dval);
140  serializer(ival);
141  serializer(sval);
142  serializer.vector(rsval);
143  serializer.vector(uval);
144  serializer(type);
145  serializer(item_name);
146  serializer.template vector<value::status, false>(value_status);
147  serializer(raw_data);
148  serializer.vector(active_dimensions);
149  serializer.vector(default_dimensions);
150  }
151 
152  void reserve_additionalRawString(std::size_t);
153  private:
154  mutable std::vector< double > dval;
155  std::vector< int > ival;
156  std::vector< std::string > sval;
157  std::vector< RawString > rsval;
158  std::vector< UDAValue > uval;
159 
160  type_tag type = type_tag::unknown;
161 
162  std::string item_name;
163  std::vector<value::status> value_status;
164  /*
165  To save space we mutate the dval object in place when asking for SI
166  data; the current state of of the dval member is tracked with the
167  raw_data bool member.
168  */
169  mutable bool raw_data = true;
170  std::vector< Dimension > active_dimensions;
171  std::vector< Dimension > default_dimensions;
172 
173  template< typename T > std::vector< T >& value_ref();
174  template< typename T > const std::vector< T >& value_ref() const;
175  template< typename T > void push( T );
176  template< typename T > void push( T, size_t );
177  template< typename T > void push_default( T, std::size_t n );
178  template< typename T > void write_vector(DeckOutput& writer, const std::vector<T>& data) const;
179  };
180 }
181 #endif /* DECKITEM_HPP */
182 
Definition: DeckItem.hpp:37
Definition: DeckOutput.hpp:29
Definition: Typetools.hpp:37
Definition: Serializer.hpp:38
Definition: UDAValue.hpp:32
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:29