def test_Arc(): """ Test of the Arc class. Position : OK Mesh : OK """ name = "test_Arc" gmsh.model.add(name) coords = [ (0.05, 0.0), (1.8, 0.0), (2.0, 0.2), (2.0, 1.95), (1.95, 2.0), (0.2, 2.0), (0.0, 1.8), (0.0, 0.05), ] mesh_size = [0.01] * len(coords) pts = [geo.Point(np.array(c), m_s) for c, m_s in zip(coords, mesh_size)] lines = [ geo.Line(pts[0], pts[1]), geo.Line(pts[2], pts[3]), geo.Line(pts[4], pts[5]), geo.Line(pts[6], pts[7]), ] centers = [(1.8, 0.2), (1.95, 1.95), (0.2, 1.8), (0.05, 0.05)] centers = [geo.Point(np.array(c)) for c in centers] arcs = [ geo.Arc(pts[1], centers[0], pts[2]), geo.Arc(pts[3], centers[1], pts[4]), geo.Arc(pts[5], centers[2], pts[6]), geo.Arc(pts[7], centers[3], pts[0]), ] for ln in lines: ln.add_gmsh() for arc in arcs: arc.add_gmsh() factory.synchronize() data = gmsh.model.getEntities() print("model name : %s \n" % name, data) gmsh.model.mesh.generate(1) # We can generatate 1D meshes for the 2 lines fig, ax = plt.subplots() plt.axis("equal") for pt in pts: pt.plot2D() for ln in lines: ln.plot2D() for arc in arcs: arc.plot2D() plt.pause(0.1) gmsh.model.mesh.generate(1) gmsh.write("%s.brep" % name) gmsh.write("%s.msh" % name) os.system("gmsh %s.brep &" % name) os.system("gmsh %s.msh &" % name)
def test_mesh_only_phy_groups(): """ Test of the set_group_mesh classmethod. A mesh must be generated only entities that belong to physical groups. """ name = "test_mesh_only_phy_groups" gmsh.model.add(name) geo.set_gmsh_option("Mesh.SaveAll", 1) coords = [ (0.0, 0.05), (0.05, 0.0), (1.8, 0.0), (2.0, 0.2), (2.0, 1.95), (1.95, 2.0), (0.2, 2.0), (0.0, 1.8), ] pts = [geo.Point(np.array(c), 0.03) for c in coords] lines = [ geo.Line(pts[2 * i - 1], pts[2 * i]) for i in range(len(pts) // 2) ] centers = [ geo.Point(np.array(c), 0.03) for c in [(0.05, 0.05), (1.8, 0.2), (1.95, 1.95), (0.2, 1.8)] ] arcs = [ geo.Arc(pts[2 * i], centers[i], pts[2 * i + 1]) for i in range(len(pts) // 2) ] elmts_1D = [item for pair in zip(lines, arcs) for item in pair] ll_1 = geo.LineLoop(elmts_1D, explicit=True) coords = [(1.0, 1.0), (3.0, 1.0), (3.0, 3.0), (1.0, 3.0)] vertc = [geo.Point(np.array(c), 0.01) for c in coords] ll_2 = geo.LineLoop(vertc, explicit=False) surf_1 = geo.PlaneSurface(ll_1) surf_2 = geo.PlaneSurface(ll_2) rect_vtcs = [ geo.Point(np.array(c), 0.05) for c in [(4, 2), (4, 4), (6, 4), (6, 2)] ] hole_vtcs = [ geo.Point(np.array(c), 0.02) for c in [(5 - 0.1, 3), (5, 3 - 0.5), (5 + 0.1, 3), (5, 3 + 0.5)] ] rect_ll = geo.LineLoop(rect_vtcs, explicit=False) hole_ll = geo.LineLoop(hole_vtcs, explicit=False) surf_with_hole = geo.PlaneSurface(rect_ll, [hole_ll]) all_surf = [surf_1, surf_2, surf_with_hole] for s in all_surf: s.add_gmsh() factory.synchronize() surf_group = geo.PhysicalGroup([surf_1, surf_with_hole], 2, "surf_group") line_group = geo.PhysicalGroup(ll_2.sides + ll_1.sides, 1, "line_group") surf_group.add_gmsh() line_group.add_gmsh() surf_group.set_color([22, 160, 133, 255]) line_group.set_color([234, 97, 83, 255]) geo.PhysicalGroup.set_group_mesh(True) factory.synchronize() # * Important before meshing data = model.getPhysicalGroups() logger.info( f"All physical groups in '{name}' model : \n" f"{data} \n" f"Names : {[model.getPhysicalName(*dimtag) for dimtag in data]}") gmsh.model.mesh.generate(2) gmsh.write("%s.brep" % name) gmsh.write("%s.msh" % name) os.system("gmsh %s.brep &" % name) os.system("gmsh %s.msh &" % name)
def test_physical_group(): """ Test of the PhysicalGroup class. Group membership in the gmsh model : OK Colors in the gmsh GUI opened with gmsh.fltk.run() : OK Information about Physical group membership are exported in the msh file : OK """ name = "test_PhysicalGroup" gmsh.model.add(name) geo.set_gmsh_option("Mesh.SaveAll", 0) # gmsh.option.setNumber('Mesh.MeshOnlyVisible',1) coords = [ (0.0, 0.05), (0.05, 0.0), (1.8, 0.0), (2.0, 0.2), (2.0, 1.95), (1.95, 2.0), (0.2, 2.0), (0.0, 1.8), ] pts = [geo.Point(np.array(c), 0.03) for c in coords] lines = [ geo.Line(pts[2 * i - 1], pts[2 * i]) for i in range(len(pts) // 2) ] centers = [ geo.Point(np.array(c), 0.03) for c in [(0.05, 0.05), (1.8, 0.2), (1.95, 1.95), (0.2, 1.8)] ] arcs = [ geo.Arc(pts[2 * i], centers[i], pts[2 * i + 1]) for i in range(len(pts) // 2) ] elmts_1D = [item for pair in zip(lines, arcs) for item in pair] ll_1 = geo.LineLoop(elmts_1D, explicit=True) coords = [(1.0, 1.0), (3.0, 1.0), (3.0, 3.0), (1.0, 3.0)] vertc = [geo.Point(np.array(c), 0.01) for c in coords] ll_2 = geo.LineLoop(vertc, explicit=False) surf_1 = geo.PlaneSurface(ll_1) surf_2 = geo.PlaneSurface(ll_2) rect_vtcs = [ geo.Point(np.array(c), 0.05) for c in [(4, 2), (4, 4), (6, 4), (6, 2)] ] hole_vtcs = [ geo.Point(np.array(c), 0.02) for c in [(5 - 0.1, 3), (5, 3 - 0.5), (5 + 0.1, 3), (5, 3 + 0.5)] ] rect_ll = geo.LineLoop(rect_vtcs, explicit=False) hole_ll = geo.LineLoop(hole_vtcs, explicit=False) surf_with_hole = geo.PlaneSurface(rect_ll, [hole_ll]) all_surf = [surf_1, surf_2, surf_with_hole] logger.info( f"In model {name}, PlaneSurface instances added to the gmsh model.") for s in all_surf: s.add_gmsh() logger.info(f"Python objects, surfaces tag : {[s.tag for s in all_surf]}") logger.info( f"Entities in model {name} before synchronize() call : {model.getEntities()}" ) factory.synchronize() logger.info( f"After synchronize() call : \n Points : {model.getEntities(0)} \n Curves : {model.getEntities(1)} \n Surfaces : {model.getEntities(2)}" ) surf_group = geo.PhysicalGroup([surf_1, surf_with_hole], 2, "surf_group") surf_group.add_gmsh() factory.synchronize() logger.info( "Operations : synchronize() -> def surf_group -> adding surf_group to the model -> synchronize()" ) logger.info( f"Physical groups in model after : {model.getPhysicalGroups()}") # * OK, Here is no need of a second sync or a second physical group added in the model. factory.synchronize() logger.info( f"Physical groups in model after a second synchronize() : {model.getPhysicalGroups()}" ) # surf_group.add_to_group(surf_2) #* Raise an AttributeError, OK line_group = geo.PhysicalGroup(ll_2.sides, 1, "line_group") line_group.add_to_group(ll_1.sides) line_group.add_gmsh() factory.synchronize() logger.info( "Ops : def line_group -> adding line_group to the model -> synchronize()" ) logger.info( f"Physical groups in model after that : {model.getPhysicalGroups()}") pts_group = geo.PhysicalGroup( [pt for crv in ll_2.sides for pt in crv.def_pts], 0, "pts_group") pts_group.add_gmsh() factory.synchronize() logger.info( "Ops : def pts_group -> adding pts_group to the model -> synchronize()" ) logger.info( f"Physical groups in model after that : {model.getPhysicalGroups()}") surf_group.set_color([22, 160, 133, 255]) line_group.set_color([234, 97, 83, 255]) factory.synchronize() # * Important before meshing data = model.getPhysicalGroups() logger.info( f"All physical groups in '{name}' model : {data} Names : {[model.getPhysicalName(*dimtag) for dimtag in data]}" ) prev_val = gmsh.option.getNumber("General.Verbosity") geo.set_gmsh_option("General.Verbosity", 3) gmsh.model.mesh.generate(2) geo.set_gmsh_option("General.Verbosity", prev_val) gmsh.write("%s.brep" % name) gmsh.write("%s.msh" % name) os.system("gmsh %s.brep &" % name) os.system("gmsh %s.msh &" % name)
def test_bool_ops(): """ Test of boolean operations performed on PlaneSurfaces instances. cut : OK intersection : geometry : OK intersection : mesh size : No. Very coarse mesh if the characteristic length is set from the values at Points #? What the second output of the booleans operations functions is? """ name = "test_PlaneSurface_bool_ops" gmsh.model.add(name) coords = [ (0.0, 0.05), (0.05, 0.0), (1.8, 0.0), (2.0, 0.2), (2.0, 1.95), (1.95, 2.0), (0.2, 2.0), (0.0, 1.8), ] pts = [geo.Point(np.array(c), 0.03) for c in coords] lines = [ geo.Line(pts[2 * i - 1], pts[2 * i]) for i in range(len(pts) // 2) ] centers = [ geo.Point(np.array(c), 0.03) for c in [(0.05, 0.05), (1.8, 0.2), (1.95, 1.95), (0.2, 1.8)] ] arcs = [ geo.Arc(pts[2 * i], centers[i], pts[2 * i + 1]) for i in range(len(pts) // 2) ] elmts_1D = [item for pair in zip(lines, arcs) for item in pair] ll_1 = geo.LineLoop(elmts_1D, explicit=True) coords = [(1.0, 1.0), (3.0, 1.0), (3.0, 3.0), (1.0, 3.0)] vertc = [geo.Point(np.array(c), 0.1) for c in coords] ll_2 = geo.LineLoop(vertc, explicit=False) rect_vtcs = [ geo.Point(np.array(c), 0.05) for c in [(4, 2), (4, 4), (6, 4), (6, 2)] ] hole_vtcs_1 = [ geo.Point(np.array(c), 0.02) for c in [(5 - 0.1, 3), (5, 3 - 0.5), (5 + 0.1, 3), (5, 3 + 0.5)] ] hole_vtcs_2 = [ geo.Point(np.array(c), 0.02) for c in [(5, 3 - 0.1), (5 - 0.5, 3), (5, 3 + 0.1), (5 + 0.5, 3)] ] rect_ll = geo.LineLoop(rect_vtcs, explicit=False) hole_ll_1 = geo.LineLoop(hole_vtcs_1, explicit=False) hole_ll_2 = geo.LineLoop(hole_vtcs_2, explicit=False) surf_1 = geo.PlaneSurface(ll_1) surf_2 = geo.PlaneSurface(ll_2) surf_rect = geo.PlaneSurface(rect_ll) surf_hole_1 = geo.PlaneSurface(hole_ll_1) surf_hole_2 = geo.PlaneSurface(hole_ll_2) for s in [surf_1, surf_2, surf_rect, surf_hole_1, surf_hole_2]: s.add_gmsh() print(f"""Tags before boolean operations : surf_1 : {surf_1.tag}; surf_2 : {surf_2.tag}; surf_rect : {surf_rect.tag}; surf_hole_1 : {surf_hole_1.tag}; surf_hole_2 : {surf_hole_2.tag}""" ) surf_with_hole = geo.surface_bool_cut(surf_rect, [surf_hole_1, surf_hole_2]) print(surf_with_hole) surf_with_hole = surf_with_hole[0] surf_inter = geo.surface_bool_intersect(surf_1, surf_2) print(surf_inter) surf_inter = surf_inter[0] factory.synchronize() # * Intersection, when a surface is inside another one. coords3 = [ geo.Point(np.array(c), 0.05) for c in [(10, 10), (10, 14), (14, 14), (14, 10)] ] coords4 = [ geo.Point(np.array(c), 0.05) for c in [(11, 11), (11, 13), (13, 13), (13, 11)] ] surf3 = geo.PlaneSurface(geo.LineLoop(coords3, explicit=False)) surf3.add_gmsh() surf4 = geo.PlaneSurface(geo.LineLoop(coords4, explicit=False)) surf4.add_gmsh() factory.synchronize() surf_inter34 = geo.surface_bool_intersect(surf3, [surf4]) factory.synchronize() data = gmsh.model.getEntities() print(f"model name : {name}") print(data) print(f"""Tags after boolean operations : surf_1 : {surf_1.tag}; surf_2 : {surf_2.tag}; surf_rect : {surf_rect.tag}; surf_hole_1 : {surf_hole_1.tag}; surf_hole_2 : {surf_hole_2.tag}; surf_with_hole : {surf_with_hole.tag}; surf_inter: {surf_inter.tag}""" ) gmsh.model.mesh.generate(2) gmsh.write(f"{name}.brep") gmsh.write(f"{name}.msh") os.system(f"gmsh {name}.brep &") os.system(f"gmsh {name}.msh &")
def test_PlaneSurface(): """ Test of the PlaneSurface class. Geometry of surf_1, surf_2 and surf_with_hole : #*OK Mesh of the 3 surfaces : #*OK """ name = "test_PlaneSurface" gmsh.model.add(name) coords = [ (0.0, 0.05), (0.05, 0.0), (1.8, 0.0), (2.0, 0.2), (2.0, 1.95), (1.95, 2.0), (0.2, 2.0), (0.0, 1.8), ] pts = [geo.Point(np.array(c), 0.03) for c in coords] lines = [ geo.Line(pts[2 * i - 1], pts[2 * i]) for i in range(len(pts) // 2) ] centers = [ geo.Point(np.array(c), 0.03) for c in [(0.05, 0.05), (1.8, 0.2), (1.95, 1.95), (0.2, 1.8)] ] arcs = [ geo.Arc(pts[2 * i], centers[i], pts[2 * i + 1]) for i in range(len(pts) // 2) ] elmts_1D = [item for pair in zip(lines, arcs) for item in pair] ll_1 = geo.LineLoop(elmts_1D, explicit=True) coords = [(1.0, 1.0), (3.0, 1.0), (3.0, 3.0), (1.0, 3.0)] vertc = [geo.Point(np.array(c), 0.01) for c in coords] ll_2 = geo.LineLoop(vertc, explicit=False) surf_1 = geo.PlaneSurface(ll_1) surf_2 = geo.PlaneSurface(ll_2) rect_vtcs = [ geo.Point(np.array(c), 0.05) for c in [(4, 2), (4, 4), (6, 4), (6, 2)] ] hole_vtcs = [ geo.Point(np.array(c), 0.02) for c in [(5 - 0.1, 3), (5, 3 - 0.5), (5 + 0.1, 3), (5, 3 + 0.5)] ] rect_ll = geo.LineLoop(rect_vtcs, explicit=False) hole_ll = geo.LineLoop(hole_vtcs, explicit=False) surf_with_hole = geo.PlaneSurface(rect_ll, [hole_ll]) all_surf = [surf_1, surf_2, surf_with_hole] print( "Model : %s \n Add PlaneSurface instances to a gmsh model. \n Surfaces tag :" % name) for s in all_surf: s.add_gmsh() print(s.tag) factory.synchronize() data = gmsh.model.getEntities() print("model name : %s" % name) print(data) print("Option Mesh.SaveAll is set to 1. Initial value : %i" % gmsh.option.getNumber("Mesh.SaveAll")) gmsh.option.setNumber("Mesh.SaveAll", 1) gmsh.model.mesh.generate(2) gmsh.write("%s.brep" % name) gmsh.write("%s.msh" % name) os.system("gmsh %s.brep &" % name) os.system("gmsh %s.msh &" % name)
def test_LineLoop(): """ Test of the LineLoop class. Instantiate (explicit and implicit) : #*OK Add to the gmsh geometry model : #*OK Plot : #*OK #TODO : Tester methodes round_corner #TODO : Test symétries """ name = "test_LineLoop" gmsh.model.add(name) # * Explicit constructor coords = [ (0.05, 0.0), (1.8, 0.0), (2.0, 0.2), (2.0, 1.95), (1.95, 2.0), (0.2, 2.0), (0.0, 1.8), (0.0, 0.05), ] mesh_size = [0.01] * len(coords) pts = [geo.Point(np.array(c), m_s) for c, m_s in zip(coords, mesh_size)] lines = [ geo.Line(pts[0], pts[1]), geo.Line(pts[2], pts[3]), geo.Line(pts[4], pts[5]), geo.Line(pts[6], pts[7]), ] centers = [(1.8, 0.2), (1.95, 1.95), (0.2, 1.8), (0.05, 0.05)] centers = [geo.Point(np.array(c)) for c in centers] arcs = [ geo.Arc(pts[1], centers[0], pts[2]), geo.Arc(pts[3], centers[1], pts[4]), geo.Arc(pts[5], centers[2], pts[6]), geo.Arc(pts[7], centers[3], pts[0]), ] elmts_1D = [item for pair in zip(lines, arcs) for item in pair] ll_1 = geo.LineLoop(elmts_1D, explicit=True) # * Implicit constructor coords = [(1.0, 1.0), (3.0, 1.0), (3.0, 3.0), (1.0, 3.0)] mesh_size = [0.01] * len(coords) vertc = [geo.Point(np.array(c), m_s) for c, m_s in zip(coords, mesh_size)] ll_2 = geo.LineLoop(vertc, explicit=False) ll_1.add_gmsh() ll_2.add_gmsh() factory.synchronize() data = gmsh.model.getEntities() print("model name : %s \n" % name, data) gmsh.model.mesh.generate(1) # We can generatate 1D meshes for the 2 lines fig, ax = plt.subplots() plt.axis("equal") ll_1.plot2D() ll_2.plot2D() plt.pause(0.1) gmsh.model.mesh.generate(1) gmsh.write("%s.brep" % name) gmsh.write("%s.msh" % name) os.system("gmsh %s.brep &" % name) os.system("gmsh %s.msh &" % name)