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

```// equality and inequality
assert(Vector3D() == Vector3D(0, 0, 0));
assert(Vector3D(1, 2, 3) != Vector3D(0, 0, 0));

// Indexing
auto u = Vector3D(1.5, 2.0, -3.0);
assert(u == 1.5);
assert(u == 2.0);
assert(u == -3.0);

// sum and difference
auto sum = Vector3D(1, 0, 1) + Vector3D(0, 2, 1);
assert(sum == Vector3D(1, 2, 2));

auto diff = Vector3D(1, 0, 1) - Vector3D(0, 2, 1);
assert(diff == Vector3D(1, -2, 0));

// product and division by a scalar
auto rhs = Vector3D(1, 0, 1) * 5;
assert(rhs == Vector3D(5, 0, 5));

auto lhs = 5 * Vector3D(1, 0, 1);
assert(lhs == Vector3D(5, 0, 5));

auto div = Vector3D(2, 0, 3) / 3;
assert(div == Vector3D(2.0/3, 0, 1));
```

Public Functions

`Vector3D`()

Create a Vector3D with all components equal to 0.

```auto v = Vector3D();
assert(v == 0.0);
assert(v == 0.0);
assert(v == 0.0);
```

`Vector3D`(double x, double y, double z)

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

```auto u = Vector3D(1.5, 2.0, -3.0);
assert(u == 1.5);
assert(u == 2.0);
assert(u == -3.0);
```

double `norm`() const

Compute the euclidean norm of this Vector3D.

```auto v = Vector3D(1.0, 1.0, 1.0);
assert(v.norm() == sqrt(3));
```

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

```auto u = Vector3D(0, 1, 2);
auto v = Vector3D(1, 0, 2);

assert(dot(u, v) == 4);
```

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

Compute the cross product of the vectors `lhs` and `rhs`.

```auto u = Vector3D(0, 1, 2);
auto v = Vector3D(1, 0, 2);

assert(cross(u, v) == Vector3D(2, 2, -1));
```

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.

```// equality and inequality
assert(Matrix3D::zero() == Matrix3D(0, 0, 0, 0, 0, 0, 0, 0, 0));
assert(Matrix3D::unit() != Matrix3D::zero());

// Indexing
auto M = Matrix3D(
11, 12, 13,
21, 22, 23,
31, 32, 33
);
assert(M == 11);
assert(M == 12);
assert(M == 13);

assert(M == 21);
assert(M == 22);
assert(M == 23);

assert(M == 31);
assert(M == 32);
assert(M == 33);

// Matrix-vector multiplication
auto mat_vec = Matrix3D::unit() * Vector3D(1, 3, 2);
assert(mat_vec == Vector3D(1, 3, 2));

// Matrix-Matrix multiplication
auto A = Matrix3D(
0.5, 0, 0,
0, 2, 0,
0, 0, 1
);
auto mat_mat = Matrix3D::unit() * A;
assert(mat_mat == Matrix3D(0.5, 0, 0, 0, 2, 0, 0, 0, 1));

// Scalar-Matrix multiplication
assert(2 * A == Matrix3D(1.0, 0, 0, 0, 4, 0, 0, 0, 2));
assert(A * 2 == Matrix3D(1.0, 0, 0, 0, 4, 0, 0, 0, 2));
```

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.

```auto A = Matrix3D(
11, 12, 13,
21, 22, 23,
31, 32, 33
);
assert(A == 11);
assert(A == 12);
assert(A == 13);

assert(A == 21);
assert(A == 22);
assert(A == 23);

assert(A == 31);
assert(A == 32);
assert(A == 33);
```

double `determinant`() const

Compute the determinant of this matrix.

```auto A = Matrix3D(
3, 0, 0,
0, 2, 0,
0, 0, 1
);
assert(A.determinant() == 6);
```

Matrix3D `invert`() const

Compute the inverse of this matrix.

Exceptions
• `Error`: if the matrix is not inversible (i.e. if the determinant is zero)
```auto A = Matrix3D(
3, 0, 0,
0, 2, 0,
0, 0, 1
);
auto inverse = Matrix3D(
1.0/3.0,    0,    0,
0,     1.0/2.0, 0,
0,       0,     1.0);
assert(A.invert() == inverse);
```

Matrix3D `transpose`() const

Transpose the matrix.

```auto A = Matrix3D(
3, 0, 5,
1, 2, 6,
2, 0, 1
);
auto transposed = Matrix3D(
3, 1, 2,
0, 2, 0,
5, 6, 1
);
assert(A.transpose() == transposed);
```

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.

Public Static Functions

static Matrix3D `zero`()

Create a `Matrix3D` with all components set to zero.

```auto C = Matrix3D::zero();
assert(C == 0);
assert(C == 0);
assert(C == 0);

assert(C == 0);
assert(C == 0);
assert(C == 0);

assert(C == 0);
assert(C == 0);
assert(C == 0);
```

static Matrix3D `unit`()

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

```auto B = Matrix3D::unit();
assert(B == 1);
assert(B == 0);
assert(B == 0);

assert(B == 0);
assert(B == 1);
assert(B == 0);

assert(B == 0);
assert(B == 0);
assert(B == 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 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.

```// a function to do some work with the warnings
void work_with_warning(const std::string&);

/// Use a function as warning callback
chemfiles::set_warning_callback(work_with_warning);

/// Use a lambda as warning callback
chemfiles::set_warning_callback([](const std::string& message){
std::cout << "got a warning from chemfiles: " << message << std::endl;
});
```

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

```chemfiles::add_configuration("local-file.toml");

// Reading a trajectory will now use data from local-file.toml
```

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.