Automatic Differentiation
 
Loading...
Searching...
No Matches
coupled_ode_system.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUNCTOR_COUPLED_ODE_SYSTEM_HPP
2#define STAN_MATH_PRIM_FUNCTOR_COUPLED_ODE_SYSTEM_HPP
3
8#include <ostream>
9#include <vector>
10
11namespace stan {
12namespace math {
13
14template <bool ArithmeticArguments, typename F, typename T_y0, typename... Args>
16
27template <typename F, typename T_y0, typename... Args>
28struct coupled_ode_system_impl<true, F, T_y0, Args...> {
29 const F& f_;
30 const Eigen::VectorXd& y0_;
31 std::tuple<const Args&...> args_tuple_;
32 const size_t N_;
33 std::ostream* msgs_;
34
45 coupled_ode_system_impl(const F& f, const Eigen::VectorXd& y0,
46 std::ostream* msgs, const Args&... args)
47 : f_(f), y0_(y0), args_tuple_(args...), N_(y0.size()), msgs_(msgs) {}
48
62 void operator()(const std::vector<double>& z, std::vector<double>& dz_dt,
63 double t) const {
64 Eigen::VectorXd y(z.size());
65 for (size_t n = 0; n < z.size(); ++n)
66 y.coeffRef(n) = z[n];
67
68 dz_dt.resize(y.size());
69
70 Eigen::VectorXd f_y_t = math::apply(
71 [&](const Args&... args) { return f_(t, y, msgs_, args...); },
72 args_tuple_);
73
74 check_size_match("coupled_ode_system", "dy_dt", f_y_t.size(), "states",
75 y.size());
76
77 Eigen::Map<Eigen::VectorXd>(dz_dt.data(), dz_dt.size()) = f_y_t;
78 }
79
85 size_t size() const { return N_; }
86
93 std::vector<double> initial_state() const {
94 std::vector<double> initial(size(), 0.0);
95
96 for (size_t i = 0; i < N_; i++) {
97 initial[i] = value_of(y0_(i));
98 }
99
100 return initial;
101 }
102};
103
104template <typename F, typename T_y0, typename... Args>
107 std::is_arithmetic<return_type_t<T_y0, Args...>>::value, F, T_y0,
108 Args...> {
110 const Eigen::Matrix<T_y0, Eigen::Dynamic, 1>& y0,
111 std::ostream* msgs, const Args&... args)
113 std::is_arithmetic<return_type_t<T_y0, Args...>>::value, F, T_y0,
114 Args...>(f, y0, msgs, args...) {}
115};
116
117} // namespace math
118} // namespace stan
119#endif
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>>.
Definition size.hpp:19
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
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.
constexpr decltype(auto) apply(F &&f, Tuple &&t, PreArgs &&... pre_args)
Definition apply.hpp:52
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
STL namespace.
coupled_ode_system_impl(const F &f, const Eigen::VectorXd &y0, std::ostream *msgs, const Args &... args)
Construct a coupled ode system from the base system function, initial state of the base system,...
std::vector< double > initial_state() const
Returns the initial state of the coupled system.
size_t size() const
Returns the size of the coupled system.
void operator()(const std::vector< double > &z, std::vector< double > &dz_dt, double t) const
Evaluate the right hand side of the coupled system at state z and time t, and store the sensitivities...
coupled_ode_system(const F &f, const Eigen::Matrix< T_y0, Eigen::Dynamic, 1 > &y0, std::ostream *msgs, const Args &... args)