|
| 1 | +using ModelingToolkit |
| 2 | +using LinearSolve |
| 3 | +using SciMLBase |
| 4 | +using StaticArrays |
| 5 | +using SparseArrays |
| 6 | +using Test |
| 7 | +using ModelingToolkit: t_nounits as t, D_nounits as D, SystemCompatibilityError |
| 8 | + |
| 9 | +@testset "Rejects non-affine systems" begin |
| 10 | + @variables x y |
| 11 | + @mtkbuild sys = System([0 ~ x^2 + y, 0 ~ x - y]) |
| 12 | + @test_throws SystemCompatibilityError LinearProblem(sys, nothing) |
| 13 | +end |
| 14 | + |
| 15 | +@variables x[1:3] [irreducible = true] |
| 16 | +@parameters p[1:3, 1:3] q[1:3] |
| 17 | + |
| 18 | +@mtkbuild sys = System([p * x ~ q]) |
| 19 | +# sanity check |
| 20 | +@test length(unknowns(sys)) == length(equations(sys)) == 3 |
| 21 | +A = Float64[1 2 3; 4 3.5 1.7; 5.2 1.8 9.7] |
| 22 | +b = Float64[2, 5, 8] |
| 23 | +ps = [p => A, q => b] |
| 24 | + |
| 25 | +@testset "Basics" begin |
| 26 | + # Ensure it works without providing `u0` |
| 27 | + prob = LinearProblem(sys, ps) |
| 28 | + @test prob.u0 === nothing |
| 29 | + @test SciMLBase.isinplace(prob) |
| 30 | + @test prob.A ≈ A |
| 31 | + @test prob.b ≈ b |
| 32 | + @test eltype(prob.A) == Float64 |
| 33 | + @test eltype(prob.b) == Float64 |
| 34 | + |
| 35 | + @test prob.ps[p * q] ≈ A * b |
| 36 | + |
| 37 | + sol = solve(prob) |
| 38 | + # https://github.com/SciML/LinearSolve.jl/issues/532 |
| 39 | + @test_broken SciMLBase.successful_retcode(sol) |
| 40 | + @test prob.A * sol.u - prob.b≈zeros(3) atol=1e-10 |
| 41 | + |
| 42 | + A2 = rand(3, 3) |
| 43 | + b2 = rand(3) |
| 44 | + @testset "remake" begin |
| 45 | + prob2 = remake(prob; p = [p => A2, q => b2]) |
| 46 | + @test prob2.ps[p] ≈ A2 |
| 47 | + @test prob2.ps[q] ≈ b2 |
| 48 | + @test prob2.A ≈ A2 |
| 49 | + @test prob2.b ≈ b2 |
| 50 | + end |
| 51 | + |
| 52 | + prob.ps[p] = A2 |
| 53 | + @test prob.A ≈ A2 |
| 54 | + prob.ps[q] = b2 |
| 55 | + @test prob.b ≈ b2 |
| 56 | + A2[1, 1] = prob.ps[p[1, 1]] = 1.5 |
| 57 | + @test prob.A ≈ A2 |
| 58 | + b2[1] = prob.ps[q[1]] = 2.5 |
| 59 | + @test prob.b ≈ b2 |
| 60 | + |
| 61 | + @testset "expression = Val{true}" begin |
| 62 | + prob3e = LinearProblem(sys, ps; expression = Val{true}) |
| 63 | + @test prob3e isa Expr |
| 64 | + prob3 = eval(prob3e) |
| 65 | + |
| 66 | + @test prob3.u0 === nothing |
| 67 | + @test SciMLBase.isinplace(prob3) |
| 68 | + @test prob3.A ≈ A |
| 69 | + @test prob3.b ≈ b |
| 70 | + @test eltype(prob3.A) == Float64 |
| 71 | + @test eltype(prob3.b) == Float64 |
| 72 | + |
| 73 | + @test prob3.ps[p * q] ≈ A * b |
| 74 | + |
| 75 | + sol = solve(prob3) |
| 76 | + # https://github.com/SciML/LinearSolve.jl/issues/532 |
| 77 | + @test_broken SciMLBase.successful_retcode(sol) |
| 78 | + @test prob3.A * sol.u - prob3.b≈zeros(3) atol=1e-10 |
| 79 | + end |
| 80 | +end |
| 81 | + |
| 82 | +@testset "With `u0`" begin |
| 83 | + prob = LinearProblem(sys, [x => ones(3); ps]) |
| 84 | + @test prob.u0 ≈ ones(3) |
| 85 | + @test SciMLBase.isinplace(prob) |
| 86 | + @test eltype(prob.u0) == Float64 |
| 87 | + |
| 88 | + # Observed should work |
| 89 | + @test prob[x[1] + x[2]] ≈ 2.0 |
| 90 | + |
| 91 | + @testset "expression = Val{true}" begin |
| 92 | + prob3e = LinearProblem(sys, [x => ones(3); ps]; expression = Val{true}) |
| 93 | + @test prob3e isa Expr |
| 94 | + prob3 = eval(prob3e) |
| 95 | + @test prob3.u0 ≈ ones(3) |
| 96 | + @test eltype(prob3.u0) == Float64 |
| 97 | + end |
| 98 | +end |
| 99 | + |
| 100 | +@testset "SArray OOP form" begin |
| 101 | + prob = LinearProblem(sys, SVector{2}(ps)) |
| 102 | + @test prob.A isa SMatrix{3, 3, Float64} |
| 103 | + @test prob.b isa SVector{3, Float64} |
| 104 | + @test !SciMLBase.isinplace(prob) |
| 105 | + @test prob.ps[p * q] ≈ A * b |
| 106 | + |
| 107 | + sol = solve(prob) |
| 108 | + # https://github.com/SciML/LinearSolve.jl/issues/532 |
| 109 | + @test_broken SciMLBase.successful_retcode(sol) |
| 110 | + @test prob.A * sol.u - prob.b≈zeros(3) atol=1e-10 |
| 111 | + |
| 112 | + A2 = rand(3, 3) |
| 113 | + b2 = rand(3) |
| 114 | + @testset "remake" begin |
| 115 | + prob2 = remake(prob; p = [p => A2, q => b2]) |
| 116 | + # Despite passing `Array` to `remake` |
| 117 | + @test prob2.A isa SMatrix{3, 3, Float64} |
| 118 | + @test prob2.b isa SVector{3, Float64} |
| 119 | + @test prob2.ps[p] ≈ A2 |
| 120 | + @test prob2.ps[q] ≈ b2 |
| 121 | + @test prob2.A ≈ A2 |
| 122 | + @test prob2.b ≈ b2 |
| 123 | + end |
| 124 | + |
| 125 | + @testset "expression = Val{true}" begin |
| 126 | + prob3e = LinearProblem(sys, SVector{2}(ps); expression = Val{true}) |
| 127 | + @test prob3e isa Expr |
| 128 | + prob3 = eval(prob3e) |
| 129 | + @test prob3.A isa SMatrix{3, 3, Float64} |
| 130 | + @test prob3.b isa SVector{3, Float64} |
| 131 | + @test !SciMLBase.isinplace(prob3) |
| 132 | + @test prob3.ps[p * q] ≈ A * b |
| 133 | + |
| 134 | + sol = solve(prob3) |
| 135 | + # https://github.com/SciML/LinearSolve.jl/issues/532 |
| 136 | + @test_broken SciMLBase.successful_retcode(sol) |
| 137 | + @test prob3.A * sol.u - prob3.b≈zeros(3) atol=1e-10 |
| 138 | + end |
| 139 | +end |
| 140 | + |
| 141 | +@testset "u0_constructor" begin |
| 142 | + prob = LinearProblem{false}(sys, ps; u0_constructor = x -> SArray{Tuple{size(x)...}}(x)) |
| 143 | + @test prob.A isa SMatrix{3, 3, Float64} |
| 144 | + @test prob.b isa SVector{3, Float64} |
| 145 | + @test prob.ps[p * q] ≈ A * b |
| 146 | +end |
| 147 | + |
| 148 | +@testset "sparse form" begin |
| 149 | + prob = LinearProblem(sys, ps; sparse = true) |
| 150 | + @test issparse(prob.A) |
| 151 | + @test !issparse(prob.b) |
| 152 | + |
| 153 | + sol = solve(prob) |
| 154 | + # This might end up failing because of |
| 155 | + # https://github.com/SciML/LinearSolve.jl/issues/532 |
| 156 | + @test SciMLBase.successful_retcode(sol) |
| 157 | + |
| 158 | + A2 = rand(3, 3) |
| 159 | + prob.ps[p] = A2 |
| 160 | + @test prob.A ≈ A2 |
| 161 | + b2 = rand(3) |
| 162 | + prob.ps[q] = b2 |
| 163 | + @test prob.b ≈ b2 |
| 164 | + |
| 165 | + A2 = rand(3, 3) |
| 166 | + b2 = rand(3) |
| 167 | + @testset "remake" begin |
| 168 | + prob2 = remake(prob; p = [p => A2, q => b2]) |
| 169 | + @test issparse(prob2.A) |
| 170 | + @test !issparse(prob2.b) |
| 171 | + @test prob2.ps[p] ≈ A2 |
| 172 | + @test prob2.ps[q] ≈ b2 |
| 173 | + @test prob2.A ≈ A2 |
| 174 | + @test prob2.b ≈ b2 |
| 175 | + end |
| 176 | + |
| 177 | + @testset "expression = Val{true}" begin |
| 178 | + prob3e = LinearProblem(sys, ps; sparse = true, expression = Val{true}) |
| 179 | + @test prob3e isa Expr |
| 180 | + prob3 = eval(prob3e) |
| 181 | + @test issparse(prob3.A) |
| 182 | + @test !issparse(prob3.b) |
| 183 | + |
| 184 | + sol = solve(prob3) |
| 185 | + # This might end up failing because of |
| 186 | + # https://github.com/SciML/LinearSolve.jl/issues/532 |
| 187 | + @test SciMLBase.successful_retcode(sol) |
| 188 | + end |
| 189 | +end |
0 commit comments