[英]Appending an array in a while loop
我試圖將值從新創建的數組追加到以前創建的數組,然后運行while循環以執行此操作10,000次。 這是我到目前為止的內容:
def evolve(m_all, pp_all, pv_all, tf, dt):
"""
Evolves the initial positions and initial velocites of particles for a given time step and total time, then
updates their positions.
Parameters
----------
m_all : np.ndarray
1-D array containing masses for all particles, length N, where
N is the number of particles.
pp_all : np.ndarray
2-D array containing (x, y, z) positions for all particles.
Shape is (N, 3) where N is the number of particles.
pv_all : np.ndarray
2-D array containing (x, y, z) velocities for all particles.
Shape is (N, 3) where N is the number of particles.
tf : float
The total time to evolve the system.
dt : float
Evolve system for time dt.
Returns
-------
partpos : N-D array
N-D array with the updated particle postions for each time step, dt.
partvel : N-D array
N-D array with the updated particle velocities for each time step, dt.
"""
partpos = np.zeros((10000, 3)) # create empty array with 10000 elements
partvel = np.zeros((10000, 3)) # create empty array with 10000 elements
t = 0 # initial time
i = 0
while t < tf:
new_positions, new_velocities = \
evolve_particles(pp_all, pv_all, m_all, dt)
t += dt # add time step
partpos[i] = new_positions[i]
partvel[i] = new_velocities[i]
i += 1
pp_all = new_positions
pv_all = new_velocities
return partpos, partvel
我正在嘗試將new_positions和new_velocities數組中新創建的值附加到partpos和partvel數組中,但是出現以下錯誤:
IndexError: index 2 is out of bounds for axis 0 with size 2
我將如何解決這個問題? 以下是輸入:
m_all = np.array([1.98e30, 5.972e24]) # the array of masses
N = len(m_all)
x = np.array([0, 0, 0, 1.5e11, 0, 0]) # an array of all the positions
pp_all = np.reshape(x, (N, 3)) # reshapes position array into Nx3 array
v = np.array([0, 0, 0, 0, 29779.5, 0]) # array of velocities
pv_all = np.reshape(v, (N, 3)) # reshapes velocity array into Nx3 array
tf = (2 * np.pi * 1.5e11) / 29779.5 # calculates total time
dt = tf / 10e4 # calculate the time step
該代碼應使用10,000次步長來模擬地球繞太陽的軌道。 new_positions和new_velocities均為2D數組。 謝謝!
這是evolution_particles函數:
def evolve_particles(pp_all, pv_all, m_all, dt):
"""
Evolve particles in time via leap-frog integrator scheme.
Parameters
----------
pp_all : np.ndarray
2-D array containing (x, y, z) positions for all particles.
Shape is (N, 3) where N is the number of particles.
pv_all : np.ndarray
2-D array containing (x, y, z) velocities for all particles.
Shape is (N, 3) where N is the number of particles.
m_all : np.ndarray
1-D array containing masses for all particles, length N, where
N is the number of particles.
dt : float
Evolve system for time dt.
Returns
-------
Updated particle positions and particle velocities, each being a 2-D
array with shape (N, 3), where N is the number of particles.
"""
# Make copies of position/velocity arrays that we can modify in-place.
pp = pp_all.copy()
pv = pv_all.copy()
N = len(m_all) # Number of particles in system
dims = pp_all.shape[-1] # Dimensionality of problem
# Compute net force vectors on all particles
forces = netGravForces(m_all, pp_all)
# Leap-frog method takes two half-steps (first dimension of acc array)
acc = np.zeros([2,N,dims])
# Leap-frog integrator takes two half-steps
step = 0
while step < 2:
# Loop over particles, compute acceleration,
# update positions and velocities
for k in xrange(N):
# Rec-calculate acceleration at each half-step
acc[step,k] = forces[k] / m_all[k]
# Update position on first half-step, velocity on second
if step == 0:
pp[k,:] = pp[k] + pv[k] * dt + 0.5 * acc[0,k] * dt**2
else:
pv[k,:] = pv[k] + 0.5 * (acc[0,k] + acc[1,k]) * dt
step += 1
return pp, pv
這是錯誤消息,其中包含產生錯誤的行:
IndexError Traceback (most recent call last)
<ipython-input-24-843691efebda> in <module>()
----> 1 partpos, partvel = evolve(m_all, pp_all, pv_all, tf, dt)
<ipython-input-22-f0eb2f7f6e98> in evolve(m_all, pp_all, pv_all, tf, dt)
38 t += dt # add time step
39 while i < 10000:
---> 40 partpos[i] = new_positions[i]
41 partvel[i] = new_velocities[i]
42 i += 1
IndexError: index 2 is out of bounds for axis 0 with size 2
while t < tf
evolve
while t < tf
, while
循環將重復100,000次。 這意味着代碼期望new_positions
具有100,000個元素。 但是evolve_particles
的返回值是僅包含2個元素的列表,因為它基於pp_all
輸入中的元素數量。 因此,在循環中第三次出現索引錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.