Files
FemLite/examples/shell_shell_contact/check_contact_results.py
T
shenw 2b7962eb96
FemLite CI / build-test (push) Has been cancelled
Release FemLite 0.5.3
2026-07-01 18:39:18 +08:00

109 lines
4.1 KiB
Python

from __future__ import annotations
import csv
import glob
import os
import sys
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 main() -> int:
result_dir = sys.argv[1] if len(sys.argv) > 1 else "results"
pair_rows = read_pair_rows(result_dir)
if not pair_rows:
print("no contact pair rows found")
return 1
profile_path = os.path.join(result_dir, "shell_shell_contact_profile.csv")
with open(profile_path, "w", newline="") as stream:
fieldnames = [
"sample",
"active_count",
"force_norm",
"gap_residual",
"gap_l2",
"failed_projection_count",
"active_set_flip_count",
"status",
"activation_count",
"release_count",
"aug_factor",
"aug_lambda",
"search_masters",
]
writer = csv.DictWriter(stream, fieldnames=fieldnames)
writer.writeheader()
for index, row in enumerate(pair_rows):
writer.writerow(
{
"sample": index,
"active_count": int(float(row["active_count"])),
"force_norm": float(row["force_norm"]),
"gap_residual": float(row["gap_residual"]),
"gap_l2": float(row["gap_l2"]),
"failed_projection_count": int(float(row["failed_projection_count"])),
"active_set_flip_count": 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"]),
"search_masters": int(float(row.get("search_masters", "0") or "0")),
}
)
max_active = max(int(float(row["active_count"])) for row in pair_rows)
max_force = max(float(row["force_norm"]) for row in pair_rows)
max_gap = max(float(row["gap_residual"]) for row in pair_rows)
max_gap_l2 = max(float(row["gap_l2"]) for row in pair_rows)
max_failed_projection = max(int(float(row["failed_projection_count"])) for row in pair_rows)
max_aug_factor = max(float(row["aug_factor"]) for row in pair_rows)
max_lambda = max(float(row["aug_lambda"]) for row in pair_rows)
max_masters = max(int(float(row.get("search_masters", "0") or "0")) for row in pair_rows)
if max_active <= 0 or max_force <= 0.0 or max_gap <= 0.0:
print("shell-shell contact did not activate")
return 1
if max_masters <= 0:
print("shell-shell contact did not search a master shell facet")
return 1
if max_gap_l2 < max_gap:
print("shell-shell gap L2 diagnostic is inconsistent")
return 1
if max_failed_projection != 0:
print(f"unexpected shell-shell failed projection count: {max_failed_projection}")
return 1
if any(
int(float(row["active_set_flip_count"]))
< int(float(row["activation_count"])) + int(float(row["release_count"]))
for row in pair_rows
):
print("shell-shell active-set flip diagnostic is inconsistent")
return 1
if max_aug_factor <= 0.0 or max_lambda <= 0.0:
print("shell-shell augmented multiplier did not grow")
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_force={:.6e}, max_gap={:.6e}, "
"max_aug_lambda={:.6e}, max_masters={}".format(
max_active, max_force, max_gap, max_lambda, max_masters
)
)
print(f"wrote {profile_path}")
return 0
if __name__ == "__main__":
raise SystemExit(main())