Merge pull request #4416 from akva2/simulatortimer_serialize

SimulatorTimer: add serialization support
This commit is contained in:
Bård Skaflestad
2023-02-13 21:08:31 +01:00
committed by GitHub
7 changed files with 224 additions and 11 deletions

View File

@@ -20,11 +20,12 @@
#include "config.h"
#include <opm/simulators/timestepping/SimulatorTimer.hpp>
#include <opm/common/utility/parameters/ParameterGroup.hpp>
#include <opm/input/eclipse/Schedule/Schedule.hpp>
#include <opm/input/eclipse/Units/Units.hpp>
#include <ostream>
#include <numeric>
#include <boost/date_time/gregorian_calendar.hpp>
#include <boost/date_time/gregorian/gregorian_types.hpp>
#include <boost/date_time/posix_time/conversion.hpp>
namespace Opm
@@ -34,10 +35,22 @@ namespace Opm
SimulatorTimer::SimulatorTimer()
: current_step_(0),
current_time_(0.0),
start_date_(std::make_shared<boost::gregorian::date>(2012,1,1)) // A really arbitrary default starting value?!
start_date_(2012,1,1) // A really arbitrary default starting value?!
{
}
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)
@@ -61,7 +74,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 +124,7 @@ namespace Opm
boost::posix_time::ptime SimulatorTimer::startDateTime() const
{
return boost::posix_time::ptime(*start_date_);
return boost::posix_time::ptime(start_date_);
}
@@ -162,6 +175,13 @@ namespace Opm
return std::make_unique<SimulatorTimer>(*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

View File

@@ -20,18 +20,19 @@
#ifndef OPM_SIMULATORTIMER_HEADER_INCLUDED
#define OPM_SIMULATORTIMER_HEADER_INCLUDED
#include <opm/input/eclipse/Schedule/Schedule.hpp>
#include <opm/simulators/timestepping/SimulatorTimerInterface.hpp>
#include <iosfwd>
#include <vector>
#include <boost/date_time/gregorian/gregorian_types.hpp>
namespace boost { namespace gregorian { class date; } }
#include <iosfwd>
#include <memory>
#include <vector>
namespace Opm
{
class ParameterGroup;
class Schedule;
class SimulatorTimer : public SimulatorTimerInterface
{
@@ -43,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)
@@ -114,14 +117,26 @@ namespace Opm
bool lastStepFailed() const override { return false; }
/// return copy of object
std::unique_ptr< SimulatorTimerInterface > clone() const override;
std::unique_ptr<SimulatorTimerInterface> clone() const override;
template<class Serializer>
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<double> timesteps_;
int current_step_;
double current_time_;
double total_time_;
std::shared_ptr<boost::gregorian::date> start_date_;
boost::gregorian::date start_date_;
};

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <opm/simulators/utils/SerializationPackers.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
namespace Opm {
namespace Serialization {
namespace detail {
std::size_t Packing<false,boost::gregorian::date>::
packSize(const boost::gregorian::date& data)
{
return Packing<false,std::string>::packSize(boost::gregorian::to_simple_string(data));
}
void Packing<false,boost::gregorian::date>::
pack(const boost::gregorian::date& data,
std::vector<char>& buffer, int& position)
{
Packing<false,std::string>::pack(boost::gregorian::to_simple_string(data), buffer, position);
}
void Packing<false,boost::gregorian::date>::
unpack(boost::gregorian::date& data,
std::vector<char>& buffer, int& position)
{
std::string date;
Packing<false,std::string>::unpack(date, buffer, position);
data = boost::gregorian::from_simple_string(date);
}
} // end namespace detail
} // end namespace Serialization
} // end namespace Opm

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef SERIALIZATION_PACKERS_HPP
#define SERIALIZATION_PACKERS_HPP
#include <opm/common/utility/MemPacker.hpp>
#include <boost/date_time/gregorian/gregorian_types.hpp>
// Additional packers for serializers using the mempacker.
namespace Opm {
namespace Serialization {
namespace detail {
template<>
struct Packing<false,boost::gregorian::date>
{
static std::size_t packSize(const boost::gregorian::date& data);
static void pack(const boost::gregorian::date& data,
std::vector<char>& buffer, int& position);
static void unpack(boost::gregorian::date& data,
std::vector<char>& buffer, int& position);
};
}
} // end namespace Serialization
} // end namespace Opm
#endif // SERIALIZATION_PACKERS_HPP