Tim Green / MagresPython

# Selecting atoms

The most basic operations in the MagresPython library are to select groups of atoms in a molecule or crystal. This tutorial will go through a few common operations.

First, we import the MagresAtoms class from the library. This is a container for a bunch of atoms.

from magres.atoms import MagresAtoms


Then, using the MagresAtoms class, we load and parse an output file.

atoms = MagresAtoms.load_magres('../samples/ethanol-all.magres')


atoms is now a collection containing all the atoms in the system

atoms

<magres.atom.MagresAtoms - 9 atoms>

MagresPython has been designed to be pythonic, meaning that it behaves as similarly to native Python as possible. MagresAtoms behaves like a list, you can use standard builtin functions like len on it, which returns the number of atoms

len(atoms)

9

And, like a list, you can iterate over all the atoms

for atom in atoms:
print atom

1H1
1H2
1H3
1H4
1H5
1H6
13C1
13C2
17O1


There are shortcuts for selecting a particular atom of a particular species. The following code picks out the first carbon atom

atoms.C1

<magres.atom.MagresAtom - 13C1>

Atoms are objects and have properties, such as position, which is returned as a numpy array

print atoms.C1.position

[-0.004 -0.004 -0.004]


You can select subsets of atoms, which also behave like lists. For example, to select all hydrogen atoms in the system

atoms.species("H")

<magres.atom.MagresAtomsView - 6 atoms>

You can select all atoms within a certain distance of each other. The following code selects all atoms within 2 Angstrom of the H1 atom.

atoms.within(atoms.H1, 2.0)

<magres.atom.MagresAtomsView - 4 atoms>