|
| 1 | +from matplotlib import pyplot as plt |
| 2 | +from matplotlib import animation |
| 3 | +from random import uniform |
| 4 | +import timeit |
| 5 | + |
| 6 | +class Particle: |
| 7 | + |
| 8 | + __slots__ = ('x', 'y', 'ang_speed') |
| 9 | + |
| 10 | + def __init__(self, x, y, ang_speed): |
| 11 | + self.x = x |
| 12 | + self.y = y |
| 13 | + self.ang_speed = ang_speed |
| 14 | + |
| 15 | + |
| 16 | +class ParticleSimulator: |
| 17 | + |
| 18 | + def __init__(self, particles): |
| 19 | + self.particles = particles |
| 20 | + |
| 21 | + def evolve(self, dt): |
| 22 | + timestep = 0.00001 |
| 23 | + nsteps = int(dt/timestep) |
| 24 | + |
| 25 | + for i in range(nsteps): |
| 26 | + for p in self.particles: |
| 27 | + |
| 28 | + norm = (p.x**2 + p.y**2)**0.5 |
| 29 | + v_x = (-p.y)/norm |
| 30 | + v_y = p.x/norm |
| 31 | + |
| 32 | + d_x = timestep * p.ang_speed * v_x |
| 33 | + d_y = timestep * p.ang_speed * v_y |
| 34 | + |
| 35 | + p.x += d_x |
| 36 | + p.y += d_y |
| 37 | + |
| 38 | + # def evolve(self, dt): |
| 39 | + # timestep = 0.00001 |
| 40 | + # nsteps = int(dt/timestep) |
| 41 | + |
| 42 | + # # First, change the loop order |
| 43 | + # for p in self.particles: |
| 44 | + # t_x_ang = timestep * p.ang_speed |
| 45 | + # for i in range(nsteps): |
| 46 | + # norm = (p.x**2 + p.y**2)**0.5 |
| 47 | + # p.x, p.y = p.x - t_x_ang*p.y/norm, p.y + t_x_ang * p.x/norm |
| 48 | + |
| 49 | +def visualize(simulator): |
| 50 | + |
| 51 | + X = [p.x for p in simulator.particles] |
| 52 | + Y = [p.y for p in simulator.particles] |
| 53 | + |
| 54 | + fig = plt.figure() |
| 55 | + ax = plt.subplot(111, aspect='equal') |
| 56 | + line, = ax.plot(X, Y, 'ro') |
| 57 | + |
| 58 | + # Axis limits |
| 59 | + plt.xlim(-1, 1) |
| 60 | + plt.ylim(-1, 1) |
| 61 | + |
| 62 | + # It will be run when the animation starts |
| 63 | + def init(): |
| 64 | + line.set_data([], []) |
| 65 | + return line, |
| 66 | + |
| 67 | + def animate(i): |
| 68 | + # We let the particle evolve for 0.1 time units |
| 69 | + simulator.evolve(0.01) |
| 70 | + X = [p.x for p in simulator.particles] |
| 71 | + Y = [p.y for p in simulator.particles] |
| 72 | + |
| 73 | + line.set_data(X, Y) |
| 74 | + return line, |
| 75 | + |
| 76 | + # Call the animate function each 10 ms |
| 77 | + anim = animation.FuncAnimation(fig, |
| 78 | + animate, |
| 79 | + init_func=init, |
| 80 | + blit=True, |
| 81 | + interval=10) |
| 82 | + plt.show() |
| 83 | + |
| 84 | + |
| 85 | +def test_visualize(): |
| 86 | + particles = [Particle( 0.3, 0.5, +1), |
| 87 | + Particle( 0.0, -0.5, -1), |
| 88 | + Particle(-0.1, -0.4, +3)] |
| 89 | + |
| 90 | + simulator = ParticleSimulator(particles) |
| 91 | + visualize(simulator) |
| 92 | + |
| 93 | +def test_evolve(): |
| 94 | + particles = [Particle( 0.3, 0.5, +1), |
| 95 | + Particle( 0.0, -0.5, -1), |
| 96 | + Particle(-0.1, -0.4, +3)] |
| 97 | + |
| 98 | + simulator = ParticleSimulator(particles) |
| 99 | + |
| 100 | + simulator.evolve(0.1) |
| 101 | + |
| 102 | + p0, p1, p2 = particles |
| 103 | + |
| 104 | + def fequal(a, b): |
| 105 | + return abs(a - b) < 1e-5 |
| 106 | + |
| 107 | + assert fequal(p0.x, 0.2102698450356825) |
| 108 | + assert fequal(p0.y, 0.5438635787296997) |
| 109 | + |
| 110 | + assert fequal(p1.x, -0.0993347660567358) |
| 111 | + assert fequal(p1.y, -0.4900342888538049) |
| 112 | + |
| 113 | + assert fequal(p2.x, 0.1913585038252641) |
| 114 | + assert fequal(p2.y, -0.3652272210744360) |
| 115 | + |
| 116 | + |
| 117 | +def benchmark(): |
| 118 | + particles = [Particle(uniform(-1.0, 1.0), |
| 119 | + uniform(-1.0, 1.0), |
| 120 | + uniform(-1.0, 1.0)) |
| 121 | + for i in range(100)] |
| 122 | + |
| 123 | + simulator = ParticleSimulator(particles) |
| 124 | + simulator.evolve(0.1) |
| 125 | + |
| 126 | + |
| 127 | +def timing(): |
| 128 | + result = timeit.timeit('benchmark()', |
| 129 | + setup='from __main__ import benchmark', |
| 130 | + number=10) |
| 131 | + # Result is the time it takes to run the whole loop |
| 132 | + print(result) |
| 133 | + |
| 134 | + result = timeit.repeat('benchmark()', |
| 135 | + setup='from __main__ import benchmark', |
| 136 | + number=10, |
| 137 | + repeat=3) |
| 138 | + # Result is a list of times |
| 139 | + print(result) |
| 140 | + |
| 141 | + |
| 142 | +def benchmark_memory(): |
| 143 | + particles = [Particle(uniform(-1.0, 1.0), |
| 144 | + uniform(-1.0, 1.0), |
| 145 | + uniform(-1.0, 1.0)) |
| 146 | + for i in range(100000)] |
| 147 | + |
| 148 | + simulator = ParticleSimulator(particles) |
| 149 | + simulator.evolve(0.001) |
| 150 | + |
| 151 | + |
| 152 | +if __name__ == '__main__': |
| 153 | + benchmark() |
0 commit comments