{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "from matplotlib import cm\n", "import numpy as np\n", "from celluloid import Camera\n", "\n", "i=0\n", "t=0\n", "all_clear = 0\n", "\n", "\n", "\n", "#set up the evironment\n", "\n", "#box\n", "x_left = -4\n", "x_right = 4\n", "y_bottom = -4\n", "y_top = 4\n", "wall_center = 0\n", "door_width = 3\n", "\n", "\n", "ball_count = 40\n", "\n", "# random starting speed and direction for each ball\n", "speed = np.random.uniform(low=0.1, high=1, size=(ball_count,2))\n", "speed /=10\n", "\n", "for i in range(0,ball_count): \n", " k = (-1)**np.random.randint(2) \n", " if k != 0 : \n", " speed[i][0] *= k \n", " \n", "for i in range(1,ball_count): \n", " k = (-1)**np.random.randint(2) \n", " if k != 0 : \n", " speed[i][1] *= k \n", "\n", " \n", "\n", "\n", "# ball coord. array\n", "ball = np.random.uniform(low=x_left, high=x_right, size=(ball_count,2))\n", "\n", "camera = Camera(plt.figure())\n", "\n", "\n", "\n", "while all_clear !=1 :\n", " \n", " # draw wall\n", " plt.plot([0,0], [y_bottom,0 - door_width/2], lw=2 , color = 'black')\n", " plt.plot([0,0], [door_width/2, y_top], lw=2 , color = 'black')\n", " plt.plot([0,0], [-door_width/2, door_width/2], lw=2 , color = 'cyan')\n", "\n", " \n", " for j in range(0, ball_count):\n", " \n", " b_x = ball[j][0]\n", " b_y = ball[j][1]\n", " s_x = speed[j][0]\n", " s_y = speed[j][1] \n", " \n", " # x bounce logic\n", " if b_x <= x_left or b_x >= x_right:\n", " \n", " speed[j][0] *= -1\n", " \n", " if x_left < b_x < wall_center:\n", " \n", " if b_x >= wall_center - 0.1 and s_x > 0 :\n", " \n", " if j >= ball_count // 2 or b_y >= door_width/2 or b_y <= - door_width/2:\n", " \n", " speed[j][0] *= -1 \n", " \n", " else:\n", " plt.plot([0,0], [-door_width/2, door_width/2], lw=2 , color = 'purple')\n", " \n", " \n", " elif wall_center < b_x < x_right:\n", " \n", " if b_x <= wall_center + 0.1 and s_x < 0 :\n", " \n", " if j < ball_count // 2 or b_y >= door_width/2 or b_y <= - door_width/2:\n", " \n", " speed[j][0] *= -1\n", " \n", " else:\n", " plt.plot([0,0], [-door_width/2, door_width/2], lw=2 , color = 'yellow')\n", " \n", " # y bounce logic\n", " if b_y >= y_top or b_y <= y_bottom:\n", " \n", " speed[j][1] *= -1\n", " \n", " \n", " \n", " \n", " # move ball \n", " ball[j][0] += speed[j][0]\n", " ball[j][1] += speed[j][1]\n", " \n", " \n", " \n", " # differentiate red and blue\n", " for j in range(0, ball_count // 2 - 1 ):\n", " \n", " plt.scatter(ball[j][0],ball[j][1], c='red', s=40) \n", " \n", " for j in range(ball_count // 2, ball_count):\n", "\n", " plt.scatter(ball[j][0],ball[j][1], c ='blue', s=40)\n", " \n", " plt.xlim(x_left, x_right) \n", " plt.ylim(y_bottom, y_top)\n", " \n", " camera.snap()\n", " \n", " # frame count\n", " if t == 1000:\n", " \n", " all_clear = 1\n", " \n", " t += 1\n", " \n", "anim = camera.animate(interval = 30, blit=True)\n", "\n", "anim.save('Maxwell_s_Demon.mp4')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ball_count % 2 - 1" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m ball[*][0]<0\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "ball[][0]<0\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" } }, "nbformat": 4, "nbformat_minor": 2 }