The Atoms Object

Overview

Teaching: 15 min
Exercises: 5 min
Questions
  • How can I describe a molecule or crystal using the Atoms class?

  • How can I access and adjust Atoms information?

  • What built-in help is available from ASE?

Objectives
  • Create and visualise an Atoms object

  • Return simple structural information from an Atoms object

  • Adjust the default properties for an Atoms object

  • Access built-in help using ? and tab-completion

Code connection

In this episode we explore the Atoms class, which contains methods for getting and setting simple properties for a molecule or material.

Molecules and materials are represented by the Atoms class

from ase import Atoms

d = 1.10
molecule = Atoms(['N', 'N'], positions=[(0., 0., 0.), (0., 0., d)])
molecule = Atoms('N2', positions=[(0., 0., 0.), (0., 0., d)])

ase.visualize.view and nglview can be used to visualise an Atoms object

from ase.visualize import view
view(molecule, viewer='ngl')

image of molecule in notebook

To describe crystals we use the cell and pbc keywords

Shortcuts

We have defined a Python function show() which will show enlarged atoms and a unit cell with nglview. This will save us from writing four lines of code every time we want to display a crystal.

a = 5.387
crystal = Atoms('Zn4S4',
                scaled_positions=[[0., 0., 0.],
                           [0., 0.5, 0.5],
                           [0.5, 0., 0.5],
                           [0.5, 0.5, 0.],
                           [0.25, 0.75, 0.75],
                           [0.25, 0.25, 0.25],
                           [0.75, 0.75, 0.25],
                           [0.75, 0.25, 0.75]],
               cell=[a, a, a],
               pbc=True)

view(crystal, viewer='ngl')

image of Zn4S4 with unit cell

To access information from Atoms we use “getter” methods

print("N2 positions")
print(molecule.get_positions(), end="\n\n")

print("N2 symbols")
print(molecule.get_chemical_symbols(), end="\n\n")
N2 positions
[[0.  0.  0. ]
 [0.  0.  1.1]]

N2 symbols
['N', 'N']
print("N2 masses")
print(molecule.get_masses(), end="\n\n")

print("N2 center of mass")
print(molecule.get_center_of_mass())
N2 masses
[14.007 14.007]

N2 center of mass
[0.   0.   0.55]

We can override default keyword values when we create an Atoms object

d = 1.10
isotope = Atoms('N2',
                positions=[(0., 0., 0.), (0., 0., d)],
                masses=[13.006, 14.003])

print("13N-14N masses")
print(isotope.get_masses(), end="\n\n")

print("13N-14N center of mass")
print(isotope.get_center_of_mass())
13N-14N masses
[13.006 14.003]

13N-14N center of mass
[0.         0.         0.57030249]

To adjust information in Atoms we use “setter” methods

isotope = molecule.copy()
print("Center of mass before modifying masses:")
print(isotope.get_center_of_mass(), end='\n\n')

isotope.set_masses([13.006, 14.003])
print("Center of mass after modifying masses:")
print(isotope.get_center_of_mass())
Center of mass before modifying masses:
[0.   0.   0.55]

Center of mass after modifying masses:
[0.         0.         0.57030249]

Accessing help

In a jupyter notebook or IPython terminal we can get the “docstring” of a method or function by adding ? to the name

isotope.get_center_of_mass?
Signature: isotope.get_center_of_mass(scaled=False)
Docstring:
Get the center of mass.

If scaled=True the center of mass in scaled coordinates
is returned.
File:      ~/src/ase/ase/atoms.py
Type:      method

We can also get access to the available methods and properties with tab-completion. In a Jupyter notebook or IPython terminal, try:

crystal.[TAB]

where [TAB] means “hit the TAB key”. You should see that the Atoms object has a lot of features available; however not all of them will work until we start using the Calculator class.

Exercise: Atom features

Use tab-completion and docstrings to explore the features of Atoms. In the ZnS structure, find the distance between the first Zn atom and the four S atoms. Are some of them nearer than others?

Hint: you may need to use the “minimum image convention”

Key Points

  • Molecules and materials are represented by the Atoms class

  • ase.visualize.view and nglview can be used to visualise an Atoms object

  • To describe crystals we use the cell and pbc keywords

  • To access information from Atoms we use “getter” methods

  • We can override default keyword values when we create an Atoms object

  • To adjust information in Atoms we use “setter” methods