Esempio n. 1
0
def mesh_bar_gmshapi(
    name, Lx, Ly, lc, tdim, order=1, msh_file=None, comm=MPI.COMM_WORLD
):
    """
    Create mesh of 3d tensile test specimen according to ISO 6892-1:2019 using the Python API of Gmsh.
    """
    # Perform Gmsh work only on rank = 0

    if comm.rank == 0:

        import gmsh

        # Initialise gmsh and set options
        gmsh.initialize()
        gmsh.option.setNumber("General.Terminal", 1)

        # gmsh.option.setNumber("Mesh.Algorithm", 6)

        gmsh.option.setNumber("Mesh.Algorithm", 5)
        gmsh.model.mesh.optimize("Netgen")
        model = gmsh.model()
        model.add("Rectangle")
        model.setCurrent("Rectangle")
        p0 = model.geo.addPoint(0.0, 0.0, 0, lc, tag=0)
        p1 = model.geo.addPoint(Lx, 0.0, 0, lc, tag=1)
        p2 = model.geo.addPoint(Lx, Ly, 0.0, lc, tag=2)
        p3 = model.geo.addPoint(0, Ly, 0, lc, tag=3)
        # points = [p0, p1, p2, p3]
        bottom = model.geo.addLine(p0, p1)
        right = model.geo.addLine(p1, p2)
        top = model.geo.addLine(p2, p3)
        left = model.geo.addLine(p3, p0)
        cloop1 = model.geo.addCurveLoop([bottom, right, top, left])
        # surface_1 =
        model.geo.addPlaneSurface([cloop1])

        model.geo.synchronize()
        surface_entities = [model[1] for model in model.getEntities(tdim)]
        model.addPhysicalGroup(tdim, surface_entities, tag=5)
        model.setPhysicalName(tdim, 5, "Rectangle surface")

        # Set geometric order of mesh cells
        gmsh.model.mesh.setOrder(order)
        gmsh.model.addPhysicalGroup(tdim - 1, [3, 0], tag=6)
        gmsh.model.setPhysicalName(tdim - 1, 6, "left")
        gmsh.model.addPhysicalGroup(tdim - 1, [1, 2], tag=7)
        gmsh.model.setPhysicalName(tdim - 1, 7, "right")
        gmsh.model.addPhysicalGroup(tdim - 1, [2, 3], tag=8)
        gmsh.model.setPhysicalName(tdim - 1, 8, "top")
        gmsh.model.addPhysicalGroup(tdim - 1, [0, 1], tag=9)
        gmsh.model.setPhysicalName(tdim - 1, 9, "bottom")

        model.mesh.generate(tdim)

        # Optional: Write msh file
        if msh_file is not None:
            os.makedirs(os.path.dirname(msh_file), exist_ok=True)
            gmsh.write(msh_file)

    return gmsh.model if comm.rank == 0 else None, tdim
Esempio n. 2
0
def mesh_circle_gmshapi(name,
                        R,
                        lc,
                        tdim,
                        order=1,
                        msh_file=None,
                        comm=MPI.COMM_WORLD):
    """
    Create 2d circle mesh using the Python API of Gmsh.
    """
    # Perform Gmsh work only on rank = 0

    if comm.rank == 0:
        import gmsh

        # Initialise gmsh and set options
        gmsh.initialize()
        gmsh.option.setNumber("General.Terminal", 1)

        gmsh.option.setNumber("Mesh.Algorithm", 6)
        model = gmsh.model()
        model.add("Circle")
        model.setCurrent("Circle")
        p0 = model.geo.addPoint(0.0, 0.0, 0, lc, tag=0)
        p1 = model.geo.addPoint(R, 0.0, 0, lc, tag=1)
        p2 = model.geo.addPoint(0.0, R, 0.0, lc, tag=2)
        p3 = model.geo.addPoint(-R, 0, 0, lc, tag=3)
        p4 = model.geo.addPoint(0, -R, 0, lc, tag=4)
        # points = [p0, p1, p2, p3]
        c1 = gmsh.model.geo.addCircleArc(p1, p0, p2)
        c2 = gmsh.model.geo.addCircleArc(p2, p0, p3)
        c3 = gmsh.model.geo.addCircleArc(p3, p0, p4)
        c4 = gmsh.model.geo.addCircleArc(p4, p0, p1)

        circle = model.geo.addCurveLoop([c1, c2, c3, c4])
        model.geo.addPlaneSurface([circle])

        model.geo.synchronize()
        surface_entities = [model[1] for model in model.getEntities(tdim)]
        model.addPhysicalGroup(tdim, surface_entities, tag=5)
        model.setPhysicalName(tdim, 5, "Film surface")

        gmsh.model.mesh.setOrder(order)

        model.mesh.generate(tdim)

        # Optional: Write msh file
        if msh_file is not None:
            gmsh.write(msh_file)

    return gmsh.model if comm.rank == 0 else None, tdim
Esempio n. 3
0
import numpy as np
from dolfinx import cpp
from dolfinx.cpp.io import extract_local_entities, perm_gmsh
from dolfinx.io import (XDMFFile, extract_gmsh_geometry,
                        extract_gmsh_topology_and_markers, ufl_mesh_from_gmsh)
from dolfinx.mesh import create_mesh, create_meshtags
from mpi4py import MPI

import gmsh

# Generate a mesh on each rank with the gmsh API, and create a DOLFIN-X mesh
# on each rank. ::

gmsh.initialize()
gmsh.option.setNumber("General.Terminal", 0)
model = gmsh.model()
model.add("Sphere")
model.setCurrent("Sphere")
model.occ.addSphere(0, 0, 0, 1, tag=1)

# Generate mesh
model.occ.synchronize()
model.mesh.generate(3)

# Sort mesh nodes according to their index in gmsh (Starts at 1)
x = extract_gmsh_geometry(model, model_name="Sphere")

# Extract cells from gmsh (Only interested in tetrahedrons)
element_types, element_tags, node_tags = model.mesh.getElements(dim=3)
assert len(element_types) == 1
name, dim, order, num_nodes, local_coords, num_first_order_nodes = model.mesh.getElementProperties(
Esempio n. 4
0
def mesh_ep_gmshapi(name,
                    Lx,
                    Ly,
                    L0,
                    s,
                    lc,
                    tdim,
                    order=1,
                    msh_file=None,
                    sep=0.1,
                    comm=MPI.COMM_WORLD):
    if comm.rank == 0:

        import gmsh

        # Initialise gmsh and set options
        gmsh.initialize()
        gmsh.option.setNumber("General.Terminal", 1)

        gmsh.option.setNumber("Mesh.Algorithm", 6)
        model = gmsh.model()
        model.add("Rectangle")
        model.setCurrent("Rectangle")
        p0 = model.geo.addPoint(0.0, 0.0, 0, lc, tag=0)
        p1 = model.geo.addPoint(Lx, 0.0, 0, lc, tag=1)
        p2 = model.geo.addPoint(Lx, Ly, 0.0, lc, tag=2)
        p3 = model.geo.addPoint(0, Ly, 0, lc, tag=3)
        #pLa= model.geo.addPoint(0, Ly/2-s/2, 0, lc, tag=4)
        pRa = model.geo.addPoint(Lx, Ly / 2 + s / 2 - sep, 0, lc, tag=6)
        pRb = model.geo.addPoint(Lx, Ly / 2 + s / 2 + sep, 0, lc, tag=7)
        pLa = model.geo.addPoint(0, Ly / 2 - s / 2 - sep, 0, lc, tag=8)
        pLb = model.geo.addPoint(0, Ly / 2 - s / 2 + sep, 0, lc, tag=5)
        plM = model.geo.addPoint(L0, Ly / 2 - s / 2, 0, lc, tag=9)
        prM = model.geo.addPoint(Lx - L0, Ly / 2 + s / 2, 0, lc, tag=10)
        # points = [p0, p1, p2, p3]
        bottom = model.geo.addLine(p0, p1, tag=0)
        #right = model.geo.addLine(p1, p2, tag=1)
        rightB = model.geo.addLine(p1, pRa, tag=1)
        crackBR = model.geo.addLine(pRa, prM, tag=2)
        crackTR = model.geo.addLine(prM, pRb, tag=3)
        rightT = model.geo.addLine(pRb, p2, tag=4)
        top = model.geo.addLine(p2, p3, tag=5)
        #left=model.geo.addLine(p3, p0, tag=6)
        leftT = model.geo.addLine(p3, pLb, tag=6)
        crackTL = model.geo.addLine(pLb, plM, tag=7)
        crackBL = model.geo.addLine(plM, pLa, tag=8)
        leftB = model.geo.addLine(pLa, p0, tag=9)
        #cloop1 = model.geo.addCurveLoop([bottom, right, top, left])
        cloop1 = model.geo.addCurveLoop([
            crackTR, rightT, top, leftT, crackTL, crackBL, leftB, bottom,
            rightB, crackBR
        ])
        # surface_1 =
        model.geo.addPlaneSurface([cloop1])

        model.geo.synchronize()
        surface_entities = [model[1] for model in model.getEntities(tdim)]
        model.addPhysicalGroup(tdim, surface_entities, tag=5)
        model.setPhysicalName(tdim, 5, "Rectangle surface")

        # Set mesh size via points
        # gmsh.model.mesh.setSize(points, lc)  # heuristic

        # gmsh.model.mesh.optimize("Netgen")

        # Set geometric order of mesh cells
        gmsh.model.mesh.setOrder(order)

        # Define physical groups for subdomains (! target tag > 0)
        # domain = 1
        # gmsh.model.addPhysicalGroup(tdim, [v[1] for v in volumes], domain)
        # gmsh.model.setPhysicalName(tdim, domain, 'domain')
        #gmsh.model.addPhysicalGroup(tdim - 2, [9], tag=18)
        #gmsh.model.setPhysicalName(tdim - 2, 18, "nodeLeftMiddle")
        gmsh.model.addPhysicalGroup(tdim - 1, [0], tag=10)
        gmsh.model.setPhysicalName(tdim - 1, 10, "bottom")
        gmsh.model.addPhysicalGroup(tdim - 1, [5], tag=11)
        gmsh.model.setPhysicalName(tdim - 1, 11, "top")

        gmsh.model.addPhysicalGroup(tdim - 1, [6, 7, 8, 9], tag=12)
        #gmsh.model.addPhysicalGroup(tdim - 1, [6], tag=12)
        gmsh.model.setPhysicalName(tdim - 1, 12, "left")
        gmsh.model.addPhysicalGroup(tdim - 1, [1, 2, 3, 4], tag=13)
        #gmsh.model.addPhysicalGroup(tdim - 1, [1], tag=13)
        gmsh.model.setPhysicalName(tdim - 1, 13, "right")
        gmsh.model.addPhysicalGroup(tdim - 1, [7], tag=14)
        gmsh.model.setPhysicalName(tdim - 1, 14, "Lliptop")
        gmsh.model.addPhysicalGroup(tdim - 1, [8], tag=15)
        gmsh.model.setPhysicalName(tdim - 1, 15, "Llipbot")
        gmsh.model.addPhysicalGroup(tdim - 1, [2], tag=16)
        gmsh.model.setPhysicalName(tdim - 1, 16, "Rliptop")
        gmsh.model.addPhysicalGroup(tdim - 1, [3], tag=17)
        gmsh.model.setPhysicalName(tdim - 1, 17, "Rlipbot")

        model.mesh.generate(tdim)

        # Define physical groups for interfaces (! target tag > 0)
        # surface = 1
        # gmsh.model.addPhysicalGroup(tdim - 1, [s[1] for s in surfaces], surface)
        # gmsh.model.setPhysicalName(tdim - 1, surface, 'surface')
        """surface_grip_left = 2
        gmsh.model.addPhysicalGroup(tdim - 1, [s0], surface_grip_left)
        gmsh.model.setPhysicalName(tdim - 1, surface_grip_left, 'surface_grip_left')
        surface_grip_right = 3
        gmsh.model.addPhysicalGroup(tdim - 1, [s1], surface_grip_right)
        gmsh.model.setPhysicalName(tdim - 1, surface_grip_right, 'surface_grip_right')
        surface_plane_left = 4
        gmsh.model.addPhysicalGroup(tdim - 1, [s2], surface_plane_left)
        gmsh.model.setPhysicalName(tdim - 1, surface_plane_left, 'surface_plane_left')
        surface_plane_right = 5
        gmsh.model.addPhysicalGroup(tdim - 1, [s3], surface_plane_right)
        gmsh.model.setPhysicalName(tdim - 1, surface_plane_right, 'surface_plane_right')"""
        # Optional: Write msh file
        if msh_file is not None:
            gmsh.write(msh_file)
            # gmsh.write(name + ".step")

    return gmsh.model if comm.rank == 0 else None, tdim
Esempio n. 5
0
def mesh_bar_gmshapi(name,
                     Lx,
                     Ly,
                     lc,
                     tdim,
                     order=1,
                     msh_file=None,
                     comm=MPI.COMM_WORLD):
    """
    Create mesh of 3d tensile test specimen according to ISO 6892-1:2019 using the Python API of Gmsh.
    """
    # Perform Gmsh work only on rank = 0

    if comm.rank == 0:

        import gmsh

        # Initialise gmsh and set options
        gmsh.initialize()
        gmsh.option.setNumber("General.Terminal", 1)

        gmsh.option.setNumber("Mesh.Algorithm", 6)
        model = gmsh.model()
        model.add("Rectangle")
        model.setCurrent("Rectangle")
        p0 = model.geo.addPoint(0.0, 0.0, 0, lc, tag=0)
        p1 = model.geo.addPoint(Lx, 0.0, 0, lc, tag=1)
        p2 = model.geo.addPoint(Lx, Ly, 0.0, lc, tag=2)
        p3 = model.geo.addPoint(0, Ly, 0, lc, tag=3)
        # points = [p0, p1, p2, p3]
        bottom = model.geo.addLine(p0, p1, tag=0)
        right = model.geo.addLine(p1, p2, tag=1)
        top = model.geo.addLine(p2, p3, tag=2)
        left = model.geo.addLine(p3, p0, tag=3)
        cloop1 = model.geo.addCurveLoop([bottom, right, top, left])
        # surface_1 =
        model.geo.addPlaneSurface([cloop1])

        model.geo.synchronize()
        surface_entities = [model[1] for model in model.getEntities(tdim)]
        model.addPhysicalGroup(tdim, surface_entities, tag=5)
        model.setPhysicalName(tdim, 5, "Rectangle surface")

        # Set mesh size via points
        # gmsh.model.mesh.setSize(points, lc)  # heuristic

        # gmsh.model.mesh.optimize("Netgen")

        # Set geometric order of mesh cells
        gmsh.model.mesh.setOrder(order)

        # Define physical groups for subdomains (! target tag > 0)
        # domain = 1
        # gmsh.model.addPhysicalGroup(tdim, [v[1] for v in volumes], domain)
        # gmsh.model.setPhysicalName(tdim, domain, 'domain')
        gmsh.model.addPhysicalGroup(tdim - 1, [3], tag=6)
        gmsh.model.setPhysicalName(tdim - 1, 6, "left")
        gmsh.model.addPhysicalGroup(tdim - 1, [1], tag=7)
        gmsh.model.setPhysicalName(tdim - 1, 7, "right")
        gmsh.model.addPhysicalGroup(tdim - 1, [2], tag=8)
        gmsh.model.setPhysicalName(tdim - 1, 8, "top")
        gmsh.model.addPhysicalGroup(tdim - 1, [0], tag=9)
        gmsh.model.setPhysicalName(tdim - 1, 9, "bottom")

        model.mesh.generate(tdim)

        # Define physical groups for interfaces (! target tag > 0)
        # surface = 1
        # gmsh.model.addPhysicalGroup(tdim - 1, [s[1] for s in surfaces], surface)
        # gmsh.model.setPhysicalName(tdim - 1, surface, 'surface')
        """surface_grip_left = 2
        gmsh.model.addPhysicalGroup(tdim - 1, [s0], surface_grip_left)
        gmsh.model.setPhysicalName(tdim - 1, surface_grip_left, 'surface_grip_left')
        surface_grip_right = 3
        gmsh.model.addPhysicalGroup(tdim - 1, [s1], surface_grip_right)
        gmsh.model.setPhysicalName(tdim - 1, surface_grip_right, 'surface_grip_right')
        surface_plane_left = 4
        gmsh.model.addPhysicalGroup(tdim - 1, [s2], surface_plane_left)
        gmsh.model.setPhysicalName(tdim - 1, surface_plane_left, 'surface_plane_left')
        surface_plane_right = 5
        gmsh.model.addPhysicalGroup(tdim - 1, [s3], surface_plane_right)
        gmsh.model.setPhysicalName(tdim - 1, surface_plane_right, 'surface_plane_right')"""

        # Optional: Write msh file
        if msh_file is not None:
            gmsh.write(msh_file)
            # gmsh.write(name + ".step")

    return gmsh.model if comm.rank == 0 else None, tdim
Esempio n. 6
0
def mesh_V(
    a,
    h,
    L,
    n,
    gamma,
    de,
    de2,
    key=0,
    show=False,
    filename='mesh.unv',
    order=1,
):
    """
    Create a 2D mesh of a notched three-point flexure specimen using GMSH.
    a = height of the notch
    h = height of the specimen
    L = width of the specimen
    n = width of the load interface
    gamma = notch angle
    de = density of elements at specimen
    de2 = density of elements at the notch and crack
    key = 0 -> create model for Fenicxs (default)
          1 -> create model for Cast3M
    show = False -> doesn't open Gmsh to vizualise the mesh (default)
           True -> open Gmsh to vizualise the mesh
    filename = name and format of the output file for key = 1 
    order = order of the function of form
    """
    gmsh.initialize()
    gmsh.option.setNumber("General.Terminal", 1)
    gmsh.option.setNumber("Mesh.Algorithm", 5)
    hopen = a * np.tan((gamma / 2.0) * np.pi / 180)
    c0 = h / 40
    load_len = n
    tdim = 2

    model = gmsh.model()
    model.add('TPB')
    model.setCurrent('TPB')
    #Generating the points of the geometrie
    p0 = model.geo.addPoint(0.0, a, 0.0, de2, tag=0)
    p1 = model.geo.addPoint(hopen, 0.0, 0.0, de, tag=1)
    p2 = model.geo.addPoint(L / 2, 0.0, 0.0, de, tag=2)
    p3 = model.geo.addPoint(L / 2, h, 0.0, de, tag=3)
    p4 = model.geo.addPoint(0.0, h, 0.0, de, tag=4)
    if key == 0:
        p5 = model.geo.addPoint(-L / 2, h, 0.0, de, tag=5)
        p6 = model.geo.addPoint(-L / 2, 0.0, 0.0, de, tag=6)
        p7 = model.geo.addPoint(-hopen, 0.0, 0.0, de, tag=7)
        #Load facet
        p21 = model.geo.addPoint(load_len, h, 0.0, de, tag=30)
        p22 = model.geo.addPoint(-load_len, h, 0.0, de, tag=31)
    elif key == 1:
        p20 = model.geo.addPoint(0, a + c0, 0, de2, tag=20)
    #Creating the lines by connecting the points
    notch_right = model.geo.addLine(p0, p1, tag=8)
    bot_right = model.geo.addLine(p1, p2, tag=9)
    right = model.geo.addLine(p2, p3, tag=10)
    #top_right = model.geo.addLine(p3, p4, tag=11)
    if key == 0:
        top_right = model.geo.addLine(p3, p21, tag=11)
        top_left = model.geo.addLine(p22, p5, tag=12)
        left = model.geo.addLine(p5, p6, tag=13)
        bot_left = model.geo.addLine(p6, p7, tag=14)
        notch_left = model.geo.addLine(p7, p0, tag=15)
        #Load facet
        load_right = model.geo.addLine(p21, p4, tag=32)
        load_left = model.geo.addLine(p4, p22, tag=33)
    elif key == 1:
        top_right = model.geo.addLine(p3, p4, tag=11)
        sym_plan = model.geo.addLine(p4, p20, tag=21)
        fissure = model.geo.addLine(p20, p0, tag=22)
    #Creating the surface using the lines created
    if key == 0:
        perimeter = model.geo.addCurveLoop([
            notch_right, bot_right, right, top_right, load_right, load_left,
            top_left, left, bot_left, notch_left
        ])
    elif key == 1:
        perimeter = model.geo.addCurveLoop(
            [notch_right, bot_right, right, top_right, sym_plan, fissure])
    surface = model.geo.addPlaneSurface([perimeter])
    #model.geo.addSurfaceLoop([surface,16])
    model.mesh.setOrder(order)

    #Creating Physical Groups to extract data from the geometrie
    if key == 0:
        gmsh.model.addPhysicalGroup(tdim - 1, [left], tag=101)
        gmsh.model.setPhysicalName(tdim - 1, 101, 'Left')

        gmsh.model.addPhysicalGroup(tdim - 1, [right], tag=102)
        gmsh.model.setPhysicalName(tdim - 1, 102, 'Right')

        gmsh.model.addPhysicalGroup(tdim - 2, [p6], tag=103)
        gmsh.model.setPhysicalName(tdim - 2, 103, 'Left_point')

        gmsh.model.addPhysicalGroup(tdim - 2, [p2], tag=104)
        gmsh.model.setPhysicalName(tdim - 2, 104, 'Right_point')

        gmsh.model.addPhysicalGroup(tdim - 2, [p4], tag=105)
        gmsh.model.setPhysicalName(tdim - 2, 105, 'Load_point')

        gmsh.model.addPhysicalGroup(tdim - 2, [p0], tag=106)
        gmsh.model.setPhysicalName(tdim - 2, 106, 'Notch_point')

        gmsh.model.addPhysicalGroup(tdim - 1, [load_right], tag=107)
        gmsh.model.setPhysicalName(tdim - 1, 107, 'load_right')

        gmsh.model.addPhysicalGroup(tdim - 1, [load_left], tag=108)
        gmsh.model.setPhysicalName(tdim - 1, 108, 'load_left')

        gmsh.model.addPhysicalGroup(tdim, [surface], tag=110)
        gmsh.model.setPhysicalName(tdim, 110, 'mesh_surface')

    #Cast3M can't read Physical Groups of points (dim = 0). Instead, we check the number in the mesh and input in manually in the code.
    #The number of a node doesn't change if it's in a point of the geometry
    if key == 1:
        gmsh.model.addPhysicalGroup(tdim, [surface], tag=110)
        gmsh.model.setPhysicalName(tdim, 110, 'mesh_surface')

        gmsh.model.addPhysicalGroup(tdim - 1, [fissure], tag=111)
        gmsh.model.setPhysicalName(tdim - 1, 111, 'fissure')

        gmsh.model.addPhysicalGroup(tdim - 1, [sym_plan], tag=112)
        gmsh.model.setPhysicalName(tdim - 1, 112, 'sym_plan')

        #gmsh.model.addPhysicalGroup(tdim-2, [p20], tag=113)
        #gmsh.model.setPhysicalName(tdim-2, 113, 'Crack_tip')

        #gmsh.model.addPhysicalGroup(tdim-2, [p4], tag=114)
        #gmsh.model.setPhysicalName(tdim-2, 114, 'Load_point')

        #gmsh.model.addPhysicalGroup(tdim-2, [p2], tag=115)
        #gmsh.model.setPhysicalName(tdim-2, 115,'Right_point')
    #Generating the mesh
    model.geo.synchronize()
    model.mesh.generate(tdim)
    if show:
        gmsh.fltk.run()
    if key == 1:
        gmsh.write(filename)
    return gmsh.model
Esempio n. 7
0
def mesh_tdcb(
    name,
    geom_parameters,
    lc,
    tdim=2,
    order=1,
    msh_file=None,
    comm=MPI.COMM_WORLD,
):
    """
    Create mesh of 2d TDCB test specimen according to ... using the Python API of Gmsh.
    """
    # Perform Gmsh work only on rank = 0

    if comm.rank == 0:

        import gmsh

        # Initialise gmsh and set options
        gmsh.initialize()
        gmsh.option.setNumber("General.Terminal", 1)

        gmsh.option.setNumber("Mesh.Algorithm", 5)
        model = gmsh.model()

        eta = geom_parameters.get("eta")
        Lx = geom_parameters.get("Lx")
        h1 = geom_parameters.get("L1")
        h2 = geom_parameters.get("L2")
        a0 = geom_parameters.get("Lcrack")
        cx = geom_parameters.get("Cx")
        cy = geom_parameters.get("Cy")
        rad = geom_parameters.get("rad")

        # pdb.set_trace()
        p1 = model.geo.addPoint(0, eta, 0, lc, tag=1)
        p100 = model.geo.addPoint(0, -eta, 0, lc, tag=100)
        p2 = model.geo.addPoint(0, h1 / 2.0, 0, lc, tag=2)
        p200 = model.geo.addPoint(0, -h1 / 2.0, 0, lc, tag=200)
        p3 = model.geo.addPoint(Lx, h2 / 2.0, 0, lc, tag=3)
        p300 = model.geo.addPoint(Lx, -h2 / 2.0, 0, lc, tag=300)
        p4 = model.geo.addPoint(a0, eta, 0, lc, tag=4)
        p400 = model.geo.addPoint(a0, -eta, 0, lc, tag=400)

        p5 = model.geo.addPoint(cx, cy, 0.0, lc, tag=5)
        p500 = model.geo.addPoint(cx, cy - rad, 0.0, lc, tag=500)
        p501 = model.geo.addPoint(cx, cy + rad, 0.0, lc, tag=501)
        p502 = model.geo.addPoint(cx - rad, cy, 0.0, lc, tag=502)
        p503 = model.geo.addPoint(cx + rad, cy, 0.0, lc, tag=503)
        p6 = model.geo.addPoint(cx, -cy, 0.0, lc, tag=6)
        p600 = model.geo.addPoint(cx, -cy + rad, 0.0, lc, tag=600)
        p601 = model.geo.addPoint(cx, -cy - rad, 0.0, lc, tag=601)
        p602 = model.geo.addPoint(cx - rad, -cy, 0.0, lc, tag=602)
        p603 = model.geo.addPoint(cx + rad, -cy, 0.0, lc, tag=603)

        # left = model.geo.addLine(p200, p2, tag=5)
        left_top = model.geo.addLine(p1, p2, tag=5)
        top = model.geo.addLine(p2, p3, tag=6)
        right = model.geo.addLine(p3, p300, tag=7)
        bottom = model.geo.addLine(p300, p200, tag=8)
        left_bottom = model.geo.addLine(p200, p100, tag=9)
        notch_bottom = model.geo.addLine(p100, p400, tag=10)
        notch_tip = model.geo.addLine(p400, p4, tag=11)
        notch_top = model.geo.addLine(p4, p1, tag=13)

        model.geo.addCircleArc(501, 5, 503, tag=14)
        model.geo.addCircleArc(503, 5, 500, tag=15)
        model.geo.addCircleArc(500, 5, 502, tag=16)
        model.geo.addCircleArc(502, 5, 501, tag=17)
        model.geo.addCircleArc(600, 6, 603, tag=18)
        model.geo.addCircleArc(603, 6, 601, tag=19)
        model.geo.addCircleArc(601, 6, 602, tag=20)
        model.geo.addCircleArc(602, 6, 600, tag=21)

        cloop1 = model.geo.addCurveLoop([
            left_top,
            top,
            right,
            bottom,
            # left,
            left_bottom,
            notch_bottom,
            notch_tip,
            notch_top,
        ])

        cloop2 = model.geo.addCurveLoop([15, 16, 17, 14])
        cloop3 = model.geo.addCurveLoop([18, 19, 20, 21])

        s = model.geo.addPlaneSurface([cloop1, cloop2, cloop3])
        model.geo.addSurfaceLoop([s, 1000])
        model.geo.synchronize()
        surface_entities = [model[1] for model in model.getEntities(tdim)]
        model.addPhysicalGroup(tdim, surface_entities, tag=1)
        model.setPhysicalName(tdim, 1, "TDCB surface")

        # Define physical groups for subdomains (! target tag > 0)

        gmsh.model.addPhysicalGroup(tdim - 1, [15, 16, 17, 14], tag=2)
        gmsh.model.setPhysicalName(tdim - 1, 2, "top_pin")

        gmsh.model.addPhysicalGroup(tdim - 1, [18, 19, 20, 21], tag=3)
        gmsh.model.setPhysicalName(tdim - 1, 3, "bottom_pin")

        gmsh.model.addPhysicalGroup(tdim - 1, [6], tag=4)
        gmsh.model.setPhysicalName(tdim - 1, 4, "top_boundary")

        gmsh.model.addPhysicalGroup(tdim - 1, [8], tag=5)
        gmsh.model.setPhysicalName(tdim - 1, 5, "bottom_boundary")

        model.mesh.generate(tdim)

        cell_tag_names = {"Domain": 1}

        facet_tag_names = {
            "top_pin": 2,
            "bottom_pin": 3,
            "top_boundary": 4,
            "bottom_boundary": 5
        }

        # Optional: Write msh file
        if msh_file is not None:
            gmsh.write(msh_file)

    tag_names = {"facets": facet_tag_names, "cells": cell_tag_names}

    return gmsh.model if comm.rank == 0 else None, tdim, tag_names
Esempio n. 8
0
def build_piston(n, length, radius, write_file=False, fname=""):
    """
    Build a hex cylinder mesh using gmsh
    """
    gmsh.initialize()
    gmsh.option.setNumber("General.Terminal", 0)
    model = gmsh.model()

    if MPI.COMM_WORLD.rank == 0:
        model.add("Piston")
        model.setCurrent("Piston")

        h = length / n

        gmsh.option.setNumber("Mesh.RecombineAll", 2)
        gmsh.option.setNumber("Mesh.RecombinationAlgorithm", 2)
        gmsh.option.setNumber("Mesh.CharacteristicLengthMin", 0.98 * h)
        gmsh.option.setNumber("Mesh.CharacteristicLengthMax", 1.02 * h)

        circle = model.occ.addDisk(0, 0, 0, radius, radius)
        model.occ.rotate([(2, circle)], 0., 0., 0., 0., 1., 0., np.pi / 2)
        model.occ.extrude([(2, circle)],
                          length,
                          0,
                          0,
                          numElements=[n],
                          recombine=True)

        model.occ.synchronize()
        model.mesh.generate(3)

        # sort mesh according to their index in gmsh
        x = extract_gmsh_geometry(model, model.getCurrent())

        # extract cells from gmsh
        element_types, element_tags, node_tags = model.mesh.getElements(dim=3)
        name, dim, order, num_nodes, local_coords, num_first_order_nodes = \
            model.mesh.getElementProperties(element_types[0])

        # broadcast cell type data and geometric dimension
        gmsh_cell_id = MPI.COMM_WORLD.bcast(element_types[0], root=0)

        # get mesh data for dim (0, tdim)
        cells = node_tags[0].reshape(-1, num_nodes) - 1

        num_nodes = MPI.COMM_WORLD.bcast(cells.shape[1], root=0)
        gmsh.finalize()
    else:
        gmsh_cell_id = MPI.COMM_WORLD.bcast(None, root=0)
        num_nodes = MPI.COMM_WORLD.bcast(None, root=0)
        cells, x = np.empty([0, num_nodes]), np.empty([0, 3])

    # permute the mesh topology from GMSH ordering to DOLFIN-X ordering
    domain = ufl_mesh_from_gmsh(gmsh_cell_id, 3)
    gmsh_hex = perm_gmsh(cpp.mesh.CellType.hexahedron, 8)
    cells = cells[:, gmsh_hex]

    mesh = create_mesh(MPI.COMM_WORLD, cells, x, domain)
    mesh.name = "piston_hex"

    if write_file:
        with XDMFFile(MPI.COMM_WORLD, "{}.xdmf".format(fname), "w") as file:
            file.write_mesh(mesh)

    return mesh
Esempio n. 9
0
def mesh_V_notch(
        name,
        L,
        L_crack,
        theta,
        lc,
        shift=(0, 0),
        tdim=2,
        order=1,
        msh_file=None,
        comm=MPI.COMM_WORLD,
):
    """
    Create mesh of 3d tensile test specimen according to ISO 6892-1:2019 using the Python API of Gmsh.
    """
    # Perform Gmsh work only on rank = 0

    if comm.rank == 0:

        import gmsh

        # Initialise gmsh and set options
        gmsh.initialize()
        gmsh.option.setNumber("General.Terminal", 1)

        gmsh.option.setNumber("Mesh.Algorithm", 5)
        model = gmsh.model()

        p0 = model.geo.addPoint(shift[0] + 0.0, shift[1] + 0.0, 0, lc, tag=0)
        p1 = model.geo.addPoint(shift[0] + 0.0, shift[1] + L, 0.0, lc, tag=1)
        p2 = model.geo.addPoint(shift[0] + L, shift[1] + L, 0.0, lc, tag=2)
        p3 = model.geo.addPoint(shift[0] + L, shift[1] + 0.0, 0.0, lc, tag=3)
        p4 = model.geo.addPoint(shift[0] + L_crack,
                                shift[1] + L / 2.0,
                                0.0,
                                lc,
                                tag=4)
        p5 = model.geo.addPoint(
            shift[0] + 0.0,
            shift[1] + L / 2.0 + L_crack * np.tan(theta / 2.0),
            0.0,
            lc,
            tag=5,
        )
        p6 = model.geo.addPoint(
            shift[0] + 0.0,
            shift[1] + L / 2.0 - L_crack * np.tan(theta / 2.0),
            0.0,
            lc,
            tag=6,
        )

        top = model.geo.addLine(p1, p2, tag=7)
        right = model.geo.addLine(p2, p3, tag=8)
        bottom = model.geo.addLine(p3, p0, tag=9)
        left_bottom = model.geo.addLine(p0, p6, tag=10)
        left_top = model.geo.addLine(p5, p1, tag=11)
        notch_top = model.geo.addLine(p6, p4, tag=12)
        notch_bottom = model.geo.addLine(p4, p5, tag=13)

        cell_tag_names = {"Domain": 15}

        cloop = model.geo.addCurveLoop([
            top, right, bottom, left_bottom, notch_bottom, notch_top, left_top
        ])

        s = model.geo.addPlaneSurface([cloop])
        model.geo.addSurfaceLoop([s, 14])
        model.geo.synchronize()
        surface_entities = [model[1] for model in model.getEntities(tdim)]
        model.addPhysicalGroup(tdim, surface_entities, tag=15)
        model.setPhysicalName(tdim, 15, "Square surface")

        # Define physical groups for subdomains (! target tag > 0)
        # gmsh.model.addPhysicalGroup(tdim - 1, [10, 11], tag=16)
        # gmsh.model.setPhysicalName(tdim - 1, 16, "left")
        # gmsh.model.addPhysicalGroup(tdim - 1, [7], tag=17)
        # gmsh.model.setPhysicalName(tdim - 1, 17, "top")
        # gmsh.model.addPhysicalGroup(tdim - 1, [8], tag=18)
        # gmsh.model.setPhysicalName(tdim - 1, 18, "right")
        # gmsh.model.addPhysicalGroup(tdim - 1, [9], tag=19)
        # gmsh.model.setPhysicalName(tdim - 1, 19, "bottom")
        # gmsh.model.addPhysicalGroup(tdim - 1, [12, 13], tag=20)
        # gmsh.model.setPhysicalName(tdim - 1, 20, "notch")
        gmsh.model.addPhysicalGroup(tdim - 1, [10, 11, 7, 8, 9], tag=100)
        gmsh.model.setPhysicalName(tdim - 1, 100, "extboundary")

        model.mesh.generate(tdim)

        facet_tag_names = {'extboundary': 100}
        tag_names = {"facets": facet_tag_names, "cells": cell_tag_names}

        # Optional: Write msh file
        if msh_file is not None:
            gmsh.write(msh_file)

    return gmsh.model if comm.rank == 0 else None, tdim, tag_names