Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# imports
# standard
import math
from numbers import Real
import geopandas as gpd
# *****************************************************************************
# *****************************************************************************
# local, internal
from src.topupopt.data.gis.utils import read_gdf_file
from src.topupopt.data.buildings.dk import heat
# *****************************************************************************
# *****************************************************************************
class TestDataBuildingsDK:
# *************************************************************************
# *************************************************************************
def test_demand_dict(self):
# heat_demand_dict_by_building_entrance
osm_data_filename = 'tests/data/gdf_osm.gpkg'
building_data_filename = 'tests/data/gdf_buildings.gpkg'
bdg_gdf_container_columns = ('ejerskaber','koordinater','bygningspunkt')
number_time_intervals = 12
min_to_max_ratio = 0.1
intraperiod_time_interval_duration = [
30*24*3600
for i in range(number_time_intervals)
]
total_demand_true = 1000
total_area_true = 4563 # 5%: 4563 # 100%: 100882
assessments = ['q']
annual_heat_demand = {'q': 1000}
air_temperature = {'q': [5+i for i in range(number_time_intervals)]}
gdf_osm = gpd.read_file(osm_data_filename)
gdf_osm.set_index(['element_type', 'osmid'], drop=True, inplace=True)
gdf_buildings = read_gdf_file(
filename=building_data_filename,
packed_columns=bdg_gdf_container_columns,
index='index'
)
def verify_result(
out_dict,
out_area,
total_demand_true,
total_area_true,
# assessments,
# number_time_intervals
):
assert type(out_dict) == dict
assert isinstance(out_area, Real)
assert len(out_dict) == len(gdf_osm)
assert math.isclose(out_area, total_area_true, abs_tol=1e-3) # 5%: 4563 # 100%: 100882
for q in assessments:
assert math.isclose(
sum(sum(v[q]) for k, v in out_dict.items() if len(v[q]) != 0),
total_demand_true,
abs_tol=1e-3
)
# output dict must be keyed by entrance id and then by scenario
for k, v in out_dict.items():
assert k in gdf_osm.index
if len(v) == 0:
continue
for q in assessments:
assert q in v
assert len(v[q]) == number_time_intervals or len(v[q]) == 0
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# drop entries to keep things fast
share_keeper_osm_entries = 0.05
number_osm_entries = len(gdf_osm)
for index in gdf_osm.index:
if len(gdf_osm) < round(share_keeper_osm_entries*number_osm_entries):
break
gdf_osm.drop(index=index, inplace=True)
# create profiles in accordance with a set of states and a positive gain
heat_demand_dict, total_area = heat.heat_demand_profiles(
gdf_osm=gdf_osm,
gdf_buildings=gdf_buildings,
time_interval_durations=intraperiod_time_interval_duration,
assessments=assessments,
annual_heat_demand=annual_heat_demand,
air_temperature=air_temperature,
deviation_gain=1
)
verify_result(heat_demand_dict, total_area, total_demand_true, total_area_true)
# create profiles in accordance with a set of states and a negative gain
heat_demand_dict, total_area = heat.heat_demand_profiles(
gdf_osm=gdf_osm,
gdf_buildings=gdf_buildings,
time_interval_durations=intraperiod_time_interval_duration,
assessments=assessments,
annual_heat_demand=annual_heat_demand,
air_temperature=air_temperature,
deviation_gain=-1
)
verify_result(heat_demand_dict, total_area, total_demand_true, total_area_true)
# create profiles in accordance with a sinusoidal function (no phase shift)
heat_demand_dict, total_area = heat.heat_demand_profiles(
gdf_osm=gdf_osm,
gdf_buildings=gdf_buildings,
time_interval_durations=intraperiod_time_interval_duration,
assessments=assessments,
annual_heat_demand=annual_heat_demand,
min_max_ratio=min_to_max_ratio,
# air_temperature=air_temperature,
# state_correlates_with_output=False
# deviation_gain=1
verify_result(heat_demand_dict, total_area, total_demand_true, total_area_true)
# create profiles in accordance with a sinusoidal function (with phase shift)
heat_demand_dict, total_area = heat.heat_demand_profiles(
gdf_osm=gdf_osm,
gdf_buildings=gdf_buildings,
time_interval_durations=intraperiod_time_interval_duration,
assessments=assessments,
annual_heat_demand=annual_heat_demand,
min_max_ratio=min_to_max_ratio,
phase_shift_radians=math.pi/2
# air_temperature=air_temperature,
# state_correlates_with_output=False
# deviation_gain=1
)
verify_result(heat_demand_dict, total_area, total_demand_true, total_area_true)
# create profiles in accordance with states but without a predefined gain
# create profile (no optimisation)
heat_demand_dict, total_area = heat.heat_demand_profiles(
gdf_osm=gdf_osm,
gdf_buildings=gdf_buildings,
time_interval_durations=intraperiod_time_interval_duration,
assessments=assessments,
annual_heat_demand=annual_heat_demand,
air_temperature=air_temperature,
min_max_ratio=min_to_max_ratio,
states_correlate_profile=True,
)
verify_result(heat_demand_dict, total_area, total_demand_true, total_area_true)
# create profiles in accordance with states but without a predefined gain (optimisation)
# remove all but one osm entry (to keep things light)
for index in gdf_osm.index:
if len(gdf_osm) <= 1:
break
gdf_osm.drop(index=index, inplace=True)
# create profile
heat_demand_dict, total_area = heat.heat_demand_profiles(
gdf_osm=gdf_osm,
gdf_buildings=gdf_buildings,
time_interval_durations=intraperiod_time_interval_duration,
assessments=assessments,
annual_heat_demand=annual_heat_demand,
air_temperature=air_temperature,
min_max_ratio=min_to_max_ratio,
states_correlate_profile=True,
solver='glpk'
total_area_true = 200
verify_result(heat_demand_dict, total_area, total_demand_true, total_area_true)
# *************************************************************************
# *************************************************************************
# def test_demand_dict3(self):
# # heat_demand_dict_by_building_entrance
# osm_data_filename = 'tests/data/gdf_osm.gpkg'
# building_data_filename = 'tests/data/gdf_buildings.gpkg'
# bdg_gdf_container_columns = ('ejerskaber','koordinater','bygningspunkt')
# number_time_intervals = 12
# min_to_max_ratio = 0.1
# intraperiod_time_interval_duration = [
# 30*24*3600
# for i in range(number_time_intervals)
# ]
# annual_heat_demand_scenario = 1000
# total_area = 1000
# states = [10 for i in range(number_time_intervals)]
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# gdf_osm = gpd.read_file(osm_data_filename)
# gdf_osm.set_index(['element_type', 'osmid'], drop=True, inplace=True)
# gdf_buildings = read_gdf_file(
# filename=building_data_filename,
# packed_columns=bdg_gdf_container_columns,
# index='index'
# )
# # sinusoidal
# heat_demand_dict = heat.heat_demand_dict_by_building_entrance2(
# gdf_osm=gdf_osm,
# gdf_buildings=gdf_buildings,
# number_intervals=number_time_intervals,
# time_interval_durations=intraperiod_time_interval_duration,
# min_max_ratio=min_to_max_ratio,
# specific_demand=annual_heat_demand_scenario/total_area,
# )
# assert type(heat_demand_dict) == dict
# assert len(heat_demand_dict) == len(gdf_osm)
# assert math.isclose(
# annual_heat_demand_scenario,
# sum(sum(value) for value in heat_demand_dict.values()),
# abs_tol=1e-3,
# )
# # sinusoidal with phase shift
# heat_demand_dict = heat.heat_demand_dict_by_building_entrance2(
# gdf_osm=gdf_osm,
# gdf_buildings=gdf_buildings,
# number_intervals=number_time_intervals,
# time_interval_durations=intraperiod_time_interval_duration,
# min_max_ratio=min_to_max_ratio,
# specific_demand=annual_heat_demand_scenario/total_area ,
# phase_shift_radians=math.pi,
# )
# assert type(heat_demand_dict) == dict
# assert len(heat_demand_dict) == len(gdf_osm)
# assert math.isclose(
# annual_heat_demand_scenario,
# sum(sum(value) for value in heat_demand_dict.values()),
# abs_tol=1e-3,
# )
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# # predefined deviation gain, positive
# heat_demand_dict = heat.heat_demand_dict_by_building_entrance2(
# gdf_osm=gdf_osm,
# gdf_buildings=gdf_buildings,
# number_intervals=number_time_intervals,
# time_interval_durations=intraperiod_time_interval_duration,
# states=states,
# specific_demand=annual_heat_demand_scenario/total_area ,
# deviation_gain=3,
# )
# assert type(heat_demand_dict) == dict
# assert len(heat_demand_dict) == len(gdf_osm)
# assert math.isclose(
# annual_heat_demand_scenario,
# sum(sum(value) for value in heat_demand_dict.values()),
# abs_tol=1e-3,
# )
# # predefined deviation gain, negative
# heat_demand_dict = heat.heat_demand_dict_by_building_entrance2(
# gdf_osm=gdf_osm,
# gdf_buildings=gdf_buildings,
# number_intervals=number_time_intervals,
# time_interval_durations=intraperiod_time_interval_duration,
# states=states,
# specific_demand=annual_heat_demand_scenario/total_area ,
# deviation_gain=-3,
# )
# assert type(heat_demand_dict) == dict
# assert len(heat_demand_dict) == len(gdf_osm)
# assert math.isclose(
# annual_heat_demand_scenario,
# sum(sum(value) for value in heat_demand_dict.values()),
# abs_tol=1e-3,
# )
# # optimisation
# heat_demand_dict = heat.heat_demand_dict_by_building_entrance2(
# gdf_osm=gdf_osm,
# gdf_buildings=gdf_buildings,
# number_intervals=number_time_intervals,
# time_interval_durations=intraperiod_time_interval_duration,
# states=states,
# specific_demand=annual_heat_demand_scenario/total_area,
# states_correlate_profile=True,
# solver='glpk'
# )
# assert type(heat_demand_dict) == dict
# assert len(heat_demand_dict) == len(gdf_osm)
# assert math.isclose(
# annual_heat_demand_scenario,
# sum(sum(value) for value in heat_demand_dict.values()),
# abs_tol=1e-3,
# )
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# *************************************************************************
# *************************************************************************
# def test_bbr(self):
# # test get_bbr_building_data_geodataframe
# osm_data_filename = 'tests/data/gdf_osm.gpkg'
# gdf_osm = gpd.read_file(osm_data_filename)
# gdf_osm.set_index(['element_type', 'osmid'], drop=True, inplace=True)
# error_raised = False
# try:
# gdf_buildings, drop_list = bbr.get_bbr_building_data_geodataframe(
# list(gdf_osm[heat.label_osm_entrance_id]),
# None,
# None,
# None)
# except UnboundLocalError:
# error_raised = True
# assert error_raised
# # drop the rows with no data
# gdf_osm = gdf_osm.drop(
# index=[
# (gdf_osm[
# gdf_osm[
# heat.label_osm_entrance_id
# ]==bdg_entrance_id].index)[0]
# for bdg_entrance_id in drop_list
# ]
# )
# *****************************************************************************
# *****************************************************************************