Skip to content

Commit

Permalink
update unitful example
Browse files Browse the repository at this point in the history
  • Loading branch information
lmiq committed May 3, 2024
1 parent 782aabd commit f554851
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
2 changes: 2 additions & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
[deps]
CellListMap = "69e1c6dd-3888-40e6-b3c8-31ac5f578864"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
LiveServer = "16fef848-5104-11e9-1b77-fb7a48bbb589"
Measurements = "eff96d63-e80a-5855-80a2-b1b0885c5ab7"
PDBTools = "e29189f1-7114-4dbd-93d0-c5673a921a58"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
21 changes: 9 additions & 12 deletions docs/src/ecosystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ julia> neighborlist(positions, cutoff)

Allowing automatic differentiation follows the same principles, meaning that we only need to allow the propagation of dual types through the computation by proper initialization of the input data. However, it is easier to work with the low level interface, which accepts matrices as the input for positions and a more fine control of the types of the variables. Matrices are easier input types for auto diff packages.

The variables are each component of each vector, thus the easiest way to represent the points such that automatic differentiation packages understand is by creating a matrix:
The variables are each component of each vector, thus the easiest way to represent the points to interface with differentiation packages is providing the coordinates as a matrix:

```julia-repl
julia> x = rand(3,1000)
Expand All @@ -98,21 +98,18 @@ julia> x = rand(3,1000)
The key here is allow all the types of the parameters to follow the type propagation of the elements of `x` inside the differentiation routine. The function we define to compute the derivative is, then:

```julia-repl
julia> function sum_sqr(x,sides,cutoff)
cutoff = eltype(x)(cutoff)
sides = eltype(x).(sides)
box = Box(sides,cutoff)
cl = CellList(x,box)
sum_sqr = zero(eltype(x))
sum_sqr = map_pairwise!(
(x,y,i,j,d2,sum_sqr) -> sum_sqr += d2,
sum_sqr, box, cl
julia> function sum_sqr(x, sides, cutoff)
sys = ParticleSystem(
positions=x,
unitcell=eltype(x).(sides),
cutoff=eltype(x).(cutoff),
output=zero(eltype(x))
)
return sum_sqr
return map_pairwise((_, _, _, _, d2, sum_sqr) -> sum_sqr += d2, sys)
end
```

Note that we allow `cutoff` and `sides` to be converted to the same type of the input `x` of the function. For a simple call to the function this is inconsequential:
Note that we convert `cutoff` and `sides` to the same type of the input `x` of the function, and set the type of the `output` variable accordingly. For a simple call to the function this is inconsequential:

```julia-repl
julia> cutoff = 0.1; sides = [1,1,1];
Expand Down

0 comments on commit f554851

Please sign in to comment.