| """Verify a Pass A Zarr store: structural checks on every shard, plus a full re-parse comparison |
| for a random sample of calculations. This is the gate before scaling to the full run. |
| """ |
| from __future__ import annotations |
| import argparse, glob, os, random, sys |
| import numpy as np |
| import zarr |
|
|
| sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) |
| from omol_parse import parse_archive |
| from omol_store import (read_calc, inflate_fock, frontier, SCALARS_F8, SCALARS_I, FLAGS, |
| ATOM_1D, ATOM_SHELL, PAIRS) |
|
|
| M5250 = "/global/cfs/projectdirs/m5250/OMol_elec" |
|
|
|
|
| def shard_paths(root, side): |
| out = [] |
| for depth in ("*", "*/*/*"): |
| out += glob.glob(os.path.join(root, side, depth, "*.zarr")) |
| return sorted(set(out)) |
|
|
|
|
| def close(a, b, tol=0.0): |
| if a is None or b is None: |
| return a is None and b is None |
| a = np.asarray(a, dtype=float) |
| b = np.asarray(b, dtype=float) |
| if a.shape != b.shape: |
| return False |
| return bool(np.all((np.isnan(a) & np.isnan(b)) | (np.abs(a - b) <= tol))) |
|
|
|
|
| def main(): |
| ap = argparse.ArgumentParser() |
| ap.add_argument("--store", required=True) |
| ap.add_argument("--sample", type=int, default=30) |
| ap.add_argument("--seed", type=int, default=7) |
| args = ap.parse_args() |
|
|
| p1 = shard_paths(args.store, "p1") |
| p2 = shard_paths(args.store, "p2") |
| print(f"shards: p1 {len(p1)}, p2 {len(p2)}") |
| if len(p1) != len(p2): |
| print(" ERROR: p1 and p2 shard counts differ") |
|
|
| total = 0 |
| problems = [] |
| nbas_all, natm_all, uhf_n, nofock = [], [], 0, 0 |
| for sp in p2: |
| g = zarr.open_group(sp, mode="r") |
| n = int(g.attrs["n_calc"]) |
| total += n |
| si = np.asarray(g["scalar_i"]) |
| icol = {k: j for j, k in enumerate(g.attrs["scalar_i_cols"])} |
| nbas = si[:, icol["nbas"]] |
| natm = si[:, icol["n_atoms"]] |
| nbas_all.append(nbas) |
| natm_all.append(natm) |
| for nm, key in (("atom", "atom_offsets"), ("fock_a", "fock_a_offsets"), |
| ("fock_b", "fock_b_offsets"), ("eps_a", "eps_a_offsets")): |
| o = np.asarray(g[key]) |
| if len(o) != n + 1 or o[0] != 0 or np.any(np.diff(o) < 0): |
| problems.append(f"{os.path.basename(sp)}: bad {nm} offsets") |
| if not np.array_equal(np.diff(np.asarray(g["atom_offsets"])), natm): |
| problems.append(f"{os.path.basename(sp)}: atom offsets != n_atoms") |
| want = nbas.astype("i8") * (nbas.astype("i8") + 1) // 2 |
| flags = np.asarray(g["flags"]) |
| fcol = {k: j for j, k in enumerate(g.attrs["flag_cols"])} |
| is_uhf = flags[:, fcol["is_uhf"]].astype(bool) |
| has_f = flags[:, fcol["has_fock"]].astype(bool) |
| uhf_n += int(is_uhf.sum()) |
| nofock += int((~has_f).sum()) |
| fa = np.diff(np.asarray(g["fock_a_offsets"])) |
| if not np.array_equal(fa[has_f], want[has_f]): |
| problems.append(f"{os.path.basename(sp)}: fock_a length != nbas(nbas+1)/2") |
| if np.any(fa[~has_f] != 0): |
| problems.append(f"{os.path.basename(sp)}: fock_a stored where has_fock is false") |
| fb = np.diff(np.asarray(g["fock_b_offsets"])) |
| if np.any((fb > 0) != (is_uhf & has_f)): |
| problems.append(f"{os.path.basename(sp)}: beta Fock presence != is_uhf & has_fock") |
| if not np.array_equal(fb[is_uhf & has_f], want[is_uhf & has_f]): |
| problems.append(f"{os.path.basename(sp)}: beta Fock length wrong") |
| if len(g.attrs["calc_id"]) != n or len(g.attrs["rel_path"]) != n: |
| problems.append(f"{os.path.basename(sp)}: attrs length != n_calc") |
| if np.any(~np.asarray(g["flags"])[:, fcol["terminated_normally"]].astype(bool)): |
| problems.append(f"{os.path.basename(sp)}: some runs not terminated normally") |
| nbas_all = np.concatenate(nbas_all) if nbas_all else np.zeros(0) |
| natm_all = np.concatenate(natm_all) if natm_all else np.zeros(0) |
| print(f"calculations: {total:,} ({uhf_n:,} UHF, {total-uhf_n:,} RHF, " |
| f"{nofock:,} without a Fock matrix)") |
| if total: |
| print(f" nbas min {nbas_all.min()} median {int(np.median(nbas_all))} max {nbas_all.max()}") |
| print(f" atoms min {natm_all.min()} median {int(np.median(natm_all))} max {natm_all.max()}") |
| for p in problems[:12]: |
| print(" STRUCT ERROR:", p) |
| if not problems: |
| print(" structural checks passed on every shard") |
|
|
| rng = random.Random(args.seed) |
| picks = [] |
| for sp in p2: |
| g = zarr.open_group(sp, mode="r") |
| n = int(g.attrs["n_calc"]) |
| if n: |
| picks.append((sp, rng.randrange(n))) |
| rng.shuffle(picks) |
| picks = picks[:args.sample] |
| print(f"\nre-parse comparison on {len(picks)} calculations:") |
|
|
| nbad = 0 |
| for sp, i in picks: |
| g = zarr.open_group(sp, mode="r") |
| stored = read_calc(g, i) |
| rel = stored["rel_path"] |
| fresh = parse_archive(os.path.join(M5250, rel, "orca.tar.zst")) |
| fresh["n_atoms"] = len(fresh["elements"]) |
| fresh["homo_a"], fresh["lumo_a"], fresh["gap_a"] = frontier(fresh["eps_a"], fresh["occ_a"]) |
| fresh["homo_b"], fresh["lumo_b"], fresh["gap_b"] = frontier(fresh["eps_b"], fresh["occ_b"]) |
| bad = [] |
| for k in SCALARS_F8: |
| a, b = stored[k], fresh.get(k) |
| if b is None: |
| continue |
| if not close(a, b, tol=abs(float(b)) * 1e-12 + 1e-12): |
| bad.append(f"{k}: {a} vs {b}") |
| for k in SCALARS_I: |
| b = fresh.get(k) |
| if b is not None and stored[k] != int(b): |
| bad.append(f"{k}: {stored[k]} vs {b}") |
| for k in ("scf_converged", "terminated_normally", "nbo_available", "npa_available"): |
| if stored[k] != bool(fresh.get(k)): |
| bad.append(f"{k}: {stored[k]} vs {fresh.get(k)}") |
| if stored["is_uhf"] != (fresh["hftyp"] == "UHF"): |
| bad.append("is_uhf mismatch") |
| if not close(stored["coords"], fresh["coords"], 1e-9): |
| bad.append("coords differ") |
| if fresh["forces"] is not None and not close(stored["forces"], fresh["forces"], 1e-12): |
| bad.append("forces differ") |
| if fresh["atomic_numbers"] is not None and not np.array_equal( |
| stored["atomic_numbers"], fresh["atomic_numbers"]): |
| bad.append("atomic numbers differ") |
| for k in ATOM_1D: |
| if fresh.get(k) is not None and not close(stored[k], fresh[k], 1e-9): |
| bad.append(f"{k} differs") |
| for k in ATOM_SHELL: |
| if fresh.get(k) is not None and not close(stored[k], fresh[k], 2e-5): |
| bad.append(f"{k} differs") |
| for k in PAIRS: |
| idx, val = stored[k] |
| ref = fresh.get(k) or [] |
| if len(val) != len(ref): |
| bad.append(f"{k} count {len(val)} vs {len(ref)}") |
| elif ref: |
| if (not np.array_equal(idx, np.array([[a, b] for a, b, _ in ref])) |
| or not close(val, np.array([v for _, _, v in ref]), 1e-4)): |
| bad.append(f"{k} content differs") |
| if fresh["fock_a"] is None: |
| if stored["has_fock"] or len(stored["fock_a"]): |
| bad.append("fock_a stored but source has none") |
| elif not np.array_equal(stored["fock_a"], fresh["fock_a"]): |
| bad.append("fock_a differs") |
| fb = fresh.get("fock_b") |
| if fb is None: |
| if len(stored["fock_b"]): |
| bad.append("fock_b present but should be absent") |
| elif not np.array_equal(stored["fock_b"], fb): |
| bad.append("fock_b differs") |
| if not close(stored["eps_a"], fresh["eps_a"], 1e-12): |
| bad.append("eps_a differs") |
| if stored["has_fock"]: |
| F = inflate_fock(stored["fock_a"], stored["nbas"]) |
| if np.abs(F - F.T).max() != 0: |
| bad.append("inflated Fock not symmetric") |
| if bad: |
| nbad += 1 |
| print(f" MISMATCH {rel}") |
| for b in bad[:6]: |
| print(f" {b}") |
| else: |
| print(f" ok {rel[:76]}") |
| print(f"\n{len(picks)-nbad}/{len(picks)} exact, {len(problems)} structural problems") |
|
|
| def du(p): |
| return sum(os.path.getsize(os.path.join(r, f)) for r, _, fs in os.walk(p) for f in fs) |
| b1 = sum(du(p) for p in p1) |
| b2 = sum(du(p) for p in p2) |
| print(f"\nsize: p1 {b1/1e9:.3f} GB ({b1/max(total,1)/1e3:.1f} kB/calc), " |
| f"p2 {b2/1e9:.3f} GB ({b2/max(total,1)/1e6:.3f} MB/calc)") |
| if total: |
| print(f"extrapolated to 3.73 M: p1 {b1/total*3.73e6/1e12:.2f} TB, " |
| f"p2 {b2/total*3.73e6/1e12:.2f} TB") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|