#!/usr/bin/env python3
"""Split multi-image .con files into per-image structures.
.. versionadded:: 0.0.2
"""
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "click",
# "ase",
# "rich",
# "chemparseplot[neb]>=1.13.0,<2",
# "readcon>=0.17.1",
# "rgpycrumbs>=1.10.2",
# ]
# ///
import logging
import sys
from enum import Enum
from pathlib import Path
import click
from ase.io import read as aseread
from rich.console import Console
from rich.logging import RichHandler
from rgpycrumbs.geom.api.alignment import IRAConfig, align_structure_robust
[docs]
_CON_SUFFIXES = {".con", ".convel"}
[docs]
def _is_con_path(path: Path) -> bool:
return path.suffix.lower() in _CON_SUFFIXES
[docs]
def _read_trajectory_frames(path: Path) -> tuple[list, list | None]:
"""Load trajectory frames.
``.con`` / ``.convel`` stay ``ConFrame`` objects from ``con_io`` so a later
write can keep sections. Every other suffix stays on ``ase.io.read``.
"""
if _is_con_path(path):
from chemparseplot.parse.eon.con_io import read_con_frames
con_frames = read_con_frames(path)
atoms_list = [frame.to_ase() for frame in con_frames]
for atoms, frame in zip(atoms_list, con_frames, strict=True):
if frame.energy is not None:
atoms.info["energy"] = float(frame.energy)
return atoms_list, con_frames
return list(aseread(path, index=":")), None
[docs]
def _energy_from_atoms(atoms) -> float | None:
energy = atoms.info.get("energy") if getattr(atoms, "info", None) else None
if energy is None and getattr(atoms, "calc", None) is not None:
try:
energy = atoms.get_potential_energy()
except Exception:
energy = None
if energy is None:
return None
try:
return float(energy)
except (TypeError, ValueError):
return None
[docs]
def _energy_changed(frame, energy: float | None) -> bool:
if energy is None:
return False
current = getattr(frame, "energy", None)
if current is None:
return True
return float(energy) != float(current)
[docs]
def _with_energy(frame, energy: float | None):
"""Copy *frame* through ``frame_with_energy`` only when the energy changes."""
if not _energy_changed(frame, energy):
return frame
from chemparseplot.parse.eon.con_io import frame_with_energy
return frame_with_energy(frame, float(energy))
[docs]
def _same_symbol_order(frame, atoms) -> bool:
return [atom.symbol for atom in frame.atoms] == list(atoms.get_chemical_symbols())
[docs]
def _is_pure_translation(src, dst) -> bool:
"""True when every atom moved by the same vector, including the zero vector."""
if len(src) != len(dst):
return False
if len(src) == 0:
return True
dx = float(dst[0][0]) - float(src[0][0])
dy = float(dst[0][1]) - float(src[0][1])
dz = float(dst[0][2]) - float(src[0][2])
for origin, moved in zip(src, dst, strict=True):
if abs((float(moved[0]) - float(origin[0])) - dx) > _POSITION_ATOL:
return False
if abs((float(moved[1]) - float(origin[1])) - dy) > _POSITION_ATOL:
return False
if abs((float(moved[2]) - float(origin[2])) - dz) > _POSITION_ATOL:
return False
return True
[docs]
def _positions_unmoved(frame, atoms) -> bool:
src = frame.xyz
dst = atoms.get_positions()
if len(src) != len(dst):
return False
for origin, moved in zip(src, dst, strict=True):
if abs(float(moved[0]) - float(origin[0])) > _POSITION_ATOL:
return False
if abs(float(moved[1]) - float(origin[1])) > _POSITION_ATOL:
return False
if abs(float(moved[2]) - float(origin[2])) > _POSITION_ATOL:
return False
return True
[docs]
def _cell_changed(frame, atoms) -> bool:
lengths = [float(value) for value in atoms.cell.lengths()]
angles = [float(value) for value in atoms.cell.angles()]
for got, old in zip(lengths, frame.cell, strict=True):
if abs(got - float(old)) > _POSITION_ATOL:
return True
for got, old in zip(angles, frame.angles, strict=True):
if abs(got - float(old)) > _POSITION_ATOL:
return True
return False
[docs]
def _sections_still_true(frame, atoms) -> bool:
"""Forces, displacements, and spreads still describe *atoms*.
A pure translation keeps them. A rotation or a reorder does not.
"""
if not _same_symbol_order(frame, atoms):
return False
return _is_pure_translation(frame.xyz, atoms.get_positions())
[docs]
def _with_cell(frame, atoms):
if not _cell_changed(frame, atoms):
return frame
import readcon
metadata = frame.metadata
if hasattr(metadata, "items"):
metadata = {str(key): value for key, value in metadata.items()}
else:
metadata = dict(metadata or {})
return readcon.ConFrame(
[float(value) for value in atoms.cell.lengths()],
[float(value) for value in atoms.cell.angles()],
list(frame.atoms),
frame.prebox_header,
frame.postbox_header,
metadata,
)
[docs]
def _write_fresh_geometry(dest: Path, atoms, energy: float | None) -> None:
"""Write a new geometry. Old forces, displacements, and spreads stay unset."""
from chemparseplot.parse.eon.con_io import write_atoms_as_con
clone = atoms.copy()
clone.calc = None
energies = [energy] if energy is not None else None
write_atoms_as_con(dest, [clone], energies=energies)
[docs]
def _write_output_frame(
dest: Path,
atoms,
frame,
energy: float | None,
*,
preserve_source: bool,
) -> None:
"""Write one image with precision omitted and canonical left off.
An unmoved CON frame is the original object, so a RoundTrip rewrite can
match the input bytes. A translation uses ``frame_with_positions``. A
rotation or a reorder goes through ``from_ase`` with the calculator cleared.
"""
from chemparseplot.parse.eon.con_io import frame_with_positions, write_con_frames
if frame is not None and energy is None and frame.energy is not None:
energy = float(frame.energy)
if frame is None:
if energy is None:
energy = _energy_from_atoms(atoms)
_write_fresh_geometry(dest, atoms, energy)
return
unchanged = (
_sections_still_true(frame, atoms)
and _positions_unmoved(frame, atoms)
and not _cell_changed(frame, atoms)
)
if preserve_source or unchanged:
write_con_frames(dest, [_with_energy(frame, energy)])
return
if _sections_still_true(frame, atoms):
moved = frame_with_positions(frame, atoms.get_positions())
moved = _with_cell(moved, atoms)
write_con_frames(dest, [_with_energy(moved, energy)])
return
_write_fresh_geometry(dest, atoms, energy)
# Optional IRA import logic
try:
from rgpycrumbs._aux import _import_from_parent_env
[docs]
ira_mod = _import_from_parent_env("ira_mod")
except ImportError:
ira_mod = None
logging.basicConfig(
level=logging.INFO,
format="%(message)s",
datefmt="[%X]",
handlers=[
RichHandler(
console=CONSOLE,
rich_tracebacks=True,
markup=True,
show_path=False,
show_level=True,
show_time=True,
)
],
)
[docs]
class AlignMode(Enum):
"""Defines structural alignment strategies."""
[docs]
ENDPOINTS = "endpoints"
[docs]
class SplitMode(Enum):
"""Defines trajectory validation strictness."""
[docs]
NEB = "neb" # Strict: must be a clean multiple of images_per_path
[docs]
FLEX = "flex" # Flexible: allows partial paths or simple slicing
[docs]
def align_path(frames, mode: AlignMode, iraconf: IRAConfig):
"""Applies the selected alignment strategy to the image sequence."""
if mode == AlignMode.NONE or len(frames) < 2:
return frames
ref = frames[0]
if mode == AlignMode.ALL:
logging.info("Aligning [bold]all[/bold] images to reactant reference.")
return [ref.copy()] + [
align_structure_robust(
ref, f.copy(), IRAConfig(iraconf.use_ira, iraconf.kmax)
).atoms
for f in frames[1:]
]
if mode == AlignMode.ENDPOINTS:
logging.info("Aligning [bold]endpoints[/bold] (product to reactant) only.")
aligned_product = align_structure_robust(ref, frames[-1].copy(), iraconf).atoms
# Intermediate frames remain unchanged in this specific mode logic,
# Usually, endpoint alignment implies ensuring the BCs match.
new_frames = [f.copy() for f in frames]
new_frames[-1] = aligned_product
return new_frames
return frames
@click.command()
@click.argument(
"neb_trajectory_file",
type=click.Path(exists=True, dir_okay=False, readable=True, path_type=Path),
)
@click.option(
"--mode",
type=click.Choice([m.value for m in SplitMode]),
default=SplitMode.FLEX.value,
help="Validation mode: 'neb' (strict multiples) or 'normal' (flexible).",
)
@click.option(
"--output-dir",
type=click.Path(file_okay=False, writable=True, path_type=Path),
default=None,
help="Directory to save output files. Defaults to the input filename stem.",
)
@click.option(
"--images-per-path",
type=int,
required=True,
help="Number of images in a single NEB path (e.g., 7). [REQUIRED]",
)
@click.option(
"--path-index",
type=int,
default=-1,
show_default=True,
help="Index of the NEB path to extract (0-based). Use -1 for the last path.",
)
@click.option(
"--center/--no-center",
default=False,
help="Center the atomic coordinates around the origin.",
)
@click.option(
"--box-diagonal",
nargs=3,
type=(float, float, float),
default=(25.0, 25.0, 25.0),
show_default=True,
help="Override the unit cell dimensions (Å) during processing.",
)
@click.option(
"--align-type",
type=click.Choice([m.value for m in AlignMode]),
default=AlignMode.NONE.value,
help="Alignment: 'all' (every image), 'endpoints' (reactant/product), or 'none'.",
)
@click.option(
"--use-ira",
is_flag=True,
help="Enable Iterative Reordering and Alignment (requires ira_mod).",
)
@click.option(
"--ira-kmax",
type=float,
default=1.8,
help="kmax factor for the IRA matching algorithm.",
)
@click.option(
"--path-list-filename",
default="ipath.dat",
help="Name of the file listing the generated .con absolute paths.",
)
[docs]
def con_splitter(
neb_trajectory_file: Path,
mode: str,
output_dir: Path | None,
images_per_path: int,
path_index: int,
center: bool,
box_diagonal: tuple[float, float, float],
align_type: str,
use_ira: bool,
ira_kmax: float,
path_list_filename: str,
):
"""
Splits a multi-step trajectory file (.traj, .con, etc.) into
individual .con files for a *single* specified path.
This script reads a trajectory file, which may contain multiple NEB
optimization steps (paths), and extracts only the frames corresponding
to a single specified path.
It writes each frame of that path into a separate .con file
(e.g., ipath_000.con, ipath_001.con, ...).
It also generates a text file (default: 'ipath.dat') that lists the
absolute paths of all created .con files.
This utility extracts specific optimization steps and applies physical
chemistry refinements such as centering, cell overrides, and structural
alignment (RMSD minimization).
"""
if output_dir is None:
output_dir = Path(neb_trajectory_file.stem)
output_dir.mkdir(parents=True, exist_ok=True)
CONSOLE.rule(f"[bold green]Processing {neb_trajectory_file.name}[/bold green]")
if images_per_path <= 0:
logging.critical("--images-per-path must be a positive integer.")
sys.exit(1)
try:
all_frames, all_con_frames = _read_trajectory_frames(Path(neb_trajectory_file))
if not all_frames:
logging.error("No frames found in input file.")
sys.exit(1)
except Exception as e:
logging.critical(f"Failed to read trajectory: {e}")
sys.exit(1)
total_frames = len(all_frames)
num_paths = total_frames // images_per_path
remainder = total_frames % images_per_path
# Validation Logic based on Mode
if mode == SplitMode.NEB.value and remainder != 0:
logging.warning(
f"Trajectory has {total_frames} frames,"
f" which is not a multiple of {images_per_path}. "
f"This often indicates an interrupted NEB calculation."
)
if total_frames < images_per_path:
logging.critical(
f"Total frames ({total_frames})"
f" is less than images per path ({images_per_path})."
)
sys.exit(1)
target_idx = num_paths - 1 if path_index == -1 else path_index
if not (0 <= target_idx < num_paths):
logging.critical(
f"Path index {target_idx} is out of bounds (0 to {num_paths - 1})."
)
sys.exit(1)
start, end = target_idx * images_per_path, (target_idx + 1) * images_per_path
frames = all_frames[start:end]
con_frames = all_con_frames[start:end] if all_con_frames is not None else None
logging.info(f"Extracted [cyan]Path {target_idx}[/cyan] with {len(frames)} images.")
if center:
logging.info("Centering structures...")
if box_diagonal:
logging.info("Overriding box...")
if center and len(frames) > 0:
ref_atoms = frames[0].copy()
ref_center = ref_atoms.get_center_of_mass()
box_center = [d / 2.0 for d in box_diagonal]
shift = box_center - ref_center
for atoms in frames:
atoms.set_cell(box_diagonal)
atoms.translate(shift)
align_strategy = AlignMode(align_type)
if align_strategy != AlignMode.NONE:
frames = align_path(
frames, align_strategy, IRAConfig(enabled=use_ira, kmax=ira_kmax)
)
preserve_source = (not center) and align_strategy == AlignMode.NONE
created_paths = []
for i, atoms in enumerate(frames):
name = f"ipath_{i:03d}.con"
dest = output_dir / name
source = con_frames[i] if con_frames is not None else None
_write_output_frame(
dest,
atoms,
source,
_energy_from_atoms(atoms),
preserve_source=preserve_source,
)
created_paths.append(str(dest.resolve()))
logging.info(f" - Saved [green]{name}[/green]")
with open(output_dir / path_list_filename, "w") as f:
f.write("\n".join(created_paths) + "\n")
logging.info(f"Path list saved to [magenta]{path_list_filename}[/magenta]")
CONSOLE.rule("[bold green]Complete[/bold green]")
if __name__ == "__main__":
con_splitter()