def setUp(self): self.cell = vtkStructuredGrid() pts = vtkPoints() pts.SetDataTypeToDouble() pts.InsertNextPoint(-1.0, -1.0, -1.0) pts.InsertNextPoint( 1.0, -1.0, -1.0) pts.InsertNextPoint( 1.0, 1.0, -1.0) pts.InsertNextPoint(-1.0, 1.0, -1.0) pts.InsertNextPoint(-1.0, -1.0, 1.0) pts.InsertNextPoint( 1.0, -1.0, 1.0) pts.InsertNextPoint( 1.0, 1.0, 1.0) pts.InsertNextPoint(-1.0, 1.0, 1.0) self.cell.SetDimensions(2,2,2) self.cell.SetPoints(pts) scalar = vtkDoubleArray() scalar.SetName('scalar') scalar.SetNumberOfTuples(8) scalar.SetValue(0, 0.0) scalar.SetValue(1, 0.0) scalar.SetValue(2, 0.0) scalar.SetValue(3, 0.0) scalar.SetValue(4, 1.0) scalar.SetValue(5, 1.0) scalar.SetValue(6, 1.0) scalar.SetValue(7, 1.0) self.cell.GetPointData().SetScalars(scalar)
def setUp(self): self.cell = vtkUnstructuredGrid() pts = vtkPoints() pts.SetDataTypeToDouble() pts.InsertNextPoint(-1.0, -1.0, -1.0) pts.InsertNextPoint( 1.0, -1.0, -1.0) pts.InsertNextPoint( 1.0, 1.0, -1.0) pts.InsertNextPoint(-1.0, 1.0, -1.0) pts.InsertNextPoint(-1.0, -1.0, 1.0) pts.InsertNextPoint( 1.0, -1.0, 1.0) pts.InsertNextPoint( 1.0, 1.0, 1.0) pts.InsertNextPoint(-1.0, 1.0, 1.0) self.cell.SetPoints(pts) self.cell.Allocate(1,1) ids = vtkIdList() for i in range(8): ids.InsertId(i,i) self.cell.InsertNextCell(vtk_const.VTK_HEXAHEDRON, ids) scalar = vtkDoubleArray() scalar.SetName('scalar') scalar.SetNumberOfTuples(8) scalar.SetValue(0, 0.0) scalar.SetValue(1, 0.0) scalar.SetValue(2, 0.0) scalar.SetValue(3, 0.0) scalar.SetValue(4, 1.0) scalar.SetValue(5, 1.0) scalar.SetValue(6, 1.0) scalar.SetValue(7, 1.0) self.cell.GetPointData().SetScalars(scalar)
def unstructured_from_composite_arrays(points, arrays, controller=None): """Given a set of VTKCompositeDataArrays, creates a vtkUnstructuredGrid. The main goal of this function is to transform the output of XXX_per_block() methods to a single dataset that can be visualized and further processed. Here arrays is an iterable (e.g. list) of (array, name) pairs. Here is an example: centroid = mean_per_block(composite_data.Points) T = mean_per_block(composite_data.PointData['Temperature']) ug = unstructured_from_composite_arrays(centroid, (T, 'Temperature')) When called in parallel, this function makes sure that each array in the input dataset is represented only on 1 process. This is important because methods like mean_per_block() return the same value for blocks that are partitioned on all of the participating processes. If the same point were to be created across multiple processes in the output, filters like histogram would report duplicate values erroneously. """ try: dataset = points.DataSet except AttributeError: dataset = None if dataset is None and points is not dsa.NoneArray: raise ValueError( "Expecting a points arrays with an associated dataset.") if points is dsa.NoneArray: cpts = [] else: cpts = points.Arrays ownership = numpy.zeros(len(cpts), dtype=numpy.int32) rank = 0 # Let's first create a map of array index to composite ids. if dataset is None: ids = [] else: it = dataset.NewIterator() it.UnRegister(None) itr = cpts.__iter__() ids = numpy.empty(len(cpts), dtype=numpy.int32) counter = 0 while not it.IsDoneWithTraversal(): _id = it.GetCurrentFlatIndex() ids[counter] = _id counter += 1 it.GoToNextItem() if controller is None and vtkMultiProcessController is not None: controller = vtkMultiProcessController.GetGlobalController() if controller and controller.IsA("vtkMPIController"): from mpi4py import MPI comm = vtkMPI4PyCommunicator.ConvertToPython( controller.GetCommunicator()) rank = comm.Get_rank() # Determine the max id to use for reduction # operations # Get all ids from dataset, including empty ones. lmax_id = numpy.int32(0) if dataset is not None: it = dataset.NewIterator() it.UnRegister(None) it.SetSkipEmptyNodes(False) while not it.IsDoneWithTraversal(): _id = it.GetCurrentFlatIndex() lmax_id = numpy.max((lmax_id, _id)).astype(numpy.int32) it.GoToNextItem() max_id = numpy.array(0, dtype=numpy.int32) mpitype = _lookup_mpi_type(numpy.int32) comm.Allreduce([lmax_id, mpitype], [max_id, mpitype], MPI.MAX) # Now we figure out which processes have which ids lownership = numpy.empty(max_id, dtype=numpy.int32) lownership.fill(numpy.iinfo(numpy.int32).max) ownership = numpy.empty(max_id, dtype=numpy.int32) if dataset is not None: it = dataset.NewIterator() it.UnRegister(None) it.InitTraversal() itr = cpts.__iter__() while not it.IsDoneWithTraversal(): _id = it.GetCurrentFlatIndex() if itr.next() is not dsa.NoneArray: lownership[_id] = rank it.GoToNextItem() mpitype = _lookup_mpi_type(numpy.int32) # The process with the lowest id containing a block will # produce the output for that block. comm.Allreduce([lownership, mpitype], [ownership, mpitype], MPI.MIN) # Iterate over blocks to produce points and arrays from vtk.vtkCommonDataModel import vtkUnstructuredGrid from vtk.vtkCommonCore import vtkDoubleArray, vtkPoints ugrid = vtkUnstructuredGrid() da = vtkDoubleArray() da.SetNumberOfComponents(3) pts = vtkPoints() pts.SetData(da) counter = 0 for pt in cpts: if ownership[ids[counter]] == rank: pts.InsertNextPoint(tuple(pt)) counter += 1 ugrid.SetPoints(pts) for ca, name in arrays: if ca is not dsa.NoneArray: da = vtkDoubleArray() ncomps = ca.Arrays[0].flatten().shape[0] da.SetNumberOfComponents(ncomps) counter = 0 for a in ca.Arrays: if ownership[ids[counter]] == rank: a = a.flatten() for i in range(ncomps): da.InsertNextValue(a[i]) counter += 1 if len(a) > 0: da.SetName(name) ugrid.GetPointData().AddArray(da) return ugrid
def unstructured_from_composite_arrays(points, arrays, controller=None): """Given a set of VTKCompositeDataArrays, creates a vtkUnstructuredGrid. The main goal of this function is to transform the output of XXX_per_block() methods to a single dataset that can be visualized and further processed. Here arrays is an iterable (e.g. list) of (array, name) pairs. Here is an example: centroid = mean_per_block(composite_data.Points) T = mean_per_block(composite_data.PointData['Temperature']) ug = unstructured_from_composite_arrays(centroid, (T, 'Temperature')) When called in parallel, this function makes sure that each array in the input dataset is represented only on 1 process. This is important because methods like mean_per_block() return the same value for blocks that are partitioned on all of the participating processes. If the same point were to be created across multiple processes in the output, filters like histogram would report duplicate values erroneously. """ try: dataset = points.DataSet except AttributeError: dataset = None if dataset is None and points is not dsa.NoneArray: raise ValueError, "Expecting a points arrays with an associated dataset." if points is dsa.NoneArray: cpts = [] else: cpts = points.Arrays ownership = numpy.zeros(len(cpts), dtype=numpy.int32) rank = 0 # Let's first create a map of array index to composite ids. if dataset is None: ids = [] else: it = dataset.NewIterator() it.UnRegister(None) itr = cpts.__iter__() ids = numpy.empty(len(cpts), dtype=numpy.int32) counter = 0 while not it.IsDoneWithTraversal(): _id = it.GetCurrentFlatIndex() ids[counter] = _id counter += 1 it.GoToNextItem() if controller is None and vtkMultiProcessController is not None: controller = vtkMultiProcessController.GetGlobalController() if controller and controller.IsA("vtkMPIController"): from mpi4py import MPI comm = vtkMPI4PyCommunicator.ConvertToPython(controller.GetCommunicator()) rank = comm.Get_rank() # Determine the max id to use for reduction # operations # Get all ids from dataset, including empty ones. lmax_id = numpy.int32(0) if dataset is not None: it = dataset.NewIterator() it.UnRegister(None) it.SetSkipEmptyNodes(False) while not it.IsDoneWithTraversal(): _id = it.GetCurrentFlatIndex() lmax_id = numpy.max((lmax_id, _id)).astype(numpy.int32) it.GoToNextItem() max_id = numpy.array(0, dtype=numpy.int32) mpitype = _lookup_mpi_type(numpy.int32) comm.Allreduce([lmax_id, mpitype], [max_id, mpitype], MPI.MAX) # Now we figure out which processes have which ids lownership = numpy.empty(max_id, dtype = numpy.int32) lownership.fill(numpy.iinfo(numpy.int32).max) ownership = numpy.empty(max_id, dtype = numpy.int32) if dataset is not None: it = dataset.NewIterator() it.UnRegister(None) it.InitTraversal() itr = cpts.__iter__() while not it.IsDoneWithTraversal(): _id = it.GetCurrentFlatIndex() if itr.next() is not dsa.NoneArray: lownership[_id] = rank it.GoToNextItem() mpitype = _lookup_mpi_type(numpy.int32) # The process with the lowest id containing a block will # produce the output for that block. comm.Allreduce([lownership, mpitype], [ownership, mpitype], MPI.MIN) # Iterate over blocks to produce points and arrays from vtk.vtkCommonDataModel import vtkUnstructuredGrid from vtk.vtkCommonCore import vtkDoubleArray, vtkPoints ugrid = vtkUnstructuredGrid() da = vtkDoubleArray() da.SetNumberOfComponents(3) pts = vtkPoints() pts.SetData(da) counter = 0 for pt in cpts: if ownership[ids[counter]] == rank: pts.InsertNextPoint(tuple(pt)) counter += 1 ugrid.SetPoints(pts) for ca, name in arrays: if ca is not dsa.NoneArray: da = vtkDoubleArray() ncomps = ca.Arrays[0].flatten().shape[0] da.SetNumberOfComponents(ncomps) counter = 0 for a in ca.Arrays: if ownership[ids[counter]] == rank: a = a.flatten() for i in range(ncomps): da.InsertNextValue(a[i]) counter += 1 if len(a) > 0: da.SetName(name) ugrid.GetPointData().AddArray(da) return ugrid