Skip to content

Commit

Permalink
pu prototyped
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderclaeys committed Jan 17, 2020
1 parent efa631d commit 0e7358f
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 107 deletions.
33 changes: 27 additions & 6 deletions src/io/data_model_components.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#TODO
# Can buses in a voltage zone have different terminals?


import PowerModels
_PMs = PowerModels

Expand Down Expand Up @@ -59,10 +63,7 @@ end
function create_data_model(;kwargs...)
data_model = Dict{String, Any}()
data_model["quantity_scalars"] = Dict{String, Any}()
add_kwarg!(data_model["quantity_scalars"], kwargs, :v_scalar, 1E3)
add_kwarg!(data_model["quantity_scalars"], kwargs, :s_scalar, 1E3)
add_kwarg!(data_model["quantity_scalars"], kwargs, :z_scalar, 1.0)
add_kwarg!(data_model["quantity_scalars"], kwargs, :i_scalar, 1.0)
add_kwarg!(data_model["v_var_scalar"], kwargs, :v_var_scalar, 1E3)
return data_model
end

Expand Down Expand Up @@ -201,10 +202,20 @@ DTYPES[:voltage_zone] = Dict(
:id => String,
:buses => Array{String, 1},
:vnom => Real,
:vm_ln_min => Array{<:Real, 1},
:vm_ln_max => Array{<:Real, 1},
:vm_lg_min => Array{<:Real, 1},
:vm_lg_max => Array{<:Real, 1},
:vm_ng_min => Array{<:Real, 1},
:vm_ng_max => Array{<:Real, 1},
:vm_ll_min => Array{<:Real, 1},
:vm_ll_max => Array{<:Real, 1},
)


CHECKS[:voltage_zone] = function check_voltage_zone(data, bus)
i = bus["id"]
@assert(haskey(bus, "vnom"), "Voltage zone $i: a voltage zone should specify a vnom.")
end


Expand Down Expand Up @@ -266,6 +277,16 @@ CHECKS[:bus] = function check_bus(data, bus)
end
end
end
if haskey(bus, "voltage_zone")
zone_id = bus["voltage_zone"]
@assert(haskey(data["voltage_zone"], zone_id), "Bus $i: the voltage zone $zone_id is not defined.")
voltage_zone = data["voltage_zone"][zone_id]
for key in ["vm_ln_min", "vm_ln_max", "vm_lg_min", "vm_lg_max", "vm_ng_min", "vm_ng_max", "vm_ll_min", "vm_ll_max"]
if haskey(voltage_zone, key)
@assert(!haskey(bus, key), "Bus $i: the property $key is already specified in the voltage_zone; this cannot be overwritten.")
end
end
end
end

function create_bus(id; kwargs...)
Expand Down Expand Up @@ -460,12 +481,12 @@ CHECKS[:transformer_2w3ph_ideal] = function check_transformer_2w3ph_ideal(data,
end


function create_transformer_2w3ph_ideal(id, f_bus, t_bus, vnom; kwargs...)
function create_transformer_2w3ph_ideal(id, f_bus, t_bus, tm_nom; kwargs...)
trans = Dict{String,Any}()
trans["id"] = id
trans["f_bus"] = f_bus
trans["t_bus"] = t_bus
trans["vnom"] = vnom
trans["tm_nom"] = tm_nom
add_kwarg!(trans, kwargs, :configuration, "wye")
add_kwarg!(trans, kwargs, :f_terminals, trans["configuration"]=="wye" ? [1, 2, 3, 4] : [1, 2, 3])
add_kwarg!(trans, kwargs, :t_terminals, [1, 2, 3, 4])
Expand Down
43 changes: 39 additions & 4 deletions src/io/data_model_mapping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import LinearAlgebra
function map_down_data_model(data_model_user)
data_model_base = deepcopy(data_model_user)
data_model_base["source_data_model"] = data_model_user
load_to_shunt!(data_model_base)
capacitor_to_shunt!(data_model_base)
decompose_transformer_nw3ph_lossy!(data_model_base)

_expand_linecode!(data_model_base)
_expand_voltage_zone!(data_model_base)
_load_to_shunt!(data_model_base)
_capacitor_to_shunt!(data_model_base)
_decompose_transformer_nw3ph_lossy!(data_model_base)

# add low level component types if not present yet
for comp_type in ["load", "generator", "bus", "line", "shunt", "transformer_2w3ph_ideal"]
Expand All @@ -18,6 +21,38 @@ function map_down_data_model(data_model_user)
end


function _expand_voltage_zone!(data_model)
# expand line codes
for (id, bus) in data_model["bus"]
if haskey(bus, "voltage_zone")
voltage_zone = data_model["voltage_zone"][bus["voltage_zone"]]
bus["vnom"] = voltage_zone["vnom"]
for key in ["vm_ln_min", "vm_ln_max", "vm_lg_min", "vm_lg_max", "vm_ng_min", "vm_ng_max", "vm_ll_min", "vm_ll_max"]
if haskey(voltage_zone, key)
bus[key] = voltage_zone[key]
end
end
delete!(bus, "voltage_zone")
end
end
end


function _expand_linecode!(data_model)
# expand line codes
for (id, line) in data_model["line"]
if haskey(line, "linecode")
linecode = data_model["linecode"][line["linecode"]]
for key in ["rs", "xs", "g_fr", "g_to", "b_fr", "b_to"]
line[key] = line["length"]*linecode[key]
end
delete!(line, "linecode")
delete!(line, "length")
end
end
end


function _load_to_shunt!(data_model)
for (id, load) in data_model["load"]
if load["model"]=="constant_impedance"
Expand Down Expand Up @@ -122,7 +157,7 @@ function _decompose_transformer_nw3ph_lossy!(data_model)
# convert x_sc from list of upper triangle elements to an explicit dict
y_sh = g_sh + im*b_sh
z_sc = Dict([(key, im*x_sc[i]) for (i,key) in enumerate([(i,j) for i in 1:nrw for j in i+1:nrw])])
vbuses, vlines, trans_t_bus_w = build_loss_model!(data_model, r_s, z_sc, y_sh)
vbuses, vlines, trans_t_bus_w = _build_loss_model!(data_model, r_s, z_sc, y_sh)


trans_w = Array{Dict, 1}(undef, nrw)
Expand Down
143 changes: 123 additions & 20 deletions src/io/data_model_pu.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


function _find_zones(data_model)
unused_line_ids = Set(keys(data_model["line"]))
bus_lines = Dict([(id,Set()) for id in keys(data_model["bus"])])
Expand Down Expand Up @@ -36,7 +35,7 @@ end

function _calc_vbase(data_model, vbase_sources::Dict{String,<:Real})
# find zones of buses connected by lines
zones = find_zones(data_model)
zones = _find_zones(data_model)
bus_to_zone = Dict([(bus,zone) for (zone, buses) in zones for bus in buses])

# assign specified vbase to corresponding zones
Expand All @@ -55,9 +54,9 @@ function _calc_vbase(data_model, vbase_sources::Dict{String,<:Real})
push!(edges,i)
f_zone = bus_to_zone[trans["f_bus"]]
t_zone = bus_to_zone[trans["t_bus"]]
vnom = trans["vnom"]
push!(zone_edges[f_zone], (i, t_zone, 1/vnom))
push!(zone_edges[t_zone], (i, f_zone, vnom))
tm_nom = trans["tm_nom"]
push!(zone_edges[f_zone], (i, t_zone, 1/tm_nom))
push!(zone_edges[t_zone], (i, f_zone, tm_nom))
end

# initialize the stack with all specified zones
Expand All @@ -83,57 +82,161 @@ function _calc_vbase(data_model, vbase_sources::Dict{String,<:Real})
end


function _make_pu!(data_model, sbase=1E3)
bus_vbase, line_vbase = calc_vbase(data_model, Dict("1"=>230.0))
function make_pu!(data_model; sbase=1E3, vbases=missing)

sbase_old = get(data_model, "sbase", 1.0)
sbase_scale = sbase_old/sbase

# automatically find a good vbase
if ismissing(vbases)
buses_vnom = [(id, bus["vnom"]) for (id,bus) in data_model["bus"] if haskey(bus, "vnom")]
if !isempty(buses_vnom)
vbases = Dict([buses_vnom[1]])
else
Memento.error("Please specify vbases manually; cannot make an educated guess for this data model.")
end
end

bus_vbase, line_vbase = _calc_vbase(data_model, vbases)

for (id, bus) in data_model["bus"]
make_pu_bus!(bus, bus_vbase[id], sbase)
_rebase_pu_bus!(bus, bus_vbase[id], sbase_scale)
end

for (id, line) in data_model["line"]
make_pu_line!(line, line_vbase[id], sbase)
vbase = line_vbase[id]
_make_pu_line!(line, line_vbase[id], sbase)
end

for (id, shunt) in data_model["shunt"]
make_pu_shunt!(shunt, bus_vbase[shunt["bus"]], sbase)
_make_pu_shunt!(shunt, bus_vbase[shunt["bus"]], sbase_scale)
end

for (id, load) in data_model["load"]
make_pu_load!(load, bus_vbase[load["bus"]], sbase)
_make_pu_load!(load, bus_vbase[load["bus"]], sbase_scale)
end

for (id, gen) in data_model["generator"]
make_pu_generator!(gen, bus_vbase[gen["bus"]], sbase)
_make_pu_generator!(gen, bus_vbase[gen["bus"]], sbase_scale)
end

for (id, trans) in data_model["transformer_2w3ph_ideal"]
make_pu_transformer_2w3ph_ideal!(trans, bus_vbase[trans["bus"]], sbase)
# voltage base across transformer does not have to be consistent with the ratio!
f_vbase = bus_vbase[trans["f_bus"]]
t_vbase = bus_vbase[trans["t_bus"]]
_make_pu_transformer_2w3ph_ideal!(trans, f_vbase, t_vbase, sbase)
end

return data_model
end


function _make_pu_bus!(data_model, vbase, sbase)
function _rebase_pu_bus!(bus, vbase, sbase_scale)
vbase_old = get(bus, "vbase", 1.0)
vbase_scale = vbase_old/vbase

vbounds = ["vm_ln_min", "vm_ln_max", "vm_lg_min", "vm_lg_max", "vm_ng_min", "vm_ng_max", "vm_ll_min", "vm_ll_max"]

# if no vbase set, first remove normalization with respect to vnom, to implied base of 1V
if !haskey(bus, "vbase")
if any([haskey(bus, x) for x in vbounds])
vnom = bus["vnom"]
for key in vbounds
scale(bus, key, vnom)
end
end
end

# now renormalize vbase_old->vbase
for key in [vbounds..., "vm_set", "vnom"]
scale(bus, key, vbase_old/vbase)
end

# rebase grounding resistance
z_scale = (vbase_scale)^2/sbase_scale
scale(bus, "rg", z_scale)
scale(bus, "xg", z_scale)

# save new vbase
bus["vbase"] = vbase
end


function _make_pu_line!(data_model, vbase, sbase)
function _make_pu_line!(line, vbase, sbase_scale)
vbase_old = get(line, "vbase", 1.0)
vbase_scale = vbase_old/vbase

# rebase grounding resistance
z_scale = (vbase_scale)^2/sbase_scale
y_scale = 1/z_scale
scale(line, "rs", z_scale)
scale(line, "xs", z_scale)
for key in ["b_fr", "g_fr", "b_to", "g_to"]
scale(line, key, y_scale)
end

# save new vbase
line["vbase"] = vbase
end


function _make_pu_shunt!(data_model, vbase, sbase)
function _make_pu_shunt!(shunt, vbase, sbase_scale)
vbase_old = get(shunt, "vbase", 1.0)
vbase_scale = vbase_old/vbase

# rebase grounding resistance
z_scale = (vbase_scale)^2/sbase_scale
y_scale = 1/z_scale
scale(shunt, "g_sh", y_scale)
scale(shunt, "b_sh", y_scale)

# save new vbase
shunt["vbase"] = vbase
end


function _make_pu_load!(data_model, vbase, sbase)
function _make_pu_load!(load, vbase, sbase_scale)
vbase_old = get(load, "vbase", 1.0)
vbase_scale = vbase_old/vbase

scale(load, "vm_nom", vbase_scale)

scale(load, "pd", sbase_scale)
scale(load, "qd", sbase_scale)

# save new vbase
load["vbase"] = vbase
end


function _make_pu_generator!(data_model, vbase, sbase)
function _make_pu_generator!(gen, vbase, sbase_scale)
vbase_old = get(gen, "vbase", 1.0)
vbase_scale = vbase_old/vbase

for key in ["pd_set", "qd_set", "pd_min", "qd_min", "pd_max", "qd_max"]
scale(gen, key, sbase_scale)
end

# save new vbase
gen["vbase"] = vbase
end


function _make_pu_transformer_2w3ph_ideal!(data_model, vbase, sbase)
function _make_pu_transformer_2w3ph_ideal!(trans, f_vbase, t_vbase, sbase_scale)
f_vbase_old = get(trans, "f_vbase", 1.0)
t_vbase_old = get(trans, "t_vbase", 1.0)
f_vbase_scale = f_vbase_old/f_vbase
t_vbase_scale = t_vbase_old/t_vbase

scale(trans, "tm_nom", f_vbase_scale/t_vbase_scale)

# save new vbase
trans["f_vbase"] = f_vbase
trans["t_vbase"] = t_vbase
end

data_model_user = make_test_data_model()
data_model_base = map_down_data_model(data_model_user)
#bus_vbase, line_vbase = get_vbase(data_model_base, Dict("1"=>230.0))

bus_vbase, line_vbase = get_vbase(data_model_base, Dict("1"=>230.0))
make_pu!(data_model_base)
Loading

0 comments on commit 0e7358f

Please sign in to comment.