Merge pull request #2712 from joakim-hove/serialize-action-state

Serialize Action::State
This commit is contained in:
Joakim Hove
2021-09-27 13:47:22 +02:00
committed by GitHub
5 changed files with 136 additions and 0 deletions

View File

@@ -81,6 +81,15 @@ public:
WellSet& intersect(const WellSet& other);
WellSet& add(const WellSet& other);
bool operator==(const WellSet& other) const;
template<class Serializer>
void serializeOp(Serializer& serializer)
{
serializer.template set<std::unordered_set<std::string>, false>(this->well_set);
}
static WellSet serializeObject();
private:
std::unordered_set<std::string> well_set;
};
@@ -89,6 +98,7 @@ private:
class Result {
public:
Result() = default;
explicit Result(bool result_arg);
Result(bool result_arg, const std::vector<std::string>& wells);
Result(bool result_arg, const WellSet& wells);
@@ -104,6 +114,15 @@ public:
Result& operator&=(const Result& other);
bool operator==(const Result& other) const;
template<class Serializer>
void serializeOp(Serializer& serializer)
{
serializer(this->result);
serializer(this->matching_wells);
}
static Result serializeObject();
private:
void assign(bool value);
bool result;

View File

@@ -38,6 +38,8 @@ class Actions;
class State {
struct RunState {
RunState() = default;
RunState(std::time_t sim_time)
: run_count(1)
, last_run(sim_time)
@@ -48,16 +50,51 @@ struct RunState {
this->run_count += 1;
}
static RunState serializeObject()
{
RunState rs;
rs.run_count = 100;
rs.last_run = 123456;
return rs;
}
bool operator==(const RunState& other) const {
return this->run_count == other.run_count &&
this->last_run == other.last_run;
}
template<class Serializer>
void serializeOp(Serializer& serializer)
{
serializer(this->run_count);
serializer(this->last_run);
}
std::size_t run_count;
std::time_t last_run;
};
public:
void add_run(const ActionX& action, std::time_t sim_time, Result result);
std::size_t run_count(const ActionX& action) const;
std::time_t run_time(const ActionX& action) const;
std::optional<Result> result(const std::string& action) const;
void load_rst(const Actions& action_config, const RestartIO::RstState& rst_state);
template<class Serializer>
void serializeOp(Serializer& serializer)
{
serializer.map(this->run_state);
serializer.map(this->last_result);
}
static State serializeObject();
bool operator==(const State& other) const;
private:
using action_id = std::pair<std::string, std::size_t>;
static action_id make_id(const ActionX& action);

View File

@@ -35,6 +35,7 @@ namespace RestartIO {
class UDQState {
public:
UDQState() = default;
UDQState(double undefined);
bool has(const std::string& key) const;
@@ -55,6 +56,57 @@ public:
std::vector<char> serialize() const;
void deserialize(const std::vector<char>& buffer);
bool operator==(const UDQState& other) const;
static UDQState serializeObject() {
UDQState st;
st.undef_value = 78;
st.scalar_values = {{"FU1", 100}, {"FU2", 200}};
st.assignments = {{"GU1", 99}, {"GU2", 199}};
st.assignments = {{"DU1", 299}, {"DU2", 399}};
st.well_values.emplace("W1", std::unordered_map<std::string, double>{{"U1", 100}, {"U2", 200}});
st.well_values.emplace("W2", std::unordered_map<std::string, double>{{"U1", 700}, {"32", 600}});
st.group_values.emplace("G1", std::unordered_map<std::string, double>{{"U1", 100}, {"U2", 200}});
st.group_values.emplace("G2", std::unordered_map<std::string, double>{{"U1", 700}, {"32", 600}});
return st;
}
template<class Serializer>
void pack_unpack_wgmap(Serializer& serializer, std::unordered_map<std::string, std::unordered_map<std::string, double>>& wgmap) {
std::size_t map_size = wgmap.size();
serializer(map_size);
if (serializer.isSerializing()) {
for (auto& [udq_key, values] : wgmap) {
serializer(udq_key);
serializer.template map<std::unordered_map<std::string, double>, false>(values);
}
} else {
for (std::size_t index=0; index < map_size; index++) {
std::string udq_key;
std::unordered_map<std::string, double> inner_map;
serializer(udq_key);
serializer.template map<std::unordered_map<std::string, double>, false>(inner_map);
wgmap.emplace(udq_key, inner_map);
}
}
}
template<class Serializer>
void serializeOp(Serializer& serializer)
{
serializer(this->undef_value);
serializer.template map<std::unordered_map<std::string, double>, false>(this->scalar_values);
serializer.template map<std::unordered_map<std::string, std::size_t>, false>(this->assignments);
serializer.template map<std::unordered_map<std::string, std::size_t>, false>(this->defines);
pack_unpack_wgmap(serializer, this->well_values);
pack_unpack_wgmap(serializer, this->group_values);
}
private:
void add(const std::string& udq_key, const UDQSet& result);
double get_wg_var(const std::string& well, const std::string& key, UDQVarType var_type) const;

View File

@@ -155,5 +155,19 @@ bool WellSet::operator==(const WellSet& other) const {
return this->well_set == other.well_set;
}
WellSet WellSet::serializeObject() {
WellSet ws;
ws.well_set = {"W1", "W2", "W3"};
return ws;
}
Result Result::serializeObject() {
Result rs;
rs.result = false;
rs.matching_wells = WellSet::serializeObject();
return rs;
}
}
}

View File

@@ -83,5 +83,19 @@ void State::load_rst(const Actions& action_config, const RestartIO::RstState& rs
}
}
bool State::operator==(const State& other) const {
return this->run_state == other.run_state &&
this->last_result == other.last_result;
}
State State::serializeObject() {
State st;
st.run_state.insert(std::make_pair( std::make_pair("ACTION", 100), RunState::serializeObject()));
st.last_result.insert( std::make_pair("ACTION", Result::serializeObject()));
return st;
}
}
}