我有一些代码,我认为它大部分都有效。我应该模拟两个粒子在重力作用下在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)
答案 0 :(得分: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
如果要返回其中一个,那么您需要添加一些逻辑。