Skip to content

Commit

Permalink
fix blocker tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bluejuniper committed Jan 8, 2025
1 parent e3839c6 commit 29fa003
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 100 deletions.
7 changes: 7 additions & 0 deletions src/core/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,13 @@ function add_gmd_3w_branch!(data::Dict{String,<:Any})
end


function add_connected_components!(data::Dict{String,<:Any})
components = get_connected_components(data)
data["connected_components"] = components
return components
end


function get_connected_components(data::Dict{String,<:Any})
g = build_adjacency_matrix(data)
c = _Gph.SimpleGraph(g)
Expand Down
22 changes: 20 additions & 2 deletions src/core/solution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,26 @@ function solution_gmd_qloss!(pm::_PM.AbstractPowerModel, solution::Dict{String,A
for (l, branch) in _PM.ref(pm,nw_id,:branch)
key = (branch["index"], branch["hi_bus"], branch["lo_bus"])
branch_solution = nw_data["branch"][string(l)]
branch_solution["ieff"] = JuMP.value.(_PM.var(pm, nw_id, :ieff, l))
branch_solution["gmd_qloss"] = JuMP.value.(_PM.var(pm,nw_id,:qloss,key))

function set_ieff!(branch_solution)
branch_solution["ieff"] = JuMP.value.(_PM.var(pm, nw_id, :i_dc_mag, l))
end

try
set_ieff!(branch_solution)
catch
Memento.warn(_LOGGER, "Could not set ieff for branch $l")
end

function set_qloss!(branch_solution)
branch_solution["gmd_qloss"] = JuMP.value.(_PM.var(pm, nw_id, :qloss, key))
end

try
set_qloss!(branch_solution)
catch
Memento.warn(_LOGGER, "Could not set qloss for branch $l")
end
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions test/data/matpower/b4gic_15kvm_contingency.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@


%% gmd_branch data
%column_names% f_bus t_bus parent_index br_status br_r br_v len_km name
%column_names% f_bus t_bus parent_type parent_index br_status br_r br_v len_km name
mpc.gmd_branch = {
3 1 1 1 0.1 0 0 'dc_xf1_hi'
3 4 2 1 1.00073475 2561.82886 170.78806587354 'dc_br1'
4 2 3 1 0.1 0 0 'dc_xf2_hi'
};
3 1 'branch' 1 1 0.1 0 0 'dc_xf1_hi'
3 4 'branch' 2 1 1.00073475 2561.82886 170.78806587354 'dc_br1'
4 2 'branch' 3 1 0.1 0 0 'dc_xf2_hi'
};'branch'


%% branch_gmd data
Expand Down
8 changes: 4 additions & 4 deletions test/data/matpower/b4gic_ne_blocker.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@


%% gmd_branch data
%column_names% f_bus t_bus parent_index br_status br_r br_v len_km name
%column_names% f_bus t_bus parent_type parent_index br_status br_r br_v len_km name
mpc.gmd_branch = {
3 1 1 1 0.1 0 0 'dc_xf1_hi'
3 4 2 1 1.00073475 170.78806587354 170.78806587354 'dc_br1'
4 2 3 1 0.1 0 0 'dc_xf2_hi'
3 1 'branch' 1 1 0.1 0 0 'dc_xf1_hi'
3 4 'branch' 2 1 1.00073475 170.78806587354 170.78806587354 'dc_br1'
4 2 'branch' 3 1 0.1 0 0 'dc_xf2_hi'
};


Expand Down
2 changes: 1 addition & 1 deletion test/data/matpower/epri21_ne_blocker.m
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@

%% gmd_blocker data
%column_names% gmd_bus status construction_cost
mpc.gmd_blocker = {
mpc.gmd_ne_blocker = {
1 1 1.0
2 1 1.0
3 1 1.0
Expand Down
176 changes: 89 additions & 87 deletions test/gmd_blocker.jl
Original file line number Diff line number Diff line change
@@ -1,126 +1,128 @@
@testset "TEST GIC BLOCKER" begin
@testset "Steady-state placement"
@testset "NE BLOCKER DATA" begin
case_b4gic = _PM.parse_file(data_b4gic_ne_blocker)
pm = _PM.instantiate_model(case_b4gic, _PM.ACPPowerModel, _PMGMD.build_blocker_placement; ref_extensions = [
_PMGMD.ref_add_gmd!
_PMGMD.ref_add_ne_blocker!
])

@test length(_PM.ref(pm,:gmd_ne_blocker)) == 6
@test _PM.ref(pm,:gmd_ne_blocker,6)["construction_cost"] == 100.0
@test _PM.ref(pm,:gmd_bus_ne_blockers,1)[1] == 1
@test _PM.ref(pm, :load_served_ratio) == 1.0
end
@testset "Steady-state placement" begin
@testset "NE BLOCKER DATA" begin
case_b4gic = _PM.parse_file(data_b4gic_ne_blocker)
_PMGMD.add_connected_components!(case_b4gic)
pm = _PM.instantiate_model(case_b4gic, _PM.ACPPowerModel, _PMGMD.build_blocker_placement; ref_extensions = [
_PMGMD.ref_add_gmd!,
_PMGMD.ref_add_ne_blocker!,
_PMGMD.ref_add_gmd_connections!,
])

@test length(_PM.ref(pm,:gmd_ne_blocker)) == 6
@test _PM.ref(pm,:gmd_ne_blocker,6)["construction_cost"] == 100.0
@test _PM.ref(pm,:gmd_bus_ne_blockers,1)[1] == 1
@test _PM.ref(pm, :load_served_ratio) == 1.0
end

@testset "Existing blocker test" begin
blocker_test = _PM.parse_file(data_blocker_test)
@testset "Existing blocker test" begin
blocker_test = _PM.parse_file(data_blocker_test)

result = _PMGMD.solve_ac_gmd_mld(blocker_test, ipopt_solver; setting=setting)
@test result["termination_status"] == _PM.LOCALLY_SOLVED
@test isapprox(result["objective"], 100.0; atol = 1e-1)
result = _PMGMD.solve_ac_gmd_mld(blocker_test, ipopt_solver; setting=setting)
@test result["termination_status"] == _PM.LOCALLY_SOLVED
@test isapprox(result["objective"], 10.0; atol = 1e-1)

result = _PMGMD.solve_soc_gmd_mld(blocker_test, ipopt_solver; setting=setting)
@test result["termination_status"] == _PM.LOCALLY_SOLVED
@test isapprox(result["objective"], 100.0; atol = 1e-1)
result = _PMGMD.solve_soc_gmd_mld(blocker_test, ipopt_solver; setting=setting)
@test result["termination_status"] == _PM.LOCALLY_SOLVED
@test isapprox(result["objective"], 10.0; atol = 1e-1)

# check what happens when the blockers are removed
for (i,blocker) in blocker_test["gmd_blocker"]
blocker["status"] = 0
end
# check what happens when the blockers are removed
for (i,blocker) in blocker_test["gmd_blocker"]
blocker["status"] = 0
end

result = _PMGMD.solve_ac_gmd_mld(blocker_test, ipopt_solver; setting=setting)
@test result["termination_status"] == _PM.LOCALLY_INFEASIBLE
result = _PMGMD.solve_ac_gmd_mld(blocker_test, ipopt_solver; setting=setting)
@test result["termination_status"] == _PM.LOCALLY_INFEASIBLE

result = _PMGMD.solve_soc_gmd_mld(blocker_test, ipopt_solver; setting=setting)
@test result["termination_status"] == _PM.LOCALLY_INFEASIBLE
end
result = _PMGMD.solve_soc_gmd_mld(blocker_test, ipopt_solver; setting=setting)
@test result["termination_status"] == _PM.LOCALLY_INFEASIBLE
end

@testset "B4GIC base ne case" begin
case_b4gic = _PM.parse_file(data_b4gic_ne_blocker)
@testset "B4GIC base ne case" begin
case_b4gic = _PM.parse_file(data_b4gic_ne_blocker)

result = _PMGMD.solve_ac_blocker_placement(case_b4gic, juniper_solver; setting=setting)
@test result["termination_status"] == _PM.LOCALLY_SOLVED
@test isapprox(result["objective"], 0.0; atol = 1e-1)
@test isapprox(result["solution"]["gmd_ne_blocker"]["1"]["blocker_placed"], 0.0; atol=1e-6)
result = _PMGMD.solve_ac_blocker_placement(case_b4gic, juniper_solver; setting=setting)
@test result["termination_status"] == _PM.LOCALLY_SOLVED
@test isapprox(result["objective"], 0.0; atol = 1e-1)
@test isapprox(result["solution"]["gmd_ne_blocker"]["1"]["blocker_placed"], 0.0; atol=1e-6)

result = _PMGMD.solve_soc_blocker_placement(case_b4gic, juniper_solver; setting=setting)
@test result["termination_status"] == _PM.LOCALLY_SOLVED
@test isapprox(result["objective"], 0.0; atol = 1e-1)
@test isapprox(result["solution"]["gmd_ne_blocker"]["1"]["blocker_placed"], 0.0; atol=1e-6)
end
result = _PMGMD.solve_soc_blocker_placement(case_b4gic, juniper_solver; setting=setting)
@test result["termination_status"] == _PM.LOCALLY_SOLVED
@test isapprox(result["objective"], 0.0; atol = 1e-1)
@test isapprox(result["solution"]["gmd_ne_blocker"]["1"]["blocker_placed"], 0.0; atol=1e-6)
end

@testset "EPRI 21 contrived ne case" begin
case_epri21 = _PM.parse_file(data_epri21_ne_blocker)
@testset "EPRI 21 contrived ne case" begin
case_epri21 = _PM.parse_file(data_epri21_ne_blocker)

case_epri21["branch"]["4"]["rate_a"] = 3.7
case_epri21["branch"]["4"]["rate_a"] = 3.7

for (i, gmd_bus) in case_epri21["gmd_bus"]
gmd_bus["g_gnd"] = gmd_bus["g_gnd"] * 1000.0
end
for (i, gmd_bus) in case_epri21["gmd_bus"]
gmd_bus["g_gnd"] = gmd_bus["g_gnd"] * 1000.0
end

result = _PMGMD.solve_ac_blocker_placement(case_epri21, juniper_solver; setting=setting)
@test result["termination_status"] == _PM.LOCALLY_SOLVED
@test isapprox(result["objective"], 2.0; atol = 1e-1)
@test isapprox(result["solution"]["gmd_ne_blocker"]["1"]["blocker_placed"], 0.0; atol=1e-6)
result = _PMGMD.solve_ac_blocker_placement(case_epri21, juniper_solver; setting=setting)
@test result["termination_status"] == _PM.LOCALLY_SOLVED
@test isapprox(result["objective"], 2.0; atol = 1e-1)
@test isapprox(result["solution"]["gmd_ne_blocker"]["3"]["blocker_placed"], 0.0; atol=1e-6)

case_epri21["branch"]["4"]["rate_a"] = .09925
case_epri21["branch"]["4"]["rate_a"] = .09925

result = _PMGMD.solve_soc_blocker_placement(case_epri21, juniper_solver; setting=setting)
@test result["termination_status"] == _PM.LOCALLY_SOLVED
@test isapprox(result["objective"], 1.0; atol = 1e-1)
@test isapprox(result["solution"]["gmd_ne_blocker"]["1"]["blocker_placed"], 0.0; atol=1e-6)
end
result = _PMGMD.solve_soc_blocker_placement(case_epri21, juniper_solver; setting=setting)
@test result["termination_status"] == _PM.LOCALLY_SOLVED
@test isapprox(result["objective"], 0.0; atol = 1e-1)
@test isapprox(result["solution"]["gmd_ne_blocker"]["3"]["blocker_placed"], 0.0; atol=1e-6)
end

@testset "B4GIC contigency ne case" begin
case_b4gic = _PM.parse_file(data_b4gic_15kvm_contingency)
@testset "B4GIC contigency ne case" begin
case_b4gic = _PM.parse_file(data_b4gic_15kvm_contingency)

result = _PMGMD.solve_ac_blocker_placement(case_b4gic, juniper_solver; setting=setting)
@test result["termination_status"] == _PM.LOCALLY_SOLVED
@test isapprox(result["objective"], 1.0; atol = 1e-1)
@test isapprox(result["solution"]["gmd_ne_blocker"]["1"]["blocker_placed"], 0.0; atol=1e-6)
result = _PMGMD.solve_ac_blocker_placement(case_b4gic, juniper_solver; setting=setting)
@test result["termination_status"] == _PM.LOCALLY_SOLVED
@test isapprox(result["objective"], 1.0; atol = 1e-1)
@test isapprox(result["solution"]["gmd_ne_blocker"]["1"]["blocker_placed"], 1.0; atol=1e-6)

result = _PMGMD.solve_soc_blocker_placement(case_b4gic, juniper_solver; setting=setting)
@test result["termination_status"] == _PM.LOCALLY_SOLVED
@test isapprox(result["objective"], 1.0; atol = 1e-1)
@test isapprox(result["solution"]["gmd_ne_blocker"]["1"]["blocker_placed"], 0.0; atol=1e-6)
end
result = _PMGMD.solve_soc_blocker_placement(case_b4gic, juniper_solver; setting=setting)
@test result["termination_status"] == _PM.LOCALLY_SOLVED
@test isapprox(result["objective"], 1.0; atol = 1e-1)
@test isapprox(result["solution"]["gmd_ne_blocker"]["2"]["blocker_placed"], 1.0; atol=1e-6)
end

@testset "EPRI 21 contigency ne case" begin
case_epri21_15kvm_contingency = _PM.parse_file(data_epri21_15kvm_contingency)
@testset "EPRI 21 contigency ne case" begin
case_epri21_15kvm_contingency = _PM.parse_file(data_epri21_15kvm_contingency)

result = _PMGMD.solve_ac_blocker_placement(case_epri21_15kvm_contingency, juniper_solver; setting=setting)
@test result["termination_status"] == _PM.LOCALLY_SOLVED
@test isapprox(result["objective"], 0.0; atol = 1e-1)
@test isapprox(result["solution"]["gmd_ne_blocker"]["1"]["blocker_placed"], 0.0; atol=1e-6)
result = _PMGMD.solve_ac_blocker_placement(case_epri21_15kvm_contingency, juniper_solver; setting=setting)
@test result["termination_status"] == _PM.LOCALLY_SOLVED
@test isapprox(result["objective"], 0.0; atol = 1e-1)
@test isapprox(result["solution"]["gmd_ne_blocker"]["2"]["blocker_placed"], 0.0; atol=1e-6)

result = _PMGMD.solve_soc_blocker_placement(case_epri21_15kvm_contingency, juniper_solver; setting=setting)
@test result["termination_status"] == _PM.LOCALLY_SOLVED
@test isapprox(result["objective"], 0.0; atol = 1e-1)
@test isapprox(result["solution"]["gmd_ne_blocker"]["1"]["blocker_placed"], 0.0; atol=1e-6)
result = _PMGMD.solve_soc_blocker_placement(case_epri21_15kvm_contingency, juniper_solver; setting=setting)
@test result["termination_status"] == _PM.LOCALLY_SOLVED
@test isapprox(result["objective"], 0.0; atol = 1e-1)
@test isapprox(result["solution"]["gmd_ne_blocker"]["2"]["blocker_placed"], 0.0; atol=1e-6)
end
end

#@testset "Multi-scenario placement"
#end

@testset "Time-extended blocker placement"
@testset "Time-extended blocker placement" begin
@testset "b4gic case" begin
ts_setting = deepcopy(setting)
ts_setting["ts"] = false

net = PowerModels.parse_file(data_b4gic)
net["load_served_ratio"] = 0.90
components = get_connected_components(net)
net["connected_components"] = components
add_blockers!(net)
filter_gmd_ne_blockers!(net) # doesn't work for time-series extended. Run this before calling PowerModels.replicate?
_PMGMD.add_connected_components!(net)
_PMGMD.add_blockers!(net)
_PMGMD.filter_gmd_ne_blockers!(net) # doesn't work for time-series extended. Run this before calling PowerModels.replicate?

io = open(wf_path)
io = open(data_b4gic_wf)
waveform = JSON.parse(io)
close(io)

n = 2
ts_net = PowerModels.replicate(net, n)
ts_net = _PM.replicate(net, n)

wf_ids = [1, 27]
wf_time = waveform["time"]
Expand All @@ -130,8 +132,8 @@
δ_t = wf_time[2] - wf_time[1]

# TODO: move this to a function
ts_net = create_ts_net(net, waveform, ids=wf_ids, scaling=10.0)
result = solve_ac_blocker_placement_multi_scenario(ts_net, juniper_solver; setting=setting)
ts_net = _PMGMD.create_ts_net(net, waveform, ids=wf_ids, scaling=10.0)
result = _PMGMD.solve_ac_blocker_placement_multi_scenario(ts_net, juniper_solver; setting=setting)
@test result["termination_status"] == _PM.LOCALLY_SOLVED
@test isapprox(result["objective"], 1.0; atol = 1e-1)
end
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ include("test_cases.jl")
include("parse.jl") # GIC parser
include("coupling.jl")
# include("gic.jl") # RAW/GIC network builder
include("gmd_blocker.jl")
end
2 changes: 1 addition & 1 deletion test/test_cases.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ b4gic_offbase = "../test/data/matpower/b4gic_offbase.m"
autotransformer = "../test/data/matpower/autotransformer.m"
epricase = "../test/data/matpower/epricase_aug2022_v22_fix.m"

data_b4gic_wf = "../PowerModelsGMD.jl/test/data/suppl/b4gic-gmd-waveform.json"
data_b4gic_wf = "../test/data/suppl/b4gic-gmd-waveform.json"

0 comments on commit 29fa003

Please sign in to comment.