28#ifndef EWOMS_SIMULATOR_HH
29#define EWOMS_SIMULATOR_HH
31#include <dune/common/parallel/mpihelper.hh>
51#define EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(code) \
53 const auto& comm = Dune::MPIHelper::getCommunication(); \
54 bool exceptionThrown = false; \
56 catch (const Dune::Exception& e) { \
57 exceptionThrown = true; \
58 std::cerr << "Process " << comm.rank() << " threw a fatal exception: " \
59 << e.what() << ". Abort!" << std::endl; \
61 catch (const std::exception& e) { \
62 exceptionThrown = true; \
63 std::cerr << "Process " << comm.rank() << " threw a fatal exception: " \
64 << e.what() << ". Abort!" << std::endl; \
67 exceptionThrown = true; \
68 std::cerr << "Process " << comm.rank() << " threw a fatal exception. " \
69 <<" Abort!" << std::endl; \
72 if (comm.max(exceptionThrown)) \
90template <
class TypeTag>
99 using MPIComm =
typename Dune::MPIHelper::MPICommunicator;
100 using Communication = Dune::Communication<MPIComm>;
117 verbose_ = verbose && comm.rank() == 0;
122 endTime_ = Parameters::Get<Parameters::EndTime<Scalar>>();
123 timeStepSize_ = Parameters::Get<Parameters::InitialTimeStepSize<Scalar>>();
124 assert(timeStepSize_ > 0);
125 const std::string& predetTimeStepFile =
126 Parameters::Get<Parameters::PredeterminedTimeStepsFile>();
127 if (!predetTimeStepFile.empty()) {
128 forcedTimeSteps_ = readTimeStepFile<Scalar>(predetTimeStepFile);
132 episodeStartTime_ = 0;
133 episodeLength_ = std::numeric_limits<Scalar>::max();
138 std::cout <<
"Allocating the simulation vanguard\n" << std::flush;
140 int exceptionThrown = 0;
144 [&exceptionThrown, &what, comm](
const std::exception& e,
148 if (comm.size() > 1) {
152 std::cerr <<
"Rank " << comm.rank() <<
" threw an exception: " << e.what() << std::endl;
155 auto checkParallelException =
156 [comm](
const std::string& prefix,
157 int exceptionThrown_,
158 const std::string& what_)
160 if (comm.max(exceptionThrown_)) {
162 assert(!all_what.empty());
163 throw std::runtime_error(prefix + all_what.front());
168 { vanguard_.reset(
new Vanguard(*
this)); }
169 catch (
const std::exception& e) {
170 catchAction(e, verbose_);
172 checkParallelException(
"Allocating the simulation vanguard failed: ",
173 exceptionThrown, what);
176 std::cout <<
"Distributing the vanguard's data\n" << std::flush;
179 { vanguard_->loadBalance(); }
180 catch (
const std::exception& e) {
181 catchAction(e, verbose_);
183 checkParallelException(
"Could not distribute the vanguard data: ",
184 exceptionThrown, what);
187 std::cout <<
"Allocating the model\n" << std::flush;
189 model_.reset(
new Model(*
this));
191 catch (
const std::exception& e) {
192 catchAction(e, verbose_);
194 checkParallelException(
"Could not allocate model: ",
195 exceptionThrown, what);
198 std::cout <<
"Allocating the problem\n" << std::flush;
201 problem_.reset(
new Problem(*
this));
203 catch (
const std::exception& e) {
204 catchAction(e, verbose_);
206 checkParallelException(
"Could not allocate the problem: ",
207 exceptionThrown, what);
210 std::cout <<
"Initializing the model\n" << std::flush;
213 { model_->finishInit(); }
214 catch (
const std::exception& e) {
215 catchAction(e, verbose_);
217 checkParallelException(
"Could not initialize the model: ",
218 exceptionThrown, what);
221 std::cout <<
"Initializing the problem\n" << std::flush;
224 { problem_->finishInit(); }
225 catch (
const std::exception& e) {
226 catchAction(e, verbose_);
228 checkParallelException(
"Could not initialize the problem: ",
229 exceptionThrown, what);
234 std::cout <<
"Simulator successfully set up\n" << std::flush;
242 Parameters::Register<Parameters::EndTime<Scalar>>
243 (
"The simulation time at which the simulation is finished [s]");
244 Parameters::Register<Parameters::InitialTimeStepSize<Scalar>>
245 (
"The size of the initial time step [s]");
246 Parameters::Register<Parameters::RestartTime<Scalar>>
247 (
"The simulation time at which a restart should be attempted [s]");
248 Parameters::Register<Parameters::PredeterminedTimeStepsFile>
249 (
"A file with a list of predetermined time step sizes (one "
250 "time step per line)");
252 Vanguard::registerParameters();
253 Model::registerParameters();
254 Problem::registerParameters();
261 {
return *vanguard_; }
267 {
return *vanguard_; }
273 {
return vanguard_->gridView(); }
292 {
return *problem_; }
299 {
return *problem_; }
313 {
return startTime_; }
333 timeStepIdx_ = stepIdx;
366 {
return setupTimer_; }
373 {
return executionTimer_; }
375 {
return executionTimer_; }
382 {
return prePostProcessTimer_; }
389 {
return linearizeTimer_; }
396 {
return solveTimer_; }
403 {
return updateTimer_; }
410 {
return writeTimer_; }
424 timeStepSize_ = value;
433 { timeStepIdx_ = value; }
441 {
return timeStepSize_; }
448 {
return timeStepIdx_; }
458 { finished_ = yesno; }
468 assert(timeStepSize_ >= 0.0);
471 *std::numeric_limits<Scalar>::epsilon()*1e3;
472 return finished_ || (this->
time()*(1.0 + eps) >=
endTime());
481 static const Scalar eps = std::numeric_limits<Scalar>::epsilon()*1e3;
483 return finished_ || (this->
time() + timeStepSize_)*(1.0 + eps) >=
endTime();
522 episodeStartTime_ = startTime_ + time_;
523 episodeLength_ = len;
532 { episodeIdx_ = episodeIdx; }
540 {
return episodeIdx_; }
547 {
return episodeStartTime_; }
555 { episodeLength_ = dt; }
562 {
return episodeLength_; }
570 static const Scalar eps = std::numeric_limits<Scalar>::epsilon()*1e3;
572 return this->
time() <= (episodeStartTime_ -
startTime())*(1 + eps);
581 static const Scalar eps = std::numeric_limits<Scalar>::epsilon()*1e3;
592 static const Scalar eps = std::numeric_limits<Scalar>::epsilon()*1e3;
613 return std::max<Scalar>(0.0,
632 TimerGuard executionTimerGuard(executionTimer_);
633 TimerGuard prePostProcessTimerGuard(prePostProcessTimer_);
637 Scalar restartTime = Parameters::Get<Parameters::RestartTime<Scalar>>();
638 if (restartTime > -1e30) {
645 std::cout <<
"Deserialize from file '" << res.
fileName() <<
"'\n" << std::flush;
651 std::cout <<
"Deserialization done."
655 <<
"\n" << std::flush;
660 std::cout <<
"Applying the initial solution of the \"" << problem_->name()
661 <<
"\" problem\n" << std::flush;
663 Scalar oldTimeStepSize = timeStepSize_;
664 int oldTimeStepIdx = timeStepIdx_;
671 if (problem_->shouldWriteOutput())
674 timeStepSize_ = oldTimeStepSize;
675 timeStepIdx_ = oldTimeStepIdx;
679 executionTimer_.
start();
683 prePostProcessTimer_.
start();
693 prePostProcessTimer_.
stop();
698 episodeBegins =
false;
701 std::cout <<
"Begin time step " <<
timeStepIndex() + 1 <<
". "
715 prePostProcessTimer_.
stop();
719 prePostProcessTimer_.
stop();
723 problem_->timeIntegration();
728 const auto&
model = problem_->model();
729 prePostProcessTimer_ +=
model.prePostProcessTimer();
730 linearizeTimer_ +=
model.linearizeTimer();
731 solveTimer_ +=
model.solveTimer();
732 updateTimer_ +=
model.updateTimer();
737 const auto&
model = problem_->model();
738 prePostProcessTimer_ +=
model.prePostProcessTimer();
739 linearizeTimer_ +=
model.linearizeTimer();
740 solveTimer_ +=
model.solveTimer();
741 updateTimer_ +=
model.updateTimer();
744 prePostProcessTimer_.
start();
746 prePostProcessTimer_.
stop();
750 if (problem_->shouldWriteOutput())
759 std::cout <<
"Time step " <<
timeStepIndex() + 1 <<
" done. "
763 <<
"\n" << std::flush;
770 prePostProcessTimer_.
start();
775 episodeBegins =
true;
779 if (timeStepIdx_ <
static_cast<int>(forcedTimeSteps_.size()))
781 dt = forcedTimeSteps_[timeStepIdx_];
788 prePostProcessTimer_.
stop();
792 if (problem_->shouldWriteRestartFile())
796 executionTimer_.
stop();
821 std::cout <<
"Serialize to file '" << res.fileName() <<
"'"
823 <<
"\n" << std::flush;
826 problem_->serialize(res);
827 model_->serialize(res);
838 template <
class Restarter>
841 restarter.serializeSectionBegin(
"Simulator");
842 restarter.serializeStream()
843 << episodeIdx_ <<
" "
844 << episodeStartTime_ <<
" "
845 << episodeLength_ <<
" "
848 << timeStepIdx_ <<
" ";
849 restarter.serializeSectionEnd();
859 template <
class Restarter>
862 restarter.deserializeSectionBegin(
"Simulator");
863 restarter.deserializeStream()
870 restarter.deserializeSectionEnd();
873 template<
class Serializer>
876 serializer(*vanguard_);
878 serializer(*problem_);
879 serializer(episodeIdx_);
880 serializer(episodeStartTime_);
881 serializer(episodeLength_);
882 serializer(startTime_);
884 serializer(timeStepIdx_);
888 std::unique_ptr<Vanguard> vanguard_;
889 std::unique_ptr<Model> model_;
890 std::unique_ptr<Problem> problem_;
893 Scalar episodeStartTime_;
894 Scalar episodeLength_;
897 Timer executionTimer_;
898 Timer prePostProcessTimer_;
899 Timer linearizeTimer_;
904 std::vector<Scalar> forcedTimeSteps_;
909 Scalar timeStepSize_;
916namespace Properties {
917template<
class TypeTag>
Defines a type tags and some fundamental properties all models.
Load or save a state of a problem to/from the harddisk.
Definition: restart.hpp:41
void serializeBegin(Simulator &simulator)
Write the current state of the model to disk.
Definition: restart.hpp:88
const std::string & fileName() const
Returns the name of the file which is (de-)serialized.
Definition: restart.hpp:81
void deserializeBegin(Simulator &simulator, Scalar t)
Start reading a restart file at a certain simulated time.
Definition: restart.hpp:151
void deserializeEnd()
Stop reading the restart file.
Manages the initializing and running of time dependent problems.
Definition: simulator.hh:92
const Timer & writeTimer() const
Returns a reference to the timer object which measures the time needed to write the visualization out...
Definition: simulator.hh:409
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:440
Scalar startTime() const
Return the time of the start of the simulation.
Definition: simulator.hh:312
int timeStepIndex() const
Returns number of time steps which have been executed since the beginning of the simulation.
Definition: simulator.hh:447
void serialize()
This method writes the complete state of the simulation to the harddisk.
Definition: simulator.hh:815
Timer & executionTimer()
Definition: simulator.hh:374
Scalar episodeLength() const
Returns the length of the current episode in simulated time .
Definition: simulator.hh:561
const Timer & prePostProcessTimer() const
Returns a reference to the timer object which measures the time needed for pre- and postprocessing of...
Definition: simulator.hh:381
const Timer & solveTimer() const
Returns a reference to the timer object which measures the time needed by the solver.
Definition: simulator.hh:395
void startNextEpisode(Scalar len=std::numeric_limits< Scalar >::max())
Start the next episode, but don't change the episode identifier.
Definition: simulator.hh:519
void serializeOp(Serializer &serializer)
Definition: simulator.hh:874
const Vanguard & vanguard() const
Return a reference to the grid manager of simulation.
Definition: simulator.hh:266
void serialize(Restarter &restarter)
Write the time manager's state to a restart file.
Definition: simulator.hh:839
const 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:402
const Timer & executionTimer() const
Returns a reference to the timer object which measures the time needed to run the simulation.
Definition: simulator.hh:372
void startNextEpisode(Scalar episodeStartTime, Scalar episodeLength)
Change the current episode of the simulation.
Definition: simulator.hh:505
void setEndTime(Scalar t)
Set the time of simulated seconds at which the simulation runs.
Definition: simulator.hh:351
const Timer & linearizeTimer() const
Returns a reference to the timer object which measures the time needed for linarizing the solutions.
Definition: simulator.hh:388
void setStartTime(Scalar t)
Set the time of the start of the simulation.
Definition: simulator.hh:306
void deserialize(Restarter &restarter)
Read the time manager's state from a restart file.
Definition: simulator.hh:860
Simulator(Communication comm, bool verbose=true)
Definition: simulator.hh:111
void setTimeStepSize(Scalar value)
Set the current time step size to a given value.
Definition: simulator.hh:422
bool episodeStarts() const
Returns true if the current episode has just been started at the current time.
Definition: simulator.hh:568
void run()
Runs the simulation using a given problem class.
Definition: simulator.hh:628
Vanguard & vanguard()
Return a reference to the grid manager of simulation.
Definition: simulator.hh:260
int episodeIndex() const
Returns the index of the current episode.
Definition: simulator.hh:539
void setTimeStepIndex(unsigned value)
Set the current time step index to a given value.
Definition: simulator.hh:432
void setFinished(bool yesno=true)
Specify whether the simulation is finished.
Definition: simulator.hh:457
Problem & problem()
Return the object which specifies the pysical setup of the simulation.
Definition: simulator.hh:291
static void registerParameters()
Registers all runtime parameters used by the simulation.
Definition: simulator.hh:240
void setTime(Scalar t)
Set the current simulated time, don't change the current time step index.
Definition: simulator.hh:321
bool willBeFinished() const
Returns true if the simulation is finished after the time level is incremented by the current time st...
Definition: simulator.hh:479
bool finished() const
Returns true if the simulation is finished.
Definition: simulator.hh:466
Scalar maxTimeStepSize() const
Aligns the time step size to the episode boundary and to the end time of the simulation.
Definition: simulator.hh:490
const Model & model() const
Return the physical model used in the simulation.
Definition: simulator.hh:284
Simulator(bool verbose=true)
Definition: simulator.hh:106
void setTime(Scalar t, unsigned stepIdx)
Set the current simulated time and the time step index.
Definition: simulator.hh:330
bool episodeIsOver() const
Returns true if the current episode is finished at the current time.
Definition: simulator.hh:579
Scalar endTime() const
Returns the number of (simulated) seconds which the simulation runs.
Definition: simulator.hh:358
Simulator(const Simulator &)=delete
bool episodeWillBeOver() const
Returns true if the current episode will be finished after the current time step.
Definition: simulator.hh:590
const GridView & gridView() const
Return the grid view for which the simulation is done.
Definition: simulator.hh:272
void setEpisodeIndex(int episodeIdx)
Sets the index of the current episode.
Definition: simulator.hh:531
Scalar time() const
Return the number of seconds of simulated time which have elapsed since the start time.
Definition: simulator.hh:343
void setEpisodeLength(Scalar dt)
Sets the length in seconds of the current episode.
Definition: simulator.hh:554
const Problem & problem() const
Return the object which specifies the pysical setup of the simulation.
Definition: simulator.hh:298
Model & model()
Return the physical model used in the simulation.
Definition: simulator.hh:278
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:602
Scalar episodeStartTime() const
Returns the absolute time when the current episode started .
Definition: simulator.hh:546
const 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:365
A simple class which makes sure that a timer gets stopped if an exception is thrown.
Definition: timerguard.hh:41
Provides an encapsulation to measure the system time.
Definition: timer.hpp:46
void start()
Start counting the time resources used by the simulation.
double realTimeElapsed() const
Return the real time [s] elapsed during the periods the timer was active since the last reset.
double stop()
Stop counting the time resources.
Declare the properties used by the infrastructure code of the finite volume discretizations.
Definition: blackoilboundaryratevector.hh:37
std::vector< std::string > gatherStrings(const std::string &local_string)
std::string humanReadableTime(double timeInSeconds, bool isAmendment=true)
Given a time step size in seconds, return it in a format which is more easily parsable by humans.
typename Properties::Detail::GetPropImpl< TypeTag, Property >::type::type GetPropType
get the type alias defined in the property (equivalent to old macro GET_PROP_TYPE(....
Definition: propertysystem.hh:235
std::string to_string(const ConvergenceReport::ReservoirFailure::Type t)
This file provides the infrastructure to retrieve run-time parameters.
The Opm property system, traits with inheritance.
#define EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(code)
Definition: simulator.hh:51
Manages the simulation time.
Definition: basicproperties.hh:116