@@ -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 build : Optional [bool ] = True
@@ -375,7 +375,6 @@ def __init__(
375375 self .linearinputs = [] # linearization input list
376376 self .linearoutputs = [] # linearization output list
377377 self .linearstates = [] # linearization states list
378- self .tempdir = ""
379378
380379 if session is not None :
381380 if not isinstance (session , OMCSessionZMQ ):
@@ -413,7 +412,7 @@ def __init__(
413412 self .setCommandLineOptions ("--linearizationDumpLanguage=python" )
414413 self .setCommandLineOptions ("--generateSymbolicLinearization" )
415414
416- self .setTempDirectory (customBuildDirectory )
415+ self .tempdir = self . setTempDirectory (customBuildDirectory )
417416
418417 if self .fileName is not None :
419418 self .loadLibrary (lmodel = self .lmodel )
@@ -461,22 +460,24 @@ def loadLibrary(self, lmodel: list):
461460 '1)["Modelica"]\n '
462461 '2)[("Modelica","3.2.3"), "PowerSystems"]\n ' )
463462
464- def setTempDirectory (self , customBuildDirectory ):
463+ def setTempDirectory (self , customBuildDirectory ) -> pathlib . Path :
465464 # create a unique temp directory for each session and build the model in that directory
466465 if customBuildDirectory is not None :
467466 if not os .path .exists (customBuildDirectory ):
468467 raise IOError (customBuildDirectory , " does not exist" )
469- self . tempdir = customBuildDirectory
468+ tempdir = pathlib . Path ( customBuildDirectory )
470469 else :
471- self . tempdir = tempfile .mkdtemp ()
472- if not os . path . exists ( self . tempdir ):
473- raise IOError (self . tempdir , " cannot be created" )
470+ tempdir = pathlib . Path ( tempfile .mkdtemp () )
471+ if not tempdir . is_dir ( ):
472+ raise IOError (tempdir , " cannot be created" )
474473
475- logger .info ("Define tempdir as %s" , self . tempdir )
476- exp = f'cd("{ pathlib . Path ( self . tempdir ) .absolute ().as_posix ()} ")'
474+ logger .info ("Define tempdir as %s" , tempdir )
475+ exp = f'cd("{ tempdir .absolute ().as_posix ()} ")'
477476 self .sendExpression (exp )
478477
479- def getWorkDirectory (self ):
478+ return tempdir
479+
480+ def getWorkDirectory (self ) -> pathlib .Path :
480481 return self .tempdir
481482
482483 def buildModel (self , variableFilter = None ):
@@ -820,15 +821,15 @@ def simulate(self, resultfile: Optional[str] = None, simflags: Optional[str] = N
820821 >>> simulate(simargs={"noEventEmit": None, "noRestart": None, "override": "e=0.3,g=10"}) # using simargs
821822 """
822823
823- om_cmd = ModelicaSystemCmd (runpath = pathlib . Path ( self .tempdir ) , modelname = self .modelName , timeout = timeout )
824+ om_cmd = ModelicaSystemCmd (runpath = self .tempdir , modelname = self .modelName , timeout = timeout )
824825
825826 if resultfile is None :
826827 # default result file generated by OM
827- self .resultfile = (pathlib . Path ( self .tempdir ) / f"{ self .modelName } _res.mat" ).as_posix ()
828+ self .resultfile = (self .tempdir / f"{ self .modelName } _res.mat" ).as_posix ()
828829 elif os .path .exists (resultfile ):
829830 self .resultfile = resultfile
830831 else :
831- self .resultfile = (pathlib . Path ( self .tempdir ) / resultfile ).as_posix ()
832+ self .resultfile = (self .tempdir / resultfile ).as_posix ()
832833 # always define the resultfile to use
833834 om_cmd .arg_set (key = "r" , val = self .resultfile )
834835
@@ -839,7 +840,7 @@ def simulate(self, resultfile: Optional[str] = None, simflags: Optional[str] = N
839840 if simargs :
840841 om_cmd .args_set (args = simargs )
841842
842- overrideFile = pathlib . Path ( self .tempdir ) / f"{ self .modelName } _override.txt"
843+ overrideFile = self .tempdir / f"{ self .modelName } _override.txt"
843844 if self .overridevariables or self .simoptionsoverride :
844845 tmpdict = self .overridevariables .copy ()
845846 tmpdict .update (self .simoptionsoverride )
@@ -1114,7 +1115,7 @@ def createCSVData(self) -> pathlib.Path:
11141115 ]
11151116 csv_rows .append (row )
11161117
1117- csvFile = pathlib . Path ( self .tempdir ) / f'{ self .modelName } .csv'
1118+ csvFile = self .tempdir / f'{ self .modelName } .csv'
11181119
11191120 with open (csvFile , "w" , newline = "" ) as f :
11201121 writer = csv .writer (f )
@@ -1206,9 +1207,9 @@ def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = N
12061207 raise IOError ("Linearization cannot be performed as the model is not build, "
12071208 "use ModelicaSystem() to build the model first" )
12081209
1209- om_cmd = ModelicaSystemCmd (runpath = pathlib . Path ( self .tempdir ) , modelname = self .modelName , timeout = timeout )
1210+ om_cmd = ModelicaSystemCmd (runpath = self .tempdir , modelname = self .modelName , timeout = timeout )
12101211
1211- overrideLinearFile = pathlib . Path ( self .tempdir ) / f'{ self .modelName } _override_linear.txt'
1212+ overrideLinearFile = self .tempdir / f'{ self .modelName } _override_linear.txt'
12121213
12131214 with open (overrideLinearFile , "w" ) as file :
12141215 for key , value in self .overridevariables .items ():
@@ -1241,7 +1242,7 @@ def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = N
12411242 self .simulationFlag = om_cmd .run ()
12421243
12431244 # code to get the matrix and linear inputs, outputs and states
1244- linearFile = pathlib . Path ( self .tempdir ) / "linearized_model.py"
1245+ linearFile = self .tempdir / "linearized_model.py"
12451246
12461247 # support older openmodelica versions before OpenModelica v1.16.2 where linearize() generates "linear_modelname.mo" file
12471248 if not linearFile .exists ():
0 commit comments