コード例 #1
0
    space = LagrangeFiniteElementSpace(mesh, p)
    A = space.stiff_matrix()
    b = space.source_vector(pde.source)
    
    bc = DirichletBC(space, pde.dirichlet)
    AD, b= bc.apply(A, b)
    
    uh = space.function()
    uh[:] = spsolve(AD, b)
    
    Ndof[i] = space.mesh.number_of_nodes()
    errorMatrix[0, i] = space.integralalg.L2_error(pde.solution, uh)
    errorMatrix[1, i] = space.integralalg.L2_error(pde.gradient, uh.grad_value)
    
    eta = post_error(pde, uh, space, recover=False)
    markedCell = mark(eta,theta=theta,method='MAX')
    if i < maxit - 1:
        markedCell = mark(eta,theta=theta,method='MAX')
        mesh.refine(markedCell)

# 显示误差
show_error_table(Ndof, errorType, errorMatrix)
# 可视化误差收敛阶
showmultirate(plt, 0, Ndof, errorMatrix, errorType)
# 可视化数值解和真解
node = mesh.entity('node')
u = pde.solution(node)
idx = np.argsort(node)
fig = plt.figure()
plt.plot(node[idx], uh[idx],"r*--",linewidth = 2,label = "$u_h$")
plt.plot(node[idx], u[idx],'k-',linewidth = 2,label = "$u$")
コード例 #2
0
    NDof[i] = space.number_of_global_dofs()
    bc = DirichletBC(space, pde.dirichlet)

    uh = space.function()
    A, F = bc.apply(A, F, uh)
    uh[:] = spsolve(A, F)

    errorMatrix[0, i] = space.integralalg.error(pde.solution,
                                                uh.value,
                                                power=2)
    errorMatrix[1, i] = space.integralalg.error(pde.gradient,
                                                uh.grad_value,
                                                power=2)
    errorMatrix[2, i] = space.integralalg.error(pde.gradient,
                                                rguh.value,
                                                power=2)
    eta = space.recovery_estimate(uh)
    errorMatrix[3, i] = np.sqrt(np.sum(eta**2))

    if i < maxit - 1:
        isMarkedCell = mark(eta, theta=theta)
        mesh.bisect(isMarkedCell)
        mesh.add_plot(plt)
        plt.savefig('./test-' + str(i + 1) + '.png')
        plt.close()

mesh.add_plot(plt)
showmultirate(plt, maxit - 5, NDof, errorMatrix, errorType)
plt.show()
コード例 #3
0
ファイル: PoissonAdaptiveVEM.py プロジェクト: liao0415/fealpy
 def refine_marker(self, qtmesh):
     idx = qtmesh.leaf_cell_index()
     markedIdx = mark(self.eta, self.theta)
     return idx[markedIdx]
コード例 #4
0
ファイル: test_parabolic_pde.py プロジェクト: mfkiwl/fealpy
node = mesh.entity('node')
cell = mesh.entity('cell')
print('cell', cell.shape)
bc = mesh.entity_barycenter('cell')
integrator = mesh.integrator(p + 2)
cellmeasure = mesh.entity_measure('cell')

space = LagrangeFiniteElementSpace(mesh, p, spacetype='C')
integralalg = FEMeshIntegralAlg(integrator, mesh)
uI = space.interpolation(lambda x: pde.solution(x, t))
#uI = pde.solution(node,p=node, t=t)
gu = pde.gradient
#uI = space.function()
guI = uI.grad_value
eta = integralalg.L2_error(gu, guI, celltype=True)  # size = (cell.shape[0], 2)
eta = np.sum(eta, axis=-1)
print('eta', eta.shape)

tmesh = TriangleMesh(node, cell)
mark = mark(eta, 0.75)
options = tmesh.adaptive_options(method='mean',
                                 maxrefine=10,
                                 maxcoarsen=0,
                                 theta=0.5)
adaptmesh = tmesh.adaptive(eta, options)
#node = adaptmesh.node
fig = plt.figure()
axes = fig.gca()
tmesh.add_plot(axes)
plt.show()
コード例 #5
0
    def alg_3_4(self, maxit=None):
        """
        1. 最粗网格上求解最小特征特征值问题,得到最小特征值 d_H 和特征向量 u_H
        2. 自适应求解  - \Delta u_h = u_H
            *  u_H 插值到下一层网格上做为新 u_H
        3. 最新网格层上求出的 uh 做为一个基函数,加入到最粗网格的有限元空间中,
           求解最小特征值问题。

        自适应 maxit, picard:2
        """
        print("算法 3.4")
        if maxit is None:
            maxit = self.maxit

        start = timer()
        if self.step == 0:
            idx = []
        else:
            idx = list(range(0, self.maxit, self.step)) + [self.maxit - 1]

        mesh = self.pde.init_mesh(n=self.numrefine)
        integrator = mesh.integrator(self.q)

        # 1. 粗网格上求解最小特征值问题
        space = LagrangeFiniteElementSpace(mesh, 1)
        AH = self.get_stiff_matrix(space)
        MH = self.get_mass_matrix(space)
        isFreeHDof = ~(space.boundary_dof())

        gdof = space.number_of_global_dofs()
        print("initial mesh :", gdof)

        uH = np.zeros(gdof, dtype=np.float)

        A = AH[isFreeHDof, :][:, isFreeHDof].tocsr()
        M = MH[isFreeHDof, :][:, isFreeHDof].tocsr()

        if self.matlab is False:
            uH[isFreeHDof], d = self.eig(A, M)
        else:
            uH[isFreeHDof], d = self.meigs(A, M)

        uh = space.function()
        uh[:] = uH

        GD = mesh.geo_dimension()
        if (self.step > 0) and (0 in idx):
            NN = mesh.number_of_nodes()
            fig = plt.figure()
            fig.set_facecolor('white')
            if GD == 2:
                axes = fig.gca()
            else:
                axes = Axes3D(fig)
            mesh.add_plot(axes, cellcolor='w')
            fig.savefig(self.resultdir + 'mesh_3_4_0_' + str(NN) + '.pdf')
            plt.close()
            self.savemesh(mesh,
                          self.resultdir + 'mesh_3_4_0_' + str(NN) + '.mat')

        # 2. 以 u_H 为右端项自适应求解 -\Deta u = u_H
        I = eye(gdof)
        final = 0
        for i in range(maxit):
            eta = self.residual_estimate(uh)
            markedCell = mark(eta, self.theta)
            IM = mesh.bisect(markedCell, returnim=True)
            print(i + 1, "refine :", mesh.number_of_nodes())
            if (self.step > 0) and (i in idx):
                NN = mesh.number_of_nodes()
                fig = plt.figure()
                fig.set_facecolor('white')
                if GD == 2:
                    axes = fig.gca()
                else:
                    axes = Axes3D(fig)
                mesh.add_plot(axes, cellcolor='w')
                fig.savefig(self.resultdir + 'mesh_3_4_' + str(i + 1) + '_' +
                            str(NN) + '.pdf')
                plt.close()
                self.savemesh(
                    mesh, self.resultdir + 'mesh_3_4_' + str(i + 1) + '_' +
                    str(NN) + '.mat')

            I = IM @ I
            uH = IM @ uH

            space = LagrangeFiniteElementSpace(mesh, 1)
            gdof = space.number_of_global_dofs()

            A = self.get_stiff_matrix(space)
            M = self.get_mass_matrix(space)
            isFreeDof = ~(space.boundary_dof())
            b = M @ uH

            uh = space.function()
            if self.matlab is False:
                uh[isFreeDof] = self.psolve(
                    A[isFreeDof, :][:, isFreeDof].tocsr(), b[isFreeDof],
                    M[isFreeDof, :][:, isFreeDof].tocsr())
            else:
                uh[isFreeDof] = self.msolve(
                    A[isFreeDof, :][:, isFreeDof].tocsr(), b[isFreeDof])
            final = i + 1
            if gdof > self.maxdof:
                break

        if self.step > 0:
            NN = mesh.number_of_nodes()
            fig = plt.figure()
            fig.set_facecolor('white')
            if GD == 2:
                axes = fig.gca()
            else:
                axes = Axes3D(fig)
            mesh.add_plot(axes, cellcolor='w')
            fig.savefig(self.resultdir + 'mesh_3_4_' + str(final) + '_' +
                        str(NN) + '.pdf')
            plt.close()
            self.savemesh(
                mesh, self.resultdir + 'mesh_3_4_' + str(final) + '_' +
                str(NN) + '.mat')

        # 3. 把 uh 加入粗网格空间, 组装刚度和质量矩阵
        w0 = uh @ A
        w1 = w0 @ uh
        w2 = w0 @ I
        AA = bmat([[AH, w2.reshape(-1, 1)], [w2, w1]], format='csr')

        w0 = uh @ M
        w1 = w0 @ uh
        w2 = w0 @ I
        MM = bmat([[MH, w2.reshape(-1, 1)], [w2, w1]], format='csr')

        isFreeDof = np.r_[isFreeHDof, True]

        u = np.zeros(len(isFreeDof))

        ## 求解特征值
        A = AA[isFreeDof, :][:, isFreeDof].tocsr()
        M = MM[isFreeDof, :][:, isFreeDof].tocsr()

        if self.multieigs is True:
            self.A = A
            self.M = M
            self.ml = pyamg.ruge_stuben_solver(self.A)
            self.eigs()
        else:
            if self.matlab is False:
                u[isFreeDof], d = self.eig(A, M)
            else:
                u[isFreeDof], d = self.meigs(A, M)
            print("smallest eigns:", d)
            uh *= u[-1]
            uh += I @ u[:-1]

            uh /= np.max(np.abs(uh))
            uh = space.function(array=uh)
            return uh

        end = timer()
        print("with time: ", end - start)
コード例 #6
0
    #ruh = curl_recover(uh)
    space = LagrangeFiniteElementSpace(mesh, p=1)
    rcuh = space.function(dim=1)
    rcuh[:] = spr(uh, edge_curl_value)  # curl

    ruh = space.function(dim=2)
    ruh[:, 0] = spr(uh, edge_value_0)  #
    ruh[:, 1] = spr(uh, edge_value_1)

    errorMatrix[0, i] = space.integralalg.L2_error(pde.solution, uh)
    errorMatrix[1, i] = space.integralalg.L2_error(pde.curl, uh.curl_value)
    errorMatrix[2, i] = space.integralalg.L2_error(pde.solution, ruh)
    errorMatrix[3, i] = space.integralalg.L2_error(pde.curl, rcuh)

    # 计算单元上的恢复型误差
    eta0 = space.integralalg.error(uh.curl_value, rcuh, power=2,
                                   celltype=True)  # eta_K
    eta1 = space.integralalg.error(uh.value, ruh, power=2,
                                   celltype=True)  # xi_K
    eta = np.sqrt(eta0**2 + eta1**2)  # S_K
    errorMatrix[4, i] = np.sqrt(np.sum(eta**2))  # S_h
    if i < args.maxit - 1:
        isMarkedCell = mark(eta, theta=args.theta)
        mesh.bisect(isMarkedCell)
        mesh.add_plot(plt)
        plt.savefig('./test-' + str(i + 1) + '.eps')
        plt.close()

showmultirate(plt, args.maxit - 10, NDof, errorMatrix, errorType, propsize=20)
plt.show()
コード例 #7
0
    def alg_3_2(self, maxit=None):
        """
        1. 自适应求解 -\Delta u = 1。
        1. 在最细网格上求最小特征值和特征向量。

        refine maxit, picard: 1
        """
        print("算法 3.2")
        if maxit is None:
            maxit = self.maxit

        start = timer()
        if self.step == 0:
            idx = []
        else:
            idx = list(range(0, self.maxit, self.step)) + [self.maxit - 1]

        mesh = self.pde.init_mesh(n=self.numrefine)
        GD = mesh.geo_dimension()
        if (self.step > 0) and (0 in idx):
            NN = mesh.number_of_nodes()
            fig = plt.figure()
            fig.set_facecolor('white')
            if GD == 2:
                axes = fig.gca()
            else:
                axes = Axes3D(fig)
            mesh.add_plot(axes, cellcolor='w')
            fig.savefig(self.resultdir + 'mesh_3_2_0_' + str(NN) + '.pdf')
            plt.close()
            self.savemesh(mesh,
                          self.resultdir + 'mesh_3_2_0_' + str(NN) + '.mat')

        final = 0
        integrator = mesh.integrator(self.q)
        for i in range(maxit):
            space = LagrangeFiniteElementSpace(mesh, 1)
            gdof = space.number_of_global_dofs()

            A = self.get_stiff_matrix(space)
            M = self.get_mass_matrix(space)
            b = M @ np.ones(gdof)

            isFreeDof = ~(space.boundary_dof())
            A = A[isFreeDof, :][:, isFreeDof].tocsr()
            M = M[isFreeDof, :][:, isFreeDof].tocsr()

            uh = space.function()
            if self.matlab is False:
                uh[isFreeDof] = self.psolve(A, b[isFreeDof], M)
            else:
                uh[isFreeDof] = self.msolve(A, b[isFreeDof])

            eta = self.residual_estimate(uh)
            markedCell = mark(eta, self.theta)
            IM = mesh.bisect(markedCell, returnim=True)
            NN = mesh.number_of_nodes()
            print(i + 1, "refine :", NN)
            if (self.step > 0) and (i in idx):
                fig = plt.figure()
                fig.set_facecolor('white')
                if GD == 2:
                    axes = fig.gca()
                else:
                    axes = Axes3D(fig)
                mesh.add_plot(axes, cellcolor='w')
                fig.savefig(self.resultdir + 'mesh_3_2_' + str(i + 1) + '_' +
                            str(NN) + '.pdf')
                plt.close()
                self.savemesh(
                    mesh, self.resultdir + 'mesh_3_2_' + str(i + 1) + '_' +
                    str(NN) + '.mat')
            final = i + 1
            if NN > self.maxdof:
                break

        if self.step > 0:
            NN = mesh.number_of_nodes()
            fig = plt.figure()
            fig.set_facecolor('white')
            if GD == 2:
                axes = fig.gca()
            else:
                axes = Axes3D(fig)
            mesh.add_plot(axes, cellcolor='w')
            fig.savefig(self.resultdir + 'mesh_3_2_' + str(final) + '_' +
                        str(NN) + '.pdf')
            plt.close()
            self.savemesh(
                mesh, self.resultdir + 'mesh_3_2_' + str(final) + '_' +
                str(NN) + '.mat')

        space = LagrangeFiniteElementSpace(mesh, 1)
        gdof = space.number_of_global_dofs()

        A = self.get_stiff_matrix(space)
        M = self.get_mass_matrix(space)
        isFreeDof = ~(space.boundary_dof())
        A = A[isFreeDof, :][:, isFreeDof].tocsr()
        M = M[isFreeDof, :][:, isFreeDof].tocsr()

        if self.multieigs is True:
            self.A = A
            self.M = M
            self.ml = pyamg.ruge_stuben_solver(self.A)
            self.eigs()
        else:
            uh = IM @ uh
            if self.matlab is False:
                uh[isFreeDof], d = self.eig(A, M)
            else:
                uh[isFreeDof], d = self.meigs(A, M)
            print("smallest eigns:", d)
            return space.function(array=uh)
        end = timer()
        print("with time: ", end - start)
コード例 #8
0
    def alg_3_3(self, maxit=None):
        """
        1. 最粗网格上求解最小特征特征值问题,得到最小特征值 d_H 和特征向量 u_H
        2. 自适应求解  - \Delta u_h = u_H
            *  u_H 插值到下一层网格上做为新 u_H
        3. 在最细网格上求解一次最小特征值问题

        自适应 maxit, picard:2
        """
        print("算法 3.3")

        if maxit is None:
            maxit = self.maxit

        start = timer()

        if self.step == 0:
            idx = []
        else:
            idx = list(range(0, self.maxit, self.step))

        mesh = self.pde.init_mesh(n=self.numrefine)
        # 1. 粗网格上求解最小特征值问题
        space = LagrangeFiniteElementSpace(mesh, 1)
        AH = self.get_stiff_matrix(space)
        MH = self.get_mass_matrix(space)
        isFreeHDof = ~(space.boundary_dof())

        gdof = space.number_of_global_dofs()
        uH = np.zeros(gdof, dtype=mesh.ftype)
        print("initial mesh :", gdof)

        A = AH[isFreeHDof, :][:, isFreeHDof].tocsr()
        M = MH[isFreeHDof, :][:, isFreeHDof].tocsr()
        if self.matlab is False:
            uH[isFreeHDof], d = self.eig(A, M)
        else:
            uH[isFreeHDof], d = self.meigs(A, M)

        uh = space.function()
        uh[:] = uH

        GD = mesh.geo_dimension()
        if (self.step > 0) and (0 in idx):
            NN = mesh.number_of_nodes()
            fig = plt.figure()
            fig.set_facecolor('white')
            if GD == 2:
                axes = fig.gca()
            else:
                axes = Axes3D(fig)
            mesh.add_plot(axes, cellcolor='w')
            fig.savefig(self.resultdir + 'mesh_3_3_0_' + str(NN) + '.pdf')
            plt.close()
            self.savemesh(mesh,
                          self.resultdir + 'mesh_3_3_0_' + str(NN) + '.mat')

        # 2. 以 u_H 为右端项自适应求解 -\Deta u = u_H
        I = eye(gdof)
        final = 0
        for i in range(maxit - 1):
            eta = self.residual_estimate(uh)
            markedCell = mark(eta, self.theta)
            IM = mesh.bisect(markedCell, returnim=True)
            NN = mesh.number_of_nodes()
            print(i + 1, "refine : ", NN)
            if (self.step > 0) and (i in idx):
                NN = mesh.number_of_nodes()
                fig = plt.figure()
                fig.set_facecolor('white')
                if GD == 2:
                    axes = fig.gca()
                else:
                    axes = Axes3D(fig)
                mesh.add_plot(axes, cellcolor='w')
                fig.savefig(self.resultdir + 'mesh_3_3_' + str(i + 1) + '_' +
                            str(NN) + '.pdf')
                plt.close()
                self.savemesh(
                    mesh, self.resultdir + 'mesh_3_3_' + str(i + 1) + '_' +
                    str(NN) + '.mat')
            final = i + 1
            if NN > self.maxdof:
                break

            I = IM @ I
            uH = IM @ uH

            space = LagrangeFiniteElementSpace(mesh, 1)
            gdof = space.number_of_global_dofs()

            A = self.get_stiff_matrix(space)
            M = self.get_mass_matrix(space)
            isFreeDof = ~(space.boundary_dof())
            b = M @ uH

            uh = space.function()
            if self.matlab is False:
                uh[isFreeDof] = self.psolve(
                    A[isFreeDof, :][:, isFreeDof].tocsr(), b[isFreeDof],
                    M[isFreeDof, :][:, isFreeDof].tocsr())
            else:
                uh[isFreeDof] = self.msolve(
                    A[isFreeDof, :][:, isFreeDof].tocsr(), b[isFreeDof])

        # 3. 在最细网格上求解一次最小特征值问题

        if self.step > 0:
            NN = mesh.number_of_nodes()
            fig = plt.figure()
            fig.set_facecolor('white')
            if GD == 2:
                axes = fig.gca()
            else:
                axes = Axes3D(fig)
            mesh.add_plot(axes, cellcolor='w')
            fig.savefig(self.resultdir + 'mesh_3_3_' + str(final) + '_' +
                        str(NN) + '.pdf')
            plt.close()
            self.savemesh(
                mesh, self.resultdir + 'mesh_3_3_' + str(final) + '_' +
                str(NN) + '.mat')

        space = LagrangeFiniteElementSpace(mesh, 1)
        gdof = space.number_of_global_dofs()
        A = self.get_stiff_matrix(space)
        M = self.get_mass_matrix(space)
        isFreeDof = ~(space.boundary_dof())
        uh = space.function(array=uh)
        A = A[isFreeDof, :][:, isFreeDof].tocsr()
        M = M[isFreeDof, :][:, isFreeDof].tocsr()
        uh = space.function()

        if self.multieigs is True:
            self.A = A
            self.M = M
            self.ml = pyamg.ruge_stuben_solver(self.A)
            self.eigs()
        else:
            if self.matlab is False:
                uh[isFreeDof], d = self.eig(A, M)
            else:
                uh[isFreeDof], d = self.meigs(A, M)
            print("smallest eigns:", d)
            return uh
        end = timer()
        print("with time: ", end - start)
コード例 #9
0
    def alg_3_1(self, maxit=None):
        """
        1. 自适应在每层网格上求解最小特征值问题

        refine: maxit, picard: maxit + 1
        """
        print("算法 3.1")

        if maxit is None:
            maxit = self.maxit

        start = timer()
        if self.step == 0:
            idx = []
        else:
            idx = list(range(0, self.maxit, self.step)) + [self.maxit - 1]

        mesh = self.pde.init_mesh(n=self.numrefine)
        GD = mesh.geo_dimension()
        if (self.step > 0) and (0 in idx):
            NN = mesh.number_of_nodes()
            fig = plt.figure()
            fig.set_facecolor('white')
            if GD == 2:
                axes = fig.gca()
            else:
                axes = Axes3D(fig)
            mesh.add_plot(axes, cellcolor='w')
            fig.savefig(self.resultdir + 'mesh_3_1_0_' + str(NN) + '.pdf')
            plt.close()
            self.savemesh(mesh,
                          self.resultdir + 'mesh_3_1_0_' + str(NN) + '.mat')

        space = LagrangeFiniteElementSpace(mesh, 1)
        isFreeDof = ~(space.boundary_dof())
        gdof = space.number_of_global_dofs()
        uh = np.ones(gdof, dtype=mesh.ftype)
        uh[~isFreeDof] = 0
        IM = eye(gdof)
        for i in range(maxit + 1):
            area = mesh.entity_measure('cell')
            A = self.get_stiff_matrix(space)
            M = self.get_mass_matrix(space)
            uh = IM @ uh
            A = A[isFreeDof, :][:, isFreeDof].tocsr()
            M = M[isFreeDof, :][:, isFreeDof].tocsr()

            if self.matlab is False:
                uh[isFreeDof], d = self.eig(A, M)
            else:
                uh[isFreeDof], d = self.meigs(A, M)

            if i < maxit:
                uh = space.function(array=uh)
                eta = self.residual_estimate(uh)
                markedCell = mark(eta, self.theta)
                IM = mesh.bisect(markedCell, returnim=True)
                print(i + 1, "refine: ", mesh.number_of_nodes())

                if (self.step > 0) and (i in idx):
                    NN = mesh.number_of_nodes()
                    fig = plt.figure()
                    fig.set_facecolor('white')
                    if GD == 2:
                        axes = fig.gca()
                    else:
                        axes = Axes3D(fig)
                    mesh.add_plot(axes, cellcolor='w')
                    fig.savefig(self.resultdir + 'mesh_3_1_' + str(i + 1) +
                                '_' + str(NN) + '.pdf')
                    plt.close()
                    self.savemesh(
                        mesh, self.resultdir + 'mesh_3_1_' + str(i + 1) + '_' +
                        str(NN) + '.mat')

                space = LagrangeFiniteElementSpace(mesh, 1)
                isFreeDof = ~(space.boundary_dof())
                gdof = space.number_of_global_dofs()
            if gdof > self.maxdof:
                break

        if self.step > 0:
            NN = mesh.number_of_nodes()
            fig = plt.figure()
            fig.set_facecolor('white')
            if GD == 2:
                axes = fig.gca()
            else:
                axes = Axes3D(fig)
            mesh.add_plot(axes, cellcolor='w')
            fig.savefig(self.resultdir + 'mesh_3_1_' + str(i + 1) + '_' +
                        str(NN) + '.pdf')
            plt.close()
            self.savemesh(
                mesh, self.resultdir + 'mesh_3_1_' + str(i + 1) + '_' +
                str(NN) + '.mat')

        if self.multieigs is True:
            self.A = A
            self.M = M
            self.ml = pyamg.ruge_stuben_solver(self.A)
            self.eigs()

        end = timer()
        print("smallest eigns:", d, "with time: ", end - start)

        uh = space.function(array=uh)
        return uh
コード例 #10
0
    def alg_0(self, maxit=None):
        """
        1. 最粗网格上求解最小特征特征值问题,得到最小特征值 d_H 和特征向量 u_H
        2. 自适应求解  - \Delta u_h = d_H*u_H
            *  每层网格上求出的 u_h,插值到下一层网格上做为 u_H
            *  并更新 d_H = u_h@A@u_h/u_h@M@u_h, 其中 A 是当前网格层上的刚度矩
               阵,M 为当前网格层的质量矩阵。

        自适应 maxit, picard: 1
        """
        print("算法 0")
        if maxit is None:
            maxit = self.maxit

        start = timer()
        if self.step == 0:
            idx = []
        else:
            idx = list(range(0, self.maxit, self.step)) + [self.maxit - 1]

        mesh = self.pde.init_mesh(n=self.numrefine)

        # 1. 粗网格上求解最小特征值问题
        space = LagrangeFiniteElementSpace(mesh, 1)
        gdof = space.number_of_global_dofs()
        print("initial mesh:", gdof)
        uh = np.zeros(gdof, dtype=np.float)
        AH = self.get_stiff_matrix(space)
        MH = self.get_mass_matrix(space)
        isFreeHDof = ~(space.boundary_dof())
        A = AH[isFreeHDof, :][:, isFreeHDof].tocsr()
        M = MH[isFreeHDof, :][:, isFreeHDof].tocsr()

        if self.picard is True:
            uh[isFreeHDof], d = picard(A,
                                       M,
                                       np.ones(sum(isFreeHDof)),
                                       sigma=self.sigma)
        else:
            uh[isFreeHDof], d = self.eig(A, M)

        GD = mesh.geo_dimension()
        if (self.step > 0) and (0 in idx):
            NN = mesh.number_of_nodes()
            fig = plt.figure()
            fig.set_facecolor('white')
            if GD == 2:
                axes = fig.gca()
            else:
                axes = Axes3D(fig)
            mesh.add_plot(axes, cellcolor='w')
            fig.savefig(self.resultdir + 'mesh_0_0_' + str(NN) + '.pdf')
            plt.close()
            self.savemesh(mesh,
                          self.resultdir + 'mesh_0_0_' + str(NN) + '.mat')

        # 2. 以 u_h 为右端项自适应求解 -\Deta u = d*u_h
        I = eye(gdof)
        for i in range(maxit):
            uh = space.function(array=uh)
            eta = self.residual_estimate(uh)
            markedCell = mark(eta, self.theta)
            IM = mesh.bisect(markedCell, returnim=True)
            print(i + 1, "refine: ", mesh.number_of_nodes())
            if (self.step > 0) and (i in idx):
                NN = mesh.number_of_nodes()
                fig = plt.figure()
                fig.set_facecolor('white')
                if GD == 2:
                    axes = fig.gca()
                else:
                    axes = Axes3D(fig)
                mesh.add_plot(axes, cellcolor='w')
                fig.savefig(self.resultdir + 'mesh_0_' + str(i + 1) + '_' +
                            str(NN) + '.pdf')
                plt.close()
                self.savemesh(
                    mesh, self.resultdir + 'mesh_0_' + str(i + 1) + '_' +
                    str(NN) + '.mat')

            I = IM @ I
            uh = IM @ uh
            space = LagrangeFiniteElementSpace(mesh, 1)
            gdof = space.number_of_global_dofs()
            A = self.get_stiff_matrix(space)
            M = self.get_mass_matrix(space)
            isFreeDof = ~(space.boundary_dof())
            b = d * M @ uh
            if self.sigma is None:
                ml = pyamg.ruge_stuben_solver(
                    A[isFreeDof, :][:, isFreeDof].tocsr())
                uh[isFreeDof] = ml.solve(b[isFreeDof],
                                         x0=uh[isFreeDof],
                                         tol=1e-12,
                                         accel='cg').reshape((-1, ))
            else:
                K = A[isFreeDof, :][:, isFreeDof].tocsr(
                ) + self.sigma * M[isFreeDof, :][:, isFreeDof].tocsr()
                b += self.sigma * M @ uh
                ml = pyamg.ruge_stuben_solver(K)
                uh[isFreeDof] = ml.solve(b[isFreeDof],
                                         x0=uh[isFreeDof],
                                         tol=1e-12,
                                         accel='cg').reshape(-1)
                # uh[isFreeDof] = spsolve(A[isFreeDof, :][:, isFreeDof].tocsr(), b[isFreeDof])
            d = uh @ A @ uh / (uh @ M @ uh)

            if gdof > self.maxdof:
                break

        if self.multieigs is True:
            self.A = A[isFreeDof, :][:, isFreeDof].tocsr()
            self.M = M[isFreeDof, :][:, isFreeDof].tocsr()
            self.ml = pyamg.ruge_stuben_solver(self.A)
            self.eigs()

        end = timer()
        print("smallest eigns:", d, "with time: ", end - start)

        uh = space.function(array=uh)
        return uh
コード例 #11
0
    #    errorMatrix[4, i] = funNorm.div_error(model.laplace, fem.rgh)
    #    errorMatrix[5, i] = funNorm.L2_error(model.laplace, fem.rlh)
    #    errorMatrix[6, i] = np.sqrt(np.sum(eta2**2))

    errorMatrix[0, i] = funNorm.L2_error(model.solution, fem.uh)
    errorMatrix[1, i] = funNorm.H1_semi_error(model.gradient, fem.uh)
    errorMatrix[2, i] = funNorm.div_error(model.laplace, fem.rgh)
    errorMatrix[3, i] = np.sqrt(np.sum(eta2**2))
    if i in idx:
        fig = plt.figure()
        fig.set_facecolor('white')
        axes = fig.gca()
        mesh.add_plot(axes, cellcolor='w')
        fig.savefig(d + '/mesh' + str(m - 2) + '-' + str(i) + '.pdf')

    markedCell = mark(eta2, theta)
    if i < maxit - 1:
        mesh.bisect(markedCell)
        fem.reinit(mesh)
        funNorm.area = fem.area

fig2 = plt.figure()
fig2.set_facecolor('white')
axes = fig2.gca(projection='3d')
x = mesh.point[:, 0]
y = mesh.point[:, 1]
s = axes.plot_trisurf(x,
                      y,
                      fem.uh,
                      triangles=mesh.ds.cell,
                      cmap=plt.cm.jet,
コード例 #12
0
    fem.recover_grad(uh, rgh)
    fem.recover_laplace(rgh, rlh)

    eta = fem.recover_estimate(uh, rgh)

    Ndof[i] = V.number_of_global_dofs()
    errorMatrix[0, i] = np.sqrt(np.sum(eta**2))

    if i in idx:
        fig = plt.figure()
        fig.set_facecolor('white')
        axes = fig.gca()
        mesh.add_plot(axes, cellcolor='w')
        fig.savefig('mesh' + str(m - 2) + '-' + str(i) + '.pdf')

    markedCell = mark(eta, theta)

    if i < maxit - 1:
        mesh.bisect(markedCell)

fig2 = plt.figure()
fig2.set_facecolor('white')
axes = fig2.gca(projection='3d')
x = mesh.point[:, 0]
y = mesh.point[:, 1]
s = axes.plot_trisurf(x,
                      y,
                      uh,
                      triangles=mesh.ds.cell,
                      cmap=plt.cm.jet,
                      lw=0.,
コード例 #13
0
    errorMatrix[0, i] = e0  # L2
    errorMatrix[1, i] = e1  # H1
    errorMatrix[2, i] = e5  # grad u_h - G(grad u_h)
    errorMatrix[3, i] = e2  # grad u - G(grad u_h)
    errorMatrix[4, i] = e3  # Delta u - div G(grad u_h)
    errorMatrix[5, i] = e4  # Delta u - G(div G(grad u_h))
    errorMatrix[6, i] = e6  # div G(grad u_h) - G(div G(grad u_h))

    if i in idx:
        fig = plt.figure()
        fig.set_facecolor('white')
        axes = fig.gca()
        mesh.add_plot(axes, cellcolor='w')
        fig.savefig(d + '/mesh' + str(m - 2) + '-' + str(i) + '.pdf')

    markedCell = mark(eta1, theta)
    if i < maxit - 1:
        mesh.bisect(markedCell)

fig2 = plt.figure()
fig2.set_facecolor('white')
axes = fig2.gca(projection='3d')
node = mesh.entity('node')
x = mesh.node[:, 0]
y = mesh.node[:, 1]
s = axes.plot_trisurf(x,
                      y,
                      fem.uh,
                      triangles=mesh.ds.cell,
                      cmap=plt.cm.jet,
                      lw=0.0)