|
2 | 2 | "cells": [ |
3 | 3 | { |
4 | 4 | "cell_type": "code", |
5 | | - "execution_count": 1, |
| 5 | + "execution_count": null, |
6 | 6 | "metadata": {}, |
7 | 7 | "outputs": [], |
8 | 8 | "source": [ |
9 | | - "# Import required libraries\n", |
10 | | - "from imdclient.IMD import IMDReader\n", |
| 9 | + "#Import required libraries\n", |
11 | 10 | "import MDAnalysis as mda\n", |
12 | 11 | "\n", |
13 | 12 | "import numpy as np\n", |
|
59 | 58 | } |
60 | 59 | ], |
61 | 60 | "source": [ |
62 | | - "%matplotlib widget\n", |
| 61 | + "#%matplotlib widget\n", |
63 | 62 | "NAMD_TOPOL = \"../exec-files/T3_MNN.psf\"\n", |
64 | 63 | "\n", |
65 | | - "# Initialize MDAnalysis Universe\n", |
66 | | - "u = mda.Universe(NAMD_TOPOL, \"imd://localhost:1027\")\n", |
67 | | - "\n", |
68 | | - "# Enable interactive plotting\n", |
69 | 64 | "plt.ion()\n", |
70 | 65 | "\n", |
71 | | - "# Initialize the plot\n", |
72 | 66 | "fig, ax = plt.subplots(figsize=(10,6))\n", |
73 | | - "line, = ax.plot([], [], lw=2) # Line for dynamic updates\n", |
74 | | - "ax.set_xlim(0, 1000) # Adjust as necessary\n", |
75 | | - "ax.set_ylim(-150, 150) # Adjust based on expected distance range\n", |
| 67 | + "line, = ax.plot([], [], lw=2)\n", |
| 68 | + "ax.set_xlim(0, 1000)\n", |
| 69 | + "ax.set_ylim(-150, 150)\n", |
76 | 70 | "ax.set_xlabel(\"Simulation Time (steps)\")\n", |
77 | 71 | "ax.set_ylabel(\"Charge Flow (nA)\")\n", |
78 | 72 | "ax.set_title(\"Ion Current vs. Time\")\n", |
79 | 73 | "\n", |
80 | | - "# Start the processing thread\n", |
81 | 74 | "x_data = np.zeros(144000) \n", |
82 | 75 | "y_data = np.zeros(144000)\n", |
83 | 76 | "\n", |
84 | 77 | "i = 0\n", |
85 | 78 | "\n", |
86 | | - "timestep = 2.0 # timestep passed to NAMD in conf file\n", |
87 | | - "framediff = 1 # IMDfreq\n", |
88 | | - "skip = 1 # if any frames need to be skipped\n", |
| 79 | + "timestep = 2.0 \n", |
| 80 | + "framediff = 1\n", |
| 81 | + "skip = 1\n", |
89 | 82 | "\n", |
90 | 83 | "update_frequency=1\n", |
91 | | - "chargeFactor = 1.6022e-19/1.0e-9/1.0e-9 #It's in nanoamps\n", |
| 84 | + "chargeFactor = 1.6022e-19/1.0e-9/1.0e-9\n", |
92 | 85 | "dt = 1.0*timestep*framediff*skip/1.0e6\n", |
93 | | - "#ionText = \"name POT CLA CAL SOD LIT CES IOD BRO H11 H21 H31 H41\"\n", |
94 | | - "ionText = \"name POT\"\n", |
95 | | - "ions = u.select_atoms(ionText)\n", |
96 | | - "charge = ions.charges\n", |
97 | | - "dims = ions.dimensions\n", |
98 | | - "zdist = dims[2]\n", |
| 86 | + "ionText = \"name POT CLA\"\n", |
99 | 87 | "\n", |
100 | | - "for t in u.trajectory:\n", |
101 | | - " newPos = ions.positions\n", |
102 | | - " idx = i % 1000\n", |
| 88 | + "try:\n", |
| 89 | + " u = mda.Universe(NAMD_TOPOL, \"imd://localhost:8888\", buffersize = 1000*1024*1024)\n", |
| 90 | + " ions = u.select_atoms(ionText)\n", |
| 91 | + " charge = ions.charges\n", |
| 92 | + " dims = ions.dimensions\n", |
| 93 | + " zdist = dims[2]\n", |
| 94 | + "except Exception as e:\n", |
| 95 | + " print(f\"Error during connection: {e}\")\n", |
| 96 | + "else:\n", |
| 97 | + " try:\n", |
| 98 | + " for t in u.trajectory:\n", |
| 99 | + " newPos = ions.positions\n", |
| 100 | + " idx = i % 1000\n", |
103 | 101 | "\n", |
104 | | - " if i == 0:\n", |
105 | | - " ionPos = newPos\n", |
106 | | - " else:\n", |
107 | | - " deltas = newPos[:,2]-ionPos[:,2]\n", |
108 | | - " deltas[deltas>zdist/2.] = deltas[deltas>zdist/2.] - zdist\n", |
109 | | - " deltas[deltas<-zdist/2.] = deltas[deltas<-zdist/2.] + zdist\n", |
110 | | - " cd = deltas*charge\n", |
111 | | - " tCur = np.sum(cd)/zdist/dt*chargeFactor\n", |
112 | | - " ionPos = newPos\n", |
113 | | - " time = round(dt * i - dt/2, 4)\n", |
114 | | - " x_data[i] = i + 1\n", |
115 | | - " y_data[i] = tCur\n", |
116 | | - " i = i + 1\n", |
| 102 | + " if i == 0:\n", |
| 103 | + " ionPos = newPos\n", |
| 104 | + " else:\n", |
| 105 | + " deltas = newPos[:,2]-ionPos[:,2]\n", |
| 106 | + " deltas[deltas>zdist/2.] = deltas[deltas>zdist/2.] - zdist\n", |
| 107 | + " deltas[deltas<-zdist/2.] = deltas[deltas<-zdist/2.] + zdist\n", |
| 108 | + " cd = deltas*charge\n", |
| 109 | + " tCur = np.sum(cd)/zdist/dt*chargeFactor\n", |
| 110 | + " ionPos = newPos\n", |
| 111 | + " time = round(dt * i - dt/2, 4)\n", |
| 112 | + " x_data[i] = i + 1\n", |
| 113 | + " y_data[i] = tCur\n", |
| 114 | + " i = i + 1\n", |
117 | 115 | "\n", |
118 | | - " valid_indices = range(0, idx + 1)\n", |
| 116 | + " valid_indices = range(0, idx + 1)\n", |
119 | 117 | "\n", |
120 | | - " # Update the plot periodically\n", |
121 | | - " if i % update_frequency == 0:\n", |
122 | | - " line.set_data(x_data[valid_indices], y_data[valid_indices]) # Efficiently update the line with all data\n", |
123 | | - " # ax.set_xlim(0, max(200,max(x_data)+100)) # Adjust X-axis dynamically\n", |
124 | | - " clear_output(wait=True)\n", |
125 | | - " display(fig)" |
| 118 | + " if i % update_frequency == 0:\n", |
| 119 | + " line.set_data(x_data[valid_indices], y_data[valid_indices])\n", |
| 120 | + " clear_output(wait=True)\n", |
| 121 | + " display(fig)\n", |
| 122 | + " finally:\n", |
| 123 | + " u.trajectory.close()" |
126 | 124 | ] |
127 | 125 | } |
128 | 126 | ], |
|
0 commit comments