Skip to content

Commit

Permalink
Merge pull request #748 from adrhill/ah/update-sparsity-detection
Browse files Browse the repository at this point in the history
Update sparsity detection docs
  • Loading branch information
ChrisRackauckas authored Sep 6, 2024
2 parents 9222d20 + b03c9e3 commit ab8e9d7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
8 changes: 5 additions & 3 deletions docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
AlgebraicMultigrid = "2169fc97-5a83-5252-b627-83903c6c433c"
BSON = "fbb218c0-5317-5bc6-957e-2ee96dd4b1f0"
BVProblemLibrary = "ded0fc24-dfea-4565-b1d9-79c027d14d84"
Expand Down Expand Up @@ -31,13 +32,14 @@ RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd"
SDEProblemLibrary = "c72e72a9-a271-4b2b-8966-303ed956772e"
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
SciMLOperators = "c0aeaf25-5076-4817-a8d5-81caf7dfa961"
SparseConnectivityTracer = "9f842d2f-2579-4b1d-911e-f412cf18a3f5"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Sundials = "c3572dad-4567-51f8-b174-8c6c989267f4"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[compat]
AlgebraicMultigrid = "0.6"
ADTypes = "1.7"
AlgebraicMultigrid = "0.5, 0.6"
BSON = "0.3"
BVProblemLibrary = "0.1.2"
BenchmarkTools = "1"
Expand Down Expand Up @@ -68,7 +70,7 @@ RecursiveArrayTools = "3"
SDEProblemLibrary = "0.1"
SciMLBase = "2"
SciMLOperators = "0.3"
SparseConnectivityTracer = "0.6"
StaticArrays = "1"
Sundials = "4.11.3"
Symbolics = "6"
Unitful = "1"
31 changes: 19 additions & 12 deletions docs/src/tutorials/advanced_ode_example.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,23 +145,30 @@ Note that you should only do this if the sparsity is high, for example, 0.1%
of the matrix is non-zeros, otherwise the overhead of sparse matrices can be higher
than the gains from sparse differentiation!

One of the useful companion tools for DifferentialEquations.jl is
[Symbolics.jl](https://github.com/JuliaSymbolics/Symbolics.jl).
This allows for automatic declaration of Jacobian sparsity types. To see this
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`.
[ADTypes.jl](https://github.com/SciML/ADTypes.jl) provides a [common interface for automatic sparsity detection](https://sciml.github.io/ADTypes.jl/stable/#Sparsity-detector)
via its function `jacobian_sparsity`.
This function can be called using sparsity detectors from [SparseConnectivityTracer.jl](https://github.com/adrhill/SparseConnectivityTracer.jl)
or [Symbolics.jl](https://github.com/JuliaSymbolics/Symbolics.jl).

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`.

Let's try SparseConnectivityTracer's [`TracerSparsityDetector`](https://adrianhill.de/SparseConnectivityTracer.jl/stable/user/api/#SparseConnectivityTracer.TracerSparsityDetector):

```@example stiff1
using Symbolics
using SparseConnectivityTracer, ADTypes
detector = TracerSparsityDetector()
du0 = copy(u0)
jac_sparsity = Symbolics.jacobian_sparsity((du, u) -> brusselator_2d_loop(du, u, p, 0.0),
du0, u0)
jac_sparsity = ADTypes.jacobian_sparsity(
(du, u) -> brusselator_2d_loop(du, u, p, 0.0), du0, u0, detector)
```

Notice that Julia gives a nice print out of the sparsity pattern. That's neat, and
would be tedious to build by hand! Now we just pass it to the `ODEFunction`
like as before:
Using a different backend for sparsity detection just requires swapping out the detector,
e.g. for Symbolics' [`SymbolicsSparsityDetector`](https://docs.sciml.ai/Symbolics/stable/manual/sparsity_detection/#Symbolics.SymbolicsSparsityDetector).

Notice that Julia gives a nice print out of the sparsity pattern.
That's neat, and would be tedious to build by hand!
Now we just pass it to the `ODEFunction` like as before:

```@example stiff1
f = ODEFunction(brusselator_2d_loop; jac_prototype = float.(jac_sparsity))
Expand Down

0 comments on commit ab8e9d7

Please sign in to comment.