Skip to content

Commit

Permalink
use ntuple(..., Val(N)) instead of ntuple(..., N)
Browse files Browse the repository at this point in the history
  • Loading branch information
lmiq committed Jan 8, 2025
1 parent 6c258e6 commit 1b2593f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
14 changes: 7 additions & 7 deletions src/Box.jl
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ function _compute_nc_and_cell_size(::Type{<:OrthorhombicCellType}, xmin, xmax, c
end
function _compute_nc_and_cell_size(::Type{TriclinicCell}, xmin::SVector{N,T}, xmax::SVector{N,T}, cutoff, lcell) where {N,T}
_nc = ceil.(Int, (xmax .- xmin) / (cutoff / lcell))
cell_size = SVector{N,T}(ntuple(_ -> cutoff/lcell, N))
cell_size = SVector{N,T}(ntuple(_ -> cutoff/lcell, Val(N)))
nc = _nc .+ 2 * lcell .+ 1
return nc, cell_size
end
Expand Down Expand Up @@ -553,8 +553,8 @@ cells of a cell where the computing cell index is `box.lcell`.
function neighbor_cells(box::Box{UnitCellType,N}) where {UnitCellType,N}
@unpack lcell = box
return Iterators.filter(
!isequal(CartesianIndex(ntuple(i -> 0, N))),
CartesianIndices(ntuple(i -> -lcell:lcell, N))
!isequal(CartesianIndex(ntuple(i -> 0, Val(N)))),
CartesianIndices(ntuple(i -> -lcell:lcell, Val(N)))
)
end

Expand All @@ -568,7 +568,7 @@ Returns an iterator over all neighbor cells, including the center one.
=#
function current_and_neighbor_cells(box::Box{UnitCellType,N}) where {UnitCellType,N}
@unpack lcell = box
return CartesianIndices(ntuple(i -> -lcell:lcell, N))
return CartesianIndices(ntuple(i -> -lcell:lcell, Val(N)))
end

#=
Expand All @@ -583,7 +583,7 @@ computing box with positive coordinates has indexes `Box.lcell + 1`.
=#
@inline function particle_cell(x::SVector{N}, box::Box) where {N}
CartesianIndex(
ntuple(N) do i
ntuple(Val(N)) do i
xmin = box.computing_box[1][i]
xi = (x[i] - xmin) / box.cell_size[i]
index = floor(Int, xi) + 1
Expand All @@ -601,7 +601,7 @@ of points. Returns a `SVector{N,T}`
=#
@inline function cell_center(c::CartesianIndex{N}, box::Box{UnitCellType,N,T}) where {UnitCellType,N,T}
SVector{N,T}(
ntuple(N) do i
ntuple(Val(N)) do i
xmin = box.computing_box[1][i]
ci = xmin + box.cell_size[i] * c[i] - box.cell_size[i] / 2
return ci
Expand Down Expand Up @@ -639,7 +639,7 @@ Replicates the particle as many times as necessary to fill the computing box.
=#
function replicate_particle!(ip, p::SVector{N}, box, cl) where {N}
itr = Iterators.product(ntuple(i -> -1:1, N)...)
itr = Iterators.product(ntuple(i -> -1:1, Val(N))...)
for indices in itr
(count(isequal(0), indices) == N) && continue
x = translation_image(p, box.aligned_unit_cell.matrix, indices)
Expand Down
12 changes: 6 additions & 6 deletions src/CellLists.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ neighboring cells need to be wrapped)
=#
Base.@kwdef struct Cell{N,T}
linear_index::Int = 0
cartesian_index::CartesianIndex{N} = CartesianIndex{N}(ntuple(i -> 0, N))
cartesian_index::CartesianIndex{N} = CartesianIndex{N}(ntuple(i -> 0, Val(N)))
center::SVector{N,T} = zeros(SVector{N,T})
contains_real::Bool = false
n_particles::Int = 0
Expand Down Expand Up @@ -556,12 +556,12 @@ function CellList(
) where {UnitCellType,N,T}
if !autoswap || length(x) >= length(y)
isnothing(validate_coordinates) || validate_coordinates(x)
ref = [SVector{N,T}(ntuple(i -> el[i], N)) for el in x]
ref = [SVector{N,T}(ntuple(i -> el[i], Val(N))) for el in x]
target = CellList(y, box; parallel, validate_coordinates)
swap = NotSwapped()
else
isnothing(validate_coordinates) || validate_coordinates(y)
ref = [SVector{N,T}(ntuple(i -> el[i], N)) for el in y]
ref = [SVector{N,T}(ntuple(i -> el[i], Val(N))) for el in y]
target = CellList(x, box; parallel, validate_coordinates)
swap = Swapped()
end
Expand Down Expand Up @@ -825,7 +825,7 @@ function add_particles!(x, box, ishift, cl::CellList{N,T}) where {N,T}
for ip in eachindex(x)
xp = x[ip]
# This converts the coordinates to static arrays, if necessary
p = SVector{N,T}(ntuple(i -> xp[i], N))
p = SVector{N,T}(ntuple(i -> xp[i], Val(N)))
p = box.rotation * wrap_to_first(p, box.input_unit_cell.matrix)
add_particle_to_celllist!(ishift + ip, p, box, cl) # add real particle
replicate_particle!(ishift + ip, p, box, cl) # add virtual particles to border cells
Expand Down Expand Up @@ -947,7 +947,7 @@ end
# This cannot happen because then running over the neighboring boxes can cause an
# invalid access to an index of a cell.
function real_particle_border_case(cartesian_index::CartesianIndex{N}, box) where {N}
cidxs = ntuple(i -> cartesian_index[i], N)
cidxs = ntuple(i -> cartesian_index[i], Val(N))
for i in 1:N
if cidxs[i] == box.lcell
@set! cidxs[i] += 1
Expand Down Expand Up @@ -1233,7 +1233,7 @@ function _update_CellListPair!(ref, target, cl_pair::CellListPair{V,N,T,Swap}) w
resize!(cl_pair.ref, length(ref))
end
for i in eachindex(ref, cl_pair.ref)
@inbounds cl_pair.ref[i] = SVector{N,T}(ntuple(j -> ref[i][j], N))
@inbounds cl_pair.ref[i] = SVector{N,T}(ntuple(j -> ref[i][j], Val(N)))
end
cl_pair = CellListPair{V,N,T,Swap}(cl_pair.ref, target)
return cl_pair
Expand Down
12 changes: 6 additions & 6 deletions src/CellOperations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Wraps the coordinates of point `x` such that it is the minimum image relative to
unit_cell_matrix = invu * unit_cell_matrix
x_f = wrap_cell_fraction(invu * x, unit_cell_matrix)
xref_f = wrap_cell_fraction(invu * xref, unit_cell_matrix)
xw = wrap_relative_to(x_f, xref_f, SVector{N,eltype(x_f)}(ntuple(i -> 1, N)))
xw = wrap_relative_to(x_f, xref_f, SVector{N,eltype(x_f)}(ntuple(i -> 1, Val(N))))
return oneunit(T) * unit_cell_matrix * (xw - xref_f) + xref
end

Expand Down Expand Up @@ -171,7 +171,7 @@ provided.
=#
@inline translation_image(x::SVector{N,T}, unit_cell_matrix, indices) where {N,T} =
x + unit_cell_matrix * SVector{N,Int}(ntuple(i -> indices[i], N))
x + unit_cell_matrix * SVector{N,Int}(ntuple(i -> indices[i], Val(N)))

#=
translation_image(x::AbstractVector{<:AbstractVector},unit_cell_matrix,indices)
Expand Down Expand Up @@ -254,7 +254,7 @@ function replicate_system!(
ranges::Tuple
) where {N,T}
length(ranges) == N || throw(DimensionMismatch("Tuple of ranges must have the same dimension as the vectors: $N"))
i0 = ntuple(i -> 0, N)
i0 = ntuple(i -> 0, Val(N))
imgs = Iterators.filter(!isequal(i0),
Iterators.product(ranges...)
)
Expand All @@ -270,7 +270,7 @@ end
# resized in-place.
function replicate_system(x::AbstractMatrix{T}, cell, ranges) where {T}
N = size(x, 1)
x_re = [SVector{N,T}(ntuple(i -> x[i, j], N)) for j in axes(x, 2)]
x_re = [SVector{N,T}(ntuple(i -> x[i, j], Val(N))) for j in axes(x, 2)]
replicate_system!(x_re, cell, ranges)
x = Matrix(reinterpret(reshape, Float64, x_re))
return x
Expand Down Expand Up @@ -312,7 +312,7 @@ of the cell (for arbitrary dimension N).
=#
@inline cell_cartesian_indices(nc::SVector{N,Int}, i1D) where {N} =
CartesianIndices(ntuple(i -> nc[i], N))[i1D]
CartesianIndices(ntuple(i -> nc[i], Val(N)))[i1D]

#=
cell_linear_index(nc::SVector{N,Int}, indices) where N
Expand All @@ -323,7 +323,7 @@ Returns the index of the cell, in the 1D representation, from its cartesian coor
=#
@inline cell_linear_index(nc::SVector{N,Int}, indices) where {N} =
LinearIndices(ntuple(i -> nc[i], N))[ntuple(i -> indices[i], N)...]
LinearIndices(ntuple(i -> nc[i], Val(N)))[ntuple(i -> indices[i], Val(N))...]

@testitem "cell cartesian/linear indices" begin
using StaticArrays
Expand Down
4 changes: 2 additions & 2 deletions test/namd/ParticleSystem_vs_NAMD.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@

function copy_to_svector(positions)
if positions isa AbstractVector{<:AbstractVector}
posvec = [ SVector(ntuple(i -> v[i], length(v))) for v in positions ]
posvec = [ SVector(ntuple(i -> v[i], Val(length(v)))) for v in positions ]
elseif positions isa AbstractMatrix
posvec = [ SVector(ntuple(i -> v[i], length(v))) for v in eachcol(positions) ]
posvec = [ SVector(ntuple(i -> v[i], Val(length(v)))) for v in eachcol(positions) ]
end
return posvec
end
Expand Down

0 comments on commit 1b2593f

Please sign in to comment.