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) {
177 std::cout <<
"Adding LGRs, if any\n" << std::flush;
180 { vanguard_->addLgrs(); }
181 catch (
const std::exception&
e) {
188 std::cout <<
"Distributing the vanguard's data\n" << std::flush;
191 { vanguard_->loadBalance(); }
192 catch (
const std::exception&
e) {
200 std::cout <<
"Allocating the model\n" << std::flush;
202 model_.reset(
new Model(*
this));
204 catch (
const std::exception&
e) {
211 std::cout <<
"Allocating the problem\n" << std::flush;
214 problem_.reset(
new Problem(*
this));
216 catch (
const std::exception&
e) {
223 std::cout <<
"Initializing the model\n" << std::flush;
226 { model_->finishInit(); }
227 catch (
const std::exception&
e) {
234 std::cout <<
"Initializing the problem\n" << std::flush;
237 { problem_->finishInit(); }
238 catch (
const std::exception&
e) {
247 std::cout <<
"Simulator successfully set up\n" << std::flush;
255 Parameters::Register<Parameters::EndTime<Scalar>>
256 (
"The simulation time at which the simulation is finished [s]");
257 Parameters::Register<Parameters::InitialTimeStepSize<Scalar>>
258 (
"The size of the initial time step [s]");
259 Parameters::Register<Parameters::RestartTime<Scalar>>
260 (
"The simulation time at which a restart should be attempted [s]");
261 Parameters::Register<Parameters::PredeterminedTimeStepsFile>
262 (
"A file with a list of predetermined time step sizes (one "
263 "time step per line)");
265 Vanguard::registerParameters();
266 Model::registerParameters();
267 Problem::registerParameters();
274 {
return *vanguard_; }
280 {
return *vanguard_; }
286 {
return vanguard_->gridView(); }
305 {
return *problem_; }
312 {
return *problem_; }
326 {
return startTime_; }
379 {
return setupTimer_; }
386 {
return executionTimer_; }
388 {
return executionTimer_; }
395 {
return prePostProcessTimer_; }
402 {
return linearizeTimer_; }
409 {
return solveTimer_; }
416 {
return updateTimer_; }
423 {
return writeTimer_; }
437 timeStepSize_ = value;
446 { timeStepIdx_ = value; }
454 {
return timeStepSize_; }
461 {
return timeStepIdx_; }
471 { finished_ =
yesno; }
481 assert(timeStepSize_ >= 0.0);
484 *std::numeric_limits<Scalar>::epsilon()*1
e3;
494 static const Scalar
eps = std::numeric_limits<Scalar>::epsilon()*1
e3;
496 return finished_ || (this->
time() + timeStepSize_)*(1.0 +
eps) >=
endTime();
535 episodeStartTime_ = startTime_ + time_;
536 episodeLength_ =
len;
553 {
return episodeIdx_; }
560 {
return episodeStartTime_; }
568 { episodeLength_ =
dt; }
575 {
return episodeLength_; }
583 static const Scalar
eps = std::numeric_limits<Scalar>::epsilon()*1
e3;
594 static const Scalar
eps = std::numeric_limits<Scalar>::epsilon()*1
e3;
605 static const Scalar
eps = std::numeric_limits<Scalar>::epsilon()*1
e3;
626 return std::max<Scalar>(0.0,
650 Scalar
restartTime = Parameters::Get<Parameters::RestartTime<Scalar>>();
656 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(
res.deserializeBegin(*
this, time_));
658 std::cout <<
"Deserialize from file '" <<
res.fileName() <<
"'\n" << std::flush;
659 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(this->
deserialize(res));
660 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->deserialize(
res));
661 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(model_->deserialize(
res));
662 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(
res.deserializeEnd());
664 std::cout <<
"Deserialization done."
668 <<
"\n" << std::flush;
673 std::cout <<
"Applying the initial solution of the \"" << problem_->name()
674 <<
"\" problem\n" << std::flush;
681 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(model_->applyInitialSolution());
684 if (problem_->shouldWriteOutput())
685 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->writeOutput(
true));
692 executionTimer_.
start();
696 prePostProcessTimer_.
start();
700 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->beginEpisode());
705 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->endEpisode());
706 prePostProcessTimer_.
stop();
714 std::cout <<
"Begin time step " <<
timeStepIndex() + 1 <<
". "
721 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->beginTimeStep());
726 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->endTimeStep());
727 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->endEpisode());
728 prePostProcessTimer_.
stop();
732 prePostProcessTimer_.
stop();
736 problem_->timeIntegration();
741 const auto&
model = problem_->model();
742 prePostProcessTimer_ +=
model.prePostProcessTimer();
743 linearizeTimer_ +=
model.linearizeTimer();
744 solveTimer_ +=
model.solveTimer();
745 updateTimer_ +=
model.updateTimer();
750 const auto&
model = problem_->model();
751 prePostProcessTimer_ +=
model.prePostProcessTimer();
752 linearizeTimer_ +=
model.linearizeTimer();
753 solveTimer_ +=
model.solveTimer();
754 updateTimer_ +=
model.updateTimer();
757 prePostProcessTimer_.
start();
758 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->endTimeStep());
759 prePostProcessTimer_.
stop();
763 if (problem_->shouldWriteOutput())
764 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->writeOutput(
true));
769 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->advanceTimeLevel());
772 std::cout <<
"Time step " <<
timeStepIndex() + 1 <<
" done. "
776 <<
"\n" << std::flush;
783 prePostProcessTimer_.
start();
787 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->endEpisode());
792 if (timeStepIdx_ <
static_cast<int>(forcedTimeSteps_.size()))
794 dt = forcedTimeSteps_[timeStepIdx_];
801 prePostProcessTimer_.
stop();
805 if (problem_->shouldWriteRestartFile())
806 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(
serialize());
809 executionTimer_.
stop();
811 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->finalize());
834 std::cout <<
"Serialize to file '" <<
res.fileName() <<
"'"
836 <<
"\n" << std::flush;
839 problem_->serialize(
res);
840 model_->serialize(
res);
851 template <
class Restarter>
854 restarter.serializeSectionBegin(
"Simulator");
856 << episodeIdx_ <<
" "
857 << episodeStartTime_ <<
" "
858 << episodeLength_ <<
" "
861 << timeStepIdx_ <<
" ";
872 template <
class Restarter>
875 restarter.deserializeSectionBegin(
"Simulator");
886 template<
class Serializer>
901 std::unique_ptr<Vanguard> vanguard_;
902 std::unique_ptr<Model> model_;
903 std::unique_ptr<Problem> problem_;
906 Scalar episodeStartTime_;
907 Scalar episodeLength_;
910 Timer executionTimer_;
911 Timer prePostProcessTimer_;
912 Timer linearizeTimer_;
917 std::vector<Scalar> forcedTimeSteps_;
922 Scalar timeStepSize_;
929namespace Properties {
930template<
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:422
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:453
Scalar startTime() const
Return the time of the start of the simulation.
Definition simulator.hh:325
int timeStepIndex() const
Returns number of time steps which have been executed since the beginning of the simulation.
Definition simulator.hh:460
void serialize()
This method writes the complete state of the simulation to the harddisk.
Definition simulator.hh:828
Scalar episodeLength() const
Returns the length of the current episode in simulated time .
Definition simulator.hh:574
const Timer & prePostProcessTimer() const
Returns a reference to the timer object which measures the time needed for pre- and postprocessing of...
Definition simulator.hh:394
const Timer & solveTimer() const
Returns a reference to the timer object which measures the time needed by the solver.
Definition simulator.hh:408
void startNextEpisode(Scalar len=std::numeric_limits< Scalar >::max())
Start the next episode, but don't change the episode identifier.
Definition simulator.hh:532
const Vanguard & vanguard() const
Return a reference to the grid manager of simulation.
Definition simulator.hh:279
void serialize(Restarter &restarter)
Write the time manager's state to a restart file.
Definition simulator.hh:852
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:415
const Timer & executionTimer() const
Returns a reference to the timer object which measures the time needed to run the simulation.
Definition simulator.hh:385
void startNextEpisode(Scalar episodeStartTime, Scalar episodeLength)
Change the current episode of the simulation.
Definition simulator.hh:518
void setEndTime(Scalar t)
Set the time of simulated seconds at which the simulation runs.
Definition simulator.hh:364
const Timer & linearizeTimer() const
Returns a reference to the timer object which measures the time needed for linarizing the solutions.
Definition simulator.hh:401
void setStartTime(Scalar t)
Set the time of the start of the simulation.
Definition simulator.hh:319
void deserialize(Restarter &restarter)
Read the time manager's state from a restart file.
Definition simulator.hh:873
void setTimeStepSize(Scalar value)
Set the current time step size to a given value.
Definition simulator.hh:435
bool episodeStarts() const
Returns true if the current episode has just been started at the current time.
Definition simulator.hh:581
void run()
Runs the simulation using a given problem class.
Definition simulator.hh:641
Vanguard & vanguard()
Return a reference to the grid manager of simulation.
Definition simulator.hh:273
int episodeIndex() const
Returns the index of the current episode.
Definition simulator.hh:552
void setTimeStepIndex(unsigned value)
Set the current time step index to a given value.
Definition simulator.hh:445
void setFinished(bool yesno=true)
Specify whether the simulation is finished.
Definition simulator.hh:470
Problem & problem()
Return the object which specifies the pysical setup of the simulation.
Definition simulator.hh:304
static void registerParameters()
Registers all runtime parameters used by the simulation.
Definition simulator.hh:253
void setTime(Scalar t)
Set the current simulated time, don't change the current time step index.
Definition simulator.hh:334
bool willBeFinished() const
Returns true if the simulation is finished after the time level is incremented by the current time st...
Definition simulator.hh:492
bool finished() const
Returns true if the simulation is finished.
Definition simulator.hh:479
Scalar maxTimeStepSize() const
Aligns the time step size to the episode boundary and to the end time of the simulation.
Definition simulator.hh:503
const Model & model() const
Return the physical model used in the simulation.
Definition simulator.hh:297
void setTime(Scalar t, unsigned stepIdx)
Set the current simulated time and the time step index.
Definition simulator.hh:343
bool episodeIsOver() const
Returns true if the current episode is finished at the current time.
Definition simulator.hh:592
Scalar endTime() const
Returns the number of (simulated) seconds which the simulation runs.
Definition simulator.hh:371
bool episodeWillBeOver() const
Returns true if the current episode will be finished after the current time step.
Definition simulator.hh:603
const GridView & gridView() const
Return the grid view for which the simulation is done.
Definition simulator.hh:285
void setEpisodeIndex(int episodeIdx)
Sets the index of the current episode.
Definition simulator.hh:544
Scalar time() const
Return the number of seconds of simulated time which have elapsed since the start time.
Definition simulator.hh:356
void setEpisodeLength(Scalar dt)
Sets the length in seconds of the current episode.
Definition simulator.hh:567
const Problem & problem() const
Return the object which specifies the pysical setup of the simulation.
Definition simulator.hh:311
Model & model()
Return the physical model used in the simulation.
Definition simulator.hh:291
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:615
Scalar episodeStartTime() const
Returns the absolute time when the current episode started .
Definition simulator.hh:559
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:378
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.