Exemplo n.º 1
0
    def poisson_fem_2d_neuman_test(self, p=1):
        pde = CosCosData()
        mesh = pde.init_mesh(n=2)
        for i in range(4):
            space = LagrangeFiniteElementSpace(mesh, p=p)
            A = space.stiff_matrix()
            b = space.source_vector(pde.source)
            uh = space.function()
            bc = BoundaryCondition(space, neuman=pde.neuman)
            bc.apply_neuman_bc(b)
            c = space.integral_basis()
            AD = bmat([[A, c.reshape(-1, 1)], [c, None]], format='csr')
            bb = np.r_[b, 0]
            x = spsolve(AD, bb)
            uh[:] = x[:-1]

            area = np.sum(space.integralalg.cellmeasure)
            ubar = space.integralalg.integral(pde.solution,
                                              barycenter=False) / area

            def solution(p):
                return pde.solution(p) - ubar

            error = space.integralalg.L2_error(solution, uh)
            print(error)
            mesh.uniform_refine()
Exemplo n.º 2
0
 def poisson_test(self, p=2):
     pde = CosCosCosData()
     mesh = self.plane_pmesh(n=4)
     space = PrismFiniteElementSpace(mesh, p=p)
     A = space.stiff_matrix()
     b = space.source_vector(pde.source)
     bc = BoundaryCondition(space, dirichlet=pde.dirichlet)
     uh = space.function()
     A, b = bc.apply_dirichlet_bc(A, b, uh)
     uh[:] = spsolve(A, b).reshape(-1)
     error = space.integralalg.L2_error(pde.solution, uh)
     print(error)
Exemplo n.º 3
0
 def poisson_fem_2d_test(self, p=1):
     pde = CosCosData()
     mesh = pde.init_mesh(n=3)
     for i in range(4):
         space = LagrangeFiniteElementSpace(mesh, p=p)
         A = space.stiff_matrix()
         b = space.source_vector(pde.source)
         uh = space.function()
         bc = BoundaryCondition(space, dirichlet=pde.dirichlet)
         A, b = bc.apply_dirichlet_bc(A, b, uh)
         uh[:] = spsolve(A, b).reshape(-1)
         error = space.integralalg.L2_error(pde.solution, uh)
         print(error)
         mesh.uniform_refine()
Exemplo n.º 4
0
    def solve_poisson_robin(self, p=1, n=1, plot=True):

        pde = CosCosCosData()
        mesh = pde.init_mesh(n=n)

        space = LagrangeFiniteElementSpace(mesh, p=p)

        A = space.stiff_matrix()
        F = space.source_vector(pde.source)
        uh = space.function()
        bc = BoundaryCondition(space, robin=pde.robin)
        A, b = space.set_robin_bc(A, F, pde.robin)
        uh[:] = spsolve(A, b).reshape(-1)
        error = space.integralalg.L2_error(pde.solution, uh)
        print(error)
Exemplo n.º 5
0
import numpy as np
from scipy.sparse.linalg import spsolve

import matplotlib.pyplot as plt

from fealpy.pde.linear_elasticity_model import HuangModel2d
from fealpy.functionspace import LagrangeFiniteElementSpace
from fealpy.boundarycondition import BoundaryCondition

p = int(sys.argv[1])
n = int(sys.argv[2])

pde = HuangModel2d(lam=100000, mu=1.0)
mu = pde.mu
lam = pde.lam
mesh = pde.init_mesh(n=n)

space = LagrangeFiniteElementSpace(mesh, p=p)
bc = BoundaryCondition(space, dirichlet=pde.dirichlet)
uh = space.function(dim=2)
#A = space.linear_elasticity_matrix(mu, lam)
A = space.recovery_linear_elasticity_matrix(mu, lam)
F = space.source_vector(pde.source, dim=2)
A, F = bc.apply_dirichlet_bc(A,
                             F,
                             uh,
                             is_dirichlet_boundary=pde.is_dirichlet_boundary)
uh.T.flat[:] = spsolve(A, F)
error = space.integralalg.L2_error(pde.displacement, uh)
print(error)
from fealpy.pde.poisson_2d import CosCosData
from fealpy.functionspace import LagrangeFiniteElementSpace
from fealpy.boundarycondition import BoundaryCondition


pde = CosCosData()

print('test')
mesh = pde.init_mesh(n=7, meshtype='tri')
print('test')
space = LagrangeFiniteElementSpace(mesh, p=1)
print('test')

A = space.stiff_matrix()
print('test')
b = space.source_vector(pde.source)
print('test')
uh = space.function()
bc = BoundaryCondition(space, robin=pde.robin)
bc.apply_robin_bc(A, b)
uh[:] = spsolve(A, b).reshape(-1)
error = space.integralalg.L2_error(pde.solution, uh)
print(error)

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

Exemplo n.º 7
0
n = int(sys.argv[1])
p = int(sys.argv[2])
scale = float(sys.argv[3])

E = 1e+5
nu = 0.3
pde = LShapeDomainData2d(E=E, nu=nu)

mu = pde.mu
lam = pde.lam

mesh = pde.init_mesh(n=n)

space = LagrangeFiniteElementSpace(mesh, p=p)
bc = BoundaryCondition(space, dirichlet=pde.dirichlet, neuman=pde.neuman)
uh = space.function(dim=2)
A = space.linear_elasticity_matrix(mu, lam)
F = space.source_vector(pde.source, dim=2)

bc.apply_neuman_bc(F, is_neuman_boundary=pde.is_neuman_boundary)
A, F = bc.apply_dirichlet_bc(A,
                             F,
                             uh,
                             is_dirichlet_boundary=pde.is_dirichlet_boundary)

uh.T.flat[:] = spsolve(A, F)

error = space.integralalg.L2_error(pde.displacement, uh)

bc = mesh.entity_barycenter('edge')
Exemplo n.º 8
0
    maxit = 4
    pde = CosCosData()
    box = pde.domain()

    # # error settings
    errorType = [
        '$|| u - u_h||_0$', '$||\\nabla u - \\nabla u_h||_E$',
        '$||\\nabla u - \\nabla u_h||_E0$', '$||\\nabla u - \\nabla u_h||_1$'
    ]
    errorMatrix = np.zeros((len(errorType), maxit), dtype=np.float)

    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()

        A0 = space.stiff_matrix()
        F0 = space.source_vector(pde.source)

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

        uh[:] = spsolve(A, F)
        sh = space.project_to_smspace(uh)

        # # L2-error
        errorMatrix[0, i] = space.integralalg.L2_error(pde.solution, sh)
Exemplo n.º 9
0
 def apply_boundary_condition(self, A, b, timeline):
     t1 = timeline.next_time_level()
     bc = BoundaryCondition(self.space,
                            neuamn=lambda x: self.pde.neuman(x, t1))
     b = bc.apply_neuman_bc(b)
     return A, b