When loading and visualizing structures with ASE, atoms may sometimes appear to be outside the cell, as shown below. This article introduces methods to address this issue.
1. Use the wrap() Method of Atoms
The wrap() method rewrites atomic coordinates considering periodic boundaries. By visualizing after applying wrap(), atoms are displayed within the cell. Applying wrap() to the structure above yields the following visualization.
The code example is as follows.
from ase.io import read
from pfcc_extras.visualize.view import view_ngl
atoms = read("your_structure_file.xyz")
atoms.wrap()
view_ngl(atoms)
For data with multiple frames such as trajectory files, apply wrap() to each atoms object in a loop.
from ase.io import Trajectory
from pfcc_extras.visualize.view import view_ngl
traj = Trajectory("your_traj_file.traj")
new_traj = []
for atoms in traj:
atoms.wrap()
new_traj.append(atoms)
view_ngl(new_traj)
2. Use the wrap_molecule Function (For Molecular Systems)
The wrap() method adjusts coordinates atom-by-atom, so when dealing with molecules, some atoms within a molecule may move to the opposite side of the cell, causing the molecule to appear broken apart.
If you want to keep the molecular structure intact while fitting it inside the cell, it is convenient to use the pfcc_extras wrap_molecule function. Applying wrap_molecule yields the following visualization, allowing the molecule to fit inside the cell as much as possible without breaking its bonds.
The code example is as follows.
from ase.io import read
from pfcc_extras.visualize.view import view_ngl
from pfcc_extras.structure.molecule import wrap_molecule
atoms = read("your_structure_file.xyz")
wrap_molecule(atoms)
view_ngl(atoms)