Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 13th, 2012  |  syntax: None  |  size: 9.98 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //Demonstration of the shallow water equations in a channel.  This demo has
  2. //periodic boundary conditions in the East/West and walls in the North/South.
  3.  
  4. tributary.trace = false;
  5. //rectangle area parameters
  6. var xT = 30;
  7. var yT = 30;
  8.  
  9. var W = 1024;
  10. var H = 388;
  11.  
  12. //graphic parameters
  13. var backgroundColor = "#F1F1F1";
  14. var numberOfColors = 64
  15.  
  16. //number of points to use in solution line
  17. var m = 40
  18. var n = 40
  19.  
  20. //domain sizes, in km
  21. var length = 6000.
  22. var width = 4400.
  23.  
  24. //initial condition parameters
  25. var h0 = 2000
  26. var h1 = -220.
  27. var h2 = 133.
  28.  
  29. // Gravity, m/s^2
  30. var gp = 9.81
  31. // Coriolis factor
  32. var f = 1.0e-4
  33. // Beta-plane 1/(m s)
  34. var beta = 1.5e-11
  35.  
  36. //time stretching
  37. var timeStretch = 10
  38. var colorMap = null;
  39.  
  40. //var vizMode = "phi";
  41. //var vizMode = "u";
  42. //var vizMode = "v";
  43. var vizMode = "uv";
  44. var oldt, u_old, v_old, phi_old, dx, dy, realT, k, u_new,v_new,phi_new;
  45. tributary.init = function(g) {
  46.  
  47.   oldt  = null
  48.   u_old = null
  49.   v_old = null
  50.   phi_old = null
  51.   dx = 2*length*1000/ (m-2)
  52.   dy = 2*width*1000/ (n-2)
  53.   realT = 0.
  54.   k = 0
  55.   u_new = create2DArray(m, n)
  56.   v_new = create2DArray(m, n)
  57.   phi_new = create2DArray(m, n)
  58.   //initialConditions(u_new, v_new, phi_new)
  59.   //getColorMap();
  60.  
  61.   //initialize svg stuff
  62.   var inds = d3.range(n*m);
  63.   var cells = g.selectAll("g.cell")
  64.     .data(inds)
  65.     .enter()
  66.     .append("g").classed("cell", true)
  67.    //cells.attr("transform",
  68.    cells
  69.     .append("rect");
  70.  
  71. };
  72.  
  73. tributary.run = function(g,t) {
  74.  
  75.   if (u_old != null) {
  76.     dt = timeStretch*10000*(t - oldt)
  77.     //advanceModel(u_new, v_new, phi_new, u_old, v_old, phi_old, k, dt)
  78.     k++;
  79.   }
  80.   else {
  81.     dt = 0.;
  82.   }
  83.  
  84.   //drawShallowWater(ctx, u_new, v_new, phi_new);
  85.   //updateValues();
  86.  
  87.   realT = realT + dt;
  88.   oldt  = t;
  89. };
  90.  
  91. //drawing functions
  92. function drawShallowWater(ctx, u_new, v_new, phi_new) {
  93.   if (vizMode == "phi") {
  94.     vmin = 14000;
  95.     vmax = 24000;
  96.     vsrc = phi_new
  97.   }
  98.   else if (vizMode == "u") {
  99.     vmin = -30;
  100.     vmax = 10;
  101.     vsrc = u_new
  102.   }
  103.   else if (vizMode == "v") {
  104.     vmin = -20;
  105.     vmax = 20;
  106.     vsrc = v_new;
  107.   }
  108.   else {
  109.     vmin = 0;
  110.     vmax = 30;
  111.     // vsrc specified below
  112.   }
  113.  
  114.   for (i = 0; i < m; i++) {
  115.     for (j = 0; j < n; j++) {
  116.       if (vizMode == "uv") {
  117.         lv = Math.sqrt(Math.pow(u_new[i][j],2) + Math.pow(v_new[i][j],2)) - vmin;
  118.       }
  119.       else {
  120.         lv = vsrc[i][j] - vmin;
  121.       }
  122.       hv = lv/(vmax-vmin)
  123.       ci = Math.min(Math.max(Math.round(numberOfColors*hv),0),numberOfColors-1)
  124.       if (!isNaN(ci)) {
  125.         color = colorMap[ci];
  126.         ctx.fillStyle = RGB2Color(color)
  127.       }
  128.       else {
  129.         ctx.fillStyle = "#000000"                  
  130.       }
  131.  
  132.       drawRectT(ctx, (i + 0.)/m, (n - j - 1.)/n, 1./m, 1./n)
  133.     }
  134.   }
  135. }
  136.  
  137. function getColorMap() {
  138.   colorMap = create2DArray(numberOfColors, 3);
  139.    
  140.   nc4 = numberOfColors /4;
  141.   // red
  142.   colorTriangle(0, 3*nc4, nc4, numberOfColors);
  143.   // green
  144.   colorTriangle(1, 2*nc4, nc4, numberOfColors);
  145.   // blue
  146.   colorTriangle(2, 1*nc4, nc4, numberOfColors);
  147. }
  148.  
  149. function colorTriangle(colorIndex, start, width, length) {    
  150.   lb  = start - 3*width/2 - 1;
  151.   mb1 = start - width/2 - 1;
  152.   mb2 = start + width/2 - 1;
  153.   rb  = start + 3*width/2 - 1;
  154.  
  155.   for (i = 0; i < length; i++) {        
  156.     // rising regime
  157.     if (i <= mb1 && i > lb) {
  158.       colorMap[i][colorIndex] = lininterp(i, lb, mb1, 0, 1);
  159.     }
  160.     // one regime
  161.     else if (i <= mb2 && i > mb1) {
  162.       colorMap[i][colorIndex] = 1.;
  163.     }
  164.     // falling regime
  165.     else if (i <= rb && i > mb2) {
  166.       colorMap[i][colorIndex] = lininterp(i, mb2, rb, 1, 0);
  167.     }
  168.     // zero regime
  169.     else {
  170.       colorMap[i][colorIndex] = 0.;
  171.     }
  172.   }
  173. }
  174.  
  175. //model functions
  176. function initialConditions(u_new, v_new, phi_new) {
  177.   hx = 1./(4.*dx)
  178.   hy = 1./(4.*dy)
  179.   hhx = 2.*hx
  180.   hhy = 2.*hy
  181.   pi = Math.PI
  182.   mlength = length*1000
  183.   mwidth = width*1000
  184.  
  185.   for (i=0; i < m; i++) {
  186.     for (j=0; j < n; j++) {
  187.       xix = (i - 1)*dx/2.
  188.       triarg = 2.*pi*xix/mlength
  189.       xtrig = Math.sin(triarg)
  190.       dtrig = (2.*pi/mlength)*Math.cos(triarg)
  191.  
  192.       yiy = (j - 1)*dy/2.
  193.       fr = f + beta*(yiy-mwidth/2.)
  194.  
  195.       co = 9.*((mwidth/2.) - yiy)/(2.*mwidth)
  196.  
  197.       ab1 = cosh(co)
  198.       bb = 1./(ab1*ab1)
  199.       dco = -4.5/mwidth
  200.       derth = bb*dco
  201.  
  202.       aa = tanh(co)
  203.       dersec = -2.*aa*derth
  204.  
  205.       u_new[i][j] = -gp*(h1*derth + h2*xtrig*dersec )/fr
  206.       v_new[i][j] = gp*(h2*bb*dtrig)/fr
  207.       phi_new[i][j] = gp*(h0 + h1*aa + h2*bb*xtrig)
  208.     }
  209.   }
  210.  
  211.   setBoundaryConditions(u_new, v_new, phi_new, m, n)
  212. }
  213.  
  214. function advanceModel(u_new, v_new, phi_new, u_old, v_old, phi_old, k, dt) {
  215.  
  216.   if (u_old == null || v_old == null || phi_old == null)
  217.     return;
  218.   if (k == 0) {
  219.     dtv = dt;
  220.     kv = 1;
  221.   }
  222.   else {
  223.     kv = 0;
  224.     dtv = 2*dt;
  225.   }
  226.  
  227.   hx = 1./(4.*dx)
  228.   hy = 1./(4.*dy)
  229.   hhx = 2.*hx
  230.   hhy = 2.*hy
  231.  
  232.   for (i=1; i < m-1; i++) {
  233.     for (j=1; j < n-1; j++) {
  234.       ip1=i+1
  235.       im1=i-1
  236.       jp1=j+1
  237.       jm1=j-1
  238.  
  239.       aa = (u_old[ip1][j][1] + u_old[im1][j][1])*
  240.            (u_old[ip1][j][1] - u_old[im1][j][1])*hx
  241.  
  242.       bb = (v_old[i][jp1][1] + v_old[i][jm1][1])*
  243.            (u_old[i][jp1][1] - u_old[i][jm1][1])*hy
  244.  
  245.       cc = (u_old[ip1][j][1] + u_old[im1][j][1])*
  246.            (v_old[ip1][j][1] - v_old[im1][j][1])*hx
  247.  
  248.       dd = (v_old[i][jp1][1] + v_old[i][jm1][1])*
  249.            (v_old[i][jp1][1] - v_old[i][jm1][1])*hy
  250.  
  251.       phix = (phi_old[ip1][j][1] - phi_old[im1][j][1])*hhx
  252.       phiy = (phi_old[i][jp1][1] - phi_old[i][jm1][1])*hhy
  253.      
  254.       fu = f*u_old[i][j][1]
  255.       fv = f*v_old[i][j][1]
  256.  
  257.       phiu = (phi_old[ip1][j][1]*u_old[ip1][j][1] -
  258.               phi_old[im1][j][1]*u_old[im1][j][1])*hhx
  259.       phiv = (phi_old[i][jp1][1]*v_old[i][jp1][1] -
  260.               phi_old[i][jm1][1]*v_old[i][jm1][1])*hhy
  261.  
  262.       u_new[i][j] = u_old[i][j][kv] - dtv*(aa + bb + phix - fv)
  263.       v_new[i][j] = v_old[i][j][kv] - dtv*(cc + dd + phiy + fu)
  264.       phi_new[i][j] = phi_old[i][j][kv] - dtv*(phiu + phiv)
  265.     }
  266.   }
  267.  
  268.   setBoundaryConditions(u_new, v_new, phi_new, m, n)
  269. }
  270.  
  271. function setBoundaryConditions(u, v, phi, m, n) {
  272.  
  273.   for (i=1; i < n-1; i++) {
  274.     // left boundary, periodic
  275.     u[  0][i] = u[  m-2][i]
  276.     v[  0][i] = v[  m-2][i]
  277.     phi[0][i] = phi[m-2][i]
  278.   }
  279.  
  280.   for (i=0; i < m; i++) {
  281.     // top boundary, wall
  282.     u[  i][n-1] = u[  i][n-2]
  283.     v[  i][n-1] = 0.
  284.     phi[i][n-1] = phi[i][n-2]
  285.   }
  286.  
  287.   for (i = 1; i < n-1; i++) {
  288.     // right boundary, periodic
  289.     u[  m-1][i] = u[  1][i]
  290.     v[  m-1][i] = v[  1][i]
  291.     phi[m-1][i] = phi[1][i]
  292.   }
  293.  
  294.   for (i=0; i < m; i++) {
  295.     // bottom boundary, wall
  296.     u[i][0]   = u[i][1]
  297.     v[i][0]   = 0.
  298.     phi[i][0] = phi[i][1]
  299.   }
  300.  
  301.   // top-right corner
  302.   u[m-1][n-1] = (u[m-2][n-1] + u[m-1][n-2] + u[m-2][n-2])/3.                          
  303.   v[m-1][n-1] = (v[m-2][n-1] + v[m-1][n-2] + v[m-2][n-2])/3.                          
  304.   phi[m-1][n-1] = (phi[m-2][n-1] + phi[m-1][n-2] + phi[m-2][n-2])/3.                  
  305.                                                                                      
  306.   // bottom-right corner                                                              
  307.   u[m-1][0] = (u[m-2][0] + u[m-1][1] + u[m-2][1])/3.                                  
  308.   v[m-1][0] = (v[m-2][0] + v[m-1][1] + v[m-2][1])/3.                                  
  309.   phi[m-1][0] = (phi[m-2][0] + phi[m-1][1] + phi[m-2][1])/3.                          
  310.                                                                                      
  311.   // bottom-left corner                                                              
  312.   u[0][0] = (u[0][1] + u[1][0] + u[1][1])/3.                                          
  313.   v[0][0] = (v[0][1] + v[1][0] + v[1][1])/3.                                          
  314.   phi[0][0] = (phi[0][1] + phi[1][0] + phi[1][1])/3.                                  
  315.                                                                                      
  316.   // top-left corner                                                                  
  317.   u[0][n-1] = (u[0][n-2] + u[1][n-1] + u[1][n-2])/3.                                  
  318.   v[0][n-1] = (v[0][n-2] + v[1][n-1] + v[1][n-2])/3.                                  
  319.   phi[0][n-1] = (phi[0][n-2] + phi[1][n-1] + phi[1][n-2])/3.  
  320. }
  321.  
  322. function updateValues() {  
  323.   if (u_old == null) {
  324.     u_old = create3DArray(m, n, 2);
  325.     v_old = create3DArray(m, n, 2);
  326.     phi_old = create3DArray(m, n, 2);
  327.     type = 0;
  328.   }
  329.   else {
  330.     type = 1;
  331.   }
  332.  
  333.   if (type != 0) {    
  334.     for (i=0; i < m; i++) {
  335.       for (j=0; j < n; j++) {
  336.         u_old  [i][j][0] = u_old  [i][j][1]
  337.         v_old  [i][j][0] = v_old  [i][j][1]
  338.         phi_old[i][j][0] = phi_old[i][j][1]
  339.       }
  340.     }
  341.   }
  342.  
  343.   for (i=0; i < m; i++) {
  344.     for (j=0; j < n; j++) {
  345.       u_old  [i][j][1] = u_new  [i][j]
  346.       v_old  [i][j][1] = v_new  [i][j]
  347.       phi_old[i][j][1] = phi_new[i][j]
  348.     }
  349.   }
  350. }
  351.  
  352. //drawing primitves
  353. function drawRectT(ctx, x, y, w, h){
  354.   ctx.fillRect(x*W+xT, y*H+yT, w*W, h*H);
  355. }
  356.  
  357. function lineT(ctx, x0, y0, x1, y1){
  358.   ctx.beginPath();
  359.   moveToT(ctx, x0, y0);
  360.   lineToT(ctx, x1, y1);
  361.   ctx.stroke();
  362. }
  363.  
  364. function moveToT(ctx, x, y){
  365.   ctx.moveTo(x*W+xT, (1-y)*H+yT);
  366. }
  367.  
  368. function lineToT(ctx, x, y){
  369.   ctx.lineTo(x*W+xT, (1-y)*H+yT);
  370. }
  371.  
  372. //helper functions
  373. function create2DArray(m, n) {
  374.   var a = new Array(m);
  375.  
  376.   for (i = 0; i < m; i++) {
  377.       a[i] = new Array(n);
  378.   }
  379.  
  380.   return a;
  381. }
  382.  
  383. function create3DArray(m, n, o) {
  384.   var a = new Array(m);
  385.  
  386.   for (i = 0; i < m; i++) {
  387.     a[i] = new Array(n);
  388.     for (j = 0; j < n; j++) {
  389.       a[i][j] = new Array(o);
  390.     }
  391.   }
  392.  
  393.   return a;
  394. }
  395.  
  396.  
  397. // Hyperbolic cosine of x
  398. function cosh(x) {
  399.   return (Math.exp(x) + Math.exp(-x)) / 2;
  400. }
  401.  
  402. // Hyperbolic tangent of x
  403. function tanh(x) {
  404.   return (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x));
  405. }
  406.  
  407. // Convert r, g, b to hex color
  408. function RGB2Color(color)
  409. {
  410.   return '#' + byte2Hex(255*color[0] % 256) + byte2Hex(255*color[1] % 256) + byte2Hex(255*color[2] % 256);
  411. }
  412.  
  413. function byte2Hex(n)
  414. {
  415.   // from http://krazydad.com/tutorials/makecolors.php
  416.   var nybHexString = "0123456789ABCDEF";
  417.   return String(nybHexString.substr((n >> 4) & 0x0F,1)) + nybHexString.substr(n & 0x0F,1);
  418. }
  419.  
  420. function lininterp(x, x0, x1, y0, y1) {
  421.     delta = x - x0;
  422.     return y0 + (delta*y1 - delta*y0)/(x1 - x0);
  423. }