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 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()
] errorMatrix = np.zeros((3, maxit), dtype=np.float) dof = np.zeros(maxit, dtype=np.float) for i in range(maxit): #mesh = pde.init_mesh(n=i+2, meshtype='poly') mesh = pde.init_mesh(n=i + 2, meshtype='quad') mesh = PolygonMesh.from_mesh(mesh) NE = mesh.number_of_edges() NC = mesh.number_of_cells() idof = (p - 2) * (p - 1) // 2 dof[i] = NC uspace = ReducedDivFreeNonConformingVirtualElementSpace2d(mesh, p, q=6) pspace = ScaledMonomialSpace2d(mesh, 0) isBdDof = uspace.boundary_dof() udof = uspace.number_of_global_dofs() pdof = pspace.number_of_global_dofs() uh = uspace.function() ph = pspace.function() A = uspace.matrix_A() P = uspace.matrix_P() F = uspace.source_vector(pde.source) AA = bmat([[A, P.T], [P, None]], format='csr')
from fealpy.functionspace import RaviartThomasFiniteElementSpace2d from fealpy.functionspace import DivFreeNonConformingVirtualElementSpace2d from fealpy.functionspace import ReducedDivFreeNonConformingVirtualElementSpace2d from fealpy.functionspace import ScaledMonomialSpace2d from fealpy.pde.stokes_model_2d import StokesModelData_0, StokesModelData_1 p = 2 n = 5 tmesh = MeshFactory.boxmesh2d([0, 1, 0, 1], nx=5, ny=5, meshtype='tri') # RT 空间 space0 = RaviartThomasFiniteElementSpace2d(tmesh, p=p - 1) # 三角形转化为多边形网格, 注意这里要求三角形的 0 号边转化后仍然是多边形的 0 # 号边 NC = tmesh.number_of_cells() NV = 3 node = tmesh.entity('node') cell = tmesh.entity('cell')[:, [1, 2, 0]] cellLocation = np.arange(0, (NC + 1) * NV, NV) pmesh = PolygonMesh(node, cell.reshape(-1), cellLocation) # 缩减虚单元空间 space1 = ReducedDivFreeNonConformingVirtualElementSpace2d(pmesh, p=p) b0 = space1.source_vector(pde.source) b1 = space1.pressure_robust_source_vector(pde.source, space0) plt.show()
def stokes_equation_test(self, p=2, maxit=4, mtype=1): from scipy.sparse import bmat from fealpy.pde.Stokes_Model_2d import CosSinData, PolyY2X2Data from fealpy.mesh.simple_mesh_generator import triangle h = 0.4 #pde = CosSinData() pde = PolyY2X2Data() error = np.zeros((maxit, ), dtype=np.float) for i in range(maxit): 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) else: mesh = pde.init_mesh(n=i + 2, meshtype='poly') NE = mesh.number_of_edges() NC = mesh.number_of_cells() idof = (p - 2) * (p - 1) // 2 if True: fig = plt.figure() axes = fig.gca() mesh.add_plot(axes) uspace = ReducedDivFreeNonConformingVirtualElementSpace2d(mesh, p) pspace = ScaledMonomialSpace2d(mesh, 0) isBdDof = uspace.boundary_dof() udof = uspace.number_of_global_dofs() pdof = pspace.number_of_global_dofs() uh = uspace.function() ph = pspace.function() uspace.set_dirichlet_bc(uh, pde.dirichlet) A = uspace.matrix_A() P = uspace.matrix_P() F = uspace.source_vector(pde.source) AA = bmat([[A, P.T], [P, None]], format='csr') FF = np.block([F, np.zeros(pdof, dtype=uspace.ftype)]) x = np.block([uh, ph]) isBdDof = np.block( [isBdDof, isBdDof, np.zeros(NC * idof + pdof, dtype=np.bool)]) gdof = udof + pdof FF -= AA @ x bdIdx = np.zeros(gdof, dtype=np.int) bdIdx[isBdDof] = 1 Tbd = spdiags(bdIdx, 0, gdof, gdof) T = spdiags(1 - bdIdx, 0, gdof, gdof) AA = T @ AA @ T + Tbd FF[isBdDof] = x[isBdDof] x[:] = spsolve(AA, FF) uh[:] = x[:udof] ph[:] = x[udof:] up = uspace.project_to_smspace(uh) integralalg = uspace.integralalg error[i] = integralalg.L2_error(pde.velocity, up) h /= 2 print(error) print(error[0:-1] / error[1:]) plt.show()