From 1b2593f8203cdbb902089d9ab7fc77c2d61efe60 Mon Sep 17 00:00:00 2001 From: Leandro Martinez Date: Wed, 8 Jan 2025 11:46:09 -0300 Subject: [PATCH] use ntuple(..., Val(N)) instead of ntuple(..., N) --- src/Box.jl | 14 +++++++------- src/CellLists.jl | 12 ++++++------ src/CellOperations.jl | 12 ++++++------ test/namd/ParticleSystem_vs_NAMD.jl | 4 ++-- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/Box.jl b/src/Box.jl index 23202f66..1ff44e8f 100644 --- a/src/Box.jl +++ b/src/Box.jl @@ -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 @@ -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 @@ -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 #= @@ -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 @@ -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 @@ -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) diff --git a/src/CellLists.jl b/src/CellLists.jl index a4bd9c0f..04387e6e 100644 --- a/src/CellLists.jl +++ b/src/CellLists.jl @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/src/CellOperations.jl b/src/CellOperations.jl index bcc5122a..a0a24e08 100644 --- a/src/CellOperations.jl +++ b/src/CellOperations.jl @@ -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 @@ -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) @@ -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...) ) @@ -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 @@ -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 @@ -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 diff --git a/test/namd/ParticleSystem_vs_NAMD.jl b/test/namd/ParticleSystem_vs_NAMD.jl index 440aae1d..438537f9 100644 --- a/test/namd/ParticleSystem_vs_NAMD.jl +++ b/test/namd/ParticleSystem_vs_NAMD.jl @@ -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