Skip to content

Commit 841c913

Browse files
authored
Merge pull request #363 from alphaville/fix/362-multiple-solvers
Issue with multiple solvers
2 parents d302884 + 1eb10b8 commit 841c913

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

open-codegen/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88
Note: This is the Changelog file of `opengen` - the Python interface of OpEn
99

1010

11+
## [0.9.2] - 2024-11-05
12+
13+
### Fixed
14+
15+
- In CasADi-generated C files some functions are made static to avoid clashes when creating multiple solvers
1116

1217

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

216221

222+
[0.9.2]: https://github.com/alphaville/optimization-engine/compare/opengen-0.9.1...opengen-0.9.2
217223
[0.9.1]: https://github.com/alphaville/optimization-engine/compare/opengen-0.9.0...opengen-0.9.1
218224
[0.9.0]: https://github.com/alphaville/optimization-engine/compare/opengen-0.8.1...opengen-0.9.0
219225
[0.8.1]: https://github.com/alphaville/optimization-engine/compare/v0.9.0...opengen-0.8.1

open-codegen/VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.9.1
1+
0.9.2

open-codegen/opengen/builder/optimizer_builder.py

+33-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import subprocess
22
import shutil
3-
import datetime
43
import yaml
5-
import warnings
64

75
import opengen.config as og_cfg
86
import opengen.definitions as og_dfn
@@ -809,6 +807,38 @@ def __info(self):
809807
}
810808
return info
811809

810+
def __casadi_make_static(self):
811+
"""Makes some casadi functions static to avoid clashes (see #362)
812+
"""
813+
self.__logger.info("Making CasADi functions static")
814+
815+
def replace_in_casadi_file(casadi_source_fname, function_names):
816+
# Read casadi_source_fname, line by line, replace, write to destination
817+
# Open the source file in read mode
818+
# Replace and write to a different file (with extension .tmp)
819+
with open(casadi_source_fname, 'r') as fin, open(f"{casadi_source_fname}.tmp", 'w') as fout:
820+
for line_in in fin:
821+
for fnc in function_names:
822+
line_in = line_in.replace(
823+
f"casadi_real {fnc}", f"static casadi_real {fnc}")
824+
fout.write(line_in)
825+
# Move the .tmp file to replace the original one
826+
shutil.move(f"{casadi_source_fname}.tmp", f"{casadi_source_fname}")
827+
828+
# Folder with external CasADi files (auto-generated C code)
829+
icasadi_extern_dir = os.path.join(
830+
self.__icasadi_target_dir(), "extern") # casadi extern folder
831+
# Function to make static
832+
fncs_list = ["casadi_sq", "casadi_fmax",
833+
"casadi_fmin", "casadi_hypot", "casadi_sign",
834+
"casadi_log1p", "casadi_expm1"]
835+
# make static
836+
for casadi_fname in [_AUTOGEN_COST_FNAME, _AUTOGEN_ALM_MAPPING_F1_FNAME,
837+
_AUTOGEN_GRAD_FNAME, _AUTOGEN_PNLT_CONSTRAINTS_FNAME,
838+
_AUTOGEN_PRECONDITIONING_FNAME]:
839+
replace_in_casadi_file(os.path.join(
840+
icasadi_extern_dir, casadi_fname), fncs_list)
841+
812842
def build(self):
813843
"""Generate code and build project
814844
@@ -837,6 +867,7 @@ def build(self):
837867
self.__generate_main_project_code()
838868
self.__generate_build_rs() # generate build.rs file
839869
self.__generate_yaml_data_file() # create YAML file with metadata
870+
self.__casadi_make_static() # make casadi functions static
840871

841872
if not self.__generate_not_build:
842873
self.__logger.info("Building optimizer")

0 commit comments

Comments
 (0)