def test_poisson_fem_2d(): degree = 1 dim = 2 nrefine = 4 maxit = 4 from fealpy.pde.poisson_2d import CosCosData as PDE pde = PDE() mesh = pde.init_mesh(n=nrefine) errorMatrix = np.zeros((2, maxit), dtype=np.float64) NDof = np.zeros(maxit, dtype=np.int64) for i in range(maxit): space = LagrangeFiniteElementSpace(mesh, p=degree) NDof[i] = space.number_of_global_dofs() bc = DirichletBC(space, pde.dirichlet) uh = space.function() A = space.stiff_matrix() F = space.source_vector(pde.source) A, F = bc.apply(A, F, uh) uh[:] = spsolve(A, F).reshape(-1) errorMatrix[0, i] = space.integralalg.L2_error(pde.solution, uh) errorMatrix[1, i] = space.integralalg.L2_error(pde.gradient, uh.grad_value) if i < maxit-1: mesh.uniform_refine()
def test_poisson(): p = 1 # 有限元空间次数, 可以增大 p, 看输出结果的变化 n = 4 # 初始网格加密次数 maxit = 4 # 最大迭代次数 pde = PDE() mesh = pde.init_mesh(n=n) errorMatrix = np.zeros((2, maxit), dtype=np.float) NDof = np.zeros(maxit, dtype=np.float) for i in range(maxit): space = LagrangeFiniteElementSpace(mesh, p=p) # 建立有限元空间 NDof[i] = space.number_of_global_dofs() # 有限元空间自由度的个数 bc = DirichletBC(space, pde.dirichlet) # DirichletBC 条件 uh = space.function() # 有限元函数 A = space.stiff_matrix() # 刚度矩阵 F = space.source_vector(pde.source) # 载荷向量 A, F = bc.apply(A, F, uh) # 处理边界条件 uh[:] = spsolve(A, F).reshape(-1) # 稀疏矩阵直接解法器 # ml = pyamg.ruge_stuben_solver(A) # 代数多重网格解法器 # uh[:] = ml.solve(F, tol=1e-12, accel='cg').reshape(-1) errorMatrix[0, i] = space.integralalg.L2_error( pde.solution, uh ) # 计算 L2 误差 errorMatrix[1, i] = space.integralalg.L2_error( pde.gradient, uh.grad_value ) # 计算 H1 误差 if i < maxit - 1: mesh.uniform_refine() # 一致加密网格 assert (errorMatrix < 1.0).all()
from fealpy.functionspace import LagrangeFiniteElementSpace from fealpy.boundarycondition import RobinBC from fealpy.tools.show import showmultirate p = int(sys.argv[1]) n = int(sys.argv[2]) maxit = int(sys.argv[3]) d = int(sys.argv[4]) if d == 2: from fealpy.pde.poisson_2d import CosCosData as PDE elif d == 3: from fealpy.pde.poisson_3d import CosCosCosData as PDE pde = PDE() mesh = pde.init_mesh(n=n) errorType = [ '$|| u - u_h||_{\Omega,0}$', '$||\\nabla u - \\nabla u_h||_{\Omega, 0}$' ] errorMatrix = np.zeros((2, maxit), dtype=np.float) NDof = np.zeros(maxit, dtype=np.float) for i in range(maxit): space = LagrangeFiniteElementSpace(mesh, p=p) NDof[i] = space.number_of_global_dofs() uh = space.function() A = space.stiff_matrix()
from fealpy.pde.poisson_2d import CosCosData as PDE import numpy as np from ShowCls import ShowCls from PoissonDGModel2d import PoissonDGModel2d from fealpy.mesh import MeshFactory as mf from fealpy.mesh import HalfEdgeMesh2d # from fealpy.mesh.mesh_tools import find_entity # import matplotlib.pyplot as plt # --- begin setting --- # d = 2 # the dimension p = 1 # the polynomial order n = 1 # the number of refine mesh maxit = 5 # the max iteration of the mesh pde = PDE() # create pde model pde.epsilon = -1 # setting the DG-scheme parameter # # epsilon may take -1, 0, 1, # # the corresponding DG-scheme is called symmetric interior penalty Galerkin (SIPG), # # incomplete interior penalty Galerkin (IIPG) and nonsymmetric interior penalty Galerkin (NIPG) pde.eta = 16 # setting the penalty parameter # # To get the optimal rate, one should choose the appropriate 'eta' # # according to the polynomial order 'p', 'epsilon' and mesh. # # error settings errorType = ['$|| u - u_h||_0$', '$||\\nabla u - \\nabla u_h||_0$'] errorMatrix = np.zeros((len(errorType), maxit), dtype=np.float) Ndof = np.zeros(maxit, dtype=np.int) # the array to store the number of dofs
#!/usr/bin/env python3 # import sys import numpy as np import matplotlib.pyplot as plt import scipy.io as sio # 导入 Poisson 有限元模型 from fealpy.fem.PoissonQBFEMModel import PoissonQBFEMModel from fealpy.tools.show import showmultirate, show_error_table from fealpy.pde.poisson_2d import CosCosData as PDE n = 3 maxit = 4 pde = PDE() # 创建 pde 模型 # 误差类型与误差存储数组 errorType = ['$|| u - u_h||_0$', '$||\\nabla u - \\nabla u_h||_0$'] errorMatrix = np.zeros((len(errorType), maxit), dtype=np.float) # 自由度数组 Ndof = np.zeros(maxit, dtype=np.int) # 创建初始网格对象 mesh = pde.init_mesh(n, meshtype='quad') for i in range(maxit): fem = PoissonQBFEMModel(pde, mesh, q=3) # 创建 Poisson 有限元模型 ls = fem.solve() # 求解 Ndof[i] = fem.space.number_of_global_dofs() # 获得空间自由度个数 errorMatrix[0, i] = fem.L2_error() # 计算 L2 误差