1 #ifndef STAN_MATH_REV_ARR_FUNCTOR_COUPLED_ODE_SYSTEM_HPP
2 #define STAN_MATH_REV_ARR_FUNCTOR_COUPLED_ODE_SYSTEM_HPP
35 std::vector<std::vector<stan::math::var> >& y) {
36 for (
size_t n = 0; n < y.size(); n++)
37 for (
size_t m = 0; m < y0.size(); m++)
66 const std::vector<stan::math::var>&
theta_;
68 const std::vector<double>&
x_;
88 const std::vector<double>& y0,
89 const std::vector<stan::math::var>& theta,
90 const std::vector<double>& x,
91 const std::vector<int>& x_int,
96 theta_dbl_(theta.
size(), 0.0),
103 for (
size_t m = 0; m <
M_; m++)
125 std::vector<double>& dz_dt,
130 vector<double> y(z.begin(), z.begin() +
N_);
131 dz_dt = f_(t, y, theta_dbl_, x_, x_int_, msgs_);
133 "dz_dt", dz_dt.size(),
N_);
135 vector<double> coupled_sys(N_ * M_);
136 vector<double>
grad(N_ + M_);
142 z_vars.reserve(N_ + M_);
144 vector<var> y_vars(y.begin(), y.end());
145 z_vars.insert(z_vars.end(), y_vars.begin(), y_vars.end());
147 vector<var> theta_vars(theta_dbl_.begin(), theta_dbl_.end());
148 z_vars.insert(z_vars.end(), theta_vars.begin(), theta_vars.end());
150 vector<var> dy_dt_vars = f_(t, y_vars, theta_vars, x_, x_int_, msgs_);
152 for (
size_t i = 0; i <
N_; i++) {
154 dy_dt_vars[i].grad(z_vars, grad);
156 for (
size_t j = 0; j <
M_; j++) {
160 double temp_deriv = grad[N_ + j];
161 for (
size_t k = 0; k <
N_; k++)
162 temp_deriv += z[N_ + N_ * j + k] * grad[k];
164 coupled_sys[i + j *
N_] = temp_deriv;
167 }
catch (
const std::exception&
e) {
173 dz_dt.insert(dz_dt.end(), coupled_sys.begin(), coupled_sys.end());
199 std::vector<double> state(size_, 0.0);
200 for (
size_t n = 0; n <
N_; n++)
201 state[n] = y0_dbl_[n];
211 std::vector<std::vector<stan::math::var> >
214 std::vector<stan::math::var> temp_vars(N_);
215 std::vector<double> temp_gradients(M_);
216 std::vector<std::vector<stan::math::var> > y_return(y.size());
218 for (
size_t i = 0; i < y.size(); i++) {
220 for (
size_t j = 0; j <
N_; j++) {
222 for (
size_t k = 0; k <
M_; k++)
223 temp_gradients[k] = y[i][y0_dbl_.size() + y0_dbl_.size() * k + j];
229 y_return[i] = temp_vars;
261 template <
typename F>
264 const std::vector<stan::math::var>&
y0_;
267 const std::vector<double>&
x_;
288 const std::vector<stan::math::var>& y0,
289 const std::vector<double>& theta,
290 const std::vector<double>& x,
291 const std::vector<int>& x_int,
295 y0_dbl_(y0.
size(), 0.0),
302 size_(N_ + N_ * N_) {
303 for (
size_t n = 0; n <
N_; n++)
324 std::vector<double>& dz_dt,
329 std::vector<double> y(z.begin(), z.begin() +
N_);
330 for (
size_t n = 0; n <
N_; n++)
333 dz_dt = f_(t, y, theta_dbl_, x_, x_int_, msgs_);
335 "dz_dt", dz_dt.size(),
N_);
337 std::vector<double> coupled_sys(N_ * N_);
338 std::vector<double>
grad(N_);
346 vector<var> y_vars(y.begin(), y.end());
347 z_vars.insert(z_vars.end(), y_vars.begin(), y_vars.end());
349 vector<var> dy_dt_vars = f_(t, y_vars, theta_dbl_, x_, x_int_, msgs_);
351 for (
size_t i = 0; i <
N_; i++) {
353 dy_dt_vars[i].grad(z_vars, grad);
355 for (
size_t j = 0; j <
N_; j++) {
359 double temp_deriv = grad[j];
360 for (
size_t k = 0; k <
N_; k++)
361 temp_deriv += z[N_ + N_ * j + k] * grad[k];
363 coupled_sys[i + j *
N_] = temp_deriv;
366 }
catch (
const std::exception&
e) {
372 dz_dt.insert(dz_dt.end(), coupled_sys.begin(), coupled_sys.end());
399 return std::vector<double>(
size_, 0.0);
409 std::vector<std::vector<stan::math::var> >
415 vector<var> temp_vars(N_);
416 vector<double> temp_gradients(N_);
417 vector<vector<var> > y_return(y.size());
419 for (
size_t i = 0; i < y.size(); i++) {
421 for (
size_t j = 0; j <
N_; j++) {
423 for (
size_t k = 0; k <
N_; k++)
424 temp_gradients[k] = y[i][y0_.size() + y0_.size() * k + j];
427 y0_, temp_gradients);
429 y_return[i] = temp_vars;
473 template <
typename F>
476 const std::vector<stan::math::var>&
y0_;
478 const std::vector<stan::math::var>&
theta_;
480 const std::vector<double>&
x_;
501 const std::vector<stan::math::var>& y0,
502 const std::vector<stan::math::var>& theta,
503 const std::vector<double>& x,
504 const std::vector<int>& x_int,
508 y0_dbl_(y0.
size(), 0.0),
510 theta_dbl_(theta.
size(), 0.0),
515 size_(N_ + N_ * (N_ + M_)),
517 for (
size_t n = 0; n <
N_; n++)
520 for (
size_t m = 0; m <
M_; m++)
541 std::vector<double>& dz_dt,
546 vector<double> y(z.begin(), z.begin() +
N_);
547 for (
size_t n = 0; n <
N_; n++)
550 dz_dt = f_(t, y, theta_dbl_, x_, x_int_, msgs_);
552 "dz_dt", dz_dt.size(),
N_);
554 vector<double> coupled_sys(N_ * (N_ + M_));
555 vector<double>
grad(N_ + M_);
561 z_vars.reserve(N_ + M_);
563 vector<var> y_vars(y.begin(), y.end());
564 z_vars.insert(z_vars.end(), y_vars.begin(), y_vars.end());
566 vector<var> theta_vars(theta_dbl_.begin(), theta_dbl_.end());
567 z_vars.insert(z_vars.end(), theta_vars.begin(), theta_vars.end());
569 vector<var> dy_dt_vars = f_(t, y_vars, theta_vars, x_, x_int_, msgs_);
571 for (
size_t i = 0; i <
N_; i++) {
573 dy_dt_vars[i].grad(z_vars, grad);
575 for (
size_t j = 0; j < N_ +
M_; j++) {
579 double temp_deriv = grad[j];
580 for (
size_t k = 0; k <
N_; k++)
581 temp_deriv += z[N_ + N_ * j + k] * grad[k];
583 coupled_sys[i + j *
N_] = temp_deriv;
586 }
catch (
const std::exception&
e) {
592 dz_dt.insert(dz_dt.end(), coupled_sys.begin(), coupled_sys.end());
616 return std::vector<double>(
size_, 0.0);
626 std::vector<std::vector<stan::math::var> >
632 vector<var> vars = y0_;
633 vars.insert(vars.end(), theta_.begin(), theta_.end());
635 vector<var> temp_vars(N_);
636 vector<double> temp_gradients(N_ + M_);
637 vector<vector<var> > y_return(y.size());
639 for (
size_t i = 0; i < y.size(); i++) {
641 for (
size_t j = 0; j <
N_; j++) {
643 for (
size_t k = 0; k < N_ +
M_; k++)
644 temp_gradients[k] = y[i][N_ + N_ * k + j];
647 vars, temp_gradients);
649 y_return[i] = temp_vars;
var precomputed_gradients(const double value, const std::vector< var > &operands, const std::vector< double > &gradients)
This function returns a var for an expression that has the specified value, vector of operands...
std::vector< std::vector< stan::math::var > > decouple_states(const std::vector< std::vector< double > > &y)
Returns the base ODE system state corresponding to the specified coupled system state.
const std::vector< stan::math::var > & theta_
std::vector< double > y0_dbl_
std::vector< std::vector< stan::math::var > > decouple_states(const std::vector< std::vector< double > > &y)
Return the solutions to the basic ODE system, including appropriate autodiff partial derivatives...
coupled_ode_system(const F &f, const std::vector< double > &y0, const std::vector< stan::math::var > &theta, const std::vector< double > &x, const std::vector< int > &x_int, std::ostream *msgs)
Construct a coupled ODE system with the specified base ODE system, base initial state, parameters, data, and a message stream.
std::vector< double > theta_dbl_
T value_of(const fvar< T > &v)
Return the value of the specified variable.
static void set_zero_all_adjoints_nested()
Reset all adjoint values in the top nested portion of the stack to zero.
std::vector< double > initial_state()
Returns the initial state of the coupled system.
std::vector< std::vector< stan::math::var > > decouple_states(const std::vector< std::vector< double > > &y)
Return the basic ODE solutions given the specified coupled system solutions, including the partials v...
Independent (input) and dependent (output) variables for gradients.
static void grad(vari *vi)
Compute the gradient for all variables starting from the specified root variable implementation.
size_t size() const
Returns the size of the coupled system.
std::vector< double > theta_dbl_
const std::vector< double > & theta_dbl_
void operator()(const std::vector< double > &z, std::vector< double > &dz_dt, double t)
Calculates the derivative of the coupled ode system with respect to the state y at time t...
void operator()(const std::vector< double > &z, std::vector< double > &dz_dt, double t)
Assign the derivative vector with the system derivatives at the specified state and time...
std::vector< double > initial_state()
Returns the initial state of the coupled system.
coupled_ode_system(const F &f, const std::vector< stan::math::var > &y0, const std::vector< double > &theta, const std::vector< double > &x, const std::vector< int > &x_int, std::ostream *msgs)
Construct a coupled ODE system for an unknown initial state and known parameters givne the specified ...
const std::vector< int > & x_int_
bool check_equal(const char *function, const char *name, const T_y &y, const T_eq &eq)
Return true if y is equal to eq.
const std::vector< double > & y0_dbl_
void add_initial_values(const std::vector< stan::math::var > &y0, std::vector< std::vector< stan::math::var > > &y)
Increment the state derived from the coupled system in the with the original initial state...
const std::vector< double > & x_
const std::vector< int > & x_int_
double e()
Return the base of the natural logarithm.
const std::vector< stan::math::var > & y0_
std::vector< double > initial_state()
Returns the initial state of the coupled system.
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
const std::vector< stan::math::var > & theta_
coupled_ode_system(const F &f, const std::vector< stan::math::var > &y0, const std::vector< stan::math::var > &theta, const std::vector< double > &x, const std::vector< int > &x_int, std::ostream *msgs)
Construct a coupled ODE system with unknown initial value and known parameters, given the base ODE sy...
const std::vector< double > & x_
void operator()(const std::vector< double > &z, std::vector< double > &dz_dt, double t)
Populates the derivative vector with derivatives of the coupled ODE system state with respect to time...
const std::vector< double > & x_
Base template class for a coupled ordinary differential equation system, which adds sensitivities to ...
static void recover_memory_nested()
Recover only the memory used for the top nested call.
const std::vector< stan::math::var > & y0_
std::vector< double > y0_dbl_
size_t size() const
Returns the size of the coupled system.
size_t size() const
Returns the size of the coupled system.
const std::vector< int > & x_int_
static void start_nested()
Record the current position so that recover_memory_nested() can find it.