Esempio n. 1
0
def coarsening_example1(**kwargs):
    #######################
    # Simple 2d coarsening based on tpfa for simplex grids
    # isotropic permeability
    #######################
    Nx = Ny = 7
    g = simplex.StructuredTriangleGrid([Nx, Ny], [1, 1])
    g.compute_geometry()

    if kwargs["visualize"]:
        plot_grid(g, info="all", alpha=0)

    part = create_partition(tpfa_matrix(g))
    g = generate_coarse_grid(g, part)
    g.compute_geometry(is_starshaped=True)

    if kwargs["visualize"]:
        plot_grid(g, info="all", alpha=0)

    g = simplex.StructuredTriangleGrid([Nx, Ny], [1, 1])
    g.compute_geometry()

    part = create_partition(tpfa_matrix(g), cdepth=3)
    g = generate_coarse_grid(g, part)
    g.compute_geometry(is_starshaped=True)

    if kwargs["visualize"]:
        plot_grid(g, info="all", alpha=0)
Esempio n. 2
0
def coarsening_example3(**kwargs):
    #######################
    # Simple 2d coarsening based on tpfa for simplex grids
    # anisotropic permeability
    #######################
    Nx = Ny = 7
    g = simplex.StructuredTriangleGrid([Nx, Ny], [1, 1])
    g.compute_geometry()

    if kwargs["visualize"]:
        plot_grid(g, info="all", alpha=0)

    kxx = 3 * np.ones(g.num_cells)
    kyy = np.ones(g.num_cells)
    perm = tensor.SecondOrderTensor(g.dim, kxx=kxx, kyy=kyy)

    part = create_partition(tpfa_matrix(g, perm=perm))
    g = generate_coarse_grid(g, part)
    g.compute_geometry(is_starshaped=True)

    if kwargs["visualize"]:
        plot_grid(g, info="all", alpha=0)

    g = simplex.StructuredTriangleGrid([Nx, Ny], [1, 1])
    g.compute_geometry()

    part = create_partition(tpfa_matrix(g, perm=perm), cdepth=3)
    g = generate_coarse_grid(g, part)
    g.compute_geometry(is_starshaped=True)

    if kwargs["visualize"]:
        plot_grid(g, info="all", alpha=0)

    g = simplex.StructuredTriangleGrid([Nx, Ny], [1, 1])
    g.compute_geometry()

    part = create_partition(tpfa_matrix(g, perm=perm), cdepth=2, epsilon=1e-2)
    g = generate_coarse_grid(g, part)
    g.compute_geometry(is_starshaped=True)

    if kwargs["visualize"]:
        plot_grid(g, info="all", alpha=0)

    g = simplex.StructuredTriangleGrid([Nx, Ny], [1, 1])
    g.compute_geometry()

    part = create_partition(tpfa_matrix(g, perm=perm), cdepth=2, epsilon=1)
    g = generate_coarse_grid(g, part)
    g.compute_geometry(is_starshaped=True)

    if kwargs["visualize"]:
        plot_grid(g, info="all", alpha=0)
Esempio n. 3
0
    def test_upwind_2d_simplex_surf_discharge_positive(self):
        g = simplex.StructuredTriangleGrid([2, 1], [1, 1])
        R = cg.rot(np.pi / 2., [1, 1, 0])
        g.nodes = np.dot(R, g.nodes)
        g.compute_geometry()

        solver = upwind.Upwind()
        param = Parameters(g)
        dis = solver.discharge(g, np.dot(R, [1, 0, 0]))

        bf = g.tags["domain_boundary_faces"].nonzero()[0]
        bc = BoundaryCondition(g, bf, bf.size * ["neu"])
        param.set_bc(solver, bc)

        data = {"param": param, "discharge": dis}
        M = solver.matrix_rhs(g, data)[0].todense()
        deltaT = solver.cfl(g, data)

        M_known = np.array([[1, -1, 0, 0], [0, 1, 0, 0], [0, 0, 0, -1],
                            [-1, 0, 0, 1]])
        deltaT_known = 1 / 6

        rtol = 1e-15
        atol = rtol
        self.assertTrue(np.allclose(M, M_known, rtol, atol))
        self.assertTrue(np.allclose(deltaT, deltaT_known, rtol, atol))
Esempio n. 4
0
def main(N):
    Nx = Ny = N

    #g = structured.CartGrid([Nx, Ny], [2, 2])
    g = simplex.StructuredTriangleGrid([Nx, Ny], [1, 1])
    g.compute_geometry()
    #co.coarsen(g, 'by_volume')

    # Assign parameters
    data = add_data(g)

    # Choose and define the solvers
    solver_flow = vem_dual.DualVEM('flow')
    A_flow, b_flow = solver_flow.matrix_rhs(g, data)

    solver_source = vem_source.DualSource('flow')
    A_source, b_source = solver_source.matrix_rhs(g, data)

    up = sps.linalg.spsolve(A_flow + A_source, b_flow + b_source)

    u = solver_flow.extract_u(g, up)
    p = solver_flow.extract_p(g, up)
    P0u = solver_flow.project_u(g, u, data)

    diam = np.amax(g.cell_diameters())
    return diam, error_p(g, p)
Esempio n. 5
0
    def test_upwind_2d_simplex_discharge_negative(self):
        g = simplex.StructuredTriangleGrid([2, 1], [1, 1])
        g.compute_geometry()

        solver = upwind.Upwind()
        param = Parameters(g)
        dis = solver.discharge(g, [-1, 0, 0])

        bf = g.get_boundary_faces()
        bc = BoundaryCondition(g, bf, bf.size * ['neu'])
        param.set_bc(solver, bc)

        data = {'param': param, 'discharge': dis}
        M = solver.matrix_rhs(g, data)[0].todense()
        deltaT = solver.cfl(g, data)

        M_known = np.array([[ 1, 0, 0,-1],
                            [-1, 0, 0, 0],
                            [ 0, 0, 1, 0],
                            [ 0, 0,-1, 1]])
        deltaT_known = 1/6

        rtol = 1e-15
        atol = rtol
        assert np.allclose(M, M_known, rtol, atol)
        assert np.allclose(deltaT, deltaT_known, rtol, atol)
Esempio n. 6
0
 def test_create_partition_2d_tri(self):
     g = simplex.StructuredTriangleGrid([3, 2])
     g.compute_geometry()
     part = co.create_partition(co.tpfa_matrix(g))
     known = np.array([1, 1, 1, 0, 0, 1, 0, 2, 2, 0, 2, 2])
     known_map = np.array([4, 3, 7, 5, 11, 8, 1, 2, 10, 6, 12, 9]) - 1
     assert np.array_equal(part, known[known_map])
Esempio n. 7
0
    def test_upwind_2d_simplex_surf_discharge_negative(self):
        g = simplex.StructuredTriangleGrid([2, 1], [1, 1])
        R = cg.rot(-np.pi / 5., [1, 1, -1])
        g.nodes = np.dot(R, g.nodes)
        g.compute_geometry(is_embedded=True)

        solver = upwind.Upwind()
        param = Parameters(g)
        dis = solver.discharge(g, np.dot(R, [-1, 0, 0]))

        bf = g.tags['domain_boundary_faces'].nonzero()[0]
        bc = BoundaryCondition(g, bf, bf.size * ['neu'])
        param.set_bc(solver, bc)

        data = {'param': param, 'discharge': dis}
        M = solver.matrix_rhs(g, data)[0].todense()
        deltaT = solver.cfl(g, data)

        M_known = np.array([[1, 0, 0, -1],
                            [-1, 0, 0, 0],
                            [0, 0, 1, 0],
                            [0, 0, -1, 1]])
        deltaT_known = 1 / 6

        rtol = 1e-15
        atol = rtol
        assert np.allclose(M, M_known, rtol, atol)
        assert np.allclose(deltaT, deltaT_known, rtol, atol)
Esempio n. 8
0
def make_grid(grid, grid_dims, domain, dim):
    if grid.lower() == "cart" or grid.lower() == "cartesian":
        return structured.CartGrid(grid_dims, domain)
    elif (grid.lower() == "simplex" and dim == 2) or grid.lower() == "triangular":
        return simplex.StructuredTriangleGrid(grid_dims, domain)
    elif grid.lower() == "tetrahedral":
        g = simplex.StructuredTetrahedralGrid(grid_dims, domain)
        return g
Esempio n. 9
0
def make_grid(grid, grid_dims, domain, dim):
    if grid.lower() == 'cart' or grid.lower() == 'cartesian':
        return structured.CartGrid(grid_dims, domain)
    elif (grid.lower() == 'simplex' and dim == 2) \
            or grid.lower() == 'triangular':
        return simplex.StructuredTriangleGrid(grid_dims, domain)
    elif grid.lower() == 'tetrahedral':
        g =simplex.StructuredTetrahedralGrid(grid_dims, domain)
        return g
Esempio n. 10
0
    def test_simplex_2d(self):
        g = simplex.StructuredTriangleGrid(np.array([3, 2]))
        g.compute_geometry()
        c = np.array([0, 1, 3])
        true_nodes = np.array([0, 1, 4, 5, 6])
        true_faces = np.array([0, 1, 2, 4, 5, 10, 13])

        h, sub_f, sub_n = partition.extract_subgrid(g, c)

        assert np.array_equal(true_nodes, sub_n)
        assert np.array_equal(true_faces, sub_f)

        self.compare_grid_geometries(g, h, c, true_faces, true_nodes)
Esempio n. 11
0
def darcy_dualVEM_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()

    T = cg.tangent_matrix(g.nodes)

    kxx = np.ones(g.num_cells)
    perm = tensor.SecondOrderTensor(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_all_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, 0])
    assert np.allclose(errors, errors_known)
Esempio n. 12
0
def main(N):
    Nx = Ny = N

    #g = structured.CartGrid([Nx, Ny], [1, 1])
    g = simplex.StructuredTriangleGrid([Nx, Ny], [1, 1])
    g.compute_geometry()

    # Assign parameters
    data = add_data(g)

    # Choose and define the solvers
    solver = mpfa.Mpfa('flow')
    A, b_flux = solver.matrix_rhs(g, data)
    _, b_source = source.Integral('flow').matrix_rhs(g, data)
    p = sps.linalg.spsolve(A, b_flux + b_source)

    diam = np.amax(g.cell_diameters())
    return diam, error_p(g, p)
Esempio n. 13
0

# ------------------------------------------------------------------------------#


def error_p(g, p):

    sol = np.array([solution(*pt) for pt in g.cell_centers.T])
    return np.sqrt(np.sum(np.power(np.abs(p - sol), 2) * g.cell_volumes))


# ------------------------------------------------------------------------------#

Nx = Ny = 20
# g = structured.CartGrid([Nx, Ny], [1, 1])
g = simplex.StructuredTriangleGrid([Nx, Ny], [1, 1])
R = cg.rot(np.pi / 4.0, [1, 0, 0])
g.nodes = np.dot(R, g.nodes)
g.compute_geometry()
# co.coarsen(g, 'by_volume')

# Assign parameters
data = add_data(g)

# Choose and define the solvers
solver = dual.DualVEM("flow")
A, b = solver.matrix_rhs(g, data)
up = sps.linalg.spsolve(A, b)

u = solver.extract_u(g, up)
p = solver.extract_p(g, up)
Esempio n. 14
0
 def test_coverage_triangles(self):
     domain = np.array([2, 3])
     grid_size = np.array([3, 5])
     g = simplex.StructuredTriangleGrid(grid_size, domain)
     g.compute_geometry()
     self.assertTrue(np.abs(domain.prod() - np.sum(g.cell_volumes)) < 1e-10)
Esempio n. 15
0
 def setUp(self):
     g = simplex.StructuredTriangleGrid([1, 1])
     g.compute_geometry()
     self.g = g
Esempio n. 16
0
    def test_tag_2d_simplex(self):
        g = simplex.StructuredTriangleGrid([3] * 2, [1] * 2)

        self.assertTrue(
            np.array_equal(g.tags["fracture_faces"], [False] * g.num_faces))
        self.assertTrue(
            np.array_equal(g.tags["fracture_nodes"], [False] * g.num_nodes))
        self.assertTrue(
            np.array_equal(g.tags["tip_faces"], [False] * g.num_faces))
        self.assertTrue(
            np.array_equal(g.tags["tip_nodes"], [False] * g.num_nodes))
        known = np.array(
            [
                True,
                True,
                False,
                True,
                False,
                False,
                True,
                False,
                False,
                True,
                False,
                True,
                False,
                False,
                False,
                False,
                False,
                False,
                False,
                True,
                False,
                True,
                False,
                False,
                False,
                False,
                False,
                False,
                False,
                True,
                True,
                True,
                True,
            ],
            dtype=bool,
        )
        self.assertTrue(np.array_equal(g.tags["domain_boundary_faces"], known))
        known = np.array(
            [
                True,
                True,
                True,
                True,
                True,
                False,
                False,
                True,
                True,
                False,
                False,
                True,
                True,
                True,
                True,
                True,
            ],
            dtype=bool,
        )
        self.assertTrue(np.array_equal(g.tags["domain_boundary_nodes"], known))
Esempio n. 17
0
def test_determine_eta():
    g = simplex.StructuredTriangleGrid([1, 1])
    assert fvutils.determine_eta(g) == 1 / 3
    g = structured.CartGrid([1, 1])
    assert fvutils.determine_eta(g) == 0
Esempio n. 18
0
 def test_determine_eta(self):
     g = simplex.StructuredTriangleGrid([1, 1])
     self.assertTrue(fvutils.determine_eta(g) == 1 / 3)
     g = structured.CartGrid([1, 1])
     self.assertTrue(fvutils.determine_eta(g) == 0)