building an n body simulator from scratch
play

Building an N-Body Simulator from scratch 29/03/2016 Philippos - PowerPoint PPT Presentation

Philippos Papaphilippou Building an N-Body Simulator from scratch 29/03/2016 Philippos Papaphilippou FYS 012 Physics and Applications The N-Body Problem The understanding of the motion of stars, planets and other space objects has


  1. Philippos Papaphilippou Building an N-Body Simulator from scratch 29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications

  2. The N-Body Problem ● The understanding of the motion of stars, planets and other space objects has always been desirable ● The N-Body Problem is the problem of predicting the motion of N objects when the only forces with which they interact are the forces described in Newton's Law of Universal Gravitation F 1 F 2 F 1 = F 2 = G m 1 ⋅ m 2 2 d ● Currently, the two-body problem has already been solved, but it remains unsolved for N > 2 ● Three-body motions has been described as chaotic 29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications

  3. The N-Body Problem (2) ● Sir Isaac Newton (1643-1727) has been studying the movement of planet orbits and concluded that they haven't all been following the motion equations created by the astronomers of the time ● Therefore he concluded that it was the gravitational forces that were affecting the objects orbits ● The two-body problem has been solved by Johann Bernoulli (1667-1748) ● The three-body problem has been studied by many, including Newton (1687), Euler (1767), Lagrange (1772) and Forest Ray Moulton (1917) 29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications

  4. The N-Body Problem (3) ● The three-body problem remained unsolved but there has been solutions for a restricted version of the problem that stated that one of the bodies has a negligible mass ● Newton implies in some of his writings that n-body problem may be unsolvable due to the gravitational forces ● 19th century's King Oscar II of Sweden announced that a prize would been given to anyone who could solve the n-body problem, mentioning the use of Newton's Law of Universal Gravitation properties 29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications

  5. N-Body Simulations ● N-Body simulators are tools that astrophysicists and astronomers use to predict the motions of solar objects ● They include from few-body system simulations such as for our solar systems to large-scale computations including formations of galaxy structures and effects from dark matter ● In computer science it is described to have a complexity of O(N 2 ) which means the time required is proportional to the square of the number of the bodies in the simulation. Though, optimizations exists 29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications

  6. N-Body Simulations (2) ● The first simulations were done by Sebastian von Hoerner at the Astronomisches Rechen-Institut in Heidelberg, Germany ● Image: CUDA Code Samples by NVIDIA include an N-Body simulator https://developer.nvidia.com/cuda-code-samples 29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications

  7. Parallelization ● Due to the problem's nature, it can be efficiently parallelized because at each time step there are many computations that can be performed independently for each body in the simulation ● Therefore they are usually performed in GPUs because of their massive number of processing cores ● They are also often used as performance benchmark for GPUs 29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications

  8. Implementation ● Each body has 4 parameters – Mass (kg) – Position in 3D Cartesian space (unit-vector, in meters) – Velocity in each axis (unit-vector, in m/s) – Net Force (unit-vector, in N) ● Random values – During the program's initialization, the first three get a random value according to the defined ranges – Net forces are recalculated according to the first 3 parameters of each object in each time step 29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications

  9. Implementation (2) ● Time steps – The time in which each objects are recalculated is in every predefined time step, for example for each 0.1 sec – Smaller time steps mean more accurate simulation – There is not an optimal time step for the current application but the selection is based on ● Humanly noticeable inaccuracy ● Calculations overhead ● Interesting motion speeds (in conjunction with the 60 frames per second limit) ● Vectors – Each calculation is done in unit-vectors because of simplicity in calculations – We could also use absolute values and directions but this would led to additional computations (for example Pythagorean theorem in order to find the motion and forces in each axis) 29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications

  10. Implementation Equations ● Each body has 4 parameters mass = m A position = [ z A ] velocity = [ V z ] Fnet = [ F z ] x A V x F x y A V y F y ● Vector form of the Newton's law of universal gravitation F 12 = G m 1 ⋅ m 2 ⃗ 3 ( ⃗ r 1 − ⃗ r 2 ) F 12 F 21 | r 12 | Unit vector distance Distance r 2 = [ z 1 − z 2 ] x 1 − x 2 | r 12 | = √ ( x 1 − x 2 ) 2 +( y 1 − y 2 ) 2 +( z 1 − z 2 ) 2 ,where G = 6.67408 ⃗ r 1 − ⃗ y 1 − y 2 29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications

  11. Implementation Equations (2) ● Vector form of displacement equation V A ⋅ t + 1 pos A = ⃗ a A ⋅ t 2 ⃗ 2 ⋅ ⃗ x A = V x ⋅ t + 1 z A = V z ⋅ t + 1 2 ⋅ a x ⋅ t 2 2 ⋅ a z ⋅ t 2 y A = V y ⋅ t + 1 2 ⋅ a y ⋅ t 2 ● Vector form of equation for velocity after plastic collision using momentum V = m 1 ⋅ ⃗ V 1 + m 2 ⋅ ⃗ V 2 ⃗ m 1 + m 2 (objects merge and we have conservation of momentum) 29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications

  12. Using JavaFX ● Java is a programming language designed by Sun Microsystems (now acquired by Oracle Corporation) in 1995 ● The main goals of Java was to be general purpose, platform independent/portable, object-oriented, high- level and widely available ● JavaFX was created in order to replace Swing, the existing rich content framework creation library ● JavaFX supports 3D graphics 29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications

  13. Using JavaFX (2) ● Advantages of using JavaFX for N-Body simulations – Excellent simple 3D visuals – Java is an object-oriented language ● Disadvantages – Not for complex 3D graphics – Not very suitable for parallelization and performance ● The simulator is based on Oracle's tutorial “JavaFX: Working with JavaFX Graphics” which explains how to draw simple 3D molecule structures http://docs.oracle.com/javase/8/javafx/graphics-tutorial/sampleapp3d.htm 29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications

  14. Screenshots ● Simulation Controls Time step in seconds Number of Bodies – N Add more computations Random mass for extra between 0 and accuracy … kg Enable Random Velocity merging between 0 and … (default m/s (for each option), dimension) otherwise Random Position unstable between 0 and … m (for each axis) Show motions (memory, CPU Restart overhead) Visualize Simulation Pause/Resume Velocities 29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications

  15. Screenshots (2) ● 2-body simulation 29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications

  16. Screenshots (3) ● 3-body simulation 29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications

  17. Screenshots (4) ● 1000-body simulation 29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications

  18. Screenshots (5) ● Other many-body simulations 29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications

  19. Source Code Statistics ● Lines of code – 1056 for my simulator (interface, 3D objects, interaction) – 173 for the 3D form (Ready from Oracle's tutorial for JavaFX 3D drawing) – And some Billion lines for JavaFX itself and the Operating System ● Estimated development hours – 30 minutes for JavaFX framework familiarization (as a computer science student) – 24 hours of total coding – Two weeks for developing 29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications

  20. Future Work ● Extra validation that the formulas are correct ● Parallelization using GPUs or/and multi-threading ● Optimize using common optimization techniques ● Save/Export current universe settings ● Prepare presets of realistic data like our solar system ● Give the option to change randomness distribution curves ● Custom object density ● Other types of collision reactions (such as bouncing etc.) 29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications

  21. END ● Website – http://www.cs.ucy.ac.cy/~ppapap01/nbody/ ● References – Newton's Law of Universal Gravitation ● Wikipedia aticle https://en.wikipedia.org/wiki/Newton's_law_of_universal_gravitation ● Learner.org article https://www.learner.org/courses/physics/unit/text.html?unit=3&secNum=3 – Momentum ● Wikipedia article https://en.wikipedia.org/wiki/Momentum – N-Body problem and simulations ● Wikipedia article https://en.wikipedia.org/wiki/N-body_problem ● Wikipedia article https://en.wikipedia.org/wiki/N-body_simulation – JavaFX ● JavaFX: Working with JavaFX Graphics - 8 Building a 3D Sample Application http://docs.oracle.com/javase/8/javafx/graphics-tutorial/sampleapp3d.htm ● Using the JavaFX AnimationTimer http://blog.netopyr.com/2012/06/14/using-the-javafx-animationtimer/ 29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications

Recommend


More recommend