OGS 6.2.2-87-g988ee9c30.dirty.20200123122242
NonlinearSolver.h
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include <memory>
14 #include <utility>
15 #include <logog/include/logog.hpp>
16 
17 #include "ConvergenceCriterion.h"
18 #include "NonlinearSolverStatus.h"
19 #include "NonlinearSystem.h"
20 #include "Types.h"
21 
22 namespace BaseLib
23 {
24 class 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 
31 namespace NumLib
32 {
37 {
38 public:
49  virtual void assemble(std::vector<GlobalVector*> const& x,
50  int const process_id) const = 0;
51 
53  std::vector<GlobalVector*> const& x, int const process_id) = 0;
54 
64  virtual NonlinearSolverStatus solve(
65  std::vector<GlobalVector*>& x,
66  std::function<void(int, std::vector<GlobalVector*> const&)> const&
67  postIterationCallback,
68  int const process_id) = 0;
69 
70  virtual ~NonlinearSolverBase() = default;
71 };
72 
75 
80 template <NonlinearSolverTag NLTag>
82 
85 template <>
87  : public NonlinearSolverBase
88 {
89 public:
92 
101  explicit NonlinearSolver(GlobalLinearSolver& linear_solver,
102  int const maxiter,
103  double const damping = 1.0)
104  : _linear_solver(linear_solver), _maxiter(maxiter), _damping(damping)
105  {
106  }
107 
111  {
112  _equation_system = &eq;
113  _convergence_criterion = &conv_crit;
114  }
115 
116  void assemble(std::vector<GlobalVector*> const& x,
117  int const process_id) const override;
118 
120  std::vector<GlobalVector*> const& x, int const process_id) override;
121 
122  NonlinearSolverStatus solve(
123  std::vector<GlobalVector*>& x,
124  std::function<void(int, std::vector<GlobalVector*> const&)> const&
125  postIterationCallback,
126  int const process_id) override;
127 
129  {
130  _compensate_non_equilibrium_initial_residuum = value;
131  }
132 
133 private:
134  GlobalLinearSolver& _linear_solver;
135  System* _equation_system = nullptr;
136 
137  // TODO doc
138  ConvergenceCriterion* _convergence_criterion = nullptr;
139  int const _maxiter;
140 
145  double const _damping;
146 
147  GlobalVector* _r_neq = nullptr;
148  std::size_t _res_id = 0u;
149  std::size_t _J_id = 0u;
150  std::size_t _minus_delta_x_id = 0u;
151  std::size_t _x_new_id =
152  0u;
153 
159  bool _compensate_non_equilibrium_initial_residuum = false;
160 };
161 
166 template <>
168  : public NonlinearSolverBase
169 {
170 public:
173 
180  explicit NonlinearSolver(GlobalLinearSolver& linear_solver,
181  const int maxiter)
182  : _linear_solver(linear_solver), _maxiter(maxiter)
183  {
184  }
185 
189  {
190  _equation_system = &eq;
191  _convergence_criterion = &conv_crit;
192  }
193 
194  void assemble(std::vector<GlobalVector*> const& x,
195  int const process_id) const override;
196 
198  std::vector<GlobalVector*> const& x, int const process_id) override;
199 
200  NonlinearSolverStatus solve(
201  std::vector<GlobalVector*>& x,
202  std::function<void(int, std::vector<GlobalVector*> const&)> const&
203  postIterationCallback,
204  int const process_id) override;
205 
207  {
208  _compensate_non_equilibrium_initial_residuum = value;
209  }
210 
211 private:
212  GlobalLinearSolver& _linear_solver;
213  System* _equation_system = nullptr;
214 
215  // TODO doc
216  ConvergenceCriterion* _convergence_criterion = nullptr;
217  const int _maxiter;
218 
219  GlobalVector* _r_neq = nullptr;
220  std::size_t _A_id = 0u;
221  std::size_t _rhs_id = 0u;
222  std::size_t _x_new_id = 0u;
223 
225  // clang-format off
227  bool _compensate_non_equilibrium_initial_residuum = false;
228  // clang-format on
229 };
230 
241 std::pair<std::unique_ptr<NonlinearSolverBase>, NonlinearSolverTag>
243  GlobalLinearSolver& linear_solver,
244  BaseLib::ConfigTree const& config);
245 
247 
248 } // namespace NumLib
const int _maxiter
maximum number of iterations
std::pair< std::unique_ptr< NonlinearSolverBase >, NonlinearSolverTag > createNonlinearSolver(GlobalLinearSolver &linear_solver, BaseLib::ConfigTree const &config)
NonlinearSolver(GlobalLinearSolver &linear_solver, int const maxiter, double const damping=1.0)
int const _maxiter
maximum number of iterations
NonlinearSolverTag
Tag used to specify which nonlinear solver will be used.
Definition: Types.h:20
Status of the non-linear solver.
NonlinearSolver(GlobalLinearSolver &linear_solver, const int maxiter)
Functionality to build different search length algorithm objects from given config.
void setEquationSystem(System &eq, ConvergenceCriterion &conv_crit)
void calculateNonEquilibriumInitialResiduum(std::vector< std::unique_ptr< ProcessData >> const &per_process_data, std::vector< GlobalVector *> process_solutions)
Definition: TimeLoop.cpp:170
void setEquationSystem(System &eq, ConvergenceCriterion &conv_crit)