Skip to content

Commit

Permalink
implement mapping of coordinates to list. Needs testing and updated d…
Browse files Browse the repository at this point in the history
…ocs.
  • Loading branch information
lmiq committed Dec 11, 2024
1 parent fe06c95 commit f560948
Showing 1 changed file with 113 additions and 2 deletions.
115 changes: 113 additions & 2 deletions src/core_computing/cross.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""
map_pairwise!(f::Function,output,box::Box,cl::CellListPair)
map_pairwise!(f::Function, output, box::Box, cl::CellListPair)
The same but to evaluate some function between pairs of the particles of the vectors.
Evaluate function f for pairs in two independent sets of particles, for which the `CellListPair`
object was constructed.
"""
function map_pairwise!(f::F1, output, box::Box, cl::CellListPair{N,T};
Expand Down Expand Up @@ -138,4 +139,114 @@ function _current_cell_interactions!(box::Box, f::F, cellᵢ::Cell, cellⱼ::Cel
end
end
return output
end

#
# Cross-computations when only one cell list was computed
#
"""
map_pairwise!(f::Function, x::AbstractVector{<:AbstractVector}, sys::ParticleSystem1; kargs...)
map_pairwise!(f::Function, x::AbstractMatrix, sys::ParticleSystem1; kargs...)
Evaluate function f for pairs in two independent sets of particles, where the `sys::ParicleSystem1` object
contains the previously computed cell lists of one set of particles, and the second set is given by the
array of positions `x`.
This function can be advantageous over computing the interactions with `CellListPair`, because here the
cell lists are only computed for one set. This is advantageous in two situations:
1. The second set of particles is not changing, and the first set is changing. Thus, the cell lists
of the second set can be computed only once.
2. One of the sets is much smaller than the other. In this case, computing the cell lists of the largest
set might be too expensive. Construct the `ParticleSystem` object for the smallest set, and use this
function to compute the interactions with the largest set.
## Keyword arguments:
- `show_progress::Bool=false`: Show progress bar.
- `update_lists::Bool=true`: Update the cell lists or not. If the positions of the `ParticleSystem1` object
have not changed, it is not necessary to update the cell lists.
## Example
```julia-repl
julia> using CellListMap
```
"""
function map_pairwise!(

Check warning on line 178 in src/core_computing/cross.jl

View check run for this annotation

Codecov / codecov/patch

src/core_computing/cross.jl#L178

Added line #L178 was not covered by tests
f::F, x::AbstractVector{<:AbstractVector}, sys::ParticleSystem1;
show_progress::Bool=false, update_lists::Bool=true,
) where {F<:Function}
sys.output = _reset_all_output!(sys.output, sys._output_threaded)
UpdateParticleSystem!(sys, update_lists)
sys.output = map_pairwise!(

Check warning on line 184 in src/core_computing/cross.jl

View check run for this annotation

Codecov / codecov/patch

src/core_computing/cross.jl#L182-L184

Added lines #L182 - L184 were not covered by tests
f, sys.output, sys._box, x, sys._cell_list;
output_threaded=sys._output_threaded,
reduce=(output, output_threaded) -> reduce_output!(reducer, output, output_threaded),

Check warning on line 187 in src/core_computing/cross.jl

View check run for this annotation

Codecov / codecov/patch

src/core_computing/cross.jl#L187

Added line #L187 was not covered by tests
sys.parallel, show_progress,
)
return sys.output

Check warning on line 190 in src/core_computing/cross.jl

View check run for this annotation

Codecov / codecov/patch

src/core_computing/cross.jl#L190

Added line #L190 was not covered by tests
end

function _batch_pairs!(f::F, x, x_atom_indices, ibatch, output_threaded, box, cl, p) where {F<:Function}
for i in x_atom_indices
output_threaded[ibatch] = single_particle_vs_list!(f, output_threaded[ibatch], box, i, x[i], cl)
_next!(p)
end

Check warning on line 197 in src/core_computing/cross.jl

View check run for this annotation

Codecov / codecov/patch

src/core_computing/cross.jl#L193-L197

Added lines #L193 - L197 were not covered by tests
end

function map_pairwise!(

Check warning on line 200 in src/core_computing/cross.jl

View check run for this annotation

Codecov / codecov/patch

src/core_computing/cross.jl#L200

Added line #L200 was not covered by tests
f::F1, output, box::Box, x::AbstractVector{<:AbstractVector}, cl::CellList;
parallel::Bool=true, show_progress::Bool=false, output_threaded=nothing, reduce::F2=reduce,
) where {F1<:Function, F2<:Function}
p = show_progress ? Progress(length(x), dt=1) : nothing
output = if parallel
_nbatches = nbatches(cl, :map)
if isnothing(output_threaded)
output_threaded = [deepcopy(output) for _ in 1:_nbatches]

Check warning on line 208 in src/core_computing/cross.jl

View check run for this annotation

Codecov / codecov/patch

src/core_computing/cross.jl#L204-L208

Added lines #L204 - L208 were not covered by tests
end
p = show_progress ? Progress(length(x), dt=1) : nothing
@sync for (ibatch, x_atom_indices) in enumerate(index_chunks(1:length(x); n=_nbatches, split=Consecutive()))
@spawn _batch_pairs!($f, $x, $x_atom_indices, $ibatch, $output_threaded, $box, $cl, $p)
end
reduce(output, output_threaded)

Check warning on line 214 in src/core_computing/cross.jl

View check run for this annotation

Codecov / codecov/patch

src/core_computing/cross.jl#L210-L214

Added lines #L210 - L214 were not covered by tests
else
for i in eachindex(x)
output = single_particle_vs_list!(f, output, box, i, x[i], cl)
_next!(p)
end
output

Check warning on line 220 in src/core_computing/cross.jl

View check run for this annotation

Codecov / codecov/patch

src/core_computing/cross.jl#L216-L220

Added lines #L216 - L220 were not covered by tests
end
return output

Check warning on line 222 in src/core_computing/cross.jl

View check run for this annotation

Codecov / codecov/patch

src/core_computing/cross.jl#L222

Added line #L222 was not covered by tests
end

function single_particle_vs_list!(

Check warning on line 225 in src/core_computing/cross.jl

View check run for this annotation

Codecov / codecov/patch

src/core_computing/cross.jl#L225

Added line #L225 was not covered by tests
f::F, output, box::Box,
i::Integer, x::SVector{N,T},
cl::CellList{N,T};
) where {F,N,T}
@unpack nc, cutoff_sqr, inv_rotation, rotation = box
xpᵢ = box.rotation * wrap_to_first(x, box.input_unit_cell.matrix)
ic = particle_cell(xpᵢ, box)
for neighbor_cell in current_and_neighbor_cells(box)
jc_cartesian = neighbor_cell + ic
jc_linear = cell_linear_index(nc, jc_cartesian)

Check warning on line 235 in src/core_computing/cross.jl

View check run for this annotation

Codecov / codecov/patch

src/core_computing/cross.jl#L230-L235

Added lines #L230 - L235 were not covered by tests
# If cellⱼ is empty, cycle
if cl.cell_indices[jc_linear] == 0
continue

Check warning on line 238 in src/core_computing/cross.jl

View check run for this annotation

Codecov / codecov/patch

src/core_computing/cross.jl#L237-L238

Added lines #L237 - L238 were not covered by tests
end
cellⱼ = cl.cells[cl.cell_indices[jc_linear]]

Check warning on line 240 in src/core_computing/cross.jl

View check run for this annotation

Codecov / codecov/patch

src/core_computing/cross.jl#L240

Added line #L240 was not covered by tests
# loop over particles of cellⱼ
for j in 1:cellⱼ.n_particles
@inbounds pⱼ = cellⱼ.particles[j]
xpⱼ = pⱼ.coordinates
d2 = norm_sqr(xpᵢ - xpⱼ)
if d2 <= cutoff_sqr
output = f(inv_rotation * xpᵢ, inv_rotation * xpⱼ, i, pⱼ.index, d2, output)

Check warning on line 247 in src/core_computing/cross.jl

View check run for this annotation

Codecov / codecov/patch

src/core_computing/cross.jl#L242-L247

Added lines #L242 - L247 were not covered by tests
end
end
end
return output

Check warning on line 251 in src/core_computing/cross.jl

View check run for this annotation

Codecov / codecov/patch

src/core_computing/cross.jl#L249-L251

Added lines #L249 - L251 were not covered by tests
end

0 comments on commit f560948

Please sign in to comment.