Skip to content

Commit

Permalink
gfli and gfmi models added
Browse files Browse the repository at this point in the history
  • Loading branch information
jtabarez committed Feb 13, 2024
1 parent 28a9e81 commit 488319c
Show file tree
Hide file tree
Showing 14 changed files with 1,191 additions and 120 deletions.
400 changes: 349 additions & 51 deletions src/core/admittance_matrix.jl

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions src/core/solution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ function build_output_schema!(output::Dict{String,Any}, v::SparseArrays.SparseMa
for j = 1:3
v_bus[j,1] = v[data["admittance_map"][(bus["bus_i"],j)],1]
end
i = fault*v_bus.*1000
i = fault*v_bus
i012 = get_current_sequence(i,connections)
obj = Dict{String,Any}(
"fault" => Dict{String,Any}(
Expand Down Expand Up @@ -304,7 +304,7 @@ function build_output_schema!(output::Dict{String,Any}, v::SparseArrays.SparseMa
v_t_bus[_j,1] = v[data["admittance_map"][(t_bus["bus_i"], j)],1]
end
end
i_line = y_line * (v_f_bus - v_t_bus) * 1000
i_line = y_line * (v_f_bus - v_t_bus)
i012 = get_current_sequence(i_line,branch["f_connections"])
branch_obj = Dict{String,Any}(
"|I| (A)" => abs.(i_line),
Expand Down Expand Up @@ -336,7 +336,7 @@ function build_output_schema!(output::Dict{String,Any}, v::SparseArrays.SparseMa
for j =1:2
v_bus[j,1] = v[data["admittance_map"][(bus["bus_i"],connections[j])],1]
end
i = fault*v_bus.*1000
i = fault*v_bus
i012 = get_current_sequence(i,connections)
obj = Dict{String,Any}(
"fault" => Dict{String,Any}(
Expand Down Expand Up @@ -375,7 +375,7 @@ function build_output_schema!(output::Dict{String,Any}, v::SparseArrays.SparseMa
v_t_bus[_j,1] = v[data["admittance_map"][(t_bus["bus_i"], j)],1]
end
end
i_line = y_line * (v_f_bus - v_t_bus) * 1000
i_line = y_line * (v_f_bus - v_t_bus)
i012 = get_current_sequence(i_line,branch["f_connections"])
branch_obj = Dict{String,Any}(
"|I| (A)" => abs.(i_line),
Expand Down Expand Up @@ -405,7 +405,7 @@ function build_output_schema!(output::Dict{String,Any}, v::SparseArrays.SparseMa
connections = [bus["terminals"][indx]]
v_bus = zeros(Complex{Float64},2)
v_bus[1,1] = v[data["admittance_map"][(bus["bus_i"],connections[1])],1]
i = fault*v_bus.*1000
i = fault*v_bus
i012 = get_current_sequence(i,connections)
obj = Dict{String,Any}(
"fault" => Dict{String,Any}(
Expand Down Expand Up @@ -443,7 +443,7 @@ function build_output_schema!(output::Dict{String,Any}, v::SparseArrays.SparseMa
v_t_bus[_j,1] = v[data["admittance_map"][(t_bus["bus_i"], j)],1]
end
end
i_line = y_line * (v_f_bus - v_t_bus) * 1000
i_line = y_line * (v_f_bus - v_t_bus)
i012 = get_current_sequence(i_line,branch["f_connections"])
branch_obj = Dict{String,Any}(
"|I| (A)" => abs.(i_line),
Expand Down
6 changes: 4 additions & 2 deletions src/core/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ struct AdmittanceModel
data::Dict{String,<:Any}
y::Matrix{Complex{Float64}}
z::Matrix{Complex{Float64}}
c::Matrix{Complex{Float64}}
v::Matrix{Complex{Float64}}
i::Matrix{Complex{Float64}}
end
delta_i_control::Matrix{Complex{Float64}}
delta_i::Matrix{Complex{Float64}}
end

58 changes: 41 additions & 17 deletions src/data_model/components.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Creates a fault dictionary given the `type` of fault, i.e., one of "3p", "ll", "
the `connections` on which the fault applies, the `resistance` between the phase and ground, in the case of "lg", or phase and phase.
"""
function create_fault(buses::Dict{String,Any})::Dict{String,Any}
phase_resistance = 1e-6
ground_resistance = 1e-6
phase_resistance = 1e-4
ground_resistance = 1e-4

Gf_3p = zeros(Real, 3, 3)
gp = 1 / phase_resistance
Expand Down Expand Up @@ -53,22 +53,24 @@ function create_fault(buses::Dict{String,Any})::Dict{String,Any}

fault = Dict{String,Any}()
for (indx,bus) in buses
fault[indx] = Dict{String,Any}()
if length(bus["terminals"]) > 3
fault[indx]["3pg"] = Gf_3p
elseif length(bus["terminals"]) == 3 && !(4 in bus["terminals"])
fault[indx]["3pg"] = Gf_3p
end
fault[indx]["lg"] = Dict{Int,Any}()
fault[indx]["ll"] = Dict{Tuple,Any}()
for i = 1:length(bus["terminals"])
if bus["grounded"][i] == 0
for j = i:length(bus["terminals"])
if bus["grounded"][j] == 0 && i != j
fault[indx]["ll"][(i,j)] = Gf_ll
if !(occursin("virtual", bus["name"]))
fault[indx] = Dict{String,Any}()
if length(bus["terminals"]) > 3
fault[indx]["3pg"] = Gf_3p
elseif length(bus["terminals"]) == 3 && !(4 in bus["terminals"])
fault[indx]["3pg"] = Gf_3p
end
fault[indx]["lg"] = Dict{Int,Any}()
fault[indx]["ll"] = Dict{Tuple,Any}()
for i = 1:length(bus["terminals"])
if bus["grounded"][i] == 0
for j = i:length(bus["terminals"])
if bus["grounded"][j] == 0 && i != j
fault[indx]["ll"][(i,j)] = Gf_ll
end
end
fault[indx]["lg"][i] = Gf_lg
end
fault[indx]["lg"][i] = Gf_lg
end
end
end
Expand Down Expand Up @@ -354,6 +356,28 @@ function _map_eng2math_mc_admittance_voltage_source!(data_math::Dict{String,<:An
end


function _map_eng2math_mc_admittance_solar!(data_math::Dict{String,<:Any}, data_eng::Dict{String,<:Any}; pass_props::Vector{String}=String[])
if haskey(data_math, "gen")
for (name, gen) in data_math["gen"]
if occursin("solar", gen["source_id"])
n = length(gen["connections"])
y = zeros(n, n)
if gen["configuration"] == _PMD.WYE
for (i, connection) in enumerate(gen["connections"])
j = findall(x->x==4, gen["connections"])[1]
if connection != 4
y[i,i] = 1/1e6
y[j,j] = y[i,i]
end
end
end
gen["p_matrix"] = y
end
end
end
end


function _map_eng2math_mc_admittance_load!(data_math::Dict{String,<:Any}, data_eng::Dict{String,<:Any}; pass_props::Vector{String}=String[])
if haskey(data_math, "load")
for (name, load) in data_math["load"]
Expand All @@ -364,7 +388,7 @@ function _map_eng2math_mc_admittance_load!(data_math::Dict{String,<:Any}, data_e
for (j,_j) in enumerate(load["connections"])
if _i != 4 && _j == 4
s = conj.(load["pd"][i] + 1im .* load["qd"][i])
_y = s / load["vnom_kv"]^2 / 1000
_y = s / (load["vnom_kv"])^2 / 1000
y[i,i] += _y
y[i,j] -= _y
y[j,i] -= _y
Expand Down
17 changes: 8 additions & 9 deletions src/data_model/eng2math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,10 @@ end
"field/values to passthrough from the ENGINEERING to MATHEMATICAL data models"
const _pmp_eng2math_passthrough = Dict{String,Vector{String}}(
"generator" => String["zr", "zx", "grid_forming"],
"solar" => String["i_max", "solar_max", "kva", "pf", "grid_forming"],
"voltage_source" => String["zr", "zx", "grid_forming"],
"solar" => String["i_max", "solar_max", "kva", "pf", "grid_forming", "balanced", "vminpu", "transformer", "type", "pv_model", "transformer_id"],
"voltage_source" => String["zr", "zx"],
"load" => String["vminpu", "vmaxpu"],
"transformer" => String["leadlag"]
)


Expand All @@ -178,14 +180,14 @@ transform_data_model(

"admittance model"
const _mc_admittance_asset_types = [
"line", "voltage_source", "load", "transformer", "shunt"
"line", "voltage_source", "load", "transformer", "shunt", "solar"
]

"custom version of 'transform_data_model' to build admittance model and deal with transformers"
function transform_admittance_data_model(
data::Dict{String,<:Any};
global_keys::Set{String}=Set{String}(),
eng2math_passthrough::Dict{String,<:Vector{<:String}}=Dict{String,Vector{String}}(),
eng2math_passthrough::Dict{String,<:Vector{<:String}}=_pmp_eng2math_passthrough,
eng2math_extensions::Vector{<:Function}=Function[],
make_pu::Bool=true,
make_pu_extensions::Vector{<:Function}=Function[],
Expand All @@ -199,7 +201,8 @@ function transform_admittance_data_model(
elseif data["method"] == "PMD"
data_math = _map_eng2math_mc_admittance(
data;
eng2math_extensions = eng2math_extensions,
eng2math_extensions = [_eng2math_link_transformer, eng2math_extensions...],
# eng2math_extensions = [_eng2math_gen_model!], # TODO concat eng2math_extensions
eng2math_passthrough = eng2math_passthrough,
make_pu_extensions = make_pu_extensions,
global_keys = global_keys,
Expand Down Expand Up @@ -270,10 +273,6 @@ function _map_eng2math_mc_admittance_nw!(data_math::Dict{String,<:Any}, data_eng
for type in _mc_admittance_asset_types # --> anything from missing from the model needed for the solve or admittance matrix maybe per unit to actual
getfield(PowerModelsProtection, Symbol("_map_eng2math_mc_admittance_$(type)!"))(data_math, data_eng; pass_props=get(eng2math_passthrough, type, String[]))
end

for eng2math_func! in eng2math_extensions
eng2math_func!(data_math, data_eng)
end
end


Expand Down
69 changes: 69 additions & 0 deletions src/data_model/eng2math_pmd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ const pmp_eng_asset_types = String[
"transformer", "voltage_source", "switch",
]

const pmd_math_asset_types = String[
"branch", "load", "gen"
]


function _map_eng2math_nw!(data_math::Dict{String,<:Any}, data_eng::Dict{String,<:Any}; eng2math_passthrough::Dict{String,Vector{String}}=Dict{String,Vector{String}}(), eng2math_extensions::Vector{<:Function}=Function[])
data_math["map"] = Vector{Dict{String,Any}}([
Dict{String,Any}("unmap_function" => "_map_math2eng_root!")
Expand All @@ -38,6 +43,9 @@ function _map_eng2math_nw!(data_math::Dict{String,<:Any}, data_eng::Dict{String,
getfield(PowerModelsProtection, Symbol("_map_eng2math_$(type)!"))(data_math, data_eng; pass_props=get(eng2math_passthrough, type, String[]))
end

# Custom base eng2math transformation functions for pmd assets
_add_bus_phases_key!(data_math, data_eng)

# Custom eng2math transformation functions
for eng2math_func! in eng2math_extensions
eng2math_func!(data_math, data_eng)
Expand Down Expand Up @@ -354,4 +362,65 @@ function _map_eng2math_switch!(data_math::Dict{String,<:Any}, data_eng::Dict{Str
"unmap_function" => "_map_math2eng_switch!",
))
end
end


function _add_bus_phases_key!(data_math::Dict{String,<:Any}, data_eng::Dict{String,<:Any})
for (_, bus) in data_math["bus"]
phases = 0
if !(haskey(bus, "phases"))
for (i, terminal) in enumerate(bus["terminals"])
if bus["grounded"][i]
phases += 1
end
end
end
bus["phases"] = phases
end
end


"fix the control components by adding extra data from dss model"
function _fix_control_components!(data_math::Dict{String,<:Any}, data_eng::Dict{String,<:Any})
_fix_control_load!(data_math, data_eng)
# _fix_control_transformer!(data_math, data_eng)
end


"fix load control adds vminpu from dss"
function _fix_control_load!(data_math::Dict{String,<:Any}, data_eng::Dict{String,<:Any})
if haskey(data_math, "load")
for (name, load) in data_math["load"]
vminpu = .95
vmaxpu = 1.05
vlowpu = .5
if haskey(load, "dss")
if haskey(load["dss"], "vminpu")
load["dss"]["vminpu"] >= .75 ? vminpu = load["dss"]["vminpu"] : vminpu = .75
end
haskey(load["dss"], "vmaxpu") ? vmaxpu = load["dss"]["vmaxpu"] : nothing
if haskey(load["dss"], "vlowpu")
load["dss"]["vlowpu"] < vminpu && load["dss"]["vlowpu"] >= .5 ? vlowpu = load["dss"]["vlowpu"] : nothing
end
end
end
end
end



function _eng2math_link_transformer(data_math::Dict{String,<:Any}, data_eng::Dict{String,<:Any})
if haskey(data_math, "gen")
for (name, gen) in data_math["gen"]
if haskey(gen, "transformer")
if !(gen["transformer"])
for (id, transformer) in data_math["transformer"]
if gen["transformer_id"] == transformer["name"]
gen["transformer_id"] = id
end
end
end
end
end
end
end
32 changes: 31 additions & 1 deletion src/data_model/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,34 @@ function _map_conductor_ids!(data_math::Dict{String,<:Any})
for (_,bus) in data_math["bus"]
bus["terminals"] = Vector{Int}([cnd_map[t] for t in bus["terminals"]])
end
end
end


function _convert_sparse_matrix(m::Dict{Tuple,Complex{Float64}})
rows = zeros(Int64, length(m))
columns = zeros(Int64, length(m))
values = zeros(Complex{Float64}, length(m))
indx = 1
for ((i,j), val) in m
rows[indx] = i
columns[indx] = j
values[indx] = val
indx += 1
end
return SparseArrays.sparse(rows, columns, values)
end


function _add_phases!(data)
phases = 0
if haskey(data, "connections")
phases = length(f_connections)
end
data["phases"] = phases
end


" checks if delta-gwye transformer is connected to gen "
function check_gen_transformer(data)
nothing
end
Loading

0 comments on commit 488319c

Please sign in to comment.