Skip to content

Commit

Permalink
improve coverage (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
frapac authored and michel2323 committed May 10, 2022
1 parent 2e77608 commit 58bbb14
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/ExaPF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const KA = KernelAbstractions
import Base: show, get

export run_pf
export State, Control, PolarForm
export State, Control, AllVariables, PolarForm

# Export KernelAbstractions devices
export CPU
Expand Down
9 changes: 0 additions & 9 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,3 @@ function blockmul!(y::AbstractArray, A::AbstractMatrix, x::AbstractArray, alpha,
end
end

struct BlockSparseMatrix{SMT}
k::Int
Js::Vector{SMT}
end

function BlockSparseMatrix(J::AbstractSparseMatrix, k::Int)
return BlockSparseMatrix(k, [copy(J) for _ in 1:k])
end

5 changes: 3 additions & 2 deletions test/Polar/TestPolarForm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ function runtests(datafile, device, AT)
@testset "PolarForm API" begin
test_polar_api(polar, device, AT)
test_polar_stack(polar, device, AT)
test_polar_blk_stack(polar, device, AT)
test_polar_blockstack(polar, device, AT)
test_polar_blk_expressions(polar, device, AT)
test_polar_constraints(polar, device, AT)
test_polar_powerflow(polar, device, AT)
end
Expand All @@ -49,7 +50,7 @@ function runtests(datafile, device, AT)
test_constraints_jacobian(polar, device, AT)
test_constraints_adjoint(polar, device, AT)
test_full_space_jacobian(polar, device, AT)
test_batch_jacobian(polar, device, AT)
test_block_jacobian(polar, device, AT)
test_reduced_gradient(polar, device, AT)
end

Expand Down
53 changes: 52 additions & 1 deletion test/Polar/api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function test_polar_stack(polar, device, M)
mapx = ExaPF.mapping(polar, State()) |> M
mapu = ExaPF.mapping(polar, Control()) |> M
u0 = rand(length(mapu)) |> M

stack = ExaPF.NetworkStack(polar)

# Test display
Expand Down Expand Up @@ -40,6 +41,56 @@ function test_polar_stack(polar, device, M)
return nothing
end

function test_polar_blockstack(polar, device, M)
nblocks = 2
ngen = get(polar, PS.NumberOfGenerators())
nbus = get(polar, PS.NumberOfBuses())
nlines = get(polar, PS.NumberOfLines())
nu = ExaPF.number(polar, Control())
nx = ExaPF.number(polar, State())

mapx = ExaPF.mapping(polar, State(), nblocks) |> M
@test length(mapx) == nx * nblocks
mapu = ExaPF.mapping(polar, Control(), nblocks) |> M
mapxu = ExaPF.mapping(polar, AllVariables(), nblocks) |> M
u0 = rand(nu) |> M

x0 = rand(nx * nblocks) |> M

stack = ExaPF.BlockNetworkStack(polar, nblocks)

# Test display
println(devnull, stack)
# By defaut, values are equal to 0
@test isa(stack.input, M)
@test isa(stack.vmag, M)

# Copy state x0 inside cache
copyto!(stack, mapx, x0)
@test stack.input[mapx] == x0
# Copy control u0 inside cache
ExaPF.blockcopy!(stack, mapu, u0)

# Test that all attributes have valid length
@test length(stack.vang) == length(stack.vmag) == nbus * nblocks
@test length(stack.pgen) == ngen * nblocks
@test length(stack.input) == (ngen + 2 * nbus) * nblocks
@test length(stack.ψ) == (2 * nlines + nbus) * nblocks

# Bounds
b♭, b♯ = ExaPF.bounds(polar, stack)
@test myisless(b♭, b♯)

empty!(stack)
@test iszero(stack.input)

# Test constructor with scenarios
pload = rand(nbus, nblocks)
qload = rand(nbus, nblocks)
stack = ExaPF.BlockNetworkStack(polar, pload, qload)
return nothing
end

function test_polar_api(polar, device, M)
pf = polar.network
npv, npq, nref = length(pf.pv), length(pf.pq), length(pf.ref)
Expand Down Expand Up @@ -159,7 +210,7 @@ function test_polar_powerflow(polar, device, M)
end
end

function test_polar_blk_stack(polar, device, M)
function test_polar_blk_expressions(polar, device, M)
nblocks = 2
stack = ExaPF.NetworkStack(polar)
blk_stack = ExaPF.BlockNetworkStack(polar, nblocks)
Expand Down
14 changes: 13 additions & 1 deletion test/Polar/first_order.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ function test_reduced_gradient(polar, device, MT)
@test isapprox(grad_fd[:], grad_adjoint, rtol=1e-4)
end

function test_batch_jacobian(polar, device, MT)
function test_block_jacobian(polar, device, MT)
nblocks = 3
mapx = ExaPF.mapping(polar, State())

Expand All @@ -226,5 +226,17 @@ function test_batch_jacobian(polar, device, MT)
J_cpu = jac.J |> SparseMatrixCSC
@test blk_J_cpu blockdiag([J_cpu for i in 1:nblocks]...)
end

constraints = [
ExaPF.PowerFlowBalance(polar),
ExaPF.PowerGenerationBounds(polar),
ExaPF.VoltageMagnitudeBounds(polar),
ExaPF.LineFlows(polar),
]
mycons = ExaPF.MultiExpressions(constraints) ExaPF.PolarBasis(polar)
for X in [State(), Control(), AllVariables()]
blk_jac = ExaPF.ArrowheadJacobian(polar, mycons, X, nblocks)
ExaPF.jacobian!(blk_jac, blk_stack)
end
end

9 changes: 7 additions & 2 deletions test/Polar/second_order.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,15 @@ function test_batch_hessian(polar, device, MT)
# Block evaluation
blk_hess = ExaPF.ArrowheadHessian(polar, mycons, ExaPF.State(), nblocks)
blk_H = ExaPF.hessian!(blk_hess, blk_stack, blk_y)

blk_H_cpu = blk_H |> SparseMatrixCSC
H_cpu = H |> SparseMatrixCSC

@test blk_H_cpu blockdiag([H_cpu for i in 1:nblocks]...)

# Multivariables
for X in [State(), Control(), AllVariables()]
blk_hess = ExaPF.ArrowheadHessian(polar, mycons, X, nblocks)
blk_H = ExaPF.hessian!(blk_hess, blk_stack, blk_y)
@test isa(blk_H, AbstractMatrix)
end
end

0 comments on commit 58bbb14

Please sign in to comment.