|
17 | 17 | "\n", |
18 | 18 | "## Exercise 1 Solution: Charge Density Analysis\n", |
19 | 19 | "\n", |
20 | | - "Calculate the total charge within 4 Å of the protein at each timestep." |
| 20 | + "Calculate the total charge within 8 Å of the protein at each timestep." |
21 | 21 | ] |
22 | 22 | }, |
23 | 23 | { |
|
26 | 26 | "metadata": {}, |
27 | 27 | "outputs": [], |
28 | 28 | "source": [ |
29 | | - "from imdclient.IMD import IMDReader\n", |
30 | 29 | "import MDAnalysis as mda\n", |
31 | 30 | "import numpy as np\n", |
32 | 31 | "from graph_utils import live_plot\n", |
33 | 32 | "\n", |
34 | 33 | "u = mda.Universe(\"sample_simulation/GROMACS/input/start.gro\", \"imd://localhost:8889\", buffer_size=100*1024**2)\n", |
35 | 34 | "\n", |
36 | 35 | "try:\n", |
37 | | - " # Select solvent atoms within 4 Å of protein (updating each frame)\n", |
38 | | - " nearby = u.select_atoms('not protein and around 4 protein', updating=True)\n", |
| 36 | + " # Select Na+ and Cl- ions within 8 Å of protein (updating each frame)\n", |
| 37 | + " na_sel = u.select_atoms('resname SOD and around 8 protein', updating=True)\n", |
| 38 | + " cl_sel = u.select_atoms('resname CLA and around 8 protein', updating=True)\n", |
39 | 39 | "\n", |
40 | 40 | " # Create live plot\n", |
41 | 41 | " plot = live_plot(\n", |
42 | | - " title=\"Charge Density within 4 Å\",\n", |
| 42 | + " title=\"Net Ion Charge within 8 Å\",\n", |
43 | 43 | " ylabel=\"Total Charge (e)\",\n", |
44 | 44 | " update_interval=1\n", |
45 | 45 | " )\n", |
46 | 46 | "\n", |
47 | 47 | " for ts in u.trajectory:\n", |
48 | | - " # Calculate total charge in selection\n", |
49 | | - " total_charge = np.sum(nearby.charges)\n", |
| 48 | + " n_na = na_sel.n_atoms\n", |
| 49 | + " n_cl = cl_sel.n_atoms\n", |
| 50 | + " total_charge = n_na - n_cl # +1 for Na, -1 for Cl\n", |
50 | 51 | " plot['update'](ts.time, total_charge)\n", |
51 | | - " \n", |
52 | | - " \n", |
53 | | - " print(f\"Frame {ts.frame:4d} | Time: {ts.time:8.2f} ps | Charge: {total_charge:+.4f} e | Atoms: {nearby.n_atoms}\")\n", |
| 52 | + " print(f\"Frame {ts.frame:4d} | Time: {ts.time:8.2f} ps | Na+: {n_na} | Cl-: {n_cl} | Net Charge: {total_charge:+d} e\")\n", |
54 | 53 | "\n", |
55 | 54 | "except Exception as e:\n", |
56 | 55 | " print(f\"\\n\\nError: {e}\")\n", |
|
80 | 79 | "metadata": {}, |
81 | 80 | "outputs": [], |
82 | 81 | "source": [ |
83 | | - "from imdclient.IMD import IMDReader\n", |
84 | 82 | "import MDAnalysis as mda\n", |
85 | 83 | "from MDAnalysis.analysis import rms\n", |
86 | 84 | "import numpy as np\n", |
87 | 85 | "from graph_utils import live_plot\n", |
88 | 86 | "\n", |
89 | 87 | "u = mda.Universe(\"sample_simulation/GROMACS/input/start.gro\", \"imd://localhost:8889\", buffer_size=100*1024**2)\n", |
| 88 | + "# Have fixed axis for better visualization\n", |
| 89 | + "# plot[\"ax\"].set_xlim(0, 100) # Time axis from 0 to 100 ps\n", |
| 90 | + "# plot[\"ax\"].set_ylim(13, 16) # Rg axis from 13 to 16 Å\n", |
90 | 91 | "\n", |
91 | 92 | "try:\n", |
92 | 93 | " # Select backbone atoms (N, CA, C, O - the protein main chain)\n", |
|
127 | 128 | ], |
128 | 129 | "metadata": { |
129 | 130 | "kernelspec": { |
130 | | - "display_name": "Python 3", |
| 131 | + "display_name": "workshop", |
131 | 132 | "language": "python", |
132 | 133 | "name": "python3" |
133 | 134 | }, |
134 | 135 | "language_info": { |
| 136 | + "codemirror_mode": { |
| 137 | + "name": "ipython", |
| 138 | + "version": 3 |
| 139 | + }, |
| 140 | + "file_extension": ".py", |
| 141 | + "mimetype": "text/x-python", |
135 | 142 | "name": "python", |
136 | | - "version": "3.10.0" |
| 143 | + "nbconvert_exporter": "python", |
| 144 | + "pygments_lexer": "ipython3", |
| 145 | + "version": "3.14.0" |
137 | 146 | } |
138 | 147 | }, |
139 | 148 | "nbformat": 4, |
|
0 commit comments