-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathentbody.js
647 lines (578 loc) · 19.2 KB
/
entbody.js
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
// all of the global variables for dynamics
var x=[];
var y=[];
var vx=[];
var vy=[];
var fx=[];
var fy=[];
var r=[];
var type=[];
var col=[];
var colavg;
var ivor=[];
var ivoravg;
// things we can change
var n = 500;
var pbc = [1,1];
// neighborlist stuff
var lx, ly;
var size = [0,0];
var NMAX = 50;
var cells = [];
var count = [];
var radius = 1.0;
var R = 2*radius;
var FR= 2*R;
var gdt = 0.1;
// the variables we change
var epsilon = 100;
var flock = 0.55;
var noise = 0.0;
// some other constants that are 1
var vhappy = 1.0;
var damp = 1.0;
var frac = 0.15;
// display variables
var c;
var ctx;
var empty;
var frame = 0;
var keys = [0,0,0,0];
var frameskip = 2;
var colscale=25;
var dodraw = true;
var docircle = true;
var dovorticity = false;
var showforce = false;
var playmusic = false;
var MONITOR_GLOBALS=false;
function rgb(r,g,b) {
return 'rgb('+r+','+g+','+b+')';
}
function toFixed(value, precision) {
var precision = precision || 0,
neg = value < 0,
power = Math.pow(10, precision),
value = Math.round(value * power),
integral = String((neg ? Math.ceil : Math.floor)(value / power)),
fraction = String((neg ? -value : value) % power),
padding = new Array(Math.max(precision - fraction.length, 0) + 1).join('0');
return precision ? integral + '.' + padding + fraction : integral;
}
function mymod(a, b){
return a - b*Math.floor(a/b) + b*(a<0);
}
function mod_rvec(a, b, p, image, iind){
image[iind] = 1;
if (b==0) {if (a==0) {image[iind]=0;} return 0;}
if (p != 0){
if (a>b) {return a-b-1;}
if (a<0) {return a+b+1;}
} else {
if (a>b) {return b;}
if (a<0) {return 0;}
}
image[iind] = 0;
return a;
}
function calc_vorticity(){
var vor = 0.0;
var cmx = 0.0;
var cmy = 0.0;
var count = 0;
for (var i=0; i<n; i++){
if (type[i] == 1){
cmx += x[i];
cmy += y[i];
count++;
}
}
cmx /= count;
cmy /= count;
for (var i=0; i<n; i++){
if (type[i] == 1){
var tx = x[i] - cmx;
var ty = y[i] - cmy;
var tvx = vx[i];
var tvy = vy[i];
var tv = tvx*ty - tvy*tx;
vor += tv;//sqrt(tx*tx+ty*ty);
ivor[i] = tv;
}
}
ivoravg = vor/count;
return -vor/count;
}
function nbl_bin(){
for (var i=0; i<size[0]*size[1]; i++){
count[i] = 0;
}
for (var i=0; i<n; i++){
var indx = Math.floor(x[i]/lx * size[0]);
var indy = Math.floor(y[i]/ly * size[1]);
var tt = indx + indy*size[0];
cells[NMAX*tt + count[tt]] = i;
count[tt]++;
}
}
function update(){
colavg = 0.0;
var image = [0,0];
for (var i=0; i<n; i++) {
col[i] = 0.0;
fx[i] = 0.0;
fy[i] = 0.0;
var wx = 0.0;
var wy = 0.0;
var neigh = 0;
var indx = Math.floor(x[i]/lx * size[0]);
var indy = Math.floor(y[i]/ly * size[1]);
for (var ttx=-1; ttx<=1; ttx++){
for (var tty=-1; tty<=1; tty++){
var goodcell = 1;
var tixx = mod_rvec(indx+ttx, size[0]-1, pbc[0], image, 0);
var tixy = mod_rvec(indy+tty, size[1]-1, pbc[1], image, 1);
if (pbc[0] < image[0]){ goodcell = 0; }
if (pbc[1] < image[1]){ goodcell = 0; }
if (goodcell){
var cell = tixx + tixy*size[0];
for (var cc=0; cc<count[cell]; cc++){
var j = cells[NMAX*cell + cc];
var dx = x[j] - x[i]; if (image[0]) {dx += lx*ttx;}
var dy = y[j] - y[i]; if (image[1]) {dy += ly*tty;}
var l = Math.sqrt(dx*dx + dy*dy);
if (l > 1e-6 && l < R){
var r0 = (r[i]+r[j]);
var f = (1-l/r0);
var c0 = -epsilon * f*f * (l<r0);
fx[i] += c0*dx;
fy[i] += c0*dy;
var tcol = c0*c0*dx*dx + c0*c0*dy*dy;//fx[i]*fx[i] + fy[i]*fy[i];
col[i] += tcol;
}
if (type[i] == 1 && type[j] == 1 && l > 1e-6 && l < FR){
wx += vx[j]; wy += vy[j];
neigh++;
}
}
}
} }
var wlen = (wx*wx + wy*wy);
if (type[i] == 1 && neigh > 0 && wlen > 1e-6){
fx[i] += flock * wx / wlen;
fy[i] += flock * wy / wlen;
}
var vlen = (vx[i]*vx[i] + vy[i]*vy[i]);
var vhap = 0.0;
if (type[i]==1) { vhap = vhappy; } else { vhap = 0.0; }
if (vlen > 1e-6){
fx[i] += damp*(vhap - vlen)*vx[i]/vlen;
fy[i] += damp*(vhap - vlen)*vy[i]/vlen;
}
if (type[i] == 1){
fx[i] += noise * (Math.random()-0.5);
fy[i] += noise * (Math.random()-0.5);
}
if (type[i] == 1){
if (keys[0] == 1) {fy[i] -= 5.0;}
if (keys[1] == 1) {fy[i] += 5.0;}
if (keys[2] == 1) {fx[i] -= 5.0;}
if (keys[3] == 1) {fx[i] += 5.0;}
}
//var tcol = fx[i]*fx[i] + fy[i]*fy[i];
//col[i] += tcol;
colavg += col[i];
}
for (var i=0; i<n; i++){
vx[i] += fx[i] * gdt;
vy[i] += fy[i] * gdt;
x[i] += vx[i] * gdt;
y[i] += vy[i] * gdt;
if (pbc[0] == 0){
if (x[i] >= lx){x[i] = 2*lx-x[i]; vx[i] *= -1;}
if (x[i] < 0) {x[i] = -x[i]; vx[i] *= -1;}
} else {
if (x[i] >= lx || x[i] < 0) {x[i] = mymod(x[i], lx);}
}
if (pbc[1] == 0){
if (y[i] >= ly){y[i] = 2*ly-y[i]; vy[i] *= -1;}
if (y[i] < 0) {y[i] = -y[i]; vy[i] *= -1;}
} else {
if (y[i] >= ly || y[i] < 0) {y[i] = mymod(y[i], ly);}
}
if (dovorticity == true){
graph_vel(Math.sqrt(vx[i]*vx[i]+vy[i]*vy[i]));
}
}
colavg /= n;
}
function draw_all(x, y, r, lx, ly, cw, ch, ctx) {
var sx = cw/lx;
var sy = ch/ly;
var ss = Math.sqrt(sx*sy);
for (var i=0; i<x.length; i++) {
var indx = Math.floor(x[i]/lx * size[0]);
var indy = Math.floor(y[i]/ly * size[1]);
ctx.beginPath();
ctx.arc(sx*x[i], sy*y[i], ss*r[i], 0, 2*Math.PI, true);
ctx.lineWidth = 1;
ctx.strokeStyle = "#000000";
ctx.stroke();
var cr,cg,cb;
if (type[i] == 0){
if (showforce == true){
//cr = Math.floor(255*col[i]/(4*colavg+1e-5)+10);
//if (cr > 255) {cr = 255;}
cr = Math.abs(col[i]/colscale);
if (cr < 0) {cr = 0.0;}
if (cr > 1) {cr = 1.0;}
cr = Math.floor(255*cr);
cg = cr;
cb = cr;
} else {
cr = 50;
cg = 50;
cb = 50;
}
} else {
/*if (dovorticity == true){
if (ivor[i] < 0){
cr = Math.floor(Math.abs(255*ivor[i]/(4*ivoravg)));
cb = 0;
} else {
cb = Math.floor(Math.abs(255*ivor[i]/(4*ivoravg)));
cr = 0;
}
cg = 0;
} else {*/
cr = 255;
cg = 0;
cb = 0;
}
ctx.fillStyle = rgb(cr,cg,cb);
ctx.fill();
}
if (dovorticity == true){
graph_clear();
graph_draw();
}
}
function calc_sidelength(){
return Math.floor(1.03*Math.sqrt(Math.PI*radius*radius*n));
}
function init_sidelength(L){
lx = L;//
ly = lx;
update_boxslider();
/* initialize the neighborlist */
size[0] = Math.floor(lx / FR);
size[1] = Math.floor(ly / FR);
for (var i=0; i<size[0]*size[1]*NMAX; i++){
cells[i] = 0;
}
for (var i=0; i<size[0]*size[1]; i++){
count[i] = 0;
}
}
function init_empty(){
r = [];
x = [];
y = [];
vx = [];
vy = [];
type = [];
for (var i=0; i<n; i++) {
r.push(0.0);
x.push(0.0);
y.push(0.0);
type.push(0);
vx.push(0.0);
vy.push(0.0);
fx.push(0.0);
fy.push(0.0);
col.push(0.0);
}
}
function init_circle(){
for (var i=0; i<n; i++) {
var tx = lx*Math.random();
var ty = ly*Math.random();
var tt = 2*Math.PI*Math.random();
type[i] = 0;
r[i] = radius;
x[i] = tx;
y[i] = ty;
var dd = Math.sqrt((tx-lx/2)*(tx-lx/2) + (ty-ly/2)*(ty-ly/2));
var rad = Math.sqrt(frac*lx*ly/Math.PI);
if (docircle==true){
if (frac==0.01){type[0] =1;}
else if (frac==1.0) {type[i] = 1;}
else { if (dd<rad){ type[i] = 1; }else{ type[i] = 0; } }
} else {
if (frac==0.01){type[0] =1;}
else if (frac==1.0) {type[i] = 1;}
else { if (Math.random() < frac){ type[i] = 1;} else {type[i]=0;} }
}
vx[i] = vhappy*(Math.random()-0.5);
vy[i] = vhappy*(Math.random()-0.5);
}
}
/*======================================================================
the javascript interface stuff
=========================================================================*/
function update_epsilon(){
epsilon = document.getElementById('epsilon').value;
document.getElementById('label_epsilon').innerHTML = toFixed(epsilon,2);
}
function update_flock(){
flock = document.getElementById('flock').value;
document.getElementById('label_flock').innerHTML = toFixed(flock,2);
}
function update_noise(){
noise = document.getElementById('noise').value;
document.getElementById('label_noise').innerHTML = toFixed(noise,2);
}
function update_speed(){
vhappy = document.getElementById('speed').value;
document.getElementById('label_speed').innerHTML = toFixed(vhappy,2);
}
function update_damp(){
damp = document.getElementById('damp').value;
document.getElementById('label_damp').innerHTML = toFixed(damp,2);
}
function update_boxsize(){
lx = ly = document.getElementById('boxsize').value;
document.getElementById('label_boxsize').innerHTML = toFixed(lx, 2);
}
function update_dt(){
gdt = document.getElementById('dt').value;
document.getElementById('label_dt').innerHTML = toFixed(gdt, 2);
}
function update_frames(){
frameskip = document.getElementById('frames').value;
document.getElementById('label_frames').innerHTML = toFixed(frameskip, 2);
}
function update_frac(){
frac = document.getElementById('frac').value;
document.getElementById('label_frac').innerHTML = toFixed(frac, 2);
}
function update_colscale(){
colscale = document.getElementById('colscale').value;
document.getElementById('label_colscale').innerHTML = toFixed(colscale, 2);
}
function update_pbcx(){ pbc[0] = document.getElementById('periodicx').checked; }
function update_pbcy(){ pbc[1] = document.getElementById('periodicy').checked; }
function update_force(){ showforce = document.getElementById('showforce').checked; }
function update_circle(){docircle = document.getElementById('docircle').checked; }
/*function update_music() {
playmusic = document.getElementById('music').checked;
if (playmusic == true) {
var yt = document.getElementById('yt')
if (yt.playVideo) {
yt.playVideo();
}
}
else {
var yt = document.getElementById('yt')
if (yt.pauseVideo) {
yt.pauseVideo();
}
}
}*/
function update_vorticity(){
dovorticity = document.getElementById('vorticity').checked;
graph_del();
graph_clear();
}
function update_restart(){
init_empty();
init_circle(frac);
graph_del();
if (dodraw == false){ update_pause(); }
}
function update_pause(){
if (dodraw == true){
document.getElementById('pause').value = 'Start';
dodraw = false;
} else {
document.getElementById('pause').value = 'Pause';
requestAnimationFrame(tick, c);
dodraw = true;
}
}
function update_boxslider(){
/* var box = document.getElementById('boxsize');
var boxsize = Math.floor(lx);
var frac = 0.15
box.min = Math.floor(boxsize*(1-frac));
box.max = Math.floor(boxsize*(1+frac));
box.step = (2*frac*boxsize/20);
box.value = boxsize;
document.getElementById('label_boxsize').innerHTML = toFixed(lx, 2);*/
}
function update_num(){
n = document.getElementById('num').value;
init_sidelength(lx);//calc_sidelength());
update_restart();
update_boxslider();
}
/*===============================================================================
initialization and drawing
================================================================================*/
var tick = function(T) {
if (dodraw == true) {
ctx.fillStyle = 'rgba(200,200,200,0.2)';
ctx.clearRect(0, 0, c.width, c.height);
ctx.fillRect(0,0,c.width,c.height);
for (var i=0; i<frameskip; i++){
frame++;
nbl_bin();
update();
if (dovorticity == true){
graph_push(calc_vorticity());
}
}
draw_all(x, y, r, lx, ly, c.width, c.height, ctx);
requestAnimationFrame(tick, c);
}
};
function update_allcontrols(){
document.getElementById('num').value = n;
document.getElementById('periodicx').checked = pbc[0];
document.getElementById('periodicy').checked = pbc[1];
document.getElementById('docircle').checked = docircle
document.getElementById('showforce').checked = showforce;
document.getElementById('vorticity').checked = dovorticity;
document.getElementById('epsilon').value = epsilon;
document.getElementById('flock').value = flock;
document.getElementById('noise').value = noise;
document.getElementById('speed').value = vhappy;
document.getElementById('damp').value = damp;
document.getElementById('boxsize').value = lx;
document.getElementById('dt').value = gdt;
document.getElementById('frames').value = frameskip;
document.getElementById('frac').value = frac;
document.getElementById('colscale').value = colscale;
document.getElementById('label_epsilon').innerHTML = toFixed(epsilon,2);
document.getElementById('label_flock').innerHTML = toFixed(flock,2);
document.getElementById('label_noise').innerHTML = toFixed(noise,2);
document.getElementById('label_speed').innerHTML = toFixed(vhappy,2);
document.getElementById('label_damp').innerHTML = toFixed(damp,2);
document.getElementById('label_boxsize').innerHTML = toFixed(lx,2);
document.getElementById('label_dt').innerHTML = toFixed(gdt,2);
document.getElementById('label_frames').innerHTML = toFixed(frameskip,2);
document.getElementById('label_frac').innerHTML = toFixed(frac,2);
document.getElementById('label_colscale').innerHTML = toFixed(colscale,2);
/*document.getElementById('music').value = playmusic;
if (playmusic == true) {
var yt = document.getElementById('yt')
if (yt.playVideo) {
yt.playVideo();
}
}
else {
var yt = document.getElementById('yt')
if (yt.pauseVideo) {
yt.pauseVideo();
}
}*/
}
function create_moshpit(){
graph_init(); init_empty();
n=500; frac=0.15; frameskip = 2;
vhappy=1.0; noise=2.0; flock=0.1; epsilon=100; damp=1.0;dt=0.1;
init_sidelength(calc_sidelength()); init_circle(frac); dovorticity = true; dodraw=false;update_pause();
showforce = false; update_allcontrols(); graph_del(); graph_clear();
}
function create_circlepit(){
graph_init(); init_empty();
n=500; frac=0.15; frameskip = 2;
vhappy=1.0; noise=0.3; flock=1.0; epsilon=100; damp=1.0;dt=0.1;
init_sidelength(calc_sidelength()); init_circle(frac); dovorticity = true; dodraw=false;update_pause();
showforce = false; update_allcontrols(); graph_del(); graph_clear();
}
function create_chains(){
graph_init(); init_empty();
n=500; frac=0.01; frameskip=3;
vhappy=1.0; noise=0.0; flock=0.0; epsilon=150; damp=1.0;dt=0.1;
init_sidelength(48); init_circle(frac); showforce = true; dodraw=false;update_pause();
dovorticity = false; update_allcontrols(); graph_del(); graph_clear();
}
function create_crystal(){
graph_init(); init_empty();
n=500; frac=0.01; frameskip=2;
vhappy=0.0; noise=0.0; flock=0.0; epsilon=100; damp=1.0;dt=0.1;
init_sidelength(39.0); init_circle(frac); showforce = true; dodraw=false;update_pause();
dovorticity = false; update_allcontrols(); graph_del(); graph_clear();
}
function reverse() { for (var i=0; i<n; i++){ vx[i] = -vx[i]; vy[i] = -vy[i];} }
var init = function() {
// create the canvas element
empty = document.createElement('canvas');
empty.width = empty.height = 1;
c = document.getElementById('canvas');
c.style.cursor = 'url('+empty.toDataURL()+')';
ctx = c.getContext('2d');
graph_init();
graph_clear();
init_empty();
init_sidelength(calc_sidelength());
init_circle(frac);
update_allcontrols();
document.body.addEventListener('keyup', function(ev) {
if (ev.keyCode == 87){ keys[0] = 0; } //up
if (ev.keyCode == 83){ keys[1] = 0; } //down
if (ev.keyCode == 65){ keys[2] = 0; } //left
if (ev.keyCode == 68){ keys[3] = 0; } //right
if (ev.keyCode == 32){ ev.preventDefault(); update_pause(); } //space is pause
if (ev.keyCode == 82){ update_restart(); } //r is restart
if (ev.keyCode == 84){ reverse(); } //t reverses time
}, false);
document.body.addEventListener('keydown', function(ev) {
if (ev.keyCode == 87){ keys[0] = 1; } //up
if (ev.keyCode == 83){ keys[1] = 1; } //down
if (ev.keyCode == 65){ keys[2] = 1; } //left
if (ev.keyCode == 68){ keys[3] = 1; } //right
}, false);
registerAnimationRequest();
requestAnimationFrame(tick, c);
};
window.onload = init;
// Provides requestAnimationFrame in a cross browser way.
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
function registerAnimationRequest() {
if ( !window.requestAnimationFrame ) {
window.requestAnimationFrame = ( function() {
return window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame || // comment out if FF4 is slow (it caps framerate at ~30fps)
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element ) {
window.setTimeout( callback, 1 ); /*1000 / 60 );*/
};
} )();
}
}
if (MONITOR_GLOBALS) {
(function(){
var globals = {};
var startGlobals = [];
for (var j in window) {
globals[j] = true;
startGlobals.push(j);
}
if (false ) // PRINT_INITIAL_GLOBALS )
console.log("Initial globals: "+startGlobals.sort().join(', '));
setInterval(function() {
var newGlobals = [];
for (var j in window) {
if (!globals[j]) {
globals[j] = true;
newGlobals.push(j);
}
}
if (newGlobals.length > 0)
console.log("NEW GLOBALS: "+newGlobals.sort().join(', '));
}, 1000);
})();
}