@@ -301,7 +301,7 @@ def __init__(
301301 lmodel : Optional [list [str | tuple [str , str ]]] = None ,
302302 commandLineOptions : Optional [str ] = None ,
303303 variableFilter : Optional [str ] = None ,
304- customBuildDirectory : Optional [str | os .PathLike | pathlib . Path ] = None ,
304+ customBuildDirectory : Optional [str | os .PathLike ] = None ,
305305 omhome : Optional [str ] = None ,
306306 omc_process : Optional [OMCProcessLocal ] = None ,
307307 build : bool = True ,
@@ -401,7 +401,7 @@ def __init__(
401401 self .setCommandLineOptions ("--linearizationDumpLanguage=python" )
402402 self .setCommandLineOptions ("--generateSymbolicLinearization" )
403403
404- self ._tempdir = self .setTempDirectory (customBuildDirectory )
404+ self ._work_dir : pathlib . Path = self .setWorkDirectory (customBuildDirectory )
405405
406406 if self ._file_name is not None :
407407 self ._loadLibrary (lmodel = self ._lmodel )
@@ -449,25 +449,34 @@ def _loadLibrary(self, lmodel: list):
449449 '1)["Modelica"]\n '
450450 '2)[("Modelica","3.2.3"), "PowerSystems"]\n ' )
451451
452- def setTempDirectory (self , customBuildDirectory : Optional [str | os .PathLike | pathlib .Path ] = None ) -> pathlib .Path :
453- # create a unique temp directory for each session and build the model in that directory
452+ def setWorkDirectory (self , customBuildDirectory : Optional [str | os .PathLike ] = None ) -> pathlib .Path :
453+ """
454+ Define the work directory for the ModelicaSystem / OpenModelica session. The model is build within this
455+ directory. If no directory is defined a unique temporary directory is created.
456+ """
454457 if customBuildDirectory is not None :
455- if not os . path . exists (customBuildDirectory ):
456- raise IOError ( f" { customBuildDirectory } does not exist" )
457- tempdir = pathlib . Path ( customBuildDirectory ). absolute ( )
458+ workdir = pathlib . Path (customBuildDirectory ). absolute ()
459+ if not workdir . is_dir ():
460+ raise IOError ( f"Provided work directory does not exists: { customBuildDirectory } !" )
458461 else :
459- tempdir = pathlib .Path (tempfile .mkdtemp ()).absolute ()
460- if not tempdir .is_dir ():
461- raise IOError (f"{ tempdir } could not be created" )
462+ workdir = pathlib .Path (tempfile .mkdtemp ()).absolute ()
463+ if not workdir .is_dir ():
464+ raise IOError (f"{ workdir } could not be created" )
462465
463- logger .info ("Define tempdir as %s" , tempdir )
464- exp = f'cd("{ tempdir .as_posix ()} ")'
466+ logger .info ("Define work dir as %s" , workdir )
467+ exp = f'cd("{ workdir .as_posix ()} ")'
465468 self .sendExpression (exp )
466469
467- return tempdir
470+ # set the class variable _tempdir ...
471+ self ._work_dir = workdir
472+ # ... and also return the defined path
473+ return workdir
468474
469475 def getWorkDirectory (self ) -> pathlib .Path :
470- return self ._tempdir
476+ """
477+ Return the defined working directory for this ModelicaSystem / OpenModelica session.
478+ """
479+ return self ._work_dir
471480
472481 def buildModel (self , variableFilter : Optional [str ] = None ):
473482 if variableFilter is not None :
@@ -943,7 +952,11 @@ def simulate_cmd(
943952 An instance if ModelicaSystemCmd to run the requested simulation.
944953 """
945954
946- om_cmd = ModelicaSystemCmd (runpath = self ._tempdir , modelname = self ._model_name , timeout = timeout )
955+ om_cmd = ModelicaSystemCmd (
956+ runpath = self .getWorkDirectory (),
957+ modelname = self ._model_name ,
958+ timeout = timeout ,
959+ )
947960
948961 # always define the result file to use
949962 om_cmd .arg_set (key = "r" , val = result_file .as_posix ())
@@ -955,7 +968,7 @@ def simulate_cmd(
955968 if simargs :
956969 om_cmd .args_set (args = simargs )
957970
958- overrideFile = self ._tempdir / f"{ self ._model_name } _override.txt"
971+ overrideFile = self .getWorkDirectory () / f"{ self ._model_name } _override.txt"
959972 if self ._override_variables or self ._simulate_options_override :
960973 tmpdict = self ._override_variables .copy ()
961974 tmpdict .update (self ._simulate_options_override )
@@ -1016,11 +1029,11 @@ def simulate(
10161029
10171030 if resultfile is None :
10181031 # default result file generated by OM
1019- self ._result_file = self ._tempdir / f"{ self ._model_name } _res.mat"
1032+ self ._result_file = self .getWorkDirectory () / f"{ self ._model_name } _res.mat"
10201033 elif os .path .exists (resultfile ):
10211034 self ._result_file = pathlib .Path (resultfile )
10221035 else :
1023- self ._result_file = self ._tempdir / resultfile
1036+ self ._result_file = self .getWorkDirectory () / resultfile
10241037
10251038 om_cmd = self .simulate_cmd (
10261039 result_file = self ._result_file ,
@@ -1322,7 +1335,7 @@ def _createCSVData(self, csvfile: Optional[pathlib.Path] = None) -> pathlib.Path
13221335 csv_rows .append (row )
13231336
13241337 if csvfile is None :
1325- csvfile = self ._tempdir / f'{ self ._model_name } .csv'
1338+ csvfile = self .getWorkDirectory () / f'{ self ._model_name } .csv'
13261339
13271340 with open (file = csvfile , mode = "w" , encoding = "utf-8" , newline = "" ) as fh :
13281341 writer = csv .writer (fh )
@@ -1452,9 +1465,13 @@ def load_module_from_path(module_name, file_path):
14521465 "use ModelicaSystem() to build the model first"
14531466 )
14541467
1455- om_cmd = ModelicaSystemCmd (runpath = self ._tempdir , modelname = self ._model_name , timeout = timeout )
1468+ om_cmd = ModelicaSystemCmd (
1469+ runpath = self .getWorkDirectory (),
1470+ modelname = self ._model_name ,
1471+ timeout = timeout ,
1472+ )
14561473
1457- overrideLinearFile = self ._tempdir / f'{ self ._model_name } _override_linear.txt'
1474+ overrideLinearFile = self .getWorkDirectory () / f'{ self ._model_name } _override_linear.txt'
14581475
14591476 with open (file = overrideLinearFile , mode = "w" , encoding = "utf-8" ) as fh :
14601477 for key , value in self ._override_variables .items ():
@@ -1491,7 +1508,7 @@ def load_module_from_path(module_name, file_path):
14911508 self ._simulated = True
14921509
14931510 # code to get the matrix and linear inputs, outputs and states
1494- linearFile = self ._tempdir / "linearized_model.py"
1511+ linearFile = self .getWorkDirectory () / "linearized_model.py"
14951512
14961513 # support older openmodelica versions before OpenModelica v1.16.2 where linearize() generates "linear_model_name.mo" file
14971514 if not linearFile .exists ():
0 commit comments