Miscelaneous classes and functions

Basic types

class Vector3D : std::array<double, 3>

3D vector for basic data storage in chemfiles.

This type defines the following operators, with the usual meaning:

  • Comparison operators: == and != performs strict float equality comparison;
  • Mathematical operators: + and - for addition and substraction of vectors, * and / for multiplication and division by double values. Multiplication with * is also defined for multiplication by a Matrix3D.

Public Functions


Create a Vector3D with all components equal to 0.

Vector3D(double x, double y, double z)

Create a Vector3D from the three components x, y, and z.

double norm() const

Compute the euclidean norm of this Vector3D.

Vector3D &operator+=(const Vector3D &rhs)

Compound addition of two vectors.

Vector3D &operator-=(const Vector3D &rhs)

Compound subtraction of two vectors.

Vector3D &operator*=(double rhs)

Compound multiplication of a vector and a scalar.

Vector3D &operator/=(double rhs)

Compound division of a vector by a scalar.

double chemfiles::dot(const Vector3D &lhs, const Vector3D &rhs)

Compute the dot product of the vectors lhs and rhs.

Vector3D chemfiles::cross(const Vector3D &lhs, const Vector3D &rhs)

Compute the cross product of the vectors lhs and rhs.

class Matrix3D : std::array<std::array<double, 3>, 3>

A 3x3 matrix class.

This type defines the following operators, with the usual meaning:

  • Comparison operators: == and != performs strict float equality comparison;
  • Mathematical operators: only mulitplication between matrixes and between matrix and vector are defined, using the * operator.

Public Functions

Matrix3D(double m11, double m12, double m13, double m21, double m22, double m23, double m31, double m32, double m33)

Create a Matrix3D by explicitly specifying all the m_ij components of the matrix.

double determinant() const

Compute the determinant of this matrix.

Matrix3D invert() const

Compute the inverse of this matrix.

  • Error: if the matrix is not inversible (i.e. if the determinant is zero)

Matrix3D transpose() const

Transpose the matrix.

Matrix3D &operator+=(const Matrix3D &rhs)

Compound addition of two matrices.

Matrix3D &operator-=(const Matrix3D &rhs)

Compound subtraction of two matrices.

Matrix3D &operator*=(double rhs)

Compound multiplication of a matrix and a scalar.

Matrix3D &operator/=(double rhs)

Compound division of a matrix by a scalar.

Public Static Functions

static Matrix3D zero()

Create a Matrix3D with all components set to zero.

static Matrix3D unit()

Create an unit Matrix3D (a diagonal matrix with all diagonal values set to 1).

Errors handling

In chemfiles, any error will throw an exception. Any program using chemfiles should thus wrap any call in a try ... catch block, and handle the errors in any pertinent way.

#include <iostream>
#include <chemfiles.hpp>

int main() {
    try {
        chemfiles::Trajectory file("filename.xyz");
        auto frame = file.read();
        auto positions = frame.positions();
        // Do something here
    } catch (const chemfiles::Error& e) {
        // Basic error handling logging error to stdout
        std::cout << "Error in chemfiles:" << e.what() << std::endl;
        return -1;

    return 0;

Any exceptions thown by chemfiles will derive from chemfiles::Error. Catching chemfiles::Error will then catch any exception thown by chemfiles. You also can catch any other error if you need finer grain control. chemfiles::Error derives from std::runtime_error, so it should play nicely with any exisiting C++ error handling.

struct Error : public std::runtime_error

Base exception class for chemfiles library.

Subclassed by chemfiles::ConfigurationError, chemfiles::FileError, chemfiles::FormatError, chemfiles::MemoryError, chemfiles::OutOfBounds, chemfiles::PropertyError, chemfiles::SelectionError

struct FileError : public chemfiles::Error

Exception for files related failures.

struct MemoryError : public chemfiles::Error

Exception for memory related failures.

struct FormatError : public chemfiles::Error

Exception for formats related failures.

struct SelectionError : public chemfiles::Error

Exception for errors in selections.

struct ConfigurationError : public chemfiles::Error

Exception for errors in configuration files.

struct OutOfBounds : public chemfiles::Error

Exception for out of bounds error when accessing atoms or residues.

struct PropertyError : public chemfiles::Error

Exception for errors related to frame and atomic properties.


Chemfiles send warnings when it encounters malformed files, or any other condition that the user might want to know about. By default, these warnings are printed to the standard error stream. chemfiles::set_warning_callback() allow to redirect these warning by giving it a callback function to be called on each warning event.

void chemfiles::set_warning_callback(warning_callback callback)

Set the global callback for warning events. The default is to print them on the standard error stream.

  • callback: callback function that will be called on each warning

typedef std::function<void(const std::string &message)> chemfiles::warning_callback

Callback type used to process a warning event.

Configuration files

You can add more configuration file to chemfiles with chemfiles::add_configuration().

void chemfiles::add_configuration(const std::string &path)

Read configuration data from the file at path.

By default, chemfiles reads configuration from any file named .chemfiles.toml or chemfiles.toml in the current directory or any parent directory. This function can be used to add data from another configuration file.

This function will throw a ConfigurationError if there is no file at path, or if the file is incorectly formatted. Data from the new configuration file will overwrite any existing data.

  • path: path to the configuration file to add