from __future__ import annotations import argparse import math from pathlib import Path L1 = 85.0 L2 = 20.0 H1 = 20.0 H2 = 10.0 WIDTH = 1.0 RHO = 1940.0 GRAVITY = 9.81 def coord_line(start: float, end: float, step: float) -> list[float]: count = max(1, int(math.ceil((end - start) / step))) return [start + (end - start) * i / count for i in range(count + 1)] def merged_coords(*groups: list[float]) -> list[float]: values: dict[int, float] = {} for group in groups: for value in group: values[round(value * 1.0e9)] = value return [values[key] for key in sorted(values)] def rounded_key(x: float, y: float, z: float) -> tuple[int, int, int]: return (round(x * 1.0e9), round(y * 1.0e9), round(z * 1.0e9)) def quad_area_xz(points: list[tuple[float, float]]) -> float: area = 0.0 for i, (x0, z0) in enumerate(points): x1, z1 = points[(i + 1) % len(points)] area += x0 * z1 - x1 * z0 return abs(area) * 0.5 def build_mesh(alpha_deg: float, mesh_size: float): alpha = math.radians(alpha_deg) lslope = H2 / math.tan(alpha) nodes: list[tuple[int, float, float, float]] = [] node_by_key: dict[tuple[int, int, int], int] = {} element_quads: list[tuple[tuple[float, float], tuple[float, float], tuple[float, float], tuple[float, float]]] = [] def add_node(x: float, y: float, z: float) -> int: key = rounded_key(x, y, z) if key in node_by_key: return node_by_key[key] node_id = len(nodes) + 1 node_by_key[key] = node_id nodes.append((node_id, x, y, z)) return node_id elements: list[tuple[int, int, int, int, int, int, int, int, int]] = [] def add_hex(x0: float, x1: float, z0: float, z1: float) -> None: n1 = add_node(x0, 0.0, z0) n2 = add_node(x1, 0.0, z0) n3 = add_node(x1, WIDTH, z0) n4 = add_node(x0, WIDTH, z0) n5 = add_node(x0, 0.0, z1) n6 = add_node(x1, 0.0, z1) n7 = add_node(x1, WIDTH, z1) n8 = add_node(x0, WIDTH, z1) elem_id = len(elements) + 1 elements.append((elem_id, n1, n2, n3, n4, n5, n6, n7, n8)) element_quads.append(((x0, z0), (x1, z0), (x1, z1), (x0, z1))) z_upper = coord_line(H1, H1 + H2, mesh_size) upper_cols = max(1, int(math.ceil((L1 - L2) / mesh_size))) upper_base = [L2 + (L1 - L2) * ix / upper_cols for ix in range(upper_cols + 1)] x_lower = merged_coords(coord_line(0.0, L1, mesh_size), upper_base) z_lower = coord_line(0.0, H1, mesh_size) for iz in range(len(z_lower) - 1): for ix in range(len(x_lower) - 1): add_hex(x_lower[ix], x_lower[ix + 1], z_lower[iz], z_lower[iz + 1]) for iz in range(len(z_upper) - 1): z0 = z_upper[iz] z1 = z_upper[iz + 1] left0 = L2 + (z0 - H1) / math.tan(alpha) left1 = L2 + (z1 - H1) / math.tan(alpha) for ix in range(upper_cols): u0 = ix / upper_cols u1 = (ix + 1) / upper_cols x00 = left0 + u0 * (L1 - left0) x10 = left0 + u1 * (L1 - left0) x01 = left1 + u0 * (L1 - left1) x11 = left1 + u1 * (L1 - left1) n1 = add_node(x00, 0.0, z0) n2 = add_node(x10, 0.0, z0) n3 = add_node(x10, WIDTH, z0) n4 = add_node(x00, WIDTH, z0) n5 = add_node(x01, 0.0, z1) n6 = add_node(x11, 0.0, z1) n7 = add_node(x11, WIDTH, z1) n8 = add_node(x01, WIDTH, z1) elem_id = len(elements) + 1 elements.append((elem_id, n1, n2, n3, n4, n5, n6, n7, n8)) element_quads.append(((x00, z0), (x10, z0), (x11, z1), (x01, z1))) nsets: dict[str, list[int]] = { "ALLNODES": [], "BASE": [], "LEFT": [], "RIGHT": [], "CREST": [], "SLOPE_FACE": [], } tol = 1.0e-7 for node_id, x, _y, z in nodes: nsets["ALLNODES"].append(node_id) if abs(z) <= tol: nsets["BASE"].append(node_id) if abs(x) <= tol: nsets["LEFT"].append(node_id) if abs(x - L1) <= tol: nsets["RIGHT"].append(node_id) if abs(z - (H1 + H2)) <= tol: nsets["CREST"].append(node_id) if z >= H1 - tol: left = L2 + max(0.0, z - H1) / math.tan(alpha) if abs(x - left) <= 5.0e-6: nsets["SLOPE_FACE"].append(node_id) return lslope, nodes, elements, nsets def write_set(lines: list[str], name: str, values: list[int]) -> None: lines.append(f"*NSET, NSET={name}") sorted_values = sorted(values) for i in range(0, len(sorted_values), 12): lines.append(", ".join(str(value) for value in sorted_values[i : i + 12])) def write_input(path: Path, alpha_deg: float, mesh_size: float) -> None: lslope, nodes, elements, nsets = build_mesh(alpha_deg, mesh_size) lines: list[str] = [ "*HEADING", "FemLite strength reduction method benchmark from COMSOL geomechanics reference", "** PDF: models.geomech.strength_reduction_method.pdf", f"** PDF-SRM L1={L1:.6g} L2={L2:.6g} H1={H1:.6g} H2={H2:.6g}", f"** PDF-SRM DEFAULT_ALPHA={alpha_deg:.6g} LSLOPE={lslope:.9g}", f"** PDF-SRM MATERIAL_CASE=1 E=2.0E7 NU=0.3 C=2.0E4 PHI=25 PSI=0 RHO={RHO:.6g}", f"** PDF-SRM MESH_SIZE={mesh_size:.6g} WIDTH={WIDTH:.6g}", "** PDF-SRM CONFORMING_INTERFACE=YES LOWER_X_SPLIT_INCLUDES_UPPER_BASE=YES", "** PDF-SRM GRAVITY_DLOAD_GRAV=YES G=9.81 DIRECTION=0,0,-1", "*NODE", ] for node_id, x, y, z in nodes: lines.append(f"{node_id}, {x:.9e}, {y:.9e}, {z:.9e}") lines.append("*ELEMENT, TYPE=C3D8, ELSET=EMBANKMENT") for elem in elements: lines.append(", ".join(str(value) for value in elem)) for name, values in nsets.items(): write_set(lines, name, values) lines.extend( [ "*MATERIAL, NAME=MC_SOIL_CASE1", "*ELASTIC", "2.0E7, 0.3", "*DENSITY", f"{RHO:.9e}", "*MOHR COULOMB", "25.0, 2.0E4, 0.0, 0.0", "0.0, 0.0, 0.0, 1.0", "*SOLID SECTION, ELSET=EMBANKMENT, MATERIAL=MC_SOIL_CASE1", "*BOUNDARY", "ALLNODES, 2, 2, 0.0", "ALLNODES, 4, 6, 0.0", "BASE, 1, 3, 0.0", "LEFT, 1, 1, 0.0", "RIGHT, 1, 1, 0.0", "*STEP", "*STATIC", "*DLOAD", "EMBANKMENT, GRAV, 9.81, 0.0, 0.0, -1.0", ] ) lines.append("*END STEP") lines.append("") path.write_text("\n".join(lines), encoding="utf-8") def main() -> int: parser = argparse.ArgumentParser(description="Generate the strength reduction reference slope input.") parser.add_argument("--alpha", type=float, default=45.0) parser.add_argument("--mesh-size", type=float, default=0.75) parser.add_argument("--output", default="strength_reduction_slope_static.inp") args = parser.parse_args() out = Path(args.output) if not out.is_absolute(): out = Path(__file__).resolve().parent / out write_input(out, args.alpha, args.mesh_size) print(f"wrote {out}") return 0 if __name__ == "__main__": raise SystemExit(main())