Exemplo n.º 1
0
    def test_lines_no_crossing(self):
        p = np.array([[-1, 1, 0, 0],
                      [0, 0, -1, 1]])

        lines = np.array([[0, 1],
                          [2, 3]])
        box = np.array([[2], [2]])
        new_pts, new_lines = basics.remove_edge_crossings(p, lines, box=box)
        assert np.allclose(new_pts, p)
        assert np.allclose(new_lines, lines)
Exemplo n.º 2
0
    def test_three_lines_no_crossing(self):
        # This test gave an error at some point
        p = np.array([[ 0., 0.,   0.3, 1.,  1.,   0.5],
                      [2/3, 1/.7, 0.3, 2/3, 1/.7, 0.5]])
        lines = np.array([[0, 3], [1, 4], [2, 5]]).T
        box = np.array([[1], [2]])

        new_pts, new_lines = basics.remove_edge_crossings(p, lines, box=box)
        p_known = basics.snap_to_grid(p, box=box)
        assert np.allclose(new_pts, p_known)
        assert np.allclose(new_lines, lines)
Exemplo n.º 3
0
    def test_split_segment_partly_overlapping(self):
        p = np.array([[0, 1, 2, 3],
                      [0, 0, 0, 0]])
        lines = np.array([[0, 2], [1, 3]]).T
        box = np.array([[1], [1]])

        new_pts, new_lines = basics.remove_edge_crossings(p, lines, box=box)

        p_known = basics.snap_to_grid(p, box=box)
        lines_known = np.array([[0, 1], [1, 2], [2, 3]]).T

        assert np.allclose(new_pts, p_known)
        assert np.allclose(new_lines, lines_known)
Exemplo n.º 4
0
    def test_three_lines_one_crossing(self):
        # This test gave an error at some point
        p = np.array([[ 0., 0.5,   0.3, 1.,  0.3,  0.5],
                      [2/3, 0.3, 0.3, 2/3, 0.5, 0.5]])
        lines = np.array([[0, 3], [2, 5], [1, 4]]).T
        box = np.array([[1], [2]])

        new_pts, new_lines = basics.remove_edge_crossings(p, lines, box=box)
        p_known = np.hstack((p, np.array([[0.4], [0.4]])))
        p_known = basics.snap_to_grid(p_known, box=box)
        lines_known = np.array([[0, 3], [2, 6], [6, 5], [1, 6], [6, 4]]).T
        assert np.allclose(new_pts, p_known)
        assert np.allclose(new_lines, lines_known)
Exemplo n.º 5
0
    def test_split_segment_fully_overlapping_switched_order(self):
        p = np.array([[0, 1, 2, 3],
                      [0, 0, 0, 0]])
        lines = np.array([[0, 3], [2, 1]]).T
        box = np.array([[1], [1]])

        new_pts, new_lines = basics.remove_edge_crossings(p, lines, box=box)
        new_lines = np.sort(new_lines, axis=0)

        p_known = basics.snap_to_grid(p, box=box)
        lines_known = np.array([[0, 1], [1, 2], [2, 3]]).T

        assert np.allclose(new_pts, p_known)
        assert np.allclose(new_lines, lines_known)
Exemplo n.º 6
0
    def test_split_segment_partly_overlapping_switched_order(self):
        # Same partly overlapping test, but switch order of edge-point
        # connection. Should make no difference
        p = np.array([[0, 1, 2, 3],
                      [0, 0, 0, 0]])
        lines = np.array([[0, 2], [3, 1]]).T
        box = np.array([[1], [1]])

        new_pts, new_lines = basics.remove_edge_crossings(p, lines, box=box)

        new_lines = np.sort(new_lines, axis=0)
        p_known = basics.snap_to_grid(p, box=box)
        lines_known = np.array([[0, 1], [1, 2], [2, 3]]).T

        assert np.allclose(new_pts, p_known)
        assert np.allclose(new_lines, lines_known)
Exemplo n.º 7
0
    def test_lines_crossing_origin(self):
        p = np.array([[-1, 1, 0, 0],
                      [0, 0, -1, 1]])
        lines = np.array([[0, 2],
                          [1, 3],
                          [1, 2],
                          [3, 4]])
        box = np.array([[2], [2]])

        new_pts, new_lines = basics.remove_edge_crossings(p, lines, box=box)

        p_known = np.hstack((p, np.array([[0], [0]])))
        p_known = basics.snap_to_grid(p_known, box=box)

        lines_known = np.array([[0, 4, 2, 4],
                                [4, 1, 4, 3],
                                [1, 1, 2, 2],
                                [3, 3, 4, 4]])

        assert np.allclose(new_pts, p_known)
        assert np.allclose(new_lines, lines_known)
Exemplo n.º 8
0
def triangle_grid(fracs, domain, **kwargs):
    """
    Generate a gmsh grid in a 2D domain with fractures.

    The function uses modified versions of pygmsh and mesh_io,
    both downloaded from github.

    To be added:
    Functionality for tuning gmsh, including grid size, refinements, etc.

    Examples
    >>> p = np.array([[-1, 1, 0, 0], [0, 0, -1, 1]])
    >>> lines = np.array([[0, 2], [1, 3]])
    >>> char_h = 0.5 * np.ones(p.shape[1])
    >>> tags = np.array([1, 3])
    >>> fracs = {'points': p, 'edges': lines}
    >>> box = {'xmin': -2, 'xmax': 2, 'ymin': -2, 'ymax': 2}
    >>> path_to_gmsh = '~/gmsh/bin/gmsh'
    >>> g = create_grid(fracs, box, gmsh_path=path_to_gmsh)
    >>> plot_grid.plot_grid(g)

    Parameters
    ----------
    fracs: (dictionary) Two fields: points (2 x num_points) np.ndarray,
        lines (2 x num_lines) connections between points, defines fractures.
    box: (dictionary) keys xmin, xmax, ymin, ymax, [together bounding box
        for the domain]
    **kwargs: To be explored. Must contain the key 'gmsh_path'
    Returns
    -------
    list (length 3): For each dimension (2 -> 0), a list of all grids in 
        that dimension.
    """
    # Verbosity level
    verbose = kwargs.get('verbose', 1)

    # File name for communication with gmsh
    file_name = kwargs.get('file_name', 'gmsh_frac_file')

    in_file = file_name + '.geo'
    out_file = file_name + '.msh'

    # Pick out fracture points, and their connections
    frac_pts = fracs['points']
    frac_con = fracs['edges']

    # Unified description of points and lines for domain, and fractures
    pts_all, lines = __merge_domain_fracs_2d(domain, frac_pts, frac_con)

    # We split all fracture intersections so that the new lines do not
    # intersect, except possible at the end points
    dx = np.array([[domain['xmax'] - domain['xmin']],
                   [domain['ymax'] - domain['ymin']]])
    pts_split, lines_split = cg.remove_edge_crossings(pts_all, lines, box=dx)
    # We find the end points that is shared by more than one intersection
    intersections = __find_intersection_points(lines_split)

    # Constants used in the gmsh.geo-file
    const = constants.GmshConstants()
    # Gridding size
    lchar_dom = kwargs.get('lchar_dom', None)
    lchar_frac = kwargs.get('lchar_frac', lchar_dom)

    # gmsh options
    gmsh_path = kwargs.get('gmsh_path')

    gmsh_verbose = kwargs.get('gmsh_verbose', verbose)
    gmsh_opts = {'-v': gmsh_verbose}

    # Create a writer of gmsh .geo-files
    gw = gmsh_interface.GmshWriter(pts_split,
                                   lines_split,
                                   domain=domain,
                                   mesh_size=lchar_dom,
                                   mesh_size_bound=lchar_frac,
                                   intersection_points=intersections)
    gw.write_geo(in_file)

    # Run gmsh
    gmsh_status = gmsh_interface.run_gmsh(gmsh_path,
                                          in_file,
                                          out_file,
                                          dims=2,
                                          **gmsh_opts)

    if verbose > 0:
        start_time = time.time()
        if gmsh_status == 0:
            print('Gmsh processed file successfully')
        else:
            print('Gmsh failed with status ' + str(gmsh_status))

    pts, cells, phys_names, cell_info = mesh_io.read(out_file)

    # Create grids from gmsh mesh.
    g_2d = mesh_2_grid.create_2d_grids(pts, cells, is_embedded=False)
    g_1d, _ = mesh_2_grid.create_1d_grids(
        pts,
        cells,
        phys_names,
        cell_info,
        line_tag=const.PHYSICAL_NAME_FRACTURES)
    g_0d = mesh_2_grid.create_0d_grids(pts, cells)
    grids = [g_2d, g_1d, g_0d]

    if verbose > 0:
        print('\n')
        print('Grid creation completed. Elapsed time ' +
              str(time.time() - start_time))
        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')

    return grids