Add CCLite 0.2.8 verification cases
This commit is contained in:
@@ -0,0 +1,281 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import math
|
||||
from collections import Counter
|
||||
from pathlib import Path
|
||||
|
||||
from common_csv import (
|
||||
add_failure,
|
||||
as_float,
|
||||
as_int,
|
||||
read_candidate_report,
|
||||
read_contact_report,
|
||||
read_force_balance,
|
||||
read_ipc_report,
|
||||
write_verification_report,
|
||||
)
|
||||
|
||||
|
||||
def build_report(args: argparse.Namespace) -> dict:
|
||||
output_dir = Path(args.output_dir)
|
||||
input_path = Path(args.input)
|
||||
failed: list[str] = []
|
||||
metrics: dict[str, int | float] = {}
|
||||
log_path = output_dir / "run.log"
|
||||
|
||||
try:
|
||||
_, ipc_rows = read_ipc_report(output_dir)
|
||||
_, contact_rows = read_contact_report(output_dir)
|
||||
_, candidate_rows = read_candidate_report(output_dir)
|
||||
_, force_rows = read_force_balance(output_dir)
|
||||
except AssertionError as exc:
|
||||
ipc_rows = []
|
||||
contact_rows = []
|
||||
candidate_rows = []
|
||||
force_rows = []
|
||||
failed.append(str(exc))
|
||||
|
||||
if not log_path.exists():
|
||||
failed.append(f"missing log file: {log_path}")
|
||||
elif not args.headers_only and "contact_broad_phase" not in log_path.read_text(encoding="utf-8"):
|
||||
failed.append("run.log missing contact_broad_phase diagnostics")
|
||||
|
||||
if args.headers_only:
|
||||
return make_report(args, failed, input_path, output_dir, log_path, metrics)
|
||||
|
||||
expected_steps = set(range(1, args.expected_steps + 1))
|
||||
validate_ipc(ipc_rows, args, failed, metrics)
|
||||
validate_candidates(candidate_rows, args, expected_steps, failed, metrics)
|
||||
validate_contacts(contact_rows, args, expected_steps, failed, metrics)
|
||||
validate_force_balance(force_rows, args, expected_steps, failed, metrics)
|
||||
return make_report(args, failed, input_path, output_dir, log_path, metrics)
|
||||
|
||||
|
||||
def validate_ipc(rows: list[dict[str, str]], args: argparse.Namespace, failed: list[str], metrics: dict) -> None:
|
||||
import_load_rows = 0
|
||||
advance_rows = 0
|
||||
stop_rows = 0
|
||||
for index, row in enumerate(rows, start=1):
|
||||
location = f"ipc_report.csv row {index}"
|
||||
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"{location}: {exc}")
|
||||
continue
|
||||
if command_seq != ack_seq:
|
||||
add_failure(failed, location, "seq/ack", command_seq, ack_seq)
|
||||
if error_code != 0:
|
||||
add_failure(failed, location, "error_code", 0, 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:
|
||||
add_failure(failed, "ipc_report.csv", "IMPORT_LOADS rows", expected_worker_step_rows, import_load_rows)
|
||||
if advance_rows != expected_worker_step_rows:
|
||||
add_failure(failed, "ipc_report.csv", "ADVANCE rows", expected_worker_step_rows, advance_rows)
|
||||
if stop_rows != 2:
|
||||
add_failure(failed, "ipc_report.csv", "STOP rows", 2, stop_rows)
|
||||
metrics["ipc_rows"] = len(rows)
|
||||
metrics["import_load_rows"] = import_load_rows
|
||||
metrics["advance_rows"] = advance_rows
|
||||
|
||||
|
||||
def validate_candidates(
|
||||
rows: list[dict[str, str]],
|
||||
args: argparse.Namespace,
|
||||
expected_steps: set[int],
|
||||
failed: list[str],
|
||||
metrics: dict,
|
||||
) -> None:
|
||||
expected_total = args.expected_steps * args.expected_candidates_per_step
|
||||
if len(rows) != expected_total:
|
||||
add_failure(failed, "candidate_report.csv", "row_count", expected_total, len(rows))
|
||||
|
||||
counts: Counter[int] = Counter()
|
||||
seen: set[tuple[int, int, int]] = set()
|
||||
for index, row in enumerate(rows, start=1):
|
||||
location = f"candidate_report.csv row {index}"
|
||||
try:
|
||||
step_id = as_int(row, "step_id")
|
||||
sph_id = as_int(row, "sph_id")
|
||||
facet_id = as_int(row, "facet_id")
|
||||
except AssertionError as exc:
|
||||
failed.append(f"{location}: {exc}")
|
||||
continue
|
||||
counts[step_id] += 1
|
||||
key = (step_id, sph_id, facet_id)
|
||||
if key in seen:
|
||||
add_failure(failed, location, "duplicate_candidate", "unique step/sph/facet", key)
|
||||
seen.add(key)
|
||||
|
||||
for step_id in expected_steps:
|
||||
actual = counts.get(step_id, 0)
|
||||
if actual != args.expected_candidates_per_step:
|
||||
add_failure(failed, f"candidate_report.csv step {step_id}", "candidate_count",
|
||||
args.expected_candidates_per_step, actual)
|
||||
metrics["candidate_rows"] = len(rows)
|
||||
|
||||
|
||||
def validate_contacts(
|
||||
rows: list[dict[str, str]],
|
||||
args: argparse.Namespace,
|
||||
expected_steps: set[int],
|
||||
failed: list[str],
|
||||
metrics: dict,
|
||||
) -> None:
|
||||
expected_total = args.expected_steps * args.expected_contacts_per_step
|
||||
if len(rows) != expected_total:
|
||||
add_failure(failed, "contact_report.csv", "row_count", expected_total, len(rows))
|
||||
|
||||
counts: Counter[int] = Counter()
|
||||
max_penetration = 0.0
|
||||
force_z_per_contact = args.expected_force_z_per_contact
|
||||
if force_z_per_contact is None and args.expected_contacts_per_step > 0:
|
||||
force_z_per_contact = args.expected_total_force_norm / args.expected_contacts_per_step
|
||||
|
||||
for index, row in enumerate(rows, start=1):
|
||||
location = f"contact_report.csv row {index}"
|
||||
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"{location}: {exc}")
|
||||
continue
|
||||
counts[step_id] += 1
|
||||
max_penetration = max(max_penetration, penetration)
|
||||
expect_close(failed, location, "penetration", args.expected_max_penetration, penetration, args.tolerance)
|
||||
expect_close(failed, location, "normal_z", args.expected_normal_z, normal_z, args.tolerance)
|
||||
if force_z_per_contact is not None:
|
||||
expect_close(failed, location, "force_z", force_z_per_contact, force_z, args.force_tolerance)
|
||||
if node_count != args.expected_node_count:
|
||||
add_failure(failed, location, "node_count", args.expected_node_count, node_count)
|
||||
|
||||
for step_id in expected_steps:
|
||||
actual = counts.get(step_id, 0)
|
||||
if actual != args.expected_contacts_per_step:
|
||||
add_failure(failed, f"contact_report.csv step {step_id}", "contact_count",
|
||||
args.expected_contacts_per_step, actual)
|
||||
metrics["contact_rows"] = len(rows)
|
||||
metrics["max_contact_penetration"] = max_penetration
|
||||
|
||||
|
||||
def validate_force_balance(
|
||||
rows: list[dict[str, str]],
|
||||
args: argparse.Namespace,
|
||||
expected_steps: set[int],
|
||||
failed: list[str],
|
||||
metrics: dict,
|
||||
) -> None:
|
||||
seen_steps: set[int] = set()
|
||||
for index, row in enumerate(rows, start=1):
|
||||
location = f"force_balance.csv row {index}"
|
||||
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")
|
||||
total_force = [as_float(row, key) for key in ("total_fx", "total_fy", "total_fz")]
|
||||
except AssertionError as exc:
|
||||
failed.append(f"{location}: {exc}")
|
||||
continue
|
||||
seen_steps.add(step_id)
|
||||
if num_contacts != args.expected_contacts_per_step:
|
||||
add_failure(failed, location, "num_contacts", args.expected_contacts_per_step, num_contacts)
|
||||
expect_close(failed, location, "max_penetration", args.expected_max_penetration,
|
||||
max_penetration, args.tolerance)
|
||||
expect_close(failed, location, "total_contact_force_norm", args.expected_total_force_norm,
|
||||
total_force_norm, args.force_tolerance)
|
||||
if force_balance_error > args.tolerance:
|
||||
add_failure(failed, location, "force_balance_error", f"<={args.tolerance}", force_balance_error)
|
||||
if moment_balance_error > args.tolerance:
|
||||
add_failure(failed, location, "moment_balance_error", f"<={args.tolerance}", moment_balance_error)
|
||||
if nonfinite_count != 0:
|
||||
add_failure(failed, location, "nonfinite_contact_count", 0, nonfinite_count)
|
||||
for axis, value in zip(("total_fx", "total_fy", "total_fz"), total_force):
|
||||
if abs(value) > args.tolerance:
|
||||
add_failure(failed, location, axis, f"abs <= {args.tolerance}", value)
|
||||
|
||||
if seen_steps != expected_steps:
|
||||
add_failure(failed, "force_balance.csv", "steps", sorted(expected_steps), sorted(seen_steps))
|
||||
metrics["force_rows"] = len(rows)
|
||||
|
||||
|
||||
def expect_close(
|
||||
failed: list[str],
|
||||
location: str,
|
||||
field: str,
|
||||
expected: float,
|
||||
actual: float,
|
||||
tolerance: float,
|
||||
) -> None:
|
||||
if not math.isfinite(actual) or abs(actual - expected) > tolerance:
|
||||
add_failure(failed, location, field, f"{expected} +/- {tolerance}", actual)
|
||||
|
||||
|
||||
def make_report(
|
||||
args: argparse.Namespace,
|
||||
failed: list[str],
|
||||
input_path: Path,
|
||||
output_dir: 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(input_path),
|
||||
"output_files": {
|
||||
"run_log": str(log_path),
|
||||
"ipc_report": str(output_dir / "ipc_report.csv"),
|
||||
"contact_report": str(output_dir / "contact_report.csv"),
|
||||
"candidate_report": str(output_dir / "candidate_report.csv"),
|
||||
"force_balance": str(output_dir / "force_balance.csv"),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
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=1)
|
||||
parser.add_argument("--expected-contacts-per-step", type=int, required=True)
|
||||
parser.add_argument("--expected-candidates-per-step", type=int, required=True)
|
||||
parser.add_argument("--expected-max-penetration", type=float, required=True)
|
||||
parser.add_argument("--expected-total-force-norm", type=float, required=True)
|
||||
parser.add_argument("--expected-force-z-per-contact", type=float)
|
||||
parser.add_argument("--expected-normal-z", type=float, default=1.0)
|
||||
parser.add_argument("--expected-node-count", type=int, default=4)
|
||||
parser.add_argument("--tolerance", type=float, default=1.0e-7)
|
||||
parser.add_argument("--force-tolerance", type=float, default=1.0e-5)
|
||||
parser.add_argument("--headers-only", action="store_true")
|
||||
args = parser.parse_args()
|
||||
|
||||
report = build_report(args)
|
||||
report_path = Path(args.report)
|
||||
write_verification_report(report_path, report)
|
||||
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