def Lshapetest(self, plot=True): node = np.array([ (-1.0, -1.0), (0.0, -1.0), (1.0, -1.0), (1.0, 0.0), (0.0, 0.0), (0.0, 1.0), (-1.0, 1.0), (-1.0,0.0)], dtype=np.float) cell = np.array([[0, 1, 4, 7],[1,2,3,4],[7,4,5,6]], dtype=np.int) mesh = QuadrangleMesh(node, cell) mesh.uniform_refine(n=2) #mesh.node = mesh.node[np.lexsort(mesh.node[:,::-1].T)] ''' l = len(mesh.node) node1 = np.zeros((l,2)) node1[:9,:] = mesh.node[mesh.node[:,1] = -1] node1[9:18,:] = mesh.node[mesh.node[:,1] = -1] ''' if plot: fig = plt.figure() axes = fig.gca() mesh.add_plot(axes) mesh.node = mesh.node[np.lexsort(mesh.node[:,::-1].T)] mesh.find_node(axes,showindex = True) #mesh.find_edge(axes) #mesh.print() plt.show()
def halfedgemesh(n=4, h=4): """ 半边数据结构的网格 """ node = np.array([(0, 0), (h, 0), (h, h), (0, h)], dtype=np.float64) cell = np.array([(0, 1, 2, 3)], dtype=np.int_) mesh = QuadrangleMesh(node, cell) mesh = HalfEdgeMesh2d.from_mesh(mesh) mesh.uniform_refine(n) return mesh
def refine_quad(self, l, plot=True): 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 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} c = np.array([1, 10000.5]) r = 10000 h = 1e-2 k = 0 NB = 0 fig = plt.figure() axes = fig.gca() plt.ion() while k < l: halfedge = mesh.ds.halfedge halfedge1 = halfedge[:, 3] node = mesh.node flag = node - c flag = flag[:, 0]**2 + flag[:, 1]**2 flag = flag <= r**2 flag1 = flag[halfedge[:, 0]].astype(int) flag2 = flag[halfedge[halfedge1, 0]].astype(int) markedge = flag1 + flag2 == 1 markedcell = halfedge[markedge, 1] markedcell = np.unique(markedcell) cell = np.unique(halfedge[:, 1]) nc = cell.shape[0] markedcell1 = np.zeros(nc) markedcell1[markedcell] = 1 print('makee', markedcell) mesh.refine_quad(markedcell1.astype(np.bool_)) k += 1 print('循环', k, '次***************************') print(np.c_[np.arange(mesh.ds.NE * 2), mesh.hedgecolor['color'], mesh.hedgecolor['level']]) if plot: plt.cla() mesh.add_plot(axes) plt.pause(0.001) plt.ioff() plt.show()
def project_test(self, u, p=2, mtype=0, plot=True): from fealpy.mesh.simple_mesh_generator import triangle if mtype == 0: node = np.array([(-1, -1), (1, -1), (1, 1), (-1, 1)], dtype=np.float) cell = np.array([0, 1, 2, 3], dtype=np.int) cellLocation = np.array([0, 4], dtype=np.int) mesh = PolygonMesh(node, cell, cellLocation) elif mtype == 1: node = np.array([(-1, -1), (1, -1), (1, 1), (-1, 1)], dtype=np.float) cell = np.array([0, 1, 2, 3, 0, 2], dtype=np.int) cellLocation = np.array([0, 3, 6], dtype=np.int) mesh = PolygonMesh(node, cell, cellLocation) elif mtype == 2: h = 0.025 mesh = triangle([-1, 1, -1, 1], h, meshtype='polygon') elif mtype == 3: node = np.array([(-1, -1), (1, -1), (1, 1), (-1, 1)], dtype=np.float) cell = np.array([[0, 1, 2, 3]], dtype=np.int) mesh = QuadrangleMesh(node, cell) mesh.uniform_refine() mesh = PolygonMesh.from_mesh(mesh) elif mtype == 4: node = np.array([(-1, -1), (1, -1), (1, 0), (1, 1), (-1, 1), (-1, 0)], dtype=np.float) cell = np.array([0, 1, 2, 5, 2, 3, 4, 5], dtype=np.int) cellLocation = np.array([0, 4, 8], dtype=np.int) mesh = PolygonMesh(node, cell, cellLocation) if True: cell, cellLocation = mesh.entity('cell') edge = mesh.entity('edge') cell2edge = mesh.ds.cell_to_edge() bc = mesh.entity_barycenter('edge') uspace = ReducedDivFreeNonConformingVirtualElementSpace2d(mesh, p) up = uspace.project(u) up = uspace.project_to_smspace(up) print(up) integralalg = uspace.integralalg error = integralalg.L2_error(u, up) print(error) A = uspace.matrix_A() P = uspace.matrix_P() sio.savemat('A.mat', {"A": A.toarray(), "P": P.toarray()}) if plot: mesh.print() fig = plt.figure() axes = fig.gca() mesh.add_plot(axes) mesh.find_node(axes, showindex=True) mesh.find_edge(axes, showindex=True) mesh.find_cell(axes, showindex=True) plt.show()
def verify_matrix(self, u, p=2, mtype=0, plot=True): from fealpy.mesh.simple_mesh_generator import triangle if mtype == 0: node = np.array([(-1, -1), (1, -1), (1, 1), (-1, 1)], dtype=np.float) cell = np.array([0, 1, 2, 3], dtype=np.int) cellLocation = np.array([0, 4], dtype=np.int) mesh = PolygonMesh(node, cell, cellLocation) elif mtype == 1: node = np.array([(-1, -1), (1, -1), (1, 1), (-1, 1)], dtype=np.float) cell = np.array([0, 1, 2, 3, 0, 2], dtype=np.int) cellLocation = np.array([0, 3, 6], dtype=np.int) mesh = PolygonMesh(node, cell, cellLocation) elif mtype == 2: h = 0.1 mesh = triangle([-1, 1, -1, 1], h, meshtype='polygon') elif mtype == 3: node = np.array([(-1, -1), (1, -1), (1, 1), (-1, 1)], dtype=np.float) cell = np.array([[0, 1, 2, 3]], dtype=np.int) mesh = QuadrangleMesh(node, cell) mesh.uniform_refine() mesh = PolygonMesh.from_mesh(mesh) uspace = ReducedDivFreeNonConformingVirtualElementSpace2d(mesh, p) uspace.verify_matrix() up = uspace.project(u) up = uspace.project_to_smspace(up)
def quad_refine(): 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)
def quadMesh(self): 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} return mesh
def test_quadrangle_mesh(): from fealpy.mesh import QuadrangleMesh node = np.array( [ (0.0, 0.0), # 0 号点 (0.0, 1.0), # 1 号点 (0.0, 2.0), # 2 号点 (1.0, 0.0), # 3 号点 (1.0, 1.0), # 4 号点 (1.0, 2.0), # 5 号点 (2.0, 0.0), # 6 号点 (2.0, 1.0), # 7 号点 (2.0, 2.0), # 8 号点 ], dtype=np.float64) cell = np.array( [ (0, 3, 4, 1), # 0 号单元 (1, 4, 5, 3), # 1 号单元 (3, 6, 7, 4), # 2 号单元 (4, 7, 8, 5), # 3 号单元 ], dtype=np.int_) mesh = QuadrangleMesh(node, cell) cell2node = mesh.ds.cell_to_node() cell2edge = mesh.ds.cell_to_edge() cell2cell = mesh.ds.cell_to_cell() edge2node = mesh.ds.edge_to_node() edge2edge = mesh.ds.edge_to_edge() edge2cell = mesh.ds.edge_to_cell() node2node = mesh.ds.node_to_node() node2edge = mesh.ds.node_to_edge() node2cell = mesh.ds.node_to_cell() # 加密测试 mesh.uniform_refine()
def refine_quad(self, plot=True): 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) halfedge = mesh.ds.halfedge 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 mesh.hedgecolor = color c = np.array([0.8, 0.8]) r = 0.9 h = 1e-2 l = 10 k = 0 NB = 0 while k < l: halfedge = mesh.ds.halfedge halfedge1 = halfedge[:, 3] node = mesh.node flag = node - c flag = flag[:, 0]**2 + flag[:, 1]**2 flag = flag <= r**2 flag1 = flag[halfedge[:, 0]].astype(int) flag2 = flag[halfedge[halfedge1, 0]].astype(int) markedge = flag1 + flag2 == 1 markedcell = halfedge[markedge, 1] markedcell = np.unique(markedcell) cell = np.unique(halfedge[:, 1]) nc = cell.shape[0] markedcell1 = np.zeros(nc) markedcell1[markedcell] = 1 print('makee', markedcell) mesh.refine_quad(markedcell1.astype(np.bool_)) k += 1 print('循环', k, '次***************************') if plot: fig = plt.figure() axes = fig.gca() nindex = mesh.nodedata['level'] mesh.add_plot(axes) #mesh.add_halfedge_plot(axes, showindex=True) #mesh.find_node(axes, showindex=True, multiindex=nindex) plt.show()
def project_test(self, u, p=2, mtype=0, plot=True): from fealpy.mesh.simple_mesh_generator import triangle if mtype == 0: node = np.array([(-1, -1), (1, -1), (1, 1), (-1, 1)], dtype=np.float) cell = np.array([0, 1, 2, 3], dtype=np.int) cellLocation = np.array([0, 4], dtype=np.int) mesh = PolygonMesh(node, cell, cellLocation) elif mtype == 1: node = np.array([(-1, -1), (1, -1), (1, 1), (-1, 1)], dtype=np.float) cell = np.array([0, 1, 2, 3, 0, 2], dtype=np.int) cellLocation = np.array([0, 3, 6], dtype=np.int) mesh = PolygonMesh(node, cell, cellLocation) elif mtype == 2: h = 0.1 mesh = triangle([-1, 1, -1, 1], h, meshtype='polygon') elif mtype == 3: node = np.array([(-1, -1), (1, -1), (1, 1), (-1, 1)], dtype=np.float) cell = np.array([[0, 1, 2, 3]], dtype=np.int) mesh = QuadrangleMesh(node, cell) mesh.uniform_refine() mesh = PolygonMesh.from_mesh(mesh) cell, cellLocation = mesh.entity('cell') edge = mesh.entity('edge') cell2edge = mesh.ds.cell_to_edge() uspace = DivFreeNonConformingVirtualElementSpace2d(mesh, p) up = uspace.project(u) print(up) up = uspace.project_to_smspace(up) print(up) integralalg = uspace.integralalg error = integralalg.L2_error(u, up) print(error) if plot: fig = plt.figure() axes = fig.gca() mesh.add_plot(axes) mesh.find_node(axes, showindex=True) mesh.find_edge(axes, showindex=True) mesh.find_cell(axes, showindex=True) plt.show()
def init_mesh(self, n=1, meshtype='tri'): node = np.array([ (-1, -1), ( 0, -1), (-1, 0), ( 0, 0), ( 1, 0), (-1, 1), ( 0, 1), ( 1, 1)], dtype=np.float) if meshtype == 'tri': cell = np.array([ (2, 0, 3), (1, 3, 0), (5, 2, 6), (3, 6, 2), (6, 3, 7), (4, 7, 3)], dtype=np.int) mesh = TriangleMesh(node, cell) mesh.uniform_refine(n) return mesh elif meshtype == 'quad': cell = np.array([ (0, 1, 3, 2), (2, 3, 6, 5), (3, 4, 7, 6)], dtype=np.int) mesh = QuadrangleMesh(node, cell) mesh.uniform_refine(n) return mesh elif meshtype == 'poly': cell = np.array([ (2, 0, 3), (1, 3, 0), (5, 2, 6), (3, 6, 2), (6, 3, 7), (4, 7, 3)], dtype=np.int) mesh = TriangleMesh(node, cell) mesh.uniform_refine(n) nmesh = TriangleMeshWithInfinityNode(mesh) pnode, pcell, pcellLocation = nmesh.to_polygonmesh() pmesh = PolygonMesh(pnode, pcell, pcellLocation) return pmesh
#!/usr/bin/env python3 # import numpy as np import sys import matplotlib.pyplot as plt import time from fealpy.mesh import HalfEdgeMesh2d,Quadtree from fealpy.mesh import TriangleMesh, PolygonMesh, QuadrangleMesh 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.uniform_refine(n=4) fig = plt.figure() axes = fig.gca() mesh.add_plot(axes) mesh.find_cell(axes, showindex=True) plt.show() c = np.array([0.5,0.5]) r = 0.5 maxit = 6 k=0 while k < maxit: aopts = mesh.adaptive_options(method='numrefine', maxcoarsen=3, HB=True) node = mesh.node h = np.sqrt(mesh.cell_area()) print('NN', mesh.number_of_nodes())
def refine_quad(self, plot=True): 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.uniform_refine() mesh = HalfEdgeMesh.from_mesh(mesh) c = np.array([0.8, 0.8]) r = 0.5 h = 1e-2 l = 6 k = 0 NB = 0 while k < l: halfedge = mesh.ds.halfedge halfedge1 = halfedge[:, 3] node = mesh.node flag = node - c flag = flag[:, 0]**2 + flag[:, 1]**2 flag = flag <= r**2 flag1 = flag[halfedge[:, 0]].astype(int) flag2 = flag[halfedge[halfedge1, 0]].astype(int) markedge = flag1 + flag2 == 1 markedcell = halfedge[markedge, 1] markedcell = np.unique(markedcell) cell = np.unique(halfedge[:, 1]) nc = cell.shape[0] markedcell1 = np.zeros(nc) markedcell1[markedcell] = 1 print('makee', markedcell) mesh.refine_quad(markedcell1) k += 1 print('循环', k, '次***************************') if plot: fig = plt.figure() axes = fig.gca() nindex = mesh.nodedata['level'] mesh.add_plot(axes) #mesh.add_halfedge_plot(axes, showindex=True) #mesh.find_node(axes, showindex=True, multiindex=nindex) plt.show() if 0: fig = plt.figure() axes = fig.gca() mesh.add_plot(axes) mesh.find_node(axes, showindex=True) mesh.find_cell(axes, showindex=True) cindex, = np.nonzero(mesh.ds.cflag) fig = plt.figure() axes = fig.gca() mesh.add_plot(axes) mesh.find_node(axes, showindex=True) mesh.find_cell(axes, showindex=True, multiindex=cindex) NN = mesh.number_of_nodes() nindex = np.zeros(NN, dtype=np.int) halfedge = mesh.ds.halfedge nindex = mesh.nodedata['level'] cindex = mesh.get_data('cell', 'level') fig = plt.figure() axes = fig.gca() mesh.add_plot(axes) mesh.find_node(axes, showindex=True, multiindex=nindex) mesh.find_cell(axes, showindex=True, multiindex=cindex) plt.show()
def init_mesh(self, meshtype='tri', returnnc=False, p=None): if meshtype == 'tri': t = (np.sqrt(5) - 1)/2 node = np.array([ [ 0, 1, t], [ 0, 1,-t], [ 1, t, 0], [ 1,-t, 0], [ 0,-1,-t], [ 0,-1, t], [ t, 0, 1], [-t, 0, 1], [ t, 0,-1], [-t, 0,-1], [-1, t, 0], [-1,-t, 0]], dtype=np.float64) cell = np.array([ [6, 2, 0], [3, 2, 6], [5, 3, 6], [5, 6, 7], [6, 0, 7], [3, 8, 2], [2, 8, 1], [2, 1, 0], [0, 1,10], [1, 9,10], [8, 9, 1], [4, 8, 3], [4, 3, 5], [4, 5,11], [7,10,11], [0,10, 7], [4,11, 9], [8, 4, 9], [5, 7,11], [10,9,11]], dtype=np.int) node, d = self.project(node) if returnnc: return node, cell else: if p is None: from fealpy.mesh import TriangleMesh return TriangleMesh(node, cell) else: from fealpy.mesh import LagrangeTriangleMesh return LagrangeTriangleMesh(node, cell, p=p, surface=self) elif meshtype == 'quad': node = np.array([ (-1, -1, -1), (-1, -1, 1), (-1, 1, -1), (-1, 1, 1), (1, -1, -1), (1, -1, 1), (1, 1, -1), (1, 1, 1)], dtype=np.float64) cell = np.array([ (0, 1, 4, 5), (6, 7, 2, 3), (2, 3, 0, 1), (4, 5, 6, 7), (1, 3, 5, 7), (2, 0, 6, 4)], dtype=np.int_) node, d = self.project(node) if returnnc: return node, cell else: if p is None: from fealpy.mesh import QuadrangleMesh return QuadrangleMesh(node, cell) else: from fealpy.mesh import LagrangeQuadrangleMesh return LagrangeQuadrangleMesh(node, cell, p=p, surface=self)
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()
def refine_RB_test(self, plot=True): node = np.array([ (0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)], dtype=np.float) cell = np.array([[0, 1, 2, 3]], dtype=np.int) mesh = QuadrangleMesh(node, cell) mesh.uniform_refine() markedCell = np.array([3], dtype=np.int) mesh.refine_RB(markedCell) if plot: fig = plt.figure() axes = fig.gca() mesh.add_plot(axes) mesh.find_node(axes, showindex=True) mesh.find_edge(axes, showindex=True) mesh.find_cell(axes, showindex=True) mesh.print() plt.show()
def coarsen_quad(self, plot=True): 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 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} for i in range(1): NC = mesh.number_of_all_cells() isMarkedCell = np.ones(NC, dtype=np.bool_) mesh.refine_quad(isMarkedCell) NC = mesh.number_of_all_cells() isMarkedCell = np.zeros(NC, dtype=np.bool_) isMarkedCell[[3, 5, 7, 8]] = True #isMarkedCell[[1, 3, 12,18, 20, 17, 16, 9, 7, 2, 4,6, 7, 8, 9 , 27, 15, # 8, 23, 32, 5, 24, 16, 29, 4, 7, 28, 11, 14, 22, 31, 19]] = True mesh.coarsen_quad(isMarkedCell) print(np.c_[np.arange(len(mesh.ds.halfedge)), mesh.ds.halfedge]) if 0: NC = mesh.number_of_all_cells() isMarkedCell = np.zeros(NC, dtype=np.bool_) isMarkedCell[[ 20, 21, 23, 22, 18, 24, 26, 11, 1, 7, 13, 3, 8, 15, 12, 17 ]] = True mesh.coarsen_quad(isMarkedCell) for i in range(0): print(i, '*************************************************') NC = mesh.number_of_all_cells() isMarkedCell = np.ones(NC, dtype=np.bool_) mesh.refine_quad(isMarkedCell) print(np.where(mesh.hedgecolor['color'] == 2)) #print(np.c_[np.arange(mesh.ds.NE*2), mesh.hedgecolor['color'], # mesh.hedgecolor['level']]) #print(np.c_[np.arange(mesh.ds.NE*2), mesh.halfedgedata['level']]) if plot: fig = plt.figure() axes = fig.gca() mesh.add_plot(axes) mesh.add_halfedge_plot(axes, showindex=True) mesh.find_node(axes, showindex=True) mesh.find_cell(axes, showindex=True) plt.show()
import numpy as np from fealpy.mesh import QuadrangleMesh import matplotlib.pyplot as plt node = np.array([[0.0, 0.0], [0.5, 0.0], [1.0, 0.0], [0.0, 0.5], [0.5, 0.5], [1.0, 0.5], [0.0, 1.0], [0.5, 1.0], [1.0, 1.0]], dtype=np.float) cell = np.array([[0, 1, 4, 3], [3, 4, 7, 6], [1, 2, 5, 4], [4, 5, 8, 7]], dtype=np.int32) mesh = QuadrangleMesh(node, cell) fig = plt.figure() axes = fig.gca() mesh.add_plot(axes) mesh.find_node(axes, showindex=True) mesh.find_cell(axes, showindex=True) plt.show()
def interpolation(self, n=2, plot=True): from fealpy.pde.poisson_2d import CosCosData from fealpy.functionspace import ConformingVirtualElementSpace2d pde = CosCosData() node = np.array([(0, 0), (1, 0), (1, 1), (0, 1)], dtype=np.float64) cell = np.array([(0, 1, 2, 3)], dtype=np.int_) mesh = QuadrangleMesh(node, cell) #mesh = PolygonMesh.from_mesh(mesh) mesh = HalfEdgeMesh2d.from_mesh(mesh) mesh.uniform_refine(n=n) #mesh.print() space = ConformingVirtualElementSpace2d(mesh, p=1) uI = space.interpolation(pde.solution) up = space.project_to_smspace(uI) error = space.integralalg.L2_error(pde.solution, up) print(error) if plot: fig = plt.figure() axes = fig.gca() #mesh.add_halfedge_plot(axes, showindex=True) mesh.add_plot(axes) mesh.find_node(axes, showindex=True) mesh.find_edge(axes, showindex=True) mesh.find_cell(axes, showindex=True) plt.show()
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()
scale=1) axes.quiver(space.ccenter[:, 0], space.ccenter[:, 1], A0[:, 0], A0[:, 1], angles='xy', scale_units='xy', scale=1) plt.show() node = np.array([(0, 0), (1, 0), (1, 1), (0, 1), (0.5, 0), (1, 0.4), (0.3, 1), (0, 0.6), (0.5, 0.45)], dtype=np.float) cell = np.array([(0, 4, 8, 7), (4, 1, 5, 8), (7, 8, 6, 3), (8, 5, 2, 6)], dtype=np.int) pde = CosCosData() mesh = QuadrangleMesh(node, cell) mesh.uniform_refine(7) space = QuadBilinearFiniteElementSpace(mesh) uI = space.interpolation(pde.solution) L2 = space.integralalg.L2_error(pde.solution, uI.value) print("L2:", L2) H1 = space.integralalg.L2_error(pde.gradient, uI.grad_value) print("H1:", H1)