A new SimulatorTimer Class

This commit is contained in:
Kai Bao 2014-03-26 15:53:54 +01:00
parent a845cfdfd7
commit 500053d6aa
2 changed files with 20 additions and 87 deletions

View File

@ -58,63 +58,34 @@ namespace Opm
}
/// Use the SimulatorTimer as a shim around opm-parser's Opm::TimeMap
void SimulatorTimer::init(Opm::TimeMapConstPtr timeMap,
size_t beginReportStepIdx,
size_t endReportStepIdx)
void SimulatorTimer::init(Opm::TimeMapConstPtr timeMap)
{
timeMap_ = timeMap;
current_step_ = 0;
beginReportStepIdx_ = beginReportStepIdx;
endReportStepIdx_ = std::min(timeMap_->numTimesteps() + 1, endReportStepIdx);
total_time_ = timeMap->getTotalTime();
timesteps_.resize(timeMap->numTimesteps());
for ( size_t i = 0; i < timeMap->numTimesteps(); ++i ) {
timesteps_[i] = timeMap->getTimeStepLength(i);
}
boost::posix_time::ptime start_time = timeMap->getStartTime(0);
start_date_ = start_time.date();
}
/// Total number of steps.
int SimulatorTimer::numSteps() const
{
if (timeMap_)
return endReportStepIdx_ - beginReportStepIdx_;
else
return timesteps_.size();
}
/// Index of the first considered simulation episode
size_t SimulatorTimer::beginReportStepIndex() const
{
if (!timeMap_) {
OPM_THROW(std::runtime_error, "indexFirstEpisode() is only implemented "
"for simulation timers which are based on Opm::TimeMap");
}
return beginReportStepIdx_;
}
/// Index of the last considered simulation episode
size_t SimulatorTimer::endReportStepIndex() const
{
if (!timeMap_) {
OPM_THROW(std::runtime_error, "indexLastEpisode() is only implemented "
"for simulation timers which are based on Opm::TimeMap");
}
return endReportStepIdx_;
}
/// Current step number.
int SimulatorTimer::currentStepNum() const
{
return current_step_ + beginReportStepIdx_;
return current_step_;
}
/// Set current step number.
void SimulatorTimer::setCurrentStepNum(int step)
{
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);
}
@ -123,28 +94,18 @@ namespace Opm
double SimulatorTimer::currentStepLength() const
{
assert(!done());
if (timeMap_)
return timeMap_->getTimeStepLength(beginReportStepIdx_ + current_step_);
else
return timesteps_[current_step_];
}
double SimulatorTimer::stepLengthTaken() const
{
assert(current_step_ > 0);
if (timeMap_)
return timeMap_->getTimeStepLength(beginReportStepIdx_ + current_step_ - 1);
else
return timesteps_[current_step_ - 1];
}
/// time elapsed since the start of the simulation [s].
double SimulatorTimer::simulationTimeElapsed() const
{
if (timeMap_)
return
timeMap_->getTimePassedUntil(beginReportStepIdx_ + current_step_);
else
return current_time_;
}
@ -157,9 +118,6 @@ namespace Opm
boost::posix_time::ptime SimulatorTimer::currentDateTime() const
{
if (timeMap_)
return timeMap_->getStartTime(beginReportStepIdx_ + current_step_);
else
return boost::posix_time::ptime(start_date_) + boost::posix_time::seconds( (int) current_time_ );
}
@ -168,10 +126,6 @@ namespace Opm
/// Total time.
double SimulatorTimer::totalTime() const
{
if (timeMap_)
return
timeMap_->getTotalTime();
else
return total_time_;
}
@ -181,12 +135,6 @@ 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;
}
@ -204,7 +152,6 @@ namespace Opm
SimulatorTimer& SimulatorTimer::operator++()
{
assert(!done());
if (!timeMap_)
current_time_ += timesteps_[current_step_];
++current_step_;
return *this;
@ -213,9 +160,6 @@ namespace Opm
/// Return true if op++() has been called numSteps() times.
bool SimulatorTimer::done() const
{
if (timeMap_)
return current_step_ > int(endReportStepIdx_ - beginReportStepIdx_ - 1);
else
return int(timesteps_.size()) == current_step_;
}

View File

@ -50,19 +50,11 @@ namespace Opm
void init(const EclipseGridParser& deck);
/// Use the SimulatorTimer as a shim around opm-parser's Opm::TimeMap
void init(TimeMapConstPtr timeMap,
size_t beginReportStepIdx = 0,
size_t endReportStepIdx = std::numeric_limits<size_t>::max());
void init(TimeMapConstPtr timeMap);
/// Total number of steps.
int numSteps() const;
/// Index of the first report step considered
size_t beginReportStepIndex() const;
/// Index of the next-after-last report step to be considered
size_t endReportStepIndex() const;
/// 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
@ -117,10 +109,7 @@ namespace Opm
bool done() const;
private:
Opm::TimeMapConstPtr timeMap_;
std::vector<double> timesteps_;
size_t beginReportStepIdx_;
size_t endReportStepIdx_;
int current_step_;
double current_time_;
double total_time_;