public ComplexPlainAnalysis()
     : base()
 {
     WMax = SigmaMax = 1E4;
     WMin = SigmaMin = -1E4;
     Points = 91;
     Solver = new ComplexPlainSolver();
 }
        private void Update()
        {
            cir2.Reset();
            cir2.Solve();
            sol1 = (ComplexPlainSolver)ac1.Solver;

            model.MinX = ac1.SigmaMin;
            model.MaxX = ac1.SigmaMax;
            model.MaxY = ac1.WMax;
            model.MinY = ac1.WMin ;
            model.Columns = ac1.Points;
            model.Rows = ac1.Points;


            var data = Redraw(txtPlotted.Text);

        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            model = new ComplexPlainViewModel();
            model.ColorCoding = ColorCoding.ByLights;

            Circuit cir = new Circuit();
            cir.ReadCircuit("Circuits/RCL.net");
            cir2 = (Circuit)cir.Clone();
            cir2.Setup.RemoveAt(0);
            ComplexPlainAnalysis ac1 = new ComplexPlainAnalysis();
            cir2.Setup.Add(ac1);
            ACSweepSolver.Optimize(cir2);
            cir2.Solve();
            sol1 = (ComplexPlainSolver)ac1.Solver;

            int scalefactor = 5000;
            model.MinX = ac1.SigmaMin / scalefactor;
            model.MaxX = ac1.SigmaMax / scalefactor;
            model.MaxY = ac1.WMax / scalefactor;
            model.MinY = ac1.WMin / scalefactor;
            model.Columns = ac1.Points;
            model.Rows = ac1.Points;
            var data = new Point3D[model.Rows, model.Columns];
            //public Tuple<Complex32, Complex32>[,] Results { get; set; }
            MathNet.Numerics.Complex32 W;
            for (int i = 0; i < model.Rows; i++)
                for (int j = 0; j < model.Columns; j++)
                {
                    W = sol1.WfromIndexes[new Tuple<int, int>(i, j)];
                    foreach (var node in sol1.Voltages[W])
                    {
                        if (node.Key == "out")
                            data[i, j] = new Point3D(W.Real /scalefactor, 
                                                    W.Imaginary /scalefactor,
                                                    //node.Value.Magnitude);
                                                    2 * Math.Log10(node.Value.Magnitude));
                    }
                }

            model.Data = data;
            model.UpdateModel(false);         
            //model.CreateDataArray(ModuleInDB);

            DataContext = model;

        }
        private void LoadField()
        {
            Circuit cir = new Circuit();
            cir.ReadCircuit("Circuits/RCL.net");
            cir2 = (Circuit)cir.Clone();
            cir2.Setup.RemoveAt(0);
            ComplexPlainAnalysis ac1 = new ComplexPlainAnalysis();
            cir2.Setup.Add(ac1);
            ACSweepSolver.Optimize(cir2);
            cir2.Solve();
            sol1 = (ComplexPlainSolver)ac1.Solver;

          
            int Rows = ac1.Points;
            int Columns = ac1.Points;
            double[,] data = new double[Rows, Columns];
            Point[,] gridData = new Point[Rows, Columns];
            MathNet.Numerics.Complex32 W;
            for (int i = 0; i < Rows; i++)
                for (int j = 0; j < Columns; j++)
                {
                    W = sol1.WfromIndexes[new Tuple<int, int>(i, j)];
                    foreach (var node in sol1.Voltages[W])
                    {
                        if (node.Key == "out")
                        {
                            data[i, j] = 20 * Math.Log10(node.Value.Magnitude);
                            //data[i, j] = 180 * node.Value.Phase / Math.PI;
                            gridData[i, j] = new Point(W.Real, W.Imaginary);
                        }
                        
                      
                    }
                }
            
            WarpedDataSource2D<double> dataSource = new WarpedDataSource2D<double>(data, gridData);
            isolineGraph.DataSource = dataSource;
            trackingGraph.DataSource = dataSource;

        }
        static void Main(string[] args)
        {
            int i = 2;

            switch (i)
            {
                case 0:
                    cir.ReadCircuit("circuits/RLcharge.net");
                    cir2 = (Circuit)cir.Clone();
                    DCSolver.Optimize(cir2);
                    DCAnalysis ac0 = (DCAnalysis)cir2.Setup[0];
                    DCSolver solver = (DCSolver)ac0.Solver;
                    //solver.Solve(cir2, );
                    cir2.Solve();
                    solver.ExportToCSV("e:/Test.csv");

                    break;
                case 1:
                    cir.ReadCircuit("circuits/testidc.net");
                    cir2 = (Circuit)cir.Clone();
                    DCSolver.Optimize(cir2);
                    DCAnalysis ac3 = (DCAnalysis)cir2.Setup[0];
                    DCSolver solver3 = (DCSolver)ac3.Solver;
                    //solver.Solve(cir2, );
                    cir2.Solve();
                    solver3.ExportToCSV("e:/Test.csv");

                    break;


                case 2:
                    cir.ReadCircuit("circuits/derivador.net");
                    //cir.ReadCircuit("RCL.net");
                    cir2 = (Circuit)cir.Clone();
                    cir2.Setup.RemoveAt(0);
                    ACAnalysis ac = new ACAnalysis();
                    cir2.Setup.Add(ac);
                    ACSweepSolver.Optimize(cir2);
                    cir2.Solve();

                    ACSweepSolver sol = (ACSweepSolver)ac.Solver;
                    sol.ExportToCSV("ACSweep.csv");
                    break;

                case 3:
                    //cir.ReadCircuit("derivador.net");
                    cir.ReadCircuit("circuits/RLC.net");
                    cir2 = (Circuit)cir.Clone();
                    cir2.Setup.RemoveAt(0);
                    ComplexPlainAnalysis ac1 = new ComplexPlainAnalysis();
                    cir2.Setup.Add(ac1);
                    ACSweepSolver.Optimize(cir2);
                    cir2.Solve();
                    sol1 = (ComplexPlainSolver)ac1.Solver;
                    sol1.SelectedNode = sol1.CurrentCircuit.Nodes["out"];
                   // sol1.
                    sol1.ExportToCSV("e:/plain.csv");
                    Bitmap bmp =  FileUtils.DrawImage(func, ac1.Points, ac1.Points);
                    bmp.Save("e:/plain.bmp");
                    break;


                case 4:
                    cir.ReadCircuit("circuits/RCL.net");
                    //cir.ReadCircuit("RCcharge.net");
                    cir2 = (Circuit)cir.Clone();
                    cir2.Setup.RemoveAt(0);
                    TransientAnalysis ac5 = new TransientAnalysis();
                    ac5.Step = "10n";
                    cir2.Setup.Add(ac5);
                    TransientSolver sol5 = (TransientSolver)ac5.Solver;
                    TransientSolver.Optimize(cir2);
                    cir2.Solve();
                    sol5.ExportToCSV("e:/time.csv");
                    break;

                case 5:
                    cir.ReadCircuit("circuits/vsingain.net");
                    //cir.ReadCircuit("RCcharge.net");
                    cir2 = (Circuit)cir.Clone();
                    cir2.Setup.RemoveAt(0);
                    TransientAnalysis ac6 = new TransientAnalysis();
                    ac6.Step = "100n";
                    cir2.Setup.Add(ac6);
                    TransientSolver sol6 = (TransientSolver)ac6.Solver;
                    TransientSolver.Optimize(cir2);
                    cir2.Solve();
                    sol6.ExportToCSV("e:/time.csv");
                    break;

                default:
                    break;
            }

      
       


            Console.ReadKey();  
      }