Download fullC/code/check_overlap.py from ericqu/OME: direct link, hf CLI and curl.
- Browser
- Download file 5.2 kB
-
https://huggingface.co/datasets/ericqu/OME/resolve/main/fullC/code/check_overlap.py
- Command line
-
hf download hf://datasets/ericqu/OME/fullC/code/check_overlap.py
-
curl -L -o check_overlap.py https://huggingface.co/datasets/ericqu/OME/resolve/main/fullC/code/check_overlap.py
5.2 kB
| """Verify ORCA's printed AO Fock matrix + pyscf overlap reproduces ORCA orbital energies. | |
| Established convention (ORCA 6.0, def2-TZVPD): | |
| * AO order per atom = def2-TZVP shells grouped by l (s,p,d,f,g; basis order within l), | |
| then the def2-TZVPD augmentation (diffuse) shells appended in l order. | |
| * m order: p = (z, x, y); d = (z2, xz, yz, x2-y2, xy); f = (0,+1,-1,+2,-2,+3,-3); g = (0,+1,-1,...,+4,-4) | |
| * relative to pyscf real solid harmonics, f(+3) and f(-3) have opposite sign (g signs searched if present). | |
| """ | |
| import re, sys, time, itertools | |
| import numpy as np | |
| from scipy.linalg import eigh | |
| from pyscf import gto | |
| def parse(path): | |
| lines = open(path, errors="replace").read().split("\n") | |
| i = lines.index("CARTESIAN COORDINATES (ANGSTROEM)") | |
| atoms = [] | |
| for l in lines[i+2:]: | |
| p = l.split() | |
| if len(p) != 4: break | |
| atoms.append((p[0], (float(p[1]), float(p[2]), float(p[3])))) | |
| charge = int([l for l in lines if "Total Charge" in l][0].split("....")[1]) | |
| mult = int([l for l in lines if l.strip().startswith("Multiplicity") and "Mult " in l][0].split("....")[1]) | |
| hftyp = [l for l in lines if "Hartree-Fock type" in l][0].split("....")[1].strip() | |
| nbas = int([l for l in lines if l.startswith("Number of basis functions")][0].split("...")[1]) | |
| k = lines.index("ORBITAL ENERGIES") | |
| start = k + 4 | |
| eps = [] | |
| for l in lines[start:]: | |
| p = l.split() | |
| if len(p) != 4: break | |
| eps.append(float(p[2])) | |
| fi = lines.index("FOCK"); i = fi + 2 | |
| F = np.zeros((nbas, nbas)); done = 0 | |
| while done < nbas: | |
| ncol = len(lines[i].split()) | |
| F[:, done:done+ncol] = np.fromstring("\n".join(lines[i+1:i+1+nbas]), sep=" ").reshape(nbas, ncol+1)[:, 1:] | |
| done += ncol; i += 1 + nbas | |
| return atoms, charge, mult, hftyp, nbas, np.array(eps), F | |
| ORCA_M = {l: [0] + [m for k in range(1, l+1) for m in (k, -k)] for l in range(6)} | |
| FLIP = {(3, 3), (3, -3)} # sign flips relative to pyscf | |
| def orca_perm_and_sign(mol, extra_flips=()): | |
| tzvp = {} | |
| for ia in range(mol.natm): | |
| el = mol.atom_symbol(ia) | |
| if el not in tzvp: | |
| tzvp[el] = {(sh[0], tuple(round(p[0], 6) for p in sh[1:])) for sh in gto.basis.load("def2-tzvp", el)} | |
| perm = []; sgn = []; labels = [] | |
| ao = 0; table = {} | |
| for ish in range(mol.nbas): | |
| ia = mol.bas_atom(ish); l = mol.bas_angular(ish) | |
| for c in range(mol.bas_nctr(ish)): | |
| for m in ([1, -1, 0] if l == 1 else list(range(-l, l+1))): | |
| table[(ish, c, m)] = ao; ao += 1 | |
| for ia in range(mol.natm): | |
| el = mol.atom_symbol(ia) | |
| shells = [] | |
| for ish in [i for i in range(mol.nbas) if mol.bas_atom(i) == ia]: | |
| l = mol.bas_angular(ish); ex = tuple(round(float(e), 6) for e in mol.bas_exp(ish)) | |
| for c in range(mol.bas_nctr(ish)): | |
| shells.append((l, ish, c, (l, ex) not in tzvp[el])) | |
| shells = sorted([s for s in shells if not s[3]], key=lambda s: (s[0], s[1], s[2])) + \ | |
| sorted([s for s in shells if s[3]], key=lambda s: (s[0], s[1], s[2])) | |
| for l, ish, c, aug in shells: | |
| for m in ORCA_M[l]: | |
| perm.append(table[(ish, c, m)]) | |
| sgn.append(-1.0 if ((l, m) in FLIP) ^ ((l, m) in extra_flips) else 1.0) | |
| labels.append((ia, el, l, m, aug)) | |
| return np.array(perm), np.array(sgn), labels | |
| def orca_overlap(mol, extra_flips=()): | |
| perm, sgn, labels = orca_perm_and_sign(mol, extra_flips) | |
| S = mol.intor("int1e_ovlp")[np.ix_(perm, perm)] * np.outer(sgn, sgn) | |
| return S, labels | |
| if __name__ == "__main__": | |
| for path in sys.argv[1:]: | |
| t0 = time.time(); name = path.split("/samples/")[1].split("/")[0] | |
| try: | |
| atoms, charge, mult, hftyp, nbas, eps, F = parse(path) | |
| mol = gto.M(atom=atoms, basis="def2-tzvpd", ecp="def2-tzvpd", unit="Angstrom", charge=charge, spin=mult-1, verbose=0) | |
| except Exception as e: | |
| print(f"{name:24s} SKIP: {type(e).__name__}: {str(e)[:120]}"); continue | |
| if mol.nao != nbas: | |
| print(f"{name:24s} SKIP: pyscf nao {mol.nao} != ORCA nbas {nbas}"); continue | |
| nmo = int((eps != 0).sum()); nocc = (mol.nelectron + (mult-1)) // 2 | |
| S, labels = orca_overlap(mol) | |
| w = eigh(F, S, eigvals_only=True); d = np.abs(w[:nmo] - eps[:nmo]) | |
| note = "" | |
| has_g = any(l == 4 for _, _, l, _, _ in labels) | |
| if d[:nocc].max() > 1e-4 and has_g: | |
| gl = [(4, m) for m in ORCA_M[4] if m != 0] | |
| for flips in itertools.product([0, 1], repeat=len(gl)): | |
| ef = tuple(x for x, f in zip(gl, flips) if f) | |
| S2, _ = orca_overlap(mol, ef) | |
| w2 = eigh(F, S2, eigvals_only=True); d2 = np.abs(w2[:nmo] - eps[:nmo]) | |
| if d2[:nocc].max() < d[:nocc].max(): | |
| d = d2; note = f" g-flips={ef}" | |
| if d[:nocc].max() < 1e-4: break | |
| sS = np.linalg.eigvalsh(S) | |
| print(f"{name:24s} {hftyp} nbas={nbas} nmo={nmo} Smin={sS.min():.1e} | occ max|de|={d[:nocc].max():.1e} all max={d.max():.1e} mean={d.mean():.1e}{note} | {time.time()-t0:.0f}s", flush=True) | |