Esempio n. 1
0
        private void loadButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();

            dialog.InitialDirectory = Directory.GetCurrentDirectory();
            dialog.RestoreDirectory = true;
            dialog.Filter           = "yml files (*.yml)|*.yml|All files (*.*)|*.*";
            if (dialog.ShowDialog() == true)
            {
                SolutionInput input = SolutionInput.FromFile(dialog.FileName);
                input.SetInput(parameters);

                if (input.IsFilterGrid)
                {
                    Dependencies.RemoveFilter(); // TODO reduce extra code?
                    FilterGrid filter = new FilterGrid(filterPanel);
                    filter.Set(input.FilterGrid);
                    filterBuilder = filter;
                }
                else
                {
                    Dependencies.RemoveFilter();
                    FilterFormulas filter = new FilterFormulas(filterPanel, Dependencies);
                    filter.Deserialize(input.FilterFormulas);
                    filterBuilder = filter;
                }
            }
        }
Esempio n. 2
0
        private SolutionInput BuildInput()
        {
            SolutionInput res = new SolutionInput();

            foreach (UIParam param in parameters)
            {
                res.TrySet(param.Name, param.Text);
            }
            res.SetFilter(filterBuilder);
            return(res);
        }
Esempio n. 3
0
        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();

            dialog.InitialDirectory = Directory.GetCurrentDirectory();
            dialog.RestoreDirectory = true;
            dialog.Filter           = "yml files (*.yml)|*.yml|All files (*.*)|*.*";
            if (dialog.ShowDialog() == true)
            {
                SolutionInput input = BuildInput();
                input.Save(dialog.FileName);
            }
        }
Esempio n. 4
0
        private void BuildSolver()
        {
            SolutionInput input = BuildInput();
            Filter        P     = input.Filter;

            Solver newSolver;
            string errorElem = "";

            try
            {
                int n = P.Size;
                errorElem = "D";
                double D = double.Parse(input.D);
                errorElem = "A0";
                Complex A0 = ComplexUtils.Parse(input.A_0);
                errorElem = "K";
                double K = double.Parse(input.K);
                errorElem = "T";
                double T = double.Parse(input.T);
                errorElem = "M";
                int M = int.Parse(input.t_count);
                errorElem = "N";
                int N = int.Parse(input.x_count);

                double chi = Solver.GetChi(K, P[n, n], A0);
                Dependencies.Set(MathAliases.ConvertName("chi"), chi);

                errorElem = "u0";
                IExpression expr = MainParser.Parse(input.u_0);
                textBlock_u0.Text = "u0 = " + expr.AsString();

                string[] deps = MainParser.GetDependencies(expr);
                double[] u0   = MainParser.EvalArrayD(expr, Dependencies, deps, "x", 2 * Math.PI, N);

                errorElem = "solver";
                newSolver = new Solver(P, T, N, M);
                ModelParams param = new ModelParams(A0, K, u0, D);
                newSolver.SetParams(param);
                curSolver = newSolver;
            }
            catch (Exception ex)
            {
                Logger.Write(errorElem + ": " + ex.Message);
                return;
            }
        }
Esempio n. 5
0
        private void DrawExpectedSolution()
        {
            SolutionInput input = BuildInput();
            IExpression   expr  = MainParser.Parse(input.v);

            string[] deps = MainParser.GetDependencies(expr);
            double   T    = double.Parse(input.T);
            int      M    = int.Parse(input.t_count);
            int      N    = int.Parse(input.x_count);

            if (M > VISUALIZATION_HEIGHT)
            {
                M = VISUALIZATION_HEIGHT;
            }
            if (N > VISUALIZATION_WIDTH)
            {
                N = VISUALIZATION_WIDTH;
            }

            if (Dependencies.Get(MathAliases.ConvertName("chi")) == null)
            {
                Dependencies.Set(MathAliases.ConvertName("chi"), 0);
            }

            SwitchDrawButton(true);
            calcBar.Value = 0;
            var calcProgress = new Progress <double>(value => calcBar.Value = value);

            ShowProgress();

            Task.Run(() =>
            {
                AsyncArg arg  = new AsyncArg(calcProgress, solveCancellation.Token);
                double[,] sol = MainParser.EvalMatrixD(arg, expr, Dependencies, deps, "t", T, M, "x", 2 * Math.PI, N);
                if (arg.Token.IsCancellationRequested)
                {
                    return;
                }

                this.Dispatcher.Invoke(() =>
                {
                    UpdateVisualization(sol, "U(x,t)");
                    SwitchDrawButton(false);
                });
            });
        }
Esempio n. 6
0
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Logger.Destination = log;
            RenderOptions.SetBitmapScalingMode(scopeImage, BitmapScalingMode.NearestNeighbor);

            RadioButton[] filterModeButtons = { matrixRadioButton, formulaRadioButton };
            FilterModeGroup          = new RadioButtonGroup(filterModeButtons);
            FilterModeGroup.Changed += FillModeChanged;

            RadioButton[] solMethodButtons = { explicitRadioButton, implicitRadioButton };
            SolutionMethodGroup          = new RadioButtonGroup(solMethodButtons);
            SolutionMethodGroup.Changed += SolutionMethodChanged;

            Dependencies = new DependencySpace();
            AddParam("D", "D");
            AddParam("A_0", "A0");
            AddParam("K", "K");
            AddParam("T", "T");
            AddParam("t_count", "time");
            AddParam("x_count", "spatial");
            AddParam("u_0", "u0(x)");
            AddLabel("");
            AddParam("v", "v(x, t)");
            AddButton("Draw v(x,t)", (a, b) => DrawExpectedSolution());
            SolutionInput defaultInput = new SolutionInput();

            defaultInput.SetInput(parameters);

            if (defaultInput.IsFilterGrid)
            {
                FilterGrid filterGrid = new FilterGrid(filterPanel);
                filterGrid.Set(defaultInput.FilterGrid);
                filterBuilder = filterGrid;
            }
            else
            {
                FilterFormulas filterFormulas = new FilterFormulas(filterPanel, Dependencies);
                filterFormulas.Deserialize(defaultInput.FilterFormulas);
                filterBuilder = filterFormulas;
            }
        }
Esempio n. 7
0
        private async void drawButton_ClickAsync(object sender, RoutedEventArgs e)
        {
            if (solveCancellation != null)
            {
                SwitchDrawButton(false);
                return;
            }

            BuildSolver();

            SwitchDrawButton(true);
            calcBar.Value = 0;
            var calcProgress = new Progress <double>(value => calcBar.Value = value);

            ShowProgress();

            Task.Run(() =>
            {
                AsyncArg asyncArg = new AsyncArg(calcProgress, solveCancellation.Token);
                curSolver.Solve(method, asyncArg);
                if (asyncArg.Token.IsCancellationRequested)
                {
                    return;
                }

                this.Dispatcher.Invoke(() =>
                {
                    textBlock_Khi.Text = "𝜒 = " + curSolver.Chi.ToString("f4");
                    var eigen          = FilterInfo.UpdateEigen(filterBuilder.Filter, textBlock_critical, curSolver.Parameters);

                    Complex[] eigenvalies = eigen.Item1;
                    int k         = -1;
                    bool isTuring = false;
                    for (int i = 0; i < eigenvalies.Length; i++)
                    {
                        if (eigenvalies[i].Real > 0 || Math.Abs(eigenvalies[i].Real) < 0.005)
                        {
                            if (eigenvalies[i].Imaginary > 0)
                            {
                                k        = i;
                                isTuring = false;
                                break;
                            }
                            if (Math.Abs(eigenvalies[i].Imaginary) < 0.00001)
                            {
                                k        = i;
                                isTuring = true;
                                break;
                            }
                        }
                    }
                    if (k >= 0)
                    {
                        Complex[] eigenvector = new Complex[eigenvalies.Length];
                        for (int i = 0; i < eigenvector.Length; i++)
                        {
                            eigenvector[i] = eigen.Item2[i, k];
                        }
                        int center = eigenvector.Length / 2;

                        string v;
                        if (isTuring)
                        {
                            v = SolutionInput.GenExpectedTuring(
                                (n) => (eigenvector[center + n] + eigenvector[center - n]).Real,
                                (n) => - (eigenvector[center + n] - eigenvector[center - n]).Imaginary,
                                center, true, 1);
                        }
                        else
                        {
                            v = SolutionInput.GenExpectedHopf(eigenvalies[k].Imaginary,
                                                              (n) => (eigenvector[center + n] + eigenvector[center - n]).Real,
                                                              (n) => - (eigenvector[center + n] - eigenvector[center - n]).Imaginary,
                                                              (n) => (eigenvector[center + n] + eigenvector[center - n]).Imaginary,
                                                              (n) => (eigenvector[center + n] - eigenvector[center - n]).Real,
                                                              center, true, 1);
                        }

                        foreach (UIParam param in parameters)
                        {
                            if (param.Name == "v")
                            {
                                param.Text = v;
                                break;
                            }
                        }
                    }

                    UpdateVisualization(curSolver.Solution, "u(x,T)");
                    SwitchDrawButton(false);
                });
            });
        }