-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.html
406 lines (303 loc) · 15.4 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vortex Shedding Fluid Simulation</title>
<meta name="viewport" content="width=400">
<link rel="stylesheet" type="text/css" href="dependencies/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="dependencies/flat-ui.min.css">
<link rel="stylesheet" type="text/css" href="main.css">
<script id="2d-vertex-shader" type="x-shader/x-vertex">
attribute vec2 a_position;
void main() {
gl_Position = vec4(a_position, 0, 1);
}
</script>
<script id="resetVelocityShader" type="x-shader/x-fragment">
precision mediump float;
void main() {
gl_FragColor = vec4(1.0, 0.0, 0.0, 0.0);
}
</script>
<script id="boundaryConditionsShader" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D u_texture;
uniform float u_scale;
uniform vec2 u_textureSize;
uniform vec2 u_obstaclePosition;//scaled to texture size
uniform float u_obstacleRad;
void main() {
vec2 fragCoord = gl_FragCoord.xy;
vec2 dir = fragCoord - 3.0*vec2(0.5, 0.5) - u_obstaclePosition;//not sure where this fac of 3 came from?
float dist = length(dir);
if (dist < u_obstacleRad){
gl_FragColor = vec4(0);
return;
}
if (dist < u_obstacleRad+1.0){
gl_FragColor = u_scale*texture2D(u_texture, (fragCoord + dir/dist*2.0)/u_textureSize);
return;
}
gl_FragColor = texture2D(u_texture, fragCoord/u_textureSize);
}
</script>
<script id="2d-render-shader" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D u_material;
uniform vec2 u_obstaclePosition;
uniform vec2 u_textureSize;
uniform float u_obstacleRad;
void main() {
vec2 fragCoord = gl_FragCoord.xy;
vec2 dir = fragCoord - vec2(0.5, 0.5) - u_obstaclePosition;
float dist = length(dir);
if (dist < u_obstacleRad){
gl_FragColor = vec4(0.925, 0, 0.55, 1);
return;
}
float mat1 = texture2D(u_material, fragCoord/u_textureSize).x;
vec3 color = vec3(0.98, 0.93, 0.84);
gl_FragColor = vec4(mat1*color, 1);
}
</script>
<script id="gradientSubtractionShader" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D u_velocity;
uniform sampler2D u_pressure;
uniform vec2 u_textureSize;
uniform float u_const;
vec2 repeatBoundary(vec2 coord){
coord -= vec2(0.5, 0.5);
//if (coord.x < 0.0) coord.x = u_textureSize.x-1.0;
if (coord.x >= u_textureSize.x-1.0) coord.x = 0.0;
if (coord.y < 0.0) coord.y = u_textureSize.y-1.0;
else if (coord.y >= u_textureSize.y-1.0) coord.y = 0.0;
coord += vec2(0.5, 0.5);
return coord;
}
void main() {
vec2 fragCoord = gl_FragCoord.xy;
vec2 currentVelocity = texture2D(u_velocity, fragCoord/u_textureSize).xy;
float n = texture2D(u_pressure, repeatBoundary(fragCoord+vec2(0.0, 1.0))/u_textureSize).x;
float s = texture2D(u_pressure, repeatBoundary(fragCoord+vec2(0.0, -1.0))/u_textureSize).x;
float e = texture2D(u_pressure, (fragCoord+vec2(1.0, 0.0))/u_textureSize).x;
float w = texture2D(u_pressure, (fragCoord+vec2(-1.0, 0.0))/u_textureSize).x;
gl_FragColor = vec4(currentVelocity-u_const*vec2(e-w, n-s), 0, 0);
}
</script>
<script id="divergenceShader" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D u_velocity;
uniform vec2 u_textureSize;
uniform float u_const;
vec2 repeatBoundary(vec2 coord){
coord -= vec2(0.5, 0.5);
if (coord.x < 0.0) coord.x = u_textureSize.x-1.0;
else if (coord.x >= u_textureSize.x-1.0) coord.x = 0.0;
if (coord.y < 0.0) coord.y = u_textureSize.y-1.0;
else if (coord.y >= u_textureSize.y-1.0) coord.y = 0.0;
coord += vec2(0.5, 0.5);
return coord;
}
void main() {
vec2 fragCoord = gl_FragCoord.xy;
//finite difference formulation of divergence
//periodic boundary
float n = texture2D(u_velocity, repeatBoundary(fragCoord+vec2(0.0, 1.0))/u_textureSize).y;
float s = texture2D(u_velocity, repeatBoundary(fragCoord+vec2(0.0, -1.0))/u_textureSize).y;
float e = texture2D(u_velocity, repeatBoundary(fragCoord+vec2(1.0, 0.0))/u_textureSize).x;
float w = texture2D(u_velocity, (fragCoord+vec2(-1.0, 0.0))/u_textureSize).x;
float div = u_const*(e-w + n-s);
gl_FragColor = vec4(div, 0, 0, 0);
}
</script>
<script id="forceShader" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D u_velocity;
uniform vec2 u_textureSize;
uniform vec2 u_mouseCoord;
uniform vec2 u_mouseDir;
uniform float u_reciprocalRadius;
uniform float u_dt;
void main() {
vec2 fragCoord = gl_FragCoord.xy;
vec2 currentVelocity = texture2D(u_velocity, fragCoord/u_textureSize).xy;
vec2 pxDist = fragCoord - u_mouseCoord;
currentVelocity += u_mouseDir*u_dt*exp(-(pxDist.x*pxDist.x+pxDist.y*pxDist.y)*u_reciprocalRadius);
gl_FragColor = vec4(currentVelocity, 0, 0);
}
</script>
<script id="jacobiShader" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D u_b;
uniform sampler2D u_x;
uniform vec2 u_textureSize;
uniform float u_alpha;
uniform float u_reciprocalBeta;
void main() {
vec2 fragCoord = gl_FragCoord.xy;
vec2 currentState = texture2D(u_b, fragCoord/u_textureSize).xy;
//implicitly solve diffusion via jacobi iteration
vec2 n = texture2D(u_x, (fragCoord+vec2(0.0, 1.0))/u_textureSize).xy;
vec2 s = texture2D(u_x, (fragCoord+vec2(0.0, -1.0))/u_textureSize).xy;
vec2 e = texture2D(u_x, (fragCoord+vec2(1.0, 0.0))/u_textureSize).xy;
vec2 w = texture2D(u_x, (fragCoord+vec2(-1.0, 0.0))/u_textureSize).xy;
vec2 nextState = (n + s + e + w + u_alpha * currentState) * u_reciprocalBeta;
gl_FragColor = vec4(nextState, 0, 0);
}
</script>
<script id="advectShaderMat" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D u_velocity;
uniform sampler2D u_material;
uniform vec2 u_textureSize;
uniform float u_scale;
uniform float u_dt;
vec2 bilinearInterp(vec2 pos, sampler2D texture, vec2 size){
//bilinear interp between nearest cells
vec2 pxCenter = vec2(0.5, 0.5);
vec2 ceiled = ceil(pos);
vec2 floored = floor(pos);
vec2 n = texture2D(texture, (ceiled+pxCenter)/size).xy;//actually ne
vec2 s = texture2D(texture, (floored+pxCenter)/size).xy;//actually sw
if (ceiled.x != floored.x){
vec2 se = texture2D(texture, (vec2(ceiled.x, floored.y)+pxCenter)/size).xy;
vec2 nw = texture2D(texture, (vec2(floored.x, ceiled.y)+pxCenter)/size).xy;
n = n*(pos.x-floored.x) + nw*(ceiled.x-pos.x);
s = se*(pos.x-floored.x) + s*(ceiled.x-pos.x);
}
vec2 materialVal = n;
if (ceiled.y != floored.y){
materialVal = n*(pos.y-floored.y) + s*(ceiled.y-pos.y);
}
return materialVal;
}
void main() {
vec2 fragCoord = gl_FragCoord.xy;
vec2 pxCenter = vec2(0.5, 0.5);
//bilinear interp
//vec2 currentVelocity = 1.0/u_scale*texture2D(u_velocity, fragCoord/u_textureSize).xy;
vec2 currentVelocity = 1.0/u_scale*bilinearInterp((fragCoord-pxCenter)*u_scale + pxCenter, u_velocity, u_textureSize*u_scale);
//implicitly solve advection
if (fragCoord.x < 1.0){//boundary
float numCols = floor(u_textureSize.y/10.0);
if (mod(numCols, 2.0) == 1.0) numCols--;
float numPx = u_textureSize.y/numCols;
if (floor(mod((fragCoord.y-2.0)/numPx, 2.0)) == 0.0) gl_FragColor = vec4(1, 0, 0, 0);
else gl_FragColor = vec4(0, 0, 0, 0);
return;
}
if (length(currentVelocity) == 0.0) {//no velocity
gl_FragColor = vec4(texture2D(u_material, fragCoord/u_textureSize).xy, 0, 0);
return;
}
vec2 pos = fragCoord - pxCenter - u_dt*currentVelocity;
if (pos.x >= u_textureSize.x-1.0) {
gl_FragColor = vec4(0, 0, 0, 0);
return;
}
//periodic boundary in y
if (pos.y < 0.0) pos.y += u_textureSize.y-1.0;
if (pos.y >= u_textureSize.y-1.0) pos.y -= u_textureSize.y-1.0;
gl_FragColor = vec4(bilinearInterp(pos, u_material, u_textureSize), 0, 0);
}
</script>
<script id="advectShaderVel" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D u_velocity;
uniform sampler2D u_material;
uniform vec2 u_textureSize;
uniform float u_scale;
uniform float u_dt;
void main() {
vec2 fragCoord = gl_FragCoord.xy;
vec2 currentVelocity = u_scale*texture2D(u_velocity, fragCoord/u_textureSize).xy;
//implicitly solve advection
if (length(currentVelocity) == 0.0) {//boundary or no velocity
gl_FragColor = vec4(texture2D(u_material, fragCoord/u_textureSize).xy, 0, 0);
return;
}
vec2 pxCenter = vec2(0.5, 0.5);
vec2 pos = fragCoord - pxCenter - u_dt*currentVelocity;
if (pos.x < 1.0) {
gl_FragColor = vec4(1.0, 0, 0, 0);
return;
}
if (pos.x >= u_textureSize.x-1.0) {
gl_FragColor = vec4(0, 0, 0, 0);
//return;
}
if (pos.x >= u_textureSize.x-1.0) pos.x -= u_textureSize.x-1.0;
//periodic boundary in y
if (pos.y < 0.0) {
pos.y += u_textureSize.y-1.0;
}
if (pos.y >= u_textureSize.y-1.0) {
pos.y -= u_textureSize.y-1.0;
}
//bilinear interp between nearest cells
vec2 ceiled = ceil(pos);
vec2 floored = floor(pos);
vec2 n = texture2D(u_material, (ceiled+pxCenter)/u_textureSize).xy;//actually ne
vec2 s = texture2D(u_material, (floored+pxCenter)/u_textureSize).xy;//actually sw
if (ceiled.x != floored.x){
vec2 se = texture2D(u_material, (vec2(ceiled.x, floored.y)+pxCenter)/u_textureSize).xy;
vec2 nw = texture2D(u_material, (vec2(floored.x, ceiled.y)+pxCenter)/u_textureSize).xy;
n = n*(pos.x-floored.x) + nw*(ceiled.x-pos.x);
s = se*(pos.x-floored.x) + s*(ceiled.x-pos.x);
}
vec2 materialVal = n;
if (ceiled.y != floored.y){
materialVal = n*(pos.y-floored.y) + s*(ceiled.y-pos.y);
}
gl_FragColor = vec4(materialVal, 0, 0);
}
</script>
<script type="text/javascript" src="dependencies/jquery-3.1.0.min.js"></script>
<script type="text/javascript" src="dependencies/flat-ui.min.js"></script>
<script type="text/javascript" src="GlBoilerplate.js"></script>
<script type="text/javascript" src="GPUMath.js"></script>
<script type="text/javascript" src="main.js"></script>
</head>
<body>
<canvas id="glcanvas"></canvas>
<a href="#" id="about">?</a>
<div class="modal fade" id="aboutModal" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-body">
<b>Vortex Shedding Fluid Simulation</b><br/><br/>
This simulation solves the <a href="https://en.wikipedia.org/wiki/Navier%E2%80%93Stokes_equations" target="_blank">Navier-Stokes equations</a> for incompressible fluid flow past an obstacle in a GPU fragment shader.
It exhibits a phenomenon called <a href="https://en.wikipedia.org/wiki/Vortex_shedding" target="_blank">vortex shedding</a>,
where vortices of alternating spin spontaneously emerge behind the obstacle.
To increase performance, I solved for the velocity vector field of the fluid at a lower resolution than I used to compute the distribution of material moving through the fluid (shown in black and white).
I used bilinear interpolation to smooth out most artifacts caused by this speedup - though you can still see some artifacts around the boundary of the obstacle.
I ignored the viscous diffusion term from the Navier-Stokes formula to encourage better vortex formation (the implicit advection solving I'm using creates enough diffusion on its own for this system).
<br/><br/>
Click and drag to apply a force to the fluid:
<img style="width:100%" src="img2.jpg"/>
<br/><br/>
To learn more about the math involved, check out the following sources:<br/>
<a href="http://developer.download.nvidia.com/books/HTML/gpugems/gpugems_ch38.html" target="_blank">Fast Fluid Dynamics Simulation on the GPU</a> - a very well written tutorial about programming the Navier-Stokes equations on a GPU.
Though not WebGL specific, it was still very useful.<br/>
<a href="http://jamie-wong.com/2016/08/05/webgl-fluid-simulation/" target="_blank">Fluid Simulation (with WebGL demo)</a> - this article has some nice, interactive graphics that helped me debug my code.<br/>
<a href="http://www.dgp.toronto.edu/people/stam/reality/Research/pdf/ns.pdf" target="_blank">Stable Fluids</a> - a paper about stable numerical methods for evaluating Navier-Stokes on a discrete grid.<br/>
<br/>
Written by <a href="http://www.amandaghassaei.com/" target="_blank">Amanda Ghassaei</a> as a homework assignment for <a href="http://fab.cba.mit.edu/classes/MAS.864/" target="_blank">The Nature of Mathematical Modeling</a>, code on <a href="https://github.com/amandaghassaei/VortexShedding" target="_blank">Github</a>.
If you like this, you might also check out my other <a href="http://git.amandaghassaei.com/FluidSimulation">Fluid Simulation app</a>.
<br/><br/>
</div>
</div>
</div>
</div>
<div class="modal fade" id="noSupportModal" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-body">
This app uses GPU functions that are not supported by this device/browser, please try again on desktop with Chrome/Firefox.<br/><br/>
</div>
</div>
</div>
</div>
</body>
</html>