OGS
NumLib::NonlinearSolver< NonlinearSolverTag::Picard > Class Referencefinal

Detailed Description

Find a solution to a nonlinear equation using the Picard fixpoint iteration method.

Notation used throughout this class and its documentation: \( x_k \) is the current iterate and \( g \) is the Picard fixpoint map, i.e. \( g(x_k) \) is the solution of the linearized system \( A(x_k)\,x = b(x_k) \) (the result of one linear solve). A converged iteration is a fixpoint \( x = g(x) \), and one iteration reads \( x_{k+1} = g(x_k) \).

With an under-relaxation (damping) coefficient \( \beta \in (0, 1] \) the iteration becomes \( x_{k+1} = (1-\beta)\,x_k + \beta\,g(x_k) \); the value \( \beta = 1 \) recovers the plain Picard update above. Damping is applied to every step, independently of whether Anderson acceleration is enabled.

For an Anderson depth \( m \ge 2 \) the damped steps of the last \( m \) iterations are additionally mixed, see AndersonAcceleration.h.

Definition at line 190 of file NonlinearSolver.h.

#include <NonlinearSolver.h>

Inheritance diagram for NumLib::NonlinearSolver< NonlinearSolverTag::Picard >:
[legend]
Collaboration diagram for NumLib::NonlinearSolver< NonlinearSolverTag::Picard >:
[legend]

Public Types

using System = NonlinearSystem<NonlinearSolverTag::Picard>
 Type of the nonlinear equation system to be solved.

Public Member Functions

 NonlinearSolver (GlobalLinearSolver &linear_solver, int const maxiter, int const anderson_depth, double const damping)
 ~NonlinearSolver ()
void setEquationSystem (System &eq, ConvergenceCriterion &conv_crit)
void calculateNonEquilibriumInitialResiduum (std::vector< GlobalVector * > const &x, std::vector< GlobalVector * > const &x_prev, int const process_id) override
NonlinearSolverStatus solve (std::vector< GlobalVector * > &x, std::vector< GlobalVector * > const &x_prev, std::function< void(int, std::vector< GlobalVector * > const &)> const &postIterationCallback, int const process_id) override
void compensateNonEquilibriumInitialResiduum (bool const value)
Public Member Functions inherited from NumLib::NonlinearSolverBase
virtual ~NonlinearSolverBase ()=default

Private Attributes

GlobalLinearSolver & _linear_solver
System * _equation_system = nullptr
ConvergenceCriterion * _convergence_criterion = nullptr
double const _damping
int const _anderson_depth
int const _maxiter
 maximum number of iterations
GlobalVector * _r_neq = nullptr
 non-equilibrium initial residuum.
std::size_t _A_id = 0u
 ID of the \( A \) matrix.
std::size_t _rhs_id = 0u
 ID of the right-hand side vector.
std::size_t _x_new_id = 0u
std::size_t _r_neq_id = 0u
bool _compensate_non_equilibrium_initial_residuum = false

Member Typedef Documentation

◆ System

Type of the nonlinear equation system to be solved.

Definition at line 195 of file NonlinearSolver.h.

Constructor & Destructor Documentation

◆ NonlinearSolver()

NumLib::NonlinearSolver< NonlinearSolverTag::Picard >::NonlinearSolver ( GlobalLinearSolver & linear_solver,
int const maxiter,
int const anderson_depth,
double const damping )
inlineexplicit

Constructs a new instance.

Parameters
linear_solverthe linear solver used by this nonlinear solver.
maxiterthe maximum number of iterations used to solve the equation.
anderson_depththe number of previous iterates to use in Anderson acceleration; 0 and 1 both give a plain Picard iteration, mixing starts at 2.
dampingunder-relaxation coefficient \( \beta \in (0, 1] \) applied to the Picard update (see class description); \( \beta = 1 \) disables damping.

Definition at line 209 of file NonlinearSolver.h.

References _anderson_depth, _damping, _linear_solver, and _maxiter.

Referenced by ~NonlinearSolver(), calculateNonEquilibriumInitialResiduum(), and solve().

◆ ~NonlinearSolver()

Definition at line 672 of file NonlinearSolver.cpp.

673{
674 if (_r_neq != nullptr)
675 {
677 }
678}
GlobalVector * _r_neq
non-equilibrium initial residuum.

References NonlinearSolver(), _r_neq, and NumLib::GlobalVectorProvider::provider.

Member Function Documentation

◆ calculateNonEquilibriumInitialResiduum()

void NumLib::NonlinearSolver< NonlinearSolverTag::Picard >::calculateNonEquilibriumInitialResiduum ( std::vector< GlobalVector * > const & x,
std::vector< GlobalVector * > const & x_prev,
int const process_id )
overridevirtual

Implements NumLib::NonlinearSolverBase.

Definition at line 114 of file NonlinearSolver.cpp.

118{
120 {
121 return;
122 }
123
124 INFO("Calculate non-equilibrium initial residuum.");
125
128 _equation_system->assemble(x, x_prev, process_id);
129 _equation_system->getA(A);
131
132 // r_neq = A * x - rhs
135 MathLib::LinAlg::axpy(*_r_neq, -1.0, rhs); // res -= rhs
136
137 // Set the values of the selected entries of _r_neq, which are associated
138 // with the equations that do not need initial residual compensation, to
139 // zero.
141 _equation_system->getIndicesOfResiduumWithoutInitialCompensation();
142
143#ifdef USE_PETSC
144 // Ghost entry with global index 0 is encoded as -global_size
145 // After abs(), it appears as global_size and must be converted back to 0
146 auto const global_size = _r_neq->size();
147 for (auto& idx : selected_global_indices)
148 {
149 if (idx == global_size)
150 {
151 idx = 0;
152 }
153 }
154#endif
155
158 _equation_system->setReleaseNodalForces(_r_neq, process_id);
159
161
164}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
std::size_t _rhs_id
ID of the right-hand side vector.
void finalizeAssembly(PETScMatrix &A)
Definition LinAlg.cpp:200
void matMult(PETScMatrix const &A, PETScVector const &x, PETScVector &y)
Definition LinAlg.cpp:151
void axpy(PETScVector &y, PetscScalar const a, PETScVector const &x)
Definition LinAlg.cpp:50

References NonlinearSolver(), _A_id, _compensate_non_equilibrium_initial_residuum, _equation_system, _r_neq, _r_neq_id, _rhs_id, MathLib::LinAlg::axpy(), MathLib::LinAlg::finalizeAssembly(), INFO(), MathLib::LinAlg::matMult(), NumLib::GlobalMatrixProvider::provider, and NumLib::GlobalVectorProvider::provider.

◆ compensateNonEquilibriumInitialResiduum()

void NumLib::NonlinearSolver< NonlinearSolverTag::Picard >::compensateNonEquilibriumInitialResiduum ( bool const value)
inline

◆ setEquationSystem()

void NumLib::NonlinearSolver< NonlinearSolverTag::Picard >::setEquationSystem ( System & eq,
ConvergenceCriterion & conv_crit )
inline

Set the nonlinear equation system that will be solved. TODO doc

Definition at line 223 of file NonlinearSolver.h.

References _convergence_criterion, and _equation_system.

◆ solve()

NonlinearSolverStatus NumLib::NonlinearSolver< NonlinearSolverTag::Picard >::solve ( std::vector< GlobalVector * > & x,
std::vector< GlobalVector * > const & x_prev,
std::function< void(int, std::vector< GlobalVector * > const &)> const & postIterationCallback,
int const process_id )
overridevirtual

Assemble and solve the equation system.

Parameters
xin: the initial guess, out: the solution.
x_prevprevious time step solution.
postIterationCallbackcalled after each iteration if set.
process_idusually used in staggered schemes.
Return values
trueif the equation system could be solved
falseotherwise

Implements NumLib::NonlinearSolverBase.

Definition at line 166 of file NonlinearSolver.cpp.

172{
173 namespace LinAlg = MathLib::LinAlg;
174 auto& sys = *_equation_system;
175
176 if ((_damping != 1.0 ||
178 sys.isLinear())
179 {
180 OGS_FATAL(
181 "Damping (under-relaxation) and Anderson acceleration are not "
182 "compatible with a linear equation system: a single Picard step "
183 "already yields the exact solution, so the mixed/damped iterate "
184 "would be accepted as converged but wrong. Remove the 'damping' "
185 "parameter and the 'anderson' subtree for linear problems.");
186 }
187
190
194 LinAlg::copy(*x[process_id], *x_new[process_id]); // set initial guess
195
196 bool error_norms_met = false;
197
198 _convergence_criterion->preFirstIteration();
199
200 // Anderson acceleration of the damped Picard step. Inert for
201 // _anderson_depth below 2 (plain Picard). With beta = _damping = 1 the
202 // stored step reduces to the plain residual g(x) - x; for beta < 1 every
203 // stored step is scaled by beta, which leaves the mixing weights unchanged
204 // (beta cancels).
206
207 int iteration = 1;
208 for (; iteration <= _maxiter; ++iteration, _convergence_criterion->reset())
209 {
211 double time_dirichlet = 0.0;
212
214 time_iteration.start();
215
216 INFO("Iteration #{:d} started.", iteration);
217 timer_dirichlet.start();
220 sys.computeKnownSolutions(x_new_process, process_id);
221 sys.applyKnownSolutions(x_new_process);
222 time_dirichlet += timer_dirichlet.elapsed();
223
224 sys.preIteration(iteration, x_new_process);
225
227 time_assembly.start();
228 if (!assembledOnAllRanks([&]
229 { sys.assemble(x_new, x_prev, process_id); }))
230 {
231 error_norms_met = false;
232 break;
233 }
234 sys.getA(A);
235 sys.getRhs(*x_prev[process_id], rhs);
236
237 // Normalize the linear equation system, if required
238 if (sys.requiresNormalization() &&
239 !_linear_solver.canSolveRectangular())
240 {
241 sys.getAandRhsNormalized(A, rhs);
242 WARN(
243 "The equation system is rectangular, but the current linear "
244 "solver only supports square systems. "
245 "The system will be normalized, which lead to a squared "
246 "condition number and potential numerical issues. "
247 "It is recommended to use a solver that supports rectangular "
248 "equation systems for better numerical stability.");
249 }
250
251 INFO("[time] Assembly took {:g} s.", time_assembly.elapsed());
252
253 // Subtract non-equilibrium initial residuum if set
254 if (_r_neq != nullptr)
255 {
256 LinAlg::axpy(rhs, -1, *_r_neq);
257 }
258
259 auto const solver_needs_to_compute = sys.linearSolverNeedsToCompute();
260 bool const solver_will_compute =
262
263 timer_dirichlet.start();
264 sys.applyKnownSolutionsPicard(
270 time_dirichlet += timer_dirichlet.elapsed();
271 INFO("[time] Applying Dirichlet BCs took {:g} s.", time_dirichlet);
272
273 if (!sys.isLinear() && _convergence_criterion->hasResidualCheck())
274 {
276 {
277 // !solver_will_compute means that the Dirichlet BC application
278 // is incomplete (i.e., A not properly modified) and the
279 // computed residual is wrong.
280 OGS_FATAL(
281 "Logic error. The solver skips the compute step for a "
282 "non-linear equation system.");
283 }
285 LinAlg::matMult(A, x_new_process, res); // res = A * x_new
286 LinAlg::axpy(res, -1.0, rhs); // res -= rhs
287 _convergence_criterion->checkResidual(res);
288 }
289
292
294 {
295 // x_old = x[process_id] (iterate entering this step)
296 // x_new_process (raw Picard output g(x_old))
297 // beta relaxation (always active when damping != 1):
298 // x_new = x_old + beta*(g(x_old) - x_old)
299 // = (1-beta)*x_old + beta*g(x_old)
300 if (_damping != 1.0)
301 {
304 }
305 // Anderson acceleration additionally mixes the last damped steps
306 // f_i = beta*(g(x_i) - x_i) (the difference x_new - x_old after
307 // the beta relaxation above); inert when anderson_depth < 2, in
308 // which case only the beta relaxation applies.
309 anderson.accelerate(*x[process_id], x_new_process);
310
312 {
314 }
315
316 switch (sys.postIteration(x_new_process))
317 {
319 // Don't copy here. The old x might still be used further
320 // below. Although currently it is not.
321 break;
323 ERR("Picard: The postIteration() hook reported a "
324 "non-recoverable error.");
325 iteration_succeeded = false;
326 // Copy new solution to x.
327 // Thereby the failed solution can be used by the caller for
328 // debugging purposes.
330 break;
332 INFO(
333 "Picard: The postIteration() hook decided that this "
334 "iteration has to be repeated.");
336 *x[process_id],
337 x_new_process); // throw the iteration result away
338 anderson.dropLastStep();
339 continue;
340 }
341 }
342
344 {
345 // Don't compute error norms, break here.
346 error_norms_met = false;
347 break;
348 }
349
350 if (sys.isLinear())
351 {
352 error_norms_met = true;
353 }
354 else
355 {
356 if (_convergence_criterion->hasDeltaXCheck())
357 {
360 x_new_process); // minus_delta_x = x - x_new
363 }
364
366 }
367
368 // Update x s.t. in the next iteration we will compute the right delta x
370
371 INFO("[time] Iteration #{:d} took {:g} s.", iteration,
372 time_iteration.elapsed());
373
374 if (error_norms_met)
375 {
376 break;
377 }
378
379 // Avoid increment of the 'iteration' if the error norms are not met,
380 // but maximum number of iterations is reached.
381 if (iteration >= _maxiter)
382 {
383 break;
384 }
385 }
386
387 if (iteration > _maxiter)
388 {
389 ERR("Picard: Could not solve the given nonlinear system within {:d} "
390 "iterations",
391 _maxiter);
392 }
393
397
398 return {error_norms_met, iteration};
399}
#define OGS_FATAL(...)
Definition Error.h:10
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34
void copy(PETScVector const &x, PETScVector &y)
Definition LinAlg.cpp:30
void setLocalAccessibleVector(PETScVector const &x)
Definition LinAlg.cpp:20
void scale(PETScVector &x, PetscScalar const a)
Definition LinAlg.cpp:37
bool solvePicard(GlobalLinearSolver &linear_solver, GlobalMatrix &A, GlobalVector &rhs, GlobalVector &x, MathLib::LinearSolverBehaviour const linear_solver_behaviour)

References NonlinearSolver(), _A_id, _anderson_depth, _convergence_criterion, _damping, _equation_system, _linear_solver, _maxiter, _r_neq, _rhs_id, _x_new_id, NumLib::AndersonAcceleration::accelerate(), MathLib::LinAlg::axpy(), MathLib::COMPLETE_MATRIX_UPDATE, MathLib::LinAlg::copy(), NumLib::AndersonAcceleration::dropLastStep(), BaseLib::RunTime::elapsed(), ERR(), NumLib::FAILURE, INFO(), MathLib::LinAlg::matMult(), NumLib::AndersonAcceleration::min_mixing_depth, OGS_FATAL, NumLib::GlobalMatrixProvider::provider, NumLib::GlobalVectorProvider::provider, NumLib::REPEAT_ITERATION, MathLib::LinAlg::scale(), MathLib::LinAlg::setLocalAccessibleVector(), NumLib::detail::solvePicard(), BaseLib::RunTime::start(), NumLib::SUCCESS, and WARN().

Member Data Documentation

◆ _A_id

std::size_t NumLib::NonlinearSolver< NonlinearSolverTag::Picard >::_A_id = 0u
private

ID of the \( A \) matrix.

Definition at line 265 of file NonlinearSolver.h.

Referenced by calculateNonEquilibriumInitialResiduum(), and solve().

◆ _anderson_depth

int const NumLib::NonlinearSolver< NonlinearSolverTag::Picard >::_anderson_depth
private

Number of previous iterates retained for Anderson acceleration. 0 and 1 both disable the mixing (plain Picard).

Definition at line 260 of file NonlinearSolver.h.

Referenced by NonlinearSolver(), and solve().

◆ _compensate_non_equilibrium_initial_residuum

bool NumLib::NonlinearSolver< NonlinearSolverTag::Picard >::_compensate_non_equilibrium_initial_residuum = false
private

Enables computation of the non-equilibrium initial residuum \( r_{\rm neq} \) before the first time step. The forces are zero if the external forces are in equilibrium with the initial state/initial conditions. During the simulation the new residuum reads \( \tilde r = r - r_{\rm neq} \).

Definition at line 274 of file NonlinearSolver.h.

Referenced by calculateNonEquilibriumInitialResiduum(), and compensateNonEquilibriumInitialResiduum().

◆ _convergence_criterion

ConvergenceCriterion* NumLib::NonlinearSolver< NonlinearSolverTag::Picard >::_convergence_criterion = nullptr
private

Definition at line 251 of file NonlinearSolver.h.

Referenced by setEquationSystem(), and solve().

◆ _damping

double const NumLib::NonlinearSolver< NonlinearSolverTag::Picard >::_damping
private

Under-relaxation (damping) coefficient beta in (0, 1] applied to the Picard update x_{k+1} = (1-beta)*x_k + beta*g(x_k); 1.0 disables damping.

Definition at line 256 of file NonlinearSolver.h.

Referenced by NonlinearSolver(), and solve().

◆ _equation_system

System* NumLib::NonlinearSolver< NonlinearSolverTag::Picard >::_equation_system = nullptr
private

◆ _linear_solver

Definition at line 247 of file NonlinearSolver.h.

Referenced by NonlinearSolver(), and solve().

◆ _maxiter

int const NumLib::NonlinearSolver< NonlinearSolverTag::Picard >::_maxiter
private

maximum number of iterations

Definition at line 262 of file NonlinearSolver.h.

Referenced by NonlinearSolver(), and solve().

◆ _r_neq

non-equilibrium initial residuum.

Definition at line 264 of file NonlinearSolver.h.

Referenced by ~NonlinearSolver(), calculateNonEquilibriumInitialResiduum(), and solve().

◆ _r_neq_id

std::size_t NumLib::NonlinearSolver< NonlinearSolverTag::Picard >::_r_neq_id = 0u
private

ID of the non-equilibrium initial residuum vector.

Definition at line 269 of file NonlinearSolver.h.

Referenced by calculateNonEquilibriumInitialResiduum().

◆ _rhs_id

std::size_t NumLib::NonlinearSolver< NonlinearSolverTag::Picard >::_rhs_id = 0u
private

ID of the right-hand side vector.

Definition at line 266 of file NonlinearSolver.h.

Referenced by calculateNonEquilibriumInitialResiduum(), and solve().

◆ _x_new_id

std::size_t NumLib::NonlinearSolver< NonlinearSolverTag::Picard >::_x_new_id = 0u
private

ID of the vector storing the solution of the linearized equation.

Definition at line 267 of file NonlinearSolver.h.

Referenced by solve().


The documentation for this class was generated from the following files: