Add CCLite 0.2.7 contact controller diagnostics
This commit is contained in:
@@ -0,0 +1,225 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
from common_csv import as_float, as_int, read_required_csv
|
||||
|
||||
|
||||
IPC_HEADERS = [
|
||||
"step_id",
|
||||
"solver_id",
|
||||
"command_id",
|
||||
"command_seq",
|
||||
"ack_seq",
|
||||
"worker_state",
|
||||
"heartbeat_seq",
|
||||
"elapsed_ms",
|
||||
"error_code",
|
||||
]
|
||||
|
||||
CONTACT_HEADERS = [
|
||||
"step_id",
|
||||
"pair_id",
|
||||
"sph_id",
|
||||
"facet_id",
|
||||
"gap",
|
||||
"penetration",
|
||||
"normal_x",
|
||||
"normal_y",
|
||||
"normal_z",
|
||||
"force_x",
|
||||
"force_y",
|
||||
"force_z",
|
||||
"node_count",
|
||||
]
|
||||
|
||||
FORCE_HEADERS = [
|
||||
"step_id",
|
||||
"sph_fx",
|
||||
"sph_fy",
|
||||
"sph_fz",
|
||||
"fem_fx",
|
||||
"fem_fy",
|
||||
"fem_fz",
|
||||
"total_fx",
|
||||
"total_fy",
|
||||
"total_fz",
|
||||
"num_contacts",
|
||||
"max_penetration",
|
||||
"total_contact_force_norm",
|
||||
"force_balance_error",
|
||||
"moment_balance_error",
|
||||
"nonfinite_contact_count",
|
||||
"skipped_separated_count",
|
||||
"skipped_degenerate_count",
|
||||
"skipped_invalid_count",
|
||||
]
|
||||
|
||||
|
||||
def build_report(args: argparse.Namespace) -> dict:
|
||||
output_dir = Path(args.output_dir)
|
||||
ipc_path = output_dir / "ipc_report.csv"
|
||||
contact_path = output_dir / "contact_report.csv"
|
||||
force_path = output_dir / "force_balance.csv"
|
||||
log_path = output_dir / "run.log"
|
||||
failed: list[str] = []
|
||||
metrics: dict[str, int | float] = {}
|
||||
|
||||
try:
|
||||
_, ipc_rows = read_required_csv(ipc_path, IPC_HEADERS)
|
||||
_, contact_rows = read_required_csv(contact_path, CONTACT_HEADERS)
|
||||
_, force_rows = read_required_csv(force_path, FORCE_HEADERS)
|
||||
except AssertionError as exc:
|
||||
ipc_rows = []
|
||||
contact_rows = []
|
||||
force_rows = []
|
||||
failed.append(str(exc))
|
||||
|
||||
if not log_path.exists():
|
||||
failed.append(f"missing log file: {log_path}")
|
||||
elif "contact_narrow_force" not in log_path.read_text(encoding="utf-8"):
|
||||
failed.append("run.log missing contact_narrow_force diagnostics")
|
||||
|
||||
import_load_rows = 0
|
||||
advance_rows = 0
|
||||
stop_rows = 0
|
||||
for index, row in enumerate(ipc_rows, start=1):
|
||||
try:
|
||||
command_seq = as_int(row, "command_seq")
|
||||
ack_seq = as_int(row, "ack_seq")
|
||||
command_id = as_int(row, "command_id")
|
||||
error_code = as_int(row, "error_code")
|
||||
except AssertionError as exc:
|
||||
failed.append(f"ipc row {index}: {exc}")
|
||||
continue
|
||||
if command_seq != ack_seq:
|
||||
failed.append(f"ipc row {index}: seq/ack mismatch {command_seq}/{ack_seq}")
|
||||
if error_code != 0:
|
||||
failed.append(f"ipc row {index}: nonzero error_code {error_code}")
|
||||
if command_id == 5:
|
||||
import_load_rows += 1
|
||||
if command_id == 6:
|
||||
advance_rows += 1
|
||||
if command_id == 99:
|
||||
stop_rows += 1
|
||||
|
||||
expected_worker_step_rows = args.expected_steps * 2
|
||||
if import_load_rows != expected_worker_step_rows:
|
||||
failed.append(f"expected {expected_worker_step_rows} IMPORT_LOADS rows, got {import_load_rows}")
|
||||
if advance_rows != expected_worker_step_rows:
|
||||
failed.append(f"expected {expected_worker_step_rows} ADVANCE rows, got {advance_rows}")
|
||||
if stop_rows != 2:
|
||||
failed.append(f"expected two STOP rows, got {stop_rows}")
|
||||
|
||||
contact_steps: set[int] = set()
|
||||
for index, row in enumerate(contact_rows, start=1):
|
||||
try:
|
||||
step_id = as_int(row, "step_id")
|
||||
penetration = as_float(row, "penetration")
|
||||
normal_z = as_float(row, "normal_z")
|
||||
force_z = as_float(row, "force_z")
|
||||
node_count = as_int(row, "node_count")
|
||||
except AssertionError as exc:
|
||||
failed.append(f"contact row {index}: {exc}")
|
||||
continue
|
||||
contact_steps.add(step_id)
|
||||
if node_count != 4:
|
||||
failed.append(f"contact row {index}: node_count expected 4, got {node_count}")
|
||||
if penetration <= 0.0:
|
||||
failed.append(f"contact row {index}: penetration should be positive")
|
||||
if abs(normal_z - 1.0) > 1.0e-12:
|
||||
failed.append(f"contact row {index}: normal_z expected 1")
|
||||
if force_z <= 0.0:
|
||||
failed.append(f"contact row {index}: force_z should be positive")
|
||||
|
||||
expected_steps = set(range(1, args.expected_steps + 1))
|
||||
if contact_steps != expected_steps:
|
||||
failed.append(f"contact steps mismatch: expected {sorted(expected_steps)}, got {sorted(contact_steps)}")
|
||||
|
||||
force_steps: set[int] = set()
|
||||
for index, row in enumerate(force_rows, start=1):
|
||||
try:
|
||||
step_id = as_int(row, "step_id")
|
||||
num_contacts = as_int(row, "num_contacts")
|
||||
max_penetration = as_float(row, "max_penetration")
|
||||
total_force_norm = as_float(row, "total_contact_force_norm")
|
||||
force_balance_error = as_float(row, "force_balance_error")
|
||||
moment_balance_error = as_float(row, "moment_balance_error")
|
||||
nonfinite_count = as_int(row, "nonfinite_contact_count")
|
||||
totals = [as_float(row, key) for key in ("total_fx", "total_fy", "total_fz")]
|
||||
except AssertionError as exc:
|
||||
failed.append(f"force row {index}: {exc}")
|
||||
continue
|
||||
force_steps.add(step_id)
|
||||
if num_contacts != 1:
|
||||
failed.append(f"force row {index}: num_contacts expected 1, got {num_contacts}")
|
||||
if max_penetration <= 0.0:
|
||||
failed.append(f"force row {index}: max_penetration should be positive")
|
||||
if total_force_norm <= 0.0:
|
||||
failed.append(f"force row {index}: total_contact_force_norm should be positive")
|
||||
if force_balance_error > args.tolerance:
|
||||
failed.append(f"force row {index}: force_balance_error {force_balance_error}")
|
||||
if moment_balance_error > args.tolerance:
|
||||
failed.append(f"force row {index}: moment_balance_error {moment_balance_error}")
|
||||
if nonfinite_count != 0:
|
||||
failed.append(f"force row {index}: nonfinite_contact_count {nonfinite_count}")
|
||||
if max(abs(value) for value in totals) > args.tolerance:
|
||||
failed.append(f"force row {index}: total force columns are not balanced")
|
||||
|
||||
if force_steps != expected_steps:
|
||||
failed.append(f"force steps mismatch: expected {sorted(expected_steps)}, got {sorted(force_steps)}")
|
||||
|
||||
metrics["ipc_rows"] = len(ipc_rows)
|
||||
metrics["contact_rows"] = len(contact_rows)
|
||||
metrics["force_rows"] = len(force_rows)
|
||||
metrics["import_load_rows"] = import_load_rows
|
||||
metrics["advance_rows"] = advance_rows
|
||||
return make_report(args, failed, ipc_path, contact_path, force_path, log_path, metrics)
|
||||
|
||||
|
||||
def make_report(
|
||||
args: argparse.Namespace,
|
||||
failed: list[str],
|
||||
ipc_path: Path,
|
||||
contact_path: Path,
|
||||
force_path: Path,
|
||||
log_path: Path,
|
||||
metrics: dict,
|
||||
) -> dict:
|
||||
return {
|
||||
"case_id": args.case_id,
|
||||
"status": "PASS" if not failed else "FAIL",
|
||||
"metrics": metrics,
|
||||
"failed_checks": failed,
|
||||
"input": str(Path(args.input)),
|
||||
"output_files": {
|
||||
"run_log": str(log_path),
|
||||
"ipc_report": str(ipc_path),
|
||||
"contact_report": str(contact_path),
|
||||
"force_balance": str(force_path),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--case-id", required=True)
|
||||
parser.add_argument("--input", required=True)
|
||||
parser.add_argument("--output-dir", required=True)
|
||||
parser.add_argument("--report", required=True)
|
||||
parser.add_argument("--expected-steps", type=int, default=2)
|
||||
parser.add_argument("--tolerance", type=float, default=1.0e-7)
|
||||
args = parser.parse_args()
|
||||
|
||||
report = build_report(args)
|
||||
report_path = Path(args.report)
|
||||
report_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
report_path.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
||||
print(f"{report['case_id']} {report['status']} -> {report_path}")
|
||||
return 0 if report["status"] == "PASS" else 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user