def test_save_1d_tensor(tempfile, file_options): mesh = UnitIntervalMesh(MPI.COMM_WORLD, 32) u = Function(TensorFunctionSpace(mesh, ("Lagrange", 2))) u.vector.set(1) VTKFile(tempfile + "u.pvd").write(u) for file_option in file_options: VTKFile(tempfile + "u.pvd", file_option).write(u)
def test_save_1d_vector(tempfile, file_options): mesh = UnitIntervalMesh(MPI.comm_world, 32) u = Function(VectorFunctionSpace(mesh, ("Lagrange", 2))) u.vector.set(1.0) VTKFile(tempfile + "u.pvd").write(u) for file_option in file_options: VTKFile(tempfile + "u.pvd", file_option).write(u)
def test_save_vtk_mixed(tempdir): mesh = create_unit_cube(MPI.COMM_WORLD, 3, 3, 3) P2 = ufl.VectorElement("Lagrange", mesh.ufl_cell(), 1) P1 = ufl.FiniteElement("Lagrange", mesh.ufl_cell(), 1) W = FunctionSpace(mesh, P2 * P1) V1 = FunctionSpace(mesh, P1) V2 = FunctionSpace(mesh, P2) U = Function(W) U.sub(0).interpolate(lambda x: np.vstack((x[0], 0.2 * x[1], np.zeros_like(x[0])))) U.sub(1).interpolate(lambda x: 0.5 * x[0]) U1, U2 = Function(V1), Function(V2) U1.interpolate(U.sub(1)) U2.interpolate(U.sub(0)) U2.name = "u" U1.name = "p" filename = os.path.join(tempdir, "u.pvd") with VTKFile(mesh.comm, filename, "w") as vtk: vtk.write_function([U2, U1], 0.) with VTKFile(mesh.comm, filename, "w") as vtk: vtk.write_function([U1, U2], 0.) Up = U.sub(1) Up.name = "psub" with pytest.raises(RuntimeError): with VTKFile(mesh.comm, filename, "w") as vtk: vtk.write_function([U2, Up, U1], 0) with pytest.raises(RuntimeError): with VTKFile(mesh.comm, filename, "w") as vtk: vtk.write_function([U.sub(i) for i in range(W.num_sub_spaces)], 0)
def test_save_1d_mesh(tempfile, file_options): mesh = UnitIntervalMesh(MPI.COMM_WORLD, 32) VTKFile(tempfile + "mesh.pvd").write(mesh) f = VTKFile(tempfile + "mesh.pvd") f.write(mesh, 0.) f.write(mesh, 1.) for file_option in file_options: VTKFile(tempfile + "mesh.pvd", file_option).write(mesh)
def test_save_3d_mesh(tempfile, file_options): mesh = UnitCubeMesh(MPI.COMM_WORLD, 8, 8, 8) VTKFile(tempfile + "mesh.pvd").write(mesh) f = VTKFile(tempfile + "mesh.pvd") f.write(mesh, 0.) f.write(mesh, 1.) for file_option in file_options: VTKFile(tempfile + "mesh.pvd", file_option).write(mesh)
def test_save_2d_mesh(tempfile, file_options): mesh = UnitSquareMesh(MPI.comm_world, 32, 32) VTKFile(tempfile + "mesh.pvd").write(mesh) f = VTKFile(tempfile + "mesh.pvd") f.write(mesh, 0.) f.write(mesh, 1.) for file_option in file_options: VTKFile(tempfile + "mesh.pvd", file_option).write(mesh)
def test_save_1d_meshfunctions(tempfile, mesh_function_types, file_options, type_conv): mesh = UnitIntervalMesh(MPI.comm_world, 32) for d in range(mesh.topology.dim + 1): for t in mesh_function_types: mf = MeshFunction(t, mesh, mesh.topology.dim - d, type_conv[t](1)) VTKFile(tempfile + "mf.pvd").write(mf) f = VTKFile(tempfile + "mf.pvd") f.write(mf, 0.) f.write(mf, 1.)
def test_save_2d_tensor(tempfile, file_options): mesh = UnitSquareMesh(MPI.comm_world, 16, 16) u = Function(TensorFunctionSpace(mesh, ("Lagrange", 2))) u.vector.set(1) VTKFile(tempfile + "u.pvd").write(u) f = VTKFile(tempfile + "u.pvd") f.write(u, 0.) f.write(u, 1.) for file_option in file_options: VTKFile(tempfile + "u.pvd", file_option).write(u)
def test_save_3d_vector(tempfile, file_options): mesh = UnitCubeMesh(MPI.comm_world, 8, 8, 8) u = Function(VectorFunctionSpace(mesh, "Lagrange", 2)) u.vector.set(1) VTKFile(tempfile + "u.pvd").write(u) f = VTKFile(tempfile + "u.pvd") f.write(u, 0.) f.write(u, 1.) for file_option in file_options: VTKFile(tempfile + "u.pvd", file_option).write(u)
def test_save_2d_scalar(tempfile, file_options): mesh = UnitSquareMesh(MPI.COMM_WORLD, 16, 16) u = Function(FunctionSpace(mesh, ("Lagrange", 2))) u.vector.set(1.0) VTKFile(tempfile + "u.pvd").write(u) f = VTKFile(tempfile + "u.pvd") f.write(u, 0.) f.write(u, 1.) for file_option in file_options: VTKFile(tempfile + "u.pvd", file_option).write(u)
def test_save_3d_tensor(tempfile, file_options): mesh = UnitCubeMesh(MPI.COMM_WORLD, 8, 8, 8) u = Function(TensorFunctionSpace(mesh, ("Lagrange", 2))) u.vector.set(1) VTKFile(tempfile + "u.pvd").write(u) f = VTKFile(tempfile + "u.pvd") f.write(u, 0.) f.write(u, 1.) for file_option in file_options: VTKFile(tempfile + "u.pvd", file_option).write(u)
def test_save_3d_meshfunctions(tempfile, mesh_function_types, file_options, type_conv, cell_types_3D): mesh = UnitCubeMesh(MPI.comm_world, 8, 8, 8) for d in range(mesh.topology.dim + 1): for t in mesh_function_types: for cell_type in cell_types_3D: mf = MeshFunction(t, mesh, mesh.topology.dim - d, type_conv[t](1)) VTKFile(tempfile + "mf_{0:d}_{1:s}.pvd".format(mesh.topology.dim - d, str(cell_type).split(".")[-1])).write(mf) f = VTKFile(tempfile + "mf{0:d}_{1:s}.pvd".format(mesh.topology.dim - d, str(cell_type).split(".")[-1])) f.write(mf, 0.) f.write(mf, 1.)
def test_save_2d_mixed(tempdir): mesh = UnitCubeMesh(MPI.COMM_WORLD, 3, 3, 3) P2 = ufl.VectorElement("Lagrange", mesh.ufl_cell(), 2) P1 = ufl.FiniteElement("Lagrange", mesh.ufl_cell(), 1) TH = P2 * P1 W = FunctionSpace(mesh, TH) def vec_func(x): vals = np.zeros((3, x.shape[1])) vals[0] = x[0] vals[1] = 0.2 * x[1] return vals def scal_func(x): return 0.5 * x[0] U = Function(W) U.sub(0).interpolate(vec_func) U.sub(1).interpolate(scal_func) U.vector.ghostUpdate(addv=PETSc.InsertMode.INSERT, mode=PETSc.ScatterMode.FORWARD) filename = os.path.join(tempdir, "u.pvd") with VTKFile(mesh.mpi_comm(), filename, "w") as vtk: vtk.write_function([U.sub(i) for i in range(W.num_sub_spaces())], 0.)
def test_HDF5_io(tempdir, order, element): pytest.importorskip("pygmsh") h5py = pytest.importorskip("h5py") from pygmsh.opencascade import Geometry from pygmsh import generate_mesh # Generate a sphere with gmsh with tetrahedral elements geo = Geometry() geo.add_raw_code("Mesh.Algorithm = 2;") geo.add_raw_code("Mesh.Algorithm3D = 10;") geo.add_raw_code("Mesh.ElementOrder = {0:d};".format(order)) geo.add_ball([0, 0, 0], 1, char_length=0.3) geo.add_raw_code("Physical Volume (1) = {1};") msh = generate_mesh(geo, verbose=False, dim=3) # Write gmsh to HDF5 filename = os.path.join(tempdir, "mesh_order{0:d}.h5".format(order)) f = h5py.File(filename, "w", driver='mpio', comm=MPI4PY.COMM_WORLD) grp = f.create_group("my_mesh") grp.create_dataset("cell_indices", data=range(msh.cells[element].shape[0])) grp.create_dataset("coordinates", data=msh.points) top = grp.create_dataset("topology", data=msh.cells[element]) top.attrs["celltype"] = np.bytes_('tetrahedron') f.close() # Read mesh from HDF5 mesh_file = HDF5File(MPI.comm_world, filename, "r") mesh = mesh_file.read_mesh("/my_mesh", False, cpp.mesh.GhostMode.none) mesh_file.close() # Save mesh with VTK outfile = os.path.join(tempdir, "mesh{0:d}.pvd".format(order)) VTKFile(outfile).write(mesh)
def test_save_1d_scalar(tempdir): mesh = create_unit_interval(MPI.COMM_WORLD, 32) u = Function(FunctionSpace(mesh, ("Lagrange", 2))) u.interpolate(lambda x: x[0]) filename = os.path.join(tempdir, "u.pvd") with VTKFile(MPI.COMM_WORLD, filename, "w") as vtk: vtk.write_function(u, 0.)
def test_save_3d_tensor(tempdir): mesh = create_unit_cube(MPI.COMM_WORLD, 8, 8, 8) u = Function(TensorFunctionSpace(mesh, ("Lagrange", 2))) u.x.array[:] = 1.0 filename = os.path.join(tempdir, "u.pvd") with VTKFile(mesh.comm, filename, "w") as vtk: vtk.write_function(u, 0.)
def test_save_1d_tensor(tempdir): mesh = create_unit_interval(MPI.COMM_WORLD, 32) element = ufl.TensorElement("Lagrange", mesh.ufl_cell(), 2, shape=(2, 2)) u = Function(FunctionSpace(mesh, element)) u.x.array[:] = 1.0 filename = os.path.join(tempdir, "u.pvd") with VTKFile(mesh.comm, filename, "w") as vtk: vtk.write_function(u, 0.)
def test_save_1d_tensor(tempdir): mesh = UnitIntervalMesh(MPI.COMM_WORLD, 32) element = ufl.TensorElement("Lagrange", mesh.ufl_cell(), 2, shape=(2, 2)) u = Function(FunctionSpace(mesh, element)) with u.vector.localForm() as loc: loc.set(1.0) filename = os.path.join(tempdir, "u.pvd") with VTKFile(mesh.mpi_comm(), filename, "w") as vtk: vtk.write_function(u, 0.)
def test_save_3d_scalar(tempdir, cell_type): mesh = create_unit_cube(MPI.COMM_WORLD, 8, 8, 8, cell_type=cell_type) u = Function(FunctionSpace(mesh, ("Lagrange", 2))) u.x.array[:] = 1.0 filename = os.path.join(tempdir, "u.pvd") with VTKFile(MPI.COMM_WORLD, filename, "w") as vtk: vtk.write_function(u, 0.) vtk.write_function(u, 1.)
def test_save_3d_tensor(tempdir): mesh = UnitCubeMesh(MPI.COMM_WORLD, 8, 8, 8) u = Function(TensorFunctionSpace(mesh, ("Lagrange", 2))) with u.vector.localForm() as loc: loc.set(1.0) filename = os.path.join(tempdir, "u.pvd") with VTKFile(mesh.mpi_comm(), filename, "w") as vtk: vtk.write_function(u, 0.)
def test_save_vtk_cell_point(tempdir): """Test writing cell-wise and point-wise data""" mesh = create_unit_cube(MPI.COMM_WORLD, 3, 3, 3) P2 = ufl.VectorElement("Lagrange", mesh.ufl_cell(), 1) P1 = ufl.FiniteElement("Discontinuous Lagrange", mesh.ufl_cell(), 0) V2, V1 = FunctionSpace(mesh, P2), FunctionSpace(mesh, P1) U2, U1 = Function(V2), Function(V1) U2.interpolate(lambda x: np.vstack((x[0], 0.2 * x[1], np.zeros_like(x[0])))) U1.interpolate(lambda x: 0.5 * x[0]) U2.name = "A" U1.name = "B" filename = os.path.join(tempdir, "u.pvd") with VTKFile(mesh.comm, filename, "w") as vtk: vtk.write_function([U2, U1], 0.) with VTKFile(mesh.comm, filename, "w") as vtk: vtk.write_function((U1, U2), 0.)
def test_save_3d_scalar(tempdir, cell_type): mesh = UnitCubeMesh(MPI.COMM_WORLD, 8, 8, 8, cell_type=cell_type) u = Function(FunctionSpace(mesh, ("Lagrange", 2))) with u.vector.localForm() as loc: loc.set(1.0) filename = os.path.join(tempdir, "u.pvd") with VTKFile(MPI.COMM_WORLD, filename, "w") as vtk: vtk.write_function(u, 0.) vtk.write_function(u, 1.)
def test_save_1d_scalar(tempdir): mesh = UnitIntervalMesh(MPI.COMM_WORLD, 32) def f(x): return x[0] u = Function(FunctionSpace(mesh, ("CG", 2))) u.interpolate(f) u.vector.ghostUpdate(addv=PETSc.InsertMode.INSERT, mode=PETSc.ScatterMode.FORWARD) filename = os.path.join(tempdir, "u.pvd") with VTKFile(MPI.COMM_WORLD, filename, "w") as vtk: vtk.write_function(u, 0.) vtk.write_function(u, 1.)
def test_save_1d_vector(tempdir): mesh = create_unit_interval(MPI.COMM_WORLD, 32) def f(x): vals = np.zeros((2, x.shape[1])) vals[0] = x[0] vals[1] = 2 * x[0] * x[0] return vals element = ufl.VectorElement("Lagrange", mesh.ufl_cell(), 2, dim=2) u = Function(FunctionSpace(mesh, element)) u.interpolate(f) filename = os.path.join(tempdir, "u.pvd") with VTKFile(MPI.COMM_WORLD, filename, "w") as vtk: vtk.write_function(u, 0.)
def test_save_2d_vector_CG2(tempdir): points = np.array([[0, 0], [1, 0], [1, 2], [0, 2], [1 / 2, 0], [1, 1], [1 / 2, 2], [0, 1], [1 / 2, 1]]) points = np.array([[0, 0], [1, 0], [0, 2], [0.5, 1], [0, 1], [0.5, 0], [1, 2], [0.5, 2], [1, 1]]) cells = np.array([[0, 1, 2, 3, 4, 5], [1, 6, 2, 7, 3, 8]]) domain = ufl.Mesh(ufl.VectorElement("Lagrange", "triangle", 2)) mesh = create_mesh(MPI.COMM_WORLD, cells, points, domain) u = Function(VectorFunctionSpace(mesh, ("Lagrange", 2))) u.interpolate(lambda x: np.vstack((x[0], x[1]))) filename = os.path.join(tempdir, "u.pvd") with VTKFile(mesh.comm, filename, "w") as vtk: vtk.write_function(u, 0.)
def test_save_2d_vector(tempdir, cell_type): mesh = UnitSquareMesh(MPI.COMM_WORLD, 16, 16, cell_type=cell_type) u = Function(VectorFunctionSpace(mesh, ("Lagrange", 1))) def f(x): vals = np.zeros((2, x.shape[1])) vals[0] = x[0] vals[1] = 2 * x[0] * x[1] return vals u.interpolate(f) u.vector.ghostUpdate(addv=PETSc.InsertMode.INSERT, mode=PETSc.ScatterMode.FORWARD) filename = os.path.join(tempdir, "u.pvd") with VTKFile(MPI.COMM_WORLD, filename, "w") as vtk: vtk.write_function(u, 0.)
def test_save_2d_vector(tempdir, cell_type): mesh = create_unit_square(MPI.COMM_WORLD, 16, 16, cell_type=cell_type) u = Function(VectorFunctionSpace(mesh, ("Lagrange", 1))) def f(x): vals = np.zeros((2, x.shape[1])) vals[0] = x[0] vals[1] = 2 * x[0] * x[1] return vals u.interpolate(f) filename = os.path.join(tempdir, "u.pvd") with VTKFile(MPI.COMM_WORLD, filename, "w") as vtk: vtk.write_function(u, 0.) vtk.write_function(u, 1.)
def test_save_1d_vector(tempdir): mesh = UnitIntervalMesh(MPI.COMM_WORLD, 32) def f(x): vals = np.zeros((2, x.shape[1])) vals[0] = x[0] vals[1] = 2 * x[0] * x[0] return vals element = ufl.VectorElement("Lagrange", mesh.ufl_cell(), 2, dim=2) u = Function(FunctionSpace(mesh, element)) u.interpolate(f) u.vector.ghostUpdate(addv=PETSc.InsertMode.INSERT, mode=PETSc.ScatterMode.FORWARD) filename = os.path.join(tempdir, "u.pvd") with VTKFile(MPI.COMM_WORLD, filename, "w") as vtk: vtk.write_function(u, 0.)
def test_save_2d_vector_CG2(tempdir): points = np.array([[0, 0], [1, 0], [1, 2], [0, 2], [1 / 2, 0], [1, 2 / 2], [1 / 2, 2], [0, 2 / 2], [1 / 2, 2 / 2]]) points = np.array([[0, 0], [1, 0], [0, 2], [0.5, 1], [0, 1], [0.5, 0], [1, 2], [0.5, 2], [1, 1]]) cells = np.array([[0, 1, 2, 3, 4, 5], [1, 6, 2, 7, 3, 8]]) domain = ufl.Mesh(ufl.VectorElement("Lagrange", "triangle", 2)) mesh = create_mesh(MPI.COMM_WORLD, cells, points, domain) u = Function(VectorFunctionSpace(mesh, ("Lagrange", 2))) def func(x): vals = np.zeros((2, x.shape[1])) vals[0] = x[0] vals[1] = x[1] return vals u.interpolate(func) filename = os.path.join(tempdir, "u.pvd") with VTKFile(mesh.mpi_comm(), filename, "w") as vtk: vtk.write_function(u, 0.)
def test_save_3d_mesh(tempdir, cell_type): mesh = UnitCubeMesh(MPI.COMM_WORLD, 8, 8, 8, cell_type=cell_type) filename = os.path.join(tempdir, f"mesh_{cpp.mesh.to_string(cell_type)}.pvd") with VTKFile(MPI.COMM_WORLD, filename, "w") as vtk: vtk.write_mesh(mesh, 0.) vtk.write_mesh(mesh, 2.)