Stan Math Library  2.10.0
reverse mode automatic differentiation
cvodes_utils.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_MAT_FUNCTOR_CVODES_UTILS_HPP
2 #define STAN_MATH_REV_MAT_FUNCTOR_CVODES_UTILS_HPP
3 
4 #include <cvodes/cvodes.h>
5 #include <cvodes/cvodes_band.h>
6 #include <cvodes/cvodes_dense.h>
7 #include <nvector/nvector_serial.h>
8 #include <sstream>
9 #include <string>
10 
11 namespace stan {
12 
13  namespace math {
14 
15  // no-op error handler to silence CVodes error output; errors handled
16  // directly by Stan
17  extern "C"
18  inline void cvodes_silent_err_handler(int error_code, const char *module,
19  const char *function, char *msg,
20  void *eh_data) {
21  }
22 
23  inline void cvodes_check_flag(int flag, const std::string& func_name) {
24  if (flag < 0) {
25  std::ostringstream ss;
26  ss << func_name << " failed with error flag " << flag;
27  throw std::runtime_error(ss.str());
28  }
29  }
30 
31  inline void cvodes_set_options(void* cvodes_mem,
32  double rel_tol, double abs_tol,
33  // NOLINTNEXTLINE(runtime/int)
34  long int max_num_steps) {
35  // forward CVode errors to noop error handler
36  CVodeSetErrHandlerFn(cvodes_mem, cvodes_silent_err_handler, 0);
37 
38  // Initialize solver parameters
39  cvodes_check_flag(CVodeSStolerances(cvodes_mem, rel_tol, abs_tol),
40  "CVodeSStolerances");
41 
42  cvodes_check_flag(CVodeSetMaxNumSteps(cvodes_mem, max_num_steps),
43  "CVodeSetMaxNumSteps");
44 
45  double init_step = 0;
46  cvodes_check_flag(CVodeSetInitStep(cvodes_mem, init_step),
47  "CVodeSetInitStep");
48 
49  long int max_err_test_fails = 20; // NOLINT(runtime/int)
50  cvodes_check_flag(CVodeSetMaxErrTestFails(cvodes_mem, max_err_test_fails),
51  "CVodeSetMaxErrTestFails");
52 
53  long int max_conv_fails = 50; // NOLINT(runtime/int)
54  cvodes_check_flag(CVodeSetMaxConvFails(cvodes_mem, max_conv_fails),
55  "CVodeSetMaxConvFails");
56  }
57 
58  }
59 }
60 #endif
void cvodes_set_options(void *cvodes_mem, double rel_tol, double abs_tol, long int max_num_steps)
void cvodes_check_flag(int flag, const std::string &func_name)
void cvodes_silent_err_handler(int error_code, const char *module, const char *function, char *msg, void *eh_data)

     [ Stan Home Page ] © 2011–2016, Stan Development Team.