#!/usr/bin/env python3 """Check FemLite shell membrane patch-test results.""" from __future__ import annotations import argparse import csv import math from pathlib import Path from typing import Dict, List, Sequence, Tuple YOUNG_MODULUS = 30.0 POISSON_RATIO = 0.25 EXX = 0.01 DISP_TOL = 1.0e-11 STRESS_TOL = 1.0e-8 Point3 = Tuple[float, float, float] Element = Tuple[int, ...] def parse_inp(path: Path) -> Tuple[Dict[int, Point3], List[Element]]: nodes: Dict[int, Point3] = {} elements: List[Element] = [] section = "" for raw in path.read_text(encoding="ascii").splitlines(): line = raw.strip() if not line or line.startswith("**"): continue if line.startswith("*"): upper = line.upper() if upper.startswith("*NODE"): section = "node" elif upper.startswith("*ELEMENT"): section = "element" else: section = "" continue fields = [item.strip() for item in line.split(",") if item.strip()] if section == "node": nodes[int(fields[0])] = ( float(fields[1]), float(fields[2]), float(fields[3]), ) elif section == "element": elements.append(tuple(int(item) for item in fields[1:])) return nodes, elements def read_node_csv(path: Path) -> Dict[int, Dict[str, float]]: with path.open(newline="", encoding="ascii") as f: return {int(row["node_id"]): {k: float(v) for k, v in row.items() if k != "node_id"} for row in csv.DictReader(f)} def read_element_csv(path: Path) -> List[Dict[str, float]]: with path.open(newline="", encoding="ascii") as f: rows: List[Dict[str, float]] = [] for row in csv.DictReader(f): parsed = {k: float(v) for k, v in row.items() if k not in ("elem_id", "status")} parsed["elem_id"] = float(row["elem_id"]) parsed["status"] = float(row["status"]) rows.append(parsed) return rows def local_basis(nodes: Dict[int, Point3], elem: Sequence[int]) -> Tuple[Tuple[float, float], Tuple[float, float]]: x1, y1, _ = nodes[elem[0]] x2, y2, _ = nodes[elem[1]] dx = x2 - x1 dy = y2 - y1 length = math.hypot(dx, dy) if length <= 0.0: raise ValueError("coincident first edge nodes") e1 = (dx / length, dy / length) e2 = (-e1[1], e1[0]) return e1, e2 def expected_local_stress(nodes: Dict[int, Point3], elem: Sequence[int]) -> Tuple[float, float, float]: factor = YOUNG_MODULUS / (1.0 - POISSON_RATIO * POISSON_RATIO) sx = factor * EXX sy = factor * POISSON_RATIO * EXX txy = 0.0 e1, e2 = local_basis(nodes, elem) def stress_on(a: Tuple[float, float], b: Tuple[float, float]) -> float: return ( a[0] * (sx * b[0] + txy * b[1]) + a[1] * (txy * b[0] + sy * b[1]) ) return stress_on(e1, e1), stress_on(e2, e2), stress_on(e1, e2) def check_case(inp_path: Path, result_dir: Path) -> Tuple[float, float]: nodes, elements = parse_inp(inp_path) node_rows = read_node_csv(result_dir / "nodes_00000001.csv") elem_rows = read_element_csv(result_dir / "elements_00000001.csv") if len(elem_rows) != len(elements): raise RuntimeError(f"element count mismatch for {inp_path.name}") max_disp_error = 0.0 for nid, (x, _, _) in nodes.items(): row = node_rows[nid] max_disp_error = max(max_disp_error, abs(row["ux"] - EXX * x)) max_disp_error = max(max_disp_error, abs(row["uy"])) max_disp_error = max(max_disp_error, abs(row["uz"])) max_stress_error = 0.0 for elem, row in zip(elements, elem_rows): s11, s22, s12 = expected_local_stress(nodes, elem) max_stress_error = max(max_stress_error, abs(row["s11"] - s11)) max_stress_error = max(max_stress_error, abs(row["s22"] - s22)) max_stress_error = max(max_stress_error, abs(row["s12"] - s12)) max_stress_error = max(max_stress_error, abs(row["s13"])) max_stress_error = max(max_stress_error, abs(row["s23"])) if max_disp_error > DISP_TOL: raise RuntimeError( f"{inp_path.name}: displacement error {max_disp_error:.6E} > {DISP_TOL:.6E}" ) if max_stress_error > STRESS_TOL: raise RuntimeError( f"{inp_path.name}: stress error {max_stress_error:.6E} > {STRESS_TOL:.6E}" ) return max_disp_error, max_stress_error def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--case", choices=("s3", "s4"), required=True) parser.add_argument("--root", type=Path, default=Path(__file__).resolve().parent) args = parser.parse_args() case_name = f"shell_patch_{args.case}.inp" result_dir = args.root / "results" / args.case disp_error, stress_error = check_case(args.root / case_name, result_dir) print(f"{case_name}: max displacement error = {disp_error:.6E}") print(f"{case_name}: max local stress error = {stress_error:.6E}") print("PASS") if __name__ == "__main__": main()