28#ifndef EWOMS_SIMULATOR_HH
29#define EWOMS_SIMULATOR_HH
31#include <dune/common/parallel/mpihelper.hh>
42#include <opm/models/utils/simulatorutils.hpp>
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);
126 Parameters::Get<Parameters::PredeterminedTimeStepsFile>();
132 episodeStartTime_ = 0;
133 episodeLength_ = std::numeric_limits<Scalar>::max();
138 std::cout <<
"Allocating the simulation vanguard\n" << std::flush;
148 if (comm.size() > 1) {
149 what +=
" (on rank " + std::to_string(comm.rank()) +
")";
152 std::cerr <<
"Rank " << comm.rank() <<
" threw an exception: " <<
e.what() << std::endl;
156 [comm](
const std::string&
prefix,
158 const std::string&
what_)
168 { vanguard_.reset(
new Vanguard(*
this)); }
169 catch (
const std::exception&
e) {
176 std::cout <<
"Distributing the vanguard's data\n" << std::flush;
179 { vanguard_->loadBalance(); }
180 catch (
const std::exception&
e) {
187 std::cout <<
"Allocating the model\n" << std::flush;
189 model_.reset(
new Model(*
this));
191 catch (
const std::exception&
e) {
198 std::cout <<
"Allocating the problem\n" << std::flush;
201 problem_.reset(
new Problem(*
this));
203 catch (
const std::exception&
e) {
210 std::cout <<
"Initializing the model\n" << std::flush;
213 { model_->finishInit(); }
214 catch (
const std::exception&
e) {
221 std::cout <<
"Initializing the problem\n" << std::flush;
224 { problem_->finishInit(); }
225 catch (
const std::exception&
e) {
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_; }
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()*1
e3;
481 static const Scalar
eps = std::numeric_limits<Scalar>::epsilon()*1
e3;
483 return finished_ || (this->
time() + timeStepSize_)*(1.0 +
eps) >=
endTime();
522 episodeStartTime_ = startTime_ + time_;
523 episodeLength_ =
len;
540 {
return episodeIdx_; }
547 {
return episodeStartTime_; }
555 { episodeLength_ =
dt; }
562 {
return episodeLength_; }
570 static const Scalar
eps = std::numeric_limits<Scalar>::epsilon()*1
e3;
581 static const Scalar
eps = std::numeric_limits<Scalar>::epsilon()*1
e3;
592 static const Scalar
eps = std::numeric_limits<Scalar>::epsilon()*1
e3;
613 return std::max<Scalar>(0.0,
637 Scalar
restartTime = Parameters::Get<Parameters::RestartTime<Scalar>>();
643 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(
res.deserializeBegin(*
this, time_));
645 std::cout <<
"Deserialize from file '" <<
res.fileName() <<
"'\n" << std::flush;
646 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(this->
deserialize(res));
647 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->deserialize(
res));
648 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(model_->deserialize(
res));
649 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(
res.deserializeEnd());
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;
668 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(model_->applyInitialSolution());
671 if (problem_->shouldWriteOutput())
672 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->writeOutput());
679 executionTimer_.
start();
683 prePostProcessTimer_.
start();
687 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->beginEpisode());
692 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->endEpisode());
693 prePostProcessTimer_.
stop();
701 std::cout <<
"Begin time step " <<
timeStepIndex() + 1 <<
". "
708 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->beginTimeStep());
713 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->endTimeStep());
714 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->endEpisode());
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();
745 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->endTimeStep());
746 prePostProcessTimer_.
stop();
750 if (problem_->shouldWriteOutput())
751 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->writeOutput());
756 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->advanceTimeLevel());
759 std::cout <<
"Time step " <<
timeStepIndex() + 1 <<
" done. "
763 <<
"\n" << std::flush;
770 prePostProcessTimer_.
start();
774 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->endEpisode());
779 if (timeStepIdx_ <
static_cast<int>(forcedTimeSteps_.size()))
781 dt = forcedTimeSteps_[timeStepIdx_];
788 prePostProcessTimer_.
stop();
792 if (problem_->shouldWriteRestartFile())
793 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(
serialize());
796 executionTimer_.
stop();
798 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->finalize());
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");
843 << episodeIdx_ <<
" "
844 << episodeStartTime_ <<
" "
845 << episodeLength_ <<
" "
848 << timeStepIdx_ <<
" ";
859 template <
class Restarter>
862 restarter.deserializeSectionBegin(
"Simulator");
873 template<
class Serializer>
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
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
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
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
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
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
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.
Definition timer.cpp:46
double realTimeElapsed() const
Return the real time [s] elapsed during the periods the timer was active since the last reset.
Definition timer.cpp:90
double stop()
Stop counting the time resources.
Definition timer.cpp:52
Declare the properties used by the infrastructure code of the finite volume discretizations.
Simplifies handling of buffers to be used in conjunction with MPI.
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition blackoilboundaryratevector.hh:37
std::vector< std::string > gatherStrings(const std::string &local_string)
From each rank, gather its string (if not empty) into a vector.
Definition mpiutil.cpp:141
std::string humanReadableTime(double timeInSeconds, bool isAmendment)
Given a time step size in seconds, return it in a format which is more easily parsable by humans.
Definition simulatorutils.cpp:45
constexpr auto getPropValue()
get the value data member of a property
Definition propertysystem.hh:242
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
This file provides the infrastructure to retrieve run-time parameters.
The Opm property system, traits with inheritance.
Load or save a state of a problem to/from the harddisk.
Provides an encapsulation to measure the system time.
A simple class which makes sure that a timer gets stopped if an exception is thrown.