Skip to content

Commit

Permalink
Add unified gas heater setup with options
Browse files Browse the repository at this point in the history
  • Loading branch information
kknos committed Jan 10, 2024
1 parent 31233d9 commit 81ba7c4
Show file tree
Hide file tree
Showing 8 changed files with 446 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@


@dataclass
class ModularHouseholdConfig(SystemSetupConfigBase):
class Options:

""" Set options for the system setup."""

pass


@dataclass
class ModularHouseholdConfig(SystemSetupConfigBase):
"""Modular Household Config class."""

#: configuration of the technological equipment of the household
# configuration of the technological equipment of the household
system_config_: Optional[system_config.SystemConfig] = None
#: configuration of the framework of the household (climate, house type, mobility behaviour, heating system, etc. )
# configuration of the framework of the household (climate, house type, mobility behaviour, heating system, etc. )
archetype_config_: Optional[archetype_config.ArcheTypeConfig] = None

@classmethod
Expand All @@ -43,7 +50,8 @@ def read_in_configs(pathname: str) -> ModularHouseholdConfig:
try:
with open(pathname, encoding="utf8") as config_file:
household_config_dict = json.load(config_file) # type: ignore
household_config: ModularHouseholdConfig = ModularHouseholdConfig.from_dict(household_config_dict.get("system_setup_config")) # type: ignore
household_config: ModularHouseholdConfig = ModularHouseholdConfig.from_dict(
household_config_dict.get("system_setup_config")) # type: ignore
log.information(f"Read modular household config from {pathname}")
if (household_config.system_config_ is None) and (household_config.archetype_config_ is None):
raise ValueError()
Expand Down
23 changes: 20 additions & 3 deletions hisim/system_setup_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# clean

from typing import Any
import json
from dataclasses import dataclass
from dataclass_wizard import JSONWizard
Expand All @@ -25,23 +26,33 @@ def load_from_json(cls, module_config_path: str) -> Self:
with open(module_config_path, "r", encoding="utf8") as file:
module_config_dict = json.loads(file.read())

# Read building config
# Read building config overwrites. It is used to scale the system setup.
building_config_dict = module_config_dict.pop("building_config", {})
if building_config_dict:
log.information("Using `building_config` for scaling.")
building_config = building.BuildingConfig.from_dict(building_config_dict)
else:
building_config = None

# Load (scaled) default values for system setup configuration
# Load option overwrites.
options_dict = module_config_dict.pop("options", {})
options = cls.get_default_options()
if options_dict:
log.information("Using `options`.")
utils.set_attributes_of_dataclass_from_dict(options, options_dict)

# Load (scaled) default values for system setup configuration.
if options_dict and not building_config:
raise ValueError("Options for default setup not yet implemented.")
if building_config:
my_config = cls.get_scaled_default(
building_config=building_config,
options=options
)
else:
my_config = cls.get_default()

# Read setup config
# Read setup config overwrites
setup_config_dict = module_config_dict.pop("system_setup_config", {})
if setup_config_dict:
log.information("Using `system_setup_config` to overwrite defaults.")
Expand All @@ -53,6 +64,11 @@ def load_from_json(cls, module_config_path: str) -> Self:

return my_config

@classmethod
def get_default_options(cls) -> Any:
"""Get default options."""
raise NotImplementedError

@classmethod
def get_default(cls) -> Self:
"""Get default."""
Expand All @@ -62,6 +78,7 @@ def get_default(cls) -> Self:
def get_scaled_default(
cls,
building_config: building.BuildingConfig,
options: Any
) -> Self:
"""Get scaled default."""
raise NotImplementedError
20 changes: 4 additions & 16 deletions hisim/system_setup_starter.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Build and simulate a system setup for a specific system setup that is defined in a JSON file.
Result files are stored in `/results`.
See `tests/test_system_setup_starter.py` for an system setup.
See `tests/test_system_setup_starter.py` for a system setup.
Run `hisim/system_setup_starter.py <json-file>` to start a simulation.
Required fields in the JSON file are: `path_to_module`, `function_in_module` and
`simulation_parameters`. SimulationParameters from the system_setups is not used. Instead the
`simulation_parameters`. SimulationParameters from the system_setups is not used. Instead, the
parameters from the JSON are set.
Optional field: `building_config`
Expand All @@ -16,7 +16,6 @@
The values from `system_setup_config` overwrite specific values of the configuration object.
Arguments that are not present keep the (scaled) default value.
"""
# clean

import json
import datetime
Expand All @@ -29,13 +28,6 @@
from hisim.utils import set_attributes_of_dataclass_from_dict
from hisim.simulator import SimulationParameters

# System setups need to use `create_configuration()` and their config class needs to implement
# `get_default()` to run with the system setup starter.
SUPPORTED_MODULES = [
"system_setups.modular_example",
"system_setups.household_1_advanced_hp_diesel_car",
]


def make_system_setup(
parameters_json: Union[dict, list], result_directory: str
Expand All @@ -52,18 +44,14 @@ def make_system_setup(
_parameters_json = deepcopy(parameters_json)
Path(result_directory).mkdir(parents=True, exist_ok=True) # pylint: disable=unexpected-keyword-arg
path_to_module = _parameters_json.pop("path_to_module")
setup_module_name = "system_setups." + path_to_module.split("/")[-1].replace(".py", "")
if setup_module_name not in SUPPORTED_MODULES:
raise NotImplementedError(
f"System setup starter can only be used with one of {', '.join(SUPPORTED_MODULES)}"
)

simulation_parameters_dict = _parameters_json.pop("simulation_parameters")
module_config_path = str(Path(result_directory).joinpath("module_config.json"))
simulation_parameters_path = str(Path(result_directory).joinpath("simulation_parameters.json"))
options = _parameters_json.pop("options", {})
building_config = _parameters_json.pop("building_config", {})
system_setup_config = _parameters_json.pop("system_setup_config", {})
module_config_dict = {"building_config": building_config, "system_setup_config": system_setup_config}
module_config_dict = {"options": options, "building_config": building_config, "system_setup_config": system_setup_config}

if _parameters_json:
raise AttributeError(
Expand Down
14 changes: 14 additions & 0 deletions system_setups/household_1_advanced_hp_diesel_car.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
__status__ = "development"


@dataclass
class Options:

""" Set options for the system setup."""

pass


@dataclass
class HouseholdAdvancedHPDieselCarConfig(SystemSetupConfigBase):

Expand All @@ -61,6 +69,11 @@ class HouseholdAdvancedHPDieselCarConfig(SystemSetupConfigBase):
car_config: generic_car.CarConfig
electricity_meter_config: electricity_meter.ElectricityMeterConfig

@classmethod
def get_default_options(cls):
"""Get default options."""
return Options()

@classmethod
def get_default(cls) -> "HouseholdAdvancedHPDieselCarConfig":
"""Get default HouseholdAdvancedHPDieselCarConfig."""
Expand All @@ -86,6 +99,7 @@ def get_default(cls) -> "HouseholdAdvancedHPDieselCarConfig":
def get_scaled_default(
cls,
building_config: building.BuildingConfig,
options: Options = Options()
) -> "HouseholdAdvancedHPDieselCarConfig":
"""Get scaled default HouseholdAdvancedHPDieselCarConfig."""

Expand Down
Loading

0 comments on commit 81ba7c4

Please sign in to comment.