Skip to content

Commit

Permalink
Merge pull request #363 from alphaville/fix/362-multiple-solvers
Browse files Browse the repository at this point in the history
Issue with multiple solvers
  • Loading branch information
alphaville authored Nov 7, 2024
2 parents d302884 + 1eb10b8 commit 841c913
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
6 changes: 6 additions & 0 deletions open-codegen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
Note: This is the Changelog file of `opengen` - the Python interface of OpEn


## [0.9.2] - 2024-11-05

### Fixed

- In CasADi-generated C files some functions are made static to avoid clashes when creating multiple solvers


## [0.9.1] - 2024-10-14
Expand Down Expand Up @@ -214,6 +219,7 @@ Note: This is the Changelog file of `opengen` - the Python interface of OpEn
* Fixed `lbfgs` typo


[0.9.2]: https://github.com/alphaville/optimization-engine/compare/opengen-0.9.1...opengen-0.9.2
[0.9.1]: https://github.com/alphaville/optimization-engine/compare/opengen-0.9.0...opengen-0.9.1
[0.9.0]: https://github.com/alphaville/optimization-engine/compare/opengen-0.8.1...opengen-0.9.0
[0.8.1]: https://github.com/alphaville/optimization-engine/compare/v0.9.0...opengen-0.8.1
Expand Down
2 changes: 1 addition & 1 deletion open-codegen/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.9.1
0.9.2
35 changes: 33 additions & 2 deletions open-codegen/opengen/builder/optimizer_builder.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import subprocess
import shutil
import datetime
import yaml
import warnings

import opengen.config as og_cfg
import opengen.definitions as og_dfn
Expand Down Expand Up @@ -809,6 +807,38 @@ def __info(self):
}
return info

def __casadi_make_static(self):
"""Makes some casadi functions static to avoid clashes (see #362)
"""
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
fncs_list = ["casadi_sq", "casadi_fmax",
"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,
_AUTOGEN_PRECONDITIONING_FNAME]:
replace_in_casadi_file(os.path.join(
icasadi_extern_dir, casadi_fname), fncs_list)

def build(self):
"""Generate code and build project
Expand Down Expand Up @@ -837,6 +867,7 @@ def build(self):
self.__generate_main_project_code()
self.__generate_build_rs() # generate build.rs file
self.__generate_yaml_data_file() # create YAML file with metadata
self.__casadi_make_static() # make casadi functions static

if not self.__generate_not_build:
self.__logger.info("Building optimizer")
Expand Down

0 comments on commit 841c913

Please sign in to comment.