Skip to content

Commit

Permalink
Same environment for ill conditioned nlprob
Browse files Browse the repository at this point in the history
Signed-off-by: ErikQQY <[email protected]>
  • Loading branch information
ErikQQY committed Sep 17, 2023
1 parent daea27f commit b490926
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
2 changes: 2 additions & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[deps]
AlgebraicMultigrid = "2169fc97-5a83-5252-b627-83903c6c433c"
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
IncompleteLU = "40713840-3770-5561-ab4c-a76e7d0d7895"
Expand All @@ -13,6 +14,7 @@ Sundials = "c3572dad-4567-51f8-b174-8c6c989267f4"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"

[compat]
AlgebraicMultigrid = "0.5"
BenchmarkTools = "1"
Documenter = "0.27"
IncompleteLU = "0.2"
Expand Down
18 changes: 9 additions & 9 deletions docs/src/tutorials/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ are then applied at each point in space (they are broadcast). Use `dx=dy=1/32`.

The resulting `NonlinearProblem` definition is:

```@example
```@example ill_conditioned_nlprob
using NonlinearSolve, LinearAlgebra, SparseArrays, LinearSolve
const N = 32
Expand Down Expand Up @@ -130,7 +130,7 @@ in action, we can give an example `du` and `u` and call `jacobian_sparsity`
on our function with the example arguments, and it will kick out a sparse matrix
with our pattern, that we can turn into our `jac_prototype`.

```@example
```@example ill_conditioned_nlprob
using Symbolics
du0 = copy(u0)
jac_sparsity = Symbolics.jacobian_sparsity((du, u) -> brusselator_2d_loop(du, u, p),
Expand All @@ -141,19 +141,19 @@ Notice that Julia gives a nice print out of the sparsity pattern. That's neat, a
would be tedious to build by hand! Now we just pass it to the `NonlinearFunction`
like as before:

```@example
```@example ill_conditioned_nlprob
ff = NonlinearFunction(brusselator_2d_loop; jac_prototype = float.(jac_sparsity))
```

Build the `NonlinearProblem`:

```@example
```@example ill_conditioned_nlprob
prob_brusselator_2d_sparse = NonlinearProblem(ff, u0, p)
```

Now let's see how the version with sparsity compares to the version without:

```@example
```@example ill_conditioned_nlprob
using BenchmarkTools # for @btime
@btime solve(prob_brusselator_2d, NewtonRaphson());
@btime solve(prob_brusselator_2d_sparse, NewtonRaphson());
Expand All @@ -172,7 +172,7 @@ matrices is to use a Krylov subspace method. This requires choosing a linear
solver for changing to a Krylov method. To swap the linear solver out, we use
the `linsolve` command and choose the GMRES linear solver.

```@example
```@example ill_conditioned_nlprob
@btime solve(prob_brusselator_2d, NewtonRaphson(linsolve = KrylovJL_GMRES()));
nothing # hide
```
Expand All @@ -197,7 +197,7 @@ approximate the inverse of `W = I - gamma*J` used in the solution of the ODE.
An example of this with using [IncompleteLU.jl](https://github.com/haampie/IncompleteLU.jl)
is as follows:

```@example
```@example ill_conditioned_nlprob
using IncompleteLU
function incompletelu(W, du, u, p, t, newW, Plprev, Prprev, solverdata)
if newW === nothing || newW
Expand Down Expand Up @@ -233,7 +233,7 @@ requires a well-tuned `τ` parameter. Another option is to use
[AlgebraicMultigrid.jl](https://github.com/JuliaLinearAlgebra/AlgebraicMultigrid.jl)
which is more automatic. The setup is very similar to before:

```@example
```@example ill_conditioned_nlprob
using AlgebraicMultigrid
function algebraicmultigrid(W, du, u, p, t, newW, Plprev, Prprev, solverdata)
if newW === nothing || newW
Expand All @@ -252,7 +252,7 @@ nothing # hide

or with a Jacobi smoother:

```@example
```@example ill_conditioned_nlprob
function algebraicmultigrid2(W, du, u, p, t, newW, Plprev, Prprev, solverdata)
if newW === nothing || newW
A = convert(AbstractMatrix, W)
Expand Down

0 comments on commit b490926

Please sign in to comment.