Skip to content

Commit a2f442c

Browse files
N-body simulations are moving here from DiffEqPhysics (#2)
transfer of n-body simulation files from DIffEqPhysics
1 parent 22120b0 commit a2f442c

29 files changed

+3054
-6
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# NbodySimulator
22

33
[![Build Status](https://travis-ci.org/JuliaDiffEq/NBodySimulator.jl.svg?branch=master)](https://travis-ci.org/JuliaDiffEq/NBodySimulator.jl)
4-
[![Build status](https://ci.appveyor.com/api/projects/status/1ofg9ianvcciq26v?svg=true)](https://ci.appveyor.com/project/Mikhail-Vaganov/nbodysimulator-jl)
4+
[![Build status](https://ci.appveyor.com/api/projects/status/1ofg9ianvcciq26v?svg=true)](https://ci.appveyor.com/project/Mikhail-Vaganov/nbodysimulator-jl)
5+
6+
This project is under development at the moment. The implementation of potential calculations is fairly experimental and has not been extensively verified yet.
7+
8+
You can test simulation of different systems now but be aware of possible changes in future.

REQUIRE

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
julia 0.7-beta2
1+
julia 0.7-beta2
2+
DiffEqBase 3.0.0
3+
OrdinaryDiffEq 3.0.0
4+
Reexport
5+
StaticArrays
6+
RecursiveArrayTools
7+
RecipesBase
8+
DiffEqCallbacks
9+
FileIO

examples/liquid_argon.jl

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using NBodySimulator
2+
3+
function generate_bodies_in_cell_nodes(n::Int, m::Real, v_dev::Real, L::Real)
4+
5+
rng = MersenneTwister(n);
6+
velocities = v_dev * randn(rng, Float64, (3, n))
7+
bodies = MassBody[]
8+
9+
count = 1
10+
dL = L / (ceil(n^(1 / 3)))
11+
for x = dL/2:dL:L, y = dL/2:dL:L, z = dL/2:dL:L
12+
if count > n
13+
break
14+
end
15+
r = SVector(x, y, z)
16+
v = SVector{3}(velocities[:,count])
17+
body = MassBody(r, v, m)
18+
push!(bodies, body)
19+
count += 1
20+
end
21+
return bodies
22+
end
23+
24+
function generate_bodies_in_line(n::Int, m::Real, v_dev::Real, L::Real)
25+
dL = L / (ceil(n^(1 / 3)))
26+
n_line = floor(Int, L / dL)
27+
rng = MersenneTwister(n);
28+
velocities = v_dev * randn(rng, Float64, (3, n_line))
29+
bodies = MassBody[]
30+
x = y = L / 2
31+
for i = 1:n_line
32+
r = SVector(x, y, i * dL)
33+
v = SVector{3}(velocities[:,i])
34+
body = MassBody(r, v, m)
35+
push!(bodies, body)
36+
end
37+
return bodies
38+
end
39+
40+
function generate_random_directions(n::Int)
41+
theta = acos.(1 - 2 * rand(n));
42+
phi = 2 * pi * rand(n);
43+
directions = [@SVector [sin(theta[i]) .* cos(phi[i]), sin(theta[i]) .* sin(phi[i]), cos(theta[i])] for i = 1:n]
44+
end
45+
46+
units = :real
47+
units = :reduced
48+
49+
const T = 120.0 # °K
50+
const kb = 1.38e-23 # J/K
51+
const ϵ = T * kb
52+
const σ = 3.4e-10 # m
53+
const ρ = 1374 # kg/m^3
54+
const m = 39.95 * 1.6747 * 1e-27 # kg
55+
const N = 125#floor(Int, ρ * L^3 / m)
56+
const L = (m*N/ρ)^(1/3)#10.229σ
57+
const R = 2.25σ
58+
const v_dev = sqrt(kb * T / m)
59+
const τ = 1e-14 # σ/v
60+
const t1 = 0τ
61+
const t2 = 2000τ
62+
#bodies = generate_bodies_randomly(N, m, v_dev, L)
63+
bodies = generate_bodies_in_cell_nodes(N, m, v_dev, L)
64+
#bodies = generate_bodies_in_line(N, m, v_dev, L)
65+
jl_parameters = LennardJonesParameters(ϵ, σ, R)
66+
pbc = CubicPeriodicBoundaryConditions(L)
67+
thermostat = AndersenThermostat(0.02, T, kb)
68+
lj_system = PotentialNBodySystem(bodies, Dict(:lennard_jones => jl_parameters));
69+
simulation = NBodySimulation(lj_system, (t1, t2), pbc, thermostat);
70+
#result = run_simulation(simulation, Tsit5())
71+
result = @time run_simulation(simulation, VelocityVerlet(), dt=τ)
72+
73+
#=
74+
using Plots
75+
import GR
76+
(rs, grf) = rdf(result)
77+
(ts, dr2) = msd(result)
78+
plot(rs/σ, grf, xlim=[0, 0.4999L/σ], label=["Radial distribution function"],ylabel="g(r)", xlabel="r/sigma")
79+
80+
using JLD
81+
time_now = Dates.format(now(), "yyyy_mm_dd_HH_MM_SS")
82+
Nactual = length(bodies)
83+
timesteps = round(length(result.solution.t))
84+
#save("D:/water $Nactual molecules $timesteps steps.jld", "rs", rs, "grf", grf, "ts", ts, "dr2", dr2)
85+
save("D:/liquid argon $Nactual molecules $timesteps steps $time_now.jld", "rs", rs, "grf", grf, "ts", ts, "dr2", dr2, "e_tot", e_tot, "e_kin", e_kin, "e_pot", e_pot)
86+
=#
87+
#=
88+
using Plots
89+
import GR
90+
time_now = Dates.format(now(), "yyyy_mm_dd_HH_MM_SS")
91+
Nactual = length(bodies)
92+
timesteps = round(length(result.solution.t))
93+
@time animate(result, "D:/$Nactual liquid argon particles with $timesteps timesteps $time_now.gif")
94+
95+
#plot(simulation)
96+
=#

examples/liquid_argon_omm_units.jl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using NBodySimulator
2+
using StochasticDiffEq
3+
4+
T = 120.0 # °K
5+
T0 = 90.0 # °K
6+
kb = 8.3144598e-3 # kJ/(K*mol)
7+
ϵ = T * kb
8+
σ = 0.34 # nm
9+
ρ = 1374/1.6747# Da/nm^3
10+
m = 39.95# Da
11+
N = 216
12+
L = (m*N/ρ)^(1/3)#10.229σ
13+
R = 0.5*L
14+
v_dev = sqrt(kb * T / m)
15+
bodies = generate_bodies_in_cell_nodes(N, m, v_dev, L)
16+
17+
τ = 0.5e-3 # ps or 1e-12 s
18+
t1 = 0.0
19+
t2 = 2000τ
20+
21+
parameters = LennardJonesParameters(ϵ, σ, R)
22+
lj_system = PotentialNBodySystem(bodies, Dict(:lennard_jones => parameters));
23+
thermostat = AndersenThermostat(90, 0.01/τ)
24+
#thermostat = BerendsenThermostat(90, 2000τ)
25+
#thermostat = LangevinThermostat(90, 0.00)
26+
#thermostat = NoseHooverThermostat(T0, 200τ)
27+
pbc = CubicPeriodicBoundaryConditions(L)
28+
simulation = NBodySimulation(lj_system, (t1, t2), pbc, thermostat, kb);
29+
result = @time run_simulation(simulation, VelocityVerlet(), dt=τ)
30+
#result = @time run_simulation_sde(simulation, ISSEM(symplectic=true,theta=0.5))
31+
32+
#t = t1:τ:result.solution.t[end-1]
33+
#temper = temperature.(result, t)
34+
35+
36+
#using Plots
37+
#pl=plot(t, temper, ylim=[0,200], xlabel="t, ps", ylabel = "T, °K", label="Temperature, °K", linewidth=2)
38+
#plot!(pl, t, T0*ones(length(t)), label = "90 °K", linewidth=2)
39+
#plot!(pl, title="Andersen thermostat at dt*v=$(thermostat.ν*τ) ")
40+
#plot!(pl, title="Berendsen thermostat at tau=$(thermostat.τ) ps")
41+
#plot!(pl, title="Nose-Hoover thermostat at tau=$(thermostat.τ) ps")
42+
43+
44+
#time_now = Dates.format(now(), "yyyy_mm_dd_HH_MM_SS")
45+
#Nactual = length(bodies)
46+
#timesteps = round(length(result.solution.t))
47+
48+
#@time save_to_pdb(result, "D:/liquid argon simulation $Nactual molecules and $timesteps steps $time_now.pdb" )
49+
50+
#using JLD
51+
#save("d:/nosehoover thermostat for water liquid argon $T0 and $(thermostat.τ) _$time_now.jld", "t",t, "temper", temper, "T0", T0, "τ", thermostat.τ)
52+
53+

examples/liquid_argon_reduced.jl

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using NBodySimulator
2+
3+
function generate_bodies_in_cell_nodes(n::Int, m::Real, v_dev::Real, L::Real)
4+
5+
rng = MersenneTwister(n);
6+
velocities = v_dev * randn(rng, Float64, (3, n))
7+
bodies = MassBody[]
8+
9+
count = 1
10+
dL = L / (ceil(n^(1 / 3)))
11+
for x = dL / 2:dL:L, y = dL / 2:dL:L, z = dL / 2:dL:L
12+
if count > n
13+
break
14+
end
15+
r = SVector(x, y, z)
16+
v = SVector{3}(velocities[:,count])
17+
body = MassBody(r, v, m)
18+
push!(bodies, body)
19+
count += 1
20+
end
21+
return bodies
22+
end
23+
24+
const T = 120.0 # °K
25+
const kb = 1.38e-23 # J/K
26+
const ϵ = T * kb
27+
const σ = 3.4e-10 # m
28+
const ρ = 1374 # kg/m^3
29+
const m = 39.95 * 1.6747 * 1e-27 # kg
30+
const N = 216#floor(Int, ρ * L^3 / m)
31+
const L = (m*N/ρ)^(1/3)#10.229σ
32+
const R = 2.25σ
33+
const v_dev = sqrt(kb * T / m)
34+
const τ = 0.5e-15 # σ/v, fs
35+
const t1 = 0.0
36+
const t2 = 3000τ
37+
38+
const _L = L / σ
39+
const= 1.0
40+
const= 1.0
41+
const _m = 1.0
42+
const _v = v_dev / sqrt/ m)
43+
const _R = R / σ
44+
const= τ * sqrt/ m) / σ
45+
const _t1 = t1 * sqrt/ m) / σ
46+
const _t2 = t2 * sqrt/ m) / σ
47+
bodies = generate_bodies_in_cell_nodes(N, _m, _v, _L)
48+
parameters = LennardJonesParameters(_ϵ, _σ, _R)
49+
lj_system = PotentialNBodySystem(bodies, Dict(:lennard_jones => parameters));
50+
simulation = NBodySimulation(lj_system, (_t1, _t2), CubicPeriodicBoundaryConditions(_L), _ϵ/T);
51+
#result = run_simulation(simulation, Tsit5())
52+
result = @time run_simulation(simulation, VelocityVerlet(), dt=_τ)
53+
54+
#(rs, grf) = rdf(result)
55+
#(ts, dr2) = msd(result)
56+
57+
#using Plots
58+
#plot(rs, grf, xlim=[0, 0.4999_L], label=["Radial distribution function"],ylabel="g(r)", xlabel="r")
59+
#plot(rs/_σ, grf, xlim=[0, 0.4999_L/_σ], label=["Radial distribution function"],ylabel="g(r)", xlabel="r/sigma")
60+
61+
62+
#time_now = Dates.format(now(), "yyyy_mm_dd_HH_MM_SS")
63+
#Nactual = length(bodies)
64+
#timesteps = round(length(result.solution.t))
65+
66+
#@time save_to_pdb(result, "D:/liquid argon simulation $Nactual molecules and $timesteps steps $time_now.pdb" )

examples/liquid_argon_sde.jl

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using NBodySimulator
2+
using StochasticDiffEq
3+
4+
T = 120.0 # °K
5+
T0 = 90
6+
kb = 8.3144598e-3 # kJ/(K*mol)
7+
ϵ = T * kb
8+
σ = 0.34 # nm
9+
ρ = 1374/1.6747# Da/nm^3
10+
m = 39.95# Da
11+
N = 216
12+
L = (m*N/ρ)^(1/3)#10.229σ
13+
R = 0.5*L
14+
v_dev = sqrt(kb * T / m)
15+
bodies = generate_bodies_in_cell_nodes(N, m, v_dev, L)
16+
17+
τ = 0.5e-3 # ps or 1e-12 s
18+
t1 = 0.0
19+
t2 = 10000τ
20+
21+
parameters = LennardJonesParameters(ϵ, σ, R)
22+
lj_system = PotentialNBodySystem(bodies, Dict(:lennard_jones => parameters));
23+
#thermostat = AndersenThermostat(T0, 1000)
24+
#thermostat = BerendsenThermostat(T0, 20τ)
25+
thermostat = LangevinThermostat(T0, 10.0)
26+
pbc = CubicPeriodicBoundaryConditions(L)
27+
simulation = NBodySimulation(lj_system, (t1, t2), pbc, thermostat, kb);
28+
#result = @time run_simulation(simulation, VelocityVerlet(), dt=τ)
29+
result = @time run_simulation_sde(simulation, EM(), dt=τ)
30+
31+
#=
32+
time_now = Dates.format(now(), "yyyy_mm_dd_HH_MM_SS")
33+
Nactual = length(bodies)
34+
timesteps = round(length(result.solution.t))
35+
36+
37+
using Plots
38+
t = t1:τ:result.solution.t[end-1]
39+
temper = temperature.(result, t)
40+
pl=plot(t, temper, ylim=[0,500], xlabel="t, ps", ylabel = "T, °K", label="Temperature, °K", linewidth=2)
41+
plot!(pl, t, T0*ones(length(t)), label = "$T0 °K", linewidth=2)
42+
plot!(pl, title="Langevin thermostat at gamma=$(thermostat.γ) 1/ps")
43+
44+
45+
using JLD
46+
save("d:/$(typeof(thermostat)) thermostat for water $T0 _$time_now.jld", "t",t, "temper", temper, "T0", T0, "thermostat", thermostat)
47+
=#

0 commit comments

Comments
 (0)