Stan Math Library  2.20.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 <stan/math/rev/meta.hpp>
5 #include <cvodes/cvodes.h>
6 #include <sstream>
7 #include <stdexcept>
8 
9 namespace stan {
10 namespace math {
11 
12 // no-op error handler to silence CVodes error output; errors handled
13 // directly by Stan
14 extern "C" inline void cvodes_silent_err_handler(int error_code,
15  const char* module,
16  const char* function,
17  char* msg, void* eh_data) {}
18 
19 inline void cvodes_check_flag(int flag, const char* func_name) {
20  if (flag < 0) {
21  std::ostringstream ss;
22  ss << func_name << " failed with error flag " << flag;
23  throw std::runtime_error(ss.str());
24  }
25 }
26 
27 inline void cvodes_set_options(void* cvodes_mem, double rel_tol, double abs_tol,
28  // NOLINTNEXTLINE(runtime/int)
29  long int max_num_steps) {
30  // forward CVode errors to noop error handler
31  CVodeSetErrHandlerFn(cvodes_mem, cvodes_silent_err_handler, nullptr);
32 
33  // Initialize solver parameters
34  cvodes_check_flag(CVodeSStolerances(cvodes_mem, rel_tol, abs_tol),
35  "CVodeSStolerances");
36 
37  cvodes_check_flag(CVodeSetMaxNumSteps(cvodes_mem, max_num_steps),
38  "CVodeSetMaxNumSteps");
39 
40  double init_step = 0;
41  cvodes_check_flag(CVodeSetInitStep(cvodes_mem, init_step),
42  "CVodeSetInitStep");
43 
44  long int max_err_test_fails = 20; // NOLINT(runtime/int)
45  cvodes_check_flag(CVodeSetMaxErrTestFails(cvodes_mem, max_err_test_fails),
46  "CVodeSetMaxErrTestFails");
47 
48  long int max_conv_fails = 50; // NOLINT(runtime/int)
49  cvodes_check_flag(CVodeSetMaxConvFails(cvodes_mem, max_conv_fails),
50  "CVodeSetMaxConvFails");
51 }
52 
53 } // namespace math
54 } // namespace stan
55 #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 char *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–2018, Stan Development Team.