277 lines
10 KiB
Python
277 lines
10 KiB
Python
from __future__ import annotations
|
|
|
|
import csv
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
NAFEMS_S22_D = -5.38e6
|
|
|
|
|
|
def require(condition: bool, message: str) -> None:
|
|
if not condition:
|
|
raise AssertionError(message)
|
|
|
|
|
|
def parse_marker(text: str, name: str) -> int:
|
|
match = re.search(rf"{name}\s*=\s*(\d+)", text)
|
|
require(match is not None, f"missing {name} marker in input")
|
|
return int(match.group(1))
|
|
|
|
|
|
def clean_output(out_dir: Path) -> None:
|
|
out_dir.mkdir(parents=True, exist_ok=True)
|
|
for pattern in ("history.csv", "nodes_*.csv", "elements_*.csv", "reactions_*.csv", "result_*.vtk"):
|
|
for path in out_dir.glob(pattern):
|
|
path.unlink()
|
|
contact_dir = out_dir / "contact"
|
|
if contact_dir.exists():
|
|
shutil.rmtree(contact_dir)
|
|
|
|
|
|
def read_row_by_id(path: Path, key: str, value: int) -> dict[str, str]:
|
|
require(path.exists(), f"missing output: {path}")
|
|
with path.open(newline="", encoding="utf-8") as handle:
|
|
for row in csv.DictReader(handle):
|
|
if int(row[key]) == value:
|
|
return row
|
|
raise AssertionError(f"{path.name} does not contain {key}={value}")
|
|
|
|
|
|
def read_displacements(path: Path) -> dict[int, tuple[float, float, float]]:
|
|
require(path.exists(), f"missing output: {path}")
|
|
with path.open(newline="", encoding="utf-8") as handle:
|
|
return {
|
|
int(row["node_id"]): (float(row["ux"]), float(row["uy"]), float(row["uz"]))
|
|
for row in csv.DictReader(handle)
|
|
}
|
|
|
|
|
|
def read_reaction_sum(path: Path, field: str) -> float:
|
|
require(path.exists(), f"missing output: {path}")
|
|
with path.open(newline="", encoding="utf-8") as handle:
|
|
return sum(float(row[field]) for row in csv.DictReader(handle))
|
|
|
|
|
|
def parse_total_fz(text: str) -> float:
|
|
match = re.search(r"total_Fz=([+-]?\d+\.\d+E[+-]\d+)", text)
|
|
require(match is not None, "missing total_Fz marker in input")
|
|
return float(match.group(1))
|
|
|
|
|
|
def parse_input_mesh(path: Path) -> tuple[dict[int, tuple[float, float, float]], dict[int, list[int]]]:
|
|
nodes: dict[int, tuple[float, float, float]] = {}
|
|
elements: dict[int, list[int]] = {}
|
|
mode = ""
|
|
for raw_line in path.read_text(encoding="ascii").splitlines():
|
|
line = raw_line.strip()
|
|
if not line or line.startswith("**"):
|
|
continue
|
|
if line.startswith("*"):
|
|
upper = line.upper()
|
|
if upper.startswith("*NODE"):
|
|
mode = "node"
|
|
elif upper.startswith("*ELEMENT"):
|
|
mode = "element"
|
|
else:
|
|
mode = ""
|
|
continue
|
|
|
|
parts = [part.strip() for part in line.split(",") if part.strip()]
|
|
if mode == "node":
|
|
nodes[int(parts[0])] = (float(parts[1]), float(parts[2]), float(parts[3]))
|
|
elif mode == "element":
|
|
elements[int(parts[0])] = [int(part) for part in parts[1:]]
|
|
return nodes, elements
|
|
|
|
|
|
def shape_c3d20_derivatives(r: float, s: float, t: float) -> list[list[float]]:
|
|
dndr = [[0.0 for _ in range(20)] for _ in range(3)]
|
|
corners = (
|
|
(-1.0, -1.0, -1.0),
|
|
(1.0, -1.0, -1.0),
|
|
(1.0, 1.0, -1.0),
|
|
(-1.0, 1.0, -1.0),
|
|
(-1.0, -1.0, 1.0),
|
|
(1.0, -1.0, 1.0),
|
|
(1.0, 1.0, 1.0),
|
|
(-1.0, 1.0, 1.0),
|
|
)
|
|
for index, (xi, eta, zeta) in enumerate(corners):
|
|
ar = 1.0 + xi * r
|
|
bs = 1.0 + eta * s
|
|
ct = 1.0 + zeta * t
|
|
q = xi * r + eta * s + zeta * t - 2.0
|
|
dndr[0][index] = 0.125 * xi * bs * ct * (q + ar)
|
|
dndr[1][index] = 0.125 * eta * ar * ct * (q + bs)
|
|
dndr[2][index] = 0.125 * zeta * ar * bs * (q + ct)
|
|
|
|
def edge_xi(index: int, eta: float, zeta: float) -> None:
|
|
bs = 1.0 + eta * s
|
|
ct = 1.0 + zeta * t
|
|
dndr[0][index] = -0.5 * r * bs * ct
|
|
dndr[1][index] = 0.25 * (1.0 - r * r) * eta * ct
|
|
dndr[2][index] = 0.25 * (1.0 - r * r) * bs * zeta
|
|
|
|
def edge_eta(index: int, xi: float, zeta: float) -> None:
|
|
ar = 1.0 + xi * r
|
|
ct = 1.0 + zeta * t
|
|
dndr[0][index] = 0.25 * xi * (1.0 - s * s) * ct
|
|
dndr[1][index] = -0.5 * ar * s * ct
|
|
dndr[2][index] = 0.25 * ar * (1.0 - s * s) * zeta
|
|
|
|
def edge_zeta(index: int, xi: float, eta: float) -> None:
|
|
ar = 1.0 + xi * r
|
|
bs = 1.0 + eta * s
|
|
dndr[0][index] = 0.25 * xi * bs * (1.0 - t * t)
|
|
dndr[1][index] = 0.25 * ar * eta * (1.0 - t * t)
|
|
dndr[2][index] = -0.5 * ar * bs * t
|
|
|
|
edge_xi(8, -1.0, -1.0)
|
|
edge_eta(9, 1.0, -1.0)
|
|
edge_xi(10, 1.0, -1.0)
|
|
edge_eta(11, -1.0, -1.0)
|
|
edge_xi(12, -1.0, 1.0)
|
|
edge_eta(13, 1.0, 1.0)
|
|
edge_xi(14, 1.0, 1.0)
|
|
edge_eta(15, -1.0, 1.0)
|
|
edge_zeta(16, -1.0, -1.0)
|
|
edge_zeta(17, 1.0, -1.0)
|
|
edge_zeta(18, 1.0, 1.0)
|
|
edge_zeta(19, -1.0, 1.0)
|
|
return dndr
|
|
|
|
|
|
def invert3(matrix: list[list[float]]) -> tuple[list[list[float]], float]:
|
|
det = (
|
|
matrix[0][0] * (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1])
|
|
- matrix[0][1] * (matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0])
|
|
+ matrix[0][2] * (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0])
|
|
)
|
|
require(abs(det) > 1.0e-16, "singular D-probe element Jacobian")
|
|
inv = [[0.0 for _ in range(3)] for _ in range(3)]
|
|
inv[0][0] = (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1]) / det
|
|
inv[0][1] = -(matrix[0][1] * matrix[2][2] - matrix[0][2] * matrix[2][1]) / det
|
|
inv[0][2] = (matrix[0][1] * matrix[1][2] - matrix[0][2] * matrix[1][1]) / det
|
|
inv[1][0] = -(matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0]) / det
|
|
inv[1][1] = (matrix[0][0] * matrix[2][2] - matrix[0][2] * matrix[2][0]) / det
|
|
inv[1][2] = -(matrix[0][0] * matrix[1][2] - matrix[0][2] * matrix[1][0]) / det
|
|
inv[2][0] = (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]) / det
|
|
inv[2][1] = -(matrix[0][0] * matrix[2][1] - matrix[0][1] * matrix[2][0]) / det
|
|
inv[2][2] = (matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]) / det
|
|
return inv, det
|
|
|
|
|
|
def recover_c3d20_stress_at_d(
|
|
element_nodes: list[int],
|
|
coords: dict[int, tuple[float, float, float]],
|
|
disp: dict[int, tuple[float, float, float]],
|
|
) -> tuple[float, list[float]]:
|
|
dndr = shape_c3d20_derivatives(-1.0, -1.0, 1.0)
|
|
jac = [[0.0 for _ in range(3)] for _ in range(3)]
|
|
for local, node_id in enumerate(element_nodes):
|
|
xyz = coords[node_id]
|
|
for i in range(3):
|
|
for j in range(3):
|
|
jac[i][j] += xyz[i] * dndr[j][local]
|
|
inv_jac, det_jac = invert3(jac)
|
|
require(det_jac > 0.0, f"D-probe element has non-positive Jacobian: {det_jac:.6E}")
|
|
|
|
dndx = [[0.0 for _ in range(20)] for _ in range(3)]
|
|
for local in range(20):
|
|
for i in range(3):
|
|
dndx[i][local] = sum(inv_jac[k][i] * dndr[k][local] for k in range(3))
|
|
|
|
strain = [0.0 for _ in range(6)]
|
|
for local, node_id in enumerate(element_nodes):
|
|
ux, uy, uz = disp[node_id]
|
|
strain[0] += dndx[0][local] * ux
|
|
strain[1] += dndx[1][local] * uy
|
|
strain[2] += dndx[2][local] * uz
|
|
strain[3] += dndx[1][local] * ux + dndx[0][local] * uy
|
|
strain[4] += dndx[2][local] * ux + dndx[0][local] * uz
|
|
strain[5] += dndx[2][local] * uy + dndx[1][local] * uz
|
|
|
|
young = 2.10e11
|
|
poisson = 0.30
|
|
lam = young * poisson / ((1.0 + poisson) * (1.0 - 2.0 * poisson))
|
|
mu = young / (2.0 * (1.0 + poisson))
|
|
trace = strain[0] + strain[1] + strain[2]
|
|
stress = [0.0 for _ in range(6)]
|
|
stress[0] = lam * trace + 2.0 * mu * strain[0]
|
|
stress[1] = lam * trace + 2.0 * mu * strain[1]
|
|
stress[2] = lam * trace + 2.0 * mu * strain[2]
|
|
stress[3] = mu * strain[3]
|
|
stress[4] = mu * strain[4]
|
|
stress[5] = mu * strain[5]
|
|
return stress[1], strain
|
|
|
|
|
|
def main() -> int:
|
|
if len(sys.argv) != 4:
|
|
raise SystemExit("usage: check_thick_plate_results.py REPO_ROOT FEMLITE_EXE OUTPUT_DIR")
|
|
|
|
repo = Path(sys.argv[1]).resolve()
|
|
femlite = Path(sys.argv[2]).resolve()
|
|
out_dir = Path(sys.argv[3]).resolve()
|
|
inp = repo / "examples" / "thick_plate" / "thick_plate_le10.inp"
|
|
text = inp.read_text(encoding="ascii")
|
|
|
|
require("smoke" not in text.lower(), "thick plate benchmark input still describes a smoke case")
|
|
require("NAFEMS LE10" in text, "input does not identify the LE10 benchmark")
|
|
d_node = parse_marker(text, "D_NODE")
|
|
d_probe = parse_marker(text, "D_PROBE_ELEMENT")
|
|
total_fz = parse_total_fz(text)
|
|
|
|
clean_output(out_dir)
|
|
completed = subprocess.run(
|
|
[
|
|
str(femlite),
|
|
str(inp),
|
|
"--static-linear",
|
|
"--output-dir",
|
|
str(out_dir),
|
|
"--output-interval",
|
|
"1",
|
|
],
|
|
cwd=repo,
|
|
text=True,
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.STDOUT,
|
|
)
|
|
print(completed.stdout)
|
|
if completed.returncode != 0:
|
|
raise RuntimeError(f"{femlite} exited with code {completed.returncode}")
|
|
|
|
node_row = read_row_by_id(out_dir / "nodes_00000001.csv", "node_id", d_node)
|
|
coords, elements = parse_input_mesh(inp)
|
|
displacements = read_displacements(out_dir / "nodes_00000001.csv")
|
|
s22, _strain = recover_c3d20_stress_at_d(elements[d_probe], coords, displacements)
|
|
uz = float(node_row["uz"])
|
|
reaction_z = read_reaction_sum(out_dir / "reactions_00000001.csv", "reaction_uz")
|
|
|
|
rel_error = abs((s22 - NAFEMS_S22_D) / NAFEMS_S22_D)
|
|
require(s22 < 0.0, f"D-near s22 should be compressive, got {s22:.6E}")
|
|
require(0.0 < abs(s22) < 2.0e7, f"D-near s22 magnitude is outside benchmark bounds: {s22:.6E}")
|
|
require(rel_error <= 0.12, f"D-point recovered s22 differs too much from NAFEMS: {rel_error:.3%}")
|
|
require(uz < 0.0, f"D-point vertical displacement should be negative, got {uz:.6E}")
|
|
require(abs(uz) > 1.0e-8, f"D-point vertical displacement is too small: {uz:.6E}")
|
|
require(
|
|
abs((reaction_z + total_fz) / total_fz) <= 1.0e-6,
|
|
f"vertical reactions do not balance pressure load: reaction_z={reaction_z:.6E}, total_fz={total_fz:.6E}",
|
|
)
|
|
|
|
print(
|
|
"thick plate LE10: "
|
|
f"s22_D={s22:.6E}, rel_error_vs_NAFEMS={rel_error:.3%}, "
|
|
f"uz_D={uz:.6E}, reaction_z={reaction_z:.6E}"
|
|
)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|