Modelling two particles under gravity

297 views Asked by At

I have some code and I think it mostly works. I am supposed to be modelling the way two particles interact under gravity in 2D. It works until the second loop over the update function, so I think there is something wrong with the update function but I can't see it.

from __future__ import print_function, division
import numpy as np # imports the scientific computing package
G = 10 # of course G is not really 10 but this make it easier to track the accuracy of calculations.  
t=5 # defines the timestep

class Particle:
    def __init__(self,mass):
        self.mass = mass # gives the particle mass
        self.position = np.array([0,0],int) # gives the particle a position defined by a vector. Therefore all answers will now be given as vectors.    

    def calculateForce(self,other):
        '''
        this function calculates the force acting on the particles
        '''
        force = (G*self.mass*other.mass)/(other.position - self.position)**2
        print('The force acting on the particles is', force)
        return(force)

    def calculateAcceleration(self,force):
        '''
        calculates the acceleration of the first particle
        '''
        acceleration = (force/self.mass)    
        print('Acceleration of particle is', acceleration)
        return acceleration   

    def calculateAcceleration1(other, force):
        '''
        calculates the acceleration of the second particle
        '''
        acceleration1 = (force/other.mass)
        print('Acceleration of particle1 is', acceleration1)
        return acceleration1        

    def calculateVelocity(self, acceleration):
        '''
        calculates the velocity of the first particle
        '''
        velocity = (acceleration*t)
        print('Velocity of particle is', velocity)
        return velocity

    def calculateVelocity1(self, acceleration1):
        '''
        calculates the velocity of the second particle
        '''
        velocity1 = (acceleration1*t)
        print('Velocity of particle1 is', velocity1)
        return velocity1

    def calculatePosition(self, velocity):
        '''
        calculates the position of the first particle
        '''
        position = (velocity*t)
        print('Position of particle is', position)
        return position

    def calculatePosition1(self,velocity1):
        '''
        calculates the position of the second particle
        '''
        position1 = (velocity1*t)
        print('Position of particle1 is', position1)
        return position1


    def update(self,other):
       # for x in range(0,1):
            self.position = position
            other.position = position1
            print(self.position)
            print(other.position)
            return self.position
            return other.position

    def repeat(self,other):
        for x in range(0,5):
            force = p.calculateForce(p1) 
            acceleration = p.calculateAcceleration(force)
            acceleration1 = p1.calculateAcceleration1(force)
            velocity = p.calculateVelocity(acceleration)
            velocity1 = p1.calculateVelocity1(acceleration1)
            position = p.calculatePosition(velocity)
            position1 = p1.calculatePosition1(velocity1)
            p.update(p1)

p = Particle(10) # gives first particle a mass of 10
p1 = Particle(20) # gives second particle a mass of 20      
p1.position[0] = 5 # x coordinate of second particle is 5
p1.position[1] = 5 # y coordinate of second particle is 5  
print(p1.position)

force = p.calculateForce(p1)
acceleration = p.calculateAcceleration(force)
acceleration1 = p1.calculateAcceleration1(force)
velocity = p.calculateVelocity(acceleration)
velocity1 = p1.calculateVelocity1(acceleration1)
position = p.calculatePosition(velocity)
position1 = p1.calculatePosition1(velocity1)
p.update(p1)
p.repeat(p1)
1

There are 1 answers

0
Padraic Cunningham On BEST ANSWER

Your second return is unreachable in update:

 def update(self,other):
       # for x in range(0,1):
            self.position = position
            other.position = position1
            print(self.position)
            print(other.position)
            return self.position
            return other.position <- unreachable

If you wanted to return two values you could return a tuple of both and change your code appropriately to use the values:

 return self.position, other.position

If either one or the other should be returned then you need to add some logic.