Пример #1
0
def darcy_dual_hybridVEM_example3(**kwargs):
    #######################
    # Simple 3d Darcy problem with known exact solution
    #######################
    Nx = Ny = Nz = 7
    g = structured.CartGrid([Nx, Ny, Nz], [1, 1, 1])
    g.compute_geometry()

    kxx = np.ones(g.num_cells)
    perm = tensor.SecondOrder(g.dim, kxx)

    def funP_ex(pt):
        return np.sin(2*np.pi*pt[0])*np.sin(2*np.pi*pt[1])\
            * np.sin(2*np.pi*pt[2])

    def funU_ex(pt):
        return [-2*np.pi*np.cos(2*np.pi*pt[0])\
                * np.sin(2*np.pi*pt[1])*np.sin(2*np.pi*pt[2]),
                -2*np.pi*np.sin(2*np.pi*pt[0])\
                * np.cos(2*np.pi*pt[1])*np.sin(2*np.pi*pt[2]),
                -2*np.pi*np.sin(2*np.pi*pt[0])\
                * np.sin(2*np.pi*pt[1])*np.cos(2*np.pi*pt[2])]

    def fun(pt):
        return 12 * np.pi**2 * funP_ex(pt)

    f = np.array([fun(pt) for pt in g.cell_centers.T])

    b_faces = g.get_boundary_faces()
    bnd = bc.BoundaryCondition(g, b_faces, ['dir'] * b_faces.size)
    bnd_val = np.zeros(g.num_faces)
    bnd_val[b_faces] = funP_ex(g.face_centers[:, b_faces])

    solver = hybrid.HybridDualVEM()
    data = {'perm': perm, 'source': f, 'bc': bnd, 'bc_val': bnd_val}
    H, rhs = solver.matrix_rhs(g, data)

    l = sps.linalg.spsolve(H, rhs)
    u, p = solver.compute_up(g, l, data)
    P0u = dual.DualVEM().project_u(g, u)

    if kwargs['visualize']:
        plot_grid(g, p, P0u)

    p_ex = error.interpolate(g, funP_ex)
    u_ex = error.interpolate(g, funU_ex)

    np.set_printoptions(linewidth=999999)
    np.set_printoptions(precision=16)

    errors = np.array(
        [error.error_L2(g, p, p_ex),
         error.error_L2(g, P0u, u_ex)])
    errors_known = np.array([0.1010936831876412, 0.0680593765009036])
    assert np.allclose(errors, errors_known)
Пример #2
0
def darcy_dual_hybridVEM_example2(**kwargs):
    #######################
    # Simple 2d Darcy problem on a surface with known exact solution
    #######################
    Nx = Ny = 25
    g = simplex.StructuredTriangleGrid([Nx, Ny], [1, 1])
    R = cg.rot(np.pi / 6., [0, 1, 1])
    g.nodes = np.dot(R, g.nodes)
    g.compute_geometry(is_embedded=True)

    T = cg.tangent_matrix(g.nodes)

    kxx = np.ones(g.num_cells)
    perm = tensor.SecondOrder(g.dim, kxx)

    def funP_ex(pt):
        return np.pi * pt[0] - 6 * pt[1] + np.exp(1) * pt[2] - 4

    def funU_ex(pt):
        return np.dot(T, [-np.pi, 6, -np.exp(1)])

    def fun(pt):
        return 0

    f = np.array([fun(pt) for pt in g.cell_centers.T])

    b_faces = g.get_boundary_faces()
    bnd = bc.BoundaryCondition(g, b_faces, ['dir'] * b_faces.size)
    bnd_val = np.zeros(g.num_faces)
    bnd_val[b_faces] = funP_ex(g.face_centers[:, b_faces])

    solver = hybrid.HybridDualVEM()
    data = {'perm': perm, 'source': f, 'bc': bnd, 'bc_val': bnd_val}
    H, rhs = solver.matrix_rhs(g, data)

    l = sps.linalg.spsolve(H, rhs)
    u, p = solver.compute_up(g, l, data)
    P0u = dual.DualVEM().project_u(g, u)

    if kwargs['visualize']:
        plot_grid(g, p, P0u)

    p_ex = error.interpolate(g, funP_ex)
    u_ex = error.interpolate(g, funU_ex)

    errors = np.array(
        [error.error_L2(g, p, p_ex),
         error.error_L2(g, P0u, u_ex)])
    errors_known = np.array([0, 0])
    assert np.allclose(errors, errors_known)
Пример #3
0
def darcy_dualVEM_example1(**kwargs):
    #######################
    # Simple 2d Darcy problem with known exact solution
    #######################
    Nx = Ny = 25
    g = structured.CartGrid([Nx, Ny], [1, 1])
    g.compute_geometry()

    kxx = np.ones(g.num_cells)
    perm = tensor.SecondOrder(g.dim, kxx)

    def funP_ex(pt):
        return np.sin(2 * np.pi * pt[0]) * np.sin(2 * np.pi * pt[1])

    def funU_ex(pt):
        return [
            -2 * np.pi * np.cos(2 * np.pi * pt[0]) * np.sin(2 * np.pi * pt[1]),
            -2 * np.pi * np.sin(2 * np.pi * pt[0]) * np.cos(2 * np.pi * pt[1]),
            0
        ]

    def fun(pt):
        return 8 * np.pi**2 * funP_ex(pt)

    f = np.array([fun(pt) for pt in g.cell_centers.T])

    b_faces = g.get_boundary_faces()
    bnd = bc.BoundaryCondition(g, b_faces, ['dir'] * b_faces.size)
    bnd_val = np.zeros(g.num_faces)
    bnd_val[b_faces] = funP_ex(g.face_centers[:, b_faces])

    solver = dual.DualVEM()
    data = {'perm': perm, 'source': f, 'bc': bnd, 'bc_val': bnd_val}
    D, rhs = solver.matrix_rhs(g, data)

    up = sps.linalg.spsolve(D, rhs)
    u, p = solver.extract_u(g, up), solver.extract_p(g, up)
    P0u = solver.project_u(g, u)

    if kwargs['visualize']:
        plot_grid(g, p, P0u)

    p_ex = error.interpolate(g, funP_ex)
    u_ex = error.interpolate(g, funU_ex)

    errors = np.array(
        [error.error_L2(g, p, p_ex),
         error.error_L2(g, P0u, u_ex)])
    errors_known = np.array([0.0210718223032, 0.00526933885613])
    assert np.allclose(errors, errors_known)
Пример #4
0
def gb_error(gb, v1, v2, norm='L2'):
    gb.add_node_props(['error_1', 'error_2'])
    has_splitter.split(gb, 'error_1', v1)
    has_splitter.split(gb, 'error_2', v2)
    e = gb.apply_function_to_nodes(
        lambda g, d: error_L2(g, d['error_1'], d['error_2'], relative=False))
    return e
Пример #5
0
    def test_1d_elimination_3d_2d_1d(self):
        """
        3d case with a single 1d grid.
        """
        f1 = np.array([[0, 1, 1, 0], [0, 0, 1, 1], [.5, .5, .5, .5]])
        f2 = np.array([[.5, .5, .5, .5], [0, 1, 1, 0], [0, 0, 1, 1]])

        gb = meshing.cart_grid([f1, f2], [2, 2, 2], **{'physdims': [1, 1, 1]})
        gb.compute_geometry()
        gb.assign_node_ordering()

        tol = 1e-3
        solver = tpfa.Tpfa()
        gb.add_node_props(['param'])

        a = 1e-2
        for g, d in gb:
            param = Parameters(g)

            aperture = np.ones(g.num_cells) * np.power(a, gb.dim_max() - g.dim)
            param.set_aperture(aperture)

            p = tensor.SecondOrder(
                3,
                np.ones(g.num_cells) * np.power(1e3, g.dim < gb.dim_max()))
            param.set_tensor('flow', p)
            bound_faces = g.get_boundary_faces()
            bound_face_centers = g.face_centers[:, bound_faces]

            left = bound_face_centers[0, :] > 1 - tol
            right = bound_face_centers[0, :] < tol

            labels = np.array(['neu'] * bound_faces.size)
            labels[np.logical_or(left, right)] = ['dir']

            bc_val = np.zeros(g.num_faces)
            bc_dir = bound_faces[np.logical_or(left, right)]
            bc_val[bc_dir] = g.face_centers[0, bc_dir]

            param.set_bc(solver, bc.BoundaryCondition(g, bound_faces, labels))
            param.set_bc_val(solver, bc_val)

            d['param'] = param

        coupling_conditions = tpfa.TpfaCoupling(solver)
        solver_coupler = coupler.Coupler(solver, coupling_conditions)
        A, rhs = solver_coupler.matrix_rhs(gb)

        p = sps.linalg.spsolve(A, rhs)
        p_cond, _, _, _ = condensation.solve_static_condensation(\
                                                                 A, rhs, gb, dim=1)

        solver_coupler.split(gb, "pressure", p)
        solver_coupler.split(gb, "p_cond", p_cond)

        tol = 1e-5
        assert ((np.amax(np.absolute(p - p_cond))) < tol)
        assert (np.sum(
            error.error_L2(g, d['pressure'], d['p_cond'])
            for g, d in gb) < tol)
Пример #6
0
    def test_1d_elimination_2d_1d(self):
        """
        Simplest case possible:
        2d case with two fractures intersecting in a single 0d grid
        at the center of the domain.
        """
        f1 = np.array([[0, 1], [.5, .5]])

        gb = meshing.cart_grid([f1], [2, 2], **{'physdims': [1, 1]})
        gb.compute_geometry()
        gb.assign_node_ordering()

        tol = 1e-3
        solver = tpfa.Tpfa()
        gb.add_node_props(['param'])
        a = 1e-2
        for g, d in gb:
            param = Parameters(g)

            a_dim = np.power(a, gb.dim_max() - g.dim)
            aperture = np.ones(g.num_cells) * a_dim
            param.set_aperture(aperture)

            kxx = np.ones(g.num_cells) * np.power(1e3, g.dim < gb.dim_max())
            #print(kxx, 'dim', g.dim)
            p = tensor.SecondOrder(3, kxx, kyy=kxx, kzz=kxx)
            #print(p.perm)
            param.set_tensor('flow', p)
            bound_faces = g.get_boundary_faces()
            bound_face_centers = g.face_centers[:, bound_faces]

            right = bound_face_centers[0, :] > 1 - tol
            left = bound_face_centers[0, :] < tol

            labels = np.array(['neu'] * bound_faces.size)
            labels[right] = ['dir']

            bc_val = np.zeros(g.num_faces)
            bc_dir = bound_faces[right]
            bc_neu = bound_faces[left]
            bc_val[bc_dir] = g.face_centers[0, bc_dir]
            bc_val[bc_neu] = -g.face_areas[bc_neu] * a_dim

            param.set_bc(solver, bc.BoundaryCondition(g, bound_faces, labels))
            param.set_bc_val(solver, bc_val)
            d['param'] = param

        coupling_conditions = tpfa.TpfaCoupling(solver)
        solver_coupler = coupler.Coupler(solver, coupling_conditions)
        A, rhs = solver_coupler.matrix_rhs(gb)

        p = sps.linalg.spsolve(A, rhs)
        p_cond, _, _, _ = condensation.solve_static_condensation(\
                                                                 A, rhs, gb, dim=1)

        solver_coupler.split(gb, "pressure", p)
        solver_coupler.split(gb, "p_cond", p_cond)

        tol = 1e-10
        assert ((np.amax(np.absolute(p - p_cond))) < tol)
        assert (np.sum(
            error.error_L2(g, d['pressure'], d['p_cond'])
            for g, d in gb) < tol)
Пример #7
0
    def test_tpfa_fluxes_2d_1d_cross_with_elimination(self):
        f1 = np.array([[0, 1], [.5, .5]])
        f2 = np.array([[.5, .5], [0, 1]])
        domain = {'xmin': 0, 'ymin': 0, 'xmax': 1, 'ymax': 1}
        mesh_size = 0.4
        mesh_kwargs = {}
        mesh_kwargs['mesh_size'] = {
            'mode': 'constant',
            'value': mesh_size,
            'bound_value': mesh_size
        }
        gb = meshing.cart_grid([f1, f2], [2, 2], **{'physdims': [1, 1]})
        #gb = meshing.simplex_grid( [f1, f2],domain,**mesh_kwargs)
        gb.compute_geometry()
        gb.assign_node_ordering()

        # Enforce node orderning because of Python 3.5 and 2.7.
        # Don't do it in general.
        cell_centers_1 = np.array([[7.50000000e-01, 2.500000000e-01],
                                   [5.00000000e-01, 5.00000000e-01],
                                   [-5.55111512e-17, 5.55111512e-17]])
        cell_centers_2 = np.array([[5.00000000e-01, 5.00000000e-01],
                                   [7.50000000e-01, 2.500000000e-01],
                                   [-5.55111512e-17, 5.55111512e-17]])

        for g, d in gb:
            if g.dim == 1:
                if np.allclose(g.cell_centers, cell_centers_1):
                    d['node_number'] = 1
                elif np.allclose(g.cell_centers, cell_centers_2):
                    d['node_number'] = 2
                else:
                    raise ValueError('Grid not found')

        tol = 1e-3
        solver = tpfa.TpfaMixedDim('flow')
        gb.add_node_props(['param'])
        a = 1e-2
        for g, d in gb:
            param = Parameters(g)

            a_dim = np.power(a, gb.dim_max() - g.dim)
            aperture = np.ones(g.num_cells) * a_dim
            param.set_aperture(aperture)

            kxx = np.ones(g.num_cells) * np.power(1e3, g.dim < gb.dim_max())
            p = tensor.SecondOrder(3, kxx, kyy=kxx, kzz=kxx)
            param.set_tensor('flow', p)

            bound_faces = g.get_boundary_faces()
            bound_face_centers = g.face_centers[:, bound_faces]

            right = bound_face_centers[0, :] > 1 - tol
            left = bound_face_centers[0, :] < tol

            labels = np.array(['neu'] * bound_faces.size)
            labels[right] = ['dir']

            bc_val = np.zeros(g.num_faces)
            bc_dir = bound_faces[right]
            bc_neu = bound_faces[left]
            bc_val[bc_dir] = g.face_centers[0, bc_dir]
            bc_val[bc_neu] = -g.face_areas[bc_neu] * a_dim

            param.set_bc('flow', bc.BoundaryCondition(g, bound_faces, labels))
            param.set_bc_val('flow', bc_val)

            d['param'] = param

        gb.add_edge_prop('param')
        for e, d in gb.edges_props():
            g_h = gb.sorted_nodes_of_edge(e)[1]
            d['param'] = Parameters(g_h)

        A, rhs = solver.matrix_rhs(gb)
        p = sps.linalg.spsolve(A, rhs)

        p = sps.linalg.spsolve(A, rhs)
        p_cond, p_red, _, _ = condensation.solve_static_condensation(\
                                                    A, rhs, gb, dim=0)

        solver.split(gb, "p_cond", p_cond)
        solver.split(gb, "pressure", p)

        # Make a copy of the grid bucket without the 0d grid
        dim_to_remove = 0
        gb_r, elimination_data = gb.duplicate_without_dimension(dim_to_remove)
        # Compute the flux discretization on the new edges
        condensation.compute_elimination_fluxes(gb, gb_r, elimination_data)
        # Compute the discharges from the flux discretizations and computed pressures
        solver.split(gb_r, "pressure", p_red)
        fvutils.compute_discharges(gb)
        fvutils.compute_discharges(gb_r)

        # Known discharges
        d_0, d_1, d_2 = fluxes_2d_1d_cross_with_elimination()

        # Check node fluxes, ...
        rtol = 1e-6
        atol = rtol
        for g, d in gb:
            if d['node_number'] == 0:
                assert np.allclose(d['discharge'], d_0, rtol, atol)
            if d['node_number'] == 1:
                assert np.allclose(d['discharge'], d_1, rtol, atol)
            if d['node_number'] == 2:
                assert np.allclose(d['discharge'], d_2, rtol, atol)
        for g, d in gb_r:

            if d['node_number'] == 0:
                assert np.allclose(d['discharge'], d_0, rtol, atol)
            if d['node_number'] == 1:
                assert np.allclose(d['discharge'], d_1, rtol, atol)
            if d['node_number'] == 2:
                assert np.allclose(d['discharge'], d_2, rtol, atol)

        # ... edge fluxes ...
        d_01, d_10, d_02, d_20, d_13, d_23 = coupling_fluxes_2d_1d_cross_no_el(
        )

        for e, data in gb.edges_props():
            g1, g2 = gb.sorted_nodes_of_edge(e)
            pa = data['param']
            node_numbers = gb.nodes_prop([g2, g1], 'node_number')
            if pa is not None:

                if node_numbers == (0, 1):
                    assert np.allclose( data['discharge'], d_01, rtol, atol) or \
                        np.allclose( data['discharge'], d_10, rtol, atol)
                if node_numbers == (0, 2):
                    assert np.allclose( data['discharge'], d_02, rtol, atol) or \
                        np.allclose( data['discharge'], d_20, rtol, atol)
                if node_numbers == (1, 3):
                    assert np.allclose(data['discharge'], d_13, rtol, atol)
                if node_numbers == (2, 3):
                    assert np.allclose(data['discharge'], d_23, rtol, atol)

        d_11, d_21, d_22 = coupling_fluxes_2d_1d_cross_with_el()
        for e, data in gb_r.edges_props():
            g1, g2 = gb_r.sorted_nodes_of_edge(e)
            pa = data['param']
            node_numbers = gb_r.nodes_prop([g2, g1], 'node_number')
            if pa is not None:

                if node_numbers == (0, 1):
                    assert np.allclose( data['discharge'], d_01, rtol, atol) or \
                        np.allclose( data['discharge'], d_10, rtol, atol)
                if node_numbers == (0, 2):
                    assert np.allclose( data['discharge'], d_02, rtol, atol) or \
                        np.allclose( data['discharge'], d_20, rtol, atol)
                if node_numbers == (1, 1):
                    assert np.allclose(data['discharge'], d_11, rtol, atol)
                if node_numbers == (2, 1):
                    assert np.allclose(data['discharge'], d_21, rtol, atol)
                if node_numbers == (2, 2):
                    assert np.allclose(data['discharge'], d_22, rtol, atol)
        # ... and pressures
        tol = 1e-10
        assert ((np.amax(np.absolute(p - p_cond))) < tol)
        assert (np.sum(
            error.error_L2(g, d['pressure'], d['p_cond'])
            for g, d in gb) < tol)