def edge_to_cell_test(self, plot=True): node = np.array([(0.0, 0.0), (0.0, 1.0), (0.0, 2.0), (1.0, 0.0), (1.0, 1.0), (1.0, 2.0), (2.0, 0.0), (2.0, 1.0), (2.0, 2.0)], dtype=np.float) cell = np.array([0, 3, 4, 4, 1, 0, 1, 4, 5, 2, 3, 6, 7, 4, 4, 7, 8, 5], dtype=np.int) cellLocation = np.array([0, 3, 6, 10, 14, 18], dtype=np.int) mesh = PolygonMesh(node, cell, cellLocation) mesh = HalfEdgePolygonMesh.from_polygonmesh(mesh) NE = mesh.number_of_edges() edge = mesh.entity('edge') edge2cell = mesh.ds.edge_to_cell() print('edge:') for i in range(NE): print(i, ":", edge[i], edge2cell[i]) NC = mesh.number_of_cells() cell, cellLocation = mesh.entity('cell') cell2edge = mesh.ds.cell_to_edge() for i in range(NC): print(i, ":", cell[cellLocation[i]:cellLocation[i + 1]]) print(i, ":", cell2edge[cellLocation[i]:cellLocation[i + 1]]) 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 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 cell_to_node(self): node = np.array([(0.0, 0.0), (0.0, 1.0), (0.0, 2.0), (1.0, 0.0), (1.0, 1.0), (1.0, 2.0), (2.0, 0.0), (2.0, 1.0), (2.0, 2.0)], dtype=np.float) cell = np.array([0, 3, 4, 4, 1, 0, 1, 4, 5, 2, 3, 6, 7, 4, 4, 7, 8, 5], dtype=np.int) cellLocation = np.array([0, 3, 6, 10, 14, 18], dtype=np.int) print('node:', node) print('cell:', cell) print('cellLocation:', cellLocation) mesh = PolygonMesh(node, cell, cellLocation) fig = plt.figure() axes = fig.gca() mesh.add_plot(axes) mesh.find_node(axes, showindex=True) mesh.find_cell(axes, showindex=True) mesh = HalfEdgeMesh2d.from_mesh(mesh) mesh.print() cell, cellLocation = mesh.entity('cell') print('cell:', cell) print('cellLocation:', cellLocation) 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 show_solution_test(self): node = np.array([(0.0, 0.0), (0.0, 1.0), (0.0, 2.0), (1.0, 0.0), (1.0, 1.0), (1.0, 2.0), (2.0, 0.0), (2.0, 1.0), (2.0, 2.0)], dtype=np.float) / 2.0 cell = np.array([0, 3, 4, 4, 1, 0, 1, 4, 5, 2, 3, 6, 7, 4, 4, 7, 8, 5], dtype=np.int) cellLocation = np.array([0, 3, 6, 10, 14, 18], dtype=np.int) mesh = PolygonMesh(node, cell, cellLocation) mesh = HalfEdgePolygonMesh.from_polygonmesh(mesh) def u(p): pi = np.pi x = p[..., 0] y = p[..., 1] return np.sin(pi * x) * np.sin(pi * y) node = mesh.entity('node') solution = u(node) plot = MatlabShow() plot.show_solution(mesh, solution)
import numpy as np import matplotlib.pyplot as plt from fealpy.mesh import PolygonMesh, HalfEdgeMesh2d node3 = np.array([(0.0, 0.0), (0.0, 1.0), (0.0, 2.0), (1.0, 0.0), (1.0, 1.0), (1.0, 2.0), (2.0, 0.0), (2.0, 1.0), (2.0, 2.0)], dtype=np.float64) cell3 = np.array([0, 3, 4, 4, 1, 0, 1, 4, 5, 2, 3, 6, 7, 4, 4, 7, 8, 5], dtype=np.int) cellLocation = np.array([0, 3, 6, 10, 14, 18], dtype=np.int) mesh3 = PolygonMesh(node3, cell3, cellLocation) mesh3 = HalfEdgeMesh2d.from_mesh(mesh3) mesh3.uniform_refine(2) # 这里一致加密的时候会报错 node3 = mesh3.entity("node") edge3 = mesh3.entity("edge") cell3, cellLocation = mesh3.entity("cell") halfedge = mesh3.entity("halfedge") fig3 = plt.figure() axes3 = fig3.gca() mesh3.add_plot(axes3) mesh3.find_node(axes3, showindex=True) mesh3.find_edge(axes3) plt.show()