Skip to content

Instantly share code, notes, and snippets.

@nicow-elia
Created June 4, 2024 09:54
Show Gist options
  • Save nicow-elia/37c77f8ca7dd71c687093ea7f26a5672 to your computer and use it in GitHub Desktop.
Save nicow-elia/37c77f8ca7dd71c687093ea7f26a5672 to your computer and use it in GitHub Desktop.
import pypowsybl
import numpy as np
import pandas as pd
def make_net_with_pst():
net = pypowsybl.network.create_ieee57()
net.create_buses(
id="PSTBus",
voltage_level_id="VL1"
)
net.create_lines(
id="PSTLine",
voltage_level1_id="VL1",
bus1_id="PSTBus",
voltage_level2_id="VL31",
bus2_id="B31",
r=0.0001,
x=0.001,
g1=0.0,
b1=5,
g2=0.0,
b2=5,
)
net.create_2_windings_transformers(
id="PST",
voltage_level1_id="VL1",
bus1_id="PSTBus",
voltage_level2_id="VL1",
bus2_id="B1",
rated_u1=1,
rated_u2=1,
rated_s=np.nan,
r=0.0,
x=0.005,
g=0.0,
b=0.0,
)
ptc_df = pd.DataFrame.from_records(
index='id', columns=['id', 'regulation_mode', 'low_tap', 'tap'],
data=[('PST', 'FIXED_TAP', 0, 1)])
steps_df = pd.DataFrame.from_records(
index='id', columns=['id', 'b', 'g', 'r', 'x', 'rho', 'alpha'],
data=[('PST', 0, 0, 0, 0, 1, 0),
('PST', 0, 0, 0, 0, 1, -8),
('PST', 0, 0, 0, 0, 1, -16)])
net.create_phase_tap_changers(ptc_df=ptc_df, steps_df=steps_df)
return net
net = make_net_with_pst()
analysis = pypowsybl.security.create_analysis()
monitored_branches = [i for i in net.get_branches().index if i != "PST"]
analysis.add_monitored_elements(branch_ids=monitored_branches)
analysis.add_single_element_contingency("PST")
res = analysis.run_dc(net)
p1_sec = res.branch_results.loc[("PST", "", monitored_branches)]["p1"].values
net.remove_elements("PST")
pypowsybl.loadflow.run_dc(net)
p1_ref = pd.concat([net.get_lines()["p1"], net.get_2_windings_transformers()["p1"]]).loc[monitored_branches].values
assert np.allclose(p1_sec, p1_ref)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment