Example #1
0
 static void Main(string[] args)
 {
     //EulerMethod.Solve();
     //EulerPendulum.Solve();
     RungeKuttaMethod.Solve();
 }
Example #2
0
        public GraphWindow()
        {
            try
            {
                InitializeComponent();
                CanvasStates.Clear();

                canvas.Children.Clear();
                canvasGraph.Children.Clear();


                if (ConfigurationHelper.ComputeCharts || ConfigurationHelper.ComputeGraph || ConfigurationHelper.ComputeEquations)
                {
                    System.Diagnostics.Stopwatch myStopwatch = new System.Diagnostics.Stopwatch();
                    myStopwatch.Start();

                    var elementInf = new List <ElementInformation>();

                    foreach (var elm in ConfigurationHelper.Modules.DistinctBy(b => b.Name))
                    {
                        elementInf.Add(new ElementInformation()
                        {
                            M = elm.M, L = elm.L, Name = elm.Name, AllRepairCount = ConfigurationHelper.UseRepairModules ? elm.RepairCount : 0
                        });
                    }

                    var systemState = new SystemState(elementInf, ConfigurationHelper.WorkCondition, ConfigurationHelper.Modules.Count);
                    systemState.GetDistinctSystemStates(elementInf);

                    var connections = systemState.SystemStates.Sum(s => s.BaseStates.Count) + systemState.SystemStates.Sum(s => s.ChildStates.Count);

                    dateGrid.ItemsSource = systemState.SystemStates;

                    var textArray = new string[] {
                        "",
                        $"Modules: {systemState.CountModule}",
                        $"CurrentRepairCount: {systemState.SystemElements.First().AllRepairCount}",
                        $"SystemStates: {systemState.SystemStates.Count}",
                        $"connections: {connections}",
                        ""
                    };
                    File.AppendAllLines(@"C:\Users\cemiv\Desktop\1.txt", textArray);


                    SystemState = systemState;
                    myStopwatch.Stop();
                    var ms = myStopwatch.Elapsed;
                    Presenters.Path.logFormElements.Add($"Get system states {ms} ms.");
                }
                if (ConfigurationHelper.ComputeGraph)
                {
                    System.Diagnostics.Stopwatch myStopwatch = new System.Diagnostics.Stopwatch();
                    myStopwatch.Start();
                    //todo ShowGraph();
                    myStopwatch.Stop();
                    var ms = myStopwatch.Elapsed;
                    Presenters.Path.logFormElements.Add($"Get graph {ms} ms.");
                }


                if (ConfigurationHelper.ComputeEquations)
                {
                    System.Diagnostics.Stopwatch myStopwatch = new System.Diagnostics.Stopwatch();
                    myStopwatch.Start();
                    var result = GetEquations();
                    dateGridQ.ItemsSource = result;
                    myStopwatch.Stop();
                    var ms = myStopwatch.Elapsed;
                    Presenters.Path.logFormElements.Add($"Get sysytem equations {ms} ms.");
                }

                if (ConfigurationHelper.ComputeCharts)
                {
                    System.Diagnostics.Stopwatch myStopwatch = new System.Diagnostics.Stopwatch();
                    myStopwatch.Start();


                    List <double> workValues      = new List <double>();
                    List <double> downTimeValues  = new List <double>();
                    List <double> errorTimeValues = new List <double>();
                    List <double> timeList        = new List <double>();


                    baseValues[0] = 1;

                    System.Diagnostics.Stopwatch myStopwatch1 = new System.Diagnostics.Stopwatch();
                    myStopwatch1.Start();

                    //   var result1 = ConfigurationHelper.MatLabContext.ComputeDifferentialEquations(equations, baseValues, ConfigurationHelper.Step, 0, (int)ConfigurationHelper.MaxTime);

                    //var aaa = $"F=@(t,p) [{String.Join("; ", equations).Replace(",", ".")}]; [t p]=ode45(F,[{1} : {1} : {1}], [{String.Join(" ", baseValues)}]);";
                    myStopwatch1.Stop();



                    System.Diagnostics.Stopwatch myStopwatch2 = new System.Diagnostics.Stopwatch();
                    myStopwatch2.Start();

                    var result1 = new List <List <double> >();

                    //var result1 = ConfigurationHelper.MatLabContext.ComputeDifferentialEquations(equations, baseValues, ConfigurationHelper.Step, 0, (int)ConfigurationHelper.MaxTime);

                    var result12 = RungeKuttaMethod.RungeKuttaMethods(equations.Count, (int)ConfigurationHelper.Step, (int)ConfigurationHelper.MaxTime, equationCoefficients, baseValues.Select(s => (double)s).ToArray());

                    var baseValuesq = baseValues.Select(s => (double)s).ToList();

                    result1.Add(baseValuesq);
                    result1.AddRange(result12);
                    myStopwatch2.Stop();



                    double startTime = 0;

                    ConfigurationHelper.RungeKutteStringTable.Clear();



                    // Add columns
                    var headers  = new List <string>();
                    var gridView = new GridView();
                    this.listView.View = gridView;
                    gridView.Columns.Add(new GridViewColumn
                    {
                        Header = $"{Properties.Resources.Time}",
                        DisplayMemberBinding = new Binding($"{Properties.Resources.Time}")
                    });
                    headers.Add(Properties.Resources.Time);


                    for (var i = 0; i <= SystemState.SystemStates.Count; i++)
                    {
                        gridView.Columns.Add(new GridViewColumn
                        {
                            Header = $"S{i+1}",
                            DisplayMemberBinding = new Binding($"S{i+1}")
                        });

                        headers.Add($"S{i + 1}");
                    }

                    gridView.Columns.Add(new GridViewColumn
                    {
                        Header = $"{Properties.Resources.SumWorkingStates}",
                        DisplayMemberBinding = new Binding($"Working")
                    });
                    headers.Add(Properties.Resources.SumWorkingStates);

                    gridView.Columns.Add(new GridViewColumn
                    {
                        Header = $"{Properties.Resources.SumDownTiimeStates}",
                        DisplayMemberBinding = new Binding($"Downtime")
                    });
                    headers.Add(Properties.Resources.SumDownTiimeStates);


                    gridView.Columns.Add(new GridViewColumn
                    {
                        Header = $"{Properties.Resources.SumRefusalStates}",
                        DisplayMemberBinding = new Binding($"Refusal")
                    });
                    headers.Add(Properties.Resources.SumRefusalStates);

                    ConfigurationHelper.RungeKutteStringTable.AppendLine(String.Join(", ", headers));

                    var workDictionary     = new Dictionary <double, double>();
                    var downTimeDictionary = new Dictionary <double, double>();
                    var refusalDictionary  = new Dictionary <double, double>();


                    double indexCount = 0;
                    foreach (var item in result1)
                    {
                        var ListViewObject = new System.Dynamic.ExpandoObject() as IDictionary <string, Object>;

                        timeList.Add(startTime);
                        double workValue     = 0;
                        double downTimeValue = 0;

                        for (var index = 0; index < item.Count; index++)
                        {
                            if (SystemState.WorkStateIndexList.Contains(index))
                            {
                                workValue += item[index];
                            }
                            if (SystemState.DownTimeStateIndexList.Contains(index))
                            {
                                downTimeValue += item[index];
                            }

                            ListViewObject.Add($"S{index + 1}", Math.Round(item[index], 15));
                        }
                        workValues.Add(workValue);
                        downTimeValues.Add(downTimeValue);
                        var sum = downTimeValue + workValue;
                        errorTimeValues.Add(1 - sum);
                        workDictionary.Add(indexCount, workValue);
                        downTimeDictionary.Add(indexCount, downTimeValue);
                        refusalDictionary.Add(indexCount, 1 - sum);
                        indexCount += ConfigurationHelper.Step;



                        ListViewObject.Add("Time", startTime);

                        ListViewObject.Add("Working", Math.Round(workValue, 15));
                        ListViewObject.Add("Downtime", Math.Round(downTimeValue, 15));
                        ListViewObject.Add("Refusal", Math.Round(1 - sum, 15));

                        var tableValues = new List <string>();

                        tableValues.Add(Math.Round(workValue, 15).ToString());
                        tableValues.AddRange(item.Select(s => Math.Round(s, 15).ToString()).ToList());
                        tableValues.Add(Math.Round(workValue, 15).ToString());
                        tableValues.Add(downTimeValue.ToString());
                        tableValues.Add(Math.Round(1 - sum, 15).ToString());
                        ConfigurationHelper.RungeKutteStringTable.AppendLine(String.Join(", ", tableValues));

                        // Populate list
                        this.listView.Items.Add(ListViewObject);



                        startTime += ConfigurationHelper.Step;
                    }



                    var RectangleMethodWork = Integral.RectangleMethod(workDictionary, 0, ConfigurationHelper.MaxTime, (int)(ConfigurationHelper.MaxTime / ConfigurationHelper.Step));
                    var TrapeziumMethodWork = Integral.TrapeziumMethod(workDictionary, 0, ConfigurationHelper.MaxTime, (int)(ConfigurationHelper.MaxTime / ConfigurationHelper.Step));

                    this.T_work.Content = $"{Properties.Resources.AverageWorkTime} = {Math.Round(TrapeziumMethodWork,6)}";


                    var RectangleMethodDownTime = Integral.RectangleMethod(downTimeDictionary, 0, ConfigurationHelper.MaxTime, (int)(ConfigurationHelper.MaxTime / ConfigurationHelper.Step));
                    var TrapeziumMethodDownTime = Integral.TrapeziumMethod(downTimeDictionary, 0, ConfigurationHelper.MaxTime, (int)(ConfigurationHelper.MaxTime / ConfigurationHelper.Step));

                    this.T_down.Content = $"{Properties.Resources.AverageDowntimeTime} = {Math.Round(TrapeziumMethodDownTime, 6)}";


                    var RectangleMethodRefusal = Integral.RectangleMethod(refusalDictionary, 0, ConfigurationHelper.MaxTime, (int)(ConfigurationHelper.MaxTime / ConfigurationHelper.Step));
                    var TrapeziumMethodRefusal = Integral.TrapeziumMethod(refusalDictionary, 0, ConfigurationHelper.MaxTime, (int)(ConfigurationHelper.MaxTime / ConfigurationHelper.Step));
                    this.T_error.Content = $"{Properties.Resources.AverageRefusalTime} = {Math.Round(TrapeziumMethodRefusal, 6)}";

                    var yDataSource = new EnumerableDataSource <double>(workValues);

                    var suumm  = RectangleMethodWork + RectangleMethodDownTime + RectangleMethodRefusal;
                    var suumm1 = TrapeziumMethodWork + TrapeziumMethodDownTime + TrapeziumMethodRefusal;


                    yDataSource.SetYMapping(Y => Y);

                    var xDataSource = new EnumerableDataSource <double>(timeList);
                    xDataSource.SetXMapping(X => X);

                    CompositeDataSource compositeDataSource = new CompositeDataSource(xDataSource, yDataSource);

                    //   plotterWork.AddHandler(CircleElementPointMarker.ToolTipTextProperty, s => String.Format("Y-Data : {0}\nX-Data : {1}", s.Y, s.X));
                    plotterWork.Children.RemoveAll(typeof(LineGraph));
                    plotterWork.AddLineGraph(compositeDataSource, new Pen(Brushes.Green, 2), new PenDescription("Work Line"));
                    plotterWork.FitToView();


                    yDataSource = new EnumerableDataSource <double>(downTimeValues);
                    yDataSource.SetYMapping(Y => Y);

                    xDataSource = new EnumerableDataSource <double>(timeList);
                    xDataSource.SetXMapping(X => X);

                    compositeDataSource = new CompositeDataSource(xDataSource, yDataSource);

                    plotterDownTime.Children.RemoveAll(typeof(LineGraph));
                    plotterDownTime.AddLineGraph(compositeDataSource, new Pen(Brushes.Orange, 2), new PenDescription("Down Time Line"));
                    plotterDownTime.FitToView();

                    yDataSource = new EnumerableDataSource <double>(errorTimeValues);
                    yDataSource.SetYMapping(Y => Y);

                    xDataSource = new EnumerableDataSource <double>(timeList);
                    xDataSource.SetXMapping(X => X);

                    compositeDataSource = new CompositeDataSource(xDataSource, yDataSource);

                    plotterErrorTime.Children.RemoveAll(typeof(LineGraph));
                    plotterErrorTime.AddLineGraph(compositeDataSource, new Pen(Brushes.Red, 2), new PenDescription("Error Time Line"));
                    plotterErrorTime.FitToView();

                    myStopwatch.Stop();
                    var ms = myStopwatch.Elapsed;
                    Presenters.Path.logFormElements.Add($"Get charts of reliability time ex. {ms} ms.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MessageBox.Show(ex.StackTrace);
            }
        }
Example #3
0
        private void DoMaths()
        {
            MatrixRequest?.Invoke(); //updates matrix
            double t, toX, step;
            IEnumerable <double> points;

            double[,] ode;
            DiffMethod method;

            try
            {
                t      = DrawingInterval.From;
                toX    = DrawingInterval.To;
                step   = IntegrationStep;
                method = CurrentlySelectedMethod;
                points = VariableMatrix.Cast <double>();
                ode    = ConstMatrix;
            }
            catch (Exception e)
            {
                MessageBox.Show(Locale["#ParsingErrMsg"] + e.Message, Locale["#ParsingErr"], MessageBoxButton.OK,
                                MessageBoxImage.Error);
                return;
            }

            var dynFun = new List <DynamicDiffFun>
            {
                (dt, vars) => ode[0, 0] * vars.x + ode[0, 1] * vars.y + ode[0, 2] * vars.z + ode[0, 3] * vars.w + ode[0, 4],
                (dt, vars) => ode[1, 0] * vars.x + ode[1, 1] * vars.y + ode[1, 2] * vars.z + ode[1, 3] * vars.w + ode[1, 4],
                (dt, vars) => ode[2, 0] * vars.x + ode[2, 1] * vars.y + ode[2, 2] * vars.z + ode[2, 3] * vars.w + ode[2, 4],
                (dt, vars) => ode[3, 0] * vars.x + ode[3, 1] * vars.y + ode[3, 2] * vars.z + ode[3, 3] * vars.w + ode[3, 4]
            };

            var coeffFunctions = new List <string> {
                "x", "y", "z", "w"
            };

            //Test case:
            // *Produces beautiful e^ 2x
            //*
            //*
            // var dynFun = new List<DifferentialService.DynamicDiffFun>
            //{
            //     (dt, variables) => -2*variables.x+4*variables.y,
            //     (dt, variables) => 3*variables.x-variables.y
            //};
            // var coeffFunctions = new List<string> { "x", "y", };
            // var points = new double[] { 1, 1 };

            RungeKuttaMethod rungeKuttaMethod = Rk4;

            switch (method)
            {
            case DiffMethod.RK4:
                rungeKuttaMethod = Rk4;
                break;

            case DiffMethod.Ralston:
                rungeKuttaMethod = Ralston;
                break;
            }

            var cmp     = new List <DataPoint>();
            var results = coeffFunctions.Select(coeffFunction => new List <DataPoint>()).ToList();

            while (t < toX)
            {
                //kutta
                var result = rungeKuttaMethod(t, points, step, dynFun, coeffFunctions);

                var i = 0;
                foreach (var d in result)
                {
                    results[i++].Add(new DataPoint(t, d));
                }

                if (t + step > toX)
                {
                    step = toX - t;
                }
                points = result.ToArray();

                //draw compare
                if (CurrentlySelectedFunction != null)
                {
                    cmp.Add(new DataPoint(t, CurrentlySelectedFunction.GetValue(t)));
                }
                t += step;
            }

            //FUN FUN FUN FUN FUN FUN FUN FUN FUN FUN FUN FUN FUN
            //for (int i = 0; i < results.Count; i++)
            //{
            //    results[0][i] = new DataPoint(results[1][i].Y,results[0][i].Y);
            //}
            //FUN FUN FUN FUN FUN FUN FUN FUN FUN FUN FUN FUN FUN

            CoeffFunctionsAvaibleToDraw =
                coeffFunctions.Select((coeff, i) => new Tuple <string, List <DataPoint> >(coeff, results[i])).ToList();
            ComparisionDataPoints      = cmp;
            SelectedCoeffFunctionIndex = 0;
        }