aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Terrain.BasicTerrain/libTerrainBSD/Channel/Manipulators/NavierStokes.cs
blob: f14cea7950297f180ada0fd17847a8b662e8f7d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* Copyright (c) Contributors, http://opensimulator.org/
* See CONTRIBUTORS.TXT for a full list of copyright holders.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the OpenSim Project nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* 
*/

namespace libTerrain
{
    partial class Channel
    {
        // Navier Stokes Algorithms ported from
        // "Real-Time Fluid Dynamics for Games" by Jos Stam.
        // presented at GDC 2003.

        // Poorly ported from C++. (I gave up making it properly native somewhere after nsSetBnd)

        private static int nsIX(int i, int j, int N)
        {
            return ((i) + (N + 2)*(j));
        }

        private static void nsSwap(ref double x0, ref double x)
        {
            double tmp = x0;
            x0 = x;
            x = tmp;
        }

        private static void nsSwap(ref double[] x0, ref double[] x)
        {
            double[] tmp = x0;
            x0 = x;
            x = tmp;
        }

        private void nsAddSource(int N, ref double[] x, ref double[] s, double dt)
        {
            int i;
            int size = (N + 2)*(N + 2);
            for (i = 0; i < size; i++)
            {
                x[i] += dt*s[i];
            }
        }

        private void nsSetBnd(int N, int b, ref double[] x)
        {
            int i;
            for (i = 0; i <= N; i++)
            {
                x[nsIX(0, i, N)] = b == 1 ? -x[nsIX(1, i, N)] : x[nsIX(1, i, N)];
                x[nsIX(0, N + 1, N)] = b == 1 ? -x[nsIX(N, i, N)] : x[nsIX(N, i, N)];
                x[nsIX(i, 0, N)] = b == 2 ? -x[nsIX(i, 1, N)] : x[nsIX(i, 1, N)];
                x[nsIX(i, N + 1, N)] = b == 2 ? -x[nsIX(i, N, N)] : x[nsIX(i, N, N)];
            }
            x[nsIX(0, 0, N)] = 0.5f*(x[nsIX(1, 0, N)] + x[nsIX(0, 1, N)]);
            x[nsIX(0, N + 1, N)] = 0.5f*(x[nsIX(1, N + 1, N)] + x[nsIX(0, N, N)]);
            x[nsIX(N + 1, 0, N)] = 0.5f*(x[nsIX(N, 0, N)] + x[nsIX(N + 1, 1, N)]);
            x[nsIX(N + 1, N + 1, N)] = 0.5f*(x[nsIX(N, N + 1, N)] + x[nsIX(N + 1, N, N)]);
        }

        private void nsLinSolve(int N, int b, ref double[] x, ref double[] x0, double a, double c)
        {
            int i, j;
            for (i = 1; i <= N; i++)
            {
                for (j = 1; j <= N; j++)
                {
                    x[nsIX(i, j, N)] = (x0[nsIX(i, j, N)] + a*
                                                            (x[nsIX(i - 1, j, N)] +
                                                             x[nsIX(i + 1, j, N)] +
                                                             x[nsIX(i, j - 1, N)] + x[nsIX(i, j + 1, N)])
                                       )/c;
                }
            }

            nsSetBnd(N, b, ref x);
        }

        private void nsDiffuse(int N, int b, ref double[] x, ref double[] x0, double diff, double dt)
        {
            double a = dt*diff*N*N;
            nsLinSolve(N, b, ref x, ref x0, a, 1 + 4*a);
        }

        private void nsAdvect(int N, int b, ref double[] d, ref double[] d0, ref double[] u, ref double[] v, double dt)
        {
            int i, j, i0, j0, i1, j1;
            double x, y, s0, t0, s1, t1, dt0;

            dt0 = dt*N;

            for (i = 1; i <= N; i++)
            {
                for (j = 1; j <= N; j++)
                {
                    x = i - dt0*u[nsIX(i, j, N)];
                    y = j - dt0*v[nsIX(i, j, N)];

                    if (x < 0.5)
                        x = 0.5;
                    if (x > N + 0.5)
                        x = N + 0.5;
                    i0 = (int) x;
                    i1 = i0 + 1;

                    if (y < 0.5)
                        y = 0.5;
                    if (y > N + 0.5)
                        y = N + 0.5;
                    j0 = (int) y;
                    j1 = j0 + 1;

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

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

            nsSetBnd(N, b, ref d);
        }

        public void nsProject(int N, ref double[] u, ref double[] v, ref double[] p, ref double[] div)
        {
            int i, j;

            for (i = 1; i <= N; i++)
            {
                for (j = 1; j <= N; j++)
                {
                    div[nsIX(i, j, N)] = -0.5*
                                         (u[nsIX(i + 1, j, N)] - u[nsIX(i - 1, j, N)] + v[nsIX(i, j + 1, N)] -
                                          v[nsIX(i, j - 1, N)])/N;
                    p[nsIX(i, j, N)] = 0;
                }
            }

            nsSetBnd(N, 0, ref div);
            nsSetBnd(N, 0, ref p);

            nsLinSolve(N, 0, ref p, ref div, 1, 4);

            for (i = 1; i <= N; i++)
            {
                for (j = 1; j <= N; j++)
                {
                    u[nsIX(i, j, N)] -= 0.5*N*(p[nsIX(i + 1, j, N)] - p[nsIX(i - 1, j, N)]);
                    v[nsIX(i, j, N)] -= 0.5*N*(p[nsIX(i, j + 1, N)] - p[nsIX(i, j - 1, N)]);
                }
            }

            nsSetBnd(N, 1, ref u);
            nsSetBnd(N, 2, ref v);
        }

        private void nsDensStep(int N, ref double[] x, ref double[] x0, ref double[] u, ref double[] v, double diff,
                                double dt)
        {
            nsAddSource(N, ref x, ref x0, dt);
            nsSwap(ref x0, ref x);
            nsDiffuse(N, 0, ref x, ref x0, diff, dt);
            nsSwap(ref x0, ref x);
            nsAdvect(N, 0, ref x, ref x0, ref u, ref v, dt);
        }

        private void nsVelStep(int N, ref double[] u, ref double[] v, ref double[] u0, ref double[] v0, double visc,
                               double dt)
        {
            nsAddSource(N, ref u, ref u0, dt);
            nsAddSource(N, ref v, ref v0, dt);
            nsSwap(ref u0, ref u);
            nsDiffuse(N, 1, ref u, ref u0, visc, dt);
            nsSwap(ref v0, ref v);
            nsDiffuse(N, 2, ref v, ref v0, visc, dt);
            nsProject(N, ref u, ref v, ref u0, ref v0);
            nsSwap(ref u0, ref u);
            nsSwap(ref v0, ref v);
            nsAdvect(N, 1, ref u, ref u0, ref u0, ref v0, dt);
            nsAdvect(N, 2, ref v, ref v0, ref u0, ref v0, dt);
            nsProject(N, ref u, ref v, ref u0, ref v0);
        }

        private void nsBufferToDoubles(ref double[] dens, int N, ref double[,] doubles)
        {
            int i;
            int j;

            for (i = 1; i <= N; i++)
            {
                for (j = 1; j <= N; j++)
                {
                    doubles[i - 1, j - 1] = dens[nsIX(i, j, N)];
                }
            }
        }

        private void nsDoublesToBuffer(double[,] doubles, int N, ref double[] dens)
        {
            int i;
            int j;

            for (i = 1; i <= N; i++)
            {
                for (j = 1; j <= N; j++)
                {
                    dens[nsIX(i, j, N)] = doubles[i - 1, j - 1];
                }
            }
        }

        private void nsSimulate(int N, int rounds, double dt, double diff, double visc)
        {
            int size = (N*2)*(N*2);

            double[] u = new double[size]; // Force, X axis
            double[] v = new double[size]; // Force, Y axis
            double[] u_prev = new double[size];
            double[] v_prev = new double[size];
            double[] dens = new double[size];
            double[] dens_prev = new double[size];

            nsDoublesToBuffer(map, N, ref dens);
            nsDoublesToBuffer(map, N, ref dens_prev);

            for (int i = 0; i < rounds; i++)
            {
                u_prev = u;
                v_prev = v;
                dens_prev = dens;

                nsVelStep(N, ref u, ref v, ref u_prev, ref v_prev, visc, dt);
                nsDensStep(N, ref dens, ref dens_prev, ref u, ref v, diff, dt);
            }

            nsBufferToDoubles(ref dens, N, ref map);
        }

        /// <summary>
        /// Performs computational fluid dynamics on a channel
        /// </summary>
        /// <param name="rounds">The number of steps to perform (Recommended: 20)</param>
        /// <param name="dt">Delta Time - The time between steps (Recommended: 0.1)</param>
        /// <param name="diff">Fluid diffusion rate (Recommended: 0.0)</param>
        /// <param name="visc">Fluid viscosity (Recommended: 0.0)</param>
        public void navierStokes(int rounds, double dt, double diff, double visc)
        {
            nsSimulate(h, rounds, dt, diff, visc);
        }

        public void navierStokes(int rounds, double dt, double diff, double visc, ref double[,] uret, ref double[,] vret)
        {
            int N = h;

            int size = (N*2)*(N*2);

            double[] u = new double[size]; // Force, X axis
            double[] v = new double[size]; // Force, Y axis
            double[] u_prev = new double[size];
            double[] v_prev = new double[size];
            double[] dens = new double[size];
            double[] dens_prev = new double[size];

            nsDoublesToBuffer(map, N, ref dens);
            nsDoublesToBuffer(map, N, ref dens_prev);

            for (int i = 0; i < rounds; i++)
            {
                u_prev = u;
                v_prev = v;
                dens_prev = dens;

                nsVelStep(N, ref u, ref v, ref u_prev, ref v_prev, visc, dt);
                nsDensStep(N, ref dens, ref dens_prev, ref u, ref v, diff, dt);
            }

            nsBufferToDoubles(ref u, N, ref uret);
            nsBufferToDoubles(ref v, N, ref vret);
            nsBufferToDoubles(ref dens, N, ref map);
        }
    }
}