NavierStokes.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. */
  28. namespace libTerrain
  29. {
  30. partial class Channel
  31. {
  32. // Navier Stokes Algorithms ported from
  33. // "Real-Time Fluid Dynamics for Games" by Jos Stam.
  34. // presented at GDC 2003.
  35. // Poorly ported from C++. (I gave up making it properly native somewhere after nsSetBnd)
  36. private static int nsIX(int i, int j, int N)
  37. {
  38. return ((i) + (N + 2)*(j));
  39. }
  40. private static void nsSwap(ref double x0, ref double x)
  41. {
  42. double tmp = x0;
  43. x0 = x;
  44. x = tmp;
  45. }
  46. private static void nsSwap(ref double[] x0, ref double[] x)
  47. {
  48. double[] tmp = x0;
  49. x0 = x;
  50. x = tmp;
  51. }
  52. private void nsAddSource(int N, ref double[] x, ref double[] s, double dt)
  53. {
  54. int i;
  55. int size = (N + 2)*(N + 2);
  56. for (i = 0; i < size; i++)
  57. {
  58. x[i] += dt*s[i];
  59. }
  60. }
  61. private void nsSetBnd(int N, int b, ref double[] x)
  62. {
  63. int i;
  64. for (i = 0; i <= N; i++)
  65. {
  66. x[nsIX(0, i, N)] = b == 1 ? -x[nsIX(1, i, N)] : x[nsIX(1, i, N)];
  67. x[nsIX(0, N + 1, N)] = b == 1 ? -x[nsIX(N, i, N)] : x[nsIX(N, i, N)];
  68. x[nsIX(i, 0, N)] = b == 2 ? -x[nsIX(i, 1, N)] : x[nsIX(i, 1, N)];
  69. x[nsIX(i, N + 1, N)] = b == 2 ? -x[nsIX(i, N, N)] : x[nsIX(i, N, N)];
  70. }
  71. x[nsIX(0, 0, N)] = 0.5f*(x[nsIX(1, 0, N)] + x[nsIX(0, 1, N)]);
  72. x[nsIX(0, N + 1, N)] = 0.5f*(x[nsIX(1, N + 1, N)] + x[nsIX(0, N, N)]);
  73. x[nsIX(N + 1, 0, N)] = 0.5f*(x[nsIX(N, 0, N)] + x[nsIX(N + 1, 1, N)]);
  74. x[nsIX(N + 1, N + 1, N)] = 0.5f*(x[nsIX(N, N + 1, N)] + x[nsIX(N + 1, N, N)]);
  75. }
  76. private void nsLinSolve(int N, int b, ref double[] x, ref double[] x0, double a, double c)
  77. {
  78. int i, j;
  79. for (i = 1; i <= N; i++)
  80. {
  81. for (j = 1; j <= N; j++)
  82. {
  83. x[nsIX(i, j, N)] = (x0[nsIX(i, j, N)] + a*
  84. (x[nsIX(i - 1, j, N)] +
  85. x[nsIX(i + 1, j, N)] +
  86. x[nsIX(i, j - 1, N)] + x[nsIX(i, j + 1, N)])
  87. )/c;
  88. }
  89. }
  90. nsSetBnd(N, b, ref x);
  91. }
  92. private void nsDiffuse(int N, int b, ref double[] x, ref double[] x0, double diff, double dt)
  93. {
  94. double a = dt*diff*N*N;
  95. nsLinSolve(N, b, ref x, ref x0, a, 1 + 4*a);
  96. }
  97. private void nsAdvect(int N, int b, ref double[] d, ref double[] d0, ref double[] u, ref double[] v, double dt)
  98. {
  99. int i, j, i0, j0, i1, j1;
  100. double x, y, s0, t0, s1, t1, dt0;
  101. dt0 = dt*N;
  102. for (i = 1; i <= N; i++)
  103. {
  104. for (j = 1; j <= N; j++)
  105. {
  106. x = i - dt0*u[nsIX(i, j, N)];
  107. y = j - dt0*v[nsIX(i, j, N)];
  108. if (x < 0.5)
  109. x = 0.5;
  110. if (x > N + 0.5)
  111. x = N + 0.5;
  112. i0 = (int) x;
  113. i1 = i0 + 1;
  114. if (y < 0.5)
  115. y = 0.5;
  116. if (y > N + 0.5)
  117. y = N + 0.5;
  118. j0 = (int) y;
  119. j1 = j0 + 1;
  120. s1 = x - i0;
  121. s0 = 1 - s1;
  122. t1 = y - j0;
  123. t0 = 1 - t1;
  124. d[nsIX(i, j, N)] = s0*(t0*d0[nsIX(i0, j0, N)] + t1*d0[nsIX(i0, j1, N)]) +
  125. s1*(t0*d0[nsIX(i1, j0, N)] + t1*d0[nsIX(i1, j1, N)]);
  126. }
  127. }
  128. nsSetBnd(N, b, ref d);
  129. }
  130. public void nsProject(int N, ref double[] u, ref double[] v, ref double[] p, ref double[] div)
  131. {
  132. int i, j;
  133. for (i = 1; i <= N; i++)
  134. {
  135. for (j = 1; j <= N; j++)
  136. {
  137. div[nsIX(i, j, N)] = -0.5*
  138. (u[nsIX(i + 1, j, N)] - u[nsIX(i - 1, j, N)] + v[nsIX(i, j + 1, N)] -
  139. v[nsIX(i, j - 1, N)])/N;
  140. p[nsIX(i, j, N)] = 0;
  141. }
  142. }
  143. nsSetBnd(N, 0, ref div);
  144. nsSetBnd(N, 0, ref p);
  145. nsLinSolve(N, 0, ref p, ref div, 1, 4);
  146. for (i = 1; i <= N; i++)
  147. {
  148. for (j = 1; j <= N; j++)
  149. {
  150. u[nsIX(i, j, N)] -= 0.5*N*(p[nsIX(i + 1, j, N)] - p[nsIX(i - 1, j, N)]);
  151. v[nsIX(i, j, N)] -= 0.5*N*(p[nsIX(i, j + 1, N)] - p[nsIX(i, j - 1, N)]);
  152. }
  153. }
  154. nsSetBnd(N, 1, ref u);
  155. nsSetBnd(N, 2, ref v);
  156. }
  157. private void nsDensStep(int N, ref double[] x, ref double[] x0, ref double[] u, ref double[] v, double diff,
  158. double dt)
  159. {
  160. nsAddSource(N, ref x, ref x0, dt);
  161. nsSwap(ref x0, ref x);
  162. nsDiffuse(N, 0, ref x, ref x0, diff, dt);
  163. nsSwap(ref x0, ref x);
  164. nsAdvect(N, 0, ref x, ref x0, ref u, ref v, dt);
  165. }
  166. private void nsVelStep(int N, ref double[] u, ref double[] v, ref double[] u0, ref double[] v0, double visc,
  167. double dt)
  168. {
  169. nsAddSource(N, ref u, ref u0, dt);
  170. nsAddSource(N, ref v, ref v0, dt);
  171. nsSwap(ref u0, ref u);
  172. nsDiffuse(N, 1, ref u, ref u0, visc, dt);
  173. nsSwap(ref v0, ref v);
  174. nsDiffuse(N, 2, ref v, ref v0, visc, dt);
  175. nsProject(N, ref u, ref v, ref u0, ref v0);
  176. nsSwap(ref u0, ref u);
  177. nsSwap(ref v0, ref v);
  178. nsAdvect(N, 1, ref u, ref u0, ref u0, ref v0, dt);
  179. nsAdvect(N, 2, ref v, ref v0, ref u0, ref v0, dt);
  180. nsProject(N, ref u, ref v, ref u0, ref v0);
  181. }
  182. private void nsBufferToDoubles(ref double[] dens, int N, ref double[,] doubles)
  183. {
  184. int i;
  185. int j;
  186. for (i = 1; i <= N; i++)
  187. {
  188. for (j = 1; j <= N; j++)
  189. {
  190. doubles[i - 1, j - 1] = dens[nsIX(i, j, N)];
  191. }
  192. }
  193. }
  194. private void nsDoublesToBuffer(double[,] doubles, int N, ref double[] dens)
  195. {
  196. int i;
  197. int j;
  198. for (i = 1; i <= N; i++)
  199. {
  200. for (j = 1; j <= N; j++)
  201. {
  202. dens[nsIX(i, j, N)] = doubles[i - 1, j - 1];
  203. }
  204. }
  205. }
  206. private void nsSimulate(int N, int rounds, double dt, double diff, double visc)
  207. {
  208. int size = (N*2)*(N*2);
  209. double[] u = new double[size]; // Force, X axis
  210. double[] v = new double[size]; // Force, Y axis
  211. double[] u_prev = new double[size];
  212. double[] v_prev = new double[size];
  213. double[] dens = new double[size];
  214. double[] dens_prev = new double[size];
  215. nsDoublesToBuffer(map, N, ref dens);
  216. nsDoublesToBuffer(map, N, ref dens_prev);
  217. for (int i = 0; i < rounds; i++)
  218. {
  219. u_prev = u;
  220. v_prev = v;
  221. dens_prev = dens;
  222. nsVelStep(N, ref u, ref v, ref u_prev, ref v_prev, visc, dt);
  223. nsDensStep(N, ref dens, ref dens_prev, ref u, ref v, diff, dt);
  224. }
  225. nsBufferToDoubles(ref dens, N, ref map);
  226. }
  227. /// <summary>
  228. /// Performs computational fluid dynamics on a channel
  229. /// </summary>
  230. /// <param name="rounds">The number of steps to perform (Recommended: 20)</param>
  231. /// <param name="dt">Delta Time - The time between steps (Recommended: 0.1)</param>
  232. /// <param name="diff">Fluid diffusion rate (Recommended: 0.0)</param>
  233. /// <param name="visc">Fluid viscosity (Recommended: 0.0)</param>
  234. public void navierStokes(int rounds, double dt, double diff, double visc)
  235. {
  236. nsSimulate(h, rounds, dt, diff, visc);
  237. }
  238. public void navierStokes(int rounds, double dt, double diff, double visc, ref double[,] uret, ref double[,] vret)
  239. {
  240. int N = h;
  241. int size = (N*2)*(N*2);
  242. double[] u = new double[size]; // Force, X axis
  243. double[] v = new double[size]; // Force, Y axis
  244. double[] u_prev = new double[size];
  245. double[] v_prev = new double[size];
  246. double[] dens = new double[size];
  247. double[] dens_prev = new double[size];
  248. nsDoublesToBuffer(map, N, ref dens);
  249. nsDoublesToBuffer(map, N, ref dens_prev);
  250. for (int i = 0; i < rounds; i++)
  251. {
  252. u_prev = u;
  253. v_prev = v;
  254. dens_prev = dens;
  255. nsVelStep(N, ref u, ref v, ref u_prev, ref v_prev, visc, dt);
  256. nsDensStep(N, ref dens, ref dens_prev, ref u, ref v, diff, dt);
  257. }
  258. nsBufferToDoubles(ref u, N, ref uret);
  259. nsBufferToDoubles(ref v, N, ref vret);
  260. nsBufferToDoubles(ref dens, N, ref map);
  261. }
  262. }
  263. }