Skip to content

Commit

Permalink
feat(optimizer_builder): make casadi functions static
Browse files Browse the repository at this point in the history
About:
According to code_generator.cpp (Casadi), the functions to
be made static are casadi_sq, casadi_fmax,
casadi_fmin, casadi_hypot, casadi_sign,
casadi_log1p, and casadi_expm1; all done
  • Loading branch information
alphaville committed Nov 5, 2024
1 parent cc94aed commit 3a5fa59
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions open-codegen/opengen/builder/optimizer_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,26 +812,29 @@ def __info(self):

def __casadi_make_static(self):
"""Makes some casadi functions static to avoid clashes (see #362)
Experimental - it needs to be tested
"""
self.__logger.info("Making CasADi functions static")

def replace_in_casadi_file(casadi_source_fname, function_names):
# Read casadi_source_fname, line by line, replace, write to destination
# Open the source file in read mode
# Replace and write to a different file (with extension .tmp)
with open(casadi_source_fname, 'r') as fin, open(f"{casadi_source_fname}.tmp", 'w') as fout:
for line_in in fin:
for fnc in function_names:
line_in = line_in.replace(
f"casadi_real {fnc}", f"static casadi_real {fnc}")
fout.write(line_in)
# Move the .tmp file to replace the original one
shutil.move(f"{casadi_source_fname}.tmp", f"{casadi_source_fname}")

# Folder with external CasADi files (auto-generated C code)
icasadi_extern_dir = os.path.join(
self.__icasadi_target_dir(), "extern") # casadi extern folder
# function to make static
# Function to make static
fncs_list = ["casadi_sq", "casadi_fmax",
"casadi_fmin", "casadi_hypot", "casadi_sign"]
"casadi_fmin", "casadi_hypot", "casadi_sign",
"casadi_log1p", "casadi_expm1"]
# make static
for casadi_fname in [_AUTOGEN_COST_FNAME, _AUTOGEN_ALM_MAPPING_F1_FNAME,
_AUTOGEN_GRAD_FNAME, _AUTOGEN_PNLT_CONSTRAINTS_FNAME,
Expand Down

0 comments on commit 3a5fa59

Please sign in to comment.