alpaqa develop
Nonconvex constrained optimization
Loading...
Searching...
No Matches
CasADiFunctionWrapper.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <alpaqa/casadi-loader-export.h>
6
7#include <stdexcept>
8#include <string>
9#include <vector>
10
11#if ALPAQA_WITH_EXTERNAL_CASADI
12#include <casadi/core/function.hpp>
13#include <casadi/mem.h>
14#else
16#endif
17
18namespace alpaqa {
20namespace casadi_loader {
21
23 : std::invalid_argument {
24 using std::invalid_argument::invalid_argument;
25};
26
27/// Class for evaluating CasADi functions, allocating the necessary workspace
28/// storage in advance for allocation-free evaluations.
29template <Config Conf, size_t N_in, size_t N_out>
31 public:
33 static_assert(std::is_same_v<real_t, casadi_real>);
34
35 using casadi_dim = std::pair<casadi_int, casadi_int>;
36
37 /// @throws invalid_argument_dimensions
39#if ALPAQA_WITH_EXTERNAL_CASADI
40 : fun(std::move(f)), iwork(fun.sz_iw()), dwork(fun.sz_w()),
41 arg_work(fun.sz_arg()), res_work(fun.sz_res())
42#else
43 : fun(std::move(f))
44#endif
45 {
47 }
48
49 /// @throws invalid_argument_dimensions
51 const std::array<casadi_dim, N_in> &dim_in,
52 const std::array<casadi_dim, N_out> &dim_out)
53 : CasADiFunctionEvaluator{std::move(f)} {
55 }
56
57 /// @throws invalid_argument_dimensions
59 using namespace std::literals::string_literals;
60 if (N_in != fun.n_in())
62 "Invalid number of input arguments: got "s +
63 std::to_string(fun.n_in()) + ", should be " +
64 std::to_string(N_in) + ".");
65 if (N_out != fun.n_out())
67 "Invalid number of output arguments: got "s +
68 std::to_string(fun.n_out()) + ", should be " +
69 std::to_string(N_out) + ".");
70 }
71
72 /// @throws invalid_argument_dimensions
73 static void
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",
80 "seventh", "eighth"};
81 static_assert(N_in <= count.size());
82 static_assert(N_out <= count.size());
83 auto to_string = [](casadi_dim d) {
84 return "(" + std::to_string(d.first) + ", " +
85 std::to_string(d.second) + ")";
86 };
87 for (size_t n = 0; n < N_in; ++n) {
88 auto cs_n = static_cast<casadi_int>(n);
89 if (dim_in[n].first != 0 && dim_in[n] != fun.size_in(cs_n))
90 throw invalid_argument_dimensions(
91 "Invalid dimension of "s + count[n] +
92 " input argument: got " + to_string(fun.size_in(cs_n)) +
93 ", should be " + to_string(dim_in[n]) + ".");
94 }
95 for (size_t n = 0; n < N_out; ++n) {
96 auto cs_n = static_cast<casadi_int>(n);
97 if (dim_out[n].first != 0 && dim_out[n] != fun.size_out(cs_n))
98 throw invalid_argument_dimensions(
99 "Invalid dimension of "s + count[n] +
100 " output argument: got " + to_string(fun.size_out(cs_n)) +
101 ", should be " + to_string(dim_out[n]) + ".");
102 }
103 }
104
105 /// @throws invalid_argument_dimensions
106 void
107 validate_dimensions(const std::array<casadi_dim, N_in> &dim_in = {},
108 const std::array<casadi_dim, N_out> &dim_out = {}) {
110 }
111
112#if ALPAQA_WITH_EXTERNAL_CASADI
113 protected:
114 void operator()(const double *const *in, double *const *out) const {
115 std::copy_n(in, N_in, arg_work.begin());
116 std::copy_n(out, N_out, res_work.begin());
117 fun(arg_work.data(), res_work.data(), iwork.data(), dwork.data(), 0);
118 }
119
120 public:
121 void operator()(const double *const (&in)[N_in],
122 double *const (&out)[N_out]) const {
123 this->operator()(&in[0], &out[0]);
124 }
125#else
126 public:
127 void operator()(const double *const (&in)[N_in],
128 double *const (&out)[N_out]) {
129 fun(std::span{in}, std::span{out});
130 }
131#endif
132
133 public:
135
136#if ALPAQA_WITH_EXTERNAL_CASADI
137 private:
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;
142#endif
143};
144
145} // namespace casadi_loader
147} // namespace alpaqa
#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])
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={})
#define USING_ALPAQA_CONFIG(Conf)
Definition config.hpp:77
constexpr const auto inf
Definition config.hpp:112
long long int casadi_int