Пример #1
0
        public static NCGrid_Distribution from_file(string title, bool using_full_path)
        {
            string path = Paths.DistributionRepo + "\\" + title + ".dist";

            if (using_full_path)
            {
                path = title;
            }
            string[]  contents   = File.ReadAllLines(path);
            int       length     = contents.Length;
            RBounds2D new_bounds = RBounds2D.fromstring(contents[0]);

            string[] numbers = contents[1].Split(':');
            int      nx, ny;

            if (!(int.TryParse(numbers[1], out nx) && int.TryParse(numbers[3], out ny)))
            {
                throw new Exception("File format error");
            }
            NCGrid_Distribution created = new NCGrid_Distribution(new_bounds, nx, ny);

            for (int q = additional_file_lines; q < contents.Length; q++)
            {
                int qprime = q - additional_file_lines;
                int i      = qprime % nx;
                int j      = (qprime - i) / nx;
                created[i, j] = NCAMRNode.fromstring(contents[q]);
            }
            return(created);
        }
Пример #2
0
        public static NCGrid_Distribution NiceFunction(string vb_syntax_eval, RBounds2D bounds, int xcount, int ycount, bool keepfile, string title)
        {
            ExactFunctionGeneratorVB2D.GenerateFunction(vb_syntax_eval, bounds, xcount, ycount, title);
            NCGrid_Distribution n = NCGrid_Distribution.from_file(title, false);

            return(n);
        }
Пример #3
0
        private static double area_of_influence_region(NCGrid_Distribution A, int i, int j)
        {
            int imax = A.Xcount - 1;
            int jmax = A.Ycount - 1;

            double[]    x    = new double[4];
            double[]    y    = new double[4];
            NCAMRNode[] grid =
            {
                A.GetFake(i - 1, j + 1),
                A.GetFake(i,     j + 1),
                A.GetFake(i + 1, j + 1),
                A.GetFake(i - 1, j),
                A.GetFake(i,     j),
                A.GetFake(i + 1, j),
                A.GetFake(i - 1, j - 1),
                A.GetFake(i,     j - 1),
                A.GetFake(i + 1, j - 1)
            };
            NCAMRNode[] poly =
            {
                xyavg(grid[3], grid[4], grid[6], grid[7]),
                xyavg(grid[4], grid[5], grid[7], grid[8]),
                xyavg(grid[1], grid[2], grid[4], grid[5]),
                xyavg(grid[0], grid[1], grid[3], grid[4])
            };
            for (int z = 0; z < poly.Length; z++)
            {
                x[z] = poly[z].X;
                y[z] = poly[z].Y;
            }
            return(quad_area(x, y));
        }
Пример #4
0
        public BVPLinear2D(BoundaryConditions _conditions, LinearOperatorOrder2 _operator, NCGrid_Distribution _discretization)
        {
            lin_operator        = _operator;
            discretization      = _discretization;
            boundary_conditions = _conditions;
            bool[] all_necessary_compatibility_conditions =
            {
                boundary_conditions.Bounds.Xmax == discretization.Bounds.Xmax,
                boundary_conditions.Bounds.Xmin == discretization.Bounds.Xmin,
                boundary_conditions.Bounds.Ymax == discretization.Bounds.Ymax,
                boundary_conditions.Bounds.Ymin == discretization.Bounds.Ymin,
                boundary_conditions.Xcount == discretization.Xcount,
                boundary_conditions.Ycount == discretization.Ycount
            };
            bool compatible = true;

            foreach (bool i in all_necessary_compatibility_conditions)
            {
                compatible = compatible && i;
            }
            if (!compatible)
            {
                throw new Exception("Error: Boundary conditions and initial discretization are incompatible.");
            }
        }
Пример #5
0
        public static double NormDifference(NCGrid_Distribution A, NCGrid_Distribution B)
        {
            NCGrid_Distribution newgrid = new NCGrid_Distribution(A.Bounds, A.Xcount + 1, A.Ycount + 1);
            int       n    = newgrid.Ycount;
            int       m    = newgrid.Xcount;
            double    xmin = newgrid.Xmin;
            double    xmax = newgrid.Xmax;
            double    ymin = newgrid.Ymin;
            double    ymax = newgrid.Ymax;
            NCAMRNode ll   = new NCAMRNode(xmin, ymin, 0);
            NCAMRNode lr   = new NCAMRNode(xmax, ymin, 0);
            NCAMRNode ul   = new NCAMRNode(xmin, ymax, 0);
            NCAMRNode ur   = new NCAMRNode(xmax, ymax, 0);

            newgrid[0, 0]         = ll;
            newgrid[0, n - 1]     = ul;
            newgrid[m - 1, 0]     = lr;
            newgrid[m - 1, n - 1] = ur;
            for (int i = 0; i < A.Xcount - 1; i++)
            {
                newgrid[i + 1, 0]        = xyavg(A[i, 0], A[i + 1, 0]);
                newgrid[i + 1, A.Ycount] = xyavg(A[i, A.Ycount - 1], A[i + 1, A.Ycount - 1]);
            }
            for (int j = 0; j < A.Ycount - 1; j++)
            {
                newgrid[0, j + 1]        = xyavg(A[0, j], A[0, j + 1]);
                newgrid[A.Xcount, j + 1] = xyavg(A[A.Xcount - 1, j], A[A.Xcount - 1, j + 1]);
            }
            for (int i = 0; i < A.Xcount - 1; i++)
            {
                for (int j = 0; j < A.Ycount - 1; j++)
                {
                    newgrid[i + 1, j + 1] = xyavg(A[i, j], A[i + 1, j], A[i, j + 1], A[i + 1, j + 1]);
                }
            }
            double accumulator = 0;

            for (int i = 0; i < m - 1; i++)
            {
                for (int j = 0; j < n - 1; j++)
                {
                    NCAMRNode[] nodes =
                    {
                        newgrid[i,     j],
                        newgrid[i + 1, j],
                        newgrid[i + 1, j + 1],
                        newgrid[i,     j + 1]
                    };
                    double[] x = new double[nodes.Length];
                    double[] y = new double[nodes.Length];
                    for (int z = 0; z < nodes.Length; z++)
                    {
                        x[z] = nodes[z].X;
                        y[z] = nodes[z].Y;
                    }
                    accumulator += quad_area(x, y) * (A[i, j].Value - B[i, j].Value) * (A[i, j].Value - B[i, j].Value);
                }
            }
            return(Math.Sqrt(accumulator));
        }
Пример #6
0
 public DistributionSketch2D(NCGrid_Distribution _subject, DistributionSketchSettings S)
 {
     settings      = S;
     subject       = _subject;
     canvas        = new Bitmap(S.ImageWidth, S.ImageHeight);
     x_pixel_count = canvas.Width;
     y_pixel_count = canvas.Height;
     get_positions();
 }
Пример #7
0
        public static NCGrid_Distribution NiceFunction(string vb_syntax_eval, RBounds2D bounds, int xcount, int ycount)
        {
            ExactFunctionGeneratorVB2D.GenerateFunction(vb_syntax_eval, bounds, xcount, ycount, "temp");
            NCGrid_Distribution n = NCGrid_Distribution.from_file("temp", false);
            string to_delete      = Paths.DistributionRepo + "\\temp.dist";

            File.Delete(to_delete);
            return(n);
        }
        public static NCGrid_Distribution GenerateFunctionToGrid(string vb_eval, NCGrid_Distribution discretization)
        {
            string absolute_temp_path = Paths.DistributionRepo + "\\temp.dist";

            GenerateFunction(vb_eval, discretization, "temp");
            NCGrid_Distribution output = NCGrid_Distribution.from_file("temp", false);

            File.Delete(absolute_temp_path);
            return(output);
        }
Пример #9
0
 public void MimicMorph(NCGrid_Distribution template)
 {
     for (int i = 0; i < x_node_count; i++)
     {
         for (int j = 0; j < y_node_count; j++)
         {
             this[i, j].X = template[i, j].X;
             this[i, j].Y = template[i, j].Y;
         }
     }
 }
Пример #10
0
        static void run_presentation_transient(int numframes)
        {
            double              t           = 0;
            double              dt          = 0.1;
            int                 n           = 25;
            double              L           = 10;
            double              H           = 10;
            RBounds2D           domain      = new RBounds2D(0, L, 0, H);
            NCGrid_Distribution soln        = new NCGrid_Distribution(domain, n, n);
            double              max1        = 4;
            double              max2        = 3;
            double              max3        = 5;
            double              max4        = 2;
            double              omega1      = 1;
            double              omega2      = 0.6;
            double              omega3      = 0.2;
            double              omega4      = 1.2;
            double              phi1        = -0.4;
            double              phi2        = -0.9;
            double              phi3        = 1.3;
            double              phi4        = 0.2;
            double              dx          = L / (n - 1);
            double              dy          = H / (n - 1);
            double              refinestep  = 0.1;
            int                 refinecount = 3;

            for (int i = 0; i < numframes; i++)
            {
                BoundaryConditions cond = new BoundaryConditions(soln, BoundaryConditions.BoundaryConditionType.Dirichlet);
                t += dt;
                for (int q = 0; q < n; q++)
                {
                    double x = q * dx;
                    double y = q * dy;
                    cond[q, BoundaryConditions.Direction.Negative_X] = max1 * Math.Sin(4 * Math.PI * x / L) * Math.Sin(omega1 * t - phi1);
                    cond[q, BoundaryConditions.Direction.Negative_Y] = max2 * Math.Sin(2 * Math.PI * y / L) * Math.Sin(omega2 * t - phi2);
                    cond[q, BoundaryConditions.Direction.Positive_X] = max3 * Math.Sin(3 * Math.PI * x / L) * Math.Sin(omega3 * t - phi3);
                    cond[q, BoundaryConditions.Direction.Positive_Y] = max4 * Math.Sin(5 * Math.PI * y / L) * Math.Sin(omega4 * t - phi4);
                }
                BVPLinear2D         problem = new BVPLinear2D(cond, LinearOperatorOrder2.Laplace, soln);
                NCGrid_Distribution sol     = problem.SolveKaczMarzExt(100, 10, 20);
                sol.ApplyMeshMorphGA(refinestep);
                for (int z = 0; z < refinecount - 1; z++)
                {
                    problem = new BVPLinear2D(cond, LinearOperatorOrder2.Laplace, soln);
                    sol     = problem.Solve(Matrix.SystemSolvingScheme.Kaczmarz);
                    sol.ApplyMeshMorphGA(refinestep);
                }
                sol.WriteToFile("longtransient_" + i.ToString());
                sol.QuickSketch("sol_" + bufferint(i, 4));
            }
        }
Пример #11
0
        public NCGrid_Distribution solve_iterative_morph(int max_morph_count, double size)
        {
            NCGrid_Distribution soln = Solve();

            for (int i = 0; i < max_morph_count - 1; i++)
            {
                soln.QuickSketch("soln-" + i.ToString());
                soln.ApplyMeshMorphGA(size);
                discretization = soln.Clone();
                soln           = Solve();
            }
            return(soln);
        }
Пример #12
0
        public static NCGrid_Distribution operator *(double scale, NCGrid_Distribution A)
        {
            NCGrid_Distribution output = A.Clone();

            for (int i = 0; i < A.Xcount; i++)
            {
                for (int j = 0; j < A.Ycount; j++)
                {
                    output.assign_value_at(i, j, scale * A[i, j].Value);
                }
            }
            return(output);
        }
Пример #13
0
        public static NCGrid_Distribution CompareNiceClean(NCGrid_Distribution test, string vbs_test_function)
        {
            string title = "temp";
            string path  = Paths.DistributionRepo + "\\" + title + ".dist";

            ExactFunctionGeneratorVB2D.GenerateFunction(vbs_test_function, test, title);
            NCGrid_Distribution true_dist = NCGrid_Distribution.from_file(title, false);

            File.Delete(path);
            NCGrid_Distribution dif = test - true_dist;

            dif.force_extrema_update();
            return(dif);
        }
Пример #14
0
        public NCGrid_Distribution Clone()
        {
            NCGrid_Distribution n = new NCGrid_Distribution(bounds, x_node_count, y_node_count);

            for (int i = 0; i < x_node_count; i++)
            {
                for (int j = 0; j < y_node_count; j++)
                {
                    n.assign_value_at(i, j, this[i, j].Value);
                    n[i, j].X = this[i, j].X;
                    n[i, j].Y = this[i, j].Y;
                }
            }
            return(n);
        }
Пример #15
0
 public NavierStokesTransient(int timesteps, NCGrid_Distribution u_initial, NCGrid_Distribution v_initial, BoundaryConditions p, BoundaryConditions u, BoundaryConditions v, FluidProperties props, double dt)
 {
     deltat        = dt;
     properties    = props;
     current_ustar = new NCGrid_Distribution(u_initial.Bounds, u_initial.Xcount, u_initial.Ycount);
     current_vstar = new NCGrid_Distribution(v_initial.Bounds, v_initial.Xcount, v_initial.Ycount);
     current_ustar.SetConstant(1);
     current_vstar.SetConstant(1);
     time_steps = timesteps;
     p_boundary = p;
     u_boundary = u;
     v_boundary = v;
     initial_u  = u_initial;
     initial_v  = v_initial;
 }
Пример #16
0
        static void Main(string[] args)
        {
            NCGrid_Distribution        funcp    = new NCGrid_Distribution(new RBounds2D(0, 10, 0, 10), 400, 400);
            NCGrid_Distribution        func     = ExactFunctionGeneratorVB2D.GenerateFunctionToGrid("(-1*x) + (2*y)", funcp);
            DistributionSketchSettings settings = DistributionSketchSettings.Fancy();
            int n = 10;

            settings.XLabelCount    = n;
            settings.YLabelCount    = n;
            settings.XGridlineCount = n;
            settings.YGridlineCount = n;
            DistributionSketch2D sktch = new DistributionSketch2D(func, settings);

            sktch.CreateSketch(true);
            sktch.SaveImage(@"C:\Users\Will\Desktop\output.png", true);
        }
Пример #17
0
        public static NCGrid_Distribution MakeMagnitude(NCGrid_Distribution A, NCGrid_Distribution B)
        {
            NCGrid_Distribution output = A.Clone();

            for (int i = 0; i < A.Xcount; i++)
            {
                for (int j = 0; j < B.Ycount; j++)
                {
                    double x = A[i, j].Value;
                    double y = B[i, j].Value;
                    output.assign_value_at(i, j, Math.Sqrt((x * x) + (y * y)));
                }
            }
            output.force_extrema_update();
            return(output);
        }
Пример #18
0
        public void WriteAllDerivs(string title, bool enable_console_output, bool using_full_path, bool allow_overwrite)
        {
            DateTime            Then    = DateTime.Now;
            NCGrid_Distribution this_x  = new NCGrid_Distribution(bounds, this.Xcount, this.Ycount);
            NCGrid_Distribution this_y  = new NCGrid_Distribution(bounds, this.Xcount, this.Ycount);
            NCGrid_Distribution this_xx = new NCGrid_Distribution(bounds, this.Xcount, this.Ycount);
            NCGrid_Distribution this_yy = new NCGrid_Distribution(bounds, this.Xcount, this.Ycount);
            NCGrid_Distribution this_xy = new NCGrid_Distribution(bounds, this.Xcount, this.Ycount);

            for (int i = 1; i < this.Xcount - 1; i++)
            {
                for (int j = 1; j < this.Ycount - 1; j++)
                {
                    Matrix xi = EstimateSecondDerivs(i, j, SurplusNodeAccessingMode.UPPER_RIGHT);
                    this_x[i, j].Value  = xi[0];
                    this_y[i, j].Value  = xi[1];
                    this_xx[i, j].Value = xi[2];
                    this_yy[i, j].Value = xi[3];
                    this_xy[i, j].Value = xi[4];
                }
            }
            DateTime Now      = DateTime.Now;
            string   title_x  = title + "_x";
            string   title_y  = title + "_y";
            string   title_xx = title + "_xx";
            string   title_yy = title + "_yy";
            string   title_xy = title + "_xy";

            if (using_full_path)
            {
                string basestring = title.Split('.')[0];
                title_x  = basestring + "_x.dist";
                title_y  = basestring + "_y.dist";
                title_xx = basestring + "_xx.dist";
                title_yy = basestring + "_yy.dist";
                title_xy = basestring + "_xy.dist";
            }
            this_x.WriteToFile(title_x, enable_console_output, allow_overwrite, using_full_path);
            this_y.WriteToFile(title_y, enable_console_output, allow_overwrite, using_full_path);
            this_xx.WriteToFile(title_xx, enable_console_output, allow_overwrite, using_full_path);
            this_yy.WriteToFile(title_yy, enable_console_output, allow_overwrite, using_full_path);
            this_xy.WriteToFile(title_xy, enable_console_output, allow_overwrite, using_full_path);
            if (enable_console_output)
            {
                Console.WriteLine((Now - Then).TotalMilliseconds);
            }
        }
Пример #19
0
        public static void quickPlot(string vb_eval, string title, RBounds2D bounds)
        {
            int n = 100;

            GenerateFunction(vb_eval, bounds, n, n, title);
            NCGrid_Distribution p = NCGrid_Distribution.from_file(title, false);

            File.Delete(Paths.DistributionRepo + "\\" + title + ".dist");
            DistributionSketchSettings S = DistributionSketchSettings.Fancy();

            S.HasHeatmap = true;
            S.HasInfo    = false;
            S.Mode       = SketchMode.DISTRIBUTION_ONLY;
            DistributionSketch2D sk = new DistributionSketch2D(p, S);

            sk.CreateSketch(false);
            sk.SaveImage(title, false);
        }
Пример #20
0
        public static void GenerateFunction(string vb_syntax_eval, NCGrid_Distribution discretization, string name)
        {
            discretization.WriteToFile(@"C:\Users\Will\Desktop\Folders\MATH435\repo\visual-basic-templates\TEMPORARY.dist", false, true, true);
            string[] vbsraw    = File.ReadAllLines(script_disc_template);
            int      fileconst = discretization.Xcount * discretization.Ycount + 2;

            vbsraw[0]  = String.Format(vbsraw[0], discretization.Ycount);
            vbsraw[1]  = String.Format(vbsraw[1], discretization.Xcount);
            vbsraw[16] = String.Format(vbsraw[16], fileconst);
            vbsraw[13] = String.Format(vbsraw[13], fileconst);
            vbsraw[2]  = String.Format(vbsraw[2], name);
            vbsraw[28] = String.Format(vbsraw[28], vb_syntax_eval);
            string newname = @"C:\Users\Will\Desktop\Folders\MATH435\repo\visual-basic-templates\TEMPORARY.vbs";

            File.WriteAllLines(newname, vbsraw);
            run_script(newname);
            File.Delete(@"C:\Users\Will\Desktop\Folders\MATH435\repo\visual-basic-templates\TEMPORARY.dist");
        }
Пример #21
0
        public NCGrid_Distribution SolveSRDD()
        {
            Matrix system_matrix, RHS;
            NCGrid_Distribution dist = discretization.Clone();

            dist.ApplyBoundary(boundary_conditions);
            buildsystem(out system_matrix, out RHS);
            Matrix results = RunExteriorSolve(system_matrix, RHS);

            for (int i = 0; i < RHS.Rows; i++)
            {
                int offset = discretization.Ycount - 2;
                int J      = i % offset;
                int I      = (i - J) / offset;
                dist.assign_value_at(I + 1, J + 1, results[i]);
            }
            return(dist);
        }
Пример #22
0
        public static bool check_compatibility(BoundaryConditions B, NCGrid_Distribution C)
        {
            bool[] stuff =
            {
                B.Bounds.Xmax == C.Xmax,
                B.Bounds.Xmin == C.Xmin,
                B.Bounds.Ymax == C.Ymax,
                B.Bounds.Ymin == C.Ymin,
                B.Ycount == C.Ycount,
                B.Xcount == C.Xcount
            };
            bool valid = true;

            foreach (bool i in stuff)
            {
                valid = valid && i;
            }
            return(valid);
        }
Пример #23
0
 public static NCGrid_Distribution operator /(NCGrid_Distribution A, NCGrid_Distribution B)
 {
     if (A.Xcount == B.Xcount && A.Ycount == B.Ycount)
     {
         NCGrid_Distribution C = new NCGrid_Distribution(A.Bounds, A.Xcount, B.Ycount);
         for (int i = 0; i < A.Xcount; i++)
         {
             for (int j = 0; j < A.Ycount; j++)
             {
                 C[i, j]       = A[i, j].Clone();
                 C[i, j].Value = A[i, j].Value / B[i, j].Value;
             }
         }
         return(C);
     }
     else
     {
         throw new Exception("Dimensions do not agree.");
     }
 }
Пример #24
0
 public BoundaryConditions(NCGrid_Distribution boundary_of, BoundaryConditionType _type)
 {
     bounds = boundary_of.Bounds;
     SetAllBoundaryConditionTypes(_type);
     xcount          = boundary_of.Xcount;
     ycount          = boundary_of.Ycount;
     positive_x_vals = new double[xcount];
     positive_y_vals = new double[xcount];
     negative_x_vals = new double[ycount];
     negative_y_vals = new double[ycount];
     for (int i = 0; i < xcount; i++)
     {
         positive_x_vals[i] = boundary_of[i, ycount - 1].Value;
         negative_x_vals[i] = boundary_of[i, 0].Value;
     }
     for (int j = 0; j < ycount; j++)
     {
         positive_y_vals[j] = boundary_of[xcount - 1, j].Value;
         negative_y_vals[j] = boundary_of[0, j].Value;
     }
 }
Пример #25
0
 public DistributionSketch3DBasic(NCGrid_Distribution sub)
 {
     subject = sub;
     output  = new Bitmap(2000, 2000);
 }
Пример #26
0
        public NCGrid_Distribution SolveSlow()
        {
            int m           = discretization.Xcount;
            int n           = discretization.Ycount;
            int total_nodes = (m - 2) * (n - 2);
            //NCGrid_Distribution dist = new NCGrid_Distribution(boundary_conditions.Bounds, discretization.Xcount, discretization.Ycount);
            NCGrid_Distribution dist = discretization.Clone();

            dist.ApplyBoundary(boundary_conditions);
            Matrix        system_matrix = new Matrix(total_nodes, total_nodes);
            Matrix        RHS           = new Matrix(total_nodes, 1);
            int           neg_y         = 0;
            int           pos_y         = 0;
            int           neg_x         = 0;
            int           pos_x         = 0;
            int           body          = 0;
            int           ll_corner     = 0;
            int           lr_corner     = 0;
            int           ur_corner     = 0;
            int           ul_corner     = 0;
            List <string> indices       = new List <string>();

            if (console_output)
            {
                Console.WriteLine("Populating linear system...");
            }
            for (int i = 1; i < m - 1; i++)
            {
                if (console_output && i % (m - 1) / 13 == 0)
                {
                    Console.WriteLine((100 * i / (m - 1)).ToString() + "%");
                }
                for (int j = 1; j < n - 1; j++)
                {
                    double rhs_here = 0;
                    indices.Add(i.ToString() + "," + j.ToString());
                    //for now, assume zero forcing function.
                    BoundaryCase _case;
                    bool         interior     = !isCloseToBoundary(i, j, m, n, out _case);
                    int          surplusi     = 1;
                    int          surplusj     = 1;
                    Matrix       b            = dist.GetTaylorSystemCoeffs(i, j, surplusi, surplusj);
                    double       uijterm      = 0;
                    double       ui1jterm     = 0;
                    double       uij1term     = 0;
                    double       ui_1jterm    = 0;
                    double       uij_1term    = 0;
                    double       usurplusterm = 0;
                    int          row          = (m - 2) * (i - 1) + j - 1;
                    for (int h = 0; h < 5; h++)
                    {
                        ui1jterm     += b[h, 0] * lin_operator[h];
                        uij1term     += b[h, 1] * lin_operator[h];
                        ui_1jterm    += b[h, 2] * lin_operator[h];
                        uij_1term    += b[h, 3] * lin_operator[h];
                        usurplusterm += b[h, 4] * lin_operator[h];
                        double temp_ij = 0;
                        for (int k = 0; k < 5; k++)
                        {
                            temp_ij += b[h, k];
                        }
                        uijterm += lin_operator[h] * temp_ij;
                    }
                    switch (_case)
                    {
                    case BoundaryCase.NegativeYBoundary:
                    {
                        rhs_here -= dist[i, j - 1].Value * uij_1term;
                        uij_1term = 0;
                        neg_y++;
                        break;
                    }

                    case BoundaryCase.PositiveYBoundary:
                    {
                        rhs_here    -= dist[i, j + 1].Value * uij1term;
                        rhs_here    -= dist[i + surplusi, j + surplusj].Value * usurplusterm;
                        usurplusterm = 0;
                        uij1term     = 0;
                        pos_y++;
                        break;
                    }

                    case BoundaryCase.NegativeXBoundary:
                    {
                        rhs_here -= dist[i - 1, j].Value * ui_1jterm;
                        ui_1jterm = 0;
                        neg_x++;
                        break;
                    }

                    case BoundaryCase.PositiveXBoundary:
                    {
                        rhs_here    -= dist[i + 1, j].Value * ui1jterm;
                        rhs_here    -= dist[i + surplusi, j + surplusj].Value * usurplusterm;
                        usurplusterm = 0;
                        ui1jterm     = 0;
                        pos_x++;
                        break;
                    }

                    case BoundaryCase.ULCorner:
                    {
                        rhs_here    -= dist[i, j + 1].Value * uij1term;
                        rhs_here    -= dist[i - 1, j].Value * ui_1jterm;
                        rhs_here    -= dist[i + surplusi, j + surplusj].Value * usurplusterm;
                        uij1term     = 0;
                        ui_1jterm    = 0;
                        usurplusterm = 0;
                        ul_corner++;
                        break;
                    }

                    case BoundaryCase.LLCorner:
                    {
                        rhs_here -= dist[i - 1, j].Value * ui_1jterm;
                        rhs_here -= dist[i, j - 1].Value * uij_1term;
                        ui_1jterm = 0;
                        uij_1term = 0;
                        ll_corner++;
                        break;
                    }

                    case BoundaryCase.URCorner:
                    {
                        rhs_here    -= dist[i + 1, j].Value * ui1jterm;
                        rhs_here    -= dist[i, j + 1].Value * uij1term;
                        rhs_here    -= dist[i + surplusi, j + surplusj].Value * usurplusterm;
                        ui1jterm     = 0;
                        uij1term     = 0;
                        usurplusterm = 0;
                        ur_corner++;
                        break;
                    }

                    case BoundaryCase.LRCorner:
                    {
                        rhs_here    -= dist[i, j - 1].Value * uij_1term;
                        rhs_here    -= dist[i + 1, j].Value * ui1jterm;
                        rhs_here    -= dist[i + surplusj, j + surplusj].Value * usurplusterm;
                        uij_1term    = 0;
                        ui1jterm     = 0;
                        usurplusterm = 0;
                        lr_corner++;
                        break;
                    }

                    case BoundaryCase.Body:
                    {
                        body++;
                        break;
                    }
                    }
                    system_matrix[row, map2Dto1D(i - 1, j - 1, m - 1, n - 1)] = -1 * uijterm;
                    if (ui1jterm != 0)
                    {
                        system_matrix[row, map2Dto1D(i, j - 1, m - 1, n - 1)] = ui1jterm;
                    }
                    if (uij1term != 0)
                    {
                        system_matrix[row, map2Dto1D(i - 1, j, m - 1, n - 1)] = uij1term;
                    }
                    if (ui_1jterm != 0)
                    {
                        system_matrix[row, map2Dto1D(i - 2, j - 1, m - 1, n - 1)] = ui_1jterm;
                    }
                    if (uij_1term != 0)
                    {
                        system_matrix[row, map2Dto1D(i - 1, j - 2, m - 1, n - 1)] = uij_1term;
                    }
                    if (usurplusterm != 0)
                    {
                        system_matrix[row, map2Dto1D(i - 1 + surplusi, j - 1 + surplusj, m - 1, n - 1)] = usurplusterm;
                    }
                    RHS[row] = rhs_here;
                }
            }
            if (console_output)
            {
                Console.WriteLine("System populated.");
            }
            Matrix results = Matrix.solve_system(system_matrix, RHS, Matrix.SystemSolvingScheme.Basic, true, true);

            for (int i = 0; i < total_nodes; i++)
            {
                int offset = n - 2;
                int J      = i % offset;
                int I      = (i - J) / offset;
                dist.assign_value_at(I + 1, J + 1, results[i]);
            }
            return(dist);
        }
Пример #27
0
 public HybridDistributionSketch2D(NCGrid_Distribution subject, DistributionSketchSettings S) : base(subject, S)
 {
     has_superimposed = false;
 }
Пример #28
0
 public void set_superimposed_grid(NCGrid_Distribution _super)
 {
     has_superimposed = true;
     superimpose_grid = _super;
 }
Пример #29
0
        private void buildsystem(out Matrix system_matrix, out Matrix RHS)
        {
            int m                    = discretization.Xcount;
            int n                    = discretization.Ycount;
            int total_nodes          = (m - 2) * (n - 2);
            NCGrid_Distribution dist = discretization.Clone();

            dist.ApplyBoundary(boundary_conditions);
            system_matrix = new Matrix(total_nodes, total_nodes);
            RHS           = new Matrix(total_nodes, 1);
            int neg_y     = 0;
            int pos_y     = 0;
            int neg_x     = 0;
            int pos_x     = 0;
            int body      = 0;
            int ll_corner = 0;
            int lr_corner = 0;
            int ur_corner = 0;
            int ul_corner = 0;

            if (console_output)
            {
                Console.WriteLine("Populating linear system...");
            }
            for (int i = 1; i < m - 1; i++)
            {
                if (console_output && i % (m - 1) / 13 == 0)
                {
                    Console.WriteLine((100 * i / (m - 1)).ToString() + "%");
                }
                for (int j = 1; j < n - 1; j++)
                {
                    double rhs_here = 0;
                    //for now, assume zero forcing function.
                    BoundaryCase _case;
                    bool         interior     = !isCloseToBoundary(i, j, m, n, out _case);
                    int          surplusi     = 1;
                    int          surplusj     = 1;
                    Matrix       b            = dist.GetTaylorSystemCoeffs(i, j, surplusi, surplusj);
                    double       uijterm      = 0;
                    double       ui1jterm     = 0;
                    double       uij1term     = 0;
                    double       ui_1jterm    = 0;
                    double       uij_1term    = 0;
                    double       usurplusterm = 0;
                    int          row          = (m - 2) * (i - 1) + j - 1;
                    for (int h = 0; h < 5; h++)
                    {
                        ui1jterm     += b[h, 0] * lin_operator[h];
                        uij1term     += b[h, 1] * lin_operator[h];
                        ui_1jterm    += b[h, 2] * lin_operator[h];
                        uij_1term    += b[h, 3] * lin_operator[h];
                        usurplusterm += b[h, 4] * lin_operator[h];
                        double temp_ij = 0;
                        for (int k = 0; k < 5; k++)
                        {
                            temp_ij += b[h, k];
                        }
                        uijterm += lin_operator[h] * temp_ij;
                    }
                    switch (_case)
                    {
                    case BoundaryCase.NegativeYBoundary:
                    {
                        rhs_here -= dist[i, j - 1].Value * uij_1term;
                        uij_1term = 0;
                        neg_y++;
                        break;
                    }

                    case BoundaryCase.PositiveYBoundary:
                    {
                        rhs_here    -= dist[i, j + 1].Value * uij1term;
                        rhs_here    -= dist[i + surplusi, j + surplusj].Value * usurplusterm;
                        usurplusterm = 0;
                        uij1term     = 0;
                        pos_y++;
                        break;
                    }

                    case BoundaryCase.NegativeXBoundary:
                    {
                        rhs_here -= dist[i - 1, j].Value * ui_1jterm;
                        ui_1jterm = 0;
                        neg_x++;
                        break;
                    }

                    case BoundaryCase.PositiveXBoundary:
                    {
                        rhs_here    -= dist[i + 1, j].Value * ui1jterm;
                        rhs_here    -= dist[i + surplusi, j + surplusj].Value * usurplusterm;
                        usurplusterm = 0;
                        ui1jterm     = 0;
                        pos_x++;
                        break;
                    }

                    case BoundaryCase.ULCorner:
                    {
                        rhs_here    -= dist[i, j + 1].Value * uij1term;
                        rhs_here    -= dist[i - 1, j].Value * ui_1jterm;
                        rhs_here    -= dist[i + surplusi, j + surplusj].Value * usurplusterm;
                        uij1term     = 0;
                        ui_1jterm    = 0;
                        usurplusterm = 0;
                        ul_corner++;
                        break;
                    }

                    case BoundaryCase.LLCorner:
                    {
                        rhs_here -= dist[i - 1, j].Value * ui_1jterm;
                        rhs_here -= dist[i, j - 1].Value * uij_1term;
                        ui_1jterm = 0;
                        uij_1term = 0;
                        ll_corner++;
                        break;
                    }

                    case BoundaryCase.URCorner:
                    {
                        rhs_here    -= dist[i + 1, j].Value * ui1jterm;
                        rhs_here    -= dist[i, j + 1].Value * uij1term;
                        rhs_here    -= dist[i + surplusi, j + surplusj].Value * usurplusterm;
                        ui1jterm     = 0;
                        uij1term     = 0;
                        usurplusterm = 0;
                        ur_corner++;
                        break;
                    }

                    case BoundaryCase.LRCorner:
                    {
                        rhs_here    -= dist[i, j - 1].Value * uij_1term;
                        rhs_here    -= dist[i + 1, j].Value * ui1jterm;
                        rhs_here    -= dist[i + surplusj, j + surplusj].Value * usurplusterm;
                        uij_1term    = 0;
                        ui1jterm     = 0;
                        usurplusterm = 0;
                        lr_corner++;
                        break;
                    }

                    case BoundaryCase.Body:
                    {
                        body++;
                        break;
                    }
                    }
                    system_matrix[row, map2Dto1D(i - 1, j - 1, m - 1, n - 1)] = -1 * uijterm;
                    if (ui1jterm != 0)
                    {
                        system_matrix[row, map2Dto1D(i, j - 1, m - 1, n - 1)] = ui1jterm;
                    }
                    if (uij1term != 0)
                    {
                        system_matrix[row, map2Dto1D(i - 1, j, m - 1, n - 1)] = uij1term;
                    }
                    if (ui_1jterm != 0)
                    {
                        system_matrix[row, map2Dto1D(i - 2, j - 1, m - 1, n - 1)] = ui_1jterm;
                    }
                    if (uij_1term != 0)
                    {
                        system_matrix[row, map2Dto1D(i - 1, j - 2, m - 1, n - 1)] = uij_1term;
                    }
                    if (usurplusterm != 0)
                    {
                        system_matrix[row, map2Dto1D(i - 1 + surplusi, j - 1 + surplusj, m - 1, n - 1)] = usurplusterm;
                    }
                    RHS[row] = rhs_here;
                }
            }
            if (console_output)
            {
                Console.WriteLine("System populated.");
            }
        }
Пример #30
0
        public override void create_animation(string output_title, bool enable_console_output, bool using_full_path, bool allow_overwrite, bool use_floating_color_scale, DistributionSketchSettings S)
        {
            string absolute_video_output_path = video_default_repo + "\\" + output_title + ".avi";

            if (using_full_path)
            {
                absolute_video_output_path = output_title;
            }
            if (File.Exists(absolute_video_output_path) && !allow_overwrite)
            {
                throw new Exception("Error: File exists and overwrite permission not granted.");
            }
            Directory.CreateDirectory(AbsoluteDataSetLocation + "\\images");
            string[]      allfiles      = Directory.GetFiles(absolute_set_location);
            List <string> distrib_files = new List <string>();
            int           q             = 0;

            foreach (string i in allfiles)
            {
                if (i.EndsWith(".dist"))
                {
                    distrib_files.Add(i);
                    NCGrid_Distribution n = NCGrid_Distribution.from_file(i, true);
                    NCGrid_Distribution following_grid = new NCGrid_Distribution(n.Bounds, n.Xcount, n.Ycount);
                    int border = 1;
                    for (int ii = border; ii < following_grid.Xcount - border; ii++)
                    {
                        for (int jj = border; jj < following_grid.Ycount - border; jj++)
                        {
                            double   deltax  = (n.Xmax - n.Xmin) / (n.Xcount - 1);
                            double   deltay  = (n.Ymax - n.Ymin) / (n.Ycount - 1);
                            double[] stencil =
                            {
                                n[ii - 1, jj - 1].Value,
                                n[ii,     jj - 1].Value,
                                n[ii + 1, jj - 1].Value,
                                n[ii - 1, jj].Value,
                                n[ii,     jj].Value,
                                n[ii + 1, jj].Value,
                                n[ii - 1, jj + 1].Value,
                                n[ii,     jj + 1].Value,
                                n[ii + 1, jj + 1].Value,
                            };
                            double  ux   = (stencil[5] - stencil[3]) / (2 * deltax);
                            double  uy   = (stencil[7] - stencil[1]) / (2 * deltay);
                            double  uxx  = (stencil[5] + stencil[3] - (2 * stencil[4])) / (deltax * deltax);
                            double  uyy  = (stencil[7] + stencil[1] - (2 * stencil[4])) / (deltay * deltay);
                            double  uxy  = (stencil[8] + stencil[0] - stencil[6] - stencil[2]) / (4 * deltax * deltay);
                            double  dx   = 0.06 * ((uxx * ux) + (uxy * uy));
                            double  dy   = 0.06 * ((uyy * uy) + (ux * uxy));
                            Vector3 move = new Vector3(dx, dy, 0);
                            while ((following_grid[ii, jj] + move).X > n.Bounds.Xmax || (following_grid[ii, jj] + move).X <n.Bounds.Xmin || (following_grid[ii, jj] + move).Y> n.Bounds.Ymax || (following_grid[ii, jj] + move).Y < n.Bounds.Ymin)
                            {
                                dx   = 0.5 * dx;
                                dy   = 0.5 * dy;
                                move = new Vector3(dx, dy, 0);
                            }
                            following_grid[ii, jj] = following_grid[ii, jj] + move;
                        }
                    }
                    HybridDistributionSketch2D sk = new HybridDistributionSketch2D(n, S);
                    if (!use_floating_color_scale)
                    {
                        sk.override_colormap_limits(info);
                    }
                    sk.set_superimposed_grid(following_grid);
                    sk.CreateSketch(true);
                    sk.SaveImage(AbsoluteDataSetLocation + "\\images\\render" + bufferint(q++, 5) + ".bmp", true);
                }
            }
            AVIWriter V = new AVIWriter("wmv3");

            string[]      filenames = Directory.GetFiles(AbsoluteDataSetLocation + "\\images");
            List <string> bmpnames  = new List <string>();

            foreach (string i in filenames)
            {
                if (i.EndsWith(".bmp"))
                {
                    bmpnames.Add(i);
                }
            }
            if (bmpnames.Count != 0)
            {
                Bitmap first = (Bitmap)Bitmap.FromFile(bmpnames[0]);
                V.Open(absolute_video_output_path, first.Width, first.Height);
                V.FrameRate = 50;
                V.AddFrame(first);
                int ct = bmpnames.Count;
                for (int i = 1; i < bmpnames.Count; i++)
                {
                    Bitmap butt = (Bitmap)Bitmap.FromFile(bmpnames[i]);
                    if (enable_console_output)
                    {
                        Console.WriteLine(i.ToString() + " of " + ct.ToString() + " frames stacked" + "(" + (i * 100 / ct).ToString() + "%)");
                    }
                    V.AddFrame(butt);
                    butt.Dispose();
                }
                V.Close();
                first.Dispose();
                if (enable_console_output)
                {
                    Console.WriteLine("AVI successfully created on " + DateTime.Now.ToString() + " in directory " + absolute_video_output_path);
                }
                string[] imagenames = Directory.GetFiles(AbsoluteDataSetLocation + "\\images");
                foreach (string g in imagenames)
                {
                    File.Delete(g);
                }
                Directory.Delete(AbsoluteDataSetLocation + "\\images");
            }
            else
            {
                Console.WriteLine("No bitmap images found in the current directory.");
            }
        }