Exemple #1
0
    def test_merge_two_grids_hanging_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]])

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

        # 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

        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)
        assert len(grid_list_1d) == 1

        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)
Exemple #2
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)
Exemple #3
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)
Exemple #4
0
    def test_merge_three_grids_common_point(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(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]])
        nodes_3 = np.array([[0, 0, -1], [0, 1, 0], [0, 0, 0]])

        # Central grid
        g1 = MockGrid(2,
                      num_faces=3,
                      face_nodes=fn,
                      cell_faces=cf,
                      num_cells=1,
                      nodes=nodes_1)
        # First neighboring grid
        g2 = MockGrid(2,
                      num_faces=3,
                      face_nodes=fn,
                      cell_faces=cf,
                      num_cells=1,
                      nodes=nodes_2)
        # Second neighboring grid
        g3 = MockGrid(2,
                      num_faces=3,
                      face_nodes=fn,
                      cell_faces=cf,
                      num_cells=1,
                      nodes=nodes_3)
        # First 1d grid, as seen from g1
        g_11 = TensorGrid(np.array([0, 1]))
        g_11.global_point_ind = np.arange(2)
        # Second 1d grid, as seen from g1
        g_13 = TensorGrid(np.array([0, 1]))
        g_13.nodes = np.array([[0, 0], [0, 1], [0, 0]])
        # Point indices adjusted according to ordering in nodes_1
        g_13.global_point_ind = np.array([0, 2])

        # First 1d grid, as seen from g2
        g_22 = TensorGrid(np.array([0, 1]))
        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, 0], [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)
Exemple #5
0
    def test_merge_three_grids_internal_intersection_hanging_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(20)
        rows = np.array(
            [0, 1, 1, 2, 2, 3, 0, 4, 0, 5, 1, 4, 2, 4, 2, 5, 3, 4, 3, 5])
        cols = np.array(
            [0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9])
        fn_1 = sps.coo_matrix((data, (rows, cols)))

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

        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_2 = 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_2 = sps.coo_matrix((data, (rows, cols)))

        nodes_1 = np.array([[-1, -0.5, 0, 1, 0, 0], [0, 0, 0, 0, -1, 1],
                            [0, 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=10,
                       face_nodes=fn_1,
                       cell_faces=cf_1,
                       num_cells=5,
                       nodes=nodes_1)
        # First neighboring grid
        gxz = MockGrid(2,
                       num_faces=8,
                       face_nodes=fn_2,
                       cell_faces=cf_2,
                       num_cells=4,
                       nodes=nodes_2)
        gyz = MockGrid(2,
                       num_faces=8,
                       face_nodes=fn_2,
                       cell_faces=cf_2,
                       num_cells=4,
                       nodes=nodes_3)

        # First 1d grid, as seen from g1
        g_1x = TensorGrid(np.array([0, 1, 2, 3]))
        g_1x.nodes = np.array([[-1, -0.5, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0]])
        g_1x.global_point_ind = np.arange(4)
        # 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([4, 2, 5])

        # 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 = [[[gxz], [g_2z, g_2x]], [[gyz], [g_3z, g_3y]],
              [[gxy], [g_1x, g_1y]]]
        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,
                                    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)

        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,
                                    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[2]
        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)
 def test_refinement_grid_1d_non_uniform(self):
     x = np.array([0, 2, 6])
     g = TensorGrid(x)
     h = refinement.refine_grid_1d(g, ratio=2)
     self.assertTrue(np.allclose(h.nodes[0], np.array([0, 1, 2, 4, 6])))
 def test_grid_perturbation_1d_internal_nodes_fixed(self):
     g = TensorGrid(np.arange(4))
     h = refinement.distort_grid_1d(g, fixed_nodes=[1])
     self.assertTrue(
         np.allclose(g.nodes[:, [0, 1, 3]], h.nodes[:, [0, 1, 3]]))
 def test_grid_perturbation_1d_bound_nodes_fixed(self):
     g = TensorGrid(np.array([0, 1, 2]))
     h = refinement.distort_grid_1d(g)
     self.assertTrue(np.allclose(g.nodes[:, [0, 2]], h.nodes[:, [0, 2]]))
Exemple #9
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))

        print('\n')
        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'
                print(s)
        print('\n')

    tag_faces(grids, check_highest_dim=False)
    logger.warn('Assemble in bucket')
    tic = time.time()
    gb = assemble_in_bucket(grids)
    logger.warn('Done. Elapsed time ' + str(time.time() - tic))
    logger.warn('Compute geometry')
    tic = time.time()
    gb.compute_geometry()
    logger.warn('Done. Elapsed time ' + str(time.time() - tic))
    logger.warn('Split fractures')
    tic = time.time()
    split_grid.split_fractures(gb)
    logger.warn('Done. Elapsed time ' + str(time.time() - tic))
    return gb