My Project
Loading...
Searching...
No Matches
State.hpp
1/*
2 Copyright 2020 Equinor 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 ACTION_STATE_HPP
21#define ACTION_STATE_HPP
22
23#include <cstddef>
24#include <ctime>
25#include <map>
26#include <memory>
27#include <optional>
28#include <string>
29#include <utility>
30#include <vector>
31
32namespace Opm::RestartIO {
33 struct RstState;
34} // namespace Opm::RestartIO
35
36namespace Opm::Action {
37
38 class ActionX;
39 class Actions;
40 class PyAction;
41 class Result;
42
43} // namespace Opm::Action
44
45namespace Opm::Action {
46
50class State
51{
52public:
55 {
56 public:
64 bool hasWell(const std::string& well) const;
65
68
75 bool operator==(const MatchSet& that) const;
76
82 template <class Serializer>
83 void serializeOp(Serializer& serializer)
84 {
85 serializer(this->wells_);
86 }
87
88 friend class State;
89
90 private:
92 std::vector<std::string> wells_{};
93 };
94
103 void add_run(const ActionX& action, std::time_t sim_time, const Result& result);
104
110 void add_run(const PyAction& action, bool result);
111
117 std::size_t run_count(const ActionX& action) const;
118
127 std::time_t run_time(const ActionX& action) const;
128
136 const MatchSet* result(const std::string& action) const;
137
143 std::optional<bool> python_result(const std::string& action) const;
144
151 void load_rst(const Actions& action_config,
152 const RestartIO::RstState& rst_state);
153
159 template<class Serializer>
160 void serializeOp(Serializer& serializer)
161 {
162 serializer(this->run_state);
163 serializer(this->last_result);
164 serializer(this->m_python_result);
165 }
166
169
176 bool operator==(const State& other) const;
177
178private:
180 struct RunState
181 {
183 RunState() = default;
184
188 explicit RunState(const std::time_t sim_time)
189 : run_count(1)
190 , last_run(sim_time)
191 {}
192
196 void add_run(const std::time_t sim_time)
197 {
198 this->last_run = sim_time;
199 this->run_count += 1;
200 }
201
203 static RunState serializationTestObject()
204 {
205 RunState rs;
206
207 rs.run_count = 100;
208 rs.last_run = 123456;
209
210 return rs;
211 }
212
219 bool operator==(const RunState& other) const
220 {
221 return (this->run_count == other.run_count)
222 && (this->last_run == other.last_run);
223 }
224
230 template <class Serializer>
231 void serializeOp(Serializer& serializer)
232 {
233 serializer(this->run_count);
234 serializer(this->last_run);
235 }
236
238 std::size_t run_count{};
239
241 std::time_t last_run{};
242 };
243
248 using ActionID = std::pair<std::string, std::size_t>;
249
252 std::map<ActionID, RunState> run_state{};
253
256 std::map<std::string, MatchSet> last_result{};
257
259 std::map<std::string, bool> m_python_result{};
260};
261
262} // namespace Opm::Action
263
264#endif // ACTION_STATE_HPP
Matching entities from a successfully triggered ActionX object.
Definition State.hpp:55
bool operator==(const MatchSet &that) const
Equality predicate.
Definition State.cpp:68
static MatchSet serializationTestObject()
Create a serialisation test object.
Definition State.cpp:57
void serializeOp(Serializer &serializer)
Convert between byte array and object representation.
Definition State.hpp:83
bool hasWell(const std::string &well) const
Whether or not a particular well exists in the set of matching entities.
Definition State.cpp:49
Management information about the current run's ACTION system, especially concerning the number of tim...
Definition State.hpp:51
void add_run(const ActionX &action, std::time_t sim_time, const Result &result)
Record ActionX Run.
Definition State.cpp:95
std::size_t run_count(const ActionX &action) const
Retrieve number of times an action has run.
Definition State.cpp:75
std::time_t run_time(const ActionX &action) const
Retrieve timestamp of the last time an action ran.
Definition State.cpp:83
const MatchSet * result(const std::string &action) const
Retrieve set of matching entities from the last time an action ran.
Definition State.cpp:125
void serializeOp(Serializer &serializer)
Convert between byte array and object representation.
Definition State.hpp:160
void load_rst(const Actions &action_config, const RestartIO::RstState &rst_state)
Load action state from restart file.
Definition State.cpp:147
static State serializationTestObject()
Create a serialisation test object.
Definition State.cpp:169
std::optional< bool > python_result(const std::string &action) const
Query for the result of running a PyAction.
Definition State.cpp:134
bool operator==(const State &other) const
Equality predicate.
Definition State.cpp:161
Class for (de-)serializing.
Definition Serializer.hpp:94
Definition state.hpp:56