2 * Copyright 2013 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.
26 * Update 10/21/2013: fixed loop variables at line 119
29 var NavierStokes = new BenchmarkSuite('NavierStokes', [1484000],
30 [new Benchmark('NavierStokes',
40 var nsFrameCounter = 0;
42 function runNavierStokes()
47 if(nsFrameCounter==15)
48 checkResult(solver.getDens());
51 function checkResult(dens) {
54 for (var i=7000;i<7100;i++) {
55 this.result+=~~((dens[i]*10));
58 if (this.result!=77) {
59 throw(new Error("checksum failed"));
63 function setupNavierStokes()
65 solver = new FluidField(null);
66 solver.setResolution(128, 128);
67 solver.setIterations(20);
68 solver.setDisplayFunction(function(){});
69 solver.setUICallback(prepareFrame);
73 function tearDownNavierStokes()
78 function addPoints(field) {
80 for (var i = 1; i <= n; i++) {
81 field.setVelocity(i, i, n, n);
82 field.setDensity(i, i, 5);
83 field.setVelocity(i, n - i, -n, -n);
84 field.setDensity(i, n - i, 20);
85 field.setVelocity(128 - i, n + i, -n, -n);
86 field.setDensity(128 - i, n + i, 30);
90 var framesTillAddingPoints = 0;
91 var framesBetweenAddingPoints = 5;
93 function prepareFrame(field)
95 if (framesTillAddingPoints == 0) {
97 framesTillAddingPoints = framesBetweenAddingPoints;
98 framesBetweenAddingPoints++;
100 framesTillAddingPoints--;
104 // Code from Oliver Hunt (http://nerget.com/fluidSim/pressure.js) starts here.
105 function FluidField(canvas) {
106 function addFields(x, s, dt)
108 for (var i=0; i<size ; i++ ) x[i] += dt*s[i];
111 function set_bnd(b, x)
114 for (var i = 1; i <= width; i++) {
115 x[i] = x[i + rowSize];
116 x[i + (height+1) *rowSize] = x[i + height * rowSize];
119 for (var j = 1; j <= height; j++) {
120 x[j * rowSize] = -x[1 + j * rowSize];
121 x[(width + 1) + j * rowSize] = -x[width + j * rowSize];
123 } else if (b === 2) {
124 for (var i = 1; i <= width; i++) {
125 x[i] = -x[i + rowSize];
126 x[i + (height + 1) * rowSize] = -x[i + height * rowSize];
129 for (var j = 1; j <= height; j++) {
130 x[j * rowSize] = x[1 + j * rowSize];
131 x[(width + 1) + j * rowSize] = x[width + j * rowSize];
134 for (var i = 1; i <= width; i++) {
135 x[i] = x[i + rowSize];
136 x[i + (height + 1) * rowSize] = x[i + height * rowSize];
139 for (var j = 1; j <= height; j++) {
140 x[j * rowSize] = x[1 + j * rowSize];
141 x[(width + 1) + j * rowSize] = x[width + j * rowSize];
144 var maxEdge = (height + 1) * rowSize;
145 x[0] = 0.5 * (x[1] + x[rowSize]);
146 x[maxEdge] = 0.5 * (x[1 + maxEdge] + x[height * rowSize]);
147 x[(width+1)] = 0.5 * (x[width] + x[(width + 1) + rowSize]);
148 x[(width+1)+maxEdge] = 0.5 * (x[width + maxEdge] + x[(width + 1) + height * rowSize]);
151 function lin_solve(b, x, x0, a, c)
153 if (a === 0 && c === 1) {
154 for (var j=1 ; j<=height; j++) {
155 var currentRow = j * rowSize;
157 for (var i = 0; i < width; i++) {
158 x[currentRow] = x0[currentRow];
165 for (var k=0 ; k<iterations; k++) {
166 for (var j=1 ; j<=height; j++) {
167 var lastRow = (j - 1) * rowSize;
168 var currentRow = j * rowSize;
169 var nextRow = (j + 1) * rowSize;
170 var lastX = x[currentRow];
172 for (var i=1; i<=width; i++)
173 lastX = x[currentRow] = (x0[currentRow] + a*(lastX+x[++currentRow]+x[++lastRow]+x[++nextRow])) * invC;
180 function diffuse(b, x, x0, dt)
183 lin_solve(b, x, x0, a, 1 + 4*a);
186 function lin_solve2(x, x0, y, y0, a, c)
188 if (a === 0 && c === 1) {
189 for (var j=1 ; j <= height; j++) {
190 var currentRow = j * rowSize;
192 for (var i = 0; i < width; i++) {
193 x[currentRow] = x0[currentRow];
194 y[currentRow] = y0[currentRow];
202 for (var k=0 ; k<iterations; k++) {
203 for (var j=1 ; j <= height; j++) {
204 var lastRow = (j - 1) * rowSize;
205 var currentRow = j * rowSize;
206 var nextRow = (j + 1) * rowSize;
207 var lastX = x[currentRow];
208 var lastY = y[currentRow];
210 for (var i = 1; i <= width; i++) {
211 lastX = x[currentRow] = (x0[currentRow] + a * (lastX + x[currentRow] + x[lastRow] + x[nextRow])) * invC;
212 lastY = y[currentRow] = (y0[currentRow] + a * (lastY + y[++currentRow] + y[++lastRow] + y[++nextRow])) * invC;
221 function diffuse2(x, x0, y, y0, dt)
224 lin_solve2(x, x0, y, y0, a, 1 + 4 * a);
227 function advect(b, d, d0, u, v, dt)
229 var Wdt0 = dt * width;
230 var Hdt0 = dt * height;
231 var Wp5 = width + 0.5;
232 var Hp5 = height + 0.5;
233 for (var j = 1; j<= height; j++) {
234 var pos = j * rowSize;
235 for (var i = 1; i <= width; i++) {
236 var x = i - Wdt0 * u[++pos];
237 var y = j - Hdt0 * v[pos];
254 var row1 = j0 * rowSize;
255 var row2 = j1 * rowSize;
256 d[pos] = s0 * (t0 * d0[i0 + row1] + t1 * d0[i0 + row2]) + s1 * (t0 * d0[i1 + row1] + t1 * d0[i1 + row2]);
262 function project(u, v, p, div)
264 var h = -0.5 / Math.sqrt(width * height);
265 for (var j = 1 ; j <= height; j++ ) {
266 var row = j * rowSize;
267 var previousRow = (j - 1) * rowSize;
268 var prevValue = row - 1;
269 var currentRow = row;
270 var nextValue = row + 1;
271 var nextRow = (j + 1) * rowSize;
272 for (var i = 1; i <= width; i++ ) {
273 div[++currentRow] = h * (u[++nextValue] - u[++prevValue] + v[++nextRow] - v[++previousRow]);
280 lin_solve(0, p, div, 1, 4 );
281 var wScale = 0.5 * width;
282 var hScale = 0.5 * height;
283 for (var j = 1; j<= height; j++ ) {
284 var prevPos = j * rowSize - 1;
285 var currentPos = j * rowSize;
286 var nextPos = j * rowSize + 1;
287 var prevRow = (j - 1) * rowSize;
288 var currentRow = j * rowSize;
289 var nextRow = (j + 1) * rowSize;
291 for (var i = 1; i<= width; i++) {
292 u[++currentPos] -= wScale * (p[++nextPos] - p[++prevPos]);
293 v[currentPos] -= hScale * (p[++nextRow] - p[++prevRow]);
300 function dens_step(x, x0, u, v, dt)
302 addFields(x, x0, dt);
303 diffuse(0, x0, x, dt );
304 advect(0, x, x0, u, v, dt );
307 function vel_step(u, v, u0, v0, dt)
309 addFields(u, u0, dt );
310 addFields(v, v0, dt );
311 var temp = u0; u0 = u; u = temp;
312 var temp = v0; v0 = v; v = temp;
313 diffuse2(u,u0,v,v0, dt);
314 project(u, v, u0, v0);
315 var temp = u0; u0 = u; u = temp;
316 var temp = v0; v0 = v; v = temp;
317 advect(1, u, u0, u0, v0, dt);
318 advect(2, v, v0, u0, v0, dt);
319 project(u, v, u0, v0 );
321 var uiCallback = function(d,u,v) {};
323 function Field(dens, u, v) {
324 // Just exposing the fields here rather than using accessors is a measurable win during display (maybe 5%)
325 // but makes the code ugly.
326 this.setDensity = function(x, y, d) {
327 dens[(x + 1) + (y + 1) * rowSize] = d;
329 this.getDensity = function(x, y) {
330 return dens[(x + 1) + (y + 1) * rowSize];
332 this.setVelocity = function(x, y, xv, yv) {
333 u[(x + 1) + (y + 1) * rowSize] = xv;
334 v[(x + 1) + (y + 1) * rowSize] = yv;
336 this.getXVelocity = function(x, y) {
337 return u[(x + 1) + (y + 1) * rowSize];
339 this.getYVelocity = function(x, y) {
340 return v[(x + 1) + (y + 1) * rowSize];
342 this.width = function() { return width; }
343 this.height = function() { return height; }
345 function queryUI(d, u, v)
347 for (var i = 0; i < size; i++)
348 u[i] = v[i] = d[i] = 0.0;
349 uiCallback(new Field(d, u, v));
352 this.update = function () {
353 queryUI(dens_prev, u_prev, v_prev);
354 vel_step(u, v, u_prev, v_prev, dt);
355 dens_step(dens, dens_prev, u, v, dt);
356 displayFunc(new Field(dens, u, v));
358 this.setDisplayFunction = function(func) {
362 this.iterations = function() { return iterations; }
363 this.setIterations = function(iters) {
364 if (iters > 0 && iters <= 100)
367 this.setUICallback = function(callback) {
368 uiCallback = callback;
387 size = (width+2)*(height+2);
388 dens = new Array(size);
389 dens_prev = new Array(size);
391 u_prev = new Array(size);
393 v_prev = new Array(size);
394 for (var i = 0; i < size; i++)
395 dens_prev[i] = u_prev[i] = v_prev[i] = dens[i] = u[i] = v[i] = 0;
398 this.getDens = function()
402 this.setResolution = function (hRes, wRes)
404 var res = wRes * hRes;
405 if (res > 0 && res < 1000000 && (wRes != width || hRes != height)) {
413 this.setResolution(64, 64);