Public Member Functions | Public Attributes | List of all members
stan::math::coupled_ode_system< F, var, double > Struct Template Reference

#include "stan/math/rev/arr/functor/coupled_ode_system.hpp"

Public Member Functions

 coupled_ode_system (const F &f, const std::vector< var > &y0, const std::vector< double > &theta, const std::vector< double > &x, const std::vector< int > &x_int, std::ostream *msgs)
 
void operator() (const std::vector< double > &z, std::vector< double > &dz_dt, double t) const
 
size_t size () const
 
std::vector< double > initial_state () const
 
std::vector< std::vector< var > > decouple_states (const std::vector< std::vector< double > > &y) const
 

Public Attributes

const Ff_
 
const std::vector< var > & y0_
 
const std::vector< double > y0_dbl_
 
const std::vector< double > & theta_dbl_
 
const std::vector< double > & x_
 
const std::vector< int > & x_int_
 
std::ostream * msgs_
 
const size_t N_
 
const size_t M_
 
const size_t size_
 

Detailed Description

template<typename F>
struct stan::math::coupled_ode_system< F, var, double >

The coupled ODE system for unknown initial values and known parameters.

If the original ODE has states of size N, the coupled system has N + N * N states. (derivatives of each state with respect to each initial value)

The coupled system has N + N * N states, where N is the size of the state vector in the base system.

The first N states correspond to the base system's N states: $ \frac{d x_n}{dt} $

The next N states correspond to the sensitivities of the initial conditions with respect to the to the first base system equation:

\[ \frac{d x_{N+n}}{dt} = \frac{d}{dt} \frac{\partial x_1}{\partial y0_n} \]

The next N states correspond to the sensitivities with respect to the second base system equation, etc.

Template Parameters
Ftype of base ODE system functor

Definition at line 227 of file coupled_ode_system.hpp.

Constructor & Destructor Documentation

template<typename F >
stan::math::coupled_ode_system< F, var, double >::coupled_ode_system ( const F f,
const std::vector< var > &  y0,
const std::vector< double > &  theta,
const std::vector< double > &  x,
const std::vector< int > &  x_int,
std::ostream *  msgs 
)
inline

Construct a coupled ODE system for an unknown initial state and known parameters givne the specified base system functor, base initial state, parameters, data, and an output stream for messages.

Parameters
[in]fbase ODE system functor.
[in]y0initial state of the base ODE.
[in]thetasystem parameters.
[in]xreal data.
[in]x_intinteger data.
[in,out]msgsoutput stream for messages.

Definition at line 252 of file coupled_ode_system.hpp.

256  : f_(f),
257  y0_(y0),
258  y0_dbl_(value_of(y0)),
259  theta_dbl_(theta),
260  x_(x),
261  x_int_(x_int),
262  msgs_(msgs),
263  N_(y0.size()),
264  M_(theta.size()),
265  size_(N_ + N_ * N_) {}
T value_of(const fvar< T > &v)
Definition: value_of.hpp:16

Member Function Documentation

template<typename F >
std::vector<std::vector<var> > stan::math::coupled_ode_system< F, var, double >::decouple_states ( const std::vector< std::vector< double > > &  y) const
inline

Return the solutions to the basic ODE system, including appropriate autodiff partial derivatives, given the specified coupled system solution.

Parameters
ythe vector of the coupled states after solving the ode

Definition at line 359 of file coupled_ode_system.hpp.

References MECModelEnuComparisons::i, calib::j, N_, stan::math::precomputed_gradients(), and submit_syst::y.

360  {
361  using std::vector;
362 
363  vector<var> temp_vars(N_);
364  vector<double> temp_gradients(N_);
365  vector<vector<var> > y_return(y.size());
366 
367  for (size_t i = 0; i < y.size(); i++) {
368  // iterate over number of equations
369  for (size_t j = 0; j < N_; j++) {
370  // iterate over parameters for each equation
371  for (size_t k = 0; k < N_; k++)
372  temp_gradients[k] = y[i][y0_.size() + y0_.size() * k + j];
373 
374  temp_vars[j] = precomputed_gradients(y[i][j], y0_, temp_gradients);
375  }
376  y_return[i] = temp_vars;
377  }
378 
379  return y_return;
380  }
var precomputed_gradients(double value, const std::vector< var > &operands, const std::vector< double > &gradients)
const double j
Definition: BetheBloch.cxx:29
template<typename F >
std::vector<double> stan::math::coupled_ode_system< F, var, double >::initial_state ( ) const
inline

Returns the initial state of the coupled system.

Because the starting state is unknown, the coupled system incorporates the initial conditions as parameters. The initial conditions for the coupled part of the system are set to zero along with the rest of the initial state, because the value of the initial state has been moved into the parameters.

Returns
the initial condition of the coupled system. This is a vector of length size() where all elements are 0.

Definition at line 343 of file coupled_ode_system.hpp.

References MECModelEnuComparisons::i, and N_.

343  {
344  std::vector<double> initial(size_, 0.0);
345  for (size_t i = 0; i < N_; i++)
346  initial[i] = y0_dbl_[i];
347  for (size_t i = 0; i < N_; i++)
348  initial[N_ + i * N_ + i] = 1.0;
349  return initial;
350  }
template<typename F >
void stan::math::coupled_ode_system< F, var, double >::operator() ( const std::vector< double > &  z,
std::vector< double > &  dz_dt,
double  t 
) const
inline

Calculates the derivative of the coupled ode system with respect to the state y at time t.

Parameters
[in]zthe current state of the coupled, shifted ode system. This is a a vector of double of length size().
[out]dz_dta vector of length size() with the derivatives of the coupled system evaluated with state y and time t.
[in]ttime.
Exceptions
exceptionif the system functor does not return a derivative vector of the same size as the state vector.

y is the base ODE system state

Definition at line 283 of file coupled_ode_system.hpp.

References stan::math::check_size_match(), stan::math::e(), stan::math::grad(), MECModelEnuComparisons::i, calib::j, N_, stan::math::recover_memory_nested(), stan::math::set_zero_all_adjoints_nested(), and stan::math::start_nested().

284  {
285  using std::vector;
286 
287  std::vector<double> grad(N_);
288 
289  try {
290  start_nested();
291 
292  vector<var> y_vars(z.begin(), z.begin() + N_);
293 
294  vector<var> dy_dt_vars = f_(t, y_vars, theta_dbl_, x_, x_int_, msgs_);
295 
296  check_size_match("coupled_ode_system", "dz_dt", dy_dt_vars.size(),
297  "states", N_);
298 
299  for (size_t i = 0; i < N_; i++) {
300  dz_dt[i] = dy_dt_vars[i].val();
302  dy_dt_vars[i].grad(y_vars, grad);
303 
304  for (size_t j = 0; j < N_; j++) {
305  // orders derivatives by equation (i.e. if there are 2 eqns
306  // (y1, y2) and 2 parameters (a, b), dy_dt will be ordered as:
307  // dy1_dt, dy2_dt, dy1_da, dy2_da, dy1_db, dy2_db
308  double temp_deriv = 0;
309  for (size_t k = 0; k < N_; k++)
310  temp_deriv += z[N_ + N_ * j + k] * grad[k];
311 
312  dz_dt[N_ + i + j * N_] = temp_deriv;
313  }
314  }
315  } catch (const std::exception& e) {
317  throw;
318  }
320  }
static void set_zero_all_adjoints_nested()
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
static void grad(vari *vi)
Definition: grad.hpp:30
const double j
Definition: BetheBloch.cxx:29
z
Definition: test.py:28
double e()
Definition: constants.hpp:89
static void recover_memory_nested()
static void start_nested()
template<typename F >
size_t stan::math::coupled_ode_system< F, var, double >::size ( void  ) const
inline

Returns the size of the coupled system.

Returns
size of the coupled system.

Definition at line 327 of file coupled_ode_system.hpp.

References size_.

Member Data Documentation

template<typename F >
const F& stan::math::coupled_ode_system< F, var, double >::f_

Definition at line 228 of file coupled_ode_system.hpp.

template<typename F >
const size_t stan::math::coupled_ode_system< F, var, double >::M_

Definition at line 236 of file coupled_ode_system.hpp.

template<typename F >
std::ostream* stan::math::coupled_ode_system< F, var, double >::msgs_

Definition at line 234 of file coupled_ode_system.hpp.

template<typename F >
const size_t stan::math::coupled_ode_system< F, var, double >::N_

Definition at line 235 of file coupled_ode_system.hpp.

template<typename F >
const size_t stan::math::coupled_ode_system< F, var, double >::size_

Definition at line 237 of file coupled_ode_system.hpp.

template<typename F >
const std::vector<double>& stan::math::coupled_ode_system< F, var, double >::theta_dbl_

Definition at line 231 of file coupled_ode_system.hpp.

template<typename F >
const std::vector<double>& stan::math::coupled_ode_system< F, var, double >::x_

Definition at line 232 of file coupled_ode_system.hpp.

template<typename F >
const std::vector<int>& stan::math::coupled_ode_system< F, var, double >::x_int_

Definition at line 233 of file coupled_ode_system.hpp.

template<typename F >
const std::vector<var>& stan::math::coupled_ode_system< F, var, double >::y0_

Definition at line 229 of file coupled_ode_system.hpp.

template<typename F >
const std::vector<double> stan::math::coupled_ode_system< F, var, double >::y0_dbl_

Definition at line 230 of file coupled_ode_system.hpp.


The documentation for this struct was generated from the following file: