Miscelaneous¶
Basic types¶
-
class Vector3D : private 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 aMatrix3D
.
-
inline double chemfiles::dot(const Vector3D &lhs, const Vector3D &rhs)¶
Compute the dot product of the vectors
lhs
andrhs
.
-
inline Vector3D chemfiles::cross(const Vector3D &lhs, const Vector3D &rhs)¶
Compute the cross product of the vectors
lhs
andrhs
.
-
class Matrix3D : private 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 multiplication between matrixes and between matrix and vector are defined, using the
*
operator.
Public Functions
-
inline 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 them_ij
components of the matrix.
-
inline double determinant() const¶
Compute the determinant of this matrix.
Format list and Metadata¶
-
std::string chemfiles::guess_format(std::string path, char mode = 'r')¶
Get the format that chemfiles would use to read a file at the given path.
Most of the time, the format is only guessed from the filename extension, without reading the file to guess the format. When two or more format can share the same extension (for example CIF and mmCIF), chemfiles tries to read the file to distinguish between them. If reading fails, the default format for this extension is returned.
Opening the file using the returned format string might still fail. For example, it will fail if the file is not actually formatted according to the guessed format; or the format/compression combination is not supported (e.g.
XTC / GZ
will not work since the XTC reader does not support compressed files).The format is represented in a way compatible with the various
Trajectory
constructors, i.e."<format name> [/ <compression>]"
, where compression is optional.- Parameters:
path – path of the file we are trying to read
mode – the opening mode of the file, can be ‘r’, ‘a’ or ‘w’
- Throws:
FormatError – if no format matching this filename is found.
- Returns:
guessed format of the file
-
std::vector<std::reference_wrapper<const FormatMetadata>> chemfiles::formats_list()¶
Get the list of formats chemfiles knows about, and all associated metadata
-
class FormatMetadata¶
Metadata associated with a format.
Public Members
-
const char *name = ""¶
Name of the format.
-
const char *description = ""¶
User facing format description.
-
const char *reference = ""¶
URL pointing to the format definition/reference.
-
bool read = false¶
Is reading files in this format implemented?
-
bool write = false¶
Is writing files in this format implemented?
-
bool memory = false¶
Does this format support in-memory IO?
-
bool positions = false¶
Does this format support storing atomic positions?
-
bool velocities = false¶
Does this format support storing atomic velocities?
-
bool unit_cell = false¶
Does this format support storing unit cell information?
-
bool atoms = false¶
Does this format support storing atom names or types?
-
bool bonds = false¶
Does this format support storing bonds between atoms?
-
bool residues = false¶
Does this format support storing residues?
-
const char *name = ""¶
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 thrown by chemfiles will derive from chemfiles::Error
.
Catching chemfiles::Error
will then catch any exception thrown 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 existing 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
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_t 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_t¶
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
orchemfiles.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 atpath
, or if the file is incorrectly formatted. Data from the new configuration file will overwrite any existing data.- Parameters:
path – path to the configuration file to add
- Throws:
ConfigurationError – if the file at
path
can not be read, or if it is invalid.