/
callbacks_run.py
271 lines (256 loc) · 8.66 KB
/
callbacks_run.py
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import dash
import pandas as pd
from dash import CeleryManager, DiskcacheManager, Input, Output, State, dcc, html
from dash.exceptions import PreventUpdate
import pickle
from app import app
from DevelopThermalDynamicsModel import (
Building,
join_PV_load_temp,
run_scenarios,
add_emission_column,
)
from PVPerformance import calculate_PV_output
from functions import (
read_demand_from_xlsx_file,
process_tariff_rates,
)
from figures import line_plot
from dynamicFigures import generate_single_building_graphs
import os
figure_border_style = {
"border-style": "solid",
"border-color": "#ff4d4d",
"border-width": "1px",
"border-radius": "0.25rem",
}
# Create thermal model
@app.callback(
output=(
Output("thermal-model-creation-result", "children"),
Output("hidden-div-df-data-storage", "children"),
Output("coefficients-thermal-model", "children"),
),
inputs=(
Input("create-thermal-model-button", "n_clicks"),
State("building-type-radio", "value"),
State("construction-weight-radio", "value"),
State("dwelling-type-radio", "value"),
State("floor-area-radio", "value"),
State("location-radio", "value"),
),
background=True,
running=[
(Output("create-thermal-model-button", "disabled"), True, False),
(
Output("thermal-model-creation-result", "children"),
"Thermal model creation is in progress! Please wait!",
"",
),
(
Output("thermal-model-creation-result", "style"),
{"visibility": "visible"},
{"visibility": "visible"},
),
],
# cancel=[Input("create-thermal-model-button", "n_clicks")],
progress=Output("thermal-model-creation-result", "children"),
prevent_initial_call=True,
)
def update_thermal_model(
set_progress, n_clicks, starRating, weight, dwelling_type, size, location
):
if n_clicks:
building = Building(
starRating=starRating,
weight=weight,
type=dwelling_type,
size=size,
AC_size=0,
city=location,
)
print("Callback thermal model after click: OK")
coeffs_df = building.thermal_coefficients.to_frame()
# coeffs_df.to_csv("coefficients.csv")
coeffs_json = coeffs_df.to_json(date_format="iso", orient="split")
thermal_dynamics_json = (
building.thermal_dynamics_df.to_json(date_format="iso", orient="split"),
)
return (
"Thermal model is ready! Go to next steps or change the building specifications",
thermal_dynamics_json,
coeffs_json,
)
# PV performance
@dash.callback(
output=(
Output("PV-simulation-result", "children"),
Output("PV-results-hidden-div", "children"),
),
inputs=(
Input("PV-simulation-button", "n_clicks"),
State("PV-orientation-radio", "value"),
State("location-radio", "value"),
State("PV-rated-capacity", "value"),
# State("inverter-efficiency", "value"), # should be divided by 100
),
background=True,
running=[
(Output("PV-simulation-button", "disabled"), True, False),
# (Output("cancel-button-id", "disabled"), False, True),
(
Output("PV-simulation-result", "style"),
{"visibility": "visible"},
{"visibility": "visible"},
),
(
Output("PV-simulation-result", "children"),
"PV simulation in progress",
"",
),
],
# cancel=[Input("PV-simulation-button", "n_clicks")],
progress=Output("PV-simulation-result", "children"),
prevent_initial_call=True,
)
def update_progress(
set_progress,
n_clicks,
orientation,
location,
PV_rated_capacity,
):
inverter_efficiency = 100
if n_clicks:
# print(orientation, location, PV_rated_capacity, inverter_efficiency)
inverter_efficiency = inverter_efficiency / 100 # number to percent
PV_rated_capacity = PV_rated_capacity * 1000 # kW to Watt
PV_generation = calculate_PV_output(
city=location,
orientation=orientation,
PV_capacity_W=PV_rated_capacity,
inv_efficiency=inverter_efficiency,
)
print("PV output calculation OK")
# PV_generation_df = PV_generation.to_frame()
PV_generation_json = PV_generation.to_json(date_format="iso", orient="split")
print("PV to Json ok")
PV_generation.to_csv("PV_generation.csv")
return (["PV performance is ready! Go to next steps!"], 1)
# Solar pre-cooling main simulation loop
@dash.callback(
output=(
Output("paragraph-id", "children"),
Output("single-building-results-div", "children"),
# Output("single-building-results-div", "style"),
Output("run-simulation-hidden-div", "children"),
),
inputs=(
Input("run-button", "n_clicks"),
State("coefficients-thermal-model", "children"),
State("hidden-div-df-data-storage", "children"),
State("PV-results-hidden-div", "children"),
State("neutral-temp", "value"),
State("deviation-up-temp", "value"),
State("deviation-low-temp", "value"),
State("weekdays-occupancy-checklist", "value"),
State("AC-rated-capacity", "value"),
State("selected-demand-div-id", "children"),
State("run-simulation-hidden-div", "children"),
State("building-type-radio", "value"),
State("construction-weight-radio", "value"),
State("dwelling-type-radio", "value"),
State("floor-area-radio", "value"),
State("location-radio", "value"),
State("dropdown-tariff", "value"),
),
background=True,
running=[
(Output("run-button", "disabled"), True, False),
# (Output("cancel-button-id", "disabled"), False, True),
(
Output("paragraph-id", "style"),
{"visibility": "visible"},
{"visibility": "visible"},
),
(
Output("paragraph-id", "children"),
"Solar pre-cooling simulation in progress",
"",
),
],
# cancel=[Input("cancel-button-id", "n_clicks")],
progress=Output("paragraph-id", "children"),
prevent_initial_call=True,
)
def update_progress(
set_progress,
n_clicks,
thermal_coefficients,
hidden_div_thermal,
hidden_div_PV,
neutral_temp,
upper_limit,
lower_limit,
weekdays_occ,
AC_size,
site_id,
hidden_div_run,
starRating,
weight,
building_type,
building_size,
location,
tariff_id,
):
AC_size = (
AC_size * 3.5
) # user entecttrica capacity and here we convert it to thermal
if n_clicks != hidden_div_run[0]:
if hidden_div_thermal is None:
output_text = (
"No thermal model is available! Please create the thermal model first!"
)
return ([output_text], [], hidden_div_run)
elif hidden_div_PV is None:
output_text = (
"No PV simulation is available! Please simulate PV performance first!"
)
return ([output_text], [], hidden_div_run)
elif site_id is None:
output_text = (
"No demand profile is selected! Please select a demand profile first!"
)
return ([output_text], [], hidden_div_run) # None,
df_TMY = pd.read_json(hidden_div_thermal[0], orient="split")
print("Thermal model ok")
df_coeffs = pd.read_json(thermal_coefficients, orient="split")
print("Coefficient reading OK")
df_PV = pd.read_csv("PV_generation.csv")
print("read PV simulation results OK")
df_demand = read_demand_from_xlsx_file(site_id)
print("Read demand file Ok")
ready_df = join_PV_load_temp(PV=df_PV, load_temp=df_TMY, real_demand=df_demand)
# This df is used for baseline and pre-cooling scenarios
print("ready df ok")
building = Building(
starRating=starRating,
weight=weight,
type=building_type,
size=building_size,
AC_size=AC_size,
city=location,
)
building.occupancy_checklist = weekdays_occ
building.update_temperature_preferences(
ready_df, neutral_temp, upper_limit, lower_limit
)
# Add emission column
building = process_tariff_rates(building, tariff_id)
building = run_scenarios(building)
hidden_div_run = [n_clicks]
return (
["Successful!"],
generate_single_building_graphs(building),
hidden_div_run,
)