Skip to content

Commit c6dacf0

Browse files
committed
Updated code NAMD ion-flux demo to new version. Added run.sh files to run the NAMD simulation, but NAMD needs to be built to execute the command
1 parent 4cc99f4 commit c6dacf0

File tree

2 files changed

+48
-46
lines changed

2 files changed

+48
-46
lines changed

namd-demos/exec-files/run.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
~/namd-3.0/Linux-x86_64-g++/namd3 +p8 150mV-2.conf
4+

namd-demos/ion-flux/ion-flux.ipynb

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 1,
5+
"execution_count": null,
66
"metadata": {},
77
"outputs": [],
88
"source": [
9-
"# Import required libraries\n",
10-
"from imdclient.IMD import IMDReader\n",
9+
"#Import required libraries\n",
1110
"import MDAnalysis as mda\n",
1211
"\n",
1312
"import numpy as np\n",
@@ -59,70 +58,69 @@
5958
}
6059
],
6160
"source": [
62-
"%matplotlib widget\n",
61+
"#%matplotlib widget\n",
6362
"NAMD_TOPOL = \"../exec-files/T3_MNN.psf\"\n",
6463
"\n",
65-
"# Initialize MDAnalysis Universe\n",
66-
"u = mda.Universe(NAMD_TOPOL, \"imd://localhost:1027\")\n",
67-
"\n",
68-
"# Enable interactive plotting\n",
6964
"plt.ion()\n",
7065
"\n",
71-
"# Initialize the plot\n",
7266
"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",
7670
"ax.set_xlabel(\"Simulation Time (steps)\")\n",
7771
"ax.set_ylabel(\"Charge Flow (nA)\")\n",
7872
"ax.set_title(\"Ion Current vs. Time\")\n",
7973
"\n",
80-
"# Start the processing thread\n",
8174
"x_data = np.zeros(144000) \n",
8275
"y_data = np.zeros(144000)\n",
8376
"\n",
8477
"i = 0\n",
8578
"\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",
8982
"\n",
9083
"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",
9285
"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",
9987
"\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",
103101
"\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",
117115
"\n",
118-
" valid_indices = range(0, idx + 1)\n",
116+
" valid_indices = range(0, idx + 1)\n",
119117
"\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()"
126124
]
127125
}
128126
],

0 commit comments

Comments
 (0)