Skip to content
Merged
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
78 changes: 43 additions & 35 deletions OMPython/ModelicaSystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,19 +395,11 @@ def xmlparse(self):
scalar["changeable"] = sv.get('isValueChangeable')
scalar["aliasvariable"] = sv.get('aliasVariable')
ch = list(sv)
start = None
min = None
max = None
unit = None
for att in ch:
start = att.get('start')
min = att.get('min')
max = att.get('max')
unit = att.get('unit')
scalar["start"] = start
scalar["min"] = min
scalar["max"] = max
scalar["unit"] = unit
scalar["start"] = att.get('start')
scalar["min"] = att.get('min')
scalar["max"] = att.get('max')
scalar["unit"] = att.get('unit')

if scalar["variability"] == "parameter":
if scalar["name"] in self.overridevariables:
Expand Down Expand Up @@ -438,6 +430,8 @@ def getQuantities(self, names=None): # 3
elif isinstance(names, list):
return [x for y in names for x in self.quantitiesList if x["name"] == y]

raise ModelicaSystemError("Unhandled input for getQuantities()")

def getContinuous(self, names=None): # 4
"""
This method returns dict. The key is continuous names and value is corresponding continuous value.
Expand Down Expand Up @@ -482,6 +476,8 @@ def getContinuous(self, names=None): # 4
raise ModelicaSystemError(f"OM error: {i} is not continuous")
return valuelist

raise ModelicaSystemError("Unhandled input for getContinous()")

def getParameters(self, names: Optional[str | list[str]] = None) -> dict[str, str] | list[str]: # 5
"""Get parameter values.

Expand Down Expand Up @@ -509,7 +505,9 @@ def getParameters(self, names: Optional[str | list[str]] = None) -> dict[str, st
elif isinstance(names, str):
return [self.paramlist.get(names, "NotExist")]
elif isinstance(names, list):
return ([self.paramlist.get(x, "NotExist") for x in names])
return [self.paramlist.get(x, "NotExist") for x in names]

raise ModelicaSystemError("Unhandled input for getParameters()")

def getInputs(self, names: Optional[str | list[str]] = None) -> dict | list: # 6
"""Get input values.
Expand Down Expand Up @@ -543,7 +541,9 @@ def getInputs(self, names: Optional[str | list[str]] = None) -> dict | list: #
elif isinstance(names, str):
return [self.inputlist.get(names, "NotExist")]
elif isinstance(names, list):
return ([self.inputlist.get(x, "NotExist") for x in names])
return [self.inputlist.get(x, "NotExist") for x in names]

raise ModelicaSystemError("Unhandled input for getInputs()")

def getOutputs(self, names: Optional[str | list[str]] = None): # 7
"""Get output values.
Expand Down Expand Up @@ -588,7 +588,7 @@ def getOutputs(self, names: Optional[str | list[str]] = None): # 7
elif isinstance(names, str):
return [self.outputlist.get(names, "NotExist")]
else:
return ([self.outputlist.get(x, "NotExist") for x in names])
return [self.outputlist.get(x, "NotExist") for x in names]
else:
if names is None:
for i in self.outputlist:
Expand All @@ -601,7 +601,7 @@ def getOutputs(self, names: Optional[str | list[str]] = None): # 7
self.outputlist[names] = value[0][-1]
return [self.outputlist.get(names)]
else:
return (names, " is not Output")
return names, " is not Output"
elif isinstance(names, list):
valuelist = []
for i in names:
Expand All @@ -610,9 +610,11 @@ def getOutputs(self, names: Optional[str | list[str]] = None): # 7
self.outputlist[i] = value[0][-1]
valuelist.append(value[0][-1])
else:
return (i, "is not Output")
return i, "is not Output"
return valuelist

raise ModelicaSystemError("Unhandled input for getOutputs()")

def getSimulationOptions(self, names=None): # 8
"""
This method returns dict. The key is simulation option names and value is corresponding simulation option value.
Expand All @@ -627,7 +629,9 @@ def getSimulationOptions(self, names=None): # 8
elif isinstance(names, str):
return [self.simulateOptions.get(names, "NotExist")]
elif isinstance(names, list):
return ([self.simulateOptions.get(x, "NotExist") for x in names])
return [self.simulateOptions.get(x, "NotExist") for x in names]

raise ModelicaSystemError("Unhandled input for getSimulationOptions()")

def getLinearizationOptions(self, names=None): # 9
"""
Expand All @@ -643,7 +647,9 @@ def getLinearizationOptions(self, names=None): # 9
elif isinstance(names, str):
return [self.linearOptions.get(names, "NotExist")]
elif isinstance(names, list):
return ([self.linearOptions.get(x, "NotExist") for x in names])
return [self.linearOptions.get(x, "NotExist") for x in names]

raise ModelicaSystemError("Unhandled input for getLinearizationOptions()")

def getOptimizationOptions(self, names=None): # 10
"""
Expand All @@ -657,7 +663,9 @@ def getOptimizationOptions(self, names=None): # 10
elif isinstance(names, str):
return [self.optimizeOptions.get(names, "NotExist")]
elif isinstance(names, list):
return ([self.optimizeOptions.get(x, "NotExist") for x in names])
return [self.optimizeOptions.get(x, "NotExist") for x in names]

raise ModelicaSystemError("Unhandled input for getOptimizationOptions()")

def get_exe_file(self) -> pathlib.Path:
"""Get path to model executable."""
Expand Down Expand Up @@ -752,41 +760,40 @@ def getSolutions(self, varList=None, resultfile=None): # 12

# check for result file exits
if not os.path.exists(resFile):
errstr = f"Error: Result file does not exist {resFile}"
self._raise_error(errstr=errstr)
return
raise ModelicaSystemError(f"Result file does not exist {resFile}")
resultVars = self.sendExpression(f'readSimulationResultVars("{resFile}")')
self.sendExpression("closeSimulationResultFile()")
if varList is None:
return resultVars
elif isinstance(varList, str):
if varList not in resultVars and varList != "time":
self._raise_error(errstr=f'!!! {varList} does not exist')
return
raise ModelicaSystemError(f"Requested data {repr(varList)} does not exist")
res = self.sendExpression(f'readSimulationResult("{resFile}", {{{varList}}})')
npRes = np.array(res)
self.sendExpression("closeSimulationResultFile()")
return npRes
elif isinstance(varList, list):
# varList, = varList
for v in varList:
if v == "time":
for var in varList:
if var == "time":
continue
if v not in resultVars:
self._raise_error(errstr=f'!!! {v} does not exist')
return
if var not in resultVars:
raise ModelicaSystemError(f"Requested data {repr(var)} does not exist")
variables = ",".join(varList)
res = self.sendExpression(f'readSimulationResult("{resFile}",{{{variables}}})')
npRes = np.array(res)
self.sendExpression("closeSimulationResultFile()")
return npRes

raise ModelicaSystemError("Unhandled input for getSolutions()")

def strip_space(self, name):
if isinstance(name, str):
return name.replace(" ", "")
elif isinstance(name, list):
return [x.replace(" ", "") for x in name]

raise ModelicaSystemError("Unhandled input for strip_space()")

def setMethodHelper(self, args1, args2, args3, args4=None):
"""
Helper function for setParameter(),setContinuous(),setSimulationOptions(),setLinearizationOption(),setOptimizationOption()
Expand All @@ -811,7 +818,8 @@ def apply_single(args1):
return True

else:
self._raise_error(errstr=f'"{value[0]}" is not a {args3} variable')
raise ModelicaSystemError("Unhandled case in setMethodHelper.apply_single() - "
f"{repr(value[0])} is not a {repr(args3)} variable")

result = []
if isinstance(args1, str):
Expand Down Expand Up @@ -847,7 +855,7 @@ def setParameters(self, pvals): # 14

def isParameterChangeable(self, name, value):
q = self.getQuantities(name)
if (q[0]["changeable"] == "false"):
if q[0]["changeable"] == "false":
if self._verbose:
logger.info("setParameters() failed : It is not possible to set "
f'the following signal "{name}", It seems to be structural, final, '
Expand Down Expand Up @@ -916,10 +924,10 @@ def setInputs(self, name): # 15
value = var.split("=")
if value[0] in self.inputlist:
tmpvalue = eval(value[1])
if (isinstance(tmpvalue, int) or isinstance(tmpvalue, float)):
if isinstance(tmpvalue, int) or isinstance(tmpvalue, float):
self.inputlist[value[0]] = [(float(self.simulateOptions["startTime"]), float(value[1])),
(float(self.simulateOptions["stopTime"]), float(value[1]))]
elif (isinstance(tmpvalue, list)):
elif isinstance(tmpvalue, list):
self.checkValidInputs(tmpvalue)
self.inputlist[value[0]] = tmpvalue
self.inputFlag = True
Expand Down
Loading