class Particle(object): def __init__(self, mass, x, y, z, vx, vy, vz): self.mass = mass self.x = x self.y = y self.z = z self.vx = vx self.vy = vy self.vz = vz def kinetic_energy(self): return 0.5 * self.mass * (self.vx ** 2 + self.vy ** 2 + self.vz ** 2) def separation(self, other): return np.sqrt((self.x - other.x) ** 2 + (self.y - other.y) ** 2 + (self.z - other.z) ** 2) def move(self, dt): self.x += dt * self.vx self.y += dt * self.vy self.z += dt * self.vz p1 = Particle(1., 5.5, 4.4, 3.4, 3.4, 4.4, 7.7) p1.kinetic_energy() p2 = Particle(0.2, 3.3, 4.1, 3.1, -2.2, 4.4, -5.4) p1.separation(p2) p2.separation(p1) p1.move(1.) print p1.x, p1.y, p1.z for i in range(10): p1.move(0.1) print p1.x, p1.y, p1.z class ChargedParticle(Particle): def __init__(self, mass, x, y, z, vx, vy, vz, charge): self.charge = charge Particle.__init__(self, mass, x, y, z, vx, vy, vz) # avoids having to re-define all the attributes p3 = ChargedParticle(4., 2.2, 3.2, 4.3, 6.6, -2.3, 2.2, -1) p3.separation(p1) p3.charge