Files
2026-07-05 22:48:27 +08:00

59 lines
1.4 KiB
Python

from __future__ import annotations
import csv
import glob
import os
STATE_COLUMNS = [
"open_count",
"closed_stick_count",
"closed_slip_count",
"closed_restick_count",
"separating_count",
"state_failed_projection_count",
]
COMPACT_CONTACT_COLUMNS = STATE_COLUMNS + [
"gap_residual",
"gap_l2",
"failed_projection_count",
"active_set_flip_count",
"aug_factor",
"aug_lambda",
]
FRICTION_CONTACT_COLUMNS = [
"slip_increment_norm",
"slip_residual",
"friction_residual",
]
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 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 require_pair_schema(
rows: list[dict[str, str]],
extra_columns: list[str] | None = None,
) -> bool:
if not rows:
print("no contact pair rows found")
return False
columns = list(COMPACT_CONTACT_COLUMNS)
if extra_columns:
columns.extend(extra_columns)
return require_columns(rows[0], columns)