def test_compute_collisions_tree_3d(self): references = [[set([18, 19, 20, 21, 22, 23, 42, 43, 44, 45, 46, 47]), set([0, 1, 2, 3, 4, 5, 24, 25, 26, 27, 28, 29])], [set([6, 7, 8, 9, 10, 11, 30, 31, 32, 33, 34, 35]), set([12, 13, 14, 15, 16, 17, 36, 37, 38, 39, 40, 41])]] points = [Point(0.52, 0.51, 0.3), Point(0.9, -0.9, 0.3)] for i, point in enumerate(points): mesh_A = UnitCubeMesh(2, 2, 2) mesh_B = UnitCubeMesh(2, 2, 2) mesh_B.translate(point) tree_A = BoundingBoxTree() tree_A.build(mesh_A) tree_B = BoundingBoxTree() tree_B.build(mesh_B) entities_A, entities_B = tree_A.compute_collisions(tree_B) if MPI.size(mesh_A.mpi_comm()) == 1: self.assertEqual(set(entities_A), references[i][0]) self.assertEqual(set(entities_B), references[i][1])
def test_compute_first_collision_3d(): # FIXME: This test should not use facet indices as there are no guarantees # on how DOLFIN numbers facets reference = {1: [1364], 2: [1967, 1968, 1970, 1972, 1974, 1976], 3: [876, 877, 878, 879, 880, 881]} p = Point(0.3, 0.3, 0.3) mesh = UnitCubeMesh(8, 8, 8) for dim in range(1, 4): tree = BoundingBoxTree() tree.build(mesh, dim) first = tree.compute_first_collision(p) # FIXME: Face and test is excluded because it mistakingly # relies in the facet indices tdim = mesh.topology().dim() if dim != tdim - 1 and dim != tdim - 2: assert first in reference[dim] tree = mesh.bounding_box_tree() first = tree.compute_first_collision(p) assert first in reference[mesh.topology().dim()]
def test_compute_entity_collisions_tree_3d(): references = [[set([18, 19, 20, 21, 22, 23, 42, 43, 44, 45, 46, 47]), set([0, 1, 2, 3, 4, 5, 24, 25, 26, 27, 28, 29])], [set([7, 8, 30, 31, 32]), set([15, 16, 17, 39, 41])]] points = [Point(0.52, 0.51, 0.3), Point(0.9, -0.9, 0.3)] for i, point in enumerate(points): mesh_A = UnitCubeMesh(2, 2, 2) mesh_B = UnitCubeMesh(2, 2, 2) mesh_B.translate(point) tree_A = BoundingBoxTree() tree_A.build(mesh_A) tree_B = BoundingBoxTree() tree_B.build(mesh_B) entities_A, entities_B = tree_A.compute_entity_collisions(tree_B) assert set(entities_A) == references[i][0] assert set(entities_B) == references[i][1]
def test_mesh_point_3d(self): "Test mesh-point intersection in 3D" point = Point(0.1, 0.2, 0.3) mesh = UnitCubeMesh(8, 8, 8) intersection = intersect(mesh, point) if MPI.size(mesh.mpi_comm()) == 1: self.assertEqual(intersection.intersected_cells(), [816])
def test_compute_entity_collisions_3d(self): reference = set([876, 877, 878, 879, 880, 881]) p = Point(0.3, 0.3, 0.3) mesh = UnitCubeMesh(8, 8, 8) tree = BoundingBoxTree() tree.build(mesh) entities = tree.compute_entity_collisions(p) if MPI.size(mesh.mpi_comm()) == 1: self.assertEqual(set(entities), reference)
def test_compute_first_entity_collision_3d(): reference = [876, 877, 878, 879, 880, 881] p = Point(0.3, 0.3, 0.3) mesh = UnitCubeMesh(8, 8, 8) tree = BoundingBoxTree() tree.build(mesh) first = tree.compute_first_entity_collision(p) assert first in reference tree = mesh.bounding_box_tree() first = tree.compute_first_entity_collision(p) assert first in reference
def test_compute_collisions_point_3d(self): reference = {1: set([1364]), 2: set([1967, 1968, 1970, 1972, 1974, 1976]), 3: set([876, 877, 878, 879, 880, 881])} p = Point(0.3, 0.3, 0.3) mesh = UnitCubeMesh(8, 8, 8) for dim in range(1, 4): tree = BoundingBoxTree() tree.build(mesh, dim) entities = tree.compute_collisions(p) if MPI.size(mesh.mpi_comm()) == 1: self.assertEqual(set(entities), reference[dim])
def test_save_3d_vector_series(tempdir, encoding): if invalid_config(encoding): pytest.skip("XDMF unsupported in current configuration") filename = os.path.join(tempdir, "u_3D.xdmf") mesh = UnitCubeMesh(MPI.comm_world, 2, 2, 2) u = Function(VectorFunctionSpace(mesh, "Lagrange", 2)) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as file: u.vector()[:] = 1.0 file.write(u, 0.1) u.vector()[:] = 2.0 file.write(u, 0.2) u.vector()[:] = 3.0 file.write(u, 0.3)
def test_compute_first_entity_collision_3d(): reference = [876, 877, 878, 879, 880, 881] p = Point(0.3, 0.3, 0.3) mesh = UnitCubeMesh(MPI.comm_world, 8, 8, 8) tree = BoundingBoxTree(mesh, mesh.topology.dim) first = tree.compute_first_entity_collision(p, mesh) assert first in reference
def test_compute_entity_collisions_3d(): reference = set([876, 877, 878, 879, 880, 881]) p = Point(0.3, 0.3, 0.3) mesh = UnitCubeMesh(MPI.comm_world, 8, 8, 8) tree = BoundingBoxTree(mesh, mesh.topology.dim) entities = tree.compute_entity_collisions_mesh(p, mesh) assert set(entities) == reference
def test_radius_ratio_min_radius_ratio_max(): mesh1d = UnitIntervalMesh(MPI.comm_self, 4) x = mesh1d.geometry.points x[4] = mesh1d.geometry.points[3] # Create 2D mesh with one equilateral triangle mesh2d = RectangleMesh.create(MPI.comm_world, [Point(0, 0), Point(1, 1)], [1, 1], CellType.Type.triangle, cpp.mesh.GhostMode.none, 'left') x = mesh2d.geometry.points x[3] += 0.5 * (sqrt(3.0) - 1.0) # Create 3D mesh with regular tetrahedron and degenerate cells mesh3d = UnitCubeMesh(MPI.comm_self, 1, 1, 1) x = mesh3d.geometry.points x[6][0] = 1.0 x[3][1] = 0.0 rmin, rmax = MeshQuality.radius_ratio_min_max(mesh1d) assert round(rmin - 0.0, 7) == 0 assert round(rmax - 1.0, 7) == 0 rmin, rmax = MeshQuality.radius_ratio_min_max(mesh2d) assert round(rmin - 2.0 * sqrt(2.0) / (2.0 + sqrt(2.0)), 7) == 0 assert round(rmax - 1.0, 7) == 0 rmin, rmax = MeshQuality.radius_ratio_min_max(mesh3d) assert round(rmin - 0.0, 7) == 0 assert round(rmax - 1.0, 7) == 0
def test_radius_ratio_tetrahedron(): # Create mesh and compute ratios mesh = UnitCubeMesh(MPI.comm_world, 14, 14, 14) ratios = MeshQuality.radius_ratios(mesh) for c in Cells(mesh): assert round(ratios[c] - 0.717438935214, 7) == 0
def test_compute_first_entity_collision_3d(self): reference = [876, 877, 878, 879, 880, 881] p = Point(0.3, 0.3, 0.3) mesh = UnitCubeMesh(8, 8, 8) tree = BoundingBoxTree() tree.build(mesh) first = tree.compute_first_entity_collision(p) if MPI.size(mesh.mpi_comm()) == 1: self.assertIn(first, reference) tree = mesh.bounding_box_tree() first = tree.compute_first_entity_collision(p) if MPI.size(mesh.mpi_comm()) == 1: self.assertIn(first, reference)
def test_p4_parallel_3d(): mesh = UnitCubeMesh(MPI.comm_world, 3, 5, 8) Q = FunctionSpace(mesh, ("CG", 5)) F = Function(Q) @function.expression.numba_eval def x0(values, x, cell_idx): values[:, 0] = x[:, 0] F.interpolate(Expression(x0)) # Generate random points in this mesh partition (one per cell) x = numpy.zeros(4) tree = cpp.geometry.BoundingBoxTree(mesh, mesh.geometry.dim) for c in Cells(mesh): x[0] = random() x[1] = random() * (1 - x[0]) x[2] = random() * (1 - x[0] - x[1]) x[3] = 1 - x[0] - x[1] - x[2] p = Point(0.0, 0.0, 0.0) for i, v in enumerate(VertexRange(c)): p += v.point() * x[i] p = p.array() assert numpy.isclose(F(p, tree)[0], p[0])
def mesh_factory(tdim, n): if tdim == 1: return UnitIntervalMesh(MPI.comm_world, n) elif tdim == 2: return UnitSquareMesh(MPI.comm_world, n, n) elif tdim == 3: return UnitCubeMesh(MPI.comm_world, n, n, n)
def test_save_3D_cell_function(tempdir, encoding, data_type): dtype_str, dtype = data_type mesh = UnitCubeMesh(MPI.comm_world, 4, 4, 4) mf = MeshFunction(dtype_str, mesh, mesh.topology.dim, 0) mf.name = "cells" mf.values[:] = np.arange(mesh.num_entities(3), dtype=dtype) filename = os.path.join(tempdir, "mf_3D_%s.xdmf" % dtype_str) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as file: file.write(mf) with XDMFFile(mesh.mpi_comm(), filename) as xdmf: read_function = getattr(xdmf, "read_mf_" + dtype_str) mf_in = read_function(mesh, "cells") diff = mf_in.values - mf.values assert np.all(diff == 0)
def mk_scheme(N, Vname, Vorder, cpp_expr, expr_args, convection_inp, dim=2, comm=None): if comm is None: comm = MPI.comm_world parameters['ghost_mode'] = 'shared_vertex' if dim == 2: mesh = UnitSquareMesh(comm, N, N) else: mesh = UnitCubeMesh(comm, N, N, N) V = FunctionSpace(mesh, Vname, Vorder) C = Function(V) e = Expression(cpp_expr, element=V.ufl_element(), **expr_args) C.interpolate(e) D = Function(V) D.assign(C) sim = Simulation() sim.set_mesh(mesh) sim.data['constrained_domain'] = None sim.data['C'] = C for key, value in convection_inp.items(): sim.input.set_value('convection/C/%s' % key, value) scheme_name = convection_inp['convection_scheme'] return get_convection_scheme(scheme_name)(sim, 'C')
def test_compute_first_entity_collision_3d(self): reference = [876, 877, 878, 879, 880, 881] p = Point(0.3, 0.3, 0.3) mesh = UnitCubeMesh(8, 8, 8) tree = BoundingBoxTree() tree.build(mesh) first = tree.compute_first_entity_collision(p) if MPI.num_processes() == 1: self.assertIn(first, reference) tree = mesh.bounding_box_tree() first = tree.compute_first_entity_collision(p) if MPI.num_processes() == 1: self.assertIn(first, reference)
def generate_cube(Nelements, length, refinements=0): """ Creates a square mesh of given elements and length with markers on the sides: left, bottom, right and top """ from dolfin import UnitCubeMesh, SubDomain, MeshFunction, Measure, near, refine mesh = UnitCubeMesh(Nelements, Nelements, Nelements) for i in range(refinements): mesh = refine(mesh) mesh.coordinates()[:] *= length # Subdomains: Solid class Xp(SubDomain): def inside(self, x, on_boundary): return near(x[0], length) and on_boundary class Xm(SubDomain): def inside(self, x, on_boundary): return near(x[0], 0.0) and on_boundary class Yp(SubDomain): def inside(self, x, on_boundary): return near(x[1], length) and on_boundary class Ym(SubDomain): def inside(self, x, on_boundary): return near(x[1], 0.0) and on_boundary class Zp(SubDomain): def inside(self, x, on_boundary): return near(x[2], length) and on_boundary class Zm(SubDomain): def inside(self, x, on_boundary): return near(x[2], 0.0) and on_boundary xp, xm, yp, ym, zp, zm = Xp(), Xm(), Yp(), Ym(), Zp(), Zm() XP, XM, YP, YM, ZP, ZM = 1, 2, 3, 4, 5, 6 # Set numbering markers = MeshFunction("size_t", mesh, 2) markers.set_all(0) boundaries = (xp, xm, yp, ym, zp, zm) def_names = (XP, XM, YP, YM, ZP, ZM) for side, num in zip(boundaries, def_names): side.mark(markers, num) return mesh, markers, XP, XM, YP, YM, ZP, ZM
def test_advect_periodic(advection_scheme): xmin, ymin, zmin = 0., 0., 0. xmax, ymax, zmax = 1., 1., 1. pres = 10 mesh = UnitCubeMesh(10, 10, 10) lims = np.array([[xmin, xmin, ymin, ymax, zmin, zmax], [xmax, xmax, ymin, ymax, zmin, zmax], [xmin, xmax, ymin, ymin, zmin, zmax], [xmin, xmax, ymax, ymax, zmin, zmax], [xmin, xmax, ymin, ymax, zmin, zmin], [xmin, xmax, ymin, ymax, zmax, zmax]]) vexpr = Constant((1., 1., 1.)) V = VectorFunctionSpace(mesh, "CG", 1) v = Function(V) v.assign(vexpr) x = RandomBox(Point(0., 0., 0.), Point(1., 1., 1.)).generate([pres, pres, pres]) x = comm.bcast(x, root=0) dt = 0.05 p = particles(x, [x * 0, x**2], mesh) if advection_scheme == 'euler': ap = advect_particles(p, V, v, 'periodic', lims.flatten()) elif advection_scheme == 'rk2': ap = advect_rk2(p, V, v, 'periodic', lims.flatten()) elif advection_scheme == 'rk3': ap = advect_rk3(p, V, v, 'periodic', lims.flatten()) else: assert False xp0 = p.positions() t = 0. while t < 1. - 1e-12: ap.do_step(dt) t += dt xpE = p.positions() xp0_root = comm.gather(xp0, root=0) xpE_root = comm.gather(xpE, root=0) assert len(xp0) == len(xpE) num_particles = p.number_of_particles() if comm.Get_rank() == 0: xp0_root = np.float32(np.vstack(xp0_root)) xpE_root = np.float32(np.vstack(xpE_root)) # Sort on x positions xp0_root = xp0_root[xp0_root[:, 0].argsort(), :] xpE_root = xpE_root[xpE_root[:, 0].argsort(), :] error = np.linalg.norm(xp0_root - xpE_root) assert error < 1e-10 assert num_particles - pres**3 == 0
def test_compute_first_collision_3d(): reference = {1: [1364], 2: [1967, 1968, 1970, 1972, 1974, 1976], 3: [876, 877, 878, 879, 880, 881]} p = Point(0.3, 0.3, 0.3) mesh = UnitCubeMesh(8, 8, 8) for dim in range(1, 4): tree = BoundingBoxTree() tree.build(mesh, dim) first = tree.compute_first_collision(p) assert first in reference[dim] tree = mesh.bounding_box_tree() first = tree.compute_first_collision(p) assert first in reference[mesh.topology().dim()]
def test_save_points_3D(tempdir, encoding): mesh = UnitCubeMesh(MPI.comm_world, 4, 4, 4) points, values = [], [] for v in Vertices(mesh): points.append(v.point()) values.append(v.point().norm()) vals = numpy.array(values) with XDMFFile(mesh.mpi_comm(), os.path.join(tempdir, "points_3D.xdmf"), encoding=encoding) as file: file.write(points) with XDMFFile(mesh.mpi_comm(), os.path.join(tempdir, "points_values_3D.xdmf"), encoding=encoding) as file: file.write(points, vals)
def test_save_3d_mesh(tempfile, file_options): mesh = UnitCubeMesh(MPI.comm_world, 8, 8, 8) VTKFile(tempfile + "mesh.pvd", "ascii").write(mesh) f = VTKFile(tempfile + "mesh.pvd", "ascii") f.write(mesh, 0.) f.write(mesh, 1.) for file_option in file_options: VTKFile(tempfile + "mesh.pvd", file_option).write(mesh)
def test_compute_closest_entity_3d(): reference = (0, 0.1) p = Point(0.1, 0.05, -0.1) mesh = UnitCubeMesh(MPI.comm_world, 8, 8, 8) tree = BoundingBoxTree(mesh.geometry.dim) tree.build(mesh, mesh.topology.dim) entity, distance = tree.compute_closest_entity(p, mesh) assert entity == reference[0] assert round(distance - reference[1], 7) == 0 tree = mesh.bounding_box_tree() entity, distance = tree.compute_closest_entity(p, mesh) assert entity == reference[0] assert round(distance - reference[1], 7) == 0
def test_compute_entity_collisions_3d(self): reference = [876, 877, 878, 879, 880, 881] p = Point(0.3, 0.3, 0.3) mesh = UnitCubeMesh(8, 8, 8) tree = BoundingBoxTree() tree.build(mesh) entities = tree.compute_entity_collisions(p, mesh) if MPI.num_processes() == 1: self.assertEqual(sorted(entities), reference) tree = mesh.bounding_box_tree() entities = tree.compute_entity_collisions(p, mesh) if MPI.num_processes() == 1: self.assertEqual(sorted(entities), reference)
def test_compute_closest_entity_3d(): reference = (0, 0.1) p = Point(0.1, 0.05, -0.1) mesh = UnitCubeMesh(8, 8, 8) tree = BoundingBoxTree() tree.build(mesh) entity, distance = tree.compute_closest_entity(p) assert entity == reference[0] assert round(distance - reference[1], 7) == 0 tree = mesh.bounding_box_tree() entity, distance = tree.compute_closest_entity(p) assert entity == reference[0] assert round(distance - reference[1], 7) == 0
def test_compute_closest_entity_3d(): reference = (0, 0.1) p = numpy.array([0.1, 0.05, -0.1]) mesh = UnitCubeMesh(MPI.comm_world, 8, 8, 8) tree = BoundingBoxTree(mesh, mesh.topology.dim) entity, distance = tree.compute_closest_entity(p, mesh) assert entity == reference[0] assert round(distance - reference[1], 7) == 0
def test_tensor_constant(): mesh = UnitCubeMesh(MPI.comm_world, 2, 2, 2) data = [[1.0, 2.0, 1.0], [1.0, 2.0, 1.0], [1.0, 2.0, 1.0]] c0 = Constant(mesh, data) assert c0.value.shape == (3, 3) assert c0.value.all() == np.asarray(data).all() c0.value *= 2.0 assert c0.value.all() == (2.0 * np.asarray(data)).all()
def test_append_and_load_mesh_functions(tempdir, encoding, data_type): if invalid_config(encoding): pytest.skip("XDMF unsupported in current configuration") dtype_str, dtype = data_type meshes = [ UnitSquareMesh(MPI.comm_world, 12, 12), UnitCubeMesh(MPI.comm_world, 2, 2, 2) ] for mesh in meshes: dim = mesh.topology.dim vf = MeshFunction(dtype_str, mesh, 0, 0) vf.rename("vertices") ff = MeshFunction(dtype_str, mesh, mesh.topology.dim - 1, 0) ff.rename("facets") cf = MeshFunction(dtype_str, mesh, mesh.topology.dim, 0) cf.rename("cells") if (MPI.size(mesh.mpi_comm()) == 1): for vertex in Vertices(mesh): vf[vertex] = dtype(vertex.index()) for facet in Facets(mesh): ff[facet] = dtype(facet.index()) for cell in Cells(mesh): cf[cell] = dtype(cell.index()) else: for vertex in Vertices(mesh): vf[vertex] = dtype(vertex.global_index()) for facet in Facets(mesh): ff[facet] = dtype(facet.global_index()) for cell in Cells(mesh): cf[cell] = dtype(cell.global_index()) filename = os.path.join(tempdir, "appended_mf_%dD.xdmf" % dim) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as xdmf: xdmf.write(mesh) xdmf.write(vf) xdmf.write(ff) xdmf.write(cf) with XDMFFile(mesh.mpi_comm(), filename) as xdmf: read_function = getattr(xdmf, "read_mf_" + dtype_str) vf_in = read_function(mesh, "vertices") ff_in = read_function(mesh, "facets") cf_in = read_function(mesh, "cells") diff = 0 for vertex in Vertices(mesh): diff += (vf_in[vertex] - vf[vertex]) for facet in Facets(mesh): diff += (ff_in[facet] - ff[facet]) for cell in Cells(mesh): diff += (cf_in[cell] - cf[cell]) assert diff == 0
def test_compute_entity_collisions_tree_3d(self): references = [[set([18, 19, 20, 21, 22, 23, 42, 43, 44, 45, 46, 47]), set([0, 1, 2, 3, 4, 5, 24, 25, 26, 27, 28, 29])], [set([7, 8, 30, 31, 32]), set([15, 16, 17, 39, 41])]] points = [Point(0.52, 0.51, 0.3), Point(0.9, -0.9, 0.3)] for i, point in enumerate(points): mesh_A = UnitCubeMesh(2, 2, 2) mesh_B = UnitCubeMesh(2, 2, 2) mesh_B.translate(point) tree_A = BoundingBoxTree() tree_A.build(mesh_A) tree_B = BoundingBoxTree() tree_B.build(mesh_B) entities_A, entities_B = tree_A.compute_entity_collisions(tree_B) if MPI.size(mesh_A.mpi_comm()) == 1: self.assertEqual(set(entities_A), references[i][0]) self.assertEqual(set(entities_B), references[i][1])
def test_compute_collisions_point_3d(): reference = {1: set([1364]), 2: set([1967, 1968, 1970, 1972, 1974, 1976]), 3: set([876, 877, 878, 879, 880, 881])} p = Point(0.3, 0.3, 0.3) mesh = UnitCubeMesh(8, 8, 8) for dim in range(1, 4): tree = BoundingBoxTree() tree.build(mesh, dim) entities = tree.compute_collisions(p) # FIXME: Face and edges tests are excluded because test # mistakingly relies on the face and edge indices tdim = mesh.topology().dim() if dim != tdim - 1 and dim != tdim - 2: assert set(entities) == reference[dim]
def testFacetArea(): references = [(UnitIntervalMesh(MPI.comm_world, 1), 2, 2), (UnitSquareMesh( MPI.comm_world, 1, 1), 4, 4), (UnitCubeMesh(MPI.comm_world, 1, 1, 1), 6, 3)] for mesh, surface, ref_int in references: c0 = ufl.FacetArea(mesh) c1 = dolfin.FacetArea(mesh) assert (c0) assert (c1)
def test_vector_p1_3d(): meshc = UnitCubeMesh(MPI.comm_world, 2, 3, 4) meshf = UnitCubeMesh(MPI.comm_world, 3, 4, 5) Vc = VectorFunctionSpace(meshc, "CG", 1) Vf = VectorFunctionSpace(meshf, "CG", 1) u = Expression(("x[0] + 2*x[1]", "4*x[0]", "3*x[2] + x[0]"), degree=1) uc = interpolate(u, Vc) uf = interpolate(u, Vf) mat = PETScDMCollection.create_transfer_matrix(Vc, Vf).mat() Vuc = Function(Vf) mat.mult(uc.vector().vec(), Vuc.vector().vec()) diff = Vuc.vector() diff.vec().axpy(-1, uf.vector().vec()) assert diff.norm(Norm.l2) < 1.0e-12
def test_vector_constant(): mesh = UnitCubeMesh(MPI.comm_world, 2, 2, 2) c0 = Constant(mesh, [1.0, 2.0]) c1 = Constant(mesh, np.array([1.0, 2.0])) assert (c0.value.all() == c1.value.all()) c0.value += 1.0 assert c0.value.all() == np.array([2.0, 3.0]).all() c0.value -= [1.0, 2.0] assert c0.value[0] == c0.value[1]
def test_compute_first_entity_collision_3d(): reference = [876, 877, 878, 879, 880, 881] p = Point(0.3, 0.3, 0.3) mesh = UnitCubeMesh(MPI.comm_world, 8, 8, 8) tree = BoundingBoxTree(mesh.geometry.dim) tree.build_mesh(mesh, mesh.topology.dim) first = tree.compute_first_entity_collision(p, mesh) assert first in reference # FIXME: remove after Mesh is wrapped in Python tree_cpp = mesh.bounding_box_tree() tree = BoundingBoxTree() tree._cpp_object = tree_cpp first = tree.compute_first_entity_collision(p, mesh) assert first in reference
def test_distance_tetrahedron(): mesh = UnitCubeMesh(MPI.comm_self, 1, 1, 1) cell = Cell(mesh, 5) assert round(cell.distance(Point(-1.0, -1.0, -1.0)) - numpy.sqrt(3), 7) == 0 assert round(cell.distance(Point(-1.0, 0.5, 0.5)) - 1, 7) == 0 assert round(cell.distance(Point(0.5, 0.5, 0.5)) - 0.0, 7) == 0
def test_scalar_constant(): mesh = UnitCubeMesh(MPI.comm_world, 2, 2, 2) c = Constant(mesh, 1.0) assert c.value.shape == () assert c.value == 1.0 c.value += 1.0 assert c.value == 2.0 c.value = 3.0 assert c.value == 3.0
def test_save_3D_facet_function(tempdir, encoding, data_type): dtype_str, dtype = data_type mesh = UnitCubeMesh(MPI.comm_world, 4, 4, 4) tdim = mesh.topology.dim mf = MeshFunction(dtype_str, mesh, tdim - 1, 0) mf.name = "facets" global_indices = mesh.topology.global_indices(tdim - 1) mf.values[:] = global_indices[:] filename = os.path.join(tempdir, "mf_facet_3D_%s.xdmf" % dtype_str) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as xdmf: xdmf.write(mf) with XDMFFile(mesh.mpi_comm(), filename) as xdmf: read_function = getattr(xdmf, "read_mf_" + dtype_str) mf_in = read_function(mesh, "facets") diff = mf_in.values - mf.values assert numpy.all(diff == 0)
def test_computed_norms_against_references(): # Reference values for norm of solution vector reference = { ("16x16 unit tri square", 1): 9.547454087328376, ("16x16 unit tri square", 2): 18.42366670418269, ("16x16 unit tri square", 3): 27.29583104732836, ("16x16 unit tri square", 4): 36.16867128121694, ("4x4x4 unit tet cube", 1): 12.23389289626038, ("4x4x4 unit tet cube", 2): 28.96491629163837, ("4x4x4 unit tet cube", 3): 49.97350551329799, ("4x4x4 unit tet cube", 4): 74.49938266409099, ("16x16 unit quad square", 1): 9.550848071820747, ("16x16 unit quad square", 2): 18.423668706176354, ("16x16 unit quad square", 3): 27.295831017251672, ("16x16 unit quad square", 4): 36.168671281610855, ("4x4x4 unit hex cube", 1): 12.151954087339782, ("4x4x4 unit hex cube", 2): 28.965646690046885, ("4x4x4 unit hex cube", 3): 49.97349423895635, ("4x4x4 unit hex cube", 4): 74.49938136593539 } # Mesh files and degrees to check meshes = [(UnitSquareMesh(MPI.comm_world, 16, 16), "16x16 unit tri square"), (UnitCubeMesh(MPI.comm_world, 4, 4, 4), "4x4x4 unit tet cube")] # (UnitSquareMesh(MPI.comm_world, 16, 16, CellType.Type.quadrilateral), "16x16 unit quad square"), # (UnitCubeMesh(MPI.comm_world, 4, 4, 4, CellType.Type.hexahedron), "4x4x4 unit hex cube")] degrees = [1, 2] # For MUMPS, increase estimated require memory increase. Typically # required for high order elements on small meshes in 3D PETScOptions.set("mat_mumps_icntl_14", 40) # Iterate over test cases and collect results results = [] for mesh in meshes: for degree in degrees: gc_barrier() norm = compute_norm(mesh[0], degree) results.append((mesh[1], degree, norm)) # Change option back to default PETScOptions.set("mat_mumps_icntl_14", 20) # Check results errors = check_results(results, reference, tol) # Print errors for debugging if they fail if errors: print_errors(errors) # Print results for use as reference if any(e[-1] is None for e in errors): # e[-1] is diff print_reference(results) # A passing test should have no errors assert len(errors) == 0 # See stdout for detailed norms and diffs.
def test_mesh_point_3d(): "Test mesh-point intersection in 3D" point = Point(0.1, 0.2, 0.3) mesh = UnitCubeMesh(8, 8, 8) intersection = intersect(mesh, point) assert intersection.intersected_cells() == [816]
def test_compute_closest_entity_3d(self): reference = (2, numpy.sqrt(3.0)) p = Point(-1.0, -1.0, -1.0) mesh = UnitCubeMesh(8, 8, 8) tree = BoundingBoxTree() tree.build(mesh) entity, distance = tree.compute_closest_entity(p, mesh) if MPI.num_processes() == 1: self.assertEqual(entity, reference[0]) self.assertAlmostEqual(distance, reference[1]) tree = mesh.bounding_box_tree() entity, distance = tree.compute_closest_entity(p, mesh) if MPI.num_processes() == 1: self.assertEqual(entity, reference[0]) self.assertAlmostEqual(distance, reference[1])
def test_compute_collisions_3d(self): reference = {1: [1364], 2: [1967, 1968, 1970, 1972, 1974, 1976], 3: [876, 877, 878, 879, 880, 881]} p = Point(0.3, 0.3, 0.3) mesh = UnitCubeMesh(8, 8, 8) for dim in range(1, 4): tree = BoundingBoxTree() tree.build(mesh, dim) entities = tree.compute_collisions(p) if MPI.num_processes() == 1: self.assertEqual(sorted(entities), reference[dim]) tree = mesh.bounding_box_tree() entities = tree.compute_collisions(p) if MPI.num_processes() == 1: self.assertEqual(sorted(entities), reference[mesh.topology().dim()])
def test_mesh_point_3d_hexahedron(): "Test mesh-point intersection in 3D for hexahedral mesh" point = Point(0.1, 0.2, 0.3) mesh = UnitCubeMesh.create(8, 8, 8, CellType.Type.hexahedron) intersection = intersect(mesh, point) # Returns [] now, but [136] is the correct cell. assert intersection.intersected_cells() == [136]
def test_compute_closest_entity_3d(self): reference = (0, 0.1) p = Point(0.1, 0.05, -0.1) mesh = UnitCubeMesh(8, 8, 8) tree = BoundingBoxTree() tree.build(mesh) entity, distance = tree.compute_closest_entity(p) if MPI.size(mesh.mpi_comm()) == 1: self.assertEqual(entity, reference[0]) self.assertAlmostEqual(distance, reference[1]) tree = mesh.bounding_box_tree() entity, distance = tree.compute_closest_entity(p) if MPI.size(mesh.mpi_comm()) == 1: self.assertEqual(entity, reference[0]) self.assertAlmostEqual(distance, reference[1])