|
| 1 | +using BoundaryValueDiffEqFIRK |
| 2 | +using Test |
| 3 | + |
| 4 | +# Index-1 DAE BVPs solved via unprojected collocation (Ascher & Spiteri 1994). |
| 5 | +# The algebraic constraint is enforced exactly at the mesh points, so accuracy for |
| 6 | +# algebraic variables is only checked there: the continuous interpolant is not |
| 7 | +# accurate for algebraic components, which is also why mesh adaptivity is not |
| 8 | +# supported for DAEs (the defect estimate cannot converge). |
| 9 | +# |
| 10 | +# LobattoIIIa and LobattoIIIb are excluded: their tableau structure leaves the |
| 11 | +# algebraic components of the stages underdetermined, so they cannot solve DAEs. |
| 12 | + |
| 13 | +@testset "Simple index-1 DAE" begin |
| 14 | + using BoundaryValueDiffEqFIRK, SciMLBase |
| 15 | + using LinearAlgebra |
| 16 | + |
| 17 | + nested = false |
| 18 | + |
| 19 | + # u1' = u2, 0 = u2 - cos(t) with u1(0) = 0 |
| 20 | + # Analytic solution: u1 = sin(t), u2 = cos(t) |
| 21 | + function f1!(du, u, p, t) |
| 22 | + du[1] = u[2] |
| 23 | + du[2] = u[2] - cos(t) |
| 24 | + end |
| 25 | + f1(u, p, t) = [u[2], u[2] - cos(t)] |
| 26 | + function bc1!(res, u, p, t) |
| 27 | + res[1] = u(0.0)[1] |
| 28 | + res[2] = u(0.0)[2] - 1.0 |
| 29 | + end |
| 30 | + bc1(u, p, t) = [u(0.0)[1], u(0.0)[2] - 1.0] |
| 31 | + |
| 32 | + mass_matrix = [1.0 0.0; 0.0 0.0] |
| 33 | + tspan = (0.0, pi / 2) |
| 34 | + prob_iip = BVProblem(BVPFunction(f1!, bc1!; mass_matrix), [0.0, 1.0], tspan) |
| 35 | + prob_oop = BVProblem(BVPFunction(f1, bc1; mass_matrix), [0.0, 1.0], tspan) |
| 36 | + |
| 37 | + @testset "$(nameof(typeof(alg))), $(SciMLBase.isinplace(prob) ? "iip" : "oop")" for alg in |
| 38 | + ( |
| 39 | + RadauIIa3(; nested_nlsolve = nested), RadauIIa5(; nested_nlsolve = nested), |
| 40 | + RadauIIa7(; nested_nlsolve = nested), LobattoIIIc4(; nested_nlsolve = nested), |
| 41 | + ), |
| 42 | + prob in (prob_iip, prob_oop) |
| 43 | + |
| 44 | + sol = solve(prob, alg; dt = 0.01, adaptive = false) |
| 45 | + @test SciMLBase.successful_retcode(sol) |
| 46 | + @test maximum(abs(sol.u[i][1] - sin(sol.t[i])) for i in eachindex(sol.t)) < 1.0e-10 |
| 47 | + @test maximum(abs(sol.u[i][2] - cos(sol.t[i])) for i in eachindex(sol.t)) < 1.0e-12 |
| 48 | + end |
| 49 | +end |
| 50 | + |
| 51 | +@testset "Ascher & Spiteri example problem 1" begin |
| 52 | + using BoundaryValueDiffEqFIRK, SciMLBase |
| 53 | + using LinearAlgebra |
| 54 | + |
| 55 | + nested = false |
| 56 | + |
| 57 | + # Singular index-1 BVDAE from the Ascher & Spiteri paper. |
| 58 | + # Analytic solution: [sin(t), sin(t), 1, 0] |
| 59 | + function f2!(du, u, p, t) |
| 60 | + e = 2.7 |
| 61 | + du[1] = (1 + u[2] - sin(t)) * u[4] + cos(t) |
| 62 | + du[2] = cos(t) |
| 63 | + du[3] = u[4] |
| 64 | + du[4] = (u[1] - sin(t)) * (u[4] - e^t) |
| 65 | + end |
| 66 | + function f2(u, p, t) |
| 67 | + e = 2.7 |
| 68 | + return [ |
| 69 | + (1 + u[2] - sin(t)) * u[4] + cos(t), cos(t), |
| 70 | + u[4], (u[1] - sin(t)) * (u[4] - e^t), |
| 71 | + ] |
| 72 | + end |
| 73 | + function bc2!(res, u, p, t) |
| 74 | + res[1] = u(0.0)[1] |
| 75 | + res[2] = u(0.0)[3] - 1.0 |
| 76 | + res[3] = u(1.0)[2] - sin(1.0) |
| 77 | + res[4] = u(0.0)[4] |
| 78 | + end |
| 79 | + bc2(u, p, t) = [u(0.0)[1], u(0.0)[3] - 1.0, u(1.0)[2] - sin(1.0), u(0.0)[4]] |
| 80 | + f2_analytic(t) = [sin(t), sin(t), 1.0, 0.0] |
| 81 | + |
| 82 | + mass_matrix = [ |
| 83 | + 1.0 0.0 0.0 0.0; 0.0 1.0 0.0 0.0; |
| 84 | + 0.0 0.0 1.0 0.0; 0.0 0.0 0.0 0.0 |
| 85 | + ] |
| 86 | + tspan = (0.0, 1.0) |
| 87 | + prob_iip = BVProblem(BVPFunction(f2!, bc2!; mass_matrix), zeros(4), tspan) |
| 88 | + prob_oop = BVProblem(BVPFunction(f2, bc2; mass_matrix), zeros(4), tspan) |
| 89 | + |
| 90 | + @testset "RadauIIa5, $(SciMLBase.isinplace(prob) ? "iip" : "oop")" for prob in |
| 91 | + (prob_iip, prob_oop) |
| 92 | + |
| 93 | + sol = solve(prob, RadauIIa5(; nested_nlsolve = nested); dt = 0.01, adaptive = false) |
| 94 | + @test SciMLBase.successful_retcode(sol) |
| 95 | + err = maximum( |
| 96 | + maximum(abs.(sol.u[i] .- f2_analytic(sol.t[i]))) for i in eachindex(sol.t) |
| 97 | + ) |
| 98 | + @test err < 1.0e-8 |
| 99 | + end |
| 100 | +end |
| 101 | + |
| 102 | +@testset "Mesh adaptivity is not supported for DAEs" begin |
| 103 | + using BoundaryValueDiffEqFIRK, SciMLBase |
| 104 | + using LinearAlgebra |
| 105 | + |
| 106 | + nested = false |
| 107 | + |
| 108 | + function f3!(du, u, p, t) |
| 109 | + du[1] = u[2] |
| 110 | + du[2] = u[2] - cos(t) |
| 111 | + end |
| 112 | + function bc3!(res, u, p, t) |
| 113 | + res[1] = u(0.0)[1] |
| 114 | + res[2] = u(0.0)[2] - 1.0 |
| 115 | + end |
| 116 | + mass_matrix = [1.0 0.0; 0.0 0.0] |
| 117 | + prob = BVProblem(BVPFunction(f3!, bc3!; mass_matrix), [0.0, 1.0], (0.0, pi / 2)) |
| 118 | + |
| 119 | + @test_throws ArgumentError solve(prob, RadauIIa5(; nested_nlsolve = nested); dt = 0.05) |
| 120 | + @test_throws ArgumentError solve( |
| 121 | + prob, RadauIIa5(; nested_nlsolve = nested); dt = 0.05, adaptive = true |
| 122 | + ) |
| 123 | +end |
0 commit comments