Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flow and Balance Computations from Data #488

Merged
merged 4 commits into from
Apr 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ PowerModels.jl Change Log
=========================

### Staged
- Added calc_power_balance function for computing bus balance from the data model
- Added calc_branch_flow functions for computing flows from the data model
- Improved starting point of piecewise linear cost function variables
- Added Base.norm for multiconductorvectors
- Minor fix to multiconductor broadcasting
Expand Down
278 changes: 276 additions & 2 deletions src/core/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ end


"prints the text summary for a data file or dictionary to stdout"
function print_summary(obj::Union{String, Dict{String,Any}}; kwargs...)
function print_summary(obj::Union{String, Dict{String,<:Any}}; kwargs...)
summary(stdout, obj; kwargs...)
end

Expand Down Expand Up @@ -516,14 +516,288 @@ function _rescale_cost_model(comp::Dict{String,<:Any}, scale::Real)
end





"assumes a vaild ac solution is included in the data and computes the branch flow values"
function calc_branch_flow_ac(data::Dict{String,<:Any})
@assert("per_unit" in keys(data) && data["per_unit"])
@assert(!haskey(data, "conductors"))

if InfrastructureModels.ismultinetwork(data)
nws = Dict{String,Any}()
for (i,nw_data) in data["nw"]
nws[i] = _calc_branch_flow_ac(nw_data)
end
return Dict{String,Any}(
"nw" => nws,
"per_unit" => data["per_unit"],
"baseMVA" => data["baseMVA"]
)
else
flows = _calc_branch_flow_ac(data)
flows["per_unit"] = data["per_unit"]
flows["baseMVA"] = data["baseMVA"]
return flows
end
end


"helper function for calc_branch_flow_ac"
function _calc_branch_flow_ac(data::Dict{String,<:Any})
vm = Dict(bus["index"] => bus["vm"] for (i,bus) in data["bus"])
va = Dict(bus["index"] => bus["va"] for (i,bus) in data["bus"])

flows = Dict{String,Any}()
for (i,branch) in data["branch"]
if branch["br_status"] != 0
f_bus = branch["f_bus"]
t_bus = branch["t_bus"]

g, b = calc_branch_y(branch)
tr, ti = calc_branch_t(branch)
g_fr = branch["g_fr"]
b_fr = branch["b_fr"]
g_to = branch["g_to"]
b_to = branch["b_to"]

tm = branch["tap"]

vm_fr = vm[f_bus]
vm_to = vm[t_bus]
va_fr = va[f_bus]
va_to = va[t_bus]

p_fr = (g+g_fr)/tm^2*vm_fr^2 + (-g*tr+b*ti)/tm^2*(vm_fr*vm_to*cos(va_fr-va_to)) + (-b*tr-g*ti)/tm^2*(vm_fr*vm_to*sin(va_fr-va_to))
q_fr = -(b+b_fr)/tm^2*vm_fr^2 - (-b*tr-g*ti)/tm^2*(vm_fr*vm_to*cos(va_fr-va_to)) + (-g*tr+b*ti)/tm^2*(vm_fr*vm_to*sin(va_fr-va_to))

p_to = (g+g_to)*vm_to^2 + (-g*tr-b*ti)/tm^2*(vm_to*vm_fr*cos(va_to-va_fr)) + (-b*tr+g*ti)/tm^2*(vm_to*vm_fr*sin(va_to-va_fr))
q_to = -(b+b_to)*vm_to^2 - (-b*tr+g*ti)/tm^2*(vm_to*vm_fr*cos(va_to-va_fr)) + (-g*tr-b*ti)/tm^2*(vm_to*vm_fr*sin(va_to-va_fr))
else
p_fr = NaN
q_fr = NaN

p_to = NaN
q_to = NaN
end

flows[i] = Dict(
"pf" => p_fr,
"qf" => q_fr,
"pt" => p_to,
"qt" => q_to
)
end

return Dict{String,Any}("branch" => flows)
end



"assumes a vaild dc solution is included in the data and computes the branch flow values"
function calc_branch_flow_dc(data::Dict{String,<:Any})
@assert("per_unit" in keys(data) && data["per_unit"])
@assert(!haskey(data, "conductors"))

if InfrastructureModels.ismultinetwork(data)
nws = Dict{String,Any}()
for (i,nw_data) in data["nw"]
nws[i] = _calc_branch_flow_dc(nw_data)
end
return Dict{String,Any}(
"nw" => nws,
"per_unit" => data["per_unit"],
"baseMVA" => data["baseMVA"]
)
else
flows = _calc_branch_flow_dc(data)
flows["per_unit"] = data["per_unit"]
flows["baseMVA"] = data["baseMVA"]
return flows
end
end


"helper function for calc_branch_flow_dc"
function _calc_branch_flow_dc(data::Dict{String,<:Any})
vm = Dict(bus["index"] => bus["vm"] for (i,bus) in data["bus"])
va = Dict(bus["index"] => bus["va"] for (i,bus) in data["bus"])

flows = Dict{String,Any}()
for (i,branch) in data["branch"]
if branch["br_status"] != 0
f_bus = branch["f_bus"]
t_bus = branch["t_bus"]

g, b = calc_branch_y(branch)

p_fr = -b*(va[f_bus] - va[t_bus])
else
p_fr = NaN
end

flows[i] = Dict(
"pf" => p_fr,
"qf" => NaN,
"pt" => -p_fr,
"qt" => NaN
)
end

return Dict{String,Any}("branch" => flows)
end




"assumes a vaild solution is included in the data and computes the power balance at each bus"
function calc_power_balance(data::Dict{String,<:Any})
@assert("per_unit" in keys(data) && data["per_unit"]) # may not be strictly required
@assert(!haskey(data, "conductors"))

if InfrastructureModels.ismultinetwork(data)
nws = Dict{String,Any}()
for (i,nw_data) in data["nw"]
nws[i] = _calc_power_balance(nw_data)
end
return Dict{String,Any}(
"nw" => nws,
"per_unit" => data["per_unit"],
"baseMVA" => data["baseMVA"]
)
else
flows = _calc_power_balance(data)
flows["per_unit"] = data["per_unit"]
flows["baseMVA"] = data["baseMVA"]
return flows
end
end


"helper function for calc_power_balance"
function _calc_power_balance(data::Dict{String,<:Any})
bus_values = Dict(bus["index"] => Dict{String,Float64}() for (i,bus) in data["bus"])
for (i,bus) in data["bus"]
bvals = bus_values[bus["index"]]
bvals["vm"] = bus["vm"]

bvals["pd"] = 0.0
bvals["qd"] = 0.0

bvals["gs"] = 0.0
bvals["bs"] = 0.0

bvals["ps"] = 0.0
bvals["qs"] = 0.0

bvals["pg"] = 0.0
bvals["qg"] = 0.0

bvals["p"] = 0.0
bvals["q"] = 0.0

bvals["p_dc"] = 0.0
bvals["q_dc"] = 0.0
end

for (i,load) in data["load"]
if load["status"] != 0
bvals = bus_values[load["load_bus"]]
bvals["pd"] += load["pd"]
bvals["qd"] += load["qd"]
end
end

for (i,shunt) in data["shunt"]
if shunt["status"] != 0
bvals = bus_values[shunt["shunt_bus"]]
bvals["gs"] += shunt["gs"]
bvals["bs"] += shunt["bs"]
end
end

for (i,storage) in data["storage"]
if storage["status"] != 0
bvals = bus_values[storage["storage_bus"]]
if haskey(storage, "ps")
bvals["ps"] += storage["ps"]
end
if haskey(storage, "qs")
bvals["qs"] += storage["qs"]
end
end
end

for (i,gen) in data["gen"]
if gen["gen_status"] != 0
bvals = bus_values[gen["gen_bus"]]
bvals["pg"] += gen["pg"]
bvals["qg"] += gen["qg"]
end
end

for (i,branch) in data["branch"]
if branch["br_status"] != 0
bus_fr = branch["f_bus"]
bvals_fr = bus_values[bus_fr]
bvals_fr["p"] += branch["pf"]
bvals_fr["q"] += branch["qf"]

bus_to = branch["t_bus"]
bvals_to = bus_values[bus_to]
bvals_to["p"] += branch["pt"]
bvals_to["q"] += branch["qt"]
end
end

for (i,dcline) in data["dcline"]
if dcline["br_status"] != 0
bus_fr = dcline["f_bus"]
bvals_fr = bus_values[bus_fr]
bvals_fr["p_dc"] += dcline["pf"]
bvals_fr["q_dc"] += dcline["qf"]

bus_to = dcline["t_bus"]
bvals_to = bus_values[bus_to]
bvals_to["p_dc"] += dcline["pt"]
bvals_to["q_dc"] += dcline["qt"]
end
end

deltas = Dict{String,Any}()
for (i,bus) in data["bus"]
if bus["bus_type"] != 4
bvals = bus_values[bus["index"]]
p_delta = bvals["p"] + bvals["p_dc"] - bvals["pg"] + bvals["ps"] + bvals["pd"] + bvals["gs"]*(bvals["vm"]^2)
q_delta = bvals["q"] + bvals["q_dc"] - bvals["qg"] + bvals["qs"] + bvals["qd"] - bvals["bs"]*(bvals["vm"]^2)
else
p_delta = NaN
q_delta = NaN
end

deltas[i] = Dict(
"p_delta" => p_delta,
"q_delta" => q_delta,
)
end

return Dict{String,Any}("bus" => deltas)
end







""
function check_conductors(data::Dict{String,<:Any})
if InfrastructureModels.ismultinetwork(data)
for (i,nw_data) in data["nw"]
_check_conductors(nw_data)
end
else
_check_conductors(data)
_check_conductors(data)
end
end

Expand Down
4 changes: 2 additions & 2 deletions src/io/matpower.jl
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ end
"""
Converts a Matpower dict into a PowerModels dict
"""
function matpower_to_powermodels(mp_data::Dict{String,Any})
function matpower_to_powermodels(mp_data::Dict{String,<:Any})
pm_data = deepcopy(mp_data)

# required default values
Expand Down Expand Up @@ -929,7 +929,7 @@ function export_matpower(io::IO, data::Dict{String,Any})
end

"Export fields of a component type"
function export_extra_data(io::IO, data::Dict{String,Any}, component, excluded_fields=Set(["index"]); postfix="")
function export_extra_data(io::IO, data::Dict{String,<:Any}, component, excluded_fields=Set(["index"]); postfix="")
if isa(data[component], Int) || isa(data[component], Int64) || isa(data[component], Float64)
println(io, "mpc.", component, " = ", data[component], ";")
println(io)
Expand Down
Loading