Topology class

class Topology

A topology contains the definition of all the atoms in the system, as well as the liaisons between the particles (bonds, angles, dihedrals, …) and the residues.

Only the atoms and the bonds are stored, the angles, dihedrals and impropers are automaticaly deduced from the bonds.

It is also possible to iterate over a Topology, yielding all the atoms in the system.

Public Functions

Topology()

Construct a new empty topology

Atom &operator[](size_t index)

Get a reference to the atom at the position index.

Parameters
  • index: the atomic index
Exceptions

const Atom &operator[](size_t index) const

Get a const reference to the atom at the position index.

Parameters
  • index: the atomic index
Exceptions

void add_atom(Atom atom)

Add an atom at the end of this topology.

Parameters
  • atom: the new atom to add

void remove(size_t i)

Delete the atom at index i in this topology, as well as all the bonds involving this atom.

This function modify the index of all the atoms after i, and modify the bond list accordingly.

Parameters
  • i: the index of the atom to remove
Exceptions

void add_bond(size_t atom_i, size_t atom_j, Bond::BondOrder bond_order = Bond::UNKNOWN)

Add a bond in the system, between the atoms at index atom_i and atom_j.

Parameters
  • atom_i: the index of the first atom in the bond
  • atom_j: the index of the second atom in the bond
  • bond_order: the bond order for the bond added
Exceptions
  • OutOfBounds: if atom_i or atom_j are greater than size()
  • Error: if atom_i == atom_j, as this is an invalid bond

void remove_bond(size_t atom_i, size_t atom_j)

Remove a bond in the system, between the atoms at index atom_i and atom_j.

If the bond does not exist, this does nothing.

Parameters
  • atom_i: the index of the first atom in the bond
  • atom_j: the index of the second atom in the bond
Exceptions

Bond::BondOrder bond_order(size_t atom_i, size_t atom_j) const

Get the bond order for the given bond

If the bond does not exist, this will thrown an Error.

Parameters
  • atom_i: the index of the first atom in the bond
  • atom_j: the index of the second atom in the bond
Exceptions
  • OutOfBounds: if atom_i or atom_j are greater than size()
  • Error: if no bond between atom_i and atom_j exists.

size_t size() const

Get the number of atoms in the topology

void resize(size_t size)

Resize the topology to hold size atoms, adding new atoms as needed.

If the new number of atoms is bigger than the old one, pre-existing atoms are conserved.

If the new size if smaller than the old one, all atoms and connectivity elements after the new size are removed.

Parameters
  • size: the new size of the topology

void reserve(size_t size)

Allocate memory in the frame to be able to store data for size atoms.

This function does not change the actual number of atoms in the topology, and should be used as an optimisation.

Parameters
  • size: the number of elements to reserve memory for

const std::vector<Bond> &bonds() const

Get the bonds in the system

The bonds are sorted according to operator<(const Bond&, const Bond&), which mean it is possible to look for a bond in the list using a binary search (std::lower_bound).

const std::vector<Bond::BondOrder> &bond_orders() const

Get the bond orders in the system.

The bond orders are sorted so that the index of each bond is the same as its index in the array returned by Topology::bonds. This means that the bond order for Topology::bonds()[index] would be given by bond_orders()[index].

const std::vector<Angle> &angles() const

Get the angles in the system

The angles are sorted according to operator<(const Angle&, const Angle&), which mean it is possible to look for an angle in the list using a binary search (std::lower_bound).

const std::vector<Dihedral> &dihedrals() const

Get the dihedral angles in the system

The dihedrals are sorted according to operator<(const Dihedral&, const Dihedral&), which mean it is possible to look for a dihedral in the list using a binary search (std::lower_bound).

const std::vector<Improper> &impropers() const

Get the improper dihedral angles in the system

The impropers are sorted according to operator<(const Improper&, const Improper&), which mean it is possible to look for an improper in the list using a binary search (std::lower_bound).

void clear_bonds()

Remove all bonding information in the topology (bonds, angles and dihedrals)

void add_residue(Residue residue)

Add a residue to this topology.

Parameters
  • residue: the residue to add to this topology
Exceptions
  • chemfiles::Error: if any atom in the residue is already in another residue in this topology. In that case, the topology is not modified.

bool are_linked(const Residue &first, const Residue &second) const

Check if two residues are linked together, i.e. if there is a bond between one atom in the first residue and one atom in the second one. Both residues should be in this topology.

The two residues are the same (first == second), this function returns true.

optional<const Residue&> residue_for_atom(size_t index) const

Get the residue containing the atom at the given index.

If no residue contains this atom, this function returns nullopt.

This function returna an chemfiles::optional value that is close to C++17 std::optional.

const Residue &residue(size_t index) const

Get the residue at the given index in this topology

There is no guarantee that this index matches the residue id.

const std::vector<Residue> &residues() const

Get all the residues in the topology as a vector

Connectivity elements

class Bond

The Bond class ensure a canonical representation of a bond two atoms.

This class implements all the comparison operators, as well as indexing.

Public Types

enum BondOrder

Stores the type of bond.

Values:

UNKNOWN = 0
SINGLE = 1
DOUBLE = 2
TRIPLE = 3
QUADRUPLE = 4
QINTUPLET = 5
AMIDE = 254
AROMATIC = 255

Public Functions

Bond(size_t i, size_t j)

Create a new Bond containing the atoms i and j.

Exceptions

size_t operator[](size_t i) const

Get the index of the ith atom (i == 0 or i == 1) in the bond.

Exceptions

class Angle

The Angle class ensure a canonical representation of an angle between three atoms.

An angle is formed by two consecutive bonds:

|  i       k  |
|    \   /    |
|      j      |

This class implements all the comparison operators, as well as indexing.

Public Functions

Angle(size_t i, size_t j, size_t k)

Create a new Angle containing the atoms i, j and k.

Exceptions
  • Error: if i == j, j == k or i == k

size_t operator[](size_t i) const

Get the index of the ith atom (i == 0, i == 1 or i == 2) in the angle.

Exceptions

class Dihedral

The Dihedral class ensure a canonical representation of a dihedral angle between four atoms.

A dihedral angle is formed by three consecutive bonds:

|  i       k     |
|    \   /   \   |
|      j      m  |

This class implements all the comparison operators, as well as indexing.

Public Functions

Dihedral(size_t i, size_t j, size_t k, size_t m)

Create a new Dihedral containing the atoms i, j, k and m.

Exceptions
  • Error: if any of i, j, k, m has the same value as another

size_t operator[](size_t i) const

Get the index of the ith atom (i can be 0, 1, 2 or 3) in the dihedral.

Exceptions

class Improper

The Improper class ensure a canonical representation of an improper dihedral angle between four atoms.

An improper dihedral angle is formed by three bonds around a central atom:

|  i       k  |
|    \   /    |
|      j      |
|      |      |
|      m      |

This class implements all the comparison operators, as well as indexing.

The second atom of the improper is always the central atom.

Public Functions

Improper(size_t i, size_t j, size_t k, size_t m)

Create a new Improper containing the atoms i, j, k and m. j must be the central atom of the improper.

Exceptions
  • Error: if any of i, j, k, m has the same value as another

size_t operator[](size_t i) const

Get the index of the ith atom (i can be 0, 1, 2 or 3) in the improper.

Exceptions