def main():
    import meshpy.triangle as triangle
    import numpy as np

    points = [(1, 1), (-1, 1), (-1, -1), (1, -1)]

    for pt in np.random.randn(100, 2):
        points.append(pt * 0.1)

    def round_trip_connect(start, end):
        result = []
        for i in range(start, end):
            result.append((i, i + 1))
        result.append((end, start))
        return result

    info = triangle.MeshInfo()
    info.set_points(points)
    info.set_facets(round_trip_connect(0, 3))

    mesh = triangle.build(
        info, allow_volume_steiner=False, allow_boundary_steiner=False
    )

    triangle.write_gnuplot_mesh("triangles.dat", mesh)
Example #2
0
def main():
    import meshpy.triangle as triangle
    import math
    import pickle

    segments = 50

    points = [(-5,-1), (-1,-1), (0,-1), (0,0), (1,0), (5,0), (5,1), (1,1),
      (-1,1), (-5,1)]

    def round_trip_connect(seq):
      result = []
      for i in range(len(seq)):
        result.append((seq[i], seq[(i+1)%len(seq)]))
      return result

    info = triangle.MeshInfo()
    info.set_points(points)

    info.set_facets(
            round_trip_connect([0,1,8,9])
            +round_trip_connect([1,2,3,4,7,8])
            +round_trip_connect([4,5,6,7])
            )
    info.regions.resize(3)
    info.regions[0] = (-2,0,     1,0.1)
    info.regions[1] = (-0.5,0,   0,0.01)
    info.regions[2] = (1.5,0.5,  1,0.1)

    mesh = triangle.build(info)

    triangle.write_gnuplot_mesh("triangles.dat", mesh)

    mesh.write_neu(open("tri_pml.neu", "w"))
Example #3
0
def main():
    import meshpy.triangle as triangle
    import math

    points = [ (1,1),(-1,1),(-1,-1),(1,-1) ]

    def round_trip_connect(start, end):
      result = []
      for i in range(start, end):
        result.append((i, i+1))
      result.append((end, start))
      return result

    def needs_refinement(vertices, area ):
        vert_origin, vert_destination, vert_apex = vertices
        bary_x = (vert_origin.x + vert_destination.x + vert_apex.x) / 3
        bary_y = (vert_origin.y + vert_destination.y + vert_apex.y) / 3

        dist_center = math.sqrt( (bary_x-1)**2 + (bary_y-1)**2 )
        max_area = math.fabs( 0.05 * (dist_center-0.5) ) + 0.01
        return area > max_area

    info = triangle.MeshInfo()
    info.set_points(points)
    info.set_facets(round_trip_connect(0, len(points)-1))

    mesh = triangle.build(info, refinement_func=needs_refinement)

    mesh.write_neu(open("nico.neu", "w"))
    triangle.write_gnuplot_mesh("triangles.dat", mesh)
Example #4
0
def main():
    import meshpy.triangle as triangle
    import math

    points = [ (1,1),(-1,1),(-1,-1),(1,-1) ]

    def round_trip_connect(start, end):
      result = []
      for i in range(start, end):
        result.append((i, i+1))
      result.append((end, start))
      return result

    def needs_refinement(vertices, area ):
        vert_origin, vert_destination, vert_apex = vertices
        bary_x = (vert_origin.x + vert_destination.x + vert_apex.x) / 3
        bary_y = (vert_origin.y + vert_destination.y + vert_apex.y) / 3

        dist_center = math.sqrt( (bary_x-1)**2 + (bary_y-1)**2 )
        max_area = math.fabs( 0.05 * (dist_center-0.5) ) + 0.01
        return area > max_area

    info = triangle.MeshInfo()
    info.set_points(points)
    info.set_facets(round_trip_connect(0, len(points)-1))

    mesh = triangle.build(info, refinement_func=needs_refinement)

    mesh.write_neu(open("nico.neu", "w"))
    triangle.write_gnuplot_mesh("triangles.dat", mesh)
Example #5
0
def main():
    import meshpy.triangle as triangle
    import math
    import pickle

    segments = 50

    points = [(-5, -1), (-1, -1), (0, -1), (0, 0), (1, 0), (5, 0), (5, 1),
              (1, 1), (-1, 1), (-5, 1)]

    def round_trip_connect(seq):
        result = []
        for i in range(len(seq)):
            result.append((seq[i], seq[(i + 1) % len(seq)]))
        return result

    info = triangle.MeshInfo()
    info.set_points(points)

    info.set_facets(
        round_trip_connect([0, 1, 8, 9]) +
        round_trip_connect([1, 2, 3, 4, 7, 8]) +
        round_trip_connect([4, 5, 6, 7]))
    info.regions.resize(3)
    info.regions[0] = (-2, 0, 1, 0.1)
    info.regions[1] = (-0.5, 0, 0, 0.01)
    info.regions[2] = (1.5, 0.5, 1, 0.1)

    mesh = triangle.build(info)

    triangle.write_gnuplot_mesh("triangles.dat", mesh)

    mesh.write_neu(open("tri_pml.neu", "w"))
Example #6
0
def main():
    a6 = A6Triangulator()
    mesh = a6.make_triangulation(max_area=2e-6)

    import meshpy.triangle as triangle
    triangle.write_gnuplot_mesh("magnetron.dat", mesh)
    mesh.write_neu(open("magnetron.neu", "w"),
            bc={
                a6.cathode_marker:("cathode", a6.cathode_marker), 
                a6.anode_marker:("anode", a6.anode_marker),
                a6.open_marker:("open", a6.open_marker)})
Example #7
0
def main():
    import meshpy.triangle as triangle
    import math
    import pickle

    segments = 50

    points = [(1, 0), (1, 1), (-1, 1), (-1, -1), (1, -1), (1, 0)]

    for i in range(0, segments + 1):
        angle = i * 2 * math.pi / segments
        points.append((0.5 * math.cos(angle), 0.5 * math.sin(angle)))

    def round_trip_connect(start, end):
        result = []
        for i in range(start, end):
            result.append((i, i + 1))
        result.append((end, start))
        return result

    def needs_refinement(vertices, area):
        vert_origin, vert_destination, vert_apex = vertices
        bary_x = (vert_origin.x + vert_destination.x + vert_apex.x) / 3
        bary_y = (vert_origin.y + vert_destination.y + vert_apex.y) / 3

        dist_center = math.sqrt(bary_x**2 + bary_y**2)
        max_area = 100 * (math.fabs(0.002 * (dist_center - 0.5)) + 0.0001)
        return area > max_area

    info = triangle.MeshInfo()
    info.set_points(points)
    info.set_holes([(0, 0)])
    info.set_facets(round_trip_connect(0, len(points) - 1))

    mesh = triangle.build(
        info,
        refinement_func=needs_refinement,
    )

    triangle.write_gnuplot_mesh("triangles-unrefined.dat", mesh)
    print len(mesh.elements)

    mesh.element_volumes.setup()

    for i in range(len(mesh.elements)):
        mesh.element_volumes[i] = -1
    for i in range(0, len(mesh.elements), 10):
        mesh.element_volumes[i] = 1e-8

    mesh = triangle.refine(mesh)
    print len(mesh.elements)

    triangle.write_gnuplot_mesh("triangles.dat", mesh)
Example #8
0
def main():
    a6 = A6Triangulator()
    mesh = a6.make_triangulation(max_area=2e-6)

    import meshpy.triangle as triangle
    triangle.write_gnuplot_mesh("magnetron.dat", mesh)
    mesh.write_neu(open("magnetron.neu", "w"),
                   bc={
                       a6.cathode_marker: ("cathode", a6.cathode_marker),
                       a6.anode_marker: ("anode", a6.anode_marker),
                       a6.open_marker: ("open", a6.open_marker)
                   })
def main():
    import meshpy.triangle as triangle
    import math
    import pickle

    segments = 50

    points = [ (1,0),(1,1),(-1,1),(-1,-1),(1,-1),(1,0) ]

    for i in range( 0, segments + 1 ):
      angle = i * 2 * math.pi / segments
      points.append( ( 0.5 * math.cos( angle ), 0.5 * math.sin( angle ) ) )

    def round_trip_connect(start, end):
      result = []
      for i in range(start, end):
        result.append((i, i+1))
      result.append((end, start))
      return result

    def needs_refinement(vertices, area ):
        vert_origin, vert_destination, vert_apex = vertices
        bary_x = (vert_origin.x + vert_destination.x + vert_apex.x) / 3
        bary_y = (vert_origin.y + vert_destination.y + vert_apex.y) / 3

        dist_center = math.sqrt( bary_x**2 + bary_y**2 )
        max_area = 100*(math.fabs( 0.002 * (dist_center-0.5) ) + 0.0001)
        return area > max_area

    info = triangle.MeshInfo()
    info.set_points(points)
    info.set_holes([(0,0)])
    info.set_facets(round_trip_connect(0, len(points)-1))

    mesh = triangle.build(info, refinement_func=needs_refinement,
            )

    triangle.write_gnuplot_mesh("triangles-unrefined.dat", mesh)
    print(len(mesh.elements))

    mesh.element_volumes.setup()

    for i in range(len(mesh.elements)):
        mesh.element_volumes[i] = -1
    for i in range(0, len(mesh.elements), 10):
        mesh.element_volumes[i] = 1e-8

    mesh = triangle.refine(mesh)
    print(len(mesh.elements))

    triangle.write_gnuplot_mesh("triangles.dat", mesh)
Example #10
0
def main():
    import meshpy.triangle as triangle
    import math

    segments = 10

    points = [ (1,0),(1,1),(-1,1),(-1,-1),(1,-1),(1,0) ]

    for i in range( 0, segments + 1 ):
      angle = i * 2 * math.pi / segments
      points.append( ( 0.5 * math.cos( angle ), 0.5 * math.sin( angle ) ) )

    def round_trip_connect(start, end):
      result = []
      for i in range(start, end):
        result.append((i, i+1))
      result.append((end, start))
      return result

    def needs_refinement(vertices, area ):
        vert_origin, vert_destination, vert_apex = vertices
        bary_x = (vert_origin.x + vert_destination.x + vert_apex.x) / 3
        bary_y = (vert_origin.y + vert_destination.y + vert_apex.y) / 3

        dist_center = math.sqrt( bary_x**2 + bary_y**2 )
        max_area = math.fabs( 0.002 * (dist_center-0.5) ) + 0.0001
        return area > max_area

    info = triangle.MeshInfo()
    info.set_points(points)
    info.set_holes([(0,0)])
    info.set_facets(round_trip_connect(0, len(points)-1))

    mesh = triangle.build(info, refinement_func=needs_refinement)

    import numpy as np
    #np.set_printoptions(threshold=100000)
    print repr(np.array(mesh.points))
    print repr(np.array(mesh.elements))

    #import pickle
    #pickled = pickle.dumps(mesh)
    #mesh_2 = pickle.loads(pickled)

    triangle.write_gnuplot_mesh("triangles.dat", mesh)
Example #11
0
def main():
    def round_trip_connect(start, end):
      result = []
      for i in range(start, end):
        result.append((i, i+1))
      result.append((end, start))
      return result

    corners, mesh1, mesh2 = generate_meshes(2, 1, 0.3)
    points = get_vertices(corners, mesh1, mesh2)
    print "points", np.array(points)

    info = triangle.MeshInfo()
    info.set_points(points)
    info.set_facets(round_trip_connect(0, len(corners)-1))

    mesh = triangle.build(info, allow_volume_steiner=False, allow_boundary_steiner=False, min_angle=60)

    if False:
        print "vertices:"
        for i, p in enumerate(mesh.points):
            print i, p
        print "point numbers in triangles:"
        for i, t in enumerate(mesh.elements):
            print i, t

    finemesh = Mesh()
    ME = MeshEditor()
    ME.open(finemesh,2,2)
    ME.init_vertices(len(mesh.points))
    ME.init_cells(len(mesh.elements))
    for i,v in enumerate(mesh.points):
        ME.add_vertex(i,v[0],v[1])
    for i,c in enumerate(mesh.elements):
        ME.add_cell(i,c[0],c[1],c[2])
    ME.close()

    triangle.write_gnuplot_mesh("triangles.dat", mesh)

    plot(mesh1)
    plot(mesh2)
    plot(finemesh)
    interactive()
Example #12
0
def main():
    import meshpy.triangle as triangle

    points = [(1, 1), (-1, 1), (-1, -1), (1, -1)]

    def round_trip_connect(start, end):
        result = []
        for i in range(start, end):
            result.append((i, i + 1))
        result.append((end, start))
        return result

    info = triangle.MeshInfo()
    info.set_points(points)
    info.set_facets(round_trip_connect(0, len(points) - 1))

    mesh = triangle.build(info, max_volume=1e-3, min_angle=25)

    print("A")
    triangle.write_gnuplot_mesh("triangles.dat", mesh)
Example #13
0
def main():
    import meshpy.triangle as triangle

    points = [ (1,1),(-1,1),(-1,-1),(1,-1)]

    def round_trip_connect(start, end):
      result = []
      for i in range(start, end):
        result.append((i, i+1))
      result.append((end, start))
      return result

    info = triangle.MeshInfo()
    info.set_points(points)
    info.set_facets(round_trip_connect(0, len(points)-1))

    mesh = triangle.build(info, max_volume=1e-3, min_angle=25)

    print "A"
    triangle.write_gnuplot_mesh("triangles.dat", mesh)
def main():
    import meshpy.triangle as triangle
    import numpy as np

    points = [ (1,1),(-1,1),(-1,-1),(1,-1) ]

    for pt in np.random.randn(100, 2):
        points.append(pt*0.1)

    def round_trip_connect(start, end):
      result = []
      for i in range(start, end):
        result.append((i, i+1))
      result.append((end, start))
      return result

    info = triangle.MeshInfo()
    info.set_points(points)
    info.set_facets(round_trip_connect(0, 3))

    mesh = triangle.build(info, allow_volume_steiner=False,
            allow_boundary_steiner=False)

    triangle.write_gnuplot_mesh("triangles.dat", mesh)
Example #15
0
def make_squaremesh():
    def round_trip_connect(seq):
        result = []
        for i in range(len(seq)):
            result.append((i, (i + 1) % len(seq)))
        return result

    def needs_refinement(vertices, area):
        x = sum(numpy.array(v) for v in vertices) / 3

        max_area_volume = 0.7e-2 + 0.03 * (0.05 * x[1]**2 +
                                           0.3 * min(x[0] + 1, 0)**2)

        max_area_corners = 1e-3 + 0.001 * max(
            la.norm(x - corner)**4 for corner in obstacle_corners)

        return bool(area > 2.5 * min(max_area_volume, max_area_corners))

    from meshpy.geometry import make_box
    points, facets, _, _ = make_box((-0.5, -0.5), (0.5, 0.5))
    obstacle_corners = points[:]

    from meshpy.geometry import GeometryBuilder, Marker

    profile_marker = Marker.FIRST_USER_MARKER
    builder = GeometryBuilder()
    builder.add_geometry(points=points,
                         facets=facets,
                         facet_markers=profile_marker)

    points, facets, _, facet_markers = make_box((-16, -22), (25, 22))
    builder.add_geometry(points=points,
                         facets=facets,
                         facet_markers=facet_markers)

    from meshpy.triangle import MeshInfo, build
    mi = MeshInfo()
    builder.set(mi)
    mi.set_holes([(0, 0)])

    mesh = build(mi,
                 refinement_func=needs_refinement,
                 allow_boundary_steiner=True,
                 generate_faces=True)

    print("%d elements" % len(mesh.elements))

    from meshpy.triangle import write_gnuplot_mesh
    write_gnuplot_mesh("mesh.dat", mesh)

    fvi2fm = mesh.face_vertex_indices_to_face_marker

    face_marker_to_tag = {
        profile_marker: "noslip",
        Marker.MINUS_X: "inflow",
        Marker.PLUS_X: "outflow",
        Marker.MINUS_Y: "inflow",
        Marker.PLUS_Y: "inflow"
    }

    def bdry_tagger(fvi, el, fn, all_v):
        face_marker = fvi2fm[fvi]
        return [face_marker_to_tag[face_marker]]

    from grudge.mesh import make_conformal_mesh_ext
    vertices = numpy.asarray(mesh.points, dtype=float, order="C")
    from grudge.mesh.element import Triangle
    return make_conformal_mesh_ext(vertices, [
        Triangle(i, el_idx, vertices) for i, el_idx in enumerate(mesh.elements)
    ], bdry_tagger)
Example #16
0
def uniform_refine_triangles(points, elements, factor=2):
    new_points = points[:]
    new_elements = []
    old_face_to_new_faces = {}
    face_point_dict = {}

    points_per_edge = factor+1

    def get_refined_face(a, b):
        if a > b:
            a, b = b, a
            flipped = True
        else:
            flipped = False

        try:
            face_points = face_point_dict[a, b]
        except KeyError:
            a_pt, b_pt = [points[idx] for idx in [a, b]]
            dx = (b_pt - a_pt)/factor

            # build subdivided facet
            face_points = [a]

            for i in range(1, points_per_edge-1):
                face_points.append(len(new_points))
                new_points.append(a_pt + dx*i)

            face_points.append(b)

            face_point_dict[a, b] = face_points

            # build old_face_to_new_faces
            old_face_to_new_faces[frozenset([a, b])] = [
                    (face_points[i], face_points[i+1])
                    for i in range(factor)]

        if flipped:
            return face_points[::-1]
        else:
            return face_points

    for a, b, c in elements:
        a_pt, b_pt, c_pt = [points[idx] for idx in [a, b, c]]
        dr = (b_pt - a_pt)/factor
        ds = (c_pt - a_pt)/factor

        ab_refined, bc_refined, ac_refined = [
                get_refined_face(*pt_indices)
                for pt_indices in [(a, b), (b, c), (a, c)]]

        el_point_dict = {}

        # fill out edges of el_point_dict
        for i in range(points_per_edge):
            el_point_dict[i, 0] = ab_refined[i]
            el_point_dict[0, i] = ac_refined[i]
            el_point_dict[points_per_edge-1-i, i] = bc_refined[i]

        # fill out interior of el_point_dict
        for i in range(1, points_per_edge-1):
            for j in range(1, points_per_edge-1-i):
                el_point_dict[i, j] = len(new_points)
                new_points.append(a_pt + dr*i + ds*j)

        # generate elements
        for i in range(0, points_per_edge-1):
            for j in range(0, points_per_edge-1-i):
                new_elements.append((
                    el_point_dict[i, j],
                    el_point_dict[i+1, j],
                    el_point_dict[i, j+1],
                    ))
                if i+1+j+1 <= factor:
                    new_elements.append((
                        el_point_dict[i+1, j+1],
                        el_point_dict[i+1, j],
                        el_point_dict[i, j+1],
                        ))

    from meshpy.triangle import MeshInfo
    mi = MeshInfo()
    mi.set_points(new_points)
    mi.elements.resize(len(new_elements))
    for i, el in enumerate(new_elements):
        mi.elements[i] = el
    from meshpy.triangle import write_gnuplot_mesh
    write_gnuplot_mesh("mesh.dat", mi)

    return new_points, new_elements, old_face_to_new_faces
Example #17
0
def make_squaremesh():
    def round_trip_connect(seq):
        result = []
        for i in range(len(seq)):
            result.append((i, (i+1)%len(seq)))
        return result

    def needs_refinement(vertices, area):
        x =  sum(numpy.array(v) for v in vertices)/3

        max_area_volume = 0.7e-2 + 0.03*(0.05*x[1]**2 + 0.3*min(x[0]+1,0)**2)

        max_area_corners = 1e-3 + 0.001*max(
                la.norm(x-corner)**4 for corner in obstacle_corners)

        return bool(area > 2.5*min(max_area_volume, max_area_corners))

    from meshpy.geometry import make_box
    points, facets, _, _ = make_box((-0.5,-0.5), (0.5,0.5))
    obstacle_corners = points[:]

    from meshpy.geometry import GeometryBuilder, Marker

    profile_marker = Marker.FIRST_USER_MARKER
    builder = GeometryBuilder()
    builder.add_geometry(points=points, facets=facets,
            facet_markers=profile_marker)

    points, facets, _, facet_markers = make_box((-16, -22), (25, 22))
    builder.add_geometry(points=points, facets=facets,
            facet_markers=facet_markers)

    from meshpy.triangle import MeshInfo, build
    mi = MeshInfo()
    builder.set(mi)
    mi.set_holes([(0,0)])

    mesh = build(mi, refinement_func=needs_refinement,
            allow_boundary_steiner=True,
            generate_faces=True)

    print "%d elements" % len(mesh.elements)

    from meshpy.triangle import write_gnuplot_mesh
    write_gnuplot_mesh("mesh.dat", mesh)

    fvi2fm = mesh.face_vertex_indices_to_face_marker

    face_marker_to_tag = {
            profile_marker: "noslip",
            Marker.MINUS_X: "inflow",
            Marker.PLUS_X: "outflow",
            Marker.MINUS_Y: "inflow",
            Marker.PLUS_Y: "inflow"
            }

    def bdry_tagger(fvi, el, fn, all_v):
        face_marker = fvi2fm[fvi]
        return [face_marker_to_tag[face_marker]]

    from hedge.mesh import make_conformal_mesh_ext
    vertices = numpy.asarray(mesh.points, dtype=float, order="C")
    from hedge.mesh.element import Triangle
    return make_conformal_mesh_ext(
            vertices,
            [Triangle(i, el_idx, vertices)
                for i, el_idx in enumerate(mesh.elements)],
            bdry_tagger)
Example #18
0
def make_nacamesh():
    def round_trip_connect(seq):
        result = []
        for i in range(len(seq)):
            result.append((i, (i + 1) % len(seq)))
        return result

    pt_back = numpy.array([1, 0])

    #def max_area(pt):
    #max_area_front = 1e-2*la.norm(pt)**2 + 1e-5
    #max_area_back = 1e-2*la.norm(pt-pt_back)**2 + 1e-4
    #return min(max_area_front, max_area_back)

    def max_area(pt):
        x = pt[0]

        if x < 0:
            return 1e-2 * la.norm(pt)**2 + 1e-5
        elif x > 1:
            return 1e-2 * la.norm(pt - pt_back)**2 + 1e-5
        else:
            return 1e-2 * pt[1]**2 + 1e-5

    def needs_refinement(vertices, area):
        barycenter = sum(numpy.array(v) for v in vertices) / 3
        return bool(area > max_area(barycenter))

    from meshpy.naca import get_naca_points
    points = get_naca_points(naca_digits="2412", number_of_points=80)

    from meshpy.geometry import GeometryBuilder, Marker
    from meshpy.triangle import write_gnuplot_mesh

    profile_marker = Marker.FIRST_USER_MARKER
    builder = GeometryBuilder()
    builder.add_geometry(points=points,
                         facets=round_trip_connect(points),
                         facet_markers=profile_marker)
    builder.wrap_in_box(4, (10, 8))

    from meshpy.triangle import MeshInfo, build
    mi = MeshInfo()
    builder.set(mi)
    mi.set_holes([builder.center()])

    mesh = build(
        mi,
        refinement_func=needs_refinement,
        #allow_boundary_steiner=False,
        generate_faces=True)

    write_gnuplot_mesh("mesh.dat", mesh)

    print("%d elements" % len(mesh.elements))

    fvi2fm = mesh.face_vertex_indices_to_face_marker

    face_marker_to_tag = {
        profile_marker: "noslip",
        Marker.MINUS_X: "inflow",
        Marker.PLUS_X: "outflow",
        Marker.MINUS_Y: "inflow",
        Marker.PLUS_Y: "inflow"
        #Marker.MINUS_Y: "minus_y",
        #Marker.PLUS_Y: "plus_y"
    }

    def bdry_tagger(fvi, el, fn, all_v):
        face_marker = fvi2fm[fvi]
        return [face_marker_to_tag[face_marker]]

    from grudge.mesh import make_conformal_mesh_ext

    vertices = numpy.asarray(mesh.points, order="C")
    from grudge.mesh.element import Triangle
    return make_conformal_mesh_ext(
        vertices,
        [
            Triangle(i, el_idx, vertices)
            for i, el_idx in enumerate(mesh.elements)
        ],
        bdry_tagger,
        #periodicity=[None, ("minus_y", "plus_y")]
    )
Example #19
0
def uniform_refine_triangles(points, elements, factor=2):
    new_points = points[:]
    new_elements = []
    old_face_to_new_faces = {}
    face_point_dict = {}

    points_per_edge = factor + 1

    def get_refined_face(a, b):
        if a > b:
            a, b = b, a
            flipped = True
        else:
            flipped = False

        try:
            face_points = face_point_dict[a, b]
        except KeyError:
            a_pt, b_pt = [points[idx] for idx in [a, b]]
            dx = (b_pt - a_pt) / factor

            # build subdivided facet
            face_points = [a]

            for i in range(1, points_per_edge - 1):
                face_points.append(len(new_points))
                new_points.append(a_pt + dx * i)

            face_points.append(b)

            face_point_dict[a, b] = face_points

            # build old_face_to_new_faces
            old_face_to_new_faces[frozenset([a, b])] = [
                (face_points[i], face_points[i + 1]) for i in range(factor)
            ]

        if flipped:
            return face_points[::-1]
        else:
            return face_points

    for a, b, c in elements:
        a_pt, b_pt, c_pt = [points[idx] for idx in [a, b, c]]
        dr = (b_pt - a_pt) / factor
        ds = (c_pt - a_pt) / factor

        ab_refined, bc_refined, ac_refined = [
            get_refined_face(*pt_indices)
            for pt_indices in [(a, b), (b, c), (a, c)]
        ]

        el_point_dict = {}

        # fill out edges of el_point_dict
        for i in range(points_per_edge):
            el_point_dict[i, 0] = ab_refined[i]
            el_point_dict[0, i] = ac_refined[i]
            el_point_dict[points_per_edge - 1 - i, i] = bc_refined[i]

        # fill out interior of el_point_dict
        for i in range(1, points_per_edge - 1):
            for j in range(1, points_per_edge - 1 - i):
                el_point_dict[i, j] = len(new_points)
                new_points.append(a_pt + dr * i + ds * j)

        # generate elements
        for i in range(0, points_per_edge - 1):
            for j in range(0, points_per_edge - 1 - i):
                new_elements.append((
                    el_point_dict[i, j],
                    el_point_dict[i + 1, j],
                    el_point_dict[i, j + 1],
                ))
                if i + 1 + j + 1 <= factor:
                    new_elements.append((
                        el_point_dict[i + 1, j + 1],
                        el_point_dict[i + 1, j],
                        el_point_dict[i, j + 1],
                    ))

    from meshpy.triangle import MeshInfo
    mi = MeshInfo()
    mi.set_points(new_points)
    mi.elements.resize(len(new_elements))
    for i, el in enumerate(new_elements):
        mi.elements[i] = el
    from meshpy.triangle import write_gnuplot_mesh
    write_gnuplot_mesh("mesh.dat", mi)

    return new_points, new_elements, old_face_to_new_faces
Example #20
0
File: naca.py Project: gimac/hedge
def make_nacamesh():
    def round_trip_connect(seq):
        result = []
        for i in range(len(seq)):
            result.append((i, (i + 1) % len(seq)))
        return result

    pt_back = numpy.array([1, 0])

    # def max_area(pt):
    # max_area_front = 1e-2*la.norm(pt)**2 + 1e-5
    # max_area_back = 1e-2*la.norm(pt-pt_back)**2 + 1e-4
    # return min(max_area_front, max_area_back)

    def max_area(pt):
        x = pt[0]

        if x < 0:
            return 1e-2 * la.norm(pt) ** 2 + 1e-5
        elif x > 1:
            return 1e-2 * la.norm(pt - pt_back) ** 2 + 1e-5
        else:
            return 1e-2 * pt[1] ** 2 + 1e-5

    def needs_refinement(vertices, area):
        barycenter = sum(numpy.array(v) for v in vertices) / 3
        return bool(area > max_area(barycenter))

    from meshpy.naca import get_naca_points

    points = get_naca_points(naca_digits="2412", number_of_points=80)

    from meshpy.geometry import GeometryBuilder, Marker
    from meshpy.triangle import write_gnuplot_mesh

    profile_marker = Marker.FIRST_USER_MARKER
    builder = GeometryBuilder()
    builder.add_geometry(points=points, facets=round_trip_connect(points), facet_markers=profile_marker)
    builder.wrap_in_box(4, (10, 8))

    from meshpy.triangle import MeshInfo, build

    mi = MeshInfo()
    builder.set(mi)
    mi.set_holes([builder.center()])

    mesh = build(
        mi,
        refinement_func=needs_refinement,
        # allow_boundary_steiner=False,
        generate_faces=True,
    )

    write_gnuplot_mesh("mesh.dat", mesh)

    print "%d elements" % len(mesh.elements)

    fvi2fm = mesh.face_vertex_indices_to_face_marker

    face_marker_to_tag = {
        profile_marker: "noslip",
        Marker.MINUS_X: "inflow",
        Marker.PLUS_X: "outflow",
        Marker.MINUS_Y: "inflow",
        Marker.PLUS_Y: "inflow"
        # Marker.MINUS_Y: "minus_y",
        # Marker.PLUS_Y: "plus_y"
    }

    def bdry_tagger(fvi, el, fn, all_v):
        face_marker = fvi2fm[fvi]
        return [face_marker_to_tag[face_marker]]

    from hedge.mesh import make_conformal_mesh_ext

    vertices = numpy.asarray(mesh.points, order="C")
    from hedge.mesh.element import Triangle

    return make_conformal_mesh_ext(
        vertices,
        [Triangle(i, el_idx, vertices) for i, el_idx in enumerate(mesh.elements)],
        bdry_tagger,
        # periodicity=[None, ("minus_y", "plus_y")]
    )
Example #21
0

if __name__ == '__main__':
    #Create the intial mesh node pts
    nodepts = createInitialTriGridPts(4, 4)
    #Define the element triangles that link the nodes
    elements = defineElementConnectivity(nodepts)

    mesh_info = MeshInfo()
    print(nodepts)
    mesh_info.set_points(nodepts)
    print(elements.simplices)
    mesh_info.set_facets(elements.simplices)
    mesh = build(mesh_info)
    #mesh.triangle.
    write_gnuplot_mesh("test.vtk", mesh_info.points, facets=False)

    #triangle=showMesh(nodepts,elements.simplices)
    print(elements)
    print(elements.simplices)
    print(elements.points)

    nodepts, elements = refineMesh(nodepts, elements, 7)
    nodepts, elements = refineMesh(nodepts, elements, 20)
    #nodepts,elements = refineMesh(nodepts,elements,100)
    #nodepts,elements = refineMesh(nodepts,elements,50)
    #nodepts,elements = refineMesh(nodepts,elements,2)
    #nodepts,elements = refineMesh(nodepts,elements,10)

    triangle = showMesh(nodepts, elements.simplices, displayType='wireframe')
    neighbours = elements.neighbors[7]