diff --git a/OMPython/modelica_system_omc.py b/OMPython/modelica_system_omc.py index 8ee5c3b0..d7c4bf1a 100644 --- a/OMPython/modelica_system_omc.py +++ b/OMPython/modelica_system_omc.py @@ -39,13 +39,15 @@ def __init__( work_directory: Optional[str | os.PathLike] = None, omhome: Optional[str] = None, session: Optional[OMSessionABC] = None, + use_default_command_line_options: bool = True, ) -> None: """Create a ModelicaSystem instance. To define the model use model() or convertFmu2Mo(). Args: command_line_options: List with extra command line options as elements. The list elements are - provided to omc via setCommandLineOptions(). If set, the default values will be overridden. - To disable any command line options, use an empty list. + provided to omc via setCommandLineOptions() in addition to OMPython's default options. + use_default_command_line_options: Whether to set OMPython's default options for linearization. + Set to False to use only command_line_options, or no options if it is unspecified or empty. work_directory: Path to a directory to be used for temporary files like the model executable. If left unspecified, a tmp directory will be created. @@ -62,15 +64,17 @@ def __init__( work_directory=work_directory, ) - # set commandLineOptions using default values or the user defined list - if command_line_options is None: + options: list[str] = [] + if use_default_command_line_options: # set default command line options to improve the performance of linearization and to avoid recompilation if # the simulation executable is reused in linearize() via the runtime flag '-l' - command_line_options = [ + options.extend([ "--linearizationDumpLanguage=python", "--generateSymbolicLinearization", - ] - for opt in command_line_options: + ]) + if command_line_options is not None: + options.extend(command_line_options) + for opt in options: self.set_command_line_options(command_line_option=opt) def model( diff --git a/tests/test_FMIImport.py b/tests/test_FMIImport.py index bb3a1201..fec9e60b 100644 --- a/tests/test_FMIImport.py +++ b/tests/test_FMIImport.py @@ -34,8 +34,10 @@ def test_FMIImport(model_firstorder): assert os.path.exists(fmu) # import FMU & check & simulate - # TODO: why is '--allowNonStandardModelica=reinitInAlgorithms' needed? any example without this possible? - mod2 = OMPython.ModelicaSystemOMC(command_line_options=['--allowNonStandardModelica=reinitInAlgorithms']) + # --allowNonStandardModelica=reinitInAlgorithms' is needed. + # Since the generated code uses reinit inside algorithm section which is not standard Modelica. + mod2 = OMPython.ModelicaSystemOMC(command_line_options=['--allowNonStandardModelica=reinitInAlgorithms'], + use_default_command_line_options=False) mo = mod2.convertFmu2Mo(fmu=fmu) assert os.path.exists(mo) diff --git a/tests/test_ModelicaSystemOMC.py b/tests/test_ModelicaSystemOMC.py index 171db6b9..4a7f8cb6 100644 --- a/tests/test_ModelicaSystemOMC.py +++ b/tests/test_ModelicaSystemOMC.py @@ -38,6 +38,29 @@ def model_firstorder(tmp_path, model_firstorder_content): return mod +@pytest.mark.parametrize( + ("use_defaults", "custom_options", "expected_options"), + [ + (True, ["--showAnnotations"], [ + "--linearizationDumpLanguage=python", + "--generateSymbolicLinearization=true", + "--showAnnotations=true", + ]), + (False, ["--showAnnotations"], ["--showAnnotations=true"]), + ], +) +def test_command_line_options(use_defaults, custom_options, expected_options): + session = OMPython.OMCSessionLocal() + mod = OMPython.ModelicaSystemOMC( + session=session, + command_line_options=custom_options, + use_default_command_line_options=use_defaults, + ) + + current_options = mod.sendExpression("getCommandLineOptions()") + assert all(option in current_options for option in expected_options) + + def test_ModelicaSystem_loop(model_firstorder): def worker(): mod = OMPython.ModelicaSystemOMC()