Esempio n. 1
0
    def domain(self, domaintype='meshpy'):
        if domaintype is 'meshpy':
            from meshpy.triangle import MeshInfo
            domain = MeshInfo()
            points = np.zeros((16, 2), dtype=np.float)
            points[0:4, :] = np.array([(0.0, 0.0), (1.0, 0.0), (1.0, 1.0),
                                       (0.0, 1.0)],
                                      dtype=np.float)
            idx = np.arange(5, -1, -1)
            points[4:10, 0] = 0.25 + 0.1 * np.cos(idx * np.pi / 3)
            points[4:10, 1] = 0.75 + 0.1 * np.sin(idx * np.pi / 3)
            points[10:, 0] = 0.6 + 0.1 * np.cos(idx * np.pi / 3)
            points[10:, 1] = 0.4 + 0.1 * np.sin(idx * np.pi / 3)

            facets = np.zeros((16, 2), dtype=np.int)
            facets[0:4, :] = np.array([(0, 1), (1, 2), (2, 3), (3, 0)],
                                      dtype=np.int)
            facets[4:10, :] = np.array([(4, 5), (5, 6), (6, 7), (7, 8), (8, 9),
                                        (9, 4)],
                                       dtype=np.int)
            facets[10:, :] = np.array([(10, 11), (11, 12), (12, 13), (13, 14),
                                       (14, 15), (15, 10)],
                                      dtype=np.int)
            domain.set_points(points)
            domain.set_facets(facets)
            domain.set_holes([(0.25, 0.75), (0.6, 0.4)])
        return domain
Esempio n. 2
0
    def triangulate(self, options=None):
        if options is None:
            options = self._get_triangle_options()
        mesh_info = MeshInfo()
        mesh_info.set_points(self.vertices)

        if self.boundary is not None:
            segments = []
            for b in self.boundary:
                segments += self.get_segments(b)
            mesh_info.set_facets(segments)

        if self.holes is not None:
            mesh_info.set_holes(self.holes)

        try:
            import locale
        except ImportError:
            use_locale = False
        else:
            use_locale = True
            prev_num_locale = locale.getlocale(locale.LC_NUMERIC)
            locale.setlocale(locale.LC_NUMERIC, "C")

        try:
            mesh = MeshInfo()
            internals.triangulate(options, mesh_info, mesh, MeshInfo(), None)
        finally:
            # restore previous locale if we've changed it
            if use_locale:
                locale.setlocale(locale.LC_NUMERIC, prev_num_locale)

        return mesh
Esempio n. 3
0
def make_mesh(max_volume):
    def round_trip_connect(seq):
        result = []
        for i in range(len(seq)):
            result.append((i, (i+1)%len(seq)))
        return result

    shapes = read_shape()

    #from matplotlib.pyplot import plot,show
    #plot(shapes[0][:,0], shapes[0][:,1])
    #show()

    from meshpy.geometry import GeometryBuilder, Marker
    builder = GeometryBuilder()

    for shape in shapes:
        from meshpy.geometry import make_box
        points = shape
        facets = round_trip_connect(range(len(points)))
        builder.add_geometry(points=points, facets=facets,
                facet_markers=Marker.FIRST_USER_MARKER)

    points, facets, facet_markers = make_box((-200, -600), (400, -300))
    builder.add_geometry(points=points, facets=facets,
            facet_markers=facet_markers)

    def transform(pt):
        x, y = pt
        return -0.01*x, -0.01*y

    builder.apply_transform(transform)

    from meshpy.triangle import MeshInfo, build
    mi = MeshInfo()
    builder.set(mi)
    holes = []
    for shape, sign, frac in zip(shapes, [1, 1, -1, 1, 1, 1], [0.5, 0, 0, 0, 0, 0]):
        avg = np.average(shape, axis=0)
        start_idx = int(frac*shape.shape[0])
        start = shape[start_idx]
        holes.append(transform(start + sign*0.01*(avg-start)))

    mi.set_holes(holes)

    mesh = build(mi,
            allow_boundary_steiner=True,
            generate_faces=True,
            max_volume=max_volume)

    return mesh
Esempio n. 4
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)
Esempio n. 5
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")]
    )
Esempio n. 6
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)
Esempio n. 7
0
File: naca.py Progetto: 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")]
    )