-
Notifications
You must be signed in to change notification settings - Fork 13
/
Ideal Gas simulation code.py
246 lines (167 loc) · 7.72 KB
/
Ideal Gas simulation code.py
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import numpy as np
class Particle:
"""Define physics of elastic collision."""
def __init__(self, mass, radius, position, velocity):
"""Initialize a Particle object
mass the mass of particle
radius the radius of particle
position the position vector of particle
velocity the velocity vector of particle
"""
self.mass = mass
self.radius = radius
# last position and velocity
self.position = np.array(position)
self.velocity = np.array(velocity)
# all position and velocities recorded during the simulation
self.solpos = [np.copy(self.position)]
self.solvel = [np.copy(self.velocity)]
self.solvel_mag = [np.linalg.norm(np.copy(self.velocity))]
def compute_step(self, step):
"""Compute position of next step."""
self.position += step * self.velocity
self.solpos.append(np.copy(self.position))
self.solvel.append(np.copy(self.velocity))
self.solvel_mag.append(np.linalg.norm(np.copy(self.velocity)))
def check_coll(self, particle):
"""Check if there is a collision with another particle."""
r1, r2 = self.radius, particle.radius
x1, x2 = self.position, particle.position
di = x2-x1
norm = np.linalg.norm(di)
if norm-(r1+r2)*1.1 < 0:
return True
else:
return False
def compute_coll(self, particle, step):
"""Compute velocity after collision with another particle."""
m1, m2 = self.mass, particle.mass
r1, r2 = self.radius, particle.radius
v1, v2 = self.velocity, particle.velocity
x1, x2 = self.position, particle.position
di = x2-x1
norm = np.linalg.norm(di)
if norm-(r1+r2)*1.1 < step*abs(np.dot(v1-v2, di))/norm:
self.velocity = v1 - 2. * m2/(m1+m2) * np.dot(v1-v2, di) / (np.linalg.norm(di)**2.) * di
particle.velocity = v2 - 2. * m1/(m2+m1) * np.dot(v2-v1, (-di)) / (np.linalg.norm(di)**2.) * (-di)
def compute_refl(self, step, size):
"""Compute velocity after hitting an edge.
step the computation step
size the medium size
"""
r, v, x = self.radius, self.velocity, self.position
projx = step*abs(np.dot(v,np.array([1.,0.])))
projy = step*abs(np.dot(v,np.array([0.,1.])))
if abs(x[0])-r < projx or abs(size-x[0])-r < projx:
self.velocity[0] *= -1
if abs(x[1])-r < projy or abs(size-x[1])-r < projy:
self.velocity[1] *= -1.
def solve_step(particle_list, step, size):
"""Solve a step for every particle."""
# Detect edge-hitting and collision of every particle
for i in range(len(particle_list)):
particle_list[i].compute_refl(step,size)
for j in range(i+1,len(particle_list)):
particle_list[i].compute_coll(particle_list[j],step)
# Compute position of every particle
for particle in particle_list:
particle.compute_step(step)
################################################################################################################################
def init_list_random(N, radius, mass, boxsize):
"""Generate N Particle objects in a random way in a list."""
particle_list = []
for i in range(N):
v_mag = np.random.rand(1)*6
v_ang = np.random.rand(1)*2*np.pi
v = np.append(v_mag*np.cos(v_ang), v_mag*np.sin(v_ang))
collision = True
while(collision == True):
collision = False
pos = radius + np.random.rand(2)*(boxsize-2*radius)
newparticle = Particle(mass, radius, pos, v)
for j in range(len(particle_list)):
collision = newparticle.check_coll( particle_list[j] )
if collision == True:
break
particle_list.append(newparticle)
return particle_list
particle_number = 100
boxsize = 200.
# You need a larger tfin and stepnumber to get the equilibrium state. But the computation takes more time.
tfin = 10
stepnumber = 150
timestep = tfin/stepnumber
particle_list = init_list_random(particle_number, radius = 2, mass = 1, boxsize = 200)
# Compute simulation (It takes some time if stepnumber and particle_number are large)
for i in range(stepnumber):
solve_step(particle_list, timestep, boxsize)
#print(i)
################################################################################################################################
# Visualization of the solution with matplotlib. It use a slider to change the time
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(12, 6))
ax = fig.add_subplot(1,2,1)
hist = fig.add_subplot(1,2,2)
plt.subplots_adjust(bottom=0.2,left=0.15)
ax.axis('equal')
ax.axis([-1, 30, -1, 30])
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
ax.set_xlim([0,boxsize])
ax.set_ylim([0,boxsize])
# Draw Particles as circles
circle = [None]*particle_number
for i in range(particle_number):
circle[i] = plt.Circle((particle_list[i].solpos[0][0], particle_list[i].solpos[0][1]), particle_list[i].radius, ec="black", lw=1.5, zorder=20)
ax.add_patch(circle[i])
# Graph Particles speed histogram
vel_mod = [particle_list[i].solvel_mag[0] for i in range(len(particle_list))]
hist.hist(vel_mod, bins= 30, density = True, label = "Simulation Data")
hist.set_xlabel("Speed")
hist.set_ylabel("Frecuency Density")
# Compute 2d Boltzmann distribution
#total energy should be constant for any time index
def total_Energy(particle_list, index):
return sum([particle_list[i].mass / 2. * particle_list[i].solvel_mag[index]**2 for i in range(len(particle_list))])
E = total_Energy(particle_list, 0)
Average_E = E/len(particle_list)
k = 1.38064852e-23
T = 2*Average_E/(2*k)
m = particle_list[0].mass
v = np.linspace(0,10,120)
fv = m*np.exp(-m*v**2/(2*T*k))/(2*np.pi*T*k)*2*np.pi*v
hist.plot(v,fv, label = "Maxwell–Boltzmann distribution")
hist.legend(loc ="upper right")
from matplotlib.widgets import Slider
slider_ax = plt.axes([0.1, 0.05, 0.8, 0.05])
slider = Slider(slider_ax, # the axes object containing the slider
't', # the name of the slider parameter
0, # minimal value of the parameter
tfin, # maximal value of the parameter
valinit=0, # initial value of the parameter
color = '#5c05ff'
)
def update(time):
i = int(np.rint(time/timestep))
#ax.set_title('Energy =' + str(Energy[i]))
# Draw Particles as circles
for j in range(particle_number):
circle[j].center = particle_list[j].solpos[i][0], particle_list[j].solpos[i][1]
hist.clear()
# Graph Particles speed histogram
vel_mod = [particle_list[j].solvel_mag[i] for j in range(len(particle_list))]
hist.hist(vel_mod, bins= 30, density = True, label = "Simulation Data")
hist.set_xlabel("Speed")
hist.set_ylabel("Frecuency Density")
# Compute 2d Boltzmann distribution
E = total_Energy(particle_list, i)
Average_E = E/len(particle_list)
k = 1.38064852e-23
T = 2*Average_E/(2*k)
m = particle_list[0].mass
v = np.linspace(0,10,120)
fv = m*np.exp(-m*v**2/(2*T*k))/(2*np.pi*T*k)*2*np.pi*v
hist.plot(v,fv, label = "Maxwell–Boltzmann distribution")
hist.legend(loc ="upper right")
slider.on_changed(update)
plt.show()