-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexternal_functions.py
More file actions
392 lines (328 loc) · 14.8 KB
/
external_functions.py
File metadata and controls
392 lines (328 loc) · 14.8 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import pypsa
import pandas as pd
import numpy as np
import sqlite3
import json
import threading
import logging
import sys
from io import StringIO
from queue import Queue, Empty
# Global stream for capturing logs
log_stream = StringIO()
interval_disabled = False
def connect_to_db(DATABASE_PATH):
return sqlite3.connect(DATABASE_PATH)
def load_data(DATABASE_PATH):
conn = connect_to_db(DATABASE_PATH)
power_plants_df = pd.read_sql_query("SELECT * FROM power_plants", conn)
buses_df = pd.read_sql_query("SELECT * FROM buses", conn)
lines_df = pd.read_sql_query("SELECT id, name, from_bus, to_bus, length_km, max_capacity_mw, r, x FROM lines", conn)
demand_df = pd.read_sql_query("SELECT * FROM demand_profile", conn)
storage_units_df = pd.read_sql_query("SELECT * FROM storage_units", conn)
snapshots_df = pd.read_sql_query("SELECT * FROM snapshots", conn)
wind_profile_df = pd.read_sql_query("SELECT * FROM wind_profile", conn)
solar_profile_df = pd.read_sql_query("SELECT * FROM solar_profile", conn)
conn.close()
return power_plants_df, buses_df, lines_df, demand_df, storage_units_df, snapshots_df, wind_profile_df, solar_profile_df
def load_data_for_diagram(DATABASE_PATH):
conn = connect_to_db(DATABASE_PATH)
power_plants_df = pd.read_sql_query("SELECT * FROM power_plants", conn)
buses_df = pd.read_sql_query("SELECT * FROM buses", conn).set_index('id')
lines_df = pd.read_sql_query("SELECT id, name, from_bus, to_bus, length_km, max_capacity_mw, r, x FROM lines", conn)
storage_units_df = pd.read_sql_query("SELECT * FROM storage_units", conn)
conn.close()
return power_plants_df, buses_df, lines_df, storage_units_df
def load_data_table(DATABASE_PATH, table):
conn = connect_to_db(DATABASE_PATH)
df = pd.read_sql_query("SELECT * FROM " + str(table), conn)
conn.close()
return df
def save_data(DATABASE_PATH, table_name, df):
# Saves the provided dataframe 'df' into the table 'table_name' in the database located at DATABASE_PATH
conn = connect_to_db(DATABASE_PATH)
df.to_sql(table_name, conn, if_exists='replace', index=False)
conn.commit()
conn.close()
def create_network(power_plants_df, buses_df, lines_df, demand_df, storage_units_df, snapshots_df, wind_profile_df, solar_profile_df):
network = pypsa.Network() # Create a PyPSA Network
# Add snapshots to the network
network.set_snapshots(pd.to_datetime(snapshots_df['snapshot_time'], dayfirst=True))
solar_profile_df.index = pd.to_datetime(solar_profile_df.index, dayfirst=True)
wind_profile_df.index = pd.to_datetime(wind_profile_df.index, dayfirst=True)
solar_profile_df['snapshot_time'] = pd.to_datetime(solar_profile_df['snapshot_time'], errors='coerce', dayfirst=True)
wind_profile_df['snapshot_time'] = pd.to_datetime(wind_profile_df['snapshot_time'], errors='coerce', dayfirst=True)
# Add buses to the network
for _, row in buses_df.iterrows():
network.add("Bus", row["name"], v_nom=row["voltage_kv"],
longitude=row["longitude"], latitude=row["latitude"], carrier="AC")
# Add power plants (generators) to the network
for _, row in power_plants_df.iterrows():
bus_name = buses_df.loc[buses_df['id'] == int(row['bus_id']), 'name']
if not bus_name.empty:
p_nom_max = row["capacity_mw"]
# Determine the generation profile
if row['type'] == 'Solar':
filtered_df = solar_profile_df[solar_profile_df['profile_name'] == row['profile']]
elif row['type'] == 'Wind':
filtered_df = wind_profile_df[wind_profile_df['profile_name'] == row['profile']]
else:
filtered_df = pd.DataFrame()
# Validate and set the profile
if not filtered_df.empty and 'snapshot_time' in filtered_df.columns:
profile = filtered_df.set_index('snapshot_time')['profile']
profile = profile.reindex(network.snapshots).fillna(1) # Align with network snapshots
else:
profile = pd.Series(1.0, index=network.snapshots)
# Add generator to the network
network.add(
"Generator",
row["name"],
bus=bus_name.values[0],
p_nom=p_nom_max,
p_max_pu=profile,
marginal_cost=row["srmc"],
type=row['type'],
e_sum_max=1e10, # Temporary set to very high number. Default value of infinity would otherwise be overwritten by 1e6 which could be binding.
overwrite=True
)
else:
print(f"Warning: Bus ID {row['bus_id']} for generator {row['name']} not found in buses_df.")
# Add storage units to the network
for _, row in storage_units_df.iterrows():
bus_name = buses_df.loc[buses_df['id'] == int(row['bus_id']), 'name']
if not bus_name.empty:
network.add(
"StorageUnit",
row["name"],
bus=bus_name.values[0],
p_nom=row["capacity_mw"],
e_nom=row["max_energy_mwh"],
efficiency_store=row["efficiency"],
efficiency_dispatch=row["efficiency"],
overwrite=True
)
else:
print(f"Warning: Bus ID {row['bus_id']} for storage unit {row['name']} not found in buses_df.")
# Add transmission lines to the network
for _, row in lines_df.iterrows():
bus0_name = buses_df.loc[buses_df['id'] == int(row['from_bus']), 'name']
bus1_name = buses_df.loc[buses_df['id'] == int(row['to_bus']), 'name']
if not bus0_name.empty and not bus1_name.empty:
network.add(
"Line",
row["name"],
bus0=bus0_name.values[0],
bus1=bus1_name.values[0],
length=row["length_km"],
s_nom=1e6 if pd.isna(row["max_capacity_mw"]) else row["max_capacity_mw"],
r=row["r"],
x=row["x"],
carrier="AC",
overwrite=True
)
else:
print(f"Warning: Buses for line {row['name']} not found in buses_df (from_bus: {row['from_bus']}, to_bus: {row['to_bus']}).")
# Add demand as loads to the network (now including snapshot timestamp)
demand_timeseries = demand_df.pivot(index='snapshot', columns='bus_id', values='demand_mw')
# Ensure snapshot alignment
demand_timeseries.index = pd.to_datetime(demand_timeseries.index, dayfirst=True)
network.snapshots = pd.to_datetime(network.snapshots, dayfirst=True)
# Reindex demand data to match network snapshots
demand_timeseries = demand_timeseries.reindex(network.snapshots).fillna(0)
# Iterate over each bus to add the time series demand data as loads to the network
for bus_id in demand_timeseries.columns:
bus_name = buses_df.loc[buses_df['id'] == bus_id, 'name'].values[0]
if pd.notna(bus_name):
network.add(
"Load",
f"Load_{bus_id}",
bus=bus_name,
p_set=demand_timeseries[bus_id] # Provide entire time series directly
)
else:
print(f"Warning: Bus ID {bus_id} not found in buses_df.")
# Replace infinities with large finite values (since some solvers cannot handle 'inf')
network.generators.replace([np.inf], 1e6, inplace=True)
network.generators.replace([-np.inf], -1e6, inplace=True)
network.lines.replace([np.inf], 1e6, inplace=True)
network.lines.replace([-np.inf], -1e6, inplace=True)
network.loads.replace([np.inf], 1e6, inplace=True)
network.buses.replace([np.inf], 1e6, inplace=True)
network.buses.replace([-np.inf], -1e6, inplace=True)
return network
def get_network_elements(network):
nodes_data = []
edges_data = []
# Buses
for bus_id, bus in network.buses.iterrows():
nodes_data.append({
'id': str(bus_id),
'label': str(bus_id),
'type': 'bus',
'x': bus['longitude'],
'y': bus['latitude']
})
# Generators
for gen_id, gen in network.generators.iterrows():
bus = network.buses.loc[gen['bus']]
if gen['p_nom'] > 0:
nodes_data.append({
'id': str(gen_id),
'label': f'{gen_id}({gen["p_nom"]:.0f}MW)',
'type': 'generator',
'fuel': gen['type'], # To identify wind and solar plant
'capacity': gen['p_nom'],
'x': bus['longitude'],
'y': bus['latitude']
})
# Add an edge connecting generator to its bus
edges_data.append({
'source': str(gen_id),
'target': str(gen['bus']),
'type': 'secondary'
})
# Storage Units
for storage_id, storage in network.storage_units.iterrows():
bus = network.buses.loc[storage['bus']]
nodes_data.append({
'id': str(storage_id),
'label': str(storage_id),
'type': 'storage',
'capacity': storage['p_nom'],
'x': bus['longitude'],
'y': bus['latitude']
})
# Add an edge connecting storage to its bus
edges_data.append({
'source': str(storage_id),
'target': str(storage['bus']),
'type': 'secondary'
})
# Edges (Lines)
for line_id, line in network.lines.iterrows():
edges_data.append({
'source': str(line['bus0']),
'target': str(line['bus1']),
'length': line['length'],
'capacity': line['s_nom'],
'label': f'{line["s_nom"]:.0f}MW',
'type': 'primary'
})
# Clean output: Convert to JSON
clean_data = {
"nodes": nodes_data,
"links": edges_data
}
return json.dumps(clean_data)
def get_network_elements_from_df(DATABASE_PATH):
nodes_data = []
edges_data = []
power_plants_df, buses_df, lines_df, storage_units_df = load_data_for_diagram(DATABASE_PATH)
# Buses
for bus in buses_df.itertuples():
nodes_data.append({
'id': str(bus.Index),
'name': bus.name,
'label': bus.name,
'type': 'bus',
'x': bus.longitude,
'y': bus.latitude
})
# Edges (Lines)
for line in lines_df.itertuples():
edges_data.append({
'source': str(line.from_bus),
'target': str(line.to_bus),
'length': line.length_km,
'capacity': line.max_capacity_mw,
'label': f'{line.max_capacity_mw:.0f}MW',
'type': 'primary'
})
# Generators
for gen in power_plants_df.itertuples():
bus = buses_df.loc[gen.bus_id]
if gen.capacity_mw > 0:
nodes_data.append({
'id': 'gen'+str(gen.id),
'name': str(gen.name),
'label': f'{gen.name}({gen.capacity_mw:.0f}MW)',
'type': 'generator',
'fuel': gen.type, # To identify wind and solar plant
'capacity': gen.capacity_mw,
'x': bus['longitude'],
'y': bus['latitude']
})
# Add an edge connecting generator to its bus
edges_data.append({
'source': 'gen'+str(gen.id),
'target': str(gen.bus_id),
'type': 'secondary'
})
# Storage Units
for _, storage in storage_units_df.itertuples():
bus = buses_df.loc[storage.bus_id]
nodes_data.append({
'id': 'storage'+str(storage.id),
'name': str(storage.name),
'label': storage.name,
'type': 'storage',
'capacity': storage.capacity_mw,
'x': bus['longitude'],
'y': bus['latitude']
})
# Add an edge connecting storage to its bus
edges_data.append({
'source': 'storage'+str(storage.id),
'target': str(storage.bus_id),
'type': 'secondary'
})
# Clean output: Convert to JSON
clean_data = {
"nodes": nodes_data,
"links": edges_data
}
return json.dumps(clean_data)
def calc_aggregate_capacities(DATABASE_PATH):
power_plants_df, buses_df, lines_df, storage_units_df = load_data_for_diagram(DATABASE_PATH)
solar_capacity = power_plants_df.loc[power_plants_df['type'] == 'Solar', 'capacity_mw'].sum()
wind_capacity = power_plants_df.loc[power_plants_df['type'] == 'Wind', 'capacity_mw'].sum()
dsr_capacity = power_plants_df.loc[power_plants_df['type'] == 'DSR', 'capacity_mw'].sum()
return solar_capacity, wind_capacity, dsr_capacity
# Function to Run Optimization and Capture Output
def run_optimization(network):
# Load network data and create PyPSA network object
# power_plants_df, storage_units_df, buses_df, lines_df, demand_df, snapshots_df, wind_profile_df, solar_profile_df = load_data(DATABASE_PATH)
# network = create_network(power_plants_df, storage_units_df, buses_df, lines_df, demand_df, snapshots_df, wind_profile_df, solar_profile_df)
# Set up logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
try:
logger.info("Starting network optimization...")
try:
network.optimize(solver_name='cplex')
logger.info("Optimization complete!")
optimization_successful = True
except Exception as opt_error: # Catch solver errors
logger.exception("Error during optimization: %s", opt_error)
logger.debug("CPLEX log: %s", network.opt.get_log()) # Log CPLEX solver's internal log!
optimization_successful = False # Mark optimization as unsuccessful
raise # Re-raise the error for the main thread to handle
# Process results if optimization was successful
if optimization_successful:
# Convert the snapshots to a list of strings for JSON serialization
snapshots_list = [str(snapshot) for snapshot in network.snapshots]
# Store the optimization results as a dictionary
optimization_results_dict = {
"snapshots": snapshots_list,
"generators_t_p": {
"data": network.generators_t.p.rename(index=str).to_dict(),
"types": network.generators["type"].to_dict() # Add generator types from the network object
},
"storage_units_t_p": network.storage_units_t.p.rename(index=str).to_dict(),
"buses_t_marginal_price": network.buses_t.marginal_price.rename(index=str).to_dict()
}
return optimization_results_dict
except Exception as e:
logger.exception("An unexpected error occurred: %s", e)
return None