28 #ifndef EWOMS_SIMULATOR_HH 29 #define EWOMS_SIMULATOR_HH 38 #include <dune/common/version.hh> 39 #include <dune/common/parallel/mpihelper.hh> 50 namespace Properties {
74 template <
class TypeTag>
77 typedef typename GET_PROP_TYPE(TypeTag, Scalar) Scalar;
78 typedef typename GET_PROP_TYPE(TypeTag, GridManager) GridManager;
79 typedef typename GET_PROP_TYPE(TypeTag, GridView) GridView;
80 typedef typename GET_PROP_TYPE(TypeTag, Model) Model;
81 typedef typename GET_PROP_TYPE(TypeTag, Problem) Problem;
93 verbose_ = verbose && Dune::MPIHelper::getCollectiveCommunication().rank() == 0;
101 const std::string& predetTimeStepFile =
103 if (!predetTimeStepFile.empty()) {
104 std::ifstream is(predetTimeStepFile);
108 forcedTimeSteps_.push_back(dt);
113 episodeStartTime_ = 0;
114 episodeLength_ = std::numeric_limits<Scalar>::max();
119 std::cout <<
"Allocating the grid\n" << std::flush;
120 gridManager_.reset(
new GridManager(*
this));
123 std::cout <<
"Distributing the grid\n" << std::flush;
124 gridManager_->loadBalance();
127 std::cout <<
"Allocating the model\n" << std::flush;
128 model_.reset(
new Model(*
this));
131 std::cout <<
"Allocating the problem\n" << std::flush;
132 problem_.reset(
new Problem(*
this));
135 std::cout <<
"Finish init of the model\n" << std::flush;
136 model_->finishInit();
139 std::cout <<
"Finish init of the problem\n" << std::flush;
140 problem_->finishInit();
145 std::cout <<
"Construction of simulation done\n" << std::flush;
154 "The simulation time at which the simulation is finished [s]");
156 "The size of the initial time step [s]");
158 "The simulation time at which a restart should be attempted [s]");
160 "A file with a list of predetermined time step sizes (one " 161 "time step per line)");
163 GridManager::registerParameters();
164 Model::registerParameters();
165 Problem::registerParameters();
172 {
return *gridManager_; }
178 {
return *gridManager_; }
184 {
return gridManager_->gridView(); }
203 {
return *problem_; }
210 {
return *problem_; }
224 {
return startTime_; }
244 timeStepIdx_ = stepIdx;
277 {
return setupTimer_; }
284 {
return executionTimer_; }
291 {
return prePostProcessTimer_; }
298 {
return linearizeTimer_; }
305 {
return solveTimer_; }
312 {
return updateTimer_; }
319 {
return writeTimer_; }
332 { timeStepSize_ = value; }
340 { timeStepIdx_ = value; }
349 Scalar maximumTimeStepSize =
353 return std::min(timeStepSize_, maximumTimeStepSize);
361 {
return timeStepIdx_; }
371 { finished_ = yesno; }
381 assert(timeStepSize_ >= 0.0);
384 *std::numeric_limits<Scalar>::epsilon()*1e3;
385 return finished_ || (this->
time()*(1.0 + eps) >=
endTime());
394 static const Scalar eps = std::numeric_limits<Scalar>::epsilon()*1e3;
396 return finished_ || (this->
time() + timeStepSize_)*(1.0 + eps) >=
endTime();
435 episodeStartTime_ = startTime_ + time_;
436 episodeLength_ = len;
445 { episodeIdx_ = episodeIdx; }
453 {
return episodeIdx_; }
460 {
return episodeStartTime_; }
468 { episodeLength_ = dt; }
475 {
return episodeLength_; }
483 static const Scalar eps = std::numeric_limits<Scalar>::epsilon()*1e3;
494 static const Scalar eps = std::numeric_limits<Scalar>::epsilon()*1e3;
515 return std::max<Scalar>(0.0,
534 TimerGuard executionTimerGuard(executionTimer_);
535 TimerGuard prePostProcessTimerGuard(prePostProcessTimer_);
540 if (restartTime > -1e30) {
547 std::cout <<
"Deserialize from file '" << res.
fileName() <<
"'\n" << std::flush;
549 problem_->deserialize(res);
550 model_->deserialize(res);
553 std::cout <<
"Deserialization done." 557 <<
"\n" << std::flush;
562 std::cout <<
"Applying the initial solution of the \"" << problem_->name()
563 <<
"\" problem\n" << std::flush;
565 Scalar oldTimeStepSize = timeStepSize_;
566 int oldTimeStepIdx = timeStepIdx_;
570 model_->applyInitialSolution();
573 if (problem_->shouldWriteOutput())
574 problem_->writeOutput();
576 timeStepSize_ = oldTimeStepSize;
577 timeStepIdx_ = oldTimeStepIdx;
581 executionTimer_.
start();
585 prePostProcessTimer_.
start();
589 problem_->beginEpisode();
594 problem_->endEpisode();
595 prePostProcessTimer_.
stop();
599 episodeBegins =
false;
602 std::cout <<
"Begin time step " <<
timeStepIndex() + 1 <<
". " 609 problem_->beginTimeStep();
613 problem_->endTimeStep();
614 problem_->endEpisode();
615 prePostProcessTimer_.
stop();
618 prePostProcessTimer_.
stop();
622 problem_->timeIntegration();
625 const auto&
model = problem_->model();
626 prePostProcessTimer_ +=
model.prePostProcessTimer();
627 linearizeTimer_ +=
model.linearizeTimer();
628 solveTimer_ +=
model.solveTimer();
629 updateTimer_ +=
model.updateTimer();
634 const auto&
model = problem_->model();
635 prePostProcessTimer_ +=
model.prePostProcessTimer();
636 linearizeTimer_ +=
model.linearizeTimer();
637 solveTimer_ +=
model.solveTimer();
638 updateTimer_ +=
model.updateTimer();
641 prePostProcessTimer_.
start();
642 problem_->endTimeStep();
643 prePostProcessTimer_.
stop();
647 if (problem_->shouldWriteOutput())
648 problem_->writeOutput();
653 problem_->advanceTimeLevel();
657 std::cout <<
"Time step " <<
timeStepIndex() + 1 <<
" done. " 661 <<
"\n" << std::flush;
668 prePostProcessTimer_.
start();
672 problem_->endEpisode();
673 episodeBegins =
true;
677 if (timeStepIdx_ < static_cast<int>(forcedTimeSteps_.size())) {
679 dt = forcedTimeSteps_[timeStepIdx_];
683 dt = problem_->nextTimeStepSize();
688 prePostProcessTimer_.
stop();
692 if (problem_->shouldWriteRestartFile())
696 executionTimer_.
stop();
698 problem_->finalize();
709 std::ostringstream oss;
710 oss << std::setprecision(4);
713 if (timeInSeconds >= 365.25*24*60*60) {
714 int years =
static_cast<int>(timeInSeconds/(365.25*24*60*60));
715 int days =
static_cast<int>((timeInSeconds - years*(365.25*24*60*60))/(24*60*60));
717 double accuracy = 1e-2;
719 std::round(1.0/accuracy*
721 - years*(365.25*24*60*60)
722 - days*(24*60*60))/(60*60))
725 oss << years <<
" years, " << days <<
" days, " << hours <<
" hours";
727 else if (timeInSeconds >= 24.0*60*60) {
728 int days =
static_cast<int>(timeInSeconds/(24*60*60));
729 int hours =
static_cast<int>((timeInSeconds - days*(24*60*60))/(60*60));
731 double accuracy = 1e-2;
733 std::round(1.0/accuracy*
739 oss << days <<
" days, " << hours <<
" hours, " << minutes <<
" minutes";
741 else if (timeInSeconds >= 60.0*60) {
742 int hours =
static_cast<int>(timeInSeconds/(60*60));
743 int minutes =
static_cast<int>((timeInSeconds - hours*(60*60))/60);
745 double accuracy = 1e-2;
747 std::round(1.0/accuracy*
753 oss << hours <<
" hours, " << minutes <<
" minutes, " << seconds <<
" seconds";
755 else if (timeInSeconds >= 60.0) {
756 int minutes =
static_cast<int>(timeInSeconds/60);
758 double accuracy = 1e-3;
760 std::round(1.0/accuracy*
765 oss << minutes <<
" minutes, " << seconds <<
" seconds";
767 else if (!isAmendment)
768 oss << timeInSeconds <<
" seconds";
797 std::cout <<
"Serialize to file '" << res.fileName() <<
"'" 799 <<
"\n" << std::flush;
802 problem_->serialize(res);
803 model_->serialize(res);
814 template <
class Restarter>
817 restarter.serializeSectionBegin(
"Simulator");
818 restarter.serializeStream()
819 << episodeIdx_ <<
" " 820 << episodeStartTime_ <<
" " 821 << episodeLength_ <<
" " 824 << timeStepIdx_ <<
" ";
825 restarter.serializeSectionEnd();
835 template <
class Restarter>
838 restarter.deserializeSectionBegin(
"Simulator");
839 restarter.deserializeStream()
846 restarter.deserializeSectionEnd();
850 std::unique_ptr<GridManager> gridManager_;
851 std::unique_ptr<Model> model_;
852 std::unique_ptr<Problem> problem_;
855 Scalar episodeStartTime_;
856 Scalar episodeLength_;
866 std::vector<Scalar> forcedTimeSteps_;
871 Scalar timeStepSize_;
A simple class which makes sure that a timer gets stopped if an exception is thrown.
Definition: timerguard.hh:40
void setEpisodeLength(Scalar dt)
Sets the length in seconds of the current episode.
Definition: simulator.hh:467
Scalar maxTimeStepSize() const
Aligns the time step size to the episode boundary and to the end time of the simulation.
Definition: simulator.hh:403
bool willBeFinished() const
Returns true if the simulation is finished after the time level is incremented by the current time st...
Definition: simulator.hh:392
void serializeBegin(Simulator &simulator)
Write the current state of the model to disk.
Definition: restart.hh:97
void run()
Runs the simulation using a given problem class.
Definition: simulator.hh:530
Scalar episodeMaxTimeStepSize() const
Aligns the time step size to the episode boundary if the current time step crosses the boundary of th...
Definition: simulator.hh:504
void deserializeEnd()
Stop reading the restart file.
Definition: restart.hh:265
void setEndTime(Scalar t)
Set the time of simulated seconds at which the simulation runs.
Definition: simulator.hh:262
void start()
Start counting the time resources used by the simulation.
Definition: timer.hh:62
Scalar episodeLength() const
Returns the length of the current episode in simulated time .
Definition: simulator.hh:474
void setTime(Scalar t)
Set the current simulated time, don't change the current time step index.
Definition: simulator.hh:232
Definition: baseauxiliarymodule.hh:37
Model & model()
Return the physical model used in the simulation.
Definition: simulator.hh:189
Scalar episodeStartTime() const
Returns the absolute time when the current episode started .
Definition: simulator.hh:459
static void registerParameters()
Registers all runtime parameters used by the simulation.
Definition: simulator.hh:151
int timeStepIndex() const
Returns number of time steps which have been executed since the beginning of the simulation.
Definition: simulator.hh:360
Scalar startTime() const
Return the time of the start of the simulation.
Definition: simulator.hh:223
Problem & problem()
Return the object which specifies the pysical setup of the simulation.
Definition: simulator.hh:202
void startNextEpisode(Scalar len=std::numeric_limits< Scalar >::max())
Start the next episode, but don't change the episode identifier.
Definition: simulator.hh:432
bool finished() const
Returns true if the simulation is finished.
Definition: simulator.hh:379
bool episodeIsOver() const
Returns true if the current episode is finished at the current time.
Definition: simulator.hh:481
void serialize(Restarter &restarter)
Write the time manager's state to a restart file.
Definition: simulator.hh:815
Load or save a state of a problem to/from the harddisk.
#define EWOMS_REGISTER_PARAM(TypeTag, ParamType, ParamName, Description)
Register a run-time parameter.
Definition: parametersystem.hh:68
const Ewoms::Timer & solveTimer() const
Returns a reference to the timer object which measures the time needed by the solver.
Definition: simulator.hh:304
GridManager & gridManager()
Return a reference to the grid manager of simulation.
Definition: simulator.hh:171
This file provides the infrastructure to retrieve run-time parameters.
const std::string & fileName() const
Returns the name of the file which is (de-)serialized.
Definition: restart.hh:90
void setEpisodeIndex(int episodeIdx)
Sets the index of the current episode.
Definition: simulator.hh:444
void serialize()
This method writes the complete state of the simulation to the harddisk.
Definition: simulator.hh:791
Provides an encapsulation to measure the system time.
Definition: timer.hh:48
const GridView & gridView() const
Return the grid view for which the simulation is done.
Definition: simulator.hh:183
double realTimeElapsed() const
Return the real time [s] elapsed during the periods the timer was active since the last reset...
Definition: timer.hh:121
const Ewoms::Timer & prePostProcessTimer() const
Returns a reference to the timer object which measures the time needed for pre- and postprocessing of...
Definition: simulator.hh:290
const Problem & problem() const
Return the object which specifies the pysical setup of the simulation.
Definition: simulator.hh:209
#define EWOMS_GET_PARAM(TypeTag, ParamType, ParamName)
Retrieve a runtime parameter.
Definition: parametersystem.hh:99
void setTimeStepSize(Scalar value)
Set the current time step size to a given value.
Definition: simulator.hh:331
void setTimeStepIndex(unsigned value)
Set the current time step index to a given value.
Definition: simulator.hh:339
const Ewoms::Timer & writeTimer() const
Returns a reference to the timer object which measures the time needed to write the visualization out...
Definition: simulator.hh:318
void setTime(Scalar t, unsigned stepIdx)
Set the current simulated time and the time step index.
Definition: simulator.hh:241
Provides an encapsulation to measure the system time.
Load or save a state of a problem to/from the harddisk.
Definition: restart.hh:43
Provides the magic behind the eWoms property system.
const Ewoms::Timer & updateTimer() const
Returns a reference to the timer object which measures the time needed to the solutions of the non-li...
Definition: simulator.hh:311
void setStartTime(Scalar t)
Set the time of the start of the simulation.
Definition: simulator.hh:217
int episodeIndex() const
Returns the index of the current episode.
Definition: simulator.hh:452
const Ewoms::Timer & linearizeTimer() const
Returns a reference to the timer object which measures the time needed for linarizing the solutions...
Definition: simulator.hh:297
A simple class which makes sure that a timer gets stopped if an exception is thrown.
Scalar timeStepSize() const
Returns the time step length so that we don't miss the beginning of the next episode or cross the en...
Definition: simulator.hh:347
void deserialize(Restarter &restarter)
Read the time manager's state from a restart file.
Definition: simulator.hh:836
static std::string humanReadableTime(Scalar timeInSeconds, bool isAmendment=true)
Given a time step size in seconds, return it in a format which is more easily parsable by humans...
Definition: simulator.hh:707
const Ewoms::Timer & executionTimer() const
Returns a reference to the timer object which measures the time needed to run the simulation...
Definition: simulator.hh:283
Manages the initializing and running of time dependent problems.
Definition: simulator.hh:75
#define NEW_PROP_TAG(PTagName)
Define a property tag.
Definition: propertysystem.hh:247
void deserializeBegin(Simulator &simulator, Scalar t)
Start reading a restart file at a certain simulated time.
Definition: restart.hh:167
const Ewoms::Timer & setupTimer() const
Returns a reference to the timer object which measures the time needed to set up and initialize the s...
Definition: simulator.hh:276
void startNextEpisode(Scalar episodeStartTime, Scalar episodeLength)
Change the current episode of the simulation.
Definition: simulator.hh:418
bool episodeWillBeOver() const
Returns true if the current episode will be finished after the current time step. ...
Definition: simulator.hh:492
void setFinished(bool yesno=true)
Specify whether the simulation is finished.
Definition: simulator.hh:370
const GridManager & gridManager() const
Return a reference to the grid manager of simulation.
Definition: simulator.hh:177
Scalar time() const
Return the number of seconds of simulated time which have elapsed since the start time...
Definition: simulator.hh:254
double stop()
Stop counting the time resources.
Definition: timer.hh:73
const Model & model() const
Return the physical model used in the simulation.
Definition: simulator.hh:195
Scalar endTime() const
Returns the number of (simulated) seconds which the simulation runs.
Definition: simulator.hh:269