1#ifndef STAN_MATH_REV_FUNCTOR_DAE_RESIDUAL_HPP
2#define STAN_MATH_REV_FUNCTOR_DAE_RESIDUAL_HPP
15#include <nvector/nvector_serial.h>
35template <
typename F,
typename Tyy,
typename Typ,
typename... T_par>
41 std::tuple<decltype(deep_copy_vars(std::declval<const T_par&>()))...>
43 std::tuple<plain_type_t<decltype(value_of(std::declval<const T_par&>()))>...>
81 dae_system(
const F& f,
const Tyy& yy0,
const Typ& yp0, std::ostream* msgs,
99 for (
int i = 0; i <
ns; ++i) {
116 dae->dbl_yy = Eigen::Map<Eigen::VectorXd>(NV_DATA_S(
yy),
dae->N);
117 dae->dbl_yp = Eigen::Map<Eigen::VectorXd>(NV_DATA_S(
yp),
dae->N);
118 dae->eval_residual(t);
119 Eigen::Map<Eigen::VectorXd>(NV_DATA_S(rr),
dae->N) =
dae->dbl_rr;
128 N_Vector res, N_Vector* yys, N_Vector* yps,
129 N_Vector* ress,
void* user_data, N_Vector temp1,
130 N_Vector temp2, N_Vector temp3) {
133 const int n =
dae->N;
134 const int m =
dae->M;
136 for (
int i = 0; i <
ns; ++i) {
137 N_VConst(0.0, ress[i]);
143 Eigen::Matrix<
var, -1, 1> yy_var(n), yp_var(n);
144 for (
int i = 0; i < n; ++i) {
145 yy_var.coeffRef(i) = NV_Ith_S(
yy, i);
146 yp_var.coeffRef(i) = NV_Ith_S(
yp, i);
149 Eigen::VectorXd g(m);
151 [&](
auto&&... args) {
152 return dae->f_(t, yy_var, yp_var,
dae->msgs_, args...);
154 dae->local_args_tuple_);
156 for (
int i = 0; i < n; ++i) {
162 for (
int j = 0; j <
ns; ++j) {
163 auto yysp = N_VGetArrayPointer(yys[j]);
164 auto ypsp = N_VGetArrayPointer(yps[j]);
165 auto ressp = N_VGetArrayPointer(ress[j]);
166 for (
int k = 0; k < n; ++k) {
167 ressp[i] += yy_var[k].adj() * yysp[k] + yp_var[k].adj() * ypsp[k];
174 [&](
auto&&... args) {
177 dae->local_args_tuple_);
178 for (
int j = 0; j < m; ++j) {
179 auto ressp = N_VGetArrayPointer(ress[
ns - m + j]);
184 dae->local_args_tuple_);
std::tuple< const T_par &... > args_tuple_
stan::return_type_t< Tyy, Typ, T_par... > scalar_t
static constexpr bool is_var_par
static int idas_sens_res(int ns, double t, N_Vector yy, N_Vector yp, N_Vector res, N_Vector *yys, N_Vector *yps, N_Vector *ress, void *user_data, N_Vector temp1, N_Vector temp2, N_Vector temp3)
Evaluate DAE sensitivity residual according to IDAS signature.
static int idas_res(double t, N_Vector yy, N_Vector yp, N_Vector rr, void *user_data)
Evaluate DAE residual according to IDAS signature.
void eval_residual(double t)
std::vector< stan::math::var > all_vars
Tyy const & yy
state variable y
const F & f_
residual functor
std::tuple< plain_type_t< decltype(value_of(std::declval< const T_par & >()))> dbl_args_tuple_
dae_system(const F &f, const Tyy &yy0, const Typ &yp0, std::ostream *msgs, const T_par &... args)
Construct IDAS DAE system from initial condition and parameters.
Typ const & yp
time derivatives of y
std::vector< Eigen::Matrix< scalar_t, -1, 1 > > return_t
static constexpr bool use_fwd_sens
std::tuple< decltype(deep_copy_vars(std::declval< const T_par & >()))... > local_args_tuple_
static constexpr bool is_var_yp0
static constexpr bool is_var_yy0
IDAS DAE system that contains information on residual equation functor, sensitivity residual equation...
void set_zero_all_adjoints()
Reset all adjoint values in this nested stack to zero.
A class following the RAII idiom to start and recover nested autodiff scopes.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
int64_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
constexpr auto for_each(F &&f, T &&t)
Apply a function to each element of a tuple.
fvar< T > arg(const std::complex< fvar< T > > &z)
Return the phase angle of the complex argument.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Arith deep_copy_vars(Arith &&arg)
Forward arguments that do not contain vars.
size_t count_vars(Pargs &&... args)
Count the number of vars in the input argument list.
vari ** save_varis(vari **dest, const var &x, Pargs &&... args)
Save the vari pointer in x into the memory pointed to by dest, increment the dest storage pointer,...
std::vector< Eigen::Matrix< stan::return_type_t< T_yy, T_yp, T_Args... >, -1, 1 > > dae(const F &f, const T_yy &yy0, const T_yp &yp0, double t0, const std::vector< double > &ts, std::ostream *msgs, const T_Args &... args)
Solve the DAE initial value problem f(t, y, y')=0, y(t0) = yy0, y'(t0)=yp0 at a set of times,...
void zero_adjoints() noexcept
End of recursion for set_zero_adjoints.
double * accumulate_adjoints(double *dest, const var &x, Pargs &&... args)
Accumulate adjoints from x into storage pointed to by dest, increment the adjoint storage pointer,...
constexpr decltype(auto) apply(F &&f, Tuple &&t, PreArgs &&... pre_args)
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Defines a static member named value which is defined to be false as the primitive scalar types cannot...
This struct always provides access to the autodiff stack using the singleton pattern.