[Python] GMSH with Python API - Extrusion of nodes and calculus time

Discussão em 'Python' iniciado por Stack, Julho 8, 2025.

  1. Stack

    Stack Membro Participativo

    I am currently working with the GMSH Python API, to build nodes and elements based on an extrusion of a reference surface.

    As an input, I have a dictionary of nodes and a dictionary of elements, which form a rectangle meshed with quadrangle elements. From this, I build surfaces thanks to the addPlaneSurface command. This surface represents the neutral fiber, and I want to extrude following z and -z directions, in order to build plies.

    The code (see below) works, but after a certain number of plies (eight, indeed), computational time becomes very important, which is a big issue in my project.

    After studying the variable explorer once my code is executed, I noticed that the length of the lists containing the surfaces increases with the number of plies, which seems odd: in my mind, the number of surfaces extruded should be the same from one ply to another, as it’s always the same dimension of the plate that is considered.

    I think that the increase of the computation time comes from this increasing length of the list, but I don’t understand the cause of this when I look at my code, because the list is supposed to be overwritten at each iteration of the loop.

    You will find my code below.

    May someone can help me?

    import gmsh

    # Initialiser Gmsh

    gmsh.initialize()

    gmsh.model.add("Plaque Composite")


    # Exemple de dictionnaire de nœuds (au niveau de la fibre neutre)

    nodes_2d = {

    1: [0, 0, 0], 2: [2, 0, 0], 3: [4, 0, 0], 4: [6, 0, 0], 5: [8, 0, 0], 6: [10, 0, 0],

    7: [0, 2, 0], 8: [2, 2, 0], 9: [4, 2, 0], 10: [6, 2, 0], 11: [8, 2, 0], 12: [10, 2, 0],

    13: [0, 4, 0], 14: [2, 4, 0], 15: [4, 4, 0], 16: [6, 4, 0], 17: [8, 4, 0], 18: [10, 4, 0],

    19: [0, 6, 0], 20: [2, 6, 0], 21: [4, 6, 0], 22: [6, 6, 0], 23: [8, 6, 0], 24: [10, 6, 0],

    25: [0, 8, 0], 26: [2, 8, 0], 27: [4, 8, 0], 28: [6, 8, 0], 29: [8, 8, 0], 30: [10, 8, 0],

    31: [0, 10, 0], 32: [2, 10, 0], 33: [4, 10, 0], 34: [6, 10, 0], 35: [8, 10, 0], 36: [10, 10, 0]

    }


    elements_2d = {

    1: [1, 2, 8, 7], 2: [2, 3, 9, 8], 3: [3, 4, 10, 9], 4: [4, 5, 11, 10], 5: [5, 6, 12, 11],

    6: [7, 8, 14, 13], 7: [8, 9, 15, 14], 8: [9, 10, 16, 15], 9: [10, 11, 17, 16], 10: [11, 12, 18, 17],

    11: [13, 14, 20, 19], 12: [14, 15, 21, 20], 13: [15, 16, 22, 21], 14: [16, 17, 23, 22], 15: [17, 18, 24, 23],

    16: [19, 20, 26, 25], 17: [20, 21, 27, 26], 18: [21, 22, 28, 27], 19: [22, 23, 29, 28], 20: [23, 24, 30, 29],

    21: [25, 26, 32, 31], 22: [26, 27, 33, 32], 23: [27, 28, 34, 33], 24: [28, 29, 35, 34], 25: [29, 30, 36, 35]

    }


    # Vérifier que le nombre de plis est pair

    plies = 6 # Doit être pair

    if plies % 2 != 0:

    raise ValueError("Le nombre de plis doit être pair.")


    # Épaisseur de chaque pli

    ply_thickness = 0.1


    # Calculer le nombre de plis au-dessus et en-dessous de la fibre neutre

    plies_above = plies_below = plies // 2


    # Créer les points dans Gmsh à partir du dictionnaire des nœuds

    for node_id, coords in nodes_2d.items():

    gmsh.model.occ.addPoint(*coords)


    # Créer les lignes et surfaces dans Gmsh à partir des éléments

    surfaces = []

    for elem_id, node_ids in elements_2d.items():

    line_ids = []

    for i in range(4):

    start_node = node_ids

    end_node = node_ids[(i + 1) % 4]

    line_id = gmsh.model.occ.addLine(start_node, end_node)

    line_ids.append(line_id)

    loop_id = gmsh.model.occ.addCurveLoop(line_ids)

    surface_id = gmsh.model.occ.addPlaneSurface([loop_id])

    surfaces.append(surface_id)


    # Synchroniser les entités créées

    gmsh.model.occ.synchronize()


    # Extrusion des plis

    extruded_surfaces_above = surfaces

    extruded_surfaces_below = surfaces


    # Extruder les plis au-dessus de la fibre neutre

    for i in range(plies_above):

    extruded_volumes_above = gmsh.model.occ.extrude([(2, s_id) for s_id in extruded_surfaces_above], 0, 0, ply_thickness)

    extruded_surfaces_above = [vol[1] for vol in extruded_volumes_above if vol[0] == 2]

    print("extruded_surfaces_above", extruded_surfaces_above)


    # Extruder les plis en-dessous de la fibre neutre

    for i in range(plies_below):

    extruded_volumes_below = gmsh.model.occ.extrude([(2, s_id) for s_id in extruded_surfaces_below], 0, 0, -ply_thickness)

    extruded_surfaces_below = [vol[1] for vol in extruded_volumes_below if vol[0] == 2]

    print("extruded_surfaces_below", extruded_surfaces_below)


    # Synchroniser les changements

    gmsh.model.occ.synchronize()


    # Afficher le modèle dans Gmsh

    gmsh.fltk.run()


    # Finaliser Gmsh

    gmsh.finalize()

    Continue reading...

Compartilhe esta Página