Skip to content

Added integration test #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ julia = "1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
DiffEqDiffTools = "01453d9d-ee7c-5054-8395-0335cb756afa"

[targets]
test = ["Test"]
test = ["Test", "LinearAlgebra", "DiffEqDiffTools"]
2 changes: 1 addition & 1 deletion src/matrix2graph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ sparse matrix, columns are represented with vertices
and 2 vertices are connected with an edge only if
the two columns are mutually orthogonal.
"""
function matrix2graph(SparseMatrix::SparseMatrixCSC{Int64,Int64})
function matrix2graph(SparseMatrix::SparseMatrixCSC{T,Int}) where T<:Number
dropzeros(SparseMatrix)
(rows_index, cols_index, val) = findnz(SparseMatrix)

Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ using Test
@testset "Exact coloring via contraction" begin include("test_contraction.jl") end
@testset "Greedy distance-1 coloring" begin include("test_greedy_d1.jl") end
@testset "Matrix to graph conversion" begin include("test_matrix2graph.jl") end
@testset "Integration test" begin include("test_integration.jl") end
49 changes: 49 additions & 0 deletions test/test_integration.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using SparseDiffTools
using LinearAlgebra
using DiffEqDiffTools
using Test

fcalls = 0
function f(dx,x)
global fcalls += 1
for i in 2:length(x)-1
dx[i] = x[i-1] - 2x[i] + x[i+1]
end
dx[1] = -2x[1] + x[2]
dx[end] = x[end-1] - 2x[end]
nothing
end

function second_derivative_stencil(N)
A = zeros(N,N)
for i in 1:N, j in 1:N
(j-i==-1 || j-i==1) && (A[i,j]=1)
j-i==0 && (A[i,j]=-2)
end
A
end

function generate_sparsity_pattern(N::Integer)
dl = repeat([1.0],N-1)
du = repeat([1.0],N-1)
d = repeat([-2.0],N)
return Tridiagonal(dl,d,du)
end

sparsity_pattern = sparse(generate_sparsity_pattern(30))

_graph = matrix2graph(sparsity_pattern)
coloring_vector = greedy_d1(_graph)
@test coloring_vector == repeat(1:3,10)

#Jacobian computed without coloring vector
J = DiffEqDiffTools.finite_difference_jacobian(f, rand(30))
@test J ≈ second_derivative_stencil(30)
@test fcalls == 31

#Jacobian computed with coloring vectors
fcalls = 0
_J = sparsity_pattern
DiffEqDiffTools.finite_difference_jacobian!(_J, f, rand(30), color = coloring_vector)
@test fcalls == 4
@test _J ≈ J