def test_rectilinear_fails(): x = np.array([[0, 1], [2, 3]]) with pytest.raises(ValueError): RectilinearGrid('', x) x = np.array([0, 1]) with pytest.raises(ValueError): RectilinearGrid('', x, [])
def test_paraview_data(tmp_path): """ NB: This is just testing writing. Since PVD is a ParaView related extension, it cannot be tested with vanilla VTK """ x = np.linspace(0, 1, 10) y = x.copy() grid = RectilinearGrid(tmp_path / 'grid.vtr', [x, y]) grid.write() group = ParaViewData(tmp_path / 'grid.pvd') group.addFile(grid) group.write()
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_invalid_file(): x = np.array([0, 1]) grid = RectilinearGrid('', x) with pytest.raises(TypeError): grid.writer.write(1) with pytest.raises(TypeError): PRectilinearGrid(1, x, None)
def test_malformed_attributes(): x = np.array([0, 1]) rect = RectilinearGrid('', x) with pytest.raises(ValueError): rect.piece.register('', [])
def test_context_manger(): x = np.array([1, 2]) with RectilinearGrid('', x): raise Exception('Yo') with pytest.raises(NotImplementedError): with WriteManager(): pass
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 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
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'))
def write_uvw(): f = RectilinearGrid('uvw.vtr', (x, y, z)) f.addPointData(DataArray(r, range(r.ndim), ''), vtk_format='append') f.write()
def test_invalid_compression(): x = np.array([0, 1]) with pytest.raises(ValueError): RectilinearGrid('', x, compression=100)
def test_unsupported_format(): x = np.array([0, 1]) rect = RectilinearGrid('', x) with pytest.raises(ValueError): rect.addPointData(DataArray(x, [0]), '#dummy')
def test_invalid_endian(): with pytest.raises(ValueError): x = np.array([0, 1]) RectilinearGrid('', x, byte_order='WhatEndian')
"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 test_context_manger(): x = np.array([1, 2]) with RectilinearGrid('', x): raise Exception('Yo')