Public Member Functions | Public Attributes | List of all members
stan::math::coupled_ode_system< F, var, var > 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< var > &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< var > & theta_
 
const std::vector< double > theta_dbl_
 
const std::vector< double > & x_
 
const std::vector< int > & x_int_
 
const size_t N_
 
const size_t M_
 
const size_t size_
 
std::ostream * msgs_
 

Detailed Description

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

The coupled ode system for unknown intial values and unknown parameters.

The coupled system has N + N * (N + M) states, where N is size of the base ODE state vector and M is the number of parameters.

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

The next N+M states correspond to the sensitivities of the initial conditions, then to the parameters 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} \]

\[ \frac{d x_{N+N+m}}{dt} = \frac{d}{dt} \frac{\partial x_1}{\partial \theta_m} \]

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

If the original ode has a state vector of size N states and a parameter vector of size M, the coupled system has N + N * (N

Template Parameters
Fthe functor for the base ode system

Definition at line 419 of file coupled_ode_system.hpp.

Constructor & Destructor Documentation

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

Construct a coupled ODE system with unknown initial value and known parameters, given the base ODE system functor, the initial state of the base ODE, the parameters, data, and an output stream to which to write messages.

Parameters
[in]fthe base ode system functor.
[in]y0the initial state of the base ode.
[in]thetaparameters of the base ode.
[in]xreal data.
[in]x_intinteger data.
[in,out]msgsoutput stream to which to print messages.

Definition at line 445 of file coupled_ode_system.hpp.

449  : f_(f),
450  y0_(y0),
451  y0_dbl_(value_of(y0)),
452  theta_(theta),
454  x_(x),
455  x_int_(x_int),
456  N_(y0.size()),
457  M_(theta.size()),
458  size_(N_ + N_ * (N_ + M_)),
459  msgs_(msgs) {}
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, var >::decouple_states ( const std::vector< std::vector< double > > &  y) const
inline

Return the basic ODE solutions given the specified coupled system solutions, including the partials versus the parameters encoded in the autodiff results.

Parameters
ythe vector of the coupled states after solving the ode

Definition at line 557 of file coupled_ode_system.hpp.

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

558  {
559  using std::vector;
560 
561  vector<var> vars = y0_;
562  vars.insert(vars.end(), theta_.begin(), theta_.end());
563 
564  vector<var> temp_vars(N_);
565  vector<double> temp_gradients(N_ + M_);
566  vector<vector<var> > y_return(y.size());
567 
568  for (size_t i = 0; i < y.size(); i++) {
569  // iterate over number of equations
570  for (size_t j = 0; j < N_; j++) {
571  // iterate over parameters for each equation
572  for (size_t k = 0; k < N_ + M_; k++)
573  temp_gradients[k] = y[i][N_ + N_ * k + j];
574 
575  temp_vars[j] = precomputed_gradients(y[i][j], vars, temp_gradients);
576  }
577  y_return[i] = temp_vars;
578  }
579 
580  return y_return;
581  }
var precomputed_gradients(double value, const std::vector< var > &operands, const std::vector< double > &gradients)
const std::map< std::pair< std::string, std::string >, Variable > vars
const double j
Definition: BetheBloch.cxx:29
template<typename F >
std::vector<double> stan::math::coupled_ode_system< F, var, var >::initial_state ( ) const
inline

Returns the initial state of the coupled system.

Because the initial state is unknown, the coupled system incorporates the initial condition offset from zero as a parameter, and hence the return of this function is a vector of zeros.

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

Definition at line 541 of file coupled_ode_system.hpp.

References MECModelEnuComparisons::i, and N_.

541  {
542  std::vector<double> initial(size_, 0.0);
543  for (size_t i = 0; i < N_; i++)
544  initial[i] = y0_dbl_[i];
545  for (size_t i = 0; i < N_; i++)
546  initial[N_ + i * N_ + i] = 1.0;
547  return initial;
548  }
template<typename F >
void stan::math::coupled_ode_system< F, var, var >::operator() ( const std::vector< double > &  z,
std::vector< double > &  dz_dt,
double  t 
) const
inline

Populates the derivative vector with derivatives of the coupled ODE system state with respect to time evaluated at the specified state and specified time.

Parameters
[in]zthe current state of the coupled, shifted ode system, of size size().
[in,out]dz_dtpopulate with the derivatives of the coupled system evaluated at the specified state and time.
[in]ttime.
Exceptions
exceptionif the base system does not return a derivative vector of the same size as the state vector.

y is the base ODE system state

Definition at line 477 of file coupled_ode_system.hpp.

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

478  {
479  using std::vector;
480 
481  vector<double> grad(N_ + M_);
482 
483  try {
484  start_nested();
485 
486  vector<var> z_vars;
487  z_vars.reserve(N_ + M_);
488 
489  vector<var> y_vars(z.begin(), z.begin() + N_);
490  z_vars.insert(z_vars.end(), y_vars.begin(), y_vars.end());
491 
492  vector<var> theta_vars(theta_dbl_.begin(), theta_dbl_.end());
493  z_vars.insert(z_vars.end(), theta_vars.begin(), theta_vars.end());
494 
495  vector<var> dy_dt_vars = f_(t, y_vars, theta_vars, x_, x_int_, msgs_);
496 
497  check_size_match("coupled_ode_system", "dz_dt", dy_dt_vars.size(),
498  "states", N_);
499 
500  for (size_t i = 0; i < N_; i++) {
501  dz_dt[i] = dy_dt_vars[i].val();
503  dy_dt_vars[i].grad(z_vars, grad);
504 
505  for (size_t j = 0; j < N_ + M_; j++) {
506  // orders derivatives by equation (i.e. if there are 2 eqns
507  // (y1, y2) and 2 parameters (a, b), dy_dt will be ordered as:
508  // dy1_dt, dy2_dt, dy1_da, dy2_da, dy1_db, dy2_db
509  double temp_deriv = j < N_ ? 0 : grad[j];
510  for (size_t k = 0; k < N_; k++)
511  temp_deriv += z[N_ + N_ * j + k] * grad[k];
512 
513  dz_dt[N_ + i + j * N_] = temp_deriv;
514  }
515  }
516  } catch (const std::exception& e) {
518  throw;
519  }
521  }
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, var >::size ( void  ) const
inline

Returns the size of the coupled system.

Returns
size of the coupled system.

Definition at line 528 of file coupled_ode_system.hpp.

References size_.

Member Data Documentation

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

Definition at line 420 of file coupled_ode_system.hpp.

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

Definition at line 428 of file coupled_ode_system.hpp.

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

Definition at line 430 of file coupled_ode_system.hpp.

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

Definition at line 427 of file coupled_ode_system.hpp.

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

Definition at line 429 of file coupled_ode_system.hpp.

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

Definition at line 423 of file coupled_ode_system.hpp.

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

Definition at line 424 of file coupled_ode_system.hpp.

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

Definition at line 425 of file coupled_ode_system.hpp.

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

Definition at line 426 of file coupled_ode_system.hpp.

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

Definition at line 421 of file coupled_ode_system.hpp.

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

Definition at line 422 of file coupled_ode_system.hpp.


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