xyz#

irmsd.utils.xyz.CANONICAL_ID_COLUMN = 'canonical_id'#

Name of the per-atom column holding canonical atom identifiers.

irmsd.utils.xyz.EV_PER_HARTREE = 27.211386245988#

eV per Hartree (CODATA 2018; matches ase.units.Hartree).

irmsd.utils.xyz._DEFAULT_PROPERTIES = 'species:S:1:pos:R:3'#

Default per-atom column schema when no Properties= token is given.

irmsd.utils.xyz._EV_UNIT_ALIASES = frozenset({'electronvolt', 'electronvolts', 'ev'})#

Values (case-insensitive) flagging an energy given in electronvolt.

irmsd.utils.xyz._HARTREE_UNIT_ALIASES = frozenset({'a.u.', 'atomic', 'au', 'e_h', 'eh', 'ha', 'hartree', 'hartrees'})#

Values (case-insensitive) flagging an energy already given in Hartree.

irmsd.utils.xyz._PROPERTIES_WITH_IDS = 'species:S:1:pos:R:3:canonical_id:I:1'#

Schema string emitted when a Molecule carries per-atom IDs.

irmsd.utils.xyz._column_layout(fields: list[tuple[str, str, int]] | None) → tuple[int, int | None, int | None, int][source]#

Resolve a schema to column offsets.

Returns (sym_idx, pos_idx, id_idx, ncols) where the indices are 0-based offsets into the whitespace-split atom line. pos_idx/id_idx are None when the corresponding column is absent. The species column is mandatory and defaults to 0.

irmsd.utils.xyz._energy_to_hartree(value: float | None, units: str | None) → float | None[source]#

Convert a comment-line energy to Hartree given an optional unit token.

  • units is None (no marker) → assumed Hartree, returned unchanged.

  • units an eV alias → divided by EV_PER_HARTREE.

  • units a Hartree alias (or anything unrecognized) → returned unchanged.

irmsd.utils.xyz._format_cell_value(cell: ndarray) → str[source]#

Format (3,3) cell into a compact string suitable for key=value.

irmsd.utils.xyz._format_pbc_value(pbc: tuple[bool, bool, bool]) → str[source]#

Format (3,) bool PBC into string.

irmsd.utils.xyz._iter_molecules(obj: Molecule | Sequence[Molecule]) → Iterable[Molecule][source]#

Normalize to an iterable of Molecule objects.

irmsd.utils.xyz._open_maybe(path_or_file: str | Path | TextIO, mode: str) → tuple[TextIO, bool][source]#

Accept either a path-like object or an open file object. Returns (fileobj, should_close).

irmsd.utils.xyz._parse_cell_value(val: str) → ndarray | None[source]#

Parse a cell string into a (3, 3) array.

Expected formats (very tolerant): - ‘a11 a12 a13 a21 a22 a23 a31 a32 a33’ - ‘[a11, a12, …, a33]’ - 3 values → interpreted as diagonal cell lengths

irmsd.utils.xyz._parse_comment_line(line: str) → tuple[dict[str, Any], float | None, ndarray | None, tuple[bool, bool, bool] | None, str | None][source]#

Parse an extended-XYZ comment line into:

  • info dict of generic key→value entries

  • energy (if ‘energy=’ present), returned in Hartree

  • cell (if ‘cell=’ present)

  • pbc (if ‘pbc=’ present)

  • properties (the raw Properties= schema string, if present)

Remaining key=value pairs go into the info dict.

The energy is interpreted according to an optional energy_units= token: absent (or a Hartree alias) means the value is already Hartree, eV is converted to Hartree. The energy_units token is consumed and not placed in the info dict. The Properties token is likewise consumed (it describes the atom-block column layout, not per-frame metadata).

When no energy= token is present, the first bare numeric token on the line is used as the energy, assumed to be in Hartree. This matches the CREST/tag-less ensemble convention where the comment line carries only the energy as a plain number.

irmsd.utils.xyz._parse_pbc_value(val: str) → tuple[bool, bool, bool] | None[source]#

Parse a PBC string into a 3-tuple of bools.

Typical formats: - ‘T T T’ - ‘1 1 0’ - ‘[T, F, T]’

irmsd.utils.xyz._parse_properties_schema(spec: str | None) → list[tuple[str, str, int]] | None[source]#

Parse a Properties spec into a list of (name, type, count) triples.

Returns None if the spec is empty or malformed (caller falls back to the default layout).

irmsd.utils.xyz._parse_value(s: str) → Any[source]#

Try to interpret a string as int, float, bool, or fall back to str.

irmsd.utils.xyz.read_extxyz(path_or_file: str | Path | TextIO) → Molecule | list[Molecule][source]#

Read an extended-XYZ file and return either:

  • a single Molecule (if only one structure is present)

  • a list[Molecule] if there are multiple structures

The extended-XYZ comment line may contain key=value pairs. Special handling: - ‘energy=’ → stored in Molecule.energy; if absent, the first bare numeric

token on the comment line is used as the energy (Hartree), matching the tag-less CREST ensemble convention

  • ‘cell=’ → stored in Molecule.cell

  • ‘pbc=’ → stored in Molecule.pbc

  • ‘Properties=’ → describes the atom-block column layout; a canonical_id:I:1 column (if present) is read into Molecule.ids

All other key=value pairs go into Molecule.info.

irmsd.utils.xyz.write_extxyz(path_or_file: str | Path | TextIO, molecules: Molecule | Sequence[Molecule], mode: str = 'w') → None[source]#

Write one or many Molecule objects to an extended-XYZ file.

Special behavior: - If Molecule.energy is not None, writes ‘energy=<value> energy_units=Hartree’

in the comment line (Molecule energies are stored in Hartree).

  • If Molecule.cell is not None, writes ‘cell=”<a11 … a33>”’.

  • If Molecule.pbc is not None, writes ‘pbc=”T T T”’ etc.

  • If Molecule.ids is not None, writes a per-atom canonical_id column and advertises it via ‘Properties=species:S:1:pos:R:3:canonical_id:I:1’.

  • All entries in Molecule.info are written as additional key=value pairs.

Parameters:
  • path_or_file (str | Path | TextIO) – Output path or already opened file object.

  • molecules (Molecule | Sequence[Molecule]) – One Molecule or a sequence of Molecule objects.

  • mode (str) – File open mode, default “w”. Use “a” to append.