2
* Copyright 2012 the V8 project authors. All rights reserved.
3
* Copyright 2009 Oliver Hunt <http://nerget.com>
5
* Permission is hereby granted, free of charge, to any person
6
* obtaining a copy of this software and associated documentation
7
* files (the "Software"), to deal in the Software without
8
* restriction, including without limitation the rights to use,
9
* copy, modify, merge, publish, distribute, sublicense, and/or sell
10
* copies of the Software, and to permit persons to whom the
11
* Software is furnished to do so, subject to the following
14
* The above copyright notice and this permission notice shall be
15
* included in all copies or substantial portions of the Software.
17
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24
* OTHER DEALINGS IN THE SOFTWARE.
27
var NavierStokes = new BenchmarkSuite('NavierStokes', 1484000,
28
[new Benchmark('NavierStokes',
31
tearDownNavierStokes)]);
35
function runNavierStokes()
40
function setupNavierStokes()
42
solver = new FluidField(null);
43
solver.setResolution(128, 128);
44
solver.setIterations(20);
45
solver.setDisplayFunction(function(){});
46
solver.setUICallback(prepareFrame);
50
function tearDownNavierStokes()
55
function addPoints(field) {
57
for (var i = 1; i <= n; i++) {
58
field.setVelocity(i, i, n, n);
59
field.setDensity(i, i, 5);
60
field.setVelocity(i, n - i, -n, -n);
61
field.setDensity(i, n - i, 20);
62
field.setVelocity(128 - i, n + i, -n, -n);
63
field.setDensity(128 - i, n + i, 30);
67
var framesTillAddingPoints = 0;
68
var framesBetweenAddingPoints = 5;
70
function prepareFrame(field)
72
if (framesTillAddingPoints == 0) {
74
framesTillAddingPoints = framesBetweenAddingPoints;
75
framesBetweenAddingPoints++;
77
framesTillAddingPoints--;
81
// Code from Oliver Hunt (http://nerget.com/fluidSim/pressure.js) starts here.
82
function FluidField(canvas) {
83
function addFields(x, s, dt)
85
for (var i=0; i<size ; i++ ) x[i] += dt*s[i];
88
function set_bnd(b, x)
91
for (var i = 1; i <= width; i++) {
92
x[i] = x[i + rowSize];
93
x[i + (height+1) *rowSize] = x[i + height * rowSize];
96
for (var j = 1; i <= height; i++) {
97
x[j * rowSize] = -x[1 + j * rowSize];
98
x[(width + 1) + j * rowSize] = -x[width + j * rowSize];
100
} else if (b === 2) {
101
for (var i = 1; i <= width; i++) {
102
x[i] = -x[i + rowSize];
103
x[i + (height + 1) * rowSize] = -x[i + height * rowSize];
106
for (var j = 1; j <= height; j++) {
107
x[j * rowSize] = x[1 + j * rowSize];
108
x[(width + 1) + j * rowSize] = x[width + j * rowSize];
111
for (var i = 1; i <= width; i++) {
112
x[i] = x[i + rowSize];
113
x[i + (height + 1) * rowSize] = x[i + height * rowSize];
116
for (var j = 1; j <= height; j++) {
117
x[j * rowSize] = x[1 + j * rowSize];
118
x[(width + 1) + j * rowSize] = x[width + j * rowSize];
121
var maxEdge = (height + 1) * rowSize;
122
x[0] = 0.5 * (x[1] + x[rowSize]);
123
x[maxEdge] = 0.5 * (x[1 + maxEdge] + x[height * rowSize]);
124
x[(width+1)] = 0.5 * (x[width] + x[(width + 1) + rowSize]);
125
x[(width+1)+maxEdge] = 0.5 * (x[width + maxEdge] + x[(width + 1) + height * rowSize]);
128
function lin_solve(b, x, x0, a, c)
130
if (a === 0 && c === 1) {
131
for (var j=1 ; j<=height; j++) {
132
var currentRow = j * rowSize;
134
for (var i = 0; i < width; i++) {
135
x[currentRow] = x0[currentRow];
142
for (var k=0 ; k<iterations; k++) {
143
for (var j=1 ; j<=height; j++) {
144
var lastRow = (j - 1) * rowSize;
145
var currentRow = j * rowSize;
146
var nextRow = (j + 1) * rowSize;
147
var lastX = x[currentRow];
149
for (var i=1; i<=width; i++)
150
lastX = x[currentRow] = (x0[currentRow] + a*(lastX+x[++currentRow]+x[++lastRow]+x[++nextRow])) * invC;
157
function diffuse(b, x, x0, dt)
160
lin_solve(b, x, x0, a, 1 + 4*a);
163
function lin_solve2(x, x0, y, y0, a, c)
165
if (a === 0 && c === 1) {
166
for (var j=1 ; j <= height; j++) {
167
var currentRow = j * rowSize;
169
for (var i = 0; i < width; i++) {
170
x[currentRow] = x0[currentRow];
171
y[currentRow] = y0[currentRow];
179
for (var k=0 ; k<iterations; k++) {
180
for (var j=1 ; j <= height; j++) {
181
var lastRow = (j - 1) * rowSize;
182
var currentRow = j * rowSize;
183
var nextRow = (j + 1) * rowSize;
184
var lastX = x[currentRow];
185
var lastY = y[currentRow];
187
for (var i = 1; i <= width; i++) {
188
lastX = x[currentRow] = (x0[currentRow] + a * (lastX + x[currentRow] + x[lastRow] + x[nextRow])) * invC;
189
lastY = y[currentRow] = (y0[currentRow] + a * (lastY + y[++currentRow] + y[++lastRow] + y[++nextRow])) * invC;
198
function diffuse2(x, x0, y, y0, dt)
201
lin_solve2(x, x0, y, y0, a, 1 + 4 * a);
204
function advect(b, d, d0, u, v, dt)
206
var Wdt0 = dt * width;
207
var Hdt0 = dt * height;
208
var Wp5 = width + 0.5;
209
var Hp5 = height + 0.5;
210
for (var j = 1; j<= height; j++) {
211
var pos = j * rowSize;
212
for (var i = 1; i <= width; i++) {
213
var x = i - Wdt0 * u[++pos];
214
var y = j - Hdt0 * v[pos];
231
var row1 = j0 * rowSize;
232
var row2 = j1 * rowSize;
233
d[pos] = s0 * (t0 * d0[i0 + row1] + t1 * d0[i0 + row2]) + s1 * (t0 * d0[i1 + row1] + t1 * d0[i1 + row2]);
239
function project(u, v, p, div)
241
var h = -0.5 / Math.sqrt(width * height);
242
for (var j = 1 ; j <= height; j++ ) {
243
var row = j * rowSize;
244
var previousRow = (j - 1) * rowSize;
245
var prevValue = row - 1;
246
var currentRow = row;
247
var nextValue = row + 1;
248
var nextRow = (j + 1) * rowSize;
249
for (var i = 1; i <= width; i++ ) {
250
div[++currentRow] = h * (u[++nextValue] - u[++prevValue] + v[++nextRow] - v[++previousRow]);
257
lin_solve(0, p, div, 1, 4 );
258
var wScale = 0.5 * width;
259
var hScale = 0.5 * height;
260
for (var j = 1; j<= height; j++ ) {
261
var prevPos = j * rowSize - 1;
262
var currentPos = j * rowSize;
263
var nextPos = j * rowSize + 1;
264
var prevRow = (j - 1) * rowSize;
265
var currentRow = j * rowSize;
266
var nextRow = (j + 1) * rowSize;
268
for (var i = 1; i<= width; i++) {
269
u[++currentPos] -= wScale * (p[++nextPos] - p[++prevPos]);
270
v[currentPos] -= hScale * (p[++nextRow] - p[++prevRow]);
277
function dens_step(x, x0, u, v, dt)
279
addFields(x, x0, dt);
280
diffuse(0, x0, x, dt );
281
advect(0, x, x0, u, v, dt );
284
function vel_step(u, v, u0, v0, dt)
286
addFields(u, u0, dt );
287
addFields(v, v0, dt );
288
var temp = u0; u0 = u; u = temp;
289
var temp = v0; v0 = v; v = temp;
290
diffuse2(u,u0,v,v0, dt);
291
project(u, v, u0, v0);
292
var temp = u0; u0 = u; u = temp;
293
var temp = v0; v0 = v; v = temp;
294
advect(1, u, u0, u0, v0, dt);
295
advect(2, v, v0, u0, v0, dt);
296
project(u, v, u0, v0 );
298
var uiCallback = function(d,u,v) {};
300
function Field(dens, u, v) {
301
// Just exposing the fields here rather than using accessors is a measurable win during display (maybe 5%)
302
// but makes the code ugly.
303
this.setDensity = function(x, y, d) {
304
dens[(x + 1) + (y + 1) * rowSize] = d;
306
this.getDensity = function(x, y) {
307
return dens[(x + 1) + (y + 1) * rowSize];
309
this.setVelocity = function(x, y, xv, yv) {
310
u[(x + 1) + (y + 1) * rowSize] = xv;
311
v[(x + 1) + (y + 1) * rowSize] = yv;
313
this.getXVelocity = function(x, y) {
314
return u[(x + 1) + (y + 1) * rowSize];
316
this.getYVelocity = function(x, y) {
317
return v[(x + 1) + (y + 1) * rowSize];
319
this.width = function() { return width; }
320
this.height = function() { return height; }
322
function queryUI(d, u, v)
324
for (var i = 0; i < size; i++)
325
u[i] = v[i] = d[i] = 0.0;
326
uiCallback(new Field(d, u, v));
329
this.update = function () {
330
queryUI(dens_prev, u_prev, v_prev);
331
vel_step(u, v, u_prev, v_prev, dt);
332
dens_step(dens, dens_prev, u, v, dt);
333
displayFunc(new Field(dens, u, v));
335
this.setDisplayFunction = function(func) {
339
this.iterations = function() { return iterations; }
340
this.setIterations = function(iters) {
341
if (iters > 0 && iters <= 100)
344
this.setUICallback = function(callback) {
345
uiCallback = callback;
364
size = (width+2)*(height+2);
365
dens = new Array(size);
366
dens_prev = new Array(size);
368
u_prev = new Array(size);
370
v_prev = new Array(size);
371
for (var i = 0; i < size; i++)
372
dens_prev[i] = u_prev[i] = v_prev[i] = dens[i] = u[i] = v[i] = 0;
375
this.setResolution = function (hRes, wRes)
377
var res = wRes * hRes;
378
if (res > 0 && res < 1000000 && (wRes != width || hRes != height)) {
386
this.setResolution(64, 64);