@@ -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 ,
@@ -400,7 +400,7 @@ def __init__(
400400 self .setCommandLineOptions ("--linearizationDumpLanguage=python" )
401401 self .setCommandLineOptions ("--generateSymbolicLinearization" )
402402
403- self ._tempdir = self .setTempDirectory (customBuildDirectory )
403+ self ._work_dir : pathlib . Path = self .setWorkDirectory (customBuildDirectory )
404404
405405 if self ._file_name is not None :
406406 self ._loadLibrary (lmodel = self ._lmodel )
@@ -448,25 +448,34 @@ def _loadLibrary(self, lmodel: list):
448448 '1)["Modelica"]\n '
449449 '2)[("Modelica","3.2.3"), "PowerSystems"]\n ' )
450450
451- def setTempDirectory (self , customBuildDirectory : Optional [str | os .PathLike | pathlib .Path ] = None ) -> pathlib .Path :
452- # create a unique temp directory for each session and build the model in that directory
451+ def setWorkDirectory (self , customBuildDirectory : Optional [str | os .PathLike ] = None ) -> pathlib .Path :
452+ """
453+ Define the work directory for the ModelicaSystem / OpenModelica session. The model is build within this
454+ directory. If no directory is defined a unique temporary directory is created.
455+ """
453456 if customBuildDirectory is not None :
454- if not os . path . exists (customBuildDirectory ):
455- raise IOError ( f" { customBuildDirectory } does not exist" )
456- tempdir = pathlib . Path ( customBuildDirectory ). absolute ( )
457+ workdir = pathlib . Path (customBuildDirectory ). absolute ()
458+ if not workdir . is_dir ():
459+ raise IOError ( f"Provided work directory does not exists: { customBuildDirectory } !" )
457460 else :
458- tempdir = pathlib .Path (tempfile .mkdtemp ()).absolute ()
459- if not tempdir .is_dir ():
460- raise IOError (f"{ tempdir } could not be created" )
461+ workdir = pathlib .Path (tempfile .mkdtemp ()).absolute ()
462+ if not workdir .is_dir ():
463+ raise IOError (f"{ workdir } could not be created" )
461464
462- logger .info ("Define tempdir as %s" , tempdir )
463- exp = f'cd("{ tempdir .as_posix ()} ")'
465+ logger .info ("Define work dir as %s" , workdir )
466+ exp = f'cd("{ workdir .as_posix ()} ")'
464467 self .sendExpression (exp )
465468
466- return tempdir
469+ # set the class variable _tempdir ...
470+ self ._work_dir = workdir
471+ # ... and also return the defined path
472+ return workdir
467473
468474 def getWorkDirectory (self ) -> pathlib .Path :
469- return self ._tempdir
475+ """
476+ Return the defined working directory for this ModelicaSystem / OpenModelica session.
477+ """
478+ return self ._work_dir
470479
471480 def buildModel (self , variableFilter : Optional [str ] = None ):
472481 if variableFilter is not None :
@@ -942,7 +951,11 @@ def simulate_cmd(
942951 An instance if ModelicaSystemCmd to run the requested simulation.
943952 """
944953
945- om_cmd = ModelicaSystemCmd (runpath = self ._tempdir , modelname = self ._model_name , timeout = timeout )
954+ om_cmd = ModelicaSystemCmd (
955+ runpath = self .getWorkDirectory (),
956+ modelname = self ._model_name ,
957+ timeout = timeout ,
958+ )
946959
947960 # always define the result file to use
948961 om_cmd .arg_set (key = "r" , val = result_file .as_posix ())
@@ -954,7 +967,7 @@ def simulate_cmd(
954967 if simargs :
955968 om_cmd .args_set (args = simargs )
956969
957- overrideFile = self ._tempdir / f"{ self ._model_name } _override.txt"
970+ overrideFile = self .getWorkDirectory () / f"{ self ._model_name } _override.txt"
958971 if self ._override_variables or self ._simulate_options_override :
959972 tmpdict = self ._override_variables .copy ()
960973 tmpdict .update (self ._simulate_options_override )
@@ -1013,11 +1026,11 @@ def simulate(
10131026
10141027 if resultfile is None :
10151028 # default result file generated by OM
1016- self ._result_file = self ._tempdir / f"{ self ._model_name } _res.mat"
1029+ self ._result_file = self .getWorkDirectory () / f"{ self ._model_name } _res.mat"
10171030 elif os .path .exists (resultfile ):
10181031 self ._result_file = pathlib .Path (resultfile )
10191032 else :
1020- self ._result_file = self ._tempdir / resultfile
1033+ self ._result_file = self .getWorkDirectory () / resultfile
10211034
10221035 om_cmd = self .simulate_cmd (
10231036 result_file = self ._result_file ,
@@ -1419,7 +1432,7 @@ def _createCSVData(self, csvfile: Optional[pathlib.Path] = None) -> pathlib.Path
14191432 csv_rows .append (row )
14201433
14211434 if csvfile is None :
1422- csvfile = self ._tempdir / f'{ self ._model_name } .csv'
1435+ csvfile = self .getWorkDirectory () / f'{ self ._model_name } .csv'
14231436
14241437 with open (file = csvfile , mode = "w" , encoding = "utf-8" , newline = "" ) as fh :
14251438 writer = csv .writer (fh )
@@ -1541,9 +1554,13 @@ def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = N
15411554 "use ModelicaSystem() to build the model first"
15421555 )
15431556
1544- om_cmd = ModelicaSystemCmd (runpath = self ._tempdir , modelname = self ._model_name , timeout = timeout )
1557+ om_cmd = ModelicaSystemCmd (
1558+ runpath = self .getWorkDirectory (),
1559+ modelname = self ._model_name ,
1560+ timeout = timeout ,
1561+ )
15451562
1546- overrideLinearFile = self ._tempdir / f'{ self ._model_name } _override_linear.txt'
1563+ overrideLinearFile = self .getWorkDirectory () / f'{ self ._model_name } _override_linear.txt'
15471564
15481565 with open (file = overrideLinearFile , mode = "w" , encoding = "utf-8" ) as fh :
15491566 for key , value in self ._override_variables .items ():
@@ -1573,19 +1590,17 @@ def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = N
15731590 om_cmd .args_set (args = simargs )
15741591
15751592 # the file create by the model executable which contains the matrix and linear inputs, outputs and states
1576- linear_file = self ._tempdir / "linearized_model.py"
1577-
1593+ linear_file = self .getWorkDirectory () / "linearized_model.py"
15781594 linear_file .unlink (missing_ok = True )
15791595
15801596 returncode = om_cmd .run ()
15811597 if returncode != 0 :
15821598 raise ModelicaSystemError (f"Linearize failed with return code: { returncode } " )
1583-
1584- self ._simulated = True
1585-
15861599 if not linear_file .exists ():
15871600 raise ModelicaSystemError (f"Linearization failed: { linear_file } not found!" )
15881601
1602+ self ._simulated = True
1603+
15891604 # extract data from the python file with the linearized model using the ast module - this allows to get the
15901605 # needed information without executing the created code
15911606 linear_data = {}
0 commit comments