Source code for qp.cluster.charge_count

"""Validate cluster charge assignments."""

import os
from glob import glob
from qp.manager.create import get_charge


ATOM_ELECTRON_MAP = {
    "H": 1,
    "B": 5,
    "C": 6,
    "N": 7,
    "O": 8,
    "F": 9,
    "Na": 11,
    "Mg": 12,
    "Al": 13,
    "P": 15,
    "S": 16,
    "Cl": 17,
    "K": 19,
    "Ca": 20,
    "Zn": 30,
    "Se": 34,
    "Br": 35,
    "Rb": 37,
    "Cd": 48,
    "I": 53,
    "Cs": 55,
    "Pb":82,
}


[docs]def count_electron(xyzfile): """Count the total number of electrons in an XYZ file. Parameters ---------- xyzfile : str Path to an XYZ coordinate file. Returns ------- int Total electron count based on element types. """ with open(xyzfile) as f: lines = f.readlines()[2:] count = 0 for line in lines: if line: atom_type = line.split()[0].strip() count += ATOM_ELECTRON_MAP.get(atom_type, 0) return count
[docs]def check_charge(cluster_path): """Validate that charge, spin multiplicity, and electron count are consistent. Prints an error message if the parity of (electrons - charge) does not match the parity of the spin multiplicity. Parameters ---------- cluster_path : str Path to a cluster directory containing charge/spin CSVs and an XYZ file. """ charge, extraspin = get_charge(cluster_path) print(f"charge: {charge}, extraspin: {extraspin} for {os.path.basename(cluster_path)}") spinmult = 1 + extraspin xyzfile = glob(os.path.join(cluster_path, "*.xyz"))[0] num_electron = count_electron(xyzfile) if (num_electron - charge) % 2 == spinmult % 2: print(f"> ERROR: charge {charge}, spin multiplicity {spinmult}, electron number {num_electron} in {os.path.basename(cluster_path)} are not consistent!")