def test_Line(): """ Test of the Line class. Position : OK Mesh : OK """ name = "test_Line" gmsh.model.add(name) coords = [(0.0, 0.0), (0.0, 2.5), (SR2, SR2)] mesh_size = [0.1, 0.1, 0.01] coords = [np.array(c) for c in coords] pts = [geo.Point(c, m_s) for c, m_s in zip(coords, mesh_size)] lines = [geo.Line(pts[0], pts[1]), geo.Line(pts[0], pts[2])] for ln in lines: ln.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 plt.figure() plt.axis("equal") for ln in lines: ln.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_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_remove_ll_duplicates(): """ Test of the remove_duplicates static method of the class LineLoop. Results : OK """ pts_1 = [(2, 1), (-2, 1), (-2, -1), (2, -1)] pts_1 = [geo.Point(np.array(c)) for c in pts_1] pts_2 = [(-2, -1), (2, -1), (2, 1), (-2, 1)] pts_2 = [geo.Point(np.array(c)) for c in pts_2] lines_1 = [ [(3, 4), (-3, 4)], [(-3, 4), (-3, -4)], [(-3, -4), (3, -4)], [(3, -4), (3, 4)], ] lines_2 = [ [(-3, 4), (-3, -4)], [(-3, -4), (3, -4)], [(3, -4), (3, 4)], [(3, 4), (-3, 4)], ] lines_1 = [ geo.Line(*[geo.Point(np.array(c)) for c in cc]) for cc in lines_1 ] lines_2 = [ geo.Line(*[geo.Point(np.array(c)) for c in cc]) for cc in lines_2 ] ll_pts_1 = geo.LineLoop(pts_1, explicit=False) ll_pts_2 = geo.LineLoop(pts_2, explicit=False) ll_lines_1 = geo.LineLoop(lines_1, explicit=True) ll_lines_2 = geo.LineLoop(lines_2, explicit=True) all_ll = [ll_lines_2, ll_pts_1, ll_pts_2, ll_lines_1] print(all_ll) plt.figure() plt.axis("equal") colors = ["red", "green", "orange", "blue"] for ll, c in zip(all_ll, colors): ll_copy = copy.deepcopy( ll ) # The plot method will creat sides and the 3 next methods have been designed to work well only if sides is empty. ll_copy.plot2D(c) plt.pause(0.2) unique_ll = geo.remove_duplicates(all_ll) print(unique_ll) plt.figure() plt.axis("equal") for ll, c in zip(unique_ll, colors): ll.plot2D(c) plt.pause(0.2)
def test_Threshold(): """ Test of the AttractorField and ThresholdField subclass of the Field base class. #* Test OK """ name = "test_Threshold" model.add(name) rect_vtcs = [geo.Point(np.array(c)) for c in [(-4, -2), (4, -2), (4, 2), (-4, 2)]] mid_N = geo.Point(np.array((0, 2))) mid_S = geo.Point(np.array((0, -2))) attrac_NW = geo.Point(np.array((-2, 1))) attrac_mid = geo.Line(mid_N, mid_S) rect_ll = geo.LineLoop(rect_vtcs, False) rect_s = geo.PlaneSurface(rect_ll) rect_s.add_gmsh() f = msh.AttractorField( points=[attrac_NW], curves=[attrac_mid], nb_pts_discretization=10 ) g = msh.ThresholdField(f, 0.2, 1, 0.03, 0.2, True) msh.set_background_mesh(g) factory.synchronize() model.mesh.generate(2) gmsh.write("%s.msh" % name) os.system("gmsh %s.msh &" % name)
def test_periodic(): name = "periodic" model.add(name) vtcs = [ geo.Point(np.array(c), 0.5) for c in [(-2, +1), (0, +1), (+2, +1), (+2, -1), (0, -1), (-2, -1)] ] vtcs[0].mesh_size = 0.01 vtcs[2].mesh_size = 0.05 sides = [geo.Line(vtcs[i - 1], vtcs[i]) for i in range(len(vtcs))] surf = geo.PlaneSurface(geo.LineLoop(sides, explicit=True)) surf.add_gmsh() data = gmsh.model.getEntities() print("model name : %s" % name) print(data) factory.synchronize() msh.set_periodicity_pairs([sides[3]], [sides[0]], np.array((4, 0))) msh.set_periodicity_pairs( [sides[-1], sides[-2]], [sides[1], sides[2]], np.array((0, -2)) ) 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_refine_function(): """ Test of the function that has been designed to quickly specify a refinement constraint. #* Test OK """ name = "test_refine_function" model.add(name) rect_vtcs = [geo.Point(np.array(c)) for c in [(-4, -2), (4, -2), (4, 2), (-4, 2)]] mid_N = geo.Point(np.array((0, 2))) mid_S = geo.Point(np.array((0, -2))) attrac_NW = geo.Point(np.array((-2, 1))) attrac_mid = geo.Line(mid_N, mid_S) rect_ll = geo.LineLoop(rect_vtcs, False) rect_s = geo.PlaneSurface(rect_ll) rect_s.add_gmsh() f = msh.set_mesh_refinement( [0.2, 1], [0.03, 0.2], attractors={"points": [attrac_NW], "curves": [attrac_mid]}, sigmoid_interpol=True, ) msh.set_background_mesh(f) factory.synchronize() model.mesh.generate(2) gmsh.write("%s.msh" % name) os.system("gmsh %s.msh &" % name)
def test_order_curves(): """ Test of the order_curves function. Shoud be run after the test of the gather_boundary_fragments function. Test of the ordering : OK Reverse functionnality : #! not tested yet """ name = "test_order" gmsh.model.add(name) rect_vtcs = [(-4, 2), (-4, -2), (4, -2), (4, 2)] rect_vtcs = [geo.Point(np.array(c), 0.2) for c in rect_vtcs] line_N = geo.Line(geo.Point(np.array((-4, 2))), geo.Point(np.array((4, 2)))) line_W = geo.Line(geo.Point(np.array((-4, -2))), geo.Point(np.array((-4, 2)))) line_S = geo.Line(geo.Point(np.array((-4, -2))), geo.Point(np.array((4, -2)))) line_E = geo.Line(geo.Point(np.array((4, -2))), geo.Point(np.array((4, 2)))) lines = {"N": line_N, "E": line_E, "S": line_S, "W": line_W} holes = list() hole_shape = [np.array(c) for c in [(-0.2, 0), (0, -0.4), (0.2, 0), (0, 0.4)]] translations = [ (-3, 2.1), (-1, 2.1), (1, 2.1), (3, 2.1), (-3, -2.1), (-1, -2.1), (1, -2.1), (3, -2.1), (4.1, -1), (4.1, 1), (-4.1, -1), (-4.1, 1), ] translations = [np.array(t) for t in translations] for t in translations: holes.append([geo.Point(c + t, 0.05) for c in hole_shape]) rect_ll = geo.LineLoop(rect_vtcs, explicit=False) hole_ll = [geo.LineLoop(h, explicit=False) for h in holes] rect_s = geo.PlaneSurface(rect_ll) hole_s = [geo.PlaneSurface(ll) for ll in hole_ll] final_s = geo.bool_cut_S(rect_s, hole_s, remove_body=False, remove_tool=False) factory.synchronize() final_s = final_s[0] final_s.get_boundary(recursive=True) fig, ax = plt.subplots() ax.set_xlim(-4.1, 4.1) ax.set_ylim(-2.1, 2.1) for crv in final_s.boundary: crv.plot("blue") boundaries = {"N": [], "E": [], "S": [], "W": []} for key, line in lines.items(): boundaries[key] = geo.gather_boundary_fragments(final_s.boundary, line) random.shuffle(boundaries[key]) fig, ax = plt.subplots() ax.set_xlim(-4.1, 4.1) ax.set_ylim(-2.1, 2.1) colors = {"N": "red", "E": "green", "S": "orange", "W": "blue"} for key in boundaries.keys(): for l in boundaries[key]: l.plot(colors[key]) plt.pause(0.5) # In order to see the order of the curves basis = np.array(((1.0, 0.0), (0.0, 1.0), (0.0, 0.0))) dir_v = {"N": basis[:, 0], "E": basis[:, 1], "S": basis[:, 0], "W": basis[:, 1]} for key, lns in boundaries.items(): msh.order_curves(lns, dir_v[key]) fig, ax = plt.subplots() ax.set_xlim(-4.1, 4.1) ax.set_ylim(-2.1, 2.1) for key in boundaries.keys(): for l in boundaries[key]: l.plot(colors[key]) plt.pause(0.5) # In order to see the order of the curves factory.synchronize() plt.show(block=True)
import random import matplotlib.pyplot as plt # nice shortcuts model = gmsh.model factory = model.occ plt.ion() # ? COMPARER AVEC DES MAILLAGES PRODUITS A LA MAIN JUSTE AVEC LA GUI GMSH ? rect_vtcs = [geo.Point(np.array(c)) for c in [(-2, -1), (2, -1), (2, 1), (-2, 1)]] mid_N = geo.Point(np.array((0, 1))) mid_S = geo.Point(np.array((0, -1))) attrac_NW = geo.Point(np.array((-1, 0.5))) attrac_mid = geo.Line(mid_N, mid_S) rect_ll = geo.LineLoop(rect_vtcs, False) rect_s = geo.PlaneSurface(rect_ll) def test_MathEval(): """ Test of MathEval subclass of the Field base class. #* Test OK """ name = "test_MathEval" model.add(name) rect_vtcs = [geo.Point(np.array(c)) for c in [(-4, -2), (4, -2), (4, 2), (-4, 2)]]
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_gather_line(): """ Test of the macro_line_fragments function. colors on plots : OK """ name = "test_gather_line" gmsh.model.add(name) rect_vtcs = [(-4, 2), (-4, -2), (4, -2), (4, 2)] rect_vtcs = [geo.Point(np.array(c), 0.2) for c in rect_vtcs] line_N = geo.Line(geo.Point(np.array((-4, 2))), geo.Point(np.array( (4, 2)))) line_W = geo.Line(geo.Point(np.array((-4, -2))), geo.Point(np.array((-4, 2)))) line_S = geo.Line(geo.Point(np.array((-4, -2))), geo.Point(np.array((4, -2)))) line_E = geo.Line(geo.Point(np.array((4, -2))), geo.Point(np.array( (4, 2)))) holes = list() hole_shape = [ np.array(c) for c in [(-0.2, 0), (0, -0.4), (0.2, 0), (0, 0.4)] ] translations = [ (-3, 2.1), (-1, 2.1), (1, 2.1), (3, 2.1), (-3, -2.1), (-1, -2.1), (1, -2.1), (3, -2.1), (4.1, -1), (4.1, 1), (-4.1, -1), (-4.1, 1), ] translations = [np.array(t) for t in translations] for t in translations: holes.append([geo.Point(c + t, 0.05) for c in hole_shape]) rect_ll = geo.LineLoop(rect_vtcs, explicit=False) hole_ll = [geo.LineLoop(h, explicit=False) for h in holes] rect_s = geo.PlaneSurface(rect_ll) hole_s = [geo.PlaneSurface(ll) for ll in hole_ll] final_s = geo.surface_bool_cut(rect_s, hole_s) factory.synchronize() print("length of final_s : ", len(final_s)) final_s = final_s[0] final_s.get_boundary(recursive=True) plt.figure() plt.axis("equal") for crv in final_s.boundary: crv.plot2D("blue") boundary_N = geo.macro_line_fragments(final_s.boundary, line_N) boundary_W = geo.macro_line_fragments(final_s.boundary, line_W) boundary_S = geo.macro_line_fragments(final_s.boundary, line_S) boundary_E = geo.macro_line_fragments(final_s.boundary, line_E) factory.synchronize() plt.figure() plt.axis("equal") line_lists = [boundary_N, boundary_W, boundary_S, boundary_E] colors = ["red", "green", "orange", "blue"] names = ["N", "W", "S", "E"] gps = list() for l_list, col, n in zip(line_lists, colors, names): gp_l = geo.PhysicalGroup(l_list, 1, n) gp_l.add_gmsh() gps.append(gp_l) for ln in l_list: ln.plot2D(col) factory.synchronize() gmsh.option.setNumber("Mesh.SaveAll", 1) 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_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)
def auxetic_square_RVE(L, a, t, nb_cells=(1, 1), offset=(0.0, 0.0), soft_mat=False, name=""): """ Create an instance of Gmsh2DRVE that represents a RVE of the auxetic square microstructure. The generic operations will be performed with the Gmsh2DRVE methods. Parameters ---------- L : float Length of the sides of the square cell. a : float Length of the slits beetween squares. t : float Width of the slits beetween squares. nb_cells : tuple or 1D array nb of cells in each direction of repetition offset : tuple or 1D array Relative position inside a cell of the point that will coincide with the origin of the global domain. Returns ------- Instance of the Gmsh2DRVE class. """ name = name if name else "aux_square" model.add(name) # geo.reset() offset = np.asarray(offset) nb_cells = np.asarray(nb_cells) logger.info("Start defining the auxetic_square geometry") gen_vect = np.array(((L, 0.0), (0.0, L))) b = (L - a) / 2.0 e1 = np.array((L, 0.0, 0.0)) e2 = np.array((0.0, L, 0.0)) C = geo.Point(1 / 2.0 * (e1 + e2)) M = geo.Point(1 / 4.0 * (e1 + e2)) e3 = np.array((0.0, 0.0, 1.0)) center_pts = [[(b, 0.0), (a + b, 0.0)], [(0.0, -a / 2.0), (0.0, a / 2.0)]] center_pts = [[geo.Point(np.array(coord)) for coord in gp] for gp in center_pts] center_lines = [geo.Line(*pts) for pts in center_pts] center_lines += [geo.point_reflection(ln, M) for ln in center_lines] center_lines += [geo.plane_reflection(ln, C, e1) for ln in center_lines] center_lines += [geo.plane_reflection(ln, C, e2) for ln in center_lines] center_lines = geo.remove_duplicates(center_lines) for ln in center_lines: ln.ortho_dir = np.cross(e3, ln.direction()) pattern_ll = list() for ln in center_lines: vertices = [ geo.translation(ln.def_pts[0], t / 2 * ln.ortho_dir), geo.translation(ln.def_pts[1], t / 2 * ln.ortho_dir), geo.translation(ln.def_pts[1], -t / 2 * ln.ortho_dir), geo.translation(ln.def_pts[0], -t / 2 * ln.ortho_dir), ] pattern_ll.append(geo.LineLoop(vertices)) tmp_nb_bef = len(pattern_ll) pattern_ll = geo.remove_duplicates(pattern_ll) logger.debug( f"Number of line-loops removed from pattern-ll : {tmp_nb_bef - len(pattern_ll)}." ) logger.debug( f"Final number of pattern line-loops for auxetic square : {len(pattern_ll)}" ) for ll in pattern_ll: ll.round_corner_explicit(t / 2) filter_sides = list() # * Pour ne pas essayer d'ajouter au model gmsh des lignes de longueur nulle. # * (Error : could not create line) for crv in ll.sides: if not crv.def_pts[0] == crv.def_pts[-1]: filter_sides.append(crv) ll.sides = filter_sides fine_pts = geo.remove_duplicates( flatten([ln.def_pts for ln in center_lines])) return Gmsh2DRVE(pattern_ll, gen_vect, nb_cells, offset, fine_pts, soft_mat, name)