@@ -60,12 +60,14 @@ broadcast). Use `dx=dy=1/32`.
60
60
The resulting ` NonlinearProblem ` definition is:
61
61
62
62
``` @example ill_conditioned_nlprob
63
- using NonlinearSolve, LinearAlgebra, SparseArrays, LinearSolve, Symbolics
63
+ using NonlinearSolve, LinearAlgebra, SparseArrays, LinearSolve
64
64
65
65
const N = 32
66
66
const xyd_brusselator = range(0, stop = 1, length = N)
67
+
67
68
brusselator_f(x, y) = (((x - 0.3)^2 + (y - 0.6)^2) <= 0.1^2) * 5.0
68
69
limit(a, N) = a == N + 1 ? 1 : a == 0 ? N : a
70
+
69
71
function brusselator_2d_loop(du, u, p)
70
72
A, B, alpha, dx = p
71
73
alpha = alpha / dx^2
@@ -96,6 +98,7 @@ function init_brusselator_2d(xyd)
96
98
end
97
99
u
98
100
end
101
+
99
102
u0 = init_brusselator_2d(xyd_brusselator)
100
103
prob_brusselator_2d = NonlinearProblem(brusselator_2d_loop, u0, p)
101
104
```
@@ -120,6 +123,26 @@ include:
120
123
- BandedMatrix ([ BandedMatrices.jl] ( https://github.com/JuliaLinearAlgebra/BandedMatrices.jl ) )
121
124
- BlockBandedMatrix ([ BlockBandedMatrices.jl] ( https://github.com/JuliaLinearAlgebra/BlockBandedMatrices.jl ) )
122
125
126
+ ## Approximate Sparsity Detection & Sparse Jacobians
127
+
128
+ In the next section, we will discuss how to declare a sparse Jacobian and how to use
129
+ [ Symbolics.jl] ( https://github.com/JuliaSymbolics/Symbolics.jl ) , to compute exact sparse
130
+ jacobians. For this to work it is important to not load ` Symbolics.jl ` . This is triggered
131
+ if you pass in a sparse autodiff type such as ` AutoSparseForwardDiff() ` .
132
+
133
+ ``` @example ill_conditioned_nlprob
134
+ using BenchmarkTools # for @btime
135
+
136
+ @btime solve(prob_brusselator_2d, NewtonRaphson());
137
+ @btime solve(prob_brusselator_2d, NewtonRaphson(; autodiff = AutoSparseForwardDiff()));
138
+ @btime solve(prob_brusselator_2d,
139
+ NewtonRaphson(; autodiff = AutoSparseForwardDiff(),
140
+ linsolve = KLUFactorization()));
141
+ @btime solve(prob_brusselator_2d,
142
+ NewtonRaphson(; autodiff = AutoSparseForwardDiff(),
143
+ linsolve = KrylovJL_GMRES()));
144
+ ```
145
+
123
146
## Declaring a Sparse Jacobian with Automatic Sparsity Detection
124
147
125
148
Jacobian sparsity is declared by the ` jac_prototype ` argument in the ` NonlinearFunction ` .
@@ -156,7 +179,6 @@ prob_brusselator_2d_sparse = NonlinearProblem(ff, u0, p)
156
179
Now let's see how the version with sparsity compares to the version without:
157
180
158
181
``` @example ill_conditioned_nlprob
159
- using BenchmarkTools # for @btime
160
182
@btime solve(prob_brusselator_2d, NewtonRaphson());
161
183
@btime solve(prob_brusselator_2d_sparse, NewtonRaphson());
162
184
@btime solve(prob_brusselator_2d_sparse, NewtonRaphson(linsolve = KLUFactorization()));
@@ -258,10 +280,8 @@ function algebraicmultigrid2(W, du, u, p, t, newW, Plprev, Prprev, solverdata)
258
280
if newW === nothing || newW
259
281
A = convert(AbstractMatrix, W)
260
282
Pl = AlgebraicMultigrid.aspreconditioner(AlgebraicMultigrid.ruge_stuben(A,
261
- presmoother = AlgebraicMultigrid.Jacobi(rand(size(A,
262
- 1))),
263
- postsmoother = AlgebraicMultigrid.Jacobi(rand(size(A,
264
- 1)))))
283
+ presmoother = AlgebraicMultigrid.Jacobi(rand(size(A, 1))),
284
+ postsmoother = AlgebraicMultigrid.Jacobi(rand(size(A, 1)))))
265
285
else
266
286
Pl = Plprev
267
287
end
0 commit comments