Ndof = np.zeros((maxit, ), dtype=np.int) errorType = [ '$|| u - u_h||_0$ with p=1', '$|| u - u_h||_0$ with p=2', '$|| u - u_h||_0$ with p=3', '$||\\nabla u - \\nabla u_h||_0$ with p=1', '$||\\nabla u - \\nabla u_h||_0$ with p=2', '$||\\nabla u - \\nabla u_h||_0$ with p=3', ] ps = [1, 2, 3] q = [4, 5, 6] errorMatrix = np.zeros((len(errorType), maxit), dtype=np.float) for i in range(maxit): Ndof[i] = mesh.number_of_nodes() for j, p in enumerate(ps): integrator = mesh.integrator(q[j]) fem = PoissonFEMModel(pde, mesh, p, integrator) fem.solve() Ndof[i] = fem.femspace.number_of_global_dofs() errorMatrix[j, i] = fem.get_L2_error() errorMatrix[j + 3, i] = fem.get_H1_error() if i < maxit - 1: mesh.uniform_refine() print(errorMatrix) showmultirate(plt, 0, Ndof, errorMatrix, errorType) plt.show()
pde = LShapeRSinData() mesh = pde.init_mesh(n=4, meshtype='tri') integrator = mesh.integrator(3) node = mesh.entity('node') cell = mesh.entity('cell') tmesh = Tritree(node, cell) pmesh = tmesh.to_conformmesh() tol = 1.0e-4 for i in range(maxit): fem = PoissonFEMModel(pde, pmesh, p, integrator) fem.solve() res = fem.get_H1_error() #res = fem.get_L2_error() estimator = Estimator(fem.uh[:], mesh, 0.3, 0.5) fig = plt.figure() axes = fig.gca() mesh.add_plot(axes, cellcolor=estimator.eta, showcolorbar=True) if res < tol: break tmesh.adaptive_refine(estimator) pmesh = tmesh.to_conformmesh() print("Steps:", i) mesh = estimator.mesh fig = plt.figure()
maxit = 1 ralg = FEMFunctionRecoveryAlg() Ndof = np.zeros((maxit,), dtype=np.int) errorMatrix = np.zeros((len(errorType), maxit), dtype=np.float) integrator = mesh.integrator(3) for i in range(maxit): print('step:', i) fem = PoissonFEMModel(pde, mesh, 1, integrator) fem.solve() uh = fem.uh Ndof[i] = fem.mesh.number_of_nodes() errorMatrix[0, i] = fem.get_l2_error() errorMatrix[1, i] = fem.get_L2_error() errorMatrix[2, i] = fem.get_H1_error() rguh = ralg.simple_average(uh) eta = fem.recover_estimate(rguh) errorMatrix[3, i] = fem.get_recover_error(rguh) estimator = Estimator(uh[:], mesh, 0.3, 0.5) tmesh.adaptive_refine(estimator) if i < maxit - 1: mesh = estimator.mesh mesh.add_plot(plt, cellcolor='w') fig2 = plt.figure() fig2.set_facecolor('white')
p = int(sys.argv[2]) # 有限元空间的次数 n = int(sys.argv[3]) # 初始网格的加密次数 maxit = int(sys.argv[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) for i in range(maxit): fem = PoissonFEMModel(pde, mesh, p, p + 2) # 创建 Poisson 有限元模型 ls = fem.solve() # 求解 Ndof[i] = fem.space.number_of_global_dofs() # 获得空间自由度个数 errorMatrix[0, i] = fem.get_L2_error() # 计算 L2 误差 errorMatrix[1, i] = fem.get_H1_error() # 计算 H1 误差 if i < maxit - 1: mesh.uniform_refine() # 一致加密网格 # 显示误差 show_error_table(Ndof, errorType, errorMatrix) # 可视化误差收敛阶 showmultirate(plt, 0, Ndof, errorMatrix, errorType) plt.show()