Skip to content

Commit

Permalink
refactoring pm 🐑
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Schaefer committed Oct 1, 2019
1 parent a49425b commit ad1078e
Showing 3 changed files with 35 additions and 23 deletions.
2 changes: 0 additions & 2 deletions pandapower/build_gen.py
Original file line number Diff line number Diff line change
@@ -110,8 +110,6 @@ def _build_pp_ext_grid(net, ppc, f, t):
add_p_constraints(net, "ext_grid", eg_is, ppc, f, t, delta)
ppc["bus"][eg_buses, VMAX] = net["ext_grid"]["vm_pu"].values[eg_is] + delta
ppc["bus"][eg_buses, VMIN] = net["ext_grid"]["vm_pu"].values[eg_is] - delta
# ppc["bus"][eg_buses, VMAX] = ppc["bus"][ppc["bus"][:, BUS_TYPE] == REF, VM]
# ppc["bus"][eg_buses, VMIN] = ppc["bus"][ppc["bus"][:, BUS_TYPE] == REF, VM]
else:
ppc["gen"][f:t, QMIN] = 0
ppc["gen"][f:t, QMAX] = 0
32 changes: 20 additions & 12 deletions pandapower/opf/pm_conversion.py
Original file line number Diff line number Diff line change
@@ -40,17 +40,11 @@ def convert_to_pm_structure(net):
return net, pm, ppc, ppci


def _runpm(net): # pragma: no cover
# convert pandapower to power models file -> this is done in python
net, pm, ppc, ppci = convert_to_pm_structure(net)
# call optinal callback function
if net._options["pp_to_pm_callback"] is not None:
net._options["pp_to_pm_callback"](net, ppci, pm)
# writes pm json to disk, which is loaded afterwards in julia
buffer_file = dump_pm_json(pm)
# run power models optimization in julia
result_pm = _call_powermodels(buffer_file, net._options["julia_file"])
# read results and write back to net
def _read_results_to_net(net, ppc, ppci, result_pm):
"""
reads power models results from result_pm to ppc / ppci and then to pandapower net
"""
# read power models results from result_pm to result (== ppc with results)
result, multinetwork = pm_results_to_ppc_results(net, ppc, ppci, result_pm)
net._pm_result = result_pm
success = ppc["success"]
@@ -65,6 +59,20 @@ def _runpm(net): # pragma: no cover
logger.warning("OPF did not converge!")


def _runpm(net): # pragma: no cover
# convert pandapower to power models file -> this is done in python
net, pm, ppc, ppci = convert_to_pm_structure(net)
# call optinal callback function
if net._options["pp_to_pm_callback"] is not None:
net._options["pp_to_pm_callback"](net, ppci, pm)
# writes pm json to disk, which is loaded afterwards in julia
buffer_file = dump_pm_json(pm)
# run power models optimization in julia
result_pm = _call_powermodels(buffer_file, net._options["julia_file"])
# read results and write back to net
_read_results_to_net(net, ppc, ppci, result_pm)


def dump_pm_json(pm, buffer_file=None):
# dump pm dict to buffer_file (*.json)
if buffer_file is None:
@@ -314,7 +322,7 @@ def pm_results_to_ppc_results(net, ppc, ppci, result_pm): # pragma: no cover
ppci["gen"][gen_idx, QG] = gen["qg"]

# read Q from branch results (if not DC calculation)
dc_results = np.isnan(sol["branch"]["1"]["qf"])
dc_results = sol["branch"]["1"]["qf"] is None or np.isnan(sol["branch"]["1"]["qf"])
# read branch status results (OTS)
branch_status = "br_status" in sol["branch"]["1"]
for i, branch in sol["branch"].items():
24 changes: 15 additions & 9 deletions pandapower/runpm.py
Original file line number Diff line number Diff line change
@@ -61,12 +61,13 @@ def runpm(net, julia_file, pp_to_pm_callback=None, calculate_voltage_angles=True
"""
net._options = {}
ac = True if "DC" not in pm_model else False
_add_ppc_options(net, calculate_voltage_angles=calculate_voltage_angles,
trafo_model=trafo_model, check_connectivity=check_connectivity,
mode="opf", switch_rx_ratio=2, init_vm_pu="flat", init_va_degree="flat",
enforce_q_lims=True, recycle=dict(_is_elements=False, ppc=False, Ybus=False),
voltage_depend_loads=False, delta=delta, trafo3w_losses=trafo3w_losses)
_add_opf_options(net, trafo_loading='power', ac=True, init="flat", numba=True,
_add_opf_options(net, trafo_loading='power', ac=ac, init="flat", numba=True,
pp_to_pm_callback=pp_to_pm_callback, julia_file=julia_file, pm_solver=pm_solver, pm_model=pm_model,
correct_pm_network_data=correct_pm_network_data, pm_mip_solver=pm_mip_solver,
pm_nl_solver=pm_nl_solver)
@@ -120,14 +121,15 @@ def runpm_dc_opf(net, pp_to_pm_callback=None, calculate_voltage_angles=True,
**correct_pm_network_data** (bool, True) - checks if network data is correct. If not tries to correct it
"""
julia_file = os.path.join(pp_dir, "opf", 'run_powermodels.jl')
ac = True if "DC" not in pm_model else False

net._options = {}
_add_ppc_options(net, calculate_voltage_angles=calculate_voltage_angles,
trafo_model=trafo_model, check_connectivity=check_connectivity,
mode="opf", switch_rx_ratio=2, init_vm_pu="flat", init_va_degree="flat",
enforce_q_lims=True, recycle=dict(_is_elements=False, ppc=False, Ybus=False),
voltage_depend_loads=False, delta=delta, trafo3w_losses=trafo3w_losses)
_add_opf_options(net, trafo_loading='power', ac=True, init="flat", numba=True,
_add_opf_options(net, trafo_loading='power', ac=ac, init="flat", numba=True,
pp_to_pm_callback=pp_to_pm_callback, julia_file=julia_file,
correct_pm_network_data=correct_pm_network_data, pm_model=pm_model, pm_solver=pm_solver)
_runpm(net)
@@ -181,14 +183,15 @@ def runpm_ac_opf(net, pp_to_pm_callback=None, calculate_voltage_angles=True,
**correct_pm_network_data** (bool, True) - checks if network data is correct. If not tries to correct it
"""
julia_file = os.path.join(pp_dir, "opf", 'run_powermodels.jl')
ac = True if "DC" not in pm_model else False

net._options = {}
_add_ppc_options(net, calculate_voltage_angles=calculate_voltage_angles,
trafo_model=trafo_model, check_connectivity=check_connectivity,
mode="opf", switch_rx_ratio=2, init_vm_pu="flat", init_va_degree="flat",
enforce_q_lims=True, recycle=dict(_is_elements=False, ppc=False, Ybus=False),
voltage_depend_loads=False, delta=delta, trafo3w_losses=trafo3w_losses)
_add_opf_options(net, trafo_loading='power', ac=True, init="flat", numba=True,
_add_opf_options(net, trafo_loading='power', ac=ac, init="flat", numba=True,
pp_to_pm_callback=pp_to_pm_callback, julia_file=julia_file, pm_model=pm_model, pm_solver=pm_solver,
correct_pm_network_data=correct_pm_network_data)
_runpm(net)
@@ -204,6 +207,7 @@ def runpm_tnep(net, pp_to_pm_callback=None, calculate_voltage_angles=True,
"""
julia_file = os.path.join(pp_dir, "opf", 'run_powermodels_tnep.jl')
ac = True if "DC" not in pm_model else False
if pm_solver is None:
if pm_model == "DCPPowerModel":
pm_solver = "gurobi"
@@ -218,7 +222,7 @@ def runpm_tnep(net, pp_to_pm_callback=None, calculate_voltage_angles=True,
mode="opf", switch_rx_ratio=2, init_vm_pu="flat", init_va_degree="flat",
enforce_q_lims=True, recycle=dict(_is_elements=False, ppc=False, Ybus=False),
voltage_depend_loads=False, delta=delta, trafo3w_losses=trafo3w_losses)
_add_opf_options(net, trafo_loading='power', ac=True, init="flat", numba=True,
_add_opf_options(net, trafo_loading='power', ac=ac, init="flat", numba=True,
pp_to_pm_callback=pp_to_pm_callback, julia_file=julia_file, pm_model=pm_model, pm_solver=pm_solver,
correct_pm_network_data=correct_pm_network_data)
_runpm(net)
@@ -235,6 +239,7 @@ def runpm_ots(net, pp_to_pm_callback=None, calculate_voltage_angles=True,
"""
julia_file = os.path.join(pp_dir, "opf", 'run_powermodels_ots.jl')
ac = True if "DC" not in pm_model else False
if pm_solver is None:
pm_solver = "juniper"

@@ -244,7 +249,7 @@ def runpm_ots(net, pp_to_pm_callback=None, calculate_voltage_angles=True,
mode="opf", switch_rx_ratio=2, init_vm_pu="flat", init_va_degree="flat",
enforce_q_lims=True, recycle=dict(_is_elements=False, ppc=False, Ybus=False),
voltage_depend_loads=False, delta=delta, trafo3w_losses=trafo3w_losses)
_add_opf_options(net, trafo_loading='power', ac=True, init="flat", numba=True,
_add_opf_options(net, trafo_loading='power', ac=ac, init="flat", numba=True,
pp_to_pm_callback=pp_to_pm_callback, julia_file=julia_file, pm_model=pm_model, pm_solver=pm_solver,
correct_pm_network_data=correct_pm_network_data)
_runpm(net)
@@ -253,7 +258,8 @@ def runpm_ots(net, pp_to_pm_callback=None, calculate_voltage_angles=True,

def runpm_storage_opf(net, calculate_voltage_angles=True,
trafo_model="t", delta=1e-8, trafo3w_losses="hv", check_connectivity=True,
n_timesteps=24, time_elapsed=1.0, correct_pm_network_data=True): # pragma: no cover
n_timesteps=24, time_elapsed=1.0, correct_pm_network_data=True,
pm_model="ACPPowerModel"): # pragma: no cover
"""
Runs a non-linear power system optimization with storages and time series using PowerModels.jl.
@@ -268,16 +274,16 @@ def runpm_storage_opf(net, calculate_voltage_angles=True,
"""
julia_file = os.path.join(pp_dir, "opf", 'run_powermodels_mn_storage.jl')

ac = True if "DC" not in pm_model else False
net._options = {}
_add_ppc_options(net, calculate_voltage_angles=calculate_voltage_angles,
trafo_model=trafo_model, check_connectivity=check_connectivity,
mode="opf", switch_rx_ratio=2, init_vm_pu="flat", init_va_degree="flat",
enforce_q_lims=True, recycle=dict(_is_elements=False, ppc=False, Ybus=False),
voltage_depend_loads=False, delta=delta, trafo3w_losses=trafo3w_losses)
_add_opf_options(net, trafo_loading='power', ac=True, init="flat", numba=True,
_add_opf_options(net, trafo_loading='power', ac=ac, init="flat", numba=True,
pp_to_pm_callback=add_storage_opf_settings, julia_file=julia_file,
correct_pm_network_data=correct_pm_network_data)
correct_pm_network_data=correct_pm_network_data, pm_model=pm_model)

net._options["n_time_steps"] = n_timesteps
net._options["time_elapsed"] = time_elapsed

0 comments on commit ad1078e

Please sign in to comment.