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

86 lines
3.1 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 RuntimeError(message)
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 find_tool(build_dir: Path) -> Path:
candidates = [
build_dir / "euler_column_buckling_benchmark.exe",
build_dir / "euler_column_buckling_benchmark",
build_dir / "Release" / "euler_column_buckling_benchmark.exe",
build_dir / "Release" / "euler_column_buckling_benchmark",
]
for candidate in candidates:
if candidate.exists():
return candidate
raise RuntimeError(f"missing euler_column_buckling_benchmark tool under {build_dir}")
def main() -> int:
if len(sys.argv) != 3:
raise RuntimeError("usage: check_buckling_results.py BUILD_DIR OUTPUT_DIR")
build_dir = Path(sys.argv[1]).resolve()
output_dir = Path(sys.argv[2]).resolve()
repo_root = Path(__file__).resolve().parents[2]
input_file = repo_root / "examples" / "euler_column_buckling" / "euler_column_buckling.inp"
tool = find_tool(build_dir)
output_dir.mkdir(parents=True, exist_ok=True)
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}")
summary = output_dir / "buckling_summary.csv"
mode = output_dir / "buckling_mode_1.csv"
require(summary.exists(), f"missing summary: {summary}")
require(mode.exists(), f"missing mode shape: {mode}")
values = read_key_values(summary)
element_count = int(values["element_count"])
exact = float(values["euler_pcr"])
factor = float(values["mode_1_factor"])
residual = float(values["mode_1_residual"])
rel_error = float(values["relative_error"])
kg_norm = float(values["kg_mode_norm"])
require(element_count >= 8, f"element count too small: {element_count}")
require(exact > 0.0 and factor > 0.0, "critical load values must be positive")
require(rel_error <= 2.0e-2, f"Euler buckling relative error too large: {rel_error}")
require(residual <= 1.0e-8, f"buckling residual too large: {residual}")
require(abs(kg_norm - 1.0) <= 1.0e-8, f"mode is not KG-normalized: {kg_norm}")
with mode.open(newline="", encoding="utf-8") as handle:
rows = list(csv.DictReader(handle))
require(len(rows) == element_count + 1, "mode output node count mismatch")
w_values = [abs(float(row["w"])) for row in rows]
require(max(w_values) > 0.0, "mode shape is blank")
require(w_values[0] <= 1.0e-12 and w_values[-1] <= 1.0e-12, "pinned end displacement is not zero")
print(f"Euler buckling benchmark passed: Pcr={factor:.6e}, error={rel_error:.3e}")
return 0
if __name__ == "__main__":
raise SystemExit(main())