# Trajectory class¶

class chemfiles::Trajectory

A Trajectory is a chemistry file on the hard drive. It is the entry point of the chemfiles library.

Public Functions

chemfiles::TrajectoryTrajectory(std::string path, char mode = 'r', const std::string &format = "")

Open a file, automatically gessing the file format from the extension.

The format can either be guessed from the extention (“.xyz” is XYZ, “.gro” is GROMACS, etc.), or specified as the third parameter. The format names are given in the corresponding documentation section

// Simplest case: open a file for reading it, the format is guessed from
// the extension.
auto water = Trajectory("water.nc");

// Open a file in a specific mode
auto copper = Trajectory("copper.xyz", 'w');

// Specify the file format to use
auto nanotube = Trajectory("nanotube.lmp", 'r', "LAMMPS Data");


Parameters
• path: The file path. In w or a modes, the file is created if it does not exist yet. In r mode, an exception is thrown is the file does not exist yet.
• mode: Opening mode for the file. Default mode is r for read. Other supported modes depends on the underlying format and are w for write, and a for append. w mode discard any previously existing file.
• format: Specific format to use. Needed when there is no way to guess the format from the extension of the file, or when this guess would be wrong.
Exceptions
• FileError: for all errors concerning the physical file: can not open it, can not read/write it, etc.
• FormatError: if the file is not valid for the used format.

Frame chemfiles::Trajectoryread()

Read the next frame in the trajectory.

The trajectory must have been opened in read or append mode, and the underlying format must support reading.

This function throws a FileError if there are no more frames to read in the trajectory.

auto trajectory = Trajectory("water.nc");

// Use the frame for awesome science here!

// This is one way to iterate over all the frames in a trajectory
while (!trajectory.done()) {
// ...
}


Exceptions
• FileError: for all errors concerning the physical file: can not open it, can not read/write it, etc.
• FormatError: if the file is not valid for the used format, or if the format does not support reading.

Frame chemfiles::Trajectoryread_step(size_t step)

Read a single frame at specified step from the trajectory.

The trajectory must have been opened in read or append mode, and the underlying format must support reading.

This function throws a FileError if the step is bigger than the number of steps in the trajectory.

auto trajectory = Trajectory("water.nc");

// Use the frame for awesome science here!

// This is one way to iterate over all the frames in a trajectory
for (size_t i = 0; i < trajectory.nsteps(); i++) {
// ...
}


Parameters
• step: step to read from the trajectory
Exceptions
• FileError: for all errors concerning the physical file: can not open it, can not read/write it, etc.
• FormatError: if the file is not valid for the used format, or if the format does not support reading.

void chemfiles::Trajectorywrite(const Frame &frame)

Write a single frame to the trajectory.

The trajectory must have been opened in Write or append mode, and the underlying format must support reading.

auto trajectory = Trajectory("water.nc", 'w');

auto frame = Frame();

trajectory.write(frame);


Parameters
• frame: frame to write to this trajectory
Exceptions
• FileError: for all errors concerning the physical file: can not open it, can not read/write it, etc.
• FormatError: if the format does not support writing.

void chemfiles::Trajectoryset_topology(const Topology &topology)

Use the given topology instead of any pre-existing Topology when reading or writing.

This replace any topology in the file being read, or in the Frame being written.

This is mainly usefull when a format does not define topological information, as it can be the case with some molecular dynamic formats.

// A water topology
auto topology = Topology();

auto trajectory = Trajectory("water.nc");
trajectory.set_topology(topology);

// The topology of the frame is now a water molecule, regardless of
// what was defined in the trajectory.

// One can also set the topology from a file
trajectory.set_topology("water.pdb");

// Or set it from a file with a specific format
trajectory.set_topology("water.mol", "XYZ");


Parameters
• topology: the topology to use with this frame
Exceptions
• Error: if the topology does not contain the right number of atoms at any step.

void chemfiles::Trajectoryset_topology(const std::string &filename, const std::string &format = "")

Use the Topology from the first Frame of the Trajectory at filename instead any pre-existing Topology when reading or writing.

This replace any topology in the file being read, or in the Frame being written.

This is mainly usefull when a format does not define topological information, as it can be the case with some molecular dynamic formats.

// A water topology
auto topology = Topology();

auto trajectory = Trajectory("water.nc");
trajectory.set_topology(topology);

// The topology of the frame is now a water molecule, regardless of
// what was defined in the trajectory.

// One can also set the topology from a file
trajectory.set_topology("water.pdb");

// Or set it from a file with a specific format
trajectory.set_topology("water.mol", "XYZ");


Parameters
• filename: trajectory file path.
• format: Specific format to use. Needed when there is no way to guess the format from the extension of the file, or when this guess would be wrong.
Exceptions
• FileError: for all errors concerning the physical file: can not open it, can not read it, etc.
• FormatError: if the file is not valid for the used format.
• Error: if the topology does not contain the right number of atoms at any step.

void chemfiles::Trajectoryset_cell(const UnitCell &cell)

Use the given cell instead of any pre-existing UnitCell when reading or writing.

This replace any unit cell in the file being read, or in the Frame being written.

This is mainly usefull when a format does not define unti cell information.

auto trajectory = Trajectory("water.xyz");
trajectory.set_cell(UnitCell(11, 11, 22));

// The frame cell is now an orthorhombic cell with lengths of
// 11 A, 11 A and 22 A, regardless of what was defined in the file.

Parameters
• cell: the unit cell to use with this frame

size_t chemfiles::Trajectorynsteps() const

Get the number of steps (the number of frames) in this trajectory.

auto trajectory = Trajectory("water.nc");

auto nsteps = trajectory.nsteps();
for (size_t i = 0; i < nsteps; i++) {
}


bool chemfiles::Trajectorydone() const

Check if all the frames in this trajectory have been read, i.e. if the last read frame is the last frame of the trajectory.

auto trajectory = Trajectory("water.nc");

while (!trajectory.done()) {
}

/// When using read_step, done() return a value based on the last read frame
assert(!trajectory.done());

auto nsteps = trajectory.nsteps();
assert(trajectory.done());


void chemfiles::Trajectoryclose()

Close a trajectory, and synchronize all buffered content with the drive.

Calling any function on a closed trajectory will throw a FileError.

auto trajectory = Trajectory("water.nc");

auto frame = Frame();
// setup the frame

trajectory.write(frame);
trajectory.close();