def test_poisson_equation(self, p=1, maxit=4):
        h = 0.1
        pde = CosCosData()
        domain = pde.domain()
        error = np.zeros((maxit,), dtype=np.float)
        for i in range(maxit):
            mesh = triangle(domain, h, meshtype='polygon')
            dmodel = SobolevEquationWGModel2d(self.pde, mesh, p=p)
            uh = dmodel.space.function()
            dmodel.space.set_dirichlet_bc(uh, pde.dirichlet)

            S = dmodel.space.stabilizer_matrix()
            A = dmodel.G + S
            F = dmodel.space.source_vector(pde.source)
            F -= A@uh

            isBdDof = dmodel.space.boundary_dof()
            gdof = dmodel.space.number_of_global_dofs()
            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] = uh[isBdDof]
            uh[:] = spsolve(A, F)
            integralalg = dmodel.space.integralalg
            error[i] = integralalg.L2_error(pde.solution, uh)
            h /= 2

        print(error)
        print(error[0:-1]/error[1:])
    def test_sobolev_equation(self, p=1, maxit=5):
        mu = 1
        epsilon = 0.1
        pde = PolyExpData(mu, epsilon)
        domain = pde.domain()
        h = 0.25
        timeline = pde.time_mesh(0, 0.1, 160)
        errorType = ['$|| u - u_{h,i}||_{0}$',
             '$|| p - p_{h, i}||_{0}$',
             '$||\\nabla u - \\nabla_w u_h||_{0}$',
             '$||\\nabla\cdot p - \\nabla_w\cdot p_h||_{0}$',
             '$||\\nabla u - \\nabla u_{h, i}||_{0}',
             '$||\\nabla\cdot p - \\nabla\cdot p_{h, i}||_{0}']
        error = np.zeros((len(errorType), maxit), dtype=np.float)
        Ndof = np.zeros(maxit, dtype=np.int)
        for i in range(maxit):
            print(i)
            mesh = triangle(domain, h=h, meshtype='polygon')
            dt = timeline.current_time_step_length()
            dmodel = SobolevEquationWGModel2d(pde, mesh, p, q=6, dt=dt, step=50)

            uh = dmodel.space.project(lambda x:pde.solution(x, 0.0))
            solver = self.solver.divide

            data = [uh, 0, solver, error[:, i]]

            timeline.time_integration(data, dmodel, solver)

            Ndof[i] = dmodel.space.number_of_global_dofs()
            h /= 2
            timeline.uniform_refine(n=2)

        error = np.sqrt(error)
        show_error_table(Ndof, errorType, error)
    def verify_matrix(self, u, p=2, mtype=0, plot=True):
        from fealpy.mesh.simple_mesh_generator import triangle
        if mtype == 0:
            node = np.array([(-1, -1), (1, -1), (1, 1), (-1, 1)],
                            dtype=np.float)
            cell = np.array([0, 1, 2, 3], dtype=np.int)
            cellLocation = np.array([0, 4], dtype=np.int)
            mesh = PolygonMesh(node, cell, cellLocation)
        elif mtype == 1:
            node = np.array([(-1, -1), (1, -1), (1, 1), (-1, 1)],
                            dtype=np.float)
            cell = np.array([0, 1, 2, 3, 0, 2], dtype=np.int)
            cellLocation = np.array([0, 3, 6], dtype=np.int)
            mesh = PolygonMesh(node, cell, cellLocation)
        elif mtype == 2:
            h = 0.1
            mesh = triangle([-1, 1, -1, 1], h, meshtype='polygon')
        elif mtype == 3:
            node = np.array([(-1, -1), (1, -1), (1, 1), (-1, 1)],
                            dtype=np.float)
            cell = np.array([[0, 1, 2, 3]], dtype=np.int)
            mesh = QuadrangleMesh(node, cell)
            mesh.uniform_refine()
            mesh = PolygonMesh.from_mesh(mesh)

        uspace = ReducedDivFreeNonConformingVirtualElementSpace2d(mesh, p)
        uspace.verify_matrix()
        up = uspace.project(u)
        up = uspace.project_to_smspace(up)
    def project_test(self, u, p=2, mtype=0, plot=True):
        from fealpy.mesh.simple_mesh_generator import triangle
        if mtype == 0:
            node = np.array([(-1, -1), (1, -1), (1, 1), (-1, 1)],
                            dtype=np.float)
            cell = np.array([0, 1, 2, 3], dtype=np.int)
            cellLocation = np.array([0, 4], dtype=np.int)
            mesh = PolygonMesh(node, cell, cellLocation)
        elif mtype == 1:
            node = np.array([(-1, -1), (1, -1), (1, 1), (-1, 1)],
                            dtype=np.float)
            cell = np.array([0, 1, 2, 3, 0, 2], dtype=np.int)
            cellLocation = np.array([0, 3, 6], dtype=np.int)
            mesh = PolygonMesh(node, cell, cellLocation)
        elif mtype == 2:
            h = 0.025
            mesh = triangle([-1, 1, -1, 1], h, meshtype='polygon')
        elif mtype == 3:
            node = np.array([(-1, -1), (1, -1), (1, 1), (-1, 1)],
                            dtype=np.float)
            cell = np.array([[0, 1, 2, 3]], dtype=np.int)
            mesh = QuadrangleMesh(node, cell)
            mesh.uniform_refine()
            mesh = PolygonMesh.from_mesh(mesh)
        elif mtype == 4:
            node = np.array([(-1, -1), (1, -1), (1, 0), (1, 1), (-1, 1),
                             (-1, 0)],
                            dtype=np.float)
            cell = np.array([0, 1, 2, 5, 2, 3, 4, 5], dtype=np.int)
            cellLocation = np.array([0, 4, 8], dtype=np.int)
            mesh = PolygonMesh(node, cell, cellLocation)

        if True:
            cell, cellLocation = mesh.entity('cell')
            edge = mesh.entity('edge')
            cell2edge = mesh.ds.cell_to_edge()
            bc = mesh.entity_barycenter('edge')
            uspace = ReducedDivFreeNonConformingVirtualElementSpace2d(mesh, p)
            up = uspace.project(u)
            up = uspace.project_to_smspace(up)
            print(up)

            integralalg = uspace.integralalg
            error = integralalg.L2_error(u, up)
            print(error)

            A = uspace.matrix_A()
            P = uspace.matrix_P()

            sio.savemat('A.mat', {"A": A.toarray(), "P": P.toarray()})

        if plot:
            mesh.print()
            fig = plt.figure()
            axes = fig.gca()
            mesh.add_plot(axes)
            mesh.find_node(axes, showindex=True)
            mesh.find_edge(axes, showindex=True)
            mesh.find_cell(axes, showindex=True)
            plt.show()
Example #5
0
    def project_test(self, u, p=2, mtype=0, plot=True):
        from fealpy.mesh.simple_mesh_generator import triangle

        if mtype == 0:
            node = np.array([(-1, -1), (1, -1), (1, 1), (-1, 1)],
                            dtype=np.float)
            cell = np.array([0, 1, 2, 3], dtype=np.int)
            cellLocation = np.array([0, 4], dtype=np.int)
            mesh = PolygonMesh(node, cell, cellLocation)
        elif mtype == 1:
            node = np.array([(-1, -1), (1, -1), (1, 1), (-1, 1)],
                            dtype=np.float)
            cell = np.array([0, 1, 2, 3, 0, 2], dtype=np.int)
            cellLocation = np.array([0, 3, 6], dtype=np.int)
            mesh = PolygonMesh(node, cell, cellLocation)
        elif mtype == 2:
            h = 0.1
            mesh = triangle([-1, 1, -1, 1], h, meshtype='polygon')
        elif mtype == 3:
            node = np.array([(-1, -1), (1, -1), (1, 1), (-1, 1)],
                            dtype=np.float)
            cell = np.array([[0, 1, 2, 3]], dtype=np.int)
            mesh = QuadrangleMesh(node, cell)
            mesh.uniform_refine()
            mesh = PolygonMesh.from_mesh(mesh)

        cell, cellLocation = mesh.entity('cell')
        edge = mesh.entity('edge')
        cell2edge = mesh.ds.cell_to_edge()
        uspace = DivFreeNonConformingVirtualElementSpace2d(mesh, p)
        up = uspace.project(u)
        print(up)
        up = uspace.project_to_smspace(up)
        print(up)

        integralalg = uspace.integralalg
        error = integralalg.L2_error(u, up)
        print(error)
        if plot:
            fig = plt.figure()
            axes = fig.gca()
            mesh.add_plot(axes)
            mesh.find_node(axes, showindex=True)
            mesh.find_edge(axes, showindex=True)
            mesh.find_cell(axes, showindex=True)
            plt.show()
Example #6
0
    def test_ParabolicFEMModel_time(self, maxit=4):
        from fealpy.pde.parabolic_model_2d import SinSinExpData
        pde = SinSinExpData()
        domain = pde.domain()
        mesh = triangle(domain, h=0.01)
        timeline = pde.time_mesh(0, 1, 2)
        error = np.zeros(maxit, dtype=mesh.ftype)
        for i in range(maxit):
            print(i)
            dmodel = ParabolicFEMModel(pde, mesh)
            uh = dmodel.init_solution(timeline)

            timeline.time_integration(uh, dmodel, self.solver.divide)

            uI = dmodel.interpolation(timeline)

            error[i] = np.max(np.abs(uh - uI))

            timeline.uniform_refine()
            mesh.uniform_refine()

        print(error[:-1]/error[1:])
        print(error)
Example #7
0
    def test_sobolev_equation(self, p=1, maxit=4):
        pde = self.pde
        domain = pde.domain()
        timeline = pde.time_mesh(0, 1, 20)
        error = np.zeros(maxit, dtype=np.float)
        h = 0.1
        for i in range(maxit):
            print(i)
            mesh = triangle(domain, h=h, meshtype='polygon')
            dmodel = SobolevEquationWGModel2d(pde, mesh, p)
            uh = dmodel.init_solution(timeline)
            up = dmodel.projection(pde.solution, timeline)
            fp = dmodel.projection(pde.source, timeline)
            uh[:, 0] = up[:, 0]
            solver = self.solver.divide
            data = [uh, fp, solver]
            timeline.time_integration(data, dmodel, solver)
            error[i] = np.max(np.abs(uh - up))

            timeline.uniform_refine()
            h /= 2

        print(error[:-1]/error[1:])
        print(error)
 def delaunay_mesh(self, box, n=10):
     h = (box[1] - box[0]) / n
     return triangle(box, h, meshtype='tri')
Example #9
0
    cd = np.hsplit(cell, cellLocation[1:-1])
    poly3d = [[(point[i, 0], point[i, 1], uh[i]) for i in idx] for idx in cd]
    collection = Poly3DCollection(poly3d, linewidths=1, alpha=0.2)
    axes.add_collection3d(collection)
    p0 = [[(point[i, 0], point[i, 1], -1) for i in idx] for idx in cd]
    c0 = Poly3DCollection(p0, linewidths=1, alpha=0.2)
    axes.add_collection3d(c0)


m = int(sys.argv[1])
maxit = int(sys.argv[2])

pde = CosCosData()
h = 0.2
box = [0, 1, 0, 1]  # [0, 1]^2 domain
mesh = triangle(box, h, meshtype='polygon')

Ndof = np.zeros((maxit, ), dtype=np.int)
errorType = [
    '$|| u - \Pi^\\nabla u_h||_0$ with p=1',
    '$|| u - \Pi^\\nabla u_h||_0$ with p=2',
    '$|| u - \Pi^\\nabla u_h||_0$ with p=5',
    '$||\\nabla u - \\nabla \Pi^\\nabla u_h||_0$ with p=1',
    '$||\\nabla u - \\nabla \Pi^\\nabla u_h||_0$ with p=2',
    '$||\\nabla u - \\nabla \Pi^\\nabla u_h||_0$ with p=5',
]

ps = [1, 2, 5]

errorMatrix = np.zeros((len(errorType), maxit), dtype=np.float)
integrator = mesh.integrator(7)
Example #10
0
print("A:", A.toarray())
print("A0:", A0.toarray())
print("S0:", S0.toarray())

np.savetxt('A.txt', A.toarray(), fmt='%.2e')
np.savetxt('A0.txt', A0.toarray(), fmt='%.2e')
np.savetxt('S0.txt', S0.toarray(), fmt='%.2e')

if False:
    h = 0.1
    maxit = 1
    pde = CosCosData()
    box = pde.domain()

    for i in range(maxit):
        mesh = triangle(box, h / 2**(i - 1), meshtype='polygon')
        space = ConformingVirtualElementSpace2d(mesh, p=1)
        uh = space.function()
        bc = BoundaryCondition(space, dirichlet=pde.dirichlet)

        A0, S0 = space.chen_stability_term()
        A0 = A0.toarray()
        S0 = S0.toarray()

        A = space.stiff_matrix()
        F = space.source_vector(pde.source)

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

        uh[:] = spsolve(A, F)
        sh = space.project_to_smspace(uh)
Example #11
0
elif m == 5:
    model = BiharmonicData5()
    box = [-1, 1, -1, 1]

maxit = 4
degree = 1
error = np.zeros((maxit, ), dtype=np.float)
derror = np.zeros((maxit, ), dtype=np.float)
gerror = np.zeros((maxit, ), dtype=np.float)
H1Serror = np.zeros((maxit, ), dtype=np.float)
Ndof = np.zeros((maxit, ), dtype=np.int)

h0 = 0.025
data = sio.loadmat('solution.mat')
for i in range(maxit):
    mesh = triangle(box, h0 / 2**i)
    V = function_space(mesh, 'Lagrange', degree)
    V2 = function_space(mesh, 'Lagrange_2', degree)
    uh = FiniteElementFunction(V)
    ruh = FiniteElementFunction(V2)

    uh[:] = data['uh' + str(i)].reshape(-1)

    fem = BiharmonicRecoveryFEM(V, model, sigma=sigma, rtype='inv_area')
    fem.recover_grad(uh, ruh)

    Ndof[i] = V.number_of_global_dofs()
    error[i] = L2_error(model.solution, uh, order=4)
    derror[i] = div_error(model.laplace, ruh, order=4)
    gerror[i] = L2_error(model.gradient, ruh, order=5)
    H1Serror[i] = H1_semi_error(model.gradient, uh, order=5)
Example #12
0
    return mesh


if m == 1:
    model = SFContactProblemData()

integrator = TriangleQuadrature(4)

if mtype == 1:
    pmesh = load_mesh('nonconvexpmesh1.mat')
    pmesh.point += 2.0
    pmesh.point /= 4.0
else:
    h0 = 0.2
    #pmesh = distmesh2d(fd, h0, bbox, pfix, meshtype='polygon')
    pmesh = triangle([0, 1, 0, 1], h0, meshtype='polygon')
    #n = 4
    #pmesh = rectangledomainmesh([0, 1, 0, 1], nx=n, ny=n, meshtype='polygon')

Ndof = np.zeros((maxit, ), dtype=np.int)
vem = SFContactVEMModel2d(model, pmesh, p=1, integrator=integrator)
solution = {}
for i in range(maxit):
    print('step:', i)
    vem.solve(rho=0.1, maxit=40000)
    Ndof[i] = vem.vemspace.number_of_global_dofs()

    NC = pmesh.number_of_cells()
    cell = np.zeros(NC, dtype=np.object)
    cell[:] = np.vsplit(
        pmesh.ds.cell.reshape(-1, 1) + 1, pmesh.ds.cellLocation[1:-1])
Example #13
0
from fealpy.quadrature import GaussLegendreQuadrature


nu = 1
epsilon = 0.1
pde = SinSinExpData(nu, epsilon)
domain = pde.domain()
tmesh = pde.init_mesh(2, meshtype='tri')


p = 2
h = 0.1
maxit = 4
error = np.zeros((6, maxit), dtype=np.float)
for i in range(4):
    mesh = triangle(domain, h, meshtype='polygon')
    #mesh = PolygonMesh.from_mesh(tmesh)
    space = WeakGalerkinSpace2d(mesh, p=p)

    uh = space.projection(pde.init_value)
    ph = space.weak_grad(uh)

    gh = space.projection(lambda x:pde.gradient(x, 0.0), dim=2)
    dh = space.weak_div(gh)

    error[0, i] = space.integralalg.L2_error(pde.init_value, uh)
    error[1, i] = space.integralalg.L2_error(lambda x: pde.gradient(x, 0.0), ph)
    error[2, i] = space.integralalg.L2_error(lambda x: pde.gradient(x, 0.0), gh)
    error[3, i] = space.integralalg.edge_L2_error(lambda x: pde.solution(x, 0.0), uh)
    error[4, i] = space.integralalg.edge_L2_error(lambda x: pde.gradient(x, 0.0), gh)
    error[5, i] = space.integralalg.L2_error(lambda x: pde.laplace(x, 0.0), dh)
    def stokes_equation_test(self, p=2, maxit=4):
        from scipy.sparse import bmat
        from fealpy.pde.Stokes_Model_2d import CosSinData
        from fealpy.mesh.simple_mesh_generator import triangle
        h = 0.1
        pde = CosSinData()
        domain = pde.domain()
        error = np.zeros((maxit, ), dtype=np.float)
        for i in range(maxit):
            mesh = triangle(domain, h, meshtype='polygon')

            if 0:
                fig = plt.figure()
                axes = fig.gca()
                mesh.add_plot(axes)
                plt.show()

            uspace = DivFreeNonConformingVirtualElementSpace2d(mesh, p)
            pspace = ScaledMonomialSpace2d(mesh, p - 1)

            isBdDof = uspace.boundary_dof()

            udof = uspace.number_of_global_dofs()
            pdof = pspace.number_of_global_dofs()

            uh = uspace.function()
            ph = pspace.function()
            uspace.set_dirichlet_bc(uh, pde.dirichlet)

            A = uspace.matrix_A()
            P = uspace.matrix_P()
            F = uspace.source_vector(pde.source)

            AA = bmat([[A, P.T], [P, None]], format='csr')
            FF = np.block([F, np.zeros(pdof, dtype=uspace.ftype)])
            x = np.block([uh.T.flat, ph])
            isBdDof = np.block(
                [isBdDof, isBdDof,
                 np.zeros(pdof, dtype=np.bool)])

            gdof = 2 * udof + pdof
            FF -= AA @ x
            bdIdx = np.zeros(gdof, dtype=np.int)
            bdIdx[isBdDof] = 1
            Tbd = spdiags(bdIdx, 0, gdof, gdof)
            T = spdiags(1 - bdIdx, 0, gdof, gdof)
            AA = T @ AA @ T + Tbd
            FF[isBdDof] = x[isBdDof]
            x[:] = spsolve(AA, FF)
            uh[:, 0] = x[:udof]
            uh[:, 1] = x[udof:2 * udof]
            ph[:] = x[2 * udof:]

            up = uspace.project(pde.velocity)
            up = uspace.project_to_smspace(up)
            integralalg = uspace.integralalg
            error[i] = integralalg.L2_error(pde.velocity, up)
            h /= 2

        print(error)
        print(error[0:-1] / error[1:])
Example #15
0
    model = BihamonicData2(1.0, 1.0)
elif m == 3:
    model = BihamonicData4()

box = [0, 1, 0, 1]
h0 = 0.02
#mesh = triangle(box, h0)
maxit = 4
degree = 1
error = np.zeros((maxit, ), dtype=np.float)
derror = np.zeros((maxit, ), dtype=np.float)
gerror = np.zeros((maxit, ), dtype=np.float)
Ndof = np.zeros((maxit, ), dtype=np.int)

for i in range(maxit):
    mesh = triangle(box, h0 / (2**i))
    #mesh = unitsquaredomainmesh(h0/2**i)
    V = function_space(mesh, 'Lagrange', degree)
    Ndof[i] = V.number_of_global_dofs()
    uh = FiniteElementFunction(V)

    a = BihamonicRecoveryForm(V, sigma=sigma)
    L = SourceForm(V, model.source, 4)

    bc0 = DirichletBC(V, model.dirichlet, model.is_boundary_dof)

    bc1 = BihamonicRecoveryBC1(V, model.neuman, sigma=sigma)

    solve(a, L, uh, dirichlet=bc0, neuman=bc1, solver='direct')
    error[i] = L2_error(model.solution, uh, order=4)
    ruh = recover_grad(uh)