Skip to content

Commit 8bf8b87

Browse files
committed
Updated ODE function docs
1 parent a3530bb commit 8bf8b87

12 files changed

Lines changed: 268 additions & 130 deletions

stan/math/prim/fun.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@
213213
#include <stan/math/prim/fun/multiply_lower_tri_self_transpose.hpp>
214214
#include <stan/math/prim/fun/norm.hpp>
215215
#include <stan/math/prim/fun/num_elements.hpp>
216-
#include <stan/math/prim/fun/ode_store_sensitivities.hpp>
217216
#include <stan/math/prim/fun/offset_multiplier_constrain.hpp>
218217
#include <stan/math/prim/fun/offset_multiplier_free.hpp>
219218
#include <stan/math/prim/fun/one_hot_array.hpp>

stan/math/prim/fun/ode_store_sensitivities.hpp

Lines changed: 0 additions & 25 deletions
This file was deleted.

stan/math/prim/functor.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <stan/math/prim/functor/integrate_ode_rk45.hpp>
1313
#include <stan/math/prim/functor/integrate_ode_std_vector_interface_adapter.hpp>
1414
#include <stan/math/prim/functor/ode_rk45.hpp>
15+
#include <stan/math/prim/functor/ode_store_sensitivities.hpp>
1516
#include <stan/math/prim/functor/map_rect.hpp>
1617
#include <stan/math/prim/functor/map_rect_combine.hpp>
1718
#include <stan/math/prim/functor/map_rect_concurrent.hpp>

stan/math/prim/functor/integrate_ode_rk45.hpp

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,52 +11,8 @@ namespace stan {
1111
namespace math {
1212

1313
/**
14-
* Return the solutions for the specified system of ordinary
15-
* differential equations given the specified initial state,
16-
* initial times, times of desired solution, and parameters and
17-
* data, writing error and warning messages to the specified
18-
* stream.
19-
*
20-
* <b>Warning:</b> If the system of equations is stiff, roughly
21-
* defined by having varying time scales across dimensions, then
22-
* this solver is likely to be slow.
23-
*
24-
* This function is templated to allow the initial times to be
25-
* either data or autodiff variables and the parameters to be data
26-
* or autodiff variables. The autodiff-based implementation for
27-
* reverse-mode are defined in namespace <code>stan::math</code>
28-
* and may be invoked via argument-dependent lookup by including
29-
* their headers.
30-
*
31-
* This function uses the <a
32-
* href="http://en.wikipedia.org/wiki/Dormand–Prince_method">Dormand-Prince
33-
* method</a> as implemented in Boost's <code>
34-
* boost::numeric::odeint::runge_kutta_dopri5</code> integrator.
35-
*
36-
* @tparam F type of ODE system function.
37-
* @tparam T1 type of scalars for initial values.
38-
* @tparam T2 type of scalars for parameters.
39-
* @tparam T_t0 type of scalar of initial time point.
40-
* @tparam T_ts type of time-points where ODE solution is returned.
41-
* @param[in] f functor for the base ordinary differential equation.
42-
* @param[in] y0 initial state.
43-
* @param[in] t0 initial time.
44-
* @param[in] ts times of the desired solutions, in strictly
45-
* increasing order, all greater than the initial time.
46-
* @param[in] theta parameter vector for the ODE.
47-
* @param[in] x continuous data vector for the ODE.
48-
* @param[in] x_int integer data vector for the ODE.
49-
* @param[out] msgs the print stream for warning messages.
50-
* @param[in] relative_tolerance relative tolerance parameter
51-
* for Boost's ode solver. Defaults to 1e-6.
52-
* @param[in] absolute_tolerance absolute tolerance parameter
53-
* for Boost's ode solver. Defaults to 1e-6.
54-
* @param[in] max_num_steps maximum number of steps to take within
55-
* the Boost ode solver.
56-
* @return a vector of states, each state being a vector of the
57-
* same size as the state variable, corresponding to a time in ts.
14+
* @deprecated use <code>ode_rk45</code>
5815
*/
59-
6016
template <typename F, typename T1, typename T_param, typename T_t0,
6117
typename T_ts>
6218
std::vector<std::vector<return_type_t<T1, T_t0, T_ts, T_param>>>

stan/math/prim/functor/integrate_ode_std_vector_interface_adapter.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ namespace math {
1111

1212
namespace internal {
1313

14+
/**
15+
* Wrap a functor designed for use with integrate_ode_bdf, integrate_ode_rk45,
16+
* and integrate_ode_adams to use with the new ode_bdf/ode_rk45 interfaces.
17+
*
18+
* The old functors took the ODE state as a std::vector. The new ones take
19+
* state as an Eigen::Matrix. The adapter converts to and from these forms
20+
* so that the old ODE interfaces can work.
21+
*/
1422
template <typename F>
1523
struct integrate_ode_std_vector_interface_adapter {
16-
// NOTE: I made this copy by value because
17-
// this function might go out of scope in reverse mode
1824
const F f_;
1925

2026
integrate_ode_std_vector_interface_adapter(const F& f) : f_(f) {

stan/math/prim/functor/ode_rk45.hpp

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include <stan/math/prim/meta.hpp>
55
#include <stan/math/prim/functor/coupled_ode_system.hpp>
6-
#include <stan/math/prim/fun/ode_store_sensitivities.hpp>
6+
#include <stan/math/prim/functor/ode_store_sensitivities.hpp>
77
#include <boost/numeric/odeint/external/eigen/eigen_algebra.hpp>
88
#include <boost/numeric/odeint.hpp>
99
#include <ostream>
@@ -12,20 +12,94 @@
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+
*/
1552
template <typename F, typename T_initial, typename T_t0, typename T_ts,
1653
typename... Args>
1754
std::vector<Eigen::Matrix<stan::return_type_t<T_initial, Args...>, Eigen::Dynamic, 1>>
1855
ode_rk45(const F& f, const Eigen::Matrix<T_initial, Eigen::Dynamic, 1>& y0, double t0,
1956
const std::vector<double>& ts, std::ostream* msgs,
2057
const Args&... args) {
21-
double relative_tolerance = 1e-6;
22-
double absolute_tolerance = 1e-6;
23-
long int max_num_steps = 1e6;
58+
double relative_tolerance = 1e-10;
59+
double absolute_tolerance = 1e-10;
60+
long int max_num_steps = 1e8;
2461

2562
return ode_rk45_tol(f, y0, t0, ts, relative_tolerance, absolute_tolerance,
2663
max_num_steps, msgs, args...);
2764
}
2865

66+
/**
67+
* Solve the ODE initial value problem y' = f(t, y), y(t0) = y0 at a set of
68+
* times, { t1, t2, t3, ... } using the non-stiff Runge-Kutta 45 solver in Boost
69+
* with a relative tolerance of 1e-10, an absolute tolerance of 1e-10, and
70+
* taking a maximum of 1e8 steps.
71+
*
72+
* If the system of equations is stiff, <code>ode_bdf</code> will likely be
73+
* faster.
74+
*
75+
* \p f must define an operator() with the signature as:
76+
* template<typename T_t, typename T_y, typename... T_Args>
77+
* Eigen::Matrix<stan::return_type_t<T_t, T_y, T_Args...>, Eigen::Dynamic, 1>
78+
* operator()(const T_t& t, const Eigen::Matrix<T_y, Eigen::Dynamic, 1>& y,
79+
* std::ostream* msgs, const T_Args&... args);
80+
*
81+
* t is the time, y is the state, msgs is a stream for error messages, and args
82+
* are optional arguments passed to the ODE solve function (which are passed
83+
* through to \p f without modification).
84+
*
85+
* @tparam F Type of ODE right hand side
86+
* @tparam T_0 Type of initial time
87+
* @tparam T_ts Type of output times
88+
* @tparam T_Args Types of pass-through parameters
89+
*
90+
* @param f Right hand side of the ODE
91+
* @param y0 Initial state
92+
* @param t0 Initial time
93+
* @param ts Times at which to solve the ODE at. All values must be sorted and
94+
* not less than t0.
95+
* @param relative_tolerance Relative tolerance passed to CVODES
96+
* @param absolute_tolerance Absolute tolerance passed to CVODES
97+
* @param max_num_steps Upper limit on the number of integration steps to
98+
* take between each output (error if exceeded)
99+
* @param[in, out] msgs the print stream for warning messages
100+
* @param args Extra arguments passed unmodified through to ODE right hand side
101+
* @return Solution to ODE at times \p ts
102+
*/
29103
template <typename F, typename T_initial, typename T_t0, typename T_ts,
30104
typename... Args>
31105
std::vector<Eigen::Matrix<stan::return_type_t<T_initial, T_t0, T_ts, Args...>, Eigen::Dynamic, 1>>
@@ -88,6 +162,8 @@ ode_rk45_tol(const F& f, const Eigen::Matrix<T_initial, Eigen::Dynamic, 1>& y0_a
88162
bool observer_initial_recorded = false;
89163
size_t time_index = 0;
90164

165+
max_step_checker step_checker(max_num_steps);
166+
91167
// avoid recording of the initial state which is included by the
92168
// conventions of odeint in the output
93169
auto filtered_observer
@@ -99,6 +175,7 @@ ode_rk45_tol(const F& f, const Eigen::Matrix<T_initial, Eigen::Dynamic, 1>& y0_a
99175
y.emplace_back(ode_store_sensitivities(f, coupled_state, y0,
100176
t0, ts[time_index],
101177
msgs, args...));
178+
step_checker.reset();
102179
time_index++;
103180
};
104181

@@ -113,7 +190,7 @@ ode_rk45_tol(const F& f, const Eigen::Matrix<T_initial, Eigen::Dynamic, 1>& y0_a
113190
vector_space_algebra>()),
114191
std::ref(coupled_system), initial_coupled_state,
115192
std::begin(ts_vec), std::end(ts_vec), step_size, filtered_observer,
116-
max_step_checker(max_num_steps));
193+
step_checker);
117194

118195
return y;
119196
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#ifndef STAN_MATH_PRIM_FUNCTOR_ODE_STORE_SENSITIVITIES_HPP
2+
#define STAN_MATH_PRIM_FUNCTOR_ODE_STORE_SENSITIVITIES_HPP
3+
4+
#include <stan/math/prim/meta.hpp>
5+
#include <ostream>
6+
#include <vector>
7+
8+
namespace stan {
9+
namespace math {
10+
11+
/**
12+
* Build output for state of ODE solve from the current coupled_state when
13+
* all arguments are arithmetic.
14+
*
15+
* @tparam F Type of ODE right hand side
16+
*
17+
* @param f Right hand side of the ODE
18+
* @param coupled_state Current state of the coupled_ode_system
19+
* @param y0 Initial state
20+
* @param t0 Initial time
21+
* @param ts Times at which to solve the ODE at
22+
* @param[in, out] msgs the print stream for warning messages
23+
* @param args Extra arguments passed unmodified through to ODE right hand side
24+
* @return ODE state
25+
*/
26+
template <typename F, typename... Args,
27+
typename = require_all_arithmetic_t<scalar_type_t<Args>...>>
28+
Eigen::VectorXd ode_store_sensitivities(const F& f,
29+
const Eigen::VectorXd& coupled_state,
30+
const Eigen::VectorXd& y0,
31+
double t0,
32+
double t,
33+
std::ostream* msgs,
34+
const Args&... args) {
35+
return coupled_state.head(y0.size());
36+
}
37+
38+
} // namespace math
39+
} // namespace stan
40+
#endif

stan/math/rev/functor.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <stan/math/rev/functor/integrate_ode_adams.hpp>
1616
#include <stan/math/rev/functor/integrate_ode_bdf.hpp>
1717
#include <stan/math/rev/functor/ode_bdf.hpp>
18+
#include <stan/math/rev/functor/ode_store_sensitivities.hpp>
1819
#include <stan/math/rev/functor/jacobian.hpp>
1920
#include <stan/math/rev/functor/kinsol_data.hpp>
2021
#include <stan/math/rev/functor/kinsol_solve.hpp>

0 commit comments

Comments
 (0)