Skip to content

Instantly share code, notes, and snippets.

@enakai00
Created June 4, 2016 03:56
Show Gist options
  • Save enakai00/e65fbae88faad27439ebe312b006b6bb to your computer and use it in GitHub Desktop.
Save enakai00/e65fbae88faad27439ebe312b006b6bb to your computer and use it in GitHub Desktop.
Gravitation simulation with Jupyter
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib.animation as animation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class Ball:\n",
" def __init__(self, m, x, y, vx, vy):\n",
" self.m = m\n",
" self.x, self.y = x, y\n",
" self.vx, self.vy = vx, vy\n",
"\n",
" def run(self, fx, fy):\n",
" self.x += self.vx\n",
" self.y += self.vy\n",
" self.vx += fx / self.m\n",
" self.vy += fy / self.m"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib nbagg\n",
"\n",
"fig = plt.figure(figsize=(4,3))\n",
"images = []\n",
"ball = Ball(1,0,0,1,100)\n",
"for _ in range(22):\n",
" image = plt.scatter([ball.x],[ball.y])\n",
" images.append([image])\n",
" fx = 0\n",
" fy = -ball.m * 9.8\n",
" ball.run(fx,fy)\n",
"\n",
"ani = animation.ArtistAnimation(fig, images, interval=100, repeat_delay=1000)\n",
"ani.save('cannon_ball.gif', writer='imagemagick', fps=25);\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib nbagg\n",
"planet1 = Ball(10**11,0,10,-0.6,0)\n",
"planet2 = Ball(10**11,0,-10,0.6,0)\n",
"fig = plt.figure(figsize=(5,5))\n",
"images = []\n",
"plt.xlim(-50,50)\n",
"plt.ylim(-50,50)\n",
"\n",
"G = 6.6*10**(-11)\n",
"for i in range(1000):\n",
" if i % 2 == 1:\n",
" image = plt.scatter([planet1.x,planet2.x],[planet1.y,planet2.y])\n",
" images.append([image])\n",
" r2 = (planet1.x-planet2.x)**2 + (planet1.y-planet2.y)**2\n",
" fx = G*np.sign(planet2.x-planet1.x)*(planet1.m*planet2.m)/r2\n",
" fy = G*np.sign(planet2.y-planet1.y)*(planet1.m*planet2.m)/r2\n",
" planet1.run(fx,fy)\n",
" planet2.run(-fx,-fy)\n",
" \n",
"ani = animation.ArtistAnimation(fig, images, interval=4, repeat_delay=1000)\n",
"ani.save('binary_star.gif', writer='imagemagick', fps=50);\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib nbagg\n",
"\n",
"fig = plt.figure(figsize=(5,4.2))\n",
"plt.xticks([])\n",
"plt.yticks([])\n",
"plt.xlim(-15000,15000)\n",
"plt.ylim(-20000,5000)\n",
"images = []\n",
"\n",
"balls = []\n",
"num = 20\n",
"for i in range(num):\n",
" vx = 200*np.cos(2*np.pi*i/num)\n",
" vy = 200*np.sin(2*np.pi*i/num)\n",
" balls.append(Ball(1,0,0,vx,vy))\n",
"\n",
"for _ in range(110):\n",
" xs, ys = [], []\n",
" for ball in balls:\n",
" xs.append(ball.x)\n",
" ys.append(ball.y)\n",
" fx = 0\n",
" fy = -ball.m * 9.8\n",
" ball.run(fx,fy)\n",
"\n",
" image = plt.scatter(xs, ys)\n",
" images.append([image])\n",
"\n",
"ani = animation.ArtistAnimation(fig, images, interval=30, repeat_delay=1000)\n",
"ani.save('fireworks.gif', writer='imagemagick', fps=40);\n",
"plt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment