diff --git a/open-codegen/opengen/templates/casadi_memory.h.template b/open-codegen/opengen/templates/casadi_memory.h.template index 6693c156..17ed8cf6 100644 --- a/open-codegen/opengen/templates/casadi_memory.h.template +++ b/open-codegen/opengen/templates/casadi_memory.h.template @@ -22,31 +22,31 @@ /* * Cost sizes */ -#define COST_SZ_ARG {{ cost.sz_arg() }} -#define COST_SZ_IW {{ cost.sz_iw() }} -#define COST_SZ_W {{ cost.sz_w() }} -#define COST_SZ_RES {{ cost.sz_res() }} +#define COST_SZ_ARG_{{ meta.optimizer_name | upper}} {{ cost.sz_arg() }} +#define COST_SZ_IW_{{ meta.optimizer_name | upper}} {{ cost.sz_iw() }} +#define COST_SZ_W_{{ meta.optimizer_name | upper}} {{ cost.sz_w() }} +#define COST_SZ_RES_{{ meta.optimizer_name | upper}} {{ cost.sz_res() }} /* * Gradient sizes */ -#define GRAD_SZ_ARG {{ grad.sz_arg() }} -#define GRAD_SZ_IW {{ grad.sz_iw() }} -#define GRAD_SZ_W {{ grad.sz_w() }} -#define GRAD_SZ_RES {{ grad.sz_res() }} +#define GRAD_SZ_ARG_{{ meta.optimizer_name | upper}} {{ grad.sz_arg() }} +#define GRAD_SZ_IW_{{ meta.optimizer_name | upper}} {{ grad.sz_iw() }} +#define GRAD_SZ_W_{{ meta.optimizer_name | upper}} {{ grad.sz_w() }} +#define GRAD_SZ_RES_{{ meta.optimizer_name | upper}} {{ grad.sz_res() }} /* * F1 sizes */ -#define F1_SZ_ARG {{ f1.sz_arg() }} -#define F1_SZ_IW {{ f1.sz_iw() }} -#define F1_SZ_W {{ f1.sz_w() }} -#define F1_SZ_RES {{ f1.sz_res() }} +#define F1_SZ_ARG_{{ meta.optimizer_name | upper}} {{ f1.sz_arg() }} +#define F1_SZ_IW_{{ meta.optimizer_name | upper}} {{ f1.sz_iw() }} +#define F1_SZ_W_{{ meta.optimizer_name | upper}} {{ f1.sz_w() }} +#define F1_SZ_RES_{{ meta.optimizer_name | upper}} {{ f1.sz_res() }} /* * F2 sizes */ -#define F2_SZ_ARG {{ f2.sz_arg() }} -#define F2_SZ_IW {{ f2.sz_iw() }} -#define F2_SZ_W {{ f2.sz_w() }} -#define F2_SZ_RES {{ f2.sz_res() }} \ No newline at end of file +#define F2_SZ_ARG_{{ meta.optimizer_name | upper}} {{ f2.sz_arg() }} +#define F2_SZ_IW_{{ meta.optimizer_name | upper}} {{ f2.sz_iw() }} +#define F2_SZ_W_{{ meta.optimizer_name | upper}} {{ f2.sz_w() }} +#define F2_SZ_RES_{{ meta.optimizer_name | upper}} {{ f2.sz_res() }} \ No newline at end of file diff --git a/open-codegen/opengen/templates/icasadi_lib.rs.template b/open-codegen/opengen/templates/icasadi_lib.rs.template index 8e8a14ec..cd806264 100644 --- a/open-codegen/opengen/templates/icasadi_lib.rs.template +++ b/open-codegen/opengen/templates/icasadi_lib.rs.template @@ -26,20 +26,24 @@ const NUM_DECISION_VARIABLES: usize = {{ problem.dim_decision_variables() }}; const NUM_CONSTRAINTS_TYPE_ALM: usize = {{ problem.dim_constraints_aug_lagrangian() or 0 }}; /// Number of penalty constraints (dimension of F2, i.e., n2) -const NUM_CONSTAINTS_TYPE_PENALTY: usize = {{ problem.dim_constraints_penalty() or 0 }}; +const NUM_CONSTRAINTS_TYPE_PENALTY: usize = {{ problem.dim_constraints_penalty() or 0 }}; use libc::{c_double, c_int}; // might need to include: c_longlong, c_void /// C interface (Function API exactly as provided by CasADi) extern "C" { - fn cost_function(arg: *const *const c_double, casadi_results: *mut *mut c_double) -> c_int; - fn grad_cost_function(arg: *const *const c_double, casadi_results: *mut *mut c_double) + fn cost_function_{{ meta.optimizer_name }}( + arg: *const *const c_double, + casadi_results: *mut *mut c_double) -> c_int; + fn grad_cost_function_{{ meta.optimizer_name }}( + arg: *const *const c_double, + casadi_results: *mut *mut c_double) -> c_int; - fn mapping_f1_function( + fn mapping_f1_function_{{ meta.optimizer_name }}( arg: *const *const c_double, casadi_results: *mut *mut c_double, ) -> c_int; - fn mapping_f2_function( + fn mapping_f2_function_{{ meta.optimizer_name }}( arg: *const *const c_double, casadi_results: *mut *mut c_double, ) -> c_int; @@ -83,7 +87,7 @@ pub fn cost(u: &[f64], xi: &[f64], static_params: &[f64], cost_value: &mut f64) let cost = &mut [cost_value as *mut c_double]; unsafe { - cost_function( + cost_function_{{ meta.optimizer_name }}( arguments.as_ptr(), cost.as_mut_ptr(), ) as i32 @@ -128,7 +132,7 @@ pub fn grad(u: &[f64], xi: &[f64], static_params: &[f64], cost_jacobian: &mut [f let grad = &mut [cost_jacobian.as_mut_ptr()]; unsafe { - grad_cost_function( + grad_cost_function_{{ meta.optimizer_name }}( arguments.as_ptr(), grad.as_mut_ptr() ) as i32 @@ -174,7 +178,7 @@ pub fn mapping_f1( let constraints = &mut [f1.as_mut_ptr()]; unsafe { - mapping_f1_function( + mapping_f1_function_{{ meta.optimizer_name }}( arguments.as_ptr(), constraints.as_mut_ptr() ) as i32 @@ -210,7 +214,7 @@ pub fn mapping_f2( "Incompatible dimension of `p`" ); assert!( - f2.len() == NUM_CONSTAINTS_TYPE_PENALTY || NUM_CONSTAINTS_TYPE_PENALTY == 0, + f2.len() == NUM_CONSTRAINTS_TYPE_PENALTY || NUM_CONSTRAINTS_TYPE_PENALTY == 0, "Incompatible dimension of `f2` (result)" ); @@ -218,7 +222,7 @@ pub fn mapping_f2( let constraints = &mut [f2.as_mut_ptr()]; unsafe { - mapping_f2_function( + mapping_f2_function_{{ meta.optimizer_name }}( arguments.as_ptr(), constraints.as_mut_ptr() ) as i32 @@ -262,7 +266,7 @@ mod tests { fn tst_f2() { let u = [0.1; NUM_DECISION_VARIABLES]; let p = [0.1; NUM_STATIC_PARAMETERS]; - let mut f2up = [0.0; NUM_CONSTAINTS_TYPE_PENALTY]; + let mut f2up = [0.0; NUM_CONSTRAINTS_TYPE_PENALTY]; assert_eq!(0, super::mapping_f2(&u, &p, &mut f2up)); println!("F2(u, p) = {:#?}", f2up); } diff --git a/open-codegen/opengen/templates/interface.c.template b/open-codegen/opengen/templates/interface.c.template index 0e7bd612..2c46050e 100644 --- a/open-codegen/opengen/templates/interface.c.template +++ b/open-codegen/opengen/templates/interface.c.template @@ -28,23 +28,20 @@ #include #include "casadi_memory.h" -#define TRUE 1 -#define FALSE 0 - /* Number of input variables */ -#define NU {{ problem.dim_decision_variables() }} +#define NU_{{ meta.optimizer_name | upper}} {{ problem.dim_decision_variables() }} /* Number of static parameters */ -#define NP {{ problem.dim_parameters() + problem.dim_constraints_penalty() }} +#define NP_{{ meta.optimizer_name | upper}} {{ problem.dim_parameters() + problem.dim_constraints_penalty() }} /* Dimension of F1 (number of ALM constraints) */ -#define N1 {{ problem.dim_constraints_aug_lagrangian() }} +#define N1_{{ meta.optimizer_name | upper}} {{ problem.dim_constraints_aug_lagrangian() }} /* Dimension of F2 (number of PM constraints) */ -#define N2 {{ problem.dim_constraints_penalty() }} +#define N2_{{ meta.optimizer_name | upper}} {{ problem.dim_constraints_penalty() }} /* Dimension of xi = (c, y) */ -#define NXI {% if problem.dim_constraints_aug_lagrangian() + problem.dim_constraints_penalty() > 0 %}{{ 1 + problem.dim_constraints_aug_lagrangian() }}{% else %}0{% endif %} +#define NXI_{{ meta.optimizer_name | upper}} {% if problem.dim_constraints_aug_lagrangian() + problem.dim_constraints_penalty() > 0 %}{{ 1 + problem.dim_constraints_aug_lagrangian() }}{% else %}0{% endif %} #ifndef casadi_real #define casadi_real double @@ -103,26 +100,26 @@ extern int {{build_config.constraint_penalty_function_name}}( /* * Integer workspaces */ -#if COST_SZ_IW > 0 -static casadi_int allocated_i_workspace_cost[COST_SZ_IW]; /* cost (int ) */ +#if COST_SZ_IW_{{ meta.optimizer_name | upper}} > 0 +static casadi_int allocated_i_workspace_cost[COST_SZ_IW_{{ meta.optimizer_name | upper}}]; /* cost (int ) */ #else static casadi_int *allocated_i_workspace_cost = NULL; #endif -#if GRAD_SZ_IW > 0 -static casadi_int allocated_i_workspace_grad[GRAD_SZ_IW]; /* grad (int ) */ +#if GRAD_SZ_IW_{{ meta.optimizer_name | upper}} > 0 +static casadi_int allocated_i_workspace_grad[GRAD_SZ_IW_{{ meta.optimizer_name | upper}}]; /* grad (int ) */ #else static casadi_int *allocated_i_workspace_grad = NULL; #endif -#if F1_SZ_IW > 0 -static casadi_int allocated_i_workspace_f1[F1_SZ_IW]; /* f1 (int ) */ +#if F1_SZ_IW_{{ meta.optimizer_name | upper}} > 0 +static casadi_int allocated_i_workspace_f1[F1_SZ_IW_{{ meta.optimizer_name | upper}}]; /* f1 (int ) */ #else static casadi_int *allocated_i_workspace_f1 = NULL; #endif -#if F2_SZ_IW > 0 -static casadi_int allocated_i_workspace_f2[F2_SZ_IW]; /* f2 (int ) */ +#if F2_SZ_IW_{{ meta.optimizer_name | upper}} > 0 +static casadi_int allocated_i_workspace_f2[F2_SZ_IW_{{ meta.optimizer_name | upper}}]; /* f2 (int ) */ #else static casadi_int *allocated_i_workspace_f2 = NULL; #endif @@ -131,27 +128,27 @@ static casadi_int *allocated_i_workspace_f2 = NULL; /* * Real workspaces */ -#if COST_SZ_W > 0 -static casadi_real allocated_r_workspace_cost[COST_SZ_W]; /* cost (real) */ +#if COST_SZ_W_{{ meta.optimizer_name | upper}} > 0 +static casadi_real allocated_r_workspace_cost[COST_SZ_W_{{ meta.optimizer_name | upper}}]; /* cost (real) */ #else static casadi_real *allocated_r_workspace_cost = NULL; #endif -#if GRAD_SZ_W > 0 -static casadi_real allocated_r_workspace_grad[GRAD_SZ_W]; /* grad (real ) */ +#if GRAD_SZ_W_{{ meta.optimizer_name | upper}} > 0 +static casadi_real allocated_r_workspace_grad[GRAD_SZ_W_{{ meta.optimizer_name | upper}}]; /* grad (real ) */ #else static casadi_real *allocated_r_workspace_grad = NULL; #endif -#if F1_SZ_W > 0 -static casadi_real allocated_r_workspace_f1[F1_SZ_W]; /* f1 (real ) */ +#if F1_SZ_W_{{ meta.optimizer_name | upper}} > 0 +static casadi_real allocated_r_workspace_f1[F1_SZ_W_{{ meta.optimizer_name | upper}}]; /* f1 (real ) */ #else static casadi_real *allocated_r_workspace_f1 = NULL; #endif -#if F2_SZ_W > 0 -static casadi_real allocated_r_workspace_f2[F2_SZ_W]; /* f2 (real ) */ +#if F2_SZ_W_{{ meta.optimizer_name | upper}} > 0 +static casadi_real allocated_r_workspace_f2[F2_SZ_W_{{ meta.optimizer_name | upper}}]; /* f2 (real ) */ #else static casadi_real *allocated_r_workspace_f2 = NULL; #endif @@ -159,28 +156,28 @@ static casadi_real *allocated_r_workspace_f2 = NULL; /* * Result workspaces */ -#if COST_SZ_RES > 0 -static casadi_real *result_space_cost[COST_SZ_RES ]; /* cost (res ) */ +#if COST_SZ_RES_{{ meta.optimizer_name | upper}} > 0 +static casadi_real *result_space_cost[COST_SZ_RES_{{ meta.optimizer_name | upper}}]; /* cost (res ) */ #else static casadi_real **result_space_cost = NULL; #endif -#if GRAD_SZ_RES > 0 -static casadi_real *result_space_grad[GRAD_SZ_RES]; /* grad (res ) */ +#if GRAD_SZ_RES_{{ meta.optimizer_name | upper}} > 0 +static casadi_real *result_space_grad[GRAD_SZ_RES_{{ meta.optimizer_name | upper}}]; /* grad (res ) */ #else static casadi_real **result_space_grad = NULL; #endif -#if F1_SZ_RES > 0 -static casadi_real *result_space_f1[F1_SZ_RES]; /* f1 (res ) */ +#if F1_SZ_RES_{{ meta.optimizer_name | upper}} > 0 +static casadi_real *result_space_f1[F1_SZ_RES_{{ meta.optimizer_name | upper}}]; /* f1 (res ) */ #else static casadi_real **result_space_f1 = NULL; #endif -#if F2_SZ_RES > 0 -static casadi_real *result_space_f2[F2_SZ_RES]; /* f2 (res ) */ +#if F2_SZ_RES_{{ meta.optimizer_name | upper}} > 0 +static casadi_real *result_space_f2[F2_SZ_RES_{{ meta.optimizer_name | upper}}]; /* f2 (res ) */ #else static casadi_real **result_space_f2 = NULL; #endif @@ -198,7 +195,9 @@ static casadi_real **result_space_f2 = NULL; * | | * |------------------- uxip_space ------------------------------| */ -static casadi_real uxip_space[NU+NXI+NP]; +static casadi_real uxip_space[NU_{{ meta.optimizer_name | upper}} + +NXI_{{ meta.optimizer_name | upper}} + +NP_{{ meta.optimizer_name | upper}}]; /** * Copy (u, xi, p) into uxip_space @@ -208,9 +207,9 @@ static casadi_real uxip_space[NU+NXI+NP]; */ static void copy_args_into_uxip_space(const casadi_real** arg) { int i; - for (i=0; i