示例#1
0
def plot_line_triangle(mesh: MeshContainer, label: str, **kwargs):
    """Plot line/triangle mesh together."""
    assert set(mesh.cells_dict.keys()) == {'line', 'triangle'}
    fig, (ax1, ax2) = plt.subplots(ncols=2)
    mesh.plot_mpl('line', ax=ax1, label=label, **kwargs)
    mesh.plot_mpl('triangle', ax=ax2, label=label, **kwargs)
    return fig, (ax1, ax2)
示例#2
0
def sample_mesh():
    expected_fn = Path(__file__).parent / 'segmented_mesh_2d.msh'
    mesh = MeshContainer.read(expected_fn)

    sample_mesh = mesh.get('triangle')

    return sample_mesh
示例#3
0
def test_write_read(mesh_square2d, tmp_path):
    def asserts(mesh):
        assert mesh.points.shape[1] == 2
        assert 'physical' in mesh.cell_data
        assert 'gmsh:physical' not in mesh.cell_data
        assert 'gmsh:geometrical' not in mesh.cell_data

    filename = tmp_path / 'out.msh'

    asserts(mesh_square2d)

    mesh_square2d.write(filename, file_format='gmsh22')
    asserts(mesh_square2d)

    new_mesh = MeshContainer.read(filename)
    asserts(new_mesh)

    np.testing.assert_equal(mesh_square2d.points, new_mesh.points)

    assert len(mesh_square2d.cells) == len(new_mesh.cells)

    for (left, right) in zip(mesh_square2d.cells, new_mesh.cells):
        np.testing.assert_equal(left.data, right.data)

    for key, arrays in mesh_square2d.cell_data.items():
        new_arrays = new_mesh.cell_data[key]

        for left, right in zip(arrays, new_arrays):
            np.testing.assert_equal(left, right)
示例#4
0
def compare_mesh_results(mesh_container, expected_fn):
    """`result_mesh` is an instance of TetraMesh, and `expected_fn` the
    filename of the mesh to compare to."""
    if expected_fn.exists():
        expected_mesh_container = MeshContainer.read(expected_fn)
    else:
        mesh_container.write(expected_fn, file_format='gmsh22', binary=False)
        raise RuntimeError(f'Wrote expected mesh to {expected_fn.absolute()}')

    cell_type = 'tetra'

    mesh = mesh_container.get(cell_type)
    expected_mesh = expected_mesh_container.get(cell_type)

    assert mesh.points.shape == expected_mesh.points.shape
    assert mesh.cells.shape == expected_mesh.cells.shape
    np.testing.assert_allclose(mesh.points, expected_mesh.points)
    np.testing.assert_allclose(mesh.cells, expected_mesh.cells)

    np.testing.assert_allclose(mesh.region_markers,
                               expected_mesh.region_markers)

    for key in expected_mesh.cell_data:
        try:
            np.testing.assert_allclose(mesh.cell_data[key],
                                       expected_mesh.cell_data[key])
        except KeyError:
            if key not in ('physical', 'geometrical'):
                raise
示例#5
0
def test_generate_2d_mesh(segmented):
    """Test 2D mesh generation and plot."""
    expected_fn = Path(__file__).parent / 'segmented_mesh_2d.msh'

    np.random.seed(1234)  # set seed for reproducible clustering
    mesh = generate_2d_mesh(segmented, max_contour_dist=4, plot=True)

    if expected_fn.exists():
        expected_mesh = MeshContainer.read(expected_fn)
    else:
        mesh.write(expected_fn, file_format='gmsh22', binary=False)

        raise RuntimeError(f'Wrote expected mesh to {expected_fn.absolute()}')

    assert mesh.points.shape[1] == 2
    assert mesh.points.shape == expected_mesh.points.shape
    np.testing.assert_allclose(mesh.points, expected_mesh.points)

    cell_types = mesh.cells_dict.keys()
    assert cell_types == expected_mesh.cells_dict.keys()

    for cell_type in cell_types:
        cells = mesh.cells_dict[cell_type]
        expected_cells = expected_mesh.cells_dict[cell_type]

        assert cells.shape == expected_cells.shape
        np.testing.assert_allclose(cells, expected_cells)

    data_keys = mesh.cell_data_dict.keys()
    for data_key in data_keys:
        for cell_type in cell_types:
            data = mesh.cell_data_dict[data_key][cell_type]
            expected_data = expected_mesh.cell_data_dict[data_key][cell_type]

            np.testing.assert_allclose(data, expected_data)
示例#6
0
def test_tetgen_generate_3d_mesh(triangle_mesh):
    """Test 3D mesh generation."""
    expected_fn = Path(__file__).parent / 'expected_tetra_mesh.msh'

    triangle_mesh.add_region_marker((10, np.array([0.5, 0.5, 0.5])))
    triangle_mesh.add_region_marker((20, np.array([0.0, 2.0, 2.0])))

    mesh_container = triangle_mesh.tetrahedralize()

    if expected_fn.exists():
        expected_mesh_container = MeshContainer.read(expected_fn)
    else:
        mesh_container.write(expected_fn, file_format='gmsh22', binary=False)

        raise RuntimeError(f'Wrote expected mesh to {expected_fn.absolute()}')

    cell_type = 'tetra'

    mesh = mesh_container.get(cell_type)
    expected_mesh = expected_mesh_container.get(cell_type)

    assert mesh.points.shape == expected_mesh.points.shape
    assert mesh.cells.shape == expected_mesh.cells.shape
    np.testing.assert_allclose(mesh.points, expected_mesh.points)
    np.testing.assert_allclose(mesh.cells, expected_mesh.cells)

    np.testing.assert_allclose(mesh.region_markers,
                               expected_mesh.region_markers)
示例#7
0
文件: helpers.py 项目: hpgem/nanomesh
def simple_triangulate(points: np.ndarray,
                       *,
                       segments: np.ndarray = None,
                       regions: np.ndarray = None,
                       opts: str = '') -> MeshContainer:
    """Simple triangulation using `triangle`.

    Parameters
    ----------
    points : i,2 np.ndarray
        Vertex coordinates.
    segments : j,2 np.ndarray, optional
        Index array describing segments.
        Segments are edges whose presence in the triangulation
        is enforced (although each segment may be subdivided into smaller
        edges). Each segment is specified by listing the indices of its
        two endpoints. A closed set of segments describes a contour.
    regions : k,2 np.ndarray, optional
        Coordinates describing regions. A region is a coordinate inside
        (e.g. at the center) of a region/contour (i.e. enclosed by segments).
    opts : str, optional
        Additional options passed to `triangle.triangulate` documented here:
        https://rufat.be/triangle/API.html#triangle.triangulate

    Returns
    -------
    mesh : MeshContainer
        Triangle mesh
    """
    from nanomesh.mesh_container import MeshContainer

    triangle_dict_in = {'vertices': points}

    if segments is not None:
        triangle_dict_in['segments'] = segments

    if regions is not None:
        triangle_dict_in['regions'] = regions

    triangle_dict_out = tr.triangulate(triangle_dict_in, opts=opts)

    mesh = MeshContainer.from_triangle_dict(triangle_dict_out)

    return mesh
示例#8
0
def test_mesh_container_triangle_2d(triangle_mesh_2d):
    mesh = MeshContainer.from_mesh(triangle_mesh_2d)
    assert isinstance(mesh, MeshContainer)
    assert isinstance(mesh.get('triangle'), TriangleMesh)
    assert isinstance(mesh.get(), TriangleMesh)
    assert isinstance(mesh, meshio.Mesh)
示例#9
0
def test_mesh_container_tetra(tetra_mesh):
    mesh = MeshContainer.from_mesh(tetra_mesh)
    assert isinstance(mesh, MeshContainer)
    assert isinstance(mesh.get('tetra'), TetraMesh)
    assert isinstance(mesh.get(), TetraMesh)
    assert isinstance(mesh, meshio.Mesh)