# 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

Vector3D()

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)

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()

Create a Matrix3D with all components equal to 0

Matrix3D(double a, double b, double c)

Create a diagonal Matrix3D with the three diagonal elements a, b and c.

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.

Matrix3D invert() const

Compute the inverse of this matrix.

Exceptions
• Error: if the matrix is not inversible

Matrix3D &operator+=(const Matrix3D &rhs)

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.

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

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.

## Warnings¶

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.

Parameters
• 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 from the file at path. If the same configuration data is already present in a previouly read configuration file, the data is replaced by the one in this file.

If the file at path can not be opened, or if the configuration file is invalid, a ConfigurationError is thrown.

Parameters
• path: path to the configuration file to add
Exceptions
• ConfigurationError: if the file at path can not be read, or if it is invalid.