Exemplo n.º 1
0
    def animation_plot(self, method='quad', plot=True):

        if method == 'quad':
            cell = np.array([[0, 1, 2, 3], [1, 4, 5, 2]], dtype=np.int)
            node = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [2, 0], [2, 1]],
                            dtype=np.float)
            mesh = QuadrangleMesh(node, cell)
            mesh = HalfEdgeMesh2d.from_mesh(mesh)
            mesh.init_level_info()
            halfedge = mesh.ds.halfedge
            cstart = mesh.ds.cellstart
            NE = mesh.ds.NE
            color = 3 * np.ones(NE * 2, dtype=np.int_)
            color[1] = 1
            while (color == 3).any():
                red = color == 1
                gre = color == 0
                color[halfedge[red][:, [2, 3, 4]]] = 0
                color[halfedge[gre][:, [2, 3, 4]]] = 1
            colorlevel = ((color == 1) | (color == 2)).astype(np.int_)
            mesh.hedgecolor = {'color': color, 'level': colorlevel}
            mesh.ds.NV = 4
        elif method == 'rg':
            cell = np.array([[0, 1, 2], [0, 2, 3], [1, 4, 5], [2, 1, 5]],
                            dtype=np.int)
            node = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [2, 0], [2, 1]],
                            dtype=np.float)
            mesh = TriangleMesh(node, cell)
            mesh = HalfEdgeMesh2d.from_mesh(mesh)
            mesh.init_level_info()
            halfedge = mesh.ds.halfedge
            cstart = mesh.ds.cellstart
            NE = mesh.ds.NE
            color = np.zeros(NE * 2, dtype=np.int_)
            mesh.hedgecolor = color
            #mesh.ds.NV = 3
        elif method == 'nvb':
            cell = np.array([[0, 1, 2], [0, 2, 3], [1, 4, 5], [2, 1, 5]],
                            dtype=np.int)
            node = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [2, 0], [2, 1]],
                            dtype=np.float)
            mesh = TriangleMesh(node, cell)
            mesh = HalfEdgeMesh2d.from_mesh(mesh)
            mesh.init_level_info()
            halfedge = mesh.ds.halfedge
            cstart = mesh.ds.cellstart
            NE = mesh.ds.NE
            color = np.zeros(NE * 2, dtype=np.int_)
            color[[2, 3, 10, 11]] = 1
            mesh.hedgecolor = color
        elif method == 'poly':
            node = np.array([
                (0.0, 0.0),
                (0.0, 1.0),
                (1.0, 0.0),
                (1.0, 1.0),
            ],
                            dtype=np.float)
            cell = np.array([0, 2, 3, 0, 3, 1], dtype=np.int)
            cellLocation = np.array([0, 3, 6], dtype=np.int)

            mesh = PolygonMesh(node, cell, cellLocation)
            mesh = HalfEdgeMesh2d.from_mesh(mesh)
            mesh.init_level_info()
            halfedge = mesh.ds.halfedge
            cstart = mesh.ds.cellstart
        r = 0.5
        h = 1e-2
        k = 0
        N = 19
        fig = plt.figure()
        axes = fig.gca()
        plt.ion()
        for i in range(N):
            c = np.array([i * (2 / N), 0.8])
            k = 0
            sta1 = time.time()
            while True:
                halfedge = mesh.ds.halfedge
                pre = halfedge[:, 3]
                node = mesh.entity('node')
                flag = np.linalg.norm(node - c, axis=1) < r
                flag1 = flag[halfedge[:, 0]].astype(int)
                flag2 = flag[halfedge[pre, 0]].astype(int)
                isMarkedHEdge = flag1 + flag2 == 1
                NC = mesh.number_of_all_cells()
                isMarkedCell = np.zeros(NC, dtype=np.bool_)
                isMarkedCell[halfedge[isMarkedHEdge, 1]] = True
                isMarkedCell[cstart:] = isMarkedCell[cstart:] & (
                    mesh.cell_area() > h**2)
                if (~isMarkedCell[cstart:]).all():
                    break
                if method == 'quad':
                    mesh.refine_quad(isMarkedCell)
                elif method == 'rg':
                    mesh.refine_triangle_rg(isMarkedCell)
                elif method == 'nvb':
                    mesh.refine_triangle_nvb(isMarkedCell)
                elif method == 'poly':
                    mesh.refine_poly(isMarkedCell)
                k += 1
                print('加密', k, i, '次***************************')
            k = 0
            sta2 = time.time()

            aa = 2 * i
            bb = 2 * i + 1
            plt.cla()
            mesh.add_plot(axes, linewidths=0.4)
            #fig.savefig('%f.png' %aa, dpi=600, bbox_inches='tight')
            plt.pause(0.01)

            while k < 10:
                halfedge = mesh.ds.halfedge
                pre = halfedge[:, 3]
                node = mesh.entity('node')
                flag = np.linalg.norm(node - c, axis=1) < r
                flag1 = flag[halfedge[:, 0]].astype(int)
                flag2 = flag[halfedge[pre, 0]].astype(int)
                isMarkedHEdge = flag1 + flag2 == 1
                NC = mesh.number_of_all_cells()
                isMarkedCell = np.zeros(NC, dtype=np.bool_)
                isMarkedCell[halfedge[isMarkedHEdge, 1]] = True
                isMarkedCell[cstart:] = ~isMarkedCell[cstart:] & (
                    mesh.cell_area() < 0.5)
                if method == 'quad':
                    mesh.coarsen_quad(isMarkedCell)
                elif method == 'rg':
                    mesh.coarsen_triangle_rg(isMarkedCell)
                elif method == 'nvb':
                    mesh.coarsen_triangle_nvb(isMarkedCell)
                elif method == 'poly':
                    mesh.coarsen_poly(isMarkedCell)
                if (~isMarkedCell).all():
                    break
                k += 1
                print('循环', k, '次***************************')
            plt.cla()
            mesh.add_plot(axes, linewidths=0.4)
            #fig.savefig('%f.png' %bb, dpi=600, bbox_inches='tight')
            sta3 = time.time()
            plt.pause(0.01)
            print('加密时间:', sta2 - sta1)
            print('粗化时间:', sta3 - sta2)
        plt.ioff()
        plt.show()
Exemplo n.º 2
0
    def animation_plot(self, method='quad', plot=True):

        if method == 'quad':
            cell = np.array([[0, 1, 2, 3], [1, 4, 5, 2]], dtype=np.int)
            node = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [2, 0], [2, 1]],
                            dtype=np.float)
            mesh = QuadrangleMesh(node, cell)
            mesh = HalfEdgeMesh2d.from_mesh(mesh)
            mesh.init_level_info()
            halfedge = mesh.ds.halfedge
            cstart = mesh.ds.cellstart
            NE = mesh.ds.NE
            color = 3 * np.ones(NE * 2, dtype=np.int_)
            color[1] = 1
            while (color == 3).any():
                red = color == 1
                gre = color == 0
                color[halfedge[red][:, [2, 3, 4]]] = 0
                color[halfedge[gre][:, [2, 3, 4]]] = 1
            colorlevel = ((color == 1) | (color == 2)).astype(np.int_)
            mesh.hedgecolor = {'color': color, 'level': colorlevel}
        elif method == 'rg':
            cell = np.array([[0, 1, 2], [0, 2, 3], [1, 4, 5], [2, 1, 5]],
                            dtype=np.int)
            node = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [2, 0], [2, 1]],
                            dtype=np.float)
            mesh = TriangleMesh(node, cell)
            mesh = HalfEdgeMesh2d.from_mesh(mesh)
            mesh.init_level_info()
            halfedge = mesh.ds.halfedge
            cstart = mesh.ds.cellstart
            NE = mesh.ds.NE
            color = np.zeros(NE * 2, dtype=np.int_)
            mesh.hedgecolor = color
        elif method == 'nvb':
            cell = np.array([[0, 1, 2], [0, 2, 3], [1, 4, 5], [2, 1, 5]],
                            dtype=np.int)
            node = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [2, 0], [2, 1]],
                            dtype=np.float)
            mesh = TriangleMesh(node, cell)
            mesh = HalfEdgeMesh2d.from_mesh(mesh)
            mesh.init_level_info()
            halfedge = mesh.ds.halfedge
            cstart = mesh.ds.cellstart
            NE = mesh.ds.NE
            color = np.zeros(NE * 2, dtype=np.int_)
            color[[2, 3, 10, 11]] = 1
            mesh.hedgecolor = color
        elif method == 'poly':
            node = np.array([
                (0.0, 0.0),
                (0.0, 1.0),
                (1.0, 0.0),
                (1.0, 1.0),
            ],
                            dtype=np.float)
            cell = np.array([0, 2, 3, 0, 3, 1], dtype=np.int)
            cellLocation = np.array([0, 3, 6], dtype=np.int)

            mesh = PolygonMesh(node, cell, cellLocation)
            mesh = HalfEdgeMesh2d.from_mesh(mesh)
            mesh.init_level_info()
            halfedge = mesh.ds.halfedge
            cstart = mesh.ds.cellstart
        r = 0.5
        h = 1e-2
        k = 0
        N = 30
        fig = plt.figure()
        axes = fig.gca()
        plt.ion()
        for i in range(N):
            c = np.array([i * (2 / N), 0.8])
            k = 0
            sta1 = time.time()
            while True:
                halfedge = mesh.ds.halfedge
                pre = halfedge[:, 3]
                node = mesh.entity('node')
                flag = np.linalg.norm(node - c, axis=1) < r
                flag1 = flag[halfedge[:, 0]].astype(int)
                flag2 = flag[halfedge[pre, 0]].astype(int)
                isMarkedHEdge = flag1 + flag2 == 1
                NC = mesh.number_of_all_cells()
                isMarkedCell = np.zeros(NC, dtype=np.bool_)
                isMarkedCell[halfedge[isMarkedHEdge, 1]] = True
                isMarkedCell[cstart:] = isMarkedCell[cstart:] & (
                    mesh.cell_area() > h**2)
                if (~isMarkedCell[cstart:]).all():
                    break
                if method == 'quad':
                    mesh.refine_quad(isMarkedCell)
                elif method == 'rg':
                    mesh.refine_triangle_rg(isMarkedCell)
                elif method == 'nvb':
                    mesh.refine_triangle_nvb(isMarkedCell)
                elif method == 'poly':
                    mesh.refine_poly(isMarkedCell)
                k += 1
                print('加密', k, i, '次***************************')
            k = 0
            sta2 = time.time()
            while k < 10:
                halfedge = mesh.ds.halfedge
                pre = halfedge[:, 3]
                node = mesh.entity('node')
                flag = np.linalg.norm(node - c, axis=1) < r
                flag1 = flag[halfedge[:, 0]].astype(int)
                flag2 = flag[halfedge[pre, 0]].astype(int)
                isMarkedHEdge = flag1 + flag2 == 1
                NC = mesh.number_of_all_cells()
                isMarkedCell = np.zeros(NC, dtype=np.bool_)
                isMarkedCell[halfedge[isMarkedHEdge, 1]] = True
                isMarkedCell[cstart:] = ~isMarkedCell[cstart:] & (
                    mesh.cell_area() < 0.5)
                if method == 'quad':
                    mesh.coarsen_quad(isMarkedCell)
                elif method == 'rg':
                    mesh.coarsen_triangle_rg(isMarkedCell, i=i)
                elif method == 'nvb':
                    mesh.coarsen_triangle_nvb(isMarkedCell)
                elif method == 'poly':
                    mesh.coarsen_poly(isMarkedCell)
                if (~isMarkedCell).all():
                    break
                k += 1
                print('循环', k, '次***************************')
            sta3 = time.time()
            print('加密时间:', sta2 - sta1)
            print('粗化时间:', sta3 - sta2)

            if 1:
                plt.cla()
                mesh.add_plot(axes)
                #mesh.find_node(axes, showindex = True)
                #mesh.find_cell(axes, showindex = True)
                #mesh.add_halfedge_plot(axes, showindex=True)
                plt.pause(0.001)
            if i == 10000:
                break
        plt.ioff()
        plt.show()
        if 0:
            NC = mesh.number_of_all_cells()
            isMarkedCell = np.zeros(NC, dtype=np.bool_)
            isMarkedCell[[20, 23, 19, 16, 4, 5, 6, 7, 24, 15]] = True
            print('*************lll*********')
            mesh.coarsen_poly(isMarkedCell)
            NC = mesh.number_of_all_cells()
            isMarkedCell = np.zeros(NC, dtype=np.bool_)
            isMarkedCell[[1, 2, 3, 20, 10, 11, 13, 16]] = True
            print('*************lll*********')
            mesh.coarsen_poly(isMarkedCell)
            NC = mesh.number_of_all_cells()
            isMarkedCell = np.zeros(NC, dtype=np.bool_)
            isMarkedCell[[1, 5, 13, 15]] = True
            print('*************lll*********')
            mesh.coarsen_poly(isMarkedCell)
            NC = mesh.number_of_all_cells()
            isMarkedCell = np.zeros(NC, dtype=np.bool_)
            isMarkedCell[[10, 11, 12]] = True
            print('*************lll*********')
            mesh.coarsen_poly(isMarkedCell)
            NC = mesh.number_of_all_cells()
            isMarkedCell = np.zeros(NC, dtype=np.bool_)
            isMarkedCell[[4, 5, 6, 7]] = True
            print('*************lll*********')
            mesh.coarsen_poly(isMarkedCell)
            #mesh.print()
            #print(mesh.ds.edge_to_cell())
            print(np.c_[np.arange(len(halfedge)), halfedge])
            print(
                np.where(
                    halfedge[halfedge[:, 2], 3] != np.arange(len(halfedge))))
            fig = plt.figure()
            axes = fig.gca()
            mesh.add_plot(axes)
            mesh.find_node(axes, showindex=True)
            mesh.find_cell(axes, showindex=True)
            mesh.add_halfedge_plot(axes, showindex=True)
            plt.show()