def test_flip_delaunay(): mesh = meshio.read(this_dir / "meshes" / "pacman.vtk") numpy.random.seed(123) mesh.points[:, :2] += 5.0e-2 * numpy.random.rand(*mesh.points[:, :2].shape) mesh = meshplex.MeshTri(mesh.points, mesh.get_cells_type("triangle")) assert mesh.num_delaunay_violations() == 16 mesh.flip_until_delaunay() assert mesh.num_delaunay_violations() == 0 # Assert edges_cells integrity for cell_gid, edge_gids in enumerate(mesh.cells["edges"]): for edge_gid in edge_gids: num_adj_cells, edge_id = mesh._edge_gid_to_edge_list[edge_gid] assert cell_gid in mesh._edges_cells[num_adj_cells][edge_id] new_cells = mesh.cells["nodes"].copy() new_coords = mesh.node_coords.copy() # Assert that some key values are updated properly mesh2 = meshplex.MeshTri(new_coords, new_cells) assert numpy.all(mesh.idx_hierarchy == mesh2.idx_hierarchy) tol = 1.0e-15 assert near_equal(mesh.half_edge_coords, mesh2.half_edge_coords, tol) assert near_equal(mesh.cell_volumes, mesh2.cell_volumes, tol) assert near_equal(mesh.ei_dot_ej, mesh2.ei_dot_ej, tol)
def test_degenerate_small0b(h): points = numpy.array([[0, 0, 0], [1, 0, 0], [0.5, h, 0.0]]) cells = numpy.array([[0, 1, 2]]) mesh = meshplex.MeshTri(points, cells) tol = 1.0e-14 # edge lengths el = numpy.sqrt(0.5**2 + h**2) assert near_equal(mesh.edge_lengths.T, [el, el, 1.0], tol) # ce_ratios ce0 = 0.5 / h * (h**2 - 0.25) ce12 = 0.25 / h assert near_equal(mesh.ce_ratios.T, [ce12, ce12, ce0], tol) # control volumes cv12 = 0.25 * (1.0**2 * ce0 + (0.25 + h**2) * ce12) cv0 = 0.5 * (0.25 + h**2) * ce12 assert near_equal(mesh.control_volumes, [cv12, cv12, cv0], tol) # cell volumes assert near_equal(mesh.cell_volumes, [0.5 * h], tol) # circumcenters assert near_equal(mesh.cell_circumcenters, [0.5, 0.375, 0.0], tol) assert mesh.num_delaunay_violations() == 0 return
def test_shell(): points = numpy.array( [ [+0.0, +0.0, +1.0], [+1.0, +0.0, +0.0], [+0.0, +1.0, +0.0], [-1.0, +0.0, +0.0], [+0.0, -1.0, +0.0], ] ) cells = numpy.array([[0, 1, 2], [0, 2, 3], [0, 3, 4], [0, 1, 4]]) mesh = meshplex.MeshTri(points, cells) tol = 1.0e-14 ce_ratios = 0.5 / numpy.sqrt(3.0) * numpy.ones((4, 3)) assert near_equal(mesh.ce_ratios.T, ce_ratios, tol) cv = numpy.array([2.0, 1.0, 1.0, 1.0, 1.0]) / numpy.sqrt(3.0) assert near_equal(mesh.control_volumes, cv, tol) cell_vols = numpy.sqrt(3.0) / 2.0 * numpy.ones(4) assert near_equal(mesh.cell_volumes, cell_vols, tol) assert mesh.num_delaunay_violations() == 0 return
def test_angles(): # 3-4-5 triangle points = numpy.array([[0.0, 0.0, 0.0], [3.0, 0.0, 0.0], [0.0, 4.0, 0.0]]) cells = numpy.array([[0, 1, 2]]) mesh = meshplex.MeshTri(points, cells) tol = 1.0e-14 assert near_equal( mesh.angles, [[numpy.pi / 2], [numpy.arcsin(4.0 / 5.0)], [numpy.arcsin(3.0 / 5.0)]], tol, ) # 30-60-90 triangle a = 1.0 points = numpy.array( [[0.0, 0.0, 0.0], [a / 2, 0.0, 0.0], [0.0, a / 2 * numpy.sqrt(3.0), 0.0]] ) cells = numpy.array([[0, 1, 2]]) mesh = meshplex.MeshTri(points, cells) ic = mesh.angles / numpy.pi * 180 assert near_equal(ic, [[90], [60], [30]], tol) return
def test_cubesmall(): points = numpy.array([ [-0.5, -0.5, -5.0], [-0.5, +0.5, -5.0], [+0.5, -0.5, -5.0], [-0.5, -0.5, +5.0], [+0.5, +0.5, -5.0], [+0.5, +0.5, +5.0], [-0.5, +0.5, +5.0], [+0.5, -0.5, +5.0], ]) cells = numpy.array([[0, 1, 2, 3], [1, 2, 4, 5], [1, 2, 3, 5], [1, 3, 5, 6], [2, 3, 5, 7]]) mesh = meshplex.MeshTetra(points, cells) tol = 1.0e-14 cv = numpy.ones(8) * 1.25 cellvols = [5.0 / 3.0, 5.0 / 3.0, 10.0 / 3.0, 5.0 / 3.0, 5.0 / 3.0] assert near_equal(mesh.control_volumes, cv, tol) assert near_equal(mesh.cell_volumes, cellvols, tol) cv_norms = [ numpy.linalg.norm(cv, ord=2), numpy.linalg.norm(cv, ord=numpy.Inf) ] cellvol_norms = [ numpy.linalg.norm(cellvols, ord=2), numpy.linalg.norm(cellvols, ord=numpy.Inf), ] run(mesh, 10.0, cv_norms, [28.095851618771825, 1.25], cellvol_norms) return
def test_regular_tri_order(): points = numpy.array([[0.0, 1.0, 0.0], [0.0, 0.0, 0.0], [1.0, 0.0, 0.0]]) cells = numpy.array([[0, 1, 2]]) mesh = meshplex.MeshTri(points, cells) assert all((mesh.cells["nodes"] == [0, 1, 2]).flat) tol = 1.0e-14 # ce_ratios assert near_equal(mesh.ce_ratios.T, [0.5, 0.0, 0.5], tol) # control volumes assert near_equal(mesh.control_volumes, [0.125, 0.25, 0.125], tol) # cell volumes assert near_equal(mesh.cell_volumes, [0.5], tol) # circumcenters assert near_equal(mesh.cell_circumcenters, [0.5, 0.5, 0.0], tol) # centroids assert near_equal( mesh.control_volume_centroids, [[1.0 / 6.0, 2.0 / 3.0, 0.0], [0.25, 0.25, 0.0], [2.0 / 3.0, 1.0 / 6.0, 0.0]], tol, ) assert mesh.num_delaunay_violations() == 0 return
def test_regular_tri_order(): points = numpy.array([ [0.0, 1.0, 0.0], [0.0, 0.0, 0.0], [1.0, 0.0, 0.0], ]) cells = numpy.array([[0, 1, 2]]) mesh = voropy.mesh_tri.MeshTri(points, cells) assert all((mesh.cells['nodes'] == [0, 1, 2]).flat) tol = 1.0e-14 # ce_ratios assert near_equal(mesh.get_ce_ratios_per_edge().T, [0.5, 0.0, 0.5], tol) # control volumes assert near_equal(mesh.get_control_volumes(), [0.125, 0.25, 0.125], tol) # cell volumes assert near_equal(mesh.cell_volumes, [0.5], tol) # circumcenters assert near_equal(mesh.get_cell_circumcenters(), [0.5, 0.5, 0.0], tol) # centroids assert near_equal(mesh.get_control_volume_centroids(), [ [1.0 / 6.0, 2.0 / 3.0, 0.0], [0.25, 0.25, 0.0], [2.0 / 3.0, 1.0 / 6.0, 0.0], ], tol) assert mesh.num_delaunay_violations() == 0 return
def test_regular_tri2(a): points = (numpy.array([ [-0.5, -0.5 * numpy.sqrt(3.0), 0], [-0.5, +0.5 * numpy.sqrt(3.0), 0], [1, 0, 0], ]) / numpy.sqrt(3) * a) cells = numpy.array([[0, 1, 2]]) mesh = meshplex.MeshTri(points, cells) tol = 1.0e-14 # ce_ratios val = 0.5 / numpy.sqrt(3.0) assert near_equal(mesh.ce_ratios, [val, val, val], tol) # control volumes vol = numpy.sqrt(3.0) / 4 * a**2 assert near_equal(mesh.control_volumes, [vol / 3.0, vol / 3.0, vol / 3.0], tol) # cell volumes assert near_equal(mesh.cell_volumes, [vol], tol) # circumcenters assert near_equal(mesh.cell_circumcenters, [0.0, 0.0, 0.0], tol) return
def test_degenerate_small2(h): points = numpy.array([[0, 0, 0], [1, 0, 0], [0.5, h, 0.0], [0.5, -h, 0.0]]) cells = numpy.array([[0, 1, 2], [0, 1, 3]]) mesh = meshplex.MeshTri(points, cells) tol = 1.0e-11 # ce_ratios alpha = h - 1.0 / (4 * h) beta = 1.0 / (4 * h) assert near_equal(mesh.ce_ratios_per_interior_edge, [alpha], tol) alpha2 = (h - 1.0 / (4 * h)) / 2 assert near_equal(mesh.ce_ratios, [[beta, beta], [beta, beta], [alpha2, alpha2]], tol) # control volumes alpha1 = 0.125 * (3 * h - 1.0 / (4 * h)) alpha2 = 0.125 * (h + 1.0 / (4 * h)) assert near_equal(mesh.control_volumes, [alpha1, alpha1, alpha2, alpha2], tol) # circumcenters assert near_equal(mesh.cell_circumcenters, [[0.5, -12.495, 0.0], [0.5, +12.495, 0.0]], tol) # cell volumes assert near_equal(mesh.cell_volumes, [0.5 * h, 0.5 * h], tol) assert mesh.num_delaunay_violations() == 1 return
def test_flip_delaunay(): filename = download_mesh("pacman.msh", "2da8ff96537f844a95a83abb48471b6a") mesh = meshio.read(filename) numpy.random.seed(123) mesh.points[:, :2] += 5.0e-2 * numpy.random.rand(*mesh.points[:, :2].shape) mesh = meshplex.MeshTri(mesh.points, mesh.cells["triangle"]) assert mesh.num_delaunay_violations() == 16 mesh.flip_until_delaunay() assert mesh.num_delaunay_violations() == 0 # Assert edges_cells integrity for cell_gid, edge_gids in enumerate(mesh.cells["edges"]): for edge_gid in edge_gids: num_adj_cells, edge_id = mesh._edge_gid_to_edge_list[edge_gid] assert cell_gid in mesh._edges_cells[num_adj_cells][edge_id] new_cells = mesh.cells["nodes"].copy() new_coords = mesh.node_coords.copy() # Assert that some key values are updated properly mesh2 = meshplex.MeshTri(new_coords, new_cells) assert numpy.all(mesh.idx_hierarchy == mesh2.idx_hierarchy) tol = 1.0e-15 assert near_equal(mesh.half_edge_coords, mesh2.half_edge_coords, tol) assert near_equal(mesh.cell_volumes, mesh2.cell_volumes, tol) assert near_equal(mesh.ei_dot_ej, mesh2.ei_dot_ej, tol) return
def test_regular_tri_additional_points(): points = numpy.array([ [0.0, 3.4, 0.0], [0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [3.3, 4.4, 0.0], ]) cells = numpy.array([[1, 2, 3]]) mesh = meshplex.MeshTri(points, cells) mesh.mark_boundary() assert numpy.array_equal(mesh.node_is_used, [False, True, True, True, False]) assert numpy.array_equal(mesh.is_boundary_node, [False, True, True, True, False]) assert numpy.array_equal(mesh.is_interior_node, [False, False, False, False, False]) tol = 1.0e-14 assert numpy.array_equal(mesh.cells["nodes"], [[1, 2, 3]]) mesh.create_edges() assert numpy.array_equal(mesh.cells["edges"], [[2, 1, 0]]) assert numpy.array_equal(mesh.edges["nodes"], [[1, 2], [1, 3], [2, 3]]) # ce_ratios assert near_equal(mesh.ce_ratios.T, [0.0, 0.5, 0.5], tol) # control volumes assert near_equal(mesh.control_volumes, [0.0, 0.25, 0.125, 0.125, 0.0], tol) # cell volumes assert near_equal(mesh.cell_volumes, [0.5], tol) # circumcenters assert near_equal(mesh.cell_circumcenters, [0.5, 0.5, 0.0], tol) # Centroids. # Nans appear here as the some points aren't part of any cell and hence have no # control volume. cvc = mesh.control_volume_centroids assert numpy.all(numpy.isnan(cvc[0])) assert numpy.all(numpy.isnan(cvc[4])) assert near_equal( cvc[1:4], [[0.25, 0.25, 0.0], [2.0 / 3.0, 1.0 / 6.0, 0.0], [1.0 / 6.0, 2.0 / 3.0, 0.0]], tol, ) assert mesh.num_delaunay_violations() == 0 return
def test_unit_tetrahedron_geometric(a): points = numpy.array([[0, 0, 0], [a, 0, 0], [0, a, 0], [0, 0, a]]) cells = numpy.array([[0, 1, 2, 3]]) tol = 1.0e-14 mesh = meshplex.MeshTetra(points, cells) assert all((mesh.cells["nodes"] == cells).flat) assert near_equal(mesh.cell_circumcenters, [a / 2.0, a / 2.0, a / 2.0], tol) # covolume/edge length ratios ref = numpy.array([ [[-a / 24.0], [a / 8.0], [a / 8.0], [0.0]], [[-a / 24.0], [a / 8.0], [0.0], [a / 8.0]], [[-a / 24.0], [0.0], [a / 8.0], [a / 8.0]], ]) assert near_equal(mesh.ce_ratios, ref, tol) # cell volumes assert near_equal(mesh.cell_volumes, [a**3 / 6.0], tol) # control volumes assert near_equal( mesh.control_volumes, [a**3 / 8.0, a**3 / 72.0, a**3 / 72.0, a**3 / 72.0], tol, ) assert near_equal( mesh.circumcenter_face_distances.T, [-0.5 / numpy.sqrt(3) * a, 0.5 * a, 0.5 * a, 0.5 * a], tol, ) # inradius ref = a * 0.2113248654051872 assert near_equal(mesh.cell_inradius, [ref], tol) # circumradius ref = a * numpy.sqrt(3) / 2 assert near_equal(mesh.cell_circumradius, [ref], tol) # cell quality ref = 7.320508075688774e-01 assert near_equal(mesh.q_radius_ratio, [ref], tol) assert near_equal(mesh.q_min_sin_dihedral_angles, [numpy.sqrt(3) / 2], tol) assert near_equal(mesh.q_vol_rms_edgelength3, [4 * numpy.sqrt(3) / 9], tol) return
def test_update_node_coordinates(): mesh = meshio.read(this_dir / "meshes" / "pacman.vtk") assert numpy.all(numpy.abs(mesh.points[:, 2]) < 1.0e-15) mesh1 = meshplex.MeshTri(mesh.points, mesh.get_cells_type("triangle")) numpy.random.seed(123) X2 = mesh.points + 1.0e-2 * numpy.random.rand(*mesh.points.shape) mesh2 = meshplex.MeshTri(X2, mesh.get_cells_type("triangle")) mesh1.node_coords = X2 mesh1.update_values() tol = 1.0e-12 assert near_equal(mesh1.ei_dot_ej, mesh2.ei_dot_ej, tol) assert near_equal(mesh1.cell_volumes, mesh2.cell_volumes, tol)
def test_rectanglesmall(): points = numpy.array([[0.0, 0.0, 0.0], [10.0, 0.0, 0.0], [10.0, 1.0, 0.0], [0.0, 1.0, 0.0]]) cells = numpy.array([[0, 1, 2], [0, 2, 3]]) mesh = voropy.mesh_tri.MeshTri(points, cells) tol = 1.0e-14 assert near_equal(mesh.get_ce_ratios_per_edge(), [0.05, 0.0, 5.0, 5.0, 0.05], tol) assert near_equal(mesh.get_control_volumes(), [2.5, 2.5, 2.5, 2.5], tol) assert near_equal(mesh.cell_volumes, [5.0, 5.0], tol) assert mesh.num_delaunay_violations() == 0 return
def test_rectanglesmall(): points = numpy.array([[0.0, 0.0, 0.0], [10.0, 0.0, 0.0], [10.0, 1.0, 0.0], [0.0, 1.0, 0.0]]) cells = numpy.array([[0, 1, 2], [0, 2, 3]]) mesh = meshplex.MeshTri(points, cells) tol = 1.0e-14 assert near_equal(mesh.ce_ratios_per_interior_edge, [0.0], tol) assert near_equal(mesh.ce_ratios, [[5.0, 0.05], [0.0, 5.0], [0.05, 0.0]], tol) assert near_equal(mesh.control_volumes, [2.5, 2.5, 2.5, 2.5], tol) assert near_equal(mesh.cell_volumes, [5.0, 5.0], tol) assert mesh.num_delaunay_violations() == 0
def test_regular_tri(): points = numpy.array([[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]) cells = numpy.array([[0, 1, 2]]) mesh = voropy.mesh_tri.MeshTri(points, cells) tol = 1.0e-14 assert (mesh.local_idx.T == [[1, 2], [2, 0], [0, 1]]).all() assert mesh.local_idx_inv == [[(0, 2), (1, 1)], [(0, 0), (1, 2)], [(0, 1), (1, 0)]] # ce_ratios assert near_equal(mesh.get_ce_ratios().T, [0.0, 0.5, 0.5], tol) # control volumes assert near_equal(mesh.get_control_volumes(), [0.25, 0.125, 0.125], tol) # cell volumes assert near_equal(mesh.cell_volumes, [0.5], tol) # circumcenters assert near_equal(mesh.get_cell_circumcenters(), [0.5, 0.5, 0.0], tol) # centroids assert near_equal(mesh.get_control_volume_centroids(), [ [0.25, 0.25, 0.0], [2.0 / 3.0, 1.0 / 6.0, 0.0], [1.0 / 6.0, 2.0 / 3.0, 0.0], ], tol) assert mesh.num_delaunay_violations() == 0 mesh.get_cell_mask() mesh.get_edge_mask() mesh.get_vertex_mask() # dummy subdomain marker test class Subdomain(object): is_boundary_only = False def is_inside(self, X): return numpy.ones(X.shape[1:], dtype=bool) cell_mask = mesh.get_cell_mask(Subdomain()) assert sum(cell_mask) == 1 return
def test_update_node_coordinates(): filename = download_mesh("pacman.vtk", "c621cb22f8b87cecd77724c2c0601c36") mesh = meshio.read(filename) assert numpy.all(numpy.abs(mesh.points[:, 2]) < 1.0e-15) mesh1 = meshplex.MeshTri(mesh.points, mesh.get_cells_type("triangle")) numpy.random.seed(123) X2 = mesh.points + 1.0e-2 * numpy.random.rand(*mesh.points.shape) mesh2 = meshplex.MeshTri(X2, mesh.get_cells_type("triangle")) mesh1.node_coords = X2 mesh1.update_values() tol = 1.0e-12 assert near_equal(mesh1.ei_dot_ej, mesh2.ei_dot_ej, tol) assert near_equal(mesh1.cell_volumes, mesh2.cell_volumes, tol)
def test_regular_tet1_geometric_order(): a = 1.0 points = numpy.array([ [0, 0, a], [0, 0, 0], [a, 0, 0], [0, a, 0], ]) cells = numpy.array([[0, 1, 2, 3]]) tol = 1.0e-14 mesh = voropy.mesh_tetra.MeshTetra(points, cells, mode='geometric') assert all((mesh.cells['nodes'] == [0, 1, 2, 3]).flat) assert near_equal( mesh.get_cell_circumcenters(), [a/2.0, a/2.0, a/2.0], tol ) # covolume/edge length ratios ref = numpy.array([ [[0.0], [-a/24.0], [a/8.0], [a/8.0]], [[a/8.0], [-a/24.0], [a/8.0], [0.0]], [[a/8.0], [-a/24.0], [0.0], [a/8.0]], ]) assert near_equal(mesh.ce_ratios, ref, tol) # cell volumes assert near_equal(mesh.cell_volumes, [a**3/6.0], tol) # control volumes assert near_equal( mesh.get_control_volumes(), [a**3/72.0, a**3/8.0, a**3/72.0, a**3/72.0], tol ) assert near_equal( mesh.circumcenter_face_distances.T, [0.5*a, -0.5/numpy.sqrt(3)*a, 0.5*a, 0.5*a], tol ) return
def test_degenerate_small1(h, a): points = numpy.array([ [0, 0, 0], [1, 0, 0], [a, h, 0.0], ]) cells = numpy.array([[0, 1, 2]]) mesh = voropy.mesh_tri.MeshTri(points, cells, flat_cell_correction='full') tol = 1.0e-14 # edge lengths el1 = numpy.sqrt(a**2 + h**2) el2 = numpy.sqrt((1.0 - a)**2 + h**2) assert near_equal(mesh.get_edge_lengths().T, [[el2, el1, 1.0]], tol) # ce_ratios ce1 = 0.5 * h / a ce2 = 0.5 * h / (1.0 - a) assert near_equal(mesh.get_ce_ratios_per_edge(), [0.0, ce1, ce2], tol) # control volumes cv1 = ce1 * el1 alpha1 = 0.25 * el1 * cv1 cv2 = ce2 * el2 alpha2 = 0.25 * el2 * cv2 beta = 0.5 * h - (alpha1 + alpha2) assert near_equal(mesh.get_control_volumes(), [alpha1, alpha2, beta], tol) assert abs(sum(mesh.get_control_volumes()) - 0.5 * h) < tol # cell volumes assert near_equal(mesh.cell_volumes, [0.5 * h], tol) # surface areas b1 = numpy.sqrt((0.5 * el1)**2 + cv1**2) alpha0 = b1 + 0.5 * el1 b2 = numpy.sqrt((0.5 * el2)**2 + cv2**2) alpha1 = b2 + 0.5 * el2 total = 1.0 + el1 + el2 alpha2 = total - alpha0 - alpha1 surf = mesh.get_surface_areas() assert near_equal(surf, [alpha0, alpha1, alpha2], tol) assert mesh.num_delaunay_violations() == 0 return
def test_circumradius(): # 3-4-5 triangle points = numpy.array([[0.0, 0.0, 0.0], [3.0, 0.0, 0.0], [0.0, 4.0, 0.0]]) cells = numpy.array([[0, 1, 2]]) mesh = meshplex.MeshTri(points, cells) tol = 1.0e-15 assert near_equal(mesh.cell_circumradius, [2.5], tol) # 30-60-90 triangle a = 1.0 points = numpy.array([[0.0, 0.0, 0.0], [a / 2, 0.0, 0.0], [0.0, a / 2 * numpy.sqrt(3.0), 0.0]]) cells = numpy.array([[0, 1, 2]]) mesh = meshplex.MeshTri(points, cells) assert near_equal(mesh.cell_circumradius, [a / 2], tol)
def test_update_node_coordinates(): filename = download_mesh("pacman.msh", "2da8ff96537f844a95a83abb48471b6a") mesh = meshio.read(filename) assert numpy.all(numpy.abs(mesh.points[:, 2]) < 1.0e-15) mesh1 = meshplex.MeshTri(mesh.points, mesh.cells["triangle"]) numpy.random.seed(123) X2 = mesh.points + 1.0e-2 * numpy.random.rand(*mesh.points.shape) mesh2 = meshplex.MeshTri(X2, mesh.cells["triangle"]) mesh1.node_coords = X2 mesh1.update_values() tol = 1.0e-12 assert near_equal(mesh1.ei_dot_ej, mesh2.ei_dot_ej, tol) assert near_equal(mesh1.cell_volumes, mesh2.cell_volumes, tol) return
def test_degenerate_small1(h, a): points = numpy.array([[0, 0, 0], [1, 0, 0], [a, h, 0.0]]) cells = numpy.array([[0, 1, 2]]) mesh = meshplex.MeshTri(points, cells) tol = 1.0e-12 # edge lengths el0 = numpy.sqrt((1.0 - a)**2 + h**2) el1 = numpy.sqrt(a**2 + h**2) el2 = 1.0 assert near_equal(mesh.edge_lengths.T, [[el0, el1, el2]], tol) # ce_ratios ce0 = 0.5 * a / h ce1 = 0.5 * (1 - a) / h ce2 = 0.5 * (h - (1 - a) * a / h) / el2 assert near_equal(mesh.ce_ratios[:, 0], [ce0, ce1, ce2], 1.0e-8) # # control volumes # cv1 = ce1 * el1 # alpha1 = 0.25 * el1 * cv1 # cv2 = ce2 * el2 # alpha2 = 0.25 * el2 * cv2 # beta = 0.5 * h - (alpha1 + alpha2) # assert near_equal(mesh.control_volumes, [alpha1, alpha2, beta], tol) # assert abs(sum(mesh.control_volumes) - 0.5 * h) < tol # cell volumes assert near_equal(mesh.cell_volumes, [0.5 * h], tol) # # surface areas # b1 = numpy.sqrt((0.5 * el1) ** 2 + cv1 ** 2) # alpha0 = b1 + 0.5 * el1 # b2 = numpy.sqrt((0.5 * el2) ** 2 + cv2 ** 2) # alpha1 = b2 + 0.5 * el2 # total = 1.0 + el1 + el2 # alpha2 = total - alpha0 - alpha1 # assert near_equal(mesh.surface_areas, [alpha0, alpha1, alpha2], tol) assert mesh.num_delaunay_violations() == 0 return
def test_quality(): # 3-4-5 triangle points = numpy.array([[0.0, 0.0, 0.0], [3.0, 0.0, 0.0], [0.0, 4.0, 0.0]]) cells = numpy.array([[0, 1, 2]]) mesh = meshplex.MeshTri(points, cells) tol = 1.0e-15 q = mesh.cell_quality assert near_equal(q, 2 * mesh.cell_inradius / mesh.cell_circumradius, tol) # 30-60-90 triangle a = 1.0 points = numpy.array([[0.0, 0.0, 0.0], [a / 2, 0.0, 0.0], [0.0, a / 2 * numpy.sqrt(3.0), 0.0]]) cells = numpy.array([[0, 1, 2]]) mesh = meshplex.MeshTri(points, cells) q = mesh.cell_quality assert near_equal(q, 2 * mesh.cell_inradius / mesh.cell_circumradius, tol)
def test_regular_tet1_geometric(a): points = numpy.array([[0, 0, 0], [a, 0, 0], [0, a, 0], [0, 0, a]]) cells = numpy.array([[0, 1, 2, 3]]) tol = 1.0e-14 mesh = meshplex.MeshTetra(points, cells, mode="geometric") assert all((mesh.cells["nodes"] == cells).flat) assert near_equal(mesh.cell_circumcenters, [a / 2.0, a / 2.0, a / 2.0], tol) # covolume/edge length ratios ref = numpy.array([ [[-a / 24.0], [a / 8.0], [a / 8.0], [0.0]], [[-a / 24.0], [a / 8.0], [0.0], [a / 8.0]], [[-a / 24.0], [0.0], [a / 8.0], [a / 8.0]], ]) assert near_equal(mesh.ce_ratios, ref, tol) # cell volumes assert near_equal(mesh.cell_volumes, [a**3 / 6.0], tol) # control volumes assert near_equal( mesh.control_volumes, [a**3 / 8.0, a**3 / 72.0, a**3 / 72.0, a**3 / 72.0], tol, ) assert near_equal( mesh.circumcenter_face_distances.T, [-0.5 / numpy.sqrt(3) * a, 0.5 * a, 0.5 * a, 0.5 * a], tol, ) return
def test_degenerate_small0b_fcc(): h = 1.0e-3 points = numpy.array([ [0, 0, 0], [1, 0, 0], [0.5, h, 0.0], ]) cells = numpy.array([[0, 1, 2]]) mesh = voropy.mesh_tri.MeshTri(points, cells, flat_cell_correction='full') tol = 1.0e-14 # edge lengths el = numpy.sqrt(0.5**2 + h**2) assert near_equal(mesh.get_edge_lengths().T, [el, el, 1.0], tol) # ce_ratios ce = h assert near_equal(mesh.get_ce_ratios().T, [ce, ce, 0.0], tol) # control volumes cv = ce * el alpha = 0.25 * el * cv beta = 0.5 * h - 2 * alpha assert near_equal(mesh.get_control_volumes(), [alpha, alpha, beta], tol) # cell volumes assert near_equal(mesh.cell_volumes, [0.5 * h], tol) # surface areas g = numpy.sqrt((0.5 * el)**2 + (ce * el)**2) alpha = 0.5 * el + g beta = el + (1.0 - 2 * g) assert near_equal(mesh.get_surface_areas(), [alpha, alpha, beta], tol) # centroids centroids = mesh.get_control_volume_centroids() alpha = 1.0 / 6000.0 gamma = 0.00038888918518558031 assert near_equal(centroids[0], [0.166667, alpha, 0.0], tol) assert near_equal(centroids[1], [0.833333, alpha, 0.0], tol) assert near_equal(centroids[2], [0.5, gamma, 0.0], tol) assert mesh.num_delaunay_violations() == 0 return
def test_degenerate_small0b(h): points = numpy.array([ [0, 0, 0], [1, 0, 0], [0.5, h, 0.0], ]) cells = numpy.array([[0, 1, 2]]) mesh = voropy.mesh_tri.MeshTri(points, cells, flat_cell_correction=None) tol = 1.0e-14 # edge lengths el = numpy.sqrt(0.5**2 + h**2) assert near_equal(mesh.get_edge_lengths().T, [el, el, 1.0], tol) # ce_ratios ce0 = 0.5 / h * (h**2 - 0.25) ce12 = 0.25 / h assert near_equal(mesh.get_ce_ratios_per_edge(), [ce0, ce12, ce12], tol) # control volumes cv12 = 0.25 * (1.0**2 * ce0 + (0.25 + h**2) * ce12) cv0 = 0.5 * (0.25 + h**2) * ce12 assert near_equal(mesh.get_control_volumes(), [cv12, cv12, cv0], tol) # cell volumes assert near_equal(mesh.cell_volumes, [0.5 * h], tol) # circumcenters assert near_equal(mesh.get_cell_circumcenters(), [0.5, 0.375, 0.0], tol) # surface areas ids, vals = mesh.get_surface_areas() assert numpy.all(ids == [[0, 1, 2], [0, 1, 2], [0, 1, 2]]) assert near_equal(vals, [ [0.0, 0.5 * el, 0.5 * el], [0.5 * el, 0.0, 0.5 * el], [0.5, 0.5, 0.0], ], tol) assert mesh.num_delaunay_violations() == 0 return
def test_regular_tet0(a): points = (a * numpy.array([ [1.0, 0, 0], [-0.5, +numpy.sqrt(3.0) / 2.0, 0], [-0.5, -numpy.sqrt(3.0) / 2.0, 0], [0.0, 0.0, numpy.sqrt(2.0)], ]) / numpy.sqrt(3.0)) cells = numpy.array([[0, 1, 2, 3]]) mesh = meshplex.MeshTetra(points, cells.copy()) assert all((mesh.cells["nodes"] == cells).flat) mesh.show() mesh.show_edge(0) # from matplotlib import pyplot as plt # plt.show() ref_local_idx = [ [[2, 3], [3, 1], [1, 2]], [[3, 0], [0, 2], [2, 3]], [[0, 1], [1, 3], [3, 0]], [[1, 2], [2, 0], [0, 1]], ] assert (mesh.local_idx.T == ref_local_idx).all() ref_local_idx_inv = [ [(0, 0, 2), (0, 1, 1), (0, 2, 3), (1, 0, 1), (1, 1, 3), (1, 2, 2)], [(0, 0, 3), (0, 1, 2), (0, 2, 0), (1, 0, 2), (1, 1, 0), (1, 2, 3)], [(0, 0, 0), (0, 1, 3), (0, 2, 1), (1, 0, 3), (1, 1, 1), (1, 2, 0)], [(0, 0, 1), (0, 1, 0), (0, 2, 2), (1, 0, 0), (1, 1, 2), (1, 2, 1)], ] assert mesh.local_idx_inv == ref_local_idx_inv tol = 1.0e-14 z = a / numpy.sqrt(24.0) assert near_equal(mesh.cell_circumcenters, [0.0, 0.0, z], tol) # pylint: disable=protected-access mesh._compute_ce_ratios_geometric() assert near_equal(mesh.circumcenter_face_distances, [z, z, z, z], tol) # covolume/edge length ratios # alpha = a / 12.0 / numpy.sqrt(2) alpha = a / 2 / numpy.sqrt(24) / numpy.sqrt(12) vals = mesh.ce_ratios assert near_equal( vals, [[ [alpha, alpha, alpha], [alpha, alpha, alpha], [alpha, alpha, alpha], [alpha, alpha, alpha], ]], tol, ) # cell volumes vol = a**3 / 6.0 / numpy.sqrt(2) assert near_equal(mesh.cell_volumes, [vol], tol) # control volumes val = vol / 4.0 assert near_equal(mesh.control_volumes, [val, val, val, val], tol) # inradius # side_area = numpy.sqrt(3) / 4 * a ** 2 # vol = a ** 3 / 6.0 / numpy.sqrt(2) # inradius = 3 * vol / (4 * side_area) inradius = a * numpy.sqrt(6) / 12 assert near_equal(mesh.cell_inradius, [inradius], tol) # circumradius circumradius = a * numpy.sqrt(6) / 4 assert near_equal(mesh.cell_circumradius, [circumradius], tol) # cell quality assert near_equal(mesh.cell_quality, [1.0], tol) mesh.mark_boundary() assert near_equal(mesh.cell_barycenters, mesh.cell_centroids, tol) assert near_equal(mesh.cell_barycenters, [[0.0, 0.0, a * numpy.sqrt(6) / 12]], tol) assert near_equal(mesh.cell_incenters, [[0.0, 0.0, a * numpy.sqrt(6) / 12]], tol) return
def test_regular_tet0(a): points = a * numpy.array([ [1.0, 0, 0], [-0.5, numpy.sqrt(3.0) / 2.0, 0], [-0.5, -numpy.sqrt(3.0) / 2.0, 0], [0.0, 0.0, numpy.sqrt(2.0)], ]) / numpy.sqrt(3.0) cells = numpy.array([[0, 1, 2, 3]]) mesh = voropy.mesh_tetra.MeshTetra(points, cells.copy()) assert all((mesh.cells['nodes'] == cells).flat) mesh.show() mesh.show_edge(0) # from matplotlib import pyplot as plt # plt.show() ref_local_idx = [ [[2, 3], [3, 1], [1, 2]], [[3, 0], [0, 2], [2, 3]], [[0, 1], [1, 3], [3, 0]], [[1, 2], [2, 0], [0, 1]], ] assert (mesh.local_idx.T == ref_local_idx).all() ref_local_idx_inv = [ [(0, 0, 2), (0, 1, 1), (0, 2, 3), (1, 0, 1), (1, 1, 3), (1, 2, 2)], [(0, 0, 3), (0, 1, 2), (0, 2, 0), (1, 0, 2), (1, 1, 0), (1, 2, 3)], [(0, 0, 0), (0, 1, 3), (0, 2, 1), (1, 0, 3), (1, 1, 1), (1, 2, 0)], [(0, 0, 1), (0, 1, 0), (0, 2, 2), (1, 0, 0), (1, 1, 2), (1, 2, 1)] ] assert mesh.local_idx_inv == ref_local_idx_inv tol = 1.0e-14 z = a / numpy.sqrt(24.0) assert near_equal(mesh.get_cell_circumcenters(), [0.0, 0.0, z], tol) mesh._compute_ce_ratios_geometric() assert near_equal(mesh.circumcenter_face_distances, [z, z, z, z], tol) # covolume/edge length ratios # alpha = a / 12.0 / numpy.sqrt(2) alpha = a / 2 / numpy.sqrt(24) / numpy.sqrt(12) vals = mesh.ce_ratios assert near_equal( vals, [[ [alpha, alpha, alpha], [alpha, alpha, alpha], [alpha, alpha, alpha], [alpha, alpha, alpha], ]], tol ) # cell volumes vol = a**3 / 6.0 / numpy.sqrt(2) assert near_equal(mesh.cell_volumes, [vol], tol) # control volumes val = vol / 4.0 assert near_equal( mesh.get_control_volumes(), [val, val, val, val], tol ) mesh.mark_boundary() return
def test_unit_triangle(): points = numpy.array([[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]) cells = numpy.array([[0, 1, 2]]) mesh = meshplex.MeshTri(points, cells) tol = 1.0e-14 assert (mesh.local_idx.T == [[1, 2], [2, 0], [0, 1]]).all() assert mesh.local_idx_inv == [[(0, 2), (1, 1)], [(0, 0), (1, 2)], [(0, 1), (1, 0)]] # ce_ratios assert near_equal(mesh.ce_ratios.T, [0.0, 0.5, 0.5], tol) # control volumes assert near_equal(mesh.control_volumes, [0.25, 0.125, 0.125], tol) # cell volumes assert near_equal(mesh.cell_volumes, [0.5], tol) # circumcenters assert near_equal(mesh.cell_circumcenters, [0.5, 0.5, 0.0], tol) # centroids assert near_equal(mesh.cell_centroids, [1.0 / 3.0, 1.0 / 3.0, 0.0], tol) assert near_equal(mesh.cell_barycenters, [1.0 / 3.0, 1.0 / 3.0, 0.0], tol) # control volume centroids assert near_equal( mesh.control_volume_centroids, [[0.25, 0.25, 0.0], [2.0 / 3.0, 1.0 / 6.0, 0.0], [1.0 / 6.0, 2.0 / 3.0, 0.0]], tol, ) # incenter assert near_equal( mesh.cell_incenters, [[(2 - numpy.sqrt(2)) / 2, (2 - numpy.sqrt(2)) / 2, 0.0]], tol, ) # circumcenter assert near_equal(mesh.cell_circumcenters, [[0.5, 0.5, 0.0]], tol) assert mesh.num_delaunay_violations() == 0 mesh.get_cell_mask() mesh.get_edge_mask() mesh.get_vertex_mask() # dummy subdomain marker test class Subdomain(object): is_boundary_only = False def is_inside(self, X): return numpy.ones(X.shape[1:], dtype=bool) cell_mask = mesh.get_cell_mask(Subdomain()) assert sum(cell_mask) == 1 # save _, filename = tempfile.mkstemp(suffix=".png") mesh.save(filename) os.remove(filename) _, filename = tempfile.mkstemp(suffix=".vtk") mesh.save(filename) os.remove(filename) return