1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Having trouble setting mesh size fields so that the mesh has a good transition from...

Discussão em 'Python' iniciado por Stack, Setembro 11, 2025.

  1. Stack

    Stack Membro Participativo

    In my gmsh Python code, which consists of several stacked cylinders and one cylinder inside, I would like to set the meshsize field(s) so that the transitions from fine mesh to coarse mesh is more gradual. I am using the distance and threshold fields in my example code, I can see the effect of DistMin, but DistMax does not seem to change the mesh.

    Here is my Python program:

    import gmsh
    import sys

    gmsh.initialize()
    gmsh.model.add("converted_geo")

    gmsh.model.occ.addCylinder(0.0, 0.0, 35.0, 0.0, 0.0, 30.0, 9.0, tag=1)
    gmsh.model.occ.addCylinder(0.0, 0.0, 0.0, 0.0, 0.0, 20.0, 120.0, tag=2)
    gmsh.model.occ.addCylinder(0.0, 0.0, 20.0, 0.0, 0.0, 15.0, 120.0, tag=3)
    gmsh.model.occ.addCylinder(0.0, 0.0, 35.0, 0.0, 0.0, 15.0, 120.0, tag=4)
    gmsh.model.occ.addCylinder(0.0, 0.0, -20.0, 0.0, 0.0, 20.0, 120.0, tag=5)

    gmsh.model.occ.synchronize()

    # Boolean Fragments
    v1 = gmsh.model.occ.fragment([(3, 2)], [(3, 5)])
    v2 = gmsh.model.occ.fragment([(3, 3)], [(3, 1), (3, 4)])
    v3 = gmsh.model.occ.fragment([(3, 5)], [(3, 2)])
    v4 = gmsh.model.occ.fragment([(3, 8)], [(3, 6), (3, 7), (3, 3)])

    # Add final large cylinder
    gmsh.model.occ.addCylinder(0.0, 0.0, 50.0, 0.0, 0.0, 120.0, 120.0, tag=9)
    gmsh.model.occ.synchronize()

    # Final Boolean Fragment
    v5 = gmsh.model.occ.fragment([(3, 9)], [(3, 2), (3, 3), (3, 5), (3, 6), (3, 7), (3, 8)])
    gmsh.model.occ.synchronize()

    # Mesh size settings
    gmsh.option.setNumber("Mesh.MeshSizeFromPoints", 0)
    gmsh.option.setNumber("Mesh.MeshSizeFromCurvature", 25)
    gmsh.option.setNumber("Mesh.MeshSizeExtendFromBoundary", 0)
    gmsh.option.setNumber("Mesh.MeshSizeMax", 20.0)
    gmsh.option.setNumber("Mesh.MeshSizeMin", 1.0)

    # Mesh fields
    gmsh.model.mesh.field.add("Distance", 1)
    gmsh.model.mesh.field.setNumbers(1, "FacesList", [3,18,19,20,21])
    gmsh.model.mesh.field.setNumber(1, "Sampling", 20)


    gmsh.model.mesh.field.add("Threshold", 2)
    gmsh.model.mesh.field.setNumber(2, "InField", 1)
    gmsh.model.mesh.field.setNumber(2, "SizeMin", 1)
    gmsh.model.mesh.field.setNumber(2, "SizeMax", 20)
    gmsh.model.mesh.field.setNumber(2, "DistMin", 10)
    gmsh.model.mesh.field.setNumber(2, "DistMax", 50)


    gmsh.model.mesh.field.add("Min", 5)
    gmsh.model.mesh.field.setNumbers(5, "FieldsList", [1, 2])
    gmsh.model.mesh.field.setAsBackgroundMesh(5)

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

    # Optional: Save
    gmsh.write("converted_mesh.msh")

    # Finalize
    if '-nopopup' not in sys.argv:
    gmsh.fltk.run()

    gmsh.finalize()


    I've also tried an extend meshsize field, but I don't think I can combine it with distance, it did not seem to help and seemed to make the mesh generation take longer. Perhaps I am using the wrong meshsize fields or some setting incorrectly. The mesh size change from min to max with my code seems too abrupt, but I do not know how to fix that.

    Continue reading...

Compartilhe esta Página