def test_save_2d_scalar(tempdir, cell_type): mesh = UnitSquareMesh(MPI.COMM_WORLD, 16, 16, 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_2d_tensor(tempdir): mesh = UnitSquareMesh(MPI.COMM_WORLD, 16, 16) 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.) with u.vector.localForm() as loc: loc.set(2.0) vtk.write_function(u, 1.)
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_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_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_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 = 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_scalar(tempfile, file_options): mesh = UnitIntervalMesh(MPI.comm_world, 32) 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_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_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_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_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_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_dict[element].shape[0])) grp.create_dataset("coordinates", data=msh.points) top = grp.create_dataset("topology", data=msh.cells_dict[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_mesh(tempdir): filename = os.path.join(tempdir, "mesh.pvd") mesh = UnitIntervalMesh(MPI.COMM_WORLD, 32) with VTKFile(MPI.COMM_WORLD, filename, "w") as vtk: vtk.write_mesh(mesh) vtk.write_mesh(mesh, 1)
def test_save_3d_mesh(tempdir, cell_type): mesh = create_unit_cube(MPI.COMM_WORLD, 8, 8, 8, cell_type=cell_type) filename = os.path.join(tempdir, f"mesh_{cell_type.name}.pvd") with VTKFile(MPI.COMM_WORLD, filename, "w") as vtk: vtk.write_mesh(mesh, 0.) vtk.write_mesh(mesh, 2.)
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.)