# Recalculate atom positions to account for periodic boundary conditions

I have the output of a simulation done using Periodic boundary conditions (PBC), however, the atom positions have been resolved, i.e. there are atoms outside the PBC-box now. Is there a software/formula that I can use to restore these positions back inside the box?

I have an .xyz file that contains the positions of the atoms.

• I assume you want to use vmd: ks.uiuc.edu/Research/vmd/plugins/pbctools There is the option to pbc set {a b c alpha betta gamma} -all and then you can use pbc wrap Dec 21 '20 at 9:49
• Depends on what you actually have, if you have a trajectory in xtc or similar formats, you could try centering the trajectory to the center of geometry/mass, even using gromacs tools for that, trjconv explicitely with the -fit rot+trans argument. That said, I feel more details are needed to answer the question for real. Dec 22 '20 at 1:07
• @IvanP I have added the file format as part of the question Dec 22 '20 at 19:10
• @Fabian your solution looks good. is there a way to specify a different pbc box for each config and get the position of these "wrapped" atoms in a file using vmd itself ? Dec 22 '20 at 19:23
• @fireball.1 You can specify a different box for each frame using the -frame option to pbc set. VMD cannot write the coordinates back to an xyz file, but other formats are supported. I would only recommend VMD for visualization of the trajectory though. What is your goal? I only mentioned VMD because you tagged it, so I assumed you wanted to visualize and not process the coordinates. Dec 23 '20 at 16:09

## 2 Answers

To visualize the trajectory you can use VMD and use its pbctools

vmd trajectory.xyz
> pbc set {a b c alpha betta gamma} -all
> pbc wrap


where a, b, and c are the lattice vectors and alpha, beta, and gamma the angles. The angles can be omitted if the box is orthorhombic. For more information see here: https://www.ks.uiuc.edu/Research/vmd/plugins/pbctools

To process the trajectory I'd recommend using one of the available python packages. Here is a code snippet using the Atomic Simulation Environment (ASE):

from ase import io
xyz=io.read('trajectory.xyz',index=":")
for frame in xyz:
frame.set_cell([a,b,c],[alpha,beta,gamma])
frame.set_pbc([True,True,True])
frame.wrap()
io.xyz.write_xyz('trajectory_wrapped.xyz',xyz)


Because the xyz-file contains no information about the cell you have to supply it separately. You can also use ASE to further process the coordinates if you wish. The documentation is here: https://wiki.fysik.dtu.dk/ase/

• Very helpful indeed! +1. Please take a look at my edit, in case you didn't know that text can be stylized as code in-line as well as in the "code blocks" that you wrote. Dec 23 '20 at 20:21

You can use ATOMSK.

The command:
atomsk initial.xyz -wrap final.xyz
will wrap all the atom coordinates back into your simulation box.
initial.xyz is your input file and final.xyz is the output after wrapping all atoms back into the simulation box.

You can get the output in many different formats. ATOMSK is quite powerful and simple to use. Please have a look at its documentation.