Skip to content

Commit 4666014

Browse files
committed
Fix getSolutions() when returning simulation variable list
1 parent a41c298 commit 4666014

1 file changed

Lines changed: 25 additions & 28 deletions

File tree

‎OMPython/__init__.py‎

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,23 +1159,27 @@ def getSolutions(self, *varList):#12
11591159
This method returns tuple of numpy arrays. It can be called:
11601160
•with a list of quantities name in string format as argument: it returns the simulation results of the corresponding names in the same order. Here it supports Python unpacking depending upon the number of variables assigned.
11611161
"""
1162-
if len(varList) == 0:
1163-
validSolution = ['time'] + self.__getInputNames() + self.__getContinuousNames() + self.__getParameterNames()
1164-
return validSolution
1165-
1166-
#if isinstance(varList, tuple) and all(len(a)==1 for a in varList):
1167-
elif isinstance(varList, tuple) and all(isinstance(a, str) for a in varList):
1168-
for v in varList:
1169-
if v == 'time':
1170-
continue
1171-
if v not in [l.name for l in self.quantitiesList]:
1172-
print ('!!! ', v, ' does not exist\n')
1173-
return
1174-
res_mat = '_res.mat'
1175-
resFile = "".join([self.modelName, res_mat])
1176-
check_resFile_ = os.path.exists(resFile)
1177-
variables = ",".join(varList)
1178-
if(check_resFile_):
1162+
## check for result file exits
1163+
res_mat = '_res.mat'
1164+
resFile = "".join([self.modelName, res_mat])
1165+
if (not os.path.exists(resFile)):
1166+
print ("Error: Result file does not exist")
1167+
exit()
1168+
else:
1169+
if len(varList) == 0:
1170+
#validSolution = ['time'] + self.__getInputNames() + self.__getContinuousNames() + self.__getParameterNames()
1171+
validSolution = self.getconn.sendExpression("readSimulationResultVars(\"" +resFile+ "\")")
1172+
return validSolution
1173+
1174+
#if isinstance(varList, tuple) and all(len(a)==1 for a in varList):
1175+
elif isinstance(varList, tuple) and all(isinstance(a, str) for a in varList):
1176+
for v in varList:
1177+
if v == 'time':
1178+
continue
1179+
if v not in [l.name for l in self.quantitiesList]:
1180+
print ('!!! ', v, ' does not exist\n')
1181+
return
1182+
variables = ",".join(varList)
11791183
exp = "readSimulationResult(\"" + resFile + '",{' + variables + "})"
11801184
res = self.getconn.sendExpression(exp)
11811185
npRes = np.array(res)
@@ -1187,23 +1191,16 @@ def getSolutions(self, *varList):#12
11871191
else:
11881192
tup = tuple(npRes)
11891193
return tup
1190-
else:
1191-
print ("Error: mat file does not exist")
1192-
elif isinstance(varList, tuple) and len(varList) == 1:
1193-
varList, = varList
1194-
res_mat = '_res.mat'
1195-
resFile = "".join([self.modelName, res_mat])
1196-
check_resFile_ = os.path.exists(resFile)
1197-
variables = ",".join(varList)
1198-
if(check_resFile_):
1194+
1195+
elif isinstance(varList, tuple) and len(varList) == 1:
1196+
varList, = varList
1197+
variables = ",".join(varList)
11991198
exp = "readSimulationResult(\"" + resFile + '",{' + variables + "})"
12001199
res = self.getconn.sendExpression(exp)
12011200
npRes = np.array(res)
12021201
exp2 = "closeSimulationResultFile()"
12031202
self.getconn.sendExpression(exp2)
12041203
return npRes
1205-
else:
1206-
print ('Error! should be tuple of Model variables')
12071204

12081205
#to set continuous quantities values
12091206
def setContinuous(self, **cvals):#13

0 commit comments

Comments
 (0)