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