Skip to content

Commit 2620b84

Browse files
Merge pull request #41 from TheDisorderedOrganization/cleanup2
Continuation of the clean up
2 parents 6a8c10e + 031a30b commit 2620b84

7 files changed

Lines changed: 35 additions & 27 deletions

File tree

src/IO/xyz.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ end
7575
function write_header(io, system::Particles, t, format::XYZ, digits::Integer)
7676
println(io, length(system))
7777
box = replace(replace(string(system.box), r"[\[\]]" => ""), r",\s+" => ",")
78-
println(io, "step:$t columns:$(get_system_column(system, format))species,position dt:1 cell:$(box) rho:$(system.density) T:$(system.temperature) potential_energy_per_particle:$(mean(system.local_energy)/2)")
78+
println(io, "step:$t columns:$(get_system_column(system, format))species,position dt:1 cell:$(box) rho:$(system.density) T:$(system.temperature)")
7979
return nothing
8080
end
8181

src/ParticlesMC.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ include("moves.jl")
1616
get_position(system::Particles, i::Int) = @inbounds system.position[i]
1717
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)]
19-
get_local_energy(system::Particles, i::Int) = @inbounds system.local_energy[i]
2019
get_box(system::Particles) = system.box
2120
get_neighbour_list(system::Particles) = system.neighbour_list
2221
Base.length(system::Particles) = system.N
2322
Base.eachindex(system::Particles) = Base.OneTo(length(system))
24-
Base.getindex(system::Atoms, i::Int) = system.position[i], system.species[i], system.local_energy[i]
23+
Base.getindex(system::Atoms, i::Int) = system.position[i], system.species[i]
2524

2625
function Base.iterate(system::Union{Atoms, Molecules}, state=1)
2726
state > length(system) && return nothing # Stop iteration

src/atoms.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
struct Atoms{D, Q, V<:AbstractVector, C<:NeighbourList, H, T<:AbstractFloat, SM<:AbstractArray} <: Particles
1+
struct Atoms{D, V<:AbstractVector, C<:NeighbourList, H, T<:AbstractFloat, SM<:AbstractArray} <: Particles
22
position::Vector{SVector{D,T}}
33
species::V
44
density::T
@@ -8,7 +8,6 @@ struct Atoms{D, Q, V<:AbstractVector, C<:NeighbourList, H, T<:AbstractFloat, SM<
88
N::Int
99
d::Int
1010
box::SVector{D,T}
11-
local_energy::MVector{Q, T}
1211
neighbour_list::C
1312
species_list::H
1413
end
@@ -20,15 +19,17 @@ function System(position, species, density::T, temperature::T, model_matrix; lis
2019
d = length(Array(position)[1])
2120
energy = Vector{T}(undef, 1)
2221
box = @SVector fill(T((N / density)^(1 / d)), d)
23-
local_energy = MVector{N, T}(zeros(T, N))
24-
indices = MVector{N, Bool}(falses(N))
2522
maxcut = maximum([model.rcut for model in model_matrix])
2623
neighbour_list = list_type(box, maxcut, N)
2724
species_list = isa(species[1], Integer) ? SpeciesList(species) : nothing
28-
system = Atoms(position, species, density, energy, temperature, model_matrix, N, d, box, local_energy, neighbour_list, species_list)
25+
system = Atoms(position, species, density, energy, temperature, model_matrix, N, d, box, neighbour_list, species_list)
2926
build_neighbour_list!(system)
30-
system.local_energy .= [compute_energy_particle(system, i) for i in eachindex(system)]
31-
system.energy[1] = sum(system.local_energy) / 2
27+
local_energy = [compute_energy_particle(system, i, neighbour_list) for i in eachindex(position)]
28+
energy = mean(local_energy) / 2
29+
if isinf(energy) || isnan(energy)
30+
error("Initial configuration has infinite or NaN energy.")
31+
end
32+
system.energy[1] = energy
3233
return system
3334
end
3435

src/molecules.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ struct Molecules{D, VS<:AbstractVector, C<:NeighbourList, T<:AbstractFloat, SM<
1313
N::Int
1414
Nmol::Int
1515
box::SVector{D,T}
16-
local_energy::Vector{T}
1716
neighbour_list::C
1817
bonds::Vector{Vector{Int}}
1918
end
@@ -27,14 +26,17 @@ function System(position, species, molecule, density::T, temperature::T, model_m
2726
molecule_species = something(molecule_species, ones(Int, N))
2827
d = length(Array(position)[1])
2928
box = @SVector fill(T((N / density)^(1 / d)), d)
30-
local_energy = zeros(T, N)
3129
energy = zeros(T, 1)
3230
maxcut = maximum([model.rcut for model in model_matrix])
3331
neighbour_list = list_type(box, maxcut, N)
34-
system = Molecules(position, species, molecule, molecule_species, start_mol, length_mol, density, temperature, energy, model_matrix, d, N, Nmol,box, local_energy, neighbour_list, bonds)
32+
system = Molecules(position, species, molecule, molecule_species, start_mol, length_mol, density, temperature, energy, model_matrix, d, N, Nmol,box, neighbour_list, bonds)
3533
build_neighbour_list!(system)
36-
system.local_energy .= [compute_energy_particle(system, i, neighbour_list) for i in eachindex(position)]
37-
system.energy[1] = sum(system.local_energy) / 2
34+
local_energy = [compute_energy_particle(system, i, neighbour_list) for i in eachindex(position)]
35+
energy = mean(local_energy) / 2
36+
if isinf(energy) || isnan(energy)
37+
error("Initial configuration has infinite or NaN energy.")
38+
end
39+
system.energy[1] = energy
3840
return system
3941
end
4042

src/moves.jl

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ using ComponentArrays
22

33
function Arianna.perform_action!(system::Particles, action::Action)
44
e₁, e₂ = perform_action!(system, action)
5-
if isinf(e₂)
5+
if isinf(e₁) || isinf(e₂)
66
action.δe = zero(typeof(system.energy[1]))
77
else
88
action.δe = e₂ - e₁
@@ -141,15 +141,21 @@ end
141141
struct EnergyBias <: Policy end
142142

143143
function Arianna.log_proposal_density(action::DiscreteSwap, ::EnergyBias, parameters, system::Particles)
144-
numerator = parameters.θ₁ * system.local_energy[action.i] + parameters.θ₂ * system.local_energy[action.j]
145-
log_sum_exp_1 = log(sum(exp.(parameters.θ₁ .* system.local_energy[system.species_list.sp_ids[action.species[1]]])))
146-
log_sum_exp_2 = log(sum(exp.(parameters.θ₂ .* system.local_energy[system.species_list.sp_ids[action.species[2]]])))
144+
energy_particle_i = compute_energy_particle(system, action.i, system.neighbour_list)
145+
energy_particle_j = compute_energy_particle(system, action.j, system.neighbour_list)
146+
numerator = parameters.θ₁ * energy_particle_i+ parameters.θ₂ * energy_particle_j
147+
energy_particle_1 = compute_energy_particle(system, system.species_list.sp_ids[action.species[1]], system.neighbour_list)
148+
energy_particle_2 = compute_energy_particle(system, system.species_list.sp_ids[action.species[1]], system.neighbour_list)
149+
log_sum_exp_1 = log(sum(exp.(parameters.θ₁ .* energy_particle_1)))
150+
log_sum_exp_2 = log(sum(exp.(parameters.θ₂ .* energy_particle_1)))
147151
return numerator - log_sum_exp_1 - log_sum_exp_2
148152
end
149153

150154
function Arianna.sample_action!(action::DiscreteSwap, ::EnergyBias, parameters, system::Particles, rng)
151-
w1s = exp.(parameters.θ₁ .* system.local_energy[system.species_list.sp_ids[action.species[1]]])
152-
w2s = exp.(parameters.θ₂ .* system.local_energy[system.species_list.sp_ids[action.species[2]]])
155+
energy_particle_1 = compute_energy_particle(system, system.species_list.sp_ids[action.species[1]], system.neighbour_list)
156+
energy_particle_2 = compute_energy_particle(system, system.species_list.sp_ids[action.species[1]], system.neighbour_list)
157+
w1s = exp.(parameters.θ₁ .* energy_particle_1)
158+
w2s = exp.(parameters.θ₂ .* energy_particle_2)
153159
w1s .= w1s ./ sum(w1s)
154160
w2s .= w2s ./ sum(w2s)
155161
id1 = rand(rng, Categorical(w1s))

test/gerhard_energy_distribution.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ chains_ll = load_chains("test/config_0.lmp", args=Dict("temperature" => [0.231],
1111
system = chains[1]
1212
system_ll = chains_ll[1]
1313
# GERHARD: -2.676832
14-
@show mean(system.local_energy) / 2
15-
@show mean(system_ll.local_energy) / 2
14+
@show system.energy[1]
15+
@show system_ll.energy[1]
1616
chains_bkp = deepcopy(chains)
1717
chains_ll_bkp = deepcopy(chains_ll)
1818

test/runtests.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ using DelimitedFiles
1717
@test system_el.temperature == system_ll.temperature
1818
@test all.(system_el.position == system_ll.position)
1919
@test all.(system_el.species == system_ll.species)
20-
energy_el = mean(system_el.local_energy) / 2
21-
energy_ll = mean(system_ll.local_energy) / 2
20+
energy_el = system_el.energy[1]
21+
energy_ll = system_ll.energy[1]
2222
@test isapprox(energy_el, -2.676832, atol=1e-6)
2323
@test isapprox(energy_ll, -2.676832, atol=1e-6)
2424

@@ -118,8 +118,8 @@ end
118118
@test all.(system_el.position == system_ll.position)
119119
@test all.(system_el.species == system_ll.species)
120120
@test all.(system_el.bonds == system_ll.bonds)
121-
energy_el = mean(system_el.local_energy) / 2
122-
energy_ll = mean(system_ll.local_energy) / 2
121+
energy_el = system_el.energy[1]
122+
energy_ll = system_ll.energy[1]
123123
@test isapprox(energy_el, 25.65865662277199, atol=1e-6)
124124
@test isapprox(energy_ll, 25.65865662277199, atol=1e-6)
125125

0 commit comments

Comments
 (0)