Particle Trajectory, to learn classes and data structures

Question:

So the task is supposed to be done in multiply steps.
So we are given a class Particles, that we have to define.
It is supposed to have multiply methods, and here is where i meet my first struggle.

  • Create a class called Particle that has the following attributes: mass, position (as a tuple of x, y, and z coordinates), and velocity (as a tuple of x, y, and z components).

  • Implement a method update_position that updates the position of the particle based on its velocity and a given time step.

  • Implement a method kinetic_energy that calculates the kinetic energy of a particle defined as:
    E_kin = 0.5 * m * v^2

  • Implement a method get_x_position that returns the current x-coordinate of the position of a particle

  • Implement a method get_mass that returns the mass of a particle

  • Create a list of Particle instances, and update their positions after a certain time step.

these are the steps i am supposed to follow first. The last part is:

  • to calculate the sum of E_kin of all particles
  • x-position of the center of gravity for all particles after a given time step.

my main issues is with the way i implemented the tuples (x,y,z) and a function kinetic_energy that uses time step. I tried looking up what time step does, and as far as i understood it`s just there to represent a "time" that is supposed to pass by. As such i am sure i could use it as a variable. If someone please could give me some tips or a solution for it, i would appreciate it.
My code so far:

class Particle:

def __init__(self, mass=0, positions=0,posX=0, posY=0, posZ=0, velocity=0, velX=0, velY=0, velZ=0):
    self.mass = mass
    position = (posX, posY, posZ)
    self.positions = position
    vel = (velX, velY, velZ)
    self.velocity = vel

def updates_position(self, time):
    new_pos = self.position + (self.velocity * time)
    return new_pos

def kinetic_energy(self):
    E_kin = 0.5 * sellf.mass * self.velocity**2
    return E_kin

def get_x_positon(self, pos):
    x_pos = pos[0]
    return x_pos

def get_mass(self, energie):
    get_m = E_kin/(0.5 * self.velocity**2)
    return get_m

I feel like it is pretty self explanatory, for the init part, not sure if i can`t just pass the (posX, posy, posZ) directly into self.position, same for self.velocity.
The function for E_kin is a given.
The get_x_position my thought was quiet simple, i would directly pass a tuple in and the first value is the x-coordinate.
Had an identical idea for get_mass.
But i am completly clueless for updates_position. By the given code, it,s supposed to work in a for-loop on a list with the argument time_step, but i have no clue how it´s supposed to interact or how to even properly implement the method or the for-loop.

`## Create a list of Particle instances, fill with particles with:`
`## m=1, p=(0, 0, 0), v=(1, 2, 3)` 
`## m=2, p=(1, 2, 3), v=(-1, 2, -3)` 
`## m=3, p=(5, -2, 1), v=(0, 0, 1)`
`particles = [`
    [1,(0,0,0),(1,2,3)],
    [2,(1,2,3),(-1,2,-3)],
    [3,(5,-2,1),(0,0,1)]
`]`

honestly the easiest part, pretty sure everything is correct with this list.

`# Update positions after a given time step`
time_step = 2.5

for particle in particles:
particle.update_position(time_step)

As written a bit above, i´ve been trying to figure out how make this work with updates_position for 5h (including yesterday obviously), but have gotten no further and sadly my prof isn`t avaible.

`# Calculate the total kinetic energy of all particles`

total_kinetic_energy = sum(k.kinetic_energy(particles))

I just wrote that sum thing, just so i had something there. Haven´t spend any real thought on it yet as the above issues took all my time. Still would appreciate a hint.

`# Calculate the center of gravity in x of all particles`
x_center_of_gravity = 

Same as for the total_kinetic_energy.

Asked By: Azat Genlik

||

Answers:

There are a few issues in your code.

For your init method you need to provide either positions or posx, posy and posz and the same with velocity. Since later you already use tuples for them, I also suggest to use it in init:

class Particle:

    def __init__(self, mass=0, position=(0, 0, 0), velocity=(0, 0, 0)):
        self.mass = mass
        self.position = position
        self.velocity = velocity

Unfortunately, the "calculation" in your update_position will work, but not as intended. Adding tuples will lead to a concatenation of both (1,2,3) + (4,5,6) = (1,2,3,4,5,6), whereas you need a componentwise addition (1,2,3) + (4,5,6) = (1+4, 2+5, 3+6). (As a sidenote: To be able to write it like you did, position and velocity would need to be a numpy array).
The needs to be consindered for the calculation of kinetic_energy (self.velocity**2 will not work).

Also, you do not want to return the result of this method but to update the position of the particle, therefore you need to replace new_pos by self.position:

    def update_position(self, time):
        for idx in range(3):
            self.position = tuple(p + v * time for p,v in zip(self.position, self.velocity))

Similarly you do not need to provide an argument for get_x_position:

    def get_x_position(self):
        return self.position[0]

About the kinetic energy. Start with total_energy = 0 and add to it the energy of all particles:

total_energy = 0
for particle in particles:
    total_energy += particle.kinetic_energy

Forgot about the center of gravity.
The center of gravity can be caculated as follows:

(pos_x1 * mass1 + pos_x2mass2… + pos_xnmassn) / (mass1 + mass2 … + massn)

That is most likely why you were asked to provide methods for getting the x position and the mass of a partice. Try to use these with a similar approach as for the kinetic energy.
Also, get_mass should probably not calculate the mass from the particle’s energy but just return its mass, at least if the particles are not moving close to the speed of light.

Answered By: Flow
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.