-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModelHeat.py
292 lines (239 loc) · 13.1 KB
/
ModelHeat.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# file -- Module.py --
import demand_time_series_object as dtso
import pv_time_series_object as ptso
import pyomo.environ as pyo
import numpy as np
import random
from pyomo.opt import SolverFactory
from pyomo.opt import SolverStatus, TerminationCondition
def getSettings():
"""Returns a dictionary with default settings for usage in an HouseModel class object"""
settingsDict = {"time": 24 * 6,
"cost_Battery": 30 / 365,
"cost_buy": 0.25,
"dem": 1,
"num_clouds": 0,
"pow_clouds": 0,
"pow_pv_surplus": 15,
"cost_HeatStorage": 5 / 365,
"HP_COP": 3,
"dem_heat": 2,
"pv_curve": "uniform",
"cloud_dist": "equal",
"cloud_shape": "pow",
"dem_shape": "uniform",
"dem_heat_shape": "uniform",
"non_reductive": True,
"morning_mist": 0
}
return settingsDict
class HouseModel:
"""Create an instance of the House model, setting its basic parameters"""
def __init__(self, settings_dict=None):
if settings_dict is None:
self.Settings = getSettings()
else:
self.Settings = settings_dict
self.pv_curve = settings_dict["pv_curve"]
self.cloud_dist = settings_dict["cloud_dist"]
self.cloud_shape = settings_dict["cloud_shape"]
self.dem_shape = settings_dict["dem_shape"]
self.dem_heat_shape = settings_dict["dem_heat_shape"]
self.non_reductive = settings_dict["non_reductive"]
self.morning_mist = settings_dict["morning_mist"]
self.model = pyo.ConcreteModel()
def build(self):
"""Initiate the model and run it."""
# Create an instance of the model
model = pyo.ConcreteModel()
# Define index sets and preprocess inputs
time = range(self.Settings["time"])
delta_time = 1 / (self.Settings["time"] / 24)
# electric parameters
cost_Battery = self.Settings["cost_Battery"] # *((time[-1]+1)/8760) # € / (lifetime * kWh)
cost_buy_ele = self.Settings["cost_buy"] # €/kWh
cost_HeatStorage = self.Settings["cost_HeatStorage"] # €/kWh
# input processing for time series
time_step_selection = ["00:00", "01:00", "02:00", "03:00", "04:00", "05:00",
"06:00", "07:00", "08:00", "09:00", "10:00", "11:00",
"12:00", "13:00", "14:00", "15:00", "16:00", "17:00",
"18:00", "19:00", "20:00", "21:00", "22:00", "23:00"]
day = random.randint(120, 182) # May - July
# get an electric demand time series
e_dem_obj = dtso.demand_time_series_object(shape=self.dem_shape,
absolute_demand=self.Settings["dem"],
delta_time=delta_time,
length=time.__len__(),
data_path="timeseries/e_dem.csv",
day_selection=day,
time_step_selection=time_step_selection,
day_type_selection="Day")
DemandVal = e_dem_obj.get_array()
# get a heat demand time series
h_dem_obj = dtso.demand_time_series_object(shape=self.dem_shape,
absolute_demand=self.Settings["dem_heat"],
delta_time=delta_time,
length=time.__len__(),
data_path="timeseries/h_dem.csv",
day_selection=e_dem_obj.get_day_type() - 1,
time_step_selection=time_step_selection)
DemandHeatVal = h_dem_obj.get_array()
# get a PV availability time series with clouds
pv_av_obj = ptso.pv_time_series_object(shape=self.pv_curve,
delta_time=delta_time,
length=time.__len__(),
surplus=self.Settings["pow_pv_surplus"],
demand_ts=e_dem_obj,
data_path="timeseries/PV_TS.csv",
day_selection=day,
clouds=self.Settings["num_clouds"],
cloud_size=self.Settings["pow_clouds"],
cloud_dist=self.cloud_dist,
cloud_shape=self.cloud_shape,
pv_limit=[42, 126],
morning_mist=self.Settings["morning_mist"])
availability_pv = pv_av_obj.get_array()
#
if isinstance(self.Settings["pow_clouds"], list):
loss = self.Settings["pow_clouds"][0]
else:
loss = self.Settings["pow_clouds"]
# Define the Pyomo variables
# Electricity Sector
model.EnergyPV = pyo.Var(time, within=pyo.NonNegativeReals)
model.EnergyPV_curt = pyo.Var(time, within=pyo.NonNegativeReals)
model.Demand = pyo.Var(time, within=pyo.NonNegativeReals)
model.EnergyBattery = pyo.Var(time, within=pyo.NonNegativeReals)
model.EnergyBattery_IN = pyo.Var(time, within=pyo.NonNegativeReals)
model.EnergyBattery_OUT = pyo.Var(time, within=pyo.NonNegativeReals)
model.EnergyBuy = pyo.Var(time, within=pyo.NonNegativeReals)
model.CapacityBattery = pyo.Var(within=pyo.NonNegativeReals)
# indicators for evaluation purpose only
model.CostBuy = pyo.Var(within=pyo.Reals)
model.CostBat = pyo.Var(within=pyo.Reals)
model.NumClouds = pyo.Var(within=pyo.Reals)
model.PV_TS = pyo.Var(time, within=pyo.Reals)
model.PV_surplus = pyo.Var(within=pyo.Reals)
model.energy_loss = pyo.Var(within=pyo.Reals)
# Heat Sector
model.DemandHeat = pyo.Var(time, within=pyo.NonNegativeReals)
model.EnergyHeatStorage = pyo.Var(time, within=pyo.NonNegativeReals)
model.EnergyHeatStorage_IN = pyo.Var(time, within=pyo.NonNegativeReals)
model.EnergyHeatStorage_OUT = pyo.Var(time, within=pyo.NonNegativeReals)
model.CapacityHeatStorage = pyo.Var(within=pyo.NonNegativeReals)
model.HeatPump_Heat = pyo.Var(time, within=pyo.NonNegativeReals)
model.HeatPump_Electricity = pyo.Var(time, within=pyo.NonNegativeReals)
# indicators
model.costHeatStorage = pyo.Var(within=pyo.Reals)
model.morning_mist = pyo.Var(within=pyo.Reals)
# Define Objective
model.cost = pyo.Objective(expr=cost_buy_ele * sum(
model.EnergyBuy[i] for i in time) + cost_Battery * model.CapacityBattery +
cost_HeatStorage * model.CapacityHeatStorage +
# penalty so the HP isn't used for curtailment
1e-3 * sum(model.HeatPump_Electricity[i] for i in time)
, sense=pyo.minimize)
# Define Constraints
model.limEQ = pyo.ConstraintList() # technology limits
for i in time:
model.limEQ.add(
model.EnergyPV[i] + model.EnergyPV_curt[i] == availability_pv[i]) # PV cap is assumed to be 1
for i in time:
model.limEQ.add(model.EnergyBattery[i] <= model.CapacityBattery) # Battery UB
for i in time:
model.limEQ.add(model.EnergyHeatStorage[i] <= model.CapacityHeatStorage) # HeatStorage energy bound
model.limEQ.add(
model.CapacityHeatStorage <= 1000 * 0.00419 * 0.277778 * 40) # HeatStorage UB:liter * MJ/kg K * kWh/MJ * K
for i in time:
model.limEQ.add(model.HeatPump_Heat[i] <= 2 * max(DemandHeatVal)) # HeatPump UB
model.InitialBattery = pyo.Constraint(
expr=model.EnergyBattery[0] == model.EnergyBattery[time[-1]] - model.EnergyBattery_OUT[0] +
0.95 * model.EnergyBattery_IN[0]) # Battery level t=0 == t=T
model.InitialHeatStorage = pyo.Constraint(
expr=model.EnergyHeatStorage[0] == pow(0.99, delta_time) * model.EnergyHeatStorage[time[-1]] -
model.EnergyHeatStorage_OUT[0] +
model.EnergyHeatStorage_IN[0]) # Battery level t=0 == t=T
model.DemandEQ = pyo.ConstraintList()
for i in time:
model.DemandEQ.add(expr=model.Demand[i] == DemandVal[i]) # Electricity Demand
model.HeatDemandEQ = pyo.ConstraintList()
for i in time:
model.HeatDemandEQ.add(expr=model.DemandHeat[i] == DemandHeatVal[i]) # Heat Demand
model.batteryEQ = pyo.ConstraintList()
for i in time[1:]:
model.batteryEQ.add(
expr=model.EnergyBattery[i] == model.EnergyBattery[i - 1] - model.EnergyBattery_OUT[i] +
0.95 * model.EnergyBattery_IN[i]) # Battery Equation
model.heatStorageEQ = pyo.ConstraintList()
for i in time[1:]:
model.heatStorageEQ.add(
expr=model.EnergyHeatStorage[i] == pow(0.99, delta_time) * model.EnergyHeatStorage[i - 1] -
model.EnergyHeatStorage_OUT[i] +
model.EnergyHeatStorage_IN[i]) # Heat Storage Equation
model.EnergyEQ = pyo.ConstraintList()
for i in time:
model.EnergyEQ.add(
expr=model.Demand[i] == model.EnergyBuy[i] + model.EnergyBattery_OUT[i] - model.EnergyBattery_IN[
i] + model.EnergyPV[i] - model.HeatPump_Electricity[i]) # Energy Equation
model.HeatPumpEQ = pyo.ConstraintList()
for i in time:
model.HeatPumpEQ.add(
expr=model.HeatPump_Heat[i] <= model.HeatPump_Electricity[i] * self.Settings["HP_COP"]
) # Heat Pump Equation
model.HeatEQ = pyo.ConstraintList()
for i in time:
model.HeatEQ.add(
expr=model.DemandHeat[i] == model.EnergyHeatStorage_OUT[i] - model.EnergyHeatStorage_IN[i] +
model.HeatPump_Heat[i]
)
# Some equations that store input settings in a Variable
model.ValueCostBuy = pyo.Constraint(expr=model.CostBuy == cost_buy_ele)
model.ValueCostHS = pyo.Constraint(expr=model.costHeatStorage == cost_HeatStorage)
model.ValueCostBat = pyo.Constraint(expr=model.CostBat == cost_Battery)
model.ValueSurplus = pyo.Constraint(expr=model.PV_surplus == self.Settings["pow_pv_surplus"])
model.ValueLoss = pyo.Constraint(expr=model.energy_loss == loss)
model.NumberClouds = pyo.Constraint(expr=model.NumClouds == self.Settings["num_clouds"])
model.DurationMorningMist = pyo.Constraint(expr=model.morning_mist == self.Settings["morning_mist"])
model.PV = pyo.ConstraintList()
for i in time:
model.PV.add(model.PV_TS[i] == availability_pv[i])
self.model = model
def run(self):
"""Run a build model. Changes might be necessary to run with a different solver"""
model = self.model
# # # change below to use a different solver
solver = SolverFactory('cplex')
# some cplex settings
solver.options["emphasis_numerical"] = 'y'
solver.options["simplex_tolerances_optimality"] = 1e-6
# # #
results = solver.solve(model, tee=True, keepfiles=True)
results.write()
# model.pprint()
return model, results.solver.termination_condition
def build_and_run(self):
self.build()
return self.run()
def getKPI(model, basemodel=None):
"""read the values from a solved model and return them as a dictionary"""
d_x_i = {"c_b": pyo.value(model.CostBat),
"pv": [pyo.value(model.PV_TS[i]) for i in range(144)],
"c_HS": pyo.value(model.costHeatStorage)}
ResDict = {}
ResDict["Battery"] = pyo.value(model.CapacityBattery)
ResDict["HeatStorage"] = pyo.value(model.CapacityHeatStorage)
ResDict["c_Bat"] = pyo.value(model.CostBat)
ResDict["surplus"] = pyo.value(model.PV_surplus)
ResDict["clouds"] = pyo.value(model.NumClouds)
ResDict["energy_loss"] = pyo.value(model.energy_loss)
ResDict["c_HS"] = pyo.value(model.costHeatStorage)
ResDict["morning_mist"] = pyo.value(model.morning_mist)
ResDict["d_b"] = d_x_i["c_b"]
ResDict["d_p"] = d_x_i["pv"]
ResDict["d_h"] = d_x_i["c_HS"]
if basemodel is not None:
bm_kpi = getKPI(basemodel)
ResDict["d_b"] = d_x_i["c_b"] - bm_kpi["d_b"]
ResDict["d_p"] = np.linalg.norm(np.array(d_x_i["pv"]) - np.array(bm_kpi["d_p"]))
ResDict["d_h"] = d_x_i["c_HS"] - bm_kpi["d_h"]
return ResDict