Simulate the laws of Newton gravity

Here’s an example Python program that simulates the laws of Newton gravity:

pythonCopy codeimport math

class Particle:
    def __init__(self, mass, position, velocity):
        self.mass = mass
        self.position = position
        self.velocity = velocity
        self.acceleration = (0, 0)

    def update_position(self, dt):
        x = self.position[0] + self.velocity[0]*dt
        y = self.position[1] + self.velocity[1]*dt
        self.position = (x, y)

    def update_velocity(self, dt):
        vx = self.velocity[0] + self.acceleration[0]*dt
        vy = self.velocity[1] + self.acceleration[1]*dt
        self.velocity = (vx, vy)

    def update_acceleration(self, force):
        ax = force[0] / self.mass
        ay = force[1] / self.mass
        self.acceleration = (ax, ay)

class Simulation:
    def __init__(self, particles, duration, dt):
        self.particles = particles
        self.duration = duration
        self.dt = dt
        self.G = 6.6743e-11

    def calculate_force(self, p1, p2):
        distance = ((p1.position[0] - p2.position[0])**2 +
                    (p1.position[1] - p2.position[1])**2)**0.5
        force = (self.G * p1.mass * p2.mass / distance**2, 0)
        angle = math.atan2(p2.position[1] - p1.position[1], p2.position[0] - p1.position[0])
        force = (force[0] * math.cos(angle), force[0] * math.sin(angle))
        return force

    def simulate(self):
        for i in range(int(self.duration / self.dt)):
            for p1 in self.particles:
                for p2 in self.particles:
                    if p1 == p2:
                        continue
                    force = self.calculate_force(p1, p2)
                    p1.update_acceleration(force)
                p1.update_velocity(self.dt)
                p1.update_position(self.dt)
                print(f"Step {i+1}, Particle {self.particles.index(p1)}: Position = {p1.position}, Velocity = {p1.velocity}, Acceleration = {p1.acceleration}")

p1 = Particle(1, (0, 0), (0, 0))
p2 = Particle(1e+12, (1e+8, 0), (0, 30))
sim = Simulation([p1, p2], duration=86400*365*10, dt=86400)
sim.simulate()

This program defines a class Particle that represents a particle in a two-dimensional space. Each particle is characterized by its mass, position, velocity, and acceleration. The class includes methods to update the position, velocity, and acceleration of the particle based on the laws of Newtonian mechanics.

The program also defines a class Simulation that represents a simulation of particles interacting with each other based on the laws of Newton gravity. The class includes a calculate_force() method that calculates the force acting on a particle due to another particle based on the law of gravity. The class includes a simulate() method that iterates over the particles and updates their positions, velocities, and accelerations based on the forces acting on them.

In this example program, we create two particles, p1 and p2, and simulate their motion for 10 years with a time step of 1

Leave a Comment