Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mcosovic committed May 16, 2024
1 parent 592fdea commit c86c8be
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ Random = "<0.0.1, 1.8"
julia = "1.8"

[extras]
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9"
HiGHS = "87dc4568-4c63-4d18-b0c0-bb2238e4078b"
GLPK = "60bf3e95-4087-53dc-ae20-288a0d20c6a6"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["HiGHS", "Ipopt", "GLPK", "Test"]
test = ["Suppressor", "HiGHS", "Ipopt", "GLPK", "Test"]
3 changes: 1 addition & 2 deletions src/utility/routine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ function print(io::IO, label::OrderedDict{String, Int64}, data::Union{Array{Floa
end
end

function print(io::IO, label::OrderedDict{String, Int64}, data1::Array{Float64,1}, data2::Array{Float64,1})
function print(io::IO, label::OrderedDict{String, Int64}, data1::Union{Array{Float64,1}, Array{Int64,1}, Array{Int8,1}}, data2::Union{Array{Float64,1}, Array{Int64,1}, Array{Int8,1}})
for (key, value) in label
println(io::IO, key, ": ", data1[value], ", ", data2[value])
end
Expand All @@ -342,7 +342,6 @@ function print(io::IO, label::OrderedDict{String, Int64}, obj::Dict{Int64, JuMP.
try
println(io::IO, key, ": ", obj[value])
catch
# println(io::IO, "undefined")
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions test/measurement/buildUpdate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,19 @@ end
@test device.pmu.angle.mean[5] == 3
@test device.pmu.angle.variance[5] == 3
@test device.pmu.angle.mean[6] == 3

####### Test Multiple Labels #######
device = measurement()

@voltmeter(label = "!")
addVoltmeter!(system, device; bus = 1, magnitude = 1)
addVoltmeter!(system, device; bus = 1, magnitude = 1)
addVoltmeter!(system, device; bus = 1, magnitude = 1)

labels = collect(keys(device.voltmeter.label))
@test labels[1] == "1"
@test labels[2] == "1 (1)"
@test labels[3] == "1 (2)"
end

@testset "Build Random Measurement Set" begin
Expand Down Expand Up @@ -764,18 +777,23 @@ end

####### Test Ammeter Errors #######
@test_throws ErrorException("The label Ammeter 1 is not unique.") addAmmeter!(system, device; label = "Ammeter 1", from = "Branch 1", magnitude = 1)
@test_throws ErrorException("At least one of the location keywords must be provided.") addAmmeter!(system, device; label = "Ammeter 1", magnitude = 1)
@test_throws ErrorException("The concurrent definition of the location keywords is not allowed.") addAmmeter!(system, device; label = "Ammeter 1", from = "Branch 1", to = "Branch 1", magnitude = 1)
@test_throws ErrorException("The currents cannot be found.") addAmmeter!(system, device, analysis)

@test_throws LoadError @eval @ammeter(label = "Ammeter ?", means = 1)

####### Test Wattmeter Errors #######
@test_throws ErrorException("The label Wattmeter 1 is not unique.") addWattmeter!(system, device; label = "Wattmeter 1", from = "Branch 1", active = 1)
@test_throws ErrorException("The concurrent definition of the location keywords is not allowed.") addWattmeter!(system, device; label = "Wattmeter 1", from = "Branch 1", to = "Branch 1", active = 1)
@test_throws ErrorException("At least one of the location keywords must be provided.") addWattmeter!(system, device; label = "Wattmeter 1", active = 1)
@test_throws ErrorException("The powers cannot be found.") addWattmeter!(system, device, analysis)

@test_throws LoadError @eval @wattmeter(label = "Wattmeter ?", means = 1)

####### Test Varmeter Errors #######
@test_throws ErrorException("The label Varmeter 1 is not unique.") addVarmeter!(system, device; label = "Varmeter 1", from = "Branch 1", reactive = 1)
@test_throws ErrorException("The concurrent definition of the location keywords is not allowed.") addVarmeter!(system, device; label = "Varmeter 1", from = "Branch 1", to = "Branch 1", bus = "Bus 1", reactive = 1)
@test_throws ErrorException("The powers cannot be found.") addVarmeter!(system, device, analysis)

@test_throws LoadError @eval @varmeter(label = "Varmeter ?", means = 1)
Expand Down
2 changes: 0 additions & 2 deletions test/optimalPowerFlow/analysis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ system14 = powerSystem(string(pathData, "case14optimal.m"))
matpower14 = h5read(string(pathData, "results.h5"), "case14optimal/acOptimalPowerFlow")

######## Modified IEEE 14-bus Test Case ##########
acModel!(system14)
analysis = acOptimalPowerFlow(system14, Ipopt.Optimizer)
JuMP.set_silent(analysis.method.jump)
solve!(system14, analysis)
Expand Down Expand Up @@ -155,7 +154,6 @@ system30 = powerSystem(string(pathData, "case30test.m"))
end

################ Modified IEEE 30-bus Test Case ################
dcModel!(system30)
analysis = dcOptimalPowerFlow(system30, HiGHS.Optimizer)
JuMP.set_silent(analysis.method.jump)
solve!(system30, analysis)
Expand Down
25 changes: 25 additions & 0 deletions test/powerSystem/buildUpdate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ end
gs = gaussSeidel(system)

addBus!(system, label = "Bus 3")
addBus!(system, label = 4)

####### Test Deleting Models #######
@test isempty(system.model.ac.nodalMatrix) == true
Expand All @@ -537,8 +538,19 @@ end
@test system.model.dc.model == 1
@test system.model.dc.pattern == 1

####### Test Print #######
print1 = @capture_out print(system.bus.label, system.bus.voltage.magnitude)
@test print1 == "Bus 1: 1.0\nBus 2: 1.0\nBus 3: 1.0\n4: 1.0\n"

print2 = @capture_out print(system.bus.label, system.bus.voltage.magnitude, system.bus.voltage.angle)
@test print2 == "Bus 1: 1.0, 0.0\nBus 2: 1.0, 0.0\nBus 3: 1.0, 0.0\n4: 1.0, 0.0\n"

print3 = @capture_out print(system.bus.label, system.bus.layout.type)
@test print3 == "Bus 1: 3\nBus 2: 1\nBus 3: 1\n4: 1\n"

####### Test Bus Errors #######
@test_throws ErrorException("The label Bus 1 is not unique.") addBus!(system; label = "Bus 1")
@test_throws ErrorException("The label 4 is not unique.") addBus!(system; label = 4)
@test_throws ErrorException("The value 4 of the bus type keyword is illegal.") addBus!(system; label = "Bus 4", type = 4)
@test_throws ErrorException("The slack bus has already been designated.") addBus!(system; label = "Bus 5", type = 3)
@test_throws ErrorException("The DC power flow model cannot be reused when adding a new bus.") addBus!(system, dc; label = "Bus 4", active = 0.1)
Expand All @@ -548,6 +560,8 @@ end
@test_throws ErrorException("The AC power flow model cannot be reused when adding a new bus.") addBus!(system, gs; label = "Bus 4", active = 0.1)

@test_throws ErrorException("To set bus with label Bus 3 as the slack bus, reassign the current slack bus to either a generator or demand bus.") updateBus!(system; label = "Bus 3", type = 3)
@test_throws ErrorException("The bus label Bus 6 that has been specified does not exist within the available bus labels.") updateBus!(system; label = "Bus 6", active = 2)
@test_throws ErrorException("The bus label 2 that has been specified does not exist within the available bus labels.") updateBus!(system; label = 2, active = 2)
@test_throws ErrorException("The DC power flow model cannot be reused due to required bus type conversion.") updateBus!(system, dc; label = "Bus 1", type = 1)
@test_throws ErrorException("The AC power flow model cannot be reused due to required bus type conversion.") updateBus!(system, nr; label = "Bus 1", type = 1)
@test_throws ErrorException("The AC power flow model cannot be reused due to required bus type conversion.") updateBus!(system, fnrxb; label = "Bus 1", type = 1)
Expand Down Expand Up @@ -579,4 +593,15 @@ end
@test_throws ErrorException("An attempt to assign a piecewise function has been made, but the piecewise function does not exist.") cost!(system; label = "Generator 1", active = 1)

@test_throws LoadError @eval @generator(label = "Generator ?", actives = 1)

####### Test Unit Errors #######
@test_throws LoadError @eval @current(sA, deg)
@test_throws LoadError @eval @current(kV, deg)

####### Test Voltage Errors #######
@test_throws ErrorException("The voltage values are missing.") power!(system, dc)

####### Test Load Errors #######
@test_throws DomainError(".h6", "The extension .h6 is not supported.") powerSystem("case14.h6")
@test_throws DomainError("case15.h5", "The input data case15.h5 is not found.") powerSystem("case15.h5")
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using JuliaGrid
using HDF5
using Test
using JuMP, HiGHS, Ipopt, GLPK
using Suppressor

######## Path to Test Data ##########
pathData = abspath(joinpath(dirname(Base.find_package("JuliaGrid")), ".."), "test/data/")
Expand Down
36 changes: 36 additions & 0 deletions test/stateEstimation/reusing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,14 @@ system14 = powerSystem(string(pathData, "case14test.m"))
updateVarmeter!(system14, device, analysisWLS; label = 5, reactive = analysis.power.injection.reactive[5], variance = 1e-2)

updatePmu!(system14, device, analysisWLS; label = 1, statusAngle = 0, polar = true)
updatePmu!(system14, device, analysisWLS; label = 1, statusMagnitude = 0)
updatePmu!(system14, device, analysisWLS; label = 1, statusMagnitude = 1)
updatePmu!(system14, device, analysisWLS; label = 3, statusAngle = 1, polar = true)
updatePmu!(system14, device, analysisWLS; label = 5, magnitude = analysis.voltage.magnitude[5], angle = analysis.voltage.angle[5], varianceAngle = 1e-5, polar = true)
updatePmu!(system14, device, analysisWLS; label = 5, polar = false)
updatePmu!(system14, device, analysisWLS; label = 5, statusMagnitude = 0)
updatePmu!(system14, device, analysisWLS; label = 5, statusMagnitude = 1)
updatePmu!(system14, device, analysisWLS; label = 5, correlated = true)

updatePmu!(system14, device, analysisWLS; label = 15, statusAngle = 0, polar = true)
updatePmu!(system14, device, analysisWLS; label = 17, statusAngle = 1, polar = true)
Expand All @@ -212,8 +218,14 @@ system14 = powerSystem(string(pathData, "case14test.m"))
updateAmmeter!(system14, device, analysisWLS; label = "From 17", magnitude = analysis.current.from.magnitude[17], variance = 1e-2)

updatePmu!(system14, device, analysisWLS; label = "PMU 4", statusAngle = 0, polar = true)
updatePmu!(system14, device, analysisWLS; label = "PMU 4", statusMagnitude = 0)
updatePmu!(system14, device, analysisWLS; label = "PMU 4", statusMagnitude = 1)
updatePmu!(system14, device, analysisWLS; label = "PMU 15", angle = analysis.current.from.angle[15], polar = true)
updatePmu!(system14, device, analysisWLS; label = "PMU 17", magnitude = analysis.current.from.magnitude[17], polar = true)
updatePmu!(system14, device, analysisWLS; label = "PMU 17", polar = false)
updatePmu!(system14, device, analysisWLS; label = "PMU 17", statusMagnitude = 0)
updatePmu!(system14, device, analysisWLS; label = "PMU 17", statusMagnitude = 1)
updatePmu!(system14, device, analysisWLS; label = "PMU 17", correlated = true)

updateWattmeter!(system14, device, analysisWLS; label = "To 4", status = 0)
updateWattmeter!(system14, device, analysisWLS; label = "To 15", status = 1)
Expand All @@ -230,6 +242,10 @@ system14 = powerSystem(string(pathData, "case14test.m"))
updatePmu!(system14, device, analysisWLS; label = "PMU 4 To", statusAngle = 0, polar = true)
updatePmu!(system14, device, analysisWLS; label = "PMU 15 To", angle = analysis.current.to.angle[15], polar = true)
updatePmu!(system14, device, analysisWLS; label = "PMU 17 To", magnitude = analysis.current.to.magnitude[17], polar = true)
updatePmu!(system14, device, analysisWLS; label = "PMU 17 To", polar = false)
updatePmu!(system14, device, analysisWLS; label = "PMU 17 To", statusMagnitude = 0)
updatePmu!(system14, device, analysisWLS; label = "PMU 17 To", statusMagnitude = 1)
updatePmu!(system14, device, analysisWLS; label = "PMU 17 To", correlated = true)

analysisWLS = gaussNewton(system14, device)
for iteration = 1:20
Expand Down Expand Up @@ -257,6 +273,10 @@ system14 = powerSystem(string(pathData, "case14test.m"))
updatePmu!(system14, device, analysisLAV; label = 1, statusAngle = 0, polar = true)
updatePmu!(system14, device, analysisLAV; label = 3, statusAngle = 1, polar = true)
updatePmu!(system14, device, analysisLAV; label = 5, magnitude = analysis.voltage.magnitude[5], angle = analysis.voltage.angle[5], varianceAngle = 1e-5, polar = true)
updatePmu!(system14, device, analysisLAV; label = 5, polar = false)
updatePmu!(system14, device, analysisLAV; label = 5, statusMagnitude = 0)
updatePmu!(system14, device, analysisLAV; label = 5, statusMagnitude = 1)
updatePmu!(system14, device, analysisLAV; label = 5, correlated = true)

updatePmu!(system14, device, analysisLAV; label = 15, statusAngle = 0, polar = true)
updatePmu!(system14, device, analysisLAV; label = 17, statusAngle = 1, polar = true)
Expand All @@ -277,6 +297,10 @@ system14 = powerSystem(string(pathData, "case14test.m"))
updatePmu!(system14, device, analysisLAV; label = "PMU 4", statusAngle = 0, polar = true)
updatePmu!(system14, device, analysisLAV; label = "PMU 15", angle = analysis.current.from.angle[15], polar = true)
updatePmu!(system14, device, analysisLAV; label = "PMU 17", magnitude = analysis.current.from.magnitude[17], polar = true)
updatePmu!(system14, device, analysisLAV; label = "PMU 17", polar = false)
updatePmu!(system14, device, analysisLAV; label = "PMU 17", statusMagnitude = 0)
updatePmu!(system14, device, analysisLAV; label = "PMU 17", statusMagnitude = 1)
updatePmu!(system14, device, analysisLAV; label = "PMU 17", correlated = true)

updateWattmeter!(system14, device, analysisLAV; label = "To 4", status = 0)
updateWattmeter!(system14, device, analysisLAV; label = "To 15", status = 1)
Expand All @@ -293,6 +317,10 @@ system14 = powerSystem(string(pathData, "case14test.m"))
updatePmu!(system14, device, analysisLAV; label = "PMU 4 To", statusAngle = 0, polar = true)
updatePmu!(system14, device, analysisLAV; label = "PMU 15 To", angle = analysis.current.to.angle[15], polar = true)
updatePmu!(system14, device, analysisLAV; label = "PMU 17 To", magnitude = analysis.current.to.magnitude[17], polar = true)
updatePmu!(system14, device, analysisLAV; label = "PMU 17 To", polar = false)
updatePmu!(system14, device, analysisLAV; label = "PMU 17 To", statusMagnitude = 0)
updatePmu!(system14, device, analysisLAV; label = "PMU 17 To", statusMagnitude = 1)
updatePmu!(system14, device, analysisLAV; label = "PMU 17 To", correlated = true)

JuMP.set_silent(analysisLAV.method.jump)
solve!(system14, analysisLAV)
Expand Down Expand Up @@ -394,10 +422,14 @@ system14 = powerSystem(string(pathData, "case14test.m"))
updatePmu!(system14, deviceWLS, analysisWLS; label = 1, magnitude = analysis.voltage.magnitude[1])
updatePmu!(system14, deviceWLS, analysisWLS; label = 4, angle = analysis.voltage.angle[4], statusMagnitude = 1)
updatePmu!(system14, deviceWLS, analysisWLS; label = 16, magnitude = analysis.voltage.magnitude[6], angle = analysis.voltage.angle[6], statusAngle = 1)
updatePmu!(system14, deviceWLS, analysisWLS; label = 4, statusMagnitude = 0)
updatePmu!(system14, deviceWLS, analysisWLS; label = 4, statusMagnitude = 1)

updatePmu!(system14, deviceWLS, analysisWLS; label = "From 8", magnitude = analysis.current.from.magnitude[8], angle = analysis.current.from.angle[8])
updatePmu!(system14, deviceWLS, analysisWLS; label = "From 9", statusAngle = 1, statusMagnitude = 1)
updatePmu!(system14, deviceWLS, analysisWLS; label = "From 12", angle = analysis.current.from.angle[12])
updatePmu!(system14, deviceWLS, analysisWLS; label = "From 2", statusMagnitude = 0)
updatePmu!(system14, deviceWLS, analysisWLS; label = "From 2", statusMagnitude = 1)

updatePmu!(system14, deviceWLS, analysisWLS; label = "To 4", magnitude = analysis.current.to.magnitude[4])
updatePmu!(system14, deviceWLS, analysisWLS; label = "To 10", angle = analysis.current.to.angle[10])
Expand All @@ -415,10 +447,14 @@ system14 = powerSystem(string(pathData, "case14test.m"))
updatePmu!(system14, deviceLAV, analysisLAV; label = "From 8", magnitude = analysis.current.from.magnitude[8], angle = analysis.current.from.angle[8])
updatePmu!(system14, deviceLAV, analysisLAV; label = "From 9", statusAngle = 1, statusMagnitude = 1)
updatePmu!(system14, deviceLAV, analysisLAV; label = "From 12", angle = analysis.current.from.angle[12])
updatePmu!(system14, deviceLAV, analysisLAV; label = "From 2", statusMagnitude = 0)
updatePmu!(system14, deviceLAV, analysisLAV; label = "From 2", statusMagnitude = 1)

updatePmu!(system14, deviceLAV, analysisLAV; label = "To 4", magnitude = analysis.current.to.magnitude[4])
updatePmu!(system14, deviceLAV, analysisLAV; label = "To 10", angle = analysis.current.to.angle[10])
updatePmu!(system14, deviceLAV, analysisLAV; label = "To 15", statusAngle = 1)
updatePmu!(system14, deviceLAV, analysisLAV; label = "To 15", statusMagnitude = 0)
updatePmu!(system14, deviceLAV, analysisLAV; label = "To 15", statusMagnitude = 1)

JuMP.set_silent(analysisLAV.method.jump)
solve!(system14, analysisLAV)
Expand Down

0 comments on commit c86c8be

Please sign in to comment.