def test_mixed_parallel(): mesh = UnitSquareMesh(MPI.comm_world, 5, 8) V = VectorElement("Lagrange", triangle, 4) Q = FiniteElement("Lagrange", triangle, 5) W = FunctionSpace(mesh, Q * V) F = Function(W) @function.expression.numba_eval def expr_eval(values, x, cell_idx): values[:, 0] = x[:, 0] values[:, 1] = x[:, 1] values[:, 2] = numpy.sin(x[:, 0] + x[:, 1]) F.interpolate(Expression(expr_eval, shape=(3, ))) # Generate random points in this mesh partition (one per cell) x = numpy.zeros(3) for c in Cells(mesh): x[0] = random() x[1] = random() * (1 - x[0]) x[2] = (1 - x[0] - x[1]) p = Point(0.0, 0.0) for i, v in enumerate(VertexRange(c)): p += v.point() * x[i] p = p.array()[:2] val = F(p) assert numpy.allclose(val[0], p[0]) assert numpy.isclose(val[1], p[1]) assert numpy.isclose(val[2], numpy.sin(p[0] + p[1]))
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 test_mixed_iterators(): "Iterate over vertices of cells" mesh = UnitCubeMesh(MPI.comm_world, 5, 5, 5) n = 0 for c in Cells(mesh): for v in VertexRange(c): n += 1 assert n == 4 * mesh.num_cells()
def test_assign_2D_vertices(): mesh = UnitSquareMesh(MPI.comm_world, 3, 3) mesh.create_connectivity(2, 0) ncells = mesh.num_cells() f = MeshValueCollection("int", mesh, 0) all_new = True for cell in Cells(mesh): value = ncells - cell.index() for i, vert in enumerate(VertexRange(cell)): all_new = all_new and f.set_value(cell.index(), i, value + i) g = MeshValueCollection("int", mesh, 0) g.assign(f) assert ncells * 3 == f.size() assert ncells * 3 == g.size() assert all_new for cell in Cells(mesh): value = ncells - cell.index() for i, vert in enumerate(VertexRange(cell)): assert value + i == g.get_value(cell.index(), i)
def test_mesh_function_assign_2D_vertices(): mesh = UnitSquareMesh(MPI.comm_world, 3, 3) mesh.create_entities(0) f = MeshFunction("int", mesh, 0, 25) g = MeshValueCollection("int", mesh, 0) g.assign(f) assert mesh.num_entities(0) == f.size() assert mesh.num_cells() * 3 == g.size() f2 = MeshFunction("int", mesh, g, 0) for cell in Cells(mesh): for i, vert in enumerate(VertexRange(cell)): assert 25 == g.get_value(cell.index(), i) assert f2[vert] == g.get_value(cell.index(), i)
def test_p4_parallel_2d(): mesh = UnitSquareMesh(MPI.comm_world, 5, 8) Q = FunctionSpace(mesh, ("CG", 4)) F = Function(Q) F.interpolate(Expression("x[0]", degree=4)) # Generate random points in this mesh partition (one per cell) x = numpy.zeros(3) for c in Cells(mesh): x[0] = random() x[1] = random() * (1 - x[0]) x[2] = 1 - x[0] - x[1] p = Point(0.0, 0.0) for i, v in enumerate(VertexRange(c)): p += v.point() * x[i] p = p.array()[:2] assert numpy.isclose(F(p)[0], p[0])
def test_mixed_parallel(): mesh = UnitSquareMesh(MPI.comm_world, 5, 8) V = VectorElement("Lagrange", triangle, 4) Q = FiniteElement("Lagrange", triangle, 5) W = FunctionSpace(mesh, Q * V) F = Function(W) F.interpolate(Expression(("x[0]", "x[1]", "sin(x[0] + x[1])"), degree=5)) # Generate random points in this mesh partition (one per cell) x = numpy.zeros(3) for c in Cells(mesh): x[0] = random() x[1] = random() * (1 - x[0]) x[2] = (1 - x[0] - x[1]) p = Point(0.0, 0.0) for i, v in enumerate(VertexRange(c)): p += v.point() * x[i] p = p.array()[:2] val = F(p) assert numpy.allclose(val[0], p[0]) assert numpy.isclose(val[1], p[1]) assert numpy.isclose(val[2], numpy.sin(p[0] + p[1]))