0
我有一些代碼,我認爲它主要工作。我應該模擬兩種粒子在重力作用下在二維中相互作用的方式。它一直運行到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)