3#include <alpaqa/casadi-loader-export.h>
11#if ALPAQA_WITH_EXTERNAL_CASADI
12#include <casadi/core/function.hpp>
13#include <casadi/mem.h>
20namespace casadi_loader {
23 : std::invalid_argument {
24 using std::invalid_argument::invalid_argument;
29template <Config Conf,
size_t N_in,
size_t N_out>
33 static_assert(std::is_same_v<real_t, casadi_real>);
39#if ALPAQA_WITH_EXTERNAL_CASADI
51 const std::array<casadi_dim, N_in> &
dim_in,
52 const std::array<casadi_dim, N_out> &
dim_out)
59 using namespace std::literals::string_literals;
62 "Invalid number of input arguments: got "s +
63 std::to_string(
fun.
n_in()) +
", should be " +
64 std::to_string(
N_in) +
".");
67 "Invalid number of output arguments: got "s +
68 std::to_string(
fun.
n_out()) +
", should be " +
69 std::to_string(
N_out) +
".");
75 const std::array<casadi_dim, N_in> &
dim_in = {},
76 const std::array<casadi_dim, N_out> &
dim_out = {}) {
77 using namespace std::literals::string_literals;
78 static constexpr std::array count{
"first",
"second",
"third",
79 "fourth",
"fifth",
"sixth",
81 static_assert(
N_in <= count.size());
82 static_assert(
N_out <= count.size());
84 return "(" + std::to_string(d.first) +
", " +
85 std::to_string(d.second) +
")";
87 for (
size_t n = 0; n <
N_in; ++n) {
90 throw invalid_argument_dimensions(
91 "Invalid dimension of "s + count[n] +
95 for (
size_t n = 0; n <
N_out; ++n) {
98 throw invalid_argument_dimensions(
99 "Invalid dimension of "s + count[n] +
108 const std::array<casadi_dim, N_out> &
dim_out = {}) {
112#if ALPAQA_WITH_EXTERNAL_CASADI
114 void operator()(
const double *
const *
in,
double *
const *out)
const {
122 double *
const (&out)[
N_out])
const {
128 double *
const (&out)[
N_out]) {
129 fun(std::span{
in}, std::span{out});
136#if ALPAQA_WITH_EXTERNAL_CASADI
138 mutable std::vector<casadi_int>
iwork;
139 mutable std::vector<double>
dwork;
140 mutable std::vector<const double *>
arg_work;
141 mutable std::vector<double *>
res_work;
#define BEGIN_ALPAQA_CASADI_LOADER_NAMESPACE
#define END_ALPAQA_CASADI_LOADER_NAMESPACE
Class that loads and calls pre-compiled CasADi functions in a DLL/SO file.
std::pair< casadi_int, casadi_int > size_in(casadi_int) const
std::pair< casadi_int, casadi_int > size_out(casadi_int) const
Class for evaluating CasADi functions, allocating the necessary workspace storage in advance for allo...
CasADiFunctionEvaluator(casadi::Function &&f, const std::array< casadi_dim, N_in > &dim_in, const std::array< casadi_dim, N_out > &dim_out)
static void validate_num_args(const casadi::Function &fun)
void operator()(const double *const (&in)[N_in], double *const (&out)[N_out])
CasADiFunctionEvaluator(casadi::Function &&f)
void validate_dimensions(const std::array< casadi_dim, N_in > &dim_in={}, const std::array< casadi_dim, N_out > &dim_out={})
static void validate_dimensions(const casadi::Function &fun, const std::array< casadi_dim, N_in > &dim_in={}, const std::array< casadi_dim, N_out > &dim_out={})
std::pair< casadi_int, casadi_int > casadi_dim
#define USING_ALPAQA_CONFIG(Conf)