Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
inexact_newton_non_linear_solver_strategy.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2021-2026 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
4#ifndef lethe_inexact_newton_non_linear_solver_strategy_h
5#define lethe_inexact_newton_non_linear_solver_strategy_h
6
8
9#include <iomanip>
10
11/**
12 * @brief Non-linear solver for non-linear systems of equations which uses an inexact
13 * Newton method with \f$\alpha\f$ relaxation to ensure that the residual is
14 * monotonically decreasing. It allows to reuse the matrix and avoid its
15 * reassembly for every Newton step.
16 *
17 */
18template <typename VectorType>
20 : public PhysicsSolverStrategy<VectorType>
21{
22public:
23 /**
24 * @brief Constructor.
25 *
26 * @param[in] physics_solver A pointer to the physics solver to which the
27 * non-linear solver is attached.
28 *
29 * @param[in] param Non-linear solver parameters as specified in the
30 * simulation parameter file.
31 *
32 */
35 const Parameters::NonLinearSolver &param);
36
37
38 /**
39 * @brief Solve the non-linear system of equations.
40 *
41 */
42 void
43 solve() override;
44
45private:
47};
48
49template <typename VectorType>
57
58template <typename VectorType>
59void
61{
62 double global_res;
63 double current_res;
64 double last_res;
65 this->outer_iteration = 0;
66 last_res = 1e6;
67 current_res = 1e6;
68 global_res = 1e6;
69
70 // current_res and global_res are different as one is defined based on the l2
71 // norm of the residual vector (current_res) and the other (global_res) is
72 // defined by the physical solver and may differ from the l2_norm of the
73 // residual vector. Only the global_res is compared to the tolerance in order
74 // to evaluate if the nonlinear system is solved. Only current_res is used for
75 // the alpha scheme as this scheme only monitors the convergence of the
76 // non-linear system of equation (the matrix problem).
77
79
80 const double rescale_metric = solver->get_residual_rescale_metric();
81
82 auto &evaluation_point = solver->get_evaluation_point();
83 auto &present_solution = solver->get_present_solution();
84
85 if (!this->params.reuse_matrix)
87
88 while ((global_res > this->params.tolerance) &&
89 this->outer_iteration < this->params.max_iterations)
90 {
91 evaluation_point = present_solution;
92
94 {
95 solver->assemble_system_matrix();
96 solver->setup_preconditioner();
97 }
98
99 const bool assemble_rhs_this_iteration =
100 this->params.force_rhs_calculation || this->outer_iteration == 0;
101 if (assemble_rhs_this_iteration)
102 solver->assemble_system_rhs();
103
104 if (this->outer_iteration == 0)
105 {
106 auto &system_rhs = solver->get_system_rhs();
107 current_res = system_rhs.l2_norm() / rescale_metric;
108 last_res = current_res;
109 }
110
111 if (this->params.verbosity != Parameters::Verbosity::quiet)
112 {
113 solver->pcout << "Newton iteration: " << this->outer_iteration
114 << " - Residual: " << current_res << std::endl;
115 }
116
118 assemble_rhs_this_iteration,
119 rescale_metric,
120 current_res,
121 last_res,
122 global_res))
123 continue;
124
125 solver->solve_linear_system();
126 double last_alpha_res = current_res;
127
128 unsigned int alpha_iter = 0;
129 for (double alpha = 1.0; alpha > 1e-1; alpha *= 0.5)
130 {
131 auto &local_evaluation_point = solver->get_local_evaluation_point();
132 auto &newton_update = solver->get_newton_update();
133 local_evaluation_point = present_solution;
134 local_evaluation_point.add(alpha, newton_update);
135 solver->apply_constraints();
136 evaluation_point = local_evaluation_point;
137 solver->assemble_system_rhs();
138
139 auto &system_rhs = solver->get_system_rhs();
140 current_res = system_rhs.l2_norm() / rescale_metric;
141
142 if (this->params.verbosity != Parameters::Verbosity::quiet)
143 {
144 solver->pcout << "\talpha = " << std::defaultfloat << std::setw(6)
145 << alpha << std::setw(6) << std::scientific
146 << "\tres = " << std::setw(6) << current_res;
147
149 }
150
151 // If it's not the first iteration of alpha check if the residual is
152 // smaller then the last alpha iteration. If it's not smaller we fall
153 // back to the last alpha iteration, which is generally the first one.
154 if (alpha_iter != 0 and current_res > last_alpha_res)
155 {
156 alpha = 2 * alpha;
157 local_evaluation_point = present_solution;
158 local_evaluation_point.add(alpha, newton_update);
159 solver->apply_constraints();
160 evaluation_point = local_evaluation_point;
161 solver->assemble_system_rhs();
162
163 if (this->params.verbosity != Parameters::Verbosity::quiet)
164 {
165 solver->pcout << "\t\talpha value was kept at alpha = "
166 << std::defaultfloat << alpha
167 << " since alpha = " << alpha / 2
168 << std::scientific << " increased the residual"
169 << std::endl;
170 }
171 current_res = last_alpha_res;
172 break;
173 }
174 if (current_res < this->params.step_tolerance * last_res ||
175 last_res < this->params.tolerance)
176 {
177 // If the current residual has decreased by less than
178 // the last residual * matrix_tolerance or more than one alpha
179 // iteration was required, the jacobian approximation has become
180 // insufficiently performant and, consequently, it is preferable
181 // to renew the jacobian matrix
182 if (current_res > this->params.matrix_tolerance * last_res ||
183 alpha_iter > 0)
185 else
187
188 break;
189 }
190 last_alpha_res = current_res;
191 alpha_iter++;
192 }
193
194 global_res = solver->get_current_residual() / rescale_metric;
195 present_solution = evaluation_point;
196 last_res = current_res;
197 ++this->outer_iteration;
198 }
199
200 // If the non-linear solver has not converged abort simulation if
201 // abort_at_convergence_failure=true
202 if ((global_res > this->params.tolerance) &&
203 this->outer_iteration >= this->params.max_iterations &&
204 this->params.abort_at_convergence_failure)
205 {
206 throw(std::runtime_error(
207 "Stopping simulation because the non-linear solver has failed to converge"));
208 }
209}
210
211#endif
InexactNewtonNonLinearSolverStrategy(PhysicsSolver< VectorType > *physics_solver, const Parameters::NonLinearSolver &param)
Constructor.
void solve() override
Solve the non-linear system of equations.
unsigned int outer_iteration
Number of current Newton iteration.
PhysicsSolver< VectorType > * physics_solver
Physics solver for which we need a non-linear solver.
bool skip_linear_solve_if_fresh_rhs_is_already_converged(const bool rhs_was_assembled_this_iteration, const double rescale_metric, double &current_res, double &last_res, double &global_res)
Skip the inner linear solve when the current Newton evaluation already produced a freshly assembled R...
PhysicsSolverStrategy(PhysicsSolver< VectorType > *physics_solver, const Parameters::NonLinearSolver &params)
Constructor for the non-linear solver strategies.
Parameters::NonLinearSolver params
Non linear solver parameters.
Contain all the common elements of a physics solver. It creates the non-linear solver as specified by...
virtual VectorType & get_present_solution()=0
virtual double get_residual_rescale_metric() const =0
Return the metric for residual rescaling. By default, should return 1. If the rescale_residual_by_vol...
virtual void solve_linear_system()=0
Solve the linear system of equations.
ConditionalOStream pcout
virtual void setup_preconditioner()=0
Set up the preconditioner.
virtual void assemble_system_matrix()=0
Assemble the matrix.
virtual void assemble_system_rhs()=0
Assemble the right-hand side (rhs).
virtual double get_current_residual()
Default way to evaluate the residual for the nonlinear solver, which is simply the norm of the RHS ve...
virtual VectorType & get_system_rhs()=0
virtual void output_newton_update_norms()=0
Output the L2 and Linfty norms of the correction vector.
virtual VectorType & get_local_evaluation_point()=0
virtual VectorType & get_evaluation_point()=0
Getter methods that give access to the private attributes of the physics being solved....
virtual VectorType & get_newton_update()=0
virtual void apply_constraints()
Applies constraints to a local_evaluation_point.
NonLinearSolver - Parameter that controls the solution of the non-linear problems.
GlobalVectorType VectorType