From 884b3fe475a67812cb85c0c056adef2af0b80c68 Mon Sep 17 00:00:00 2001 From: Pantelis Sopasakis Date: Mon, 4 Nov 2024 14:47:47 +0000 Subject: [PATCH 1/4] making files static --- .../opengen/builder/optimizer_builder.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/open-codegen/opengen/builder/optimizer_builder.py b/open-codegen/opengen/builder/optimizer_builder.py index 55c96f64..84e73059 100644 --- a/open-codegen/opengen/builder/optimizer_builder.py +++ b/open-codegen/opengen/builder/optimizer_builder.py @@ -13,6 +13,7 @@ import logging import pkg_resources import sys +import fileinput from .ros_builder import RosBuilder @@ -809,6 +810,25 @@ 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): + with fileinput.FileInput(casadi_source_fname, inplace=True, backup='.bak') as file: + for line in file: + for fnc in function_names: + text_to_search = f"casadi_real {fnc}" + replacement_text = f"static casadi_real {fnc}" + line = line.replace(text_to_search, replacement_text) + print(line, end='') + + icasadi_extern_dir = os.path.join( + self.__icasadi_target_dir(), "extern") + replace_in_casadi_file(os.path.join( + icasadi_extern_dir, _AUTOGEN_COST_FNAME), ["casadi_sq", "casadi_fmax", "casadi_fmin"]) + def build(self): """Generate code and build project @@ -837,6 +857,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") From cc94aed14579c2161403fffa5ae0ad9694577d1b Mon Sep 17 00:00:00 2001 From: Pantelis Sopasakis Date: Mon, 4 Nov 2024 19:59:31 +0000 Subject: [PATCH 2/4] make casadi function names static --- .../opengen/builder/optimizer_builder.py | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/open-codegen/opengen/builder/optimizer_builder.py b/open-codegen/opengen/builder/optimizer_builder.py index 84e73059..4a14c01a 100644 --- a/open-codegen/opengen/builder/optimizer_builder.py +++ b/open-codegen/opengen/builder/optimizer_builder.py @@ -812,22 +812,32 @@ 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): - with fileinput.FileInput(casadi_source_fname, inplace=True, backup='.bak') as file: - for line in file: + # Read casadi_source_fname, line by line, replace, write to destination + 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: - text_to_search = f"casadi_real {fnc}" - replacement_text = f"static casadi_real {fnc}" - line = line.replace(text_to_search, replacement_text) - print(line, end='') + line_in = line_in.replace( + f"casadi_real {fnc}", f"static casadi_real {fnc}") + fout.write(line_in) + shutil.move(f"{casadi_source_fname}.tmp", f"{casadi_source_fname}") icasadi_extern_dir = os.path.join( - self.__icasadi_target_dir(), "extern") - replace_in_casadi_file(os.path.join( - icasadi_extern_dir, _AUTOGEN_COST_FNAME), ["casadi_sq", "casadi_fmax", "casadi_fmin"]) + self.__icasadi_target_dir(), "extern") # casadi extern folder + # function to make static + fncs_list = ["casadi_sq", "casadi_fmax", + "casadi_fmin", "casadi_hypot", "casadi_sign"] + # 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 From 3a5fa5902be3b2e66b41b24a374cf090d25b0bb5 Mon Sep 17 00:00:00 2001 From: Pantelis Sopasakis Date: Tue, 5 Nov 2024 15:33:25 +0000 Subject: [PATCH 3/4] feat(optimizer_builder): make casadi functions static 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 --- open-codegen/opengen/builder/optimizer_builder.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/open-codegen/opengen/builder/optimizer_builder.py b/open-codegen/opengen/builder/optimizer_builder.py index 4a14c01a..5f7e8a3d 100644 --- a/open-codegen/opengen/builder/optimizer_builder.py +++ b/open-codegen/opengen/builder/optimizer_builder.py @@ -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, From 1eb10b89f4292ccdab7fcb7e0ac3efbace254d78 Mon Sep 17 00:00:00 2001 From: Pantelis Sopasakis Date: Tue, 5 Nov 2024 15:44:51 +0000 Subject: [PATCH 4/4] fix(362): remove unnecessary imports Remove unnecessary imports (fileinput, warning, datetime) and update CHANGELOG and VERSION (opengen) --- open-codegen/CHANGELOG.md | 6 ++++++ open-codegen/VERSION | 2 +- open-codegen/opengen/builder/optimizer_builder.py | 3 --- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/open-codegen/CHANGELOG.md b/open-codegen/CHANGELOG.md index 5f960b32..31aecf7e 100644 --- a/open-codegen/CHANGELOG.md +++ b/open-codegen/CHANGELOG.md @@ -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 @@ -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 diff --git a/open-codegen/VERSION b/open-codegen/VERSION index f514a2f0..f76f9131 100644 --- a/open-codegen/VERSION +++ b/open-codegen/VERSION @@ -1 +1 @@ -0.9.1 \ No newline at end of file +0.9.2 \ No newline at end of file diff --git a/open-codegen/opengen/builder/optimizer_builder.py b/open-codegen/opengen/builder/optimizer_builder.py index 5f7e8a3d..8a43c30d 100644 --- a/open-codegen/opengen/builder/optimizer_builder.py +++ b/open-codegen/opengen/builder/optimizer_builder.py @@ -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 @@ -13,7 +11,6 @@ import logging import pkg_resources import sys -import fileinput from .ros_builder import RosBuilder