29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications
Building an N-Body Simulator from scratch 29/03/2016 Philippos - - PowerPoint PPT Presentation
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
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 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
- Currently, the two-body problem has already been solved,
but it remains unsolved for N > 2
- Three-body motions has been described as chaotic
F1 F2
F1 = F2 = G m1⋅m2 d
2
29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications
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
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
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
- f O(N2) 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
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
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
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
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
Implementation Equations
- Each body has 4 parameters
- Vector form of the Newton's law of universal gravitation
mass = mA position = [ x A y A zA] velocity = [ V x V y V z] Fnet = [ F x F y F z]
⃗
F12 = G m1⋅m2 |r12|
3 (⃗
r1−⃗ r 2)
,where G = 6.67408 ⃗ r1−⃗ r2 = [ x1−x2 y1− y2 z1−z2] |r12| = √(x1−x2)
2+( y1− y2) 2+(z1−z2) 2
Distance Unit vector distance F12 F21
29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications
Implementation Equations (2)
- Vector form of displacement equation
- Vector form of equation for velocity after plastic
collision using momentum
(objects merge and we have conservation of momentum)
⃗
pos A = ⃗ V A⋅t + 1 2⋅⃗ aA⋅t 2 x A = V x⋅t + 1 2⋅ax⋅t2 y A = V y⋅t + 1 2⋅a y⋅t2 z A = V z⋅t + 1 2⋅az⋅t2
⃗
V = m1⋅⃗ V 1 + m2⋅⃗ V 2 m1+m2
29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications
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
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
Screenshots
- Simulation Controls
Number of Bodies – N Random mass between 0 and … kg Random Velocity between 0 and … m/s (for each dimension) Random Position between 0 and … m (for each axis) Time step in seconds Add more computations for extra accuracy Enable merging (default
- ption),
- therwise
unstable Show motions (memory, CPU
- verhead)
Visualize Velocities Restart Simulation Pause/Resume
29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications
Screenshots (2)
- 2-body simulation
29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications
Screenshots (3)
- 3-body simulation
29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications
Screenshots (4)
- 1000-body simulation
29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications
Screenshots (5)
- Other many-body simulations
29/03/2016 Philippos Papaphilippou – FYS 012 – Physics and Applications
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
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
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/