示例#1
0
        internal void RunDAETest(MainWindow window, double alpha, double fAbsTol, double step, int iterations, int method)
        {
            FileTabItem tab = null;

            window.Dispatcher.Invoke(() =>
            {
                tab = window.FileTab.SelectedItem as FileTabItem;
            });
            if (tab == null)
            {
                return;
            }
            List <ErrorMessage> errorList = new List <ErrorMessage>();

            window.Dispatcher.Invoke(() =>
            {
                window.FileTab.Focus();
                window.StatusText = "Расчёт";
            });
            //Thread.Sleep(4000); //Test of UI
            List <string> outputList = new List <string>();

            try
            {
                string text = "";
                window.Dispatcher.Invoke(() => { text = tab.Document.Text; });
                Equations.DAE.Compiler compiler = new Equations.DAE.Compiler();
                Equations.DAE.Implicit.DAEIDescription compiledEquation = compiler.CompileDAEImplicit(text);

                //Equations.DAE.Implicit.RADAUIIA3 solver = new Equations.DAE.Implicit.RADAUIIA3();
                Equations.DAE.Implicit.DAEISolver solver;
                switch (method)
                {
                case (int)METHOD.RADAUIIA3:
                    solver = new Equations.DAE.Implicit.RADAUIIA3(fAbsTol, iterations, alpha, step);
                    break;

                case (int)METHOD.RADAUIIA5:
                    solver = new Equations.DAE.Implicit.RADAUIIA5(fAbsTol, iterations, alpha, step);
                    break;

                case (int)METHOD.BDF:
                    solver = new Equations.DAE.Implicit.BDF1(fAbsTol, iterations, alpha, step);
                    break;

                case (int)METHOD.TRAPEZOID:
                    throw new NotImplementedException();

                default:
                    throw new NotImplementedException();
                }
                Equations.DAE.Solution solution = Equations.DAE.Implicit.DAEISolver.Solve(compiledEquation, solver);
                window.Dispatcher.Invoke(() =>
                {
                    solution.ShowResults();
                });
            }
            catch (Equations.CompilerException exc)
            {
                outputList.Add(exc.Message);
                var errors = exc.Errors;
                foreach (var error in errors)
                {
                    errorList.Add(error);
                }
            }
            catch (Exception exc)
            {
                window.Dispatcher.Invoke(() =>
                {
                    window.OutputText += exc.Message;
                    window.OutputText += "\n";
                    window.OutputText += exc.StackTrace;
                });
                return;
            }
            try
            {
                window.Dispatcher.Invoke(() =>
                {
                    foreach (ErrorMessage error in errorList)
                    {
                        window.errors.Add(error);
                    }
                    foreach (var output in outputList)
                    {
                        window.OutputText += output;
                        window.OutputText += "\n";
                    }
                    window.StatusText = "Готово";
                }
                                         );
            }
            catch (Exception exc)
            {
                Console.Write(exc.Message);
            }
            return;
        }
        private TransientSolution Solve(string equations)
        {
            stateChangedCount = 0;
            List <IScopeElement> scopeElements = new List <IScopeElement>();

            foreach (var element in elements)
            {
                if (element is IScopeElement)
                {
                    scopeElements.Add(element as IScopeElement);
                }
            }
            Equations.DAE.Compiler compiler = new Equations.DAE.Compiler();
            Equations.DAE.Implicit.DAEIDescription compiledEquation = compiler.CompileDAEImplicit(equations);
            double t = t0;

            Equations.DAE.Implicit.DAEIAnalytic system = new Equations.DAE.Implicit.DAEIAnalytic(compiledEquation);
            Vector <double> x = Vector <double> .Build.SparseOfArray(compiledEquation.InitialValues);

            List <string> variableNames = new List <string>();

            foreach (var scope in scopeElements)
            {
                variableNames.AddRange(scope.GetTransientVariableNames());
            }
            //generate events
            InitEvents(ref system);
            TransientSolution result             = new TransientSolution(variableNames.ToArray());
            int scopeVariableCount               = variableNames.Count;
            Dictionary <string, int> variableMap = compiledEquation.GetVariableDictionary();

            {
                TransientState currentState  = new TransientState(x, variableMap);
                List <double>  currentValues = new List <double>();
                foreach (var scopeElement in scopeElements)
                {
                    currentValues.AddRange(scopeElement.GetReading(currentState));
                }
                result.AddPoint(currentValues.ToArray(), t);
            }
            while (t < t1 && maxPoints > (scopeVariableCount + 1) * result.GetPointCount())
            {
                Vector <double> xPrev = x;
                double          tPrev = t;
                //make step
                x  = solver.IntegrateStep(system, x, t);
                t += solver.Step;
                //update t
                if (events.Count != 0)
                {
                    //if state happened during [tprev,tcurr]
                    t = UpdateState(tPrev, t, xPrev, x, variableMap, ref system);
                    x = MathUtils.MathUtils.Interpolate(xPrev, x, (t - tPrev) / solver.Step);
                }
                TransientState currentState  = new TransientState(x, variableMap);
                List <double>  currentValues = new List <double>();
                foreach (var scopeElement in scopeElements)
                {
                    currentValues.AddRange(scopeElement.GetReading(currentState));
                }
                result.AddPoint(currentValues.ToArray(), t);
            }
            result.SetStateChangedCount(stateChangedCount);
            return(result);
        }
示例#3
0
        internal void RunDAEExpressionTest(MainWindow window)
        {
            FileTabItem tab = null;

            window.Dispatcher.Invoke(() =>
            {
                tab = window.FileTab.SelectedItem as FileTabItem;
            });
            if (tab == null)
            {
                return;
            }
            List <ErrorMessage> errorList = new List <ErrorMessage>();

            window.Dispatcher.Invoke(() =>
            {
                window.FileTab.Focus();
                window.StatusText = "Расчёт";
            });
            //Thread.Sleep(4000); //Test of UI
            List <string> outputList = new List <string>();

            try
            {
                string text = "";
                window.Dispatcher.Invoke(() => { text = tab.Document.Text; });
                Equations.DAE.Compiler compiler = new Equations.DAE.Compiler();
                Equations.DAE.Implicit.DAEIDescription compiledEquation = compiler.CompileDAEImplicit(text);
                outputList.Add("Variables:");
                outputList.Add(compiledEquation.PrintVariables());
                outputList.Add("Equations:");
                outputList.Add(compiledEquation.PrintEquations());
            }
            catch (Equations.CompilerException exc)
            {
                outputList.Add(exc.Message);
                var errors = exc.Errors;
                foreach (var error in errors)
                {
                    errorList.Add(error);
                }
            }
            catch (Exception exc)
            {
                window.Dispatcher.Invoke(() =>
                {
                    window.OutputText += exc.Message;
                    window.OutputText += "\n";
                    window.OutputText += exc.StackTrace;
                });
                return;
            }
            try
            {
                window.Dispatcher.Invoke(() =>
                {
                    foreach (ErrorMessage error in errorList)
                    {
                        window.errors.Add(error);
                    }
                    foreach (var output in outputList)
                    {
                        window.OutputText += output;
                        window.OutputText += "\n";
                    }
                    window.StatusText = "Готово";
                }
                                         );
            }
            catch (Exception exc)
            {
                Console.Write(exc.Message);
            }
            return;
        }