def test_call(R, V, W, Q, mesh): u0 = Function(R) u1 = Function(V) u2 = Function(W) u3 = Function(Q) @function.expression.numba_eval def expr_eval1(values, x, cell_idx): values[:, 0] = x[:, 0] + x[:, 1] + x[:, 2] e1 = Expression(expr_eval1) @function.expression.numba_eval def expr_eval2(values, x, cell_idx): values[:, 0] = x[:, 0] + x[:, 1] + x[:, 2] values[:, 1] = x[:, 0] - x[:, 1] - x[:, 2] values[:, 2] = x[:, 0] + x[:, 1] + x[:, 2] e2 = Expression(expr_eval2, shape=(3, )) @function.expression.numba_eval def expr_eval3(values, x, cell_idx): values[:, 0] = x[:, 0] + x[:, 1] + x[:, 2] values[:, 1] = x[:, 0] - x[:, 1] - x[:, 2] values[:, 2] = x[:, 0] + x[:, 1] + x[:, 2] values[:, 3] = x[:, 0] values[:, 4] = x[:, 1] values[:, 5] = x[:, 2] values[:, 6] = -x[:, 0] values[:, 7] = -x[:, 1] values[:, 8] = -x[:, 2] e3 = Expression(expr_eval3, shape=(3, 3)) u0.vector()[:] = 1.0 u1.interpolate(e1) u2.interpolate(e2) u3.interpolate(e3) p0 = ((Vertex(mesh, 0).point() + Vertex(mesh, 1).point()) / 2.0).array() x0 = (mesh.geometry.x(0) + mesh.geometry.x(1)) / 2.0 assert numpy.allclose(u0(x0), u0(x0)) assert numpy.allclose(u0(x0), u0(p0)) assert numpy.allclose(u1(x0), u1(x0)) assert numpy.allclose(u1(x0), u1(p0)) assert numpy.allclose(u2(x0)[0], u1(p0)) assert numpy.allclose(u2(x0), u2(p0)) assert numpy.allclose(u3(x0)[:3], u2(x0), rtol=1e-15, atol=1e-15) p0_list = [p for p in p0] x0_list = [x for x in x0] assert numpy.allclose(u0(x0_list), u0(x0_list)) assert numpy.allclose(u0(x0_list), u0(p0_list)) with pytest.raises(ValueError): u0([0, 0, 0, 0]) with pytest.raises(ValueError): u0([0, 0])
def hat_function_grad(vertex, cell): """Compute L^\infty-norm of gradient of hat function on 'cell' and value 1 in 'vertex'.""" # TODO: fix using ghosted mesh not_working_in_parallel("function 'hat_function_grad'") assert vertex in vertices(cell), "vertex not in cell!" # Find adjacent facet f = [f for f in facets(cell) if not vertex in vertices(f)] assert len(f) == 1, "Something strange with adjacent cell!" f = f[0] # Get unit normal n = f.normal() n /= n.norm() # Pick some vertex on facet # FIXME: Is it correct index in parallel? facet_vertex_0 = Vertex(cell.mesh(), f.entities(0)[0]) # Compute signed distance from vertex to facet plane d = (facet_vertex_0.point() - vertex.point()).dot(n) # Return norm of gradient assert d != 0.0, "Degenerate cell!" return 1.0/abs(d)
def test_call(R, V, W, Q, mesh): u0 = Function(R) u1 = Function(V) u2 = Function(W) u3 = Function(Q) def e1(values, x): values[:, 0] = x[:, 0] + x[:, 1] + x[:, 2] def e2(values, x): values[:, 0] = x[:, 0] + x[:, 1] + x[:, 2] values[:, 1] = x[:, 0] - x[:, 1] - x[:, 2] values[:, 2] = x[:, 0] + x[:, 1] + x[:, 2] def e3(values, x): values[:, 0] = x[:, 0] + x[:, 1] + x[:, 2] values[:, 1] = x[:, 0] - x[:, 1] - x[:, 2] values[:, 2] = x[:, 0] + x[:, 1] + x[:, 2] values[:, 3] = x[:, 0] values[:, 4] = x[:, 1] values[:, 5] = x[:, 2] values[:, 6] = -x[:, 0] values[:, 7] = -x[:, 1] values[:, 8] = -x[:, 2] u0.vector().set(1.0) u1.interpolate(e1) u2.interpolate(e2) u3.interpolate(e3) p0 = ((Vertex(mesh, 0).point() + Vertex(mesh, 1).point()) / 2.0) x0 = (mesh.geometry.x(0) + mesh.geometry.x(1)) / 2.0 tree = cpp.geometry.BoundingBoxTree(mesh, mesh.geometry.dim) assert np.allclose(u0(x0, tree), u0(x0, tree)) assert np.allclose(u0(x0, tree), u0(p0, tree)) assert np.allclose(u1(x0, tree), u1(x0, tree)) assert np.allclose(u1(x0, tree), u1(p0, tree)) assert np.allclose(u2(x0, tree)[0], u1(p0, tree)) assert np.allclose(u2(x0, tree), u2(p0, tree)) assert np.allclose(u3(x0, tree)[:3], u2(x0, tree), rtol=1e-15, atol=1e-15) p0_list = [p for p in p0] x0_list = [x for x in x0] assert np.allclose(u0(x0_list, tree), u0(x0_list, tree)) assert np.allclose(u0(x0_list, tree), u0(p0_list, tree)) with pytest.raises(ValueError): u0([0, 0, 0, 0], tree) with pytest.raises(ValueError): u0([0, 0], tree)
def facet_length(facet): """ calculate FEniCS facet length """ v_idx = facet.entities(0) v0 = Vertex(facet.mesh(), v_idx[0]) v1 = Vertex(facet.mesh(), v_idx[1]) x0 = v0.point().x() x1 = v1.point().x() y0 = v0.point().y() y1 = v1.point().y() return ((x1 - x0)**2 + (y1 - y0)**2)**0.5
def test_call(R, V, W, Q, mesh): u0 = Function(R) u1 = Function(V) u2 = Function(W) u3 = Function(Q) e1 = Expression("x[0] + x[1] + x[2]", degree=1) e2 = Expression( ("x[0] + x[1] + x[2]", "x[0] - x[1] - x[2]", "x[0] + x[1] + x[2]"), degree=1) e3 = Expression( (("x[0] + x[1] + x[2]", "x[0] - x[1] - x[2]", "x[0] + x[1] + x[2]"), ("x[0]", "x[1]", "x[2]"), ("-x[0]", "-x[1]", "-x[2]")), degree=1) u0.vector()[:] = 1.0 u1.interpolate(e1) u2.interpolate(e2) u3.interpolate(e3) p0 = ((Vertex(mesh, 0).point() + Vertex(mesh, 1).point()) / 2.0).array() x0 = (mesh.geometry.x(0) + mesh.geometry.x(1)) / 2.0 assert numpy.allclose(u0(x0), u0(x0)) assert numpy.allclose(u0(x0), u0(p0)) assert numpy.allclose(u1(x0), u1(x0)) assert numpy.allclose(u1(x0), u1(p0)) assert numpy.allclose(u2(x0)[0], u1(p0)) assert numpy.allclose(u2(x0), u2(p0)) assert numpy.allclose(u3(x0)[:3], u2(x0), rtol=1e-15, atol=1e-15) p0_list = [p for p in p0] x0_list = [x for x in x0] assert numpy.allclose(u0(x0_list), u0(x0_list)) assert numpy.allclose(u0(x0_list), u0(p0_list)) with pytest.raises(ValueError): u0([0, 0, 0, 0]) with pytest.raises(ValueError): u0([0, 0])
def test_call(R, V, W, Q, mesh): from numpy import all, allclose u0 = Function(R) u1 = Function(V) u2 = Function(W) u3 = Function(Q) e0 = Expression("x[0] + x[1] + x[2]", degree=1) e1 = Expression( ("x[0] + x[1] + x[2]", "x[0] - x[1] - x[2]", "x[0] + x[1] + x[2]"), degree=1) e2 = Expression( (("x[0] + x[1] + x[2]", "x[0] - x[1] - x[2]", "x[0] + x[1] + x[2]"), ("x[0]", "x[1]", "x[2]"), ("-x[0]", "-x[1]", "-x[2]")), degree=1) u0.vector()[:] = 1.0 u1.interpolate(e0) u2.interpolate(e1) u3.interpolate(e2) p0 = ((Vertex(mesh, 0).point() + Vertex(mesh, 1).point()) / 2.0).array() x0 = (mesh.geometry.x(0) + mesh.geometry.x(1)) / 2.0 assert round(u0(x0)[0] - u0(x0)[0], 7) == 0 assert round(u0(x0)[0] - u0(p0)[0], 7) == 0 assert round(u1(x0)[0] - u1(x0)[0], 7) == 0 assert round(u1(x0)[0] - u1(p0)[0], 7) == 0 assert round(u2(x0)[0][0] - u1(p0)[0], 7) == 0 assert all(u2(x0) == u2(x0)) assert all(u2(x0) == u2(p0)) assert allclose(u3(x0)[0][:3], u2(x0)[0], rtol=1e-15, atol=1e-15) with pytest.raises(TypeError): u0([0, 0, 0, 0]) with pytest.raises(TypeError): u0([0, 0])
def test_near_evaluations(R, mesh): # Test that we allow point evaluation that are slightly outside u0 = Function(R) u0.vector()[:] = 1.0 a = Vertex(mesh, 0).point().array() offset = 0.99 * DOLFIN_EPS a_shift_x = Point(a[0] - offset, a[1], a[2]).array() assert round(u0(a)[0] - u0(a_shift_x)[0], 7) == 0 a_shift_xyz = Point(a[0] - offset / sqrt(3), a[1] - offset / sqrt(3), a[2] - offset / sqrt(3)).array() assert round(u0(a)[0] - u0(a_shift_xyz)[0], 7) == 0
def test_near_evaluations(R, mesh): # Test that we allow point evaluation that are slightly outside bb_tree = cpp.geometry.BoundingBoxTree(mesh, mesh.geometry.dim) u0 = Function(R) u0.vector().set(1.0) a = Vertex(mesh, 0).point().array() offset = 0.99 * np.finfo(float).eps a_shift_x = Point(a[0] - offset, a[1], a[2]).array() assert round(u0(a, bb_tree)[0] - u0(a_shift_x, bb_tree)[0], 7) == 0 a_shift_xyz = Point(a[0] - offset / math.sqrt(3), a[1] - offset / math.sqrt(3), a[2] - offset / math.sqrt(3)).array() assert round(u0(a, bb_tree)[0] - u0(a_shift_xyz, bb_tree)[0], 7) == 0
def test_Assign(mesh, f): """Assign value of mesh function.""" f = f f[3] = 10 v = Vertex(mesh, 3) assert f[v] == 10
def myassemble(mesh): # Define basis functions and their gradients on the reference elements. def phi0(x): return 1.0 - x[0] - x[1] def phi1(x): return x[0] def phi2(x): return x[1] def f(x): return 1.0 phi = [phi0, phi1, phi2] dphi = np.array(([-1.0, -1.0], [1.0, 0.0], [0.0, 1.0])) # Define quadrature points midpoints = np.array(([0.5, 0.0], [0.5, 0.5], [0.0, 0.5])) N = mesh.num_vertices() A = np.zeros((N, N)) b = np.zeros((N, 1)) # Used to hold cell vertices coord = np.zeros([3, 2]) # Iterate over all cells, adding integral contribution to matrix/vector for c in cells(mesh): # Extract node numbers and vertex coordinates nodes = c.entities(0).astype('int') for i in range(0, 3): v = Vertex(mesh, int(nodes[i])) for j in range(0, 2): coord[i][j] = v.point()[j] # Compute Jacobian of map and area of cell J = np.outer(coord[0, :], dphi[0]) + \ np.outer(coord[1, :], dphi[1]) + \ np.outer(coord[2, :], dphi[2]) dx = 0.5 * abs(np.linalg.det(J)) # Iterate over quadrature points for p in midpoints: # Map p to physical cell x = coord[0, :] * phi[0](p) + \ coord[1, :] * phi[1](p) + \ coord[2, :] * phi[2](p) # Iterate over test functions for i in range(0, 3): v = phi[i](p) dv = np.linalg.solve(J.transpose(), dphi[i]) # Assemble vector (linear form) integral = f(x)*v*dx / 3.0 b[nodes[i]] += integral # Iterate over trial functions for j in range(0, 3): u = phi[j](p) du = np.linalg.solve(J.transpose(), dphi[j]) # Assemble matrix (bilinear form) integral = (np.inner(du, dv)) * dx / 3.0 integral += u * v * dx / 3.0 A[nodes[i], nodes[j]] += integral return A, b
def test_Assign(f, cube): f = f f[3] = 10 v = Vertex(cube, 3) assert f[v] == 10
def as_Vertex(mesh_entity): return Vertex(mesh_entity.mesh(), mesh_entity.index())
def edge_to_vector(edge): v0, v1 = edge.entities(0) mesh = edge.mesh() v0 = Vertex(mesh, v0) v1 = Vertex(mesh, v1) return point_to_array(v1.point() - v0.point())