def test_balls_intersection(): radius = 1.0 displacement = 0.5 s0 = pygalmesh.Ball([displacement, 0, 0], radius) s1 = pygalmesh.Ball([-displacement, 0, 0], radius) u = pygalmesh.Intersection([s0, s1]) a = numpy.sqrt(radius ** 2 - displacement ** 2) edge_size = 0.1 n = int(2 * numpy.pi * a / edge_size) circ = [ [0.0, a * numpy.cos(i * 2 * numpy.pi / n), a * numpy.sin(i * 2 * numpy.pi / n)] for i in range(n) ] circ.append(circ[0]) mesh = pygalmesh.generate_mesh( u, feature_edges=[circ], cell_size=0.15, edge_size=edge_size, verbose=False ) assert abs(max(mesh.points[:, 0]) - (radius - displacement)) < 0.02 assert abs(min(mesh.points[:, 0]) + (radius - displacement)) < 0.02 assert abs(max(mesh.points[:, 1]) - a) < 0.02 assert abs(min(mesh.points[:, 1]) + a) < 0.02 assert abs(max(mesh.points[:, 2]) - a) < 0.02 assert abs(min(mesh.points[:, 2]) + a) < 0.02 vol = sum(helpers.compute_volumes(mesh.points, mesh.cells["tetra"])) h = radius - displacement ref_vol = 2 * (h * numpy.pi / 6.0 * (3 * a ** 2 + h ** 2)) assert abs(vol - ref_vol) < 0.1 return
def test_cuboids_intersection(): c0 = pygalmesh.Cuboid([0, 0, -0.5], [3, 3, 0.5]) c1 = pygalmesh.Cuboid([1, 1, -2], [2, 2, 2]) u = pygalmesh.Intersection([c0, c1]) # In CGAL, feature edges must not intersect, and that's a problem here: The # intersection edges of the cuboids share eight points with the edges of # the tall and skinny cuboid. # eps = 1.0e-2 # extra_features = [ # [[1.0, 1.0 + eps, 0.5], [1.0, 2.0 - eps, 0.5]], # [[1.0 + eps, 2.0, 0.5], [2.0 - eps, 2.0, 0.5]], # [[2.0, 2.0 - eps, 0.5], [2.0, 1.0 + eps, 0.5]], # [[2.0 - eps, 1.0, 0.5], [1.0 + eps, 1.0, 0.5]], # ] mesh = pygalmesh.generate_mesh(u, cell_size=0.1, edge_size=0.1, verbose=False) # filter the vertices that belong to cells verts = mesh.points[numpy.unique(mesh.cells["tetra"])] tol = 1.0e-2 assert abs(max(verts[:, 0]) - 2.0) < tol assert abs(min(verts[:, 0]) - 1.0) < tol assert abs(max(verts[:, 1]) - 2.0) < tol assert abs(min(verts[:, 1]) - 1.0) < tol assert abs(max(verts[:, 2]) - 0.5) < 0.05 assert abs(min(verts[:, 2]) + 0.5) < 0.05 vol = sum(helpers.compute_volumes(mesh.points, mesh.cells["tetra"])) assert abs(vol - 1.0) < 0.05 return
def test_halfspace(): c = pygalmesh.Cuboid([0, 0, 0], [1, 1, 1]) s = pygalmesh.HalfSpace([1.0, 2.0, 3.0], 1.0, 2.0) u = pygalmesh.Intersection([c, s]) mesh = pygalmesh.generate_mesh(u, cell_size=0.2, edge_size=0.2, verbose=False) vol = sum(helpers.compute_volumes(mesh.points, mesh.cells["tetra"])) assert abs(vol - 1 / 750) < 1.0e-3 return
def test_halfspace(): c = pygalmesh.Cuboid([0, 0, 0], [1, 1, 1]) s = pygalmesh.HalfSpace([1.0, 2.0, 3.0], 1.0, 2.0) u = pygalmesh.Intersection([c, s]) pygalmesh.generate_mesh(u, 'out.mesh', cell_size=0.2, edge_size=0.2, verbose=False) vertices, cells, _, _, _ = meshio.read('out.mesh') vol = sum(compute_volumes(vertices, cells['tetra'])) assert abs(vol - 1 / 750) < 1.0e-3 return