@@ -317,7 +317,7 @@ def __init__(
317317 lmodel : Optional [list [str | tuple [str , str ]]] = None ,
318318 commandLineOptions : Optional [str ] = None ,
319319 variableFilter : Optional [str ] = None ,
320- customBuildDirectory : Optional [str | os .PathLike ] = None ,
320+ customBuildDirectory : Optional [str | os .PathLike | pathlib . Path ] = None ,
321321 omhome : Optional [str ] = None ,
322322 session : Optional [OMCSessionZMQ ] = None
323323 ):
@@ -371,7 +371,6 @@ def __init__(
371371 self .linearinputs = [] # linearization input list
372372 self .linearoutputs = [] # linearization output list
373373 self .linearstates = [] # linearization states list
374- self .tempdir = ""
375374
376375 if session is not None :
377376 if not isinstance (session , OMCSessionZMQ ):
@@ -409,7 +408,7 @@ def __init__(
409408 self .setCommandLineOptions ("--linearizationDumpLanguage=python" )
410409 self .setCommandLineOptions ("--generateSymbolicLinearization" )
411410
412- self .setTempDirectory (customBuildDirectory )
411+ self .tempdir = self . setTempDirectory (customBuildDirectory )
413412
414413 if self .fileName is not None :
415414 self .loadLibrary (lmodel = self .lmodel )
@@ -456,22 +455,24 @@ def loadLibrary(self, lmodel: list):
456455 '1)["Modelica"]\n '
457456 '2)[("Modelica","3.2.3"), "PowerSystems"]\n ' )
458457
459- def setTempDirectory (self , customBuildDirectory ):
458+ def setTempDirectory (self , customBuildDirectory ) -> pathlib . Path :
460459 # create a unique temp directory for each session and build the model in that directory
461460 if customBuildDirectory is not None :
462461 if not os .path .exists (customBuildDirectory ):
463462 raise IOError (customBuildDirectory , " does not exist" )
464- self . tempdir = customBuildDirectory
463+ tempdir = pathlib . Path ( customBuildDirectory )
465464 else :
466- self . tempdir = tempfile .mkdtemp ()
467- if not os . path . exists ( self . tempdir ):
468- raise IOError (self . tempdir , " cannot be created" )
465+ tempdir = pathlib . Path ( tempfile .mkdtemp () )
466+ if not tempdir . is_dir ( ):
467+ raise IOError (tempdir , " cannot be created" )
469468
470- logger .info ("Define tempdir as %s" , self . tempdir )
471- exp = f'cd("{ pathlib . Path ( self . tempdir ) .absolute ().as_posix ()} ")'
469+ logger .info ("Define tempdir as %s" , tempdir )
470+ exp = f'cd("{ tempdir .absolute ().as_posix ()} ")'
472471 self .sendExpression (exp )
473472
474- def getWorkDirectory (self ):
473+ return tempdir
474+
475+ def getWorkDirectory (self ) -> pathlib .Path :
475476 return self .tempdir
476477
477478 def buildModel (self , variableFilter = None ):
@@ -815,15 +816,15 @@ def simulate(self, resultfile: Optional[str] = None, simflags: Optional[str] = N
815816 >>> simulate(simargs={"noEventEmit": None, "noRestart": None, "override": "e=0.3,g=10"}) # using simargs
816817 """
817818
818- om_cmd = ModelicaSystemCmd (runpath = pathlib . Path ( self .tempdir ) , modelname = self .modelName , timeout = timeout )
819+ om_cmd = ModelicaSystemCmd (runpath = self .tempdir , modelname = self .modelName , timeout = timeout )
819820
820821 if resultfile is None :
821822 # default result file generated by OM
822- self .resultfile = (pathlib . Path ( self .tempdir ) / f"{ self .modelName } _res.mat" ).as_posix ()
823+ self .resultfile = (self .tempdir / f"{ self .modelName } _res.mat" ).as_posix ()
823824 elif os .path .exists (resultfile ):
824825 self .resultfile = resultfile
825826 else :
826- self .resultfile = (pathlib . Path ( self .tempdir ) / resultfile ).as_posix ()
827+ self .resultfile = (self .tempdir / resultfile ).as_posix ()
827828 # always define the resultfile to use
828829 om_cmd .arg_set (key = "r" , val = self .resultfile )
829830
@@ -834,7 +835,7 @@ def simulate(self, resultfile: Optional[str] = None, simflags: Optional[str] = N
834835 if simargs :
835836 om_cmd .args_set (args = simargs )
836837
837- overrideFile = pathlib . Path ( self .tempdir ) / f"{ self .modelName } _override.txt"
838+ overrideFile = self .tempdir / f"{ self .modelName } _override.txt"
838839 if self .overridevariables or self .simoptionsoverride :
839840 tmpdict = self .overridevariables .copy ()
840841 tmpdict .update (self .simoptionsoverride )
@@ -1109,7 +1110,7 @@ def createCSVData(self) -> pathlib.Path:
11091110 ]
11101111 csv_rows .append (row )
11111112
1112- csvFile = pathlib . Path ( self .tempdir ) / f'{ self .modelName } .csv'
1113+ csvFile = self .tempdir / f'{ self .modelName } .csv'
11131114
11141115 with open (csvFile , "w" , newline = "" ) as f :
11151116 writer = csv .writer (f )
@@ -1201,9 +1202,9 @@ def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = N
12011202 raise IOError ("Linearization cannot be performed as the model is not build, "
12021203 "use ModelicaSystem() to build the model first" )
12031204
1204- om_cmd = ModelicaSystemCmd (runpath = pathlib . Path ( self .tempdir ) , modelname = self .modelName , timeout = timeout )
1205+ om_cmd = ModelicaSystemCmd (runpath = self .tempdir , modelname = self .modelName , timeout = timeout )
12051206
1206- overrideLinearFile = pathlib . Path ( self .tempdir ) / f'{ self .modelName } _override_linear.txt'
1207+ overrideLinearFile = self .tempdir / f'{ self .modelName } _override_linear.txt'
12071208
12081209 with open (overrideLinearFile , "w" ) as file :
12091210 for key , value in self .overridevariables .items ():
@@ -1236,7 +1237,7 @@ def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = N
12361237 self .simulationFlag = om_cmd .run ()
12371238
12381239 # code to get the matrix and linear inputs, outputs and states
1239- linearFile = pathlib . Path ( self .tempdir ) / "linearized_model.py"
1240+ linearFile = self .tempdir / "linearized_model.py"
12401241
12411242 # support older openmodelica versions before OpenModelica v1.16.2 where linearize() generates "linear_modelname.mo" file
12421243 if not linearFile .exists ():
0 commit comments