|
| 1 | +using LinearSolve, LinearAlgebra, SparseArrays |
| 2 | +using Test |
| 3 | + |
| 4 | +n = 8 |
| 5 | +A = Matrix(I,n,n) |
| 6 | +b = ones(n) |
| 7 | +A1 = A/1; b1 = rand(n); x1 = zero(b) |
| 8 | +A2 = A/2; b2 = rand(n); x2 = zero(b) |
| 9 | + |
| 10 | +prob1 = LinearProblem(A1, b1; u0=x1) |
| 11 | +prob2 = LinearProblem(A2, b2; u0=x2) |
| 12 | + |
| 13 | +cache_kwargs = (;verbose=true, abstol=1e-8, reltol=1e-8, maxiter=30,) |
| 14 | + |
| 15 | +function test_interface(alg, prob1, prob2) |
| 16 | + A1 = prob1.A; b1 = prob1.b; x1 = prob1.u0 |
| 17 | + A2 = prob2.A; b2 = prob2.b; x2 = prob2.u0 |
| 18 | + |
| 19 | + y = solve(prob1, alg; cache_kwargs...) |
| 20 | + @test A1 * y ≈ b1 |
| 21 | + |
| 22 | + cache = SciMLBase.init(prob1,alg; cache_kwargs...) # initialize cache |
| 23 | + y = solve(cache) |
| 24 | + @test A1 * y ≈ b1 |
| 25 | + |
| 26 | + cache = LinearSolve.set_A(cache,copy(A2)) |
| 27 | + y = solve(cache) |
| 28 | + @test A2 * y ≈ b1 |
| 29 | + |
| 30 | + cache = LinearSolve.set_b(cache,b2) |
| 31 | + y = solve(cache) |
| 32 | + @test A2 * y ≈ b2 |
| 33 | + |
| 34 | + return |
| 35 | +end |
| 36 | + |
| 37 | +@testset "LinearSolve" begin |
| 38 | + |
| 39 | +@testset "Default Linear Solver" begin |
| 40 | + test_interface(nothing, prob1, prob2) |
| 41 | + |
| 42 | + A1 = prob1.A; b1 = prob1.b; x1 = prob1.u0 |
| 43 | + y = solve(prob1) |
| 44 | + @test A1 * y ≈ b1 |
| 45 | + |
| 46 | + _prob = LinearProblem(SymTridiagonal(A1), b1; u0=x1) |
| 47 | + y = solve(_prob) |
| 48 | + @test A1 * y ≈ b1 |
| 49 | + |
| 50 | + _prob = LinearProblem(Tridiagonal(A1), b1; u0=x1) |
| 51 | + y = solve(_prob) |
| 52 | + @test A1 * y ≈ b1 |
| 53 | + |
| 54 | + _prob = LinearProblem(Symmetric(A1), b1; u0=x1) |
| 55 | + y = solve(_prob) |
| 56 | + @test A1 * y ≈ b1 |
| 57 | + |
| 58 | + _prob = LinearProblem(Hermitian(A1), b1; u0=x1) |
| 59 | + y = solve(_prob) |
| 60 | + @test A1 * y ≈ b1 |
| 61 | + |
| 62 | + |
| 63 | + _prob = LinearProblem(sparse(A1), b1; u0=x1) |
| 64 | + y = solve(_prob) |
| 65 | + @test A1 * y ≈ b1 |
| 66 | +end |
| 67 | + |
| 68 | +@testset "UMFPACK Factorization" begin |
| 69 | + A1 = A/1; b1 = rand(n); x1 = zero(b) |
| 70 | + A2 = A/2; b2 = rand(n); x2 = zero(b) |
| 71 | + |
| 72 | + prob1 = LinearProblem(sparse(A1), b1; u0=x1) |
| 73 | + prob2 = LinearProblem(sparse(A2), b2; u0=x2) |
| 74 | + test_interface(UMFPACKFactorization(), prob1, prob2) |
| 75 | + |
| 76 | + # Test that refactoring wrong throws. |
| 77 | + cache = SciMLBase.init(prob1,UMFPACKFactorization(); cache_kwargs...) # initialize cache |
| 78 | + y = solve(cache) |
| 79 | + cache = LinearSolve.set_A(cache,sprand(n, n, 0.8)) |
| 80 | + @test_throws ArgumentError solve(cache) |
| 81 | +end |
| 82 | + |
| 83 | +@testset "KLU Factorization" begin |
| 84 | + A1 = A/1; b1 = rand(n); x1 = zero(b) |
| 85 | + A2 = A/2; b2 = rand(n); x2 = zero(b) |
| 86 | + |
| 87 | + prob1 = LinearProblem(sparse(A1), b1; u0=x1) |
| 88 | + prob2 = LinearProblem(sparse(A2), b2; u0=x2) |
| 89 | + test_interface(KLUFactorization(), prob1, prob2) |
| 90 | + |
| 91 | + # Test that refactoring wrong throws. |
| 92 | + cache = SciMLBase.init(prob1,KLUFactorization(); cache_kwargs...) # initialize cache |
| 93 | + y = solve(cache) |
| 94 | + X = copy(A1) |
| 95 | + X[8,8] = 0.0 |
| 96 | + X[7,8] = 1.0 |
| 97 | + cache = LinearSolve.set_A(cache,sparse(X)) |
| 98 | + @test_throws ArgumentError solve(cache) |
| 99 | +end |
| 100 | + |
| 101 | +@testset "Concrete Factorizations" begin |
| 102 | + for alg in ( |
| 103 | + LUFactorization(), |
| 104 | + QRFactorization(), |
| 105 | + SVDFactorization(), |
| 106 | + RFLUFactorization() |
| 107 | + ) |
| 108 | + @testset "$alg" begin |
| 109 | + test_interface(alg, prob1, prob2) |
| 110 | + end |
| 111 | + end |
| 112 | +end |
| 113 | + |
| 114 | +@testset "Generic Factorizations" begin |
| 115 | + for fact_alg in ( |
| 116 | + lu, lu!, |
| 117 | + qr, qr!, |
| 118 | + cholesky, |
| 119 | + #cholesky!, |
| 120 | + # ldlt, ldlt!, |
| 121 | + bunchkaufman, bunchkaufman!, |
| 122 | + lq, lq!, |
| 123 | + svd, svd!, |
| 124 | + LinearAlgebra.factorize, |
| 125 | + ) |
| 126 | + @testset "fact_alg = $fact_alg" begin |
| 127 | + alg = GenericFactorization(fact_alg=fact_alg) |
| 128 | + test_interface(alg, prob1, prob2) |
| 129 | + end |
| 130 | + end |
| 131 | +end |
| 132 | + |
| 133 | +@testset "KrylovJL" begin |
| 134 | + kwargs = (;gmres_restart=5,) |
| 135 | + for alg in ( |
| 136 | + ("Default",KrylovJL(kwargs...)), |
| 137 | + ("CG",KrylovJL_CG(kwargs...)), |
| 138 | + ("GMRES",KrylovJL_GMRES(kwargs...)), |
| 139 | + # ("BICGSTAB",KrylovJL_BICGSTAB(kwargs...)), |
| 140 | + ("MINRES",KrylovJL_MINRES(kwargs...)), |
| 141 | + ) |
| 142 | + @testset "$(alg[1])" begin |
| 143 | + test_interface(alg[2], prob1, prob2) |
| 144 | + end |
| 145 | + end |
| 146 | +end |
| 147 | + |
| 148 | +@testset "IterativeSolversJL" begin |
| 149 | + kwargs = (;gmres_restart=5,) |
| 150 | + for alg in ( |
| 151 | + ("Default", IterativeSolversJL(kwargs...)), |
| 152 | + ("CG", IterativeSolversJL_CG(kwargs...)), |
| 153 | + ("GMRES",IterativeSolversJL_GMRES(kwargs...)), |
| 154 | + # ("BICGSTAB",IterativeSolversJL_BICGSTAB(kwargs...)), |
| 155 | + # ("MINRES",IterativeSolversJL_MINRES(kwargs...)), |
| 156 | + ) |
| 157 | + @testset "$(alg[1])" begin |
| 158 | + test_interface(alg[2], prob1, prob2) |
| 159 | + end |
| 160 | + end |
| 161 | +end |
| 162 | + |
| 163 | +@testset "PardisoJL" begin |
| 164 | + @test_throws UndefVarError alg = PardisoJL() |
| 165 | + |
| 166 | + using Pardiso, SparseArrays |
| 167 | + |
| 168 | + A1 = sparse([ 1. 0 -2 3 |
| 169 | + 0 5 1 2 |
| 170 | + -2 1 4 -7 |
| 171 | + 3 2 -7 5 ]) |
| 172 | + b1 = rand(4) |
| 173 | + prob1 = LinearProblem(A1, b1) |
| 174 | + |
| 175 | + lambda = 3 |
| 176 | + e = ones(n) |
| 177 | + e2 = ones(n-1) |
| 178 | + A2 = spdiagm(-1 => im*e2, 0 => lambda*e, 1 => -im*e2) |
| 179 | + b2 = rand(n) + im * zeros(n) |
| 180 | + |
| 181 | + prob2 = LinearProblem(A2, b2) |
| 182 | + |
| 183 | + for alg in ( |
| 184 | + PardisoJL(), |
| 185 | + MKLPardisoFactorize(), |
| 186 | + MKLPardisoIterate(), |
| 187 | + ) |
| 188 | + |
| 189 | + u = solve(prob1, alg; cache_kwargs...).u |
| 190 | + @test A1 * u ≈ b1 |
| 191 | + |
| 192 | + u = solve(prob2, alg; cache_kwargs...).u |
| 193 | + @test eltype(u) <: Complex |
| 194 | + @test_broken A2 * u ≈ b2 |
| 195 | + end |
| 196 | + |
| 197 | +end |
| 198 | + |
| 199 | +@testset "Preconditioners" begin |
| 200 | + @testset "Vector Diagonal Preconditioner" begin |
| 201 | + s = rand(n) |
| 202 | + Pl, Pr = Diagonal(s),LinearSolve.InvPreconditioner(Diagonal(s)) |
| 203 | + |
| 204 | + x = rand(n,n) |
| 205 | + y = rand(n,n) |
| 206 | + |
| 207 | + mul!(y, Pl, x); @test y ≈ s .* x |
| 208 | + mul!(y, Pr, x); @test y ≈ s .\ x |
| 209 | + |
| 210 | + y .= x; ldiv!(Pl, x); @test x ≈ s .\ y |
| 211 | + y .= x; ldiv!(Pr, x); @test x ≈ s .* y |
| 212 | + |
| 213 | + ldiv!(y, Pl, x); @test y ≈ s .\ x |
| 214 | + ldiv!(y, Pr, x); @test y ≈ s .* x |
| 215 | + end |
| 216 | + |
| 217 | + @testset "ComposePreconditioenr" begin |
| 218 | + s1 = rand(n) |
| 219 | + s2 = rand(n) |
| 220 | + |
| 221 | + x = rand(n,n) |
| 222 | + y = rand(n,n) |
| 223 | + |
| 224 | + P1 = Diagonal(s1) |
| 225 | + P2 = Diagonal(s2) |
| 226 | + |
| 227 | + P = LinearSolve.ComposePreconditioner(P1,P2) |
| 228 | + |
| 229 | + # ComposePreconditioner |
| 230 | + ldiv!(y, P, x); @test y ≈ ldiv!(P2, ldiv!(P1, x)) |
| 231 | + y .= x; ldiv!(P, x); @test x ≈ ldiv!(P2, ldiv!(P1, y)) |
| 232 | + end |
| 233 | +end |
| 234 | + |
| 235 | +end # testset |
0 commit comments