Reference documentation for deal.II version 9.0.0
kinsol.h
1 //-----------------------------------------------------------
2 //
3 // Copyright (C) 2017 - 2018 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 //---------------------------------------------------------------
15 
16 #ifndef dealii_sundials_kinsol_h
17 #define dealii_sundials_kinsol_h
18 
19 #include <deal.II/base/config.h>
20 #ifdef DEAL_II_WITH_SUNDIALS
21 
22 #include <deal.II/base/logstream.h>
23 #include <deal.II/base/exceptions.h>
24 #include <deal.II/base/parameter_handler.h>
25 #include <deal.II/base/conditional_ostream.h>
26 #include <deal.II/base/mpi.h>
27 #include <deal.II/lac/vector.h>
28 #include <deal.II/lac/vector_memory.h>
29 
30 #include <kinsol/kinsol.h>
31 #include <kinsol/kinsol_impl.h>
32 #include <nvector/nvector_serial.h>
33 #include <sundials/sundials_math.h>
34 #include <sundials/sundials_types.h>
35 
36 #include <boost/signals2.hpp>
37 #include <memory>
38 
39 
40 DEAL_II_NAMESPACE_OPEN
41 
42 // Shorthand notation for KINSOL error codes.
43 #define AssertKINSOL(code) Assert(code >= 0, ExcKINSOLError(code))
44 
45 namespace SUNDIALS
46 {
195  template<typename VectorType=Vector<double> >
196  class KINSOL
197  {
198  public:
199 
204  {
205  public:
211  {
215  newton = KIN_NONE,
219  linesearch = KIN_LINESEARCH,
223  fixed_point = KIN_FP,
227  picard = KIN_PICARD,
228  };
229 
256  // Global parameters
258  const unsigned int &maximum_non_linear_iterations = 200,
259  const double &function_tolerance = 0.0,
260  const double &step_tolerance = 0.0,
261  const bool &no_init_setup = false,
262  const unsigned int &maximum_setup_calls = 0,
263  const double &maximum_newton_step = 0.0,
264  const double &dq_relative_error = 0.0,
265  const unsigned int &maximum_beta_failures = 0,
266  const unsigned int &anderson_subspace_size = 0) :
277  {}
278 
317  {
318  static std::string strategy_str("newton");
319  prm.add_parameter("Solution strategy", strategy_str,
320  "Choose among newton|linesearch|fixed_point|picard",
321  Patterns::Selection("newton|linesearch|fixed_point|picard"));
322  prm.add_action("Solution strategy", [&](const std::string &value)
323  {
324  if (value == "newton")
325  strategy = newton;
326  else if (value == "linesearch")
328  else if (value == "fixed_point")
330  else if (value == "picard")
331  strategy = picard;
332  else
333  Assert(false, ExcInternalError());
334  }
335  );
336  prm.add_parameter("Maximum number of nonlinear iterations",
338  prm.add_parameter("Function norm stopping tolerance",
340  prm.add_parameter("Scaled step stopping tolerance",
342 
343  prm.enter_subsection("Newton parameters");
344  prm.add_parameter("No initial matrix setup",
345  no_init_setup);
346  prm.add_parameter("Maximum iterations without matrix setup",
348  prm.add_parameter("Maximum allowable scaled length of the Newton step",
350  prm.add_parameter("Relative error for different quotient computation",
352  prm.leave_subsection();
353 
354  prm.enter_subsection("Linesearch parameters");
355  prm.add_parameter("Maximum number of beta-condition failures",
357  prm.leave_subsection();
358 
359 
360  prm.enter_subsection("Fixed point and Picard parameters");
361  prm.add_parameter("Anderson acceleration subspace size",
363  prm.leave_subsection();
364  }
365 
374 
379 
387 
395 
405 
412  unsigned int maximum_setup_calls;
413 
420 
429 
435  unsigned int maximum_beta_failures;
436 
444  };
445 
455  const MPI_Comm mpi_comm = MPI_COMM_WORLD);
456 
460  ~KINSOL();
461 
467  unsigned int solve(VectorType &initial_guess_and_solution);
468 
473  std::function<void(VectorType &)> reinit_vector;
474 
487  std::function<int(const VectorType &src,
488  VectorType &dst)> residual;
489 
503  std::function<int(const VectorType &src,
504  VectorType &dst)> iteration_function;
505 
506 
547  std::function<int(const VectorType &current_u,
548  const VectorType &current_f)> setup_jacobian;
549 
586  std::function<int(const VectorType &ycur,
587  const VectorType &fcur,
588  const VectorType &rhs,
589  VectorType &dst)> solve_jacobian_system;
590 
597  std::function<VectorType&()> get_solution_scaling;
598 
606  std::function<VectorType&()> get_function_scaling;
607 
611  DeclException1(ExcKINSOLError, int, << "One of the SUNDIALS KINSOL internal functions "
612  << " returned a negative error code: "
613  << arg1 << ". Please consult SUNDIALS manual.");
614 
615 
616  private:
617 
622  << "Please provide an implementation for the function \"" << arg1 << "\"");
623 
630 
635 
639  void *kinsol_mem;
640 
644  N_Vector solution;
645 
649  N_Vector u_scale;
650 
654  N_Vector f_scale;
655 
659  MPI_Comm communicator;
660 
665  };
666 
667 }
668 
669 
670 DEAL_II_NAMESPACE_CLOSE
671 #endif
672 
673 
674 #endif
N_Vector solution
Definition: kinsol.h:644
unsigned int maximum_non_linear_iterations
Definition: kinsol.h:378
std::function< int(const VectorType &src, VectorType &dst)> residual
Definition: kinsol.h:488
unsigned int maximum_setup_calls
Definition: kinsol.h:412
static ::ExceptionBase & ExcKINSOLError(int arg1)
std::function< int(const VectorType &src, VectorType &dst)> iteration_function
Definition: kinsol.h:504
void set_functions_to_trigger_an_assert()
Definition: kinsol.cc:340
AdditionalData data
Definition: kinsol.h:634
SolutionStrategy strategy
Definition: kinsol.h:373
N_Vector u_scale
Definition: kinsol.h:649
MPI_Comm communicator
Definition: kinsol.h:659
unsigned int anderson_subspace_size
Definition: kinsol.h:443
N_Vector f_scale
Definition: kinsol.h:654
void add_parameters(ParameterHandler &prm)
Definition: kinsol.h:316
void enter_subsection(const std::string &subsection)
std::function< int(const VectorType &ycur, const VectorType &fcur, const VectorType &rhs, VectorType &dst)> solve_jacobian_system
Definition: kinsol.h:589
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:346
#define Assert(cond, exc)
Definition: exceptions.h:1142
GrowingVectorMemory< VectorType > mem
Definition: kinsol.h:664
static ::ExceptionBase & ExcFunctionNotProvided(std::string arg1)
AdditionalData(const SolutionStrategy &strategy=linesearch, const unsigned int &maximum_non_linear_iterations=200, const double &function_tolerance=0.0, const double &step_tolerance=0.0, const bool &no_init_setup=false, const unsigned int &maximum_setup_calls=0, const double &maximum_newton_step=0.0, const double &dq_relative_error=0.0, const unsigned int &maximum_beta_failures=0, const unsigned int &anderson_subspace_size=0)
Definition: kinsol.h:255
std::function< VectorType &()> get_function_scaling
Definition: kinsol.h:606
unsigned int maximum_beta_failures
Definition: kinsol.h:435
void * kinsol_mem
Definition: kinsol.h:639
std::function< void(VectorType &)> reinit_vector
Definition: kinsol.h:473
std::function< VectorType &()> get_solution_scaling
Definition: kinsol.h:597
KINSOL(const AdditionalData &data=AdditionalData(), const MPI_Comm mpi_comm=MPI_COMM_WORLD)
Definition: kinsol.cc:148
void add_parameter(const std::string &entry, ParameterType &parameter, const std::string &documentation=std::string(), const Patterns::PatternBase &pattern= *Patterns::Tools::Convert< ParameterType >::to_pattern())
void add_action(const std::string &entry, const std::function< void(const std::string &value)> &action)
std::function< int(const VectorType &current_u, const VectorType &current_f)> setup_jacobian
Definition: kinsol.h:548
unsigned int solve(VectorType &initial_guess_and_solution)
Definition: kinsol.cc:181
static ::ExceptionBase & ExcInternalError()