from __future__ import annotations import csv import glob import os import sys CONTACT_AREA = 1.0e-2 MIN_MASTER_FACETS = 4 STATE_COLUMNS = [ "open_count", "closed_stick_count", "closed_slip_count", "closed_restick_count", "separating_count", "state_failed_projection_count", ] def require_columns(row: dict[str, str], columns: list[str]) -> bool: missing = [column for column in columns if column not in row] if missing: print("missing contact diagnostic columns: " + ", ".join(missing)) return False return True def main() -> int: result_dir = sys.argv[1] if len(sys.argv) > 1 else "results" pair_rows: list[dict[str, str]] = [] for path in sorted(glob.glob(os.path.join(result_dir, "contact_*.csv"))): with open(path, newline="") as stream: pair_rows.extend(row for row in csv.DictReader(stream) if row["kind"] == "PAIR") if not pair_rows: print("no contact pair rows found") return 1 if len(pair_rows) < 2: print("contact diagnostic path is too short") return 1 if not require_columns( pair_rows[0], STATE_COLUMNS + [ "gap_residual", "gap_mean", "gap_l2", "failed_projection_count", "active_set_flip_count", "aug_factor", "aug_lambda", ], ): return 1 profile_rows: list[dict[str, float]] = [] for index, row in enumerate(pair_rows): force_norm = float(row["force_norm"]) gap = float(row["gap_residual"]) gap_mean = float(row["gap_mean"]) gap_l2 = float(row["gap_l2"]) failed_projection = int(float(row["failed_projection_count"])) active_set_flip = int(float(row["active_set_flip_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"]) 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 = int(float(row["state_failed_projection_count"])) profile_rows.append( { "sample": float(index), "active_count": float(row["active_count"]), "status": float(status), "activation_count": float(activation_count), "release_count": float(release_count), "pressure": force_norm / CONTACT_AREA, "max_gap": gap, "gap_mean": gap_mean, "gap_l2": gap_l2, "failed_projection_count": float(failed_projection), "active_set_flip_count": float(active_set_flip), "open_count": float(open_count), "closed_stick_count": float(closed_stick_count), "closed_slip_count": float(closed_slip_count), "closed_restick_count": float(closed_restick_count), "separating_count": float(separating_count), "state_failed_projection_count": float(state_failed_projection), "weighted_gap": gap * CONTACT_AREA, "multiplier_residual": abs(gap), "aug_factor": aug_factor, "aug_lambda": aug_lambda, "search_masters": float(row.get("search_masters", "0") or "0"), } ) profile_path = os.path.join(result_dir, "pressure_profile.csv") with open(profile_path, "w", newline="") as stream: writer = csv.DictWriter( stream, fieldnames=[ "sample", "active_count", "status", "activation_count", "release_count", "pressure", "max_gap", "gap_mean", "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", "weighted_gap", "multiplier_residual", "aug_factor", "aug_lambda", "search_masters", ], ) writer.writeheader() writer.writerows(profile_rows) max_active = max(int(row["active_count"]) for row in profile_rows) max_pressure = max(row["pressure"] for row in profile_rows) max_gap = max(row["max_gap"] for row in profile_rows) max_gap_l2 = max(row["gap_l2"] for row in profile_rows) max_failed_projection = max(int(row["failed_projection_count"]) for row in profile_rows) max_state_failed_projection = max( int(row["state_failed_projection_count"]) for row in profile_rows ) max_closed_state = max( int(row["closed_stick_count"]) + int(row["closed_slip_count"]) + int(row["closed_restick_count"]) for row in profile_rows ) max_activation_count = max(int(row["activation_count"]) for row in profile_rows) max_release_count = max(int(row["release_count"]) for row in profile_rows) max_weighted_gap = max(row["weighted_gap"] for row in profile_rows) max_multiplier_residual = max(row["multiplier_residual"] for row in profile_rows) max_aug_factor = max(row["aug_factor"] for row in profile_rows) max_lambda = max(row["aug_lambda"] for row in profile_rows) max_masters = max(int(row["search_masters"]) for row in profile_rows) positive_pressure_steps = sum(1 for row in profile_rows if row["pressure"] > 0.0) if max_active <= 0 or max_pressure <= 0.0 or max_gap <= 0.0: print("face-to-face contact did not activate") return 1 if max_masters < MIN_MASTER_FACETS: print("nonmatching master surface did not expose four contact facets") return 1 if max_gap_l2 < max_gap: print("gap L2 diagnostic is inconsistent with max gap") return 1 if max_failed_projection != 0: print(f"unexpected failed projection count: {max_failed_projection}") return 1 if max_state_failed_projection != 0: print(f"unexpected failed projection state: {max_state_failed_projection}") return 1 if max_closed_state <= 0: print("closed contact state counters were not reported") return 1 if any( int(row["open_count"]) + int(row["closed_stick_count"]) + int(row["closed_slip_count"]) + int(row["closed_restick_count"]) + int(row["separating_count"]) + int(row["state_failed_projection_count"]) != 1 for row in profile_rows ): print("contact state counters do not describe exactly one pair state") return 1 if any( int(row["active_set_flip_count"]) < int(row["activation_count"]) + int(row["release_count"]) for row in profile_rows ): print("active-set flip diagnostic is inconsistent") return 1 if max_aug_factor <= 0.0: print("augmentation factor was not reported") return 1 if positive_pressure_steps < 3: print("pressure profile is too short for a benchmark check") return 1 if max_lambda <= 0.0: print("augmented contact multiplier did not grow") return 1 if not (1.0e4 <= max_pressure <= 1.0e9): print(f"contact pressure out of expected range: {max_pressure:.6e}") return 1 if not (1.0e-7 <= max_weighted_gap <= 1.0e-3): print(f"weighted gap out of expected range: {max_weighted_gap:.6e}") return 1 if max_multiplier_residual > 5.0e-3: print(f"multiplier residual out of expected range: {max_multiplier_residual:.6e}") 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_pressure={:.6e}, max_gap={:.6e}, " "max_weighted_gap={:.6e}, max_multiplier_residual={:.6e}, " "max_aug_lambda={:.6e}, max_masters={}".format( max_active, max_pressure, max_gap, max_weighted_gap, max_multiplier_residual, max_lambda, max_masters, ) ) print(f"wrote {profile_path}") return 0 if __name__ == "__main__": raise SystemExit(main())