-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Stuart Daines
committed
Jul 19, 2021
0 parents
commit 578b6f9
Showing
4 changed files
with
666 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
name = "ADelemtree" | ||
uuid = "06eadbd4-12ad-4cbc-ab6e-10f8370940a5" | ||
authors = ["Stuart Daines <stuart.daines@gmail.com>"] | ||
version = "0.1.0" | ||
|
||
[deps] | ||
DiffRules = "b552c78f-8df3-52c6-915a-8e097449b14b" | ||
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" | ||
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b" | ||
|
||
[extras] | ||
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | ||
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
|
||
[targets] | ||
test = ["Test", "Logging", "LinearAlgebra"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# ADelemtree.jl | ||
|
||
Automatic Jacobian sparsity detection using minimal scalar tracing and autodifferentiation. | ||
|
||
## Installation | ||
|
||
The package is not yet registered, so add it using: | ||
|
||
```julia | ||
julia> ] add https://github.com/sjdaines/ADelemtree.jl | ||
``` | ||
|
||
## Example | ||
```julia | ||
julia> import ADelemtree as AD | ||
|
||
julia> function rober(du,u,p) | ||
y₁,y₂,y₃ = u | ||
k₁,k₂,k₃ = p | ||
du[1] = -k₁*y₁+k₃*y₂*y₃ | ||
du[2] = k₁*y₁-k₂*y₂^2-k₃*y₂*y₃ | ||
du[3] = k₂*y₂^2 | ||
nothing | ||
end; | ||
|
||
julia> u = [1.0, 2.0, 3.0]; | ||
|
||
julia> p = (0.04,3e7,1e4); | ||
|
||
julia> u_ad = AD.create_advec(u); | ||
|
||
julia> du_ad = similar(u_ad); | ||
|
||
julia> rober(du_ad, u_ad, p) | ||
|
||
julia> AD.deriv(du_ad[3]) | ||
2-element SparseArrays.SparseVector{Float64, Int64} with 1 stored entry: | ||
[2] = 1.2e8 | ||
|
||
julia> Jad = AD.jacobian(du_ad, length(du_ad)) | ||
3×3 SparseArrays.SparseMatrixCSC{Float64, Int64} with 7 stored entries: | ||
-0.04 30000.0 20000.0 | ||
0.04 -1.2003e8 -20000.0 | ||
⋅ 1.2e8 ⋅ | ||
|
||
``` | ||
|
||
## Implementation | ||
|
||
Implements a scalar type `ADelemtree.ADval{T<:Real}` that holds a value and a binary tree of scalar derivatives. The tree is initialised to a Vector of leaf nodes by `ADelemtree.create_advec`. It is populated with derivatives calculated by `DiffRules` when a Julia function is called (eg a function `y = f(x)` calculating the RHS of an ODE). `ADelemtree.jacobian` then walks the tree and calculates the Jacobian as a sparse matrix. This provides a robust way of detecting Jacobian sparsity (and for test purposes only, a very slow way of calculating the actual derivative). The sparsity pattern may then be used to generate matrix colouring for a fast AD package eg `SparseDiffTools`. |
Oops, something went wrong.