from __future__ import annotations import csv import subprocess import sys from pathlib import Path def read_key_values(path: Path) -> dict[str, str]: with path.open(newline="", encoding="utf-8") as handle: return {row["key"]: row["value"] for row in csv.DictReader(handle)} def require(condition: bool, message: str) -> None: if not condition: raise RuntimeError(message) def find_tool(build_dir: Path) -> Path: candidates = [ build_dir / "strength_reduction_benchmark.exe", build_dir / "strength_reduction_benchmark", build_dir / "Release" / "strength_reduction_benchmark.exe", build_dir / "Release" / "strength_reduction_benchmark", ] for candidate in candidates: if candidate.exists(): return candidate raise RuntimeError(f"missing strength_reduction_benchmark tool under {build_dir}") def main() -> int: if len(sys.argv) != 2: raise RuntimeError("usage: check_strength_reduction_results.py OUTPUT_DIR") output_dir = Path(sys.argv[1]).resolve() repo_root = Path(__file__).resolve().parents[2] build_dir = output_dir.parents[1] input_file = repo_root / "examples" / "strengthReduction" / "strength_reduction_slope_static.inp" tool = find_tool(build_dir) inp_text = input_file.read_text(encoding="utf-8").upper() require("*MOHR COULOMB" in inp_text, "strength reduction input does not use Mohr-Coulomb") require("ELASTIC_SOIL" not in inp_text, "input still uses the old elastic-soil placeholder") completed = subprocess.run( [str(tool), str(input_file), str(output_dir)], cwd=repo_root, text=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, ) print(completed.stdout) if completed.returncode != 0: raise RuntimeError(f"{tool} exited with code {completed.returncode}") history = output_dir / "strength_reduction_history.csv" summary = output_dir / "strength_reduction_summary.csv" plastic_zone = output_dir / "plastic_zone.csv" displacement_probe = output_dir / "displacement_probe.csv" vtk = output_dir / "result_00000001.vtk" nodes = output_dir / "nodes_00000001.csv" for path in (history, summary, plastic_zone, displacement_probe, vtk, nodes): require(path.exists(), f"missing benchmark output: {path}") values = read_key_values(summary) require(values.get("status") == "CONVERGED", f"unexpected SR status: {values.get('status')}") factor = float(values["converged_factor"]) lower = float(values["lower"]) upper = float(values["upper"]) require(1.20 <= factor <= 1.55, f"safety factor out of range: {factor}") require(lower < factor < upper, f"factor is not bracketed: lower={lower}, factor={factor}, upper={upper}") require(float(values["stable_count"]) >= 2.0, "not enough stable SR trials") require(float(values["unstable_count"]) >= 1.0, "not enough unstable SR trials") with history.open(newline="", encoding="utf-8") as handle: rows = list(csv.DictReader(handle)) stable = [row for row in rows if row.get("stable") == "1"] unstable = [row for row in rows if row.get("stable") == "0"] require(len(stable) >= 2 and len(unstable) >= 1, "history does not contain a real bracket") with plastic_zone.open(newline="", encoding="utf-8") as handle: zone_rows = list(csv.DictReader(handle)) plastic_count = sum(1 for row in zone_rows if row["plastic"] == "1") max_branch = max(int(row["branch"]) for row in zone_rows) require(plastic_count >= 2, f"plastic zone too small: {plastic_count}") require(max_branch > 0, "plastic zone never activates a Mohr-Coulomb branch") probe = read_key_values(displacement_probe) require(float(probe["crest_uz"]) < 0.0, "crest vertical displacement probe has wrong sign") require(float(probe["toe_ux"]) > 0.0, "toe horizontal displacement probe has wrong sign") print(f"strength reduction benchmark passed: FS={factor:.4f}, plastic probes={plastic_count}") return 0 if __name__ == "__main__": raise SystemExit(main())