Files
FemLite/examples/contact_two_blocks_compression/check_contact_results.py
T
2026-06-26 12:54:40 +08:00

112 lines
3.8 KiB
Python

from __future__ import annotations
import csv
import glob
import os
import sys
CONTACT_AREA = 1.0e-2
MIN_MASTER_FACETS = 4
def main() -> int:
result_dir = sys.argv[1] if len(sys.argv) > 1 else "results"
pair_rows: list[dict[str, str]] = []
for path in sorted(glob.glob(os.path.join(result_dir, "contact_*.csv"))):
with open(path, newline="") as stream:
pair_rows.extend(row for row in csv.DictReader(stream) if row["kind"] == "PAIR")
if not pair_rows:
print("no contact pair rows found")
return 1
profile_rows: list[dict[str, float]] = []
for index, row in enumerate(pair_rows):
force_norm = float(row["force_norm"])
gap = float(row["gap_residual"])
aug_lambda = float(row["aug_lambda"])
profile_rows.append(
{
"sample": float(index),
"active_count": float(row["active_count"]),
"pressure": force_norm / CONTACT_AREA,
"max_gap": gap,
"weighted_gap": gap * CONTACT_AREA,
"multiplier_residual": abs(gap),
"aug_lambda": aug_lambda,
"search_masters": float(row.get("search_masters", "0") or "0"),
}
)
profile_path = os.path.join(result_dir, "pressure_profile.csv")
with open(profile_path, "w", newline="") as stream:
writer = csv.DictWriter(
stream,
fieldnames=[
"sample",
"active_count",
"pressure",
"max_gap",
"weighted_gap",
"multiplier_residual",
"aug_lambda",
"search_masters",
],
)
writer.writeheader()
writer.writerows(profile_rows)
max_active = max(int(row["active_count"]) for row in profile_rows)
max_pressure = max(row["pressure"] for row in profile_rows)
max_gap = max(row["max_gap"] for row in profile_rows)
max_weighted_gap = max(row["weighted_gap"] for row in profile_rows)
max_multiplier_residual = max(row["multiplier_residual"] for row in profile_rows)
max_lambda = max(row["aug_lambda"] for row in profile_rows)
max_masters = max(int(row["search_masters"]) for row in profile_rows)
positive_pressure_steps = sum(1 for row in profile_rows if row["pressure"] > 0.0)
if max_active <= 0 or max_pressure <= 0.0 or max_gap <= 0.0:
print("face-to-face contact did not activate")
return 1
if max_masters < MIN_MASTER_FACETS:
print("nonmatching master surface did not expose four contact facets")
return 1
if positive_pressure_steps < 3:
print("pressure profile is too short for a benchmark check")
return 1
if max_lambda <= 0.0:
print("augmented contact multiplier did not grow")
return 1
if not (1.0e4 <= max_pressure <= 1.0e9):
print(f"contact pressure out of expected range: {max_pressure:.6e}")
return 1
if not (1.0e-7 <= max_weighted_gap <= 1.0e-3):
print(f"weighted gap out of expected range: {max_weighted_gap:.6e}")
return 1
if max_multiplier_residual > 5.0e-3:
print(f"multiplier residual out of expected range: {max_multiplier_residual:.6e}")
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_pressure={:.6e}, max_gap={:.6e}, "
"max_weighted_gap={:.6e}, max_multiplier_residual={:.6e}, "
"max_aug_lambda={:.6e}, max_masters={}".format(
max_active,
max_pressure,
max_gap,
max_weighted_gap,
max_multiplier_residual,
max_lambda,
max_masters,
)
)
print(f"wrote {profile_path}")
return 0
if __name__ == "__main__":
raise SystemExit(main())