From c6632d5b8314a523015a718bb03226a84b9de74e Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Tue, 31 Jan 2023 11:41:41 +0100 Subject: [PATCH 1/3] added: file for extra serialization packers for now, it holds a packer for boost::gregorian::date --- CMakeLists_files.cmake | 2 + opm/simulators/utils/SerializationPackers.cpp | 53 +++++++++++++++++++ opm/simulators/utils/SerializationPackers.hpp | 49 +++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 opm/simulators/utils/SerializationPackers.cpp create mode 100644 opm/simulators/utils/SerializationPackers.hpp diff --git a/CMakeLists_files.cmake b/CMakeLists_files.cmake index f0a4fc0de..d99886baa 100644 --- a/CMakeLists_files.cmake +++ b/CMakeLists_files.cmake @@ -82,6 +82,7 @@ list (APPEND MAIN_SOURCE_FILES opm/simulators/utils/gatherDeferredLogger.cpp opm/simulators/utils/ParallelFileMerger.cpp opm/simulators/utils/ParallelRestart.cpp + opm/simulators/utils/SerializationPackers.cpp opm/simulators/wells/ALQState.cpp opm/simulators/wells/BlackoilWellModelConstraints.cpp opm/simulators/wells/BlackoilWellModelGeneric.cpp @@ -372,6 +373,7 @@ list (APPEND PUBLIC_HEADER_FILES opm/simulators/utils/ParallelEclipseState.hpp opm/simulators/utils/ParallelRestart.hpp opm/simulators/utils/PropsCentroidsDataHandle.hpp + opm/simulators/utils/SerializationPackers.hpp opm/simulators/utils/VectorVectorDataHandle.hpp opm/simulators/wells/ALQState.hpp opm/simulators/wells/BlackoilWellModel.hpp diff --git a/opm/simulators/utils/SerializationPackers.cpp b/opm/simulators/utils/SerializationPackers.cpp new file mode 100644 index 000000000..6d7d927db --- /dev/null +++ b/opm/simulators/utils/SerializationPackers.cpp @@ -0,0 +1,53 @@ +/* + Copyright 2019 Equinor AS. + + This file is part of the Open Porous Media project (OPM). + + OPM is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OPM is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with OPM. If not, see . +*/ + +#include +#include + +#include + +namespace Opm { +namespace Serialization { +namespace detail { + +std::size_t Packing:: +packSize(const boost::gregorian::date& data) +{ + return Packing::packSize(boost::gregorian::to_simple_string(data)); +} + +void Packing:: +pack(const boost::gregorian::date& data, + std::vector& buffer, int& position) +{ + Packing::pack(boost::gregorian::to_simple_string(data), buffer, position); +} + +void Packing:: +unpack(boost::gregorian::date& data, + std::vector& buffer, int& position) +{ + std::string date; + Packing::unpack(date, buffer, position); + data = boost::gregorian::from_simple_string(date); +} + +} // end namespace detail +} // end namespace Serialization +} // end namespace Opm diff --git a/opm/simulators/utils/SerializationPackers.hpp b/opm/simulators/utils/SerializationPackers.hpp new file mode 100644 index 000000000..d30bb50df --- /dev/null +++ b/opm/simulators/utils/SerializationPackers.hpp @@ -0,0 +1,49 @@ +/* + Copyright 2019 Equinor AS. + + This file is part of the Open Porous Media project (OPM). + + OPM is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OPM is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with OPM. If not, see . +*/ +#ifndef SERIALIZATION_PACKERS_HPP +#define SERIALIZATION_PACKERS_HPP + +#include + +#include + +// Additional packers for serializers using the mempacker. + +namespace Opm { +namespace Serialization { +namespace detail { + +template<> +struct Packing +{ + static std::size_t packSize(const boost::gregorian::date& data); + + static void pack(const boost::gregorian::date& data, + std::vector& buffer, int& position); + + static void unpack(boost::gregorian::date& data, + std::vector& buffer, int& position); +}; + +} + +} // end namespace Serialization +} // end namespace Opm + +#endif // SERIALIZATION_PACKERS_HPP From 4786deeffce47dc2cff073884dc1274ce859633d Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Mon, 13 Feb 2023 17:59:35 +0100 Subject: [PATCH 2/3] SimulatorTimer: include boost header instead of forwarding drop the pointer usage which was used to enable forwarding --- opm/simulators/timestepping/SimulatorTimer.cpp | 6 +++--- opm/simulators/timestepping/SimulatorTimer.hpp | 11 ++++++----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/opm/simulators/timestepping/SimulatorTimer.cpp b/opm/simulators/timestepping/SimulatorTimer.cpp index 62f57d1d6..35fa9413e 100644 --- a/opm/simulators/timestepping/SimulatorTimer.cpp +++ b/opm/simulators/timestepping/SimulatorTimer.cpp @@ -34,7 +34,7 @@ namespace Opm SimulatorTimer::SimulatorTimer() : current_step_(0), current_time_(0.0), - start_date_(std::make_shared(2012,1,1)) // A really arbitrary default starting value?! + start_date_(2012,1,1) // A really arbitrary default starting value?! { } @@ -61,7 +61,7 @@ namespace Opm } setCurrentStepNum(report_step); - *start_date_ = boost::posix_time::from_time_t(schedule.getStartTime()).date(); + start_date_ = boost::posix_time::from_time_t(schedule.getStartTime()).date(); } /// Whether the current step is the first step. @@ -111,7 +111,7 @@ namespace Opm boost::posix_time::ptime SimulatorTimer::startDateTime() const { - return boost::posix_time::ptime(*start_date_); + return boost::posix_time::ptime(start_date_); } diff --git a/opm/simulators/timestepping/SimulatorTimer.hpp b/opm/simulators/timestepping/SimulatorTimer.hpp index c4217712b..2394cde81 100644 --- a/opm/simulators/timestepping/SimulatorTimer.hpp +++ b/opm/simulators/timestepping/SimulatorTimer.hpp @@ -23,10 +23,11 @@ #include #include -#include -#include +#include -namespace boost { namespace gregorian { class date; } } +#include +#include +#include namespace Opm { @@ -114,14 +115,14 @@ namespace Opm bool lastStepFailed() const override { return false; } /// return copy of object - std::unique_ptr< SimulatorTimerInterface > clone() const override; + std::unique_ptr clone() const override; private: std::vector timesteps_; int current_step_; double current_time_; double total_time_; - std::shared_ptr start_date_; + boost::gregorian::date start_date_; }; From f2a14b2d07f60349fe9b4c8cc813301cc3266521 Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Tue, 31 Jan 2023 12:09:20 +0100 Subject: [PATCH 3/3] SimulatorTimer: add serialization support --- CMakeLists_files.cmake | 1 + .../timestepping/SimulatorTimer.cpp | 24 ++++++- .../timestepping/SimulatorTimer.hpp | 16 ++++- tests/test_RestartSerialization.cpp | 72 +++++++++++++++++++ tests/test_timer.cpp | 1 + 5 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 tests/test_RestartSerialization.cpp diff --git a/CMakeLists_files.cmake b/CMakeLists_files.cmake index d99886baa..1df312023 100644 --- a/CMakeLists_files.cmake +++ b/CMakeLists_files.cmake @@ -199,6 +199,7 @@ list (APPEND TEST_SOURCE_FILES tests/test_parallelwellinfo.cpp tests/test_preconditionerfactory.cpp tests/test_relpermdiagnostics.cpp + tests/test_RestartSerialization.cpp tests/test_stoppedwells.cpp tests/test_timer.cpp tests/test_vfpproperties.cpp diff --git a/opm/simulators/timestepping/SimulatorTimer.cpp b/opm/simulators/timestepping/SimulatorTimer.cpp index 35fa9413e..5ef763204 100644 --- a/opm/simulators/timestepping/SimulatorTimer.cpp +++ b/opm/simulators/timestepping/SimulatorTimer.cpp @@ -20,11 +20,12 @@ #include "config.h" #include #include +#include #include #include #include -#include +#include #include namespace Opm @@ -38,6 +39,18 @@ namespace Opm { } + SimulatorTimer SimulatorTimer::serializationTestObject() + { + SimulatorTimer res; + res.timesteps_ = {1.0, 2.0, 3.0}; + res.current_step_ = 4; + res.current_time_ = 5.0; + res.total_time_ = 6.0; + res.start_date_ = boost::gregorian::date(2023,1,31); + + return res; + } + /// Initialize from parameters. Accepts the following: /// num_psteps (default 1) /// stepsize_days (default 1) @@ -162,6 +175,13 @@ namespace Opm return std::make_unique(*this); } - + bool SimulatorTimer::operator==(const SimulatorTimer& rhs) const + { + return this->timesteps_ == rhs.timesteps_ && + this->current_step_ == rhs.current_step_ && + this->current_time_ == rhs.current_time_ && + this->total_time_ == rhs.total_time_ && + this->start_date_ == rhs.start_date_; + } } // namespace Opm diff --git a/opm/simulators/timestepping/SimulatorTimer.hpp b/opm/simulators/timestepping/SimulatorTimer.hpp index 2394cde81..b0af8443c 100644 --- a/opm/simulators/timestepping/SimulatorTimer.hpp +++ b/opm/simulators/timestepping/SimulatorTimer.hpp @@ -20,7 +20,6 @@ #ifndef OPM_SIMULATORTIMER_HEADER_INCLUDED #define OPM_SIMULATORTIMER_HEADER_INCLUDED -#include #include #include @@ -33,6 +32,7 @@ namespace Opm { class ParameterGroup; + class Schedule; class SimulatorTimer : public SimulatorTimerInterface { @@ -44,6 +44,8 @@ namespace Opm /// Default constructor. SimulatorTimer(); + static SimulatorTimer serializationTestObject(); + /// Initialize from parameters. Accepts the following: /// num_psteps (default 1) /// stepsize_days (default 1) @@ -117,6 +119,18 @@ namespace Opm /// return copy of object std::unique_ptr clone() const override; + template + void serializeOp(Serializer& serializer) + { + serializer(timesteps_); + serializer(current_step_); + serializer(current_time_); + serializer(total_time_); + serializer(start_date_); + } + + bool operator==(const SimulatorTimer& rhs) const; + private: std::vector timesteps_; int current_step_; diff --git a/tests/test_RestartSerialization.cpp b/tests/test_RestartSerialization.cpp new file mode 100644 index 000000000..ecc895293 --- /dev/null +++ b/tests/test_RestartSerialization.cpp @@ -0,0 +1,72 @@ +/* + Copyright 2019 SINTEF Digital, Mathematics and Cybernetics. + + This file is part of the Open Porous Media project (OPM). + + OPM is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OPM is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with OPM. If not, see . +*/ + +#include + +#include + +#include +#include + +#define BOOST_TEST_MODULE TestRestartSerialization +#define BOOST_TEST_NO_MAIN + +#include +#include + +template +std::tuple PackUnpack(T& in) +{ + Opm::Serialization::MemPacker packer; + Opm::Serializer ser(packer); + ser.pack(in); + size_t pos1 = ser.position(); + T out{}; + ser.unpack(out); + size_t pos2 = ser.position(); + + return std::make_tuple(out, pos1, pos2); +} + +#define TEST_FOR_TYPE_NAMED_OBJ(TYPE, NAME, OBJ) \ +BOOST_AUTO_TEST_CASE(NAME) \ +{ \ + auto val1 = Opm::TYPE::OBJ(); \ + auto val2 = PackUnpack(val1); \ + BOOST_CHECK_MESSAGE(std::get<1>(val2) == std::get<2>(val2), "Packed size differ from unpack size for " #TYPE); \ + BOOST_CHECK_MESSAGE(val1 == std::get<0>(val2), "Deserialized " #TYPE " differ"); \ +} + +#define TEST_FOR_TYPE_NAMED(TYPE, NAME) \ + TEST_FOR_TYPE_NAMED_OBJ(TYPE, NAME, serializationTestObject) + +#define TEST_FOR_TYPE(TYPE) \ + TEST_FOR_TYPE_NAMED(TYPE, TYPE) + +TEST_FOR_TYPE(SimulatorTimer) + +bool init_unit_test_func() +{ + return true; +} + +int main(int argc, char** argv) +{ + return boost::unit_test::unit_test_main(&init_unit_test_func, argc, argv); +} diff --git a/tests/test_timer.cpp b/tests/test_timer.cpp index 221fa9d22..f0de81534 100644 --- a/tests/test_timer.cpp +++ b/tests/test_timer.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include