def test_simple(nx, ny, potFunc, xyz): # create the grid and the edge data gr = Grid() points = numpy.zeros((nx * ny, 4, 3), numpy.float64) data = numpy.zeros((nx * ny, 4)) dx = 1.0 / float(nx) dy = 1.0 / float(ny) k = 0 for i in range(nx): x0 = i * dx x1 = x0 + dx for j in range(ny): y0 = j * dy y1 = y0 + dy # node indexing # 3-->--2 # | | # ^ ^ # | | # 0-->--1 points[k, 0, :] = x0, y0, 0. points[k, 1, :] = x1, y0, 0. points[k, 2, :] = x1, y1, 0. points[k, 3, :] = x0, y1, 0. # edge indexing # 2 # +-->--+ # | | # 3^ ^1 # | | # +-->--+ # 0 data[k, 0] = potFunc(points[k, 1, :]) - potFunc(points[k, 0, :]) data[k, 1] = potFunc(points[k, 2, :]) - potFunc(points[k, 1, :]) data[k, 2] = potFunc(points[k, 2, :]) - potFunc(points[k, 3, :]) data[k, 3] = potFunc(points[k, 3, :]) - potFunc(points[k, 0, :]) # increment the cell counter k += 1 gr.setPoints(points) gr.dump('test_polyline_integral.vtk') pli = PolylineIntegral() pli.setGrid(gr) # no periodicity in x pli.buildLocator(numCellsPerBucket=128, periodX=0.0, enableFolding=False) pli.computeWeights(xyz, counterclock=False) flux = pli.getIntegral(data=data, placement=CELL_BY_CELL_DATA) exactFlux = potFunc(xyz[-1, :]) - potFunc(xyz[0, :]) print(f'total flux: {flux:.3f} exact flux: {exactFlux:.3f}') assert abs(flux - exactFlux) < 1.e-10
def test_create_grid(): # create the grid gr = Grid() # 2 cells points = numpy.array([(0., 0., 0.), (1., 0., 0.), (1., 1., 0.), (0., 1., 0.), (1., 0., 0.), (2., 0., 0.), (2., 1., 0.), (1., 1., 0.)]).reshape(2, 4, 3) gr.setPoints(points) gr.dump('test_create_grid.vtk')
def test_create_grid(): # create the grid gr = Grid() # 2 cells points = numpy.array([(0., 0., 0.), (1., 0., 0.), (1., 1., 0.), (0., 1., 0.), (1., 0., 0.), (2., 0., 0.), (2., 1., 0.), (1., 1., 0.)]).reshape((2, 4, 3)) gr.setPoints(points) # get a pointer to the points of the cell-by-cell mesh pts = gr.getPoints() print(pts) with TemporaryDirectory() as d: fname = str(Path(d) / Path('grid.vtk')) gr.dump(fname)
def test_load_ugrid_data(): # a single cell # 3....>2....2 # : : # v ^ # 1 0 # : : # 0....<3....1 xyz = numpy.array([(0., 0., 0.), (1., 0., 0.), (1., 1., 0.), (0., 1., 0.)], dtype=numpy.float64) face2nodes = numpy.array([ (0, 1, 2, 3), ], dtype=numpy.uintp) edge2nodes = numpy.array( [ (1, 2), # edge 0 (3, 0), # edge 1 (3, 2), # edge 2 (1, 0) ], # edge 3 dtype=numpy.uintp) gr = Grid() gr.setFlags(0, 0) gr.loadFromUgrid2DData(xyz, face2nodes, edge2nodes) n0, n1 = gr.getNodeIds(cellId=0, edgeIndex=0) assert (n0 == 0) assert (n1 == 1) n0, n1 = gr.getNodeIds(cellId=0, edgeIndex=1) assert (n0 == 1) assert (n1 == 2) n0, n1 = gr.getNodeIds(cellId=0, edgeIndex=2) assert (n0 == 3) assert (n1 == 2) n0, n1 = gr.getNodeIds(cellId=0, edgeIndex=3) assert (n0 == 0) assert (n1 == 3) gr.dump('singleCell.vtk')
def test_attach_data(): # create the grid gr = Grid() # 2 cells points = numpy.array([(0., 0., 0.), (1., 0., 0.), (1., 1., 0.), (0., 1., 0.), (1., 0., 0.), (2., 0., 0.), (2., 1., 0.), (1., 1., 0.)]).reshape((2, 4, 3)) gr.setPoints(points) # create cell data, 3 per cell nDataPerCell = 3 data = numpy.arange(0, 2 * nDataPerCell, dtype=numpy.float64).reshape( (2, nDataPerCell)) gr.attach('mydata', data) with TemporaryDirectory() as d: fname = str(Path(d) / Path('grid.vtk')) gr.dump(fname) # read the data back to check the layout of the data reader = vtk.vtkUnstructuredGridReader() reader.SetFileName(fname) reader.Update() ugrid = reader.GetOutput() arr = ugrid.GetCellData().GetArray('mydata') assert (arr.GetNumberOfTuples() == gr.getNumberOfCells()) assert (arr.GetNumberOfComponents() == nDataPerCell)
def test_simple(): # create the grid and the edge data gr = Grid() nx, ny = 3, 2 points = numpy.zeros((nx * ny, 4, 3), numpy.float64) data = numpy.zeros((nx * ny, 4)) dx = 1.0 / float(nx) dy = 1.0 / float(ny) k = 0 for i in range(nx): x0 = i * dx x1 = x0 + dx for j in range(ny): y0 = j * dy y1 = y0 + dy # node indexing # 3-->--2 # | | # ^ ^ # | | # 0-->--1 points[k, 0, :] = x0, y0, 0. points[k, 1, :] = x1, y0, 0. points[k, 2, :] = x1, y1, 0. points[k, 3, :] = x0, y1, 0. # edge indexing # 2 # +-->--+ # | | # 3^ ^1 # | | # +-->--+ # 0 data[k, 0] = potentialFunc(points[k, 1, :]) - potentialFunc( points[k, 0, :]) data[k, 1] = potentialFunc(points[k, 2, :]) - potentialFunc( points[k, 1, :]) data[k, 2] = potentialFunc(points[k, 2, :]) - potentialFunc( points[k, 3, :]) data[k, 3] = potentialFunc(points[k, 3, :]) - potentialFunc( points[k, 0, :]) # increment the cell counter k += 1 gr.setPoints(points) gr.dump('test_polyline_integral.vtk') pli = PolylineIntegral() # create the polyline through which the flux will be integrated xyz = numpy.array([(0., 0., 0.), (1., 0., 0.), (1., 1., 0.), (0., 1., 0.)]) # no periodicity in x pli.build(gr, xyz, counterclock=False, periodX=0.0) flux = pli.getIntegral(data) exactFlux = potentialFunc(xyz[-1, :]) - potentialFunc(xyz[0, :]) print(f'total flux: {flux:.3f} exact flux: {exactFlux:.3f}') assert abs(flux - exactFlux) < 1.e-10