Skip to content

Commit d953ab2

Browse files
committed
Exercise 1: Fix charge calculation distance
1 parent b2137a5 commit d953ab2

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

workshop/solutions.ipynb

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"\n",
1818
"## Exercise 1 Solution: Charge Density Analysis\n",
1919
"\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."
2121
]
2222
},
2323
{
@@ -26,31 +26,30 @@
2626
"metadata": {},
2727
"outputs": [],
2828
"source": [
29-
"from imdclient.IMD import IMDReader\n",
3029
"import MDAnalysis as mda\n",
3130
"import numpy as np\n",
3231
"from graph_utils import live_plot\n",
3332
"\n",
3433
"u = mda.Universe(\"sample_simulation/GROMACS/input/start.gro\", \"imd://localhost:8889\", buffer_size=100*1024**2)\n",
3534
"\n",
3635
"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",
3939
"\n",
4040
" # Create live plot\n",
4141
" plot = live_plot(\n",
42-
" title=\"Charge Density within 4 Å\",\n",
42+
" title=\"Net Ion Charge within 8 Å\",\n",
4343
" ylabel=\"Total Charge (e)\",\n",
4444
" update_interval=1\n",
4545
" )\n",
4646
"\n",
4747
" 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",
5051
" 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",
5453
"\n",
5554
"except Exception as e:\n",
5655
" print(f\"\\n\\nError: {e}\")\n",
@@ -80,13 +79,15 @@
8079
"metadata": {},
8180
"outputs": [],
8281
"source": [
83-
"from imdclient.IMD import IMDReader\n",
8482
"import MDAnalysis as mda\n",
8583
"from MDAnalysis.analysis import rms\n",
8684
"import numpy as np\n",
8785
"from graph_utils import live_plot\n",
8886
"\n",
8987
"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",
9091
"\n",
9192
"try:\n",
9293
" # Select backbone atoms (N, CA, C, O - the protein main chain)\n",
@@ -127,13 +128,21 @@
127128
],
128129
"metadata": {
129130
"kernelspec": {
130-
"display_name": "Python 3",
131+
"display_name": "workshop",
131132
"language": "python",
132133
"name": "python3"
133134
},
134135
"language_info": {
136+
"codemirror_mode": {
137+
"name": "ipython",
138+
"version": 3
139+
},
140+
"file_extension": ".py",
141+
"mimetype": "text/x-python",
135142
"name": "python",
136-
"version": "3.10.0"
143+
"nbconvert_exporter": "python",
144+
"pygments_lexer": "ipython3",
145+
"version": "3.14.0"
137146
}
138147
},
139148
"nbformat": 4,

workshop/workshop.ipynb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@
468468
"\n",
469469
"## 🎯 Exercise 1: Charge Density Analysis\n",
470470
"\n",
471-
"**Your Task**: Calculate the **total charge within 4 Å of the protein** at each timestep.\n",
471+
"**Your Task**: Calculate the **total charge within 8 Å of the protein** at each timestep.\n",
472472
"\n",
473473
"This is an important property for understanding:\n",
474474
"- Electrostatic environment around the protein\n",
@@ -482,7 +482,7 @@
482482
"solvent = u.select_atoms('not protein')\n",
483483
"```\n",
484484
"\n",
485-
"**Distance-based selection** (solvent within 4 Å of protein):\n",
485+
"**Distance-based selection** (solvent within 8 Å of protein):\n",
486486
"```python\n",
487487
"# Use updating=True to recalculate selection each frame!\n",
488488
"nearby = u.select_atoms('not protein and around 4 protein', updating=True)\n",
@@ -525,10 +525,10 @@
525525
" ## Your code here!\n",
526526
" ## \n",
527527
" ## Steps:\n",
528-
" ## 1. Create selection for nearby solvent: nearby = u.select_atoms('not protein and around 4 protein', updating=True)\n",
529-
" ## 2. Create live plot: plot = live_plot(\"Charge Density within 4 Å\", ylabel=\"Total Charge (e)\")\n",
528+
" ## 1. Create selection for nearby ions: na_sel = u.select_atoms('resname SOD and around 8 protein', updating=True) and similary for 'CLA' ions\n",
529+
" ## 2. Create live plot: plot = live_plot(\"Charge Density within 8 Å\", ylabel=\"Total Charge (e)\")\n",
530530
" ## 3. Loop through frames\n",
531-
" ## 4. Calculate total_charge = np.sum(nearby.charges)\n",
531+
" ## 4. Calculate total_charge = na_sel - cl_sel\n",
532532
" ## 5. Update plot: plot['update'](ts.time, total_charge)\n",
533533
" \n",
534534
" pass # Remove this and add your code\n",

0 commit comments

Comments
 (0)