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  adaptiveTimeStepping_ = std::make_unique<TimeStepper>(schedule()[timer.currentStepNum()].tuning(),
175  unitSystem, terminalOutput_);
176  }
177  else {
178  adaptiveTimeStepping_ = std::make_unique<TimeStepper>(unitSystem, terminalOutput_);
179  }
180 
181  if (isRestart()) {
182  // For restarts the ebosSimulator may have gotten some information
183  // about the next timestep size from the OPMEXTRA field
184  adaptiveTimeStepping_->setSuggestedNextStep(ebosSimulator_.timeStepSize());
185  }
186  }
187  }
188 
189  bool runStep(SimulatorTimer& timer)
190  {
191  if (schedule().exitStatus().has_value()) {
192  if (terminalOutput_) {
193  OpmLog::info("Stopping simulation since EXIT was triggered by an action keyword.");
194  }
195  report_.success.exit_status = schedule().exitStatus().value();
196  return false;
197  }
198 
199  // Report timestep.
200  if (terminalOutput_) {
201  std::ostringstream ss;
202  timer.report(ss);
203  OpmLog::debug(ss.str());
204  }
205 
206  if (terminalOutput_) {
207  outputReportStep(timer);
208  }
209 
210  // write the inital state at the report stage
211  if (timer.initialStep()) {
212  Dune::Timer perfTimer;
213  perfTimer.start();
214 
215  ebosSimulator_.setEpisodeIndex(-1);
216  ebosSimulator_.setEpisodeLength(0.0);
217  ebosSimulator_.setTimeStepSize(0.0);
218 
219  wellModel_().beginReportStep(timer.currentStepNum());
220  ebosSimulator_.problem().writeOutput();
221 
222  report_.success.output_write_time += perfTimer.stop();
223  }
224 
225  // Run a multiple steps of the solver depending on the time step control.
226  solverTimer_->start();
227 
228  auto solver = createSolver(wellModel_());
229 
230  ebosSimulator_.startNextEpisode(
231  ebosSimulator_.startTime()
232  + schedule().seconds(timer.currentStepNum()),
233  timer.currentStepLength());
234  ebosSimulator_.setEpisodeIndex(timer.currentStepNum());
235  solver->model().beginReportStep();
236  bool enableTUNING = EWOMS_GET_PARAM(TypeTag, bool, EnableTuning);
237 
238  // If sub stepping is enabled allow the solver to sub cycle
239  // in case the report steps are too large for the solver to converge
240  //
241  // \Note: The report steps are met in any case
242  // \Note: The sub stepping will require a copy of the state variables
243  if (adaptiveTimeStepping_) {
244  const auto& events = schedule()[timer.currentStepNum()].events();
245  if (enableTUNING) {
246  if (events.hasEvent(ScheduleEvents::TUNING_CHANGE)) {
247  adaptiveTimeStepping_->updateTUNING(schedule()[timer.currentStepNum()].tuning());
248  }
249  }
250  bool event = events.hasEvent(ScheduleEvents::NEW_WELL) ||
251  events.hasEvent(ScheduleEvents::INJECTION_TYPE_CHANGED) ||
252  events.hasEvent(ScheduleEvents::WELL_SWITCHED_INJECTOR_PRODUCER) ||
253  events.hasEvent(ScheduleEvents::WELL_STATUS_CHANGE);
254  auto stepReport = adaptiveTimeStepping_->step(timer, *solver, event, nullptr);
255  report_ += stepReport;
256  } else {
257  // solve for complete report step
258  auto stepReport = solver->step(timer);
259  report_ += stepReport;
260  if (terminalOutput_) {
261  std::ostringstream ss;
262  stepReport.reportStep(ss);
263  OpmLog::info(ss.str());
264  }
265  }
266 
267  // write simulation state at the report stage
268  Dune::Timer perfTimer;
269  perfTimer.start();
270  const double nextstep = adaptiveTimeStepping_ ? adaptiveTimeStepping_->suggestedNextStep() : -1.0;
271  ebosSimulator_.problem().setNextTimeStepSize(nextstep);
272  ebosSimulator_.problem().writeOutput();
273  report_.success.output_write_time += perfTimer.stop();
274 
275  solver->model().endReportStep();
276 
277  // take time that was used to solve system for this reportStep
278  solverTimer_->stop();
279 
280  // update timing.
281  report_.success.solver_time += solverTimer_->secsSinceStart();
282 
283  // Increment timer, remember well state.
284  ++timer;
285 
286  if (terminalOutput_) {
287  if (!timer.initialStep()) {
288  const std::string version = moduleVersionName();
289  outputTimestampFIP(timer, eclState().getTitle(), version);
290  }
291  }
292 
293  if (terminalOutput_) {
294  std::string msg =
295  "Time step took " + std::to_string(solverTimer_->secsSinceStart()) + " seconds; "
296  "total solver time " + std::to_string(report_.success.solver_time) + " seconds.";
297  OpmLog::debug(msg);
298  }
299  return true;
300  }
301 
302  SimulatorReport finalize()
303  {
304  // make sure all output is written to disk before run is finished
305  {
306  Dune::Timer finalOutputTimer;
307  finalOutputTimer.start();
308 
309  ebosSimulator_.problem().finalizeOutput();
310  report_.success.output_write_time += finalOutputTimer.stop();
311  }
312 
313  // Stop timer and create timing report
314  totalTimer_->stop();
315  report_.success.total_time = totalTimer_->secsSinceStart();
316  report_.success.converged = true;
317 
318  return report_;
319  }
320 
321  const Grid& grid() const
322  { return ebosSimulator_.vanguard().grid(); }
323 
324 protected:
325 
326  std::unique_ptr<Solver> createSolver(WellModel& wellModel)
327  {
328  auto model = std::make_unique<Model>(ebosSimulator_,
329  modelParam_,
330  wellModel,
331  terminalOutput_);
332 
333  return std::make_unique<Solver>(solverParam_, std::move(model));
334  }
335 
336  const EclipseState& eclState() const
337  { return ebosSimulator_.vanguard().eclState(); }
338 
339 
340  const Schedule& schedule() const
341  { return ebosSimulator_.vanguard().schedule(); }
342 
343  bool isRestart() const
344  {
345  const auto& initconfig = eclState().getInitConfig();
346  return initconfig.restartRequested();
347  }
348 
349  WellModel& wellModel_()
350  { return ebosSimulator_.problem().wellModel(); }
351 
352  const WellModel& wellModel_() const
353  { return ebosSimulator_.problem().wellModel(); }
354 
355  // Data.
356  Simulator& ebosSimulator_;
357  std::unique_ptr<WellConnectionAuxiliaryModule<TypeTag>> wellAuxMod_;
358 
359  ModelParameters modelParam_;
360  SolverParameters solverParam_;
361 
362  // Observed objects.
363  PhaseUsage phaseUsage_;
364  // Misc. data
365  bool terminalOutput_;
366 
367  SimulatorReport report_;
368  std::unique_ptr<time::StopWatch> solverTimer_;
369  std::unique_ptr<time::StopWatch> totalTimer_;
370  std::unique_ptr<TimeStepper> adaptiveTimeStepping_;
371 };
372 
373 } // namespace Opm
374 
375 #endif // OPM_SIMULATOR_FULLY_IMPLICIT_BLACKOIL_EBOS_HPP
Definition: AdaptiveTimeSteppingEbos.hpp:237
Class for handling the blackoil well model.
Definition: BlackoilAquiferModel.hpp:81
A model implementation for three-phase black oil.
Definition: BlackoilModelEbos.hpp:154
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:26
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:66