def stokes_equation_test(self, p=2, maxit=4): from scipy.sparse import bmat from fealpy.pde.stokes_model_2d import StokesModelData_7 from fealpy.mesh.simple_mesh_generator import triangle h = 0.4 pde = StokesModelData_7() error = np.zeros((maxit, ), dtype=np.float) for i in range(maxit): mesh = pde.init_mesh(n=i + 2, meshtype='poly') if 0: fig = plt.figure() axes = fig.gca() mesh.add_plot(axes) plt.show() uspace = DivFreeNonConformingVirtualElementSpace2d(mesh, p) pspace = ScaledMonomialSpace2d(mesh, p - 1) ldof = pspace.number_of_local_dofs() 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() C = uspace.CM[:, 0, :ldof].reshape(-1) F = uspace.source_vector(pde.source) AA = bmat([[A, P.T, None], [P, None, C[:, None]], [None, C, None]], format='csr') FF = np.block([F, np.zeros(pdof + 1, dtype=uspace.ftype)]) x = np.block([uh, ph, np.zeros((1, ), dtype=uspace.ftype)]) isBdDof = np.r_['0', isBdDof, np.zeros(pdof + 1, dtype=np.bool)] gdof = udof + pdof + 1 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:-1] 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:])
def one_cell_test_0(self, p=2): from fealpy.pde.stokes_model_2d import StokesModelData_7 pde = StokesModelData_7() 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) uspace = DivFreeNonConformingVirtualElementSpace2d(mesh, p) pspace = ScaledMonomialSpace2d(mesh, p - 1) ldof = pspace.number_of_local_dofs() 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() C = uspace.CM[:, 0, :ldof].reshape(-1) F = uspace.source_vector(pde.source) AA = bmat([[A, P.T, None], [P, None, C[:, None]], [None, C, None]], format='csr') FF = np.block([F, np.zeros(pdof + 1, dtype=uspace.ftype)]) x = np.block([uh, ph, np.zeros((1, ), dtype=uspace.ftype)]) isBdDof = np.r_['0', isBdDof, np.zeros(pdof + 1, dtype=np.bool)] gdof = udof + pdof + 1 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:-1] print('ph:', ph) print('uh:', uh) up = uspace.project_to_smspace(uh) print('up:', up) integralalg = uspace.integralalg error = integralalg.L2_error(pde.velocity, up) print(error) uv = uspace.project(pde.velocity) print('uproject:', uv) up = uspace.project_to_smspace(uv) print('up', up) error = integralalg.L2_error(pde.velocity, up) print(error)