Merge pull request #498 from andlaus/use_timemap_in_simulatortimer

SimulatorTimer: make it possible to base it on opm-parser's TimeMap
This commit is contained in:
Joakim Hove 2014-02-27 14:35:16 +01:00
commit 167767e5aa
2 changed files with 55 additions and 11 deletions

View File

@ -57,9 +57,20 @@ namespace Opm
start_date_ = deck.getStartDate();
}
/// Use the SimulatorTimer as a shim around opm-parser's Opm::TimeMap
void SimulatorTimer::init(Opm::TimeMapConstPtr timeMap,
int timeStepIdx)
{
timeMap_ = timeMap;
current_step_ = timeStepIdx;
}
/// Total number of steps.
int SimulatorTimer::numSteps() const
{
if (timeMap_)
return timeMap_->numTimesteps();
else
return timesteps_.size();
}
@ -72,12 +83,13 @@ namespace Opm
/// Set current step number.
void SimulatorTimer::setCurrentStepNum(int step)
{
if (current_step_ < 0 || current_step_ > int(timesteps_.size())) {
if (current_step_ < 0 || current_step_ > int(numSteps())) {
// Note that we do allow current_step_ == timesteps_.size(),
// that is the done() state.
OPM_THROW(std::runtime_error, "Trying to set invalid step number: " << step);
}
current_step_ = step;
if (timeMap_)
current_time_ = std::accumulate(timesteps_.begin(), timesteps_.begin() + step, 0.0);
}
@ -86,24 +98,36 @@ namespace Opm
double SimulatorTimer::currentStepLength() const
{
assert(!done());
if (timeMap_)
return timeMap_->getTimeStepLength(current_step_);
else
return timesteps_[current_step_];
}
double SimulatorTimer::stepLengthTaken() const
{
assert(current_step_ > 0);
if (timeMap_)
return timeMap_->getTimeStepLength(current_step_ - 1);
else
return timesteps_[current_step_ - 1];
}
/// Current time.
double SimulatorTimer::currentTime() const
{
if (timeMap_)
return timeMap_->getTimePassedUntil(current_step_);
else
return current_time_;
}
boost::posix_time::ptime SimulatorTimer::currentDateTime() const
{
if (timeMap_)
return timeMap_->getStartTime(current_step_);
else
return boost::posix_time::ptime(start_date_) + boost::posix_time::seconds( (int) current_time_ );
}
@ -112,6 +136,9 @@ namespace Opm
/// Total time.
double SimulatorTimer::totalTime() const
{
if (timeMap_)
return timeMap_->getTotalTime();
else
return total_time_;
}
@ -121,6 +148,12 @@ namespace Opm
/// access to later timesteps.
void SimulatorTimer::setTotalTime(double time)
{
if (timeMap_) {
// well, what can we do if we use opm-parser's TimeMap?
OPM_THROW(std::logic_error,
"Not implemented: SimulatorTimer::setTotalTime() if using a TimeMap.");
}
else
total_time_ = time;
}
@ -138,6 +171,7 @@ namespace Opm
SimulatorTimer& SimulatorTimer::operator++()
{
assert(!done());
if (!timeMap_)
current_time_ += timesteps_[current_step_];
++current_step_;
return *this;
@ -146,6 +180,9 @@ namespace Opm
/// Return true if op++() has been called numSteps() times.
bool SimulatorTimer::done() const
{
if (timeMap_)
return int(timeMap_->numTimesteps()) <= current_step_;
else
return int(timesteps_.size()) == current_step_;
}

View File

@ -20,6 +20,8 @@
#ifndef OPM_SIMULATORTIMER_HEADER_INCLUDED
#define OPM_SIMULATORTIMER_HEADER_INCLUDED
#include <opm/parser/eclipse/EclipseState/Schedule/TimeMap.hpp>
#include <iosfwd>
#include <vector>
#include <boost/date_time/gregorian/gregorian.hpp>
@ -47,6 +49,10 @@ namespace Opm
/// Note that DATES are folded into TSTEP by the parser.
void init(const EclipseGridParser& deck);
/// Use the SimulatorTimer as a shim around opm-parser's Opm::TimeMap
void init(TimeMapConstPtr timeMap,
int timeStepIdx = 0);
/// Total number of steps.
int numSteps() const;
@ -99,6 +105,7 @@ namespace Opm
bool done() const;
private:
Opm::TimeMapConstPtr timeMap_;
std::vector<double> timesteps_;
int current_step_;
double current_time_;