|
| 1 | +using OrdinaryDiffEq |
| 2 | +using Test |
| 3 | + |
| 4 | +# Test for SubArray support with ODEProblem |
| 5 | +# Regression test for https://github.com/SciML/OrdinaryDiffEq.jl/issues/2900 |
| 6 | +# Fixed in https://github.com/SciML/DiffEqBase.jl/pull/1219 |
| 7 | + |
| 8 | +@testset "SubArray as initial conditions" begin |
| 9 | + # Test 1: Simple ODE with SubArray initial conditions |
| 10 | + @testset "Basic ODE with SubArray" begin |
| 11 | + f(du, u, p, t) = du .= u |
| 12 | + u0_full = ones(10) |
| 13 | + u0 = @view u0_full[1:5] |
| 14 | + |
| 15 | + # This should not throw NoFunctionWrapperFoundError |
| 16 | + prob = ODEProblem(f, u0, (0.0, 1.0)) |
| 17 | + sol = solve(prob, Tsit5()) |
| 18 | + |
| 19 | + @test sol.retcode == ReturnCode.Success |
| 20 | + @test length(sol[end]) == 5 |
| 21 | + end |
| 22 | + |
| 23 | + # Test 2: Simple pendulum with SubArray initial conditions |
| 24 | + @testset "Simple pendulum with SubArray" begin |
| 25 | + g = 9.81 |
| 26 | + L = 1.00 |
| 27 | + |
| 28 | + # Initial Conditions as SubArray |
| 29 | + u₀ = @view [0, π / 60][:] # Initial speed and initial angle |
| 30 | + tspan = (0.0, 6.3) |
| 31 | + |
| 32 | + # Define the pendulum problem |
| 33 | + function simplependulum(du, u, p, t) |
| 34 | + θ = u[1] |
| 35 | + dθ = u[2] |
| 36 | + du[1] = dθ |
| 37 | + du[2] = -(g / L) * θ |
| 38 | + end |
| 39 | + |
| 40 | + # This should not throw NoFunctionWrapperFoundError |
| 41 | + prob = ODEProblem(simplependulum, u₀, tspan) |
| 42 | + integrator = init(prob, Tsit5(); reltol = 1e-6) |
| 43 | + |
| 44 | + @test integrator !== nothing |
| 45 | + |
| 46 | + # Solve and verify the solution |
| 47 | + sol = solve(prob, Tsit5(); reltol = 1e-6) |
| 48 | + @test sol.retcode == ReturnCode.Success |
| 49 | + @test length(sol[end]) == 2 |
| 50 | + end |
| 51 | + |
| 52 | + # Test 3: Various SubArray slicing patterns |
| 53 | + @testset "Different SubArray patterns" begin |
| 54 | + f(du, u, p, t) = du .= 2u |
| 55 | + |
| 56 | + # Test different ways to create SubArrays |
| 57 | + @testset "range slice" begin |
| 58 | + arr = [1.0, 2.0, 3.0] |
| 59 | + u0 = @view arr[1:2] |
| 60 | + prob = ODEProblem(f, u0, (0.0, 1.0)) |
| 61 | + sol = solve(prob, Tsit5()) |
| 62 | + @test sol.retcode == ReturnCode.Success |
| 63 | + end |
| 64 | + |
| 65 | + @testset "colon slice" begin |
| 66 | + arr = [1.0, 2.0, 3.0] |
| 67 | + u0 = @view arr[:] |
| 68 | + prob = ODEProblem(f, u0, (0.0, 1.0)) |
| 69 | + sol = solve(prob, Tsit5()) |
| 70 | + @test sol.retcode == ReturnCode.Success |
| 71 | + end |
| 72 | + |
| 73 | + @testset "offset range" begin |
| 74 | + arr = [1.0, 2.0, 3.0] |
| 75 | + u0 = @view arr[2:3] |
| 76 | + prob = ODEProblem(f, u0, (0.0, 1.0)) |
| 77 | + sol = solve(prob, Tsit5()) |
| 78 | + @test sol.retcode == ReturnCode.Success |
| 79 | + end |
| 80 | + |
| 81 | + @testset "view of constructed array" begin |
| 82 | + arr = ones(5) |
| 83 | + u0 = @view arr[1:3] |
| 84 | + prob = ODEProblem(f, u0, (0.0, 1.0)) |
| 85 | + sol = solve(prob, Tsit5()) |
| 86 | + @test sol.retcode == ReturnCode.Success |
| 87 | + end |
| 88 | + end |
| 89 | +end |
0 commit comments