Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
lmiq committed Aug 6, 2024
1 parent 0f6554b commit 3fe3c76
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 82 deletions.
34 changes: 17 additions & 17 deletions src/CellLists.jl
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ function UpdateCellList!(
box::Box,
cl::CellList;
parallel::Bool=true,
validate_coordinates = _validate_coordinates,
validate_coordinates=_validate_coordinates,
)
if parallel
aux = AuxThreaded(cl)
Expand Down Expand Up @@ -732,7 +732,7 @@ function UpdateCellList!(
cl::CellList{N,T},
aux::Union{Nothing,AuxThreaded{N,T}};
parallel::Bool=true,
validate_coordinates = _validate_coordinates,
validate_coordinates=_validate_coordinates,
) where {N,T}

# validate coordinates
Expand Down Expand Up @@ -949,7 +949,7 @@ end
function real_particle_border_case(cartesian_index::CartesianIndex{N}, box) where {N}
cidxs = ntuple(i -> cartesian_index[i], N)
for i in 1:N
if cidxs[i] == box.lcell
if cidxs[i] == box.lcell
@set! cidxs[i] += 1
end
if cidxs[i] == box.nc[i] - box.lcell + 1
Expand Down Expand Up @@ -985,7 +985,7 @@ function add_particle_to_celllist!(
cl.n_particles += 1
# Cell of this particle
cartesian_index = particle_cell(x, box)
if real_particle
if real_particle
cartesian_index = real_particle_border_case(cartesian_index, box)
end

Expand Down Expand Up @@ -1285,24 +1285,24 @@ particles_per_cell(cl::CellListPair) = particles_per_cell(cl.target)
x[50] = SVector(1.0, NaN, 1.0)
box = Box([1.0, 1.0, 1.0], 0.1)
y = rand(SVector{3,Float64}, 100)
@test_throws ArgumentError CellList(x,box)
cl = CellList(y,box)
@test_throws ArgumentError CellList(x, box)
cl = CellList(y, box)
@test_throws ArgumentError UpdateCellList!(x, box, cl)
@test_throws ArgumentError CellList(x,y,box)
@test_throws ArgumentError CellList(y,x,box)
cl = CellList(y,y,box)
@test_throws ArgumentError CellList(x, y, box)
@test_throws ArgumentError CellList(y, x, box)
cl = CellList(y, y, box)
@test_throws ArgumentError UpdateCellList!(x, y, box, cl)
@test_throws ArgumentError UpdateCellList!(y, x, box, cl)
x = rand(3,100)
x[2,50] = NaN
x = rand(3, 100)
x[2, 50] = NaN
box = Box([1.0, 1.0, 1.0], 0.1)
y = rand(3,100)
@test_throws ArgumentError CellList(x,box)
cl = CellList(y,box)
y = rand(3, 100)
@test_throws ArgumentError CellList(x, box)
cl = CellList(y, box)
@test_throws ArgumentError UpdateCellList!(x, box, cl)
@test_throws ArgumentError CellList(x,y,box)
@test_throws ArgumentError CellList(y,x,box)
cl = CellList(y,y,box)
@test_throws ArgumentError CellList(x, y, box)
@test_throws ArgumentError CellList(y, x, box)
cl = CellList(y, y, box)
@test_throws ArgumentError UpdateCellList!(x, y, box, cl)
@test_throws ArgumentError UpdateCellList!(y, x, box, cl)
end
86 changes: 43 additions & 43 deletions src/CellOperations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
Function that validates the coordinates.
=#
=#
function _validate_coordinates(x)
for (i,v) in enumerate(x)
for (i, v) in enumerate(x)
if any(isnan, v) || any(ismissing, v)
throw(ArgumentError("""\n
Invalid coordinates found: $v for particle of index $i.
"""))
end
end
Expand Down Expand Up @@ -108,8 +108,8 @@ Wraps the coordinates of point `x` such that it is the minimum image relative to
@inline function wrap_relative_to(x, xref, unit_cell_matrix::SMatrix{N,N,T}) where {N,T}
invu = inv(oneunit(T))
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)
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)))
return oneunit(T) * unit_cell_matrix * (xw - xref_f) + xref
end
Expand All @@ -134,21 +134,21 @@ end
using StaticArrays
using Unitful
for (x, y, xy, yx) in [
([15.0, 13.0], [4.0, 2.0], [5.0, 3.0], [14.0, 12.0]),
([15.0, 13.0], [4.0, 2.0], [5.0, 3.0], [14.0, 12.0]),
([-7.0, -6.0], [1.0, 2.0], [3.0, 4.0], [-9.0, -8.0]),
]
# triclinic cells: unitcell is a matrix
unit_cell_matrix = SMatrix{2,2}(10.0, 0.0, 0.0, 10.0)
@test CellListMap.wrap_relative_to(x, y, unit_cell_matrix) xy
@test CellListMap.wrap_relative_to(y, x, unit_cell_matrix) yx
@test CellListMap.wrap_relative_to(x, y, unit_cell_matrix) xy
@test CellListMap.wrap_relative_to(y, x, unit_cell_matrix) yx
unit_cell_matrix = SMatrix{2,2}(-10.0, 0.0, 0.0, -10.0)
@test CellListMap.wrap_relative_to(x, y, unit_cell_matrix) xy
@test CellListMap.wrap_relative_to(y, x, unit_cell_matrix) yx
# orthorhombic cells: sides is a vector
sides = [10.0, 10.0]
sides = [10.0, 10.0]
@test CellListMap.wrap_relative_to(x, y, sides) xy
@test CellListMap.wrap_relative_to(y, x, sides) yx
sides = [-10.0, -10.0]
sides = [-10.0, -10.0]
@test CellListMap.wrap_relative_to(x, y, sides) xy
@test CellListMap.wrap_relative_to(y, x, sides) yx
end
Expand Down Expand Up @@ -207,13 +207,13 @@ end

@testitem "translation image" begin
using StaticArrays
x = SVector{2}[ [1.0, 1.0], [2.0, 2.0], [3.0, 3.0] ]
unitcell = [10.0 0.0 ; 0.0 10.0]
@test CellListMap.translation_image(x, unitcell, [0, 0]) [ [1.0, 1.0], [2.0, 2.0], [3.0, 3.0] ]
@test CellListMap.translation_image(x, unitcell, [1, 1]) [ [11.0, 11.0], [12.0, 12.0], [13.0, 13.0] ]
@test CellListMap.translation_image(x, unitcell, [1, 2]) [ [11.0, 21.0], [12.0, 22.0], [13.0, 23.0] ]
@test CellListMap.translation_image(x, unitcell, [2, 1]) [ [21.0, 11.0], [22.0, 12.0], [23.0, 13.0] ]
@test CellListMap.translation_image(x, unitcell, [-1, -1]) [ [-9.0, -9.0], [-8.0, -8.0], [-7.0, -7.0] ]
x = SVector{2}[[1.0, 1.0], [2.0, 2.0], [3.0, 3.0]]
unitcell = [10.0 0.0; 0.0 10.0]
@test CellListMap.translation_image(x, unitcell, [0, 0]) [[1.0, 1.0], [2.0, 2.0], [3.0, 3.0]]
@test CellListMap.translation_image(x, unitcell, [1, 1]) [[11.0, 11.0], [12.0, 12.0], [13.0, 13.0]]
@test CellListMap.translation_image(x, unitcell, [1, 2]) [[11.0, 21.0], [12.0, 22.0], [13.0, 23.0]]
@test CellListMap.translation_image(x, unitcell, [2, 1]) [[21.0, 11.0], [22.0, 12.0], [23.0, 13.0]]
@test CellListMap.translation_image(x, unitcell, [-1, -1]) [[-9.0, -9.0], [-8.0, -8.0], [-7.0, -7.0]]
end

#=
Expand Down Expand Up @@ -279,26 +279,26 @@ end
@testitem "replicate system" begin
using StaticArrays
using CellListMap: replicate_system!, replicate_system
unitcell = [ 10.0 0.0; 0.0 10.0 ]
x = SVector{2,Float64}[ [1.0, 1.0], [2.0, 2.0], [3.0, 3.0] ]
replicate_system!(x, unitcell, (1,0))
@test x [ [1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [11.0, 1.0], [12.0, 2.0], [13.0, 3.0] ]
x = SVector{2,Float64}[ [1.0, 1.0], [2.0, 2.0], [3.0, 3.0] ]
replicate_system!(x, unitcell, (1,1))
@test x [ [1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [11.0, 11.0], [12.0, 12.0], [13.0, 13.0] ]
x = SVector{2,Float64}[ [1.0, 1.0], [2.0, 2.0], [3.0, 3.0] ]
replicate_system!(x, unitcell, (0,1))
@test x [ [1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [1.0, 11.0], [2.0, 12.0], [3.0, 13.0] ]
x = SVector{2,Float64}[ [1.0, 1.0], [2.0, 2.0], [3.0, 3.0] ]
replicate_system!(x, unitcell, (-1,-1))
@test x [ [1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [-9.0, -9.0], [-8.0, -8.0], [-7.0, -7.0] ]
unitcell = [10.0 0.0; 0.0 10.0]
x = SVector{2,Float64}[[1.0, 1.0], [2.0, 2.0], [3.0, 3.0]]
replicate_system!(x, unitcell, (1, 0))
@test x [[1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [11.0, 1.0], [12.0, 2.0], [13.0, 3.0]]
x = SVector{2,Float64}[[1.0, 1.0], [2.0, 2.0], [3.0, 3.0]]
replicate_system!(x, unitcell, (1, 1))
@test x [[1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [11.0, 11.0], [12.0, 12.0], [13.0, 13.0]]
x = SVector{2,Float64}[[1.0, 1.0], [2.0, 2.0], [3.0, 3.0]]
replicate_system!(x, unitcell, (0, 1))
@test x [[1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [1.0, 11.0], [2.0, 12.0], [3.0, 13.0]]
x = SVector{2,Float64}[[1.0, 1.0], [2.0, 2.0], [3.0, 3.0]]
replicate_system!(x, unitcell, (-1, -1))
@test x [[1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [-9.0, -9.0], [-8.0, -8.0], [-7.0, -7.0]]
# with a matrix input
x = [1.0 2.0 3.0; 1.0 2.0 3.0]
y = replicate_system(x, unitcell, (1,0))
y = replicate_system(x, unitcell, (1, 0))
@test y [1.0 2.0 3.0 11.0 12.0 13.0; 1.0 2.0 3.0 1.0 2.0 3.0]
# throw error if dimensions do not match
x = SVector{2,Float64}[ [1.0, 1.0], [2.0, 2.0], [3.0, 3.0] ]
@test_throws DimensionMismatch replicate_system!(x, unitcell, (1,0,1))
x = SVector{2,Float64}[[1.0, 1.0], [2.0, 2.0], [3.0, 3.0]]
@test_throws DimensionMismatch replicate_system!(x, unitcell, (1, 0, 1))
end


Expand Down Expand Up @@ -379,13 +379,13 @@ by passing a function that takes the coordinates as input and throws an error if
are invalid.
"""
function limits(x::AbstractVector{<:AbstractVector}; validate_coordinates::Union{Nothing, Function} = _validate_coordinates)
function limits(x::AbstractVector{<:AbstractVector}; validate_coordinates::Union{Nothing,Function}=_validate_coordinates)
isnothing(validate_coordinates) || validate_coordinates(x)
xmin, xmax = _minmax(x)
return Limits(xmax .- xmin)
end

function limits(x::AbstractMatrix; validate_coordinates::Union{Nothing, Function}=_validate_coordinates)
function limits(x::AbstractMatrix; validate_coordinates::Union{Nothing,Function}=_validate_coordinates)
N = size(x, 1)
(N == 2 || N == 3) || throw(DimensionMismatch("The first dimension of the matrix must be the dimension (2 or 3)"))
x_re = reinterpret(reshape, SVector{N,eltype(x)}, x)
Expand All @@ -406,9 +406,9 @@ are invalid.
"""
function limits(
x::AbstractVector{<:AbstractVector},
x::AbstractVector{<:AbstractVector},
y::AbstractVector{<:AbstractVector};
validate_coordinates::Union{Nothing, Function} = _validate_coordinates
validate_coordinates::Union{Nothing,Function}=_validate_coordinates
)
isnothing(validate_coordinates) || validate_coordinates(x)
isnothing(validate_coordinates) || validate_coordinates(y)
Expand All @@ -419,7 +419,7 @@ function limits(
return Limits(xymax .- xymin)
end

function limits(x::AbstractMatrix, y::AbstractMatrix; validate_coordinates::Union{Nothing, Function}=_validate_coordinates)
function limits(x::AbstractMatrix, y::AbstractMatrix; validate_coordinates::Union{Nothing,Function}=_validate_coordinates)
N = size(x, 1)
M = size(y, 1)
N == M || throw(DimensionMismatch("The first dimension of the input matrices must be equal. "))
Expand All @@ -438,9 +438,9 @@ end
@test_throws ArgumentError limits(x)
@test_throws ArgumentError limits(x, y)
@test_throws ArgumentError limits(y, x)
x = rand(3,100)
x[2,50] = NaN
y = rand(3,100)
x = rand(3, 100)
x[2, 50] = NaN
y = rand(3, 100)
@test_throws ArgumentError limits(x)
@test_throws ArgumentError limits(x, y)
@test_throws ArgumentError limits(y, x)
Expand Down Expand Up @@ -588,8 +588,8 @@ end
R = random_rotation()
mr = R * m
ma, Ra = align_cell(mr)
@test ma[:,1] m[:,1]
@test cross([1,0,0], cross(ma[:,2],ma[:,3])) zeros(3) atol=1e-10
@test ma[:, 1] m[:, 1]
@test cross([1, 0, 0], cross(ma[:, 2], ma[:, 3])) zeros(3) atol = 1e-10
end

# throw error if not 2D or 3D
Expand Down
44 changes: 22 additions & 22 deletions src/ParticleSystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -860,16 +860,16 @@ end
x = coor(readPDB(CellListMap.argon_pdb_file))
sys1 = ParticleSystem(xpositions=x, cutoff=8.0, output=0.0)
@test unitcelltype(sys1) == NonPeriodicCell
@test sys1.unitcell [ 35.63 0.0 0.0; 0.0 35.76 0.0; 0.0 0.0 35.79 ] atol = 1e-2
@test sys1.unitcell [35.63 0.0 0.0; 0.0 35.76 0.0; 0.0 0.0 35.79] atol = 1e-2
update_cutoff!(sys1, 10.0)
@test sys1.unitcell [ 39.83 0.0 0.0; 0.0 39.96 0.0; 0.0 0.0 39.99 ] atol = 1e-2
@test sys1.unitcell [39.83 0.0 0.0; 0.0 39.96 0.0; 0.0 0.0 39.99] atol = 1e-2
a = @ballocated update_cutoff!($sys1, 8.0) evals = 1 samples = 1
@test a == 0
sys2 = ParticleSystem(xpositions=x[1:50], ypositions=x[51:100], cutoff=8.0, output=0.0)
@test unitcelltype(sys2) == NonPeriodicCell
@test sys2.unitcell [ 35.63 0.0 0.0; 0.0 35.76 0.0; 0.0 0.0 35.79 ] atol = 1e-2
@test sys2.unitcell [35.63 0.0 0.0; 0.0 35.76 0.0; 0.0 0.0 35.79] atol = 1e-2
update_cutoff!(sys2, 10.0)
@test sys2.unitcell [ 39.83 0.0 0.0; 0.0 39.96 0.0; 0.0 0.0 39.99 ] atol = 1e-2
@test sys2.unitcell [39.83 0.0 0.0; 0.0 39.96 0.0; 0.0 0.0 39.99] atol = 1e-2
a = @ballocated update_cutoff!($sys2, 8.0) evals = 1 samples = 1
@test a == 0
end
Expand Down Expand Up @@ -917,7 +917,7 @@ function UpdateParticleSystem!(sys::ParticleSystem1, update_lists::Bool=true)
end

function _update_ref_positions!(cl::CellListPair{V,N,T,Swap}, sys) where {V,N,T,Swap<:NotSwapped}
isnothing(sys.validate_coordinates) || sys.validate_coordinates(sys.xpositions)
isnothing(sys.validate_coordinates) || sys.validate_coordinates(sys.xpositions)
resize!(cl.ref, length(sys.xpositions))
cl.ref .= sys.xpositions
end
Expand Down Expand Up @@ -987,10 +987,10 @@ end

# Throw error when trying to *not* update lists with autoswap on:
sys = ParticleSystem(
xpositions = rand(SVector{2,Float64}, 100),
ypositions = rand(SVector{2,Float64}, 200),
unitcell = [1,1],
cutoff = 0.1,
xpositions=rand(SVector{2,Float64}, 100),
ypositions=rand(SVector{2,Float64}, 200),
unitcell=[1, 1],
cutoff=0.1,
output=0.0,
autoswap=true,
)
Expand Down Expand Up @@ -1074,27 +1074,27 @@ end
x = rand(SVector{3,Float64}, 100)
x[50] = SVector(1.1, NaN, 1.1)
@test_throws ArgumentError ParticleSystem(positions=x, cutoff=0.1, output=0.0)
@test_throws ArgumentError ParticleSystem(positions=x, cutoff=0.1, unitcell=[1,1,1], output=0.0)
@test_throws ArgumentError ParticleSystem(positions=x, cutoff=0.1, unitcell=[1, 1, 1], output=0.0)
y = rand(SVector{3,Float64}, 100)
p = ParticleSystem(positions=copy(y), cutoff=0.1, unitcell=[1,1,1], output=0.0)
p = ParticleSystem(positions=copy(y), cutoff=0.1, unitcell=[1, 1, 1], output=0.0)
p.positions .= x
@test_throws ArgumentError map_pairwise((x,y,i,j,d2,out) -> out += d2, p)
p = ParticleSystem(xpositions=copy(y), cutoff=0.1, unitcell=[1,1,1], output=0.0, validate_coordinates=nothing)
@test map_pairwise((x,y,i,j,d2,out) -> out += d2, p) > 0.0
@test_throws ArgumentError map_pairwise((x, y, i, j, d2, out) -> out += d2, p)
p = ParticleSystem(xpositions=copy(y), cutoff=0.1, unitcell=[1, 1, 1], output=0.0, validate_coordinates=nothing)
@test map_pairwise((x, y, i, j, d2, out) -> out += d2, p) > 0.0
# 2-set system
x = rand(SVector{3,Float64}, 100)
x[50] = SVector(1.1, NaN, 1.1)
y = rand(SVector{3,Float64}, 100)
@test_throws ArgumentError ParticleSystem(xpositions=x, ypositions=y, cutoff=0.1, output=0.0)
@test_throws ArgumentError ParticleSystem(xpositions=y, ypositions=x, cutoff=0.1, output=0.0)
@test_throws ArgumentError ParticleSystem(xpositions=x, ypositions=y, cutoff=0.1, unitcell=[1,1,1], output=0.0)
@test_throws ArgumentError ParticleSystem(xpositions=y, ypositions=x, cutoff=0.1, unitcell=[1,1,1], output=0.0)
p = ParticleSystem(xpositions=copy(y), ypositions=copy(y), cutoff=0.1, unitcell=[1,1,1], output=0.0)
@test_throws ArgumentError ParticleSystem(xpositions=x, ypositions=y, cutoff=0.1, unitcell=[1, 1, 1], output=0.0)
@test_throws ArgumentError ParticleSystem(xpositions=y, ypositions=x, cutoff=0.1, unitcell=[1, 1, 1], output=0.0)
p = ParticleSystem(xpositions=copy(y), ypositions=copy(y), cutoff=0.1, unitcell=[1, 1, 1], output=0.0)
p.xpositions .= x
@test_throws ArgumentError map_pairwise((x,y,i,j,d2,out) -> out += d2, p)
p = ParticleSystem(xpositions=copy(y), ypositions=copy(y), cutoff=0.1, unitcell=[1,1,1], output=0.0)
@test_throws ArgumentError map_pairwise((x, y, i, j, d2, out) -> out += d2, p)
p = ParticleSystem(xpositions=copy(y), ypositions=copy(y), cutoff=0.1, unitcell=[1, 1, 1], output=0.0)
p.ypositions .= x
@test_throws ArgumentError map_pairwise((x,y,i,j,d2,out) -> out += d2, p)
p = ParticleSystem(xpositions=copy(y), ypositions=copy(y), cutoff=0.1, unitcell=[1,1,1], output=0.0, validate_coordinates=nothing)
@test map_pairwise((x,y,i,j,d2,out) -> out += d2, p) > 0.0
@test_throws ArgumentError map_pairwise((x, y, i, j, d2, out) -> out += d2, p)
p = ParticleSystem(xpositions=copy(y), ypositions=copy(y), cutoff=0.1, unitcell=[1, 1, 1], output=0.0, validate_coordinates=nothing)
@test map_pairwise((x, y, i, j, d2, out) -> out += d2, p) > 0.0
end

0 comments on commit 3fe3c76

Please sign in to comment.