Skip to content

Commit 31b5624

Browse files
authored
ModelicaSystem getSolution (#316)
* [ModelicaSystem] cleanup getSolutions() * [ModelicaSystem] improve getSolutions() * convert str input data to list such that both cases use the same code * [ModelicaSystem.getSolution] rename variables
1 parent d585207 commit 31b5624

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

OMPython/ModelicaSystem.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -950,39 +950,39 @@ def getSolutions(self, varList=None, resultfile=None): # 12
950950
>>> getSolutions(["Name1","Name2"],resultfile=""c:/a.mat"")
951951
"""
952952
if resultfile is None:
953-
resFile = self.resultfile.as_posix()
953+
result_file = self.resultfile
954954
else:
955-
resFile = resultfile
955+
result_file = pathlib.Path(resultfile)
956956

957957
# check for result file exits
958-
if not os.path.exists(resFile):
959-
raise ModelicaSystemError(f"Result file does not exist {resFile}")
960-
resultVars = self.sendExpression(f'readSimulationResultVars("{resFile}")')
958+
if not result_file.is_file():
959+
raise ModelicaSystemError(f"Result file does not exist {result_file}")
960+
961+
# get absolute path
962+
result_file = result_file.absolute()
963+
964+
result_vars = self.sendExpression(f'readSimulationResultVars("{result_file.as_posix()}")')
961965
self.sendExpression("closeSimulationResultFile()")
962966
if varList is None:
963-
return resultVars
967+
return result_vars
964968

965969
if isinstance(varList, str):
966-
if varList not in resultVars and varList != "time":
967-
raise ModelicaSystemError(f"Requested data {repr(varList)} does not exist")
968-
res = self.sendExpression(f'readSimulationResult("{resFile}", {{{varList}}})')
969-
npRes = np.array(res)
970-
self.sendExpression("closeSimulationResultFile()")
971-
return npRes
972-
973-
if isinstance(varList, list):
974-
for var in varList:
975-
if var == "time":
976-
continue
977-
if var not in resultVars:
978-
raise ModelicaSystemError(f"Requested data {repr(var)} does not exist")
979-
variables = ",".join(varList)
980-
res = self.sendExpression(f'readSimulationResult("{resFile}",{{{variables}}})')
981-
npRes = np.array(res)
982-
self.sendExpression("closeSimulationResultFile()")
983-
return npRes
984-
985-
raise ModelicaSystemError("Unhandled input for getSolutions()")
970+
var_list_checked = [varList]
971+
elif isinstance(varList, list):
972+
var_list_checked = varList
973+
else:
974+
raise ModelicaSystemError("Unhandled input for getSolutions()")
975+
976+
for var in var_list_checked:
977+
if var == "time":
978+
continue
979+
if var not in result_vars:
980+
raise ModelicaSystemError(f"Requested data {repr(var)} does not exist")
981+
variables = ",".join(var_list_checked)
982+
res = self.sendExpression(f'readSimulationResult("{result_file.as_posix()}",{{{variables}}})')
983+
np_res = np.array(res)
984+
self.sendExpression("closeSimulationResultFile()")
985+
return np_res
986986

987987
@staticmethod
988988
def _strip_space(name):

0 commit comments

Comments
 (0)