public static bool test(int n, string v1, string v2) {
     var bodies = new NBodySystem();
     if (!v1.equals("" + bodies.energy()))
     	return false;
     for (int i = 0; i < n; ++i)
        bodies.advance(0.01);
     return v2.equals("" + bodies.energy());
 }
    private void CreateBody(NBodySystem system, GravityObjectInitial init)
    {
        GameObject obj = Instantiate(system.GravityObjectPrefab);

        obj.transform.parent        = system.gameObject.transform;
        obj.transform.localPosition = init.StartPos;
        GravityObject gObj = obj.GetComponent <GravityObject>();

        gObj.SetInit(init);
    }
 public void BenchSetup()
 {
     bodyPtr    = (Body *)Marshal.AllocHGlobal(Marshal.SizeOf <Body>() * 6);
     rsPtr      = (RS *)Marshal.AllocHGlobal(3 * 1024 * sizeof(double));
     nBodyPP    = Marshal.AllocHGlobal(Marshal.SizeOf <BodyVector>() * 12);
     nBodyPtr   = (BodyVector *)((ulong)nBodyPP - ((ulong)nBodyPP + 16) % 16);
     rPtr       = (R *)Marshal.AllocHGlobal(Marshal.SizeOf <R>() * 1024);
     magPtr     = (double *)Marshal.AllocHGlobal(sizeof(double) * 1024);
     bodySystem = new NBodySystem(bodyPtr);
     NBodySystem.Initialize(nBodyPtr);
 }
Exemple #4
0
 public static void Main (String[] args) {
     int n = args.Length > 0 ? Int32.Parse(args[0]) : 10000;
     NBodySystem bodies = new NBodySystem();
   
     Console.WriteLine("{0:f9}", bodies.Energy());
   
     for (int i = 0; i < n; i++)
         bodies.Advance(0.01);
   
     Console.WriteLine("{0:f9}", bodies.Energy());
 }
Exemple #5
0
        public void Run(int arg)
        {
            NBodySystem bodies = new NBodySystem();

            Console.WriteLine("{0:f9}", bodies.Energy());
            for (int i = 0; i < arg; i++)
            {
                bodies.Advance(0.01);
            }
            Console.WriteLine("{0:f9}", bodies.Energy());
        }
Exemple #6
0
 public static void Bench()
 {
     int n = 5000000;
     foreach (var iteration in Benchmark.Iterations)
     {
         using (iteration.StartMeasurement())
         {
             NBodySystem bodies = new NBodySystem();
             for (int i = 0; i < n; i++) bodies.Advance(0.01);
         }
     }
 }
Exemple #7
0
    public static Tuple <double, double> Main(String[] args)
    {
        int         n      = args.Length > 0 ? Int32.Parse(args[0]) : 10000;
        NBodySystem bodies = new NBodySystem();
        var         s      = bodies.Energy(); // Console.WriteLine("{0:f9}", bodies.Energy());

        for (int i = 0; i < n; i++)
        {
            bodies.Advance(0.01);
        }
        return(Tuple.Create(s, bodies.Energy())); // Console.WriteLine("{0:f9}", bodies.Energy());
    }
Exemple #8
0
    public static void Main(String[] args)
    {
        int         n      = args.Length > 0 ? Int32.Parse(args[0]) : 10000;
        NBodySystem bodies = new NBodySystem();

        Console.WriteLine("{0:f9}", bodies.Energy());
        for (int i = 0; i < n; i++)
        {
            bodies.Advance(0.01);
        }
        Console.WriteLine("{0:f9}", bodies.Energy());
    }
Exemple #9
0
    public override void RunOnce()
    {
        NBodySystem bodies = new NBodySystem();

        WriteLine("{0:f9}", bodies.Energy());

        for (int i = 0; i < n; i++)
        {
            bodies.Advance(0.01);
        }

        WriteLine("{0:f9}", bodies.Energy());
    }
Exemple #10
0
    public static void Main()
    {
        Console.WriteLine("NBody");
        int         n      = 500000;
        NBodySystem bodies = new NBodySystem();

        Console.WriteLine(bodies.Energy());
        for (int i = 0; i < n; i++)
        {
            bodies.Advance(0.01);
        }
        Console.WriteLine(bodies.Energy());
    }
Exemple #11
0
    public static void AdvanceStaticIntr1(double dt, R *rPtr, double *magPtr, ref NBodySystem bodySystem, int count = 50000000)
    {
        Body *    items = bodySystem.bodies;
        const int N     = (BodyCount - 1) * BodyCount / 2;
        R *       r     = rPtr;
        double *  mag   = magPtr;

        {
            int      k     = 0;
            ref Body iBody = ref items[0];
            for (int j = 1; j < 5; ++j, ++k)
            {
                ref Body jBody = ref items[j];
                ref R    rf    = ref r[k];
    public static bool test(int n, string v1, string v2)
    {
        var bodies = new NBodySystem();

        if (!v1.equals("" + bodies.energy()))
        {
            return(false);
        }
        for (int i = 0; i < n; ++i)
        {
            bodies.advance(0.01);
        }
        return(v2.equals("" + bodies.energy()));
    }
Exemple #13
0
 public static int Main(String[] args)
 {
     int n = args.Length > 0 ? Int32.Parse(args[0]) : 10000;
     NBodySystem bodies = new NBodySystem();
     double initialEnergy = bodies.Energy();
     Console.WriteLine("{0:f9}", initialEnergy);
     for (int i = 0; i < n; i++) bodies.Advance(0.01);
     double finalEnergy = bodies.Energy();
     Console.WriteLine("{0:f9}", finalEnergy);
     double deltaEnergy = Math.Abs(initialEnergy - finalEnergy);
     bool result = deltaEnergy < 1e-4;
     Console.WriteLine("Energy {0} conserved", result ? "was" : "was not");
     return (result ? 100 : -1);
 }
Exemple #14
0
    public static void Test()
    {
        const int n = 10000;

        NBodySystem.Initialize();

        InitialEnergy = (long)Math.Floor(NBodySystem.Energy() * Scale);

        for (int i = 0; i < n; i++)
        {
            NBodySystem.Advance(0.01);
        }

        FinalEnergy = (long)Math.Floor(NBodySystem.Energy() * Scale);
    }
Exemple #15
0
    public static void Bench()
    {
        int n = 5000000;

        foreach (var iteration in Benchmark.Iterations)
        {
            using (iteration.StartMeasurement())
            {
                NBodySystem bodies = new NBodySystem();
                for (int i = 0; i < n; i++)
                {
                    bodies.Advance(0.01);
                }
            }
        }
    }
        public static double[] Main(String[] args)
        {
            int         n      = args.Length > 0 ? Int32.Parse(args[0]) : 10000;
            NBodySystem bodies = new NBodySystem();

            var result = new double[2];

            result[0] = bodies.Energy();

            for (int i = 0; i < n; i++)
            {
                bodies.Advance(0.01);
            }

            result[1] = bodies.Energy();
            return(result);
        }
        public unsafe void InitializeVerify()
        {
            double *bvPtr = stackalloc double[sizeof(BodyVector) / sizeof(double)];

            NBodySystem.Initialize((BodyVector *)bvPtr);

            Body *      bodies     = stackalloc Body[5];
            NBodySystem bodySystem = new NBodySystem(bodies);

            var result = NBodySystem.DumpBodyVector(5);

            Assert.NotNull(result);

            var expected = NBodySystem.DumpBodies(bodies, 5);

            Assert.NotNull(expected);

            Assert.Equal(expected, result);
        }
Exemple #18
0
    public static int Main(String[] args)
    {
        int         n             = args.Length > 0 ? Int32.Parse(args[0]) : 10000;
        NBodySystem bodies        = new NBodySystem();
        double      initialEnergy = bodies.Energy();

        Console.WriteLine("{0:f9}", initialEnergy);
        for (int i = 0; i < n; i++)
        {
            bodies.Advance(0.01);
        }
        double finalEnergy = bodies.Energy();

        Console.WriteLine("{0:f9}", finalEnergy);
        double deltaEnergy = Math.Abs(initialEnergy - finalEnergy);
        bool   result      = deltaEnergy < 1e-4;

        Console.WriteLine("Energy {0} conserved", result ? "was" : "was not");
        return(result ? 100 : -1);
    }
Exemple #19
0
    public static void Run(String[] args)
    {
        int n = args.Length > 0 ? Int32.Parse(args[0]) : 50_000; //50_000_000;

        Console.WriteLine("Calculating N-Body for {0} iterations", n);
        NBodySystem bodies = new NBodySystem();

        Console.WriteLine("{0:f9}", bodies.Energy());

        var dtStart = DateTime.UtcNow;

        for (int i = 0; i < n; i++)
        {
            bodies.Advance(0.01);
        }
        var elapsed = DateTime.UtcNow - dtStart;

        Console.WriteLine("{0:f9}", bodies.Energy());
        Console.WriteLine("Elapsed {0:f3} sec", elapsed.TotalSeconds);
    }
        public unsafe void AdvanceByOne()
        {
            double *bvPtr = stackalloc double[sizeof(BodyVector) / sizeof(double)];

            NBodySystem.Initialize((BodyVector *)bvPtr);

            Body *      bodies     = stackalloc Body[5];
            NBodySystem bodySystem = new NBodySystem(bodies);

            // Other data structures
            RS *    rsaPtr  = stackalloc RS[1];
            R *     rPtr    = stackalloc R[1024];
            double *magPtr  = stackalloc double[1024];
            double *mag2Ptr = stackalloc double[1024];

            NBodySystem.AdvanceStaticIntrSoa(0.01, rsaPtr, magPtr);
            var result = NBodySystem.DumpBodyVector(5);

            NBodySystem.AdvanceStaticIntr2(0.01, rPtr, mag2Ptr, ref bodySystem);
            var expected = NBodySystem.DumpBodies(bodies, 5);

            Assert.Equal(expected, result);
        }
Exemple #21
0
    public float DistanceScale = 1; //How many metres 1 unit represents

    private void Awake()
    {
        Instance = this;
    }
Exemple #22
0
        public static void Main(string[] args)
        {
            var target = "C#";

            if (args.Length > 0)
            {
                target = args[0];
            }
            Console.WriteLine($"============ {target} ============");
            {
                Console.WriteLine("##### N-Body #####");
                NBodySystem dummy = new NBodySystem();
                dummy.Advance(0.01);

                Func <Int32, TimeSpan> test = (ti) =>
                {
                    NBodySystem bodies = new NBodySystem();
                    var         e      = new Elapsed("11");
                    for (int i = 0; i < 50000000; i++)
                    {
                        bodies.Advance(0.01);
                    }
                    var d = e.Done();
                    Console.WriteLine($"Try #{ti} : {d}, {bodies.Energy()}");
                    return(d);
                };

                var times0 = new List <TimeSpan>(10);
                for (var i = 0; i < 10; i++)
                {
                    times0.Add(test(i));
                }

                var times   = times0.Where(x => x != times0.Min() && x != times0.Max()).ToList();
                var average = new TimeSpan((long)times.Select(ts => ts.Ticks).Average());
                //Console.WriteLine($"{target} [N-Body] : {average}\n");
                using (StreamWriter sw = File.AppendText("averages.txt"))
                {
                    sw.WriteLine($"{target} [N-Body] : {average}");
                }
            }
            {
                Console.WriteLine("##### Spectral-Norm #####");
                SpectralNorm.RunGame(0);

                Func <Int32, TimeSpan> test = (ti) =>
                {
                    var e = new Elapsed("11");
                    var r = SpectralNorm.RunGame(5500);
                    var d = e.Done();
                    //Console.WriteLine("{0:f9}", r);
                    Console.WriteLine($"Try #{ti} : {d}, {r}");
                    return(d);
                };

                var times0 = new List <TimeSpan>(10);
                for (var i = 0; i < 10; i++)
                {
                    times0.Add(test(i));
                }

                var times   = times0.Where(x => x != times0.Min() && x != times0.Max()).ToList();
                var average = new TimeSpan((long)times.Select(ts => ts.Ticks).Average());
                //Console.WriteLine($"{target} [Spectral-Norm] : {average}");
                using (StreamWriter sw = File.AppendText("averages.txt"))
                {
                    sw.WriteLine($"{target} [S-Norm] : {average}");
                }
            }
            {
                Console.WriteLine("##### Quicksort #####");
                Quicksort.DoSort(new Int32[3] {
                    0, 1, 2
                }, 0, 2);
                Func <Int32, TimeSpan> test = (ti) =>
                {
                    var n     = 50000000;
                    var r     = new Random();
                    var array = new Int32[n];
                    for (var i = 0; i < n; i++)
                    {
                        array[i] = r.Next(0, n);
                    }
                    var e = new Elapsed("");
                    Quicksort.DoSort(array, 0, n - 1);
                    var d = e.Done();
                    Console.WriteLine($"Try #{ti} : {d}, {array[1000]}");
                    return(d);
                };

                var times0 = new List <TimeSpan>(10);
                for (var i = 0; i < 10; i++)
                {
                    times0.Add(test(i));
                }

                var times   = times0.Where(x => x != times0.Min() && x != times0.Max()).ToList();
                var average = new TimeSpan((long)times.Select(ts => ts.Ticks).Average());
                //Console.WriteLine($"{target} [Spectral-Norm] : {average}");
                using (StreamWriter sw = File.AppendText("averages.txt"))
                {
                    sw.WriteLine($"{target} [Q-Sort] : {average}");
                }
            }
        }
        public unsafe void Initialize()
        {
            double *bvPtr = stackalloc double[sizeof(BodyVector) / sizeof(double)];

            NBodySystem.Initialize((BodyVector *)bvPtr);
        }
 public void AdvanceStaticIntr1()
 {
     NBodySystem.AdvanceStaticIntr1(0.01, rPtr, magPtr, ref bodySystem);
 }