def matrix_test(self, p=2): """ node = np.array([ (-1.0, -1.0), ( 0.0, -1.0), ( 1.0, -1.0), (-1.0, 0.0), ( 1.0, 0.0), (-1.0, 1.0), ( 0.0, 1.0), ( 1.0, 1.0)], dtype=np.float) cell = np.array([0, 1, 2, 4, 7, 6, 5, 3], dtype=np.int) cellLocation = np.array([0, 8], dtype=np.int) mesh = PolygonMesh(node, cell, cellLocation) """ 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([0, 1, 2, 3], dtype=np.int) cellLocation = np.array([0, 4], dtype=np.int) mesh = PolygonMesh(node, cell, cellLocation) space = DivFreeNonConformingVirtualElementSpace2d(mesh, p) print("G:", space.G) print("B:", space.B) print("R:", space.R) print("J:", space.J) print("Q:", space.Q) print("L:", space.L) print("D:", space.D)
def stokes_equation_test(self, p=2, maxit=4): from scipy.sparse import bmat from fealpy.pde.Stokes_Model_2d import CosSinData from fealpy.mesh.simple_mesh_generator import triangle h = 0.4 pde = CosSinData() 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) 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.T.flat, ph]) isBdDof = np.block( [isBdDof, isBdDof, np.zeros(pdof, dtype=np.bool)]) gdof = 2 * 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[:, 0] = x[:udof] uh[:, 1] = x[udof:2 * udof] ph[:] = x[2 * 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:])
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)
def matrix_A_test(self, p=2): 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([0, 1, 2, 3], dtype=np.int) cellLocation = np.array([0, 4], dtype=np.int) mesh = PolygonMesh(node, cell, cellLocation) space = DivFreeNonConformingVirtualElementSpace2d(mesh, p) A = space.matrix_A() print(A)
def index2_test(self, p=2): 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([0, 1, 2, 3], dtype=np.int) cellLocation = np.array([0, 4], dtype=np.int) mesh = PolygonMesh(node, cell, cellLocation) space = DivFreeNonConformingVirtualElementSpace2d(mesh, 3) idx = space.index2(p=p) print("p=", p, idx)
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 matrix_G_test(self, p=2): 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) space = DivFreeNonConformingVirtualElementSpace2d(mesh, p)
errorMatrix = np.zeros((5, 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 = DivFreeNonConformingVirtualElementSpace2d(mesh, p, q=6) pspace = ScaledMonomialSpace2d(mesh, p-1) uh = uspace.function() ph = pspace.function() # 缩减非协调向量虚单元空间 tuspace = ReducedDivFreeNonConformingVirtualElementSpace2d(mesh, p, q=6) # 分片常数压力空间 tpspace = ScaledMonomialSpace2d(mesh, 0) isBdDof = tuspace.boundary_dof() tudof = tuspace.number_of_global_dofs() tpdof = tpspace.number_of_global_dofs() tuh = tuspace.function()