Skip to content

Commit

Permalink
generators added and lag 2 transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
jtabarez committed Feb 20, 2024
1 parent 488319c commit 498f8f3
Show file tree
Hide file tree
Showing 12 changed files with 265 additions and 610 deletions.
40 changes: 40 additions & 0 deletions src/core/admittance_matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,39 @@ function build_mc_delta_current_vector(data, v, z_matrix)
(n, m) = size(v)
delta_i = zeros(Complex{Float64}, n, 1)
build_mc_delta_current_load!(delta_i, v, data)
build_mc_delta_current_generator!(delta_i, v, data)
build_mc_delta_current_inverter!(delta_i, v, data, z_matrix)
return _SP.sparse(delta_i)
end


function build_mc_delta_current_generator!(delta_i, v, data)
for (_, gen) in data["gen"]
if occursin("generator", gen["source_id"])
if gen["gen_model"] == 1
calc_delta_current_generator!(gen, delta_i, v, data)
end
end
end
end


function calc_delta_current_generator!(gen, delta_i, v, data)
bus = gen["gen_bus"]
if occursin("generator", gen["source_id"])
if gen["gen_model"] == 1
for (_j, j) in enumerate(gen["connections"])
if haskey(data["admittance_map"], (bus, j))
s = -(gen["pg"][_j] + 1im * gen["qg"][_j])
y = conj(s) / gen["vnom_kv"]^2 / 1000
delta_i[data["admittance_map"][(bus, j)], 1] += conj(s * data["settings"]["power_scale_factor"] / v[data["admittance_map"][(bus, j)], 1]) - y * v[data["admittance_map"][(bus, j)], 1]
end
end
end
end
end


function build_mc_delta_current_load!(delta_i, v, data)
for (_, load) in data["load"]
if load["model"] == _PMD.POWER
Expand Down Expand Up @@ -565,11 +593,23 @@ end
function update_mc_delta_current_vector(model, v)
(n, m) = size(v)
delta_i = zeros(Complex{Float64}, n, 1)
update_mc_delta_current_generator!(delta_i, v, model.data)
update_mc_delta_current_load!(delta_i, v, model.data)
return _SP.sparse(delta_i)
end


function update_mc_delta_current_generator!(delta_i, v, data)
for (_, gen) in data["gen"]
if occursin("generator", gen["source_id"])
if gen["gen_model"] == 1
calc_delta_current_generator!(gen, delta_i, v, data)
end
end
end
end


function update_mc_delta_current_load!(delta_i, v, data)
for (_, load) in data["load"]
if data["settings"]["loading"]
Expand Down
184 changes: 105 additions & 79 deletions src/data_model/components.jl
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,34 @@ function _map_eng2math_mc_admittance_voltage_source!(data_math::Dict{String,<:An
end


function _map_eng2math_mc_admittance_generator!(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("generator", gen["source_id"])
if gen["gen_model"] == 1
y = zeros(Complex{Float64}, 4, 4)
for (i, pg) in enumerate(gen["pg"])
kv = gen["vnom_kv"]
s = -(pg + 1im * gen["qg"][i])
y_ = conj(s) / kv^2 / 1000
y[i,i] += y_
y[i,4] -= y[i,i]
y[4,i] -= y[i,4]
y[4,4] += y[i,i]
end
end
gen["p_matrix"] = y
end
end
end
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"])
n = length(gen["connections"])
y = zeros(n, n)
if gen["configuration"] == _PMD.WYE
for (i, connection) in enumerate(gen["connections"])
Expand Down Expand Up @@ -436,90 +459,93 @@ function _map_eng2math_mc_admittance_2w_transformer!(transformer::Dict{String,<:
(2,1) => [3,2],
(2,2) => [7,4],
(2,3) => [11,6]
)

if transformer["dss"]["phases"] == 3
z = sum(transformer["rw"]) + 1im .* transformer["xsc"][1]
z_1volt= z * 3/transformer["sm_nom"][1]/1000
z_b = [z_1volt 0 0;0 z_1volt 0;0 0 z_1volt]
b = [1 0 0;-1 0 0;0 1 0;0 -1 0;0 0 1;0 0 -1]
y1 = b*inv(z_b)*transpose(b)
n = zeros(Float64, 12, 6)
a = zeros(Int64,8,12)
for w = 1:2
if transformer["configuration"][w] == _PMD.WYE
w == 1 ? connections = transformer["f_connections"] : connections = transformer["t_connections"]
for (_,k) in enumerate(connections)
if haskey(lookup, (w,k))
i = lookup[(w,k)][1]
j = lookup[(w,k)][2]
n[i,j] = 1/(transformer["tm_nom"][w]/sqrt(3)*1000*transformer["tm_set"][w][k])
n[i+1,j] = - n[i,j]
end
end
if w == 1
a[1,1] = a[2,5] = a[3,9] = a[4,2] = a[4,6] = a[4,10] = 1
else
a[5,3] = a[6,7] = a[7,11] = a[8,4] = a[8,8] = a[8,12] = 1
end
elseif transformer["configuration"][w] == _PMD.DELTA
w == 1 ? connections = transformer["f_connections"] : connections = transformer["t_connections"]
for (_,k) in enumerate(connections)
if haskey(lookup, (w,k))
i = lookup[(w,k)][1]
j = lookup[(w,k)][2]
n[i,j] = 1/(transformer["tm_nom"][w]*1000*transformer["tm_set"][w][k])
n[i+1,j] = - n[i,j]
end
end
if w == 1
a[1,1] = a[1,6] = a[2,5] = a[2,10] = a[3,9] = a[3,2] = 1
else
a[5,3] = a[6,7] = a[7,11] = a[8,4] = a[8,8] = a[8,12] = 1 # wrong
end
)
if transformer["dss"]["phases"] == 3
z = sum(transformer["rw"]) + 1im .* transformer["xsc"][1]
z_1volt= z * 3/transformer["sm_nom"][1]/1000
z_b = [z_1volt 0 0;0 z_1volt 0;0 0 z_1volt]
b = [1 0 0;-1 0 0;0 1 0;0 -1 0;0 0 1;0 0 -1]
y1 = b*inv(z_b)*transpose(b)
n = zeros(Float64, 12, 6)
a = zeros(Int64,8,12)
for w = 1:2
if transformer["configuration"][w] == _PMD.WYE
w == 1 ? connections = transformer["f_connections"] : connections = transformer["t_connections"]
for (_,k) in enumerate(connections)
if haskey(lookup, (w,k))
i = lookup[(w,k)][1]
j = lookup[(w,k)][2]
n[i,j] = 1/(transformer["tm_nom"][w]/sqrt(3)*1000*transformer["tm_set"][w][k])
n[i+1,j] = - n[i,j]
end
end
y_w = n*y1*transpose(n)
p_matrix = a*y_w*transpose(a)

ybase = (transformer["sm_nom"][1]/3) / (transformer["tm_nom"][2]*transformer["tm_set"][2][1]/sqrt(3))^2 /1000
if haskey(transformer["dss"], "%noloadloss")
shunt = (transformer["g_sh"] + 1im * transformer["b_sh"])*ybase
p_matrix[5,5] += shunt
p_matrix[5,8] -= shunt
p_matrix[6,6] += shunt
p_matrix[6,8] -= shunt
p_matrix[7,7] += shunt
p_matrix[7,8] -= shunt
p_matrix[8,5] -= shunt
p_matrix[8,6] -= shunt
p_matrix[8,7] -= shunt
p_matrix[8,8] += 3*shunt
if w == 1
a[1,1] = a[2,5] = a[3,9] = a[4,2] = a[4,6] = a[4,10] = 1
else
a[5,3] = a[6,7] = a[7,11] = a[8,4] = a[8,8] = a[8,12] = 1
end
transformer["p_matrix"] = p_matrix
elseif transformer["dss"]["phases"] == 1
z = sum(transformer["rw"]) + 1im .* transformer["xsc"][1]
z_1volt= z * 1/transformer["sm_nom"][1]/1000
b = [1 ;-1]
y1 = b*1/z_1volt*transpose(b)
n = zeros(Float64, 4, 2)
a = zeros(Int64,4,4)
for w = 1:2
if transformer["configuration"][w] == _PMD.WYE
i = lookup[(w,1)][1]
j = lookup[(w,1)][2]
n[i,j] = 1/(transformer["tm_nom"][w]*1000*transformer["tm_set"][w][1])
elseif transformer["configuration"][w] == _PMD.DELTA
w == 1 ? connections = transformer["f_connections"] : connections = transformer["t_connections"]
for (_,k) in enumerate(connections)
if haskey(lookup, (w,k))
i = lookup[(w,k)][1]
j = lookup[(w,k)][2]
n[i,j] = 1/(transformer["tm_nom"][w]*1000*transformer["tm_set"][w][k])
n[i+1,j] = - n[i,j]
if w == 1
a[1,1] = a[2,2] = 1
else
a[3,3] = a[4,4] = 1
end
end
end
y_w = n*y1*transpose(n)
p_matrix = a*y_w*transpose(a)
transformer["p_matrix"] = p_matrix
if w == 1
if transformer["leadlag"] == "lag"
a[1,1] = a[1,6] = a[2,5] = a[2,10] = a[3,9] = a[3,2] = 1
else
a[1,1] = a[1,10] = a[2,2] = a[2,5] = a[3,6] = a[3,9] = 1
end
else
a[5,3] = a[6,7] = a[7,11] = a[8,4] = a[8,8] = a[8,12] = 1 # wrong
end
end
end
y_w = n*y1*transpose(n)
p_matrix = a*y_w*transpose(a)

ybase = (transformer["sm_nom"][1]/3) / (transformer["tm_nom"][2]*transformer["tm_set"][2][1]/sqrt(3))^2 /1000
if haskey(transformer["dss"], "%noloadloss")
shunt = (transformer["g_sh"] + 1im * transformer["b_sh"])*ybase
p_matrix[5,5] += shunt
p_matrix[5,8] -= shunt
p_matrix[6,6] += shunt
p_matrix[6,8] -= shunt
p_matrix[7,7] += shunt
p_matrix[7,8] -= shunt
p_matrix[8,5] -= shunt
p_matrix[8,6] -= shunt
p_matrix[8,7] -= shunt
p_matrix[8,8] += 3*shunt
end
transformer["p_matrix"] = p_matrix
elseif transformer["dss"]["phases"] == 1
z = sum(transformer["rw"]) + 1im .* transformer["xsc"][1]
z_1volt= z * 1/transformer["sm_nom"][1]/1000
b = [1 ;-1]
y1 = b*1/z_1volt*transpose(b)
n = zeros(Float64, 4, 2)
a = zeros(Int64,4,4)
for w = 1:2
if transformer["configuration"][w] == _PMD.WYE
i = lookup[(w,1)][1]
j = lookup[(w,1)][2]
n[i,j] = 1/(transformer["tm_nom"][w]*1000*transformer["tm_set"][w][1])
n[i+1,j] = - n[i,j]
if w == 1
a[1,1] = a[2,2] = 1
else
a[3,3] = a[4,4] = 1
end
end
end
y_w = n*y1*transpose(n)
p_matrix = a*y_w*transpose(a)
transformer["p_matrix"] = p_matrix
end
end

Expand Down
4 changes: 2 additions & 2 deletions src/data_model/eng2math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ 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"],
"generator" => String["zr", "zx", "grid_forming", "gen_model", "xdp", "rp", "xdpp", "vnom_kv"],
"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"],
Expand All @@ -180,7 +180,7 @@ transform_data_model(

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

"custom version of 'transform_data_model' to build admittance model and deal with transformers"
Expand Down
46 changes: 35 additions & 11 deletions src/io/dss/dss2eng.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,43 @@ end
function _dss2eng_gen_dynamics!(data_eng::Dict{String,<:Any}, data_dss::Dict{String,<:Any})
if haskey(data_eng, "generator")
for (id, generator) in data_eng["generator"]
dss_obj = data_dss["generator"][id]
_PMD._apply_like!(dss_obj, data_dss, "generator")
defaults = _PMD._apply_ordered_properties(_PMD._create_generator(id; _PMD._to_kwargs(dss_obj)...), dss_obj)
zbase = defaults["kv"]^2/defaults["kva"]*1000
xdp = defaults["xdp"] * zbase
rp = xdp/defaults["xrdp"]
xdpp = defaults["xdpp"] * zbase
generator["xdp"] = fill(xdp, length(generator["connections"]))
generator["rp"] = fill(rp, length(generator["connections"]))
generator["xdpp"] = fill(xdpp, length(generator["connections"]))
if haskey(generator["dss"], "model")
if generator["dss"]["model"] == 3
dss_obj = data_dss["generator"][id]
_PMD._apply_like!(dss_obj, data_dss, "generator")
defaults = _PMD._apply_ordered_properties(_PMD._create_generator(id; _PMD._to_kwargs(dss_obj)...), dss_obj)
zbase = defaults["kv"]^2/defaults["kva"]*1000
xdp = defaults["xdp"] * zbase
rp = xdp/defaults["xrdp"]
xdpp = defaults["xdpp"] * zbase
generator["xdp"] = fill(xdp, length(generator["connections"]))
generator["rp"] = fill(rp, length(generator["connections"]))
generator["xdpp"] = fill(xdpp, length(generator["connections"]))
model = generator["dss"]["model"]
else
model = 1
end
generator["gen_model"] = model
if model == 1
if haskey(generator["dss"], "kvar")
generator["qg"] = fill(generator["dss"]["kvar"]/length(generator["pg"]), length(generator["pg"]))
else
generator["qg"] = fill(0.0, length(generator["pg"]))
end
if haskey(generator["dss"], "kv")
generator["vnom_kv"] = generator["dss"]["kv"] / sqrt(3)
end
# if generator["dss"]["model"] == 3
# dss_obj = data_dss["generator"][id]
# _PMD._apply_like!(dss_obj, data_dss, "generator")
# defaults = _PMD._apply_ordered_properties(_PMD._create_generator(id; _PMD._to_kwargs(dss_obj)...), dss_obj)
# zbase = defaults["kv"]^2/defaults["kva"]*1000
# xdp = defaults["xdp"] * zbase
# rp = xdp/defaults["xrdp"]
# xdpp = defaults["xdpp"] * zbase
# generator["xdp"] = fill(xdp, length(generator["connections"]))
# generator["rp"] = fill(rp, length(generator["connections"]))
# generator["xdpp"] = fill(xdpp, length(generator["connections"]))
# end
end
end
end
Expand Down
Loading

0 comments on commit 498f8f3

Please sign in to comment.