我的主要功能calculateTrajectories定义如下:
def calculateTrajectories(masses, positions, velocities, T, dt):
#create lists for where we want to know positions, velocities at some time and convert them to numpy arrays
current_positions = []
current_velocities = []
#call updateParticles function to get new positions, velocities at each step
#loop starts at 0, ends at T, has step value of dt
for i in range(0, T, dt):
#show all the time steps in the total time range
steps = np.array(i)
#call updateParticles function
Positions, Velocities = updateParticles(masses, positions, velocities, dt)
#assign the position and velocity results to their respective lists to get turned into arrays
current_positions.append(Positions)
current_velocities.append(Velocities)
#convert lists into numpy arrays
new_positions = np.array(current_positions)
new_velocities = np.array(current_velocities)
#to make sure the calculation is working
print(f"computing positions, velocities for time step {i}")
return steps, new_positions, new_velocities
updateParticles函数使用跨越式积分器方案,该方案在给定的时间范围内使粒子演化。它采用参数(质量,位置,速度,dt),其中dt是时间步长值。 computeTrajectories函数中的T参数是计算必须经过的总时间。
我正在尝试将下面的输入输入上述函数:
#from 1000 days into seconds
T4 = 86400000
#step value
dt4_test = 864000
#in kg
masses4 = [1.989e30, 5.972e24]
#converted to meters
positions4 = [(-448794, 0.0, 0.0),(1.4959742e11, 0.0, 0.0)]
#in m/s
velocities4 = [(0.0, -8.94e02, 0.0),(0.0, 2.98e4, 0.0)]
calculation4 = calculateTrajectories(masses4, positions4, velocities4, T4, dt4_test)
print(calculation4)
我得到正确的打印语句,告诉步值,并且我也获得了new_positions和new_velocities的数组。但是,位置和速度数组不会更改,它们在每个步骤中都包含相同的值。
如何编辑我的calculateTrajectories函数,以便它使用最近的位置和速度进行计算,以便获得下一个和下一个,依此类推,直到所需的时间结束?换句话说,如何在for循环中调用我的updateParticles函数,以便它始终使用循环最后一步中的new_positions和new_velocities?
感谢您对此的支持!
Positions, Velocities = updateParticles(masses, positions, velocities, dt)
需要使用最新的位置和速度,如:
Positions, Velocities = updateParticles(masses, current_positions[-1], current_velocities[-1], dt)
新的calculateTrajectories函数
def calculateTrajectories(masses, positions, velocities, T, dt): #create lists for where we want to know positions, velocities at some time and convert them to numpy arrays current_positions = [positions] current_velocities = [velocities] #call updateParticles function to get new positions, velocities at each step #loop starts at 0, ends at T, has step value of dt for i in range(0, T, dt): #show all the time steps in the total time range steps = np.array(i) #--this could be moved outside the for loop # call updateParticles function--pass in the latest position and velocities (i.e. last index of each array) Positions, Velocities = updateParticles(masses, current_positions[-1], current_velocities[-1], dt) # Update position and velocities current_positions.append(Positions) current_velocities.append(Velocities) #to make sure the calculation is working print(f"computing positions, velocities for time step {i}") #assign the position and velocity results to their respective lists to get turned into arrays # only need to convert to bumpy arrays once so moved outside for loop new_positions = np.array(current_positions) new_velocities = np.array(current_velocities) return steps, new_positions, new_velocities