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()
45.105000000000004
p2 = Particle(0.2, 3.3, 4.1, 3.1, -2.2, 4.4, -5.4)
p1.separation(p2)
2.2405356502408083
p2.separation(p1)
2.2405356502408083
p1.move(1.)
print p1.x, p1.y, p1.z
8.9 8.8 11.1
for i in range(10):
p1.move(0.1)
print p1.x, p1.y, p1.z
9.24 9.24 11.87 9.58 9.68 12.64 9.92 10.12 13.41 10.26 10.56 14.18 10.6 11.0 14.95 10.94 11.44 15.72 11.28 11.88 16.49 11.62 12.32 17.26 11.96 12.76 18.03 12.3 13.2 18.8
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)
20.304186760370378
p3.charge
-1