-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdsmc0.html
318 lines (252 loc) · 7.23 KB
/
dsmc0.html
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<!-- Javascript+HTML DSMC Demo, Lubos Brieda, Dec 2012
Please visit http://www.particleincell.com/2012/html5-dsmc for more info -->
<canvas id="cell" style="border:1px solid #888;box-shadow:10px 10px 5px #888;" width=600 height=400>
<h2>Your browser does not support the <canvas> element. Please use a different browser or click <a href="http://www.particleincell.com/2012/dsmc0/">here</a> if you are viewing this in an email.</h2>
</canvas>
<script>
/***** START OF DSMC CLASS *****/
var dsmcCell = (function() {
var AMU = 1.66053886e-27; /*atomic mass*/
var K = 1.3806488e-23; /*Boltzmann constant*/
var V_c=0.001*0.01*0.001; /*cell volume*/
var sigma_cr_max=1e-18; /*initial guess*/
var Fn = 5e8; /*macroparticle weight*/
var delta_t=2e-5; /*time step length*/
var it; /*current iteration*/
var vdf; /*public array to hold VDF data*/
var part_list = new Array(); /*particle list*/
/*particle class, creates particle with isotropic velocity,
see: http://www.particleincell.com/2012/isotropic-velocity/ */
function Part(vdrift,temp,mass)
{
this.v=[];
/*sample speed*/
var vth = sampleVth(temp,mass);
/*pick a random direction*/
var theta = 2*Math.PI*Math.random();
var R = -1.0+2*Math.random();
var a = Math.sqrt(1-R*R);
this.v[0] = vth*Math.cos(theta)*a;
this.v[1] = vth*Math.sin(theta)*a;
this.v[2] = vth*R + vdrift;
this.mass=mass;
}
/*creates initial particle populations*/
function init()
{
/*clear data, important if restarting*/
part_list = [];
it = 0;
/*first create slow particles*/
for (var i=0;i<1000;i++)
part_list.push(new Part(100,5,32*AMU));
/*and now the fast ones*/
for (var i=0;i<1000;i++)
part_list.push(new Part(300,5,32*AMU));
/*show initial plot*/
vdf = computeVDF(0,450,25);
plotXY(vdf);
}
/* performs a single iteration of DSMC*/
function performDSMC()
{
/*fetch number of particles*/
var np = part_list.length;
/*compute number of groups according to NTC*/
var ng_f = 0.5*np*np*Fn*sigma_cr_max*delta_t/V_c;
var ng = Math.floor(ng_f+0.5); /*number of groups, round*/
var nc = 0; /*number of collisions*/
/*for updating sigma_cr_maxx*/
var sigma_cr_max_temp = 0;
var cr_vec = [];
/*iterate over groups*/
for (var i=0;i<ng;i++)
{
var part1,part2;
/*select first particle*/
part1=part_list[Math.floor(Math.random()*np)];
/*select the second one, making sure the two particles are unique*/
do {part2=part_list[Math.floor(Math.random()*np)];}
while (part1==part2);
/*relative velocity*/
for (var j=0;j<3;j++)
cr_vec[j] = part1.v[j]-part2.v[j];
var cr = mag(cr_vec);
/*eval cross section*/
var sigma = evalSigma(cr);
/*eval sigma_cr*/
var sigma_cr=sigma*cr;
/*update sigma_cr_max*/
if (sigma_cr>sigma_cr_max_temp)
sigma_cr_max_temp=sigma_cr;
/*eval prob*/
var P=sigma_cr/sigma_cr_max;
/*did the collision occur?*/
if (P>Math.random())
{
nc++;
collide(part1,part2);
}
}
/*update sigma_cr_max if we had collisions*/
if (nc)
sigma_cr_max = sigma_cr_max_temp;
/*show diagnostics*/
vdf = computeVDF(0,450,25);
plotXY(vdf);
it++;
/*output more data to the console*/
console.log("it: "+it+"\tng: "+ng+"\tnc: "+nc+"\tscmax: "+sigma_cr_max);
/*run for 500 time steps*/
if (it<500)
setTimeout(performDSMC,10);
}
/*samples from 1D Maxwellian using the Birdsall method*/
function maxw1D()
{
return 0.5*(Math.random()+Math.random()+Math.random()-1.5);
}
/*returns random thermal velocity*/
function sampleVth(temp,mass)
{
var sum=0;
var vth = Math.sqrt(2*K*temp/mass);
for (var i=0;i<3;i++)
{
var m1 = vth*maxw1D();
sum += m1*m1;
}
return Math.sqrt(sum);
}
/*evaluates cross-section using a simple (non-physical) inverse relationship*/
function evalSigma(rel_g)
{
return 1e-18*Math.pow(rel_g,-0.5);
}
/*returns magnitude of a 3 component vector*/
function mag(v)
{
return Math.sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);
}
/*performs momentum transfer collision between two particles*/
function collide (part1, part2)
{
/*center of mass velocity*/
var cm = [];
for (var i=0;i<3;i++)
cm[i] = (part1.mass*part1.v[i] + part2.mass*part2.v[i])/
(part1.mass+part2.mass);
/*relative velocity, magnitude remains constant through the collision*/
var cr=[];
for (var i=0;i<3;i++)
cr[i] = part1.v[i]-part2.v[i];
var cr_mag = mag(cr);
/*pick two random angles, per Bird's VHS method*/
var theta = Math.acos(2*Math.random()-1);
var phi = 2*Math.PI*Math.random();
/*perform rotation*/
cr[0] = cr_mag*Math.cos(theta);
cr[1] = cr_mag*Math.sin(theta)*Math.cos(phi);
cr[2] = cr_mag*Math.sin(theta)*Math.sin(phi);
/*post collision velocities*/
for (var i=0;i<3;i++)
{
part1.v[i] = cm[i]+part2.mass/(part1.mass+part2.mass)*cr[i];
part2.v[i] = cm[i]-part1.mass/(part1.mass+part2.mass)*cr[i];
}
}
/* bins velocities into nbins between min and max */
function computeVDF(min, max, nbins)
{
/*pointers for easier access*/
vel = [];
bin = [];
/*set delta range*/
var delta = (max-min)/(nbins-1);
/*shift left by half delta to center bins*/
min -= 0.5*delta;
max -= 0.5*delta;
/*set initial values*/
for (var i=0;i<nbins;i++)
{
vel[i] = i/(nbins-1);
bin[i] = 0;
}
/*compute histogram*/
for (var i=0;i<part_list.length;i++)
{
var part = part_list[i];
var vmag = mag(part.v);
var b = Math.floor((vmag-min)/(delta));
if (b<0 || b>=nbins-1) continue;
bin[b]++;
}
/*normalize values*/
var max_bin=0;
for (var b=0;b<nbins;b++)
if (bin[b]>max_bin) max_bin=bin[b];
for (var b=0;b<nbins;b++)
bin[b]/=max_bin;
return [vel, bin];
}
/*expose public members*/
return {init:init,performDSMC:performDSMC,vdf:vdf};
})();
/**** END OF DSMC CLASS *****/
/*grab canvas and related properties*/
var canvas=document.getElementById("cell");
var ctx = canvas.getContext("2d");
var height=canvas.height;
var width=canvas.width;
/*paint background before we scale the context*/
var grad=ctx.createRadialGradient(0,0,width/200,0,0,width/2);
grad.addColorStop(0,"purple");
grad.addColorStop(1,"rgba(255,255,255,0)");
ctx.fillStyle=grad;
ctx.fillRect(0,0,width,height);
/*create axis*/
ctx.strokeStyle="black";
ctx.fillStyle="purple";
ctx.lineWidth=3;
setLineDashSafe([10,6]);
ctx.strokeRect(0.05*width,0.05*height,0.9*width,0.9*height);
setLineDashSafe([]);
/*save background*/
var saved_bk = ctx.getImageData(1, 1, width, height);
/*scale to [0,1][0,1] and put origin at left/bottom to simplify graphing*/
ctx.translate(0.05*width,0.95*height);
ctx.scale(0.9*width,-0.9*height);
/*show initial distribution*/
dsmcCell.init();
/*handler for the button click*/
function startSim()
{
dsmcCell.init();
dsmcCell.performDSMC();
}
/*generates XY plot from xy[2][]*/
function plotXY(xy)
{
var x=xy[0],y=xy[1];
/*restore background*/
ctx.putImageData(saved_bk, 1, 1);
/*start plotting*/
ctx.beginPath();
ctx.moveTo(0,0);
for (var i=0;i<x.length;i++)
ctx.lineTo(x[i],y[i]);
/*return to the first point, and fill*/
ctx.lineTo(1,0);
ctx.lineTo(0,0);
ctx.fill();
}
/*helper function since Firefox does not seem to support setLineDash*/
function setLineDashSafe(style)
{
if (ctx.setLineDash)
ctx.setLineDash(style);
}
</script>
<form action="" style="padding-top: 20px">
<input type="button" value="Start the simulation!" style="font-size:1.2em;color:green;" onclick="startSim();">
</form>