From f88be85ddbc62c056ec187328b22f1a23f590563 Mon Sep 17 00:00:00 2001 From: adrhill Date: Tue, 3 Sep 2024 21:17:21 +0200 Subject: [PATCH 1/2] Update sparsity detection docs --- docs/Project.toml | 8 +++--- docs/src/tutorials/advanced_ode_example.md | 31 +++++++++++++--------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/docs/Project.toml b/docs/Project.toml index 1c32ab0ad..8261e1dc5 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -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" @@ -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" @@ -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" diff --git a/docs/src/tutorials/advanced_ode_example.md b/docs/src/tutorials/advanced_ode_example.md index e1fec7aa5..67e845630 100644 --- a/docs/src/tutorials/advanced_ode_example.md +++ b/docs/src/tutorials/advanced_ode_example.md @@ -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)) From b03c9e3bf9b8fffcf323a21510455208bfebfa55 Mon Sep 17 00:00:00 2001 From: adrhill Date: Wed, 4 Sep 2024 13:52:34 +0200 Subject: [PATCH 2/2] Run JuliaFormatter --- docs/src/tutorials/advanced_ode_example.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/src/tutorials/advanced_ode_example.md b/docs/src/tutorials/advanced_ode_example.md index 67e845630..567aeb5f3 100644 --- a/docs/src/tutorials/advanced_ode_example.md +++ b/docs/src/tutorials/advanced_ode_example.md @@ -145,9 +145,9 @@ 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! -[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) +[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) +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, @@ -166,8 +166,8 @@ jac_sparsity = ADTypes.jacobian_sparsity( 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! +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