def sequential_setup(uvw_type, vtk_type, filename, args, fixtures): (coords, r, e_r, field, order), compress, \ vtk_format, comp_order, tmp_path = fixtures dim = r.ndim out_name = tmp_path / filename rect = uvw_type(out_name, *args(coords), dim * [0], compression=compress, byte_order=order) rect.addPointData( DataArray( r, range(dim), 'point', components_order=comp_order.param ), vtk_format=vtk_format) \ .addCellData(DataArray( e_r, range(dim), 'cell', components_order=comp_order.param ), vtk_format=vtk_format) \ .addFieldData(DataArray( field, [0], 'field', components_order=comp_order.param ), vtk_format=vtk_format).write() reader = vtk_type() vtk_r, vtk_e_r, vtk_f = get_vtk_data(reader, out_name) vtk_r = vtk_r.reshape(r.shape, order='F') vtk_e_r = vtk_e_r.reshape(e_r.shape, order='F') \ .transpose(comp_order.transp(dim)) assert all(vtk_r == r) assert all(vtk_e_r == e_r) assert all(vtk_f == field)
def test_array_dimensions(): x = np.array([0, 1]) with pytest.raises(ValueError): DataArray(x, range(2)) with pytest.raises(ValueError): DataArray(x, range(1), components_order=2)
def test_rectilinear_grid(field_data, compression_fixture, format_fixture, ordering_fixture): coords, r, e_r, field, order = field_data dim = r.ndim f = io.StringIO() compress = compression_fixture.param format = format_fixture.param rect = RectilinearGrid(f, coords, compression=compress, byte_order=order) rect.addPointData( DataArray(r, range(dim), 'point', ordering_fixture.param), vtk_format=format).addCellData( DataArray(e_r, range(dim), 'cell', ordering_fixture.param), vtk_format=format).addFieldData(DataArray(field, [0], 'field', ordering_fixture.param), vtk_format=format) rect.write() reader = vtkXMLRectilinearGridReader() # Testing the xml pretty print output as well pretty_sstream = io.StringIO(str(rect.writer)) for ss in [f, pretty_sstream]: vtk_r, vtk_e_r, vtk_f = get_vtk_data(reader, ss) vtk_r = vtk_r.reshape(r.shape, order='F') vtk_e_r = vtk_e_r.reshape(e_r.shape, order='F') \ .transpose(ordering_fixture.transp(dim)) assert all(vtk_r == r) assert all(vtk_e_r == e_r) assert all(vtk_f == field)
def test_image_data(field_data, compression_fixture, format_fixture, ordering_fixture): coords, r, e_r, field, order = field_data dim = r.ndim f = io.StringIO() compress = compression_fixture.param format = format_fixture.param with ImageData(f, [(min(x), max(x)) for x in coords], [x.size for x in coords], compression=compress, byte_order=order) as fh: fh.addPointData(DataArray(r, range(dim), 'point', ordering_fixture.param), vtk_format=format).addCellData( DataArray(e_r, range(dim), 'cell', ordering_fixture.param), vtk_format=format).addFieldData(DataArray( field, [0], 'field', ordering_fixture.param), vtk_format=format) reader = vtkXMLImageDataReader() vtk_r, vtk_e_r, vtk_f = get_vtk_data(reader, f) vtk_r = vtk_r.reshape(r.shape, order='F') vtk_e_r = vtk_e_r.reshape(e_r.shape, order='F') \ .transpose(ordering_fixture.transp(dim)) assert all(vtk_r == r) assert all(vtk_e_r == e_r) assert all(vtk_f == field)
def fields(world, species): import numpy as np from uvw import RectilinearGrid, DataArray # Creating coordinates x = np.linspace(-0.5, 0.5, 10) y = np.linspace(-0.5, 0.5, 20) z = np.linspace(-0.9, 0.9, 30) # Creating the file (with possible data compression) grid = RectilinearGrid('grid.vtr', (x, y, z), compression=True) # A centered ball x, y, z = np.meshgrid(x, y, z, indexing='ij') r = np.sqrt(x**2 + y**2 + z**2) ball = r < 0.3 # Some multi-component multi-dimensional data data = np.zeros([10, 20, 30, 3, 3]) data[ball, ...] = np.array([[0, 1, 0], [1, 0, 0], [0, 1, 1]]) # Some cell data cell_data = np.zeros([9, 19, 29]) cell_data[0::2, 0::2, 0::2] = 1 # Adding the point data (see help(DataArray) for more info) grid.addPointData(DataArray(data, range(3), 'ball')) # Adding the cell data grid.addCellData(DataArray(cell_data, range(3), 'checkers')) grid.write()
def test_structured_grid(compression_fixture, format_fixture): f = io.StringIO() N = 5 r = np.linspace(0, 1, N) theta = np.linspace(0, 2 * np.pi, 5 * N) theta, r = np.meshgrid(theta, r, indexing='ij') x = r * np.cos(theta) y = r * np.sin(theta) points = np.vstack([x.ravel(), y.ravel()]).T compress = compression_fixture.param format = format_fixture.param grid = StructuredGrid(f, points, (N, 5 * N), compression=compress) data = np.exp(-4 * r**2) grid.addPointData(DataArray(data, reversed(range(2)), 'data'), vtk_format=format) grid.write() reader = vtkXMLStructuredGridReader() reader.SetReadFromInputString(True) reader.SetInputString(f.getvalue()) reader.Update() output = reader.GetOutput() vtk_data = vtk_to_numpy(output.GetPointData().GetArray('data')) vtk_data = vtk_data.reshape(data.shape, order='C') assert all(vtk_data == data)
def test_prectilinear_grid(field_data, compression_fixture, format_fixture, ordering_fixture): coords, r, e_r, field, order = field_data dim = r.ndim out_name = 'test_prectilinear_grid.pvtr' compress = compression_fixture.param format = format_fixture.param rect = PRectilinearGrid(out_name, coords, dim * [0], compression=compress, byte_order=order) rect.init_master(None) # useless here: for coverage only rect.addPointData( DataArray(r, range(dim), 'point', components_order=ordering_fixture.param), vtk_format=format, ).addCellData( DataArray(e_r, range(dim), 'cell', components_order=ordering_fixture.param), vtk_format=format, ).addFieldData( DataArray(field, [0], 'field', components_order=ordering_fixture.param), vtk_format=format, ).write() reader = vtkXMLPRectilinearGridReader() vtk_r, vtk_e_r, vtk_f = get_vtk_data(reader, out_name) vtk_r = vtk_r.reshape(r.shape, order='F') vtk_e_r = vtk_e_r.reshape(e_r.shape, order='F') \ .transpose(ordering_fixture.transp(dim)) assert all(vtk_r == r) assert all(vtk_e_r == e_r) assert all(vtk_f == field) clean(rect)
def test_unstructured_grid(compression_fixture, format_fixture): f = io.StringIO() compress = compression_fixture.param format = format_fixture.param nodes = np.array([ [0, 0, 0], [1, 0, 0], [0, 1, 0], ]) point_data = np.array([[0, 1], [1, 2], [2, 3]]) cell_data = np.array([1, 2, 3, 4]) connectivity = { CellType.TRIANGLE: np.array([range(3)], dtype=np.int32), 6: np.array([[0, 1, 2]], dtype=np.int32), # Testing true VTK type id CellType.POLY_LINE: np.array([ # Testing variable length cell type [0, 1], [1, 2, 0], ]), } grid = UnstructuredGrid(f, nodes, connectivity, compression=compress) grid.addPointData(DataArray(point_data, [0], 'point'), vtk_format=format) grid.addCellData(DataArray(cell_data, [0], 'cell'), vtk_format=format) grid.write() reader = vtkXMLUnstructuredGridReader() reader.SetReadFromInputString(True) reader.SetInputString(f.getvalue()) reader.Update() vtk_pdata = vtk_to_numpy( reader.GetOutput().GetPointData().GetArray('point')) vtk_cdata = vtk_to_numpy(reader.GetOutput().GetCellData().GetArray('cell')) vtk_pdata.reshape(point_data.shape, order='C') vtk_cdata.reshape(cell_data.shape, order='C') assert all(vtk_pdata == point_data) assert all(vtk_cdata == cell_data)
def parallel_setup(uvw_type, vtk_type, filename, args, fixtures): compress, vtk_format, tmp_path = fixtures comm = MPI.COMM_WORLD rank = comm.Get_rank() N = 3 bounds = [(0, 1), (1, 2)] offsets = [ [0, 0, 0], [2 * N, 0, 0], ] x = np.linspace(*bounds[rank], N * 2) y = np.linspace(0, 1, N + 2) z = np.linspace(0, 1, N) tmp_path = comm.bcast(tmp_path, root=0) out_name = tmp_path / filename print(out_name) xx, yy, zz = np.meshgrid(x, y, z, indexing='ij', sparse=True) r = np.sqrt(xx**2 + yy**2 + zz**2) rect = uvw_type(out_name, *args((x, y, z)), offsets[rank], compression=compress) rect.addPointData(DataArray(r, range(3), 'R'), vtk_format=vtk_format) rect.write() if rank == 0: reader = vtk_type() reader.SetFileName(str(out_name)) reader.Update() output = reader.GetOutput() vtk_r = vtk_to_numpy(output.GetPointData().GetArray('R')) else: vtk_r = None vtk_r = comm.bcast(vtk_r, root=0) vtk_r = vtk_r.reshape([4 * N - 1, N + 2, N], order='F') i, j, k = [int(i) for i in offsets[rank]] # Adjusting for overlap if offsets[rank][0] != 0: i -= 1 sub_vtk = vtk_r[i:i + x.size, j:j + y.size, k:k + z.size] assert np.all(sub_vtk == r)
def test_prectilinear_grid_mpi(compression_fixture, format_fixture): comm = MPI.COMM_WORLD rank = comm.Get_rank() N = 3 bounds = [(0, 1), (1, 2)] offsets = [ [0, 0, 0], [2 * N, 0, 0], ] x = np.linspace(*bounds[rank], N * 2) y = np.linspace(0, 1, N + 2) z = np.linspace(0, 1, N) out_name = 'test_prectilinear_grid_mpi.pvtr' xx, yy, zz = np.meshgrid(x, y, z, indexing='ij', sparse=True) r = np.sqrt(xx**2 + yy**2 + zz**2) compress = compression_fixture.param format = format_fixture.param rect = PRectilinearGrid(out_name, (x, y, z), offsets[rank], compression=compress) rect.addPointData(DataArray(r, range(3), 'R'), vtk_format=format) rect.write() if rank == 0: reader = vtk.vtkXMLPRectilinearGridReader() reader.SetFileName(out_name) reader.Update() output = reader.GetOutput() vtk_r = vtk_to_numpy(output.GetPointData().GetArray('R')) else: vtk_r = None vtk_r = comm.bcast(vtk_r, root=0) vtk_r = vtk_r.reshape([4 * N - 1, N + 2, N], order='F') i, j, k = [int(i) for i in offsets[rank]] # Adjusting for overlap if offsets[rank][0] != 0: i -= 1 sub_vtk = vtk_r[i:i + x.size, j:j + y.size, k:k + z.size] assert np.all(sub_vtk == r) clean(rect)
def vtr_exp(sdi,i,L,H,ny,nx,u,v,p,vort,T): sivgt = np.zeros((ny,nx)) dx,dy = L/(nx-1),H/(ny-1) sivgt[1:-1,1:-1] = ((u[1:-1,2:]-u[1:-1,:-2])/(dx**2))*((v[2:,1:-1]-v[:-2,1:-1])/(dy**2)) - ((u[2:,1:-1]-u[:-2,1:-1])/(dy**2))*((v[1:-1,2:]-v[1:-1,:-2])/(dx**2)) # Creating coordinates y = np.linspace(0, L, nx) x = np.linspace(0, H, ny) xx, yy = np.meshgrid(x, y, indexing='ij') original_stdout = sys.stdout with open(sdi+'/out_it'+str(i+1)+'.vtr','w') as f: sys.stdout = f with RectilinearGrid(sys.stdout, (x, y)) as grid: grid.addPointData(DataArray(u, range(2), 'u velocity')) grid.addPointData(DataArray(v, range(2), 'v velocity')) grid.addPointData(DataArray(p, range(2), 'pressure')) grid.addPointData(DataArray(vort, range(2), 'vorticity')) grid.addPointData(DataArray(T, range(2), 'temperature')) grid.addPointData(DataArray(sivgt, range(2), 'SIVGT')) sys.stdout = original_stdout
def test_check_array_type_error(): array = np.array([0, 1, 2], dtype=np.complex128) with pytest.raises(TypeError): DataArray(array, [0], '')
def test_unsupported_format(): x = np.array([0, 1]) rect = RectilinearGrid('', x) with pytest.raises(ValueError): rect.addPointData(DataArray(x, [0]), '#dummy')
# Size offsets per rank offsets = [ [0, 0], [0, N], [N, 0], [0, 2 * N - 1], ] x = np.linspace(*bounds[rank]['x'], sizes[rank]['x']) y = np.linspace(*bounds[rank]['y'], sizes[rank]['y']) out_name = 'parallel_mpi.pvtr' xx, yy = np.meshgrid(x, y, indexing='ij', sparse=True) r = np.sqrt(xx**2 + yy**2) data = np.exp(-r**2) # Indicating rank info with a cell array proc = np.ones((x.size - 1, y.size - 1)) * rank with PRectilinearGrid(out_name, (x, y), offsets[rank]) as rect: rect.addPointData(DataArray(data, range(2), 'gaussian')) rect.addCellData(DataArray(proc, range(2), 'proc')) out_name = 'parallel_mpi.pvti' ranges = [bounds[rank]['x'], bounds[rank]['y']] points = [sizes[rank]['x'], sizes[rank]['y']] with PImageData(out_name, ranges, points, offsets[rank]) as rect: rect.addPointData(DataArray(data, range(2), 'gaussian')) rect.addCellData(DataArray(proc, range(2), 'proc'))
import sys import numpy as np from uvw import RectilinearGrid, DataArray # Creating coordinates x = np.linspace(-0.5, 0.5, 10) y = np.linspace(-0.5, 0.5, 20) # A centered disk xx, yy = np.meshgrid(x, y, indexing='ij') r = np.sqrt(xx**2 + yy**2) R = 0.3 disk = r < R data = np.zeros([10, 20]) data[disk] = np.sqrt(1 - (r[disk] / R)**2) cell_data = np.zeros([9, 19]) cell_data[0::2, 0::2] = 1 cell_data[1::2, 1::2] = 1 # File object can be used as a context manager # and you can write to stdout! with RectilinearGrid(sys.stdout, (x, y)) as grid: grid.addPointData(DataArray(data, range(2), 'data')) grid.addCellData(DataArray(cell_data, range(2), 'chess_board'))
"Rectilinear grid example with reordering of components" import numpy as np from uvw import RectilinearGrid, DataArray # Creating coordinates x = np.linspace(-0.5, 0.5, 10) y = np.linspace(-0.5, 0.5, 20) z = np.linspace(-0.9, 0.9, 30) # Creating the file grid = RectilinearGrid('grid.vtr', (x, y, z), compression=True) # A centered ball z, x, y = np.meshgrid(z, y, x, indexing='ij') r = np.sqrt(x**2 + y**2 + z**2) ball = r < 0.3 # Some multi-component multi-dimensional data (components order z y x) data = np.zeros([30, 20, 10, 3, 3]) data[ball, ...] = np.array([[0, 1, 0], [1, 0, 0], [0, 1, 1]]) # Adding the point data (see help(DataArray) for more info) grid.addPointData(DataArray(data, [2, 1, 0], 'data')) grid.write()
def write_uvw(): f = RectilinearGrid('uvw.vtr', (x, y, z)) f.addPointData(DataArray(r, range(r.ndim), ''), vtk_format='append') f.write()