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
28 changes: 17 additions & 11 deletions OMPython/ModelicaSystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,22 +383,21 @@ def __init__(
self._linearized_outputs: list[str] = [] # linearization output list
self._linearized_states: list[str] = [] # linearization states list

self._session = session

# get OpenModelica version
version_str = self._session.get_version()
self._version = self._parse_om_version(version=version_str)

self._simulated = False # True if the model has already been simulated
self._result_file: Optional[OMPathABC] = None # for storing result file

self._work_dir: OMPathABC = self.setWorkDirectory(work_directory)

self._model_name: Optional[str] = None
self._libraries: Optional[list[str | tuple[str, str]]] = None
self._file_name: Optional[OMPathABC] = None
self._variable_filter: Optional[str] = None

self._session = session
# get OpenModelica version
version_str = self._session.get_version()
self._version = self._parse_om_version(version=version_str)

self._work_dir: OMPathABC = self.setWorkDirectory(work_directory)

def get_session(self) -> OMSessionABC:
"""
Return the OMC session used for this class.
Expand Down Expand Up @@ -468,6 +467,8 @@ def _xmlparse(self, xml_file: OMPathABC):
xml_content = xml_file.read_text()
tree = ET.ElementTree(ET.fromstring(xml_content))
root = tree.getroot()
if root is None:
raise ModelicaSystemError(f"Cannot read XML file: {xml_file}")
for attr in root.iter('DefaultExperiment'):
for key in ("startTime", "stopTime", "stepSize", "tolerance",
"solver", "outputFormat"):
Expand Down Expand Up @@ -1935,7 +1936,7 @@ def getSolutions(
self,
varList: Optional[str | list[str]] = None,
resultfile: Optional[str | os.PathLike] = None,
) -> tuple[str] | np.ndarray:
) -> tuple[str, ...] | np.ndarray:
"""Extract simulation results from a result data file.

Args:
Expand Down Expand Up @@ -1984,7 +1985,8 @@ def getSolutions(
result_vars = self.sendExpression(expr=f'readSimulationResultVars("{result_file.as_posix()}")')
self.sendExpression(expr="closeSimulationResultFile()")
if varList is None:
return result_vars
var_list = [str(var) for var in result_vars]
return tuple(var_list)

if isinstance(varList, str):
var_list_checked = [varList]
Expand Down Expand Up @@ -2064,6 +2066,8 @@ def convertFmu2Mo(
raise ModelicaSystemError(f"Missing FMU file: {fmu_path.as_posix()}")

filename = self._requestApi(apiName='importFMU', entity=fmu_path.as_posix())
if not isinstance(filename, str):
raise ModelicaSystemError(f"Invalid return value for the FMU filename: {filename}")
filepath = self.getWorkDirectory() / filename

# report proper error message
Expand Down Expand Up @@ -2106,7 +2110,9 @@ def optimize(self) -> dict[str, Any]:
"""
properties = ','.join(f"{key}={val}" for key, val in self._optimization_options.items())
self.set_command_line_options("-g=Optimica")
return self._requestApi(apiName='optimize', entity=self._model_name, properties=properties)
retval = self._requestApi(apiName='optimize', entity=self._model_name, properties=properties)
retval = cast(dict, retval)
return retval


class ModelicaSystem(ModelicaSystemOMC):
Expand Down
Loading
Loading