Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

57 class attribute api #59

Merged
merged 3 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 24 additions & 27 deletions ebcpy/simulationapi/dymola_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,22 +112,24 @@ class DymolaAPI(SimulationAPI):

"""
_sim_setup_class: SimulationSetupClass = DymolaSimulationSetup
_dymola_instances: dict = {}
_items_to_drop = ["pool", "dymola", "_dummy_dymola_instance"]
dymola = None
# Default simulation setup
_supported_kwargs = ["show_window",
"modify_structural_parameters",
"dymola_path",
"equidistant_output",
"n_restart",
"debug",
"mos_script_pre",
"mos_script_post",
"dymola_version"]
_supported_kwargs = [
"show_window",
"modify_structural_parameters",
"dymola_path",
"equidistant_output",
"n_restart",
"debug",
"mos_script_pre",
"mos_script_post",
"dymola_version"
]

def __init__(self, cd, model_name, packages=None, **kwargs):
"""Instantiate class objects."""

self.dymola = None # Avoid key-error in get-state. Instance attribute needs to be there.
# Update kwargs with regard to what kwargs are supported.
self.extract_variables = kwargs.pop("extract_variables", True)
self.fully_initialized = False
Expand Down Expand Up @@ -156,8 +158,6 @@ def __init__(self, cd, model_name, packages=None, **kwargs):
self.mos_script_pre = self._make_modelica_normpath(self.mos_script_pre)
if self.mos_script_post is not None:
self.mos_script_post = self._make_modelica_normpath(self.mos_script_post)
# Set empty dymola attribute
self.dymola = None

super().__init__(cd=cd,
model_name=model_name,
Expand Down Expand Up @@ -331,9 +331,8 @@ def _single_simulation(self, kwargs):
# Handle multiprocessing
if self.use_mp:
idx_worker = self.worker_idx
if idx_worker not in self._dymola_instances:
if self.dymola is None:
self._setup_dymola_interface(use_mp=True)
self.dymola = self._dymola_instances[idx_worker]

# Handle eventlog
if show_eventlog:
Expand Down Expand Up @@ -675,27 +674,25 @@ def close(self):
super().close()
# Always close main instance
self._single_close(dymola=self.dymola)
self.dymola = None

def _close_multiprocessing(self, _):
wrk_idx = self.worker_idx
if wrk_idx in self._dymola_instances:
self._single_close(dymola=self._dymola_instances.pop(wrk_idx))
self._single_close()
DymolaAPI.dymola = None

def _single_close(self, **kwargs):
"""Closes a single dymola instance"""
dymola = kwargs["dymola"]
if dymola is None:
if self.dymola is None:
return # Already closed prior
# Execute the mos-script if given:
if self.mos_script_post is not None:
self.logger.info("Executing given mos_script_post "
"prior to closing.")
dymola.RunScript(self.mos_script_post)
self.logger.info("Output of mos_script_post: %s", dymola.getLastErrorLog())
self.dymola.RunScript(self.mos_script_post)
self.logger.info("Output of mos_script_post: %s", self.dymola.getLastErrorLog())
self.logger.info('Closing Dymola')
dymola.close()
self.dymola.close()
self.logger.info('Successfully closed Dymola')
self.dymola = None

def _close_dummy(self):
"""
Expand Down Expand Up @@ -774,8 +771,8 @@ def _setup_dymola_interface(self, use_mp):
warnings.warn("You have no licence to use Dymola. "
"Hence you can only simulate models with 8 or less equations.")
if use_mp:
self._dymola_instances[self.worker_idx] = dymola
return True
DymolaAPI.dymola = dymola
return None
return dymola

def _open_dymola_interface(self):
Expand Down Expand Up @@ -1011,7 +1008,7 @@ def _check_restart(self):
if self.sim_counter == self.n_restart:
self.logger.info("Closing and restarting Dymola to free memory")
self.close()
self.dymola = self._setup_dymola_interface(use_mp=False)
self._dummy_dymola_instance = self._setup_dymola_interface(use_mp=False)
self.sim_counter = 1
else:
self.sim_counter += 1
5 changes: 2 additions & 3 deletions examples/e3_dymola_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ def main(
show_window=True,
n_restart=-1,
equidistant_output=False,
get_structural_parameters=True
# Only necessary if you need a specific dymola version
#dymola_path=None,
#dymola_version=None
Expand Down Expand Up @@ -165,6 +164,6 @@ def main(
if __name__ == '__main__':
# TODO-User: Change the AixLib path!
main(
aixlib_mo=r"D:\02_workshop\AixLib\AixLib\package.mo",
n_cpu=1
aixlib_mo=r"D:\04_git\AixLib\AixLib\package.mo",
n_cpu=5
)