Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
physics_solver.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_h
5#define lethe_physics_solver_h
6
7
12#include <core/parameters.h>
14
15#include <deal.II/lac/affine_constraints.h>
16
17/**
18 * @brief Contain all the common elements of a physics solver. It creates
19 * the non-linear solver as specified by the user using the parameters file and
20 * provides all the necessary elements needed by the solver to solve a physics
21 * problem.
22 *
23 * @param[in] non_linear_solver_parameters Set of parameters that will be used
24 * to construct the non-linear solver.
25 */
26template <typename VectorType>
28{
29public:
30 /**
31 * @brief Constructor for the non-linear physics.
32 *
33 * @param[in] non_linear_solver_parameters Non-linear solver parameters as
34 * specified in the simulation parameter file.
35 *
36 */
37 PhysicsSolver(const Parameters::NonLinearSolver non_linear_solver_parameters);
38
39
40 /**
41 * @brief Constructor for the linear physics. Since the Physics is linear, a LinearSolutionStrategy is automatically generated as a PhysicsSolverStrategy.
42 *
43 */
45
46
47 /**
48 * @brief Destructor.
49 *
50 */
52 {
54 }
55
56 /**
57 * @brief Assemble the matrix.
58 */
59 virtual void
61
62 /**
63 * @brief Assemble the right-hand side (rhs).
64 */
65 virtual void
67
68 /**
69 * @brief Set up the preconditioner.
70 *
71 */
72 virtual void
74
75 /**
76 * @brief Solve the linear system of equations.
77 */
78 virtual void
80
81 /**
82 * @brief Solve the global system of equations according to a given strategy, either linear or not.
83 */
84 void
86
87 /**
88 * @brief Applies constraints to a local_evaluation_point.
89 */
90 virtual void
92 {
93 auto &nonzero_constraints = get_nonzero_constraints();
94 auto &local_evaluation_point = get_local_evaluation_point();
95 nonzero_constraints.distribute(local_evaluation_point);
96 }
97
98 /**
99 * @brief Getter methods that give access to the private attributes of the
100 * physics being solved. These methods must be provided by the physics as they
101 * are the key to solve a problem using Lethe.
102 */
103 virtual VectorType &
105 virtual VectorType &
107 virtual VectorType &
109 virtual VectorType &
111 virtual VectorType &
113 virtual AffineConstraints<double> &
115
116 /**
117 * @brief Output the L2 and Linfty norms of the correction vector.
118 */
119 virtual void
121
122 /**
123 * @brief Return the metric for residual rescaling. By default, should return 1.
124 * If the rescale_residual_by_volume is set to true, the method
125 * returns the square root of the global volume of the triangulation.
126 *
127 * @return Rescale metric.
128 */
129 virtual double
131
132 /**
133 * @brief Default way to evaluate the residual for the nonlinear solver, which is simply the norm of the RHS vector.
134 * Some application may use more complex evaluation of the residual and
135 * override this method.
136 *
137 * @return l2 norm of the RHS vector.
138 */
139 virtual double
141 {
142 auto &system_rhs = get_system_rhs();
143 return system_rhs.l2_norm();
144 }
145
146 /**
147 * @brief Return whether this solver may skip the inner linear solve when the
148 * freshly assembled residual is already below the nonlinear tolerance.
149 *
150 * The default behavior is conservative and keeps the linear solve enabled.
151 * Solvers whose outer nonlinear residual may remain active for reasons other
152 * than the assembled linear-system residual can override this hook.
153 *
154 * @return `true` if the solver allows skipping the linear solve once the
155 * assembled residual is already below the nonlinear tolerance.
156 */
157 virtual bool
159 {
160 return false;
161 }
162
163 /**
164 * @brief Return the current newton iteration of this physics solver.
165 */
166 inline unsigned int
168 {
169 return physics_solving_strategy->get_current_newton_iteration();
170 }
171
172 ConditionalOStream pcout;
174
175private:
177};
178
179template <typename VectorType>
181 const Parameters::NonLinearSolver non_linear_solver_parameters)
182 : pcout(std::cout, Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0)
183{
184 switch (non_linear_solver_parameters.solver)
185 {
186 case Parameters::NonLinearSolver::SolverType::newton:
187 physics_solving_strategy =
188 new NewtonNonLinearSolverStrategy<VectorType>(
189 this, non_linear_solver_parameters);
190 break;
191 case Parameters::NonLinearSolver::SolverType::kinsol_newton:
192 physics_solving_strategy =
193 new KinsolNewtonNonLinearSolverStrategy<VectorType>(
194 this, non_linear_solver_parameters);
195 break;
196 case Parameters::NonLinearSolver::SolverType::inexact_newton:
197 physics_solving_strategy =
198 new InexactNewtonNonLinearSolverStrategy<VectorType>(
199 this, non_linear_solver_parameters);
200 break;
201 default:
202 break;
203 }
204}
205
206template <typename VectorType>
208 : pcout(std::cout, Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0)
209{
211}
212
213
214template <typename VectorType>
215void
222#endif
The linear solver strategy class is used to solve the physics as a linear problem.
Base class that works as an interface for all solver strategies (either non-linear or linear) for all...
virtual VectorType & get_present_solution()=0
Parameters::SimulationControl::TimeSteppingMethod time_stepping_method
PhysicsSolver(const Parameters::NonLinearSolver non_linear_solver_parameters)
Constructor for the non-linear physics.
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...
PhysicsSolver()
Constructor for the linear physics. Since the Physics is linear, a LinearSolutionStrategy is automati...
virtual void solve_linear_system()=0
Solve the linear system of equations.
ConditionalOStream pcout
void solve_governing_system()
Solve the global system of equations according to a given strategy, either linear or not.
unsigned int get_current_newton_iteration() const
Return the current newton iteration of this physics solver.
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).
PhysicsSolverStrategy< VectorType > * physics_solving_strategy
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 AffineConstraints< double > & get_nonzero_constraints()=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 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 ~PhysicsSolver()
Destructor.
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