Skip to content

Commit

Permalink
Fixed unit tests.
Browse files Browse the repository at this point in the history
Changed tests to use test/data_*.jl instead of the ones in data.
  • Loading branch information
daniel-thom committed Oct 20, 2019
1 parent 2ef471c commit 144f371
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 54 deletions.
42 changes: 28 additions & 14 deletions test/test_network_matrices.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using SparseArrays
using Test
include("../data/data_5bus_pu.jl")
include("../data/data_14bus_pu.jl")

include(joinpath(BASE_DIR, "test", "data_5bus_pu.jl"))
include(joinpath(BASE_DIR, "test", "data_14bus_pu.jl"))

# The 5-bus case from PowerModels data is modified to include 2 phase shifters
pm_dict = PowerSystems.parse_file(joinpath(MATPOWER_DIR, "case5.m"));
sys = PowerSystems.pm2ps_dict(pm_dict);
Expand Down Expand Up @@ -289,14 +291,18 @@ Ybus5_phaseshifter[4,5]= -3.33366670000333 + 33.3366670000333im
Ybus5_phaseshifter[5,5]= 18.8039637297063 - 188.020637297063im;

@testset "PTDF matrices" begin
P5 = PowerSystems.PTDF(branches5, nodes5);
nodes_5 = nodes5()
branches_5 = branches5(nodes_5)
P5 = PowerSystems.PTDF(branches_5, nodes_5);
@test maximum(P5.data - S5_slackB4) <= 1e-3
@test P5[branches5[1],nodes5[1]] == 0.1939166051164976
@test P5[branches_5[1],nodes_5[1]] == 0.1939166051164976

P14 = PowerSystems.PTDF(branches14, nodes14);
nodes_14 = nodes14()
branches_14 = branches14(nodes_14)
P14 = PowerSystems.PTDF(branches_14, nodes_14);
@test maximum(P14.data - S14_slackB1) <= 1e-3

P5NS = PTDF([branches5[b] for b in Br5NS_ids], [nodes5[b] for b in Bu5NS_ids]);
P5NS = PTDF([branches_5[b] for b in Br5NS_ids], [nodes_5[b] for b in Bu5NS_ids]);
for br in Br5NS_ids, b in Bu5NS_ids
@test getindex(P5NS,string(br),b) - S5_slackB4[br,b] <= 1e-3
end
Expand All @@ -309,26 +315,34 @@ Ybus5_phaseshifter[5,5]= 18.8039637297063 - 188.020637297063im;
end

@testset "LODF matrices" begin
L5 = PowerSystems.LODF(branches5,nodes5)
nodes_5 = nodes5()
branches_5 = branches5(nodes_5)
L5 = PowerSystems.LODF(branches_5,nodes_5)
@test maximum(L5.data - Lodf_5) <= 1e-3
@test L5[branches5[1],branches5[2]] == 0.3447946513849091
@test L5[branches_5[1],branches_5[2]] == 0.3447946513849091

L14 = PowerSystems.LODF(branches14,nodes14)
nodes_14 = nodes14()
branches_14 = branches14(nodes_14)
L14 = PowerSystems.LODF(branches_14, nodes_14)
@test maximum(L14.data - Lodf_14) <= 1e-3

L5NS = PowerSystems.LODF(sys)
@test getindex(L5NS,"5","4") - 0.0003413469090 <= 1e-4

L5NS = LODF([branches5[b] for b in Br5NS_ids], [nodes5[b] for b in Bu5NS_ids]);
L5NS = LODF([branches_5[b] for b in Br5NS_ids], [nodes_5[b] for b in Bu5NS_ids]);
for brf in Br5NS_ids, brt in Br5NS_ids
@test getindex(L5NS,string(brf),string(brt)) - Lodf_5[brf,brt] <= 1e-3
end

end

@testset "Ybus Matrix" begin
nodes_5 = nodes5()
branches_5 = branches5(nodes_5)
nodes_14 = nodes14()
branches_14 = branches14(nodes_14)

Ybus5 = Ybus(branches5, nodes5)
Ybus5 = Ybus(branches_5, nodes_5)

I, J, V = findnz(Ybus5.data)
indices = collect(zip(I,J))
Expand All @@ -338,7 +352,7 @@ end
end


Ybus14 = Ybus(branches14, nodes14);
Ybus14 = Ybus(branches_14, nodes_14);
I, J, V = findnz(Ybus14.data)
indices = collect(zip(I,J))

Expand All @@ -349,11 +363,11 @@ end
Y5NS = Ybus(sys)
@test isapprox(getindex(Y5NS, 10, 4), -3.3336667 + 33.336667im, atol= 1e-4)

#Y5NS = Ybus([branches5[b] for b in Br5NS_ids], [nodes5[b] for b in Bu5NS_ids]);
#Y5NS = Ybus([branches_5[b] for b in Br5NS_ids], [nodes_5[b] for b in Bu5NS_ids]);
#for buf in Bu5NS_ids, but in Bu5NS_ids
# @test isapprox(getindex(Y5NS, buf, but), Ybus5_matpower[buf,but], atol=1e-3)
#end

@test Ybus5[nodes5[1],nodes5[2]] == (-3.5234840209999647 + 35.234840209999646im)
@test Ybus5[nodes_5[1],nodes_5[2]] == (-3.5234840209999647 + 35.234840209999646im)

end
11 changes: 7 additions & 4 deletions test/test_powerflow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ result = [2.32551,
1.02131,
-0.280381]

include(joinpath(BASE_DIR,"data/data_14bus_pu.jl"))
c_sys14 = System(nodes14, thermal_generators14, loads14, branches14, nothing, 100.0, nothing, nothing);
include(joinpath(BASE_DIR, "test", "data_5bus_pu.jl"))
include(joinpath(BASE_DIR, "test", "data_14bus_pu.jl"))

include(joinpath(BASE_DIR,"data/data_5bus_pu.jl"))
c_sys5_re = System(nodes5, vcat(thermal_generators5, renewable_generators5), loads5,
nodes_5 = nodes5()
nodes_14 = nodes14()
c_sys14 = System(nodes_14, thermal_generators14(nodes_14), loads14(nodes_14), branches14(nodes_14), nothing, 100.0, nothing, nothing);

c_sys5_re = System(nodes_5, vcat(thermal_generators5(nodes_5), renewable_generators5(nodes_5)), loads5(nodes_5),
nothing, nothing, 100.0, nothing, nothing)


Expand Down
44 changes: 23 additions & 21 deletions test/test_powersystemconstructors.jl
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@

include(joinpath(DATA_DIR,"data_5bus_pu.jl"))
include(joinpath(DATA_DIR,"data_14bus_pu.jl"))
include(joinpath(BASE_DIR, "test", "data_5bus_pu.jl"))
include(joinpath(BASE_DIR, "test", "data_14bus_pu.jl"))

checksys = false

@testset "Test System constructors from .jl files" begin
tPowerSystem = System(nothing)
nodes_5 = nodes5()
nodes_14 = nodes14()

for i in nodes5
nodes5[i].angle = deg2rad(nodes5[i].angle)
for i in nodes_5
nodes_5[i].angle = deg2rad(nodes_5[i].angle)
end

# Components with forecasts cannot be added to multiple systems, so clear them on each
# test.

sys5 = System(nodes5, thermal_generators5, loads5, nothing, nothing, 100.0, nothing,
sys5 = System(nodes_5, thermal_generators5(nodes_5), loads5(nodes_5), nothing, nothing, 100.0, nothing,
nothing; runchecks = checksys)
clear_components!(sys5)

sys5b = System(nodes5, thermal_generators5, loads5, nothing, battery5, 100.0, nothing,
sys5b = System(nodes_5, thermal_generators5(nodes_5), loads5(nodes_5), nothing, battery5(nodes_5), 100.0, nothing,
nothing; runchecks = checksys)
clear_components!(sys5b)

# GitHub issue #234 - fix forecasts5 in data file, use new format
#_sys5b = PowerSystems._System(nodes5, thermal_generators5, loads5, nothing, battery5,
#_sys5b = PowerSystems._System(nodes_5, thermal_generators5(nodes_5), loads5(nodes_5), nothing, battery5(nodes_5),
# 100.0, forecasts5, nothing, nothing)
#sys5b = System(_sys5b)

sys5bh = System(nodes5, vcat(thermal_generators5, hydro_generators5), loads5, branches5,
battery5, 100.0, nothing, nothing; runchecks = checksys)
sys5bh = System(nodes_5, vcat(thermal_generators5(nodes_5), hydro_generators5(nodes_5)), loads5(nodes_5), branches5(nodes_5),
battery5(nodes_5), 100.0, nothing, nothing; runchecks = checksys)
clear_components!(sys5bh)

sys5bh = System(; buses=nodes5,
generators=vcat(thermal_generators5, hydro_generators5),
loads=loads5,
branches=branches5,
storage=battery5,
sys5bh = System(; buses=nodes_5,
generators=vcat(thermal_generators5(nodes_5), hydro_generators5(nodes_5)),
loads=loads5(nodes_5),
branches=branches5(nodes_5),
storage=battery5(nodes_5),
basepower=100.0,
services=nothing,
annex=nothing,
Expand All @@ -45,19 +47,19 @@ checksys = false
# Test Data for 14 Bus

# GitHub issue #234 - fix forecasts5 in data file, use new format
#_sys14 = PowerSystems._System(nodes14, thermal_generators14, loads14, nothing, nothing,
#_sys14 = PowerSystems._System(nodes_14, thermal_generators14, loads14, nothing, nothing,
# 100.0, Dict{Symbol,Vector{<:Forecast}}(),nothing,nothing)
#sys14 = System(_sys14)

for i in nodes14
nodes14[i].angle = deg2rad(nodes14[i].angle)
for i in nodes_14
nodes_14[i].angle = deg2rad(nodes_14[i].angle)
end

sys14b = PowerSystems.System(nodes14, thermal_generators14, loads14, nothing,
battery14, 100.0, nothing, nothing; runchecks = checksys)
sys14b = PowerSystems.System(nodes_14, thermal_generators14(nodes_14), loads14(nodes_14), nothing,
battery14(nodes_14), 100.0, nothing, nothing; runchecks = checksys)
clear_components!(sys14b)
sys14b = PowerSystems.System(nodes14, thermal_generators14, loads14, branches14,
battery14, 100.0, nothing, nothing; runchecks = checksys)
sys14b = PowerSystems.System(nodes_14, thermal_generators14(nodes_14), loads14(nodes_14), branches14(nodes_14),
battery14(nodes_14), 100.0, nothing, nothing; runchecks = checksys)
clear_components!(sys14b)
end

Expand Down
36 changes: 21 additions & 15 deletions test/test_validation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import YAML

const WRONG_FORMAT_CONFIG_FILE = joinpath(dirname(pathof(PowerSystems)),
"descriptors", "config.yml")
include(joinpath(DATA_DIR,"data_5bus_pu.jl"))
include(joinpath(BASE_DIR, "test", "data_5bus_pu.jl"))

@testset "Test reading in config data" begin
data = IS.read_validation_descriptor(PSY.POWER_SYSTEM_STRUCT_DESCRIPTOR_FILE)
Expand All @@ -21,11 +21,13 @@ include(joinpath(DATA_DIR,"data_5bus_pu.jl"))
end

@testset "Test adding custom validation YAML file to System" begin
sys_no_config = System(nodes5, thermal_generators5, loads5, nothing, nothing,
nodes = nodes5()
sys_no_config = System(nodes, thermal_generators5(nodes), loads5(nodes), nothing, nothing,
100.0, nothing, nothing; runchecks=true)
@test !isempty(sys_no_config.data.validation_descriptors)

sys_no_runchecks = System(nodes5, thermal_generators5, loads5, nothing, nothing,
nodes = nodes5()
sys_no_runchecks = System(nodes, thermal_generators5(nodes), loads5(nodes), nothing, nothing,
100.0, nothing, nothing; runchecks=false)
@test isempty(sys_no_runchecks.data.validation_descriptors)
end
Expand All @@ -47,7 +49,6 @@ end
@test haskey(descriptor, "struct_name")
@test haskey(descriptor, "fields")
@test descriptor["struct_name"] == struct_name

end

@testset "Test extracting field info from struct descriptor dictionary" begin
Expand Down Expand Up @@ -83,34 +84,38 @@ end

@testset "Test field validation" begin
#test recursive call of validate_fields and a regular valid range
bad_therm_gen_rating = deepcopy(thermal_generators5)
nodes = nodes5()
bad_therm_gen_rating = thermal_generators5(nodes)
bad_therm_gen_rating[1].tech.rating = -10
@test_logs((:error, r"Invalid range"),
@test_throws(PSY.InvalidRange,
System(nodes5, bad_therm_gen_rating, loads5, nothing, nothing,
100.0, nothing, nothing; runchecks=true)
System(nodes, bad_therm_gen_rating, loads5(nodes), nothing,
nothing, 100.0, nothing, nothing; runchecks=true)
)
)

#test custom range (activepower and activepowerlimits)
bad_therm_gen_act_power = deepcopy(thermal_generators5)
bad_therm_gen_act_power = thermal_generators5(nodes)
bad_therm_gen_act_power[1].activepower = 10
@test_logs (:warn, r"Invalid range") System(nodes5, bad_therm_gen_act_power, loads5,
nothing, nothing, 100.0, nothing, nothing; runchecks=true)
nodes = nodes5()
@test_logs (:warn, r"Invalid range") System(nodes, bad_therm_gen_act_power,
loads5(nodes), nothing, nothing, 100.0, nothing, nothing; runchecks=true)

#test validating named tuple
bad_therm_gen_ramp_lim = deepcopy(thermal_generators5)
nodes = nodes5()
bad_therm_gen_ramp_lim = thermal_generators5(nodes)
bad_therm_gen_ramp_lim[1].tech.ramplimits = (up = -10, down = -3)
@test_logs((:error, r"Invalid range"), match_mode=:any,
@test_throws(PSY.InvalidRange,
System(nodes5, bad_therm_gen_ramp_lim, loads5, nothing, nothing, 100.0,
nothing, nothing; runchecks=true)
System(nodes, bad_therm_gen_ramp_lim, loads5(nodes), nothing, nothing,
100.0, nothing, nothing; runchecks=true)
)
)
end

@testset "Test field validation" begin
sys = System(nodes5, thermal_generators5, loads5, nothing, nothing,
nodes = nodes5()
sys = System(nodes, thermal_generators5(nodes), loads5(nodes), nothing, nothing,
100.0, nothing, nothing; runchecks=true)

add_component!(sys,Bus(11,"11",PSY.PQ,1,1,(min=.9,max=1.1),123))
Expand All @@ -125,7 +130,8 @@ end
end

@testset "Test field validation after deserialization" begin
sys = System(nodes5, thermal_generators5, loads5, nothing, nothing,
nodes = nodes5()
sys = System(nodes, thermal_generators5(nodes), loads5(nodes), nothing, nothing,
100.0, nothing, nothing)

add_component!(sys, Bus(11, "11", PSY.PQ, 1, 1, (min=.9, max=1.1), 123))
Expand Down

0 comments on commit 144f371

Please sign in to comment.