def show_mesh(self, p=2, plot=True): mf = MeshFactory() mesh = mf.boxmesh2d([0, 1, 0, 1], nx=2, ny=2, meshtype='tri') node = mesh.entity('node') cell = mesh.entity('cell') ltmesh = LagrangeTriangleMesh(node, cell, p=p) NN = ltmesh.number_of_nodes() mesh.ds.edge = ltmesh.lds.edge mesh.ds.edge2cell = ltmesh.lds.edge2cell node = ltmesh.entity('node') #ltmesh.print() if plot: fig = plt.figure() axes = fig.gca() mesh.add_plot(axes) mesh.find_node(axes, node=node, showindex=True, fontsize=28) mesh.find_edge(axes, showindex=True) mesh.find_cell(axes, showindex=True) plt.show()
def save_mesh(self, p=2, fname='test.vtu'): mf = MeshFactory() mesh = mf.boxmesh2d([0, 1, 0, 1], nx=2, ny=2, meshtype='tri') node = mesh.entity('node') cell = mesh.entity('cell') mesh = LagrangeTriangleMesh(node, cell, p=p) mesh.to_vtk(fname=fname)
def init_mesh(self, n=0, h=0.005, nh=100, p=1): fname = 'initial/file1.vtu' data = meshio.read(fname) node = data.points cell = data.cells[0][1] mesh = LagrangeTriangleMesh(node * 500, cell, p=p) mesh.uniform_refine(n) mesh = LagrangeWedgeMesh(mesh, h, nh, p=p) self.mesh = mesh self.p = p return mesh
def test_lagrange_triangle_mesh(p): from fealpy.mesh import LagrangeTriangleMesh 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 = LagrangeTriangleMesh(node, cell, p=p) 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()
def interpolation(self, p=2, n=1, fname='surface.vtu'): from fealpy.geometry import SphereSurface from fealpy.pde.surface_poisson_model_3d import SphereSinSinSinData pde = SphereSinSinSinData() surface = pde.domain() mesh = pde.init_mesh(n=n) node = mesh.entity('node') cell = mesh.entity('cell') mesh = LagrangeTriangleMesh(node, cell, p=p, surface=surface) space = ParametricLagrangeFiniteElementSpace(mesh, p=p) uI = space.interpolation(pde.solution) mesh.nodedata['uI'] = uI[:] error0 = space.integralalg.error(pde.solution, uI.value) error1 = space.integralalg.error(pde.gradient, uI.grad_value) print(error0, error1) mesh.to_vtk(fname=fname)
def surface_area(self, p=2): from fealpy.geometry import SphereSurface surface = SphereSurface() mesh = surface.init_mesh() e = 0 maxit = 5 for i in range(maxit): node = mesh.entity('node') cell = mesh.entity('cell') lmesh = LagrangeTriangleMesh(node, cell, p=p, surface=surface) NC = lmesh.number_of_cells() a = lmesh.cell_area() a = sum(a) a_e = (4 * np.pi) e_new = abs(a - a_e) order = np.log2(e / e_new) e = e_new print("e:", e) print("0:", order) if i < maxit - 1: mesh.uniform_refine(surface=surface)
def opt_mesh(mesh): p = mesh.p surface = mesh.surface NCN = mesh.number_of_corner_nodes() node = mesh.entity('node')[:NCN].copy() cell = mesh.entity('cell')[:, [0, -p - 1, -1]] tmesh = TriangleMesh(node, cell) alg = SurfaceTriangleMeshOptAlg(surface, tmesh) alg.run(maxit=10) node = alg.mesh.entity('node') cell = alg.mesh.entity('cell') mesh = LagrangeTriangleMesh(node, cell, p=p, surface=surface) return mesh
def surface_mesh(self, p=2, fname='surface.vtu'): from fealpy.geometry import SphereSurface, EllipsoidSurface, SphereSurfaceTest surface = SphereSurface() #surface = SphereSurfaceTest() #surface = EllipsoidSurface() #surface = ScaledSurface(surface,scale=[9,3,1]) mesh = surface.init_mesh() node = mesh.entity('node') cell = mesh.entity('cell') lmesh = LagrangeTriangleMesh(node, cell, p=p, surface=surface) NC = lmesh.number_of_cells() a = lmesh.cell_area() lmesh.to_vtk(fname=fname)
def init_mesh(self, meshtype='tri', returnnc=False, p=None): if meshtype == 'tri': t = (np.sqrt(5) - 1)/2 node = np.array([ [ 0, 1, t], [ 0, 1,-t], [ 1, t, 0], [ 1,-t, 0], [ 0,-1,-t], [ 0,-1, t], [ t, 0, 1], [-t, 0, 1], [ t, 0,-1], [-t, 0,-1], [-1, t, 0], [-1,-t, 0]], dtype=np.float64) cell = np.array([ [6, 2, 0], [3, 2, 6], [5, 3, 6], [5, 6, 7], [6, 0, 7], [3, 8, 2], [2, 8, 1], [2, 1, 0], [0, 1,10], [1, 9,10], [8, 9, 1], [4, 8, 3], [4, 3, 5], [4, 5,11], [7,10,11], [0,10, 7], [4,11, 9], [8, 4, 9], [5, 7,11], [10,9,11]], dtype=np.int) node, d = self.project(node) if returnnc: return node, cell else: if p is None: from fealpy.mesh import TriangleMesh return TriangleMesh(node, cell) else: from fealpy.mesh import LagrangeTriangleMesh return LagrangeTriangleMesh(node, cell, p=p, surface=self) elif meshtype == 'quad': node = np.array([ (-1, -1, -1), (-1, -1, 1), (-1, 1, -1), (-1, 1, 1), (1, -1, -1), (1, -1, 1), (1, 1, -1), (1, 1, 1)], dtype=np.float64) cell = np.array([ (0, 1, 4, 5), (6, 7, 2, 3), (2, 3, 0, 1), (4, 5, 6, 7), (1, 3, 5, 7), (2, 0, 6, 4)], dtype=np.int_) node, d = self.project(node) if returnnc: return node, cell else: if p is None: from fealpy.mesh import QuadrangleMesh return QuadrangleMesh(node, cell) else: from fealpy.mesh import LagrangeQuadrangleMesh return LagrangeQuadrangleMesh(node, cell, p=p, surface=self)
pde = PDE() surface = pde.domain() mesh = pde.init_mesh(n=n) errorType = ['$|| u - u_h||_{\Omega,0}$' ] errorMatrix = np.zeros((len(errorType), maxit), dtype=np.float) NDof = np.zeros(maxit, dtype=np.float) for i in range(maxit): print("The {}-th computation:".format(i)) node = mesh.entity('node') cell = mesh.entity('cell') lmesh = LagrangeTriangleMesh(node, cell, p=p, surface=surface) space = IsoLagrangeFiniteElementSpace(lmesh, p=p) NDof[i] = space.number_of_global_dofs() qf = lmesh.integrator(q=p+3, etype='cell') bcs, ws = qf.get_quadrature_points_and_weights() Jh = lmesh.jacobi_matrix(bcs) nh = np.cross(Jh[..., 0], Jh[..., 1], axis=-1) val = np.sqrt(np.sum(nh**2, axis=-1)) nh[...,0] /=val nh[...,1] /=val nh[...,2] /=val bc = lmesh.bc_to_point(bcs) n = pde.normal(bc)
surface = pde.domain() mesh = pde.init_mesh(n=n) errorType = [ '$|| u - u_h||_{\Omega,0}$', '$||\\nabla u - \\nabla u_h||_{\Omega, 0}$', '$|| u - u_I||_{\Omega,0}$', '$||\\nabla u - \\nabla u_I||_{\Omega, 0}$' ] errorMatrix = np.zeros((len(errorType), maxit), dtype=np.float) NDof = np.zeros(maxit, dtype=np.float) for i in range(maxit): print("The {}-th computation:".format(i)) node = mesh.entity('node') cell = mesh.entity('cell') lmesh = LagrangeTriangleMesh(node, cell, p=p, surface=surface) space = IsoLagrangeFiniteElementSpace(lmesh, p=p) NDof[i] = space.number_of_global_dofs() print(NDof[i]) uh = space.function() A = space.stiff_matrix() F = space.source_vector(pde.source) # 封闭曲面,设置其第 0 个插值节点的值为真解值 bc = DirichletBC(space, pde.solution) A, F = bc.apply(A, F, uh, threshold=np.array([0])) uh[:] = spsolve(A, F).reshape(-1) uI = space.interpolation(pde.solution) errorMatrix[0, i] = space.integralalg.error(pde.solution,
pde = PDE() surface = pde.domain() mesh = pde.init_mesh(n=n) errorType = ['$|| u - u_h||_{\Omega,0}$' ] errorMatrix = np.zeros((len(errorType), maxit), dtype=np.float) NDof = np.zeros(maxit, dtype=np.float) for i in range(maxit): print("The {}-th computation:".format(i)) node = mesh.entity('node') cell = mesh.entity('cell') lmesh = LagrangeTriangleMesh(node, cell, p=p, surface=surface) space = IsoLagrangeFiniteElementSpace(lmesh, p=p) NDof[i] = space.number_of_global_dofs() qf = lmesh.integrator(q=p+3, etype='cell') bcs, ws = qf.get_quadrature_points_and_weights() print('bcs', bcs.shape) gphi = lmesh.grad_shape_function(bcs, p=p) #(NQ,NC,ldof,TD) print('gphi', gphi.shape) Jh = lmesh.jacobi_matrix(bcs) print('Jh', Jh.shape) bc = lmesh.bc_to_point(bcs) print('bc', bc.shape) val = np.sum(bc, axis =-1) print('val', val.shape) val1 = (Jh.T/val.T).T
#import scipy.io as sio from fealpy.mesh import LagrangeTriangleMesh, MeshFactory, LagrangeWedgeMesh from fealpy.writer import MeshWriter #p = int(sys.argv[1]) #n = int(sys.argv[2]) #h = sys.argv[3] #nh = int(sys.argv[4]) fname = 'initial/file1.vtu' data = meshio.read(fname) node = data.points cell = data.cells[0][1] mesh = LagrangeTriangleMesh(node*500, cell, p=1) mesh.uniform_refine(n=0) mesh = LagrangeWedgeMesh(mesh, h=0.005, nh=1, p=1) edge = mesh.entity('edge') node = mesh.entity('node') cell = mesh.entity('cell') tface, qface = mesh.entity('face') print(tface.shape) mesh.celldata['a'] = np.arange(len(cell)) mesh.to_vtk(fname='write.vtu')