Reference documentation for deal.II version 9.0.0
arkode.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_arkode_h
17 #define dealii_sundials_arkode_h
18 
19 #include <deal.II/base/config.h>
20 #include <deal.II/base/mpi.h>
21 
22 #ifdef DEAL_II_WITH_SUNDIALS
23 
24 #include <deal.II/base/logstream.h>
25 #include <deal.II/base/exceptions.h>
26 #include <deal.II/base/parameter_handler.h>
27 #include <deal.II/base/conditional_ostream.h>
28 #include <deal.II/base/mpi.h>
29 #ifdef DEAL_II_WITH_PETSC
30 # include <deal.II/lac/petsc_parallel_block_vector.h>
31 # include <deal.II/lac/petsc_parallel_vector.h>
32 #endif
33 #include <deal.II/lac/vector.h>
34 #include <deal.II/lac/vector_memory.h>
35 
36 
37 #include <arkode/arkode.h>
38 #include <arkode/arkode_impl.h>
39 #include <nvector/nvector_serial.h>
40 #ifdef DEAL_II_WITH_MPI
41 # include <nvector/nvector_parallel.h>
42 #endif
43 #include <sundials/sundials_math.h>
44 #include <sundials/sundials_types.h>
45 
46 #include <boost/signals2.hpp>
47 #include <memory>
48 
49 
50 DEAL_II_NAMESPACE_OPEN
51 
52 // Shorthand notation for ARKODE error codes.
53 #define AssertARKode(code) Assert(code >= 0, ExcARKodeError(code))
54 
58 namespace SUNDIALS
59 {
309  template<typename VectorType=Vector<double> >
310  class ARKode
311  {
312  public:
313 
318  {
319  public:
345  // Initial parameters
346  const double &initial_time = 0.0,
347  const double &final_time = 1.0,
348  const double &initial_step_size = 1e-2,
349  const double &output_period = 1e-1,
350  // Running parameters
351  const double &minimum_step_size = 1e-6,
352  const unsigned int &maximum_order = 5,
353  const unsigned int &maximum_non_linear_iterations = 10,
354  const bool implicit_function_is_linear = false,
355  const bool implicit_function_is_time_independent = false,
356  // Error parameters
357  const double &absolute_tolerance = 1e-6,
358  const double &relative_tolerance = 1e-5) :
370  {}
371 
414  {
415  prm.add_parameter("Initial time", initial_time);
416  prm.add_parameter("Final time", final_time);
417  prm.add_parameter("Time interval between each output", output_period);
418 
419  prm.enter_subsection("Running parameters");
420  prm.add_parameter("Initial step size",initial_step_size);
421  prm.add_parameter("Minimum step size", minimum_step_size);
422  prm.add_parameter("Maximum order of ARK", maximum_order);
423  prm.add_parameter("Maximum number of nonlinear iterations", maximum_non_linear_iterations);
424  prm.add_parameter("Implicit function is linear", implicit_function_is_linear);
425  prm.add_parameter("Implicit function is time independent", implicit_function_is_time_independent);
426  prm.leave_subsection();
427 
428  prm.enter_subsection("Error control");
429  prm.add_parameter("Absolute error tolerance", absolute_tolerance);
430  prm.add_parameter("Relative error tolerance", relative_tolerance);
431  prm.leave_subsection();
432  }
433 
437  double initial_time;
438 
442  double final_time;
443 
448 
453 
458 
463 
467  unsigned int maximum_order;
468 
473 
479 
484 
490  };
491 
504  const MPI_Comm mpi_comm = MPI_COMM_WORLD);
505 
509  ~ARKode();
510 
515  unsigned int solve_ode(VectorType &solution);
516 
531  void reset(const double &t,
532  const double &h,
533  const VectorType &y);
534 
539  std::function<void(VectorType &)> reinit_vector;
540 
557  std::function<int(const double t,
558  const VectorType &y,
559  VectorType &explicit_f)> explicit_function;
560 
577  std::function<int(const double t,
578  const VectorType &y,
579  VectorType &res)> implicit_function;
580 
662  std::function<int(const int convfail,
663  const double t,
664  const double gamma,
665  const VectorType &ypred,
666  const VectorType &fpred,
667  bool &j_is_current)> setup_jacobian;
668 
712  std::function<int(const double t,
713  const double gamma,
714  const VectorType &ycur,
715  const VectorType &fcur,
716  const VectorType &rhs,
717  VectorType &dst)> solve_jacobian_system;
718 
719 
753  std::function<int(const double t)> setup_mass;
754 
773  std::function<int(const VectorType &rhs, VectorType &dst)> solve_mass_system;
774 
789  std::function<void(const double t,
790  const VectorType &sol,
791  const unsigned int step_number)> output_step;
792 
810  std::function<bool (const double t,
811  VectorType &sol)> solver_should_restart;
812 
819  std::function<VectorType&()> get_local_tolerances;
820 
824  DeclException1(ExcARKodeError, int, << "One of the SUNDIALS ARKode internal functions "
825  << " returned a negative error code: "
826  << arg1 << ". Please consult SUNDIALS manual.");
827 
828 
829  private:
830 
834  DeclException1(ExcFunctionNotProvided, std::string,
835  << "Please provide an implementation for the function \"" << arg1 << "\"");
836 
843 
848 
852  void *arkode_mem;
853 
857  N_Vector yy;
858 
862  N_Vector abs_tolls;
863 
869  MPI_Comm communicator;
870 
875 
876 #ifdef DEAL_II_WITH_PETSC
877 #ifdef PETSC_USE_COMPLEX
878  static_assert(!std::is_same<VectorType, PETScWrappers::MPI::Vector>::value,
879  "Sundials does not support complex scalar types, "
880  "but PETSc is configured to use a complex scalar type!");
881 
882  static_assert(!std::is_same<VectorType, PETScWrappers::MPI::BlockVector>::value,
883  "Sundials does not support complex scalar types, "
884  "but PETSc is configured to use a complex scalar type!");
885 #endif // PETSC_USE_COMPLEX
886 #endif // DEAL_II_WITH_PETSC
887  };
888 
889 }
890 
891 DEAL_II_NAMESPACE_CLOSE
892 #endif
893 
894 
895 #endif
std::function< VectorType &()> get_local_tolerances
Definition: arkode.h:819
void * arkode_mem
Definition: arkode.h:852
GrowingVectorMemory< VectorType > mem
Definition: arkode.h:874
std::function< int(const VectorType &rhs, VectorType &dst)> solve_mass_system
Definition: arkode.h:773
MPI_Comm communicator
Definition: arkode.h:869
N_Vector yy
Definition: arkode.h:857
void reset(const double &t, const double &h, const VectorType &y)
Definition: arkode.cc:337
std::function< bool(const double t, VectorType &sol)> solver_should_restart
Definition: arkode.h:811
N_Vector abs_tolls
Definition: arkode.h:862
unsigned int maximum_non_linear_iterations
Definition: arkode.h:478
std::function< int(const double t)> setup_mass
Definition: arkode.h:753
std::function< int(const double t, const VectorType &y, VectorType &explicit_f)> explicit_function
Definition: arkode.h:559
std::function< void(const double t, const VectorType &sol, const unsigned int step_number)> output_step
Definition: arkode.h:791
void enter_subsection(const std::string &subsection)
AdditionalData data
Definition: arkode.h:847
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 bool implicit_function_is_linear=false, const bool implicit_function_is_time_independent=false, const double &absolute_tolerance=1e-6, const double &relative_tolerance=1e-5)
Definition: arkode.h:344
std::function< int(const double t, const VectorType &y, VectorType &res)> implicit_function
Definition: arkode.h:579
std::function< int(const int convfail, const double t, const double gamma, const VectorType &ypred, const VectorType &fpred, bool &j_is_current)> setup_jacobian
Definition: arkode.h:667
DeclException1(ExcARKodeError, int,<< "One of the SUNDIALS ARKode internal functions "<< " returned a negative error code: "<< arg1<< ". Please consult SUNDIALS manual.")
void add_parameters(ParameterHandler &prm)
Definition: arkode.h:413
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())
std::function< void(VectorType &)> reinit_vector
Definition: arkode.h:539
unsigned int solve_ode(VectorType &solution)
Definition: arkode.cc:251
void set_functions_to_trigger_an_assert()
Definition: arkode.cc:475
std::function< int(const double t, const double gamma, const VectorType &ycur, const VectorType &fcur, const VectorType &rhs, VectorType &dst)> solve_jacobian_system
Definition: arkode.h:717
ARKode(const AdditionalData &data=AdditionalData(), const MPI_Comm mpi_comm=MPI_COMM_WORLD)
Definition: arkode.cc:220