PETSc SNES Example 2

This implements src/snes/examples/tutorials/ex2.c from PETSc and examples/SNES_ex2.jl from PETSc.jl using automatic sparsity detection and automatic differentiation using NonlinearSolve.jl.

This solves the equations sequentially. Newton method to solve u'' + u^{2} = f, sequentially.

using NonlinearSolve, PETSc, LinearAlgebra, SparseConnectivityTracer, BenchmarkTools

u0 = fill(0.5, 128)

function form_residual!(resid, x, _)
    n = length(x)
    xp = LinRange(0.0, 1.0, n)
    F = 6xp .+ (xp .+ 1e-12) .^ 6

    dx = 1 / (n - 1)
    resid[1] = x[1]
    for i in 2:(n - 1)
        resid[i] = (x[i - 1] - 2x[i] + x[i + 1]) / dx^2 + x[i] * x[i] - F[i]
    end
    resid[n] = x[n] - 1

    return
end
form_residual! (generic function with 1 method)

To use automatic sparsity detection, we need to specify sparsity keyword argument to NonlinearFunction. See Automatic Sparsity Detection for more details.

nlfunc_dense = NonlinearFunction(form_residual!)
nlfunc_sparse = NonlinearFunction(form_residual!; sparsity = TracerSparsityDetector())

nlprob_dense = NonlinearProblem(nlfunc_dense, u0)
nlprob_sparse = NonlinearProblem(nlfunc_sparse, u0)
NonlinearProblem with uType Vector{Float64}. In-place: true
u0: 128-element Vector{Float64}:
 0.5
 0.5
 0.5
 0.5
 0.5
 0.5
 0.5
 0.5
 0.5
 0.5
 ⋮
 0.5
 0.5
 0.5
 0.5
 0.5
 0.5
 0.5
 0.5
 0.5

Now we can solve the problem using PETScSNES or with one of the native NonlinearSolve.jl solvers.

sol_dense_nr = solve(nlprob_dense, NewtonRaphson(); abstol = 1e-8)
sol_dense_snes = solve(nlprob_dense, PETScSNES(); abstol = 1e-8)
sol_dense_nr .- sol_dense_snes
128-element Vector{Float64}:
  4.1386421557519005e-19
 -2.1053427420479262e-17
 -4.252105395216588e-17
 -6.398825696737886e-17
 -8.545545998259185e-17
 -1.0692266299780484e-16
 -1.2839664227659586e-16
 -1.4986384529180885e-16
 -1.713039432527097e-16
 -1.927711462679227e-16
  ⋮
 -6.661338147750939e-16
 -6.661338147750939e-16
 -5.551115123125783e-16
 -4.440892098500626e-16
 -3.3306690738754696e-16
 -2.220446049250313e-16
 -1.1102230246251565e-16
 -1.1102230246251565e-16
  0.0
sol_sparse_nr = solve(nlprob_sparse, NewtonRaphson(); abstol = 1e-8)
sol_sparse_snes = solve(nlprob_sparse, PETScSNES(); abstol = 1e-8)
sol_sparse_nr .- sol_sparse_snes
128-element Vector{Float64}:
 -1.9281866869109483e-42
 -6.886907256769496e-18
 -1.3773602755302178e-17
 -2.0660827649426894e-17
 -2.755228770828788e-17
 -3.4436971503570835e-17
 -4.132165529885379e-17
 -4.819278656698067e-17
 -5.51316804708879e-17
 -6.196215415754658e-17
  ⋮
 -5.551115123125783e-16
 -4.440892098500626e-16
 -4.440892098500626e-16
 -3.3306690738754696e-16
 -2.220446049250313e-16
 -2.220446049250313e-16
 -1.1102230246251565e-16
  0.0
  0.0

As expected the solutions are the same (upto floating point error). Now let's compare the runtimes.

Runtimes

Dense Jacobian

@benchmark solve($(nlprob_dense), $(NewtonRaphson()); abstol = 1e-8)
BenchmarkTools.Trial: 2482 samples with 1 evaluation per sample.
 Range (minmax):  1.923 ms 20.210 ms   GC (min … max): 0.00% … 0.00%
 Time  (median):     1.966 ms                GC (median):    0.00%
 Time  (mean ± σ):   2.010 ms ± 702.154 μs   GC (mean ± σ):  0.39% ± 2.99%

          ▂ ▄▇▇▇▇█▅▂▂▁                                        
  ▂▂▃▃▃▄▅▅█████████████▆▅▅▅▄▄▃▃▃▃▂▃▂▃▂▂▂▂▂▂▁▂▂▂▁▁▁▁▂▁▁▁▁▂▁▂ ▄
  1.92 ms         Histogram: frequency by time        2.09 ms <

 Memory estimate: 740.31 KiB, allocs estimate: 225.
@benchmark solve($(nlprob_dense), $(PETScSNES()); abstol = 1e-8)
BenchmarkTools.Trial: 1028 samples with 1 evaluation per sample.
 Range (minmax):  4.653 ms 30.123 ms   GC (min … max): 0.00% … 7.52%
 Time  (median):     4.802 ms                GC (median):    0.00%
 Time  (mean ± σ):   4.863 ms ± 816.410 μs   GC (mean ± σ):  0.05% ± 0.23%

   ▁▄   ▂▄█▅▅▂▂▂        ▁ ▂▂▁                                  
  ▃██▇▆█████████▄▆█▆▇██████▇▅█▇▄▃▃▃▂▂▂▂▂▁▂▂▁▂▂▂▁▁▂▁▂▁▁▂▁▁▂▂ ▄
  4.65 ms         Histogram: frequency by time        5.29 ms <

 Memory estimate: 219.96 KiB, allocs estimate: 280.

Sparse Jacobian

@benchmark solve($(nlprob_sparse), $(NewtonRaphson()); abstol = 1e-8)
BenchmarkTools.Trial: 10000 samples with 1 evaluation per sample.
 Range (minmax):  422.797 μs 16.804 ms   GC (min … max): 0.00% … 18.54%
 Time  (median):     445.866 μs                GC (median):    0.00%
 Time  (mean ± σ):   486.071 μs ± 577.293 μs   GC (mean ± σ):  2.80% ±  2.34%

        ▁▆█▇▄▁   ▂▄▅▂                                            
  ▁▁▁▂▃▅██████▆██████▆▄▃▃▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▃
  423 μs           Histogram: frequency by time          530 μs <

 Memory estimate: 551.46 KiB, allocs estimate: 2137.
@benchmark solve($(nlprob_sparse), $(PETScSNES()); abstol = 1e-8)
BenchmarkTools.Trial: 10000 samples with 1 evaluation per sample.
 Range (minmax):  389.816 μs56.226 ms   GC (min … max): 0.00% … 14.32%
 Time  (median):     412.694 μs               GC (median):    0.00%
 Time  (mean ± σ):   485.034 μs ±  1.871 ms   GC (mean ± σ):  2.12% ±  0.55%

        ▃▇██▄▂    ▃▄▄▂                                          
  ▂▂▂▃▄▆██████▆▆▇█████▆▅▄▃▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▂▂▂▂▂▂ ▄
  390 μs          Histogram: frequency by time          501 μs <

 Memory estimate: 180.85 KiB, allocs estimate: 1497.