From e16ae36b357ed91d49918f4f8437c9796cef49eb Mon Sep 17 00:00:00 2001 From: Leandro Martinez Date: Fri, 25 Nov 2022 11:26:47 -0300 Subject: [PATCH] implements preserve_lists option --- Project.toml | 2 +- docs/src/PeriodicSystems.md | 10 ++++++++-- src/PeriodicSystems.jl | 9 +++++++-- test/BasicForPeriodicSystems.jl | 4 ++++ 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/Project.toml b/Project.toml index a1ad0ba1..ec628122 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "CellListMap" uuid = "69e1c6dd-3888-40e6-b3c8-31ac5f578864" authors = ["Leandro Martinez and contributors"] -version = "0.8.7-DEV" +version = "0.8.7" [deps] DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" diff --git a/docs/src/PeriodicSystems.md b/docs/src/PeriodicSystems.md index 1eb7a825..be1e1656 100644 --- a/docs/src/PeriodicSystems.md +++ b/docs/src/PeriodicSystems.md @@ -15,8 +15,6 @@ using CellListMap.PeriodicSystems - [Two sets of particles](@ref) - [Particle simulation](@ref) - - ## The mapped function The function to be mapped for every pair of particles within the cutoff follows the same interface as the standard interface. It must be of the form @@ -51,6 +49,14 @@ const masses = # ... some masses u = map_pairwise((x,y,i,j,d2,u) -> energy(d2,u,masses), system) ``` +To compute a different property from the same coordinates and cell lists, use `preserve_lists=true`, for example, +```julia +u = map_pairwise((x,y,i,j,d2,u) -> u += sqrt(d2), system; preserve_lists = true) +``` +in which case we are computing the sum of distances from the same cell lists used to compute the energy in the previous example +(requires version 0.8.7). Specifically, this will skip the updatding of the cell lists, thus be careful to not use this +option if the cutoff, unitcell, or any other property of the system changed. + ## Potential energy example !!! note diff --git a/src/PeriodicSystems.jl b/src/PeriodicSystems.jl index b6e3ab5e..63c9e0e4 100644 --- a/src/PeriodicSystems.jl +++ b/src/PeriodicSystems.jl @@ -822,7 +822,7 @@ end """ ``` -map_pairwise!(f::Function, system; show_progress = true) +map_pairwise!(f::Function, system; show_progress = true, preserve_lists = false) ``` Function that maps the `f` function into all pairs of particles of @@ -848,6 +848,10 @@ Thread-safety is taken care automatically in parallel executions. `map_pairwise` is an alias to `map_pairwise!` for syntax consistency when the `output` variable is immutable. +If `preserve_lists` is `true`, the cell lists will not be recomputed, +this may be useful for computing a different function from the same +coordinates. + # Example In this example we compute the sum of `1/(1+d)` where `d` is the @@ -869,10 +873,11 @@ julia> map_pairwise((x,y,i,j,d2,output) -> output += 1 / (1 + sqrt(d2)), sys) function map_pairwise!( f::F, sys::AbstractPeriodicSystem; + preserve_lists::Bool=false, show_progress::Bool=false ) where {F<:Function} sys.output = _reset_all_output!(sys.output, sys._output_threaded) - UpdatePeriodicSystem!(sys) + preserve_lists || UpdatePeriodicSystem!(sys) sys.output = CellListMap.map_pairwise!( f, sys.output, sys._box, sys._cell_list; output_threaded=sys._output_threaded, diff --git a/test/BasicForPeriodicSystems.jl b/test/BasicForPeriodicSystems.jl index d22b0140..0c38cb56 100644 --- a/test/BasicForPeriodicSystems.jl +++ b/test/BasicForPeriodicSystems.jl @@ -250,6 +250,10 @@ end naive = CellListMap.map_pairwise!((x, y, i, j, d2, u) -> potential(i, j, d2, u, mass), 0.0, box, cl) system = PeriodicSystem(xpositions=x, cutoff=cutoff, unitcell=sides, output=0.0) @test PeriodicSystems.map_pairwise!((x, y, i, j, d2, u) -> potential(i, j, d2, u, mass), system) ≈ naive + + # Check the functionality of computing a different function from the same coordinates (new_coordinates=false) + naive = CellListMap.map_pairwise!((x, y, i, j, d2, u) -> u += d2, 0.0, box, cl) + @test PeriodicSystems.map_pairwise!((x, y, i, j, d2, u) -> u += d2, system; preserve_lists = true) ≈ naive # Function to be evalulated for each pair: gravitational force function calc_forces!(x, y, i, j, d2, mass, forces)