SHOW:
|
|
- or go back to the newest paste.
| 1 | - | from matplotlib import pyplot as plt |
| 1 | + | #!/usr/bin/env python |
| 2 | - | from matplotlib import animation |
| 2 | + | |
| 3 | - | |
| 3 | + | from matplotlib import pyplot as plt |
| 4 | - | |
| 4 | + | from matplotlib import animation |
| 5 | - | class Particle: |
| 5 | + | |
| 6 | - | |
| 6 | + | |
| 7 | - | def __init__(self, x, y, ang_speed): |
| 7 | + | class Particle: |
| 8 | - | self.x = x |
| 8 | + | |
| 9 | - | self.y = y |
| 9 | + | def __init__(self, x, y, ang_speed): |
| 10 | - | self.ang_speed = ang_speed |
| 10 | + | self.x = x |
| 11 | - | |
| 11 | + | self.y = y |
| 12 | - | |
| 12 | + | self.ang_speed = ang_speed |
| 13 | - | class ParticleSimulator: |
| 13 | + | |
| 14 | - | |
| 14 | + | |
| 15 | - | def __init__(self, particles): |
| 15 | + | class ParticleSimulator: |
| 16 | - | self.particles = particles |
| 16 | + | |
| 17 | - | |
| 17 | + | def __init__(self, particles): |
| 18 | - | def evolve(self, dt): |
| 18 | + | self.particles = particles |
| 19 | - | timestep = 0.00001 |
| 19 | + | |
| 20 | - | nsteps = int(dt / timestep) |
| 20 | + | def evolve(self, dt): |
| 21 | - | |
| 21 | + | timestep = 0.00001 |
| 22 | - | for i in range(nsteps): |
| 22 | + | nsteps = int(dt / timestep) |
| 23 | - | for p in self.particles: |
| 23 | + | |
| 24 | - | |
| 24 | + | for i in range(nsteps): |
| 25 | - | # 1. calculate the direction |
| 25 | + | for p in self.particles: |
| 26 | - | norm = (p.x ** 2 + p.y ** 2) ** 0.5 |
| 26 | + | |
| 27 | - | v_x = (-p.y) / norm |
| 27 | + | # 1. calculate the direction |
| 28 | - | v_y = p.x / norm |
| 28 | + | norm = (p.x ** 2 + p.y ** 2) ** 0.5 |
| 29 | - | |
| 29 | + | v_x = (-p.y) / norm |
| 30 | - | # 2. calculate the displacement |
| 30 | + | v_y = p.x / norm |
| 31 | - | d_x = timestep * p.ang_speed * v_x |
| 31 | + | |
| 32 | - | d_y = timestep * p.ang_speed * v_y |
| 32 | + | # 2. calculate the displacement |
| 33 | - | |
| 33 | + | d_x = timestep * p.ang_speed * v_x |
| 34 | - | p.x += d_x |
| 34 | + | d_y = timestep * p.ang_speed * v_y |
| 35 | - | p.y += d_y |
| 35 | + | |
| 36 | - | |
| 36 | + | p.x += d_x |
| 37 | - | # 3. repeat for all the time steps |
| 37 | + | p.y += d_y |
| 38 | - | |
| 38 | + | |
| 39 | - | |
| 39 | + | # 3. repeat for all the time steps |
| 40 | - | def visualize(simulator): |
| 40 | + | |
| 41 | - | X = [p.x for p in simulator.particles] |
| 41 | + | |
| 42 | - | Y = [p.y for p in simulator.particles] |
| 42 | + | def visualize(simulator): |
| 43 | - | |
| 43 | + | X = [p.x for p in simulator.particles] |
| 44 | - | fig = plt.figure() |
| 44 | + | Y = [p.y for p in simulator.particles] |
| 45 | - | ax = plt.subplot(111, aspect='equal') |
| 45 | + | |
| 46 | - | line, = ax.plot(X, Y, 'ro') |
| 46 | + | fig = plt.figure() |
| 47 | - | |
| 47 | + | ax = plt.subplot(111, aspect='equal') |
| 48 | - | # Axis limits |
| 48 | + | line, = ax.plot(X, Y, 'ro') |
| 49 | - | plt.xlim(-1, 1) |
| 49 | + | |
| 50 | - | plt.ylim(-1, 1) |
| 50 | + | # Axis limits |
| 51 | - | |
| 51 | + | plt.xlim(-1, 1) |
| 52 | - | # It will be run when the animation starts |
| 52 | + | plt.ylim(-1, 1) |
| 53 | - | def init(): |
| 53 | + | |
| 54 | - | line.set_data([], []) |
| 54 | + | # It will be run when the animation starts |
| 55 | - | return line, |
| 55 | + | def init(): |
| 56 | - | |
| 56 | + | line.set_data([], []) |
| 57 | - | def animate(i): |
| 57 | + | return line, |
| 58 | - | # We let the particle evolve for 0.1 time units |
| 58 | + | |
| 59 | - | simulator.evolve(0.01) |
| 59 | + | def animate(i): |
| 60 | - | X = [p.x for p in simulator.particles] |
| 60 | + | # We let the particle evolve for 0.1 time units |
| 61 | - | Y = [p.y for p in simulator.particles] |
| 61 | + | simulator.evolve(0.01) |
| 62 | - | |
| 62 | + | X = [p.x for p in simulator.particles] |
| 63 | - | line.set_data(X, Y) |
| 63 | + | Y = [p.y for p in simulator.particles] |
| 64 | - | return line, |
| 64 | + | |
| 65 | - | |
| 65 | + | line.set_data(X, Y) |
| 66 | - | # Call the animate function each 10 ms |
| 66 | + | return line, |
| 67 | - | anim = animation.FuncAnimation(fig, animate, |
| 67 | + | |
| 68 | - | init_func=init, |
| 68 | + | # Call the animate function each 10 ms |
| 69 | - | blit=True, |
| 69 | + | anim = animation.FuncAnimation(fig, animate, |
| 70 | - | # Efficient animation |
| 70 | + | init_func=init, |
| 71 | - | interval=10) |
| 71 | + | blit=True, |
| 72 | - | plt.show() |
| 72 | + | # Efficient animation |
| 73 | - | |
| 73 | + | interval=10) |
| 74 | - | def test_visualize(): |
| 74 | + | plt.show() |
| 75 | - | particles = [Particle(0.3, 0.5, +1), |
| 75 | + | |
| 76 | - | Particle(0.0, -0.5, -1), |
| 76 | + | def test_visualize(): |
| 77 | - | Particle(-0.1, -0.4, +3)] |
| 77 | + | particles = [Particle(0.3, 0.5, +1), |
| 78 | - | |
| 78 | + | Particle(0.0, -0.5, -1), |
| 79 | - | simulator = ParticleSimulator(particles) |
| 79 | + | Particle(-0.1, -0.4, +3)] |
| 80 | - | visualize(simulator) |
| 80 | + | |
| 81 | - | |
| 81 | + | simulator = ParticleSimulator(particles) |
| 82 | - | if __name__ == '__main__': |
| 82 | + | visualize(simulator) |
| 83 | ||
| 84 | if __name__ == '__main__': | |
| 85 | test_visualize() |