from __future__ import annotations import csv import glob import os import sys sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) from contact_schema import read_pair_rows, require_pair_schema 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 if len(pair_rows) < 2: print("shell-shell contact diagnostic path is too short") return 1 if not require_pair_schema(pair_rows): 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", "gap_l2", "failed_projection_count", "active_set_flip_count", "open_count", "closed_stick_count", "closed_slip_count", "closed_restick_count", "separating_count", "state_failed_projection_count", "status", "activation_count", "release_count", "aug_factor", "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"]), "gap_l2": float(row["gap_l2"]), "failed_projection_count": int(float(row["failed_projection_count"])), "active_set_flip_count": int(float(row["active_set_flip_count"])), "open_count": int(float(row["open_count"])), "closed_stick_count": int(float(row["closed_stick_count"])), "closed_slip_count": int(float(row["closed_slip_count"])), "closed_restick_count": int(float(row["closed_restick_count"])), "separating_count": int(float(row["separating_count"])), "state_failed_projection_count": int( float(row["state_failed_projection_count"]) ), "status": int(float(row["status"])), "activation_count": int(float(row["activation_count"])), "release_count": int(float(row["release_count"])), "aug_factor": float(row["aug_factor"]), "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_gap_l2 = max(float(row["gap_l2"]) for row in pair_rows) max_failed_projection = max(int(float(row["failed_projection_count"])) for row in pair_rows) max_state_failed_projection = max( int(float(row["state_failed_projection_count"])) for row in pair_rows ) max_closed_state = max( int(float(row["closed_stick_count"])) + int(float(row["closed_slip_count"])) + int(float(row["closed_restick_count"])) for row in pair_rows ) max_aug_factor = max(float(row["aug_factor"]) 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_gap_l2 < max_gap: print("shell-shell gap L2 diagnostic is inconsistent") return 1 if max_failed_projection != 0: print(f"unexpected shell-shell failed projection count: {max_failed_projection}") return 1 if max_state_failed_projection != 0: print(f"unexpected shell-shell failed projection state: {max_state_failed_projection}") return 1 if max_closed_state <= 0: print("shell-shell closed state counters were not reported") return 1 if any( int(float(row["open_count"])) + int(float(row["closed_stick_count"])) + int(float(row["closed_slip_count"])) + int(float(row["closed_restick_count"])) + int(float(row["separating_count"])) + int(float(row["state_failed_projection_count"])) != 1 for row in pair_rows ): print("shell-shell state counters do not describe exactly one pair state") return 1 if any( int(float(row["active_set_flip_count"])) < int(float(row["activation_count"])) + int(float(row["release_count"])) for row in pair_rows ): print("shell-shell active-set flip diagnostic is inconsistent") return 1 if max_aug_factor <= 0.0 or 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())