Advertisement
The_Oddler

Fluid

Jul 2nd, 2014
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// <summary>
  2. /// Update the fluid. Part 3.2 in the paper.
  3. /// </summary>
  4. /// <param name="dt">delta time</param>
  5. /// <param name="dx">delta x. Distance between middles of grid-cells.</param>
  6. /// <param name="lowerLayersHeight">The total heights of the layers under this one. Basically this is the ground under the water.</param>
  7. public override void DoUpdate(float dt, float dx, float[][] lowerLayersHeight) {
  8.     _fluidTimer.Start();
  9.    
  10.     int x, y;
  11.     float totalHeight, dhL, dhR, dhT, dhB;
  12.     float dt_A_g_l = dt * _A * g / dx; //all constants for equation 2
  13.     float K; // scaling factor for the outflow flux
  14.     float dV;
  15.    
  16.     for (x=1 ; x <= N ; x++ ) {
  17.         for (y=1 ; y <= N ; y++ ) {
  18.             //
  19.             // 3.2.1 Outflow Flux Computation
  20.             // --------------------------------------------------------------
  21.             totalHeight = lowerLayersHeight[x][y] + _height[x][y];
  22.             dhL = totalHeight - lowerLayersHeight[x-1][y] - _height[x-1][y]; //(3)
  23.             dhR = totalHeight - lowerLayersHeight[x+1][y] - _height[x+1][y];
  24.             dhT = totalHeight - lowerLayersHeight[x][y+1] - _height[x][y+1];
  25.             dhB = totalHeight - lowerLayersHeight[x][y-1] - _height[x][y-1];
  26.            
  27.             _tempFlux[x][y].left =   Mathf.Max(0.0f, _flux[x][y].left    + dt_A_g_l * dhL ); //(2)
  28.             _tempFlux[x][y].right =  Mathf.Max(0.0f, _flux[x][y].right   + dt_A_g_l * dhR );
  29.             _tempFlux[x][y].top =    Mathf.Max(0.0f, _flux[x][y].top     + dt_A_g_l * dhT );
  30.             _tempFlux[x][y].bottom = Mathf.Max(0.0f, _flux[x][y].bottom      + dt_A_g_l * dhB );
  31.            
  32.             float totalFlux = _tempFlux[x][y].left + _tempFlux[x][y].right + _tempFlux[x][y].top + _tempFlux[x][y].bottom;
  33.             if (totalFlux > 0) {
  34.                 K = Mathf.Min(1.0f, _height[x][y] * dx * dx / totalFlux / dt);  //(4)
  35.                
  36.                 _tempFlux[x][y].left =   K * _tempFlux[x][y].left;  //(5)
  37.                 _tempFlux[x][y].right =  K * _tempFlux[x][y].right;
  38.                 _tempFlux[x][y].top =    K * _tempFlux[x][y].top;
  39.                 _tempFlux[x][y].bottom = K * _tempFlux[x][y].bottom;
  40.             }
  41.             //swap temp and the real one after the for-loops
  42.            
  43.             //
  44.             // 3.2.2 Water Surface and Velocity Field Update
  45.             // ----------------------------------------------------------------------------------------
  46.             dV = dt * (
  47.                 //sum in
  48.                 _tempFlux[x-1][y].right + _tempFlux[x][y-1].top + _tempFlux[x+1][y].left + _tempFlux[x][y+1].bottom
  49.                 //minus sum out
  50.                 - _tempFlux[x][y].right - _tempFlux[x][y].top - _tempFlux[x][y].left - _tempFlux[x][y].bottom
  51.                 ); //(6)
  52.             _tempHeight[x][y] = _height[x][y] + dV / (dx*dx); //(7)
  53.             //swap temp and the real one after the for-loops
  54.         }
  55.     }
  56.     Helpers.Swap(ref _tempFlux, ref _flux);
  57.     Helpers.Swap(ref _tempHeight, ref _height);
  58.    
  59.     _fluidTimer.Stop();
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement