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
12#include "NonlinearSystem.h"
13#include "Types.h"
14
15namespace BaseLib
16{
17class ConfigTree;
18}
19
20// TODO Document in the ODE solver lib, which matrices and vectors that are
21// passed around as method arguments are guaranteed to be of the right size
22// (and zeroed out) and which are not.
23
24namespace NumLib
25{
30{
31public:
33 std::vector<GlobalVector*> const& x,
34 std::vector<GlobalVector*> const& x_prev, int const process_id) = 0;
35
47 std::vector<GlobalVector*>& x,
48 std::vector<GlobalVector*> const& x_prev,
49 std::function<void(int, bool, std::vector<GlobalVector*> const&)> const&
50 postIterationCallback,
51 int const process_id) = 0;
52
53 virtual ~NonlinearSolverBase() = default;
54};
55
58
63template <NonlinearSolverTag NLTag>
65
68template <>
70 : public NonlinearSolverBase
71{
72public:
75
88 GlobalLinearSolver& linear_solver,
89 int const maxiter,
90 int const recompute_jacobian = 1,
91 double const damping = 1.0,
92 std::optional<double> const damping_reduction = std::nullopt)
93 : _linear_solver(linear_solver),
94 _maxiter(maxiter),
95 _recompute_jacobian(recompute_jacobian),
96 _damping(damping),
97 _damping_reduction(damping_reduction)
98 {
99 }
100
102
106 {
107 _equation_system = &eq;
108 _convergence_criterion = &conv_crit;
109 }
110
111 void calculateNonEquilibriumInitialResiduum(
112 std::vector<GlobalVector*> const& x,
113 std::vector<GlobalVector*> const& x_prev,
114 int const process_id) override;
115
117 std::vector<GlobalVector*>& x,
118 std::vector<GlobalVector*> const& x_prev,
119 std::function<void(int, bool, std::vector<GlobalVector*> const&)> const&
120 postIterationCallback,
121 int const process_id) override;
122
127
128private:
131
132 // TODO doc
134 int const _maxiter;
135
136 int const _recompute_jacobian = 1;
137
142 double const _damping;
143
147 std::optional<double> const _damping_reduction;
148
149 GlobalVector* _r_neq = nullptr;
150 std::size_t _res_id = 0u;
151 std::size_t _J_id = 0u;
152 std::size_t _minus_delta_x_id = 0u;
153 std::size_t _x_new_id =
154 0u;
155 std::size_t _r_neq_id = 0u;
157
164};
165
170template <>
172 : public NonlinearSolverBase
173{
174public:
177
184 explicit NonlinearSolver(GlobalLinearSolver& linear_solver,
185 const int maxiter)
186 : _linear_solver(linear_solver), _maxiter(maxiter)
187 {
188 }
189
191
195 {
196 _equation_system = &eq;
197 _convergence_criterion = &conv_crit;
198 }
199
200 void calculateNonEquilibriumInitialResiduum(
201 std::vector<GlobalVector*> const& x,
202 std::vector<GlobalVector*> const& x_prev,
203 int const process_id) override;
204
206 std::vector<GlobalVector*>& x,
207 std::vector<GlobalVector*> const& x_prev,
208 std::function<void(int, bool, std::vector<GlobalVector*> const&)> const&
209 postIterationCallback,
210 int const process_id) override;
211
216
217private:
220
221 // TODO doc
223 const int _maxiter;
224
225 GlobalVector* _r_neq = nullptr;
226 std::size_t _A_id = 0u;
227 std::size_t _rhs_id = 0u;
228 std::size_t _x_new_id = 0u;
230 std::size_t _r_neq_id = 0u;
232
233 // clang-format off
236 // clang-format on
237};
238
249std::pair<std::unique_ptr<NonlinearSolverBase>, NonlinearSolverTag>
251 BaseLib::ConfigTree const& config);
252
254
255} // 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
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)
NonlinearSolver(GlobalLinearSolver &linear_solver, int const maxiter, int const recompute_jacobian=1, double const damping=1.0, std::optional< double > const damping_reduction=std::nullopt)
int const _maxiter
maximum number of iterations
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
std::pair< std::unique_ptr< NonlinearSolverBase >, NonlinearSolverTag > createNonlinearSolver(GlobalLinearSolver &linear_solver, BaseLib::ConfigTree const &config)
Status of the non-linear solver.