|
| 1 | +import json |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import gmsh |
| 5 | + |
| 6 | +RESULTS_DIR = "results_incompressible_q1p1" |
| 7 | + |
| 8 | +def generate_beam2D(length, height, nx, ny, filename): |
| 9 | + """ |
| 10 | + Generate a 2D beam mesh (triangular elements) |
| 11 | + """ |
| 12 | + gmsh.initialize() |
| 13 | + gmsh.model.add("beam2d") |
| 14 | + |
| 15 | + # Corner points (tag order matches border traversal in the .edp) |
| 16 | + gmsh.model.geo.addPoint(0, 0, 0, tag=1) |
| 17 | + gmsh.model.geo.addPoint(length, 0, 0, tag=2) |
| 18 | + gmsh.model.geo.addPoint(length, height, 0, tag=3) |
| 19 | + gmsh.model.geo.addPoint(0, height, 0, tag=4) |
| 20 | + |
| 21 | + # Boundary lines — tags chosen to match .edp labels directly |
| 22 | + bottom = gmsh.model.geo.addLine(1, 2, tag=2) # label 2, y=0 |
| 23 | + right = gmsh.model.geo.addLine(2, 3, tag=3) # label 3, x=length |
| 24 | + top = gmsh.model.geo.addLine(3, 4, tag=4) # label 4, y=height |
| 25 | + left = gmsh.model.geo.addLine(4, 1, tag=1) # label 1, x=0 (clamped) |
| 26 | + |
| 27 | + loop = gmsh.model.geo.addCurveLoop([bottom, right, top, left], tag=1) |
| 28 | + surf = gmsh.model.geo.addPlaneSurface([loop], tag=1) |
| 29 | + |
| 30 | + gmsh.model.geo.synchronize() |
| 31 | + |
| 32 | + # Physical groups — boundary tags match .edp labels 1–4 |
| 33 | + gmsh.model.addPhysicalGroup(1, [left], tag=1, name="Fixed") |
| 34 | + gmsh.model.addPhysicalGroup(1, [bottom], tag=2, name="Bottom") |
| 35 | + gmsh.model.addPhysicalGroup(1, [right], tag=3, name="Right") |
| 36 | + gmsh.model.addPhysicalGroup(1, [top], tag=4, name="Top") |
| 37 | + gmsh.model.addPhysicalGroup(2, [surf], tag=5, name="Beam") |
| 38 | + |
| 39 | + # Structured transfinite layout; no recombine → triangular elements |
| 40 | + gmsh.model.mesh.setTransfiniteCurve(bottom, nx) |
| 41 | + gmsh.model.mesh.setTransfiniteCurve(top, nx) |
| 42 | + gmsh.model.mesh.setTransfiniteCurve(left, ny) |
| 43 | + gmsh.model.mesh.setTransfiniteCurve(right, ny) |
| 44 | + gmsh.model.mesh.setTransfiniteSurface(surf) |
| 45 | + |
| 46 | + gmsh.model.mesh.generate(2) |
| 47 | + gmsh.model.mesh.setOrder(1) # ensure linear (first-order) elements only |
| 48 | + _, node_coords, _ = gmsh.model.mesh.getNodes() |
| 49 | + x_positions = node_coords[0::3] |
| 50 | + y_positions = node_coords[1::3] |
| 51 | + |
| 52 | + os.makedirs(RESULTS_DIR, exist_ok=True) |
| 53 | + msh_path = os.path.join(RESULTS_DIR, filename) |
| 54 | + gmsh.option.setNumber("Mesh.MshFileVersion", 2.2) # gmshload requires MSH v2 |
| 55 | + gmsh.write(msh_path) |
| 56 | + gmsh.finalize() |
| 57 | + |
| 58 | + return msh_path, x_positions, y_positions |
| 59 | + |
| 60 | + |
| 61 | +if __name__ == "__main__": |
| 62 | + config_file = sys.argv[1] if len(sys.argv) > 1 else "params.json" |
| 63 | + with open(config_file) as f: |
| 64 | + cfg = json.load(f) |
| 65 | + |
| 66 | + msh_path, x_positions, y_positions = generate_beam2D( |
| 67 | + length=float(cfg["length"]), |
| 68 | + height=float(cfg["height"]), |
| 69 | + nx=int(cfg["nx"]), |
| 70 | + ny=int(cfg["ny"]), |
| 71 | + filename=cfg.get("meshfile"), |
| 72 | + ) |
| 73 | + print("Wrote:", msh_path) |
| 74 | + print("Node x-positions (Gmsh order):", x_positions) |
| 75 | + print("Node y-positions (Gmsh order):", y_positions) |
0 commit comments