351 lines
12 KiB
Python
351 lines
12 KiB
Python
#!/usr/bin/env python3
|
|
"""Generate FemLite Scordelis-Lo roof shell benchmark input decks.
|
|
|
|
The topology is generated in a rectangular parameter domain by the Gmsh Python
|
|
API and then mapped to the cylindrical midsurface.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import math
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
from typing import Dict, List, Sequence, Tuple
|
|
|
|
|
|
RADIUS = 25.0
|
|
ROOF_LENGTH = 50.0
|
|
MODEL_LENGTH = 0.5 * ROOF_LENGTH
|
|
MODEL_ANGLE = 40.0 * math.pi / 180.0
|
|
THICKNESS = 0.25
|
|
YOUNG_MODULUS = 4.32e8
|
|
POISSON_RATIO = 0.0
|
|
DENSITY = 1.0
|
|
FACE_LOAD_Z = -90.0
|
|
END_TIME = 0.10
|
|
TIME_STEP = 1.0e-5
|
|
STATIC_INITIAL_INCREMENT = 1.0
|
|
STATIC_TOTAL_TIME = 1.0
|
|
STATIC_MIN_INCREMENT = 0.1
|
|
STATIC_MAX_INCREMENT = 1.0
|
|
TRI_NX = 11
|
|
TRI_NTHETA = 8
|
|
QUAD_NX = 10
|
|
QUAD_NTHETA = 7
|
|
SET_LINE_WIDTH = 16
|
|
|
|
|
|
Point3 = Tuple[float, float, float]
|
|
Element = Tuple[int, ...]
|
|
|
|
|
|
@dataclass
|
|
class Mesh:
|
|
nodes: Dict[int, Point3]
|
|
elements: List[Element]
|
|
element_type: str
|
|
|
|
|
|
def try_import_gmsh():
|
|
try:
|
|
import gmsh # type: ignore
|
|
except ImportError:
|
|
return None
|
|
return gmsh
|
|
|
|
|
|
def map_to_roof(x_param: float, theta_param: float) -> Point3:
|
|
x = x_param
|
|
theta = theta_param
|
|
y = RADIUS * math.sin(theta)
|
|
z = RADIUS * math.cos(theta)
|
|
return x, y, z
|
|
|
|
|
|
def build_gmsh_mesh(element_type: str) -> Mesh:
|
|
gmsh = try_import_gmsh()
|
|
if gmsh is None:
|
|
raise RuntimeError("gmsh Python module is not available")
|
|
|
|
wanted = 2 if element_type == "S3" else 3
|
|
nx, ntheta = mesh_divisions(element_type)
|
|
gmsh.initialize()
|
|
try:
|
|
gmsh.model.add(f"scordelis_lo_{element_type.lower()}")
|
|
geo = gmsh.model.geo
|
|
p1 = geo.addPoint(0.0, 0.0, 0.0)
|
|
p2 = geo.addPoint(MODEL_LENGTH, 0.0, 0.0)
|
|
p3 = geo.addPoint(MODEL_LENGTH, MODEL_ANGLE, 0.0)
|
|
p4 = geo.addPoint(0.0, MODEL_ANGLE, 0.0)
|
|
l1 = geo.addLine(p1, p2)
|
|
l2 = geo.addLine(p2, p3)
|
|
l3 = geo.addLine(p3, p4)
|
|
l4 = geo.addLine(p4, p1)
|
|
loop = geo.addCurveLoop([l1, l2, l3, l4])
|
|
surface = geo.addPlaneSurface([loop])
|
|
|
|
geo.mesh.setTransfiniteCurve(l1, nx + 1)
|
|
geo.mesh.setTransfiniteCurve(l3, nx + 1)
|
|
geo.mesh.setTransfiniteCurve(l2, ntheta + 1)
|
|
geo.mesh.setTransfiniteCurve(l4, ntheta + 1)
|
|
geo.mesh.setTransfiniteSurface(surface)
|
|
if element_type == "S4":
|
|
geo.mesh.setRecombine(2, surface)
|
|
geo.synchronize()
|
|
gmsh.model.mesh.generate(2)
|
|
return collect_gmsh_mesh(gmsh, wanted, element_type)
|
|
finally:
|
|
gmsh.finalize()
|
|
|
|
|
|
def collect_gmsh_mesh(gmsh, wanted: int, element_type: str) -> Mesh:
|
|
node_tags, node_coords, _ = gmsh.model.mesh.getNodes()
|
|
tag_to_id: Dict[int, int] = {}
|
|
nodes: Dict[int, Point3] = {}
|
|
for new_id, tag in enumerate(node_tags, start=1):
|
|
tag_int = int(tag)
|
|
tag_to_id[tag_int] = new_id
|
|
i = 3 * (new_id - 1)
|
|
nodes[new_id] = map_to_roof(float(node_coords[i]), float(node_coords[i + 1]))
|
|
|
|
elements: List[Element] = []
|
|
elem_types, _, elem_node_tags = gmsh.model.mesh.getElements(2)
|
|
nen = 3 if wanted == 2 else 4
|
|
for etype, conn in zip(elem_types, elem_node_tags):
|
|
if int(etype) != wanted:
|
|
continue
|
|
for i in range(0, len(conn), nen):
|
|
elem = tuple(tag_to_id[int(tag)] for tag in conn[i : i + nen])
|
|
if element_type == "S3":
|
|
elem = orient_tri(nodes, elem)
|
|
else:
|
|
elem = orient_quad(nodes, elem)
|
|
elements.append(elem)
|
|
|
|
if not elements:
|
|
raise RuntimeError(f"gmsh did not generate {element_type} elements")
|
|
return Mesh(nodes=nodes, elements=elements, element_type=element_type)
|
|
|
|
|
|
def build_internal_mesh(element_type: str) -> Mesh:
|
|
nodes: Dict[int, Point3] = {}
|
|
node_grid: List[List[int]] = []
|
|
node_id = 1
|
|
nx, ntheta = mesh_divisions(element_type)
|
|
for j in range(ntheta + 1):
|
|
theta_param = MODEL_ANGLE * float(j) / float(ntheta)
|
|
row: List[int] = []
|
|
for i in range(nx + 1):
|
|
x_param = MODEL_LENGTH * float(i) / float(nx)
|
|
row.append(node_id)
|
|
nodes[node_id] = map_to_roof(x_param, theta_param)
|
|
node_id += 1
|
|
node_grid.append(row)
|
|
|
|
elements: List[Element] = []
|
|
for j in range(ntheta):
|
|
for i in range(nx):
|
|
n1 = node_grid[j][i]
|
|
n2 = node_grid[j][i + 1]
|
|
n3 = node_grid[j + 1][i + 1]
|
|
n4 = node_grid[j + 1][i]
|
|
if element_type == "S4":
|
|
elements.append(orient_quad(nodes, (n1, n2, n3, n4)))
|
|
else:
|
|
elements.append(orient_tri(nodes, (n1, n2, n3)))
|
|
elements.append(orient_tri(nodes, (n1, n3, n4)))
|
|
return Mesh(nodes=nodes, elements=elements, element_type=element_type)
|
|
|
|
|
|
def mesh_divisions(element_type: str) -> Tuple[int, int]:
|
|
if element_type == "S3":
|
|
return TRI_NX, TRI_NTHETA
|
|
return QUAD_NX, QUAD_NTHETA
|
|
|
|
|
|
def build_mesh(element_type: str, backend: str) -> Mesh:
|
|
if backend == "gmsh":
|
|
return build_gmsh_mesh(element_type)
|
|
if backend == "auto" and try_import_gmsh() is not None:
|
|
return build_gmsh_mesh(element_type)
|
|
return build_internal_mesh(element_type)
|
|
|
|
|
|
def orient_tri(nodes: Dict[int, Point3], elem: Sequence[int]) -> Element:
|
|
if element_normal_z_hint(nodes, elem) >= 0.0:
|
|
return tuple(int(n) for n in elem)
|
|
return (int(elem[0]), int(elem[2]), int(elem[1]))
|
|
|
|
|
|
def orient_quad(nodes: Dict[int, Point3], elem: Sequence[int]) -> Element:
|
|
if element_normal_z_hint(nodes, elem) >= 0.0:
|
|
return tuple(int(n) for n in elem)
|
|
return (int(elem[0]), int(elem[3]), int(elem[2]), int(elem[1]))
|
|
|
|
|
|
def element_normal_z_hint(nodes: Dict[int, Point3], elem: Sequence[int]) -> float:
|
|
p1 = nodes[int(elem[0])]
|
|
p2 = nodes[int(elem[1])]
|
|
p3 = nodes[int(elem[2])]
|
|
v12 = sub(p2, p1)
|
|
v13 = sub(p3, p1)
|
|
normal = cross(v12, v13)
|
|
centroid = tuple(sum(nodes[int(n)][i] for n in elem) / float(len(elem)) for i in range(3))
|
|
radial = (0.0, centroid[1], centroid[2])
|
|
return dot(normal, radial)
|
|
|
|
|
|
def sub(a: Point3, b: Point3) -> Point3:
|
|
return a[0] - b[0], a[1] - b[1], a[2] - b[2]
|
|
|
|
|
|
def cross(a: Point3, b: Point3) -> Point3:
|
|
return (
|
|
a[1] * b[2] - a[2] * b[1],
|
|
a[2] * b[0] - a[0] * b[2],
|
|
a[0] * b[1] - a[1] * b[0],
|
|
)
|
|
|
|
|
|
def dot(a: Point3, b: Point3) -> float:
|
|
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
|
|
|
|
|
|
def tri_area(a: Point3, b: Point3, c: Point3) -> float:
|
|
return 0.5 * math.sqrt(dot(cross(sub(b, a), sub(c, a)), cross(sub(b, a), sub(c, a))))
|
|
|
|
|
|
def element_area(nodes: Dict[int, Point3], elem: Sequence[int]) -> float:
|
|
if len(elem) == 3:
|
|
return tri_area(nodes[elem[0]], nodes[elem[1]], nodes[elem[2]])
|
|
return tri_area(nodes[elem[0]], nodes[elem[1]], nodes[elem[2]]) + tri_area(
|
|
nodes[elem[0]], nodes[elem[2]], nodes[elem[3]]
|
|
)
|
|
|
|
|
|
def nodal_vertical_loads(mesh: Mesh) -> Dict[int, float]:
|
|
loads = {nid: 0.0 for nid in mesh.nodes}
|
|
for elem in mesh.elements:
|
|
share = FACE_LOAD_Z * element_area(mesh.nodes, elem) / float(len(elem))
|
|
for nid in elem:
|
|
loads[nid] += share
|
|
return loads
|
|
|
|
|
|
def curved_inner_nodes(mesh: Mesh) -> List[int]:
|
|
return [nid for nid, xyz in sorted(mesh.nodes.items()) if abs(xyz[0]) < 1.0e-10]
|
|
|
|
|
|
def curved_outer_nodes(mesh: Mesh) -> List[int]:
|
|
return [nid for nid, xyz in sorted(mesh.nodes.items()) if abs(xyz[0] - MODEL_LENGTH) < 1.0e-10]
|
|
|
|
|
|
def straight_top_nodes(mesh: Mesh) -> List[int]:
|
|
return [nid for nid, xyz in sorted(mesh.nodes.items()) if abs(xyz[1]) < 1.0e-10]
|
|
|
|
|
|
def straight_free_nodes(mesh: Mesh) -> List[int]:
|
|
y_free = RADIUS * math.sin(MODEL_ANGLE)
|
|
return [nid for nid, xyz in sorted(mesh.nodes.items()) if abs(xyz[1] - y_free) < 1.0e-10]
|
|
|
|
|
|
def write_id_set(f, ids: Sequence[int]) -> None:
|
|
for i in range(0, len(ids), SET_LINE_WIDTH):
|
|
f.write(", ".join(str(item) for item in ids[i : i + SET_LINE_WIDTH]))
|
|
f.write("\n")
|
|
|
|
|
|
def write_inp(path: Path, mesh: Mesh, analysis: str) -> None:
|
|
curved_inner = curved_inner_nodes(mesh)
|
|
curved_outer = curved_outer_nodes(mesh)
|
|
straight_top = straight_top_nodes(mesh)
|
|
straight_free = straight_free_nodes(mesh)
|
|
loads = nodal_vertical_loads(mesh)
|
|
total_load = sum(loads.values())
|
|
total_area = sum(element_area(mesh.nodes, elem) for elem in mesh.elements)
|
|
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
with path.open("w", encoding="ascii", newline="\n") as f:
|
|
f.write("*HEADING\n")
|
|
if analysis == "static":
|
|
f.write(f"FemLite Scordelis-Lo roof {mesh.element_type} static\n")
|
|
else:
|
|
f.write(f"FemLite Scordelis-Lo roof {mesh.element_type}\n")
|
|
f.write(f"** quarter model: R={RADIUS:.10E}, 2L={ROOF_LENGTH:.10E}, angle_rad={MODEL_ANGLE:.10E}\n")
|
|
f.write(f"** thickness={THICKNESS:.10E}, area={total_area:.10E}, face_load_z={FACE_LOAD_Z:.10E}, total_Fz={total_load:.10E}\n")
|
|
f.write("*NODE\n")
|
|
for nid in sorted(mesh.nodes):
|
|
x, y, z = mesh.nodes[nid]
|
|
f.write(f"{nid}, {x:.10E}, {y:.10E}, {z:.10E}\n")
|
|
f.write(f"*ELEMENT, TYPE={mesh.element_type}, ELSET=ROOF\n")
|
|
for eid, elem in enumerate(mesh.elements, start=1):
|
|
f.write(f"{eid}, " + ", ".join(str(nid) for nid in elem) + "\n")
|
|
f.write("*NSET, NSET=CURVED_INNER_SYM\n")
|
|
write_id_set(f, curved_inner)
|
|
f.write("*NSET, NSET=CURVED_OUTER_SUPPORT\n")
|
|
write_id_set(f, curved_outer)
|
|
f.write("*NSET, NSET=STRAIGHT_TOP_SYM\n")
|
|
write_id_set(f, straight_top)
|
|
f.write("*NSET, NSET=STRAIGHT_FREE\n")
|
|
write_id_set(f, straight_free)
|
|
f.write("*MATERIAL, NAME=ALUMINUM\n")
|
|
f.write("*DENSITY\n")
|
|
f.write(f"{DENSITY:.10E}\n")
|
|
f.write("*ELASTIC\n")
|
|
f.write(f"{YOUNG_MODULUS:.10E}, {POISSON_RATIO:.10E}\n")
|
|
f.write("*SHELL SECTION, ELSET=ROOF, MATERIAL=ALUMINUM\n")
|
|
f.write(f"{THICKNESS:.10E}\n")
|
|
f.write("*BOUNDARY\n")
|
|
f.write("CURVED_OUTER_SUPPORT, 2, 3, 0.0\n")
|
|
f.write("CURVED_INNER_SYM, 1, 1, 0.0\n")
|
|
f.write("CURVED_INNER_SYM, 5, 6, 0.0\n")
|
|
f.write("STRAIGHT_TOP_SYM, 2, 2, 0.0\n")
|
|
f.write("STRAIGHT_TOP_SYM, 4, 4, 0.0\n")
|
|
f.write("STRAIGHT_TOP_SYM, 6, 6, 0.0\n")
|
|
f.write("*CLOAD\n")
|
|
for nid in sorted(loads):
|
|
f.write(f"{nid}, 3, {loads[nid]:.10E}\n")
|
|
f.write("*STEP\n")
|
|
if analysis == "static":
|
|
f.write("*STATIC\n")
|
|
f.write(
|
|
f"{STATIC_INITIAL_INCREMENT:.10E}, {STATIC_TOTAL_TIME:.10E}, "
|
|
f"{STATIC_MIN_INCREMENT:.10E}, {STATIC_MAX_INCREMENT:.10E}\n"
|
|
)
|
|
else:
|
|
f.write("*DYNAMIC\n")
|
|
f.write(f"{TIME_STEP:.10E}, {END_TIME:.10E}\n")
|
|
f.write("*END STEP\n")
|
|
|
|
|
|
def write_all(output_dir: Path, backend: str) -> None:
|
|
tri_mesh = build_mesh("S3", backend)
|
|
quad_mesh = build_mesh("S4", backend)
|
|
write_inp(output_dir / "scordelislo_tri.inp", tri_mesh, "dynamic")
|
|
write_inp(output_dir / "scordelislo_quad.inp", quad_mesh, "dynamic")
|
|
write_inp(output_dir / "scordelislo_tri_static.inp", tri_mesh, "static")
|
|
write_inp(output_dir / "scordelislo_quad_static.inp", quad_mesh, "static")
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument(
|
|
"--backend",
|
|
choices=("auto", "gmsh", "internal"),
|
|
default="auto",
|
|
help="mesh backend; auto uses gmsh when installed and otherwise falls back to internal",
|
|
)
|
|
parser.add_argument(
|
|
"--output-dir",
|
|
type=Path,
|
|
default=Path(__file__).resolve().parent,
|
|
help="directory for generated inp files",
|
|
)
|
|
args = parser.parse_args()
|
|
write_all(args.output_dir, args.backend)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|