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

120 lines
4.6 KiB
Python

from __future__ import annotations
import csv
import subprocess
import sys
from pathlib import Path
def require(condition: bool, message: str) -> None:
if not condition:
raise AssertionError(message)
def read_key_values(path: Path) -> dict[str, float]:
result: dict[str, float] = {}
with path.open(newline="") as handle:
reader = csv.reader(handle)
next(reader)
for key, value in reader:
result[key] = float(value)
return result
def read_rows(path: Path) -> list[dict[str, str]]:
with path.open(newline="") as handle:
return list(csv.DictReader(handle))
def main() -> int:
if len(sys.argv) != 3:
raise SystemExit("usage: check_cohesive_results.py BUILD_DIR OUTPUT_DIR")
repo = Path(__file__).resolve().parents[2]
build_dir = Path(sys.argv[1]).resolve()
out_dir = Path(sys.argv[2]).resolve()
out_dir.mkdir(parents=True, exist_ok=True)
case_dir = Path(__file__).resolve().parent
inp = case_dir / "ball_adhesion_cohesive_benchmark.inp"
text = inp.read_text(encoding="utf-8")
require("*BALL ADHESION COHESIVE" in text, "missing cohesive benchmark block")
tool = build_dir / "ball_adhesion_cohesive_benchmark.exe"
if not tool.exists():
tool = build_dir / "ball_adhesion_cohesive_benchmark"
completed = subprocess.run(
[str(tool), str(inp), str(out_dir)],
cwd=repo,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
if completed.returncode != 0:
print(completed.stdout)
raise RuntimeError(f"{tool} exited with code {completed.returncode}")
summary_path = out_dir / "adhesion_summary.csv"
history_path = out_dir / "adhesion_history.csv"
profile_path = out_dir / "decohesion_profile.csv"
for path in (summary_path, history_path, profile_path):
require(path.exists(), f"{path.name} missing")
require(path.stat().st_size > 0, f"{path.name} empty")
summary = read_key_values(summary_path)
history = read_rows(history_path)
profile = read_rows(profile_path)
samples = int(summary["samples"])
rings = int(summary["rings"])
contact_area = summary["contact_area"]
require(samples == 41, "unexpected sample count")
require(rings == 12, "unexpected ring count")
require(len(history) == samples, "history row count mismatch")
require(len(profile) == rings, "profile row count mismatch")
forces = [float(row["normal_force"]) for row in history]
energies = [float(row["energy_total"]) for row in history]
damages = [float(row["max_damage"]) for row in history]
failed_fracs = [float(row["failed_fraction"]) for row in history]
require(min(forces) < -50.0, "compression branch did not activate")
require(max(forces) > 20.0, "pull-off force too small")
require(summary["peak_pull_force"] == max(forces), "peak pull force mismatch")
require(summary["min_compression_force"] == min(forces),
"compression force mismatch")
require(summary["dissipated_energy"] > 0.05, "cohesive energy too small")
require(summary["hysteresis_area"] > 0.02, "hysteresis area too small")
require(summary["max_damage"] > 0.95, "cohesive damage did not activate")
require(summary["decohesion_area"] > 0.50 * contact_area,
"decohesion area too small")
require(summary["failed_fraction"] > 0.50, "failed fraction too small")
require(abs(summary["final_force"]) < 1.0e-6,
"final force should vanish after complete decohesion")
for prev, cur in zip(energies, energies[1:]):
require(cur + 1.0e-12 >= prev, "cohesive energy decreased")
for prev, cur in zip(damages, damages[1:]):
require(cur + 1.0e-12 >= prev, "cohesive damage decreased")
require(failed_fracs[-1] == summary["failed_fraction"],
"final failed fraction mismatch")
failed = [row for row in profile if int(row["status"]) == 3]
damaged = [row for row in profile if float(row["damage"]) > 1.0e-8]
failed_area = sum(float(row["area"]) for row in failed)
damaged_area = sum(float(row["area"]) for row in damaged)
max_damage = max(float(row["damage"]) for row in profile)
require(len(failed) == int(summary["failed_count"]), "failed count mismatch")
require(abs(failed_area - summary["decohesion_area"]) <= 1.0e-10,
"decohesion area mismatch")
require(abs(damaged_area - summary["damaged_area"]) <= 1.0e-10,
"damaged area mismatch")
require(abs(max_damage - summary["max_damage"]) <= 1.0e-10,
"profile damage mismatch")
return 0
if __name__ == "__main__":
raise SystemExit(main())