From a825a060c30f119ab2474a57a19261fdfed8140f Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Tue, 31 Jan 2023 12:38:54 +0100 Subject: [PATCH] EclGenericVanguard: add serialization support --- ebos/eclgenericvanguard.cc | 88 +++++++++++++++++++++++++++-- ebos/eclgenericvanguard.hh | 16 +++++- tests/test_RestartSerialization.cpp | 28 +++++++++ 3 files changed, 125 insertions(+), 7 deletions(-) diff --git a/ebos/eclgenericvanguard.cc b/ebos/eclgenericvanguard.cc index 1962a5fa0..9c3f851f1 100644 --- a/ebos/eclgenericvanguard.cc +++ b/ebos/eclgenericvanguard.cc @@ -24,18 +24,46 @@ #include #include +#include +#include + #include #include #include #include #include +#include +#include #include +#include +#include +#include +#include +#include +#include +#include #include +#include +#include #include -#include -#include #include +#include +#include +#include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include #include @@ -57,9 +85,25 @@ std::unique_ptr EclGenericVanguard::comm_; EclGenericVanguard::SimulationModelParams EclGenericVanguard::modelParams_; EclGenericVanguard::EclGenericVanguard() + : EclGenericVanguard(std::move(modelParams_)) +{} + +EclGenericVanguard::EclGenericVanguard(SimulationModelParams&& params) : python(std::make_shared()) { - defineSimulationModel(std::move(modelParams_)); + defineSimulationModel(std::move(params)); +} + +EclGenericVanguard::SimulationModelParams +EclGenericVanguard::serializationTestParams() +{ + SimulationModelParams result; + result.actionState_ = std::make_unique(Action::State::serializationTestObject()); + result.eclSchedule_ = std::make_unique(Schedule::serializationTestObject()); + result.summaryState_ = std::make_unique(SummaryState::serializationTestObject()); + result.udqState_ = std::make_unique(UDQState::serializationTestObject()); + + return result; } EclGenericVanguard::~EclGenericVanguard() = default; @@ -73,6 +117,7 @@ void EclGenericVanguard::defineSimulationModel(SimulationModelParams&& params) setupTime_ = params.setupTime_; udqState_ = std::move(params.udqState_); wtestState_ = std::move(params.wtestState_); + summaryState_ = std::move(params.summaryState_); } void EclGenericVanguard::readDeck(const std::string& filename) @@ -80,7 +125,7 @@ void EclGenericVanguard::readDeck(const std::string& filename) Dune::Timer setupTimer; setupTimer.start(); - Opm::readDeck(EclGenericVanguard::comm(), + Opm::readDeck(comm(), filename, modelParams_.eclState_, modelParams_.eclSchedule_, @@ -178,7 +223,8 @@ void EclGenericVanguard::init() std::transform(caseName_.begin(), caseName_.end(), caseName_.begin(), ::toupper); } - this->summaryState_ = std::make_unique( TimeService::from_time_t(this->eclSchedule_->getStartTime() )); + if (!this->summaryState_) + this->summaryState_ = std::make_unique( TimeService::from_time_t(this->eclSchedule_->getStartTime() )); // Initialize parallelWells with all local wells const auto& schedule_wells = schedule().getWellsatEnd(); @@ -201,7 +247,7 @@ void EclGenericVanguard::init() if (comm.rank() == 0) { const auto& wells = this->schedule().getWellsatEnd(); - for ( const auto& well: wells) + for (const auto& well : wells) { hasMsWell = hasMsWell || well.isMultiSegment(); } @@ -244,4 +290,34 @@ std::unordered_map EclGenericVanguard::allA return this->eclState_->aquifer().numericalAquifers().allAquiferCells(); } +template<> +void EclGenericVanguard:: +serializeOp>(Serializer& serializer) +{ + serializer(*summaryState_); + serializer(*udqState_); + serializer(*actionState_); + serializer(*eclSchedule_); +} + +bool EclGenericVanguard::operator==(const EclGenericVanguard& rhs) const +{ + auto cmp_ptr = [](const auto& a, const auto& b) + { + if (!a && !b) { + return true; + } + + if (a && b) { + return *a == *b; + } + + return false; + }; + return cmp_ptr(this->summaryState_, rhs.summaryState_); + cmp_ptr(this->udqState_, rhs.udqState_) && + cmp_ptr(this->actionState_, rhs.actionState_) && + cmp_ptr(this->eclSchedule_, rhs.eclSchedule_); +} + } // namespace Opm diff --git a/ebos/eclgenericvanguard.hh b/ebos/eclgenericvanguard.hh index f3eebf515..5fc211013 100644 --- a/ebos/eclgenericvanguard.hh +++ b/ebos/eclgenericvanguard.hh @@ -28,8 +28,11 @@ #define EWOMS_ECL_GENERIC_VANGUARD_HH #include + #include + #include + #if DUNE_VERSION_NEWER(DUNE_COMMON, 2, 7) #include #else @@ -68,9 +71,9 @@ public: struct SimulationModelParams { double setupTime_; - std::unique_ptr comm_; std::unique_ptr udqState_; std::unique_ptr actionState_; + std::unique_ptr summaryState_; std::unique_ptr wtestState_; std::shared_ptr eclState_; std::shared_ptr eclSchedule_; @@ -84,6 +87,7 @@ public: * \details Needs to be in compile unit. */ EclGenericVanguard(); + explicit EclGenericVanguard(SimulationModelParams&& params); /*! * \brief Destructor. @@ -91,6 +95,8 @@ public: */ ~EclGenericVanguard(); + static SimulationModelParams serializationTestParams(); + /*! * \brief Returns the canonical path to a deck file. * @@ -255,6 +261,14 @@ public: return *comm_; } + // Private to avoid pulling schedule in header. + // Static state is not serialized, only use for restart. + template + void serializeOp(Serializer& serializer); + + // Only compares dynamic state. + bool operator==(const EclGenericVanguard& rhs) const; + protected: void updateOutputDir_(std::string outputDir, bool enableEclCompatFile); diff --git a/tests/test_RestartSerialization.cpp b/tests/test_RestartSerialization.cpp index f075d0fc0..8a5f7a3a9 100644 --- a/tests/test_RestartSerialization.cpp +++ b/tests/test_RestartSerialization.cpp @@ -20,9 +20,15 @@ #include #include +#include #include +#include +#include +#include +#include + #include #include #include @@ -75,6 +81,28 @@ TEST_FOR_TYPE_NAMED_OBJ(ATE, AdaptiveTimeSteppingEbosPID, serializationTestObjec TEST_FOR_TYPE_NAMED_OBJ(ATE, AdaptiveTimeSteppingEbosPIDIt, serializationTestObjectPIDIt) TEST_FOR_TYPE_NAMED_OBJ(ATE, AdaptiveTimeSteppingEbosSimple, serializationTestObjectSimple) +BOOST_AUTO_TEST_CASE(EclGenericVanguard) +{ + auto in_params = Opm::EclGenericVanguard::serializationTestParams(); + Opm::EclGenericVanguard val1(std::move(in_params)); + Opm::Serialization::MemPacker packer; + Opm::Serializer ser(packer); + ser.pack(val1); + const size_t pos1 = ser.position(); + Opm::EclGenericVanguard::SimulationModelParams out_params; + out_params.setupTime_ = 0.0; + out_params.actionState_ = std::make_unique(); + out_params.udqState_ = std::make_unique(); + out_params.eclSchedule_ = std::make_shared(); + out_params.summaryState_ = std::make_unique(); + Opm::EclGenericVanguard val2(std::move(out_params)); + ser.unpack(val2); + const size_t pos2 = ser.position(); + + BOOST_CHECK_MESSAGE(pos1 == pos2, "Packed size differ from unpack size for EclGenericVanguard"); + BOOST_CHECK_MESSAGE(val1 == val2, "Deserialized EclGenericVanguard differ"); +} + bool init_unit_test_func() { return true;