icasadi_test/
lib.rs

1//! # CasADi Rust interface
2//!
3//! This is a Rust interface to C functions generated by Rust
4//!
5//! This is a `no-std` library (however, mind that the CasADi-generated code
6//! requires `libm` to call functions such as `sqrt`, `sin`, etc...)
7
8#![no_std]
9
10// Get the CasADi problem size and parameter size and re-export them, this is necessary to have
11// them available as compile time constants
12include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
13
14/// Number of static parameters
15pub use CASADI_NP as NUM_STATIC_PARAMETERS;
16
17/// Number of decision variables
18pub use CASADI_NU as NUM_DECISION_VARIABLES;
19
20use libc::{c_double, c_int};
21
22extern "C" {
23    fn icasadi_cost_(
24        u: *const c_double,
25        casadi_static_params: *const c_double,
26        cost_value: *mut c_double,
27    ) -> c_int;
28
29    fn icasadi_grad_(
30        u: *const c_double,
31        casadi_static_params: *const c_double,
32        cost_jacobian: *mut c_double,
33    ) -> c_int;
34
35    fn icasadi_constraints_as_penalty_(
36        u: *const c_double,
37        casadi_static_params: *const c_double,
38        constraints_as_penalty: *mut c_double,
39    ) -> c_int;
40
41}
42
43///
44/// Consume the cost function written in C
45///
46/// # Example
47/// ```
48/// fn tst_call_casadi_cost() {
49///     let u = [1.0, 2.0, 3.0, -5.0, 1.0, 10.0, 14.0, 17.0, 3.0, 5.0];
50///     let p = [1.0, -1.0];
51///     let mut cost_value = 0.0;
52///     icasadi::icasadi_cost(&u, &p, &mut cost_value);
53/// }
54/// ```
55///
56/// # Panics
57/// This method does not panic (on purpose). However, users need to be
58/// careful when providing the arguments `u` and `casadi_static_params`
59/// as they must be arrays of appropriate size.
60///
61/// As a safety measure, you may check whether
62///
63/// - `u.len() >= NUM_DECISION_VARIABLES`
64/// - `casadi_static_params.len() >= NUM_STATIC_PARAMETERS`
65///
66pub fn icasadi_cost(u: &[f64], casadi_static_params: &[f64], cost_value: &mut f64) -> c_int {
67    unsafe { icasadi_cost_(u.as_ptr(), casadi_static_params.as_ptr(), cost_value) }
68}
69
70///
71/// Consume the Jacobian function written in C
72///
73/// # Example
74/// ```
75/// fn tst_call_casadi_cost() {
76///     let u = [1.0, 2.0, 3.0, -5.0, 1.0, 10.0, 14.0, 17.0, 3.0, 5.0];
77///     let p = [1.0, -1.0];
78///     let mut jac = [0.0; 10];
79///     icasadi::icasadi_grad(&u, &p, &mut jac);
80/// }
81/// ```
82///
83/// # Panics
84/// This method does not panic (on purpose). However, users need to be
85/// careful when providing the arguments `u` and `casadi_static_params`
86/// as they must be arrays of appropriate size.
87///
88/// As a safety measure, you may check whether
89///
90/// - `u.len() >= icasadi::num_decision_variables()`
91/// - `casadi_static_params.len() >= icasadi::num_static_parameters()`
92/// - `cost_jacobian.len() >= icasadi::num_decision_variables()`
93///
94pub fn icasadi_grad(u: &[f64], casadi_static_params: &[f64], cost_jacobian: &mut [f64]) -> c_int {
95    unsafe {
96        icasadi_grad_(
97            u.as_ptr(),
98            casadi_static_params.as_ptr(),
99            cost_jacobian.as_mut_ptr(),
100        )
101    }
102}
103
104pub fn icasadi_constraints_as_penalty(u: &[f64], casadi_static_params: &[f64], constraints_as_penalty: &mut [f64]) -> c_int {
105    unsafe {
106        icasadi_constraints_as_penalty_(
107            u.as_ptr(),
108            casadi_static_params.as_ptr(),
109            constraints_as_penalty.as_mut_ptr(),
110        )
111    }
112}
113
114#[cfg(test)]
115mod tests {
116    use super::*;
117
118    #[test]
119    fn tst_num_static() {
120        let _np = NUM_STATIC_PARAMETERS;
121    }
122
123    #[test]
124    fn tst_num_decision_var() {
125        let _nu = NUM_DECISION_VARIABLES;
126    }
127
128}