@@ -825,6 +825,7 @@ def __init__(self, fileName=None, modelName=None, lmodel=[], useCorba=False, com
825825 self .linearinputs = [] # linearization input list
826826 self .linearoutputs = [] # linearization output list
827827 self .linearstates = [] # linearization states list
828+ self .tempdir = ""
828829
829830 if useCorba :
830831 self .getconn = OMCSession ()
@@ -852,51 +853,18 @@ def __init__(self, fileName=None, modelName=None, lmodel=[], useCorba=False, com
852853 print ("File Error:" + os .path .abspath (self .fileName ) + " does not exist!!!" )
853854 return
854855
855- (head , tail ) = os .path .split (self .fileName ) # to store directory/path and file)
856- self .currDir = os .getcwd ()
857- self .modelDir = head
858- self .fileName_ = tail
859-
860- if not self .modelDir :
861- file_ = os .path .exists (self .fileName_ )
862- if (file_ ): # execution from path where file is located
863- self .__loadingModel ()
864- else :
865- print ("Error: File does not exist!!!" )
866-
867- else :
868- os .chdir (self .modelDir )
869- file_ = os .path .exists (self .fileName_ )
870- self .model = self .fileName_ [:- 3 ]
871- if (self .fileName_ ): # execution from different path
872- os .chdir (self .currDir )
873- self .__loadingModel ()
874- else :
875- print ("Error: File does not exist!!!" )
856+ self .loadingModel ()
876857
877858 def __del__ (self ):
878- if self .getconn is not None :
879- self .requestApi ('quit' )
859+ OMCSessionBase .__del__ (self )
880860
881861 # for loading file/package, loading model and building model
882- def __loadingModel (self ):
862+ def loadingModel (self ):
883863 # load file
884- loadfileError = ''
885- loadfileResult = self .requestApi ("loadFile" , self .fileName )
886- loadfileError = self .requestApi ("getErrorString" )
887-
888- # print the notification to users
889- if (loadfileResult == True and loadfileError ):
890- print (loadfileError )
891-
892- if (loadfileResult == False ):
893- specError = 'Parser error: Unexpected token near: optimization (IDENT)'
894- if specError in loadfileError :
895- self .requestApi ("setCommandLineOptions" , '"+g=Optimica"' )
896- self .requestApi ("loadFile" , self .fileName )
897- else :
898- print ('loadFile Error: ' + loadfileError )
899- return
864+ loadFileExp = "" .join (["loadFile(" ,"\" " ,self .fileName ,"\" " ,")" ]).replace ("\\ " ,"/" )
865+ loadMsg = self .getconn .sendExpression (loadFileExp )
866+ if not loadMsg :
867+ return print (self .getconn .sendExpression ("getErrorString()" ))
900868
901869 # load Modelica standard libraries or Modelica files if needed
902870 for element in self .lmodel :
@@ -920,6 +888,15 @@ def __loadingModel(self):
920888 print ("| info | loadLibrary() failed, Unknown type detected: " , element , " is of type " , type (element ), ", The following patterns are supported\n 1)[\" Modelica\" ]\n 2)[(\" Modelica\" ,\" 3.2.3\" ), \" PowerSystems\" ]\n " )
921889 if loadmodelError :
922890 print (loadmodelError )
891+
892+ # create a unique temp directory for each session and build the model in that directory
893+ self .tempdir = tempfile .mkdtemp ()
894+ if not os .path .exists (self .tempdir ):
895+ return print (self .tempdir , " cannot be created" )
896+
897+ exp = "" .join (["cd(" ,"\" " ,self .tempdir ,"\" " ,")" ]).replace ("\\ " ,"/" )
898+ self .getconn .sendExpression (exp )
899+
923900 self .buildModel ()
924901
925902 def buildModel (self , variableFilter = None ):
@@ -1229,7 +1206,7 @@ def simulate(self,resultfile=None,simflags=None): # 11
12291206 """
12301207 if (resultfile is None ):
12311208 r = ""
1232- self .resultfile = "" . join ([ self .modelName , "_res.mat" ] )
1209+ self .resultfile = os . path . join (self .tempdir , self . modelName + "_res.mat" ). replace ( " \\ " , "/" )
12331210 else :
12341211 r = " -r=" + resultfile
12351212 self .resultfile = resultfile
@@ -1238,7 +1215,7 @@ def simulate(self,resultfile=None,simflags=None): # 11
12381215 if (simflags is None ):
12391216 simflags = ""
12401217 else :
1241- simflags = " " + simflags ;
1218+ simflags = " " + simflags
12421219
12431220 if (self .overridevariables or self .simoptionsoverride ):
12441221 tmpdict = self .overridevariables .copy ()
@@ -1269,13 +1246,14 @@ def simulate(self,resultfile=None,simflags=None): # 11
12691246 csvinput = ""
12701247
12711248 if (platform .system () == "Windows" ):
1272- getExeFile = os .path .join (os . getcwd () , '{}.{}' .format (self .modelName , "exe" )).replace ("\\ " , "/" )
1249+ getExeFile = os .path .join (self . tempdir , '{}.{}' .format (self .modelName , "exe" )).replace ("\\ " , "/" )
12731250 else :
1274- getExeFile = os .path .join (os . getcwd () , self .modelName ).replace ("\\ " , "/" )
1275-
1251+ getExeFile = os .path .join (self . tempdir , self .modelName ).replace ("\\ " , "/" )
1252+ currentDir = os . getcwd ()
12761253 if (os .path .exists (getExeFile )):
12771254 cmd = getExeFile + override + csvinput + r + simflags
12781255 #print(cmd)
1256+ os .chdir (self .tempdir )
12791257 if (platform .system () == "Windows" ):
12801258 omhome = os .path .join (os .environ .get ("OPENMODELICAHOME" ))
12811259 dllPath = os .path .join (omhome , "bin" ).replace ("\\ " , "/" ) + os .pathsep + os .path .join (omhome , "lib/omc" ).replace ("\\ " , "/" ) + os .pathsep + os .path .join (omhome , "lib/omc/cpp" ).replace ("\\ " , "/" ) + os .pathsep + os .path .join (omhome , "lib/omc/omsicpp" ).replace ("\\ " , "/" )
@@ -1286,11 +1264,10 @@ def simulate(self,resultfile=None,simflags=None): # 11
12861264 p .terminate ()
12871265 else :
12881266 os .system (cmd )
1267+ os .chdir (currentDir )
12891268 self .simulationFlag = True
1290-
12911269 else :
1292- raise Exception ("Error: application file not generated yet" )
1293-
1270+ raise Exception ("Error: Application file path not found: " + getExeFile )
12941271
12951272 # to extract simulation results
12961273 def getSolutions (self , varList = None , resultfile = None ): # 12
@@ -1312,7 +1289,7 @@ def getSolutions(self, varList=None, resultfile=None): # 12
13121289
13131290 # check for result file exits
13141291 if (not os .path .exists (resFile )):
1315- print ("Error: Result file does not exist" )
1292+ print ("Error: Result file does not exist " + resFile )
13161293 return
13171294 #exit()
13181295 else :
0 commit comments