From 963ef4c1c5a608c8b0f9e5ca9774430a3f2f6aa5 Mon Sep 17 00:00:00 2001 From: ljwoods2 Date: Fri, 29 Nov 2024 14:53:12 -0700 Subject: [PATCH] talk 50% complete --- .devcontainer/devcontainer.json | 5 +- activity/activity.ipynb | 111 +++++++---------- activity/run.sh | 16 --- streaming-101/imd-mda/imd-mda.ipynb | 187 ++++++++++++++++------------ streaming-101/imd-mda/run.sh | 14 +++ 5 files changed, 162 insertions(+), 171 deletions(-) create mode 100755 streaming-101/imd-mda/run.sh diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 46f17e4..3d56566 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -2,10 +2,9 @@ "build": { "dockerfile": "Dockerfile" }, "customizations": { "vscode": { - "extensions": ["ms-python.python", "ms-toolsai.jupyter"], + "extensions": ["ms-python.python", "ms-toolsai.jupyter", "donjayamanne.vscode-default-python-kernel"], "settings": { - "python.defaultInterpreterPath": "/opt/conda/envs/workshop/bin/python", - "jupyter.defaultKernel": "workshop" + "python.defaultInterpreterPath": "/opt/conda/envs/workshop/bin/python" } } }, diff --git a/activity/activity.ipynb b/activity/activity.ipynb index 290ca0f..8c131ed 100644 --- a/activity/activity.ipynb +++ b/activity/activity.ipynb @@ -51,36 +51,9 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "ename": "ConnectionError", - "evalue": "IMDClient: No handshake packet received", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTimeoutError\u001b[0m Traceback (most recent call last)", - "File \u001b[0;32m~/workspace/imdclient/imdclient/utils.py:89\u001b[0m, in \u001b[0;36mread_into_buf\u001b[0;34m(sock, buf)\u001b[0m\n\u001b[1;32m 88\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m---> 89\u001b[0m received \u001b[38;5;241m=\u001b[39m sock\u001b[38;5;241m.\u001b[39mrecv_into(view[total_received:])\n\u001b[1;32m 90\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m received \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m:\n", - "\u001b[0;31mTimeoutError\u001b[0m: timed out", - "\nDuring handling of the above exception, another exception occurred:\n", - "\u001b[0;31mTimeoutError\u001b[0m Traceback (most recent call last)", - "File \u001b[0;32m~/workspace/imdclient/imdclient/IMDClient.py:226\u001b[0m, in \u001b[0;36mIMDClient._await_IMD_handshake\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 225\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 226\u001b[0m read_into_buf(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_conn, h_buf)\n\u001b[1;32m 227\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m (\u001b[38;5;167;01mConnectionError\u001b[39;00m, \u001b[38;5;167;01mTimeoutError\u001b[39;00m, \u001b[38;5;167;01mException\u001b[39;00m) \u001b[38;5;28;01mas\u001b[39;00m e:\n", - "File \u001b[0;32m~/workspace/imdclient/imdclient/utils.py:97\u001b[0m, in \u001b[0;36mread_into_buf\u001b[0;34m(sock, buf)\u001b[0m\n\u001b[1;32m 96\u001b[0m logger\u001b[38;5;241m.\u001b[39mdebug(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mread_into_buf excepting due to read timeout\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m---> 97\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTimeoutError\u001b[39;00m\n\u001b[1;32m 98\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBlockingIOError\u001b[39;00m:\n", - "\u001b[0;31mTimeoutError\u001b[0m: ", - "\nDuring handling of the above exception, another exception occurred:\n", - "\u001b[0;31mConnectionError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[2], line 4\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mimdclient\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mIMD\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m IMDReader\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mMDAnalysis\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mmda\u001b[39;00m\n\u001b[0;32m----> 4\u001b[0m u \u001b[38;5;241m=\u001b[39m mda\u001b[38;5;241m.\u001b[39mUniverse(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msample_simulation/imdgroup.gro\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mimd://localhost:8889\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 6\u001b[0m \u001b[38;5;66;03m# Get an atom from the topology\u001b[39;00m\n\u001b[1;32m 7\u001b[0m \u001b[38;5;66;03m# Universe -> AtomGroup -> Atom\u001b[39;00m\n\u001b[1;32m 8\u001b[0m atom \u001b[38;5;241m=\u001b[39m u\u001b[38;5;241m.\u001b[39matoms[\u001b[38;5;241m0\u001b[39m]\n", - "File \u001b[0;32m~/anaconda3/envs/imdclient-test/lib/python3.12/site-packages/MDAnalysis/core/universe.py:375\u001b[0m, in \u001b[0;36mUniverse.__init__\u001b[0;34m(self, topology, all_coordinates, format, topology_format, transformations, guess_bonds, vdwradii, fudge_factor, lower_bound, in_memory, in_memory_step, *coordinates, **kwargs)\u001b[0m\n\u001b[1;32m 370\u001b[0m coordinates \u001b[38;5;241m=\u001b[39m _resolve_coordinates(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mfilename, \u001b[38;5;241m*\u001b[39mcoordinates,\n\u001b[1;32m 371\u001b[0m \u001b[38;5;28mformat\u001b[39m\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mformat\u001b[39m,\n\u001b[1;32m 372\u001b[0m all_coordinates\u001b[38;5;241m=\u001b[39mall_coordinates)\n\u001b[1;32m 374\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m coordinates:\n\u001b[0;32m--> 375\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mload_new(coordinates, \u001b[38;5;28mformat\u001b[39m\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mformat\u001b[39m, in_memory\u001b[38;5;241m=\u001b[39min_memory,\n\u001b[1;32m 376\u001b[0m in_memory_step\u001b[38;5;241m=\u001b[39min_memory_step, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[1;32m 378\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m transformations:\n\u001b[1;32m 379\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mcallable\u001b[39m(transformations):\n", - "File \u001b[0;32m~/anaconda3/envs/imdclient-test/lib/python3.12/site-packages/MDAnalysis/core/universe.py:580\u001b[0m, in \u001b[0;36mUniverse.load_new\u001b[0;34m(self, filename, format, in_memory, in_memory_step, **kwargs)\u001b[0m\n\u001b[1;32m 577\u001b[0m \u001b[38;5;66;03m# supply number of atoms for readers that cannot do it for themselves\u001b[39;00m\n\u001b[1;32m 578\u001b[0m kwargs[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mn_atoms\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39matoms\u001b[38;5;241m.\u001b[39mn_atoms\n\u001b[0;32m--> 580\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtrajectory \u001b[38;5;241m=\u001b[39m reader(filename, \u001b[38;5;28mformat\u001b[39m\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mformat\u001b[39m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[1;32m 581\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtrajectory\u001b[38;5;241m.\u001b[39mn_atoms \u001b[38;5;241m!=\u001b[39m \u001b[38;5;28mlen\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39matoms):\n\u001b[1;32m 582\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThe topology and \u001b[39m\u001b[38;5;132;01m{form}\u001b[39;00m\u001b[38;5;124m trajectory files don\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 583\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m have the same number of atoms!\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 584\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mTopology number of atoms \u001b[39m\u001b[38;5;132;01m{top_n_atoms}\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 588\u001b[0m fname\u001b[38;5;241m=\u001b[39mfilename,\n\u001b[1;32m 589\u001b[0m trj_n_atoms\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtrajectory\u001b[38;5;241m.\u001b[39mn_atoms))\n", - "File \u001b[0;32m~/anaconda3/envs/imdclient-test/lib/python3.12/site-packages/MDAnalysis/lib/util.py:2553\u001b[0m, in \u001b[0;36mstore_init_arguments..wrapper\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 2551\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 2552\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_kwargs[key] \u001b[38;5;241m=\u001b[39m arg\n\u001b[0;32m-> 2553\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m func(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n", - "File \u001b[0;32m~/workspace/imdclient/imdclient/IMD.py:62\u001b[0m, in \u001b[0;36mIMDReader.__init__\u001b[0;34m(self, filename, convert_units, n_atoms, **kwargs)\u001b[0m\n\u001b[1;32m 59\u001b[0m host, port \u001b[38;5;241m=\u001b[39m parse_host_port(filename)\n\u001b[1;32m 61\u001b[0m \u001b[38;5;66;03m# This starts the simulation\u001b[39;00m\n\u001b[0;32m---> 62\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_imdclient \u001b[38;5;241m=\u001b[39m IMDClient(host, port, n_atoms, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[1;32m 64\u001b[0m imdsinfo \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_imdclient\u001b[38;5;241m.\u001b[39mget_imdsessioninfo()\n\u001b[1;32m 65\u001b[0m \u001b[38;5;66;03m# NOTE: after testing phase, fail out on IMDv2\u001b[39;00m\n", - "File \u001b[0;32m~/workspace/imdclient/imdclient/IMDClient.py:65\u001b[0m, in \u001b[0;36mIMDClient.__init__\u001b[0;34m(self, host, port, n_atoms, socket_bufsize, multithreaded, **kwargs)\u001b[0m\n\u001b[1;32m 63\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_stopped \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m\n\u001b[1;32m 64\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_conn \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_connect_to_server(host, port, socket_bufsize)\n\u001b[0;32m---> 65\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_imdsinfo \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_await_IMD_handshake()\n\u001b[1;32m 66\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_multithreaded \u001b[38;5;241m=\u001b[39m multithreaded\n\u001b[1;32m 68\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_multithreaded:\n", - "File \u001b[0;32m~/workspace/imdclient/imdclient/IMDClient.py:229\u001b[0m, in \u001b[0;36mIMDClient._await_IMD_handshake\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 227\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m (\u001b[38;5;167;01mConnectionError\u001b[39;00m, \u001b[38;5;167;01mTimeoutError\u001b[39;00m, \u001b[38;5;167;01mException\u001b[39;00m) \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m 228\u001b[0m logger\u001b[38;5;241m.\u001b[39mdebug(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIMDClient: No handshake packet received: \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m\"\u001b[39m, e)\n\u001b[0;32m--> 229\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mConnectionError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIMDClient: No handshake packet received\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 231\u001b[0m header \u001b[38;5;241m=\u001b[39m IMDHeader(h_buf)\n\u001b[1;32m 233\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m header\u001b[38;5;241m.\u001b[39mtype \u001b[38;5;241m!=\u001b[39m IMDHeaderType\u001b[38;5;241m.\u001b[39mIMD_HANDSHAKE:\n", - "\u001b[0;31mConnectionError\u001b[0m: IMDClient: No handshake packet received" - ] - } - ], + "outputs": [], "source": [ "from imdclient.IMD import IMDReader\n", "import MDAnalysis as mda\n", @@ -113,23 +86,7 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/home/law/anaconda3/envs/imdclient-test/lib/python3.12/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", - " from .autonotebook import tqdm as notebook_tqdm\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "At time 0.070 fs, the n-terminus and c-terminus are 20.697 angrstroms apart\r" - ] - } - ], + "outputs": [], "source": [ "from imdclient.IMD import IMDReader\n", "import MDAnalysis as mda\n", @@ -151,35 +108,15 @@ "metadata": {}, "source": [ "## Live visualization of an analysis\n", - "\n" + "\n", + "Finally, we'll use matplot to create a live visualization of the distance between the two atoms." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/home/law/anaconda3/envs/imdclient-test/lib/python3.12/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", - " from .autonotebook import tqdm as notebook_tqdm\n" - ] - }, - { - "ename": "ValueError", - "evalue": "", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[1], line 22\u001b[0m\n\u001b[1;32m 19\u001b[0m nter \u001b[38;5;241m=\u001b[39m u\u001b[38;5;241m.\u001b[39mselect_atoms(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mresid 1 and name CA\u001b[39m\u001b[38;5;124m\"\u001b[39m)\u001b[38;5;241m.\u001b[39matoms[\u001b[38;5;241m0\u001b[39m]\n\u001b[1;32m 20\u001b[0m cter \u001b[38;5;241m=\u001b[39m u\u001b[38;5;241m.\u001b[39mselect_atoms(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mresid 129 and name CA\u001b[39m\u001b[38;5;124m\"\u001b[39m)\u001b[38;5;241m.\u001b[39matoms[\u001b[38;5;241m0\u001b[39m]\n\u001b[0;32m---> 22\u001b[0m graph \u001b[38;5;241m=\u001b[39m LiveScalarGraph(\n\u001b[1;32m 23\u001b[0m time_window\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1.0\u001b[39m, \u001b[38;5;66;03m# ps\u001b[39;00m\n\u001b[1;32m 24\u001b[0m dt\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0.010\u001b[39m, \u001b[38;5;66;03m# ps\u001b[39;00m\n\u001b[1;32m 25\u001b[0m title\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mDistance vs. Time\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[1;32m 26\u001b[0m ylabel\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mDistance (Å)\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[1;32m 27\u001b[0m legend_label\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mNter-Cter\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[1;32m 28\u001b[0m )\n\u001b[1;32m 30\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m ts \u001b[38;5;129;01min\u001b[39;00m u\u001b[38;5;241m.\u001b[39mtrajectory:\n\u001b[1;32m 31\u001b[0m distance \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39msqrt(np\u001b[38;5;241m.\u001b[39msum((nter\u001b[38;5;241m.\u001b[39mposition \u001b[38;5;241m-\u001b[39m cter\u001b[38;5;241m.\u001b[39mposition) \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39m \u001b[38;5;241m2\u001b[39m))\n", - "File \u001b[0;32m~/workspace/imd-workshop-2024/activity/graph_utils.py:9\u001b[0m, in \u001b[0;36mLiveScalarGraph.__init__\u001b[0;34m(self, time_window, dt, title, ylabel, legend_label)\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\u001b[38;5;28mself\u001b[39m, time_window, dt, title\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m, ylabel\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m, legend_label\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[1;32m 8\u001b[0m plt\u001b[38;5;241m.\u001b[39mion()\n\u001b[0;32m----> 9\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m\n\u001b[1;32m 10\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtime_window \u001b[38;5;241m=\u001b[39m time_window\n\u001b[1;32m 11\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mn_times \u001b[38;5;241m=\u001b[39m (\u001b[38;5;28mint\u001b[39m)(time_window \u001b[38;5;241m/\u001b[39m dt)\n", - "\u001b[0;31mValueError\u001b[0m: " - ] - } - ], + "outputs": [], "source": [ "from imdclient.IMD import IMDReader\n", "import numpy as np\n", @@ -205,6 +142,42 @@ "\n", "\n" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Challenge 1\n", + "\n", + "Your first challenge is to implement a new analysis function that calculates the number of water molecules within 4 angstroms of the protein during each timestep.\n", + "\n", + "Bonus points if you can create a live visualization of this data!\n", + "\n", + "*Hint*: \n", + "\n", + "All water molecules in the simulation can be selected like this:\n", + "```python\n", + "u.select_atoms('resname SOL')\n", + "```\n", + "For more advanced selection tips, see the [MDAnalysis documentation](https://userguide.mdanalysis.org/stable/selections.html)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from imdclient.IMD import IMDReader\n", + "import numpy as np\n", + "import MDAnalysis as mda\n", + "from graph_utils import LiveTimeseriesGraph\n", + "\n", + "u = mda.Universe(\"sample_simulation/imdgroup.gro\", \"imd://localhost:8889\")\n", + "\n", + "for ts in u.trajectory:\n", + " ## Your analysis code here" + ] } ], "metadata": { diff --git a/activity/run.sh b/activity/run.sh index 80426f6..d27f62e 100755 --- a/activity/run.sh +++ b/activity/run.sh @@ -1,21 +1,5 @@ #!/bin/bash -## TMP -source /usr/local/gromacs/bin/GMXRC - -## WAIT UNTIL PORT IS OPEN -SECONDS=0 -while lsof -n -i :8889; do - echo "Waiting for port 8889 to open" - sleep 1 - - if [ $SECONDS -ge 30 ]; then - echo "Timeout reached. Port 8889 did not open." - exit 1 - fi -done - -mkdir -p sample_simulation cd sample_simulation ## RUN GROMPP diff --git a/streaming-101/imd-mda/imd-mda.ipynb b/streaming-101/imd-mda/imd-mda.ipynb index b89c5b5..3dbd49b 100644 --- a/streaming-101/imd-mda/imd-mda.ipynb +++ b/streaming-101/imd-mda/imd-mda.ipynb @@ -8,30 +8,45 @@ "\n", "## TODO:\n", "\n", - "a context manager?\n", - "better error messages\n", - "remove unprofessional log messages\n", - "add docs for some of the messages (timeout at least)\n", - "make a nice diagram" + "Make diagrams\n", + "\n", + "Finish analysis section" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "IMDSessionInfo(version=3, endianness='<', wrapped_coords=True, energies=False, time=True, box=True, positions=True, velocities=True, forces=True)\n", + "Simulation integration step: 10\n", + "Simulation time (fs): 0.01\n", + "First atom's position (angstroms): [44.03448 29.363623 30.971706]\n" + ] + } + ], "source": [ - "# ./run.sh \n", - "\n", "import imdclient\n", "\n", - "client = imdclient.IMDClient(\"localhost\", 8889, n_atoms=100)\n", - "\n", - "info = client.get_imdsessioninfo()\n", - "## TODO: show what's in dict\n", - "## design principle: reduce redundancy in data entry\n", + "with imdclient.IMDClient(\"localhost\", 8889, n_atoms=50786) as client:\n", + " info = client.get_imdsessioninfo()\n", + " print(info)\n", "\n", - "client.stop()" + " frame = client.get_imdframe()\n", + " print(f\"Simulation integration step: {frame.step}\")\n", + " print(f\"Simulation time (fs): {frame.time}\")\n", + " print(f\"First atom's position (angstroms): {frame.positions[0]}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Iterating through a trajectory" ] }, { @@ -40,18 +55,15 @@ "metadata": {}, "outputs": [], "source": [ - "client = imdclient.IMDClient(\"localhost\", 8889, n_atoms=100)\n", - "\n", - "## Design principle: simplest possible API so anyone can use it\n", - "## Even if you hate mdanalysis, you can use this\n", - "\n", - "while True:\n", - " try:\n", - " frame = client.get_imdframe()\n", - " except EOFError:\n", - " break\n", + "import imdclient\n", "\n", - "client.stop()" + "with imdclient.IMDClient(\"localhost\", 8889, n_atoms=50786) as client:\n", + " while True:\n", + " try:\n", + " frame = client.get_imdframe()\n", + " # Do something with the frame\n", + " except EOFError:\n", + " break" ] }, { @@ -67,23 +79,28 @@ "metadata": {}, "outputs": [], "source": [ - "client = imdclient.IMDClient(\"localhost\", 8889, n_atoms=100, timeout=10)\n", - "\n", - "while True:\n", - " try:\n", - " ## Add a little wait time widget\n", - " frame = client.get_imdframe()\n", - " except EOFError:\n", - " break\n", + "import imdclient\n", "\n", - "client.stop()" + "client = imdclient.IMDClient(\"localhost\", 8889,\n", + " n_atoms=50786, \n", + " # Wait up to 10 seconds for a simulation frame\n", + " timeout=10,\n", + " # 1 MB\n", + " buffer_size=1024 ** 2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Software architecture" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## The buffer size is optional, pausing is automatic" + "## Automatic pausing and resuming" ] }, { @@ -92,43 +109,44 @@ "metadata": {}, "outputs": [], "source": [ - "# ./run.sh\n", - "\n", - "import time\n", - "import logging\n", - "\n", - "logger = logging.getLogger(\"imdclient.IMDClient\")\n", - "# Modify to use \n", - "# file_handler = logging.FileHandler(\"gromacs_test.log\")\n", - "formatter = logging.Formatter(\n", - " \"%(asctime)s - %(name)s - %(levelname)s - %(message)s\"\n", - ")\n", - "# file_handler.setFormatter(formatter)\n", - "# logger.addHandler(file_handler)\n", - "# logger.setLevel(logging.DEBUG)\n", - "\n", - "## Show the logs of the client stopping automatically\n", + "import imdclient\n", "\n", - "client = imdclient.IMDClient(\"localhost\", 8889, n_atoms=100, buffer_size=100)" + "# 2MB Buffer\n", + "with imdclient.IMDClient(\"localhost\", 8889, n_atoms=50786, buffer_size=2 * 1024**2) as client:\n", + " while True:\n", + " try:\n", + " frame = client.get_imdframe()\n", + " except EOFError:\n", + " break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## Reader wraps client, handles its limitations" + "## Reader wraps client, handles its limitations\n", + "\n", + "A stream can only be read once. This is a limitation of the client. The reader wraps the client and handles this limitation." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "A stream is not a file!\n" + ] + } + ], "source": [ - "from imdclient.IMDREADER import IMDReader\n", + "from imdclient.IMD import IMDReader\n", "import MDAnalysis as mda\n", "\n", - "u = mda.Universe(top, \"imd://localhost:8889\")\n", + "u = mda.Universe(\"sample_simulation/imdgroup.gro\", \"imd://localhost:8889\")\n", "\n", "for ts in u.trajectory[:]:\n", " pass\n", @@ -144,41 +162,42 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Stepping is allowed, but specifying an end frame isn't" + "## API interactions" ] }, { - "cell_type": "code", - "execution_count": 1, + "cell_type": "markdown", "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'top' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[1], line 4\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mimdclient\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mIMDREADER\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m IMDReader\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mMDAnalysis\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mmda\u001b[39;00m\n\u001b[0;32m----> 4\u001b[0m u \u001b[38;5;241m=\u001b[39m mda\u001b[38;5;241m.\u001b[39mUniverse(top, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mimd://localhost:8889\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m ts \u001b[38;5;129;01min\u001b[39;00m u\u001b[38;5;241m.\u001b[39mtrajectory[:]:\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28;01mpass\u001b[39;00m\n", - "\u001b[0;31mNameError\u001b[0m: name 'top' is not defined" - ] - } - ], "source": [ - "from imdclient.IMDREADER import IMDReader\n", - "import MDAnalysis as mda\n", - "\n", - "u = mda.Universe(top, \"imd://localhost:8889\")\n", + "## Stepping is allowed, but specifying an end frame isn't\n", "\n", + "This is allowed\n", + "```python\n", "for ts in u.trajectory[::10]:\n", - " pass" + " pass\n", + "```\n", + "\n", + "This is not\n", + "```python\n", + "for ts in u.trajectory[:10]:\n", + " pass\n", + "```\n", + "\n", + "The length of the trajectory is not known until it is iterated through.\n", + "\n", + "```python\n", + "# Not allowed!\n", + "len(u.trajectory)\n", + "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## The client works out of the box with MDAnalysis analysis classes" + "## The client works out of the box with MDAnalysis analysis classes\n", + "\n", + "The analysis class must be able to handle a trajectory without a known length." ] }, { @@ -187,13 +206,15 @@ "metadata": {}, "outputs": [], "source": [ - "from imdclient.IMDREADER import IMDReader\n", + "from imdclient.IMD import IMDReader\n", "import MDAnalysis as mda\n", "from MDAnalysis.analysis.rms import RMSF\n", "\n", - "u = mda.Universe(top, \"imd://localhost:8889\")\n", + "u = mda.Universe(\"sample_simulation/imdgroup.gro\", \"imd://localhost:8889\")\n", + "\n", + "imd_rmsf = RMSF(u.atoms).run()\n", "\n", - "imd_rmsf = RMSF(u.atoms).run()" + "print(imd_rmsf.rmsf)" ] }, { diff --git a/streaming-101/imd-mda/run.sh b/streaming-101/imd-mda/run.sh new file mode 100755 index 0000000..d27f62e --- /dev/null +++ b/streaming-101/imd-mda/run.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +cd sample_simulation + +## RUN GROMPP +if [ ! -f sample_simulation/topol.tpr ]; then +echo "Running grompp" +gmx grompp -f imd.mdp -c start.gro -p topol.top -o >& grompp.out +fi +echo "Starting mdrun" + +## START MDRUN +gmx mdrun -v -nt 1 -imdwait -imdport 8889 +cd ..