1

I am trying to append values from a newly created array to a previously created array, then run a while loop to do this 10,000 times. Here is what I have so far:

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

I am trying to append the newly created values from the new_positions and new_velocities array to the partpos and partvel array, but am getting the following error:

IndexError: index 2 is out of bounds for axis 0 with size 2

How would I go about fixing this? Here are the inputs:

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

This code is supposed to model the orbit of the earth around the sun using 10,000 times steps. new_positions and new_velocities are each 2D arrays. Thanks!

Here is the evolve_particles function:

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

This is the error message with the line that produced the error:

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
6
  • Can you post evolve_particles() method too? Commented Dec 4, 2015 at 17:17
  • What's the shape of the returning arrays from evolve_particles() (new_positions, new_velocities) before you do the assignment?.. sounds like a problem with "i" index, trying to get something that's impossible to get from the arrays. Commented Dec 4, 2015 at 17:46
  • Which line produced the error? Don't hide useful information. Have youu attempted to print the shape of arrays at that point? Commented Dec 4, 2015 at 17:47
  • The shape of the array that evolve_particles() returns is (2, 3). I just posted the line that produced the error. Commented Dec 4, 2015 at 17:56
  • Why are you indexing a (2,3) array with i: new_positions[i]? Commented Dec 4, 2015 at 21:16

1 Answer 1

1

The while loop while t < tf in evolve will repeat 100,000 times. That means the code expects new_positions to have 100,000 elements. But the return value from evolve_particles is list with only 2 elements, because it is based on the number of elements in the pp_all input. So you get an index error on the 3rd time through the loop.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.