Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
kinsol_newton_non_linear_solver_strategy.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2021, 2023-2025 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
4#ifndef lethe_kinsol_newton_non_linear_solver_strategy_h
5#define lethe_kinsol_newton_non_linear_solver_strategy_h
6
7#include <core/parameters.h>
9
10#include <deal.II/sundials/kinsol.h>
11
12/**
13 * @brief Non-linear Newton solver for non-linear systems of equations implemented
14 * in the SUNDIALS suite, specifically the KINSOL package. This solver has
15 * internal algorithms to determine the time step and decide whether to
16 * reassemble the Jacobian matrix or not.
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 */
34 const Parameters::NonLinearSolver &param);
35
36
37 /**
38 * @brief Solve the non-linear system of equations.
39 */
40 void
41 solve() override;
42};
43
44template <typename VectorType>
50
51template <typename VectorType>
52void
54{
55#ifdef DEAL_II_WITH_SUNDIALS
56
58
59 auto &local_evaluation_point = solver->get_local_evaluation_point();
60
61 VectorType &evaluation_point = solver->get_evaluation_point();
62 VectorType &present_solution = solver->get_present_solution();
63
64 typename SUNDIALS::KINSOL<VectorType>::AdditionalData additional_data;
65 additional_data.function_tolerance = this->params.tolerance;
66 additional_data.maximum_non_linear_iterations = this->params.max_iterations;
67 additional_data.step_tolerance = this->params.tolerance;
68
69 // Kinsol solution strategy, the default one is line search
70 if (this->params.kinsol_strategy ==
72 {
73 additional_data.strategy =
74 SUNDIALS::KINSOL<VectorType>::AdditionalData::SolutionStrategy::newton;
75 }
76 else if (this->params.kinsol_strategy ==
78 {
79 additional_data.strategy = SUNDIALS::KINSOL<
80 VectorType>::AdditionalData::SolutionStrategy::linesearch;
81 }
82 else if (this->params.kinsol_strategy ==
84 {
85 additional_data.strategy =
86 SUNDIALS::KINSOL<VectorType>::AdditionalData::SolutionStrategy::picard;
87 }
88
89 SUNDIALS::KINSOL<VectorType> nonlinear_solver(additional_data);
90
91 nonlinear_solver.reinit_vector = [&](VectorType &x) {
92 x.reinit(local_evaluation_point);
93 };
94
95 nonlinear_solver.residual = [&](const VectorType &evaluation_point_for_kinsol,
96 VectorType &residual) {
97 solver->pcout << "Computing residual vector..." << std::endl;
98 evaluation_point = evaluation_point_for_kinsol;
99 solver->apply_constraints();
100 solver->assemble_system_rhs();
101 auto &current_residual = solver->get_system_rhs();
102 residual = current_residual;
103 solver->pcout << " -Residual: " << residual.l2_norm() << std::endl;
104 return 0;
105 };
106
107 nonlinear_solver.setup_jacobian =
108 [&](const VectorType &present_solution_for_kinsol,
109 const VectorType & /*current_f*/) {
110 solver->pcout << "Computing jacobian matrix..." << std::endl;
111 evaluation_point = present_solution_for_kinsol;
112 solver->assemble_system_matrix();
113 solver->setup_preconditioner();
114 return 0;
115 };
116
117 nonlinear_solver.solve_with_jacobian = [&](const VectorType & /* residual */,
118 VectorType &dst,
119 const double /* tolerance */) {
120 solver->pcout << "Solving linear system..." << std::endl;
121 solver->solve_linear_system();
122 dst = solver->get_newton_update();
123 return 0;
124 };
125
126 local_evaluation_point = present_solution;
127
128 nonlinear_solver.solve(local_evaluation_point);
129 present_solution = local_evaluation_point;
130#else
131 throw std::runtime_error(
132 "Kinsol newton nonlinear solver requires DEAL_II to be compiled with SUNDIALS");
133#endif // DEAL_II_WITH_SUNDIALS
134}
135
136#endif
KinsolNewtonNonLinearSolverStrategy(PhysicsSolver< VectorType > *physics_solver, const Parameters::NonLinearSolver &param)
Constructor.
void solve() override
Solve the non-linear system of equations.
PhysicsSolver< VectorType > * physics_solver
Physics solver for which we need a non-linear solver.
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 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 VectorType & get_system_rhs()=0
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