Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
newton_non_linear_solver_strategy.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2019-2026 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
4#ifndef lethe_newton_non_linear_solver_strategy_h
5#define lethe_newton_non_linear_solver_strategy_h
6
8
9/**
10 * @brief Non-linear solver for non-linear systems of equations which uses a Newton
11 * method with \f$\alpha\f$ relaxation to ensure that the residual is
12 * monotonically decreasing.
13 */
14template <typename VectorType>
16{
17public:
18 /**
19 * @brief Constructor.
20 *
21 * @param[in] physics_solver A pointer to the physics solver to which the
22 * non-linear solver is attached.
23 *
24 * @param[in] param Non-linear solver parameters as specified in the
25 * simulation parameter file.
26 *
27 */
29 const Parameters::NonLinearSolver &param);
30
31
32 /**
33 * @brief Solve the non-linear system of equations.
34 *
35 */
36 void
37 solve() override;
38};
39
40template <typename VectorType>
46
47template <typename VectorType>
48void
50{
51 double global_res;
52 double current_res;
53 double last_res;
54 this->outer_iteration = 0;
55 last_res = 1e6;
56 current_res = 1e6;
57 global_res = 1e6;
58
59 // current_res and global_res are different as one is defined based on the l2
60 // norm of the residual vector (current_res) and the other (global_res) is
61 // defined by the physical solver and may differ from the l2_norm of the
62 // residual vector. Only the global_res is compared to the tolerance in order
63 // to evaluate if the nonlinear system is solved. Only current_res is used for
64 // the alpha scheme as this scheme only monitors the convergence of the
65 // non-linear system of equation (the matrix problem).
66
68
69 const double rescale_metric = solver->get_residual_rescale_metric();
70
71 auto &evaluation_point = solver->get_evaluation_point();
72 auto &present_solution = solver->get_present_solution();
73
74 while ((global_res > this->params.tolerance) &&
75 this->outer_iteration < this->params.max_iterations)
76 {
77 evaluation_point = present_solution;
78
79 solver->assemble_system_matrix();
80
81 if (!this->params.reuse_preconditioner || this->outer_iteration == 0)
82 solver->setup_preconditioner();
83
84 const bool assemble_rhs_this_iteration =
85 this->params.force_rhs_calculation || this->outer_iteration == 0;
86 if (assemble_rhs_this_iteration)
87 solver->assemble_system_rhs();
88
89 if (this->outer_iteration == 0)
90 {
91 auto &system_rhs = solver->get_system_rhs();
92 current_res = system_rhs.l2_norm() / rescale_metric;
93 last_res = current_res;
94 }
95
96 if (this->params.verbosity != Parameters::Verbosity::quiet)
97 {
98 solver->pcout << "Newton iteration: " << this->outer_iteration
99 << " - Residual: " << current_res << std::endl;
100 }
101
103 assemble_rhs_this_iteration,
104 rescale_metric,
105 current_res,
106 last_res,
107 global_res))
108 continue;
109
110 solver->solve_linear_system();
111 double last_alpha_res = current_res;
112
113 unsigned int alpha_iter = 0;
114 for (double alpha = 1.0; alpha > 1e-1; alpha *= 0.5)
115 {
116 auto &local_evaluation_point = solver->get_local_evaluation_point();
117 auto &newton_update = solver->get_newton_update();
118 local_evaluation_point = present_solution;
119 local_evaluation_point.add(alpha, newton_update);
120 solver->apply_constraints();
121 evaluation_point = local_evaluation_point;
122 solver->assemble_system_rhs();
123
124 auto &system_rhs = solver->get_system_rhs();
125 current_res = system_rhs.l2_norm() / rescale_metric;
126
127 if (this->params.verbosity != Parameters::Verbosity::quiet)
128 {
129 solver->pcout << "\talpha = " << std::defaultfloat << std::setw(6)
130 << alpha << std::setw(6) << std::scientific
131 << "\tres = " << std::setw(6) << current_res;
132
134 }
135
136 // If it's not the first iteration of alpha check if the residual is
137 // smaller than the last alpha iteration. If it's not smaller, we fall
138 // back to the last alpha iteration.
139 if (current_res > last_alpha_res and alpha_iter != 0)
140 {
141 alpha = 2 * alpha;
142 local_evaluation_point = present_solution;
143 local_evaluation_point.add(alpha, newton_update);
144 solver->apply_constraints();
145 evaluation_point = local_evaluation_point;
146 solver->assemble_system_rhs();
147
148 if (this->params.verbosity != Parameters::Verbosity::quiet)
149 {
150 solver->pcout << "\t\talpha value was kept at alpha = "
151 << std::defaultfloat << alpha
152 << " since alpha = " << alpha / 2
153 << std::scientific << " increased the residual"
154 << std::endl;
155 }
156 current_res = last_alpha_res;
157 break;
158 }
159 if (current_res < this->params.step_tolerance * last_res ||
160 last_res < this->params.tolerance)
161 {
162 break;
163 }
164 last_alpha_res = current_res;
165 alpha_iter++;
166 }
167
168 global_res = solver->get_current_residual() / rescale_metric;
169 present_solution = evaluation_point;
170 last_res = current_res;
171 ++this->outer_iteration;
172 }
173
174 // If the non-linear solver has not converged abort simulation if
175 // abort_at_convergence_failure=true
176 if ((global_res > this->params.tolerance) &&
177 this->outer_iteration >= this->params.max_iterations &&
178 this->params.abort_at_convergence_failure)
179 {
180 throw(std::runtime_error(
181 "Stopping simulation because the non-linear solver has failed to converge"));
182 }
183}
184
185#endif
void solve() override
Solve the non-linear system of equations.
NewtonNonLinearSolverStrategy(PhysicsSolver< VectorType > *physics_solver, const Parameters::NonLinearSolver &param)
Constructor.
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