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 :float

Charge in electron charge 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, bonds=None, 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=float)
Derived from:Atom

Array of the charges present on the atoms.

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 :np.ndarray((NBONDS,2), dtype=int)

A list containing the indices of the atoms connected by a bond. Example: [[0 1] [0 2] [3 4]]

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.


Guess the molecular bonds by using covalent radii information.


Translate the molecule to a new position r.


Return a json string representing the Molecule. This is useful for serialization.

The System class

class chemlab.core.System(molecules, 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.
box_vectors: np.ndarray((3,3), dtype=float), optional
You can specify a periodic box of another shape by giving 3 box vectors.

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.

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

Array of the charges present on the atoms.


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((NBONDS, 2), dtype=int)
Derived from:Molecule

An array of 2d indices that specify the index of the bonded atoms.

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
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.


Given the indices over atoms, return the indices over molecules. If an atom is selected, all the containing molecule is selected too.


selection: np.ndarray((N,), dtype=int) | np.ndarray((NATOMS,), dtype=book)
Either an index array or a boolean selection array over the atoms


np.ndarray((N,), dtype=int) an array of molecular indices.

classmethod empty(n_mol, n_atoms, 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,
classmethod from_json(string)

Create a System instance from a json string. Such strings are produced from the method chemlab.core.System.tojson()


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


Guess the bonds between the molecules constituent of the system.


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


indices: np.ndarray((N,), dtype=int)
Array of integers between 0 and System.n_mol


np.ndarray((N,), dtype=int) the indices of all the atoms belonging to the selected molecules.


Remove the atoms positioned at indices. The molecule containing the atom is removed as well.

If you have a system of 10 water molecules (and 30 atoms), if you remove the atoms at indices 0, 1 and 29 you will remove the first and last water molecules.


indices: np.ndarray((N,), dtype=int)
Array of integers between 0 and System.n_atoms

Remove the molecules positioned at indices.

For example, if you have a system comprised of 10 water molecules you can remove the first, fifth and nineth by using:

system.remove_molecules([0, 4, 8])


indices: np.ndarray((N,), dtype=int)
Array of integers between 0 and System.n_mol

Reorder the molecules in the system according to new_order.


new_order: np.ndarray((NMOL,), dtype=int)
An array of integers containing the new order of the system.

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


Serialize a System instance using json.

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.2)

Generate a system by merging sysa and sysb.

Overlapping molecules are removed by cutting the molecules of sysa that have atoms near the atoms of sysb. The cutoff distance is defined by the bounding parameter.


sysa: System
First system
sysb: System
Second system
bounding: float or False
Extra space used when cutting molecules in sysa to make space for sysb. If it is False, no overlap handling will be performed.

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.

chemlab.core.random_lattice_box(mol_list, mol_number, size, spacing=<Mock object at 0x25710d0>)

Make a box by placing the molecules specified in mol_list on random points of an evenly spaced lattice.

Using a lattice automatically ensures that no two molecules are overlapping.


mol_list: list of Molecule instances
A list of each kind of molecules to add to the system.
mol_number: list of int
The number of molecules to place for each kind.
size: np.ndarray((3,), float)
The box size in nm
spacing: np.ndarray((3,), float), [0.3 0.3 0.3]
The lattice spacing in nm.


A System instance.


Typical box with 1000 water molecules randomly placed in a box of size [2.0 2.0 2.0]:

from chemlab.db import ChemlabDB

# Example water molecule
water = ChemlabDB().get('molecule', 'example.water')

s = random_water_box([water], [1000], [2.0, 2.0, 2.0])

Project Versions

Table Of Contents

Previous topic

Using GROMACS with chemlab

Next topic

This Page