Skip to content

Commit

Permalink
Merge pull request #7 from tobirohrer/feature/refactor-building
Browse files Browse the repository at this point in the history
Refactor Simulation
  • Loading branch information
tobirohrer authored Sep 25, 2023
2 parents 17590f1 + 9bc2ea3 commit fac360c
Show file tree
Hide file tree
Showing 17 changed files with 13,752 additions and 4,685 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ questions, contact me via
## Usage

```python
from building_energy_storage_simulation import Environment
env = Environment()
from building_energy_storage_simulation import Environment, BuildingSimulation

simulation = BuildingSimulation()
env = Environment(building_simulation=simulation)

env.reset()
env.step(42)
env.step(1)
...
```

Expand Down
3 changes: 1 addition & 2 deletions building_energy_storage_simulation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from os import path

from building_energy_storage_simulation.battery import Battery
from building_energy_storage_simulation.simulation import Simulation
from building_energy_storage_simulation.building import Building
from building_energy_storage_simulation.building_simulation import BuildingSimulation
from building_energy_storage_simulation.environment import Environment

DATA_DIR = path.join(path.dirname(__file__), 'data')
30 changes: 0 additions & 30 deletions building_energy_storage_simulation/building.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
from typing import Tuple
from typing import Tuple, Iterable

from building_energy_storage_simulation.building import Building
import numpy as np

from building_energy_storage_simulation.battery import Battery
from building_energy_storage_simulation.utils import load_profile


class Simulation:
class BuildingSimulation:
"""
Simulation which wires the building, electricity load and solar generation profile together.
:param electricity_load_profile_file_name: Path to csv file containing electric load profile.
:type electricity_load_profile_file_name: str
:param solar_generation_profile_file_name: Path to csv file containing solar energy generation profile. Note that
the profile is in W per kWp of solar power installed. The actual solar generetion is determined by
multiplication with the `solar_power_installed`
:type solar_generation_profile_file_name: str
:param solar_power_installed: The installed peak photovoltaic power in kWp.
:type solar_power_installed: float
Represents the simulation of the building and wires the data profiles and the usage of the battery together.
:param electricity_load_profile: Load profile in kWh per time step.
:type electricity_load_profile: Iterable
:param solar_generation_profile: Generation profile in kWh per time step.
:type solar_generation_profile: Iterable
:param battery_capacity: The capacity of the battery in kWh.
:type battery_capacity: float
:param max_battery_charge_per_timestep: Maximum amount of energy (kWh) which can be obtained from the battery or
Expand All @@ -24,22 +22,19 @@ class Simulation:
"""

def __init__(self,
electricity_load_profile_file_name: str = 'electricity_load_profile.csv',
solar_generation_profile_file_name: str = 'solar_generation_profile.csv',
electricity_load_profile: Iterable = load_profile('electricity_load_profile.csv', 'Load [kWh]'),
solar_generation_profile: Iterable = load_profile('solar_generation_profile.csv', 'Generation [kWh]'),
battery_capacity: float = 100,
solar_power_installed: float = 240,
max_battery_charge_per_timestep: float = 20
):
self.building = Building(solar_power_installed=solar_power_installed,
battery_capacity=battery_capacity,
max_battery_charge_per_timestep=max_battery_charge_per_timestep)
self.electricity_load_profile = np.array(electricity_load_profile)
self.solar_generation_profile = np.array(solar_generation_profile)
self.battery = Battery(capacity=battery_capacity,
max_battery_charge_per_timestep=max_battery_charge_per_timestep)

self.electricity_load_profile = load_profile(electricity_load_profile_file_name, 'Load [kWh]')
self.solar_generation_profile = load_profile(solar_generation_profile_file_name, 'Inverter Power (W)')
assert len(self.solar_generation_profile) == len(self.electricity_load_profile), \
"Solar generation profile and electricity load profile must be of the same length."
# Solar Generation Profile is in W per 1KW of Solar power installed
self.solar_generation_profile = self.solar_generation_profile * self.building.solar_power_installed / 1000

self.step_count = 0
self.start_index = 0
pass
Expand All @@ -53,11 +48,11 @@ def reset(self):
"""

self.building.reset()
self.battery.reset()
self.step_count = 0
pass

def simulate_one_step(self, amount: float) -> Tuple[float, float]:
def simulate_one_step(self, action: float) -> Tuple[float, float]:
"""
Performs one simulation step by:
1. Charging or discharging the battery depending on the amount.
Expand All @@ -66,8 +61,11 @@ def simulate_one_step(self, amount: float) -> Tuple[float, float]:
4. Calculating the amount of excess energy which is considered lost.
5. Increasing the step counter.
:param amount: Amount of energy to be stored or retrieved from the battery. In kWh.
:type amount: float
:param action: Fraction of energy to be stored or retrieved from the battery. The action lies in [-1;1]. The
action represents the fraction of `max_battery_charge_per_timestep` which should be used to charge or
discharge the battery. 1 represents the maximum possible amount of energy which can be used to charge the
battery per time step.
:type action: float
:returns:
Tuple of:
1. Amount of energy consumed in this time step. This is calculated by: `battery_energy`
Expand All @@ -81,7 +79,7 @@ def simulate_one_step(self, amount: float) -> Tuple[float, float]:
electricity_load_of_this_timestep = self.electricity_load_profile[self.start_index + self.step_count]
solar_generation_of_this_timestep = self.solar_generation_profile[self.start_index + self.step_count]

electricity_consumed_for_battery = self.building.battery.use(amount)
electricity_consumed_for_battery = self.battery.use(action * self.battery.max_battery_charge_per_timestep)
electricity_consumption = electricity_consumed_for_battery + electricity_load_of_this_timestep - \
solar_generation_of_this_timestep
excess_energy = 0
Expand Down
Loading

0 comments on commit fac360c

Please sign in to comment.