def test_SurfaceParabolicFEMModel_time(self, maxit=2): from fealpy.pde.surface_parabolic_model_3d import SinSinSinExpDataSphere pde = SinSinSinExpDataSphere() mesh = pde.init_mesh(n=5) timeline = pde.time_mesh(0, 1, 2) errorType = ['$||u-u_h||_0$'] error = np.zeros((len(errorType), maxit), dtype=mesh.ftype) Ndof = np.zeros(maxit, dtype=mesh.itype) for i in range(maxit): print(i) dmodel = SurfaceParabolicFEMModel(pde, mesh) Ndof[i] = dmodel.space.number_of_global_dofs() uh = dmodel.init_solution(timeline) timeline.time_integration(uh, dmodel, self.solver.divide) u = lambda x:dmodel.pde.solution(x, 1.0) uh = dmodel.space.function(array=uh[:, -1]) error[0, i] = dmodel.space.integralalg.L2_error(u, uh) # timeline.uniform_refine() mesh.uniform_refine(surface=pde.surface) show_error_table(Ndof, errorType, error) showmultirate(plt, 0, Ndof, error, errorType) plt.show()
def test_SurfaceParabolicFEMModel_sdc_time(self, maxit=4): from fealpy.pde.surface_parabolic_model_3d import SinSinSinExpDataSphere pde = SinSinSinExpDataSphere() mesh = pde.init_mesh(n=5) timeline = pde.time_mesh(0, 1, 2, timeline='chebyshev') errorType = ['$|| u - u_h ||_\infty$', '$||u-u_h||_0$'] errorMatrix = np.zeros((len(errorType), maxit), dtype=mesh.ftype) Ndof = np.zeros(maxit, dtype=mesh.itype) for i in range(maxit): print(i) dmodel = SurfaceParabolicFEMModel(pde, mesh, p=1) Ndof[i] = dmodel.space.number_of_global_dofs() uh = dmodel.init_solution(timeline) timeline.time_integration(uh, dmodel, self.solver.divide, nupdate=1) uI = dmodel.interpolation(pde.solution, timeline) errorMatrix[0, i] = np.max(np.abs(uI - uh)) u = lambda x:dmodel.pde.solution(x, 1.0) uh = dmodel.space.function(array=uh[:, -1]) errorMatrix[1, i] = dmodel.space.integralalg.L2_error(u, uh) print(errorMatrix) timeline.uniform_refine() mesh.uniform_refine(surface=pde.surface) show_error_table(Ndof, errorType, errorMatrix) showmultirate(plt, 0, Ndof, errorMatrix, errorType) plt.show()
def my_show_error_table(N, errorType, errorMatrix, showTable='No'): if showTable is 'No': f = 'e' pre = 4 sep = ' & ' out = sys.stdout end = '\n' s = 'Dof' + sep + np.array2string(N, separator=sep, ) s = s.replace('\n', '') s = s.replace('[', '') s = s.replace(']', '') print(s, file=out, end=end) n = len(errorType) ff = '%.' + str(pre) + f for i in range(n): first = errorType[i] line = errorMatrix[i] s = first + sep + np.array2string(line, separator=sep, precision=pre, formatter=dict(float=lambda x: ff % x)) s = s.replace('\n', '') s = s.replace('[', '') s = s.replace(']', '') print(s, file=out, end=end) else: show_error_table(N, errorType, errorMatrix)
def test_ParabolicFEMModel_time(self, maxit=4): pde = CosCosExpData(0.1) mesh = pde.init_mesh(n=0) timeline = UniformTimeLine(0, 1, 2) errorType = ['$|| u - u_h ||_\infty$', '$||u-u_h||_0$'] errorMatrix = np.zeros((len(errorType), maxit), dtype=mesh.ftype) Ndof = np.zeros(maxit, dtype=mesh.itype) for i in range(maxit): print(i) model = ParabolicFEMModel(pde, mesh, p=1) Ndof[i] = model.space.number_of_global_dofs() uh = model.init_solution(timeline) timeline.time_integration(uh, model, spsolve) uI = model.interpolation(pde.solution, timeline) errorMatrix[0, i] = np.max(np.abs(uI - uh)) u = lambda x: model.pde.solution(x, 1.0) uh = model.space.function(array=uh[:, -1]) errorMatrix[1, i] = model.space.integralalg.L2_error(u, uh) print(errorMatrix) timeline.uniform_refine() mesh.uniform_refine(surface=pde.surface) show_error_table(Ndof, errorType, errorMatrix) showmultirate(plt, 0, Ndof, errorMatrix, errorType) plt.show()
#h0 /= 2 #mesh = distmesh2d(fd, h0, bbox, pfix, meshtype='polygon') n *= 2 mesh = rectangledomainmesh([-2, 2, -2, 2], nx=n, ny=n, meshtype='polygon') vem.reinit(mesh) data['errorMatrix'] = errorMatrix data['Ndof'] = Ndof fo = 'uh{}.mat'.format(p) sio.matlab.savemat(fo.format(i + 1), data) mesh.add_plot(plt, cellcolor='w') show_error_table(Ndof, errorType, errorMatrix) #fig2 = plt.figure() #fig2.set_facecolor('white') #axes = fig2.gca(projection='3d') #x = mesh.point[:, 0] #y = mesh.point[:, 1] #tri = quadtree.leaf_cell(celltype='tri') #s = axes.plot_trisurf(x, y, tri, vem.uh[:len(x)], cmap=plt.cm.jet, lw=0.0) #fig2.colorbar(s) # #fig3 = plt.figure() #fig3.set_facecolor('white') #axes = fig3.gca(projection='3d') #s = axes.plot_trisurf(x, y, tri, vem.uI-vem.gI, cmap=plt.cm.jet, lw=0.0) #fig3.colorbar(s)
e0, e1, e2, e3, e4 = fem.get_error() eta0 = fem.grad_recover_estimate() eta1 = fem.laplace_recover_estimate(etype=1) e5 = np.sqrt(np.sum(eta0**2)) e6 = np.sqrt(np.sum(eta1**2)) errorMatrix[0, i] = e0 errorMatrix[1, i] = e1 errorMatrix[2, i] = e5 errorMatrix[3, i] = e2 errorMatrix[4, i] = e3 errorMatrix[5, i] = e4 errorMatrix[6, i] = e6 show_error_table(Ndof, errorType, errorMatrix, end='\\\\\\hline\n') showmultirate(plt, 1, Ndof, errorMatrix, errorType) n = 4 if meshtype == 1: #mesh = meshzoo.regular(box, n=n) pass elif meshtype == 2: mesh = meshzoo.rice_mesh(box, n=n) elif meshtype == 3: mesh = meshzoo.cross_mesh(box, n=n) elif meshtype == 4: mesh = meshzoo.fishbone(box, n=n) elif meshtype == 5: mesh = load_mat_mesh('../data/square' + str(2) + '.mat') elif meshtype == 6:
# error analysis errorType = [ '$\| u - \Pi^\\nabla u_h\|_0$', '$\|\\nabla u - \\nabla \Pi^\\nabla u_h\|$' ] errorMatrix = np.zeros((len(errorType), maxit), dtype=np.float) for i in range(maxit - 1): S = vem.project_to_smspace(data[i]) errorMatrix[0, i] = vem.L2_error(S.value) errorMatrix[1, i] = vem.H1_semi_error(S.grad_value) solution['errorMatrix'] = errorMatrix solution['Ndof'] = Ndof f = 'solution.mat' sio.matlab.savemat(f, solution) print(Ndof) print(errorMatrix) show_error_table(Ndof[:-1], errorType, errorMatrix[:, :-1]) fig2 = plt.figure() fig2.set_facecolor('white') axes = fig2.gca(projection='3d') x = qmesh.point[:, 0] y = qmesh.point[:, 1] cell = qmesh.ds.cell tri = np.r_['0', cell[:, [1, 2, 0]], cell[:, [3, 0, 2]]] s = axes.plot_trisurf(x, y, tri, vem.uh[:len(x)], cmap=plt.cm.jet, lw=0.0) fig2.colorbar(s) plt.show()