Exemplo n.º 1
0
        public NbodyPanel(
            int width,     // width of the panel
            int height,    // height of the panel
            float xMin,    // the smallest visible x-coordinate
            float xMax,    // the largest visible x-coordinate
            float yMin,    // the smallest visible y-coordinate
            float yMax,    // the largest visible y-coordinate
            float dt,      // the time step-size
            Body[] bodies, // the bodies to be shown
            bool parallel  // should the calculations be done in parallel
            )
        {
            this.Size           = new Size(width, height);
            this.DoubleBuffered = true;
            this.BackColor      = Color.Black;

            float xScale = (float)(width / (xMax - xMin));
            float yScale = (float)(height / (yMax - yMin));

            float xTranslate = (float)Math.Abs(xMin * xScale);
            float yTranslate = (float)Math.Abs(yMin * yScale);

            float planetRadius = ((xMax - xMin) / width) * 10;


            Timer timer = new Timer();

            timer.Interval = 1;

            timer.Tick += delegate(Object sender, EventArgs e)
            {
                Bitmap   bmpNew = new Bitmap(width, height);
                Graphics g      = Graphics.FromImage(bmpNew);
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.Transform     = new System.Drawing.Drawing2D.Matrix(xScale, 0, 0, yScale, xTranslate, yTranslate);


                if (parallel)
                {
                    NBodySolver.ParallelCalcForceAndUpdate(bodies, dt);
                }
                else
                {
                    NBodySolver.SequentialCalcForceAndUpdate(bodies, dt);
                }

                NBodySolver.SequentialDraw(bodies, g, planetRadius);

                this.bmp = bmpNew;
                Refresh();
            };

            timer.Start();
        }
Exemplo n.º 2
0
        private static void NBodyConsole()
        {
            int    N     = 10000;
            int    steps = 1;
            double dt    = 100000;

            Body[] bodies = NBodySolver.GenerateRandomBodies(N);


            // To make a fair comparison, we call
            // each method once, to let the JIT kick in
            NBodySolver.ParallelCalcForceAndUpdate(bodies, dt);
            NBodySolver.SequentialCalcForceAndUpdate(bodies, dt);


            Console.WriteLine("Starting parallel N-body solver...");
            long start = DateTime.Now.Ticks / 10000;

            for (int i = 0; i < steps; i++)
            {
                NBodySolver.ParallelCalcForceAndUpdate(bodies, dt);
            }
            long end  = DateTime.Now.Ticks / 10000;
            long tPar = end - start;

            Console.WriteLine("Time spent: " + tPar);


            Console.WriteLine("Starting sequential N-body solver...");
            start = DateTime.Now.Ticks / 10000;
            for (int i = 0; i < steps; i++)
            {
                NBodySolver.SequentialCalcForceAndUpdate(bodies, dt);
            }
            end = DateTime.Now.Ticks / 10000;
            long tSeq = end - start;

            Console.WriteLine("Time spent: " + tSeq);
            Console.WriteLine("Speed up: " + ((double)tSeq / tPar));
        }