Reference documentation for deal.II version 9.0.0
ida.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_ida_h
17 #define dealii_sundials_ida_h
18 
19 #include <deal.II/base/config.h>
20 #include <deal.II/base/mpi.h>
21 #ifdef DEAL_II_WITH_SUNDIALS
22 
23 #include <deal.II/base/logstream.h>
24 #include <deal.II/base/exceptions.h>
25 #include <deal.II/base/parameter_handler.h>
26 #include <deal.II/base/conditional_ostream.h>
27 #include <deal.II/base/mpi.h>
28 #ifdef DEAL_II_WITH_PETSC
29 # include <deal.II/lac/petsc_parallel_block_vector.h>
30 # include <deal.II/lac/petsc_parallel_vector.h>
31 #endif
32 #include <deal.II/lac/vector.h>
33 #include <deal.II/lac/vector_memory.h>
34 
35 #ifdef DEAL_II_SUNDIALS_WITH_IDAS
36 #include <idas/idas.h>
37 #else
38 #include <ida/ida.h>
39 #endif
40 
41 #include <sundials/sundials_config.h>
42 #if DEAL_II_SUNDIALS_VERSION_LT(3,0,0)
43 # include <ida/ida_spgmr.h>
44 # include <ida/ida_spbcgs.h>
45 # include <ida/ida_sptfqmr.h>
46 #endif
47 #include <nvector/nvector_serial.h>
48 #include <sundials/sundials_math.h>
49 #include <sundials/sundials_types.h>
50 
51 #include <boost/signals2.hpp>
52 #include <memory>
53 
54 
55 DEAL_II_NAMESPACE_OPEN
56 
57 // Shorthand notation for IDA error codes.
58 #define AssertIDA(code) Assert(code >= 0, ExcIDAError(code))
59 
60 namespace SUNDIALS
61 {
62 
235  template<typename VectorType=Vector<double> >
236  class IDA
237  {
238  public:
239 
244  {
245  public:
256  {
260  none = 0,
261 
269 
274  };
275 
304  AdditionalData(// Initial parameters
305  const double &initial_time = 0.0,
306  const double &final_time = 1.0,
307  const double &initial_step_size = 1e-2,
308  const double &output_period = 1e-1,
309  // Running parameters
310  const double &minimum_step_size = 1e-6,
311  const unsigned int &maximum_order = 5,
312  const unsigned int &maximum_non_linear_iterations = 10,
313  // Error parameters
314  const double &absolute_tolerance = 1e-6,
315  const double &relative_tolerance = 1e-5,
316  const bool &ignore_algebraic_terms_for_errors = true,
317  // Initial conditions parameters
320  const unsigned int &maximum_non_linear_iterations_ic = 5) :
330  ic_type(ic_type),
334  {}
335 
378  {
379  prm.add_parameter("Initial time", initial_time);
380  prm.add_parameter("Final time", final_time);
381  prm.add_parameter("Time interval between each output", output_period);
382 
383  prm.enter_subsection("Running parameters");
384  prm.add_parameter("Initial step size",initial_step_size);
385  prm.add_parameter("Minimum step size", minimum_step_size);
386  prm.add_parameter("Maximum order of BDF", maximum_order);
387  prm.add_parameter("Maximum number of nonlinear iterations", maximum_non_linear_iterations);
388  prm.leave_subsection();
389 
390  prm.enter_subsection("Error control");
391  prm.add_parameter("Absolute error tolerance", absolute_tolerance);
392  prm.add_parameter("Relative error tolerance", relative_tolerance);
393  prm.add_parameter("Ignore algebraic terms for error computations", ignore_algebraic_terms_for_errors,
394  "Indicate whether or not to suppress algebraic variables "
395  "in the local error test.");
396  prm.leave_subsection();
397 
398  prm.enter_subsection("Initial condition correction parameters");
399  static std::string ic_type_str="use_y_diff";
400  prm.add_parameter("Correction type at initial time", ic_type_str,
401  "This is one of the following three options for the "
402  "initial condition calculation. \n"
403  " none: do not try to make initial conditions consistent. \n"
404  " use_y_diff: compute the algebraic components of y and differential\n"
405  " components of y_dot, given the differential components of y. \n"
406  " This option requires that the user specifies differential and \n"
407  " algebraic components in the function get_differential_components.\n"
408  " use_y_dot: compute all components of y, given y_dot.",
409  Patterns::Selection("none|use_y_diff|use_y_dot"));
410  prm.add_action("Correction type at initial time",[&](const std::string &value)
411  {
412  if (value == "use_y_diff")
414  else if (value == "use_y_dot")
415  ic_type = use_y_dot;
416  else if (value == "none")
417  ic_type = none;
418  else
419  AssertThrow(false, ExcInternalError());
420  });
421 
422  static std::string reset_type_str="use_y_diff";
423  prm.add_parameter("Correction type after restart", reset_type_str,
424  "This is one of the following three options for the "
425  "initial condition calculation. \n"
426  " none: do not try to make initial conditions consistent. \n"
427  " use_y_diff: compute the algebraic components of y and differential\n"
428  " components of y_dot, given the differential components of y. \n"
429  " This option requires that the user specifies differential and \n"
430  " algebraic components in the function get_differential_components.\n"
431  " use_y_dot: compute all components of y, given y_dot.",
432  Patterns::Selection("none|use_y_diff|use_y_dot"));
433  prm.add_action("Correction type after restart",[&](const std::string &value)
434  {
435  if (value == "use_y_diff")
437  else if (value == "use_y_dot")
439  else if (value == "none")
440  reset_type = none;
441  else
442  AssertThrow(false, ExcInternalError());
443  });
444  prm.add_parameter("Maximum number of nonlinear iterations", maximum_non_linear_iterations_ic);
445  prm.leave_subsection();
446  }
447 
451  double initial_time;
452 
456  double final_time;
457 
462 
467 
472 
477 
481  unsigned int maximum_order;
482 
487 
492 
508 
519 
524 
529  };
530 
568  const MPI_Comm mpi_comm = MPI_COMM_WORLD);
569 
573  ~IDA();
574 
579  unsigned int solve_dae(VectorType &solution,
580  VectorType &solution_dot);
581 
603  void reset(const double &t,
604  const double &h,
605  VectorType &y,
606  VectorType &yp);
607 
611  std::function<void(VectorType &)> reinit_vector;
612 
623  std::function<int(const double t,
624  const VectorType &y,
625  const VectorType &y_dot,
626  VectorType &res)> residual;
627 
658  std::function<int(const double t,
659  const VectorType &y,
660  const VectorType &y_dot,
661  const double alpha)> setup_jacobian;
662 
691  std::function<int(const VectorType &rhs, VectorType &dst)> solve_jacobian_system;
692 
707  std::function<void (const double t,
708  const VectorType &sol,
709  const VectorType &sol_dot,
710  const unsigned int step_number)> output_step;
711 
728  std::function<bool (const double t,
729  VectorType &sol,
730  VectorType &sol_dot)> solver_should_restart;
731 
740  std::function<IndexSet()> differential_components;
741 
748  std::function<VectorType&()> get_local_tolerances;
749 
753  DeclException1(ExcIDAError, int, << "One of the SUNDIALS IDA internal functions "
754  << " returned a negative error code: "
755  << arg1 << ". Please consult SUNDIALS manual.");
756 
757 
758  private:
759 
763  DeclException1(ExcFunctionNotProvided, std::string,
764  << "Please provide an implementation for the function \"" << arg1 << "\"");
765 
772 
777 
781  void *ida_mem;
782 
786  N_Vector yy;
787 
791  N_Vector yp;
792 
796  N_Vector abs_tolls;
797 
801  N_Vector diff_id;
802 
808  MPI_Comm communicator;
809 
814 
815 #ifdef DEAL_II_WITH_PETSC
816 #ifdef PETSC_USE_COMPLEX
817  static_assert(!std::is_same<VectorType, PETScWrappers::MPI::Vector>::value,
818  "Sundials does not support complex scalar types, "
819  "but PETSc is configured to use a complex scalar type!");
820 
821  static_assert(!std::is_same<VectorType, PETScWrappers::MPI::BlockVector>::value,
822  "Sundials does not support complex scalar types, "
823  "but PETSc is configured to use a complex scalar type!");
824 #endif // PETSC_USE_COMPLEX
825 #endif // DEAL_II_WITH_PETSC
826  };
827 
828 }
829 
830 DEAL_II_NAMESPACE_CLOSE
831 
832 #endif // DEAL_II_WITH_SUNDIALS
833 
834 #endif
void set_functions_to_trigger_an_assert()
Definition: ida.cc:448
GrowingVectorMemory< VectorType > mem
Definition: ida.h:813
std::function< void(VectorType &)> reinit_vector
Definition: ida.h:611
std::function< IndexSet()> differential_components
Definition: ida.h:740
unsigned int maximum_order
Definition: ida.h:481
N_Vector yy
Definition: ida.h:786
DeclException1(ExcIDAError, int,<< "One of the SUNDIALS IDA internal functions "<< " returned a negative error code: "<< arg1<< ". Please consult SUNDIALS manual.")
void add_parameters(ParameterHandler &prm)
Definition: ida.h:377
MPI_Comm communicator
Definition: ida.h:808
unsigned maximum_non_linear_iterations_ic
Definition: ida.h:523
N_Vector yp
Definition: ida.h:791
void reset(const double &t, const double &h, VectorType &y, VectorType &yp)
Definition: ida.cc:280
std::function< int(const double t, const VectorType &y, const VectorType &y_dot, VectorType &res)> residual
Definition: ida.h:626
InitialConditionCorrection reset_type
Definition: ida.h:518
#define AssertThrow(cond, exc)
Definition: exceptions.h:1221
IDA(const AdditionalData &data=AdditionalData(), const MPI_Comm mpi_comm=MPI_COMM_WORLD)
Definition: ida.cc:146
N_Vector diff_id
Definition: ida.h:801
void enter_subsection(const std::string &subsection)
std::function< void(const double t, const VectorType &sol, const VectorType &sol_dot, const unsigned int step_number)> output_step
Definition: ida.h:710
bool ignore_algebraic_terms_for_errors
Definition: ida.h:491
AdditionalData(const double &initial_time=0.0, const double &final_time=1.0, const double &initial_step_size=1e-2, const double &output_period=1e-1, const double &minimum_step_size=1e-6, const unsigned int &maximum_order=5, const unsigned int &maximum_non_linear_iterations=10, const double &absolute_tolerance=1e-6, const double &relative_tolerance=1e-5, const bool &ignore_algebraic_terms_for_errors=true, const InitialConditionCorrection &ic_type=use_y_diff, const InitialConditionCorrection &reset_type=use_y_diff, const unsigned int &maximum_non_linear_iterations_ic=5)
Definition: ida.h:304
void * ida_mem
Definition: ida.h:781
std::function< int(const VectorType &rhs, VectorType &dst)> solve_jacobian_system
Definition: ida.h:691
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())
unsigned int maximum_non_linear_iterations
Definition: ida.h:528
void add_action(const std::string &entry, const std::function< void(const std::string &value)> &action)
AdditionalData data
Definition: ida.h:776
unsigned int solve_dae(VectorType &solution, VectorType &solution_dot)
Definition: ida.cc:179
std::function< bool(const double t, VectorType &sol, VectorType &sol_dot)> solver_should_restart
Definition: ida.h:730
N_Vector abs_tolls
Definition: ida.h:796
std::function< VectorType &()> get_local_tolerances
Definition: ida.h:748
InitialConditionCorrection ic_type
Definition: ida.h:507
static ::ExceptionBase & ExcInternalError()
std::function< int(const double t, const VectorType &y, const VectorType &y_dot, const double alpha)> setup_jacobian
Definition: ida.h:661