OGS
NonlinearSolver.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#pragma once
5
6#include <memory>
7#include <utility>
8
11#include "NewtonStepStrategy.h"
13#include "NonlinearSystem.h"
14#include "Types.h"
15
16namespace BaseLib
17{
18class ConfigTree;
19}
20
21// TODO Document in the ODE solver lib, which matrices and vectors that are
22// passed around as method arguments are guaranteed to be of the right size
23// (and zeroed out) and which are not.
24
25namespace NumLib
26{
31{
32public:
34 std::vector<GlobalVector*> const& x,
35 std::vector<GlobalVector*> const& x_prev, int const process_id) = 0;
36
48 std::vector<GlobalVector*>& x,
49 std::vector<GlobalVector*> const& x_prev,
50 std::function<void(int, bool, std::vector<GlobalVector*> const&)> const&
51 postIterationCallback,
52 int const process_id) = 0;
53
54 virtual ~NonlinearSolverBase() = default;
55};
56
59
64template <NonlinearSolverTag NLTag>
66
69template <>
71 : public NonlinearSolverBase
72{
73public:
76
87 explicit NonlinearSolver(GlobalLinearSolver& linear_solver,
88 int const maxiter,
89 std::unique_ptr<NewtonStepStrategy>
90 newton_strategy,
91 int const recompute_jacobian = 1)
92 : _linear_solver(linear_solver),
93 _maxiter(maxiter),
94 _step_strategy(std::move(newton_strategy)),
95 _recompute_jacobian(recompute_jacobian)
96 {
97 }
98
100
106 {
107 _equation_system = &eq;
108 _convergence_criterion = &conv_crit;
109 _step_strategy->setDampingPolicy(
110 _convergence_criterion->dampingPolicy());
111 }
112
113 void calculateNonEquilibriumInitialResiduum(
114 std::vector<GlobalVector*> const& x,
115 std::vector<GlobalVector*> const& x_prev,
116 int const process_id) override;
117
119 std::vector<GlobalVector*>& x,
120 std::vector<GlobalVector*> const& x_prev,
121 std::function<void(int, bool, std::vector<GlobalVector*> const&)> const&
122 postIterationCallback,
123 int const process_id) override;
124
129
130private:
133
134 int const _maxiter;
135
137 std::unique_ptr<NewtonStepStrategy> _step_strategy;
138
141
143 1;
144
145 GlobalVector* _r_neq = nullptr;
146 std::size_t _res_id = 0u;
147 std::size_t _J_id = 0u;
148 std::size_t _minus_delta_x_id = 0u;
149 std::size_t _x_new_id =
150 0u;
151 std::size_t _r_neq_id = 0u;
153
160};
161
166template <>
168 : public NonlinearSolverBase
169{
170public:
173
180 explicit NonlinearSolver(GlobalLinearSolver& linear_solver,
181 const int maxiter)
182 : _linear_solver(linear_solver), _maxiter(maxiter)
183 {
184 }
185
187
191 {
192 _equation_system = &eq;
193 _convergence_criterion = &conv_crit;
194 }
195
196 void calculateNonEquilibriumInitialResiduum(
197 std::vector<GlobalVector*> const& x,
198 std::vector<GlobalVector*> const& x_prev,
199 int const process_id) override;
200
202 std::vector<GlobalVector*>& x,
203 std::vector<GlobalVector*> const& x_prev,
204 std::function<void(int, bool, std::vector<GlobalVector*> const&)> const&
205 postIterationCallback,
206 int const process_id) override;
207
212
213private:
216
217 // TODO doc
219 const int _maxiter;
220
221 GlobalVector* _r_neq = nullptr;
222 std::size_t _A_id = 0u;
223 std::size_t _rhs_id = 0u;
224 std::size_t _x_new_id = 0u;
226 std::size_t _r_neq_id = 0u;
228
229 // clang-format off
232 // clang-format on
233};
234
235
236} // namespace NumLib
MathLib::EigenLisLinearSolver GlobalLinearSolver
MathLib::EigenVector GlobalVector
virtual ~NonlinearSolverBase()=default
virtual NonlinearSolverStatus solve(std::vector< GlobalVector * > &x, std::vector< GlobalVector * > const &x_prev, std::function< void(int, bool, std::vector< GlobalVector * > const &)> const &postIterationCallback, int const process_id)=0
virtual void calculateNonEquilibriumInitialResiduum(std::vector< GlobalVector * > const &x, std::vector< GlobalVector * > const &x_prev, int const process_id)=0
ConvergenceCriterion * _convergence_criterion
Convergence criterion used to terminate the Newton iteration.
std::size_t _J_id
ID of the Jacobian matrix.
std::size_t _x_new_id
ID of the vector storing .
std::size_t _res_id
ID of the residual vector.
GlobalVector * _r_neq
non-equilibrium initial residuum.
NonlinearSystem< NonlinearSolverTag::Newton > System
Type of the nonlinear equation system to be solved.
void setEquationSystem(System &eq, ConvergenceCriterion &conv_crit)
int const _maxiter
maximum number of iterations
int const _recompute_jacobian
Recompute Jacobian every this many steps.
std::unique_ptr< NewtonStepStrategy > _step_strategy
Globalization / step-acceptance strategy (e.g. fixed damping).
NonlinearSolver(GlobalLinearSolver &linear_solver, int const maxiter, std::unique_ptr< NewtonStepStrategy > newton_strategy, int const recompute_jacobian=1)
std::size_t _rhs_id
ID of the right-hand side vector.
const int _maxiter
maximum number of iterations
GlobalVector * _r_neq
non-equilibrium initial residuum.
NonlinearSolver(GlobalLinearSolver &linear_solver, const int maxiter)
void setEquationSystem(System &eq, ConvergenceCriterion &conv_crit)
NonlinearSystem< NonlinearSolverTag::Picard > System
Type of the nonlinear equation system to be solved.
NonlinearSolverTag
Tag used to specify which nonlinear solver will be used.
Definition Types.h:13
Status of the non-linear solver.