Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
physics_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_physics_solver_strategy_h
5#define lethe_physics_solver_strategy_h
6
7#include <core/parameters.h>
8
9#include <deal.II/base/exceptions.h>
10
11template <typename VectorType>
12class PhysicsSolver;
13
14/**
15 * @brief Base class that works as an interface for all solver strategies (either non-linear or linear) for all systems of equations.
16 *
17 */
18template <typename VectorType>
20{
21public:
22 /**
23 * @brief Constructor for the non-linear solver strategies.
24 *
25 * @param[in] physics_solver A pointer to the physics solver to which the
26 * solving strategy is attached.
27 *
28 * @param[in] param Non-linear solver parameters as specified in the
29 * simulation parameter file.
30 *
31 */
34
35 /**
36 * @brief Constructor for the linear solver strategy.
37 *
38 * @param[in] physics_solver A pointer to the physics solver to which the
39 * solving strategy is attached.
40 *
41 */
43
44 /**
45 * @brief Destructor.
46 *
47 */
48 virtual ~PhysicsSolverStrategy() = default;
49
50 /**
51 * @brief Solve the system of equations.
52 *
53 */
54 virtual void
55 solve() = 0;
56
57
58 /**
59 * @brief Get the current newton iteration.
60 *
61 * @return Iteration number.
62 *
63 */
64 inline unsigned int
66 {
67 return outer_iteration;
68 }
69
70protected:
71 /**
72 * @brief Skip the inner linear solve when the current Newton evaluation
73 * already produced a freshly assembled RHS whose residual is below the
74 * nonlinear tolerance.
75 *
76 * This shortcut is only valid when the RHS was assembled for the current
77 * evaluation point. Otherwise the residual may still correspond to an older
78 * state and skipping the solve would incorrectly reuse stale information.
79 *
80 * Solvers must opt into this behavior explicitly through
81 * allow_skip_linear_solve_when_residual_is_below_tolerance(). The shortcut
82 * is always safe on Newton iteration 0 because the RHS is assembled
83 * unconditionally there. On later Newton iterations, it requires
84 * force_rhs_calculation so that the converged residual comes from a fresh
85 * RHS assembled at the current Newton evaluation point.
86 *
87 * @param[in] rhs_was_assembled_this_iteration Whether the current Newton
88 * iteration assembled the RHS at the current evaluation point.
89 * @param[in] rescale_metric Residual rescaling factor used by the nonlinear
90 * solver strategy.
91 * @param[in,out] current_res Current matrix residual norm.
92 * @param[in,out] last_res Last accepted matrix residual norm.
93 * @param[in,out] global_res Current nonlinear residual reported by the
94 * physics solver.
95 * @return true if the linear solve was skipped and the Newton state was
96 * advanced consistently, false otherwise.
97 */
98 bool
100 const bool rhs_was_assembled_this_iteration,
101 const double rescale_metric,
102 double &current_res,
103 double &last_res,
104 double &global_res);
105
106 /**
107 * @brief Physics solver for which we need a non-linear solver.
108 *
109 */
111
112 /**
113 * @brief Non linear solver parameters.
114 *
115 */
117
118 /**
119 * @brief Number of current Newton iteration.
120 *
121 */
122 unsigned int outer_iteration;
123};
124
125template <typename VectorType>
133
134template <typename VectorType>
139
140template <typename VectorType>
141bool
144 const bool rhs_was_assembled_this_iteration,
145 const double rescale_metric,
146 double &current_res,
147 double &last_res,
148 double &global_res)
149{
151
153 return false;
154
155 if (!rhs_was_assembled_this_iteration)
156 return false;
157
158 const double assembled_res =
159 solver->get_system_rhs().l2_norm() / rescale_metric;
160 if (assembled_res > this->params.tolerance)
161 return false;
162
163 AssertThrow(
164 this->params.force_rhs_calculation || this->outer_iteration == 0,
165 ExcMessage(
166 "Skipping the linear solve based on the assembled RHS residual requires "
167 "either Newton iteration 0, where the RHS is always freshly assembled, "
168 "or 'force rhs calculation = true' on later Newton iterations."));
169
170 current_res = assembled_res;
171 solver->get_newton_update() = 0;
172 global_res = solver->get_current_residual() / rescale_metric;
173 solver->get_present_solution() = solver->get_evaluation_point();
174 last_res = current_res;
175 ++this->outer_iteration;
176
177 return true;
178}
179
180#endif
unsigned int outer_iteration
Number of current Newton iteration.
PhysicsSolver< GlobalVectorType > * physics_solver
unsigned int get_current_newton_iteration() const
Get the current newton iteration.
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.
virtual ~PhysicsSolverStrategy()=default
Destructor.
virtual void solve()=0
Solve the system of equations.
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_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 bool allow_skip_linear_solve_when_residual_is_below_tolerance() const
Return whether this solver may skip the inner linear solve when the freshly assembled residual is alr...
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
NonLinearSolver - Parameter that controls the solution of the non-linear problems.