HermesCommon  3.0
nonlinear_matrix_solver.h
Go to the documentation of this file.
1 // This file is part of HermesCommon
2 //
3 // Copyright (c) 2009 hp-FEM group at the University of Nevada, Reno (UNR).
4 // Email: hpfem-group@unr.edu, home page: http://www.hpfem.org/.
5 //
6 // Hermes2D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published
8 // by the Free Software Foundation; either version 2 of the License,
9 // or (at your option) any later version.
10 //
11 // Hermes2D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with Hermes2D; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #ifndef __HERMES_COMMON_NONLINEAR_MATRIX_SOLVER_H_
23 #define __HERMES_COMMON_NONLINEAR_MATRIX_SOLVER_H_
24 
25 #include "precond.h"
26 #include "exceptions.h"
27 #include "algebra/cs_matrix.h"
28 #include "algebra/vector.h"
29 #include "mixins.h"
30 #include "algebra/algebra_mixins.h"
32 #include "solvers/matrix_solver.h"
33 
34 namespace Hermes
35 {
36  namespace Solvers
37  {
41 
44  {
45  ResidualNormRelativeToPrevious = 0x0001,
46  ResidualNormRatioToInitial = 0x0002,
47  ResidualNormRatioToPrevious = 0x0004,
48  ResidualNormAbsolute = 0x0008,
49  SolutionChangeAbsolute = 0x0010,
50  SolutionChangeRelative = 0x0020
51  };
52 
55  {
56  Converged,
57  NotConverged,
58  BelowMinDampingCoeff,
59  AboveMaxAllowedResidualNorm,
60  AboveMaxIterations,
61  Error
62  };
63 
64  template<typename Scalar> class HERMES_API NonlinearConvergenceMeasurement;
65 
68  template <typename Scalar>
69  class NonlinearMatrixSolver :
70  public virtual Hermes::Solvers::MatrixSolver<Scalar>,
71  public virtual Hermes::Mixins::OutputAttachable,
72  public virtual Hermes::Mixins::StateQueryable,
73  public virtual Hermes::Mixins::TimeMeasurable
74  {
75  public:
77  virtual ~NonlinearMatrixSolver();
78 
81  virtual void solve(Scalar* coeff_vec);
82 
85 
87  virtual void clear_tolerances();
88 
91  void set_max_allowed_residual_norm(double max_allowed_residual_norm_to_set);
92 
97  void set_tolerance(double newton_tol, NonlinearConvergenceMeasurementType toleranceType, bool handleMultipleTolerancesAnd = false);
98 
100  int get_num_iters() const;
101 
102 #pragma region damping-public
103  void set_min_allowed_damping_coeff(double min_allowed_damping_coeff_to_set);
106 
111  void set_manual_damping_coeff(bool onOff, double coeff);
112 
117  void set_initial_auto_damping_coeff(double coeff);
118 
125  void set_auto_damping_ratio(double ratio);
126 
132  void set_sufficient_improvement_factor(double ratio);
133 
139  void set_necessary_successful_steps_to_increase(unsigned int steps);
140 #pragma endregion
141 
142 #pragma region jacobian_recalculation-public
147 
150  void set_max_steps_with_reused_jacobian(unsigned int steps);
151 #pragma endregion
152 
154  virtual void free();
155 
156  protected:
157 
158 #pragma region damping-private
159  bool manual_damping;
161 
164 
176 #pragma endregion
177 
178 #pragma region jacobian_recalculation-private
179  bool force_reuse_jacobian_values(unsigned int& successful_steps_with_reused_jacobian);
182  bool jacobian_reused_okay(unsigned int& successful_steps_with_reused_jacobian);
183 
184  double sufficient_improvement_factor_jacobian;
185  unsigned int max_steps_with_reused_jacobian;
186 
189 #pragma endregion
190 
191  virtual void assemble_residual(bool store_previous_residual) = 0;
192  virtual bool assemble_jacobian(bool store_previous_jacobian) = 0;
193  virtual bool assemble(bool store_previous_jacobian, bool store_previous_residual) = 0;
194 
196  virtual void on_damping_factor_updated();
198  virtual void on_reused_jacobian_step_begin();
200  virtual void on_reused_jacobian_step_end();
201 
205 
208  virtual void init_solving(Scalar* coeff_vec);
209 
212 
215 
217  virtual void solve_linear_system();
218 
221  virtual double update_solution_return_change_norm(Scalar* linear_system_solution) = 0;
222 
224  void finalize_solving();
225 
227  virtual void deinit_solving();
228 
230  bool calculate_damping_factor(unsigned int& successful_steps);
231 
233  virtual bool damping_factor_condition();
234 
237 
239  void step_info();
240 
242  virtual double calculate_residual_norm();
243 
245  virtual bool isOkay() const;
246  inline std::string getClassName() const { return "NonlinearMatrixSolver"; }
247 
249  void init_nonlinear();
250 
255 
258 
261 
264 
268 
271 
274  Vector<Scalar>* previous_residual;
275 
276 #pragma region OutputAttachable
277  // For derived classes - read-only access.
278  const OutputParameterUnsignedInt& iteration() const { return this->p_iteration; };
279  const OutputParameterDoubleVector& residual_norms() const { return this->p_residual_norms; };
280  const OutputParameterDoubleVector& solution_norms() const { return this->p_solution_norms; };
281  const OutputParameterDoubleVector& solution_change_norms() const { return this->p_solution_change_norms; };
282  const OutputParameterUnsignedInt& successful_steps_damping() const { return this->p_successful_steps_damping; };
283  const OutputParameterUnsignedInt& successful_steps_jacobian() const { return this->p_successful_steps_jacobian; };
284  const OutputParameterDoubleVector& damping_factors() const { return this->p_damping_factors; };
285  const OutputParameterBool& residual_norm_drop() const { return this->p_residual_norm_drop; };
286  const OutputParameterBoolVector& iterations_with_recalculated_jacobian() const { return this->p_iterations_with_recalculated_jacobian; };
287 
291  OutputParameterDoubleVector p_solution_norms;
292  OutputParameterDoubleVector p_solution_change_norms;
293  OutputParameterBoolVector p_iterations_with_recalculated_jacobian;
294  OutputParameterUnsignedInt p_successful_steps_damping;
295  OutputParameterUnsignedInt p_successful_steps_jacobian;
296  OutputParameterDoubleVector p_damping_factors;
297  OutputParameterBool p_residual_norm_drop;
298  OutputParameterUnsignedInt p_iteration;
299 #pragma endregion
300 
301  Scalar* previous_sln_vector;
302  bool use_initial_guess_for_iterative_solvers;
303  friend class NonlinearConvergenceMeasurement < Scalar > ;
304  };
305  }
306 
307  namespace Exceptions
308  {
310  {
311  public:
313 
314  Hermes::Solvers::NonlinearConvergenceState get_exception_state();
315 
316  protected:
318  };
319  }
320 }
321 #endif
General namespace for the Hermes library.
General (linear/nonlinear) matrix solver functionality.
int max_allowed_iterations
Maximum number of iterations allowed.
unsigned int necessary_successful_steps_to_increase
necessary number of steps to increase back the damping coeff.
void set_max_allowed_residual_norm(double max_allowed_residual_norm_to_set)
virtual bool isOkay() const
State querying helpers.
Exception interface Basically a std::exception, but with a constructor with string and with print_msg...
Definition: exceptions.h:49
bool tolerance_set[NonlinearConvergenceMeasurementTypeCount]
info about set tolerances.
NonlinearConvergenceMeasurementType
This specifies the quantity that is compared to newton_tolerance (settable by set_tolerance()).
bool force_reuse_jacobian_values(unsigned int &successful_steps_with_reused_jacobian)
For deciding if the jacobian is reused at this point.
General (abstract) vector representation in Hermes.
int get_current_iteration_number()
Shortcut method for getting the current iteration.
void set_manual_damping_coeff(bool onOff, double coeff)
virtual double update_solution_return_change_norm(Scalar *linear_system_solution)=0
std::string getClassName() const
Get class name, for the purpose of messaging.
General (abstract) sparse matrix representation in Hermes.
virtual void free()
Frees the instances.
Vector< Scalar > * residual_back
Backup vector for unsuccessful reuse of Jacobian.
void set_min_allowed_damping_coeff(double min_allowed_damping_coeff_to_set)
Class using time measurement Can be used directly (is not abstract), so one can use e...
Definition: mixins.h:183
SparseMatrix< Scalar > * previous_jacobian
Previous structures (e.g. in Picard's residual calculation)
Basic cs (Compressed sparse) matrix classes and operations.
Base class for defining interface for nonlinear solvers.
int get_num_iters() const
Get the number of iterations.
virtual double calculate_residual_norm()
Norm for convergence.
double min_allowed_damping_coeff
Minimum allowed damping coeff.
virtual bool handle_convergence_state_return_finished(NonlinearConvergenceState state)
int num_iters
To be filled and returned on demand.
NonlinearConvergenceState
Nonlinear Convergence state.
interface for both linear and nonlinear algebraic solvers.
Definition: matrix_solver.h:35
bool calculate_damping_factor(unsigned int &successful_steps)
Calculates the new_ damping coefficient.
void set_max_allowed_iterations(int max_allowed_iterations)
Set the maximum number of iterations, thus co-determine when to stop iterations.
File containing definition of exceptions classes.
nonlinear_convergence_measurement.
void step_info()
Output info about the step.
Mixins classes for algebraic purposes.
virtual void init_solving(Scalar *coeff_vec)
double sufficient_improvement_factor
Sufficient improvement for continuing.
double tolerance[NonlinearConvergenceMeasurementTypeCount]
Tolerances for all NonlinearConvergenceMeasurementType numbered sequentially as the enum NonlinearCon...
double initial_auto_damping_factor
The initial (and maximum) damping coefficient.
Class that allows for attaching any method to particular parts of its functionality. Internal.
Definition: mixins.h:271
Basic vector classes and operations.
virtual bool damping_factor_condition()
Returns iff the damping factor condition is fulfilled.
General functionality for preconditioners. Contains class Precond.
Mix-in classes for one functionality, for various classes to be derived from.
virtual NonlinearConvergenceState get_convergence_state()
Find out the convergence state.
bool jacobian_reused_okay(unsigned int &successful_steps_with_reused_jacobian)
For deciding if the reused jacobian did not bring residual increase at this point.
const int NonlinearConvergenceMeasurementTypeCount
void init_nonlinear()
Shared code for constructors.
void set_necessary_successful_steps_to_increase(unsigned int steps)
virtual void clear_tolerances()
Clear (reset) all tolerances.
void set_tolerance(double newton_tol, NonlinearConvergenceMeasurementType toleranceType, bool handleMultipleTolerancesAnd=false)
void set_max_steps_with_reused_jacobian(unsigned int steps)
virtual void solve_linear_system()
Solve the step's linear system.