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