maxwelllink.tools.recorders module

Run-time output of the molecular drivers, one file per driver process: per-molecule properties to HDF5 or NPZ, and positions and/or velocities to extended XYZ.

class maxwelllink.tools.recorders.PropertyRecorder[source]

Bases: object

Per-molecule scalars of a run, streamed to an HDF5 file or buffered for an NPZ.

Every record_every_steps steps record() takes one value per system and per name. HDF5 files grow as the run goes (each record is written and flushed at once, so a run that stops early still leaves a readable file); NPZ files are held in memory and written by close().

Parameters:
  • path (str) – Output file, HDF5 unless it ends in .npz or h5py is not installed, in which case an .npz file of that name is written instead. Missing folders are created.

  • names (sequence of str) – The recorded quantities; each becomes a dataset of shape (n_records, num).

  • molecule_ids (sequence of int) – The systems in column order, stored as molecule_ids.

  • dt_au (float) – The time step, stored as dt_au.

  • record_every_steps (int, default: 1) – Record every this many steps.

  • record_max_steps (int, optional) – Stop recording after this many records; None for no cap.

  • append (bool, default: False) – Continue an existing file of the same layout (a restart) instead of overwriting it.

__init__(path, names, molecule_ids, dt_au, record_every_steps=1, record_max_steps=None, append=False)[source]
close()[source]

Close the HDF5 file, or write the buffered NPZ file. Safe to call twice.

record(step_index, time_au, values)[source]

Take one record if step_index falls on the recording stride.

Parameters:
  • step_index (int) – Number of production steps taken so far, counting from one.

  • time_au (float) – The time after that step, in atomic units.

  • values (array-like of float, shape (num, len(names))) – One value per system and per name, in the order of names.

class maxwelllink.tools.recorders.XYZTrajectoryWriter[source]

Bases: object

Positions, atomic velocities, and/or body-frame angular velocities with principal moments of inertia streamed to one extended-XYZ file, with one frame per independent system. Each system may contain multiple physical molecules.

Every record_every_steps steps write() appends one frame per system, in molecule_ids order. The file is time-major: with num systems, system k occupies every num-th frame starting at frame k.

For files containing positions, those frames can be read using ase.io.read(path, index=slice(k, None, num)) or maxwelllink.tools.read_xyz_trajectory(). The latter returns positions only, including for traj_content="all", and must not be used for files containing only velocities or angular velocities.

The comment line carries time_au, step and molecule_id. Extra per-atom scalar columns (e.g. Mulliken charges) are declared in per_atom; all selected columns appear in the extended-XYZ Properties string. Positions are written in Angstrom; atomic velocities are in Bohr per atomic time. Body-frame angular velocities are in inverse atomic time and always accompanied by the corresponding principal moments of inertia in electron-mass times Bohr-squared units. The angular block has six columns: three angular velocities followed by three moments, repeated for every atom of the molecule. They are declared as angular_velocity_body_au:R:3 and principal_moments_au:R:3 in Properties.

Parameters:
  • path (str) – Output file; missing folders are created.

  • symbols (sequence of str) – Element symbol of every atom, in the order used by the selected vectors.

  • molecule_ids (sequence of int) – The independent system IDs, in frame order.

  • dt_au (float) – The time step in atomic units, written into every frame’s comment line.

  • record_every_steps (int, default: 1) – Write every this many steps.

  • record_max_steps (int, optional) – Stop after this many records; None for no cap.

  • append (bool, default: False) – Continue an existing file (a restart) instead of overwriting it. The existing file must use the same traj_content and per_atom layout; its columns are not validated before appending.

  • per_atom (sequence of str, default: ()) – Names of extra per-atom scalar columns write() receives.

  • traj_content (str, default: "positions") – Content to record: “positions”, “velocities”, “angular_velocity_body”, or “all”. Angular output always includes the three corresponding principal moments of inertia. “all” records positions, atomic velocities, and this six-column angular block.

__init__(path, symbols, molecule_ids, dt_au, record_every_steps=1, record_max_steps=None, append=False, per_atom=(), traj_content='positions')[source]
close()[source]

Close the file. Safe to call twice.

write(step_index, time_au, positions_bohr=None, per_atom=None, velocities_au=None, angular_velocity_body_au=None)[source]

Append one frame per independent system on the recording stride, unless record_max_steps has been reached.

Parameters:
  • step_index (int) – Number of production steps taken so far, counting from one.

  • time_au (float) – The time after that step, in atomic units.

  • positions_bohr (array-like of float, shape (num, n_atom, 3), optional) – Every system’s atomic positions in Bohr. Required when positions are selected and a frame is written.

  • per_atom (dict of str to array-like of float, shape (num, n_atom), optional) – The extra per-atom columns, keyed by the names given at construction.

  • velocities_au (array-like of float, shape (num, n_atom, 3), optional) – Every system’s atomic velocities in atomic units (Bohr / atomic time). Required when velocities are selected and a frame is written.

  • angular_velocity_body_au (array-like of float, shape (num, n_atom, 6), optional) – Three molecular body-frame angular velocities followed by their three principal moments of inertia, repeated for each atom of the molecule. Angular velocities are in inverse atomic time; moments are in electron-mass times Bohr-squared units. Required when angular output is selected.

maxwelllink.tools.recorders.output_filename(filename, first_molecule_id)[source]

Return filename with _id_<first molecule ID> inserted before its extension.

Every driver process – a scalar driver or one batch bridge – writes its own file, and several of them run from one command line, so the name carries the first molecule ID the process owns, as the checkpoints do.