1- using Test
1+ using Test
22using OrdinaryDiffEq
33using SparseArrays
44using LinearAlgebra
55
6+ # # in-place
67# https://github.com/JuliaDiffEq/SparseDiffTools.jl/blob/master/test/test_integration.jl
7- function f (dx,x,p,t)
8- for i in 2 : length (x)- 1
9- dx[i] = x[i- 1 ] - 2 x[i] + x[i+ 1 ]
10- end
11- dx[1 ] = - 2 x[1 ] + x[2 ]
12- dx[end ] = x[end - 1 ] - 2 x[end ]
13- nothing
8+ function f_ip (dx,x,p,t)
9+ for i in 2 : length (x)- 1
10+ dx[i] = x[i- 1 ] - 2 x[i] + x[i+ 1 ]
11+ end
12+ dx[1 ] = - 2 x[1 ] + x[2 ]
13+ dx[end ] = x[end - 1 ] - 2 x[end ]
14+ nothing
15+ end
16+
17+ # # out-of-place
18+ function f_oop (x,p,t)
19+ dx = similar (x)
20+ for i in 2 : length (x)- 1
21+ dx[i] = x[i- 1 ] - 2 x[i] + x[i+ 1 ]
1422 end
23+ dx[1 ] = - 2 x[1 ] + x[2 ]
24+ dx[end ] = x[end - 1 ] - 2 x[end ]
25+ return dx
26+ end
27+
1528
1629function second_derivative_stencil (N)
1730 A = zeros (N,N)
@@ -34,22 +47,29 @@ jac_sp = sparse(generate_sparsity_pattern(10))
3447colors = repeat (1 : 3 ,10 )[1 : 10 ]
3548u0= [1. ,2. ,3 ,4 ,5 ,5 ,4 ,3 ,2 ,1 ]
3649tspan= (0. ,10. )
37- odefun_sp= ODEFunction (f,colorvec= colors,jac_prototype= jac_sp)
38- prob_sp = ODEProblem (odefun_sp,u0,tspan)
39- prob_std = ODEProblem (f,u0,tspan)
40-
41- sol_sp= solve (prob_sp,Rodas5 (autodiff= false ),abstol= 1e-10 ,reltol= 1e-10 )
42- @test sol_sp. retcode== :Success # test sparse finitediff
43- sol= solve (prob_std,Rodas5 (autodiff= false ),abstol= 1e-10 ,reltol= 1e-10 )
44- @test sol_sp. u[end ]≈ sol. u[end ] atol= 1e-10
45- @test length (sol_sp. t)== length (sol. t)
46-
47- sol_sp= solve (prob_sp,Rodas5 (autodiff= false ))
48- sol= solve (prob_std,Rodas5 (autodiff= false ))
49- @test sol_sp. u[end ]≈ sol. u[end ]
50- @test length (sol_sp. t)== length (sol. t)
51-
52- sol_sp= solve (prob_sp,Rodas5 ())
53- sol= solve (prob_std,Rodas5 ())
54- @test sol_sp. u[end ]≈ sol. u[end ]
55- @test length (sol_sp. t)== length (sol. t)
50+
51+ for f in [f_ip, f_oop]
52+ odefun_std = ODEFunction (f)
53+ prob_std = ODEProblem (odefun_std,u0,tspan)
54+
55+ for ad in [true , false ]
56+ for tol in [nothing , 1e-10 ]
57+ sol_std= solve (prob_std,Rodas5 (autodiff= ad),reltol= tol,abstol= tol)
58+ @test sol_std. retcode== :Success
59+ for prob in map (f-> ODEProblem (f,u0,tspan),
60+ [ODEFunction (f,colorvec= colors,jac_prototype= jac_sp),
61+ ODEFunction (f,jac_prototype= jac_sp),
62+ # ODEFunction(f,colorvec=colors) # this one fails both the u[end] and length tests
63+ ])
64+ sol= solve (prob,Rodas5 (autodiff= ad),reltol= tol,abstol= tol)
65+ @test sol. retcode== :Success
66+ if tol != nothing
67+ @test sol_std. u[end ]≈ sol. u[end ] atol= tol
68+ else
69+ @test sol_std. u[end ]≈ sol. u[end ]
70+ end
71+ @test length (sol_std. t)== length (sol. t)
72+ end
73+ end
74+ end
75+ end
0 commit comments