def test_p4_scalar_vector(): perms = itertools.permutations([1, 2, 3, 4]) for p in perms: cells = numpy.array([[0, 1, 2, 3], p], dtype=numpy.int64) points = numpy.array( [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0], [1.0, 1.0, 1.0]], dtype=numpy.float64) mesh = Mesh(MPI.comm_world, CellType.Type.tetrahedron, points, cells, [], GhostMode.none) mesh.geometry.coord_mapping = fem.create_coordinate_map(mesh) Q = FunctionSpace(mesh, ("CG", 4)) F0 = interpolate(Expression("x[0]", degree=4), Q) F1 = interpolate(Expression("x[1]", degree=4), Q) F2 = interpolate(Expression("x[2]", degree=4), Q) pts = numpy.array([[0.4, 0.4, 0.1], [0.4, 0.1, 0.4], [0.1, 0.4, 0.4]]) for pt in pts: assert numpy.isclose(pt[0], F0(pt)[0]) assert numpy.isclose(pt[1], F1(pt)[0]) assert numpy.isclose(pt[2], F2(pt)[0]) V = VectorFunctionSpace(mesh, ("CG", 4)) F = interpolate(Expression(("x[0]", "x[1]", "0.0"), degree=4), V) for pt in pts: result = F(pt) assert numpy.isclose(pt[0], result[0]) assert numpy.isclose(pt[1], result[1]) assert numpy.isclose(0.0, result[2])
def UnitSquareMesh(comm, nx, ny, cell_type=cpp.mesh.CellType.Type.triangle, ghost_mode=cpp.mesh.GhostMode.none, diagonal="right"): """Create a mesh of a unit square with coordinate mapping attached Parameters ---------- comm MPI communicator nx Number of cells in "x" direction ny Number of cells in "y" direction diagonal Direction of diagonal """ mesh = RectangleMesh( comm, [numpy.array([0.0, 0.0, 0.0]), numpy.array([1.0, 1.0, 0.0])], [nx, ny], cell_type, ghost_mode, diagonal) mesh.geometry.coord_mapping = fem.create_coordinate_map(mesh) return mesh
def UnitCubeMesh(comm, nx, ny, nz, cell_type=cpp.mesh.CellType.Type.tetrahedron, ghost_mode=cpp.mesh.GhostMode.none): """Create a mesh of a unit cube with coordinate mapping attached Parameters ---------- comm MPI communicator nx Number of cells in "x" direction ny Number of cells in "y" direction nz Number of cells in "z" direction """ mesh = BoxMesh( comm, [numpy.array([0.0, 0.0, 0.0]), numpy.array([1.0, 1.0, 1.0])], [nx, ny, nz], cell_type, ghost_mode) mesh.geometry.coord_mapping = fem.create_coordinate_map(mesh) return mesh
def test_nullspace_check(mesh, degree): V = VectorFunctionSpace(mesh, ('Lagrange', degree)) u, v = TrialFunction(V), TestFunction(V) mesh.geometry.coord_mapping = fem.create_coordinate_map(mesh) E, nu = 2.0e2, 0.3 mu = E / (2.0 * (1.0 + nu)) lmbda = E * nu / ((1.0 + nu) * (1.0 - 2.0 * nu)) def sigma(w, gdim): return 2.0 * mu * ufl.sym(grad(w)) + lmbda * ufl.tr( grad(w)) * ufl.Identity(gdim) a = inner(sigma(u, mesh.geometry.dim), grad(v)) * dx zero = Function(V) L = inner(zero, v) * dx # Assemble matrix and create compatible vector A, L = assembling.assemble_system(a, L, []) # Create null space basis and test null_space = build_elastic_nullspace(V) assert null_space.in_nullspace(A, tol=1.0e-8) null_space.orthonormalize() assert null_space.in_nullspace(A, tol=1.0e-8) # Create incorrect null space basis and test null_space = build_broken_elastic_nullspace(V) assert not null_space.in_nullspace(A, tol=1.0e-8) null_space.orthonormalize() assert not null_space.in_nullspace(A, tol=1.0e-8)
def test_read_write_p2_function(tempdir): mesh = cpp.generation.UnitDiscMesh.create(MPI.comm_world, 3, cpp.mesh.GhostMode.none) cmap = fem.create_coordinate_map(mesh.ufl_domain()) mesh.geometry.coord_mapping = cmap Q = FunctionSpace(mesh, ("Lagrange", 2)) F = Function(Q) if has_petsc_complex: F.interpolate(Expression("x[0] + j*x[0]", degree=1)) else: F.interpolate(Expression("x[0]", degree=1)) filename = os.path.join(tempdir, "tri6_function.xdmf") with XDMFFile(mesh.mpi_comm(), filename, encoding=XDMFFile.Encoding.HDF5) as xdmf: xdmf.write(F) Q = VectorFunctionSpace(mesh, ("Lagrange", 1)) F = Function(Q) if has_petsc_complex: F.interpolate(Expression(("x[0] + j*x[0]", "x[1] + j*x[1]"), degree=1)) else: F.interpolate(Expression(("x[0]", "x[1]"), degree=1)) filename = os.path.join(tempdir, "tri6_vector_function.xdmf") with XDMFFile(mesh.mpi_comm(), filename, encoding=XDMFFile.Encoding.HDF5) as xdmf: xdmf.write(F)
def check(mesh, edges): """Compute the physical coordinates of the dofs on the given local edges""" V = FunctionSpace(mesh, ("Lagrange", 3)) assert len(edges) == 2 dofmap = V.dofmap dofs = [dofmap.cell_dofs(c) for c in range(len(edges))] edge_dofs_local = [dofmap.dof_layout.entity_dofs(1, e) for e in edges] for edofs in edge_dofs_local: assert len(edofs) == 2 edge_dofs = [dofs[0][edge_dofs_local[0]], dofs[1][edge_dofs_local[1]]] assert set(edge_dofs[0]) == set(edge_dofs[1]) X = V.element.dof_reference_coordinates() coord_dofs = mesh.coordinate_dofs().entity_points() x_g = mesh.geometry.points x_dofs = [] cmap = fem.create_coordinate_map(mesh.ufl_domain()) for c in range(len(edges)): x_coord_new = np.zeros([3, 2]) for v in range(3): x_coord_new[v] = x_g[coord_dofs[c, v], :2] x = X.copy() cmap.compute_physical_coordinates(x, X, x_coord_new) x_dofs.append(x[edge_dofs_local[c]]) return x_dofs
def UnitCubeMesh(comm, nx, ny, nz, cell_type=cpp.mesh.CellType.Type.tetrahedron, ghost_mode=cpp.mesh.GhostMode.none): """Create a mesh of a unit cube with coordinate mapping attached Parameters ---------- comm MPI communicator nx Number of cells in "x" direction ny Number of cells in "y" direction nz Number of cells in "z" direction """ from dolfin.geometry import Point mesh = BoxMesh( comm, [Point(0.0, 0.0, 0.0)._cpp_object, Point(1.0, 1.0, 1.0)._cpp_object], [nx, ny, nz], cell_type, ghost_mode) mesh.geometry.coord_mapping = fem.create_coordinate_map(mesh) return mesh
def UnitSquareMesh(comm, nx, ny, cell_type=cpp.mesh.CellType.Type.triangle, ghost_mode=cpp.mesh.GhostMode.none, diagonal="right"): """Create a mesh of a unit square with coordinate mapping attached Parameters ---------- comm MPI communicator nx Number of cells in "x" direction ny Number of cells in "y" direction diagonal Direction of diagonal """ from dolfin.geometry import Point mesh = RectangleMesh( comm, [Point(0.0, 0.0)._cpp_object, Point(1.0, 1.0)._cpp_object], [nx, ny], cell_type, ghost_mode, diagonal) mesh.geometry.coord_mapping = fem.create_coordinate_map(mesh) return mesh
def test_p4_scalar_vector(): perms = itertools.permutations([1, 2, 3, 4]) for p in perms: cells = numpy.array([[0, 1, 2, 3], p], dtype=numpy.int64) points = numpy.array( [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0], [1.0, 1.0, 1.0]], dtype=numpy.float64) mesh = Mesh(MPI.comm_world, CellType.Type.tetrahedron, points, cells, [], GhostMode.none) mesh.geometry.coord_mapping = fem.create_coordinate_map(mesh) Q = FunctionSpace(mesh, ("CG", 4)) @function.expression.numba_eval def x0(values, x, cell_idx): values[:, 0] = x[:, 0] @function.expression.numba_eval def x1(values, x, cell_idx): values[:, 0] = x[:, 1] @function.expression.numba_eval def x2(values, x, cell_idx): values[:, 0] = x[:, 2] F0 = interpolate(Expression(x0), Q) F1 = interpolate(Expression(x1), Q) F2 = interpolate(Expression(x2), Q) tree = cpp.geometry.BoundingBoxTree(mesh, mesh.geometry.dim) pts = numpy.array([[0.4, 0.4, 0.1], [0.4, 0.1, 0.4], [0.1, 0.4, 0.4]]) for pt in pts: assert numpy.isclose(pt[0], F0(pt, tree)[0]) assert numpy.isclose(pt[1], F1(pt, tree)[0]) assert numpy.isclose(pt[2], F2(pt, tree)[0]) V = VectorFunctionSpace(mesh, ("CG", 4)) @function.expression.numba_eval def x0x10(values, x, cell_idx): values[:, 0] = x[:, 0] values[:, 1] = x[:, 1] values[:, 2] = 0.0 F = interpolate(Expression(x0x10, shape=(3,)), V) tree = cpp.geometry.BoundingBoxTree(mesh, mesh.geometry.dim) for pt in pts: result = F(pt, tree) assert numpy.isclose(pt[0], result[0]) assert numpy.isclose(pt[1], result[1]) assert numpy.isclose(0.0, result[2])
def test_read_write_p2_function(tempdir): mesh = cpp.generation.UnitDiscMesh.create(MPI.comm_world, 3, cpp.mesh.GhostMode.none) cmap = fem.create_coordinate_map(mesh.ufl_domain()) mesh.geometry.coord_mapping = cmap Q = FunctionSpace(mesh, ("Lagrange", 2)) F = Function(Q) if has_petsc_complex: @function.expression.numba_eval def expr_eval(values, x, cell_idx): values[:, 0] = x[:, 0] + 1.0j * x[:, 0] F.interpolate(Expression(expr_eval)) else: @function.expression.numba_eval def expr_eval(values, x, cell_idx): values[:, 0] = x[:, 0] F.interpolate(Expression(expr_eval)) filename = os.path.join(tempdir, "tri6_function.xdmf") with XDMFFile(mesh.mpi_comm(), filename, encoding=XDMFFile.Encoding.HDF5) as xdmf: xdmf.write(F) Q = VectorFunctionSpace(mesh, ("Lagrange", 1)) F = Function(Q) if has_petsc_complex: @function.expression.numba_eval def expr_eval(values, x, cell_idx): values[:, 0] = x[:, 0] + 1.0j * x[:, 0] values[:, 1] = x[:, 1] + 1.0j * x[:, 1] F.interpolate(Expression(expr_eval, shape=(2, ))) else: @function.expression.numba_eval def expr_eval(values, x, cell_idx): values[:, 0] = x[:, 0] values[:, 1] = x[:, 1] F.interpolate(Expression(expr_eval, shape=(2, ))) filename = os.path.join(tempdir, "tri6_vector_function.xdmf") with XDMFFile(mesh.mpi_comm(), filename, encoding=XDMFFile.Encoding.HDF5) as xdmf: xdmf.write(F)
def UnitIntervalMesh(comm, nx, ghost_mode=cpp.mesh.GhostMode.none): """Create a mesh on the unit interval with coordinate mapping attached Parameters ---------- comm MPI communicator nx Number of cells """ mesh = IntervalMesh(comm, nx, [0.0, 1.0], ghost_mode) mesh.geometry.coord_mapping = fem.create_coordinate_map(mesh) return mesh
def UnitSquareMesh(comm, nx, ny, cell_type=cpp.mesh.CellType.Type.triangle, ghost_mode=cpp.mesh.GhostMode.none, diagonal="right"): """Create a mesh of a unit square""" from dolfin.geometry import Point mesh = cpp.generation.RectangleMesh.create( comm, [Point(0.0, 0.0)._cpp_object, Point(1.0, 1.0)._cpp_object], [nx, ny], cell_type, ghost_mode, diagonal) mesh.geometry.coord_mapping = fem.create_coordinate_map(mesh) return mesh
def UnitCubeMesh(comm, nx, ny, nz, cell_type=cpp.mesh.CellType.Type.tetrahedron, ghost_mode=cpp.mesh.GhostMode.none): """Create a mesh of a unit cube""" from dolfin.geometry import Point mesh = cpp.generation.BoxMesh.create( comm, [Point(0.0, 0.0, 0.0)._cpp_object, Point(1.0, 1.0, 1.0)._cpp_object], [nx, ny, nz], cell_type, ghost_mode) mesh.geometry.coord_mapping = fem.create_coordinate_map(mesh) return mesh
def test_read_write_p2_function(tempdir): mesh = cpp.generation.UnitDiscMesh.create(MPI.comm_world, 3, cpp.mesh.GhostMode.none) cmap = fem.create_coordinate_map(mesh.ufl_domain()) mesh.geometry.coord_mapping = cmap Q = FunctionSpace(mesh, ("Lagrange", 2)) F = Function(Q) if has_petsc_complex: def expr_eval(x): return x[:, 0] + 1.0j * x[:, 0] F.interpolate(expr_eval) else: def expr_eval(x): return x[:, 0] F.interpolate(expr_eval) filename = os.path.join(tempdir, "tri6_function.xdmf") with XDMFFile(mesh.mpi_comm(), filename, encoding=XDMFFile.Encoding.HDF5) as xdmf: xdmf.write(F) Q = VectorFunctionSpace(mesh, ("Lagrange", 1)) F = Function(Q) if has_petsc_complex: def expr_eval(x): return x + 1.0j * x F.interpolate(expr_eval) else: def expr_eval(x): return x F.interpolate(expr_eval) filename = os.path.join(tempdir, "tri6_vector_function.xdmf") with XDMFFile(mesh.mpi_comm(), filename, encoding=XDMFFile.Encoding.HDF5) as xdmf: xdmf.write(F)
def read_mesh(self, mpi_comm, data_path: str, use_partition_from_file: bool, ghost_mode): mesh = self._cpp_object.read_mesh(mpi_comm, data_path, use_partition_from_file, ghost_mode) mesh.geometry.coord_mapping = fem.create_coordinate_map(mesh) return mesh
def read_mesh(self, mpi_comm, ghost_mode): mesh = self._cpp_object.read_mesh(mpi_comm, ghost_mode) mesh.geometry.coord_mapping = fem.create_coordinate_map(mesh) return mesh
geom.add_physical(ps2, label="OBSTACLE") #print("\n".join(geom._GMSH_CODE)) msh = generate_mesh(geom) points, cells, cell_data, boundary = msh.points, msh.cells, msh.cell_data, msh.field_data mesh = dolfin.cpp.mesh.Mesh( dolfin.MPI.comm_world, dolfin.cpp.mesh.CellType.Type.triangle, points[:, :2], # Converting to 2D cells['triangle'], [], dolfin.cpp.mesh.GhostMode.none) mesh.geometry.coord_mapping = fem.create_coordinate_map(mesh) mvc_boundaries = dolfin.MeshValueCollection("size_t", mesh, 1, cells["line"], cell_data["line"]['gmsh:physical']) mvc_subdomain = dolfin.MeshValueCollection( "size_t", mesh, 2, cells["triangle"], cell_data["triangle"]['gmsh:physical']) print("Constructing MeshFunction from MeshValueCollection") domains = dolfin.cpp.mesh.MeshFunctionSizet(mesh, mvc_subdomain, 0) boundaries = dolfin.cpp.mesh.MeshFunctionSizet(mesh, mvc_boundaries, 0) #print("Boundaries") #print(mvc_boundaries.values()) #print(boundaries.array())
def UnitIntervalMesh(comm, nx, ghost_mode=cpp.mesh.GhostMode.none): """Create a mesh on the unit interval""" mesh = cpp.generation.IntervalMesh.create(comm, nx, [0.0, 1.0], ghost_mode) mesh.geometry.coord_mapping = fem.create_coordinate_map(mesh) return mesh