Ejemplo n.º 1
0
    print('Step:', i)
    space = LagrangeFiniteElementSpace(mesh, p=p)
    NDof[i] = space.number_of_global_dofs()
    uh = space.function()
    a = np.array([(10.0, -1.0), (-1.0, 2.0)], dtype=np.float64)
    A = space.stiff_matrix(c=a)

    @cartesian
    def r(p):
        x = p[..., 0]
        y = p[..., 1]
        return 1 + x**2 + y**2

    M = space.mass_matrix(c=r)
    b = np.array([1.0, 1.0], dtype=np.float64)
    B = space.convection_matrix(c=b)

    @cartesian
    def f(p):
        x = p[..., 0]
        y = p[..., 1]
        pi = np.pi
        return 2 * pi**2 * np.sin(pi * x) * np.sin(
            pi * y) + 12 * pi**2 * np.cos(pi * x) * np.cos(pi * y) + (
                -pi) * np.sin(pi * x) * np.cos(pi * y) - pi * np.cos(
                    pi * x) * np.sin(pi * y) + (1 + x**2 + y**2) * np.cos(
                        pi * x) * np.cos(pi * y)

    F = space.source_vector(f)
    A += B + M
Ejemplo n.º 2
0
pde = CDRMODEL()
domain = pde.domain()
mf = MeshFactory()
mesh = mf.boxmesh2d(domain, nx=n, ny=n, meshtype='tri')
NDof = np.zeros(maxit, dtype=mesh.itype)

errorMatrix = np.zeros((2, maxit), dtype=mesh.ftype)
errorType = ['$|| u  - u_h ||_0$', '$|| \\nabla u - \\nabla u_h||_0$']

for i in range(maxit):
    print('Step:', i)
    space = LagrangeFiniteElementSpace(mesh, p=p)
    NDof[i] = space.number_of_global_dofs()
    uh = space.function()
    A = space.stiff_matrix(c=pde.diffusion_coefficient)
    B = space.convection_matrix(c=pde.convection_coefficient_ndf)
    M = space.mass_matrix(c=pde.reaction_coefficient)
    F = space.source_vector(pde.source)
    A += B
    A += M

    bc = DirichletBC(space, pde.dirichlet)
    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,