Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        public NCGrid_Distribution Solve()
        {
            int m                    = discretization.Xcount;
            int n                    = discretization.Ycount;
            int total_nodes          = (m - 2) * (n - 2);
            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;

            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.");
            }
            Matrix results = RunExteriorSolve(system_matrix, RHS);

            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);
        }
Ejemplo n.º 4
0
        static void testfunction()
        {
            Console.WriteLine("Press enter to begin, or enter \"c\" to clear repos.");
            if (Console.ReadLine() == "c")
            {
                RepoManagement.ClearRepo(Paths.DistributionRepo, Paths.ImageRepo);
            }
            DateTime then = DateTime.Now;

            Console.WriteLine("Solving...");
            double              L          = 10;
            double              H          = 10;
            int                 n          = 38;
            RBounds2D           bounds     = new RBounds2D(0, L, 0, H);
            NCGrid_Distribution dist       = new NCGrid_Distribution(bounds, n, n);
            BoundaryConditions  conditions = new BoundaryConditions(bounds, n, n, BoundaryConditions.BoundaryConditionType.Dirichlet);
            double              dx         = L / (n - 1);
            double              max        = 5;
            string              fxn        = string.Format("{0}*Sin({1}*x/{2})*(Exp({1}*y/{2}) - Exp(-1*{1}*y/{2}))/(Exp({1}*{3}/{2}) - Exp(-1*{1}*{3}/{2}))", max, Math.PI, L, H);

            conditions.SetConstant(0, BoundaryConditions.Direction.Negative_X);
            conditions.SetConstant(0, BoundaryConditions.Direction.Negative_Y);
            conditions.SetConstant(0, BoundaryConditions.Direction.Positive_Y);
            for (int i = 0; i < n; i++)
            {
                double x = i * dx;
                double z = max * Math.Sin(Math.PI * x / L);
                conditions[i, BoundaryConditions.Direction.Positive_Y] = z;
            }
            int solcount = 50;

            double[] errors              = new double[solcount];
            double[] iteration           = new double[solcount];
            DistributionSketchSettings S = DistributionSketchSettings.Fancy();

            S.SetFigureTitle("Temperature Distribution");
            for (int i = 0; i < solcount; i++)
            {
                Console.WriteLine(i.ToString() + " of " + solcount.ToString() + " iterations processed.");
                iteration[i] = i;
                BVPLinear2D problem = new BVPLinear2D(conditions, LinearOperatorOrder2.Laplace, dist);
                problem.EnableConsoleOutput();
                NCGrid_Distribution soln     = problem.Solve(Matrix.SystemSolvingScheme.Kaczmarz);
                NCGrid_Distribution analytic = ExactFunctionGeneratorVB2D.GenerateFunctionToGrid(fxn, soln);
                soln.ApplyMeshMorphGA(15);
                errors[i] = ErrorEstimation.NormDifference(soln, analytic);
                string title = "iterative-" + i.ToString();
                soln.WriteToFile(title);
                DistributionSketch2D sketch = new DistributionSketch2D(soln, S);
                dist = soln.Clone();
                sketch.CreateSketch(true);
                sketch.SaveImage(title + "-plot", false);
            }
            List <string> filestuff = new List <string>();

            for (int i = 0; i < iteration.Length; i++)
            {
                filestuff.Add(iteration[i].ToString() + "," + errors[i].ToString());
            }
            File.WriteAllLines(@"C:\Users\Will\Desktop\Folders\MATH435\repo\curves-1d\errors-temp.csv", filestuff.ToArray());
            Console.WriteLine("Done.");
            Console.ReadLine();
        }
Ejemplo n.º 5
0
        static void testfunction3()
        {
            Console.WriteLine("Press enter to begin, or enter \"c\" to clear repos.");
            if (Console.ReadLine() == "c")
            {
                RepoManagement.ClearRepo(Paths.DistributionRepo, Paths.ImageRepo);
            }
            DateTime then = DateTime.Now;

            Console.WriteLine("Solving...");
            double              L          = 10;
            double              H          = 10;
            int                 n          = 26;
            RBounds2D           bounds     = new RBounds2D(0, L, 0, H);
            NCGrid_Distribution dist       = new NCGrid_Distribution(bounds, n, n);
            BoundaryConditions  conditions = new BoundaryConditions(bounds, n, n, BoundaryConditions.BoundaryConditionType.Dirichlet);
            double              dx         = L / (n - 1);
            double              omega      = 4;
            double              max        = 8;

            conditions.SetConstant(0, BoundaryConditions.Direction.Negative_X);
            conditions.SetConstant(0, BoundaryConditions.Direction.Negative_Y);
            for (int i = 0; i < n; i++)
            {
                double x      = i * dx;
                double y      = i * dx;
                double ybound = max / (1 + Math.Exp(-1 * (omega * x / L)));
                double xbound = max / (1 + Math.Exp(-1 * (omega * y / H)));
                conditions[i, BoundaryConditions.Direction.Positive_Y] = ybound;
                conditions[i, BoundaryConditions.Direction.Positive_X] = xbound;
            }
            int solcount = 15;
            LinearOperatorOrder2       op = LinearOperatorOrder2.Laplace;
            DistributionSketchSettings S  = DistributionSketchSettings.Fancy();

            S.SetFigureTitle("Double Logistic Boundary");
            for (int i = 0; i < solcount; i++)
            {
                Console.WriteLine(i.ToString() + " of " + solcount.ToString() + " iterations processed.");
                BVPLinear2D problem = new BVPLinear2D(conditions, op, dist);
                problem.EnableConsoleOutput();
                NCGrid_Distribution soln = problem.SolveSRDD();
                string title             = "iterative-" + i.ToString();
                soln.WriteToFile(title);
                DistributionSketch2D sketch = new DistributionSketch2D(soln, S);
                dist = soln.Clone();
                sketch.CreateSketch(true);
                sketch.SaveImage(title + "-plot", false);
                //dist.ApplyMeshMorphGA(2, 0.0019);
                Random R = new Random();
                for (int h = 1; h < dist.Xcount - 1; h++)
                {
                    for (int k = 1; k < dist.Ycount - 1; k++)
                    {
                        double  ddx  = (0.5 - R.NextDouble()) * dx * 0.6;
                        double  ddy  = (0.5 - R.NextDouble()) * dx * 0.6;
                        Vector3 move = new Vector3(ddx, ddy, 0);
                        dist[h, k] = dist[h, k] + move;
                    }
                }
            }
            Console.WriteLine("Done.");
            Console.ReadLine();
        }