Skip to content

Commit 37db397

Browse files
committed
added wrapping tutorial
1 parent 2ccaf1f commit 37db397

12 files changed

Lines changed: 12355 additions & 0 deletions
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
# Wrapping tutorial: "why is my simulation exploding?"
2+
3+
Short answer: your simulation is not exploding. What you are seeing is an
4+
artifact of how MD trajectories store coordinates. This page walks through
5+
periodic boundary conditions, why molecules appear to drift "out of the box",
6+
and how to wrap them back in for visualization.
7+
8+
## Periodic boundary conditions (PBC)
9+
10+
MD simulations are computationally expensive. Simulation time scales linearly
11+
with the number of atoms, so we keep systems small — typically a few hundred
12+
thousand atoms — for tractable performance. But a small isolated system does
13+
not reflect the reality inside cells, where molecules are surrounded by a vast
14+
amount of solvent. Periodic boundary conditions are how we approximate
15+
infinite solvent without simulating it.
16+
17+
### What does this mean?
18+
19+
When computing interactions, we pretend the simulation box has identical
20+
copies of itself on every side. Those copies in turn have copies on theirs,
21+
and so on, tiling all of space. We call these copies **periodic images** of
22+
the central box. We only ever store and integrate the central box — the
23+
images are notional, used only when computing forces across the box
24+
boundaries.
25+
26+
### Example
27+
28+
Consider a simulation box with four water molecules.
29+
30+
![box](wrapping_img/box.svg)
31+
32+
When we calculate interactions we pretend that we have copies of the system
33+
on each side. Of course we are not integrating those copies — that would be a
34+
huge waste. All copies are identical to the central box.
35+
36+
![box copies](wrapping_img/box2-2.svg)
37+
38+
In MD we use cutoffs for non-bonded interactions, which limits the distance
39+
an atom "sees". Simplifying the example by treating each water as a single
40+
point, if the orange circle is the cutoff, each water interacts with every
41+
molecule inside its circle — including atoms in neighbouring periodic
42+
images. That is how we pretend to simulate infinite solvent.
43+
44+
![cutoffs across the boundary](wrapping_img/box3-2.svg)
45+
46+
## Storing coordinates during a simulation
47+
48+
Molecules can reach the borders of the simulation box. When they do we have
49+
two options:
50+
51+
1. Teleport them to the opposite side of the box.
52+
2. Let them keep moving past the boundary; record the absolute coordinates.
53+
54+
The first approach is visually pleasant but has two downsides. You can no
55+
longer measure the total drift of the atoms, and if done carelessly it can
56+
cause precision issues in force calculations.
57+
58+
The second approach is generally preferred. The atoms' stored coordinates are
59+
not reset; interactions are still computed as if every atom were located in
60+
the central simulation box.
61+
62+
So if you see atoms "leaving" the simulation box in your trajectory, that
63+
does **not** mean they have stopped interacting with the rest of the system —
64+
they have simply moved into the next periodic image, and the engine still
65+
treats them correctly.
66+
67+
### Example
68+
69+
Consider a simulation of villin in a water box. At time zero the protein
70+
looks nicely centred in the waters because of how we built the box.
71+
72+
![villin built](wrapping_img/villin_build.png)
73+
74+
After running for a while the visualization looks like this. This is normal —
75+
we chose the second storage strategy, so molecules can move outside the
76+
original box into periodic images.
77+
78+
![villin no wrap](wrapping_img/villin_sim_nowrap.png)
79+
80+
In reality all atoms are still close to each other and interacting.
81+
82+
In some viewers (e.g. VMD) you can render the periodic images directly. If we
83+
do that for the same trajectory frame, the picture shows that the system
84+
atoms are still interacting through the periodic copies:
85+
86+
![villin periodic](wrapping_img/villin_sim_nowrap_periodics.png)
87+
88+
That's confusing to interpret with so many copies around. We'd rather have a
89+
single wrapped box.
90+
91+
## Wrapping
92+
93+
When we visualize a simulation we only see the real atoms, not the notional
94+
periodic copies. If a molecule has drifted into a neighbouring image, its
95+
interactions with the rest of the system become invisible to the eye.
96+
97+
**Wrapping** is the operation that places all atoms back into a single
98+
simulation box. Since we know the box size and the absolute position of every
99+
atom, it is mechanical to fold each atom back into the central image.
100+
101+
### How does wrapping work?
102+
103+
One question remains: where is the centre of the box? Our system drifts
104+
freely during the simulation, so we cannot use a fixed XYZ coordinate as the
105+
centre. Instead, wrapping defines the centre by **the coordinates of a
106+
chosen atom selection** — either a single atom or the average over a set of
107+
atoms.
108+
109+
### How to select a good wrapping centre
110+
111+
This depends on the system and on personal taste.
112+
113+
If the simulation contains a single protein, you usually want to wrap so the
114+
protein sits in the middle of the box. Use the average coordinate of the
115+
protein as the wrapping centre:
116+
117+
![villin centred on protein](wrapping_img/villin_wrapped_correct.png)
118+
119+
The waters now wrap nicely around the protein.
120+
121+
This is an intuitive case. You can still get it wrong, e.g. by wrapping
122+
around a single water residue instead of the protein:
123+
124+
![villin centred on bad residue](wrapping_img/villin_wrapped_bad_4.png)
125+
126+
Now the protein sticks out of one face of the box and "vacuum" appears on
127+
the others. There is no real vacuum — the piece of the protein that sticks
128+
out occupies that space in a periodic image — but moleculekit prefers to
129+
keep bonded molecules intact rather than break bonds across the boundary,
130+
so the visualization stays one-sided.
131+
132+
If the simulation has two proteins, you usually centre on one of them. The
133+
average of two proteins varies strongly during the simulation, so the choice
134+
of centre changes from frame to frame. When the proteins are in contact the
135+
average works (as below); when there are unbinding events, choose one or the
136+
other.
137+
138+
![two proteins](wrapping_img/image.png)
139+
140+
If the simulation has a protein and a membrane, the right centre depends on
141+
what you want to see. In the first image we centred on the protein — the
142+
membrane wraps to the top. In the second we centred on the membrane — the
143+
protein wraps to the bottom. In the third we picked a residue half-way
144+
between the two for a clean visualization with both whole.
145+
146+
![membrane wrapping](wrapping_img/membrane_wrapping.png)
147+
148+
## How to wrap with moleculekit
149+
150+
```python
151+
from moleculekit.molecule import Molecule
152+
153+
mol = Molecule("./structure.prmtop") # PSF is also fine
154+
mol.read("./output.xtc") # DCD is also fine
155+
mol.wrap("protein") # centre on the average protein coordinate
156+
mol.write("./output_wrapped.xtc")
157+
```
158+
159+
If your system is more complex than a single protein in solvent you may need
160+
to find a specific residue or atom to wrap around. Visualize the simulation
161+
in your viewer of choice, look around, and pick a residue that gives the
162+
cleanest picture:
163+
164+
```python
165+
mol.view()
166+
mol.wrap("resid 15 and chain A")
167+
mol.write("./output_wrapped.xtc")
168+
```
169+
170+
Wrapping only affects the visual layout of the trajectory — it is not a sign
171+
of a broken or exploding simulation. The right choice of wrapping centre
172+
depends on the system; once you find it you can apply the same selection to
173+
every trajectory of the same setup.
174+
175+
## See also
176+
177+
- [How to wrap trajectories](wrap-trajectories.md) — the focused recipe (no MD background).
178+
- [Trajectories and frames](../explanation/trajectories-and-frames.md) — how moleculekit stores coordinates, frames, and box arrays.

doc/source/howto/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ set-and-get-properties
3131
guess-bonds
3232
assign-segments-and-chains
3333
wrap-trajectories
34+
WrappingTutorial
3435
```
3536

3637
## Geometry and analysis

0 commit comments

Comments
 (0)