Exemple #1
0
        private void RunSimulations()
        {
            try
            {
                Status       = "Status: Running simulations";
                PlotsEnabled = false;

                Internals = new TreeViewModel();
                Plots     = new TabsViewModel();
                Prints    = new ObservableCollection <UIElement>();

                var parseResult = SpiceHelper.GetSpiceSharpNetlist(Netlist, RandomSeed, HasTitle, Encoding);

                if (parseResult != null)
                {
                    var model = new SpiceSharpParser.SpiceSharpReader().Read(parseResult.FinalModel);


                    if (model == null)
                    {
                        Logs += $"Errors in lexing: {parseResult.ValidationResult.HasError}\n";
                    }
                    else
                    {
                        SaveExportsToFile(model);
                        Logs += $"Simulations found: {model.Simulations.Count}\n";
                        Logs += "Errors and warnings: \n";

                        foreach (var log in model.ValidationResult)
                        {
                            Logs += log.Message + ", line =" + log.LineInfo.LineNumber + "\n";
                        }

                        int simulationNo = 0;

                        Stopwatch simulationsWatch = new Stopwatch();
                        simulationsWatch.Start();
                        System.Threading.Tasks.Parallel.ForEach <Simulation>(
                            model.Simulations,
                            new ParallelOptions()
                        {
                            MaxDegreeOfParallelism = MaxDegreeOfParallelism
                        }, simulation => RunSimulation(model, (Simulation)simulation, Interlocked.Increment(ref simulationNo)));
                        simulationsWatch.Stop();

                        // Generate summary statistics
                        Dispatcher.Invoke(() =>
                        {
                            var summary = new SummarySimulationStatistics();

                            foreach (var stat in Stats)
                            {
                                summary.BehaviorCreationTime += stat.BehaviorCreationTime;
                                summary.FinishTime           += stat.FinishTime;
                                summary.ExecutionTime        += stat.ExecutionTime;
                                summary.SetupTime            += stat.SetupTime;
                                summary.ValidationTime       += stat.ValidationTime;
                            }
                            summary.TotalSimulationsTime = simulationsWatch.ElapsedMilliseconds;

                            SummaryStats.Add(summary);
                        });

                        // Generate plots
                        if (model.XyPlots.Count > 0)
                        {
                            PlotsEnabled = true;

                            Logs += $"Creating plots: {model.XyPlots.Count}\n";

                            if (model.XyPlots.Count > 0)
                            {
                                foreach (var plot in model.XyPlots)
                                {
                                    Dispatcher.Invoke(() =>
                                    {
                                        Plots.Items.Add(new TabItem()
                                        {
                                            Header = plot.Name, Content = new XyPlotControl()
                                            {
                                                Plot = plot
                                            }
                                        });
                                    });
                                }
                            }
                        }

                        // Generate plots
                        if (model.MonteCarloResult.Enabled)
                        {
                            PlotsEnabled = true;

                            Logs += $"Creating monte carlo plot\n";

                            Dispatcher.Invoke(() =>
                            {
                                var plot = new HistogramPlotControl()
                                {
                                    Data = model.MonteCarloResult
                                };
                                plot.DataBind();
                                Plots.Items.Add(new TabItem()
                                {
                                    Header = "Monte Carlo", Content = plot
                                });
                            });
                        }

                        Logs += $"Prints found: {model.Prints.Count}\n";

                        if (model.Prints.Count > 0)
                        {
                            foreach (var print in model.Prints)
                            {
                                Dispatcher.Invoke(() =>
                                {
                                    PrintControl control = new PrintControl(print);
                                    control.DataBind();
                                    Prints.Add(control);
                                });
                            }
                        }
                    }
                    Status = "Status: Finished";
                }
            }
            catch (Exception ex)
            {
                Logs  += ex.ToString();
                Status = "Status: Error";
            }
        }
Exemple #2
0
        private void RunSimulations()
        {
            try
            {
                Status       = "Status: Running simulations";
                PlotsEnabled = false;

                Internals = new TreeViewModel();
                Plots     = new TabsViewModel();
                Prints    = new ObservableCollection <UIElement>();

                var model = SpiceHelper.GetSpiceSharpNetlist(Netlist, Mode, RandomSeed, HasTitle);

                SaveExportsToFile(model);

                Logs += $"Simulations found: {model.Simulations.Count}\n";
                int simulationNo = 0;

                Stopwatch simulationsWatch = new Stopwatch();
                simulationsWatch.Start();
                Parallel.ForEach <Simulation>(
                    model.Simulations,
                    new ParallelOptions()
                {
                    MaxDegreeOfParallelism = MaxDegreeOfParallelism
                }, simulation => RunSimulation(model, (BaseSimulation)simulation, Interlocked.Increment(ref simulationNo)));
                simulationsWatch.Stop();

                // Generate summary statistics
                Dispatcher.Invoke(() =>
                {
                    var summary = new SummarySimulationStatistics();

                    foreach (var stat in Stats)
                    {
                        summary.Iterations           += stat.Iterations;
                        summary.SolveTime            += stat.SolveTime;
                        summary.LoadTime             += stat.LoadTime;
                        summary.ReorderTime          += stat.ReorderTime;
                        summary.BehaviorCreationTime += stat.BehaviorCreationTime;
                        summary.Timepoints           += stat.Timepoints;
                        summary.TransientIterations  += stat.TransientIterations;
                        summary.TransientTime        += stat.TransientTime;
                        summary.AcceptedTimepoints   += stat.AcceptedTimepoints;
                        summary.RejectedTimepoints   += stat.RejectedTimepoints;
                    }
                    summary.TotalSimulationsTime = simulationsWatch.ElapsedMilliseconds;

                    SummaryStats.Add(summary);
                });

                // Generate plots
                if (model.XyPlots.Count > 0)
                {
                    PlotsEnabled = true;

                    Logs += $"Creating plots: {model.XyPlots.Count}\n";

                    if (model.XyPlots.Count > 0)
                    {
                        foreach (var plot in model.XyPlots)
                        {
                            Dispatcher.Invoke(() =>
                            {
                                Plots.Items.Add(new TabItem()
                                {
                                    Header = plot.Name, Content = new XyPlotControl()
                                    {
                                        Plot = plot
                                    }
                                });
                            });
                        }
                    }
                }

                // Generate plots
                if (model.MonteCarloResult.Enabled)
                {
                    PlotsEnabled = true;

                    Logs += $"Creating monte carlo plot\n";

                    Dispatcher.Invoke(() =>
                    {
                        var plot = new HistogramPlotControl()
                        {
                            Data = model.MonteCarloResult
                        };
                        plot.DataBind();
                        Plots.Items.Add(new TabItem()
                        {
                            Header = "Monte Carlo", Content = plot
                        });
                    });
                }

                Logs += $"Prints found: {model.Prints.Count}\n";

                if (model.Prints.Count > 0)
                {
                    foreach (var print in model.Prints)
                    {
                        Dispatcher.Invoke(() =>
                        {
                            PrintControl control = new PrintControl(print);
                            control.DataBind();
                            Prints.Add(control);
                        });
                    }
                }

                foreach (var warning in model.Warnings)
                {
                    Dispatcher.Invoke(() =>
                    {
                        Logs += "Warning: " + warning + "\n";
                    });
                }

                Status = "Status: Finished";
            }
            catch (Exception ex)
            {
                Logs  += ex.ToString();
                Status = "Status: Error";
            }
        }