在 for 循环中调用和演化函数

Calling and evolving a function inside of a for loop

我的主要函数 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 是时间步长值。 calculateTrajectories 函数中的 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?

感谢您对此的支持!

updateParticles 使用原始位置和速度连续调用此行:

Positions, Velocities = updateParticles(masses, positions, velocities, dt)

需要使用最新的位置和速度,如:

Positions, Velocities = updateParticles(masses, current_positions[-1], current_velocities[-1], dt)

新的计算轨迹函数

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