Reference documentation for deal.II version 9.1.0-pre
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.md at
12 // the top level directory of deal.II.
13 //
14 //---------------------------------------------------------------
15 
16 #ifndef dealii_sundials_arkode_h
17 #define dealii_sundials_arkode_h
18 
19 #include <deal.II/base/config.h>
20 
21 #include <deal.II/base/mpi.h>
22 
23 #ifdef DEAL_II_WITH_SUNDIALS
24 
25 # include <deal.II/base/conditional_ostream.h>
26 # include <deal.II/base/exceptions.h>
27 # include <deal.II/base/logstream.h>
28 # include <deal.II/base/mpi.h>
29 # include <deal.II/base/parameter_handler.h>
30 # ifdef DEAL_II_WITH_PETSC
31 # include <deal.II/lac/petsc_block_vector.h>
32 # include <deal.II/lac/petsc_vector.h>
33 # endif
34 # include <deal.II/lac/vector.h>
35 # include <deal.II/lac/vector_memory.h>
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 <boost/signals2.hpp>
44 
45 # include <sundials/sundials_math.h>
46 # include <sundials/sundials_types.h>
47 
48 # include <memory>
49 
50 
51 DEAL_II_NAMESPACE_OPEN
52 
53 // Shorthand notation for ARKODE error codes.
54 # define AssertARKode(code) Assert(code >= 0, ExcARKodeError(code))
55 
59 namespace SUNDIALS
60 {
310  template <typename VectorType = Vector<double>>
311  class ARKode
312  {
313  public:
318  {
319  public:
347  // Initial parameters
348  const double &initial_time = 0.0,
349  const double &final_time = 1.0,
350  const double &initial_step_size = 1e-2,
351  const double &output_period = 1e-1,
352  // Running parameters
353  const double & minimum_step_size = 1e-6,
354  const unsigned int &maximum_order = 5,
355  const unsigned int &maximum_non_linear_iterations = 10,
356  const bool implicit_function_is_linear = false,
357  const bool implicit_function_is_time_independent = false,
358  // Error parameters
359  const double &absolute_tolerance = 1e-6,
360  const double &relative_tolerance = 1e-5)
373  {}
374 
417  void
419  {
420  prm.add_parameter("Initial time", initial_time);
421  prm.add_parameter("Final time", final_time);
422  prm.add_parameter("Time interval between each output", output_period);
423 
424  prm.enter_subsection("Running parameters");
425  prm.add_parameter("Initial step size", initial_step_size);
426  prm.add_parameter("Minimum step size", minimum_step_size);
427  prm.add_parameter("Maximum order of ARK", maximum_order);
428  prm.add_parameter("Maximum number of nonlinear iterations",
430  prm.add_parameter("Implicit function is linear",
432  prm.add_parameter("Implicit function is time independent",
434  prm.leave_subsection();
435 
436  prm.enter_subsection("Error control");
437  prm.add_parameter("Absolute error tolerance", absolute_tolerance);
438  prm.add_parameter("Relative error tolerance", relative_tolerance);
439  prm.leave_subsection();
440  }
441 
445  double initial_time;
446 
450  double final_time;
451 
456 
461 
466 
471 
475  unsigned int maximum_order;
476 
481 
487 
492 
498  };
499 
512  const MPI_Comm mpi_comm = MPI_COMM_WORLD);
513 
517  ~ARKode();
518 
523  unsigned int
524  solve_ode(VectorType &solution);
525 
540  void
541  reset(const double &t, const double &h, const VectorType &y);
542 
547  std::function<void(VectorType &)> reinit_vector;
548 
565  std::function<
566  int(const double t, const VectorType &y, VectorType &explicit_f)>
568 
585  std::function<int(const double t, const VectorType &y, VectorType &res)>
587 
672  std::function<int(const int convfail,
673  const double t,
674  const double gamma,
675  const VectorType &ypred,
676  const VectorType &fpred,
677  bool & j_is_current)>
679 
724  std::function<int(const double t,
725  const double gamma,
726  const VectorType &ycur,
727  const VectorType &fcur,
728  const VectorType &rhs,
729  VectorType & dst)>
731 
732 
766  std::function<int(const double t)> setup_mass;
767 
786  std::function<int(const VectorType &rhs, VectorType &dst)>
788 
803  std::function<void(const double t,
804  const VectorType & sol,
805  const unsigned int step_number)>
807 
825  std::function<bool(const double t, VectorType &sol)> solver_should_restart;
826 
833  std::function<VectorType &()> get_local_tolerances;
834 
838  DeclException1(ExcARKodeError,
839  int,
840  << "One of the SUNDIALS ARKode internal functions "
841  << " returned a negative error code: " << arg1
842  << ". Please consult SUNDIALS manual.");
843 
844 
845  private:
850  DeclException1(ExcFunctionNotProvided,
851  std::string,
852  << "Please provide an implementation for the function \""
853  << arg1 << "\"");
854 
860  void
862 
867 
871  void *arkode_mem;
872 
876  N_Vector yy;
877 
881  N_Vector abs_tolls;
882 
888  MPI_Comm communicator;
889 
894 
895 # ifdef DEAL_II_WITH_PETSC
896 # ifdef PETSC_USE_COMPLEX
897  static_assert(!std::is_same<VectorType, PETScWrappers::MPI::Vector>::value,
898  "Sundials does not support complex scalar types, "
899  "but PETSc is configured to use a complex scalar type!");
900 
901  static_assert(
902  !std::is_same<VectorType, PETScWrappers::MPI::BlockVector>::value,
903  "Sundials does not support complex scalar types, "
904  "but PETSc is configured to use a complex scalar type!");
905 # endif // PETSC_USE_COMPLEX
906 # endif // DEAL_II_WITH_PETSC
907  };
908 
909 } // namespace SUNDIALS
910 
911 DEAL_II_NAMESPACE_CLOSE
912 #endif
913 
914 
915 #endif
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 * arkode_mem
Definition: arkode.h:871
GrowingVectorMemory< VectorType > mem
Definition: arkode.h:893
std::function< int(const VectorType &rhs, VectorType &dst)> solve_mass_system
Definition: arkode.h:787
MPI_Comm communicator
Definition: arkode.h:888
N_Vector yy
Definition: arkode.h:876
std::function< void(const double t, const VectorType &sol, const unsigned int step_number)> output_step
Definition: arkode.h:806
void reset(const double &t, const double &h, const VectorType &y)
Definition: arkode.cc:347
N_Vector abs_tolls
Definition: arkode.h:881
std::function< VectorType &()> get_local_tolerances
Definition: arkode.h:833
unsigned int maximum_non_linear_iterations
Definition: arkode.h:486
std::function< int(const double t)> setup_mass
Definition: arkode.h:766
std::function< bool(const double t, VectorType &sol)> solver_should_restart
Definition: arkode.h:825
void enter_subsection(const std::string &subsection)
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:730
AdditionalData data
Definition: arkode.h:866
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:346
std::function< int(const double t, const VectorType &y, VectorType &res)> implicit_function
Definition: arkode.h:586
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:418
std::function< int(const double t, const VectorType &y, VectorType &explicit_f)> explicit_function
Definition: arkode.h:567
std::function< void(VectorType &)> reinit_vector
Definition: arkode.h:547
unsigned int solve_ode(VectorType &solution)
Definition: arkode.cc:265
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:678
void set_functions_to_trigger_an_assert()
Definition: arkode.cc:488
ARKode(const AdditionalData &data=AdditionalData(), const MPI_Comm mpi_comm=MPI_COMM_WORLD)
Definition: arkode.cc:233