from __future__ import annotations import csv import glob import os import sys def read_pair_rows(result_dir: str) -> list[dict[str, str]]: rows: list[dict[str, str]] = [] for path in sorted(glob.glob(os.path.join(result_dir, "contact_*.csv"))): with open(path, newline="") as stream: rows.extend(row for row in csv.DictReader(stream) if row["kind"] == "PAIR") return rows def main() -> int: result_dir = sys.argv[1] if len(sys.argv) > 1 else "results" pair_rows = read_pair_rows(result_dir) if not pair_rows: print("no contact pair rows found") return 1 profile_path = os.path.join(result_dir, "shell_shell_contact_profile.csv") with open(profile_path, "w", newline="") as stream: fieldnames = [ "sample", "active_count", "force_norm", "gap_residual", "aug_lambda", "search_masters", ] writer = csv.DictWriter(stream, fieldnames=fieldnames) writer.writeheader() for index, row in enumerate(pair_rows): writer.writerow( { "sample": index, "active_count": int(float(row["active_count"])), "force_norm": float(row["force_norm"]), "gap_residual": float(row["gap_residual"]), "aug_lambda": float(row["aug_lambda"]), "search_masters": int(float(row.get("search_masters", "0") or "0")), } ) max_active = max(int(float(row["active_count"])) for row in pair_rows) max_force = max(float(row["force_norm"]) for row in pair_rows) max_gap = max(float(row["gap_residual"]) for row in pair_rows) max_lambda = max(float(row["aug_lambda"]) for row in pair_rows) max_masters = max(int(float(row.get("search_masters", "0") or "0")) for row in pair_rows) if max_active <= 0 or max_force <= 0.0 or max_gap <= 0.0: print("shell-shell contact did not activate") return 1 if max_masters <= 0: print("shell-shell contact did not search a master shell facet") return 1 if max_lambda <= 0.0: print("shell-shell augmented multiplier did not grow") return 1 if not glob.glob(os.path.join(result_dir, "result_*.vtk")): print("no VTK output found") return 1 print( "ok: max_active={}, max_force={:.6e}, max_gap={:.6e}, " "max_aug_lambda={:.6e}, max_masters={}".format( max_active, max_force, max_gap, max_lambda, max_masters ) ) print(f"wrote {profile_path}") return 0 if __name__ == "__main__": raise SystemExit(main())