0

I need to update an array 'pos_inst' with the each value of [positions](which is a 4x2 array) in the given for loop. However when appending the positions array in each iteration it updates the whole array(including previous entries) to have values equal to me newest entry

positions = np.array([[0.4,0.4],[0.4,0.6],[0.6, 0.4], [0.6,0.6]])
velocities = np.array([[1.,0.],[1.,1.],[0.,1.],[0.,0.]])
t = 0.0 #
n_events = 4 # Number of collision events.
pos_inst=[]
n=0
    ############### Event Loop ##############################################
for event in range(n_events):
    # Wall collision times for all disks and their velocity components.
    wall_times = [wall_time(positions[i][j], velocities[i][j], Ratom) for i in range(Natoms) for j in range(2)] 
    # Pair collision times
    pair_times = [pair_time(positions[i], velocities[i], positions[j], velocities[j], Ratom) for i,j in pairs] 
    # The next collision event is the minimum of wall and pair collision times.
    next_event = min(wall_times + pair_times)  
    t += next_event 
   ###################                       ############
   ################### MAIN PROBLEM IS HERE #########


    for i in range(Natoms):
        positions[i] += velocities[i]*next_event # Evolve positions to collision event
    print(positions)
    pos_inst.append(positions)
     ################################################################################
    ###################################################################################

    if min(wall_times) < min(pair_times): # Check if next event is a collision with a wall
        wall_index = wall_times.index(next_event)
        particle, component = divmod(wall_index, 2)
        velocities[particle][component] *= -1.0 ## Velocity component normal to wall changes sign
    else:
        pair_index = pair_times.index(next_event)
        particle_1, particle_2 = pairs[pair_index] # Indices of particles participating in collision.
        rel_pos = positions[particle_2] - positions[particle_1]
        rel_vel = velocities[particle_2] - velocities[particle_1]
        distance = np.sqrt(np.dot(rel_pos,rel_pos))
        unit_perp = rel_pos/distance
        scal_prod = np.dot(rel_vel,unit_perp)
        velocities[particle_1] += scal_prod*unit_perp # Change in velocities of atoms colliding with each other
        velocities[particle_2] -= scal_prod*unit_perp
    #print( sum([np.dot(velocities[i],velocities[i]) for i in range(len(velocities))]))
    
##################################################################################
print(pos_inst)

i have verified that the individual [positions] in the loop is correct and i want to append these values to 'pos_inst' however the final output is just a homogeneous array consisting of the n entries of the final appended array and the previous entries are lost.I have tried insert() and extend() functions too but end up with the same result

Thanking you , :}

  • `append()` doesn't make a copy of the array. So you're appending the same `positions` array every time. – Barmar Jan 14 '21 at 17:33
  • 1
    Does this answer your question? [List of lists changes reflected across sublists unexpectedly](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) – Tomerikoo Jan 14 '21 at 17:38

0 Answers0