Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        public static void GetCurl(this GridField3d <Vec3d> field, Vec3d[] result, bool parallel = false)
        {
            // implementation reference
            // http://www.math.harvard.edu/archive/21a_spring_09/PDF/13-05-curl-and-divergence.pdf

            if (parallel)
            {
                Parallel.ForEach(Partitioner.Create(0, field.Count), range => Body(range.Item1, range.Item2));
            }
            else
            {
                Body(0, field.Count);
            }

            void Body(int from, int to)
            {
                var vals = field.Values;
                int nx   = field.CountX;
                int ny   = field.CountY;
                int nz   = field.CountZ;
                int nxy  = nx * ny;

                (var dx, var dy, var dz) = (0.5 / field.Scale);

                (int di, int dj, int dk) = field.GetBoundaryOffsets();
                (int i, int j, int k)    = field.IndicesAt(from);

                for (int index = from; index < to; index++, i++)
                {
                    if (i == nx)
                    {
                        j++; i = 0;
                    }
                    if (j == ny)
                    {
                        k++; j = 0;
                    }

                    Vec3d tx0 = (i == 0) ? vals[index + di] : vals[index - 1];
                    Vec3d tx1 = (i == nx - 1) ? vals[index - di] : vals[index + 1];

                    Vec3d ty0 = (j == 0) ? vals[index + dj] : vals[index - nx];
                    Vec3d ty1 = (j == ny - 1) ? vals[index - dj] : vals[index + nx];

                    Vec3d tz0 = (k == 0) ? vals[index + dk] : vals[index - nxy];
                    Vec3d tz1 = (k == nz - 1) ? vals[index - dk] : vals[index + nxy];

                    result[index] = new Vec3d(
                        (ty1.Z - ty0.Z) * dy - (tz1.Y - tz0.Y) * dz,
                        (tz1.X - tz0.X) * dz - (tx1.Z - tx0.Z) * dx,
                        (tx1.Y - tx0.Y) * dx - (ty1.X - ty0.X) * dy);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds the Laplacian of the field to the deltas array.
        /// http://en.wikipedia.org/wiki/Discrete_Laplace_operator
        /// </summary>
        /// <param name="field"></param>
        /// <param name="deltas"></param>
        /// <param name="rate"></param>
        /// <param name="parallel"></param>
        public static void Diffuse(GridField3d <double> field, double[] deltas, double rate, bool parallel = false)
        {
            if (parallel)
            {
                Parallel.ForEach(Partitioner.Create(0, field.Count), range => Body(range.Item1, range.Item2));
            }
            else
            {
                Body(0, field.Count);
            }

            void Body(int from, int to)
            {
                var vals = field.Values;
                int nx   = field.CountX;
                int ny   = field.CountY;
                int nz   = field.CountZ;
                int nxy  = field.CountXY;

                (double dx, double dy, double dz) = field.Scale;
                dx = 1.0 / (dx * dx);
                dy = 1.0 / (dy * dy);
                dz = 1.0 / (dz * dz);

                (int di, int dj, int dk) = field.GetBoundaryOffsets();
                (int i, int j, int k)    = field.IndicesAt(from);

                for (int index = from; index < to; index++, i++)
                {
                    if (i == nx)
                    {
                        j++; i = 0;
                    }
                    if (j == ny)
                    {
                        k++; j = 0;
                    }

                    double tx0 = (i == 0) ? vals[index + di] : vals[index - 1];
                    double tx1 = (i == nx - 1) ? vals[index - di] : vals[index + 1];

                    double ty0 = (j == 0) ? vals[index + dj] : vals[index - nx];
                    double ty1 = (j == ny - 1) ? vals[index - dj] : vals[index + nx];

                    double tz0 = (k == 0) ? vals[index + dk] : vals[index - nxy];
                    double tz1 = (k == nz - 1) ? vals[index - dk] : vals[index + nxy];

                    double t = vals[index] * 2.0;
                    deltas[index] += ((tx0 + tx1 - t) * dx + (ty0 + ty1 - t) * dy + (tz0 + tz1 - t) * dz) * rate;
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        public static void GetLaplacian(this GridField3d <Vec3d> field, Vec3d[] result, bool parallel = false)
        {
            if (parallel)
            {
                Parallel.ForEach(Partitioner.Create(0, field.Count), range => Body(range.Item1, range.Item2));
            }
            else
            {
                Body(0, field.Count);
            }

            void Body(int from, int to)
            {
                var vals = field.Values;
                int nx   = field.CountX;
                int ny   = field.CountY;
                int nz   = field.CountZ;
                int nxy  = nx * ny;

                (var dx, var dy, var dz) = field.Scale;
                dx = 1.0 / (dx * dx);
                dy = 1.0 / (dy * dy);
                dz = 1.0 / (dz * dz);

                (int di, int dj, int dk) = field.GetBoundaryOffsets();
                (int i, int j, int k)    = field.IndicesAt(from);

                for (int index = from; index < to; index++, i++)
                {
                    if (i == nx)
                    {
                        j++; i = 0;
                    }
                    if (j == ny)
                    {
                        k++; j = 0;
                    }

                    Vec3d tx0 = (i == 0) ? vals[index + di] : vals[index - 1];
                    Vec3d tx1 = (i == nx - 1) ? vals[index - di] : vals[index + 1];

                    Vec3d ty0 = (j == 0) ? vals[index + dj] : vals[index - nx];
                    Vec3d ty1 = (j == ny - 1) ? vals[index - dj] : vals[index + nx];

                    Vec3d tz0 = (k == 0) ? vals[index + dk] : vals[index - nxy];
                    Vec3d tz1 = (k == nz - 1) ? vals[index - dk] : vals[index + nxy];

                    Vec3d t = vals[index] * 2.0;
                    result[index] = (tx0 + tx1 - t) * dx + (ty0 + ty1 - t) * dy + (tz0 + tz1 - t) * dz;
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="result"></param>
        /// <param name="parallel"></param>
        public static void GetGradient(this GridField3d <double> field, Vec3d[] result, bool parallel = false)
        {
            if (parallel)
            {
                Parallel.ForEach(Partitioner.Create(0, field.Count), range => Body(range.Item1, range.Item2));
            }
            else
            {
                Body(0, field.Count);
            }

            void Body(int from, int to)
            {
                var vals = field.Values;
                int nx   = field.CountX;
                int ny   = field.CountY;
                int nz   = field.CountZ;
                int nxy  = nx * ny;

                (var dx, var dy, var dz) = (0.5 / field.Scale);

                (int di, int dj, int dk) = field.GetBoundaryOffsets();
                (int i, int j, int k)    = field.IndicesAt(from);

                for (int index = from; index < to; index++, i++)
                {
                    if (i == nx)
                    {
                        j++; i = 0;
                    }
                    if (j == ny)
                    {
                        k++; j = 0;
                    }

                    double tx0 = (i == 0) ? vals[index + di] : vals[index - 1];
                    double tx1 = (i == nx - 1) ? vals[index - di] : vals[index + 1];

                    double ty0 = (j == 0) ? vals[index + dj] : vals[index - nx];
                    double ty1 = (j == ny - 1) ? vals[index - dj] : vals[index + nx];

                    double tz0 = (k == 0) ? vals[index + dk] : vals[index - nxy];
                    double tz1 = (k == nz - 1) ? vals[index - dk] : vals[index + nxy];

                    result[index] = new Vec3d((tx1 - tx0) * dx, (ty1 - ty0) * dy, (tz1 - tz0) * dz);
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// http://micsymposium.org/mics_2011_proceedings/mics2011_submission_30.pdf
        /// </summary>
        /// <param name="field"></param>
        /// <param name="deltas"></param>
        /// <param name="slope"></param>
        /// <param name="rate"></param>
        /// <param name="parallel"></param>
        public static void ErodeThermal(GridField3d <double> field, double[] deltas, double slope, double rate, bool parallel = false)
        {
            if (parallel)
            {
                Parallel.ForEach(Partitioner.Create(0, field.Count), range => Body(range.Item1, range.Item2));
            }
            else
            {
                Body(0, field.Count);
            }

            void Body(int from, int to)
            {
                var vals = field.Values;
                int nx   = field.CountX;
                int ny   = field.CountY;
                int nz   = field.CountZ;
                int nxy  = field.CountXY;

                (double dx, double dy, double dz) = field.Scale;
                dx = 1.0 / Math.Abs(dx);
                dy = 1.0 / Math.Abs(dy);
                dz = 1.0 / Math.Abs(dz);

                (int di, int dj, int dk) = field.GetBoundaryOffsets();
                (int i, int j, int k)    = field.IndicesAt(from);

                for (int index = from; index < to; index++, i++)
                {
                    if (i == nx)
                    {
                        j++; i = 0;
                    }
                    if (j == ny)
                    {
                        k++; j = 0;
                    }

                    double value = vals[index];
                    double sum = 0.0;
                    double m, md;

                    //-x
                    m  = ((i == 0) ? vals[index + di] : vals[index - 1]) - value;
                    md = Math.Abs(m * dx) - slope;
                    if (md > 0.0)
                    {
                        sum += Math.Sign(m) * md;
                    }

                    //+x
                    m  = ((i == nx - 1) ? vals[index - di] : vals[index + 1]) - value;
                    md = Math.Abs(m * dx) - slope;
                    if (md > 0.0)
                    {
                        sum += Math.Sign(m) * md;
                    }

                    //-y
                    m  = ((j == 0) ? vals[index + dj] : vals[index - nx]) - value;
                    md = Math.Abs(m * dy) - slope;
                    if (md > 0.0)
                    {
                        sum += Math.Sign(m) * md;
                    }

                    //+y
                    m  = ((j == ny - 1) ? vals[index - dj] : vals[index + nx]) - value;
                    md = Math.Abs(m * dy) - slope;
                    if (md > 0.0)
                    {
                        sum += Math.Sign(m) * md;
                    }

                    //-z
                    m  = (k == 0) ? vals[index + dk] - value : vals[index - nxy] - value;
                    md = Math.Abs(m * dz) - slope;
                    if (md > 0.0)
                    {
                        sum += Math.Sign(m) * md;
                    }

                    //+z
                    m  = ((k == nz - 1) ? vals[index - dk] : vals[index + nxy]) - value;
                    md = Math.Abs(m * dz) - slope;
                    if (md > 0.0)
                    {
                        sum += Math.Sign(m) * md;
                    }

                    deltas[index] += sum * rate;
                }
            }
        }