Introduce an interface for SimulatorTimer and AdaptiveSimulatorTimer.

currentDateTime and currentPosixTime are default implementations.
This commit is contained in:
Robert K 2015-01-08 12:06:37 +01:00
parent aa9fe2a631
commit be69c4c10e
5 changed files with 147 additions and 30 deletions

View File

@ -1,5 +1,5 @@
/*
Copyright 2014 IRIS AS
Copyright (c) 2014 IRIS AS
This file is part of the Open Porous Media project (OPM).
@ -30,11 +30,13 @@
namespace Opm
{
AdaptiveSimulatorTimer::
AdaptiveSimulatorTimer( const double start_time, const double total_time, const double lastDt )
: start_time_( start_time )
, total_time_( total_time )
AdaptiveSimulatorTimer( const SimulatorTimerInterface& timer, const double lastStepTaken )
: start_date_( timer.startDate() )
, start_time_( timer.simulationTimeElapsed() )
, total_time_( start_time_ + timer.currentStepLength() )
, report_step_( timer.reportStepNum() )
, current_time_( start_time_ )
, dt_( computeInitialTimeStep( lastDt ) )
, dt_( computeInitialTimeStep( lastStepTaken ) )
, current_step_( 0 )
, steps_()
, suggestedMax_( 0.0 )
@ -86,12 +88,23 @@ namespace Opm
int AdaptiveSimulatorTimer::
currentStepNum () const { return current_step_; }
int AdaptiveSimulatorTimer::
reportStepNum () const { return report_step_; }
double AdaptiveSimulatorTimer::currentStepLength () const
{
assert( ! done () );
return dt_;
}
double AdaptiveSimulatorTimer::stepLengthTaken() const
{
assert( ! steps_.empty() );
return *(steps_.rbegin());
}
double AdaptiveSimulatorTimer::totalTime() const { return total_time_; }
double AdaptiveSimulatorTimer::simulationTimeElapsed() const { return current_time_; }
@ -143,6 +156,11 @@ namespace Opm
std::cout << "sub steps end time = " << unit::convert::to( simulationTimeElapsed(), unit::day ) << " (days)" << std::endl;
}
boost::gregorian::date AdaptiveSimulatorTimer::startDate() const
{
return start_date_;
}
double AdaptiveSimulatorTimer::
computeInitialTimeStep( const double lastDt ) const
{

View File

@ -27,6 +27,8 @@
#include <algorithm>
#include <numeric>
#include <opm/core/simulator/SimulatorTimerInterface.hpp>
namespace Opm
{
@ -35,14 +37,12 @@ namespace Opm
/// \brief Simulation timer for adaptive time stepping
///
/////////////////////////////////////////////////////////
class AdaptiveSimulatorTimer
class AdaptiveSimulatorTimer : public SimulatorTimerInterface
{
public:
/// \brief constructor taking a simulator timer to determine start and end time
/// \param start_time start time of timer
/// \param total_time total time of timer
/// \param lastDt last suggested length of time step interval
AdaptiveSimulatorTimer( const double start_time, const double total_time, const double lastDt );
/// \param timer in case of sub stepping this is the outer timer
explicit AdaptiveSimulatorTimer( const SimulatorTimerInterface& timer, const double lastStepTaken );
/// \brief advance time by currentStepLength
AdaptiveSimulatorTimer& operator++ ();
@ -53,6 +53,9 @@ namespace Opm
/// \brief \copydoc SimulationTimer::currentStepNum
int currentStepNum () const;
/// \brief return current report step
int reportStepNum() const;
/// \brief \copydoc SimulationTimer::currentStepLength
double currentStepLength () const;
@ -80,12 +83,22 @@ namespace Opm
/// \brief return average suggested step length
double suggestedAverage () const;
/// \brief Previous step length. This is the length of the step that
/// was taken to arrive at this time.
double stepLengthTaken () const;
/// \brief report start and end time as well as used steps so far
void report(std::ostream& os) const;
/// \brief start date of simulation
boost::gregorian::date startDate() const;
protected:
const boost::gregorian::date start_date_;
const double start_time_;
const double total_time_;
const int report_step_;
double current_time_;
double dt_;
int current_step_;

View File

@ -100,19 +100,11 @@ namespace Opm
return current_time_;
}
/// time elapsed since the start of the POSIX epoch (Jan 1st, 1970) [s].
time_t SimulatorTimer::currentPosixTime() const
boost::gregorian::date SimulatorTimer::startDate() const
{
tm t = boost::posix_time::to_tm(currentDateTime());
return std::mktime(&t);
return start_date_;
}
boost::posix_time::ptime SimulatorTimer::currentDateTime() const
{
return boost::posix_time::ptime(start_date_) + boost::posix_time::seconds( (int) current_time_ );
}
/// Total time.
double SimulatorTimer::totalTime() const

View File

@ -21,20 +21,23 @@
#define OPM_SIMULATORTIMER_HEADER_INCLUDED
#include <opm/parser/eclipse/EclipseState/Schedule/TimeMap.hpp>
#include <opm/core/simulator/SimulatorTimerInterface.hpp>
#include <iosfwd>
#include <vector>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
namespace Opm
{
namespace parameter { class ParameterGroup; }
class SimulatorTimer
class SimulatorTimer : public SimulatorTimerInterface
{
public:
// use default implementation of these methods
using SimulatorTimerInterface::currentDateTime;
using SimulatorTimerInterface::currentPosixTime;
/// Default constructor.
SimulatorTimer();
@ -72,20 +75,16 @@ namespace Opm
/// it is an error to call stepLengthTaken().
double stepLengthTaken () const;
/// Time elapsed since the start of the POSIX epoch (Jan 1st,
/// 1970) until the current time step begins [s].
time_t currentPosixTime() const;
/// Time elapsed since the start of the simulation until the
/// beginning of the current time step [s].
double simulationTimeElapsed() const;
/// Return the current time as a posix time object.
boost::posix_time::ptime currentDateTime() const;
/// Total time.
double totalTime() const;
/// Return start date of simulation
boost::gregorian::date startDate() const;
/// Set total time.
/// This is primarily intended for multi-epoch schedules,
/// where a timer for a given epoch does not have

View File

@ -0,0 +1,95 @@
/*
Copyright (c) 2014 IRIS 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 OPM_SIMULATORTIMERINTERFACE_HEADER_INCLUDED
#define OPM_SIMULATORTIMERINTERFACE_HEADER_INCLUDED
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/date_time/posix_time/conversion.hpp>
namespace Opm
{
namespace parameter { class ParameterGroup; }
/// Interface class for SimulatorTimer objects, to be improved.
class SimulatorTimerInterface
{
protected:
/// Default constructor, protected to not allow explicit instances of this class.
SimulatorTimerInterface() {}
public:
/// Current step number. This is the number of timesteps that
/// has been completed from the start of the run. The time
/// after initialization but before the simulation has started
/// is timestep number zero.
virtual int currentStepNum() const = 0;
/// Current report step number. This might differ from currentStepNum in case of sub stepping
virtual int reportStepNum() const { return currentStepNum(); }
/// Current step length. This is the length of the step
/// the simulator will take in the next iteration.
///
/// @note if done(), it is an error to call currentStepLength().
virtual double currentStepLength() const = 0;
/// Previous step length. This is the length of the step that
/// was taken to arrive at this time.
///
/// @note if no increments have been done (i.e. the timer is
/// still in its constructed state and currentStepNum() == 0),
/// it is an error to call stepLengthTaken().
virtual double stepLengthTaken () const = 0;
/// Time elapsed since the start of the simulation until the
/// beginning of the current time step [s].
virtual double simulationTimeElapsed() const = 0;
/// Return true if timer indicates that simulation of timer interval is finished
virtual bool done() const = 0;
/// Return start date of simulation
virtual boost::gregorian::date startDate() const = 0;
/// Return the current time as a posix time object.
virtual boost::posix_time::ptime currentDateTime() const
{
return boost::posix_time::ptime(startDate()) + boost::posix_time::seconds( (int) simulationTimeElapsed());
}
/// Time elapsed since the start of the POSIX epoch (Jan 1st,
/// 1970) until the current time step begins [s].
virtual time_t currentPosixTime() const
{
tm t = boost::posix_time::to_tm(currentDateTime());
return std::mktime(&t);
}
/// Print a report with current and total time etc.
/// Note: if done(), it is an error to call report().
//virtual void report(std::ostream& os) const = 0;
};
} // namespace Opm
#endif // OPM_SIMULATORTIMER_HEADER_INCLUDED