-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinkcell3d.js
79 lines (69 loc) · 1.97 KB
/
linkcell3d.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
function Linkcell3d( sim ) {
// link to host simulation object
this.sim = sim;
// cut off must be set
var rm = sim.rc + sim.rp;
this.rm2 = rm*rm;
// number and size of cells (have at least 3 cells in each direction)
this.nx = 3;
this.ny = 3;
this.nz = 3;
// build the 3D linkcell grid
this.setup();
}
Linkcell3d.prototype.setup = function() {
// build the 3D linkcell grid
var i, j, k, l = new Array(this.nx);
for( i = 0; i < this.nx; ++i ) {
l[i] = new Array(this.ny);
for( j = 0; j < this.ny; ++j ) {
l[i][j] = new Array(this.nz);
for( k = 0; k < this.nz; ++k ) {
l[i][j][k] = [];
}
}
}
this.data = l;
}
Linkcell3d.prototype.clear = function() {
// clear each cell
var i,j,k;
for( i = 0; i < this.nx; ++i ) {
for( j = 0; j < this.ny; ++j ) {
for( k = 0; k < this.nz; ++k ) {
//this.data[i][j][k] = [];
this.data[i][j][k].length = 0;
}
}
}
}
Linkcell3d.prototype.update = function() {
// calculate optimal linkcell size (box size can be dynamic)
var rm = sim.rc + sim.rp,
nnx = Math.max( 3, Math.floor(sim.ss.x/rm) ),
nny = Math.max( 3, Math.floor(sim.ss.y/rm) ),
nnz = Math.max( 3, Math.floor(sim.ss.z/rm) );
// do we need to redimension the linkcell grid?
if( this.nx != nnx || this.ny != nny || this.nz != nnz ) {
this.nx = nnx;
this.ny = nny;
this.nz = nnz;
this.setup();
} else {
// clear
this.clear();
}
// cell size
var dx = sim.ss.x/this.nx,
dy = sim.ss.y/this.ny,
dz = sim.ss.z/this.nz;
// repopulate with all atoms
var lx, ly,lz, i;
for( i = 0; i < this.sim.atoms.length; ++i ) {
lx = Math.floor(this.sim.atoms[i].p.x/dx) % this.nx;
ly = Math.floor(this.sim.atoms[i].p.y/dy) % this.ny;
lz = Math.floor(this.sim.atoms[i].p.z/dz) % this.nz;
this.data[lx][ly][lz].push(i);
}
// TODO: density (maximum cell occupation) could also be used as a hint for redimensioning the grid (gas!)
}