11using StochasticDiffEq, SparseArrays, Test, Random
22
3- Random. seed! (42 )
4-
5- # 4-state system driven by 2 independent Wiener processes.
6- # Only states 1 and 3 are coupled to noise; states 2 and 4 are purely deterministic.
7- # The noise matrix is 4×2 sparse: g[1,1] = 0.1*u[1], g[3,2] = 0.1*u[3], rest zero.
8-
93function f_sparse! (du, u, p, t)
104 return du .= - u
115end
@@ -25,23 +19,29 @@ noise_prototype[3, 2] = 1.0
2519prob = SDEProblem (f_sparse!, g_sparse!, u0, tspan, noise_rate_prototype = noise_prototype)
2620
2721@testset " Sparse noise_rate_prototype — EM IIP" begin
22+ Random. seed! (42 )
2823 sol = solve (prob, EM (), dt = 0.01 )
2924 @test sol. retcode == ReturnCode. Success
3025 @test length (sol) > 1
31- # States 2 and 4 have zero noise rows → evolve purely as du/dt = -u → u(1) = exp(-1 )
26+ # Dimensions 2 and 4 have no noise: pure drift exp(-t )
3227 @test sol. u[end ][2 ] ≈ exp (- 1.0 ) rtol = 0.02
3328 @test sol. u[end ][4 ] ≈ exp (- 1.0 ) rtol = 0.02
29+ # Dimensions 1 and 3 receive noise: path must deviate from pure drift
30+ @test ! isapprox (sol. u[end ][1 ], exp (- 1.0 ), rtol = 0.005 )
31+ @test ! isapprox (sol. u[end ][3 ], exp (- 1.0 ), rtol = 0.005 )
3432end
3533
3634@testset " Sparse noise_rate_prototype — EulerHeun IIP" begin
35+ Random. seed! (42 )
3736 sol = solve (prob, EulerHeun (), dt = 0.01 )
3837 @test sol. retcode == ReturnCode. Success
3938 @test length (sol) > 1
4039 @test sol. u[end ][2 ] ≈ exp (- 1.0 ) rtol = 0.02
4140 @test sol. u[end ][4 ] ≈ exp (- 1.0 ) rtol = 0.02
41+ @test ! isapprox (sol. u[end ][1 ], exp (- 1.0 ), rtol = 0.005 )
42+ @test ! isapprox (sol. u[end ][3 ], exp (- 1.0 ), rtol = 0.005 )
4243end
4344
44- # OOP variant
4545function f_sparse_oop (u, p, t)
4646 return - u
4747end
5656prob_oop = SDEProblem (f_sparse_oop, g_sparse_oop, u0, tspan, noise_rate_prototype = noise_prototype)
5757
5858@testset " Sparse noise_rate_prototype — EM OOP" begin
59+ Random. seed! (42 )
5960 sol = solve (prob_oop, EM (), dt = 0.01 )
6061 @test sol. retcode == ReturnCode. Success
6162 @test sol. u[end ][2 ] ≈ exp (- 1.0 ) rtol = 0.02
6263 @test sol. u[end ][4 ] ≈ exp (- 1.0 ) rtol = 0.02
64+ @test ! isapprox (sol. u[end ][1 ], exp (- 1.0 ), rtol = 0.005 )
65+ @test ! isapprox (sol. u[end ][3 ], exp (- 1.0 ), rtol = 0.005 )
6366end
0 commit comments