def assemble_example(): mesh = UnitSquareMesh(5, 5) mesh.init() A, b = myassemble(mesh) print("The norm of A is {}".format(np.linalg.norm(A)))
def test_normal(): "Test that the normal() method is wrapped" mesh = UnitSquareMesh(MPI.comm_world, 4, 4) mesh.init(1) for facet in Facets(mesh): n = facet.normal() nx, ny, nz = n[0], n[1], n[2] assert isinstance(nx, float) assert isinstance(ny, float) assert isinstance(nz, float)
def test_issue_568(): mesh = UnitSquareMesh(MPI.comm_self, 4, 4) cell = Cell(mesh, 0) # This no longer fails because serial mesh now is building facets, using # same pipeline as parallel. # Should throw an error, not just segfault (only works in DEBUG mode!) with pytest.raises(RuntimeError): cell.facet_area(0) # Should work after initializing the connectivity mesh.init(2, 1) cell.facet_area(0)
def test_mesh_function_assign_2D_vertices(): mesh = UnitSquareMesh(MPI.comm_world, 3, 3) mesh.init(0) f = MeshFunction("int", mesh, 0, 25) g = MeshValueCollection("int", mesh, 0) g.assign(f) assert mesh.num_vertices() == 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_mesh_connectivity_lifetime(): """Check that lifetime of MeshConnectivity is bound to underlying mesh topology object""" mesh = UnitSquareMesh(MPI.comm_world, 4, 4) mesh.init(1, 2) topology = mesh.topology # Refcount checks on the MeshConnectivity object rc = sys.getrefcount(topology) connectivity = topology.connectivity(1, 2) assert sys.getrefcount(topology) == rc + 1 del connectivity assert sys.getrefcount(topology) == rc # Refcount checks on the returned connectivities array conn = topology.connectivity(1, 2) rc = sys.getrefcount(conn) cells = conn(0) assert sys.getrefcount(conn) == rc + 1 del cells assert sys.getrefcount(conn) == rc
def test_assign_2D_vertices(): mesh = UnitSquareMesh(MPI.comm_world, 3, 3) mesh.init(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_facets(): mesh = UnitSquareMesh(MPI.comm_world, 3, 3) mesh.init(1) f = MeshFunction("int", mesh, mesh.topology.dim - 1, 25) for cell in Cells(mesh): for i, facet in enumerate(FacetRange(cell)): assert 25 == f[facet] g = MeshValueCollection("int", mesh, 1) g.assign(f) assert mesh.num_facets() == f.size() assert mesh.num_cells() * 3 == g.size() for cell in Cells(mesh): for i, facet in enumerate(FacetRange(cell)): assert 25 == g.get_value(cell.index(), i) f2 = MeshFunction("int", mesh, g, 0) for cell in Cells(mesh): for i, facet in enumerate(FacetRange(cell)): assert f2[facet] == g.get_value(cell.index(), i)
def automated_fenics_assembly(): mesh = UnitSquareMesh(5, 5) mesh.init() A, b = myassemble(mesh) f = Constant(1) V = FunctionSpace(mesh, 'CG', 1) u = TrialFunction(V) v = TestFunction(V) a = inner(grad(u), grad(v)) * dx L = f * v * dx A1, b1 = assemble_system(a, L) B = A - A1.array() print("The norm of B is {}".format(np.linalg.norm(B))) print("A = ") print(A) print("A1 = ") print(A1.array())
def test_ghost_connectivities(mode): # Ghosted mesh meshG = UnitSquareMesh(MPI.comm_world, 4, 4, ghost_mode=mode) meshG.init(1, 2) # Reference mesh, not ghosted, not parallel meshR = UnitSquareMesh(MPI.comm_self, 4, 4, ghost_mode=cpp.mesh.GhostMode.none) meshR.init(1, 2) # Create reference mapping from facet midpoint to cell midpoint reference = {} for facet in Facets(meshR): fidx = facet.index() facet_mp = tuple(facet.midpoint()[:]) reference[facet_mp] = [] for cidx in meshR.topology.connectivity(1, 2)(fidx): cell = Cell(meshR, cidx) cell_mp = tuple(cell.midpoint()[:]) reference[facet_mp].append(cell_mp) # Loop through ghosted mesh and check connectivities allowable_cell_indices = [ c.index() for c in Cells(meshG, cpp.mesh.MeshRangeType.ALL) ] for facet in Facets(meshG, cpp.mesh.MeshRangeType.REGULAR): fidx = facet.index() facet_mp = tuple(facet.midpoint()[:]) assert facet_mp in reference for cidx in meshG.topology.connectivity(1, 2)(fidx): assert cidx in allowable_cell_indices cell = Cell(meshG, cidx) cell_mp = tuple(cell.midpoint()[:]) assert cell_mp in reference[facet_mp]