-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation.jl
76 lines (67 loc) · 1.87 KB
/
simulation.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
struct System
L::Float64
μ::Float64
β::Float64
Vext::Function
ϕ::Function
particles::Vector{Float64}
System(L::Number, μ::Number, T::Number, Vext::Function, ϕ::Function) = new(L, μ, 1 / T, Vext, ϕ, [])
end
mutable struct Histograms
bins::Int
dx::Float64
ρ::Vector{Float64}
count::Int
function Histograms(system::System; bins=1000)
new(bins, system.L / bins, zeros(bins), 0)
end
end
function pbc!(system::System, i)
system.particles[i] -= floor(system.particles[i] / system.L) * system.L
end
function dist(xi, xj, L)
result = xj - xi
result -= round(result / L) * L
abs(result)
end
function add_particle!(system::System, x)
push!(system.particles, x)
end
function remove_particle!(system::System, i)
deleteat!(system.particles, i)
end
function calc_energy(system::System, i)
xi = system.particles[i]
E = system.Vext(xi)
for xj in system.particles
if xi == xj
continue
end
E += system.ϕ(dist(xi, xj, system.L))
end
E
end
function trial_insert(system::System)
add_particle!(system, rand() * system.L)
i = length(system.particles)
ΔE = calc_energy(system, i)
if rand() > system.L / length(system.particles) * exp(system.β * (system.μ - ΔE))
# Rejected, undo trial insert
remove_particle!(system, i)
end
end
function trial_delete(system::System)
if isempty(system.particles)
return
end
i = rand(1:length(system.particles))
ΔE = calc_energy(system, i)
if rand() < length(system.particles) / system.L * exp(system.β * (ΔE - system.μ))
# Accepted, do the actual removal
remove_particle!(system, i)
end
end
function get_results(system::System, histograms::Histograms)
dx = histograms.dx
collect(dx/2:dx:system.L-dx/2), histograms.ρ / (histograms.count * dx)
end