pythonnumerical-methodsorbital-mechanicsverlet-integration

Leapfrog algorithm to compute a objects trajectory in a Gravitational field around a central body (Python 3.8.2)


I pretty much deleted the last code and started new. I added a new class called Object which is the replacement for the lists called body_1 and body_2. Also all the calculations are now done from within the Object class. Most previous existing issues were resolved through this process but there is still one that presists. I believe its inside the StartVelocity() function which creates the v1/2 needed to start the Leapfrog algorithm. This should give me a geostationary Orbit but as clearly visible the Satelite escapes very quickly after zooming through earth. enter image description here

Codes are:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
from object import Object
import numpy as np


class Simulation:

    def __init__(self):

        # Index: 0 Name, 1 Position, 2 Velocity, 3 Mass
        body_1 = Object("Earth", "g", "r",
                        np.array([[0.0], [0.0], [0.0]]),
                        np.array([[0.0], [0.0], [0.0]]), 
                        5.9722 * 10**24)

        body_2 = Object("Satelite", "b", "r",
                        np.array([[42164.0], [0.0], [0.0]]),
                        np.array([[0.0], [3075.4], [0.0]]),
                        5000.0)
        
        self.bodies = [body_1, body_2]

    def ComputePath(self, time_limit, time_step):

        time_range = np.arange(0, time_limit, time_step)
        
        for body in self.bodies:

            body.StartVelocity(self.bodies, time_step)

        for T in time_range:

            for body in self.bodies:

                body.Leapfrog(self.bodies, time_step)

    def PlotObrit(self):

        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')
        
        for body in self.bodies:

            body.ReshapePath()
            X, Y, Z = [], [], []

            for position in body.path:

                X.append(position[0])
                Y.append(position[1])
                Z.append(position[2])

            ax.plot(X, Y, Z, f"{body.linecolor}--")
        
        for body in self.bodies:
            
            last_pos = body.path[-1]
            ax.plot(last_pos[0], last_pos[1], last_pos[2], f"{body.bodycolor}o", label=body.name)

        ax.set_xlabel("x-Axis")
        ax.set_ylabel("y-Axis")
        ax.set_zlabel("z-Axis")
        ax.legend()
        fig.savefig("Leapfrog.png")

if __name__ == "__main__":

    sim = Simulation()
    sim.ComputePath(0.5, 0.01)
    sim.PlotObrit()
import numpy as np


class Object:
    
    def __init__(self, name, bodycolor, linecolor, pos_0, vel_0, mass):

        self.name = name
        self.bodycolor = bodycolor
        self.linecolor = linecolor
        self.position = pos_0
        self.velocity = vel_0
        self.mass = mass

        self.path = []
    
    def StartVelocity(self, other_bodies, time_step):

        force = self.GetForce(other_bodies)
        self.velocity += (force / self.mass) * time_step * 0.5

    def Leapfrog(self, other_bodies, time_step):

        self.position += self.velocity * time_step
        self.velocity += (self.GetForce(other_bodies) / self.mass) * time_step

        self.path.append(self.position.copy())

    def GetForce(self, other_bodies):
        
        force = 0
    
        for other_body in other_bodies:
            if other_body != self:

                force += self.Force(other_body)

        return force

    def Force(self, other_body):
        
        G = 6.673 * 10**-11

        dis_vec = other_body.position - self.position
        dis_mag = np.linalg.norm(dis_vec)
        dir_vec = dis_vec / dis_mag
        for_mag = G * (self.mass * other_body.mass) / dis_mag**2
        for_vec = for_mag * dir_vec

        return for_vec
    
    def ReshapePath(self):

        for index, position in enumerate(self.path):

            self.path[index] = position.reshape(3).tolist()

Im aware that Body 2's position has to be multiplied by 1000 to get meters but it would just fly in a straight line if i would do that and there would be no signs of gravitational forces what so ever.


Solution

  • The constant G is in kg-m-sec units. The radius of the satellite orbit however only makes sense in km, else the orbit would be inside the Earth core. Then the speed in m/sec gives a near circular orbit with negligible eccentricity. (Code from a math.SE question on Kepler law quantities)

    import math as m
    G     = 6.673e-11*1e-9 # km^3 s^-2 kg^-1
    M_E   = 5.9722e24 # kg
    R_E   = 6378.137 # km
    R_sat = 42164.0 # km from Earth center
    V_sat = 3075.4/1000  # km/s
    
    theta = 0
    r0 = R_sat
    dotr0 = V_sat*m.sin(theta)
    dotphi0 = -V_sat/r0*m.cos(theta)
    
    R = (r0*V_sat*m.cos(theta))**2/(G*M_E)
    wx = R/r0-1; wy = -dotr0*(R/(G*M_E))**0.5
    E = (wx*wx+wy*wy)**0.5; psi = m.atan2(wy,wx)
    T = m.pi/(G*M_E)**0.5*(R/(1-E*E))**1.5
    print(f"orbit constants R={R} km, E={E}, psi={psi} rad")
    print(f"above ground: min={R/(1+E)-R_E} km, max={R/(1-E)-R_E} km")
    print(f"T={2*T} sec, {T/1800} h")
    

    with output

    orbit constants R=42192.12133271948 km, E=0.0006669512550867562, psi=-0.0 rad
    above ground: min=35785.863 km, max=35842.14320159004 km
    T=86258.0162673565 sec, 23.960560074265697 h
    

    for r(phi)=R/(1+E*cos(phi-psi))

    Implementing these changes in your code and calling with

        sim.ComputePath(86e+3, 600.0)
    

    gives a nice circular orbit

    enter image description here