Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 21 additions & 27 deletions OMPython/ModelicaSystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,15 @@ def get_session(self) -> OMCSession:
"""
return self._session

def get_model_name(self) -> str:
"""
Return the defined model name.
"""
if not isinstance(self._model_name, str):
raise ModelicaSystemError("No model name defined!")

return self._model_name

def set_command_line_options(self, command_line_option: str):
"""
Set the provided command line option via OMC setCommandLineOptions().
Expand Down Expand Up @@ -1931,9 +1940,13 @@ def run_doe():
resdir = mypath / 'DoE'
resdir.mkdir(exist_ok=True)

doe_mod = OMPython.ModelicaSystemDoE(
mod = OMPython.ModelicaSystem()
mod.model(
model_name="M",
model_file=model.as_posix(),
)
doe_mod = OMPython.ModelicaSystemDoE(
mod=mod,
parameters=param,
resultpath=resdir,
simargs={"override": {'stopTime': 1.0}},
Expand All @@ -1960,15 +1973,8 @@ def run_doe():

def __init__(
self,
# data to be used for ModelicaSystem
model_file: Optional[str | os.PathLike] = None,
model_name: Optional[str] = None,
libraries: Optional[list[str | tuple[str, str]]] = None,
command_line_options: Optional[list[str]] = None,
variable_filter: Optional[str] = None,
work_directory: Optional[str | os.PathLike] = None,
omhome: Optional[str] = None,
session: Optional[OMCSession] = None,
# ModelicaSystem definition to use
mod: ModelicaSystem,
# simulation specific input
# TODO: add more settings (simulation options, input options, ...)
simargs: Optional[dict[str, Optional[str | dict[str, str] | numbers.Number]]] = None,
Expand All @@ -1981,30 +1987,18 @@ def __init__(
ModelicaSystem.simulate(). Additionally, the path to store the result files is needed (= resultpath) as well as
a list of parameters to vary for the Doe (= parameters). All possible combinations are considered.
"""
if model_name is None:
raise ModelicaSystemError("No model name provided!")

self._mod = ModelicaSystem(
command_line_options=command_line_options,
work_directory=work_directory,
omhome=omhome,
session=session,
)
self._mod.model(
model_file=model_file,
model_name=model_name,
libraries=libraries,
variable_filter=variable_filter,
)
if not isinstance(mod, ModelicaSystem):
raise ModelicaSystemError("Missing definition of ModelicaSystem!")

self._model_name = model_name
self._mod = mod
self._model_name = mod.get_model_name()

self._simargs = simargs

if resultpath is None:
self._resultpath = self.get_session().omcpath_tempdir()
else:
self._resultpath = self.get_session().omcpath(resultpath)
self._resultpath = self.get_session().omcpath(resultpath).resolve()
if not self._resultpath.is_dir():
raise ModelicaSystemError("Argument resultpath must be set to a valid path within the environment used "
f"for the OpenModelica session: {resultpath}!")
Expand Down
37 changes: 27 additions & 10 deletions tests/test_ModelicaSystemDoE.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,17 @@ def test_ModelicaSystemDoE_local(tmp_path, model_doe, param_doe):
tmpdir = tmp_path / 'DoE'
tmpdir.mkdir(exist_ok=True)

doe_mod = OMPython.ModelicaSystemDoE(
mod = OMPython.ModelicaSystem()
mod.model(
model_file=model_doe,
model_name="M",
)

doe_mod = OMPython.ModelicaSystemDoE(
mod=mod,
parameters=param_doe,
resultpath=tmpdir,
simargs={"override": {'stopTime': 1.0}},
simargs={"override": {'stopTime': '1.0'}},
)

_run_ModelicaSystemDoe(doe_mod=doe_mod)
Expand All @@ -72,12 +77,18 @@ def test_ModelicaSystemDoE_docker(tmp_path, model_doe, param_doe):
omcs = OMPython.OMCSessionDocker(docker="openmodelica/openmodelica:v1.25.0-minimal")
assert omcs.sendExpression("getVersion()") == "OpenModelica 1.25.0"

doe_mod = OMPython.ModelicaSystemDoE(
mod = OMPython.ModelicaSystem(
session=omcs,
)
mod.model(
model_file=model_doe,
model_name="M",
)

doe_mod = OMPython.ModelicaSystemDoE(
mod=mod,
parameters=param_doe,
session=omcs,
simargs={"override": {'stopTime': 1.0}},
simargs={"override": {'stopTime': '1.0'}},
)

_run_ModelicaSystemDoe(doe_mod=doe_mod)
Expand All @@ -86,15 +97,21 @@ def test_ModelicaSystemDoE_docker(tmp_path, model_doe, param_doe):
@pytest.mark.skip(reason="Not able to run WSL on github")
@skip_python_older_312
def test_ModelicaSystemDoE_WSL(tmp_path, model_doe, param_doe):
tmpdir = tmp_path / 'DoE'
tmpdir.mkdir(exist_ok=True)
omcs = OMPython.OMCSessionWSL()
assert omcs.sendExpression("getVersion()") == "OpenModelica 1.25.0"

doe_mod = OMPython.ModelicaSystemDoE(
mod = OMPython.ModelicaSystem(
session=omcs,
)
mod.model(
model_file=model_doe,
model_name="M",
)

doe_mod = OMPython.ModelicaSystemDoE(
mod=mod,
parameters=param_doe,
resultpath=tmpdir,
simargs={"override": {'stopTime': 1.0}},
simargs={"override": {'stopTime': '1.0'}},
)

_run_ModelicaSystemDoe(doe_mod=doe_mod)
Expand Down