-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTwoDSim.m
298 lines (226 loc) · 6.91 KB
/
TwoDSim.m
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
% Project: 2D gas simulation
% John Lian 260408431
% Feb 2015
clear all
% Input parameters
nMolecules = 400;
dimensionSize = sqrt(nMolecules);
nCollisions = 5000;
% Particle-particle collision array
% Same size as number of pairs
parHitTable = zeros(0.5*nMolecules*(nMolecules-1),3);
% Particle-wall collison array
wallHitTable = zeros(nMolecules,3);
% Particle diameter
d = 0.1;
% Particle mass
mass = 1;
% The wall
left = 0;
right = dimensionSize+1;
bottom = 0;
top = dimensionSize+1;
wall = [left right bottom top];
deltaWallHitTime = zeros(length(wall),1);
% Initialize arrays consisting of the positions for each molecule
pos = zeros(nMolecules,2);
vel = zeros(nMolecules,2);
xy = zeros(nMolecules,2,nCollisions);
uv = zeros(nMolecules,2,nCollisions);
c = zeros(nMolecules,1,nCollisions);
E = zeros(nCollisions,1);
T = zeros(nCollisions,1);
%% Initialization
disp('Initializing...');
t = 0;
count = 0;
% Initialize array for the first frame/timestep
for i = 1:dimensionSize
for j = 1:dimensionSize
count = count+1;
pos(count,1) = i;
pos(count,2) = j;
theta = 2*pi*rand(1,1);
vel(count,1) = cos(theta);
vel(count,2) = sin(theta);
end
end
% Test for recording the positions and velocities
xy(:,:,1) = pos(:,:);
uv(:,:,1) = vel(:,:);
count = 0;
% Initialize the particle collision table
for n = 1:nMolecules-1
for m = n+1:nMolecules
count = count+1;
% Calculate the vector relative position and velocity
dr = pos(n,:)-pos(m,:);
dv = vel(n,:)-vel(m,:);
% Calculate relavent constants based on input data
% See Haile Section 3.2.1
situationA = dot(dv,dr);
situationB = situationA^2 - norm(dv)^2*(norm(dr)^2-d^2);
parHitTable(count,1) = n;
parHitTable(count,2) = m;
% Checking if the two situations are satisfied for collision
if (situationA < 0 && situationB >= 0)
% Solve for time of closest approach.
parHitTable(count,3) = min(t+(-situationA+sqrt(situationB))/norm(dv)^2,...
t+(-situationA-sqrt(situationB))/norm(dv)^2);
end
end
end
parHitTable(parHitTable==0) = NaN;
count = 0;
% Initialize wall collision table
for n = 1:nMolecules
for m = 1:length(wall)
count = count+1;
wallHitTable(count,1) = n;
wallHitTable(count,2) = m;
% Use appropriate velocity components for the appropriate walls
if m == 1 || m == 2
wallHitTable(count,3) = t+(wall(m)-pos(n,1))/vel(n,1)-(d/2)/abs(vel(n,1));
else
wallHitTable(count,3) = t+(wall(m)-pos(n,2))/vel(n,2)-(d/2)/abs(vel(n,2));
end
end
end
wallHitTable(wallHitTable<=0) = NaN;
%% Plotting the initial frame
% set up first frame
figure('Color', 'white');
h = plot(xy(:,1,1),xy(:,2,1),...
'o','Color','red','MarkerFaceColor','red','MarkerSize',6);
xlim([0 dimensionSize+1]);
ylim([0 dimensionSize+1]);
ht = title(sprintf('Time: %0.2f sec', T(1)));
drawnow update
%% Math for collision and things
for frame = 2:nCollisions
% Determine which collision is going to happen next by finding the minimum
% of collision time
[nextParHitTime,pairFlag] = min(parHitTable(:,3));
[nextWallHitTime,wallFlag] = min(wallHitTable(:,3));
if (nextParHitTime < nextWallHitTime)
% 2 particles will collide before a wall collision occurs
nInvolved = 2;
dt = nextParHitTime-t;
parA = parHitTable(pairFlag,1);
parB = parHitTable(pairFlag,2);
% Advance time to collsion time
t = t+dt;
% Update all positions
pos = pos+vel*dt;
% Find post collision velocities and update
rHat = (pos(parB,:)-pos(parA,:))/norm(pos(parB,:)-pos(parA,:));
va = vel(parA,:)-dot((vel(parA,:)-vel(parB,:)),rHat)*rHat;
vb = vel(parB,:)+dot((vel(parA,:)-vel(parB,:)),rHat)*rHat;
% Update velocities
vel(parA,:) = va;
vel(parB,:) = vb;
% Clear collision time for this event
parHitTable(pairFlag,3) = NaN;
else
% A wall collision will happen first
nInvolved = 1;
dt = nextWallHitTime-t;
parA = wallHitTable(wallFlag,1);
wallHit = wallHitTable(wallFlag,2);
% Advance time to collsion time
t = t+dt;
% Update all positions
pos = pos+vel*dt;
% Update the velocity of the colliding particle
if wallHit == 1 || wallHit == 2
vel(parA,1) = -vel(parA,1);
else
vel(parA,2) = -vel(parA,2);
end
% Clear collision time for this event
wallHitTable(wallFlag,3) = NaN;
end
if isnan(pos(pos<0)) == 0
error('Negative position detected!!!');
end
% Update particle collision table for the relevant particles
for count = 1:nInvolved
if count == 1
% Find all the pairs for particles involved
[p2p2Check,~] = find(parHitTable == parA);
p2w2Check = parA*4-3:parA*4;
else
[p2p2Check,~] = find(parHitTable == parB);
p2w2Check = parB*4-3:parB*4;
end
for i = 1:nMolecules-1
n = parHitTable(p2p2Check(i),1);
m = parHitTable(p2p2Check(i),2);
% Calculate the vector relative position and velocity
dr = pos(n,:)-pos(m,:);
dv = vel(n,:)-vel(m,:);
% Calculate relavent constants based on input data
% See Haile Section 3.2.1
situationA = dot(dv,dr);
situationB = situationA^2 - norm(dv)^2*(norm(dr)^2-d^2);
% Checking if the two situations are satisfied for collision
if (situationA < 0 && situationB >= 0)
% Solve for time of closest approach.
tc = min(t+(-situationA+sqrt(situationB))/norm(dv)^2,...
t+(-situationA-sqrt(situationB))/norm(dv)^2);
parHitTable(p2p2Check(i),3) = tc;
end
end
% Update wall collision table for the relevant particle
for i = 1:length(wall)
n = wallHitTable(p2w2Check(i),1);
m = wallHitTable(p2w2Check(i),2);
% Use appropriate velocity components for the appropriate walls
if m == 1 || m == 2
deltaWallHitTime(m) = (wall(m)-pos(n,1))/vel(n,1)-(d/2)/abs(vel(n,1));
else
deltaWallHitTime(m) = (wall(m)-pos(n,2))/vel(n,2)-(d/2)/abs(vel(n,2));
end
if deltaWallHitTime(m) <= 1e-10
deltaWallHitTime(m) = NaN;
end
wallHitTable(p2w2Check(i),3) = t + deltaWallHitTime(m);
end
end
% Record the positions and velocities
xy(:,:,frame) = pos(:,:);
uv(:,:,frame) = vel(:,:);
c(:,1,frame) = sqrt(sum(vel.^2,2));
E(frame) = (1/2)*mass*sum(sum(vel.^2,2));
T(frame) = t;
set(h, 'XData', xy(:,1,frame));
set(h, 'YData', xy(:,2,frame));
set(ht, 'String', sprintf('%0.2f s, frame(%d)', T(frame),frame));
drawnow update
end
%% Probability distribution
figure(2)
hold on
edges = [0 0:0.05:5 5];
hc = histogram(c,edges,'Normalization','pdf');
% Boltzmann
k = 1;
% Temperature
T = 1/2;
f1 = @(y) (mass/(k*T))*y*exp(-mass*(y^2)/(2*k*T));
fplot(f1,[0 5])
xlabel('Speed (units/time)')
ylabel('Probability Distribution')
legend('Simulation', 'Maxwell Distribution')
hold off
figure(3)
edges = [-2.5 -2.5:0.05:2.5 2.5];
hold on
hu = histogram(uv(:,1,:),edges,'Normalization','pdf');
hv = histogram(uv(:,2,:),edges,'Normalization','pdf');
f2 = @(y) sqrt(mass/(2*pi*k*T))*exp(-mass*(y^2)/(2*k*T));
fplot(f2,[-2.5 2.5])
xlabel('Velocity (units/time)')
ylabel('Probability Distribution')
legend('Simulation for x-velocity', 'Simulation for y-velocity','Maxwell Distribution')
hold off