From 20e21b7892b1d2f3257cac917d4b5b11ef959141 Mon Sep 17 00:00:00 2001 From: Andreas Lauser Date: Thu, 20 Feb 2014 17:59:41 +0100 Subject: [PATCH] SimulatorTimer: make it possible to base it on opm-parser's TimeMap Since SimulatorTimer is a fairly shallow shim if using the TimeMap, it can also be removed relatively easily. Having said this, that would trigger _many_ changes in _a lot_ of places and I'm not motivated at all to fight that battle as long as the old parser needs to be supported. I thus decided that the best way is to add a "wrapper mode" to SimulationTimer... --- opm/core/simulator/SimulatorTimer.cpp | 59 ++++++++++++++++++++++----- opm/core/simulator/SimulatorTimer.hpp | 7 ++++ 2 files changed, 55 insertions(+), 11 deletions(-) diff --git a/opm/core/simulator/SimulatorTimer.cpp b/opm/core/simulator/SimulatorTimer.cpp index f3bef455..fddc3aa8 100644 --- a/opm/core/simulator/SimulatorTimer.cpp +++ b/opm/core/simulator/SimulatorTimer.cpp @@ -57,10 +57,21 @@ 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 { - return timesteps_.size(); + if (timeMap_) + return timeMap_->numTimesteps(); + else + return timesteps_.size(); } /// Current step number. @@ -72,13 +83,14 @@ 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; - current_time_ = std::accumulate(timesteps_.begin(), timesteps_.begin() + step, 0.0); + if (timeMap_) + current_time_ = std::accumulate(timesteps_.begin(), timesteps_.begin() + step, 0.0); } @@ -86,25 +98,37 @@ namespace Opm double SimulatorTimer::currentStepLength() const { assert(!done()); - return timesteps_[current_step_]; + if (timeMap_) + return timeMap_->getTimeStepLength(current_step_); + else + return timesteps_[current_step_]; } double SimulatorTimer::stepLengthTaken() const { assert(current_step_ > 0); - return timesteps_[current_step_ - 1]; + if (timeMap_) + return timeMap_->getTimeStepLength(current_step_ - 1); + else + return timesteps_[current_step_ - 1]; } /// Current time. double SimulatorTimer::currentTime() const { - return current_time_; + if (timeMap_) + return timeMap_->getTimePassedUntil(current_step_); + else + return current_time_; } boost::posix_time::ptime SimulatorTimer::currentDateTime() const { - return boost::posix_time::ptime(start_date_) + boost::posix_time::seconds( (int) current_time_ ); + if (timeMap_) + return timeMap_->getStartTime(current_step_); + else + return boost::posix_time::ptime(start_date_) + boost::posix_time::seconds( (int) current_time_ ); } @@ -112,7 +136,10 @@ namespace Opm /// Total time. double SimulatorTimer::totalTime() const { - return total_time_; + if (timeMap_) + return timeMap_->getTotalTime(); + else + return total_time_; } /// Set total time. @@ -121,7 +148,13 @@ namespace Opm /// access to later timesteps. void SimulatorTimer::setTotalTime(double time) { - total_time_ = 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; } /// Print a report with current and total time etc. @@ -138,7 +171,8 @@ namespace Opm SimulatorTimer& SimulatorTimer::operator++() { assert(!done()); - current_time_ += timesteps_[current_step_]; + if (!timeMap_) + current_time_ += timesteps_[current_step_]; ++current_step_; return *this; } @@ -146,7 +180,10 @@ namespace Opm /// Return true if op++() has been called numSteps() times. bool SimulatorTimer::done() const { - return int(timesteps_.size()) == current_step_; + if (timeMap_) + return int(timeMap_->numTimesteps()) <= current_step_; + else + return int(timesteps_.size()) == current_step_; } diff --git a/opm/core/simulator/SimulatorTimer.hpp b/opm/core/simulator/SimulatorTimer.hpp index a1d8a270..949859e5 100644 --- a/opm/core/simulator/SimulatorTimer.hpp +++ b/opm/core/simulator/SimulatorTimer.hpp @@ -20,6 +20,8 @@ #ifndef OPM_SIMULATORTIMER_HEADER_INCLUDED #define OPM_SIMULATORTIMER_HEADER_INCLUDED +#include + #include #include #include @@ -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 timesteps_; int current_step_; double current_time_;