opm-simulators
NonlinearSystemNldd.hpp
1 /*
2  Copyright 2013, 2015 SINTEF ICT, Applied Mathematics.
3  Copyright 2014, 2015 Dr. Blatt - HPC-Simulation-Software & Services
4  Copyright 2014, 2015 Statoil ASA.
5  Copyright 2015 NTNU
6  Copyright 2015, 2016, 2017 IRIS AS
7 
8  This file is part of the Open Porous Media project (OPM).
9 
10  OPM is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  OPM is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with OPM. If not, see <http://www.gnu.org/licenses/>.
22 */
23 
24 #ifndef OPM_NONLINEAR_SYSTEM_NLDD_HEADER_INCLUDED
25 #define OPM_NONLINEAR_SYSTEM_NLDD_HEADER_INCLUDED
26 
27 #include <dune/common/timer.hh>
28 #include <dune/istl/istlexception.hh>
29 
30 #include <opm/common/Exceptions.hpp>
31 
32 #include <opm/grid/common/SubGridPart.hpp>
33 
34 #include <opm/simulators/aquifers/AquiferGridUtils.hpp>
35 
36 #include <opm/simulators/flow/countGlobalCells.hpp>
37 #include <opm/simulators/flow/NlddReporting.hpp>
38 #include <opm/simulators/flow/NonlinearSolver.hpp>
39 #include <opm/simulators/flow/partitionCells.hpp>
40 #include <opm/simulators/flow/priVarsPacking.hpp>
41 #include <opm/simulators/flow/SubDomain.hpp>
42 
43 #include <opm/simulators/linalg/extractMatrix.hpp>
44 
45 #if COMPILE_GPU_BRIDGE
46 #include <opm/simulators/linalg/ISTLSolverGpuBridge.hpp>
47 #else
48 #include <opm/simulators/linalg/ISTLSolver.hpp>
49 #endif
50 #include <opm/simulators/linalg/ISTLSolverRuntimeOptionProxy.hpp>
51 
52 #include <opm/simulators/timestepping/ConvergenceReport.hpp>
53 #include <opm/simulators/timestepping/SimulatorReport.hpp>
54 #include <opm/simulators/timestepping/SimulatorTimerInterface.hpp>
55 
56 #include <opm/simulators/utils/ComponentName.hpp>
57 #include <opm/simulators/utils/DeferredLoggingErrorHelpers.hpp>
58 
59 #include <opm/simulators/wells/BlackoilWellModelNldd.hpp>
60 
61 #include <fmt/format.h>
62 
63 #include <algorithm>
64 #include <array>
65 #include <cassert>
66 #include <cmath>
67 #include <cstddef>
68 #include <filesystem>
69 #include <memory>
70 #include <numeric>
71 #include <set>
72 #include <sstream>
73 #include <string>
74 #include <type_traits>
75 #include <utility>
76 #include <vector>
77 
78 namespace Opm {
79 
80 template<class TypeTag> class NonlinearSystemBlackOilReservoir;
81 
83 template <class TypeTag>
85 {
86 public:
92  using ModelParameters = typename NonlinearSystemBlackOilReservoir<TypeTag>::ModelParameters;
94 
95  using BVector = typename NonlinearSystemBlackOilReservoir<TypeTag>::BVector;
96  using Domain = SubDomain<Grid>;
98  using Mat = typename NonlinearSystemBlackOilReservoir<TypeTag>::Mat;
99 
100  static constexpr int numEq = Indices::numEq;
101 
105  : model_(model)
106  , wellModel_(model.wellModel())
107  , rank_(model_.simulator().vanguard().grid().comm().rank())
108  {
109  // Create partitions.
110  const auto& [partition_vector_initial, num_domains_initial] = this->partitionCells();
111 
112  int num_domains = num_domains_initial;
113  std::vector<int> partition_vector = partition_vector_initial;
114 
115  // Fix-up for an extreme case: Interior cells who do not have any on-rank
116  // neighbours. Move all such cells into a single domain on this rank,
117  // and mark the domain for skipping. For what it's worth, we've seen this
118  // case occur in practice when testing on field cases.
119  bool isolated_cells = false;
120  for (auto& domainId : partition_vector) {
121  if (domainId < 0) {
122  domainId = num_domains;
123  isolated_cells = true;
124  }
125  }
126  if (isolated_cells) {
127  num_domains++;
128  }
129 
130  // Set nldd handler in main well model
131  model.wellModel().setNlddAdapter(&wellModel_);
132 
133  // Scan through partitioning to get correct size for each.
134  std::vector<int> sizes(num_domains, 0);
135  for (const auto& p : partition_vector) {
136  ++sizes[p];
137  }
138 
139  // Set up correctly sized vectors of entity seeds and of indices for each partition.
140  using EntitySeed = typename Grid::template Codim<0>::EntitySeed;
141  std::vector<std::vector<EntitySeed>> seeds(num_domains);
142  std::vector<std::vector<int>> partitions(num_domains);
143  for (int domain = 0; domain < num_domains; ++domain) {
144  seeds[domain].resize(sizes[domain]);
145  partitions[domain].resize(sizes[domain]);
146  }
147 
148  // Iterate through grid once, setting the seeds of all partitions.
149  // Note: owned cells only!
150  const auto& grid = model_.simulator().vanguard().grid();
151 
152  std::vector<int> count(num_domains, 0);
153  const auto& gridView = grid.leafGridView();
154  const auto beg = gridView.template begin<0, Dune::Interior_Partition>();
155  const auto end = gridView.template end<0, Dune::Interior_Partition>();
156  int cell = 0;
157  for (auto it = beg; it != end; ++it, ++cell) {
158  const int p = partition_vector[cell];
159  seeds[p][count[p]] = it->seed();
160  partitions[p][count[p]] = cell;
161  ++count[p];
162  }
163  assert(count == sizes);
164 
165  // Create the domains.
166  for (int index = 0; index < num_domains; ++index) {
167  std::vector<bool> interior(partition_vector.size(), false);
168  for (int ix : partitions[index]) {
169  interior[ix] = true;
170  }
171 
172  Dune::SubGridPart<Grid> view{grid, std::move(seeds[index])};
173 
174  // Mark the last domain for skipping if it contains isolated cells
175  const bool skip = isolated_cells && (index == num_domains - 1);
176  this->domains_.emplace_back(index,
177  std::move(partitions[index]),
178  std::move(interior),
179  std::move(view),
180  skip);
181  }
182 
183  // Initialize storage for previous mobilities in a single flat vector
184  const auto numCells = grid.size(0);
185  previousMobilities_.resize(numCells * FluidSystem::numActivePhases(), 0.0);
186  for (const auto& domain : domains_) {
187  updateMobilities(domain);
188  }
189 
190  // Initialize domain_needs_solving_ to true for all domains
191  domain_needs_solving_.resize(num_domains, true);
192 
193  // Set up container for the local system matrices.
194  domain_matrices_.resize(num_domains);
195 
196  // Set up container for the local linear solvers.
197  for (int index = 0; index < num_domains; ++index) {
198  // TODO: The ISTLSolver constructor will make
199  // parallel structures appropriate for the full grid
200  // only. This must be addressed before going parallel.
201  const auto& eclState = model_.simulator().vanguard().eclState();
202  FlowLinearSolverParameters loc_param;
203  loc_param.is_nldd_local_solver_ = true;
204  loc_param.init(eclState.getSimulationConfig().useCPR());
205  // Override solver type with umfpack if small domain.
206  if (domains_[index].cells.size() < 200) {
207  loc_param.linsolver_ = "umfpack";
208  }
209  loc_param.linear_solver_print_json_definition_ = false;
210  const bool force_serial = true;
211  domain_linsolvers_.emplace_back(model_.simulator(), loc_param, force_serial);
212  domain_linsolvers_.back().setDomainIndex(index);
213  }
214 
215  assert(int(domains_.size()) == num_domains);
216 
217  domain_reports_accumulated_.resize(num_domains);
218 
219  // Print domain distribution summary
220  ::Opm::printDomainDistributionSummary(
221  partition_vector,
222  domains_,
223  local_reports_accumulated_,
224  domain_reports_accumulated_,
225  grid,
226  wellModel_.numLocalWellsEnd());
227  }
228 
230  void prepareStep()
231  {
232  // Setup domain->well mapping.
233  wellModel_.setupDomains(domains_);
234  }
235 
237  template <class NonlinearSolverType>
239  NonlinearSolverType& nonlinear_solver)
240  {
241  // ----------- Set up reports and timer -----------
242  SimulatorReportSingle report;
243  // When nldd_num_initial_newton_iter_ == 0, NLDD starts immediately but wells are still
244  // properly initialized via NewtonIterationContext::needsTimestepInit() which
245  // ensures prepareTimeStep() is called during the initial global linearization.
246  // If nldd_num_initial_newton_iter_ > 0, run global Newton iterations first.
247  // TODO: Investigate whether wells and other subsystems are correctly initialized
248  // for the following NLDD iteration.
249  if (model_.simulator().problem().iterationContext().iteration() < model_.param().nldd_num_initial_newton_iter_) {
250  report = model_.nonlinearIterationNewton(timer, nonlinear_solver);
251  return report;
252  }
253 
254  model_.initialLinearization(report, model_.param().newton_min_iter_,
255  model_.param().newton_max_iter_, timer);
256 
257  if (report.converged) {
258  return report;
259  }
260 
261  // Remove the initial linearization entry; the final Newton step will
262  // record its own, keeping one entry per outer iteration.
263  model_.popLastConvergenceReport();
264 
265  // ----------- If not converged, do an NLDD iteration -----------
266  Dune::Timer localSolveTimer;
267  Dune::Timer detailTimer;
268  localSolveTimer.start();
269  detailTimer.start();
270  auto& solution = model_.simulator().model().solution(0);
271  auto initial_solution = solution;
272  auto locally_solved = initial_solution;
273 
274  // ----------- Decide on an ordering for the domains -----------
275  const auto domain_order = this->getSubdomainOrder();
276  local_reports_accumulated_.success.pre_post_time += detailTimer.stop();
277 
278  // ----------- Solve each domain separately -----------
279  DeferredLogger logger;
280  std::vector<SimulatorReportSingle> domain_reports(domains_.size());
281 
282  OPM_BEGIN_PARALLEL_TRY_CATCH()
283  for (const int domain_index : domain_order) {
284  const auto& domain = domains_[domain_index];
285  SimulatorReportSingle local_report;
286  detailTimer.reset();
287  detailTimer.start();
288 
289  domain_needs_solving_[domain_index] = checkIfSubdomainNeedsSolving(domain);
290 
291  updateMobilities(domain);
292 
293  if (domain.skip || !domain_needs_solving_[domain_index]) {
294  local_report.skipped_domains = true;
295  local_report.converged = true;
296  domain_reports[domain.index] = local_report;
297  continue;
298  }
299  switch (model_.param().local_solve_approach_) {
300  case DomainSolveApproach::Jacobi:
301  solveDomainJacobi(solution, locally_solved, local_report, logger,
302  timer, domain);
303  break;
304  default:
305  case DomainSolveApproach::GaussSeidel:
306  solveDomainGaussSeidel(solution, locally_solved, local_report, logger,
307  timer, domain);
308  break;
309  }
310  // This should have updated the global matrix to be
311  // dR_i/du_j evaluated at new local solutions for
312  // i == j, at old solution for i != j.
313  if (!local_report.converged) {
314  // TODO: more proper treatment, including in parallel.
315  logger.debug(fmt::format("Convergence failure in domain {} on rank {}." , domain.index, rank_));
316  }
317  local_report.solver_time += detailTimer.stop();
318  domain_reports[domain.index] = local_report;
319  }
320  OPM_END_PARALLEL_TRY_CATCH("Unexpected exception in local domain solve: ", model_.simulator().vanguard().grid().comm());
321 
322  detailTimer.reset();
323  detailTimer.start();
324  // Communicate and log all messages.
325  auto global_logger = gatherDeferredLogger(logger, model_.simulator().vanguard().grid().comm());
326  global_logger.logMessages();
327 
328  // Accumulate local solve data.
329  // Putting the counts in a single array to avoid multiple
330  // comm.sum() calls. Keeping the named vars for readability.
331  std::array<int, 5> counts{ 0, 0, 0, static_cast<int>(domain_reports.size()), 0 };
332  int& num_converged = counts[0];
333  int& num_converged_already = counts[1];
334  int& num_local_newtons = counts[2];
335  int& num_domains = counts[3];
336  int& num_skipped = counts[4];
337  {
338  auto step_newtons = 0;
339  const auto dr_size = domain_reports.size();
340  for (auto i = 0*dr_size; i < dr_size; ++i) {
341  // Reset the needsSolving flag for the next iteration
342  domain_needs_solving_[i] = false;
343  const auto& dr = domain_reports[i];
344  if (dr.converged) {
345  ++num_converged;
346  if (dr.total_newton_iterations == 0) {
347  ++num_converged_already;
348  }
349  else {
350  // If we needed to solve the domain, we also solve in next iteration
351  domain_needs_solving_[i] = true;
352  }
353  }
354  if (dr.skipped_domains) {
355  ++num_skipped;
356  }
357  step_newtons += dr.total_newton_iterations;
358  // Accumulate local reports per domain
359  domain_reports_accumulated_[i] += dr;
360  // Accumulate local reports per rank
361  local_reports_accumulated_ += dr;
362  }
363  num_local_newtons = step_newtons;
364  }
365 
366  if (model_.param().local_solve_approach_ == DomainSolveApproach::Jacobi) {
367  solution = locally_solved;
368  model_.simulator().model().invalidateAndUpdateIntensiveQuantities(/*timeIdx=*/0);
369  }
370 
371 #if HAVE_MPI
372  // Communicate solutions:
373  // With multiple processes, this process' overlap (i.e. not
374  // owned) cells' solution values have been modified by local
375  // solves in the owning processes, and remain unchanged
376  // here. We must therefore receive the updated solution on the
377  // overlap cells and update their intensive quantities before
378  // we move on.
379  const auto& comm = model_.simulator().vanguard().grid().comm();
380  if (comm.size() > 1) {
381  const auto* ccomm = model_.simulator().model().newtonMethod().linearSolver().comm();
382 
383  // Copy numerical values from primary vars.
384  ccomm->copyOwnerToAll(solution, solution);
385 
386  // Copy flags from primary vars.
387  const std::size_t num = solution.size();
388  Dune::BlockVector<std::size_t> allmeanings(num);
389  for (std::size_t ii = 0; ii < num; ++ii) {
390  allmeanings[ii] = PVUtil::pack(solution[ii]);
391  }
392  ccomm->copyOwnerToAll(allmeanings, allmeanings);
393  for (std::size_t ii = 0; ii < num; ++ii) {
394  PVUtil::unPack(solution[ii], allmeanings[ii]);
395  }
396 
397  // Update intensive quantities for our overlap values.
398  model_.simulator().model().invalidateAndUpdateIntensiveQuantitiesOverlap(/*timeIdx=*/0);
399 
400  // Make total counts of domains converged.
401  comm.sum(counts.data(), counts.size());
402  }
403 #endif // HAVE_MPI
404 
405  const bool is_iorank = this->rank_ == 0;
406  if (is_iorank) {
407  OpmLog::debug(fmt::format(fmt::runtime("Local solves finished. Converged for {}/{} domains. {} domains were skipped. {} domains did no work. {} total local Newton iterations.\n"),
408  num_converged, num_domains, num_skipped, num_converged_already, num_local_newtons));
409  }
410  auto total_local_solve_time = localSolveTimer.stop();
411  report.local_solve_time += total_local_solve_time;
412  local_reports_accumulated_.success.total_time += total_local_solve_time;
413  local_reports_accumulated_.success.pre_post_time += detailTimer.stop();
414 
415  // Finish with a global Newton step.
416  auto rep = model_.nonlinearIterationNewton(timer, nonlinear_solver);
417  report += rep;
418  if (rep.converged) {
419  report.converged = true;
420  }
421  return report;
422  }
423 
426  {
427  return local_reports_accumulated_;
428  }
429 
431  const std::vector<SimulatorReport>& domainAccumulatedReports() const
432  {
433  // Update the number of wells for each domain that has been added to the well model at this point
434  // this is a mutable operation that updates the well counts in the domain reports.
435  const auto dr_size = domain_reports_accumulated_.size();
436  // Reset well counts before updating
437  for (auto i = 0*dr_size; i < dr_size; ++i) {
438  domain_reports_accumulated_[i].success.num_wells = 0;
439  }
440  // Update the number of wells for each domain
441  for (const auto& [wname, domain] : wellModel_.well_domain()) {
442  domain_reports_accumulated_[domain].success.num_wells++;
443  }
444  return domain_reports_accumulated_;
445  }
446 
449  void writePartitions(const std::filesystem::path& odir) const
450  {
451  const auto& grid = this->model_.simulator().vanguard().grid();
452  const auto& elementMapper = this->model_.simulator().model().elementMapper();
453  const auto& cartMapper = this->model_.simulator().vanguard().cartesianIndexMapper();
454 
455  ::Opm::writePartitions(
456  odir,
457  domains_,
458  grid,
459  elementMapper,
460  cartMapper);
461  }
462 
464  void writeNonlinearIterationsPerCell(const std::filesystem::path& odir) const
465  {
466  const auto& grid = this->model_.simulator().vanguard().grid();
467  const auto& elementMapper = this->model_.simulator().model().elementMapper();
468  const auto& cartMapper = this->model_.simulator().vanguard().cartesianIndexMapper();
469 
470  ::Opm::writeNonlinearIterationsPerCell(
471  odir,
472  domains_,
473  domain_reports_accumulated_,
474  grid,
475  elementMapper,
476  cartMapper);
477  }
478 
479 private:
482  solveDomain(const Domain& domain,
483  const SimulatorTimerInterface& timer,
484  SimulatorReportSingle& local_report,
485  DeferredLogger& logger,
486  const bool initial_assembly_required)
487  {
488  auto& modelSimulator = model_.simulator();
489  Dune::Timer detailTimer;
490 
491  // RAII guard: local iteration context for this domain, restored on scope exit.
492  LocalContextGuard localCtxGuard(modelSimulator.problem());
493  auto& localCtx = localCtxGuard.context();
494 
495  // When called, if assembly has already been performed
496  // with the initial values, we only need to check
497  // for local convergence. Otherwise, we must do a local
498  // assembly.
499  if (initial_assembly_required) {
500  detailTimer.start();
501  // TODO: we should have a beginIterationLocal function()
502  // only handling the well model for now
503  wellModel_.assemble(modelSimulator.timeStepSize(),
504  domain);
505  const double tt0 = detailTimer.stop();
506  local_report.assemble_time += tt0;
507  local_report.assemble_time_well += tt0;
508  detailTimer.reset();
509  detailTimer.start();
510  // Assemble reservoir locally.
511  this->assembleReservoirDomain(domain);
512  local_report.assemble_time += detailTimer.stop();
513  local_report.total_linearizations += 1;
514  }
515  detailTimer.reset();
516  detailTimer.start();
517  std::vector<Scalar> resnorms;
518  auto convreport = this->getDomainConvergence(domain, timer, logger, resnorms);
519  local_report.update_time += detailTimer.stop();
520  if (convreport.converged()) {
521  // TODO: set more info, timing etc.
522  local_report.converged = true;
523  return convreport;
524  }
525 
526  // We have already assembled for the first iteration,
527  // but not done the Schur complement for the wells yet.
528  detailTimer.reset();
529  detailTimer.start();
530  model_.wellModel().linearizeDomain(domain,
531  modelSimulator.model().linearizer().jacobian(),
532  modelSimulator.model().linearizer().residual());
533  const double tt1 = detailTimer.stop();
534  local_report.assemble_time += tt1;
535  local_report.assemble_time_well += tt1;
536 
537  // Local Newton loop.
538  const int max_iter = model_.param().max_local_solve_iterations_;
539  const auto& grid = modelSimulator.vanguard().grid();
540  double damping_factor = 1.0;
541  std::vector<std::vector<Scalar>> convergence_history;
542  convergence_history.reserve(20);
543  convergence_history.push_back(resnorms);
544  do {
545  // Solve local linear system.
546  // Note that x has full size, we expect it to be nonzero only for in-domain cells.
547  const int nc = grid.size(0);
548  BVector x(nc);
549  detailTimer.reset();
550  detailTimer.start();
551  double setup_time = 0.0;
552  try {
553  this->solveJacobianSystemDomain(domain, x, setup_time);
554  }
555  catch (const NumericalProblem& e) {
556  // Local linear solve failed - treat as domain-level failure, not global timestep cut.
557  logger.debug(fmt::format(
558  "Local linear solver failed in domain {} on rank {}: {}",
559  domain.index, rank_, e.what()));
560  // record statistics and return early
561  local_report.linear_solve_time += detailTimer.stop();
562  local_report.linear_solve_setup_time += setup_time;
563  local_report.total_linear_iterations = domain_linsolvers_[domain.index].iterations();
564  modelSimulator.problem().endIteration();
565  local_report.converged = false;
566  local_report.total_newton_iterations = localCtx.iteration();
567  local_report.total_linearizations += localCtx.iteration();
568  return convreport;
569  }
570  model_.wellModel().postSolveDomain(x, domain);
571  if (damping_factor != 1.0) {
572  x *= damping_factor;
573  }
574  local_report.linear_solve_time += detailTimer.stop();
575  local_report.linear_solve_setup_time += setup_time;
576  local_report.total_linear_iterations = domain_linsolvers_[domain.index].iterations();
577 
578  // Update local solution. // TODO: x is still full size, should we optimize it?
579  detailTimer.reset();
580  detailTimer.start();
581  this->updateDomainSolution(domain, x);
582  local_report.update_time += detailTimer.stop();
583 
584  // Assemble well and reservoir.
585  detailTimer.reset();
586  detailTimer.start();
587  localCtx.advanceIteration();
588  // TODO: we should have a beginIterationLocal function()
589  // only handling the well model for now
590  // Assemble reservoir locally.
591  wellModel_.assemble(modelSimulator.timeStepSize(),
592  domain);
593  const double tt3 = detailTimer.stop();
594  local_report.assemble_time += tt3;
595  local_report.assemble_time_well += tt3;
596  detailTimer.reset();
597  detailTimer.start();
598  this->assembleReservoirDomain(domain);
599  local_report.assemble_time += detailTimer.stop();
600 
601  // Check for local convergence.
602  detailTimer.reset();
603  detailTimer.start();
604  resnorms.clear();
605  convreport = this->getDomainConvergence(domain, timer, logger, resnorms);
606  convergence_history.push_back(resnorms);
607  local_report.update_time += detailTimer.stop();
608 
609  // apply the Schur complement of the well model to the
610  // reservoir linearized equations
611  detailTimer.reset();
612  detailTimer.start();
613  model_.wellModel().linearizeDomain(domain,
614  modelSimulator.model().linearizer().jacobian(),
615  modelSimulator.model().linearizer().residual());
616  const double tt2 = detailTimer.stop();
617  local_report.assemble_time += tt2;
618  local_report.assemble_time_well += tt2;
619 
620  // Check if we should dampen. Only do so if wells are converged.
621  if (!convreport.converged() && !convreport.wellFailed()) {
622  bool oscillate = false;
623  bool stagnate = false;
624  const auto num_residuals = convergence_history.front().size();
625  detail::detectOscillations(convergence_history, localCtx.iteration(), num_residuals,
626  Scalar{0.2}, 1, oscillate, stagnate);
627  if (oscillate) {
628  damping_factor *= 0.85;
629  logger.debug(fmt::format(fmt::runtime("| Damping factor is now {}"), damping_factor));
630  }
631  }
632  } while (!convreport.converged() && localCtx.iteration() <= max_iter);
633 
634  modelSimulator.problem().endIteration();
635 
636  local_report.converged = convreport.converged();
637  local_report.total_newton_iterations = localCtx.iteration();
638  local_report.total_linearizations += localCtx.iteration();
639  // TODO: set more info, timing etc.
640  return convreport;
641  }
642 
644  void assembleReservoirDomain(const Domain& domain)
645  {
646  OPM_TIMEBLOCK(assembleReservoirDomain);
647  // -------- Mass balance equations --------
648  model_.simulator().model().linearizer().linearizeDomain(domain);
649  }
650 
652  void solveJacobianSystemDomain(const Domain& domain, BVector& global_x, double& setup_time)
653  {
654  const auto& modelSimulator = model_.simulator();
655 
656  Dune::Timer perfTimer;
657  perfTimer.start();
658 
659  const Mat& main_matrix = modelSimulator.model().linearizer().jacobian().istlMatrix();
660  if (domain_matrices_[domain.index]) {
661  Details::copySubMatrix(main_matrix, domain.cells, *domain_matrices_[domain.index]);
662  } else {
663  domain_matrices_[domain.index] = std::make_unique<Mat>(Details::extractMatrix(main_matrix, domain.cells));
664  }
665  auto& jac = *domain_matrices_[domain.index];
666  auto res = Details::extractVector(modelSimulator.model().linearizer().residual(),
667  domain.cells);
668  auto x = res;
669 
670  // set initial guess
671  global_x = 0.0;
672  x = 0.0;
673 
674  auto& linsolver = domain_linsolvers_[domain.index];
675 
676  linsolver.prepare(jac, res);
677  setup_time = perfTimer.stop();
678  linsolver.setResidual(res);
679  linsolver.solve(x);
680 
681  Details::setGlobal(x, domain.cells, global_x);
682  }
683 
685  void updateDomainSolution(const Domain& domain, const BVector& dx)
686  {
687  OPM_TIMEBLOCK(updateDomainSolution);
688  auto& simulator = model_.simulator();
689  auto& newtonMethod = simulator.model().newtonMethod();
690  SolutionVector& solution = simulator.model().solution(/*timeIdx=*/0);
691 
692  newtonMethod.update_(/*nextSolution=*/solution,
693  /*curSolution=*/solution,
694  /*update=*/dx,
695  /*resid=*/dx,
696  domain.cells); // the update routines of the black
697  // oil model do not care about the
698  // residual
699 
700  // if the solution is updated, the intensive quantities need to be recalculated
701  simulator.model().invalidateAndUpdateIntensiveQuantities(/*timeIdx=*/0, domain);
702  }
703 
705  std::pair<Scalar, Scalar> localDomainConvergenceData(const Domain& domain,
706  std::vector<Scalar>& R_sum,
707  std::vector<Scalar>& maxCoeff,
708  std::vector<Scalar>& B_avg,
709  std::vector<int>& maxCoeffCell)
710  {
711  const auto& modelSimulator = model_.simulator();
712 
713  Scalar pvSumLocal = 0.0;
714  Scalar numAquiferPvSumLocal = 0.0;
715  const auto& model = modelSimulator.model();
716  const auto& problem = modelSimulator.problem();
717 
718  const auto& modelResid = modelSimulator.model().linearizer().residual();
719 
720  ElementContext elemCtx(modelSimulator);
721  const auto& gridView = domain.view;
722  const auto& elemEndIt = gridView.template end</*codim=*/0>();
723  IsNumericalAquiferCell isNumericalAquiferCell(gridView.grid());
724 
725  for (auto elemIt = gridView.template begin</*codim=*/0>();
726  elemIt != elemEndIt;
727  ++elemIt)
728  {
729  if (elemIt->partitionType() != Dune::InteriorEntity) {
730  continue;
731  }
732  const auto& elem = *elemIt;
733  elemCtx.updatePrimaryStencil(elem);
734  elemCtx.updatePrimaryIntensiveQuantities(/*timeIdx=*/0);
735 
736  const unsigned cell_idx = elemCtx.globalSpaceIndex(/*spaceIdx=*/0, /*timeIdx=*/0);
737  const auto& intQuants = elemCtx.intensiveQuantities(/*spaceIdx=*/0, /*timeIdx=*/0);
738  const auto& fs = intQuants.fluidState();
739 
740  const auto pvValue = problem.referencePorosity(cell_idx, /*timeIdx=*/0) *
741  model.dofTotalVolume(cell_idx);
742  pvSumLocal += pvValue;
743 
744  if (isNumericalAquiferCell(elem))
745  {
746  numAquiferPvSumLocal += pvValue;
747  }
748 
749  model_.getMaxCoeff(cell_idx, intQuants, fs, modelResid, pvValue,
750  B_avg, R_sum, maxCoeff, maxCoeffCell);
751  }
752 
753  // compute local average in terms of global number of elements
754  const int bSize = B_avg.size();
755  for ( int i = 0; i<bSize; ++i )
756  {
757  B_avg[ i ] /= Scalar(domain.cells.size());
758  }
759 
760  return {pvSumLocal, numAquiferPvSumLocal};
761  }
762 
763  ConvergenceReport getDomainReservoirConvergence(const double reportTime,
764  const double dt,
765  const Domain& domain,
766  DeferredLogger& logger,
767  std::vector<Scalar>& B_avg,
768  std::vector<Scalar>& residual_norms)
769  {
770  using Vector = std::vector<Scalar>;
771 
772  const auto& iterCtx = model_.simulator().problem().iterationContext();
773 
774  const int numComp = numEq;
775  Vector R_sum(numComp, 0.0 );
776  Vector maxCoeff(numComp, std::numeric_limits<Scalar>::lowest() );
777  std::vector<int> maxCoeffCell(numComp, -1);
778  const auto [ pvSum, numAquiferPvSum]
779  = this->localDomainConvergenceData(domain, R_sum, maxCoeff, B_avg, maxCoeffCell);
780 
781  auto cnvErrorPvFraction = computeCnvErrorPvLocal(domain, B_avg, dt);
782  cnvErrorPvFraction /= (pvSum - numAquiferPvSum);
783 
784  // For each iteration, we need to determine whether to use the relaxed tolerances.
785  // To disable the usage of relaxed tolerances, you can set the relaxed tolerances
786  // equal to the strict tolerances.
787  //
788  // If min_strict_mb_iter_ = -1 (default) we use a relaxed tolerance for the mass
789  // balance only on the last local iteration. For non-negative values we use the
790  // relaxed tolerance after the given number of iterations.
791  const int maxLocalIter = model_.param().max_local_solve_iterations_;
792 
793  const bool relax_final_iteration_mb =
794  model_.param().min_strict_mb_iter_ < 0 && iterCtx.iteration() == maxLocalIter;
795 
796  const bool relax_iter_mb =
797  model_.param().min_strict_mb_iter_ >= 0 &&
798  iterCtx.shouldRelax(model_.param().min_strict_mb_iter_);
799 
800  const bool use_relaxed_mb = relax_final_iteration_mb || relax_iter_mb;
801 
802  const Scalar tol_mb = model_.param().local_tolerance_scaling_mb_
803  * (use_relaxed_mb ? model_.param().tolerance_mb_relaxed_ : model_.param().tolerance_mb_);
804 
805  // If min_strict_cnv_iter_ = -1 (default) we use a relaxed tolerance for the CNV
806  // only on the last local iteration. For non-negative values we use the relaxed
807  // tolerance after the given number of iterations. We also use relaxed tolerances
808  // for cells with total pore-volume less than relaxed_max_pv_fraction_.
809  const bool relax_final_iteration_cnv =
810  model_.param().min_strict_cnv_iter_ < 0 && iterCtx.iteration() == maxLocalIter;
811 
812  const bool relax_iter_cnv =
813  model_.param().min_strict_cnv_iter_ >= 0 &&
814  iterCtx.shouldRelax(model_.param().min_strict_cnv_iter_);
815 
816  const bool relax_pv_fraction_cnv =
817  cnvErrorPvFraction < model_.param().relaxed_max_pv_fraction_;
818 
819  const bool use_relaxed_cnv = relax_final_iteration_cnv ||
820  relax_iter_cnv ||
821  relax_pv_fraction_cnv;
822 
823  // Tighter bound for local convergence should increase the
824  // likelyhood of: local convergence => global convergence
825  const Scalar tol_cnv = model_.param().local_tolerance_scaling_cnv_
826  * (use_relaxed_cnv ? model_.param().tolerance_cnv_relaxed_
827  : model_.param().tolerance_cnv_);
828 
829  // TODO: Energy-specific tolerances are not currently applied. A closer analysis
830  // is needed to determine if everything in NLDD is compatible with thermal simulations.
831 
832  // Finish computation
833  std::vector<Scalar> CNV(numComp);
834  std::vector<Scalar> mass_balance_residual(numComp);
835  for (int compIdx = 0; compIdx < numComp; ++compIdx )
836  {
837  CNV[compIdx] = B_avg[compIdx] * dt * maxCoeff[compIdx];
838  mass_balance_residual[compIdx] = std::abs(B_avg[compIdx]*R_sum[compIdx]) * dt / pvSum;
839  residual_norms.push_back(CNV[compIdx]);
840  }
841 
842  // Create convergence report.
843  ConvergenceReport report{reportTime};
844  using CR = ConvergenceReport;
845  for (int compIdx = 0; compIdx < numComp; ++compIdx) {
846  Scalar res[2] = { mass_balance_residual[compIdx], CNV[compIdx] };
847  CR::ReservoirFailure::Type types[2] = { CR::ReservoirFailure::Type::MassBalance,
848  CR::ReservoirFailure::Type::Cnv };
849  Scalar tol[2] = { tol_mb, tol_cnv };
850  model_.addReservoirConvergenceMetrics(
851  report,
852  compIdx,
853  model_.compNames().name(compIdx),
854  std::span<const Scalar>{res},
855  std::span<const CR::ReservoirFailure::Type>{types},
856  std::span<const Scalar>{tol},
857  model_.param().max_residual_allowed_,
858  [&logger](const std::string& message)
859  {
860  logger.debug(message);
861  });
862  }
863 
864  // Output of residuals. If converged at initial state, log nothing.
865  const bool converged_at_initial_state = (report.converged() && iterCtx.iteration() == 0);
866  if (!converged_at_initial_state) {
867  if (iterCtx.iteration() == 0) {
868  // Log header.
869  std::string msg = fmt::format("Domain {} on rank {}, size {}, containing cell {}\n| Iter",
870  domain.index, this->rank_, domain.cells.size(), domain.cells[0]);
871  for (int compIdx = 0; compIdx < numComp; ++compIdx) {
872  msg += " MB(";
873  msg += model_.compNames().name(compIdx)[0];
874  msg += ") ";
875  }
876  for (int compIdx = 0; compIdx < numComp; ++compIdx) {
877  msg += " CNV(";
878  msg += model_.compNames().name(compIdx)[0];
879  msg += ") ";
880  }
881  logger.debug(msg);
882  }
883  // Log convergence data.
884  std::ostringstream ss;
885  ss << "| ";
886  const std::streamsize oprec = ss.precision(3);
887  const std::ios::fmtflags oflags = ss.setf(std::ios::scientific);
888  ss << std::setw(4) << iterCtx.iteration();
889  for (int compIdx = 0; compIdx < numComp; ++compIdx) {
890  ss << std::setw(11) << mass_balance_residual[compIdx];
891  }
892  for (int compIdx = 0; compIdx < numComp; ++compIdx) {
893  ss << std::setw(11) << CNV[compIdx];
894  }
895  ss.precision(oprec);
896  ss.flags(oflags);
897  logger.debug(ss.str());
898  }
899 
900  return report;
901  }
902 
903  ConvergenceReport getDomainConvergence(const Domain& domain,
904  const SimulatorTimerInterface& timer,
905  DeferredLogger& logger,
906  std::vector<Scalar>& residual_norms)
907  {
908  OPM_TIMEBLOCK(getDomainConvergence);
909  std::vector<Scalar> B_avg(numEq, 0.0);
910  auto report = this->getDomainReservoirConvergence(timer.simulationTimeElapsed(),
911  timer.currentStepLength(),
912  domain,
913  logger,
914  B_avg,
915  residual_norms);
916  report += wellModel_.getWellConvergence(domain, B_avg, logger);
917  return report;
918  }
919 
921  std::vector<int> getSubdomainOrder()
922  {
923  const auto& modelSimulator = model_.simulator();
924  const auto& solution = modelSimulator.model().solution(0);
925 
926  std::vector<int> domain_order(domains_.size());
927  std::iota(domain_order.begin(), domain_order.end(), 0);
928 
929  if (model_.param().local_solve_approach_ == DomainSolveApproach::Jacobi) {
930  // Do nothing, 0..n-1 order is fine.
931  return domain_order;
932  } else if (model_.param().local_solve_approach_ == DomainSolveApproach::GaussSeidel) {
933  // Calculate the measure used to order the domains.
934  std::vector<Scalar> measure_per_domain(domains_.size());
935  switch (model_.param().local_domains_ordering_) {
936  case DomainOrderingMeasure::AveragePressure: {
937  // Use average pressures to order domains.
938  for (const auto& domain : domains_) {
939  const Scalar press_sum =
940  std::accumulate(domain.cells.begin(), domain.cells.end(), Scalar{0},
941  [&solution](const auto acc, const auto c)
942  { return acc + solution[c][Indices::pressureSwitchIdx]; });
943  const Scalar avgpress = press_sum / domain.cells.size();
944  measure_per_domain[domain.index] = avgpress;
945  }
946  break;
947  }
948  case DomainOrderingMeasure::MaxPressure: {
949  // Use max pressures to order domains.
950  for (const auto& domain : domains_) {
951  measure_per_domain[domain.index] =
952  std::accumulate(domain.cells.begin(), domain.cells.end(), Scalar{0},
953  [&solution](const auto acc, const auto c)
954  { return std::max(acc, solution[c][Indices::pressureSwitchIdx]); });
955  }
956  break;
957  }
958  case DomainOrderingMeasure::Residual: {
959  // Use maximum residual to order domains.
960  const auto& residual = modelSimulator.model().linearizer().residual();
961  const int num_vars = residual[0].size();
962  for (const auto& domain : domains_) {
963  Scalar maxres = 0.0;
964  for (const int c : domain.cells) {
965  for (int ii = 0; ii < num_vars; ++ii) {
966  maxres = std::max(maxres, std::fabs(residual[c][ii]));
967  }
968  }
969  measure_per_domain[domain.index] = maxres;
970  }
971  break;
972  }
973  } // end of switch (model_.param().local_domains_ordering_)
974 
975  // Sort by largest measure, keeping index order if equal.
976  const auto& m = measure_per_domain;
977  std::stable_sort(domain_order.begin(), domain_order.end(),
978  [&m](const int i1, const int i2){ return m[i1] > m[i2]; });
979  return domain_order;
980  } else {
981  throw std::logic_error("Domain solve approach must be Jacobi or Gauss-Seidel");
982  }
983  }
984 
985  template<class GlobalEqVector>
986  void solveDomainJacobi(GlobalEqVector& solution,
987  GlobalEqVector& locally_solved,
988  SimulatorReportSingle& local_report,
989  DeferredLogger& logger,
990  const SimulatorTimerInterface& timer,
991  const Domain& domain)
992  {
993  auto initial_local_well_primary_vars = wellModel_.getPrimaryVarsDomain(domain.index);
994  auto initial_local_solution = Details::extractVector(solution, domain.cells);
995  auto convrep = solveDomain(domain, timer, local_report, logger, false);
996  if (local_report.converged) {
997  auto local_solution = Details::extractVector(solution, domain.cells);
998  Details::setGlobal(local_solution, domain.cells, locally_solved);
999  Details::setGlobal(initial_local_solution, domain.cells, solution);
1000  model_.simulator().model().invalidateAndUpdateIntensiveQuantities(/*timeIdx=*/0, domain);
1001  } else {
1002  wellModel_.setPrimaryVarsDomain(domain.index, initial_local_well_primary_vars);
1003  Details::setGlobal(initial_local_solution, domain.cells, solution);
1004  model_.simulator().model().invalidateAndUpdateIntensiveQuantities(/*timeIdx=*/0, domain);
1005  }
1006  }
1007 
1008  template<class GlobalEqVector>
1009  void solveDomainGaussSeidel(GlobalEqVector& solution,
1010  GlobalEqVector& locally_solved,
1011  SimulatorReportSingle& local_report,
1012  DeferredLogger& logger,
1013  const SimulatorTimerInterface& timer,
1014  const Domain& domain)
1015  {
1016  auto initial_local_well_primary_vars = wellModel_.getPrimaryVarsDomain(domain.index);
1017  auto initial_local_solution = Details::extractVector(solution, domain.cells);
1018  auto convrep = solveDomain(domain, timer, local_report, logger, true);
1019  if (!local_report.converged) {
1020  // We look at the detailed convergence report to evaluate
1021  // if we should accept the unconverged solution.
1022  // We do not accept a solution if the wells are unconverged.
1023  if (!convrep.wellFailed()) {
1024  // Calculare the sums of the mb and cnv failures.
1025  Scalar mb_sum = 0.0;
1026  Scalar cnv_sum = 0.0;
1027  for (const auto& rc : convrep.reservoirConvergence()) {
1028  if (rc.type() == ConvergenceReport::ReservoirFailure::Type::MassBalance) {
1029  mb_sum += rc.value();
1030  } else if (rc.type() == ConvergenceReport::ReservoirFailure::Type::Cnv) {
1031  cnv_sum += rc.value();
1032  }
1033  }
1034  // If not too high, we overrule the convergence failure.
1035  const Scalar acceptable_local_mb_sum = 1e-3;
1036  const Scalar acceptable_local_cnv_sum = 1.0;
1037  if (mb_sum < acceptable_local_mb_sum && cnv_sum < acceptable_local_cnv_sum) {
1038  local_report.converged = true;
1039  local_report.accepted_unconverged_domains += 1;
1040  logger.debug(fmt::format("Accepting solution in unconverged domain {} on rank {}.", domain.index, rank_));
1041  logger.debug(fmt::format("Value of mb_sum: {} cnv_sum: {}", mb_sum, cnv_sum));
1042  } else {
1043  logger.debug("Unconverged local solution.");
1044  }
1045  } else {
1046  logger.debug("Unconverged local solution with well convergence failures:");
1047  for (const auto& wf : convrep.wellFailures()) {
1048  logger.debug(to_string(wf));
1049  }
1050  }
1051  }
1052  if (local_report.converged) {
1053  local_report.converged_domains += 1;
1054  auto local_solution = Details::extractVector(solution, domain.cells);
1055  Details::setGlobal(local_solution, domain.cells, locally_solved);
1056  } else {
1057  local_report.unconverged_domains += 1;
1058  wellModel_.setPrimaryVarsDomain(domain.index, initial_local_well_primary_vars);
1059  Details::setGlobal(initial_local_solution, domain.cells, solution);
1060  model_.simulator().model().invalidateAndUpdateIntensiveQuantities(/*timeIdx=*/0, domain);
1061  }
1062  }
1063 
1064  Scalar computeCnvErrorPvLocal(const Domain& domain,
1065  const std::vector<Scalar>& B_avg, double dt) const
1066  {
1067  Scalar errorPV{};
1068  const auto& simulator = model_.simulator();
1069  const auto& model = simulator.model();
1070  const auto& problem = simulator.problem();
1071  const auto& residual = simulator.model().linearizer().residual();
1072 
1073  for (const int cell_idx : domain.cells) {
1074  const Scalar pvValue = problem.referencePorosity(cell_idx, /*timeIdx=*/0) *
1075  model.dofTotalVolume(cell_idx);
1076  const auto& cellResidual = residual[cell_idx];
1077  bool cnvViolated = false;
1078 
1079  for (unsigned eqIdx = 0; eqIdx < cellResidual.size(); ++eqIdx) {
1080  using std::fabs;
1081  Scalar CNV = cellResidual[eqIdx] * dt * B_avg[eqIdx] / pvValue;
1082  cnvViolated = cnvViolated || (fabs(CNV) > model_.param().tolerance_cnv_);
1083  }
1084 
1085  if (cnvViolated) {
1086  errorPV += pvValue;
1087  }
1088  }
1089  return errorPV;
1090  }
1091 
1092  decltype(auto) partitionCells() const
1093  {
1094  const auto& grid = this->model_.simulator().vanguard().grid();
1095 
1096  using GridView = std::remove_cv_t<std::remove_reference_t<
1097  decltype(grid.leafGridView())>>;
1098 
1099  using Element = std::remove_cv_t<std::remove_reference_t<
1100  typename GridView::template Codim<0>::Entity>>;
1101 
1102  const auto& param = this->model_.param();
1103 
1104  auto zoltan_ctrl = ZoltanPartitioningControl<Element>{};
1105 
1106  zoltan_ctrl.domain_imbalance = param.local_domains_partition_imbalance_;
1107 
1108  zoltan_ctrl.index =
1109  [elementMapper = &this->model_.simulator().model().elementMapper()]
1110  (const Element& element)
1111  {
1112  return elementMapper->index(element);
1113  };
1114 
1115  zoltan_ctrl.local_to_global =
1116  [cartMapper = &this->model_.simulator().vanguard().cartesianIndexMapper()]
1117  (const int elemIdx)
1118  {
1119  return cartMapper->cartesianIndex(elemIdx);
1120  };
1121 
1122  // Forming the list of wells is expensive, so do this only if needed.
1123  const auto need_wells = param.local_domains_partition_method_ == "zoltan";
1124 
1125  const auto wells = need_wells
1126  ? this->model_.simulator().vanguard().schedule().getWellsatEnd()
1127  : std::vector<Well>{};
1128 
1129  const auto& possibleFutureConnectionSet = need_wells
1130  ? this->model_.simulator().vanguard().schedule().getPossibleFutureConnections()
1131  : std::unordered_map<std::string, std::set<int>> {};
1132 
1133  // If defaulted parameter for number of domains, choose a reasonable default.
1134  constexpr int default_cells_per_domain = 1000;
1135  const int num_domains = (param.num_local_domains_ > 0)
1136  ? param.num_local_domains_
1137  : detail::countGlobalCells(grid) / default_cells_per_domain;
1138  return ::Opm::partitionCells(param.local_domains_partition_method_,
1139  num_domains, grid.leafGridView(), wells,
1140  possibleFutureConnectionSet, zoltan_ctrl,
1141  param.local_domains_partition_well_neighbor_levels_);
1142  }
1143 
1144  void updateMobilities(const Domain& domain)
1145  {
1146  if (domain.skip || model_.param().nldd_relative_mobility_change_tol_ == 0.0) {
1147  return;
1148  }
1149  const auto numActivePhases = FluidSystem::numActivePhases();
1150  for (const auto globalDofIdx : domain.cells) {
1151  const auto& intQuants = model_.simulator().model().intensiveQuantities(globalDofIdx, /* time_idx = */ 0);
1152 
1153  for (unsigned activePhaseIdx = 0; activePhaseIdx < numActivePhases; ++activePhaseIdx) {
1154  const auto phaseIdx = FluidSystem::activeToCanonicalPhaseIdx(activePhaseIdx);
1155  const auto mobIdx = globalDofIdx * numActivePhases + activePhaseIdx;
1156  previousMobilities_[mobIdx] = getValue(intQuants.mobility(phaseIdx));
1157  }
1158  }
1159  }
1160 
1161  bool checkIfSubdomainNeedsSolving(const Domain& domain)
1162  {
1163  if (domain.skip) {
1164  return false;
1165  }
1166 
1167  // If we domain was marked as needing solving in previous iterations,
1168  // we do not need to check again
1169  if (domain_needs_solving_[domain.index]) {
1170  return true;
1171  }
1172 
1173  // If we do not check for mobility changes, we need to solve the domain
1174  if (model_.param().nldd_relative_mobility_change_tol_ == 0.0) {
1175  return true;
1176  }
1177 
1178  // Skip mobility check on first global iteration (no previous mobilities to compare)
1179  if (model_.simulator().problem().iterationContext().isFirstGlobalIteration()) {
1180  return true;
1181  }
1182 
1183  return checkSubdomainChangeRelative(domain);
1184  }
1185 
1186  bool checkSubdomainChangeRelative(const Domain& domain)
1187  {
1188  const auto numActivePhases = FluidSystem::numActivePhases();
1189 
1190  // Check mobility changes for all cells in the domain
1191  for (const auto globalDofIdx : domain.cells) {
1192  const auto& intQuants = model_.simulator().model().intensiveQuantities(globalDofIdx, /* time_idx = */ 0);
1193 
1194  // Calculate average previous mobility for normalization
1195  Scalar cellMob = 0.0;
1196  for (unsigned activePhaseIdx = 0; activePhaseIdx < numActivePhases; ++activePhaseIdx) {
1197  const auto mobIdx = globalDofIdx * numActivePhases + activePhaseIdx;
1198  cellMob += previousMobilities_[mobIdx] / numActivePhases;
1199  }
1200 
1201  // Check relative changes for each phase
1202  for (unsigned activePhaseIdx = 0; activePhaseIdx < numActivePhases; ++activePhaseIdx) {
1203  const auto phaseIdx = FluidSystem::activeToCanonicalPhaseIdx(activePhaseIdx);
1204  const auto mobIdx = globalDofIdx * numActivePhases + activePhaseIdx;
1205  const auto mobility = getValue(intQuants.mobility(phaseIdx));
1206  const auto relDiff = std::abs(mobility - previousMobilities_[mobIdx]) / cellMob;
1207  if (relDiff > model_.param().nldd_relative_mobility_change_tol_) {
1208  return true;
1209  }
1210  }
1211  }
1212  return false;
1213  }
1214 
1215  NonlinearSystemBlackOilReservoir<TypeTag>& model_;
1216  BlackoilWellModelNldd<TypeTag> wellModel_;
1217  std::vector<Domain> domains_;
1218  std::vector<std::unique_ptr<Mat>> domain_matrices_;
1219  std::vector<ISTLSolverType> domain_linsolvers_;
1220  SimulatorReport local_reports_accumulated_;
1221  // mutable because we need to update the number of wells for each domain in getDomainAccumulatedReports()
1222  mutable std::vector<SimulatorReport> domain_reports_accumulated_;
1223  int rank_ = 0;
1224  // Store previous mobilities to check for changes - single flat vector indexed by (globalCellIdx * numActivePhases + activePhaseIdx)
1225  std::vector<Scalar> previousMobilities_;
1226  // Flag indicating if this domain should be solved in the next iteration
1227  std::vector<bool> domain_needs_solving_;
1228 };
1229 
1230 } // namespace Opm
1231 
1232 #endif // OPM_NONLINEAR_SYSTEM_NLDD_HEADER_INCLUDED
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:233
Representing a part of a grid, in a way suitable for performing local solves.
Definition: SubDomain.hpp:84
void writeNonlinearIterationsPerCell(const std::filesystem::path &odir) const
Write the number of nonlinear iterations per cell to a file in ResInsight compatible format...
Definition: NonlinearSystemNldd.hpp:464
This class solves the fully implicit black-oil system by solving the reduced system (after eliminatin...
Definition: FlowLinearSolverParameters.hpp:40
void writePartitions(const std::filesystem::path &odir) const
Write the partition vector to a file in ResInsight compatible format for inspection and a partition f...
Definition: NonlinearSystemNldd.hpp:449
Structs needed for tpfalinearizer and its gpuparams struct extracted to be defined in one place that ...
Definition: blackoilbioeffectsmodules.hh:45
Solver parameters for the NonlinearSystemBlackOilReservoir.
Definition: BlackoilModelParameters.hpp:200
RAII guard for NLDD domain-local iteration context.
Definition: NewtonIterationContext.hpp:152
Definition: DeferredLogger.hpp:56
A model implementation for three-phase black oil.
Definition: NonlinearSystemBlackOilReservoir.hpp:62
A struct for returning timing data from a simulator to its caller.
Definition: SimulatorReport.hpp:33
Interface class for SimulatorTimer objects, to be improved.
Definition: SimulatorTimerInterface.hpp:33
const SimulatorReport & localAccumulatedReports() const
return the statistics of local solves accumulated for this rank
Definition: NonlinearSystemNldd.hpp:425
void logMessages()
Log all messages to the OpmLog backends, and clear the message container.
Definition: DeferredLogger.cpp:92
This class carries all parameters for the NewtonIterationBlackoilInterleaved class.
Definition: FlowLinearSolverParameters.hpp:97
Opm::DeferredLogger gatherDeferredLogger(const Opm::DeferredLogger &local_deferredlogger, Opm::Parallel::Communication)
Create a global log combining local logs.
Definition: gatherDeferredLogger.cpp:205
Represents the convergence status of the whole simulator, to make it possible to query and store the ...
Definition: ConvergenceReport.hpp:37
const std::vector< SimulatorReport > & domainAccumulatedReports() const
return the statistics of local solves accumulated for each domain on this rank
Definition: NonlinearSystemNldd.hpp:431
Definition: SimulatorReport.hpp:121
A NLDD implementation used by the reservoir nonlinear system.
Definition: NonlinearSystemNldd.hpp:84
NonlinearSystemNldd(NonlinearSystemBlackOilReservoir< TypeTag > &model)
The constructor sets up the subdomains.
Definition: NonlinearSystemNldd.hpp:104
SimulatorReportSingle nonlinearIterationNldd(const SimulatorTimerInterface &timer, NonlinearSolverType &nonlinear_solver)
Do one non-linear NLDD iteration.
Definition: NonlinearSystemNldd.hpp:238
void prepareStep()
Called before starting a time step.
Definition: NonlinearSystemNldd.hpp:230