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,64 +58,35 @@ namespace Opm
} }
/// Use the SimulatorTimer as a shim around opm-parser's Opm::TimeMap /// Use the SimulatorTimer as a shim around opm-parser's Opm::TimeMap
void SimulatorTimer::init(Opm::TimeMapConstPtr timeMap, void SimulatorTimer::init(Opm::TimeMapConstPtr timeMap)
size_t beginReportStepIdx,
size_t endReportStepIdx)
{ {
timeMap_ = timeMap;
current_step_ = 0; current_step_ = 0;
beginReportStepIdx_ = beginReportStepIdx; total_time_ = timeMap->getTotalTime();
endReportStepIdx_ = std::min(timeMap_->numTimesteps() + 1, endReportStepIdx); 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. /// Total number of steps.
int SimulatorTimer::numSteps() const int SimulatorTimer::numSteps() const
{ {
if (timeMap_) return timesteps_.size();
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. /// Current step number.
int SimulatorTimer::currentStepNum() const int SimulatorTimer::currentStepNum() const
{ {
return current_step_ + beginReportStepIdx_; return current_step_;
} }
/// Set current step number. /// Set current step number.
void SimulatorTimer::setCurrentStepNum(int step) 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; current_step_ = step;
if (timeMap_) current_time_ = std::accumulate(timesteps_.begin(), timesteps_.begin() + step, 0.0);
current_time_ = std::accumulate(timesteps_.begin(), timesteps_.begin() + step, 0.0);
} }
@ -123,29 +94,19 @@ namespace Opm
double SimulatorTimer::currentStepLength() const double SimulatorTimer::currentStepLength() const
{ {
assert(!done()); assert(!done());
if (timeMap_) return timesteps_[current_step_];
return timeMap_->getTimeStepLength(beginReportStepIdx_ + current_step_);
else
return timesteps_[current_step_];
} }
double SimulatorTimer::stepLengthTaken() const double SimulatorTimer::stepLengthTaken() const
{ {
assert(current_step_ > 0); assert(current_step_ > 0);
if (timeMap_) return timesteps_[current_step_ - 1];
return timeMap_->getTimeStepLength(beginReportStepIdx_ + current_step_ - 1);
else
return timesteps_[current_step_ - 1];
} }
/// time elapsed since the start of the simulation [s]. /// time elapsed since the start of the simulation [s].
double SimulatorTimer::simulationTimeElapsed() const double SimulatorTimer::simulationTimeElapsed() const
{ {
if (timeMap_) return current_time_;
return
timeMap_->getTimePassedUntil(beginReportStepIdx_ + current_step_);
else
return current_time_;
} }
/// time elapsed since the start of the POSIX epoch (Jan 1st, 1970) [s]. /// time elapsed since the start of the POSIX epoch (Jan 1st, 1970) [s].
@ -157,10 +118,7 @@ namespace Opm
boost::posix_time::ptime SimulatorTimer::currentDateTime() const boost::posix_time::ptime SimulatorTimer::currentDateTime() const
{ {
if (timeMap_) return boost::posix_time::ptime(start_date_) + boost::posix_time::seconds( (int) current_time_ );
return timeMap_->getStartTime(beginReportStepIdx_ + current_step_);
else
return boost::posix_time::ptime(start_date_) + boost::posix_time::seconds( (int) current_time_ );
} }
@ -168,11 +126,7 @@ namespace Opm
/// Total time. /// Total time.
double SimulatorTimer::totalTime() const double SimulatorTimer::totalTime() const
{ {
if (timeMap_) return total_time_;
return
timeMap_->getTotalTime();
else
return total_time_;
} }
/// Set total time. /// Set total time.
@ -181,13 +135,7 @@ namespace Opm
/// access to later timesteps. /// access to later timesteps.
void SimulatorTimer::setTotalTime(double time) void SimulatorTimer::setTotalTime(double time)
{ {
if (timeMap_) { total_time_ = time;
// 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. /// Print a report with current and total time etc.
@ -204,8 +152,7 @@ namespace Opm
SimulatorTimer& SimulatorTimer::operator++() SimulatorTimer& SimulatorTimer::operator++()
{ {
assert(!done()); assert(!done());
if (!timeMap_) current_time_ += timesteps_[current_step_];
current_time_ += timesteps_[current_step_];
++current_step_; ++current_step_;
return *this; return *this;
} }
@ -213,10 +160,7 @@ namespace Opm
/// Return true if op++() has been called numSteps() times. /// Return true if op++() has been called numSteps() times.
bool SimulatorTimer::done() const bool SimulatorTimer::done() const
{ {
if (timeMap_) return int(timesteps_.size()) == current_step_;
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); void init(const EclipseGridParser& deck);
/// Use the SimulatorTimer as a shim around opm-parser's Opm::TimeMap /// Use the SimulatorTimer as a shim around opm-parser's Opm::TimeMap
void init(TimeMapConstPtr timeMap, void init(TimeMapConstPtr timeMap);
size_t beginReportStepIdx = 0,
size_t endReportStepIdx = std::numeric_limits<size_t>::max());
/// Total number of steps. /// Total number of steps.
int numSteps() const; 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 /// Current step number. This is the number of timesteps that
/// has been completed from the start of the run. The time /// has been completed from the start of the run. The time
/// after initialization but before the simulation has started /// after initialization but before the simulation has started
@ -117,10 +109,7 @@ namespace Opm
bool done() const; bool done() const;
private: private:
Opm::TimeMapConstPtr timeMap_;
std::vector<double> timesteps_; std::vector<double> timesteps_;
size_t beginReportStepIdx_;
size_t endReportStepIdx_;
int current_step_; int current_step_;
double current_time_; double current_time_;
double total_time_; double total_time_;