90 std::vector<GlobalVector*>
const& x,
91 std::vector<GlobalVector*>
const& x_prev,
int const process_id)
93 if (!_compensate_non_equilibrium_initial_residuum)
98 INFO(
"Calculate non-equilibrium initial residuum.");
102 _equation_system->assemble(x, x_prev, process_id);
103 _equation_system->getA(A);
104 _equation_system->getRhs(*x_prev[process_id], rhs);
114 const auto selected_global_indices =
115 _equation_system->getIndicesOfResiduumWithoutInitialCompensation();
117 std::vector<double> zero_entries(selected_global_indices.size(), 0.0);
118 _r_neq->set(selected_global_indices, zero_entries);
127 std::vector<GlobalVector*>& x,
128 std::vector<GlobalVector*>
const& x_prev,
129 std::function<
void(
int, std::vector<GlobalVector*>
const&)>
const&
130 postIterationCallback,
131 int const process_id)
134 auto& sys = *_equation_system;
139 std::vector<GlobalVector*> x_new{x};
144 bool error_norms_met =
false;
146 _convergence_criterion->preFirstIteration();
149 for (; iteration <= _maxiter; ++iteration, _convergence_criterion->reset())
152 double time_dirichlet = 0.0;
155 time_iteration.
start();
157 timer_dirichlet.
start();
158 auto& x_new_process = *x_new[process_id];
160 sys.computeKnownSolutions(x_new_process, process_id);
161 sys.applyKnownSolutions(x_new_process);
162 time_dirichlet += timer_dirichlet.
elapsed();
164 sys.preIteration(iteration, x_new_process);
167 time_assembly.
start();
168 sys.assemble(x_new, x_prev, process_id);
170 sys.getRhs(*x_prev[process_id], rhs);
173 if (sys.requiresNormalization())
174 sys.getAandRhsNormalized(A, rhs);
176 INFO(
"[time] Assembly took {:g} s.", time_assembly.
elapsed());
179 if (_r_neq !=
nullptr)
184 timer_dirichlet.
start();
185 sys.applyKnownSolutionsPicard(A, rhs, x_new_process);
186 time_dirichlet += timer_dirichlet.
elapsed();
187 INFO(
"[time] Applying Dirichlet BCs took {:g} s.", time_dirichlet);
189 if (!sys.isLinear() && _convergence_criterion->hasResidualCheck())
194 _convergence_criterion->checkResidual(res);
197 bool iteration_succeeded =
199 sys.linearSolverNeedsToCompute());
201 if (iteration_succeeded)
203 if (postIterationCallback)
205 postIterationCallback(iteration, x_new);
208 switch (sys.postIteration(x_new_process))
215 ERR(
"Picard: The postIteration() hook reported a "
216 "non-recoverable error.");
217 iteration_succeeded =
false;
225 "Picard: The postIteration() hook decided that this "
226 "iteration has to be repeated.");
234 if (!iteration_succeeded)
237 error_norms_met =
false;
243 error_norms_met =
true;
247 if (_convergence_criterion->hasDeltaXCheck())
252 _convergence_criterion->checkDeltaX(minus_delta_x,
256 error_norms_met = _convergence_criterion->isSatisfied();
262 INFO(
"[time] Iteration #{:d} took {:g} s.", iteration,
272 if (iteration >= _maxiter)
278 if (iteration > _maxiter)
280 ERR(
"Picard: Could not solve the given nonlinear system within {:d} "
289 return {error_norms_met, iteration};
294 std::vector<GlobalVector*>
const& x,
295 std::vector<GlobalVector*>
const& x_prev,
int const process_id)
297 if (!_compensate_non_equilibrium_initial_residuum)
302 INFO(
"Calculate non-equilibrium initial residuum.");
304 _equation_system->assemble(x, x_prev, process_id);
306 _equation_system->getResidual(*x[process_id], *x_prev[process_id], *_r_neq);
311 const auto selected_global_indices =
312 _equation_system->getIndicesOfResiduumWithoutInitialCompensation();
313 std::vector<double> zero_entries(selected_global_indices.size(), 0.0);
315 _r_neq->set(selected_global_indices, zero_entries);
321 std::vector<GlobalVector*>& x,
322 std::vector<GlobalVector*>
const& x_prev,
323 std::function<
void(
int, std::vector<GlobalVector*>
const&)>
const&
324 postIterationCallback,
325 int const process_id)
328 auto& sys = *_equation_system;
331 auto& minus_delta_x =
335 bool error_norms_met =
false;
341 _convergence_criterion->preFirstIteration();
344 for (; iteration <= _maxiter; ++iteration, _convergence_criterion->reset())
347 double time_dirichlet = 0.0;
350 time_iteration.
start();
352 timer_dirichlet.
start();
353 sys.computeKnownSolutions(*x[process_id], process_id);
354 time_dirichlet += timer_dirichlet.
elapsed();
356 sys.preIteration(iteration, *x[process_id]);
359 time_assembly.
start();
360 int mpi_rank_assembly_ok = 1;
361 int mpi_all_assembly_ok = 0;
364 sys.assemble(x, x_prev, process_id);
368 ERR(
"Abort nonlinear iteration. Repeating timestep. Reason: {:s}",
370 error_norms_met =
false;
371 iteration = _maxiter;
372 mpi_rank_assembly_ok = 0;
375 if (mpi_all_assembly_ok == 0)
379 sys.getResidual(*x[process_id], *x_prev[process_id], res);
381 INFO(
"[time] Assembly took {:g} s.", time_assembly.
elapsed());
384 if (_r_neq !=
nullptr)
387 minus_delta_x.setZero();
389 timer_dirichlet.
start();
390 sys.applyKnownSolutionsNewton(J, res, *x[process_id], minus_delta_x);
391 time_dirichlet += timer_dirichlet.
elapsed();
392 INFO(
"[time] Applying Dirichlet BCs took {:g} s.", time_dirichlet);
394 if (!sys.isLinear() && _convergence_criterion->hasResidualCheck())
396 _convergence_criterion->checkResidual(res);
400 time_linear_solver.
start();
401 bool iteration_succeeded = _linear_solver.solve(J, res, minus_delta_x);
402 INFO(
"[time] Linear solver took {:g} s.", time_linear_solver.
elapsed());
404 if (!iteration_succeeded)
406 ERR(
"Newton: The linear solver failed.");
415 std::vector<GlobalVector*> x_new{x};
418 *x[process_id], _x_new_id);
419 LinAlg::axpy(*x_new[process_id], -_damping, minus_delta_x);
421 if (postIterationCallback)
423 postIterationCallback(iteration, x_new);
426 switch (sys.postIteration(*x_new[process_id]))
431 ERR(
"Newton: The postIteration() hook reported a "
432 "non-recoverable error.");
433 iteration_succeeded =
false;
437 "Newton: The postIteration() hook decided that this "
438 "iteration has to be repeated.");
451 if (!iteration_succeeded)
454 error_norms_met =
false;
460 error_norms_met =
true;
464 if (_convergence_criterion->hasDeltaXCheck())
467 _convergence_criterion->checkDeltaX(minus_delta_x,
471 error_norms_met = _convergence_criterion->isSatisfied();
474 INFO(
"[time] Iteration #{:d} took {:g} s.", iteration,
484 if (iteration >= _maxiter)
490 if (iteration > _maxiter)
492 ERR(
"Newton: Could not solve the given nonlinear system within {:d} "
501 return {error_norms_met, iteration};
513 if (type ==
"Picard")
517 return std::make_pair(
518 std::make_unique<ConcreteNLS>(linear_solver, max_iter), tag);
520 if (type ==
"Newton")
527 "The damping factor for the Newon method must be positive, got "
533 return std::make_pair(
534 std::make_unique<ConcreteNLS>(linear_solver, max_iter, damping),
538 if (boost::iequals(type,
"PETScSNES"))
545 return std::make_pair(std::make_unique<ConcreteNLS>(
546 linear_solver, max_iter, std::move(prefix)),
551 OGS_FATAL(
"Unsupported nonlinear solver type '{:s}'.", type.c_str());