-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
167 lines (156 loc) · 4.47 KB
/
index.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
/* pde-engine
*
* A PDE solver for the wave and diffusion equations.
*
* Ben Postlethwaite 2012
* benpostlethwaite.ca
*
* License MIT
*/
module.exports = function pdeEngine(spec) {
var that = {}
, spec = spec || {}
, dt = spec.dt || 0.1 // time step
, dx = spec.dx || 1 // spatial step
, gamma = spec.gamma || 0.02 // wave or diffusion paramater (controls decay)
, vel = spec.vel || 2 // wave velocity
, eqn = spec.eqn || 'wave' // or "diffusion"
, vres = spec.vres || 5 // default starting vertical resolution
, hres = spec.hres || 5 // default starting horizontal resolution
, u // main data array
, un // next time step data array
, up // previous time step data array
, uu // poisson data array
, si = [] // poisson sources x dim
, sj = [] // poisson sources y dim
, width
, height
, coeffs = [ // 2d laplace operator
0, 1, 0
, 1, -4, 1
, 0, 1, 0
]
/*
* 2D Guassian kernel for adding sources
*/
, gauss = [
1/256, 4/256, 6/256, 4/256, 1/256
, 4/256, 16/256, 24/256, 16/256, 4/256
, 6/256, 24/256, 36/256, 24/256, 6/256
, 4/256, 16/256, 24/256, 16/256, 4/256
, 1/256, 4/256, 6/256, 4/256, 1/256
]
/*
* c1, c2 & c3 are used for the wave eqn coefficients
* they have influence from gamma (wave decay)
* c4 is for diffusion equation, gamma controls rate of
* diffusion (conductivity or resistivity)
*/
, c1 = 2 - gamma * dt
, c2 = gamma * dt - 1
, c3 = (dt*dt * vel*vel) / (dx*dx)
, c4 = gamma * dt / (dx * dx)
setResolution(vres, hres)
/*
* Solves the wave equation PDE
* using convolution.
*/
function waveUpdate () {
var row, col, ind
var dum = conv2(u, coeffs)
for (row = 0; row < height; ++row)
for (col = 0; col < width; ++col) {
ind = row * width + col
un[ind] = c1 * u[ind] + c2 * up[ind] + c3 * dum[ind]
up[ind] = u[ind] // current becomes old
u[ind] = un[ind] // new becomes current
}
return u
}
/*
* Solves the diffusion equation PDE
* using convolution.
*/
function diffusionUpdate () {
var row, col, ind
var dum = conv2(u, coeffs)
for (row = 0; row < height; ++row)
for (col = 0; col < width; ++col) {
ind = row * width + col
un[ind] = u[ind] + c4 * dum[ind]
u[ind] = un[ind] // new becomes current
}
return u
}
/*
* iterates over image, then over kernel and
* multiplies the flipped kernel coeffs
* with appropriate image values, sums them
* then adds into new array entry.
*/
function conv2(image, kernel) {
var out = new Float64Array( height * width )
var acc = 0
, row, col, i, j, k
for ( row = 0; row < height; row++ ) {
for ( col = 0; col < width; col++ ) {
for ( i = -1; i <= 1; i++ ) {
for ( j = -1; j <= 1; j++ ) {
if( row+i >= 0 && col+j >= 0 &&
row+i < height && col+j < width) {
k = image[ (row + i) * width + (col + j)]
acc += k * kernel[ (1 + i) * 3 + (1 + j)]
}
}
}
out[ row * width + col] = acc
acc = 0
}
}
return out
}
/*
* adds a new gaussian droplet to u
* at specified coordinates.
*/
function addSource (row, col, mag) {
var i, j
for ( i = -2; i <= 2; i++ ) {
for ( j = -2; j <= 2; j++ ) {
if( row + i >= 0 && col + j >= 0 &&
row + i < height && col + j < width) {
u[(row + i) * width + (col + j)] += mag * gauss[ (i + 2) * 5 + j + 2]
}
}
}
}
/*
* function matches the matrix calculation sizes to
* reset size by init'ing new matrices.
*/
function reset() {
u = new Float64Array( height * width )
up = new Float64Array( height * width )
un = new Float64Array( height * width )
uu = new Float64Array( height * width )
}
/*
* when screen size is resized and upon init
* this does basic checking then calls reset
* to modify array sizes.
*/
function setResolution (vRes, hRes) {
width = hRes
height = vRes
reset()
}
if (eqn === 'diffusion') {
that.update = diffusionUpdate
}
else {
that.update = waveUpdate
}
that.setResolution = setResolution
that.addSource = addSource
return that
}