Source code for porereax.bond_length

"""
Module for sampling bond lengths and bond orders.

The module provides :class:`BondLengthSampler` to sample bond length
histograms (in Angstroms) or ReaxFF bond order histograms for specified bonds.
"""

import numpy as np

import porereax.utils as utils
from porereax.meta_sampler import _BOND_SAMPLER_INIT_PARAMS, BondSampler
from porereax.regions import Region
from porereax.utils import Substitution


[docs] @Substitution(params=_BOND_SAMPLER_INIT_PARAMS) class BondLengthSampler(BondSampler): """ Sampler class for bond lengths and bond orders. Parameters ---------- %(params)s num_bins : int Number of bins for histogram sampling. range : tuple Range (min, max) for histogram sampling. """ def __init__( self, name_out: str, bonds: list, dimension: str, region: Region, process_id: int, atom_lib: dict, masses: dict, num_frames: int, box: np.ndarray, system_properties: dict | None, num_bins: int, range: tuple, ): valid_dimensions = ["Bond Length", "Bond Order"] if not isinstance(dimension, str) or dimension not in valid_dimensions: raise ValueError( f"BondLengthSampler does not support dimension {dimension}" ) if not isinstance(num_bins, (int)) or num_bins <= 0: raise ValueError( "BondLengthSampler requires a positive integer 'num_bins' parameter." ) if ( not isinstance(range, (list, tuple)) or len(range) != 2 or range[0] >= range[1] ): raise ValueError( "BondLengthSampler requires a 'range' parameter as a list or " "tuple of two numbers (min, max) with min < max." ) self._num_bins = num_bins self._range = range super().__init__( name_out, bonds, dimension, region, process_id, atom_lib, masses, num_frames, box, system_properties, ) self._input.update( { "num_bins": num_bins, "range": range, } ) # Setup data for identifier in self._bonds: hist, bin_edges = np.histogram([], bins=self._num_bins, range=self._range) self._data[identifier] = { "num_frames": 0, "num_bonds": 0, "mean": 0.0, "std": 0.0, "hist": hist, "bin_edges": bin_edges, }
[docs] def sample( self, frame_id: int, molecule_mask: dict, molecule_bond_atoms: dict, bond_mask: dict, frame: object, bond_enum: object, positions_transformed: np.ndarray, ): bond_topology = frame.particles.bonds.topology.array positions = frame.particles.positions.array position_mask = self._region(positions) for identifier in self._bonds: b_mask = ( bond_mask[identifier] & position_mask[bond_topology[:, 0]] & position_mask[bond_topology[:, 1]] ) bonds = bond_topology[b_mask] position = positions[bonds] if self._dimension == "Bond Length": bond_lengths = np.linalg.norm( utils.min_image_convention( position[:, 0, :] - position[:, 1, :], self._box ), axis=1, ) hist, _ = np.histogram( bond_lengths, bins=self._num_bins, range=self._range ) mean = np.sum(bond_lengths) std = np.sum(bond_lengths**2) elif self._dimension == "Bond Order": bond_orders = ( frame.particles.bonds.get("Bond Order").array if "Bond Order" in frame.particles.bonds else np.zeros(frame.particles.bonds.count) ) bond_order = bond_orders[b_mask] hist, _ = np.histogram( bond_order, bins=self._num_bins, range=self._range ) mean = np.sum(bond_order) std = np.sum(bond_order**2) self._data[identifier]["hist"] += hist self._data[identifier]["mean"] += mean self._data[identifier]["std"] += std self._data[identifier]["num_frames"] += 1 self._data[identifier]["num_bonds"] += bonds.shape[0]
def _combine_identifier(self, identifier: str, data: dict) -> dict: num_frames = np.sum(data["num_frames"]) num_bonds = np.sum(data["num_bonds"]) bin_edges = data["bin_edges"][0] # Compute mean and standard error of the mean (SEM) mean = np.sum(data["mean"]) / num_bonds if num_bonds > 0 else 0.0 worker_counts = np.array(data["num_bonds"]) worker_sums = np.array(data["mean"]) valid_workers = worker_counts > 0 worker_means = worker_sums[valid_workers] / worker_counts[valid_workers] mean_std = ( np.std(worker_means, ddof=1) / np.sqrt(len(worker_means)) if len(worker_means) > 1 else np.nan ) # Normalise histogram and compute error on histogram bins hist = ( np.sum(data["hist"], axis=0) / num_frames if num_frames > 0 else np.zeros(self._num_bins) ) worker_frames = np.array(data["num_frames"]) worker_hists = np.array(data["hist"]) / worker_frames[:, np.newaxis] hist_std = ( np.std(worker_hists, axis=0, ddof=1) / np.sqrt(len(worker_hists)) if len(worker_hists) > 1 else np.full(self._num_bins, np.nan) ) # Compute standard deviation of distribution std = np.sqrt( np.sum(data["std"]) / num_bonds - mean**2 if num_bonds > 0 else np.nan ) return { "num_frames": num_frames, "num_bonds": num_bonds, "mean": mean, "hist": hist, "std": std, "mean_std": mean_std, "hist_std": hist_std, "bin_edges": bin_edges, }