math 4997 1
play

Math 4997-1 Lecture 4: N-Body simulations, Structs, Classes, and - PowerPoint PPT Presentation

Math 4997-1 Lecture 4: N-Body simulations, Structs, Classes, and generic functions Patrick Diehl https://www.cct.lsu.edu/~pdiehl/teaching/2020/4997/ This work is licensed under a Creative Commons Attribution-NonCommercial-


  1. Math 4997-1 Lecture 4: N-Body simulations, Structs, Classes, and generic functions Patrick Diehl https://www.cct.lsu.edu/~pdiehl/teaching/2020/4997/ This work is licensed under a Creative Commons “Attribution-NonCommercial- NoDerivatives 4.0 International” license.

  2. Reminder N -body simulations Structs Generic programming Summary References

  3. Reminder

  4. Lecture 3 What you should know from last lecture ◮ Iterators ◮ Lists ◮ Library algorithms ◮ Numerical limits ◮ Reading and Writing fjles

  5. N -body simulations

  6. N -body simulations 1 The N -body problem is the physically problem of predicting the individual motions of a group of celestial objects interacting with each other gravitationally. Informal description: Predict the interactive forces and true orbital motions for all future times of a group of celestial bodies. We assume that we have their quasi-steady orbital properties, e.g. instantaneous position, velocity and time. 1 By Michael L. Umbricht - Own work, CC BY-SA 4.0

  7. Recall: Vectors and basic operations Vectors 2. Direction: Inner product Cross product u = ( x , y , z ) ∈ R 3 x 2 + y 2 + z 2 1. Norm: | u | = � u | u | u 1 ◦ u 2 = x 1 x 2 + y 1 y 2 + z 1 z 2 u 1 × u 2 = | u 1 || u 2 | sin ( θ ) n where n is the normal vector perpendicular to the plane containing u 1 and u 2 .

  8. Stepping back: Two-body problem dt The universal constant of gravitation G was estimated as 3. The second Law of Mechanics: dt 2. The Calculus: Three defjnitions: is Let m i , m j be the masses of two gravitational bodies at the positions r i , r j ∈ R 3 1. The Law of Gravitation: The force of m i acting on m j r j − r i F ij = Gm i m j | r j − r i | 3 2.1 The velocity of m i is v i = d r i 2.2 The acceleration of m i is a i = d v i F = m a (Force is equal mass times acceleration) 6 . 67408 · 10 − 11 m 3 kg − 1 s − 2 in 2014 [8].

  9. Put all together: Equation of motion Derivation for the fjrst body: Note that we used Newton’s law of universal For the second body follows: m j m i gravitation [9]. r j − r i F ij = Gm i m j | r j − r i | 3 r j − r i m i a i = Gm i m j | r i − r j | 3 d v i r j − r i F i F j dt = Gm j | r j − r i | 3 d 2 r i r j − r i dt 2 = Gm j | r j − r i | 3 d 2 r j r 1 − r j dt 2 = Gm j | r i − r j | 3

  10. The N -body problem n m i m j G n n n 4. Energy: T-U=h with n 3. Angular Momentum: The force for body m i n 2. Center of Mass: More details: Simulations [2] and Astrophysics [1]. n Law of Conservation: Gm j n r j − r i F i = F ij = � � | r j − r i | 3 j =1 , i � = j j =1 ,, i � = j m i v i = M 0 � 1. Linear Momentum: i =1 m i r i = M 0 t + M 1 � i =1 m i ( r i × v i ) = c � i =1 m i v i ◦ v i , U = T = 1 � � � | r i − r j | 2 i =1 i =1 j =1

  11. Algorithm Compute the forces Update the positions Collect statistical information

  12. Complexity of force computation Force computation: Direct sum for(size_t i = 0; i < bodies.size(); i++) for(size_t j = 0; j < bodies.size(); j++) //Compute forces Advantage: Robust, accurate, and completely general Disadvantage: Tree-based codes or the Barnes-Hut method [3] reduce 1. Computational cost per body O ( n ) 2. Computational cost for all bodies O ( n 2 ) the computational costs to O ( n log ( n )) . More details [6].

  13. Update of positions Assume we have computed the forces already, using the direct sum approach and now we want to compute the evolution of the system over the time T : Discretization in time: ◮ ∆ t the uniform time step size ◮ t 0 the beginning of the evolution ◮ T the fjnal time of the evolution ◮ k the time steps such that k ∆ t = T Question: How can we compute the derivatives dt and dt 2 of the velocity v and the acceleration a of a body?

  14. Finite difgerence and Euler method derivations by (2) (1) m i Finite difgerence More details [10, 7, 5] We use the fjnite difgerence scheme to approximate the The Euler method h derivation by We can use a fjnite difgerence method to approximate the u ′ ( x ) ≈ u ( x + h ) − u ( x ) a i ( t k ) = F i = v i ( t k ) − v i ( t k − 1) ∆ t v i ( t k ) = r i ( t k +1 ) − r i ( t k ) ∆ t

  15. Compute the velocity and updated position Velocity Updated position Note that we used easy methods to update the positions and more sophisticated methods, e.g. Crank–Nicolson method [4], are available v i ( t k ) = v i ( t k − 1 ) + ∆ t F i m i using (1) r i ( t k +1 ) = r t k + ∆ t v i ( t k ) using (2)

  16. Structs

  17. Looking at the data structure 2 }; v.z=42; std::cout << v.x << std:endl; Reading/Writing elements struct vector v1 = {1,1,1}; struct vector v = {.x=1, .y=1, .z=1}; Initialization double z; For the N -body simulations, we need three dimensional double y; double x; struct vector { vectors having 2 https://en.cppreference.com/w/c/language/struct ◮ x Coordinate ◮ y Coordinate ◮ z Coordinate

  18. Constructor 3 Assign initial values struct A { int x; A(int x = 1): x(x) {}; }; A constructor has a Now struct A a; is equivalent to struct A a = {1} ; 3 https://en.cppreference.com/w/cpp/language/default_constructor ◮ Name A ◮ Arguments int x = 1 ◮ Assignment : x(x)

  19. Functions 5 Compute the norm of the vector #include <cmath> struct vector2 { double x , y , z; vector2(double x = 0, double y=0, double z=0) : x(x) , y(y) ,z(z) {} double norm(){ return std::sqrt(x*x+y*y+z*z);} } Usage struct vector2 v; std::cout << v.norm() << std::endl; 4 https://en.cppreference.com/w/cpp/header/cmath 5 https://en.cppreference.com/w/cpp/language/functions #include <cmath> 4 provides mathematical expressions

  20. Generic programming

  21. Why we need generic functions? Example //Compute the sum of two double values double add(double a, double b) { return a + b; } //Compute the sum of two float values float add(float a, float b) { return a + b; } Reasons: generic programming, e.g. std::vector<double> , std::vector<float> ◮ We have less redundant code ◮ The C++ standard library makes large usage of

  22. Function template 6 Writing a generic function: template <typename T> T add(T a, T b) { return a + b; } Using the generic function: std::cout << add<double >(2.0,1.0) << std::endl; std::cout << add<int>(2,1) << std::endl; std::cout << add<float >(2.0,1.0) << std::endl; Additional way to use the generic function: std::cout << add(2,1) << std::endl; 6 https://en.cppreference.com/w/cpp/language/function_template

  23. Generic structs 7 Writing a generic vector type template <typename T> struct vector { T x; T y; T z; }; Using a generic vector type struct vector<double > vd = {1.5,2.0,3.25}; struct vector<float> vf = {1.25,2.0,3.5}; struct vector<int> vi = {1,2,3}; 7 https://en.cppreference.com/w/cpp/language/templates

  24. Example Generic struct having functions #include <cmath> template <typename T> struct vector { T x , y , z; vector( T x = 0, T y=0, T z=0) : x(x) , y(y) ,z(z) {} T norm() { return std::sqrt(x*x+y*y+z*z);} T cross(struct vector<T> b) {return x*b.x+y*b.y+z*b.z;} }; What we need to defjne the vector data structure: ◮ Structs ◮ Generic functions

  25. Summary

  26. Summary After this lecture, you should know Further reading: 8 https://www.youtube.com/watch?v=iU3wsiJ5mts 9 https://www.youtube.com/watch?v=6PWUByLZO0g ◮ N -Body simulations ◮ Structs ◮ Generic programming (Templates) ◮ C++ Lecture 2 - Template Programing 8 ◮ C++ Lecture 4 - Template Meta Programming 9

  27. References

  28. References I [1] Sverre Aarseth, Christopher Tout, and Rosemary Mardling. The Cambridge N-body lectures , volume 760. Springer, 2008. [2] Sverre J Aarseth. Gravitational N-body simulations: tools and algorithms . Cambridge University Press, 2003. [3] Josh Barnes and Piet Hut. A hierarchical o (n log n) force-calculation algorithm. nature , 324(6096):446, 1986.

  29. References II Leonhard Euler. Algorithms , volume 1. The art of computer programming: Fundamental Donald Ervin Knuth. [6] impensis Academiae imperialis scientiarum, 1824. Institutionum calculi integralis , volume 1. [5] [4] Cambridge University Press, 1947. Philosophical Society , volume 43, pages 50–67. In Mathematical Proceedings of the Cambridge heat-conduction type. solutions of partial difgerential equations of the A practical method for numerical evaluation of John Crank and Phyllis Nicolson. Pearson Education, 1968.

  30. References III physical constants: 2014. volume 1. Philosophiae naturalis principia mathematica , Isaac Newton. [9] 45(4):043102, 2016. Journal of Physical and Chemical Reference Data , Codata recommended values of the fundamental [7] Peter J Mohr, David B Newell, and Barry N Taylor. [8] Siam, 2007. time-dependent problems , volume 98. difgerential equations: steady-state and Finite difgerence methods for ordinary and partial Randall J LeVeque. G. Brookman, 1833.

  31. References IV [10] John C Strikwerda. Finite difgerence schemes and partial difgerential equations , volume 88. Siam, 2004.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend