Example #1
0
    def test_merge_two_grids(self):
        # Merge two grids that have a common face. Check that global indices are
        # updated to match, and that they point to the same point coordinates
        data = np.ones(3)
        rows = np.array([0, 1, 2])
        cols = np.array([0, 0, 0])
        cf = sps.coo_matrix((data, (rows, cols)))

        data = np.ones(6)
        rows = np.array([0, 1, 1, 2, 2, 0])
        cols = np.array([0, 0, 1, 1, 2, 2])
        fn = sps.coo_matrix((data, (rows, cols)))
        nodes_1 = np.array([[0, 1, 0], [0, 0, 1], [0, 0, 0]])
        nodes_2 = np.array([[0, 1, 0], [0, 0, -1], [0, 0, 0]])
        g1 = MockGrid(2,
                      num_faces=3,
                      face_nodes=fn,
                      cell_faces=cf,
                      num_cells=1,
                      nodes=nodes_1)

        g2 = MockGrid(2,
                      num_faces=3,
                      face_nodes=fn,
                      cell_faces=cf,
                      num_cells=1,
                      nodes=nodes_2)
        g_11 = TensorGrid(np.array([0, 1]))
        g_11.global_point_ind = np.arange(2)
        g_22 = TensorGrid(np.array([0, 1]))
        g_22.global_point_ind = np.arange(2)

        gl = [[[g1], [g_11]], [[g2], [g_22]]]
        intersections = [np.array([1]), np.array([0])]

        list_of_grids, glob_ind = non_conforming.init_global_ind(gl)
        grid_list_1d = non_conforming.process_intersections(gl,
                                                            intersections,
                                                            glob_ind,
                                                            list_of_grids,
                                                            tol=1e-4)

        g_1d = grid_list_1d[0]
        ismem, maps = ismember_rows(g_1d.global_point_ind, g1.global_point_ind)
        assert ismem.sum() == g_1d.num_nodes
        assert np.allclose(g1.nodes[:, maps], g_1d.nodes)

        ismem, maps = ismember_rows(g_1d.global_point_ind, g2.global_point_ind)
        assert ismem.sum() == g_1d.num_nodes
        assert np.allclose(g2.nodes[:, maps], g_1d.nodes)
Example #2
0
 def grid_1d(self, n_nodes=2):
     x = np.linspace(0, 1, n_nodes)
     g = TensorGrid(x)
     g.nodes = np.tile(x, (3, 1))
     g.compute_geometry()
     g.global_point_ind = 1 + np.arange(n_nodes)
     return g
Example #3
0
 def grid_1d(self, num_pts=3):
     g = TensorGrid(np.arange(num_pts))
     g.nodes = np.vstack(
         (np.linspace(0, 1,
                      num_pts), 0.5 * np.ones(num_pts), np.zeros(num_pts)))
     g.compute_geometry()
     g.global_point_ind = np.arange(g.num_nodes)
     return g
Example #4
0
    def test_merge_1d_permuted_nodes(self):
        g = TensorGrid(np.array([0, 1, 2]))
        g.nodes = np.array([[1, -1, 0], [0, 0, 0], [0, 0, 0]])
        g.global_point_ind = np.array([2, 0, 1])
        g.face_nodes.indices = np.array([1, 2, 0])

        h = TensorGrid(np.array([-1, 0, 1]))
        h.global_point_ind = np.array([0, 1, 2])
        g.compute_geometry()
        h.compute_geometry()
        c, _, _, _, _, _ = non_conforming.merge_1d_grids(g, h)

        ismem, maps = ismember_rows(c.global_point_ind, g.global_point_ind)
        assert ismem.sum() == c.num_nodes
        assert np.allclose(g.nodes[:, maps], c.nodes)
        ismem, maps = ismember_rows(c.global_point_ind, h.global_point_ind)
        assert ismem.sum() == c.num_nodes
        assert np.allclose(h.nodes[:, maps], c.nodes)
Example #5
0
def dfn(fracs, conforming, intersections=None, keep_geo=False, tol=1e-4,
        **kwargs):
    """ Create a mesh of a DFN model, that is, only of fractures.

    The mesh can eihter be conforming along fracture intersections, or each
    fracture is meshed independently. The latter case will typically require
    some sort of sewing together external to this funciton.

    TODO: What happens if we give in a non-connected network?

    Parameters:
        fracs (either Fractures, or a FractureNetwork).
        conforming (boolean): If True, the mesh will be conforming along 1d
            intersections.
        intersections (list of lists, optional): Each item corresponds to an
            intersection between two fractures. In each sublist, the first two
            indices gives fracture ids (refering to order in fracs). The third
            item is a numpy array representing intersection coordinates. If no
            intersections provided, intersections will be detected using
            function in FractureNetwork.
        **kwargs: Parameters passed to gmsh.

    Returns:
        GridBucket (if conforming is True): Mixed-dimensional mesh that
            represents all fractures, and intersection poitns and line.

    """

    if isinstance(fracs, FractureNetwork) \
       or isinstance(fracs, FractureNetwork_full):
        network = fracs
    else:
        network = FractureNetwork(fracs)

    # Populate intersections in FractureNetowrk, or find intersections if not
    # provided.

    if intersections is not None:
        logger.warn('FractureNetwork use pre-computed intersections')
        network.intersections = [Intersection(*i) for i in intersections]
    else:
        logger.warn('FractureNetwork find intersections in DFN')
        tic = time.time()
        network.find_intersections()
        logger.warn('Done. Elapsed time ' + str(time.time() - tic))

    if conforming:
        logger.warn('Create conforming mesh for DFN network')
        grids = simplex.triangle_grid_embedded(network, find_isect=False,
                                               **kwargs)
    else:
        logger.warn('Create non-conforming mesh for DFN network')
        tic = time.time()
        grid_list = []
        neigh_list = []

        for fi in range(len(network._fractures)):
            logger.info('Meshing of fracture ' + str(fi))
            # Rotate fracture vertexes and intersection points
            fp, ip, other_frac, rot, cp = network.fracture_to_plane(fi)
            frac_i = network[fi]

            f_lines = np.reshape(np.arange(ip.shape[1]), (2, -1), order='F')
            frac_dict = {'points': ip, 'edges': f_lines}
            if keep_geo:
                file_name = 'frac_mesh_' + str(fi)
                kwargs['file_name'] = file_name
            # Create mesh on this fracture surface.
            grids = simplex.triangle_grid(frac_dict, fp, verbose=False,
                                          **kwargs)

            irot = rot.T
            # Loop over grids, rotate back again to 3d coordinates
            for gl in grids:
                for g in gl:
                    g.nodes = irot.dot(g.nodes) + cp

            # Nodes of main (fracture) grid, in 3d coordinates1
            main_nodes = grids[0][0].nodes
            main_global_point_ind = grids[0][0].global_point_ind
            # Loop over intersections, check if the intersection is on the
            # boundary of this fracture.
            for ind, isect in enumerate(network.intersections_of_fracture(fi)):
                of = isect.get_other_fracture(frac_i)
                if isect.on_boundary_of_fracture(frac_i):
                    dist, _, _ = cg.dist_points_polygon(main_nodes, of.p)
                    hit = np.argwhere(dist < tol).reshape((1, -1))[0]
                    nodes_1d = main_nodes[:, hit]
                    global_point_ind = main_global_point_ind[hit]

                    assert cg.is_collinear(nodes_1d, tol=tol)
                    sort_ind = cg.argsort_point_on_line(nodes_1d, tol=tol)
                    g_aux = TensorGrid(np.arange(nodes_1d.shape[1]))
                    g_aux.nodes = nodes_1d[:, sort_ind]
                    g_aux.global_point_ind = global_point_ind[sort_ind]
                    grids[1].insert(ind, g_aux)

            assert len(grids[0]) == 1, 'Fracture should be covered by single'\
                'mesh'

            grid_list.append(grids)
            neigh_list.append(other_frac)

        logger.warn('Finished creating grids. Elapsed time ' +
                    str(time.time() - tic))
        logger.warn('Merge grids')
        tic = time.time()
        grids = non_conforming.merge_grids(grid_list, neigh_list)
        logger.warn('Done. Elapsed time ' + str(time.time() - tic))

        for g_set in grids:
            if len(g_set) > 0:
                s = 'Created ' + str(len(g_set)) + ' ' + str(g_set[0].dim) + \
                    '-d grids with '
                num = 0
                for g in g_set:
                    num += g.num_cells
                s += str(num) + ' cells'
                logger.info(s)

    return grid_list_to_grid_bucket(grids, check_highest_dim=False)
Example #6
0
    def test_merge_three_grids_internal_intersection_no_hanging_node_reverse_order(
            self):
        # Merge three grids, where a central cell share one face each with the two
        # other. Importantly, one node will be involved in both shared faces.
        data = np.ones(16)
        rows = np.array([0, 1, 1, 2, 0, 3, 0, 4, 1, 3, 1, 4, 2, 3, 2, 4])
        cols = np.array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7])
        fn = sps.coo_matrix((data, (rows, cols)))

        data = np.ones(12)
        rows = np.array([0, 5, 3, 1, 7, 5, 0, 2, 4, 1, 4, 6])
        cols = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3])
        cf = sps.coo_matrix((data, (rows, cols)))

        nodes_1 = np.array([[-1, 0, 1, 0, 0], [0, 0, 0, -1, 1],
                            [0, 0, 0, 0, 0]])
        nodes_2 = np.array([[-1, 0, 1, 0, 0], [0, 0, 0, 0, 0],
                            [0, 0, 0, -1, 1]])
        nodes_3 = np.array([[0, 0, 0, 0, 0], [-1, 0, 1, 0, 0],
                            [0, 0, 0, -1, 1]])
        # Central grid
        gxy = MockGrid(2,
                       num_faces=8,
                       face_nodes=fn,
                       cell_faces=cf,
                       num_cells=4,
                       nodes=nodes_1)
        # First neighboring grid
        gxz = MockGrid(2,
                       num_faces=8,
                       face_nodes=fn,
                       cell_faces=cf,
                       num_cells=4,
                       nodes=nodes_2)
        gyz = MockGrid(2,
                       num_faces=8,
                       face_nodes=fn,
                       cell_faces=cf,
                       num_cells=4,
                       nodes=nodes_3)

        # First 1d grid, as seen from g1
        g_1x = TensorGrid(np.array([0, 1, 2]))
        g_1x.nodes = np.array([[1, 0, -1], [0, 0, 0], [0, 0, 0]])
        g_1x.global_point_ind = np.array([2, 1, 0])
        #g_1x.face_nodes.indices = np.array([1, 2, 0])
        # Third 1d grid, as seen from g1
        g_1y = TensorGrid(np.array([0, 1, 2]))
        g_1y.nodes = np.array([[0, 0, 0], [-1, 0, 1], [0, 0, 0]])
        # Point indices adjusted according to ordering in nodes_1
        g_1y.global_point_ind = np.array([3, 1, 4])

        # First 1d grid, as seen from g2
        g_2x = TensorGrid(np.array([0, 1, 2]))
        g_2x.nodes = np.array([[-1, 0, 1], [0, 0, 0], [0, 0, 0]])
        g_2x.global_point_ind = np.arange(3)
        # Third 1d grid, as seen from g2
        g_2z = TensorGrid(np.array([0, 1, 2]))
        g_2z.nodes = np.array([[0, 0, 0], [0, 0, 0], [-1, 0, 1]])
        # Point indices adjusted according to ordering in nodes_1
        g_2z.global_point_ind = np.array([3, 1, 4])

        g_3y = TensorGrid(np.array([0, 1, 2]))
        g_3y.nodes = np.array([[0, 0, 0], [-1, 0, 1], [0, 0, 0]])
        g_3y.global_point_ind = np.arange(3)
        # Second 1d grid, as seen from g1
        g_3z = TensorGrid(np.array([0, 1, 2]))
        g_3z.nodes = np.array([[0, 0, 0], [0, 0, 0], [-1, 0, 1]])
        # Point indices adjusted according to ordering in nodes_1
        g_3z.global_point_ind = np.array([3, 1, 4])

        gl = [[[gxy], [g_1x, g_1y]], [[gxz], [g_2x, g_2z]],
              [[gyz], [g_3y, g_3z]]]
        intersections = [np.array([1, 2]), np.array([0, 2]), np.array([0, 1])]

        list_of_grids, glob_ind = non_conforming.init_global_ind(gl)
        grid_list_1d = non_conforming.process_intersections(gl,
                                                            intersections,
                                                            glob_ind,
                                                            list_of_grids,
                                                            tol=1e-4)
        assert len(grid_list_1d) == 3

        g_1d = grid_list_1d[0]
        ismem, maps = ismember_rows(g_1d.global_point_ind,
                                    gxy.global_point_ind)
        assert ismem.sum() == g_1d.num_nodes
        assert np.allclose(gxy.nodes[:, maps], g_1d.nodes)
        ismem, maps = ismember_rows(g_1d.global_point_ind,
                                    gxz.global_point_ind)
        assert ismem.sum() == g_1d.num_nodes
        assert np.allclose(gxz.nodes[:, maps], g_1d.nodes)

        g_1d = grid_list_1d[1]
        ismem, maps = ismember_rows(g_1d.global_point_ind,
                                    gxy.global_point_ind)
        assert ismem.sum() == g_1d.num_nodes
        assert np.allclose(gxy.nodes[:, maps], g_1d.nodes)
        ismem, maps = ismember_rows(g_1d.global_point_ind,
                                    gyz.global_point_ind)
        assert ismem.sum() == g_1d.num_nodes
        assert np.allclose(gyz.nodes[:, maps], g_1d.nodes)

        g_1d = grid_list_1d[2]
        ismem, maps = ismember_rows(g_1d.global_point_ind,
                                    gxz.global_point_ind)
        assert ismem.sum() == g_1d.num_nodes
        assert np.allclose(gxz.nodes[:, maps], g_1d.nodes)

        ismem, maps = ismember_rows(g_1d.global_point_ind,
                                    gyz.global_point_ind)
        assert ismem.sum() == g_1d.num_nodes
        assert np.allclose(gyz.nodes[:, maps], g_1d.nodes)
Example #7
0
    def test_merge_three_grids_hanging_node_shared_node(self):
        # Merge three grids, where a central cell share one face each with the two
        # other. Importantly, one node will be involved in both shared faces.
        data = np.ones(10)
        rows = np.array([0, 1, 1, 2, 2, 3, 3, 0, 3, 1])
        cols = np.array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4])
        fn_1 = sps.coo_matrix((data, (rows, cols)))

        data = np.ones(6)
        rows = np.array([0, 1, 1, 2, 2, 0])
        cols = np.array([0, 0, 1, 1, 2, 2])
        fn_2 = sps.coo_matrix((data, (rows, cols)))

        data = np.ones(6)
        rows = np.array([0, 4, 3, 1, 2, 4])
        cols = np.array([0, 0, 0, 1, 1, 1])
        cf_1 = sps.coo_matrix((data, (rows, cols)))

        data = np.ones(3)
        rows = np.array([0, 1, 2])
        cols = np.array([0, 0, 0])
        cf_2 = sps.coo_matrix((data, (rows, cols)))

        nodes_1 = np.array([[0, 1, 2, 1], [0, 0, 0, 1], [0, 0, 0, 0]])
        nodes_2 = np.array([[0, 2, 1], [0, 0, -1], [0, 0, 0]])
        nodes_3 = np.array([[0, 1, 0], [0, 1, 1], [0, 0, 0]])
        # Central grid
        g1 = MockGrid(2,
                      num_faces=5,
                      face_nodes=fn_1,
                      cell_faces=cf_1,
                      num_cells=2,
                      nodes=nodes_1)
        # First neighboring grid
        g2 = MockGrid(2,
                      num_faces=3,
                      face_nodes=fn_2,
                      cell_faces=cf_2,
                      num_cells=1,
                      nodes=nodes_2)
        g3 = MockGrid(2,
                      num_faces=3,
                      face_nodes=fn_2,
                      cell_faces=cf_2,
                      num_cells=1,
                      nodes=nodes_3)

        # First 1d grid, as seen from g1
        g_11 = TensorGrid(np.array([0, 1, 2]))
        g_11.global_point_ind = np.arange(3)
        # Second 1d grid, as seen from g1
        g_13 = TensorGrid(np.array([0, 1]))
        g_13.nodes = np.array([[0, 1], [0, 1], [0, 0]])
        # Point indices adjusted according to ordering in nodes_1
        g_13.global_point_ind = np.array([0, 3])

        # First 1d grid, as seen from g2
        g_22 = TensorGrid(np.array([0, 2]))
        g_22.global_point_ind = np.arange(2)
        # Second 1d grid, as seen from g3
        g_33 = TensorGrid(np.array([0, 1]))
        g_33.nodes = np.array([[0, 1], [0, 1], [0, 0]])
        g_33.global_point_ind = np.arange(2)

        gl = [[[g1], [g_11, g_13]], [[g2], [g_22]], [[g3], [g_33]]]
        intersections = [np.array([1, 2]), np.array([0]), np.array([0])]

        list_of_grids, glob_ind = non_conforming.init_global_ind(gl)
        grid_list_1d = non_conforming.process_intersections(gl,
                                                            intersections,
                                                            glob_ind,
                                                            list_of_grids,
                                                            tol=1e-4)
        assert len(grid_list_1d) == 2

        g_1d = grid_list_1d[0]
        ismem, maps = ismember_rows(g_1d.global_point_ind, g1.global_point_ind)
        assert ismem.sum() == g_1d.num_nodes
        assert np.allclose(g1.nodes[:, maps], g_1d.nodes)
        ismem, maps = ismember_rows(g_1d.global_point_ind, g2.global_point_ind)
        assert ismem.sum() == g_1d.num_nodes
        assert np.allclose(g2.nodes[:, maps], g_1d.nodes)

        g_1d = grid_list_1d[1]
        ismem, maps = ismember_rows(g_1d.global_point_ind, g1.global_point_ind)
        assert ismem.sum() == g_1d.num_nodes
        assert np.allclose(g1.nodes[:, maps], g_1d.nodes)
        ismem, maps = ismember_rows(g_1d.global_point_ind, g3.global_point_ind)
        assert ismem.sum() == g_1d.num_nodes
        assert np.allclose(g3.nodes[:, maps], g_1d.nodes)
Example #8
0
    def test_merge_three_grids_no_common_point(self):
        # Merge three grids: One in the mid
        data = np.ones(3)
        rows = np.array([0, 1, 2])
        cols = np.array([0, 0, 0])
        cf_1 = sps.coo_matrix((data, (rows, cols)))

        data = np.ones(6)
        rows = np.array([0, 1, 2, 1, 3, 4])
        cols = np.array([0, 0, 0, 1, 1, 1])
        cf_2 = sps.coo_matrix((data, (rows, cols)))

        data = np.ones(6)
        rows = np.array([0, 1, 1, 2, 2, 0])
        cols = np.array([0, 0, 1, 1, 2, 2])
        fn_1 = sps.coo_matrix((data, (rows, cols)))

        data = np.ones(10)
        rows = np.array([0, 1, 1, 3, 3, 0, 1, 2, 2, 3])
        cols = np.array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4])
        fn_2 = sps.coo_matrix((data, (rows, cols)))

        nodes_1 = np.array([[0, 1, 1, 0], [0, 0, 1, 1], [0, 0, 0, 0]])
        nodes_2 = np.array([[0, 1, 0], [0, 0, -1], [0, 0, 0]])
        nodes_3 = np.array([[0, 1, 0], [2, 1, 1], [0, 0, 0]])

        # Middle grid, unit square divided into two. Will have neighbors on top and
        # bottom.
        g1 = MockGrid(2,
                      num_faces=5,
                      face_nodes=fn_2,
                      cell_faces=cf_2,
                      num_cells=2,
                      nodes=nodes_1)
        # Neighbor on bottom
        g2 = MockGrid(2,
                      num_faces=3,
                      face_nodes=fn_1,
                      cell_faces=cf_1,
                      num_cells=1,
                      nodes=nodes_2)
        # Neighbor on top.
        g3 = MockGrid(2,
                      num_faces=3,
                      face_nodes=fn_1,
                      cell_faces=cf_1,
                      num_cells=1,
                      nodes=nodes_3)

        # Bottom 1d grid, as seen from g1
        g_11 = TensorGrid(np.array([0, 1]))
        g_11.global_point_ind = np.arange(2)
        # Top 1d grid, as seen from g1
        g_13 = TensorGrid(np.array([0, 1]))
        g_13.nodes = np.array([[0, 1], [1, 1], [0, 0]])
        # Note global point indices here, in accordance with the ordering in
        # nodes_1
        g_13.global_point_ind = np.array([2, 3])

        # Bottom 1d grid, as seen from g2
        g_22 = TensorGrid(np.array([0, 1]))
        g_22.global_point_ind = np.arange(2)
        # Top 1d grid, as seen from g3
        g_33 = TensorGrid(np.array([1, 2]))
        g_33.nodes = np.array([[0, 1], [1, 1], [0, 0]])
        # Global point indices, as ordered in nodes_3
        g_33.global_point_ind = np.array([1, 2])

        gl = [[[g1], [g_11, g_13]], [[g2], [g_22]], [[g3], [g_33]]]
        intersections = [np.array([1, 2]), np.array([0]), np.array([0])]

        list_of_grids, glob_ind = non_conforming.init_global_ind(gl)
        grid_list_1d = non_conforming.process_intersections(gl,
                                                            intersections,
                                                            glob_ind,
                                                            list_of_grids,
                                                            tol=1e-4)
        assert len(grid_list_1d) == 2

        g_1d = grid_list_1d[0]
        ismem, maps = ismember_rows(g_1d.global_point_ind, g1.global_point_ind)
        assert ismem.sum() == g_1d.num_nodes
        assert np.allclose(g1.nodes[:, maps], g_1d.nodes)
        ismem, maps = ismember_rows(g_1d.global_point_ind, g2.global_point_ind)
        assert ismem.sum() == g_1d.num_nodes
        assert np.allclose(g2.nodes[:, maps], g_1d.nodes)

        g_1d = grid_list_1d[1]
        ismem, maps = ismember_rows(g_1d.global_point_ind, g1.global_point_ind)
        assert ismem.sum() == g_1d.num_nodes
        assert np.allclose(g1.nodes[:, maps], g_1d.nodes)
        ismem, maps = ismember_rows(g_1d.global_point_ind, g3.global_point_ind)
        assert ismem.sum() == g_1d.num_nodes
        assert np.allclose(g3.nodes[:, maps], g_1d.nodes)