My Project
SimulatorFullyImplicitBlackoilEbos.hpp
1 /*
2  Copyright 2013, 2015, 2020 SINTEF Digital, Mathematics and Cybernetics.
3  Copyright 2015 Andreas Lauser
4  Copyright 2017 IRIS
5 
6  This file is part of the Open Porous Media project (OPM).
7 
8  OPM is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  OPM is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with OPM. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #ifndef OPM_SIMULATORFULLYIMPLICITBLACKOILEBOS_HEADER_INCLUDED
23 #define OPM_SIMULATORFULLYIMPLICITBLACKOILEBOS_HEADER_INCLUDED
24 
25 #include <opm/simulators/flow/NonlinearSolverEbos.hpp>
26 #include <opm/simulators/flow/BlackoilModelEbos.hpp>
27 #include <opm/simulators/flow/BlackoilModelParametersEbos.hpp>
28 #include <opm/simulators/wells/WellState.hpp>
29 #include <opm/simulators/aquifers/BlackoilAquiferModel.hpp>
30 #include <opm/simulators/utils/moduleVersion.hpp>
31 #include <opm/simulators/timestepping/AdaptiveTimeSteppingEbos.hpp>
32 #include <opm/grid/utility/StopWatch.hpp>
33 
34 #include <opm/common/ErrorMacros.hpp>
35 
36 namespace Opm::Properties {
37 
38 template<class TypeTag, class MyTypeTag>
40  using type = UndefinedProperty;
41 };
42 template<class TypeTag, class MyTypeTag>
43 struct EnableTuning {
44  using type = UndefinedProperty;
45 };
46 
47 template<class TypeTag>
48 struct EnableTerminalOutput<TypeTag, TTag::EclFlowProblem> {
49  static constexpr bool value = true;
50 };
51 template<class TypeTag>
52 struct EnableAdaptiveTimeStepping<TypeTag, TTag::EclFlowProblem> {
53  static constexpr bool value = true;
54 };
55 template<class TypeTag>
56 struct EnableTuning<TypeTag, TTag::EclFlowProblem> {
57  static constexpr bool value = false;
58 };
59 
60 } // namespace Opm::Properties
61 
62 namespace Opm {
63 
64 void outputReportStep(const SimulatorTimer& timer);
65 void outputTimestampFIP(const SimulatorTimer& timer,
66  const std::string& title,
67  const std::string& version);
68 
70 template<class TypeTag>
72 {
73 public:
74  using Simulator = GetPropType<TypeTag, Properties::Simulator>;
75  using Grid = GetPropType<TypeTag, Properties::Grid>;
76  using FluidSystem = GetPropType<TypeTag, Properties::FluidSystem>;
77  using ElementContext = GetPropType<TypeTag, Properties::ElementContext>;
78  using BlackoilIndices = GetPropType<TypeTag, Properties::Indices>;
79  using PrimaryVariables = GetPropType<TypeTag, Properties::PrimaryVariables>;
80  using MaterialLaw = GetPropType<TypeTag, Properties::MaterialLaw>;
81  using SolutionVector = GetPropType<TypeTag, Properties::SolutionVector>;
82  using MaterialLawParams = GetPropType<TypeTag, Properties::MaterialLawParams>;
83 
85  typedef BlackOilPolymerModule<TypeTag> PolymerModule;
86  typedef BlackOilMICPModule<TypeTag> MICPModule;
87 
90  typedef typename Model::ModelParameters ModelParameters;
94 
95 
117  SimulatorFullyImplicitBlackoilEbos(Simulator& ebosSimulator)
118  : ebosSimulator_(ebosSimulator)
119  {
120  phaseUsage_ = phaseUsageFromDeck(eclState());
121 
122  // Only rank 0 does print to std::cout
123  const auto& comm = grid().comm();
124  terminalOutput_ = EWOMS_GET_PARAM(TypeTag, bool, EnableTerminalOutput);
125  terminalOutput_ = terminalOutput_ && (comm.rank() == 0);
126  }
127 
128  static void registerParameters()
129  {
130  ModelParameters::registerParameters();
131  SolverParameters::registerParameters();
132  TimeStepper::registerParameters();
133 
134  EWOMS_REGISTER_PARAM(TypeTag, bool, EnableTerminalOutput,
135  "Print high-level information about the simulation's progress to the terminal");
136  EWOMS_REGISTER_PARAM(TypeTag, bool, EnableAdaptiveTimeStepping,
137  "Use adaptive time stepping between report steps");
138  EWOMS_REGISTER_PARAM(TypeTag, bool, EnableTuning,
139  "Honor some aspects of the TUNING keyword.");
140  }
141 
149  {
150  init(timer);
151  // Main simulation loop.
152  while (!timer.done()) {
153  bool continue_looping = runStep(timer);
154  if (!continue_looping) break;
155  }
156  return finalize();
157  }
158 
159  void init(SimulatorTimer &timer)
160  {
161  ebosSimulator_.setEpisodeIndex(-1);
162 
163  // Create timers and file for writing timing info.
164  solverTimer_ = std::make_unique<time::StopWatch>();
165  totalTimer_ = std::make_unique<time::StopWatch>();
166  totalTimer_->start();
167 
168  // adaptive time stepping
169  bool enableAdaptive = EWOMS_GET_PARAM(TypeTag, bool, EnableAdaptiveTimeStepping);
170  bool enableTUNING = EWOMS_GET_PARAM(TypeTag, bool, EnableTuning);
171  if (enableAdaptive) {
172  const UnitSystem& unitSystem = this->ebosSimulator_.vanguard().eclState().getUnits();
173  if (enableTUNING) {
174  const auto& sched_state = schedule()[timer.currentStepNum()];
175  auto max_next_tstep = sched_state.max_next_tstep();
176  adaptiveTimeStepping_ = std::make_unique<TimeStepper>(max_next_tstep,
177  sched_state.tuning(),
178  unitSystem, terminalOutput_);
179  }
180  else {
181  adaptiveTimeStepping_ = std::make_unique<TimeStepper>(unitSystem, terminalOutput_);
182  }
183 
184  if (isRestart()) {
185  // For restarts the ebosSimulator may have gotten some information
186  // about the next timestep size from the OPMEXTRA field
187  adaptiveTimeStepping_->setSuggestedNextStep(ebosSimulator_.timeStepSize());
188  }
189  }
190  }
191 
192  bool runStep(SimulatorTimer& timer)
193  {
194  if (schedule().exitStatus().has_value()) {
195  if (terminalOutput_) {
196  OpmLog::info("Stopping simulation since EXIT was triggered by an action keyword.");
197  }
198  report_.success.exit_status = schedule().exitStatus().value();
199  return false;
200  }
201 
202  // Report timestep.
203  if (terminalOutput_) {
204  std::ostringstream ss;
205  timer.report(ss);
206  OpmLog::debug(ss.str());
207  }
208 
209  if (terminalOutput_) {
210  outputReportStep(timer);
211  }
212 
213  // write the inital state at the report stage
214  if (timer.initialStep()) {
215  Dune::Timer perfTimer;
216  perfTimer.start();
217 
218  ebosSimulator_.setEpisodeIndex(-1);
219  ebosSimulator_.setEpisodeLength(0.0);
220  ebosSimulator_.setTimeStepSize(0.0);
221 
222  wellModel_().beginReportStep(timer.currentStepNum());
223  ebosSimulator_.problem().writeOutput();
224 
225  report_.success.output_write_time += perfTimer.stop();
226  }
227 
228  // Run a multiple steps of the solver depending on the time step control.
229  solverTimer_->start();
230 
231  auto solver = createSolver(wellModel_());
232 
233  ebosSimulator_.startNextEpisode(
234  ebosSimulator_.startTime()
235  + schedule().seconds(timer.currentStepNum()),
236  timer.currentStepLength());
237  ebosSimulator_.setEpisodeIndex(timer.currentStepNum());
238  solver->model().beginReportStep();
239  bool enableTUNING = EWOMS_GET_PARAM(TypeTag, bool, EnableTuning);
240 
241  // If sub stepping is enabled allow the solver to sub cycle
242  // in case the report steps are too large for the solver to converge
243  //
244  // \Note: The report steps are met in any case
245  // \Note: The sub stepping will require a copy of the state variables
246  if (adaptiveTimeStepping_) {
247  const auto& events = schedule()[timer.currentStepNum()].events();
248  if (enableTUNING) {
249  if (events.hasEvent(ScheduleEvents::TUNING_CHANGE)) {
250  const auto& sched_state = schedule()[timer.currentStepNum()];
251  const auto& tuning = sched_state.tuning();
252  const auto& max_next_tstep = sched_state.max_next_tstep();
253  adaptiveTimeStepping_->updateTUNING(max_next_tstep, tuning);
254  }
255  }
256  bool event = events.hasEvent(ScheduleEvents::NEW_WELL) ||
257  events.hasEvent(ScheduleEvents::INJECTION_TYPE_CHANGED) ||
258  events.hasEvent(ScheduleEvents::WELL_SWITCHED_INJECTOR_PRODUCER) ||
259  events.hasEvent(ScheduleEvents::WELL_STATUS_CHANGE);
260  auto stepReport = adaptiveTimeStepping_->step(timer, *solver, event, nullptr);
261  report_ += stepReport;
262  //Pass simulation report to eclwriter for summary output
263  ebosSimulator_.problem().setSimulationReport(report_);
264  } else {
265  // solve for complete report step
266  auto stepReport = solver->step(timer);
267  report_ += stepReport;
268  if (terminalOutput_) {
269  std::ostringstream ss;
270  stepReport.reportStep(ss);
271  OpmLog::info(ss.str());
272  }
273  }
274 
275  // write simulation state at the report stage
276  Dune::Timer perfTimer;
277  perfTimer.start();
278  const double nextstep = adaptiveTimeStepping_ ? adaptiveTimeStepping_->suggestedNextStep() : -1.0;
279  ebosSimulator_.problem().setNextTimeStepSize(nextstep);
280  ebosSimulator_.problem().writeOutput();
281  report_.success.output_write_time += perfTimer.stop();
282 
283  solver->model().endReportStep();
284 
285  // take time that was used to solve system for this reportStep
286  solverTimer_->stop();
287 
288  // update timing.
289  report_.success.solver_time += solverTimer_->secsSinceStart();
290 
291  // Increment timer, remember well state.
292  ++timer;
293 
294  if (terminalOutput_) {
295  if (!timer.initialStep()) {
296  const std::string version = moduleVersionName();
297  outputTimestampFIP(timer, eclState().getTitle(), version);
298  }
299  }
300 
301  if (terminalOutput_) {
302  std::string msg =
303  "Time step took " + std::to_string(solverTimer_->secsSinceStart()) + " seconds; "
304  "total solver time " + std::to_string(report_.success.solver_time) + " seconds.";
305  OpmLog::debug(msg);
306  }
307  return true;
308  }
309 
310  SimulatorReport finalize()
311  {
312  // make sure all output is written to disk before run is finished
313  {
314  Dune::Timer finalOutputTimer;
315  finalOutputTimer.start();
316 
317  ebosSimulator_.problem().finalizeOutput();
318  report_.success.output_write_time += finalOutputTimer.stop();
319  }
320 
321  // Stop timer and create timing report
322  totalTimer_->stop();
323  report_.success.total_time = totalTimer_->secsSinceStart();
324  report_.success.converged = true;
325 
326  return report_;
327  }
328 
329  const Grid& grid() const
330  { return ebosSimulator_.vanguard().grid(); }
331 
332 protected:
333 
334  std::unique_ptr<Solver> createSolver(WellModel& wellModel)
335  {
336  auto model = std::make_unique<Model>(ebosSimulator_,
337  modelParam_,
338  wellModel,
339  terminalOutput_);
340 
341  return std::make_unique<Solver>(solverParam_, std::move(model));
342  }
343 
344  const EclipseState& eclState() const
345  { return ebosSimulator_.vanguard().eclState(); }
346 
347 
348  const Schedule& schedule() const
349  { return ebosSimulator_.vanguard().schedule(); }
350 
351  bool isRestart() const
352  {
353  const auto& initconfig = eclState().getInitConfig();
354  return initconfig.restartRequested();
355  }
356 
357  WellModel& wellModel_()
358  { return ebosSimulator_.problem().wellModel(); }
359 
360  const WellModel& wellModel_() const
361  { return ebosSimulator_.problem().wellModel(); }
362 
363  // Data.
364  Simulator& ebosSimulator_;
365  std::unique_ptr<WellConnectionAuxiliaryModule<TypeTag>> wellAuxMod_;
366 
367  ModelParameters modelParam_;
368  SolverParameters solverParam_;
369 
370  // Observed objects.
371  PhaseUsage phaseUsage_;
372  // Misc. data
373  bool terminalOutput_;
374 
375  SimulatorReport report_;
376  std::unique_ptr<time::StopWatch> solverTimer_;
377  std::unique_ptr<time::StopWatch> totalTimer_;
378  std::unique_ptr<TimeStepper> adaptiveTimeStepping_;
379 };
380 
381 } // namespace Opm
382 
383 #endif // OPM_SIMULATOR_FULLY_IMPLICIT_BLACKOIL_EBOS_HPP
Definition: AdaptiveTimeSteppingEbos.hpp:238
Class for handling the blackoil well model.
Definition: BlackoilAquiferModel.hpp:81
A model implementation for three-phase black oil.
Definition: BlackoilModelEbos.hpp:158
Class for handling the blackoil well model.
Definition: BlackoilWellModel.hpp:94
A nonlinear solver class suitable for general fully-implicit models, as well as pressure,...
Definition: NonlinearSolverEbos.hpp:89
a simulator for the blackoil model
Definition: SimulatorFullyImplicitBlackoilEbos.hpp:72
SimulatorReport run(SimulatorTimer &timer)
Run the simulation.
Definition: SimulatorFullyImplicitBlackoilEbos.hpp:148
SimulatorFullyImplicitBlackoilEbos(Simulator &ebosSimulator)
Initialise from parameters and objects to observe.
Definition: SimulatorFullyImplicitBlackoilEbos.hpp:117
Definition: SimulatorTimer.hpp:37
int currentStepNum() const override
Current step number.
Definition: SimulatorTimer.cpp:80
bool done() const override
Return true if op++() has been called numSteps() times.
Definition: SimulatorTimer.cpp:153
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: BlackoilPhases.hpp:27
std::string moduleVersionName()
Return the version name of the module, for example "2015.10" (for a release branch) or "2016....
Definition: moduleVersion.cpp:29
PhaseUsage phaseUsageFromDeck(const EclipseState &eclipseState)
Looks at presence of WATER, OIL and GAS keywords in state object to determine active phases.
Definition: phaseUsageFromDeck.cpp:141
Solver parameters for the BlackoilModel.
Definition: BlackoilModelParametersEbos.hpp:327
Definition: NonlinearSolverEbos.hpp:101
Definition: SimulatorFullyImplicitBlackoilEbos.hpp:39
Definition: BlackoilWellModel.hpp:82
Definition: SimulatorFullyImplicitBlackoilEbos.hpp:43
Definition: SimulatorReport.hpp:69