Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reducing mesh elements on a flat surface #37

Open
shimwell opened this issue Jun 27, 2023 · 3 comments · May be fixed by #39
Open

reducing mesh elements on a flat surface #37

shimwell opened this issue Jun 27, 2023 · 3 comments · May be fixed by #39

Comments

@shimwell
Copy link
Member

Looks like we can reduce the mesh element count by setting mesh.MeshSizeFactor to a large number.

  • [] check curved shapes
  • [] find a suitable default value
@shimwell shimwell changed the title try mesh.MeshSizeFactor for flat faces reducing mesh elements on a flat surface Jun 27, 2023
@shimwell
Copy link
Member Author

mesh.MeshSizeExtendFromBoundary appears to accept a bool alue and can grow the elements differently to their boundary size.

Screenshot from 2023-06-27 10-48-34
Screenshot from 2023-06-27 10-48-51

@shimwell
Copy link
Member Author

shimwell commented Jun 28, 2023

Example script takes the elements down from 683 to 330 by including the MeshSizeExtendFromBoundary line

import cadquery as cq
small = cq.Workplane().box(1, 1, 1)
big = cq.Workplane(origin=(1,0,0)).box(1,2,2)
assy = cq.Assembly()
assy.add(small)
assy.add(big)
r, o = cq.occ_impl.assembly.imprint(assy)
print(len(r.Faces()))  # prints 12 which is correct
topods = r.wrapped
object_to_pass_to_gmsh=topods._address()

import gmsh
gmsh.initialize()
gmsh.option.setNumber("General.Terminal", 1)
volumes = gmsh.model.occ.importShapesNativePointer(object_to_pass_to_gmsh)
gmsh.model.occ.synchronize()
gmsh.option.setNumber("Mesh.Algorithm", 1)
gmsh.option.setNumber("Mesh.MeshSizeMin", 0.2)
gmsh.option.setNumber("Mesh.MeshSizeMax", 30)
gmsh.option.setNumber("Mesh.MeshSizeExtendFromBoundary", False)
gmsh.model.mesh.generate(2)
gmsh.write("gmsh_of_cadquery_in_memory.msh")

@shimwell
Copy link
Member Author

Working a bit on this recently as the above method was not working well

import gmsh
import sys

def create_geometry():
    # Initialize gmsh
    gmsh.initialize(sys.argv)
    
    # Create a new model
    gmsh.model.add("sphere_and_box_union")
    
    # Create sphere (radius = 1.0)
    sphere = gmsh.model.occ.addSphere(0, 0, 0, 1.0)
    
    # Create box with less overlap
    # Move it to just slightly overlap with the sphere
    box = gmsh.model.occ.addBox(0.7, -1, -1, 2, 2, 2)
    
    # Create the union of the two shapes
    union = gmsh.model.occ.fuse([(3, sphere)], [(3, box)])
    
    # Synchronize the geometry
    gmsh.model.occ.synchronize()
    
    # Get all surfaces
    surfaces = gmsh.model.getEntities(dim=2)
    
    # Define mesh sizes with more contrast
    fine_size = 0.08    # Even finer for curved surfaces
    coarse_size = 80   # Coarser for flat surfaces
    
    # Function to check if a surface is planar
    def is_planar_surface(surface_tag):
        surface_type = gmsh.model.getType(2, surface_tag)
        return surface_type == "Plane"
    
    # Set mesh sizes with stronger enforcement
    for surface in surfaces:
        surface_tag = surface[1]
        points = gmsh.model.getBoundary([surface], recursive=True)
        
        for point in points:
            if point[0] == 0:  # Only process points (dim=0)
                if is_planar_surface(surface_tag):
                    gmsh.model.mesh.setSize([point], coarse_size)
                else:
                    gmsh.model.mesh.setSize([point], fine_size)
    
    # Force mesh sizes to be respected more strictly
    gmsh.option.setNumber("Mesh.CharacteristicLengthFactor", 1)
    gmsh.option.setNumber("Mesh.CharacteristicLengthMin", fine_size)
    gmsh.option.setNumber("Mesh.CharacteristicLengthMax", coarse_size)
    gmsh.option.setNumber("Mesh.MinimumCirclePoints", 20)  # More points on curved surfaces
    
    # Less aggressive smoothing to maintain size differences
    gmsh.option.setNumber("Mesh.Smoothing", 100)
    
    # Generate 2D mesh
    gmsh.model.mesh.generate(2)
    
    # Save the mesh
    gmsh.write("sphere_and_box_union.msh")
    
    # Launch the GUI
    gmsh.fltk.run()
    
    # Cleanup
    gmsh.finalize()

if __name__ == "__main__":
    create_geometry()

Screenshot from 2024-11-17 01-56-16

Perhaps this is the way to mesh a big flat shape with few elements while having lots of elements on curved surfaces. Needs testing in transport and all that but could just replaced the meshing approach with this and see how it goes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant