Skip to content

Commit 39336ce

Browse files
Merge pull request #40 from TheDisorderedOrganization/cleanup
Clean up the code with new code version
2 parents 8009dd6 + 40161be commit 39336ce

8 files changed

Lines changed: 263 additions & 281 deletions

File tree

src/IO/IO.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function Arianna.write_system(io, system::Particles)
2020
println(io, "\tCell: $(system.box)")
2121
println(io, "\tDensity: $(system.density)")
2222
println(io, "\tTemperature: $(system.temperature)")
23-
println(io, "\tCell list: " * replace(string(typeof(system.cell_list)), r"\{.*" => ""))
23+
println(io, "\tNeighbour list: " * replace(string(typeof(system.neighbour_list)), r"\{.*" => ""))
2424
return nothing
2525
end
2626

src/ParticlesMC.jl

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ abstract type Particles <: AriannaSystem end
77

88

99
include("utils.jl")
10-
include("cell_list.jl")
10+
include("neighbours.jl")
1111
include("models.jl")
1212
include("molecules.jl")
1313
include("atoms.jl")
@@ -18,7 +18,7 @@ get_species(system::Particles, i::Int) = @inbounds system.species[i]
1818
get_model(system::Particles, i::Int, j::Int) = @inbounds system.model_matrix[get_species(system, i), get_species(system, j)]
1919
get_local_energy(system::Particles, i::Int) = @inbounds system.local_energy[i]
2020
get_box(system::Particles) = system.box
21-
get_neighbour_list(system::Particles) = system.cell_list
21+
get_neighbour_list(system::Particles) = system.neighbour_list
2222
Base.length(system::Particles) = system.N
2323
Base.eachindex(system::Particles) = Base.OneTo(length(system))
2424
Base.getindex(system::Atoms, i::Int) = system.position[i], system.species[i], system.local_energy[i]
@@ -28,16 +28,8 @@ function Base.iterate(system::Union{Atoms, Molecules}, state=1)
2828
return (system.position[state], state + 1) # Return element & next state
2929
end
3030

31-
function compute_local_energy(system::Particles, i)
32-
return compute_local_energy(system, i, system.cell_list)
33-
end
34-
35-
function destroy_particle!(system::Particles, i)
36-
return destroy_particle!(system, i, system.cell_list)
37-
end
38-
39-
function create_particle!(system::Particles, i)
40-
return destroy_particle!(system, i, system.cell_list)
31+
function compute_energy_particle(system::Particles, i)
32+
return compute_energy_particle(system, i, system.neighbour_list)
4133
end
4234

4335

src/atoms.jl

Lines changed: 20 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ struct Atoms{D, Q, V<:AbstractVector, C<:NeighbourList, H, T<:AbstractFloat, SM<
99
d::Int
1010
box::SVector{D,T}
1111
local_energy::MVector{Q, T}
12-
cell_list::C
12+
neighbour_list::C
1313
species_list::H
1414
end
1515

@@ -23,11 +23,11 @@ function System(position, species, density::T, temperature::T, model_matrix; lis
2323
local_energy = MVector{N, T}(zeros(T, N))
2424
indices = MVector{N, Bool}(falses(N))
2525
maxcut = maximum([model.rcut for model in model_matrix])
26-
cell_list = list_type(box, maxcut, N)
26+
neighbour_list = list_type(box, maxcut, N)
2727
species_list = isa(species[1], Integer) ? SpeciesList(species) : nothing
28-
system = Atoms(position, species, density, energy, temperature, model_matrix, N, d, box, local_energy, cell_list, species_list)
29-
build_cell_list!(system)
30-
system.local_energy .= [compute_local_energy(system, i) for i in eachindex(system)]
28+
system = Atoms(position, species, density, energy, temperature, model_matrix, N, d, box, local_energy, neighbour_list, species_list)
29+
build_neighbour_list!(system)
30+
system.local_energy .= [compute_energy_particle(system, i) for i in eachindex(system)]
3131
system.energy[1] = sum(system.local_energy) / 2
3232
return system
3333
end
@@ -47,11 +47,11 @@ function compute_energy_ij(system::Atoms, position_i, position_j, model_ij::Mode
4747
return potential(r2, model_ij)
4848
end
4949

50-
function compute_local_energy(system::Atoms, i)
51-
return compute_local_energy(system, i, system.cell_list)
50+
function compute_energy_particle(system::Atoms, i)
51+
return compute_energy_particle(system, i, system.neighbour_list)
5252
end
5353

54-
function compute_local_energy(system::Atoms, i, ::EmptyList)
54+
function compute_energy_particle(system::Atoms, i, ::EmptyList)
5555
energy_i = zero(typeof(system.density))
5656
position_i = get_position(system, i)
5757
for (j, _) in enumerate(system)
@@ -61,140 +61,37 @@ function compute_local_energy(system::Atoms, i, ::EmptyList)
6161
end
6262

6363

64-
function destroy_particle!(system::Atoms, i, ::EmptyList)
64+
function compute_energy_particle(system::Atoms, i, neighbour_list::CellList)
6565
energy_i = zero(typeof(system.density))
6666
position_i = get_position(system, i)
67-
# Loop over particles
68-
@inbounds for (j, _) in enumerate(system)
69-
energy_ij = check_compute_energy_ij(system, i, j, position_i)
70-
energy_i += energy_ij
71-
end
72-
return energy_i
73-
end
74-
75-
function create_particle!(system::Atoms, i, ::EmptyList)
76-
energy_i = zero(typeof(system.density))
77-
position_i = get_position(system, i)
78-
# Loop over particles
79-
@inbounds for (j, position_j) in enumerate(system)
80-
energy_ij = check_compute_energy_ij(system, i, j, position_i)
81-
energy_i += energy_ij
82-
end
83-
return energy_i
84-
end
67+
c = get_cell_index(position_i, neighbour_list)
68+
neighbour_cells = neighbour_list.neighbour_cells[c]
8569

86-
function compute_local_energy(system::Atoms, i, cell_list::CellList)
87-
energy_i = zero(typeof(system.density))
88-
position_i = get_position(system, i)
89-
mc = get_cell(position_i, cell_list)
90-
# Scan the neighbourhood of cell mc (including itself)
91-
@inbounds for mc2 in Iterators.product(map(x -> x-1:x+1, mc)...)
92-
# Calculate the scalar cell index of the neighbour cell (with PBC)
93-
c2 = cell_index(cell_list, mc2)
94-
# Scan atoms in cell c2
95-
@inbounds for j in cell_list.cells[c2]
96-
energy_i += check_compute_energy_ij(system, i, j, position_i)
97-
end
98-
end
99-
return energy_i
100-
end
101-
102-
# With linked list
103-
function destroy_particle!(system::Atoms, i, cell_list::CellList)
104-
# Get cell of particle i
105-
energy_i = zero(typeof(system.density))
106-
position_i = get_position(system, i)
107-
mc = get_cell(position_i, cell_list)
108-
# Scan the neighbourhood of cell mc (including itself)
109-
@inbounds for mc2 in Iterators.product(map(x -> x-1:x+1, mc)...)
110-
# Calculate the scalar cell index of the neighbour cell (with PBC)
111-
c2 = cell_index(cell_list, mc2)
112-
# Scan atoms in cell c2
113-
neighbours = cell_list.cells[c2]
114-
@inbounds for j in neighbours
115-
energy_ij = check_compute_energy_ij(system, i, j, position_i)
116-
energy_i += energy_ij
117-
end
118-
end
119-
return energy_i
120-
end
121-
122-
function create_particle!(system::Atoms, i, cell_list::CellList)
123-
energy_i = zero(typeof(system.density))
124-
# Get cell of particle i
125-
position_i = get_position(system, i)
126-
mc = get_cell(position_i, cell_list)
12770
# Scan the neighbourhood of cell mc (including itself)
128-
@inbounds for mc2 in Iterators.product(map(x -> x-1:x+1, mc)...)
129-
# Calculate the scalar cell index of the neighbour cell (with PBC)
130-
c2 = cell_index(cell_list, mc2)
71+
@inbounds for c2 in neighbour_cells
13172
# Scan atoms in cell c2
132-
neighbours = cell_list.cells[c2]
73+
neighbours = neighbour_list.cells[c2]
13374
@inbounds for j in neighbours
134-
energy_ij = check_compute_energy_ij(system, i, j, position_i)
135-
energy_i += energy_ij
136-
end
137-
end
138-
return energy_i
139-
end
140-
141-
142-
function compute_local_energy(system::Atoms, i, cell_list::LinkedList)
143-
energy_i = zero(typeof(system.density))
144-
# Get cell of particle i
145-
position_i = get_position(system, i)
146-
mc = get_cell(position_i, cell_list)
147-
# Scan the neighbourhood of cell mc (including itself)
148-
@inbounds for mc2 in Iterators.product(map(x -> x-1:x+1, mc)...)
149-
# Calculate the scalar cell index of the neighbour cell (with PBC)
150-
c2 = cell_index(cell_list, mc2)
151-
# Scan atoms in cell c2
152-
j = cell_list.head[c2]
153-
while (j != -1)
154-
energy_ij = check_compute_energy_ij(system, i, j, position_i)
155-
energy_i += energy_ij
156-
j = cell_list.list[j]
157-
end
158-
end
159-
return energy_i
160-
end
161-
162-
163-
function destroy_particle!(system::Atoms, i, cell_list::LinkedList)
164-
energy_i = zero(typeof(system.density))
165-
# Get cell of particle i
166-
position_i = get_position(system, i)
167-
mc = get_cell(position_i, cell_list)
168-
# Scan the neighbourhood of cell mc (including itself)
169-
@inbounds for mc2 in Iterators.product(map(x -> x-1:x+1, mc)...)
170-
# Calculate the scalar cell index of the neighbour cell (with PBC)
171-
c2 = cell_index(cell_list, mc2)
172-
# Scan atoms in cell c2
173-
j = cell_list.head[c2]
174-
while (j != -1)
175-
energy_ij = check_compute_energy_ij(system, i, j, position_i)
176-
energy_i += energy_ij
177-
j = cell_list.list[j]
75+
energy_i += check_compute_energy_ij(system, i, j, position_i)
17876
end
17977
end
18078
return energy_i
18179
end
18280

183-
function create_particle!(system::Atoms, i, cell_list::LinkedList)
81+
function compute_energy_particle(system::Atoms, i, neighbour_list::LinkedList)
18482
energy_i = zero(typeof(system.density))
18583
# Get cell of particle i
18684
position_i = get_position(system, i)
187-
mc = get_cell(position_i, cell_list)
85+
c = get_cell_index(position_i, neighbour_list)
86+
neighbour_cells = neighbour_list.neighbour_cells[c]
18887
# Scan the neighbourhood of cell mc (including itself)
189-
@inbounds for mc2 in Iterators.product(map(x -> x-1:x+1, mc)...)
190-
# Calculate the scalar cell index of the neighbour cell (with PBC)
191-
c2 = cell_index(cell_list, mc2)
88+
@inbounds for c2 in neighbour_cells
19289
# Scan atoms in cell c2
193-
j = cell_list.head[c2]
90+
j = neighbour_list.head[c2]
19491
while (j != -1)
19592
energy_ij = check_compute_energy_ij(system, i, j, position_i)
19693
energy_i += energy_ij
197-
j = cell_list.list[j]
94+
j = neighbour_list.list[j]
19895
end
19996
end
20097
return energy_i

0 commit comments

Comments
 (0)