DynamicState.hpp
Go to the documentation of this file.
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 
21 #ifndef DYNAMICSTATE_HPP_
22 #define DYNAMICSTATE_HPP_
23 
25 #include <boost/shared_ptr.hpp>
26 #include <boost/date_time.hpp>
27 #include <stdexcept>
28 
29 
30 namespace Opm {
31 
55  template <class T>
56  class DynamicState {
57  public:
58 
59 
60  DynamicState(const TimeMapConstPtr timeMap, T initialValue) {
61  m_timeMap = timeMap;
62  init( initialValue );
63  }
64 
65  void globalReset( T newValue ) {
66  init( newValue );
67  }
68 
69 
70 
71  const T& at(size_t index) const {
72  if (index >= m_timeMap->size())
73  throw std::range_error("Index value is out range.");
74 
75  if (index >= m_data.size())
76  return m_currentValue;
77 
78  return m_data.at(index);
79  }
80 
81 
82  const T& operator[](size_t index) const {
83  return at(index);
84  }
85 
86 
87  T get(size_t index) const {
88  if (index >= m_timeMap->size())
89  throw std::range_error("Index value is out range.");
90 
91  if (index >= m_data.size())
92  return m_currentValue;
93 
94  return m_data[index];
95  }
96 
97 
98 
99  void updateInitial(T initialValue) {
100  if (m_initialValue != initialValue) {
101  size_t index;
102  m_initialValue = initialValue;
103  if (m_initialRange > 0) {
104  for (index = 0; index < m_initialRange; index++)
105  m_data[index] = m_initialValue;
106  } else
107  m_currentValue = initialValue;
108  }
109  }
110 
111 
112  size_t size() const {
113  return m_data.size();
114  }
115 
116 
121  bool update(size_t index , T value) {
122  bool change = (value != m_currentValue);
123  if (index >= (m_timeMap->size()))
124  throw std::range_error("Index value is out range.");
125 
126  if (m_data.size() > 0) {
127  if (index < (m_data.size() - 1))
128  throw std::invalid_argument("Elements must be added in weakly increasing order");
129  }
130 
131  {
132  size_t currentSize = m_data.size();
133  if (currentSize <= index) {
134  for (size_t i = currentSize; i <= index; i++)
135  m_data.push_back( m_currentValue );
136  }
137  }
138 
139  m_data[index] = value;
140  m_currentValue = value;
141  if (m_initialRange == 0)
142  m_initialRange = index;
143 
144  return change;
145  }
146 
147 
148 
149  private:
150 
151  void init(T initialValue) {
152  m_data.clear();
153  m_currentValue = initialValue;
154  m_initialValue = initialValue;
155  m_initialRange = 0;
156  }
157 
158 
159  std::vector<T> m_data;
160  TimeMapConstPtr m_timeMap;
161  T m_currentValue;
162  T m_initialValue;
163  size_t m_initialRange;
164  };
165 }
166 
167 
168 
169 #endif
DynamicState(const TimeMapConstPtr timeMap, T initialValue)
Definition: DynamicState.hpp:60
std::shared_ptr< const TimeMap > TimeMapConstPtr
Definition: TimeMap.hpp:77
Definition: Deck.hpp:29
const T & at(size_t index) const
Definition: DynamicState.hpp:71
Definition: DynamicState.hpp:56
size_t size() const
Definition: DynamicState.hpp:112
bool update(size_t index, T value)
Definition: DynamicState.hpp:121
const T & operator[](size_t index) const
Definition: DynamicState.hpp:82
void globalReset(T newValue)
Definition: DynamicState.hpp:65
void updateInitial(T initialValue)
Definition: DynamicState.hpp:99