diff --git a/MDvis.ipynb b/MDvis.ipynb new file mode 100644 index 0000000..8562b20 --- /dev/null +++ b/MDvis.ipynb @@ -0,0 +1,245 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "intro", + "metadata": {}, + "source": [ + "# SimpleMD — In-Notebook Visualization\n", + "\n", + "This notebook runs a short Lennard-Jones simulation and visualizes the particle trajectory using [py3Dmol](https://github.com/3dmol/3Dmol.js), which renders interactively in VS Code, Jupyter, and Google Colab.\n", + "\n", + "For more advanced visualization (depth cueing, rendering), see the VMD instructions in the SimpleMD manual (Ch. 5).\n", + "\n", + "**Controls**\n", + "- Drag to rotate\n", + "- Scroll to zoom\n", + "- Right-click drag to pan" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "colab-setup", + "metadata": {}, + "outputs": [], + "source": [ + "# --- Google Colab only: clone the repo and install py3Dmol ---\n", + "# Skip this cell if running locally.\n", + "import sys\n", + "if 'google.colab' in sys.modules:\n", + " !git clone https://github.com/emfurst/SimpleMD.git\n", + " %cd SimpleMD\n", + " !pip install py3Dmol --quiet" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "imports", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import py3Dmol\n", + "import CHEG231MD as md" + ] + }, + { + "cell_type": "markdown", + "id": "sim-header", + "metadata": {}, + "source": [ + "## Run the simulation\n", + "\n", + "Set up and equilibrate, then run a short production trajectory.\n", + "Adjust `nn`, `rho`, and `vmax` to explore different conditions." + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "id": "run-sim", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Equilibrated: T* = 0.614 P* = -0.392\n" + ] + } + ], + "source": [ + "nn = 5 # particles per side (N = nn^3)\n", + "rho = 0.4 # reduced number density\n", + "vmax = 1.5 # initial max velocity\n", + "\n", + "sim = md.MDSimulation(nn, rho, vmax)\n", + "\n", + "# Equilibration\n", + "for _ in range(200):\n", + " sim.move()\n", + "print(f\"Equilibrated: T* = {sim.T:.3f} P* = {sim.P:.3f}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "write-traj", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Wrote trajectory to MDvis.xyz (T* = 0.836, P* = -0.122)\n" + ] + } + ], + "source": [ + "XYZ_FILE = \"MDvis.xyz\"\n", + "\n", + "def write_frame(f, sim, timestep):\n", + " f.write(f\"{sim.N}\\n\")\n", + " f.write(f\"Timestep: {timestep}\\n\")\n", + " for row in sim.x:\n", + " f.write(f\"Ar {row[0]:.6f} {row[1]:.6f} {row[2]:.6f}\\n\")\n", + "\n", + "# Production run — write every 5th frame\n", + "with open(XYZ_FILE, \"w\") as f:\n", + " for t in range(1, 501):\n", + " sim.move()\n", + " if t % 10 == 0:\n", + " write_frame(f, sim, t)\n", + "\n", + "print(f\"Wrote trajectory to {XYZ_FILE} (T* = {sim.T:.3f}, P* = {sim.P:.3f})\")" + ] + }, + { + "cell_type": "markdown", + "id": "vis-header", + "metadata": {}, + "source": [ + "## Animated trajectory\n", + "\n", + "The viewer below plays through all saved frames.\n", + "The wireframe box shows the periodic simulation cell." + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "id": "animate", + "metadata": {}, + "outputs": [ + { + "data": { + "application/3dmoljs_load.v0": "
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
3Dmol.js failed to load for some reason. Please check your browser console for error messages.