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)) \
79 static constexpr T constexpr_max(T
a, T
b) {
80 return (
a >
b) ?
a :
b;
95template <
class TypeTag>
104 using MPIComm =
typename Dune::MPIHelper::MPICommunicator;
105 using Communication = Dune::Communication<MPIComm>;
108 static constexpr Scalar eps = constexpr_max(std::numeric_limits<Scalar>::epsilon(),
static_cast<Scalar
>(1.0e-9));
125 verbose_ =
verbose && comm.rank() == 0;
130 endTime_ = Parameters::Get<Parameters::EndTime<Scalar>>();
131 timeStepSize_ = Parameters::Get<Parameters::InitialTimeStepSize<Scalar>>();
132 assert(timeStepSize_ > 0);
134 Parameters::Get<Parameters::PredeterminedTimeStepsFile>();
140 episodeStartTime_ = 0;
141 episodeLength_ = std::numeric_limits<Scalar>::max();
146 std::cout <<
"Allocating the simulation vanguard\n" << std::flush;
156 if (comm.size() > 1) {
157 what +=
" (on rank " + std::to_string(comm.rank()) +
")";
160 std::cerr <<
"Rank " << comm.rank() <<
" threw an exception: " <<
e.what() << std::endl;
164 [comm](
const std::string&
prefix,
166 const std::string&
what_)
176 { vanguard_.reset(
new Vanguard(*
this)); }
177 catch (
const std::exception&
e) {
185 std::cout <<
"Adding LGRs, if any\n" << std::flush;
188 { vanguard_->addLgrs(); }
189 catch (
const std::exception&
e) {
196 std::cout <<
"Distributing the vanguard's data\n" << std::flush;
199 { vanguard_->loadBalance(); }
200 catch (
const std::exception&
e) {
208 std::cout <<
"Allocating the model\n" << std::flush;
210 model_.reset(
new Model(*
this));
212 catch (
const std::exception&
e) {
219 std::cout <<
"Allocating the problem\n" << std::flush;
222 problem_.reset(
new Problem(*
this));
224 catch (
const std::exception&
e) {
231 std::cout <<
"Initializing the model\n" << std::flush;
234 { model_->finishInit(); }
235 catch (
const std::exception&
e) {
242 std::cout <<
"Initializing the problem\n" << std::flush;
245 { problem_->finishInit(); }
246 catch (
const std::exception&
e) {
255 std::cout <<
"Simulator successfully set up\n" << std::flush;
263 Parameters::Register<Parameters::EndTime<Scalar>>
264 (
"The simulation time at which the simulation is finished [s]");
265 Parameters::Register<Parameters::InitialTimeStepSize<Scalar>>
266 (
"The size of the initial time step [s]");
267 Parameters::Register<Parameters::RestartTime<Scalar>>
268 (
"The simulation time at which a restart should be attempted [s]");
269 Parameters::Register<Parameters::PredeterminedTimeStepsFile>
270 (
"A file with a list of predetermined time step sizes (one "
271 "time step per line)");
273 Vanguard::registerParameters();
274 Model::registerParameters();
275 Problem::registerParameters();
282 {
return *vanguard_; }
288 {
return *vanguard_; }
294 {
return vanguard_->gridView(); }
313 {
return *problem_; }
320 {
return *problem_; }
334 {
return startTime_; }
387 {
return setupTimer_; }
394 {
return executionTimer_; }
396 {
return executionTimer_; }
403 {
return prePostProcessTimer_; }
410 {
return linearizeTimer_; }
417 {
return solveTimer_; }
424 {
return updateTimer_; }
431 {
return writeTimer_; }
445 timeStepSize_ = value;
454 { timeStepIdx_ = value; }
462 {
return timeStepSize_; }
469 {
return timeStepIdx_; }
479 { finished_ =
yesno; }
489 assert(timeStepSize_ >= 0.0);
490 return finished_ || (this->
time()*(1.0 + eps) >=
endTime());
499 return finished_ || (this->
time() + timeStepSize_)*(1.0 + eps) >=
endTime();
538 episodeStartTime_ = startTime_ + time_;
539 episodeLength_ =
len;
556 {
return episodeIdx_; }
563 {
return episodeStartTime_; }
571 { episodeLength_ =
dt; }
578 {
return episodeLength_; }
586 return this->
time() <= (episodeStartTime_ -
startTime())*(1 + eps);
623 return std::max<Scalar>(0.0,
647 Scalar
restartTime = Parameters::Get<Parameters::RestartTime<Scalar>>();
653 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(
res.deserializeBegin(*
this, time_));
655 std::cout <<
"Deserialize from file '" <<
res.fileName() <<
"'\n" << std::flush;
656 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(this->
deserialize(res));
657 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->deserialize(
res));
658 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(model_->deserialize(
res));
659 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(
res.deserializeEnd());
661 std::cout <<
"Deserialization done."
665 <<
"\n" << std::flush;
670 std::cout <<
"Applying the initial solution of the \"" << problem_->name()
671 <<
"\" problem\n" << std::flush;
678 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(model_->applyInitialSolution());
681 if (problem_->shouldWriteOutput())
682 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->writeOutput(
true));
689 executionTimer_.
start();
693 prePostProcessTimer_.
start();
697 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->beginEpisode());
702 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->endEpisode());
703 prePostProcessTimer_.
stop();
711 std::cout <<
"Begin time step " <<
timeStepIndex() + 1 <<
". "
718 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->beginTimeStep());
723 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->endTimeStep());
724 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->endEpisode());
725 prePostProcessTimer_.
stop();
729 prePostProcessTimer_.
stop();
733 problem_->timeIntegration();
738 const auto&
model = problem_->model();
739 prePostProcessTimer_ +=
model.prePostProcessTimer();
740 linearizeTimer_ +=
model.linearizeTimer();
741 solveTimer_ +=
model.solveTimer();
742 updateTimer_ +=
model.updateTimer();
747 const auto&
model = problem_->model();
748 prePostProcessTimer_ +=
model.prePostProcessTimer();
749 linearizeTimer_ +=
model.linearizeTimer();
750 solveTimer_ +=
model.solveTimer();
751 updateTimer_ +=
model.updateTimer();
754 prePostProcessTimer_.
start();
755 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->endTimeStep());
756 prePostProcessTimer_.
stop();
760 if (problem_->shouldWriteOutput())
761 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->writeOutput(
true));
766 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->advanceTimeLevel());
769 std::cout <<
"Time step " <<
timeStepIndex() + 1 <<
" done. "
773 <<
"\n" << std::flush;
780 prePostProcessTimer_.
start();
784 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->endEpisode());
789 if (timeStepIdx_ <
static_cast<int>(forcedTimeSteps_.size()))
791 dt = forcedTimeSteps_[timeStepIdx_];
798 prePostProcessTimer_.
stop();
802 if (problem_->shouldWriteRestartFile())
803 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(
serialize());
806 executionTimer_.
stop();
808 EWOMS_CATCH_PARALLEL_EXCEPTIONS_FATAL(problem_->finalize());
831 std::cout <<
"Serialize to file '" <<
res.fileName() <<
"'"
833 <<
"\n" << std::flush;
836 problem_->serialize(
res);
837 model_->serialize(
res);
848 template <
class Restarter>
851 restarter.serializeSectionBegin(
"Simulator");
853 << episodeIdx_ <<
" "
854 << episodeStartTime_ <<
" "
855 << episodeLength_ <<
" "
858 << timeStepIdx_ <<
" ";
869 template <
class Restarter>
872 restarter.deserializeSectionBegin(
"Simulator");
883 template<
class Serializer>
898 std::unique_ptr<Vanguard> vanguard_;
899 std::unique_ptr<Model> model_;
900 std::unique_ptr<Problem> problem_;
903 Scalar episodeStartTime_;
904 Scalar episodeLength_;
907 Timer executionTimer_;
908 Timer prePostProcessTimer_;
909 Timer linearizeTimer_;
914 std::vector<Scalar> forcedTimeSteps_;
919 Scalar timeStepSize_;
926namespace Properties {
927template<
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:97
const Timer & writeTimer() const
Returns a reference to the timer object which measures the time needed to write the visualization out...
Definition simulator.hh:430
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:461
Scalar startTime() const
Return the time of the start of the simulation.
Definition simulator.hh:333
int timeStepIndex() const
Returns number of time steps which have been executed since the beginning of the simulation.
Definition simulator.hh:468
void serialize()
This method writes the complete state of the simulation to the harddisk.
Definition simulator.hh:825
Scalar episodeLength() const
Returns the length of the current episode in simulated time .
Definition simulator.hh:577
const Timer & prePostProcessTimer() const
Returns a reference to the timer object which measures the time needed for pre- and postprocessing of...
Definition simulator.hh:402
const Timer & solveTimer() const
Returns a reference to the timer object which measures the time needed by the solver.
Definition simulator.hh:416
void startNextEpisode(Scalar len=std::numeric_limits< Scalar >::max())
Start the next episode, but don't change the episode identifier.
Definition simulator.hh:535
const Vanguard & vanguard() const
Return a reference to the grid manager of simulation.
Definition simulator.hh:287
void serialize(Restarter &restarter)
Write the time manager's state to a restart file.
Definition simulator.hh:849
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:423
const Timer & executionTimer() const
Returns a reference to the timer object which measures the time needed to run the simulation.
Definition simulator.hh:393
void startNextEpisode(Scalar episodeStartTime, Scalar episodeLength)
Change the current episode of the simulation.
Definition simulator.hh:521
void setEndTime(Scalar t)
Set the time of simulated seconds at which the simulation runs.
Definition simulator.hh:372
const Timer & linearizeTimer() const
Returns a reference to the timer object which measures the time needed for linarizing the solutions.
Definition simulator.hh:409
void setStartTime(Scalar t)
Set the time of the start of the simulation.
Definition simulator.hh:327
void deserialize(Restarter &restarter)
Read the time manager's state from a restart file.
Definition simulator.hh:870
void setTimeStepSize(Scalar value)
Set the current time step size to a given value.
Definition simulator.hh:443
bool episodeStarts() const
Returns true if the current episode has just been started at the current time.
Definition simulator.hh:584
void run()
Runs the simulation using a given problem class.
Definition simulator.hh:638
Vanguard & vanguard()
Return a reference to the grid manager of simulation.
Definition simulator.hh:281
int episodeIndex() const
Returns the index of the current episode.
Definition simulator.hh:555
void setTimeStepIndex(unsigned value)
Set the current time step index to a given value.
Definition simulator.hh:453
void setFinished(bool yesno=true)
Specify whether the simulation is finished.
Definition simulator.hh:478
Problem & problem()
Return the object which specifies the pysical setup of the simulation.
Definition simulator.hh:312
static void registerParameters()
Registers all runtime parameters used by the simulation.
Definition simulator.hh:261
void setTime(Scalar t)
Set the current simulated time, don't change the current time step index.
Definition simulator.hh:342
bool willBeFinished() const
Returns true if the simulation is finished after the time level is incremented by the current time st...
Definition simulator.hh:497
bool finished() const
Returns true if the simulation is finished.
Definition simulator.hh:487
Scalar maxTimeStepSize() const
Aligns the time step size to the episode boundary and to the end time of the simulation.
Definition simulator.hh:506
const Model & model() const
Return the physical model used in the simulation.
Definition simulator.hh:305
void setTime(Scalar t, unsigned stepIdx)
Set the current simulated time and the time step index.
Definition simulator.hh:351
bool episodeIsOver() const
Returns true if the current episode is finished at the current time.
Definition simulator.hh:593
Scalar endTime() const
Returns the number of (simulated) seconds which the simulation runs.
Definition simulator.hh:379
bool episodeWillBeOver() const
Returns true if the current episode will be finished after the current time step.
Definition simulator.hh:602
const GridView & gridView() const
Return the grid view for which the simulation is done.
Definition simulator.hh:293
void setEpisodeIndex(int episodeIdx)
Sets the index of the current episode.
Definition simulator.hh:547
Scalar time() const
Return the number of seconds of simulated time which have elapsed since the start time.
Definition simulator.hh:364
void setEpisodeLength(Scalar dt)
Sets the length in seconds of the current episode.
Definition simulator.hh:570
const Problem & problem() const
Return the object which specifies the pysical setup of the simulation.
Definition simulator.hh:319
Model & model()
Return the physical model used in the simulation.
Definition simulator.hh:299
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:612
Scalar episodeStartTime() const
Returns the absolute time when the current episode started .
Definition simulator.hh:562
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:386
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.