Stan Math Library  2.20.0
reverse mode automatic differentiation
coupled_ode_observer.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_ARR_FUNCTOR_COUPLED_ODE_OBSERVER_HPP
2 #define STAN_MATH_PRIM_ARR_FUNCTOR_COUPLED_ODE_OBSERVER_HPP
3 
8 
9 #include <vector>
10 
11 namespace stan {
12 namespace math {
13 
29 template <typename F, typename T1, typename T2, typename T_t0, typename T_ts>
32 
33  typedef operands_and_partials<std::vector<T1>, std::vector<T2>, T_t0, T_ts>
35 
36  const F& f_;
37  const std::vector<T1>& y0_;
38  const T_t0& t0_;
39  const std::vector<T_ts>& ts_;
40  const std::vector<T2>& theta_;
41  const std::vector<double>& x_;
42  const std::vector<int>& x_int_;
43  std::ostream* msgs_;
44  std::vector<std::vector<return_t>>& y_;
45  const std::size_t N_;
46  const std::size_t M_;
47  const std::size_t index_offset_theta_;
49 
70  coupled_ode_observer(const F& f, const std::vector<T1>& y0,
71  const std::vector<T2>& theta, const T_t0& t0,
72  const std::vector<T_ts>& ts,
73  const std::vector<double>& x,
74  const std::vector<int>& x_int, std::ostream* msgs,
75  std::vector<std::vector<return_t>>& y)
76  : f_(f),
77  y0_(y0),
78  t0_(t0),
79  ts_(ts),
80  theta_(theta),
81  x_(x),
82  x_int_(x_int),
83  msgs_(msgs),
84  y_(y),
85  N_(y0.size()),
86  M_(theta.size()),
87  index_offset_theta_(is_constant_all<T1>::value ? 0 : N_ * N_),
88  next_ts_index_(0) {}
89 
103  void operator()(const std::vector<double>& coupled_state, double t) {
104  check_less("coupled_ode_observer", "time-state number", next_ts_index_,
105  ts_.size());
106 
107  std::vector<return_t> yt;
108  yt.reserve(N_);
109 
110  ops_partials_t ops_partials(y0_, theta_, t0_, ts_[next_ts_index_]);
111 
112  std::vector<double> dy_dt;
114  std::vector<double> y_dbl(coupled_state.begin(),
115  coupled_state.begin() + N_);
116  dy_dt = f_(value_of(ts_[next_ts_index_]), y_dbl, value_of(theta_), x_,
117  x_int_, msgs_);
118  check_size_match("coupled_ode_observer", "dy_dt", dy_dt.size(), "states",
119  N_);
120  }
121 
122  for (size_t j = 0; j < N_; j++) {
123  // iterate over parameters for each equation
125  for (std::size_t k = 0; k < N_; k++)
126  ops_partials.edge1_.partials_[k] = coupled_state[N_ + N_ * k + j];
127  }
128 
130  for (std::size_t k = 0; k < M_; k++)
131  ops_partials.edge2_.partials_[k]
132  = coupled_state[N_ + index_offset_theta_ + N_ * k + j];
133  }
134 
136  ops_partials.edge4_.partials_[0] = dy_dt[j];
137  }
138 
139  yt.emplace_back(ops_partials.build(coupled_state[j]));
140  }
141 
142  y_.emplace_back(yt);
143  next_ts_index_++;
144  }
145 };
146 
147 } // namespace math
148 
149 } // namespace stan
150 
151 #endif
stan::return_type< T1, T2, T_t0, T_ts >::type return_t
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:17
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
Definition: conjunction.hpp:14
internal::ops_partials_edge< double, Op4 > edge4_
This template builds partial derivatives with respect to a set of operands.
coupled_ode_observer(const F &f, const std::vector< T1 > &y0, const std::vector< T2 > &theta, const T_t0 &t0, const std::vector< T_ts > &ts, const std::vector< double > &x, const std::vector< int > &x_int, std::ostream *msgs, std::vector< std::vector< return_t >> &y)
Construct a coupled ODE observer for the specified coupled vector.
void operator()(const std::vector< double > &coupled_state, double t)
Callback function for ODE solvers to record values.
operands_and_partials< std::vector< T1 >, std::vector< T2 >, T_t0, T_ts > ops_partials_t
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Check if the provided sizes match.
Observer for the coupled states.
const std::vector< T_ts > & ts_
boost::math::tools::promote_args< double, typename scalar_type< T >::type, typename return_type< Types_pack... >::type >::type type
Definition: return_type.hpp:36
const std::vector< double > & x_
empty_broadcast_array< ViewElt, Op > partials_
T_return_type build(double value)
Build the node to be stored on the autodiff graph.
std::vector< std::vector< return_t > > & y_
internal::ops_partials_edge< double, Op2 > edge2_
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
void check_less(const char *function, const char *name, const T_y &y, const T_high &high)
Check if y is strictly less than high.
Definition: check_less.hpp:63
internal::ops_partials_edge< double, Op1 > edge1_
const std::vector< int > & x_int_

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