static double RunSimulation(Action<int, double, double[,]> Simulate, int N, double dt, PopulationSystem S, string Title) { // Array of doubles representing the populations over time. double[,] data = new double[N, S.N]; // Initialize the system to have the population specified in the system. for (int i = 0; i < S.N; ++i) data[0, i] = S.x[i]; // Run the simulation once first to ensure JIT has ocurred if necessary. Simulate(N, dt, data); // Start time of the simulation. long start = Timer.Counter; // run the simulation 100 times to avoid noise. for (int i = 0; i < 100; ++i) Simulate(N, dt, data); double time = Timer.Delta(start); Console.WriteLine("{0} time: {1}", Title, time); // Plot the resulting data. Plot plot = new Plot() { x0 = 0.0, x1 = N, Title = Title, xLabel = "Timestep", yLabel = "Population", }; for (int i = 0; i < S.N; ++i) { KeyValuePair<double, double>[] points = new KeyValuePair<double, double>[N]; for (int j = 0; j < N; ++j) points[j] = new KeyValuePair<double, double>(j, data[j, i]); plot.Series.Add(new Scatter(points) { Name = i.ToString() }); } return time; }
public double RunTest(Circuit.Circuit C, Simulation S, Func<double, double> Vin, int Samples, string Name) { double t0 = (double)S.Time; double T = S.TimeStep; int N = 353; double[] input = new double[N]; List<List<double>> output = S.Output.Select(i => new List<double>(Samples)).ToList(); List<double[]> buffers = S.Output.Select(i => new double[N]).ToList(); double time = 0.0; int samples = 0; double t = 0; for (; samples < Samples; samples += N) { for (int n = 0; n < N; ++n, t += T) input[n] = Vin(t); long a = Timer.Counter; S.Run(input, buffers); time += Timer.Delta(a); for (int i = 0; i < S.Output.Count(); ++i) output[i].AddRange(buffers[i]); } simulateTime += time; int t1 = Math.Min(samples, 4000); Log.WriteLine("Performance {0}", Quantity.ToString(samples / time, Units.Hz)); Plot p = new Plot() { Title = Name, Width = 800, Height = 400, x0 = t0, x1 = T * t1, xLabel = "Time (s)", yLabel = "Voltage (V)", }; p.Series.AddRange(output.Select((i, j) => new Scatter( i.Take(t1) .Select((k, n) => new KeyValuePair<double, double>(n * T, k)).ToArray()) { Name = S.Output.ElementAt(j).ToString() })); return samples / time; }