StarSim

StarSim.git
git clone git://git.lenczewski.org/StarSim.git
Log | Files | Refs | README | LICENSE

commit 1d301abba7a3a0d2cdd6db39b5c90ad8cc11236b
parent 6a7e32f63d6ea86baf7c3abf9ff4974aae1d7ad7
Author: Mikolaj Lenczewski <33129490+EnderRifter@users.noreply.github.com>
Date:   Thu,  4 Jul 2019 22:54:09 +0100

Started debugging Barnes Hut algorithm due to instantiation issue.

Diffstat:
MStarSim/StarSim/Program.cs | 3++-
MStarSim/StarSimLib/Physics/BodyUpdater.cs | 48++++++++++++++++++++++++------------------------
2 files changed, 26 insertions(+), 25 deletions(-)

diff --git a/StarSim/StarSim/Program.cs b/StarSim/StarSim/Program.cs @@ -64,7 +64,8 @@ namespace StarSim bodyShapeMap = BodyGenerator.GenerateShapes(bodies); #if DEBUG - bodyPositionUpdater = BodyUpdater.UpdateBodiesBruteForce; + //bodyPositionUpdater = BodyUpdater.UpdateBodiesBruteForce; + bodyPositionUpdater = BodyUpdater.UpdateBodiesBarnesHut; #else bodyPositionUpdater = BodyUpdater.UpdateBodiesBarnesHut; #endif diff --git a/StarSim/StarSimLib/Physics/BodyUpdater.cs b/StarSim/StarSimLib/Physics/BodyUpdater.cs @@ -30,30 +30,6 @@ namespace StarSimLib.Physics public static void UpdateBodiesBarnesHut(IEnumerable<Body> bodies, double deltaTime) { IEnumerable<Body> bodyEnumerable = bodies as Body[] ?? bodies.ToArray(); - Vector4 forceVector = new Vector4(); - - foreach (Body body in bodyEnumerable) - { - // resets the force vector to avoid another instantiation and allocation - forceVector.X = 0; - forceVector.Y = 0; - forceVector.Z = 0; - - // use LINQ expression as it is more concise; sum attraction vectors for all other bodies - forceVector = bodyEnumerable.Where(b => b != body).Aggregate(forceVector, (current, b) => current + Body.GetForceBetween(body, b)); - - body.Update(forceVector, deltaTime); - } - } - - /// <summary> - /// Updates the positions of all the given <see cref="Body"/>s with O(n^2) time complexity, with the given time step. - /// </summary> - /// <param name="bodies">The collection of <see cref="Body"/>s whose positions to update.</param> - /// <param name="deltaTime">The time step.</param> - public static void UpdateBodiesBruteForce(IEnumerable<Body> bodies, double deltaTime) - { - IEnumerable<Body> bodyEnumerable = bodies as Body[] ?? bodies.ToArray(); OctantTree barnesHutTree = new OctantTree(UniverseOctant); // we construct our barnes-hut octant tree @@ -79,5 +55,29 @@ namespace StarSimLib.Physics } } } + + /// <summary> + /// Updates the positions of all the given <see cref="Body"/>s with O(n^2) time complexity, with the given time step. + /// </summary> + /// <param name="bodies">The collection of <see cref="Body"/>s whose positions to update.</param> + /// <param name="deltaTime">The time step.</param> + public static void UpdateBodiesBruteForce(IEnumerable<Body> bodies, double deltaTime) + { + IEnumerable<Body> bodyEnumerable = bodies as Body[] ?? bodies.ToArray(); + Vector4 forceVector = new Vector4(); + + foreach (Body body in bodyEnumerable) + { + // resets the force vector to avoid another instantiation and allocation + forceVector.X = 0; + forceVector.Y = 0; + forceVector.Z = 0; + + // use LINQ expression as it is more concise; sum attraction vectors for all other bodies + forceVector = bodyEnumerable.Where(b => b != body).Aggregate(forceVector, (current, b) => current + Body.GetForceBetween(body, b)); + + body.Update(forceVector, deltaTime); + } + } } } \ No newline at end of file