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

8
Sultan LeMarc 30/03/2012 Computational Physics 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).

description

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

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

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

Sultan LeMarc 30/03/2012Computational Physics

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:

F=Gm1m2r2

where G is a constant called the universal gravitational constant. In SI units, it’s value is:

G=6.674×10−11N ∙m2/kg2

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 position pos_m[2], velocity vel_m[2], acceleration acc_m[2],

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

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:

∆ x=x2−x1 ,∆ y= y2− y1

r=√¿¿

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:

θ=tan−1( dydx )This is coded as angle=atan2(dy,dx);

This angle is then used to resolve the components of the calculated force:

F x=Fcosθ , F y=Fsinθ

Newton’s third law, which states that if two bodies interact, the force F 12 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:

Newton' s third law : F⃗12=F⃗21

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):

New ¿n' second law : a= Fm

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

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:

ϵ=vr2

2−G¿¿

where vr is the relative velocity given by:∆ vx=vx 2−vx 1 ,∆ v y=v y 2−v y 1

v=√ (∆ vx )2+(∆v y )2

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 orbitThis 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-ε.

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

ϵ = 0: Parabolic orbitThis 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. r→∞.

ϵ > 0: Hyperbolic orbit(MIT.edu)

In a parabolic or hyperbolic trajectory the motion is not periodic, and the duration of the full trajectory is infinite. Therefore the program prints 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:

a=−G(m1+m2)2 ϵ

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:

Keple r ' s Period Law :T 2= 4 π2a3

G(m1+m2)

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 ∆ t=

T

10−6 .

Having established the time step, the new velocities are found by accumulating the change in velocity, as related by acceleration and the time:∆ v=a∆ t.

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;

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

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:

s=ut+ 12a t 2

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 rmin to 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.

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

Figure 1: Plot of X-Y positions of two masses, calculated by program.

Program input values:

Mass 1: 2E30Initial Position: 0, 0Initial Velocity: 0, 0

Mass 2: 6E24Initial Position: 1.496E11, 0Initial Velocity: 0, 29E3