Skip to content

io

Structure and trajectory I/O helpers.

import mlx_atomistic.io

class RuntimeTraceReporter
def __init__(include_samples: bool = True, include_diagnostics: bool = True, events: list[dict[str, Any]] = list())

Collect scalar runtime reporter events for parity and diagnostics traces.

Parameters

NameTypeDefaultDescription
include_samplesboolTrue
include_diagnosticsboolTrue
eventslist[dict[str, Any]]list()

Methods

def to_jsonable() -> list[dict[str, Any]]

Return the collected events as a list of JSON-serializable dicts.

Returns

  • list[dict[str, Any]] — One dict per recorded reporter event, in arrival order.
class SimulationCheckpoint
def __init__(positions: np.ndarray, velocities: np.ndarray, masses: np.ndarray, forces: np.ndarray, step: int, time: float, cell: np.ndarray | None, thermostat: dict[str, Any], neighbor_policy: dict[str, Any], force_terms: tuple[str, ...], diagnostic_cursor: int, metadata: dict[str, Any])

Serializable boundary for resuming a production MD run.

Parameters

NameTypeDefaultDescription
positionsnp.ndarray
velocitiesnp.ndarray
massesnp.ndarray
forcesnp.ndarray
stepint
timefloat
cellnp.ndarray | None
thermostatdict[str, Any]
neighbor_policydict[str, Any]
force_termstuple[str, ...]
diagnostic_cursorint
metadatadict[str, Any]

Properties

  • hmr_state dict[str, Any] — Hydrogen-mass-repartitioning state recovered from the checkpoint metadata.

Methods

def state() -> SimulationState

Rebuild the in-memory simulation state from this checkpoint.

Returns

  • SimulationState — A SimulationState with MLX-backed arrays.
class TrajectoryRecord
def __init__(sampled_positions: np.ndarray, sampled_velocities: np.ndarray, sampled_steps: np.ndarray, sampled_time: np.ndarray, diagnostic_steps: np.ndarray, diagnostic_time: np.ndarray, potential_energy: np.ndarray, kinetic_energy: np.ndarray, total_energy: np.ndarray, potential_energy_by_term: dict[str, np.ndarray], temperature: np.ndarray, pair_count: np.ndarray, rebuild_count: np.ndarray, constraint_max_error: np.ndarray, symbols: tuple[str, ...], cell: np.ndarray | None, metadata: dict[str, Any], virial_tensor: np.ndarray | None = None, pressure_tensor: np.ndarray | None = None, pressure: np.ndarray | None = None)

Native loaded trajectory record.

Parameters

NameTypeDefaultDescription
sampled_positionsnp.ndarray
sampled_velocitiesnp.ndarray
sampled_stepsnp.ndarray
sampled_timenp.ndarray
diagnostic_stepsnp.ndarray
diagnostic_timenp.ndarray
potential_energynp.ndarray
kinetic_energynp.ndarray
total_energynp.ndarray
potential_energy_by_termdict[str, np.ndarray]
temperaturenp.ndarray
pair_countnp.ndarray
rebuild_countnp.ndarray
constraint_max_errornp.ndarray
symbolstuple[str, ...]
cellnp.ndarray | None
metadatadict[str, Any]
virial_tensornp.ndarray | NoneNone
pressure_tensornp.ndarray | NoneNone
pressurenp.ndarray | NoneNone
def load_npz_trajectory(path: str | Path) -> TrajectoryRecord

Load a native trajectory record from a .npz file.

Parameters

NameTypeDefaultDescription
pathstr | PathPath to a file written by save_npz_trajectory.

Returns

  • TrajectoryRecord — The reconstructed TrajectoryRecord.
def load_simulation_checkpoint(path: str | Path) -> SimulationCheckpoint

Load a restart checkpoint written by save_simulation_checkpoint.

Parameters

NameTypeDefaultDescription
pathstr | PathPath to a checkpoint file.

Returns

  • SimulationCheckpoint — The reconstructed SimulationCheckpoint.
def read_xyz(path: str | Path) -> tuple[tuple[str, ...], np.ndarray, str]

Read a single-frame XYZ file.

Parameters

NameTypeDefaultDescription
pathstr | PathPath to the .xyz file.

Returns

  • tuple[tuple[str, ...], np.ndarray, str] — A (symbols, positions, comment) tuple: element symbols, an (n_atoms, 3) float array of coordinates, and the comment line.

Raises

  • ValueError — If the file has fewer than two lines or the declared atom count does not match the number of coordinate lines.
def restart_state_from_trajectory(record: TrajectoryRecord, masses, force_terms: ForceTerm | list[ForceTerm] | tuple[ForceTerm, ...], *, cell: Cell | None = None, frame: int = -1) -> SimulationState

Recompute forces for a continuation-ready state from a trajectory frame.

Parameters

NameTypeDefaultDescription
recordTrajectoryRecordThe TrajectoryRecord to restart from.
massesPer-particle masses, shape (n_particles,).
force_termsForceTerm | list[ForceTerm] | tuple[ForceTerm, ...]One or more force terms used to recompute forces at the frame.
cellCell | NoneNoneOptional periodic cell. Defaults to None.
frameint-1Frame index into the sampled trajectory. Defaults to -1 (last frame).

Returns

  • SimulationState — A SimulationState with the frame’s positions/velocities and freshly recomputed forces.

Raises

  • ValueError — If force_terms is empty.
def save_npz_trajectory(path: str | Path, result, *, symbols: list[str] | tuple[str, ...] | None = None, cell: Cell | None = None, metadata: dict[str, Any] | None = None) -> None

Save a native trajectory record to a compressed .npz file.

Parameters

NameTypeDefaultDescription
pathstr | PathDestination .npz path.
resultA finished simulation result (e.g. NVEResult) to serialize.
symbolslist[str] | tuple[str, ...] | NoneNoneOptional element symbols stored with the trajectory. Defaults to None.
cellCell | NoneNoneOptional periodic cell stored with the frames. Defaults to None.
metadatadict[str, Any] | NoneNoneOptional JSON-serializable metadata dict. Defaults to None.

Returns

  • None
def save_simulation_checkpoint(path: str | Path, state: SimulationState, *, cell: Cell | None = None, thermostat: dict[str, Any] | None = None, neighbor_policy: dict[str, Any] | None = None, force_terms: tuple[str, ...] | list[str] | None = None, diagnostic_cursor: int | None = None, metadata: dict[str, Any] | None = None, runtime_sync_report: dict[str, int | float] | None = None, runtime_nonbonded_report: dict[str, int | float | str | None] | None = None) -> None

Write a restart checkpoint for a runner-level continuation.

Parameters

NameTypeDefaultDescription
pathstr | PathDestination checkpoint path (parent directories are created).
stateSimulationStateThe SimulationState to serialize.
cellCell | NoneNoneOptional periodic cell. Defaults to None.
thermostatdict[str, Any] | NoneNoneOptional thermostat state (RNG offset or Nose-Hoover state). Defaults to None.
neighbor_policydict[str, Any] | NoneNoneOptional neighbor-list policy dict. Defaults to None.
force_termstuple[str, ...] | list[str] | NoneNoneOptional names of the force terms in effect. Defaults to None.
diagnostic_cursorint | NoneNoneOptional index into the diagnostic series for exact resumption. Defaults to None.
metadatadict[str, Any] | NoneNoneOptional JSON-serializable metadata dict. Defaults to None.
runtime_sync_reportdict[str, int | float] | NoneNoneOptional runtime-sync counters to persist. Defaults to None.
runtime_nonbonded_reportdict[str, int | float | str | None] | NoneNoneOptional nonbonded-runtime report to persist. Defaults to None.

Returns

  • None
def trajectory_record_from_result(result, *, symbols: list[str] | tuple[str, ...] | None = None, cell: Cell | None = None, metadata: dict[str, Any] | None = None) -> TrajectoryRecord

Create a native loaded-record view from an in-memory simulation result.

Parameters

NameTypeDefaultDescription
resultA finished simulation result to convert.
symbolslist[str] | tuple[str, ...] | NoneNoneOptional element symbols. Defaults to None.
cellCell | NoneNoneOptional periodic cell. Defaults to None.
metadatadict[str, Any] | NoneNoneOptional JSON-serializable metadata dict. Defaults to None.

Returns

  • TrajectoryRecord — A TrajectoryRecord mirroring what load_npz_trajectory would return for the same run.
def write_xyz(path: str | Path, symbols: list[str] | tuple[str, ...], positions, *, comment: str = '') -> None

Write a single-frame XYZ file.

Parameters

NameTypeDefaultDescription
pathstr | PathDestination path for the .xyz file.
symbolslist[str] | tuple[str, ...]Element symbols, one per atom.
positionsAtomic coordinates, shape (n_atoms, 3).
commentstr''Comment written as the file’s second line. Defaults to "".

Returns

  • None

Raises

  • ValueError — If positions does not have shape (len(symbols), 3).