opm-common
SummaryState.hpp
1 /*
2  Copyright 2016 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 SUMMARY_STATE_H
21 #define SUMMARY_STATE_H
22 
23 #include <opm/common/utility/TimeService.hpp>
24 #include <opm/io/eclipse/SummaryNode.hpp>
25 
26 #include <cstddef>
27 #include <ctime>
28 #include <iosfwd>
29 #include <optional>
30 #include <set>
31 #include <string>
32 #include <unordered_map>
33 #include <vector>
34 
35 namespace Opm {
36 
37 class UDQSet;
38 
39 } // namespace Opm
40 
41 // The purpose of the SummaryState class is to serve as a small container
42 // object for computed, ready to use summary values. The values will
43 // typically be used by the UDQ, WTEST and ACTIONX calculations. Observe
44 // that all values are stored in the run's output unit conventions.
45 //
46 // The main key used to access the content of this container is the eclipse
47 // style colon separated string - i.e. 'WWCT:OPX' to get the watercut in
48 // well 'OPX'. The main usage of the SummaryState class is a temporary
49 // holding ground while assembling data for the summary output, but it is
50 // also used as a context object when evaulating the condition in ACTIONX
51 // keywords. For that reason some of the data is duplicated both in the
52 // general structure and a specialized structure:
53 //
54 // SummaryState st { start, udqUndefined };
55 //
56 // st.add_well_var("OPX", "WWCT", 0.75);
57 // st.add("WGOR:OPY", 120);
58 //
59 // // The WWCT:OPX key has been added with the specialized add_well_var()
60 // // method and this data is available both with the general
61 // // st.has("WWCT:OPX") and the specialized st.has_well_var("OPX", "WWCT");
62 // st.has("WWCT:OPX") => True
63 // st.has_well_var("OPX", "WWCT") => True
64 //
65 // // The WGOR:OPY key is added with the general add("WGOR:OPY") and is *not*
66 // // accessible through the specialized st.has_well_var("OPY", "WGOR").
67 // st.has("WGOR:OPY") => True
68 // st.has_well_var("OPY", "WGOR") => False
69 
70 namespace Opm {
71 
73 {
74 public:
75  using const_iterator = std::unordered_map<std::string, double>::const_iterator;
76 
77  explicit SummaryState(time_point sim_start_arg, double udqUndefined);
78 
79  // The std::time_t constructor is only for export to Python
80  explicit SummaryState(std::time_t sim_start_arg);
81 
82  // Only used for testing purposes.
83  SummaryState() : SummaryState(std::time_t{0}) {}
84  ~SummaryState() = default;
85 
86  // The canonical way to update the SummaryState is through the
87  // update_xxx() methods which will inspect the variable and either
88  // accumulate or just assign, depending on whether it represents a total
89  // or not. The set() method is low level and unconditionally do an
90  // assignment.
91  void set(const std::string& key, double value);
92 
93  bool erase(const std::string& key);
94  bool erase_well_var(const std::string& well, const std::string& var);
95  bool erase_group_var(const std::string& group, const std::string& var);
96 
97  bool has(const std::string& key) const;
98  bool has_well_var(const std::string& well, const std::string& var) const;
99  bool has_well_var(const std::string& var) const;
100  bool has_group_var(const std::string& group, const std::string& var) const;
101  bool has_group_var(const std::string& var) const;
102  bool has_conn_var(const std::string& well, const std::string& var, std::size_t global_index) const;
103  bool has_segment_var(const std::string& well, const std::string& var, std::size_t segment) const;
104  bool has_region_var(const std::string& regSet, const std::string& var, std::size_t region) const;
105 
106  void update(const std::string& key, double value);
107  void update_well_var(const std::string& well, const std::string& var, double value);
108  void update_group_var(const std::string& group, const std::string& var, double value);
109  void update_group_var(const std::string& group, const std::string& var, EclIO::SummaryNode::Type type, double value);
110  void update_elapsed(double delta);
111  void update_udq(const UDQSet& udq_set);
112  void update_conn_var(const std::string& well, const std::string& var, std::size_t global_index, double value);
113  void update_conn_var(const std::string& well, const std::string& var, EclIO::SummaryNode::Type type, std::size_t global_index, double value);
114 
115  void update_segment_var(const std::string& well, const std::string& var, std::size_t segment, double value);
116  void update_region_var(const std::string& regSet, const std::string& var, std::size_t region, double value);
117 
118  double get(const std::string&) const;
119  double get(const std::string&, double) const;
120  double get_elapsed() const;
121  double get_well_var(const std::string& well, const std::string& var) const;
122  double get_group_var(const std::string& group, const std::string& var) const;
123  double get_conn_var(const std::string& conn, const std::string& var, std::size_t global_index) const;
124  double get_segment_var(const std::string& well, const std::string& var, std::size_t segment) const;
125  double get_region_var(const std::string& regSet, const std::string& var, std::size_t region) const;
126  double get_well_var(const std::string& well, const std::string& var, double) const;
127  double get_group_var(const std::string& group, const std::string& var, double) const;
128  double get_conn_var(const std::string& conn, const std::string& var, std::size_t global_index, double) const;
129  double get_segment_var(const std::string& well, const std::string& var, std::size_t segment, double) const;
130  double get_region_var(const std::string& regSet, const std::string& var, std::size_t region, double) const;
131  double get_udq_undefined() const { return udq_undefined; }
132 
133  bool is_undefined_value(const double val) const { return val == udq_undefined; }
134 
135  const std::vector<std::string>& wells() const;
136  std::vector<std::string> wells(const std::string& var) const;
137  const std::vector<std::string>& groups() const;
138  std::vector<std::string> groups(const std::string& var) const;
139  void append(const SummaryState& buffer);
140  const_iterator begin() const;
141  const_iterator end() const;
142  std::size_t num_wells() const;
143  std::size_t size() const;
144  bool operator==(const SummaryState& other) const;
145 
146  template<class Serializer>
147  void serializeOp(Serializer& serializer)
148  {
149  serializer(sim_start);
150  serializer(this->udq_undefined);
151  serializer(elapsed);
152  serializer(values);
153  serializer(well_values);
154  serializer(m_wells);
155  serializer(well_names);
156  serializer(group_values);
157  serializer(m_groups);
158  serializer(group_names);
159  serializer(conn_values);
160  serializer(segment_values);
161  serializer(this->region_values);
162  }
163 
164  static SummaryState serializationTestObject();
165 
166 private:
167  time_point sim_start;
168  double udq_undefined{};
169  double elapsed = 0;
170  std::unordered_map<std::string,double> values;
171 
172  // The first key is the variable and the second key is the well.
173  std::unordered_map<std::string, std::unordered_map<std::string, double>> well_values;
174  std::set<std::string> m_wells;
175  mutable std::optional<std::vector<std::string>> well_names;
176 
177  // The first key is the variable and the second key is the group.
178  std::unordered_map<std::string, std::unordered_map<std::string, double>> group_values;
179  std::set<std::string> m_groups;
180  mutable std::optional<std::vector<std::string>> group_names;
181 
182  // The first key is the variable and the second key is the well and the
183  // third is the global index. NB: The global_index has offset 1!
184  std::unordered_map<std::string, std::unordered_map<std::string, std::unordered_map<std::size_t, double>>> conn_values;
185 
186  // The first key is the variable and the second key is the well and the
187  // third is the one-based segment number.
188  std::unordered_map<std::string, std::unordered_map<std::string, std::unordered_map<std::size_t, double>>> segment_values;
189 
190  // First key is variable (e.g., ROIP), second key is region set (e.g.,
191  // FIPNUM, FIPABC), and the third key is the one-based region number.
192  std::unordered_map<std::string, std::unordered_map<std::string, std::unordered_map<std::size_t, double>>> region_values;
193 
194  // Reusable buffer for formatting connection keys in update_conn_var to avoid allocation.
195  mutable std::string conn_key_buffer_;
196 };
197 
198 std::ostream& operator<<(std::ostream& stream, const SummaryState& st);
199 
200 } // namespace Opm
201 
202 #endif // SUMMARY_STATE_H
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:30
Definition: UDQSet.hpp:186
Definition: SummaryState.hpp:72
Class for (de-)serializing.
Definition: Serializer.hpp:94