1
- using StochasticDiffEq, LinearAlgebra, SparseArrays, Random, Test, DiffEqOperators
1
+ using StochasticDiffEq, LinearAlgebra, SparseArrays, Random, LinearSolve, Test
2
2
using StochasticDiffEq. OrdinaryDiffEq: WOperator, set_gamma!, calc_W!
3
3
4
4
@testset " Derivative Utilities" begin
5
- @testset " WOperator" begin
6
- Random. seed! (123 )
7
- y = zeros (2 ); b = rand (2 )
8
- mm = rand (2 , 2 )
9
- for _J in [rand (2 , 2 ), Diagonal (rand (2 ))]
10
- _Ws = [- mm + 2.0 * _J, - mm/ 2.0 + _J]
11
- for inplace in (true , false ), (_W, W_transform) in zip (_Ws, [false , true ])
12
- W = WOperator {inplace} (mm, 1.0 , DiffEqArrayOperator (_J), b, transform= W_transform)
13
- set_gamma! (W, 2.0 )
14
- @test convert (AbstractMatrix,W) ≈ _W
15
- @test W * b ≈ _W * b
16
- mul! (y, W, b); @test y ≈ _W * b
17
- end
18
- end
19
- end
20
-
21
5
@testset " calc_W!" begin
22
6
A = [- 1.0 0.0 ; 0.0 - 0.5 ]; σ = [0.9 0.0 ; 0.0 0.8 ]
23
7
mm = [2.0 0.0 ; 0.0 1.0 ]
24
8
u0 = [1.0 , 1.0 ]; tmp = zeros (2 )
25
- tspan = (0.0 ,1.0 ); dt = 0.01
26
- concrete_W = - mm + dt * A
9
+ tspan = (0.0 ,1.0 ); dt = 0.01 ; dtgamma = 0.5 dt
10
+ concrete_W = - mm + dtgamma * A
27
11
28
12
# Out-of-place
29
13
_f = (u,p,t) -> A* u; _g = (u,p,t) -> σ* u
@@ -33,7 +17,7 @@ using StochasticDiffEq.OrdinaryDiffEq: WOperator, set_gamma!, calc_W!
33
17
prob = SDEProblem (fun, _g, u0, tspan)
34
18
integrator = init (prob, ImplicitEM (theta= 1 ); adaptive= false , dt= dt)
35
19
W = integrator. cache. nlsolver. cache. W
36
- calc_W! (W, integrator, integrator. cache. nlsolver, integrator. cache, dt , false )
20
+ calc_W! (W, integrator, integrator. cache. nlsolver, integrator. cache, dtgamma , false )
37
21
@test convert (AbstractMatrix, W) ≈ concrete_W
38
22
@test W \ u0 ≈ concrete_W \ u0
39
23
@@ -45,9 +29,17 @@ using StochasticDiffEq.OrdinaryDiffEq: WOperator, set_gamma!, calc_W!
45
29
prob = SDEProblem (fun, _g, u0, tspan)
46
30
integrator = init (prob, ImplicitEM (theta= 1 ); adaptive= false , dt= dt)
47
31
W = integrator. cache. nlsolver. cache. W
48
- calc_W! (W, integrator, integrator. cache. nlsolver, integrator. cache, dt, false )
49
- @test convert (AbstractMatrix, integrator. cache. nlsolver. cache. W) ≈ concrete_W
50
- ldiv! (tmp, lu! (integrator. cache. nlsolver. cache. W), u0); @test tmp ≈ concrete_W \ u0
32
+ calc_W! (W, integrator, integrator. cache. nlsolver, integrator. cache, dtgamma, false )
33
+
34
+ # Did not update because it's an array operator
35
+ # We don't want to build Jacobians when we have operators!
36
+ @test convert (AbstractMatrix, integrator. cache. nlsolver. cache. W) != concrete_W
37
+ ldiv! (tmp, lu! (integrator. cache. nlsolver. cache. W), u0); @test tmp != concrete_W \ u0
38
+
39
+ # But jacobian2W! will update the cache
40
+ StochasticDiffEq. OrdinaryDiffEq. jacobian2W! (integrator. cache. nlsolver. cache. W. _concrete_form, mm, dtgamma, integrator. cache. nlsolver. cache. W. J. A, false )
41
+ @test convert (AbstractMatrix, integrator. cache. nlsolver. cache. W) == concrete_W
42
+ ldiv! (tmp, lu! (integrator. cache. nlsolver. cache. W), u0); @test tmp == concrete_W \ u0
51
43
end
52
44
53
45
@testset " Implicit solver with lazy W" begin
@@ -69,7 +61,7 @@ using StochasticDiffEq.OrdinaryDiffEq: WOperator, set_gamma!, calc_W!
69
61
Random. seed! (0 ); sol2 = solve (prob2, Alg (theta= 1 ); adaptive= false , dt= 0.01 )
70
62
@test sol1 (1.0 ) ≈ sol2 (1.0 ) rtol= 1e-4
71
63
Random. seed! (0 ); sol1_ip = solve (prob1_ip, Alg (theta= 1 ); adaptive= false , dt= 0.01 )
72
- Random. seed! (0 ); sol2_ip = solve (prob2_ip, Alg (theta= 1 ,linsolve = LinSolveFactorize (lu) ); adaptive= false , dt= 0.01 )
64
+ Random. seed! (0 ); sol2_ip = solve (prob2_ip, Alg (theta= 1 ); adaptive= false , dt= 0.01 )
73
65
@test sol1_ip (1.0 ) ≈ sol2_ip (1.0 ) rtol= 1e-4
74
66
end
75
67
@@ -85,7 +77,7 @@ using StochasticDiffEq.OrdinaryDiffEq: WOperator, set_gamma!, calc_W!
85
77
Random. seed! (0 ); sol2 = solve (prob2, SKenCarp (); adaptive= false , dt= 0.01 )
86
78
@test sol1 (1.0 ) ≈ sol2 (1.0 ) rtol= 1e-4
87
79
Random. seed! (0 ); sol1_ip = solve (prob1_ip, SKenCarp (); adaptive= false , dt= 0.01 )
88
- Random. seed! (0 ); sol2_ip = solve (prob2_ip, SKenCarp (linsolve = LinSolveFactorize (lu) ); adaptive= false , dt= 0.01 )
80
+ Random. seed! (0 ); sol2_ip = solve (prob2_ip, SKenCarp (); adaptive= false , dt= 0.01 )
89
81
@test sol1_ip (1.0 ) ≈ sol2_ip (1.0 ) rtol= 1e-3
90
82
end
91
83
end
0 commit comments