from hybmeshpack import hmscript as hm hm.check_compatibility('0.5.0') # create a substrate 20x20 square grid sqr = hm.add_unf_rect_grid([0, 0], [1, 1], 20, 20) # create tube grids # 1) create a prototype tube_proto = hm.add_unf_rect_grid([0, 0], [0.15, 0.5], 10, 20) # 2) copy, replace and rotate to build left, right, bottom tubes. [tube_left, tube_right, tube_bot] = hm.copy_geom([tube_proto] * 3) hm.move_geom([tube_left, tube_right], 0.425, 1.0) hm.move_geom([tube_bot], 0.425, -0.5) hm.rotate_geom([tube_left], -45, [0.5, 0.5]) hm.rotate_geom([tube_right], 45, [0.5, 0.5]) # exclude pentagon from base grid cont5 = hm.create_contour([[0.3, 0.6], [0.35, 0.4], [0.65, 0.4], [0.7, 0.6], [0.5, 0.7], [0.3, 0.6]]) exsqr = hm.exclude_contours(sqr, [cont5], "inner") # ******* see fig2 # make series of grid superpositions with defined buffer sizes imposed = hm.unite_grids(exsqr, [(tube_left, 0.05), (tube_right, 0.05), (tube_bot, 0.1)]) # ******* see fig3 # create a boundary grid prototype around the pentagon. # 1) make partition of pentagon contour with constant step cont5 = hm.partition_contour(cont5, "const", 0.02)
from hybmeshpack import hmscript as hm from hybmeshpack.hmscript._dbg import check, check_ascii_file, checkdict import math global hm, check, checkdict, math hm.check_compatibility("0.4.3") g1 = hm.add_unf_rect_grid([0, 0], [5, 1], 10, 10) hm.set_boundary_type(g1, 1) cont = [] for i in range(100): x = float(i) / 99.0 y = 0.1 * math.sin(6 * math.pi * x) cont.append([x, y]) for p in cont[::-1]: cont.append([p[0], p[1] + 1.0]) cont.append(cont[0]) c1 = hm.create_contour(cont) hm.set_boundary_type(c1, 2) print "rectangle to square with sine edges: no, from_contour" a1 = hm.map_grid( g1, c1, [[0, 0], [5, 0], [5, 1], [0, 1]], [[0, 0], [1, 0], [1, 1], [0, 1]], snap="no", btypes="from_contour") checkdict( hm.info_grid(a1), {'cell_types': {4: 100}, 'Nnodes': 121, 'Nedges': 220, 'Ncells': 100}) checkdict( hm.info_contour(a1), {'btypes': {2: 40}, 'Nnodes': 40, 'subcont': [40], 'Nedges': 40})
from hybmeshpack import hmscript as hm from hybmeshpack.hmscript import _dbg as hmdbg hm.check_compatibility("0.4.0") g1 = hm.add_unf_rect_grid([0, 0], [1, 1], 10, 10) hm.set_boundary_type(g1, bfun=lambda x0, y0, x1, y1, bt: { y0 + y1 == 0: 1, x0 + x1 == 2: 2, y0 + y1 == 2: 3, x0 + x1 == 0: 4 }[1]) print "extrusion with zero bc" s1 = hm.extrude_grid(g1, [0, 0.1, 0.2], 0, 0, 0) hm.export3d_grid_vtk(s1, "s1.vtk", "s2.vtk") hmdbg.check_ascii_file(9760293401399903439, "s1.vtk") hmdbg.check_ascii_file(9133727113521198898, "s2.vtk") hm.remove_geom(s1) print "extrusion with side bc from contour" s1 = hm.extrude_grid(g1, [0, 0.1], 0, 0, None) hm.export3d_grid_vtk(s1, None, "s2.vtk") hmdbg.check_ascii_file(17055111601829986000, "s2.vtk") hm.remove_geom(s1) print "extrusion with non-zero at zfaces and side bc from contour" s1 = hm.extrude_grid(g1, [-0.1, 0.1, 22], 8, 10, None) hm.export3d_grid_vtk(s1, None, "s2.vtk") hmdbg.check_ascii_file(4721469755399912982, "s2.vtk") print "extrusion with function at bottom z face"
from hybmeshpack import hmscript as hm from hybmeshpack.hmscript._dbg import check_ascii_file, checkdict, check hm.check_compatibility("0.4.5") print "add_unf_rect_grid" g1 = hm.add_unf_rect_grid([0.01, -0.2], [1, 1], 15, 10) checkdict(hm.info_grid(g1), {'Ncells': 150}) hm.export_grid_vtk(g1, "g1.vtk") check_ascii_file(13180744078971206337, "g1.vtk") g1 = hm.add_unf_rect_grid([0, 0], [1, 1], 15, 10, custom_x=0.5) checkdict(hm.info_grid(g1), {'Ncells': 20}) g1 = hm.add_unf_rect_grid([1, 1], [2, 2], 15, 10, custom_y=0.2) checkdict(hm.info_grid(g1), {'Ncells': 75}) g1 = hm.add_unf_rect_grid([1, 1], [2, 2], 3, 3, custom_x=0.1, custom_y=0.2) checkdict(hm.info_grid(g1), {'Ncells': 50}) g1 = hm.add_unf_rect_grid([1, 1], [2, 2], custom_x=[0, 1], custom_y=[0, 1]) checkdict(hm.info_grid(g1), {'Ncells': 1}) g1 = hm.add_unf_rect_grid([1, 1], [2, 2], custom_x=[1, 1.9, 2], custom_y=[-1, -0.2, 2.2]) hm.export_grid_vtk(g1, "g1.vtk") check_ascii_file(3572940205872809425, "g1.vtk") print "add_unf_circ_grid" g1 = hm.add_unf_circ_grid([1, 1], 1, 13, 3, is_trian=True) hm.export_grid_vtk(g1, "g1.vtk") check_ascii_file(16381562878152009782, "g1.vtk") g1 = hm.add_unf_circ_grid([0, 0], 1, 5, 5, 0.3, is_trian=False) hm.export_grid_vtk(g1, "g1.vtk") check_ascii_file(419969290668673427, "g1.vtk")
# import os.path from hybmeshpack import hmscript as hm from hybmeshpack.hmscript import _dbg as hmdbg hm.check_compatibility("0.4.6") print "export to natives" # export/import grids 2d to native g1 = hm.add_unf_rect_grid([0, 0], [1, 1], 3, 4) g2 = hm.add_unf_rect_grid([0, 0], [1, 1], 2, 3) g3 = hm.add_unf_rect_grid([0, 0], [1, 1], 5, 6) hm.export_grid_hmg(g1, "g1.hmg") hm.export_grid_hmg(g1, "g2.hmg", "bin") hm.export_grid_hmg(g1, "g3.hmg", "fbin") hm.export_grid_hmg([g1, g2, g3], "g4.hmg") g4 = hm.import_grid_hmg("g2.hmg") hmdbg.check(hm.info_grid(g4)['Ncells'] == 12) g6 = hm.import_grid_hmg("g4.hmg", allgrids=True) hmdbg.check(len(g6) == 3 and hm.info_grid(g6[1])['Ncells'] == 6) for i, z in enumerate(hm.registered_grids()): hmdbg.check(int(z[-1]) == i + 1) hm.remove_all() g7 = hm.import_grid_hmg("g4.hmg", "Grid2D_3") hmdbg.check(g7 == "Grid2D_3" and len(hm.registered_grids()) == 1) hmdbg.check(hm.info_grid(g7)['Ncells'] == 30) # export/import contours 2d to native c1 = hm.add_rect_contour([0, 0], [1, 1], 1) c2 = hm.add_rect_contour([0, 0], [2, 2], 2) hm.export_contour_hmc(c1, "c1.hmc") hm.export_contour_hmc(c2, "c2.hmc", "bin") hm.export_contour_hmc([c1, c2], "c3.hmc", "bin")
# Register separate boundary types for edges on tube, roof and window. # All other boundary edges will get default 0 boundary type btube = hm.add_boundary_type(1, "Tube") broof = hm.add_boundary_type(2, "Roof") bwindow = hm.add_boundary_type(3, "Window") # First we create a house silhouette and assign boundary types for it. # They will be carried to all grids derived from this contour house_cont = hm.create_contour( [[0, 0], [1, 0], [1, 0.7], [0.5, 1.3], [0, 0.7], [0, 0]], [0, 0, broof, broof, 0]) # Build a background rectangular mesh and cut a house silhouette from it. # Now 'house' grid bears boundary features of 'house_cont' back_grid = hm.add_unf_rect_grid([0, 0], [1, 2], 50, 100) house = hm.exclude_contours(back_grid, house_cont, "outer") # A set of ugly cells near the roof appeared as a result of previous operation. # To get rid of 'em we build a short single layered boundary grid near the # roof section and make a union. 'roof' grid inherits its boundary types # from 'house_cont' which is its source and delivers 'em to 'house_wroof' object. oproof = hm.BoundaryGridOptions(house_cont, start_point=[1, 0.7], end_point=[0, 0.7], partition=[0, 0.01], bnd_step=0.02) roof = hm.build_boundary_grid([oproof]) house_wroof = hm.unite_grids(house, [(roof, 0.03)]) # Build a tube rectangle and set all boundary edges to 'Roof' type
hm.check_compatibility("0.5.0", 2) ncirc = 256 # precision of circle contours # I. Build body parts grids *************************************************** # build body contour as intersection of two circles circ1 = hm.add_circ_contour3([6, 0], [-6, 0], 0.08, ncirc) circ2 = hm.add_circ_contour3([-6, 0], [6, 0], 0.08, ncirc) bodyc = hm.clip_domain(circ1, circ2, "intersection") # assemble a prototype grid for body by attaching triangles to left and # right side of a rectangle. ntri, nspan = 12, 60 # vertical and horizontal partition of rectangle g3l = hm.add_triangle_grid([0, 0], [-0.5, 0.5], [0, 1], ntri) g3r = hm.add_triangle_grid([5, 0], [5.5, 0.5], [5, 1], ntri) g4 = hm.add_unf_rect_grid([0, 0], [5, 1], nspan, ntri) # Grids in triangle and rectangle areas have same partition at contact line # so we can simply unite them with zero buffer size gb = hm.unite_grids(g4, [(g3l, 0), (g3r, 0)]) # Map grid at hexagon area on body area so that points at the acute angles # of base grid contour be translated into acute angle vertices of body contour. # We use snap="no" since there is no need to preserve initial body contour # precisely. body = hm.map_grid(gb, bodyc, [[-0.5, 0.5], [5.5, 0.5]], [[-6, 0], [6, 0]], snap="no") # build tail area by clipping of three circle areas circ1 = hm.add_circ_contour3([0, 0], [-2, 2], 0.2, ncirc) circ2 = hm.add_circ_contour3([-2.5, -2.3], [0, 0], 0.1, ncirc) circ3 = hm.add_circ_contour3([-1.5, -1.8], [-2, 2], 0.2, ncirc)
print "exclude intersecting domain" g1 = hm.add_unf_circ_grid([0, 0], 1, 20, 10, 1.2, False) hm.set_boundary_type(g1, 1) c1 = hm.add_rect_contour([0, 0], [1, 1], [2, 3, 4, 5]) g2 = hm.exclude_contours(g1, c1, "outer") g3 = hm.exclude_contours(g1, c1, "inner") check_grid(g1, 200, 380, 181, {20: 1, 4: 180}) check_grid(g2, 61, 106, 46, {7: 1, 4: 45}) check_grid(g3, 161, 296, 136, {17: 1, 4: 135}) check_cont(g2, 25, 25, [25], {1: 5, 5: 10, 2: 10}) check_cont(g3, 35, 35, [35], {1: 15, 5: 10, 2: 10}) check_zero(hm.domain_area(g2) + hm.domain_area(g3) - hm.domain_area(g1)) print "exclude internal domain" g4 = hm.add_unf_rect_grid([0, 0], [0.3, 0.3], 2, 2) hm.set_boundary_type(g4, 6) hm.move_geom(g4, -0.5, 0.03) g5 = hm.exclude_contours(g1, g4, "outer") g6 = hm.exclude_contours(g1, g4, "inner") [c2] = hm.simplify_contour(g5) [c3] = hm.simplify_contour(g6) check_grid(g5, 29, 43, 15, {3: 3, 4: 7, 5: 3, 6: 2}) check_grid(g6, 215, 395, 180, {3: 2, 4: 169, 5: 3, 6: 4, 7: 1, 20: 1}) check_cont(g5, 22, 22, [22], {6: 22}) check_cont(c2, 4, 4, [4], {6: 4}) check_cont(g6, 42, 42, [20, 22], {1: 20, 6: 22}) check_cont(c3, 24, 24, [20, 4], {1: 20, 6: 4}) check_zero(hm.domain_area(g1) - hm.domain_area(c2) - hm.domain_area(c3))
from hybmeshpack import hmscript as hm from hybmeshpack.hmscript._dbg import check # NOQA hm.check_compatibility("0.4.6") g1 = hm.add_unf_rect_grid(nx=10, ny=10) z = hm.partition_segment(0, 4, 0.1, 0.1) g2 = hm.extrude_grid(g1, z, 1, 2, 3) g3 = hm.tetrahedral_fill(g2) g4 = hm.add_unf_rect_grid([-1, -1], [2, 2], nx=10, ny=10) z = hm.partition_segment(-2, 6, 0.4, 0.4) g5 = hm.extrude_grid(g4, z, 4, 5, 6) g6 = hm.tetrahedral_fill([g3, g5]) ss = hm.grid3_bnd_to_surface(g6, separate=True) check(abs(hm.domain_volume(g6) - 68.0) < 1e-8) check(abs(hm.domain_volume(ss[0]) - 72.0) < 1e-8) check(abs(hm.domain_volume(ss[1]) - 4.0) < 1e-8) ss = hm.grid3_bnd_to_surface(g6, separate=False) check(abs(hm.domain_volume(ss) - 68.0) < 1e-8) g7 = hm.merge_grids3(g6, g2) check(abs(hm.domain_volume(g7) - 72.0) < 1e-8) hm.export3d_grid_vtk(g7, "g7.vtk")
from hybmeshpack import hmscript as hm cn_bufsize = 0.100 dm_bufsize = 0.050 mr_min = [-3.500e+0, -5.000e+0] mr_max = [3.500e+0, 5.000e+0] mr_step = 0.050 with open("others/botscript1_adata/x1arr.dat") as f: x1arr = map(float, f.read().split()) with open("others/botscript1_adata/x2arr.dat") as f: x2arr = map(float, f.read().split()) cn = hm.add_unf_rect_grid([0, 0], [1, 1], 3, 3, x1arr, x2arr) mr = hm.add_unf_rect_grid(mr_min, mr_max, 13, 13, mr_step, mr_step) hm.export_grid_hmg([cn, mr], "op1.hmg") cn = hm.unite_grids(cn, [(mr, cn_bufsize)], False, False, 0, "4") dpath = "others/botscript1_adata/" files = ["000.msh", "001.msh", "002.msh", "003.msh", "004.msh"] for f in files: print(f) dimple = hm.import_grid_gmsh(dpath + f) hm.export_grid_hmg([cn, dimple], "op" + f[:3] + ".hmg") cn = hm.unite_grids(cn, [(dimple, dm_bufsize)], False, False, 0, "4") hm.export_grid_vtk(cn, "g1.vtk")
def check_grid(grid, nn, ne, nc, ct): info = hm.info_grid(grid) check(info['Nnodes'] == nn) check(info['Nedges'] == ne) check(info['Ncells'] == nc) for k in ct.keys(): check(info['cell_types'][k] == ct[k]) def check_zero(a): check(abs(a) < 1e-8) print "union of rect and ring" g1 = hm.add_unf_rect_grid([-1, -1], [1, 1], 10, 10) g2 = hm.add_unf_ring_grid([0, 0], 0.5, 0.8, 500, 7) hm.move_geom([g2], 0, 1) g3 = hm.unite_grids(g1, [(g2, 0.1)], empty_holes=False) hm.export_grid_vtk(g3, "g3.vtk") check(hm.skewness(g3)['ok']) fular = hm.domain_area(g3) - 0.5 * hm.domain_area(g2) - hm.domain_area(g1) check(abs(fular) < 1e-6) print "two squares: with/without fix_bnd, bc check" g4 = hm.add_unf_rect_grid([10, 10], [15, 15], 5, 5) g5 = hm.add_unf_rect_grid([10, 10], [11, 11], 30, 30) hm.move_geom(g5, -0.05, -0.05) hm.set_boundary_type(g4, 3) hm.set_boundary_type(g5,
c_rappendix, [c[9], c[10], [231.5, 1608.5], [286.399, 1647.06]], [c[11], c[12], [1164, 1611], [1109.88, 1649.44]], "no", "vertex", is_reversed=True) # left input: g6 p1 = hm.get_point(c_bot, vclosest=[-592.302, 1483.16]) p2 = hm.get_point(c_bot, vclosest=[-601.99, 1483.13]) [c1] = hm.extract_subcontours(c_bot, [c[16], c[15]]) [cr, ct, cl] = hm.extract_subcontours(c1, [c[16], p1, p2, c[15]], "vertex") [cb] = hm.extract_subcontours(g1, [c[16], c[15]], project_to="line") ali = hm.connect_subcontours([cb, cr, ct, cl], fix=[0, 2], close="yes") go = hm.add_unf_rect_grid(nx=3, ny=10) gb = hm.add_rect_contour([0, 0], [1, 1], [0, 0, binput_left2, 0]) go = hm.exclude_contours(go, gb, "outer") g6 = hm.map_grid(go, ali, [[0, 0], [1, 0], [1, 1], [0, 1], [0, 0.5]], [c[15], c[16], p1, p2, [-590, 1477]], project_to="vertex", algo="inverse_laplace") # right input: g3 p0 = hm.get_point(g1cont, eclosest=c[18]) p1 = hm.get_point(c_bot, vclosest=[1785.79, 1489.68]) p2 = hm.get_point(c_bot, vclosest=[1776.04, 1489.65]) p3 = hm.get_point(g1cont, eclosest=c[17]) p4 = hm.get_point(g1cont, eclosest=[1748.54, 1481.69]) p5 = hm.get_point(g1cont, eclosest=[1742, 1482])
import math global math # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv # register boundary types global bx0, bx1, by0, by1, bz0, bz1, bcustom bx0 = hmscript.add_boundary_type(1, "x0-boundary") bx1 = hmscript.add_boundary_type(2, "x1-boundary") by0 = hmscript.add_boundary_type(3, "y0-boundary") by1 = hmscript.add_boundary_type(4, "y1-boundary") bz0 = hmscript.add_boundary_type(5, "z0-boundary") bz1 = hmscript.add_boundary_type(6, "z1-boundary") bcustom = hmscript.add_boundary_type(7, "bcustom") # unit square 2D grid square = hmscript.add_unf_rect_grid([0, 0], [1, 1], 5, 5) # assign boundary types to 2D geometry so it could be inherited by 3D object def assign_boundary2d(x0, y0, x1, y1, b): if x0 == 0 and x1 == 0: return bx0 if x0 == 1 and x1 == 1: return bx1 if y0 == 0 and y1 == 0: return by0 if y0 == 1 and y1 == 1: return by1 hmscript.set_boundary_type(square, bfun=assign_boundary2d)
# build a contour from read points. # xy[0] = xy[-1] hence resulting contour will be closed. # Otherwise we'd have to call xy.append(xy[0][:]) manually. foil = hm.create_contour(xy, bfoil) # rotate at 10 degrees hm.rotate_geom(foil, -10, [0, 0]) # === build substrate grid # Here we need a rectangular grid with refinement at foil location. # First we create two list of doubles defining x and y coordinates, vert = hm.partition_segment(-1, 1, 0.2, 0.2, [-0.2, 0.03, 0.15, 0.015]) horiz = hm.partition_segment(-1, 3, 0.2, 0.2, [-0.2, 0.03, 1.1, 0.03]) # then build rectangular grid on their basis. substrate = hm.add_unf_rect_grid(custom_x=horiz, custom_y=vert) # boundary types for back grid which will be later translated to 3d grid def _substrate_bfun(x0, y0, x1, y1, bt): if abs(x0 - x1) < 1e-12: return binp if abs(x0 + 1) < 1e-12 else bout else: return bbot if abs(y0 + 1) < 1e-12 else btop hm.set_boundary_type(substrate, bfun=_substrate_bfun) hm.export_grid_vtk(substrate, "substrate.vtk") # === build boundary grid around the foil # First we make custom repartition of the foil to use
from hybmeshpack import hmscript # START OF EXAMPLE # lower level grid g1 = hmscript.add_unf_rect_grid([0, 0], [10, 10], 10, 10) # first imposition grid g2 = hmscript.add_unf_rect_grid([0, 0], [3, 3], 7, 7) # second imposition grid g3 = hmscript.add_unf_circ_grid([5, 5], 1.5, 10, 4) # impose grids impgrid = hmscript.unite_grids(g1, [(g2, 2.0), (g3, 1.0)]) # END OF EXAMPLE print "unite_grids example" hmscript.export_grid_vtk(impgrid, "_g2.vtk") if (abs(hmscript.domain_area(impgrid) - 100) > 1e-8): raise Exception if (not hmscript.skewness(impgrid)['ok']): raise Exception
from hybmeshpack.hmscript._dbg import check, check_ascii_file, checkdict global hm, check hm.check_compatibility("0.4.6") def check_cont(cont, nn, ne, scont, btypes): info = hm.info_contour(cont) check(info['Nnodes'] == nn) check(info['Nedges'] == ne) check(cmp(sorted(info['subcont']), sorted(scont)) == 0) for k in btypes.keys(): check(info['btypes'][k] == btypes[k]) print "contours unite" g1 = hm.add_unf_rect_grid([-1, -1], [1, 1], 10, 10) c1 = hm.add_rect_contour([-5, -5], [5, 5], [1, 1, 1, 1]) c2 = hm.unite_contours([c1, g1]) g2 = hm.add_unf_rect_grid([-0.4, -0.4], [0.4, 0.4], 4, 4) c3 = hm.unite_contours([c2, g2]) c4 = hm.unite_contours([c1, g1, g2]) area_g1 = hm.domain_area(g1) area_c1 = hm.domain_area(c1) area_g2 = hm.domain_area(g2) area_full = hm.domain_area(c4) check_cont(g1, 40, 40, [40], {0: 40}) check_cont(c1, 4, 4, [4], {1: 4}) check_cont(c2, 44, 44, [4, 40], {0: 40, 1: 4}) check_cont(g2, 16, 16, [16], {0: 16}) check_cont(c3, 60, 60, [4, 16, 40], {0: 56, 1: 4})