25 #include "clarabel_interface.hpp"
26 #include "casadi/core/nlp_tools.hpp"
27 #include <clarabel_runtime_str.h>
33 int CASADI_CONIC_CLARABEL_EXPORT
36 plugin->name =
"clarabel";
38 plugin->version = CASADI_VERSION;
51 const std::map<std::string, Sparsity>& st)
59 "Options to be passed to Clarabel."}},
67 for (
auto&& op : opts) {
68 if (op.first==
"clarabel") {
76 casadi_warning(
"This plugin is not implemented yet. "
77 "It's only a stub to check the generation of binaries linked to rust packages.");
95 rowp_.resize(
H_.
nnz());
97 rowa_.resize(
A_.
nnz());
106 std::string n = name +
"[]";
107 g.
local(n,
"static const int");
108 std::stringstream init;
110 for (casadi_int i = 0; i < v.size(); ++i) {
112 if (i < v.size()-1) init <<
", ";
114 if (v.empty()) init <<
"0";
120 g <<
"p.qp = &p_qp;\n";
125 g <<
"p.colindp = colindp;\n";
126 g <<
"p.rowp = rowp;\n";
127 g <<
"p.colinda = colinda;\n";
128 g <<
"p.rowa = rowa;\n";
129 g <<
"casadi_clarabel_setup(&p);\n";
139 casadi_clarabel_setup(&p_);
143 g <<
"clarabel_init_mem(&" +
codegen_mem(g) +
");\n";
148 g <<
"clarabel_free_mem(&" +
codegen_mem(g) +
");\n";
155 clarabel_init_mem(&m->d);
156 m->add_stat(
"preprocessing");
157 m->add_stat(
"solver");
158 m->add_stat(
"postprocessing");
164 clarabel_free_mem(&m->d);
169 casadi_int*& iw,
double*& w)
const {
174 casadi_clarabel_init(&m->d, &arg, &res, &iw, &w);
178 casadi_int* iw,
double* w,
void* mem)
const {
180 m->
fstats.at(
"solver").tic();
181 casadi_clarabel_solve(&m->d, arg, res, iw, w);
182 m->fstats.at(
"solver").toc();
207 g.
local(
"d",
"struct casadi_clarabel_data*");
209 g.
local(
"p",
"struct casadi_clarabel_prob");
212 g <<
"d->prob = &p;\n";
213 g <<
"d->qp = &d_qp;\n";
214 g <<
"casadi_clarabel_init(d, &arg, &res, &iw, &w);\n";
216 g <<
"casadi_clarabel_solve(d, arg, res, iw, w);\n";
217 g <<
"if (!d_qp.success) {\n";
219 g <<
"return -1000;\n";
236 s.
version(
"ClarabelInterface", 1);
244 s.
version(
"ClarabelInterface", 1);
245 s.
pack(
"ClarabelInterface::opts",
opts_);
void codegen_free_mem(CodeGenerator &g) const override
Codegen for freeing memory.
void init_dependent()
Initialize dependent (read-only) members.
int solve(const double **arg, double **res, casadi_int *iw, double *w, void *mem) const override
Solve the QP.
static ProtoFunction * deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
Dict opts_
Options passed to Clarabel (distinct from options in the solver)
static Conic * creator(const std::string &name, const std::map< std::string, Sparsity > &st)
Creator function for the plugin.
void free_mem(void *mem) const override
Free the memory block.
ClarabelInterface(const std::string &name, const std::map< std::string, Sparsity > &st)
Constructor using sparsity patterns.
void set_work(void *mem, const double **&arg, double **&res, casadi_int *&iw, double *&w) const override
Set the (persistent) work vectors.
void codegen_init_mem(CodeGenerator &g) const override
Codegen for initializing memory.
static const Options options_
Options for Clarabel.
int init_mem(void *mem) const override
Initialize the memory block.
void init(const Dict &opts) override
Initialize the solver.
Dict get_stats(void *mem) const override
Get all statistics.
static const std::string meta_doc
A documentation string.
~ClarabelInterface() override
Destructor.
void codegen_body(CodeGenerator &g) const override
Generate code for the function body.
Helper class for C code generation.
void local(const std::string &name, const std::string &type, const std::string &ref="")
Declare a local variable.
void init_local(const std::string &name, const std::string &def)
Specify the default value for a local variable.
std::string sanitize_source(const std::string &src, const std::vector< std::string > &inst, bool add_shorthand=true)
Sanitize source files for codegen.
void add_include(const std::string &new_include, bool relative_path=false, const std::string &use_ifdef=std::string())
Add an include file optionally using a relative path "..." instead of an absolute path <....
std::stringstream auxiliaries
void add_auxiliary(Auxiliary f, const std::vector< std::string > &inst={"casadi_real"})
Add a built-in auxiliary function.
static const Options options_
Options.
int init_mem(void *mem) const override
Initalize memory block.
Sparsity H_
Problem structure.
void init(const Dict &opts) override
Initialize.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
void set_work(void *mem, const double **&arg, double **&res, casadi_int *&iw, double *&w) const override
Set the (persistent) work vectors.
Dict get_stats(void *mem) const override
Get all statistics.
void qp_codegen_body(CodeGenerator &g) const
Generate code for the function body.
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
void version(const std::string &name, int v)
void alloc_iw(size_t sz_iw, bool persistent=false)
Ensure required length of iw field.
void alloc_res(size_t sz_res, bool persistent=false)
Ensure required length of res field.
void alloc_arg(size_t sz_arg, bool persistent=false)
Ensure required length of arg field.
std::string codegen_mem(CodeGenerator &g, const std::string &index="mem") const
Get thread-local memory object.
size_t sz_res() const
Get required length of res field.
size_t sz_w() const
Get required length of w field.
void alloc_w(size_t sz_w, bool persistent=false)
Ensure required length of w field.
size_t sz_arg() const
Get required length of arg field.
size_t sz_iw() const
Get required length of iw field.
static void registerPlugin(const Plugin &plugin, bool needs_lock=true)
Register an integrator in the factory.
bool error_on_fail_
Throw an exception on failure?
void clear_mem()
Clear all memory (called from destructor)
Helper class for Serialization.
void version(const std::string &name, int v)
void pack(const Sparsity &e)
Serializes an object to the output stream.
casadi_int nnz() const
Get the number of (structural) non-zeros.
casadi_int size2() const
Get the number of columns.
const casadi_int * row() const
Get a reference to row-vector,.
const casadi_int * colind() const
Get a reference to the colindex of all column element (see class description)
int CASADI_CONIC_CLARABEL_EXPORT casadi_register_conic_clarabel(Conic::Plugin *plugin)
void copy_vector(const std::vector< S > &s, std::vector< D > &d)
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
T * get_ptr(std::vector< T > &v)
Get a pointer to the data contained in the vector.
void codegen_local(CodeGenerator &g, const std::string &name, const std::vector< int > &v)
void CASADI_CONIC_CLARABEL_EXPORT casadi_load_conic_clarabel()
casadi_clarabel_data< double > d
Options metadata for a class.
std::map< std::string, FStats > fstats
const casadi_qp_prob< T1 > * qp