OGS  master
NonlinearSolver.h
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include <memory>
14 #include <utility>
15 
16 #include "ConvergenceCriterion.h"
17 #include "NonlinearSolverStatus.h"
18 #include "NonlinearSystem.h"
19 #include "Types.h"
20 
21 namespace BaseLib
22 {
23 class ConfigTree;
24 }
25 
26 // TODO Document in the ODE solver lib, which matrices and vectors that are
27 // passed around as method arguments are guaranteed to be of the right size
28 // (and zeroed out) and which are not.
29 
30 namespace NumLib
31 {
36 {
37 public:
39  std::vector<GlobalVector*> const& x,
40  std::vector<GlobalVector*> const& x_prev, int const process_id) = 0;
41 
53  std::vector<GlobalVector*>& x,
54  std::vector<GlobalVector*> const& x_prev,
55  std::function<void(int, std::vector<GlobalVector*> const&)> const&
56  postIterationCallback,
57  int const process_id) = 0;
58 
59  virtual ~NonlinearSolverBase() = default;
60 };
61 
64 
69 template <NonlinearSolverTag NLTag>
71 
74 template <>
76  : public NonlinearSolverBase
77 {
78 public:
81 
90  explicit NonlinearSolver(GlobalLinearSolver& linear_solver,
91  int const maxiter,
92  double const damping = 1.0)
93  : _linear_solver(linear_solver), _maxiter(maxiter), _damping(damping)
94  {
95  }
96 
97  ~NonlinearSolver();
98 
102  {
103  _equation_system = &eq;
104  _convergence_criterion = &conv_crit;
105  }
106 
108  std::vector<GlobalVector*> const& x,
109  std::vector<GlobalVector*> const& x_prev,
110  int const process_id) override;
111 
112  NonlinearSolverStatus solve(
113  std::vector<GlobalVector*>& x,
114  std::vector<GlobalVector*> const& x_prev,
115  std::function<void(int, std::vector<GlobalVector*> const&)> const&
116  postIterationCallback,
117  int const process_id) override;
118 
120  {
121  _compensate_non_equilibrium_initial_residuum = value;
122  }
123 
124 private:
126  System* _equation_system = nullptr;
127 
128  // TODO doc
129  ConvergenceCriterion* _convergence_criterion = nullptr;
130  int const _maxiter;
131 
136  double const _damping;
137 
138  GlobalVector* _r_neq = nullptr;
139  std::size_t _res_id = 0u;
140  std::size_t _J_id = 0u;
141  std::size_t _minus_delta_x_id = 0u;
142  std::size_t _x_new_id =
143  0u;
144  std::size_t _r_neq_id = 0u;
146 
152  bool _compensate_non_equilibrium_initial_residuum = false;
153 };
154 
159 template <>
161  : public NonlinearSolverBase
162 {
163 public:
166 
173  explicit NonlinearSolver(GlobalLinearSolver& linear_solver,
174  const int maxiter)
175  : _linear_solver(linear_solver), _maxiter(maxiter)
176  {
177  }
178 
179  ~NonlinearSolver();
180 
184  {
185  _equation_system = &eq;
186  _convergence_criterion = &conv_crit;
187  }
188 
190  std::vector<GlobalVector*> const& x,
191  std::vector<GlobalVector*> const& x_prev,
192  int const process_id) override;
193 
194  NonlinearSolverStatus solve(
195  std::vector<GlobalVector*>& x,
196  std::vector<GlobalVector*> const& x_prev,
197  std::function<void(int, std::vector<GlobalVector*> const&)> const&
198  postIterationCallback,
199  int const process_id) override;
200 
202  {
203  _compensate_non_equilibrium_initial_residuum = value;
204  }
205 
206 private:
208  System* _equation_system = nullptr;
209 
210  // TODO doc
211  ConvergenceCriterion* _convergence_criterion = nullptr;
212  const int _maxiter;
213 
214  GlobalVector* _r_neq = nullptr;
215  std::size_t _A_id = 0u;
216  std::size_t _rhs_id = 0u;
217  std::size_t _x_new_id = 0u;
219  std::size_t _r_neq_id = 0u;
221 
222  // clang-format off
224  bool _compensate_non_equilibrium_initial_residuum = false;
225  // clang-format on
226 };
227 
238 std::pair<std::unique_ptr<NonlinearSolverBase>, NonlinearSolverTag>
240  BaseLib::ConfigTree const& config);
241 
243 
244 } // namespace NumLib
Global vector based on Eigen vector.
Definition: EigenVector.h:26
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)
void calculateNonEquilibriumInitialResiduum(std::vector< std::unique_ptr< ProcessData >> const &per_process_data, std::vector< GlobalVector * > process_solutions, std::vector< GlobalVector * > const &process_solutions_prev)
Definition: TimeLoop.cpp:251
Status of the non-linear solver.