Skip to content

Commit

Permalink
Merge 78c750d into 1051fde
Browse files Browse the repository at this point in the history
  • Loading branch information
p-snft committed Jan 25, 2021
2 parents 1051fde + 78c750d commit 5fa352c
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 69 deletions.
61 changes: 37 additions & 24 deletions demandlib/examples/heat_demand_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@
# >>> holidays = dict(cal.holidays(2010))


def heat_example(testmode=False):
def heat_example(testmode=False,
ann_demands_per_type=None):
if ann_demands_per_type is None:
ann_demands_per_type = {'efh': 25000,
'mfh': 80000,
'ghd': 140000}
holidays = {
datetime.date(2010, 5, 24): 'Whit Monday',
datetime.date(2010, 4, 5): 'Easter Monday',
Expand All @@ -58,33 +63,41 @@ def heat_example(testmode=False):
periods=8760, freq='H'))

# Single family house (efh: Einfamilienhaus)
demand['efh'] = bdew.HeatBuilding(
demand.index, holidays=holidays, temperature=temperature,
shlp_type='EFH',
building_class=1, wind_class=1, annual_heat_demand=25000,
name='EFH').get_bdew_profile()
if "efh" in ann_demands_per_type:
demand['efh'] = bdew.HeatBuilding(
demand.index, holidays=holidays, temperature=temperature,
shlp_type='EFH',
building_class=1, wind_class=1,
annual_heat_demand=ann_demands_per_type['efh'],
name='EFH').get_bdew_profile()


# Multi family house (mfh: Mehrfamilienhaus)
demand['mfh'] = bdew.HeatBuilding(
demand.index, holidays=holidays, temperature=temperature,
shlp_type='MFH',
building_class=2, wind_class=0, annual_heat_demand=80000,
name='MFH').get_bdew_profile()
if "mfh" in ann_demands_per_type:
demand['mfh'] = bdew.HeatBuilding(
demand.index, holidays=holidays, temperature=temperature,
shlp_type='MFH',
building_class=2, wind_class=0,
annual_heat_demand=ann_demands_per_type['mfh'],
name='MFH').get_bdew_profile()

# Industry, trade, service (ghd: Gewerbe, Handel, Dienstleistung)
demand['ghd'] = bdew.HeatBuilding(
demand.index, holidays=holidays, temperature=temperature,
shlp_type='ghd', wind_class=0, annual_heat_demand=140000,
name='ghd').get_bdew_profile()

if plt is not None and not testmode:
# Plot demand of building
ax = demand.plot()
ax.set_xlabel("Date")
ax.set_ylabel("Heat demand in kW")
plt.show()
else:
print('Annual consumption: \n{}'.format(demand.sum()))
if "ghd" in ann_demands_per_type:
demand['ghd'] = bdew.HeatBuilding(
demand.index, holidays=holidays, temperature=temperature,
shlp_type='ghd', wind_class=0,
annual_heat_demand=ann_demands_per_type['ghd'],
name='ghd').get_bdew_profile()

if not testmode:
if plt is not None:
# Plot demand of building
ax = demand.plot()
ax.set_xlabel("Date")
ax.set_ylabel("Heat demand in kW")
plt.show()
else:
print('Annual consumption: \n{}'.format(demand.sum()))

return demand

Expand Down
84 changes: 45 additions & 39 deletions demandlib/examples/power_demand_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,63 +46,69 @@
datetime.date(2010, 12, 26): 'Second Christmas Day'}


def power_example(testmode=False):
def power_example(
ann_el_demand_per_sector=None,
testmode=False):
if ann_el_demand_per_sector is None:
ann_el_demand_per_sector = {
'g0': 3000,
'h0': 3000,
'i0': 3000,
'i1': 5000,
'i2': 6000,
'g6': 5000}
year = 2010

ann_el_demand_per_sector = {
'g0': 3000,
'h0': 3000,
'i0': 3000,
'i1': 5000,
'i2': 6000,
'g6': 5000}

# read standard load profiles
e_slp = bdew.ElecSlp(year, holidays=holidays)

# multiply given annual demand with timeseries
elec_demand = e_slp.get_profile(ann_el_demand_per_sector,
dyn_function_h0=True)
dyn_function_h0=False)

# Add the slp for the industrial group
ilp = profiles.IndustrialLoadProfile(e_slp.date_time_index,
holidays=holidays)

# Beginning and end of workday, weekdays and weekend days, and scaling
# factors by default
elec_demand['i0'] = ilp.simple_profile(ann_el_demand_per_sector['i0'])
if 'i0' in ann_el_demand_per_sector:
elec_demand['i0'] = ilp.simple_profile(ann_el_demand_per_sector['i0'])

# Set beginning of workday to 9 am
elec_demand['i1'] = ilp.simple_profile(ann_el_demand_per_sector['i1'],
am=settime(9, 0, 0))
if 'i1' in ann_el_demand_per_sector:
elec_demand['i1'] = ilp.simple_profile(ann_el_demand_per_sector['i1'],
am=settime(9, 0, 0))

# Change scaling factors
elec_demand['i2'] = ilp.simple_profile(
ann_el_demand_per_sector['i2'],
profile_factors={'week': {'day': 1.0, 'night': 0.8},
'weekend': {'day': 0.8, 'night': 0.6}})

print("Be aware that the values in the DataFrame are 15minute values with "
"a power unit. If you sum up a table with 15min values the result "
"will be of the unit 'kW15minutes'.")
print(elec_demand.sum())

print("You will have to divide the result by 4 to get kWh.")
print(elec_demand.sum() / 4)

print("Or resample the DataFrame to hourly values using the mean() "
"method.")

# Resample 15-minute values to hourly values.
elec_demand = elec_demand.resample('H').mean()
print(elec_demand.sum())

if plt is not None and not testmode:
# Plot demand
ax = elec_demand.plot()
ax.set_xlabel("Date")
ax.set_ylabel("Power demand")
plt.show()
if 'i2' in ann_el_demand_per_sector:
elec_demand['i2'] = ilp.simple_profile(
ann_el_demand_per_sector['i2'],
profile_factors={'week': {'day': 1.0, 'night': 0.8},
'weekend': {'day': 0.8, 'night': 0.6}})

if not testmode:
print("Be aware that the values in the DataFrame are 15 minute values"
+ "with a power unit. If you sum up a table with 15min values"
+ "the result will be of the unit 'kW15minutes'.")
print(elec_demand.sum())

print("You will have to divide the result by 4 to get kWh.")
print(elec_demand.sum() / 4)

print("Or resample the DataFrame to hourly values using the mean() "
"method.")

# Resample 15-minute values to hourly values.
elec_demand = elec_demand.resample('H').mean()
print(elec_demand.sum())

if plt is not None:
# Plot demand
ax = elec_demand.plot()
ax.set_xlabel("Date")
ax.set_ylabel("Power demand")
plt.show()

return elec_demand

Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def read(fname):
description='Demandlib of the open energy modelling framework',
long_description=read('README.rst'),
packages=find_packages(),
install_requires=['pandas >= 1.0',
'numpy >= 1.7.0'],
install_requires=['numpy >= 1.17.0',
'pandas >= 1.0, <1.2'],
package_data={
'demandlib': [os.path.join('bdew_data', '*.csv')],
'demandlib.examples': ['*.csv']},
Expand Down
19 changes: 17 additions & 2 deletions tests/test_electricity_demand.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,29 @@
Test the electricity demand
SPDX-FileCopyrightText: Uwe Krien <krien@uni-bremen.de>
SPDX-FileCopyrightText: Patrik Schönfeldt
SPDX-License-Identifier: MIT
"""

from demandlib.examples import power_demand_example
import numpy as np

from demandlib.examples import power_demand_example

def test_power_example():
"""Test the results of the power example."""
assert int(power_demand_example.power_example(True).sum().sum()) == 24999

ann_el_demands_per_sector = {'g0': 3000,
'h0': 3000,
'i0': 3000,
'i1': 5000,
'i2': 6000,
'g6': 5000}

demands = power_demand_example.power_example(
ann_el_demand_per_sector=ann_el_demands_per_sector,
testmode=True).sum()/4

for key in ann_el_demands_per_sector:
assert np.isclose(demands[key], ann_el_demands_per_sector[key])
24 changes: 22 additions & 2 deletions tests/test_heat_demand.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
"""
Test the electricity demand
SPDX-FileCopyrightText: Uwe Krien <krien@uni-bremen.de>
SPDX-FileCopyrightText: Patrik Schönfeldt
SPDX-License-Identifier: MIT
"""

import numpy as np

from demandlib.examples import heat_demand_example
import os


def test_heat_example():
"""Test the results of the heat example."""
assert int(heat_demand_example.heat_example(True).sum().sum()) == 245001
ann_demands_per_type = {'efh': 25000,
'mfh': 80000,
'ghd': 140000}

demands = heat_demand_example.heat_example(
ann_demands_per_type=ann_demands_per_type,
testmode=True).sum()

for key in ann_demands_per_type:
assert np.isclose(demands[key], ann_demands_per_type[key], rtol=1e-4)

0 comments on commit 5fa352c

Please sign in to comment.