-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
133 lines (103 loc) · 4.89 KB
/
app.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
import os
from io import BytesIO
from pandapower import io_utils, file_io
import uvicorn
import pandapower as pp
import pandas as pd
from pandas import ExcelWriter
from fastapi import FastAPI, UploadFile, File
from fastapi.staticfiles import StaticFiles
from fastapi.responses import Response
from pf_types import NetworkData
from dotenv import load_dotenv
load_dotenv()
app = FastAPI()
@app.post("/power_flow", response_class=Response)
def power_flow(convert_to_per_unit: bool, base_voltage: float, base_power: float, iter_limit: float, pf_method: str, network_data: NetworkData):
if convert_to_per_unit:
network = pp.create_empty_network(sn_mva=base_power)
else:
base_power = 100
base_voltage = 10
network = pp.create_empty_network(sn_mva=base_power)
bus_id_to_index = {}
line_id_to_index = {}
for i, bus_id, in enumerate(network_data.network["buses"].keys()):
bus_id_to_index[bus_id] = i
bus = pp.create_bus(network, name=bus_id, index=i, type="b", vn_kv=base_voltage)
bus_values = network_data.bus_properties[bus_id]
if bus_values["type"] == "load_bus":
if convert_to_per_unit:
# take actual
real_power = float(bus_values["real_power"])
reactive_power = float(bus_values["reactive_power"])
else:
real_power = float(bus_values["real_power"]) * base_power
reactive_power = float(bus_values["reactive_power"]) * base_power
pp.create_load(network, bus, p_mw=real_power, q_mw=reactive_power)
elif bus_values["type"] == "generator_bus":
if convert_to_per_unit:
# take actual
real_power = float(bus_values["real_power"])
# convert to pu
voltage = float(bus_values["voltage"]) / base_voltage
else:
# convert to actual
real_power = float(bus_values["real_power"]) * base_power
# take pu
voltage = float(bus_values["voltage"])
pp.create_gen(network, bus, p_mw=real_power, vm_pu=voltage)
else:
if convert_to_per_unit:
# convert to pu
voltage = float(bus_values["voltage"]) / base_voltage
else:
# take pu
voltage = float(bus_values["voltage"])
pp.create_ext_grid(network, bus, vm_pu=voltage)
for i, (line_id, bus_ids) in enumerate(network_data.network["lines"].items()):
line_values = network_data.line_properties[line_id]
if convert_to_per_unit:
# take actual
resistance = float(line_values["resistance"])
reactance = float(line_values["reactance"])
else:
# convert to actual
base_impedance = (base_voltage ** 2) / base_power
resistance = float(line_values["resistance"]) * base_impedance
reactance = float(line_values["reactance"]) * base_impedance
line_id_to_index[line_id] = i
from_bus_index = bus_id_to_index[bus_ids[0]]
to_bus_index = bus_id_to_index[bus_ids[1]]
pp.create_line_from_parameters(network, from_bus=from_bus_index, to_bus=to_bus_index, length_km=1, r_ohm_per_km = resistance, x_ohm_per_km = reactance, c_nf_per_km = 0, max_i_ka = 0, name=line_id, index=i)
pp.runpp(network, calculate_voltage_angles=True, algorithm=pf_method, max_iteration=iter_limit)
memory_store = BytesIO()
with ExcelWriter(memory_store) as excel_wkbk:
network.res_bus.to_excel(excel_wkbk, sheet_name="Bus Data")
network.res_line.to_excel(excel_wkbk, sheet_name="Line Data")
memory_store.seek(0)
headers = {
"Content-Disposition": 'attachment; filename="results.xlsx"',
"Content-Type": "application/vnd.ms-excel",
}
return Response(content=memory_store.getvalue(), status_code=200, headers=headers)
@app.post("/file")
def file_pf(pf_method: str, iter_limit: float, file: UploadFile):
xls = pd.read_excel(file.file, sheet_name=None, index_col=0, engine="openpyxl")
try:
net = io_utils.from_dict_of_dfs(xls)
except:
net = file_io._from_excel_old(xls)
pp.runpp(net, calculate_voltage_angles=True, algorithm=pf_method, max_iteration=iter_limit)
memory_store = BytesIO()
with ExcelWriter(memory_store) as excel_wkbk:
net.res_bus.to_excel(excel_wkbk, sheet_name="Bus Data")
net.res_line.to_excel(excel_wkbk, sheet_name="Line Data")
memory_store.seek(0)
headers = {
"Content-Disposition": 'attachment; filename="results.xlsx"',
"Content-Type": "application/vnd.ms-excel",
}
return Response(content=memory_store.getvalue(), status_code=200, headers=headers)
app.mount("/", StaticFiles(directory="web", html=True))
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 5000)))