Navier Stoke Fluid Simulation

What is a navier stoke fluid simulation How do you make a navier stoke fluid simulation? This script and codes were developed by Endre Simo on 01 February 2022, Tuesday.

How do I make an navier stoke fluid simulation?
  1. Navier Stoke Fluid Simulation Previews
  2. Navier Stoke Fluid Simulation HTML Codes
  3. Navier Stoke Fluid Simulation CSS Codes
  4. Navier Stoke Fluid Simulation JS Codes
Navier Stoke Fluid Simulation Previews

Navier Stoke Fluid Simulation HTML Codes

<!DOCTYPE html>
<html >
<head>
  <meta charset="UTF-8">
  <title>Navier Stoke Fluid Simulation</title>
  
  
  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>
  <div class="wrapper">   
    <div class="context"></div>	
</div>    
<div id="gui"></div>
<script>

    function getIEVersion() {
        var rv = -1; // Return value assumes failure.
        if (navigator.appName == 'Microsoft Internet Explorer') {
            var ua = navigator.userAgent;
            var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
            if (re.test(ua) != null)
                rv = parseFloat( RegExp.$1 );
        }
        return rv;
    }


    function checkVersion() {
        var ver = getIEVersion();

        if ( ver != -1 ) {
            if (ver <= 9.0) {
                document.body.innerHTML = "";
                document.body.innerHTML =
                        "<p class='no-canvas'>" +
                                "You need a <a href='https://www.google.com/chrome'>modern browser</a> to view this experiment." +
                        "</p>";
            }
        }
    }

    checkVersion();

</script>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.5/dat.gui.min.js'></script>

    <script src="js/index.js"></script>

</body>
</html>

Navier Stoke Fluid Simulation CSS Codes

@import url(https://fonts.googleapis.com/css?family=Roboto+Condensed:300);
@import url(https://fonts.googleapis.com/css?family=Lemon);

html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    background: #111;
    margin: 0;
    padding: 0;
    -webkit-user-select: none;
    -moz-user-select: none;
    -khtml-user-select: none;
    user-select: none;
}
/*******************************
* Main display
********************************/

div#main {
    position:relative!important; display: block!important; width: 900px!important; height: 600px!important;
    width: 100%;
    margin: 0 auto;
    border: 1px solid #222;
    -webkit-border-radius: 2px;
    -moz-border-radius: 2px;
    -ms-border-radius: 2px;
    -o-border-radius: 2px;
    border-radius: 2px;

    -webkit-box-shadow: 0px 2px 17px rgba(0,0,0,.80), inset 0 0 15px 15px rgba(5,5,5,.2);
       -moz-box-shadow: 0px 2px 17px rgba(0,0,0,.80), inset 0 0 15px 15px rgba(5,5,5,.2);
            box-shadow: 0px 2px 17px rgba(0,0,0,.80), inset 0 0 15px 15px rgba(5,5,5,.2);
}

div.context {
    position:realtive;
    width: 820px; height: 520px;
    margin: 0 auto;
    border: 1px solid #222;
    -webkit-border-radius: 2px;
    -moz-border-radius: 2px;
    -ms-border-radius: 2px;
    -o-border-radius: 2px;
    border-radius: 2px;

    -webkit-box-shadow: 0px 2px 17px rgba(0,0,0,.80), inset 0 0 15px 15px rgba(5,5,5,.2);
       -moz-box-shadow: 0px 2px 17px rgba(0,0,0,.80), inset 0 0 15px 15px rgba(5,5,5,.2);
            box-shadow: 0px 2px 17px rgba(0,0,0,.80), inset 0 0 15px 15px rgba(5,5,5,.2);
}

div.wrapper {
    position: absolute;
    width: 100%; margin: 0; padding: 0;
    top: 60px;
}

.context #canvas {
  position: relative;
  width: 800px; height: 500px;
  left: 10px;
  top: 10px;
}

#gui {
  position: absolute;
  right: 100px;
  top :0;
}

.no-canvas {
	color: #999999;
	font-size: 24px;
	text-align: center;
	margin-top: 150px;
}

Navier Stoke Fluid Simulation JS Codes

/**
 * created by Simo Endre
 * @simo_endre
 */

var FS = FS || { REVISION : '01'};

FS.Solver = (function() {
    // Private variables
    var _NX, _NY, _NX2, _NY2,
        _invNumCells, _dt, _isRGB,
        _solverIterations, _colorDiffusion, _doVorticityConfinement;

    var wrap_x = false;
    var wrap_y = false;

    var _visc, _fadeSpeed, _avgDensity, _uniformity, _avgSpeed;

    var temp = 0;

    // Constants
    var FLUID_DEFAULT_NX = 50,
        FLUID_DEFAULT_NY = 50,
        FLUID_DEFAULT_DT = 0.1,
        FLUID_DEFAULT_VISC = 0.00012,
        FLUID_DEFAULT_COLOR_DIFUSION = 0.1,
        FLUID_DEFAULT_FADESPEED = 0.0014,
        FLUID_DEFAULT_SOLVER_ITERATION = 4,
        FLUID_DEFAULT_VORTICITY_CONFINEMENT = false;
    var self;

    function FSolver() {

        // Public variales
        this.width = 0;
        this.height = 0;
        this.numCells = 0;        

        self = this;

        this.__defineGetter__('wrapX', function() { return wrap_x; });
        this.__defineGetter__('wrapY', function() { return wrap_y; });
		this.__defineGetter__('fadeSpeed', function() { return _fadeSpeed; });
		this.__defineGetter__('solverIterations', function() { return _solverIterations; });
        this.__defineSetter__('rgb', function(value) { _isRGB = value; });
        this.__defineSetter__('fadeSpeed', function(value) { _fadeSpeed = value; });
		this.__defineSetter__('solverIterations', function(value) { _solverIterations = value; });
        this.__defineSetter__('viscosity', function(value) { _visc = value; });
        this.__defineSetter__('deltaT', function(value) { _dt = value; });
        this.__defineSetter__('vorticityConfinement', function(value) { _doVorticityConfinement = value; });
    }

    FSolver.prototype.mainSolver = function(NX, NY) {
        setup(NX, NY);

        this.reset();
    };    

    FSolver.prototype.reset = function() {
        if (typeof Float32Array == null || typeof Float32Array == "undefined")
            Float32Array = Array;        
        this.r = new Float32Array(this.numCells);
        this.g = new Float32Array(this.numCells);
        this.b = new Float32Array(this.numCells);
        this.u = new Float32Array(this.numCells);
        this.v = new Float32Array(this.numCells);                      

        this.rOld = new Float32Array(this.numCells);
        this.gOld = new Float32Array(this.numCells);
        this.bOld = new Float32Array(this.numCells);
        this.uOld = new Float32Array(this.numCells);
        this.vOld = new Float32Array(this.numCells);        

        this.curl_abs = new Float32Array(this.numCells);
        this.curl_orig = new Float32Array(this.numCells);
        this.density = new Float32Array(this.numCells);   
        this.densityOld = new Float32Array(this.numCells);

        this.source = new Float32Array(this.numCells);
        temp = new Float32Array(this.numCells);

        var i = this.numCells;

        while(i-- > -1) {
            this.r[i] = this.rOld[i] = this.g[i] = this.gOld[i] = this.b[i] = this.bOld[i] = 0;
            this.u[i] = this.uOld[i] = this.v[i] = this.vOld[i] = 0;
            this.curl_abs[i] = this.curl_orig[i] = 0;
            this.density[i] = this.densityOld[i] = 0;
            this.source[i] = 0;
        }
    };


    FSolver.prototype.updateDensity = function() {        
        addDensitySource(this.density, this.densityOld);

        if (_doVorticityConfinement) {            
            calcVorticityConfinement(this.uOld, this.vOld);            
        }
        
        diffuse(0, this.densityOld, this.density, 0);
        advect(0, this.density, this.densityOld, this.u, this.v);
    }
	
    FSolver.prototype.updateVelocity = function() {
        this.addCellVelocity();
        this.SWAP('uOld', 'u');
        diffuse(1, this.u, this.uOld, _visc);
        this.SWAP('vOld', 'v');        
        diffuse(2, this.v, this.vOld, _visc);        
        project(this.u, this.v, this.uOld, this.vOld);


        this.SWAP('uOld', 'u');
        this.SWAP('vOld', 'v');
        advect(1, this.u, this.uOld, this.uOld, this.vOld);
        advect(2, this.v, this.vOld, this.uOld, this.vOld);
        project(this.u, this.v, this.uOld, this.vOld);
    }

    FSolver.prototype.getIndexForCellPosition = function(i, j) {
        i = (i < 1) ? 1 : ((i > _NX) ? _NX : i);
        j = (j < 1) ? 1 : ((j > _NY) ? _NY : i);        
        return FLUID_IX(i, j);
    };

    FSolver.prototype.getIndexForNormalizedPosition = function(x, y) {
        return this.getIndexForCellPosition(parseInt(x * _NX2), parseInt(y * _NY2));
    };

    FSolver.prototype.setWrap = function(x, y) {
        x = x || false;
        y = y || false;
        wrap_x = x;
        wrap_y = y;
    };

    FSolver.prototype.SWAP = function(x0, x) {
        var tmp = this[x0];
        this[x0] = this[x];
        this[x] = tmp;
    }

    FSolver.prototype.addCellVelocity = function() {
        var size = self.numCells;
        while(size-- > -1) {
            if (isNaN(self.u[size])) continue;
            self.u[size] += _dt * self.uOld[size];
            if (isNaN(self.v[size])) continue;
            self.v[size] += _dt * self.vOld[size];
        }
    }
	
	FSolver.prototype.getDensity = function(x, y) {
		return this.density[(x + 1) + (y + 1) * _NY];
	};


    // Private functions

    function setup(NX, NY) {
        _dt = FLUID_DEFAULT_DT;
        _visc = FLUID_DEFAULT_VISC;
        _fadeSpeed = FLUID_DEFAULT_FADESPEED;
        _solverIterations = this.solverIterations;
        _colorDiffusion = FLUID_DEFAULT_COLOR_DIFUSION;
        _doVorticityConfinement = FLUID_DEFAULT_VORTICITY_CONFINEMENT;

        _NX = NX;
        _NY = NY;
        _NX2 = _NX + 2; // cells + extra boundary cells
        _NY2 = _NY + 2;

        self.numCells = _NX2 * _NY2;
        _invNumCells = 1.0 / self.numCells;

        self.width = _NX2;
        self.height = _NY2;

        _isRGB = false;
    }

    function addDensitySource(x, x0) {                
        var size = self.numCells;        
        while(size-- > -1) {
            x[size] += _dt * x0[size];
        }
    }    

    function addSourceRGB() {
        var size = self.numCells;
        while(size-- > -1) {
            if (isNaN(self.r[size])) continue;
            self.r[size] += _dt * self.rOld[size];
            if (isNaN(self.g[size])) continue;
            self.g[size] += _dt * self.gOld[size];
            if (isNaN(self.b[size])) continue;
            self.b[size] += _dt * self.bOld[size];            
        }
    }

    function diffuse(bound, c, c0, _diff) {
        var a = _dt * _diff * _NX * _NY;
        linearSolver(bound, c, c0, a, 1.0 + 4 * a);
    }

    function diffuseUV(bound, _diff) {
        var a = _dt * _diff * _NX * _NY;
        linearSolverUV(bound, a, 1.0 + 4 * a);
    }

    function diffuseRGB(_diff) {
        var a = _dt * _diff * _NX * _NY;
        linearSolverRGB( a, 1.0 + 4 * a);
    }

    function calcVorticityConfinement(x, y) {
        var i, j, index, dx, dy, av, length;

        for (j = _NY; j > 0; --j) {
            index = FLUID_IX(_NX, j);
            for (i = _NX; i > 0; --i) {
                dx = self.u[parseInt(index + _NX2)] - self.u[parseInt(index - _NX2)];
                dy = self.v[parseInt(index + 1)] - self.v[parseInt(index - 1)];

                av = (dy - dx) * 0.5;
                self.curl_orig[parseInt(index)] = av;
                self.curl_abs[parseInt(index)] = (av < 0) ? -av : av;

                --index;
            }
        }

        for (j = _NY-1; j > 1; --j) {
            index = FLUID_IX(_NX-1, j);
            for (i = _NX-1; i > 1; --i) {
                dx = self.curl_abs[parseInt(index + 1)] - self.curl_abs[parseInt(index - 1)];
                dy = self.curl_abs[parseInt(index + _NX2)] - self.curl_abs[parseInt(index - _NX2)];

                length = Math.sqrt(dy * dy + dx * dx) + 0.0001;
                length = 2 / length;                
                dx *= length;
                dy *= length;

                av = self.curl_orig[parseInt(index)];                
                y[parseInt(index)] = dx * av;
                x[parseInt(index)] = dy * -av;

                --index;                
            }
        }
    }

    function fadeR() {        
        var holdAmount = 1 - _fadeSpeed;
        var totalDeviations = 0;
        var currentDeviation = 0;
        var i;

        _avgDensity = 0;
        _avgSpeed = 0;

        for (i = 0; i < self.numCells; i++) {
            self.uOld[i] = 0; self.vOld[i] = 0;
            self.rOld[i] = 0;

            _avgSpeed = self.u[i] * self.u[i] + self.v[i] * self.v[i];
            var density = Math.min(1.0, self.r[i]);
            _avgDensity += density;

            // calc deviation for uniformity
            currentDeviation = density - _avgDensity;
            totalDeviations += currentDeviation * currentDeviation;
            // fade out old
            self.r[i] = density * holdAmount;
        }
        _avgDensity *= _invNumCells;

        _uniformity = 1.0 / (1 + totalDeviations * _invNumCells); // 0: very wide distribution, 1: very uniform
    }

    function fadeRGB() {
        var holdAmount = 1 - _fadeSpeed;
        var totalDeviations = 0;
        var currentDeviation = 0;

        _avgDensity = 0;
        _avgSpeed = 0;

        for (var i = 0; i < self.numCells; i++) {
            self.uOld[i] = 0; self.vOld[i] = 0;
            self.rOld[i] = 0; self.gOld[i] = 0; self.bOld[i] = 0;

            _avgSpeed = self.u[i] * self.u[i] + self.v[i] * self.v[i];
            var dR = Math.min(1.0, self.r[i]);
            var dG = Math.min(1.0, self.g[i]);
            var dB = Math.min(1.0, self.b[i]);
            var density = Math.max(dR, Math.max(dG, dB));
            _avgDensity += density;
            currentDeviation = density - _avgDensity;

            // calc deviation for uniformity
            totalDeviations += currentDeviation * currentDeviation;
            // fade out old
            self.r[i] = dR * holdAmount;
            self.g[i] = dG * holdAmount;
            self.b[i] = dB * holdAmount;
        }

        _avgDensity *= _invNumCells;
        _avgSpeed *= _invNumCells;
        _uniformity = 1.0 / (1+ totalDeviations * _invNumCells); // 0: very wide distribution, 1: very uniform
    }

    function advect(bound, _d, d0, du, dv) {
        var i, j, i0, j0, i1, j1;
        var x, y, s0, t0, s1, t1, dt0, dt1;

        dt0 = _dt * _NX;
        dt1 = _dt * _NY;

        for (j = _NY; j > 0; --j) {
            for (i = _NX; i > 0; --i) {
                x = i - dt0 * du[FLUID_IX(i, j)];
                y = j - dt1 * dv[FLUID_IX(i, j)];

                if (x > _NX + 0.5) x = _NX + 0.5;
                if (x < 0.5) x = 0.5;

                i0 = parseInt(~~x);
                i1 = i0 + 1;

                if (y > _NY + 0.5) y = _NY + 0.5;
                if (y < 0.5) y = 0.5;

                j0 = parseInt(~~y);
                j1 = j0 + 1;

                s1 = x - i0;
                s0 = 1 - s1;
                t1 = y - j0;
                t0 = 1 - t1;

                _d[FLUID_IX(i, j)] = s0 * (t0 * d0[FLUID_IX(i0, j0)] + t1 * d0[FLUID_IX(i0, j1)]) +
                    s1 * (t0 * d0[FLUID_IX(i1, j0)] + t1 * d0[FLUID_IX(i1, j1)]);
            }
        }

        setBoundary(bound, _d);
    }

    function advectRGB(du, dv) {
        var i, j, i0, j0, i1, j1;
        var x, y, s0, t0, s1, t1, dt0x, dt0y;
        var index;

        dt0x = _dt * _NX;
        dt0y = _dt * _NY;

        for (j = _NY; j > 0; --j) {
            for (i = _NX; i > 0; --i) {               
                index = FLUID_IX[i, j];
                x = i - dt0x * du[FLUID_IX(i, j)];
                y = j - dt0y * dv[FLUID_IX(i, j)];

                if (x > _NX + 0.5) x = _NX + 0.5;
                if (x < 0.5) x = 0.5;

                i0 = parseInt(x);
                i1 = i0 + 1;

                if (y > _NY + 0.5) y = _NY + 0.5;
                if (y < 0.5) y = 0.5;

                j0 = parseInt(y);
                j1 = j0 + 1;

                s1 = x - i0;
                s0 = 1 - s1;
                t1 = y - j0;
                t0 = 1 - t1;
                j0 = i0 + _NX2;
                
                io = FLUID_IX(i0, j0);
                
                self.r[index] = s0 * (t0 * self.rOld[i0] + t1 * self.rOld[j0]) + s1 * (t0 * self.rOld[i1] + t1 * self.rOld[j0+1]);
                self.g[index] = s0 * (t0 * self.gOld[i0] + t1 * self.gOld[j0]) + s1 * (t0 * self.gOld[i1] + t1 * self.gOld[j0+1]);
                self.b[index] = s0 * (t0 * self.bOld[i0] + t1 * self.bOld[j0]) + s1 * (t0 * self.bOld[i1] + t1 * self.bOld[j0+1]);
            }
        }

        setBoundaryRGB();
    }

    function project(u, v, p, div) {
        var i, j, index;
        var h = - 0.5 / _NX;
        for (j = _NY; j > 0; --j) {
            index = FLUID_IX(_NX, j);
            for (i = _NX; i > 0; --i) {
                div[index] = h * (u[parseInt(index+1)] - u[parseInt(index-1)] + v[parseInt(index + _NX2)] - v[parseInt(index - _NX2)] );
                p[index] = 0;
                --index;
            }
        }

        setBoundary(0, div);
        setBoundary(0, p);

        linearSolver(0, p, div, 1, 4);

        for (j = _NY; j > 0; --j) {
            index = FLUID_IX(_NX, j);
            for (i = _NX; i > 0; --i) {
                u[index] -= 0.5 * _NX * (p[parseInt(index+1)] - p[parseInt(index-1)]);
                v[index] -= 0.5 * _NY * (p[parseInt(index + _NX2)] - p[parseInt(index - _NX2)]);
                --index;
            }
        }
        setBoundary(1, u);
        setBoundary(2, v);
    }

    function linearSolver(bound, x, x0, a, c) {
        var i, j, k, index;

        if (a == 1 && c == 4) {            
            for (k = 0; k < _solverIterations; k++) {
                for (j = _NY; j > 0; --j) {
                    index = FLUID_IX(_NX, j);
                    for (i = _NX; i > 0; --i) {
                        x[index] = ( (x[parseInt(index-1)] + x[parseInt(index+1)] +
                            x[parseInt(index - _NX2)] + x[parseInt(index + _NX2)]) + x0[parseInt(index)]) * .25;
                        --index;
                    }
                }

                setBoundary(bound, x);
            }
        } else {
            c = 1 / c;
            for (k = 0; k < _solverIterations; k++) {
                for (j = _NY; j > 0; --j) {
                    index = FLUID_IX(_NX, j);
                    for (i = _NX; i > 0; --i) {
                        x[index] = (a * (x[parseInt(index-1)] + x[parseInt(index+1)] +
                            x[parseInt(index - _NX2)] + x[parseInt(index + _NX2)]) + x0[parseInt(index)]) * c;
                        --index;
                    }
                }

                setBoundary(bound, x);
            }
        }
    }

    function linearSolverRGB(a, c) {
        var i, j, k, index, index2, index3;
        c = 1 / c;

        for (k = 0; k < _solverIterations; ++k) {
            for (j = _NY; j > 0; --j) {
                index = FLUID_IX(_NX, j);
                index2 = index - _NX2;
                index3 = index + _NX2;
                
                for (i = _NX; i > 0; --i) {
                    self.r[index] = ( (self.r[parseInt(index-1)] + self.r[parseInt(index+1)] +
                        self.r[parseInt(index2)] + self.r[parseInt(index3)]) * a + self.rOld[index] ) * c;

                    self.g[index] = ( (self.g[parseInt(index-1)] + self.g[parseInt(index+1)] +
                        self.g[parseInt(index2)] + self.g[parseInt(index3)]) * a + self.gOld[index] ) * c;

                    self.b[index] = ( (self.b[parseInt(index-1)] + self.b[parseInt(index+1)] +
                        self.b[parseInt(index2)] + self.b[parseInt(index3)]) * a + self.bOld[index] ) * c;

                    --index;
                    --index2;
                    --index3;
                }
            }

            setBoundaryRGB();
        }
    }

    function linearSolverUV(a, c) {
        var i, j, k, index;
        c = 1 / c;

        for (k = 0; k < _solverIterations; ++k) {
            for (j = _NY; j > 0; --j) {
                index = FLUID_IX(_NX, j);                
                for (i = _NX; i > 0; --i) {
                    self.u[index] = ( (self.u[parseInt(index-1)] + self.u[parseInt(index+1)] + 
                              self.u[parseInt(index - _NX2)] + self.u[parseInt(index + _NX2)]) * a + self.uOld[index] ) * c;
                    self.v[index] = ( (self.v[parseInt(index-1)] + self.v[parseInt(index+1)] + 
                              self.v[parseInt(index - _NX2)] + self.v[parseInt(index + _NX2)]) * a + self.vOld[index] ) * c;
                    --index;
                }
            }

            setBoundary(1, self.u);
            setBoundary(2, self.v);
        }
    }

    function setBoundary(bound, x) {
        var dst1, dst2, src1, src2, i;
        var step = FLUID_IX(0, 1) - FLUID_IX(0, 0);
        dst1 = FLUID_IX(0, 1);
        src1 = FLUID_IX(1, 1);
        dst2 = FLUID_IX(_NX+1, 1);
        src2 = FLUID_IX(_NX, 1);

        if (wrap_x) {
            src1 ^= src2;
            src2 ^= src1;
            src1 ^= src2;
        }

        if (bound == 1 && !wrap_x) {
            for (i = _NY; i > 0; --i) {
                x[dst1] = -x[src1]; dst1 += step; src1 += step;
                x[dst2] = -x[src2]; dst2 += step; src2 += step;
            }
        } else {
            for (i = _NY; i > 0; --i) {
                x[dst1] = x[src1]; dst1 += step; src1 += step;
                x[dst2] = x[src2]; dst2 += step; src2 += step;
            }
        }

        dst1 = FLUID_IX(1, 0);
        src1 = FLUID_IX(1, 1);
        dst2 = FLUID_IX(1, _NY+1);
        src2 = FLUID_IX(1, _NY);

        if (wrap_y) {
            src1 ^= src2;
            src2 ^= src1;
            src1 ^= src2;
        }

        if (bound == 2 && !wrap_y) {
            for (i = _NX; i > 0 ; --i) {
                x[dst1++] = -x[src1++];
                x[dst2++] = -x[src2++];
            }
        } else {
            for (i = _NX; i > 0; --i) {
                x[dst1++] = x[src1++];
                x[dst2++] = x[src2++];
            }
        }

        x[FLUID_IX(0, 0)] = .5 * (x[FLUID_IX(1, 0)] + x[FLUID_IX(0, 1)]);
        x[FLUID_IX(0, _NY+1)] = .5 * (x[FLUID_IX(1, _NY+1)] + x[FLUID_IX(0, _NY)]);
        x[FLUID_IX(_NX+1, 0)] = .5 * (x[FLUID_IX(_NX, 0)] + x[FLUID_IX(_NX+1, _NX)]);
        x[FLUID_IX(_NX+1, _NY+1)] = .5 * (x[FLUID_IX(_NX, _NY+1)] + x[FLUID_IX(_NX+1, _NY)]);
    }

    function setBoundaryRGB() {
       
        if (!wrap_x && !wrap_y) return;
       
        var i, src1, src2, dst1, dst2;
        var step = FLUID_IX(0, 1) - FLUID_IX(0, 0);

        if (wrap_x) {
            dst1 = FLUID_IX(0, 1);
            src1 = FLUID_IX(1, 1);
            dst2 = FLUID_IX(_NX+1, 1);
            src2 = FLUID_IX(_NX, 1);
            
            src1 ^= src2;
            src2 ^= src1;
            src1 ^= src2;        
         
            for (i = _NY; i > 0 ; --i) {
                self.r[dst1] = self.r[src1]; self.g[dst1] = self.g[src1]; self.b[dst1] = self.b[src1]; 
                dst1 += step; src1 += step;

                self.r[dst2] = self.r[src2]; self.g[dst2] = self.g[src2]; self.b[dst2] = self.b[src2]; 
                dst2 += step; src2 += step;            
            }
        }

        if (wrap_y) {
            dst1 = FLUID_IX(1, 0);
            src1 = FLUID_IX(1, 1);
            dst2 = FLUID_IX(1, _NY+1);
            src2 = FLUID_IX(1, _NY);
            
            src1 ^= src2;
            src2 ^= src1;
            src1 ^= src2;        
         
            for (i = _NX; i > 0 ; --i) {
                self.r[dst1] = self.r[src1]; self.g[dst1] = self.g[src1]; self.b[dst1] = self.b[src1]; 
                ++dst1; ++src1;

                self.r[dst2] = self.r[src2]; self.g[dst2] = self.g[src2]; self.b[dst2] = self.b[src2]; 
                ++dst2; ++src2;
            }
        }
    }        

    function swapRGB() {
        temp = self.r; self.r = self.rOld; self.rOld = temp;
        temp = self.g; self.g = self.gOld; self.gOld = temp;
        temp = self.b; self.b = self.bOld; self.bOld = temp;
    }

    function FLUID_IX(i, j) {        
        return (parseInt(i + _NX2 * j)); //get the index of a two dimensional array
    }

    return FSolver;
})();

(function() {
    var lastTime = 0;
    var vendors = ['ms', 'moz', 'webkit', 'o'];
    for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
        window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
        window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
            || window[vendors[x]+'CancelRequestAnimationFrame'];
    }

    if (!window.requestAnimationFrame)
        window.requestAnimationFrame = function(callback, element) {
            var currTime = new Date().getTime();
            var timeToCall = Math.max(0, 16 - (currTime - lastTime));
            var id = window.setTimeout(function() { callback(currTime + timeToCall); },
                timeToCall);
            lastTime = currTime + timeToCall;
            return id;
        };

    if (!window.cancelAnimationFrame)
        window.cancelAnimationFrame = function(id) {
            clearTimeout(id);
        };
}());

// Private Constants
var FLUID_WIDTH = 128;
var NUM_PARTICLES = 4;
var VMAX = 0.013;
var VMAX2 = VMAX * VMAX;
var ADD_DENSITY = 60000;
var VEL_MUL = 40;

// Private variables

var mainCanvas = document.createElement('canvas');
mainCanvas.setAttribute('width', '900');
mainCanvas.setAttribute('height', '500');
mainCanvas.setAttribute('id', 'canvas');
var mainContext = document.getElementsByClassName('context')[0];
mainContext.appendChild(mainCanvas);  

var canvasWidth = mainCanvas.width;
var canvasHeight = mainCanvas.height;
var sx = mainCanvas.width / mainCanvas.offsetWidth;
var sy = mainCanvas.height / mainCanvas.offsetHeight;
var cx = 0, cy = 0;

mainCanvas.width = FLUID_WIDTH;
mainCanvas.height = FLUID_WIDTH;
var ctx = mainCanvas.getContext('2d');

var sw = mainCanvas.width;
var sh = mainCanvas.height;
var isw = 1 / sw;
var ish = 1 / sh;
var mx = 4.0;
var my = 0;
var aspectRatio = sw * ish;
var dx, dy;
var lastX, lastY;

// Initialize GUI values
var GUI = new function() {
	    this.viscosity = 0.00006;  
    	this.fadeSpeed = 0.0012; 
	    this.densityMul = 0.98;
	    this.density = ADD_DENSITY;
    	this.velMul = VEL_MUL;  
	    this.fluidWidth = FLUID_WIDTH;
    	this.numParticles = NUM_PARTICLES; 
    	this.solverIterations = 4;
    this.doVorticity = false;
    
  return this;
}  

var partList = new Array();
var fSolver = new FS.Solver();
    fSolver.mainSolver(GUI.fluidWidth, parseInt(GUI.fluidWidth * sh / sw));

fSolver.viscosity = GUI.viscosity;
	fSolver.fadeSpeed = GUI.fadeSpeed;
	fSolver.solverIterations = GUI.solverIterations;
fSolver.vorticityConfinement = GUI.doVorticity;

var fsWidth = fSolver.width;
var fsHeight = fSolver.height;
var image = ctx.createImageData(fsWidth, fsHeight);
var data32 = new Uint32Array(image.data.buffer);

var frameCount = 0;
var drawMode = 0,
    drawParticles = true,
    drawFluid = false,
    drawLines = false,
    mouseDown = false,
    addVel    = true,
    add = true;

var counter = 0;

var gui = new dat.GUI({autoPlace:false, width: 270, align: "left"});
gui.domElement.style.position = 'relative';
gui.domElement.style.top = '60px';
gui.domElement.style.left = '80px';

var datGUI = document.getElementById('gui');
datGUI.appendChild(gui.domElement);
var gs = gui.addFolder('General settings');
gs.open();
gs.add(GUI,'viscosity').min(0.00001).max(0.001).step(0.00001).name('Viscosity').onChange(function(value) { fSolver.viscosity = value });
gs.add(GUI,'fadeSpeed').min(0.0006).max(0.009).step(0.0001).name('Fade speed').onChange(function(value) { fSolver.fadeSpeed = value });
gs.add(GUI,'density').min(10000).max(90000).step(100).name('Density').onChange(function() { fSolver.updateDensity() });
gs.add(GUI,'velMul').min(10).max(100).step(1).name('Velocity').onChange(function() { fSolver.addCellVelocity() });
gs.add(GUI,'densityMul').min(0.7).max(1.1).step(0.1).name('Density Mult.').onChange(function(value) { GUI.densityMul = value });
gs.add(GUI,'solverIterations').min(3).max(10).step(1).name('Solver Iteration').onChange(function(value) { GUI.solverIterations = fSolver.solverIterations = value;} );
gs.add(GUI,'doVorticity').onFinishChange(function(e) { 
    if (GUI.doVorticity) { 
        fSolver.vorticityConfinement = true;        
    } else {
        fSolver.vorticityConfinement = false;
    }    
});

gui.add(fSolver, 'reset').name('Clear');

render();

function draw() {
    var i=0, j;
	    ctx.fillStyle = '#000';
    	ctx.fillRect(0, 0, mainCanvas.width, mainCanvas.height);
    	ctx.fill();
	
    for (; i < fSolver.numCells; i++) {
        j = fSolver.density[i];                
        if (j > 255) j = 255;
		
        data32[i] =
            (255 << 24) | // alpha, 255 = 100% opacity
            (j << 16) | // blue
            (j << 8) | // green
            j; // red

        image.data[i * 4 + 0] = j;
        image.data[i * 4 + 1] = j;
        image.data[i * 4 + 2] = j;
        image.data[i * 4 + 3] = 255;
    }

    ctx.putImageData(image, 0, 0);
}

function getScrollX() {
    return window.pageXOffset || window.document.documentElement.scrollLeft;
}

function getScrollY() {
    return window.pageYOffset || window.document.documentElement.scrollTop;
}

function getMousePos(event) {    
    event.preventDefault();
    dx = event.pageX - (getScrollX() + mainCanvas.getBoundingClientRect().left)- lastX;
    dy = event.pageY - (getScrollY() + mainCanvas.getBoundingClientRect().top) - lastY;    
    lastX = event.pageX - (getScrollX() + mainCanvas.getBoundingClientRect().left) - cx;
    lastY = event.pageY - (getScrollY() + mainCanvas.getBoundingClientRect().top) - cy;    
    return {
        x: lastX,
        y: lastY
    }    
}

function getTouchPos(event) {    
    event.preventDefault();
    dx = event.changedTouches[0].pageX - (getScrollX() + mainCanvas.getBoundingClientRect().left) - lastX;
    dy = event.changedTouches[0].pageY - (getScrollY() + mainCanvas.getBoundingClientRect().top) - lastY;    
    lastX = event.changedTouches[0].pageX - (getScrollX() + mainCanvas.getBoundingClientRect().left);
    lastY = event.changedTouches[0].pageY - (getScrollY() + mainCanvas.getBoundingClientRect().top);        

    lastX = lastX > 0 ? (lastX < canvasWidth ? lastX : canvasWidth) : 0;
    lastY = lastY > 0 ? (lastY < canvasHeight ? lastY : canvasHeight) : 0;

    return {
        x: lastX,
        y: lastY
    }    
}

function render() {
    var id = requestAnimationFrame(render);
    loop();
}

function uiFeedback() {
    var x = ~~(lastX / GUI.numParticles);
    var y = ~~(lastY / GUI.numParticles);

    if (x > fSolver.width || y > fSolver.height) return;

    var c = getIndexForCellPosition(x, y);    

    if (!mouseDown) {
        if (dx==undefined) return;
        return;
    } 

    if (addVel) {        
        fSolver.uOld[c] += dx / GUI.numParticles * GUI.velMul;		
        fSolver.vOld[c] += dy / GUI.numParticles * GUI.velMul;
    }

    // TODO for more velocity and density, we could pick a radius around the mouse for more fluid injection.

    if (add) {		
        fSolver.source[c] += GUI.density;          
    }    
    
}

function getIndexForCellPosition(i,j) {    
    return i + (fsWidth) * j;
}

mainCanvas.addEventListener('mousemove', onMouseMove);

mainCanvas.addEventListener('mousedown', function(event) {    
    event.preventDefault();
    cx = event.pageX - (getScrollX() + mainCanvas.getBoundingClientRect().left) - lastX;
    cy = event.pageY - (getScrollY() + mainCanvas.getBoundingClientRect().top) - lastY;    
    mouseDown = true;
});

mainCanvas.addEventListener('mouseup', function(event) {
    event.preventDefault();
    mouseDown = false;
});


// Mobile devices touch events
mainCanvas.addEventListener('touchstart', function(event) {    
    event.preventDefault();    
    lastX = event.changedTouches[0].pageX - (getScrollX() + mainCanvas.getBoundingClientRect().left);
    lastY = event.changedTouches[0].pageY - (getScrollY() + mainCanvas.getBoundingClientRect().top);            
    mouseDown = true;      
});

mainCanvas.addEventListener('touchmove', onTouchMove);

mainCanvas.addEventListener('touchend', function(event) {    
    event.preventDefault();
    mouseDown = false;
});

function loop() {
    for (var i=0; i < fSolver.numCells; i++) {                
        fSolver.vOld[i] = 0;
        fSolver.uOld[i] = 0;        
        fSolver.densityOld[i] = 0;
    }

    uiFeedback();

    for (var i=0; i < fSolver.numCells; i++) {        
        fSolver.densityOld[i] += fSolver.source[i];
        fSolver.source[i] *= GUI.fadeSpeed;
        // additional code to create dissipatation
        fSolver.density[i] *= GUI.densityMul;        
    }

    fSolver.updateVelocity();    
    fSolver.updateDensity();
    draw();
}

function onMouseMove(e) {
    if (getMousePos(e).y <= 40) return;    
}

function onTouchMove(e) {
    if (getTouchPos(e).y <= 40) return;    
}
Do you want hide your ip address?Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup.