Skip to content

Commit 7811368

Browse files
committed
Merge branch 'feature/parameter-pack-odes' into feature/adjoint-odes
2 parents 0389837 + e6d7dcf commit 7811368

File tree

2 files changed

+73
-73
lines changed

2 files changed

+73
-73
lines changed

stan/math/prim/functor/ode_rk45.hpp

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,58 +12,6 @@
1212
namespace stan {
1313
namespace math {
1414

15-
/**
16-
* Solve the ODE initial value problem y' = f(t, y), y(t0) = y0 at a set of
17-
* times, { t1, t2, t3, ... } using the non-stiff Runge-Kutta 45 solver in Boost
18-
* with a relative tolerance of 1e-10, an absolute tolerance of 1e-10, and
19-
* taking a maximum of 1e8 steps.
20-
*
21-
* If the system of equations is stiff, <code>ode_bdf</code> will likely be
22-
* faster.
23-
*
24-
* \p f must define an operator() with the signature as:
25-
* template<typename T_t, typename T_y, typename... T_Args>
26-
* Eigen::Matrix<stan::return_type_t<T_t, T_y, T_Args...>, Eigen::Dynamic, 1>
27-
* operator()(const T_t& t, const Eigen::Matrix<T_y, Eigen::Dynamic, 1>& y,
28-
* std::ostream* msgs, const T_Args&... args);
29-
*
30-
* t is the time, y is the state, msgs is a stream for error messages, and args
31-
* are optional arguments passed to the ODE solve function (which are passed
32-
* through to \p f without modification).
33-
*
34-
* @tparam F Type of ODE right hand side
35-
* @tparam T_0 Type of initial time
36-
* @tparam T_ts Type of output times
37-
* @tparam T_Args Types of pass-through parameters
38-
*
39-
* @param f Right hand side of the ODE
40-
* @param y0 Initial state
41-
* @param t0 Initial time
42-
* @param ts Times at which to solve the ODE at. All values must be sorted and
43-
* not less than t0.
44-
* @param relative_tolerance Relative tolerance passed to CVODES
45-
* @param absolute_tolerance Absolute tolerance passed to CVODES
46-
* @param max_num_steps Upper limit on the number of integration steps to
47-
* take between each output (error if exceeded)
48-
* @param[in, out] msgs the print stream for warning messages
49-
* @param args Extra arguments passed unmodified through to ODE right hand side
50-
* @return Solution to ODE at times \p ts
51-
*/
52-
template <typename F, typename T_initial, typename T_t0, typename T_ts,
53-
typename... Args>
54-
std::vector<
55-
Eigen::Matrix<stan::return_type_t<T_initial, Args...>, Eigen::Dynamic, 1>>
56-
ode_rk45(const F& f, const Eigen::Matrix<T_initial, Eigen::Dynamic, 1>& y0,
57-
double t0, const std::vector<double>& ts, std::ostream* msgs,
58-
const Args&... args) {
59-
double relative_tolerance = 1e-10;
60-
double absolute_tolerance = 1e-10;
61-
long int max_num_steps = 1e8;
62-
63-
return ode_rk45_tol(f, y0, t0, ts, relative_tolerance, absolute_tolerance,
64-
max_num_steps, msgs, args...);
65-
}
66-
6715
/**
6816
* Solve the ODE initial value problem y' = f(t, y), y(t0) = y0 at a set of
6917
* times, { t1, t2, t3, ... } using the non-stiff Runge-Kutta 45 solver in Boost
@@ -192,6 +140,58 @@ ode_rk45_tol(const F& f,
192140
return y;
193141
}
194142

143+
/**
144+
* Solve the ODE initial value problem y' = f(t, y), y(t0) = y0 at a set of
145+
* times, { t1, t2, t3, ... } using the non-stiff Runge-Kutta 45 solver in Boost
146+
* with a relative tolerance of 1e-10, an absolute tolerance of 1e-10, and
147+
* taking a maximum of 1e8 steps.
148+
*
149+
* If the system of equations is stiff, <code>ode_bdf</code> will likely be
150+
* faster.
151+
*
152+
* \p f must define an operator() with the signature as:
153+
* template<typename T_t, typename T_y, typename... T_Args>
154+
* Eigen::Matrix<stan::return_type_t<T_t, T_y, T_Args...>, Eigen::Dynamic, 1>
155+
* operator()(const T_t& t, const Eigen::Matrix<T_y, Eigen::Dynamic, 1>& y,
156+
* std::ostream* msgs, const T_Args&... args);
157+
*
158+
* t is the time, y is the state, msgs is a stream for error messages, and args
159+
* are optional arguments passed to the ODE solve function (which are passed
160+
* through to \p f without modification).
161+
*
162+
* @tparam F Type of ODE right hand side
163+
* @tparam T_0 Type of initial time
164+
* @tparam T_ts Type of output times
165+
* @tparam T_Args Types of pass-through parameters
166+
*
167+
* @param f Right hand side of the ODE
168+
* @param y0 Initial state
169+
* @param t0 Initial time
170+
* @param ts Times at which to solve the ODE at. All values must be sorted and
171+
* not less than t0.
172+
* @param relative_tolerance Relative tolerance passed to CVODES
173+
* @param absolute_tolerance Absolute tolerance passed to CVODES
174+
* @param max_num_steps Upper limit on the number of integration steps to
175+
* take between each output (error if exceeded)
176+
* @param[in, out] msgs the print stream for warning messages
177+
* @param args Extra arguments passed unmodified through to ODE right hand side
178+
* @return Solution to ODE at times \p ts
179+
*/
180+
template <typename F, typename T_initial, typename T_t0, typename T_ts,
181+
typename... Args>
182+
std::vector<
183+
Eigen::Matrix<stan::return_type_t<T_initial, Args...>, Eigen::Dynamic, 1>>
184+
ode_rk45(const F& f, const Eigen::Matrix<T_initial, Eigen::Dynamic, 1>& y0,
185+
T_t0 t0, const std::vector<T_ts>& ts, std::ostream* msgs,
186+
const Args&... args) {
187+
double relative_tolerance = 1e-10;
188+
double absolute_tolerance = 1e-10;
189+
long int max_num_steps = 1e8;
190+
191+
return ode_rk45_tol(f, y0, t0, ts, relative_tolerance, absolute_tolerance,
192+
max_num_steps, msgs, args...);
193+
}
194+
195195
} // namespace math
196196
} // namespace stan
197197
#endif

stan/math/rev/functor/ode_bdf.hpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ namespace math {
1212
/**
1313
* Solve the ODE initial value problem y' = f(t, y), y(t0) = y0 at a set of
1414
* times, { t1, t2, t3, ... } using the stiff backward differentiation formula
15-
* (BDF) solver in CVODES with a relative tolerance of 1e-10, an absolute
16-
* tolerance of 1e-10, and taking a maximum of 1e8 steps.
15+
* BDF solver from CVODES.
1716
*
1817
* \p f must define an operator() with the signature as:
1918
* template<typename T_t, typename T_y, typename... T_Args>
@@ -47,21 +46,24 @@ template <typename F, typename T_initial, typename T_t0, typename T_ts,
4746
typename... T_Args>
4847
std::vector<Eigen::Matrix<stan::return_type_t<T_initial, T_t0, T_ts, T_Args...>,
4948
Eigen::Dynamic, 1>>
50-
ode_bdf(const F& f, const Eigen::Matrix<T_initial, Eigen::Dynamic, 1>& y0,
51-
const T_t0& t0, const std::vector<T_ts>& ts, std::ostream* msgs,
52-
const T_Args&... args) {
53-
double relative_tolerance = 1e-10;
54-
double absolute_tolerance = 1e-10;
55-
long int max_num_steps = 1e8;
49+
ode_bdf_tol(const F& f, const Eigen::Matrix<T_initial, Eigen::Dynamic, 1>& y0,
50+
const T_t0& t0, const std::vector<T_ts>& ts,
51+
double relative_tolerance, double absolute_tolerance,
52+
long int max_num_steps, std::ostream* msgs, const T_Args&... args) {
53+
auto integrator
54+
= new stan::math::cvodes_integrator_vari<CV_BDF, F, T_initial, T_t0, T_ts,
55+
T_Args...>(
56+
f, y0, t0, ts, relative_tolerance, absolute_tolerance, max_num_steps,
57+
msgs, args...);
5658

57-
return ode_bdf_tol(f, y0, t0, ts, relative_tolerance, absolute_tolerance,
58-
max_num_steps, msgs, args...);
59+
return (*integrator)();
5960
}
6061

6162
/**
6263
* Solve the ODE initial value problem y' = f(t, y), y(t0) = y0 at a set of
6364
* times, { t1, t2, t3, ... } using the stiff backward differentiation formula
64-
* BDF solver from CVODES.
65+
* (BDF) solver in CVODES with a relative tolerance of 1e-10, an absolute
66+
* tolerance of 1e-10, and taking a maximum of 1e8 steps.
6567
*
6668
* \p f must define an operator() with the signature as:
6769
* template<typename T_t, typename T_y, typename... T_Args>
@@ -95,17 +97,15 @@ template <typename F, typename T_initial, typename T_t0, typename T_ts,
9597
typename... T_Args>
9698
std::vector<Eigen::Matrix<stan::return_type_t<T_initial, T_t0, T_ts, T_Args...>,
9799
Eigen::Dynamic, 1>>
98-
ode_bdf_tol(const F& f, const Eigen::Matrix<T_initial, Eigen::Dynamic, 1>& y0,
99-
const T_t0& t0, const std::vector<T_ts>& ts,
100-
double relative_tolerance, double absolute_tolerance,
101-
long int max_num_steps, std::ostream* msgs, const T_Args&... args) {
102-
auto integrator
103-
= new stan::math::cvodes_integrator_vari<CV_BDF, F, T_initial, T_t0, T_ts,
104-
T_Args...>(
105-
f, y0, t0, ts, relative_tolerance, absolute_tolerance, max_num_steps,
106-
msgs, args...);
100+
ode_bdf(const F& f, const Eigen::Matrix<T_initial, Eigen::Dynamic, 1>& y0,
101+
const T_t0& t0, const std::vector<T_ts>& ts, std::ostream* msgs,
102+
const T_Args&... args) {
103+
double relative_tolerance = 1e-10;
104+
double absolute_tolerance = 1e-10;
105+
long int max_num_steps = 1e8;
107106

108-
return (*integrator)();
107+
return ode_bdf_tol(f, y0, t0, ts, relative_tolerance, absolute_tolerance,
108+
max_num_steps, msgs, args...);
109109
}
110110

111111
} // namespace math

0 commit comments

Comments
 (0)