Created
October 30, 2018 20:42
-
-
Save jhjensen2/71bdb95ca0b12e22fa176c86b46e28b5 to your computer and use it in GitHub Desktop.
noninteracting_particle_MD.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "noninteracting_particle_MD.ipynb", | |
"version": "0.3.2", | |
"provenance": [], | |
"include_colab_link": true | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/jhjensen2/71bdb95ca0b12e22fa176c86b46e28b5/noninteracting_particle_md.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "JMMqD95zQmRq", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"def get_initial_coordinates():\n", | |
" x_coord = [np.random.random()*box_width for i in range(n_particles)]\n", | |
" y_coord = [np.random.random()*box_width for i in range(n_particles)]\n", | |
" \n", | |
" return x_coord, y_coord" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "_ejOpbfWSEwt", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"def get_initial_velocities():\n", | |
" x_vel = [2*(np.random.random()-0.5)*box_width for i in range(n_particles)]\n", | |
" y_vel = [2*(np.random.random()-0.5)*box_width for i in range(n_particles)]\n", | |
" \n", | |
" return x_vel, y_vel" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "8VIHckwKSvAc", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"def take_step(x_coord, y_coord, x_vel, y_vel):\n", | |
" for i in range(n_particles):\n", | |
" x_coord[i] += x_vel[i]*dt\n", | |
" y_coord[i] += y_vel[i]*dt\n", | |
" \n", | |
" if abs(x_coord[i]) > box_width:\n", | |
" x_vel[i] = -x_vel[i]\n", | |
" x_coord[i] += x_vel[i]*dt\n", | |
" \n", | |
" if abs(y_coord[i]) > box_width:\n", | |
" y_vel[i] = -y_vel[i]\n", | |
" y_coord[i] += y_vel[i]*dt\n", | |
" \n", | |
" return x_coord, y_coord, x_vel, y_vel" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "fNwIkKkNTaDX", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"def add_frame(xs,ys,i):\n", | |
" global trajectory\n", | |
" if i == 0:\n", | |
" trajectory = ''\n", | |
" trajectory += str(n_particles) + '\\ntitle\\n'\n", | |
" for x, y in zip(xs,ys):\n", | |
" trajectory += ' '.join(['Ar',str(x),str(y),'0.0\\n'])\n", | |
" " | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "madSLHmXGOO5", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"import numpy as np\n", | |
"\n", | |
"n_particles = 100\n", | |
"box_width = 10\n", | |
"n_steps = 5000\n", | |
"dt = 0.001\n", | |
"global trajectory\n", | |
"\n", | |
"x_coord, y_coord = get_initial_coordinates()\n", | |
"\n", | |
"x_vel, y_vel = get_initial_velocities()\n", | |
"\n", | |
"for i in range(n_steps):\n", | |
" x_coord, y_coord, x_vel, y_vel = take_step(x_coord, y_coord, x_vel, y_vel)\n", | |
" \n", | |
" if i%10 == 0:\n", | |
" add_frame(x_coord, y_coord,i)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "lA45pZozRkie", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 860 | |
}, | |
"collapsed": true, | |
"outputId": "eae74bf5-6fed-46cf-b597-def66631fa10" | |
}, | |
"cell_type": "code", | |
"source": [ | |
"!pip install py3Dmol\n", | |
"\n" | |
], | |
"execution_count": 6, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Requirement already satisfied: py3Dmol in /usr/local/lib/python2.7/dist-packages (0.8.0)\n", | |
"Requirement already satisfied: jupyter in /usr/local/lib/python2.7/dist-packages (from py3Dmol) (1.0.0)\n", | |
"Requirement already satisfied: idisplay in /usr/local/lib/python2.7/dist-packages (from py3Dmol) (0.1.2)\n", | |
"Requirement already satisfied: nbconvert in /usr/local/lib/python2.7/dist-packages (from jupyter->py3Dmol) (5.4.0)\n", | |
"Requirement already satisfied: ipywidgets in /usr/local/lib/python2.7/dist-packages (from jupyter->py3Dmol) (7.4.2)\n", | |
"Requirement already satisfied: qtconsole in /usr/local/lib/python2.7/dist-packages (from jupyter->py3Dmol) (4.4.2)\n", | |
"Requirement already satisfied: jupyter-console in /usr/local/lib/python2.7/dist-packages (from jupyter->py3Dmol) (5.2.0)\n", | |
"Requirement already satisfied: ipykernel in /usr/local/lib/python2.7/dist-packages (from jupyter->py3Dmol) (4.6.1)\n", | |
"Requirement already satisfied: notebook in /usr/local/lib/python2.7/dist-packages (from jupyter->py3Dmol) (5.2.2)\n", | |
"Requirement already satisfied: ipython>=1.0 in /usr/local/lib/python2.7/dist-packages (from idisplay->py3Dmol) (5.5.0)\n", | |
"Requirement already satisfied: entrypoints>=0.2.2 in /usr/local/lib/python2.7/dist-packages (from nbconvert->jupyter->py3Dmol) (0.2.3)\n", | |
"Requirement already satisfied: jinja2 in /usr/local/lib/python2.7/dist-packages (from nbconvert->jupyter->py3Dmol) (2.10)\n", | |
"Requirement already satisfied: nbformat>=4.4 in /usr/local/lib/python2.7/dist-packages (from nbconvert->jupyter->py3Dmol) (4.4.0)\n", | |
"Requirement already satisfied: defusedxml in /usr/local/lib/python2.7/dist-packages (from nbconvert->jupyter->py3Dmol) (0.5.0)\n", | |
"Requirement already satisfied: testpath in /usr/local/lib/python2.7/dist-packages (from nbconvert->jupyter->py3Dmol) (0.4.2)\n", | |
"Requirement already satisfied: bleach in /usr/local/lib/python2.7/dist-packages (from nbconvert->jupyter->py3Dmol) (3.0.2)\n", | |
"Requirement already satisfied: mistune>=0.8.1 in /usr/local/lib/python2.7/dist-packages (from nbconvert->jupyter->py3Dmol) (0.8.4)\n", | |
"Requirement already satisfied: jupyter-core in /usr/local/lib/python2.7/dist-packages (from nbconvert->jupyter->py3Dmol) (4.4.0)\n", | |
"Requirement already satisfied: pygments in /usr/local/lib/python2.7/dist-packages (from nbconvert->jupyter->py3Dmol) (2.1.3)\n", | |
"Requirement already satisfied: pandocfilters>=1.4.1 in /usr/local/lib/python2.7/dist-packages (from nbconvert->jupyter->py3Dmol) (1.4.2)\n", | |
"Requirement already satisfied: traitlets>=4.2 in /usr/local/lib/python2.7/dist-packages (from nbconvert->jupyter->py3Dmol) (4.3.2)\n", | |
"Requirement already satisfied: widgetsnbextension~=3.4.0 in /usr/local/lib/python2.7/dist-packages (from ipywidgets->jupyter->py3Dmol) (3.4.2)\n", | |
"Requirement already satisfied: jupyter-client>=4.1 in /usr/local/lib/python2.7/dist-packages (from qtconsole->jupyter->py3Dmol) (5.2.3)\n", | |
"Requirement already satisfied: ipython-genutils in /usr/local/lib/python2.7/dist-packages (from qtconsole->jupyter->py3Dmol) (0.2.0)\n", | |
"Requirement already satisfied: prompt-toolkit<2.0.0,>=1.0.0 in /usr/local/lib/python2.7/dist-packages (from jupyter-console->jupyter->py3Dmol) (1.0.15)\n", | |
"Requirement already satisfied: tornado>=4.0 in /usr/local/lib/python2.7/dist-packages (from ipykernel->jupyter->py3Dmol) (4.5.3)\n", | |
"Requirement already satisfied: terminado>=0.3.3; sys_platform != \"win32\" in /usr/local/lib/python2.7/dist-packages (from notebook->jupyter->py3Dmol) (0.8.1)\n", | |
"Requirement already satisfied: simplegeneric>0.8 in /usr/local/lib/python2.7/dist-packages (from ipython>=1.0->idisplay->py3Dmol) (0.8.1)\n", | |
"Requirement already satisfied: pickleshare in /usr/local/lib/python2.7/dist-packages (from ipython>=1.0->idisplay->py3Dmol) (0.7.5)\n", | |
"Requirement already satisfied: backports.shutil-get-terminal-size; python_version == \"2.7\" in /usr/local/lib/python2.7/dist-packages (from ipython>=1.0->idisplay->py3Dmol) (1.0.0)\n", | |
"Requirement already satisfied: pathlib2; python_version == \"2.7\" or python_version == \"3.3\" in /usr/local/lib/python2.7/dist-packages (from ipython>=1.0->idisplay->py3Dmol) (2.3.2)\n", | |
"Requirement already satisfied: pexpect; sys_platform != \"win32\" in /usr/local/lib/python2.7/dist-packages (from ipython>=1.0->idisplay->py3Dmol) (4.6.0)\n", | |
"Requirement already satisfied: decorator in /usr/local/lib/python2.7/dist-packages (from ipython>=1.0->idisplay->py3Dmol) (4.3.0)\n", | |
"Requirement already satisfied: setuptools>=18.5 in /usr/local/lib/python2.7/dist-packages (from ipython>=1.0->idisplay->py3Dmol) (39.1.0)\n", | |
"Requirement already satisfied: configparser>=3.5; python_version == \"2.7\" in /usr/local/lib/python2.7/dist-packages (from entrypoints>=0.2.2->nbconvert->jupyter->py3Dmol) (3.5.0)\n", | |
"Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python2.7/dist-packages (from jinja2->nbconvert->jupyter->py3Dmol) (1.0)\n", | |
"Requirement already satisfied: jsonschema!=2.5.0,>=2.4 in /usr/local/lib/python2.7/dist-packages (from nbformat>=4.4->nbconvert->jupyter->py3Dmol) (2.6.0)\n", | |
"Requirement already satisfied: six in /usr/local/lib/python2.7/dist-packages (from bleach->nbconvert->jupyter->py3Dmol) (1.11.0)\n", | |
"Requirement already satisfied: webencodings in /usr/local/lib/python2.7/dist-packages (from bleach->nbconvert->jupyter->py3Dmol) (0.5.1)\n", | |
"Requirement already satisfied: enum34; python_version == \"2.7\" in /usr/local/lib/python2.7/dist-packages (from traitlets>=4.2->nbconvert->jupyter->py3Dmol) (1.1.6)\n", | |
"Requirement already satisfied: pyzmq>=13 in /usr/local/lib/python2.7/dist-packages (from jupyter-client>=4.1->qtconsole->jupyter->py3Dmol) (16.0.4)\n", | |
"Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python2.7/dist-packages (from jupyter-client>=4.1->qtconsole->jupyter->py3Dmol) (2.5.3)\n", | |
"Requirement already satisfied: wcwidth in /usr/local/lib/python2.7/dist-packages (from prompt-toolkit<2.0.0,>=1.0.0->jupyter-console->jupyter->py3Dmol) (0.1.7)\n", | |
"Requirement already satisfied: singledispatch in /usr/local/lib/python2.7/dist-packages (from tornado>=4.0->ipykernel->jupyter->py3Dmol) (3.4.0.3)\n", | |
"Requirement already satisfied: certifi in /usr/local/lib/python2.7/dist-packages (from tornado>=4.0->ipykernel->jupyter->py3Dmol) (2018.8.24)\n", | |
"Requirement already satisfied: backports_abc>=0.4 in /usr/local/lib/python2.7/dist-packages (from tornado>=4.0->ipykernel->jupyter->py3Dmol) (0.5)\n", | |
"Requirement already satisfied: ptyprocess; os_name != \"nt\" in /usr/local/lib/python2.7/dist-packages (from terminado>=0.3.3; sys_platform != \"win32\"->notebook->jupyter->py3Dmol) (0.6.0)\n", | |
"Requirement already satisfied: scandir; python_version < \"3.5\" in /usr/local/lib/python2.7/dist-packages (from pathlib2; python_version == \"2.7\" or python_version == \"3.3\"->ipython>=1.0->idisplay->py3Dmol) (1.9.0)\n", | |
"Requirement already satisfied: functools32; python_version == \"2.7\" in /usr/local/lib/python2.7/dist-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.4->nbconvert->jupyter->py3Dmol) (3.2.3.post2)\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"id": "ELWnESw2TObR", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 514 | |
}, | |
"outputId": "1dcbb324-841d-41b9-d06a-5805bfdb3484" | |
}, | |
"cell_type": "code", | |
"source": [ | |
"import py3Dmol\n", | |
"view = py3Dmol.view()\n", | |
"view.addModelsAsFrames(trajectory,'xyz')\n", | |
"view.animate({'loop': 'forward', 'reps': 1})\n", | |
"view.setStyle({'sphere':{'radius': 0.5}})\n", | |
"view.zoomTo()" | |
], | |
"execution_count": 7, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment