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

125 lines
4.5 KiB
Python

from __future__ import annotations
import csv
import glob
import os
import sys
MIN_MASTER_FACETS = 4
MIN_ACTIVE_CONTACTS = 2
MIN_ACCUMULATED_SLIP = 1.0e-1
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 write_sliding_profile(result_dir: str, rows: list[dict[str, str]]) -> str:
profile_path = os.path.join(result_dir, "sliding_profile.csv")
fieldnames = [
"sample",
"active_count",
"stick_count",
"slip_count",
"accumulated_slip",
"friction_work_total",
"tangential_force",
"gap_residual",
"aug_lambda",
"search_masters",
"search_active",
]
with open(profile_path, "w", newline="") as stream:
writer = csv.DictWriter(stream, fieldnames=fieldnames)
writer.writeheader()
for index, row in enumerate(rows):
ftx = float(row["ftx"])
fty = float(row["fty"])
ftz = float(row["ftz"])
writer.writerow(
{
"sample": index,
"active_count": int(float(row["active_count"])),
"stick_count": int(float(row["stick_count"])),
"slip_count": int(float(row["slip_count"])),
"accumulated_slip": float(row["accumulated_slip"]),
"friction_work_total": float(row["friction_work_total"]),
"tangential_force": (ftx * ftx + fty * fty + ftz * ftz) ** 0.5,
"gap_residual": float(row["gap_residual"]),
"aug_lambda": float(row["aug_lambda"]),
"search_masters": int(float(row.get("search_masters", "0") or "0")),
"search_active": int(float(row.get("search_active", "0") or "0")),
}
)
return profile_path
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 = write_sliding_profile(result_dir, pair_rows)
max_active = max(int(float(row["active_count"])) for row in pair_rows)
max_stick_count = max(int(float(row["stick_count"])) for row in pair_rows)
max_slip_count = max(int(float(row["slip_count"])) for row in pair_rows)
max_slip = max(float(row["accumulated_slip"]) for row in pair_rows)
max_fric_work = max(float(row["friction_work_total"]) for row in pair_rows)
min_fric_work = min(float(row["friction_work_total"]) for row in pair_rows)
max_ft = max(
(
float(row["ftx"]) ** 2
+ float(row["fty"]) ** 2
+ float(row["ftz"]) ** 2
)
** 0.5
for row in pair_rows
)
max_reg = max(float(row["friction_regularization"]) for row in pair_rows)
max_masters = max(int(float(row.get("search_masters", "0") or "0")) for row in pair_rows)
max_search_active = max(int(float(row.get("search_active", "0") or "0")) for row in pair_rows)
if max_active < MIN_ACTIVE_CONTACTS:
print("sliding contact did not activate")
return 1
if max_masters < MIN_MASTER_FACETS:
print("large-sliding master surface did not expose four contact facets")
return 1
if max_search_active < MIN_ACTIVE_CONTACTS:
print("large-sliding search did not retain multiple active candidates")
return 1
if max_reg <= 0.0:
print("friction regularization was not reported")
return 1
if max_slip < MIN_ACCUMULATED_SLIP or max_ft <= 0.0:
print("frictional sliding response was not detected")
return 1
if max_slip_count <= 0 or max_stick_count <= 0:
print("stick/slip transition counters were not reported")
return 1
if min_fric_work < -1.0e-12 or max_fric_work <= 0.0:
print("friction work is not positive dissipative")
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_masters={}, max_slip={:.6e}, max_ft={:.6e}, "
"max_friction_work={:.6e}".format(
max_active, max_masters, max_slip, max_ft, max_fric_work
)
)
print(f"wrote {profile_path}")
return 0
if __name__ == "__main__":
raise SystemExit(main())