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

Force a dummy value for SHW in modelica loads even if not present #626

Merged
merged 11 commits into from
Apr 5, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from geojson_modelica_translator.model_connectors.couplings.diagram import Diagram
from geojson_modelica_translator.model_connectors.load_connectors.load_base import LoadBase
from geojson_modelica_translator.scaffold import Scaffold
from geojson_modelica_translator.utils import mbl_version
from geojson_modelica_translator.utils import _add_water_heating_patch, mbl_version


def render_template(template_name, template_params):
Expand Down Expand Up @@ -158,3 +158,7 @@ def to_modelica(self):
if "Districts" not in root_package.order:
root_package.add_model("Districts")
root_package.save()

# Hack workaround for MBLv10.0.0 bug that requires DHW in the loads even if it's not used
# This hack runs after the scaffold is created and before the simulation is run.
_add_water_heating_patch(self._scaffold.project_path)
37 changes: 36 additions & 1 deletion geojson_modelica_translator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
from pathlib import Path
from uuid import uuid4

_log = logging.getLogger(__name__)
logger = logging.getLogger(__name__)
logging.basicConfig(
vtnate marked this conversation as resolved.
Show resolved Hide resolved
level=logging.INFO,
format="%(asctime)s - %(levelname)s: %(message)s",
datefmt="%d-%b-%y %H:%M:%S",
)


def copytree(src, dst, symlinks=False, ignore=None):
Expand Down Expand Up @@ -49,6 +54,36 @@ def mbl_version():
return "10.0.0"


def _add_water_heating_patch(modelica_dir: Path):
"""Add a dummy value for water heating for MBL 10 limitation."""
data_dir = Path(modelica_dir) / "Loads" / "Resources" / "Data"
if data_dir.is_dir():
for bldg_dir in data_dir.iterdir():
mo_load_file = data_dir / bldg_dir / "modelica.mos"
# In case the modelica loads file isn't named modelica.mos:
if not mo_load_file.is_file():
modelica_loads = list((data_dir / bldg_dir).rglob("*"))
if len(modelica_loads) == 1:
mo_load_file = modelica_loads[0]
if mo_load_file.is_file():
fixed_lines, fl_found = [], False
with open(mo_load_file) as mlf:
for line in mlf:
if line == "#Peak water heating load = 0 Watts\n":
logger.debug(f"Adding dummy value for water heating to {mo_load_file}")
nl = "#Peak water heating load = 1 Watts\n"
fixed_lines.append(nl)
elif not fl_found and ";" in line:
split_vals = line.split(";")
split_vals[-1] = "1.0\n"
fixed_lines.append(";".join(split_vals))
fl_found = True
else:
fixed_lines.append(line)
with open(mo_load_file, "w") as mlf:
mlf.write("".join(fixed_lines))


class ModelicaPath:
"""
Class for storing Modelica paths. This allows the path to point to
Expand Down
Loading