-
Notifications
You must be signed in to change notification settings - Fork 0
/
Brownian_Motion.html
567 lines (533 loc) · 21.3 KB
/
Brownian_Motion.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>demo_3_Brownian_Motion</title>
<style type="text/css">
h3 {
color:black;
text-align: left;}
p {color:black;}
.column {
text-align:center;
float:left;
}
.column.left {
width:750px;
}
.column.right {
width:550px;
}
.row:after {
content: "";
display: table;
clear: both;
}
input.quantity{
text-align: center;
width:60px;
}
table{
text-align: left;
}
button{
padding-top: 2.5px;
padding-bottom: 2.5px;
}
.row{
min-width: 1300px;
}
</style>
<script src="Chart.bundle.js"></script>
</head>
<body>
<div class="row">
<div class="column left">
<canvas id="myCanvas" width="600" height="600" style="border:1px solid #000000; position: absolute; left: 100px; top: 80px; z-index: 1;">CANVAS FOR SIMULATION</canvas>
<canvas id="myLayer" width="600" height="600" style="border:1px solid #000000; position: absolute; left: 100px; top: 80px; z-index: 0;">LAYER</canvas>
<div style="line-height: 150%;">
<button onclick="clearInterval(timer);clearCanvas('myCanvas');initialize();drawParticles();distplot();motion()" style="font-size: medium;"><b>Start!</b></button><br>
<button id="trajectoryControl" onclick="changeTrajectoryState()">Show Trajectory</button>
<button id="boundaryCondition" onclick="changeBoundaryCondition()">Rigid Boundary</button>
</div>
</div>
<div class="column right">
<!--设置参数-->
<h3>Change Parameters</h3>
<table>
<tr>
<td>Number of molecules:</td>
<td><input id="Number" type="number" class="quantity" min="100" max="1200"></td>
</tr>
<tr>
<td>Radius of molecules:</td>
<td><input id="lowerRadius" type="number" class="quantity" min="3" max="20"></td>
</tr>
<tr>
<td>Radius of pollen:</td>
<td><input id="upperRadius" type="number" class="quantity" min="3" max="50"></td>
</tr>
<tr>
<td>Initial velocity distribution:</td>
<td><input type="radio" name="IVD" value="uniform">uniform<input type="radio" name="IVD" value="gaussian" checked="checked">gaussian</td>
</tr>
<th colspan="2">
<button id="changeParticalNumber" onclick="changeParameter()">Submit and Restart!</button>
</th>
</table>
<h3>Velocity Distribution</h3>
<canvas id="myChart1"></canvas>
<h3 style="position: absolute; top: 550px;">Collision Point</h3>
<canvas id="myMonitor_layer1" width="120" height="120" style="border:1px solid #ADD8E6; position: absolute; left: 910px; top: 560px; z-index: 0;"></canvas>
<canvas id="myMonitor_layer2" width="120" height="120" style="border:1px solid #ADD8E6; position: absolute; left: 910px; top: 560px; z-index: 1;"></canvas>
<canvas id="myMonitor_layer3" width="120" height="120" style="border:1px solid #ADD8E6; position: absolute; left: 910px; top: 560px; z-index: 2;"></canvas>
</div>
</div>
<script>
var N=400;//total number of particles 400 or 900
var r=5;//particle radius in px 5 or 3
var m;
var R=40;//pollen size in px 30
var M;
var IVD;
var molecule=[];
var canvasSize=600;//in px 600
var dT=10;
var trajectoryState=false;
var periodicBoundary=true;
var timer;
var collision_x=[];
var collision_y=[];
var collision_count=0;
var collision_count_current=0;
var collision_count_limit=50;
document.getElementById("Number").value=N;
document.getElementById("lowerRadius").value=r;
document.getElementById("upperRadius").value=R;
var ctx_myLayer=document.getElementById("myLayer").getContext("2d");
var ctx_myCanvas=document.getElementById("myCanvas").getContext("2d");
function initialize(){
m=Math.pow(r,2);
M=Math.pow(R,2);
var v_0=[];
molecule=[];
v_0=randomNormalDistribution();
molecule[0]={
xvalue:canvasSize/2,
yvalue:canvasSize/2,
xvelocity:v_0[0]*Math.sqrt(m/M),
yvelocity:v_0[1]*Math.sqrt(m/M),
xclass:Math.floor(this.xvalue/(canvasSize/10)),
yclass:Math.floor(this.yvalue/(canvasSize/10))
};
for(var i=1;i<N+1;i++){
var position_x=Math.random()*canvasSize/Math.sqrt(N)+(i%Math.sqrt(N))*canvasSize/Math.sqrt(N);
var position_y=Math.random()*canvasSize/Math.sqrt(N)+Math.floor(i/Math.sqrt(N))*canvasSize/Math.sqrt(N);
var distance_to_pollen=Math.sqrt(Math.pow(position_x-molecule[0].xvalue,2)+Math.pow(position_y-molecule[0].yvalue,2));
while(distance_to_pollen<=(r+R)){
position_x=Math.random()*canvasSize;
position_y=Math.random()*canvasSize;
distance_to_pollen=Math.sqrt(Math.pow(position_x-molecule[0].xvalue,2)+Math.pow(position_y-molecule[0].yvalue,2));
}
var v=randomNormalDistribution();
molecule[i]={
xvalue:position_x,//in px
yvalue:position_y,//in px
xvelocity:(IVD)?(Math.random()*3.6-1.8):v[0],
yvelocity:(IVD)?(Math.random()*3.6-1.8):v[1],
xclass:Math.floor(this.xvalue/(canvasSize/10)),
yclass:Math.floor(this.yvalue/(canvasSize/10))
}
}
}
function randomNormalDistribution(){
var u=0.0, v=0.0, w=0.0, c=0.0;
do{
u=Math.random()*2-1.0;
v=Math.random()*2-1.0;//获得两个(-1,1)的独立随机变量
w=u*u+v*v;
}while(w==0.0||w>=1.0)
c=Math.sqrt((-2*Math.log(w))/w);//Box-Muller转换
return [u*c,v*c];//返回2个标准正态分布的随机数,封装进一个数组返回
}
function createCircle(x,y,r,cmap,ctx){
//var cs=document.getElementById(canvasName);
//var ctx=cs.getContext("2d");
ctx.fillStyle=cmap;//"#ADD8E6" for lightblue; "#CD5C5C" for indian red; "#87CEEB" for skyblue
ctx.beginPath();
ctx.arc(x,y,r,0,2*Math.PI);
ctx.closePath(); //关闭路径
ctx.fill();
}
function clearCanvas(canvasName){
var cs=document.getElementById(canvasName);
var ctx=cs.getContext("2d");
ctx.clearRect(0,0,cs.width,cs.height);
}
function drawParticles(){
clearCanvas("myCanvas");
for(var i=1;i<N+1;i++){
createCircle(molecule[i].xvalue,molecule[i].yvalue,r,"#00BFFF",ctx_myCanvas);
}
createCircle(molecule[0].xvalue,molecule[0].yvalue,R,"rgba(205,92,92,0.7)",ctx_myCanvas);
if(molecule[0].xvalue<R){
createCircle(molecule[0].xvalue+canvasSize,molecule[0].yvalue,R,"rgba(205,92,92,0.7)",ctx_myCanvas);
}
else if(molecule[0].xvalue>(canvasSize-R)){
createCircle(molecule[0].xvalue-canvasSize,molecule[0].yvalue,R,"rgba(205,92,92,0.7)",ctx_myCanvas);
}
if(molecule[0].yvalue<R){
createCircle(molecule[0].xvalue,molecule[0].yvalue+canvasSize,R,"rgba(205,92,92,0.7)",ctx_myCanvas);
}
else if(molecule[0].yvalue>(canvasSize-R)){
createCircle(molecule[0].xvalue,molecule[0].yvalue-canvasSize,R,"rgba(205,92,92,0.7)",ctx_myCanvas);
}
}
function drawTrajectory(){
createCircle(molecule[0].xvalue,molecule[0].yvalue,0.5,"rgba(143,188,143,0.6)",ctx_myLayer);
}
function move(){
for(var i=0;i<N+1;i++){
var radius_i=(i===0)?R:r;
var mass_i=(i===0)?M:m;
molecule[i].xvalue+=molecule[i].xvelocity;
molecule[i].yvalue+=molecule[i].yvelocity;
var x1=molecule[i].xvalue;
var y1=molecule[i].yvalue;
//周期性边界条件
if(periodicBoundary){
if(x1<0 || x1>canvasSize){
molecule[i].xvalue=(canvasSize+x1)%canvasSize;
}
if(y1<0 || y1>canvasSize){
molecule[i].yvalue=(canvasSize+y1)%canvasSize;
}
}
//非周期性边界条件
else{
if(x1<radius_i || x1>(canvasSize-radius_i)){
molecule[i].xvelocity=(-molecule[i].xvelocity);
if(x1<radius_i) molecule[i].xvalue=radius_i;
else molecule[i].xvalue=canvasSize-radius_i;
}//碰撞边界
if(y1<radius_i || y1>(canvasSize-radius_i)){
molecule[i].yvelocity=(-molecule[i].yvelocity);
if(y1<radius_i) molecule[i].yvalue=radius_i;
else molecule[i].yvalue=canvasSize-radius_i;
}//碰撞边界*/
}
molecule[i].xclass=Math.floor(molecule[i].xvalue/(canvasSize/10));
molecule[i].yclass=Math.floor(molecule[i].yvalue/(canvasSize/10));
if(i!=0){//液体分子去碰撞别的粒子(包括pollen)
for(var j=0;j<N+1 && j!=i;j++){
var radius_j=(j===0)?R:r;
var mass_j=(j===0)?M:m;
var x1=molecule[i].xvalue;
var x2=molecule[j].xvalue;
var y1=molecule[i].yvalue;
var y2=molecule[j].yvalue;
var check1=false;
var xclass_2=molecule[j].xclass;
var xclass_1=molecule[i].xclass;
var yclass_2=molecule[j].yclass;
var yclass_1=molecule[i].yclass;
//判断是否需要考虑碰撞可能
if(periodicBoundary){
if(Math.abs(xclass_2-xclass_1)<=1 && Math.abs(yclass_2-yclass_1)<=1){
check1=true;
}
else if(j===0){
if(Math.abs(yclass_2-yclass_1)<=1 && Math.abs(xclass_1-xclass_2)===9){
check1=true;
x2+=(canvasSize*Math.sign(xclass_1-xclass_2));
}
else if(Math.abs(xclass_2-xclass_1)<=1 && Math.abs(yclass_1-yclass_2)===9){
check1=true;
y2+=(canvasSize*Math.sign(yclass_1-yclass_2));
}
else if(Math.abs(xclass_1-xclass_2)==9 && Math.abs(yclass_1-yclass_2)===9){
check1=true;
x2+=(canvasSize*Math.sign(xclass_1-xclass_2));
y2+=(canvasSize*Math.sign(yclass_1-yclass_2));
}
}
}
else{
check1=(Math.abs(xclass_2-xclass_1)<=1 && Math.abs(yclass_2-yclass_1)<=1);
}
if(check1){//判断是否需要考虑碰撞可能
var distance=Math.sqrt(Math.pow((x1-x2),2)+Math.pow((y1-y2),2));
if(distance<=(radius_i+radius_j)){//判断是否有碰撞可能,是则进入循环
var totalMass=mass_i+mass_j;
//relative to central mass
var v_x_relative_to_c_i=(molecule[i].xvelocity-molecule[j].xvelocity)*mass_j/totalMass;
var v_y_relative_to_c_i=(molecule[i].yvelocity-molecule[j].yvelocity)*mass_j/totalMass;
//normalized to unit vector
var v_relative_to_c_i_magnitude=Math.sqrt(Math.pow(v_x_relative_to_c_i,2)+Math.pow(v_y_relative_to_c_i,2));
var v_x_relative_to_c_i_normalized=v_x_relative_to_c_i/v_relative_to_c_i_magnitude;
var v_y_relative_to_c_i_normalized=v_y_relative_to_c_i/v_relative_to_c_i_magnitude;
//central mass
var v_x_c=(mass_i*molecule[i].xvelocity+mass_j*molecule[j].xvelocity)/totalMass;
var v_y_c=(mass_i*molecule[i].yvelocity+mass_j*molecule[j].yvelocity)/totalMass;
//判断是否碰撞
var dotProduct=v_y_relative_to_c_i_normalized*(y2-y1)+v_x_relative_to_c_i_normalized*(x2-x1);
if(dotProduct>0){
if(j===0){
collision_x[collision_count]=(x1-x2)/distance;
collision_y[collision_count]=(y1-y2)/distance;
collision_count_current=collision_count;
collision_count=(collision_count+1)%collision_count_limit;
}
var b=(v_x_relative_to_c_i_normalized*(y2-y1)-v_y_relative_to_c_i_normalized*(x2-x1));
var theta=2*Math.acos(b/(radius_i+radius_j));//scatter angle from 0 to 2*pi; 逆时针为正
var alpha;
if(v_y_relative_to_c_i_normalized>=0){
alpha=Math.acos(v_x_relative_to_c_i_normalized);
}//from 0 to pi
else{
alpha=2*Math.PI-Math.acos(v_x_relative_to_c_i_normalized);
}//from pi to 2*pi
var alpha_new=alpha-theta;
//velocity change after scatter
var v_x_relative_to_c_i_scatter=v_relative_to_c_i_magnitude*Math.cos(alpha_new);
var v_y_relative_to_c_i_scatter=v_relative_to_c_i_magnitude*Math.sin(alpha_new);
molecule[i].xvelocity=v_x_c+v_x_relative_to_c_i_scatter;
molecule[i].yvelocity=v_y_c+v_y_relative_to_c_i_scatter;
molecule[j].xvelocity=v_x_c-v_x_relative_to_c_i_scatter*mass_i/mass_j;
molecule[j].yvelocity=v_y_c-v_y_relative_to_c_i_scatter*mass_i/mass_j;
molecule[i].xvalue+=molecule[i].xvelocity;
molecule[i].yvalue+=molecule[i].yvelocity;
molecule[j].xvalue+=molecule[j].xvelocity;
molecule[j].yvalue+=molecule[j].yvelocity;//速度改变之后就分开了
//drawParticles();
molecule[i].xclass=Math.floor(molecule[i].xvalue/(canvasSize/10));
molecule[i].yclass=Math.floor(molecule[i].yvalue/(canvasSize/10));
molecule[j].xclass=Math.floor(molecule[j].xvalue/(canvasSize/10));
molecule[j].yclass=Math.floor(molecule[j].yvalue/(canvasSize/10));
}
}
}
}
}
}
}
function motion(){
var count=0;
collision_count=0;
createMonitorBG();
timer=setInterval(function(){
var collision_last_count=collision_count;
count+=1;
move();
drawParticles();
if(trajectoryState) drawTrajectory();
if(count%100==0){
distplot();
}
if(collision_count===collision_last_count){
collision_x[collision_count]=undefined;
collision_y[collision_count]=undefined;
collision_count_current=collision_count;
collision_count=(collision_count+1)%collision_count_limit;
}
createMonitor();
},dT);
}
function changeTrajectoryState(){
trajectoryState=(!trajectoryState);
if(trajectoryState===false){
clearCanvas('myLayer');
document.getElementById("trajectoryControl").innerHTML="Show Trajectory";
}
else{
document.getElementById("trajectoryControl").innerHTML="Hide Trajectory";
}
}
function changeBoundaryCondition(){
periodicBoundary=(!periodicBoundary);
document.getElementById("boundaryCondition").innerHTML=(periodicBoundary)?"Rigid Boundary":"Periodic Boundary";
}
function changeParameter(){
var trialN=Number(document.getElementById('Number').value);
var trialR=Number(document.getElementById('upperRadius').value);
var trialr=Number(document.getElementById('lowerRadius').value);
if(trialN<100 || trialN>1200){
alert('N is not in the range of [100,1200]')
}
else if(trialR<3 || trialR>50){
alert('R is not in the range of [3,50]')
}
else if(trialr<3 || trialr>20){
alert('r is not in the range of [3,20]')
}
else{
clearInterval(timer);
clearCanvas("myCanvas");
clearCanvas("myLayer");
N=Number(document.getElementById("Number").value);
R=Number(document.getElementById("upperRadius").value);
r=Number(document.getElementById("lowerRadius").value);
IVD=document.getElementsByName("IVD");
IVD=(IVD[0].checked===true);
initialize();
drawParticles();
distplot();
//document.getElementById("show").innerHTML=N;
motion();
}
}
var MC1 = document.getElementById('myChart1');
var MC2 = document.getElementById('myChart2');
var dataValues_X = [];
var dataValues_Y = [];
var dataValues_line=[];
var dataLabels = [];
var bins=30;
var start=-3;
var end=3;
var delta=(end-start)/bins;
delta=delta.toPrecision(1);
function distplot(){
for(var i=0;i<bins;i++){
dataLabels[i]=(start+i*delta).toPrecision(2);
dataValues_X[i]=0;
dataValues_Y[i]=0;
dataValues_line[i]=1/Math.sqrt(2*Math.PI)*Math.exp(-Math.pow(dataLabels[i],2)/2)*N*delta;
}
for(var i=1;i<N+1;i++){
var x=Math.floor((molecule[i].xvelocity-start)/delta);
var y=Math.floor((molecule[i].yvelocity-start)/delta);
dataValues_X[x]+=1;
dataValues_Y[y]+=1;
}
var barChartData={
labels: dataLabels,
datasets:[{
label: 'velocity along x axis',
backgroundColor: 'rgba(106,90,205,0.5)',
data: dataValues_X
}, {
label: 'velocity along y axis',
backgroundColor: 'rgba(154,205,50,0.7)',
data: dataValues_Y
},{
label:'Maxwell\'s velocity distribution',
data:dataValues_line,
type:'line',
borderColor:'rgba(211,211,211,0.5)',
backgroundColor:'rgba(211,211,211,0.5)',
fill:false
}]
};
var myChart1 = new Chart(MC1, {
type: 'bar',
data: barChartData,
options: {
hover: {
animationDuration: 0 // duration of animations when hovering an item
},
responsiveAnimationDuration: 0 ,// animation duration after a resize
animation: {
duration: 0 // general animation time
},
scales: {
barPercentage:1,
xAxes: [{
display: true,
ticks: {
autoSkip: false,
min:-2,
max:2
},
scaleLabel:{
display: true,
labelString: 'velocity'
}
}],
yAxes: [{
//stacked:true,
ticks: {
beginAtZero:true,
min:0,
max:Math.floor(N/80)*10
},
scaleLabel:{
display:true,
labelString:'count'
}
}]
}
}
});
/*var myChart2 = new Chart(MC2, {
type: 'bar',
data: {
labels: dataLabels,
datasets: [{
label: 'velocity along y axis',
data: dataValues_Y,
backgroundColor: 'rgba(54, 162, 235, 0.2)',
}]
},
options: {
animation: {
duration: 200 // general animation time
},
scales: {
xAxes: [{
display: true,
ticks: {
autoSkip: false,
min:-2,
max:2
},
barPercentage:1
}],
yAxes: [{
ticks: {
beginAtZero:true,
min:0,
max:100
}
}]
}
}
});*/
}
function createMonitorBG(){
clearCanvas("myMonitor_layer1");
clearCanvas("myMonitor_layer3");
//底层
var cs_monitor_1=document.getElementById("myMonitor_layer1");
var ctx_monitor_1=cs_monitor_1.getContext("2d");
ctx_monitor_1.fillStyle="rgba(205,92,92,0.7)";
ctx_monitor_1.beginPath();
ctx_monitor_1.arc(60,60,R,0,2*Math.PI);
ctx_monitor_1.closePath();
ctx_monitor_1.fill();
//上层
var cs_monitor_3=document.getElementById("myMonitor_layer3");
var ctx_monitor_3=cs_monitor_3.getContext("2d");
ctx_monitor_3.fillStyle="#ffffff";
ctx_monitor_3.arc(60,60,100,0,2*Math.PI,false);
ctx_monitor_3.arc(60,60,R+1,0,2*Math.PI,true);
ctx_monitor_3.fill();
}
function createMonitor(){
//中层
clearCanvas("myMonitor_layer2");
var cs_monitor_2=document.getElementById("myMonitor_layer2");
var ctx_monitor_2=cs_monitor_2.getContext("2d");
for(var i=0;i<collision_count_limit;i++){
ctx_monitor_2.fillStyle="#FFFF00";
if(i<=collision_count_current) ctx_monitor_2.globalAlpha=(1-Math.pow((collision_count_current-i)/collision_count_limit,2));
else ctx_monitor_2.globalAlpha=(1-Math.pow((collision_count_current+collision_count_limit-i)/collision_count_limit,2));
ctx_monitor_2.beginPath();
ctx_monitor_2.arc(60+collision_x[i]*R,60+collision_y[i]*R,r,0,2*Math.PI);
ctx_monitor_2.closePath(); //关闭路径
ctx_monitor_2.fill();
}
}
</script>
</body>
</html>