160 lines
4.9 KiB
Python
160 lines
4.9 KiB
Python
|
|
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",
|
||
|
|
]
|
||
|
|
|
||
|
|
FORCE_HEADERS = [
|
||
|
|
"step_id",
|
||
|
|
"sph_fx",
|
||
|
|
"sph_fy",
|
||
|
|
"sph_fz",
|
||
|
|
"fem_fx",
|
||
|
|
"fem_fy",
|
||
|
|
"fem_fz",
|
||
|
|
"total_fx",
|
||
|
|
"total_fy",
|
||
|
|
"total_fz",
|
||
|
|
]
|
||
|
|
|
||
|
|
|
||
|
|
def build_report(args: argparse.Namespace) -> dict:
|
||
|
|
output_dir = Path(args.output_dir)
|
||
|
|
ipc_path = output_dir / "ipc_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)
|
||
|
|
_, force_rows = read_required_csv(force_path, FORCE_HEADERS)
|
||
|
|
except AssertionError as exc:
|
||
|
|
ipc_rows = []
|
||
|
|
force_rows = []
|
||
|
|
failed.append(str(exc))
|
||
|
|
|
||
|
|
if not log_path.exists():
|
||
|
|
failed.append(f"missing log file: {log_path}")
|
||
|
|
|
||
|
|
if args.headers_only:
|
||
|
|
return make_report(args, failed, metrics, ipc_path, force_path, log_path)
|
||
|
|
|
||
|
|
if not ipc_rows:
|
||
|
|
failed.append("ipc_report.csv has no data rows")
|
||
|
|
if not force_rows:
|
||
|
|
failed.append("force_balance.csv has no data rows")
|
||
|
|
|
||
|
|
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")
|
||
|
|
worker_state = as_int(row, "worker_state")
|
||
|
|
heartbeat_seq = as_int(row, "heartbeat_seq")
|
||
|
|
elapsed_ms = as_float(row, "elapsed_ms")
|
||
|
|
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 elapsed_ms < 0.0:
|
||
|
|
failed.append(f"ipc row {index}: negative elapsed_ms")
|
||
|
|
if heartbeat_seq < 0:
|
||
|
|
failed.append(f"ipc row {index}: negative heartbeat")
|
||
|
|
if command_id == 99:
|
||
|
|
stop_rows += 1
|
||
|
|
if worker_state != 6:
|
||
|
|
failed.append(f"ipc row {index}: STOP did not leave worker STOPPED")
|
||
|
|
|
||
|
|
if stop_rows != 2:
|
||
|
|
failed.append(f"expected two STOP rows, got {stop_rows}")
|
||
|
|
|
||
|
|
seen_steps: set[int] = set()
|
||
|
|
for index, row in enumerate(force_rows, start=1):
|
||
|
|
try:
|
||
|
|
step_id = as_int(row, "step_id")
|
||
|
|
values = [as_float(row, key) for key in FORCE_HEADERS if key != "step_id"]
|
||
|
|
except AssertionError as exc:
|
||
|
|
failed.append(f"force row {index}: {exc}")
|
||
|
|
continue
|
||
|
|
seen_steps.add(step_id)
|
||
|
|
for key, value in zip(FORCE_HEADERS[1:], values):
|
||
|
|
if abs(value) > 1.0e-12:
|
||
|
|
failed.append(f"force row {index}: {key} expected 0, got {value}")
|
||
|
|
|
||
|
|
expected_steps = set(range(1, args.expected_steps + 1))
|
||
|
|
if seen_steps != expected_steps:
|
||
|
|
failed.append(f"force steps mismatch: expected 1..{args.expected_steps}, got {sorted(seen_steps)}")
|
||
|
|
|
||
|
|
metrics["ipc_rows"] = len(ipc_rows)
|
||
|
|
metrics["force_rows"] = len(force_rows)
|
||
|
|
metrics["stop_rows"] = stop_rows
|
||
|
|
metrics["expected_steps"] = args.expected_steps
|
||
|
|
return make_report(args, failed, metrics, ipc_path, force_path, log_path)
|
||
|
|
|
||
|
|
|
||
|
|
def make_report(
|
||
|
|
args: argparse.Namespace,
|
||
|
|
failed: list[str],
|
||
|
|
metrics: dict,
|
||
|
|
ipc_path: Path,
|
||
|
|
force_path: Path,
|
||
|
|
log_path: Path,
|
||
|
|
) -> 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),
|
||
|
|
"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=100)
|
||
|
|
parser.add_argument("--headers-only", action="store_true")
|
||
|
|
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())
|