Ejemplo n.º 1
0
 def show_basis_test(self):
     h = 0.5
     box = [-h, 1 + h, -h, np.sqrt(3) / 2 + h]
     mesh = self.meshfactory.one_triangle_mesh()
     space = FirstKindNedelecFiniteElementSpace2d(mesh, p=0, q=2)
     fig = plt.figure()
     space.show_basis(fig, box=box)
     plt.show()
    def interpolation(self, n=4, p=0, plot=True):

        box = [-0.5, 1.5, -0.5, 1.5]

        def u(p):
            x = p[..., 0]
            y = p[..., 1]
            val = np.zeros_like(p)
            pi = np.pi
            val[..., 0] = np.sin(pi * x) * np.cos(pi * y)
            val[..., 1] = np.sin(pi * x) * np.cos(pi * y)
            return val

        mesh = self.meshfactory.regular([0, 1, 0, 1], n=n)
        space = FirstKindNedelecFiniteElementSpace2d(mesh, p=p)
        uI = space.interpolation(u)
        error = space.integralalg.L2_error(u, uI)
        print(error)
        if plot:
            fig = plt.figure()
            axes = fig.gca()
            mesh.add_plot(axes, box=box)
            plt.show()
    def solve_time_harmonic_2d(self, n=3, p=0, plot=True):
        pde = CosSinData()
        mesh = pde.init_mesh(n=n, meshtype='tri')
        space = FirstKindNedelecFiniteElementSpace2d(mesh, p=p)

        gdof = space.number_of_global_dofs()
        uh = space.function()

        A = space.curl_matrix() - space.mass_matrix()
        F = space.source_vector(pde.source)

        isBdDof = space.boundary_dof()
        bdIdx = np.zeros(gdof, dtype=np.int)
        bdIdx[isBdDof] = 1
        Tbd = spdiags(bdIdx, 0, gdof, gdof)
        T = spdiags(1 - bdIdx, 0, gdof, gdof)
        A = T @ A @ T + Tbd
        F[isBdDof] = 0
        uh[:] = spsolve(A, F)

        error0 = space.integralalg.L2_error(pde.solution, uh)
        error1 = space.integralalg.L2_error(pde.curl, uh.curl_value)
        print(error0, error1)

        if plot:
            box = [-0.5, 1.5, -0.5, 1.5]
            fig = plt.figure()
            axes = fig.gca()
            mesh.add_plot(axes, box=box)
            #mesh.find_node(axes, showindex=True)
            #mesh.find_edge(axes, showindex=True)
            #mesh.find_cell(axes, showindex=True)
            #node = ps.reshape(-1, 2)
            #uv = phi.reshape(-1, 2)
            #axes.quiver(node[:, 0], node[:, 1], uv[:, 0], uv[:, 1])
            plt.show()
Ejemplo n.º 4
0
    bc = mesh.entity_barycenter('edge')
    v = pde.solution(bc)
    axes.quiver(bc[:, 0], bc[:, 1], v[:, 0], v[:, 1])
    plt.show()

errorType = [
    '$|| u - u_h||_{\Omega,0}$',
    '$||\\nabla\\times u - \\nabla\\times u_h||_{\Omega, 0}$',
    '$|| u - R_h(u_h)||_{\Omega,0}$',
    '$||\\nabla\\times u - R_h(\\nabla\\times u_h)||_{\Omega, 0}$', 'eta'
]
errorMatrix = np.zeros((len(errorType), args.maxit), dtype=np.float)
NDof = np.zeros(args.maxit, dtype=np.float)

for i in range(args.maxit):
    space = FirstKindNedelecFiniteElementSpace2d(mesh, p=args.order)
    bc = DirichletBC(space, pde.dirichlet)

    gdof = space.number_of_global_dofs()
    NDof[i] = gdof

    uh = space.function()
    A = space.curl_matrix() - space.mass_matrix()
    F = space.source_vector(pde.source)

    A, F = bc.apply(A, F, uh)

    uh[:] = spsolve(A, F)

    #ruh = curl_recover(uh)
    space = LagrangeFiniteElementSpace(mesh, p=1)
Ejemplo n.º 5
0
##############################################

errorType = [
    '$|| u - u_h||_{\Omega,0}$',
    '$||\\nabla\\times u - \\nabla\\times u_h||_{\Omega, 0}$',
    '$|| u - u_I||_{\Omega,0}$',
    '$||\\nabla\\times u - \\nabla\\times u_I||_{\Omega, 0}$',
]
errorMatrix = np.zeros((4, maxit), dtype=np.float)
NDof = np.zeros(maxit, dtype=np.float)

lNDof = np.zeros(maxit, dtype=np.float)
#the array to store "number of dof" for every matrit[i]

for i in range(maxit):
    space = FirstKindNedelecFiniteElementSpace2d(mesh, p=p)  #Nedelec space

    gdof = space.number_of_global_dofs()

    NDof[i] = gdof

    uh = space.function()

    uI = space.interpolation(pde.solution)

    A = space.curl_matrix() - space.mass_matrix()  # original stiffness matrix
    F = space.source_vector(pde.source)  #original right-hand

    isDDof = space.set_dirichlet_bc(uh, pde.solution)
    isDDof = np.tile(isDDof, 2)
    F = F.T.flat
degree = args.degree
nrefine = args.nrefine
maxit = args.maxit

pde = CosSinData()
mesh = pde.init_mesh(n=nrefine)

errorType = [
    '$|| u - u_h||_{\Omega,0}$',
    '$||\\nabla\\times u - \\nabla\\times u_h||_{\Omega, 0}$'
]
errorMatrix = np.zeros((len(errorType), maxit), dtype=np.float)
NDof = np.zeros(maxit, dtype=np.float)

for i in range(args.maxit):
    space = FirstKindNedelecFiniteElementSpace2d(mesh, p=degree, q=7)
    bc = DirichletBC(space, pde.dirichlet)

    gdof = space.number_of_global_dofs()
    NDof[i] = gdof
    uh = space.function()
    A = space.curl_matrix() - space.mass_matrix()
    F = space.source_vector(pde.source)

    A, F = bc.apply(A, F, uh)

    uh[:] = spsolve(A, F)
    errorMatrix[0, i] = space.integralalg.L2_error(pde.solution, uh)
    errorMatrix[1, i] = space.integralalg.L2_error(pde.curl, uh.curl_value)
    mesh.uniform_refine()