1#ifndef STAN_MATH_REV_FUNCTOR_KINSOL_DATA_HPP
2#define STAN_MATH_REV_FUNCTOR_KINSOL_DATA_HPP
9#include <sundials/sundials_context.h>
10#include <kinsol/kinsol.h>
11#include <sunmatrix/sunmatrix_dense.h>
12#include <sunlinsol/sunlinsol_dense.h>
13#include <nvector/nvector_serial.h>
28template <
typename F1,
typename... Args>
31 const Eigen::VectorXd&
x_;
47 std::ostream*
const msgs,
const Args&... args)
60 N_VDestroy_Serial(
nv_x_);
68 void*
const user_data) {
72 Eigen::VectorXd x_eigen(
73 Eigen::Map<Eigen::VectorXd>(NV_DATA_S(x), explicit_system->
N_));
75 Eigen::Map<Eigen::VectorXd> f_eval_map(N_VGetArrayPointer(f_eval),
78 [&](
const auto&... args) {
79 return explicit_system->
f_(x_eigen, explicit_system->
msgs_, args...);
83 "the vector of unknowns, x,", f_eval_map);
101 const SUNMatrix J,
void*
const user_data,
102 const N_Vector tmp1,
const N_Vector tmp2) {
106 Eigen::VectorXd x_eigen(
107 Eigen::Map<Eigen::VectorXd>(NV_DATA_S(x), explicit_system->
N_));
108 Eigen::Map<Eigen::VectorXd> f_eval_map(N_VGetArrayPointer(f_eval),
109 explicit_system->
N_);
111 auto f_wrt_x = [&](
const auto& x) {
113 [&](
const auto&... args) {
114 return explicit_system->
f_(x, explicit_system->
msgs_, args...);
119 Eigen::MatrixXd Jf_x;
122 jacobian(f_wrt_x, x_eigen, f_x, Jf_x);
126 for (
int j = 0; j < Jf_x.cols(); j++)
127 for (
int i = 0; i < Jf_x.rows(); i++)
128 SM_ELEMENT_D(J, i, j) = Jf_x(i, j);
static int kinsol_jacobian(const N_Vector x, const N_Vector f_eval, const SUNMatrix J, void *const user_data, const N_Vector tmp1, const N_Vector tmp2)
Implements the function of type CVDlsJacFn which is the user-defined callbacks for KINSOL to calculat...
std::ostream *const msgs_
kinsol_system_data(const F1 &f, const Eigen::VectorXd &x, std::ostream *const msgs, const Args &... args)
sundials::Context sundials_context_
kinsol_system_data< F1, Args... > system_data
static int kinsol_f_system(const N_Vector x, const N_Vector f_eval, void *const user_data)
const Eigen::VectorXd & x_
const std::tuple< const Args &... > args_tuple_
KINSOL algebraic system data holder.
auto to_array_1d(T_x &&x)
Returns input matrix reshaped into a vector.
int64_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
void jacobian(const F &f, const Eigen::Matrix< T, Eigen::Dynamic, 1 > &x, Eigen::Matrix< T, Eigen::Dynamic, 1 > &fx, Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &J)
void check_matching_sizes(const char *function, const char *name1, const T_y1 &y1, const char *name2, const T_y2 &y2)
Check if two structures at the same size.
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 ...