-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulate.py
More file actions
134 lines (112 loc) · 5.24 KB
/
simulate.py
File metadata and controls
134 lines (112 loc) · 5.24 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import os
import pandas as pd
from buildingspy.simulate.Simulator import Simulator
from buildingspy.io.outputfile import Reader
import matplotlib.pyplot as plt
from multiprocessing import Pool
from timeit import default_timer as timer
import re
import shutil
def simulate(path, prj, loading_time, result_path, visualization=False, save_in_gml=False):
"""BuildingsPy: simulation, loading and saving results, visualization
:param save_in_gml: saves the heatload series in EnergyADE
:param visualization: Graphs the heatload, inner and outside temperature
:param result_path: path to the excated heating load Dataframe
:param loading_time: Initial TEASER+ loasing time
:param prj: TEASER+ Instance
:param path: Simulation Result path
"""
"""BuildingsPy: Setup / Simulation"""
start_simulation = timer()
li = []
for buildings in prj.buildings:
s = Simulator(modelName=prj.name + "." + buildings.name + "." + buildings.name,
simulator="dymola", packagePath=path + "\\" + prj.name,
outputDirectory=path + "\\" + 'BuildingsPyResults' + "\\" + prj.name + "\\results")
li.append(s)
po = Pool()
po.map(simulateCase, li)
end_simulation = timer()
simulation_time = (end_simulation-start_simulation)
"""Clean up"""
# for buildings in prj1.buildings:
# shutil.rmtree(path + "\\" + prj1.name)
"""BuildingsPy: Load Results"""
df_results = pd.DataFrame()
start_post_processing = timer()
for buildings in prj.buildings:
if re.match(r"(\w+)bt_", buildings.name):
try:
matchObj = re.match(r"(\w+)bt_", buildings.name)
results = os.path.join(path + "\\" + 'BuildingsPyResults' + "\\" +
prj.name + "\\results\\" + buildings.name)
r = Reader(results, "dymola")
(time1, heatload) = r.values('multizone.PHeater[1]')
if matchObj.group(1) in df_results.columns:
hl = pd.Series(heatload)
df_results[matchObj.group(1)] = df_results[matchObj.group(1)].add(hl)
else:
df_results[matchObj.group(1)] = heatload
except:
print(f'failed simulation for building {buildings.name}')
else:
try:
if os.path.exists(path + "\\" + 'BuildingsPyResults' + "\\" +
prj.name + "\\results\\" + buildings.name + '.mat'):
results = os.path.join(path + "\\" + 'BuildingsPyResults' + "\\" +
prj.name + "\\results\\" + buildings.name + '.mat')
else:
print('No results found for building: ' + buildings.name)
r = Reader(results, "dymola")
(time1, heatload) = r.values('multizone.PHeater[1]')
# print(len(heatload))
(time2, insidetemperature) = r.values('multizone.TAir[1]')
(time3, outsidetemperature) = r.values('weaDat.weaBus.TDryBul')
"""Write in a DataFrame, Each buiding gets a column for the heatload"""
df_results[f'{buildings.name}'] = heatload
# df_results.to_csv(f'C:/Users/MaxPaine33/PycharmProjects/teaserplus/gmlfiles/Hamburg/'
# f'Results_pandas_csv/5_Buildings_test/{prj.name}_results.csv')
except Exception as e:
print(f'failed simulation for building {buildings.name} with error: {e}')
buildings.simulated_heat_load = list(zip(time1.tolist(), heatload.tolist()))
if visualization:
"""BuildingsPy: plot Results"""
fig = plt.figure(figsize=(12.8, 7.2), frameon=False, dpi=150)
ax = fig.add_subplot(211)
ax.plot(time1 / 3600, heatload / 1000, 'b', label=f'{buildings.name}', linewidth=1)
ax.set_xlabel('Time [h]')
ax.set_ylabel('Heat load [kW]')
ax.xaxis.get_major_ticks(1440)
ax.set_xlim(0, 8760)
ax.legend()
ax = fig.add_subplot(212)
ax.plot(time3 / 3600, outsidetemperature - 273.15, 'b', label='Outside Temperature', linewidth=1)
ax.plot(time2 / 3600, insidetemperature - 273.15, 'r', label='Inside Temperature', linewidth=1)
ax.set_xlabel('Time [h]')
ax.set_ylabel('temperature [$^\circ$C]')
ax.xaxis.get_major_ticks(1440)
ax.set_xlim(0, 8760)
ax.legend()
plt.show()
'''calculating the simulation time'''
end_post_processing = timer()
post_processing_time = (end_post_processing-start_post_processing)
df_results['loading time'] = loading_time
df_results['simulation time'] = simulation_time
df_results['post processing time'] = post_processing_time
df_results.to_csv(result_path)
if save_in_gml:
prj.save_citygml(path="../teaserplus_e3d/gmlfiles/ADE out", results=heatload)
def simulateCase(s):
"""
sets the general parameters for the simulation
:param s: BuildingsPy Simulator object
"""
s.showGUI()
# s.translate()
s.setSolver("Cvode")
print(s.printModelAndTime())
s.setStartTime(0)
s.setStopTime(31536000)
s.setNumberOfIntervals(8760)
s.simulate()