OGS
NonlinearSolver.h
Go to the documentation of this file.
1
11#pragma once
12
13#include <memory>
14#include <utility>
15
19#include "NonlinearSystem.h"
20#include "Types.h"
21
22namespace BaseLib
23{
24class ConfigTree;
25}
26
27// TODO Document in the ODE solver lib, which matrices and vectors that are
28// passed around as method arguments are guaranteed to be of the right size
29// (and zeroed out) and which are not.
30
31namespace NumLib
32{
37{
38public:
40 std::vector<GlobalVector*> const& x,
41 std::vector<GlobalVector*> const& x_prev, int const process_id) = 0;
42
54 std::vector<GlobalVector*>& x,
55 std::vector<GlobalVector*> const& x_prev,
56 std::function<void(int, std::vector<GlobalVector*> const&)> const&
57 postIterationCallback,
58 int const process_id) = 0;
59
60 virtual ~NonlinearSolverBase() = default;
61};
62
65
70template <NonlinearSolverTag NLTag>
72
75template <>
77 : public NonlinearSolverBase
78{
79public:
82
91 explicit NonlinearSolver(GlobalLinearSolver& linear_solver,
92 int const maxiter,
93 double const damping = 1.0)
94 : _linear_solver(linear_solver), _maxiter(maxiter), _damping(damping)
95 {
96 }
97
99
103 {
104 _equation_system = &eq;
105 _convergence_criterion = &conv_crit;
106 }
107
108 void calculateNonEquilibriumInitialResiduum(
109 std::vector<GlobalVector*> const& x,
110 std::vector<GlobalVector*> const& x_prev,
111 int const process_id) override;
112
114 std::vector<GlobalVector*>& x,
115 std::vector<GlobalVector*> const& x_prev,
116 std::function<void(int, std::vector<GlobalVector*> const&)> const&
117 postIterationCallback,
118 int const process_id) override;
119
121 {
122 _compensate_non_equilibrium_initial_residuum = value;
123 }
124
125private:
127 System* _equation_system = nullptr;
128
129 // TODO doc
130 ConvergenceCriterion* _convergence_criterion = nullptr;
131 int const _maxiter;
132
137 double const _damping;
138
139 GlobalVector* _r_neq = nullptr;
140 std::size_t _res_id = 0u;
141 std::size_t _J_id = 0u;
142 std::size_t _minus_delta_x_id = 0u;
143 std::size_t _x_new_id =
144 0u;
145 std::size_t _r_neq_id = 0u;
147
153 bool _compensate_non_equilibrium_initial_residuum = false;
154};
155
160template <>
162 : public NonlinearSolverBase
163{
164public:
167
174 explicit NonlinearSolver(GlobalLinearSolver& linear_solver,
175 const int maxiter)
176 : _linear_solver(linear_solver), _maxiter(maxiter)
177 {
178 }
179
181
185 {
186 _equation_system = &eq;
187 _convergence_criterion = &conv_crit;
188 }
189
190 void calculateNonEquilibriumInitialResiduum(
191 std::vector<GlobalVector*> const& x,
192 std::vector<GlobalVector*> const& x_prev,
193 int const process_id) override;
194
196 std::vector<GlobalVector*>& x,
197 std::vector<GlobalVector*> const& x_prev,
198 std::function<void(int, std::vector<GlobalVector*> const&)> const&
199 postIterationCallback,
200 int const process_id) override;
201
203 {
204 _compensate_non_equilibrium_initial_residuum = value;
205 }
206
207private:
209 System* _equation_system = nullptr;
210
211 // TODO doc
212 ConvergenceCriterion* _convergence_criterion = nullptr;
213 const int _maxiter;
214
215 GlobalVector* _r_neq = nullptr;
216 std::size_t _A_id = 0u;
217 std::size_t _rhs_id = 0u;
218 std::size_t _x_new_id = 0u;
220 std::size_t _r_neq_id = 0u;
222
223 // clang-format off
225 bool _compensate_non_equilibrium_initial_residuum = false;
226 // clang-format on
227};
228
239std::pair<std::unique_ptr<NonlinearSolverBase>, NonlinearSolverTag>
241 BaseLib::ConfigTree const& config);
242
244
245} // namespace NumLib
Global vector based on Eigen vector.
Definition EigenVector.h:25
virtual ~NonlinearSolverBase()=default
virtual void calculateNonEquilibriumInitialResiduum(std::vector< GlobalVector * > const &x, std::vector< GlobalVector * > const &x_prev, int const process_id)=0
virtual NonlinearSolverStatus solve(std::vector< GlobalVector * > &x, std::vector< GlobalVector * > const &x_prev, std::function< void(int, std::vector< GlobalVector * > const &)> const &postIterationCallback, int const process_id)=0
NonlinearSolver(GlobalLinearSolver &linear_solver, int const maxiter, double const damping=1.0)
void setEquationSystem(System &eq, ConvergenceCriterion &conv_crit)
int const _maxiter
maximum number of iterations
const int _maxiter
maximum number of iterations
NonlinearSolver(GlobalLinearSolver &linear_solver, const int maxiter)
void setEquationSystem(System &eq, ConvergenceCriterion &conv_crit)
NonlinearSolverTag
Tag used to specify which nonlinear solver will be used.
Definition Types.h:20
std::pair< std::unique_ptr< NonlinearSolverBase >, NonlinearSolverTag > createNonlinearSolver(GlobalLinearSolver &linear_solver, BaseLib::ConfigTree const &config)
Status of the non-linear solver.