Changing all items in a list at once - python

I am trying to create a simple "Particle in a box" in Python, where multiple particles are bouncing back and forth between boundaries.
I need to keep track of the x-pos and y-pos of the particle and the x- and y-components of the speed to plot these at the end.
By each dt, the program will calculate the new position. Instead of looping through each particle, I want to update the entire list at once. Otherwise, the calculation and replacements will take forever for more particles.
This question is already asked. However, I calculate each value each step. This is different from changing an item to a predetermined value.
So, how do I replace each item at once in a list after calculating the new value?
dt = 0.001
pos_x = []
pos_y = []
speed_x = []
speed_y = []
For-loop to set the speed of each particle:
for i in range(5):
alpha = random.random() * 360
speed = 0.1 * random.random() * alpha
speed_x.append(math.sin(speed))
speed_y.append(math.cos(speed))
pos_x.append(0.25)
pos_y.append(0.75)
For-loop to update the position of each particle:
for n in range(5):
pos_x[n] = pos_x[n] + speed_x[n] * dt
pos_y[n] = pos_y[n] + speed_y[n] * dt
After this, I will plot all the points and update the window each pause to let them move.

import numpy as np
if __name__ == "__main__":
pos = np.array([5,5,5,5,5])
speed = np.array([2,2,2,2,2])
new_pos = pos + speed * 0.01
print(new_pos)
Output:
[5.02 5.02 5.02 5.02 5.02]
With the numpy package you can easily add arrays together or multiply them with predefined values.

Related

pygame.Rect objects keep getting stuck at top = 0 and don't move when .move or .move_ip is used

This is a small part of some more complicated code but basically. I've narrowed down the problem to this one bit of code.
I'm using the formula s = ut + 1/2 at^2 to get the distance traveled in one instance to time, in this case, 0.001 seconds which will then further be used to draw the rects on a window.
import pygame
run = True
clock = pygame.time.Clock()
ex_s = pygame.Rect((500, 625, 50, 50))
start_height = ex_s.top
print(start_height, "m")
g = -9.8
v = 400
t = 0.001
while run:
ex_s.top -= (v * t) + ((g / 2) * (t ** 2))
print(ex_s.top - start_height)
if (ex_s.top - start_height) * -1 <= -10:
run = False
clock.tick(30)
I want the rectangles to move upwards/downwards based on their velocity.
The rectangles move up and get stuck whenever the top reaches y = 0. This doesn't happen if the value of "t" is increased to o.o1 or above but I need it to be 0.001 for the precision. The problem is also solved when the value of v is increased by an order of magnitude, changing g does nothing.
I tried using both move and move_ip and the y coordinate did not change at all.
I've also tried manually pushing the objects above y = 0 when they hit it but it doesn't do anything. The fact that the problem changes with decimal places has led me to believe it could be something to do with the integer coordinate system of pygame but I don't see how this would cause things to move first and then get stuck only at 0.
See Pygame doesn't let me use float for rect.move, but I need it. The problem is that a pygame.Rect object can only store integer data. So when you add a value less than 1.0 to the coordinate of a rectangle, than the position stays the same. You need to store the position of the rectangle in separate variables and update the position of the rectangle using these variables.
ex_s = pygame.Rect((500, 625, 50, 50))
ex_s_y = ex_s.top
# [...]
while run:
# [...]
# change position
ex_s_y = .....
ex_s.top = round(ex_s_y)
You didn't change the time t variable, and your formula for the motion is wrong. Also your initial time, initial velocity and g are wrong.
If you want to use an exact solution for the position, do like this.
dt = 0.05
t = 0
g = 9.8 # It's positive by definition.
v = -100 # It's upward.
while run:
y = 625 + v*t + 0.5*g*t**2 # The acceleration is +g, downward.
t += dt
ex_s.top = y
...
If you want to solve the equation of motion approximately and iteratively, do like this inside the loop.
...
y = 625
while run:
v += g * dt
y += v * dt
t += dt
ex_s.top = y
...
The above method is called sympletic Euler method. There're other more accurate methods, such as Runge-Kutta method.

Removing specific item from np.array when it matches location

For a Python program "Particle in a Box", I need to insert a gap in the x,y-plane. I keep track of the x- and y-location of each particle and the x- and y-components of the velocities in numpy arrays.
I have two errors.
1) positie_y[z] <= 0 and positie_x[z] > 0.8 and positie_x[z] < 0.9 cannot be used for numpy arrays. I am new to numpy arrays, so please explain how to use any() or all() or other options?
2) The np.delete is not working: the particles are not disappearing. Is this because I am not using it the right way or is there another way to do it?
def particles(n, gap):
dt = 0.01
position_x = []
position_y = []
speed_x = []
speed_y = []
for i in range(n):
alpha = random.random() * 360
speed = (0.1)*random.random() * alpha
speed_x.append(math.sin(snelheid))
speed_y.append(math.cos(snelheid))
position_x.append(0.25)
position_y.append(0.75)
position_x = np.array(position_x)
position_y = np.array(position_y)
speed_x = np.array(speed_x)
speed_y = np.array(speed_y)
Until here, it is working fine. The problem is somewhere in the following code.
while True:
position_x = position_x + speed_x * dt
position_y = position_y + speed_y * dt
# 'z' is the position number of the particle in the numpy array.
for z in range(0, n):
# Gap == 1 means there is a gap.
if gap == 1:
# The gap is at y = 0 and 0.8 < x < 0.9
if position_y[z] <= 0 and position_x[z] > 0.8 and position_x[z] < 0.9:
np.delete(position_x, position_x[z])
np.delete(position_y, position_y[z])
np.delete(speed_x, speed_x[z])
np.delete(speed_y, speed_y[z])
After that, I will plot each particle with plt.plot(positie_x, positie_y, 'ro') and particles(100, 1)
The snippet:
np.delete(position_x, position_x[z])
isn't working because the function numpy.delete doesn't make changes on the given array object, but rather returns a new array without the deleted elements.
So your code should be written like this:
position_x = np.delete(position_x, position_x[z])

Projectile Motion in Python: How to generate list giving vertical velocity over time?

I'm trying to find how the vertical velocity Vy of a spherical particle falling vertically with drag changes as a function of time t. I would like to generate a list of values that state what Vy is for several points in time from the time at which is is released to some t_max as well as plot Vy vs time.
The equation I've been given to solve for ∆Vy is:
dVy = -g*dt - (b/m)*Vy*dt
and I've been given a suggested algorithm for finding the values
"The code should ask for the values of m, g, b and ∆t. Once these values are defined, the code should provide a series of values of Vy for each time t."
Here's my attempt so far:
import numpy as np
import matplotlib.pyplot as plt
import math
g = 9.81 #acceleration due to gravity
b = 1.6*10**-8 #linear drag coefficient
m = 1.04*10**-9 #mass of particle
dt = 10**-3 #time step
t_max = 10 #final time
t = 0 #initial time
Vy = 0 #initial velocity
t_list = [t]
Vy_list = [Vy]
dVy = -g*dt - (b/m)*Vy*dt
while t <= t_max:
t += dt
Vy += dVy
t_list.append(t)
Vy_list.append(Vy)
print(t, Vy)
plt.plot(t_list, Vy_list)
plt.show()
I'm not sure that the values it generates are quite right considering that no matter what size I set the mass to the change in velocity remains the same when I'd expect it to affect the velocity quite a bit for infinitesimally smaller masses.
I'll admit that I'm replicating a solution that was given to me for a completely different problem, but am I right in thinking that a while loop is the way to go?
You need to update dVy in the while loop:
while t <= t_max:
dVy = -g*dt - (b/m)*Vy*dt
t += dt
Vy += dVy
Otherwise it will be a constant and not update as you expect over time.

Many particles in box - physics simulation

I'm currently trying to simulate many particles in a box bouncing around.
I've taken into account #kalhartt's suggestions and this is the improved code to initialize the particles inside the box:
import numpy as np
import scipy.spatial.distance as d
import matplotlib.pyplot as plt
# 2D container parameters
# Actual container is 50x50 but chose 49x49 to account for particle radius.
limit_x = 20
limit_y = 20
#Number and radius of particles
number_of_particles = 350
radius = 1
def force_init(n):
# equivalent to np.array(list(range(number_of_particles)))
count = np.linspace(0, number_of_particles-1, number_of_particles)
x = (count + 2) % (limit_x-1) + radius
y = (count + 2) / (limit_x-1) + radius
return np.column_stack((x, y))
position = force_init(number_of_particles)
velocity = np.random.randn(number_of_particles, 2)
The initialized positions look like this:
Once I have the particles initialized I'd like to update them at each time-step. The code for updating follows the previous code immediately and is as follows:
# Updating
while np.amax(abs(velocity)) > 0.01:
# Assume that velocity slowly dying out
position += velocity
velocity *= 0.995
#Get pair-wise distance matrix
pair_dist = d.cdist(position, position)
pair_d = pair_dist<=4
#If pdist [i,j] is <=4 then the particles are too close and so treat as collision
for i in range(len(pair_d)):
for j in range(i):
# Only looking at upper triangular matrix (not inc. diagonal)
if pair_d[i,j] ==True:
# If two particles are too close then swap velocities
# It's a bad hack but it'll work for now.
vel_1 = velocity[j][:]
velocity[j] = velocity[i][:]*0.9
velocity[i] = vel_1*0.9
# Masks for particles beyond the boundary
xmax = position[:, 0] > limit_x
xmin = position[:, 0] < 0
ymax = position[:, 1] > limit_y
ymin = position[:, 1] < 0
# flip velocity and assume that it looses 10% of energy
velocity[xmax | xmin, 0] *= -0.9
velocity[ymax | ymin, 1] *= -0.9
# Force maximum positions of being +/- 2*radius from edge
position[xmax, 0] = limit_x-2*radius
position[xmin, 0] = 2*radius
position[ymax, 0] = limit_y-2*radius
position[ymin, 0] = 2*radius
After updating it and letting it run to completion I get this result:
This is infinitely better than before but there are still patches that are too close together - such as:
Too close together. I think the updating works... and thanks to #kalhartt my code is wayyyy better and faster (and I learnt some things about numpy... props #kalhartt) but I still don't know where it's screwing up. I've tried changing the order of the actual updates with the pair-wise distance going last or the position +=velocity going last but to no avail. I added the *0.9 to make the entire thing die down faster and I tried it with 4 to make sure that 2*radius (=2) wasn't too tight a criteria... but nothing seems to work.
Any and all help would be appreciated.
There are just two typos standing in your way. First for i in range(len(positions)/2): only iterates over half of your particles. This is why half the particles stay in the x bounds (if you watch for large iterations its more clear). Second, the second y condition should be a minimum (I assume) position[i][1] < 0. The following block works to bound the particles for me (I didn't test with the collision code so there could be problems there).
for i in range(len(position)):
if position[i][0] > limit_x or position[i][0] < 0:
velocity[i][0] = -velocity[i][0]
if position[i][1] > limit_y or position[i][1] < 0:
velocity[i][1] = -velocity[i][1]
As an aside, try to leverage numpy to eliminate loops when possible. It is faster, more efficient, and in my opinion more readable. For example force_init would look like this:
def force_init(n):
# equivalent to np.array(list(range(number_of_particles)))
count = np.linspace(0, number_of_particles-1, number_of_particles)
x = (count * 2) % limit_x + radius
y = (count * 2) / limit_x + radius
return np.column_stack((x, y))
And your boundary conditions would look like this:
while np.amax(abs(velocity)) > 0.01:
position += velocity
velocity *= 0.995
# Masks for particles beyond the boundary
xmax = position[:, 0] > limit_x
xmin = position[:, 0] < 0
ymax = position[:, 1] > limit_y
ymin = position[:, 1] < 0
# flip velocity
velocity[xmax | xmin, 0] *= -1
velocity[ymax | ymin, 1] *= -1
Final note, it is probably a good idea to hard clip position to the bounding box with something like position[xmax, 0] = limit_x; position[xmin, 0] = 0. There may be cases where velocity is small and a particle outside the box will be reflected but not make it inside in the next iteration. So it will just sit outside the box being reflected forever.
EDIT: Collision
The collision detection is a much harder problem, but lets see what we can do. Lets take a look at your current implementation.
pair_dist = d.cdist(position, position)
pair_d = pair_dist<=4
for i in range(len(pair_d)):
for j in range(i):
# Only looking at upper triangular matrix (not inc. diagonal)
if pair_d[i,j] ==True:
# If two particles are too close then swap velocities
# It's a bad hack but it'll work for now.
vel_1 = velocity[j][:]
velocity[j] = velocity[i][:]*0.9
velocity[i] = vel_1*0.9
Overall a very good approach, cdist will efficiently calculate the distance
between sets of points and you find which points collide with pair_d = pair_dist<=4.
The nested for loops are the first problem. We need to iterate over True values of pair_d where j > i. First your code actually iterate over the lower triangular region by using for j in range(i) so that j < i, not particularly important in this instance as long since i,j pairs are not repeated. However Numpy has two builtins we can use instead, np.triu lets us set all values below a diagonal to 0 and np.nonzero will give us the indices of non-zero elements in a matrix. So this:
pair_dist = d.cdist(position, position)
pair_d = pair_dist<=4
for i in range(len(pair_d)):
for j in range(i+1, len(pair_d)):
if pair_d[i, j]:
...
is equivalent to
pair_dist = d.cdist(position, position)
pair_d = np.triu(pair_dist<=4, k=1) # k=1 to exclude the diagonal
for i, j in zip(*np.nonzero(pair_d)):
...
The second problem (as you noted) is that the velocities are just switched and scaled instead of reflected. What we really want to do is negate and scale the component of each particles velocity along the axis that connects them. Note that to do this we will need the vector connecting them position[j] - position[i] and the length of the vector connecting them (which we already calculated). So unfortunately part of the cdist calculation gets repeated. Lets quit using cdist and do it ourselves instead. The goal here is to make two arrays diff and norm where diff[i][j] is a vector pointing from particle i to j (so diff is a 3D array) and norm[i][j] is the distance between particles i and j. We can do this with numpy like so:
nop = number_of_particles
# Give pos a 3rd index so we can use np.repeat below
# equivalent to `pos3d = np.array([ position ])
pos3d = position.reshape(1, nop, 2)
# 3D arras with a repeated index so we can form combinations
# diff_i[i][j] = position[i] (for all j)
# diff_j[i][j] = position[j] (for all i)
diff_i = np.repeat(pos3d, nop, axis=1).reshape(nop, nop, 2)
diff_j = np.repeat(pos3d, nop, axis=0)
# diff[i][j] = vector pointing from position[i] to position[j]
diff = diff_j - diff_i
# norm[i][j] = sqrt( diff[i][j]**2 )
norm = np.linalg.norm(diff, axis=2)
# check for collisions and take the region above the diagonal
collided = np.triu(norm < radius, k=1)
for i, j in zip(*np.nonzero(collided)):
# unit vector from i to j
unit = diff[i][j] / norm[i][j]
# flip velocity
velocity[i] -= 1.9 * np.dot(unit, velocity[i]) * unit
velocity[j] -= 1.9 * np.dot(unit, velocity[j]) * unit
# push particle j to be radius units from i
# This isn't particularly effective when 3+ points are close together
position[j] += (radius - norm[i][j]) * unit
...
Since this post is long enough already, here is a gist of the code with my modifications.

VPython: Iterating a function to create a new curve every run

I'm creating a program to find the optimum angle to launch a projectile at a given speed, in order to hit a target a given distance away.
i have this code:
def Trajectory(angle):
position = vector(0,0,0)
poscurve.plot(pos=position)
velocity = v0 * vector(cos(angle*pi/180), sin(angle*pi/180), 0)
while (position.y > 0) or (velocity.y > 0):
dx = velocity * dt # update dx and dv
dv = g * dt
position = position + dx # apply the updates to velocity and position
velocity = velocity + dv
poscurve.plot(pos=position)
return position
The problem is, this program repeats this function multiple times, with different angles. When it repeats it, it connects the first point of the new curve to the last point of the previous curve, because the function uses the same poscurve.plot(pos=position) every time. How do I make the function generate a new curve each time it iterates this function?
(there are variables defined outside of this code snippet, the program conceptually works fine, just how the graph looks)
Add poscurve as a parameter:
def Trajectory(angle,poscurve):
position = vector(0,0,0)
poscurve.plot(pos=position)
velocity = v0 * vector(cos(angle*pi/180), sin(angle*pi/180), 0)
while (position.y > 0) or (velocity.y > 0):
dx = velocity * dt # update dx and dv
dv = g * dt
position = position + dx # apply the updates to velocity and position
velocity = velocity + dv
poscurve.plot(pos=position)
return position
Then, pass in a new poscurve every time. You can just call gcurve again to create a new object.

Categories

Resources