2015-06-07 7 views
0

У меня есть код, и я думаю, что он работает в основном. Я должен моделировать способ взаимодействия двух частиц под действием силы тяжести в 2D. Он работает до второго цикла над функцией update, поэтому я думаю, что что-то не так с функцией update, но я не вижу ее.Моделирование двух частиц под действием силы тяжести

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

Ваше второе возвращение недостижимо обновление:

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 

Если вы хотели вернуть два значения, которые вы могли бы вернуть кортеж как и изменить код соответствующим образом использовать значение:

return self.position, other.position 

Если один или другой должен быть возвращен, вам нужно добавить некоторую логику.

Смежные вопросы