This package contains general functions and the most basic data containers such as Atom, Molecule and System. Plus some utility functions to create and edit common Systems.

The Atom class

class chemlab.core.Atom(type, r, export=None)

Create an Atom instance. Atom is a generic container for particle data.


type: str
Atomic symbol
r: {np.ndarray [3], list [3]}
Atomic coordinates in nm
export: dict, optional
Additional export information.


>>> Atom('H', [0.0, 0.0, 0.0])

In this example we’re attaching additional data to the Atom instance. The can use this information when exporting in the gro format.

>>> Atom('H', [0.0, 0.0, 0.0], {'groname': 'HW1'})
Type :str

The atomic symbol e.g. Ar, H, O.

Type :np.ndarray(3) of floats

Atomic position in nm.

Type :float

Mass in atomic mass units.

Type :dict

Dictionary containing additional information when importing data from various formats.

See also

Type :tuple

This is a class attribute. The list of attributes that constitute the Atom. This is used to iterate over the Atom attributes at runtime.


Return a copy of the original Atom.

classmethod from_fields(**kwargs)

Create an Atom instance from a set of fields. This is a slightly faster way to initialize an Atom.


>>> Atom.from_fields(type='Ar',
                     r_array=np.array([0.0, 0.0, 0.0]),

The Molecule class

class chemlab.core.Molecule(atoms, export=None)

Molecule is a data container for a set of N Atoms.


atoms: list of Atom instances
Atoms that constitute the Molecule. Beware that the data gets copied and subsequend changes in the Atom instances will not reflect in the Molecule.
export: dict, optional
Export information for the Molecule
Type :np.ndarray((N,3), dtype=float)
Derived from:Atom

An array with the coordinates of each Atom.

type_array {numpy.array[N] of str}
Type :np.ndarray(N, dtype=str)
Derived from:Atom

An array containing the chemical symbols of the constituent atoms.

Type :np.ndarray(N, dtype=float)
Derived from:Atom

Array of masses.

Type :np.ndarray(N, dtype=object) array of dicts
Derived from:Atom

Array of Atom.export dicts.

Type :int

Number of atoms present in the molecule.

Type :dict

Export information for the whole Molecule.

Type :float

Mass of the whole molecule in amu.

Type :float
Type :float
Type :str

The brute formula of the Molecule. i.e. "H2O"


Return a copy of the molecule instance

classmethod from_arrays(**kwargs)

Create a Molecule from a set of Atom-derived arrays. Please refer to the Molecule Atom Derived Attributes. Only r_array and type_array are absolutely required, the others are optional.

>>> Molecule.from_arrays(r_array=np.array([[0.0, 0.0, 0.0],
                                           [1.0, 0.0, 0.0],
                                           [0.0, 1.0, 0.0]]),
                         type_array=np.array(['O', 'H', 'H']))

Initializing a molecule in this way can be much faster than the default initialization method.


Translate the molecule to a new position r.

The System class

class chemlab.core.System(molecules, boxsize=None, box_vectors=None)

A data structure containing information of a set of N Molecules and NA Atoms.


molecules: list of molecules
Molecules that constitute the System. The data gets copied to the System, subsequent changes to the Molecule are not reflected in the System.
boxsize: float, optional
The size of one side of a cubic box containing the system. Periodic boxes are common in molecular dynamics.
box_vectors: np.ndarray((3,3), dtype=float), optional
You can specify the periodic box of another shape by giving 3 box vectors instead.

The System class has attributes derived both from the Molecule and the Atom class.

Type :np.ndarray((NA, 3), dtype=float)
Derived from:Atom

Atomic coordinates.

Type :np.ndarray(NA, dtype=float)
Derived from:Atom

Atomic masses.

Type :np.ndarray(NA, dtype=object) array of str
Derived from:Atom

Array of all the atomic symbols. It can be used to select certain atoms in a system.


Suppose you have a box of water defined by the System s, to select all oxygen atoms you can use the numpy selection rules:

>>> oxygens = s.type_array == 'O'
# oxygens is an array of booleans of length NA where
# each True corresponds to an oxygen atom i.e:
# [True, False, False, True, False, False]

You can use the oxygen array to access other properties:

>>> o_coordinates = s.r_array[oxygens] 
>>> o_indices = np.arange(s.n_atoms)[oxygens] 
Type :np.ndarray(NA, dtype=object) array of dict
Derived from:Atom
Type :np.ndarray(N, dtype=object) array of dict
Derived from:Molecule

Export information relative to the molecule.

Type :np.ndarray((3,3), dtype=float) or None

Those are the three vectors that define of the periodic box of the system.


To define an orthorombic box of size 3, 4, 5 nm:

>>> np.array([[3.0, 0.0, 0.0],  # Vector a
              [0.0, 4.0, 0.0],  # Vector b
              [0.0, 0.0, 5.0]]) # Vector c
boxsize, optional
Type :float or None

Defines the size of the periodic box. Boxes defined with boxsize are cubic. Changes in boxsize are reflected in box.

Type :int

Number of molecules.

Type :int

Number of atoms.

Type :np.ndarray(N, dtype=int)

Gives the starting index for each molecule in the atomic arrays. For example, in a System comprised of 3 water molecules:

>>> s.mol_indices
[0, 3, 6]
>>> s.type_array[0:3]
['O', 'H', 'H']

This array is used internally to retrieve all the Molecule derived data. Do not modify unless you know what you’re doing.

Type :np.ndarray(N, dtype=int)

Contains the number of atoms present in each molecule


Add the molecule mol to a System initialized through System.empty.

classmethod empty(n_mol, n_atoms, boxsize=None, box_vectors=None)

Initialize an empty System containing n_mol Molecules and n_atoms Atoms. The molecules can be added by using the method add().


How to initialize a system of 3 water molecules:

s = System.empty(3, 9)
for i in range(3):
classmethod from_arrays(**kwargs)

Initialize a System from its constituent arrays. It is the fastest way to initialize a System, well suited for reading one or more big System from data files.


The following parameters are required:

  • r_array
  • type_array
  • mol_indices

To further speed up the initialization process you optionally pass the other derived arrays:

  • m_array
  • mol_n_atoms
  • atom_export_array
  • mol_export


Our classic example of 3 water molecules:

r_array = np.random.random((3, 9))
type_array = ['O', 'H', 'H', 'O', 'H', 'H', 'O', 'H', 'H']
mol_indices = [0, 3, 6]
System.from_arrays(r_array=r_array, type_array=type_array,

Get the Molecule instance corresponding to the molecule at index.

This method is useful to use Molecule properties that are generated each time, such as Molecule.formula and Molecule.center_of_mass


Given the indices over molecules, return the indices over atoms.


Sort the molecules in the system according to their brute formula.

Routines to manipulate Systems

chemlab.core.subsystem_from_molecules(orig, selection)

Create a system from the orig system by picking the molecules specified in selection.


orig: System
The system from where to extract the subsystem
selection: np.ndarray of int or np.ndarray(N) of bool
selection can be either a list of molecular indices to select or a boolean array whose elements are True in correspondence of the molecules to select (it is usually the result of a numpy comparison operation).


In this example we can see how to select the molecules whose center of mass that is in the region of space x > 0.1:

s = System(...) # It is a set of 10 water molecules

select = []
for i range(s.n_mol):
   if s.get_molecule(i).center_of_mass[0] > 0.1:

subs = subsystem_from_molecules(s, np.ndarray(select)) 


The API for operating on molecules is not yet fully developed. In the future there will be smarter ways to filter molecule attributes instead of looping and using System.get_molecule.

chemlab.core.subsystem_from_atoms(orig, selection)

Generate a subsystem containing the atoms specified by selection. If an atom belongs to a molecule, the whole molecule is selected.


This function can be useful when selecting a part of a system based on positions. For example, in this snippet you can see how to select the part of the system (a set of molecules) whose x coordinates is bigger than 1.0 nm:

s = System(...)
subs = subsystem_from_atoms(s.r_array[0,:] > 1.0)


orig: System
Original system.
selection: np.ndarray of int or np.ndarray(NA) of bool
A boolean array that is True when the ith atom has to be selected or a set of atomic indices to be included.


A new System instance.

chemlab.core.merge_systems(sysa, sysb, bounding=0.0)

Generate a system by overlapping sysa and sysb. Overlapping molecules are removed by cutting the molecules of sysa that are found inside the space defined by sysb.box_vectors.


sysa: System
First system
sysb: System
Second system
bounding: float
Extra space used when cutting molecules in sysa to make space for sysb.

Routines to create Systems

chemlab.core.crystal(positions, molecules, group, cellpar=[1.0, 1.0, 1.0, 90, 90, 90], repetitions=[1, 1, 1])

Build a crystal from atomic positions, space group and cell parameters.


positions: list of coordinates
A list of the atomic positions
molecules: list of Molecule
The molecules corresponding to the positions, the molecule will be translated in all the equivalent positions.
group: int | str
Space group given either as its number in International Tables or as its Hermann-Mauguin symbol.
Repetition of the unit cell in each direction
Unit cell parameters

This function was taken and adapted from the spacegroup module found in ASE.

The module spacegroup module was originally developed by Jesper Frills.

Project Versions

Table Of Contents

Previous topic

Using GROMACS with chemlab

Next topic

This Page