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=0, 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) 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 mesh = TriangleMesh(node, cell) mesh.uniform_refine(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 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 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 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, 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([(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 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 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]]) 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) 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=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 = 4 ny = 4 mesh = StructureQuadMesh(self.box, nx, ny) mesh.uniform_refine(n) return mesh elif meshtype == 'poly': cell = np.array([ (1, 2, 0), (3, 0, 2)], 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 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 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 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())
edge2cell = mesh.ds.edge_to_cell() # (NE, 4) edge2node = mesh.ds.edge_to_node() # edge edge2edge = mesh.ds.edge_to_edge() # sparse, (NE, NE) node2cell = mesh.ds.node_to_cell() # sparse, (NN, NC) node2edge = mesh.ds.node_to_edge() # sparse, (NN, NE) node2node = mesh.ds.node_to_node() # sparse, (NN, NN) isBdNode = mesh.ds.boundary_node_flag() # (NN, ), bool isBdEdge = mesh.ds.boundary_edge_flag() # (NE, ), bool isBdCell = mesh.ds.boundary_cell_flag() # (NC, ), bool bdNodeIdx = mesh.ds.boundary_node_index() # bdEdgeIdx = mesh.ds.boundary_edge_index() # bdCellIdx = mesh.ds.boundary_cell_index() # mesh.uniform_refine(1) 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()
from fealpy.mesh import TriangleMesh, TriangleMeshWithInfinityNode from fealpy.decorator import cartesian, barycentric # --- mesh n = 0 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) # --- to pmesh # nmesh = TriangleMeshWithInfinityNode(mesh) # pnode, pcell, pcellLocation = nmesh.to_polygonmesh() # pmesh = PolygonMesh(pnode, pcell, pcellLocation) # mesh = pmesh # ---- plot mesh ---- # fig1 = plt.figure() # axes = fig1.gca() # mesh.add_plot(axes, cellcolor='w') # find_entity(axes, mesh, entity='cell', showindex=True, color='b', markersize=10, fontsize=8) # find_entity(axes, mesh, entity='edge', showindex=True, color='r', markersize=10, fontsize=8) # find_entity(axes, mesh, entity='node', showindex=True, color='y', markersize=10, fontsize=8) # plt.show()
node[0, :] = [0, 0, r] node[1:, 0] = 15*np.cos(theta) node[1:, 1] = 15*np.sin(theta) node[1:, 2] = h cell = np.array([ (0, 1, 2), (0, 2, 3), (0, 3, 4), (0, 4, 5), (0, 5, 6), (0, 6, 1) ], dtype=np.int32) surface = Sphere(c, r) circle = Circle(radius=rc) mesh = TriangleMesh(node, cell) for i in range(8): mesh.uniform_refine() isBdNode = mesh.ds.boundary_node_flag() r0 = np.sqrt(np.sum(mesh.node[isBdNode, 0:2]**2, axis=1)) mesh.node[isBdNode, 0:2] *=rc/r0.reshape(-1, 1) mesh.node, _ = surface.project(mesh.node) mesh0 = TriangleMesh(mesh.node.copy(), mesh.ds.cell.copy()) mesh0.node *=(r+0.03)/r mesh1 = TriangleMesh(mesh.node.copy(), mesh.ds.cell.copy()) mesh1.node *=(r-0.03)/r fig = plt.figure() axes = fig.add_subplot(111, projection='3d') #mesh.add_plot(axes)
return val def f2(p): x = p[..., 0] y = p[..., 1] val = np.exp(5 * (x**2 + (y - 1)**2)) / np.exp(10) return val theta = 0.35 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(4) integrator = mesh.integrator(3) node = mesh.entity('node') cell = mesh.entity('cell') tmesh = Tritree(node, cell) femspace = LagrangeFiniteElementSpace(mesh, p=1) integralalg = FEMeshIntegralAlg(integrator, mesh) qf = integrator bcs, ws = qf.quadpts, qf.weights u1 = femspace.interpolation(f1) grad = femspace.grad_value(u1, bcs, cellidx=None)
import numpy as np import matplotlib.pyplot as plt from fealpy.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) tmesh = TriangleMesh(node, cell) tmesh.uniform_refine(2) node = tmesh.entity('node') cell = tmesh.entity('cell') fig = plt.figure() axes = fig.gca() tmesh.add_plot(axes) tmesh.find_node(axes, showindex=True) tmesh.find_edge(axes, showindex=True) tmesh.find_cell(axes, showindex=True) plt.show()
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() fig = plt.figure()
import numpy as np import matplotlib.pyplot as plt from fealpy.mesh import TriangleMesh # 网格节点数组 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], [3, 0, 2]], dtype=np.int32) mesh = TriangleMesh(node, cell) fig = plt.figure() axes = fig.gca() mesh.add_plot(axes) mesh.find_node(axes, showindex=True) mesh.find_cell(axes, showindex=True) nodeIMatrix, cellIMatrix = mesh.uniform_refine(returnim=True) print(nodeIMatrix[0].toarray()) print(cellIMatrix[0].toarray()) fig = plt.figure() axes = fig.gca() mesh.add_plot(axes) mesh.find_node(axes, showindex=True) mesh.find_cell(axes, showindex=True) plt.show()