Computational Physics - modelling the two-dimensional gravitational problem by C++.


Published on

Computational Physics - modelling the two-dimensional gravitational problem by C++.

Published in: Education, Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Computational Physics - modelling the two-dimensional gravitational problem by C++.

  1. 1. Sultan LeMarc Computational Physics 30/03/2012 The Two Dimensional Gravitational Problem Objective The aim of this project is to model the two-dimensional gravitational problem by a program using the C++ language. The objective of this program is to read, in two dimensions, the initial positions, velocities and masses of two bodies and use Newton’s law of universal gravitation to calculate the two-dimensional forces on each body. It follows this by using Newton’s second law to calculate the acceleration of the bodies in a given time frame in order to establish their movements of the bodies. A file is generated by the program with the positions of both bodies at each time step over a whole time period, which is then used to plot the paths of the two bodies. The two-body gravitational problem concerns the motion of two point masses that interact only with each other due to gravity. Such models are approximated commonly in the Universe, for example the Earth’s orbit around the Sun, the moon’s orbit around the Earth, and two starts orbiting each other. Newton’s Law of Universal Gravitation: Newton published his work on the law of gravity in 1967, introducing the law of universal gravitation, which states that every particle in the Universe attracts every other particle with a force that is directly proportional to the product of their masses and inversely proportional to the square of the distance between them (Serway, 2010). For two bodies, of masses m1 and m2(kg), separated by a distance r (m), the magnitude of the gravitational force, in Newtons (N), is: where G is a constant called the universal gravitational constant. In SI units, it’s value is: This is often referred to as the inverse-square law, as the force varies as the inverse square of the separation of the bodies. As the constant G is used throughout the program, it is declared as a fixed variable by const double G=6.67300E-11. The program makes use of 1D arrays of size [2] for the two-dimensional components of positionpos_m[2], velocity vel_m[2], acceleration acc_m[2],
  2. 2. and force gforce_m[2]. Each mass is allocated an array for each of these variables. In order to find the distance that separates the two bodies, r, from the initial x and y positions, the change in each component is used in: This is coded as: dx = pos_m2[0]-pos_m1[0] dy = pos_m2[1]-pos_m1[1] r = sqrt ((dx*dx)+(dy*dy)) This allows the gravitational force acting on each body by the other along the line of action to be found. The two dimensional components of this force is found using trigonometric principles. First, the program finds the angle between the line of action and the x-axis by: This is coded as angle=atan2(dy,dx); This angle is then used to resolve the components of the calculated force: Newton’s third law, which states that if two bodies interact, the force F12 exerted by body 1 on body 2 is equal in magnitude and opposite in direction to the force F21 exerted by body 2 on body 1: Therefore the program implements this by equating the component forces of body 2 as the negative equivalents of the component forces of body 1: gforce_m1[0] = F*cos(angle); gforce_m1[1] = F*sin(angle); gforce_m2[0] = -F*cos(angle); gforce_m2[1] = -F*sin(angle); The two dimensional acceleration is found from these forces by using Newton’s second law, which states that the acceleration of an object is directly proportional to the net force acting on it and inversely proportional to its mass (Serway, 2010):
  3. 3. This is applied for each component of the forces for both bodies by: ax1 = Fx1/m1:acc_m1[0] = gforce_m1[0]/m1; ay1 = Fy1/m1:acc_m1[1] = gforce_m1[1]/m1 ax2= Fx2/m2: acc_m2[0] = gforce_m2[0]/m2; ay2 = Fy2/m2: acc_m2[1] = gforce_m2[1]/m2; The acceleration is used to find the change in velocity of each body in a given time step, but first the time step ∆t is determined from the calculated period T. The program assumes that the motion is an orbit. The period of the orbit is determined by the consideration of orbital energy conservation, which requires that the sum of kinetic and potential energies be constant at all points in the motion. The equation for orbital energy conservation, also known as “Vis-Viva” is: wherevr is the relative velocity given by: This is coded as:dv[0] = vel_m2[0]-vel_m1[0]; dv[1] = vel_m2[1]-vel_m1[1]; rel_vel = sqrt((dv[0]*dv[0])+(dv[1]*dv[1])); The program uses this value to find orbital energy by: orb_energy = (0.5*rel_vel*rel_vel)-(G*(m1+m2)/r); This orbital energy acts as the condition which defines the type of orbit experienced by the bodies:  ϵ < 0: Elliptical orbit
  4. 4. This corresponds to the minimum and maximum values of r for an elliptical orbit where the distances of closest and farthest approach are equal to r0/1-ε.  ϵ = 0: Parabolic orbit This corresponds to the escape velocity condition as a body is outside the bounds of another body’s gravitational force when energy is equivalent to zero i.e. .  ϵ > 0: Hyperbolic orbit ( In a parabolic or hyperbolic trajectory the motion is not periodic, and the duration of the full trajectory is infinite. Therefore the programprints an error message and aborts program upon the result of a parabolic or hyperbolic orbit when testing for the orbital energy conditions. The semi-major axis is half of the major axis of an ellipse which has a length from the centre to the edge of the ellipse along its principle axes. The orbital energy is used to find the semi-major axis, a, of the elliptical orbit: This is coded by sm_axis = -(G*(m1+m2))/(2*orb_energy). With the semi-major axis value, the period T of the orbit is determined through Kepler’s third law, which states that the square of the orbital period of a planet is directly proportional to the cube of the semi-major axis of its orbit: For this the value of π is declared as a fixed variable to 8 significant figures: const double pi = 3.1415926; The calculation for T is coded as: T = sqrt(fabs((4*pi*pi*sm_axis*sm_axis*sm_axis)/(G*(m1+m2)))); The program determines the time step ∆t according to this calculated period by setting . Having established the time step, the new velocities are found by accumulating the change in velocity, as related by acceleration and the time: .
  5. 5. As with acceleration, this is done for each of the components of velocity for both bodies: vel_m1[0] += acc_m1[0]*dt; vel_m1[1] += acc_m1[1]*dt; vel_m2[0] += acc_m2[0]*dt; vel_m2[1] += acc_m2[1]*dt; Finally, the new positions of the bodies are calculated using the following equation of motion: where s is the displacement and u is the initial velocity. As the calculation takes within a for loop where the new velocity after each time step becomes the initial velocity for the next and likewise with position, it is coded as: pos_m1[0] += ((vel_m1[0]*dt)+(0.5*acc_m1[0]*dt*dt)); pos_m1[1] += ((vel_m1[1]*dt)+(0.5*acc_m1[1]*dt*dt)); pos_m2[0] += ((vel_m2[0]*dt)+(0.5*acc_m2[0]*dt*dt)); pos_m2[1] += ((vel_m2[1]*dt)+(0.5*acc_m2[1]*dt*dt)); The program prints the positions of both bodies into a 2D array, position, of size[1001][4] and writes a spread sheet file for excel which can then be plotted. Assumptions  The bodies are point particles; their sizes are negligible centre-to-centre distance is not considered.  If orbital energy is less than zero then orbiting body has two turning points, rmin and rmax. Body gets no closer than rminto central body and no further than rmax due to conservation of angular momentum.  The gravitational fields of the bodies are uniform.  The motion of the bodies as a result of the interaction is closed and periodic i.e. elliptical or circular orbits. Open orbits of parabolic or hyperbolic give void readings.  The two bodies are isolated from the Universe, therefore do not experience gravitational force from any other body i.e. only one central force acting.  Centre of mass of the isolated system is not accelerated.  The larger of the two masses is stationary and fixed at the origin of the system.
  6. 6. Figure 1: Plot of X-Y positions of two masses, calculated by program. Program input values: Mass 1: 2E30 Initial Position: 0, 0 Initial Velocity: 0, 0 Mass 2: 6E24 Initial Position: 1.496E11, 0 Initial Velocity: 0, 29E3