-
-
Notifications
You must be signed in to change notification settings - Fork 235
Fix inability to use sparse Jacobian with colorvec for OOP #1049
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
343281d
b7b1cb6
3e55823
f60dfd8
c68e2ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,15 +3,28 @@ using OrdinaryDiffEq | |
| using SparseArrays | ||
| using LinearAlgebra | ||
|
|
||
| ## in-place | ||
| #https://github.com/JuliaDiffEq/SparseDiffTools.jl/blob/master/test/test_integration.jl | ||
| function f(dx,x,p,t) | ||
| for i in 2:length(x)-1 | ||
| dx[i] = x[i-1] - 2x[i] + x[i+1] | ||
| end | ||
| dx[1] = -2x[1] + x[2] | ||
| dx[end] = x[end-1] - 2x[end] | ||
| nothing | ||
| function f_ip(dx,x,p,t) | ||
| for i in 2:length(x)-1 | ||
| dx[i] = x[i-1] - 2x[i] + x[i+1] | ||
| end | ||
| dx[1] = -2x[1] + x[2] | ||
| dx[end] = x[end-1] - 2x[end] | ||
| nothing | ||
| end | ||
|
|
||
| ## out-of-place | ||
| function f_oop(x,p,t) | ||
| dx = similar(x) | ||
| for i in 2:length(x)-1 | ||
| dx[i] = x[i-1] - 2x[i] + x[i+1] | ||
| end | ||
| dx[1] = -2x[1] + x[2] | ||
| dx[end] = x[end-1] - 2x[end] | ||
| return dx | ||
| end | ||
|
|
||
|
|
||
| function second_derivative_stencil(N) | ||
| A = zeros(N,N) | ||
|
|
@@ -34,22 +47,54 @@ jac_sp = sparse(generate_sparsity_pattern(10)) | |
| colors = repeat(1:3,10)[1:10] | ||
| u0=[1.,2.,3,4,5,5,4,3,2,1] | ||
| tspan=(0.,10.) | ||
| odefun_sp= ODEFunction(f,colorvec=colors,jac_prototype=jac_sp) | ||
| prob_sp = ODEProblem(odefun_sp,u0,tspan) | ||
| prob_std = ODEProblem(f,u0,tspan) | ||
|
|
||
| sol_sp=solve(prob_sp,Rodas5(autodiff=false),abstol=1e-10,reltol=1e-10) | ||
| @test sol_sp.retcode==:Success#test sparse finitediff | ||
| sol=solve(prob_std,Rodas5(autodiff=false),abstol=1e-10,reltol=1e-10) | ||
| @test sol_sp.u[end]≈sol.u[end] atol=1e-10 | ||
| @test length(sol_sp.t)==length(sol.t) | ||
|
|
||
| sol_sp=solve(prob_sp,Rodas5(autodiff=false)) | ||
| sol=solve(prob_std,Rodas5(autodiff=false)) | ||
| @test sol_sp.u[end]≈sol.u[end] | ||
| @test length(sol_sp.t)==length(sol.t) | ||
|
|
||
| sol_sp=solve(prob_sp,Rodas5()) | ||
| sol=solve(prob_std,Rodas5()) | ||
| @test sol_sp.u[end]≈sol.u[end] | ||
| @test length(sol_sp.t)==length(sol.t) | ||
|
|
||
| for f in [f_oop, f_ip] | ||
| odefun_std = ODEFunction(f) | ||
| prob_std = ODEProblem(odefun_std,u0,tspan) | ||
|
|
||
| for ad in [true, false] | ||
| for Solver in [Rodas5, Trapezoid, KenCarp4] | ||
| for tol in [nothing, 1e-10] | ||
| # @show f,ad,Solver,tol | ||
| sol_std=solve(prob_std,Solver(autodiff=ad),reltol=tol,abstol=tol) | ||
| @test sol_std.retcode==:Success | ||
| for (i,prob) in enumerate(map(f->ODEProblem(f,u0,tspan), | ||
| [ODEFunction(f,colorvec=colors,jac_prototype=jac_sp), | ||
| ODEFunction(f,jac_prototype=jac_sp), | ||
| ODEFunction(f,colorvec=colors) | ||
| ])) | ||
| isbroken = i==3 && ( | ||
| (f, ad, tol) == (f_oop, true, nothing) || | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all with AD fail?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only those with
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had a closer look at the broken tests: |
||
| (f, ad, Solver, tol) == (f_oop, false, Trapezoid, nothing) || | ||
| (f, Solver, tol) == (f_ip, Trapezoid, nothing) || | ||
| (f, Solver) == (f_oop, Rodas5) || | ||
| (f, Solver) == (f_ip, Rodas5) || | ||
| (f, Solver) == (f_oop, KenCarp4) || | ||
| (f, Solver) == (f_ip, KenCarp4) | ||
| ) | ||
|
Comment on lines
+67
to
+75
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These failing tests need investigation.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't expect colors without sparsity to work.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO, the color stuff should be independent of the matrix type of J.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are still "broken". |
||
| # @show i | ||
| sol=solve(prob,Solver(autodiff=ad),reltol=tol,abstol=tol) | ||
| @test sol.retcode==:Success | ||
| if tol !=nothing | ||
| if isbroken | ||
| @test_broken sol_std.u[end]≈sol.u[end] atol=tol | ||
| else | ||
| @test sol_std.u[end]≈sol.u[end] atol=tol | ||
| end | ||
| else | ||
| if isbroken | ||
| @test_broken sol_std.u[end]≈sol.u[end] | ||
| else | ||
| @test sol_std.u[end]≈sol.u[end] | ||
| end | ||
| end | ||
| if isbroken | ||
| @test_broken length(sol_std.t)==length(sol.t) | ||
| else | ||
| @test length(sol_std.t)==length(sol.t) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
Uh oh!
There was an error while loading. Please reload this page.