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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
using Plots
N = 10 # number of masses in the 1D lattice
K = 100 # elastic force constant
m = 1 # mass of particles
M = 1000 # big mass (one only)
t_final = 2 # seconds
dt = 0.001 # timestep
initial_distance_between_masses = 10 # meters
L = initial_distance_between_masses * N # length of the 1D lattice
# 2D array of current positions and velocities (Hamiltonian state)
q = zeros(N)
p = zeros(N)
# initialize the positions and velocities
for i in 1:N
q[i] = initial_distance_between_masses * (i - 1)
p[i] = 0
end
# plot positions
function plot_positions(q, t)
# only 1D, set y to 0
y = zeros(N)
# plot the masses
plot(
q, y,
seriestype = :scatter, label = "Masses @ t = $t",
# xlims = (-1, N + 1),
ylims = (-1, 1),
markerstrokewidth = 0,
)
# plot the middle one as red
plot!(
[q[Int(N / 2)]], [0],
seriestype = :scatter, label = "Large mass @ t = $t",
color = :red,
markerstrokewidth = 0, markersize = 10,
)
return plot!()
end
# update the state
function update_state!(q, p, dt)
new_q = copy(q)
new_p = copy(p)
# update the small masses state
for i in 2:N-1
dx_right = q[i+1] - q[i]
dx_left = q[i] - q[i-1]
new_q[i] += dt * p[i] / m
new_p[i] += dt * (K * dx_right - K * dx_left)
end
# handle the ends, since our 1D system is cyclic
# case where i = 1, first particle
dx_right = q[2] - q[1]
distance_from_L = L - q[N]
dy_left = q[1] - distance_from_L
new_q[1] += dt * p[1] / m
new_p[1] += dt * (K * dx_right - K * dy_left)
# case where i = N, last particle
distance_from_0 = q[1]
dx_right = L + q[1] - q[N]
dx_left = q[N] - q[N-1]
new_q[N] += dt * p[N] / m
new_p[N] += dt * (K * dx_right - K * dx_left)
# update the large mass in middle (different mass, difference case)
middle_index = Int(N / 2)
dx_right = q[middle_index+1] - q[middle_index]
dx_left = q[middle_index] - q[middle_index-1]
new_q[middle_index] += dt * p[middle_index] / M
new_p[Int(N / 2)] += dt * (K * dx_right - K * dx_left)
# update the state
for i in 1:N
q[i] = new_q[i]
p[i] = new_p[i]
end
end
display(plot_positions(q, 0))
function progress_system(q, p, dt, t_final)
t = 0
while t < t_final
update_state!(q, p, dt)
t += dt
end
end
progress_system(q, p, dt, t_final)
println("Final state:")
println(q)
println(p)
display(plot_positions(q, t_final))
|