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