def triangle_mesh_test(self, plot=False): node = np.array([(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)]) cell = np.array([(1, 2, 0), (3, 0, 2)]) tmesh = TriangleMesh(node, cell) tmesh.uniform_refine(n=1) mesh = HalfEdgeMesh.from_mesh(tmesh) if plot: halfedge = mesh.ds.halfedge for i, idx in enumerate(halfedge): print(i, ": ", idx) fig = plt.figure() axes = fig.gca() mesh.add_plot(axes) mesh.find_node(axes, showindex=True) mesh.find_cell(axes, showindex=True) fig = plt.figure() axes = fig.gca() mesh.add_halfedge_plot(axes) mesh.find_node(axes, showindex=True) mesh.find_cell(axes, showindex=True) plt.show()
def init_mesh(self, n=4, meshtype='tri'): node = np.array([(-1, -1), (0, -1), (-1, 0), (0, 0), (1, 0), (-1, 1), (0, 1), (1, 1)], dtype=np.float64) if meshtype == 'tri': cell = np.array([(1, 3, 0), (2, 0, 3), (3, 6, 2), (5, 2, 6), (4, 7, 3), (6, 3, 7)], dtype=np.int_) mesh = TriangleMesh(node, cell) mesh.uniform_refine(n) return mesh elif meshtype == 'quadtree': cell = np.array([(0, 1, 3, 2), (2, 3, 6, 5), (3, 4, 7, 6)], dtype=np.int_) mesh = Quadtree(node, cell) mesh.uniform_refine(n) return mesh elif meshtype == 'tritree': cell = np.array([(1, 3, 0), (2, 0, 3), (3, 6, 2), (5, 2, 6), (4, 7, 3), (6, 3, 7)], dtype=np.int_) mesh = Tritree(node, cell) mesh.uniform_refine(n) return mesh else: raise ValueError("I don't know the meshtype %s".format(meshtype))
def init_mesh(self, n=4): from ..mesh import TriangleMesh node = np.array([(0, 0), (1, 0), (1, 1), (0, 1)], dtype=np.float) cell = np.array([(1, 2, 0), (3, 0, 2)], dtype=np.int) mesh = TriangleMesh(node, cell) mesh.uniform_refine(n) return mesh
def to_conformmesh(self): NN = self.number_of_nodes() NE = self.number_of_edges() NC = self.number_of_cells() node = self.entity('node') edge = self.entity('edge') cell = self.entity('cell') isLeafCell = self.is_leaf_cell() edge2cell = self.ds.edge_to_cell() flag0 = isLeafCell[edge2cell[:, 0]] & (~isLeafCell[edge2cell[:, 1]]) flag1 = isLeafCell[edge2cell[:, 1]] & (~isLeafCell[edge2cell[:, 0]]) LCell = edge2cell[flag0, 0] RCell = edge2cell[flag1, 1] isLeafCell = self.is_leaf_cell() idx0 = edge2cell[flag0, 0] N0 = len(idx0) cell20 = np.zeros((2 * N0, 3), dtype=self.itype) cell20[0:N0, 0] = edge[flag0, 0] cell20[0:N0, 1] = cell[self.child[edge2cell[flag0, 1], 3], edge2cell[flag0, 3]] cell20[0:N0, 2] = cell[idx0, edge2cell[flag0, 2]] cell20[N0:2 * N0, 0] = edge[flag0, 1] cell20[N0:2 * N0, 1] = cell[self.child[edge2cell[flag0, 1], 3], edge2cell[flag0, 3]] cell20[N0:2 * N0, 2] = cell[idx0, edge2cell[flag0, 2]] idx1 = edge2cell[flag1, 1] N1 = len(idx1) cell21 = np.zeros((2 * N1, 3), dtype=self.itype) cell21[0:N1, 0] = edge[flag1, 1] cell21[0:N1, 1] = cell[self.child[edge2cell[flag1, 0], 3], edge2cell[flag1, 2]] cell21[0:N1, 2] = cell[idx1, edge2cell[flag1, 3]] cell21[N1:2 * N1, 0] = edge[flag1, 0] cell21[N1:2 * N1, 1] = cell[self.child[edge2cell[flag1, 0], 3], edge2cell[flag1, 2]] cell21[N1:2 * N1, 2] = cell[idx1, edge2cell[flag1, 3]] isLRCell = np.zeros(NC, dtype=np.bool) isLRCell[LCell] = True isLRCell[RCell] = True idx = np.arange(NC) cell0 = cell[(~isLRCell) & (isLeafCell)] idx0 = idx[(~isLRCell) & (isLeafCell)] cell = np.r_['0', cell0, cell20, cell21] tmesh = TriangleMesh(node, cell) tmesh.celldata['idxmap'] = np.r_['0', idx0, LCell, RCell, LCell, RCell] #现在的cell单元与原来树结构中cell的对应关系. return tmesh
def init_mesh(self, n=4, meshtype='tri'): """ generate the initial mesh """ node = np.array([(0, 0), (1, 0), (1, 1), (0, 1)], dtype=np.float) cell = np.array([(1, 2, 0), (3, 0, 2)], dtype=np.int) mesh = TriangleMesh(node, cell) mesh.uniform_refine(n) return mesh
def init_mesh(self, n=2, meshtype='tri'): 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), (0.5, 1.0), (1.0, 1.0)], dtype=np.float) cell = np.array([(3, 0, 4), (1, 4, 0), (4, 1, 5), (2, 5, 1), (6, 3, 7), (4, 7, 3), (8, 4, 9), (5, 9, 4)], dtype=np.int) mesh = TriangleMesh(node, cell) mesh.uniform_refine(n) return mesh
def init_mesh(self, n=1, meshtype='tri'): """ generate the initial mesh """ node = np.array([(0, 0), (1, 0), (1, 1), (0, 1)], dtype=np.float64) if meshtype == 'tri': cell = np.array([(1, 2, 0), (3, 0, 2)], dtype=np.int_) mesh = TriangleMesh(node, cell) mesh.uniform_refine(n) return mesh else: raise ValueError("".format)
def init_mesh(self, n=2, meshtype='tri', h=None): """ generate the initial mesh """ from fealpy.mesh import TriangleMesh node = np.array([(0, 0), (-1, -1), (0, -2), (1, -1), (2, 0), (1, 1), (0, 2), (-1, 1)], dtype=np.float) cell = np.array([(1, 2, 0), (3, 0, 2), (4, 5, 3), (0, 3, 5), (5, 6, 0), (7, 0, 6)], dtype=np.int) mesh = TriangleMesh(node, cell) mesh.uniform_refine(n=n) return mesh
def init_mesh(self, n=1, meshtype='tri'): node = np.array([ (-1, -1), ( 0, -1), ( 1, -1), (-1, 0), ( 0, 0), ( 1, 0), (-1, 1), ( 0, 1)], dtype=np.float) if meshtype == 'tri': cell = np.array([ (3, 0, 4), (1, 4, 0), (4, 1, 5), (2, 5, 1), (6, 3, 7), (4, 7, 3)], dtype=np.int) mesh = TriangleMesh(node, cell) mesh.uniform_refine(n) return mesh elif meshtype == 'poly': cell = np.array([ (3, 0, 4), (1, 4, 0), (4, 1, 5), (2, 5, 1), (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
def data_structure(self, plot=True): node = np.array([[0,0],[1,0],[1,1],[0,1],[2,0],[2,1]], dtype = np.float) cell = np.array([[0,1,2],[0,2,3],[1,4,5],[2,1,5]],dtype = np.int) mesh = TriangleMesh(node, cell) mesh = HalfEdgeMesh2d.from_mesh(mesh) mesh.print() if plot: fig = plt.figure() axes = fig.gca() mesh.add_halfedge_plot(axes, showindex=True) mesh.find_node(axes, showindex=True) mesh.find_cell(axes, showindex=True) plt.show()
def init_mesh(self, n=4, meshtype='tri', h=0.1): """ generate the initial mesh """ node = np.array([(0, 0), (1, 0), (1, 1), (0, 1)], dtype=np.float) if meshtype == 'quadtree': cell = np.array([(0, 1, 2, 3)], dtype=np.int) mesh = Quadtree(node, cell) mesh.uniform_refine(n) return mesh if meshtype == 'quad': 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) mesh = QuadrangleMesh(node, cell) mesh.uniform_refine(n) return mesh elif meshtype == 'tri': cell = np.array([(1, 2, 0), (3, 0, 2)], dtype=np.int) mesh = TriangleMesh(node, cell) mesh.uniform_refine(n) return mesh elif meshtype == 'squad': mesh = StructureQuadMesh([0, 1, 0, 1], h) return mesh else: raise ValueError("".format)
def complex_mesh(r, filename, n): import meshio mesh = meshio.read(filename) node = mesh.points node = node[:,0:2]*r cell = mesh.cells mesh.node = node mesh.cell = cell cell = cell['triangle'] isUsingNode = np.zeros(node.shape[0], dtype=np.bool) isUsingNode[cell] = True NN = isUsingNode.sum() idxmap = np.zeros(node.shape[0], dtype=np.int32) idxmap[isUsingNode] = range(NN) cell = idxmap[cell] node = node[isUsingNode] #cell = cell[:,::-1] mesh = TriangleMesh(node,cell) nmesh = TriangleMeshWithInfinityNode(mesh) ppoint, pcell, pcellLocation = nmesh.to_polygonmesh() pmesh = PolygonMesh(ppoint, pcell, pcellLocation) hmesh = HalfEdgeMesh2d.from_mesh(pmesh) hmesh.init_level_info() hmesh.convexity() hmesh.uniform_refine(n=n) mesh = PolygonMesh.from_halfedgemesh(hmesh) return mesh
def tri_cut_graph(self, fname, weight=None): data = sio.loadmat(fname) node = np.array(data['node'], dtype=np.float64) cell = np.array(data['elem'] - 1, dtype=np.int_) mesh = TriangleMesh(node, cell) mesh = HalfEdgeMesh2d.from_mesh(mesh, closed=True) mesh.ds.NV = 3 gamma = mesh.tri_cut_graph(weight=weight) writer = MeshWriter(mesh) writer.write(fname='test.vtu') for i, index in enumerate(gamma): writer = MeshWriter(mesh, etype='edge', index=index) writer.write(fname='test' + str(i) + '.vtu')
def init_mesh(self, meshdata=None): import scipy.io as sio from fealpy.mesh import TriangleMesh if meshdata is None: data = sio.loadmat('../fealpy/meshdata/saddle.mat') else: data = sio.loadmat(meshdata) node = data['node'] cell = np.array(data['elem'], dtype=np.int64) return TriangleMesh(node, cell)
def init_mesh(self, meshtype='tri', h=0.1): """ generate the initial mesh """ from meshpy.triangle import build domain = self.domain() mesh = build(domain, max_volume=h**2) node = np.array(mesh.points, dtype=np.float) cell = np.array(mesh.elements, dtype=np.int) if meshtype is 'tri': mesh = TriangleMesh(node, cell) return mesh
def show_basis(self, p=0): h = 0.5 box = [-h, 1 + h, -h, 1 + h] 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([[1, 2, 0]], dtype=np.int) mesh = TriangleMesh(node, cell) space = RaviartThomasFiniteElementSpace2d(mesh, p=p) fig = plt.figure() space.show_basis(fig, box=box) cell = np.array([[3, 0, 2]], dtype=np.int) mesh = TriangleMesh(node, cell) space = RaviartThomasFiniteElementSpace2d(mesh, p=p) fig = plt.figure() space.show_basis(fig, box=box) print(space.bcoefs) plt.show()
def init_mesh(self, n=2, meshtype='tri'): """ generate the initial mesh """ node = np.array([(0, 0), (1, 0), (1, 1), (0, 1)], dtype=np.float) cell = np.array([(1, 2, 0), (3, 0, 2)], dtype=np.int) mesh = TriangleMesh(node, cell) mesh.uniform_refine(2) threshold = lambda p: (p[:, 0] > 0.25) & (p[:, 0] < 0.75) & ( p[:, 1] > 0.25) & (p[:, 1] < 0.75) mesh.delete_cell(threshold) mesh.uniform_refine(n=n) return mesh
def Arctan(self, p=1, n=3): pde = ArctanData() node = np.array([[-1, -1], [1, -1], [1, 1], [-1, 1]], dtype=np.float_) cell = np.array([[0, 1, 2], [0, 2, 3]], dtype=np.int_) mesh = TriangleMesh(node, cell) mesh.uniform_refine(n=n) space = LagrangeFiniteElementSpace(mesh, p=p) uh = space.function() A = space.stiff_matrix() b = space.source_vector(pde.source) bc = DirichletBC(space, pde.dirichlet) A, b = bc.apply(A, b, uh) uh[:] = spsolve(A, b).reshape(-1) error0 = space.integralalg.L2_error(pde.solution, uh) error1 = space.integralalg.L2_error(pde.gradient, uh.grad_value) print(error0) print(error1)
def opt_mesh(mesh): p = mesh.p surface = mesh.surface NCN = mesh.number_of_corner_nodes() node = mesh.entity('node')[:NCN].copy() cell = mesh.entity('cell')[:, [0, -p - 1, -1]] tmesh = TriangleMesh(node, cell) alg = SurfaceTriangleMeshOptAlg(surface, tmesh) alg.run(maxit=10) node = alg.mesh.entity('node') cell = alg.mesh.entity('cell') mesh = LagrangeTriangleMesh(node, cell, p=p, surface=surface) return mesh
def init_mesh(self): from fealpy.mesh import TriangleMesh t = (np.sqrt(5) - 1) / 2 point = 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.float) 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) point, d = self.project(point) return TriangleMesh(point, cell)
def plot_basis(self, p=2, q=20, plot=True): node = np.array([ (0.0, 0.0), (1.0, 0.0), (0.5, np.sqrt(3)/2)], dtype=np.float) cell = np.array([(0, 1, 2)], dtype=np.int) mesh = TriangleMesh(node, cell) space = LagrangeFiniteElementSpace(mesh, p=p) bc = space.multi_index_matrix[1](q)/q node = mesh.bc_to_point(bc) #( NQ, 1, 2) phi = space.basis(bc) # (NQ, 1, ldof) fig = plt.figure() axes = fig.gca(projection='3d') axes.plot_trisurf( node[..., 0].reshape(-1), node[..., 1].reshape(-1), phi[..., 0].reshape(-1), cmap='rainbow', lw=0.0, antialiased=True) axes.plot_trisurf( node[..., 0].reshape(-1), node[..., 1].reshape(-1), phi[..., 1].reshape(-1), cmap='rainbow', lw=0.0, antialiased=True) plt.show()
def lineTest(self): node = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [2, 0], [2, 1]], dtype=np.float) cell = np.array([[0, 1, 2], [0, 2, 3], [1, 4, 5], [2, 1, 5]], dtype=np.int) mesh = TriangleMesh(node, cell) mesh.uniform_refine(n=5) point = np.array([[0.2, 0.5], [0.5, 0.8], [1.6, 0.5], [1.2, 0.2]]) segment = np.array([[0, 2], [1, 3]]) isCrossCell = mesh.find_crossed_cell(point, segment) print(np.where(isCrossCell)) fig = plt.figure() axes = fig.gca() for i in range(len(segment)): a = segment[i, 0] b = segment[i, 1] axes.plot(point[[a, b], 0], point[[a, b], 1], 'r') mesh.add_plot(axes) mesh.find_cell(axes, index=isCrossCell) plt.show()
def init_mesh(self, n=1, meshtype='tri'): node = np.array([(0, 0), (1, 0), (1, 1), (0, 1)], dtype=np.float) if meshtype == 'tri': cell = np.array([(1, 2, 0), (3, 0, 2)], dtype=np.int) mesh = TriangleMesh(node, cell) mesh.uniform_refine(n) return mesh elif meshtype == 'quad': nx = 2 ny = 2 mesh = StructureQuadMesh(self.box, nx, ny) mesh.uniform_refine(n) return mesh
def uniform_refine_test(self): node = np.array([[0,0],[1,0],[1,1],[0,1],[2,0],[2,1]], dtype = np.float) cell = np.array([[0,1,2],[0,2,3],[1,4,5],[2,1,5]],dtype = np.int) mesh = TriangleMesh(node, cell) N,C = mesh.uniform_refine(n=2,returnim = True) fig = plt.figure() axes = fig.gca() mesh.add_plot(axes) mesh.find_cell(axes) plt.show() print(N) print(C.toarray())
def test_triangle_mesh(): from fealpy.mesh import TriangleMesh node = np.array( [ (0.0, 0.0), # 0 号点 (1.0, 0.0), # 1 号点 (1.0, 1.0), # 2 号点 (0.0, 1.0), # 3 号点 ], dtype=np.float64) cell = np.array( [ (1, 2, 0), # 0 号单元 (3, 0, 2), # 1 号单元 ], dtype=np.int_) mesh = TriangleMesh(node, cell) # 获取测试 assert id(node) == id(mesh.entity('node')) assert id(cell) == id(mesh.entity('cell')) edge = np.array([ [0, 1], [2, 0], [3, 0], [1, 2], [2, 3], ], dtype=np.int_) assert np.all(edge == mesh.entity('edge')) isBdNode = mesh.ds.boundary_node_flag() assert np.all(isBdNode) isBdEdge = mesh.ds.boundary_edge_flag() assert isBdEdge.sum() == 4 assert isBdEdge[1] == False 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 init_mesh(self, n=2): from ..mesh import TriangleMesh node = np.array([(0, 0), (1, 0), (1, 1), (0, 1)], dtype=np.float) cell = np.array([(1, 2, 0), (3, 0, 2)], dtype=np.int) mesh = TriangleMesh(node, cell) mesh.uniform_refine(n) NN = mesh.number_of_nodes() node = np.zeros((NN + 3, 2), dtype=np.float64) node[:NN] = mesh.entity('node') node[NN:] = node[[5], :] cell = mesh.entity('cell') cell[13][cell[13] == 5] = NN cell[18][cell[18] == 5] = NN cell[19][cell[19] == 5] = NN + 1 cell[12][cell[12] == 5] = NN + 1 cell[6][cell[6] == 5] = NN + 2 return TriangleMesh(node, cell)
def complex_mesh(r, filename): import meshio mesh = meshio.read(filename) node = mesh.points node = node[:, 0:2] * r cell = mesh.cells mesh.node = node mesh.cell = cell cell = cell['triangle'] isUsingNode = np.zeros(node.shape[0], dtype=np.bool) isUsingNode[cell] = True NN = isUsingNode.sum() idxmap = np.zeros(node.shape[0], dtype=np.int32) idxmap[isUsingNode] = range(NN) cell = idxmap[cell] node = node[isUsingNode] #cell = cell[:,::-1] mesh = TriangleMesh(node, cell) nmesh = TriangleMeshWithInfinityNode(mesh) ppoint, pcell, pcellLocation = nmesh.to_polygonmesh() pmesh = PolygonMesh(ppoint, pcell, pcellLocation) return pmesh
def triMesh(self, 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() if method == 'rg': 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': 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 return mesh
return False def peak(p): x = p[..., 0] y = p[..., 1] val = 3 * (1 - x)**2 * np.exp(-(x**2) - (y + 1)**2) - 10 * ( x / 5 - pow(x, 3) - pow(y, 5)) * np.exp(-x**2 - y**2) - 1 / 3 * np.exp(-(x + 1)**2 - y**2) return val node = np.array([(-5, -5), (5, -5), (5, 5), (-5, 5)], dtype=np.float) cell = np.array([(1, 2, 0), (3, 0, 2)], dtype=np.int) mesh = TriangleMesh(node, cell) mesh.uniform_refine(5) node = mesh.entity('node') cell = mesh.entity('cell') tmesh = Tritree(node, cell) femspace = LagrangeFiniteElementSpace(mesh, p=1) uI = femspace.interpolation(peak) estimator = Estimator(uI[:], mesh, 0.2, 0.5) isExtremeNode = estimator.is_extreme_node() print(isExtremeNode.sum()) tmesh.adaptive_refine(estimator) mesh = estimator.mesh isExtremeNode = estimator.is_extreme_node()