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

Detailed Description

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

Definition at line 167 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, const int maxiter)
 ~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, bool, 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
const int _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 172 of file NonlinearSolver.h.

Constructor & Destructor Documentation

◆ NonlinearSolver()

NumLib::NonlinearSolver< NonlinearSolverTag::Picard >::NonlinearSolver ( GlobalLinearSolver & linear_solver,
const int maxiter )
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.

Definition at line 180 of file NonlinearSolver.h.

References _linear_solver, and _maxiter.

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

◆ ~NonlinearSolver()

Definition at line 597 of file NonlinearSolver.cpp.

598{
599 if (_r_neq != nullptr)
600 {
602 }
603}
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 81 of file NonlinearSolver.cpp.

85{
87 {
88 return;
89 }
90
91 INFO("Calculate non-equilibrium initial residuum.");
92
96 _equation_system->getA(A);
98
99 // r_neq = A * x - rhs
102 MathLib::LinAlg::axpy(*_r_neq, -1.0, rhs); // res -= rhs
103
104 // Set the values of the selected entries of _r_neq, which are associated
105 // with the equations that do not need initial residual compensation, to
106 // zero.
108 _equation_system->getIndicesOfResiduumWithoutInitialCompensation();
109
110#ifdef USE_PETSC
111 // Ghost entry with global index 0 is encoded as -global_size
112 // After abs(), it appears as global_size and must be converted back to 0
113 auto const global_size = _r_neq->size();
114 for (auto& idx : selected_global_indices)
115 {
116 if (idx == global_size)
117 {
118 idx = 0;
119 }
120 }
121#endif
122
125 _equation_system->setReleaseNodalForces(_r_neq, process_id);
126
128
131}
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:191
void matMult(PETScMatrix const &A, PETScVector const &x, PETScVector &y)
Definition LinAlg.cpp:142
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 190 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, bool, 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 133 of file NonlinearSolver.cpp.

139{
140 namespace LinAlg = MathLib::LinAlg;
141 auto& sys = *_equation_system;
142
145
149 LinAlg::copy(*x[process_id], *x_new[process_id]); // set initial guess
150
151 bool error_norms_met = false;
152
153 _convergence_criterion->preFirstIteration();
154
155 int iteration = 1;
156 for (; iteration <= _maxiter; ++iteration, _convergence_criterion->reset())
157 {
159 double time_dirichlet = 0.0;
160
162 time_iteration.start();
163
164 INFO("Iteration #{:d} started.", iteration);
165 timer_dirichlet.start();
168 sys.computeKnownSolutions(x_new_process, process_id);
169 sys.applyKnownSolutions(x_new_process);
170 time_dirichlet += timer_dirichlet.elapsed();
171
172 sys.preIteration(iteration, x_new_process);
173
175 time_assembly.start();
176 sys.assemble(x_new, x_prev, process_id);
177 sys.getA(A);
178 sys.getRhs(*x_prev[process_id], rhs);
179
180 // Normalize the linear equation system, if required
181 if (sys.requiresNormalization() &&
182 !_linear_solver.canSolveRectangular())
183 {
184 sys.getAandRhsNormalized(A, rhs);
185 WARN(
186 "The equation system is rectangular, but the current linear "
187 "solver only supports square systems. "
188 "The system will be normalized, which lead to a squared "
189 "condition number and potential numerical issues. "
190 "It is recommended to use a solver that supports rectangular "
191 "equation systems for better numerical stability.");
192 }
193
194 INFO("[time] Assembly took {:g} s.", time_assembly.elapsed());
195
196 // Subtract non-equilibrium initial residuum if set
197 if (_r_neq != nullptr)
198 {
199 LinAlg::axpy(rhs, -1, *_r_neq);
200 }
201
202 auto const solver_needs_to_compute = sys.linearSolverNeedsToCompute();
203 bool const solver_will_compute =
205
206 timer_dirichlet.start();
207 sys.applyKnownSolutionsPicard(
213 time_dirichlet += timer_dirichlet.elapsed();
214 INFO("[time] Applying Dirichlet BCs took {:g} s.", time_dirichlet);
215
216 if (!sys.isLinear() && _convergence_criterion->hasResidualCheck())
217 {
219 {
220 // !solver_will_compute means that the Dirichlet BC application
221 // is incomplete (i.e., A not properly modified) and the
222 // computed residual is wrong.
223 OGS_FATAL(
224 "Logic error. The solver skips the compute step for a "
225 "non-linear equation system.");
226 }
228 LinAlg::matMult(A, x_new_process, res); // res = A * x_new
229 LinAlg::axpy(res, -1.0, rhs); // res -= rhs
230 _convergence_criterion->checkResidual(res);
231 }
232
235
237 {
239 {
241 }
242
243 switch (sys.postIteration(x_new_process))
244 {
246 // Don't copy here. The old x might still be used further
247 // below. Although currently it is not.
248 break;
250 ERR("Picard: The postIteration() hook reported a "
251 "non-recoverable error.");
252 iteration_succeeded = false;
253 // Copy new solution to x.
254 // Thereby the failed solution can be used by the caller for
255 // debugging purposes.
257 break;
259 INFO(
260 "Picard: The postIteration() hook decided that this "
261 "iteration has to be repeated.");
263 *x[process_id],
264 x_new_process); // throw the iteration result away
265 continue;
266 }
267 }
268
270 {
271 // Don't compute error norms, break here.
272 error_norms_met = false;
273 break;
274 }
275
276 if (sys.isLinear())
277 {
278 error_norms_met = true;
279 }
280 else
281 {
282 if (_convergence_criterion->hasDeltaXCheck())
283 {
286 x_new_process); // minus_delta_x = x - x_new
289 }
290
292 }
293
294 // Update x s.t. in the next iteration we will compute the right delta x
296
297 INFO("[time] Iteration #{:d} took {:g} s.", iteration,
298 time_iteration.elapsed());
299
300 if (error_norms_met)
301 {
302 break;
303 }
304
305 // Avoid increment of the 'iteration' if the error norms are not met,
306 // but maximum number of iterations is reached.
307 if (iteration >= _maxiter)
308 {
309 break;
310 }
311 }
312
313 if (iteration > _maxiter)
314 {
315 ERR("Picard: Could not solve the given nonlinear system within {:d} "
316 "iterations",
317 _maxiter);
318 }
319
323
324 return {error_norms_met, iteration};
325}
#define OGS_FATAL(...)
Definition Error.h:19
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
bool solvePicard(GlobalLinearSolver &linear_solver, GlobalMatrix &A, GlobalVector &rhs, GlobalVector &x, MathLib::LinearSolverBehaviour const linear_solver_behaviour)

References NonlinearSolver(), _A_id, _convergence_criterion, _equation_system, _linear_solver, _maxiter, _r_neq, _rhs_id, _x_new_id, MathLib::LinAlg::axpy(), MathLib::COMPLETE_MATRIX_UPDATE, MathLib::LinAlg::copy(), BaseLib::RunTime::elapsed(), ERR(), NumLib::FAILURE, INFO(), MathLib::LinAlg::matMult(), OGS_FATAL, NumLib::GlobalMatrixProvider::provider, NumLib::GlobalVectorProvider::provider, NumLib::REPEAT_ITERATION, 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 222 of file NonlinearSolver.h.

Referenced by calculateNonEquilibriumInitialResiduum(), 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 231 of file NonlinearSolver.h.

Referenced by calculateNonEquilibriumInitialResiduum(), and compensateNonEquilibriumInitialResiduum().

◆ _convergence_criterion

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

Definition at line 218 of file NonlinearSolver.h.

Referenced by setEquationSystem(), and solve().

◆ _equation_system

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

◆ _linear_solver

Definition at line 214 of file NonlinearSolver.h.

Referenced by NonlinearSolver(), and solve().

◆ _maxiter

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

maximum number of iterations

Definition at line 219 of file NonlinearSolver.h.

Referenced by NonlinearSolver(), and solve().

◆ _r_neq

non-equilibrium initial residuum.

Definition at line 221 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 226 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 223 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 224 of file NonlinearSolver.h.

Referenced by solve().


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