From 484dbca091ee5d2b1d4441a130a45c7a0333a06c Mon Sep 17 00:00:00 2001 From: Petr Krysl Date: Sat, 21 Jan 2023 07:35:33 -0800 Subject: [PATCH 1/2] switch to throwing errors --- src/Problem/SpkProblem.jl | 8 ++++---- src/SparseCSCInterface/SparseCSCInterface.jl | 6 +++--- src/SparseMethod/SpkLUFactor.jl | 2 +- src/SparseMethod/SpkSparseBase.jl | 16 ++++++++-------- src/SparseSpdMethod/SpkSymFct.jl | 2 +- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Problem/SpkProblem.jl b/src/Problem/SpkProblem.jl index 6a9b463..92ee285 100644 --- a/src/Problem/SpkProblem.jl +++ b/src/Problem/SpkProblem.jl @@ -176,7 +176,7 @@ The value is *added* to the existing contents. """ function inaij!(p::Problem{IT,FT}, rnum, cnum, aij=zero(FT)) where {IT<:BlasInt, FT} if (rnum < 1 || cnum < 1) - @warn "$(@__FILE__): invalid matrix subscripts $(rnum), $(cnum): input ignored" + @warn "Invalid matrix subscripts $(rnum), $(cnum): input ignored" return false end @@ -257,7 +257,7 @@ Input an entry of the right-hand side vector. """ function inbi!(p::Problem{IT, FT}, rnum::IT, bi::FT) where {IT<:BlasInt, FT} if (rnum < 1) - @error "$(@__FILE__): Invalid rhs subscript $(rnum)." + error("Invalid rhs subscript $(rnum).") return false end @@ -401,7 +401,7 @@ Updated Parameter: """ function makerhs!(p::Problem, x::Vector{FT}, mtype = "T") where {FT} if (p.nnz == 0) - @error "$(@__FILE__): Matrix is NULL. The rhs cannot be computed." + error("Matrix is NULL. The rhs cannot be computed.") return p end @@ -462,7 +462,7 @@ function computeresidual(p::Problem, res::Vector{FT}, xin::Vector{FT} = FT[], mt if (lowercase(mtype) == "t" || lowercase(mtype) == "l" || lowercase(mtype) == "u") flag = 1 else - @error "$(@__FILE__): Invalid value for mtype, $mtype." + error("Invalid value for mtype, $mtype.") return false end diff --git a/src/SparseCSCInterface/SparseCSCInterface.jl b/src/SparseCSCInterface/SparseCSCInterface.jl index f60dc57..4ca6d2a 100644 --- a/src/SparseCSCInterface/SparseCSCInterface.jl +++ b/src/SparseCSCInterface/SparseCSCInterface.jl @@ -102,7 +102,7 @@ end function _inmatrix!(s::_SparseBase{IT, FT}, m::SparseMatrixCSC{FT,IT}) where {IT, FT} if (s.n == 0) - @error "$(@__FILE__): An empty problem. No matrix." + error("An empty problem. No matrix.") return false end @@ -128,7 +128,7 @@ function _inmatrix!(s::_SparseBase{IT, FT}, m::SparseMatrixCSC{FT,IT}) where {IT for nxtsub in fstsub:lstsub irow = lindx[nxtsub] if (irow > inew) - @error "$(@__FILE__): No space for matrix element $(inew), $(jnew)." + error("No space for matrix element ($(inew), $(jnew)).") return false end if (irow == inew) @@ -151,7 +151,7 @@ function _inmatrix!(s::_SparseBase{IT, FT}, m::SparseMatrixCSC{FT,IT}) where {IT for nxtsub in fstsub:lstsub irow = lindx[nxtsub] if (irow > jnew) - @error "$(@__FILE__): No space for matrix element $(inew), $(jnew)." + error("No space for matrix element ($(inew), $(jnew)).") return false end if (irow == jnew) diff --git a/src/SparseMethod/SpkLUFactor.jl b/src/SparseMethod/SpkLUFactor.jl index b2718fb..b982421 100644 --- a/src/SparseMethod/SpkLUFactor.jl +++ b/src/SparseMethod/SpkLUFactor.jl @@ -361,7 +361,7 @@ function _luusolve!(n::IT, nsuper::IT, xsuper::Vector{IT}, xlindx::Vector{IT}, l jj = jj + 1 isub = lindx[jj] if (isub > n) - @error "$(@__FILE__): $(isub) index out of bounds in rhs" + error("Index $(isub) out of bounds in rhs") return false end temp[j] = rhs[isub] diff --git a/src/SparseMethod/SpkSparseBase.jl b/src/SparseMethod/SpkSparseBase.jl index 2ea8b86..627776f 100644 --- a/src/SparseMethod/SpkSparseBase.jl +++ b/src/SparseMethod/SpkSparseBase.jl @@ -172,7 +172,7 @@ end function _findorder!(s::_SparseBase{IT}, orderfunction::F) where {IT, F} if (s.n == 0) - @error "$(@__FILE__): An empty problem, no ordering found." + error("An empty problem, no ordering found.") return false end makestructuresymmetric(s.g) # Make it symmetric @@ -196,7 +196,7 @@ function _symbolicfactor!(s::_SparseBase{IT, FT}) where {IT, FT} # # """ if (s.n == 0) - @error "$(@__FILE__): An empty problem. No symbolic factorization done." + error("An empty problem. No symbolic factorization done.") return false end @@ -304,7 +304,7 @@ end function _inmatrix!(s::_SparseBase{IT, FT}, p::Problem{IT, FT}) where {IT, FT} if (s.n == 0) - @error "$(@__FILE__): An empty problem. No matrix." + error("An empty problem. No matrix.") return false end @@ -331,7 +331,7 @@ function _inmatrix!(s::_SparseBase{IT, FT}, p::Problem{IT, FT}) where {IT, FT} for nxtsub in fstsub:lstsub irow = lindx[nxtsub] if (irow > inew) - @error "$(@__FILE__): No space for matrix element $(inew), $(jnew)." + error("No space for matrix element ($(inew), $(jnew)).") return false end if (irow == inew) @@ -354,7 +354,7 @@ function _inmatrix!(s::_SparseBase{IT, FT}, p::Problem{IT, FT}) where {IT, FT} for nxtsub in fstsub:lstsub irow = lindx[nxtsub] if (irow > jnew) - @error "$(@__FILE__): No space for matrix element $(inew), $(jnew)." + error("No space for matrix element ($(inew), $(jnew)).") return false end if (irow == jnew) @@ -380,14 +380,14 @@ end # in the comments at the beginning of this module. function _factor!(s::_SparseBase{IT, FT}) where {IT, FT} if (s.n == 0) - @error "$(@__FILE__): An empty problem. No matrix." + error("An empty problem. No matrix.") return false end s.errflag = _lufactor!(s.n, s.nsuper, s.xsuper, s.snode, s.xlindx, s.lindx, s.xlnz, s.lnz, s.xunz, s.unz, s.ipiv) if (s.errflag != 0) - @error "$(@__FILE__): An empty problem. No matrix." + error("An empty problem. No matrix.") return false end return true @@ -402,7 +402,7 @@ end # object is applied as appropriate. function _triangularsolve!(s::_SparseBase{IT, FT}, solution::AbstractVector{FT}) where {IT, FT} if (s.n == 0) - @error "$(@__FILE__): An empty problem. No solution." + error("An empty problem. No solution.") return false end diff --git a/src/SparseSpdMethod/SpkSymFct.jl b/src/SparseSpdMethod/SpkSymFct.jl index 26df909..b08b747 100644 --- a/src/SparseSpdMethod/SpkSymFct.jl +++ b/src/SparseSpdMethod/SpkSymFct.jl @@ -225,7 +225,7 @@ function _symbolicfact!(n, xadj, adj, perm, invp, colcnt, nsuper, xsuper, snode, # - - - - - - - - - - - nzbeg = nzend + 1; nzend = nzend + knz if (nzend + 1 != xlindx[ksup + 1]) - @error "$(@__FILE__): inconsistency in data structure." + error("Inconsistency in data structure.") return false end From d7496eba1ffb91c8a0d6ee32bf71bda0566cc827 Mon Sep 17 00:00:00 2001 From: Petr Krysl Date: Sat, 21 Jan 2023 07:35:52 -0800 Subject: [PATCH 2/2] put the failing tests first --- test/runtests.jl | 8 +- test/test_small.jl | 200 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 199 insertions(+), 9 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 16a540c..08d30e4 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -3,8 +3,12 @@ using Test # # Error in LinearSolve - once fixed, move to the end... # -@time @testset "Smallmatrix" begin - include("test_smallmatrix.jl") +# @time @testset "Smallmatrix" begin +# include("test_smallmatrix.jl") +# end + +@time @testset "Small tricky problem" begin + include("test_small.jl") end @time @testset "Problem" begin diff --git a/test/test_small.jl b/test/test_small.jl index a8b1b3b..035bfeb 100644 --- a/test/test_small.jl +++ b/test/test_small.jl @@ -1,4 +1,4 @@ - +module m_simpletest using Test using Sparspak using Random, SparseArrays, LinearAlgebra @@ -14,18 +14,204 @@ function simpletest(;n=4) A[2,1]=-0.1 pr = SpkProblem.Problem(n,n, 10) @test SpkProblem.insparse!(pr, A) - @show pr s = SpkSparseSolver.SparseSolver(pr) @test SpkSparseSolver.findorder!(s) end simpletest() +end + +# +module m_inconsistency_data_structure_symm +using Test +using LinearAlgebra +using SparseArrays +using Sparspak.SpkOrdering +using Sparspak.SpkProblem +using Sparspak.SpkProblem: inaij!, inbi!, outsparse +using Sparspak.SpkSparseSolver: SparseSolver, findorder!, symbolicfactor!, inmatrix!, factor!, solve!, triangularsolve! + +function makeaproblem() + # Matrix is not symmetric, but it does have a symmetric structure. + n = 4 + A = [1.21883 -0.02432 0.0 0.942235; + 0.0243952 1.0 0.0 0.0; + 0.0 0.0 1.53656 0.0; + 0.340938 0.0 0.0 1.0] + + p = SpkProblem.Problem(n, n) + for i in 1:n, j in 1:n + if A[i, j] != 0.0 + inaij!(p, i, j, A[i, j]) + end + end + inbi!(p, 1, 1.0) + + return p +end + +function _test() + p = makeaproblem() + + s = SparseSolver(p) + + solve!(s) + + A = outsparse(p) + x = A \ p.rhs + @test norm(p.x - x) / norm(x) < 1.0e-6 + + return true +end + +_test() +end # module + +module m_inconsistency_data_structure_symm_forced +using Test +using LinearAlgebra +using SparseArrays +using Sparspak.SpkOrdering +using Sparspak.SpkProblem +using Sparspak.SpkProblem: inaij!, inbi!, outsparse +using Sparspak.SpkSparseSolver: SparseSolver, findorder!, symbolicfactor!, inmatrix!, factor!, solve!, triangularsolve! + +function makeaproblem() + # Matrix is not symmetric, but it will be input so that the structure is symmetric by definition. + n = 4 + A = [1.21883 0.0 0.0 0.942235; + 0.0243952 1.0 0.0 0.0; + 0.0 0.0 1.53656 0.0; + 0.0 0.0 0.0 1.0] + + p = SpkProblem.Problem(n, n) + for i in 1:n, j in 1:n + if A[i, j] != 0.0 + inaij!(p, i, j, A[i, j]) + inaij!(p, j, i, 0.0) + end + end + inbi!(p, 1, 1.0) + + return p +end + +function _test() + p = makeaproblem() + + s = SparseSolver(p) + + solve!(s) + + A = outsparse(p) + x = A \ p.rhs + @test norm(p.x - x) / norm(x) < 1.0e-6 -#= + return true +end + +_test() +end # module + +module m_inconsistency_data_structure_unsymm +using Test +using LinearAlgebra +using SparseArrays +using Sparspak.SpkOrdering +using Sparspak.SpkProblem +using Sparspak.SpkProblem: inaij!, inbi!, outsparse +using Sparspak.SpkSparseSolver: SparseSolver, findorder!, symbolicfactor!, inmatrix!, factor!, solve!, triangularsolve! + +function makeaproblem() + # Matrix is not symmetric and it does not have a symmetric structure. + n = 4 + A = [1.21883 0.0 0.0 0.942235; + 0.0243952 1.0 0.0 0.0; + 0.0 0.0 1.53656 0.0; + 0.340938 0.0 0.0 1.0] + + p = SpkProblem.Problem(n, n) + # inaij!(p, 1, 1, 1.21883); + # inaij!(p, 2, 1, 0.0243952); + # inaij!(p, 4, 1, 0.340938); + # inaij!(p, 2, 2, 1.0); + # inaij!(p, 3, 3, 1.53656); + # inaij!(p, 1, 4, 0.942235); + # inaij!(p, 4, 4, 1.0); + for i in 1:n, j in 1:n + if A[i, j] != 0.0 + inaij!(p, i, j, A[i, j]) + end + end + inbi!(p, 1, 1.0) + + return p +end + +function _test() + p = makeaproblem() + + s = SparseSolver(p) + + solve!(s) + + A = outsparse(p) + x = A \ p.rhs + @test norm(p.x - x) / norm(x) < 1.0e-6 + + return true +end + +_test() +end # module +# +module m_inconsistency_symfact +using Test +using LinearAlgebra +using SparseArrays +using Sparspak.SpkOrdering +using Sparspak.SpkProblem +using Sparspak.SpkProblem: inaij!, inbi!, outsparse +using Sparspak.SpkSparseSolver: SparseSolver, findorder!, symbolicfactor!, inmatrix!, factor!, solve!, triangularsolve! -julia> include("C:\\Users\\pkonl\\Documents\\00WIP\\Sparspak.jl\\test\\test_small.jl")pr = Sparspak.SpkProblem.Problem{Int64, Float64}("", 4, 10, 4, 5, [1, 3, 4, 5], [2, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 2, 2, 3, 4, 0, 0, 0, 0, 0], 4, 4, 15, 4, 5, 4, [1.00000e+00, -1.00000e-01, 1.00000e+00, 1.00000e+00, 1.00000e+00, Inf, Inf, Inf, Inf, Inf], Float64[], Float64[], [0.00000e+00, 0.00000e+00, 0.00000e+00, 0.00000e+00], [0.00000e+00, 0.00000e+00, 0.00000e+00, 0.00000e+00]) +function makeaproblem() + n = 4 + A = [1.21883 0.0 0.0 0.942235; + 0.0243952 1.0 0.0 0.0; + 0.0 0.0 1.53656 0.0; + 0.340938 0.0 0.0 1.0] + + p = SpkProblem.Problem(n, n) + inaij!(p, 1, 1, 1.21883); + inaij!(p, 2, 1, 0.0243952); + inaij!(p, 4, 1, 0.340938); + inaij!(p, 2, 2, 1.0); + inaij!(p, 3, 3, 1.53656); + inaij!(p, 1, 4, 0.942235); + inaij!(p, 4, 4, 1.0); + # for i in 1:n, j in 1:n + # if A[i, j] != 0.0 + # inaij!(p, i, j, A[i, j]) + # end + # end + + return p +end + +function _test() + p = makeaproblem() + + s = SparseSolver(p) + + solve!(s) + + A = outsparse(p) + x = A \ p.rhs + @test norm(p.x - x) / norm(x) < 1.0e-6 + + return true +end -g = Sparspak.SpkGraph.Graph{Int64}(4, 1, 4, 4, [1, 2, 2, 2, 2], [2]) -(k, first, g.adj) = (2, [1, 2, 2, 2], [2]) +_test() +end # module -=#