def generate_velocity_grid( vlsvReader, cellid, iso_surface=False ): '''Generates a velocity grid from a given spatial cell id :param cellid: The spatial cell's ID :param iso_surface: If true, plots the iso surface ''' # Create nodes # Get velocity blocks and avgs: blocksAndAvgs = vlsvReader.read_blocks(cellid) if len(blocksAndAvgs) == 0: print "CELL " + str(cellid) + " HAS NO VELOCITY BLOCK" return False # Create a new scene #engine.new_scene() #mayavi.mlab.set_engine(engine) # Create a new figure #figure = mayavi.mlab.clf() #figure.scene.disable_render = True blocks = blocksAndAvgs[0] avgs = blocksAndAvgs[1] # Get nodes: nodesAndKeys = vlsvReader.construct_velocity_cell_nodes(blocks) # Create an unstructured grid: points = nodesAndKeys[0] tets = nodesAndKeys[1] tet_type=tvtk.Voxel().cell_type#VTK_VOXEL ug=tvtk.UnstructuredGrid(points=points) # Set up the cells ug.set_cells(tet_type,tets) # Input data values=np.ravel(avgs) ug.cell_data.scalars=values ug.cell_data.scalars.name='avgs' #figure.scene.disable_render = False d = mayavi.mlab.pipeline.add_dataset(ug) ptdata = mayavi.mlab.pipeline.cell_to_point_data(d) iso = mayavi.mlab.pipeline.iso_surface(ptdata, contours=[1e-15,1e-14,1e-12], opacity=0.3) mayavi.mlab.show()
def generate_diff_grid(self, cellid1, cellid2): ''' Generates a diff grid of given cell ids (shows avgs diff) :param cellid1: The first cell id :param cellid2: The second cell id .. code-block:: python # Example: grid.generate_diff_grid( 29219, 2910 ) .. note:: If the cell id does not have a certain velocity cell, it is assumed that the avgs value of that cell is 0 ''' # Create nodes # Get velocity blocks and avgs (of cellid 1) blocksAndAvgs1 = self.vlsvReader.read_blocks(cellid1) if len(blocksAndAvgs1) == 0: print "CELL " + str(cellid1) + " HAS NO VELOCITY BLOCK" return False blocks1 = blocksAndAvgs1[0] avgs1 = blocksAndAvgs1[1] # Get velocity blocks and avgs (of cellid 2) blocksAndAvgs2 = self.vlsvReader.read_blocks(cellid2) if len(blocksAndAvgs2) == 0: print "CELL " + str(cellid2) + " HAS NO VELOCITY BLOCK" return False blocks2 = blocksAndAvgs2[0] avgs2 = blocksAndAvgs2[1] print len(avgs2) print len(blocks2) # Compare blocks and create a new avgs array values: avgs_same = [] avgs_cellid1 = [] avgs_cellid2 = [] blocks_same = [] blocks_cellid1 = [] blocks_cellid2 = [] print np.shape(avgs1[0]) for i in xrange(len(blocks1)): b = blocks1[i] # Get index of block i2 = np.where(blocks2 == b)[0] if len(i2) != 0: # Fetch the block: #print avgs1[64*i:64*(i+1)] #print avgs2[64*i2[0]:64*(i2[0]+1)] avgs_same.append(avgs1[i:(i + 1)] - avgs2[i2[0]:(i2[0] + 1)]) blocks_same.append(b) else: avgs_cellid1.append(avgs1[i:(i + 1)]) blocks_cellid1.append(b) for i in xrange(len(blocks2)): b = blocks2[i] if (b in blocks1) == False: avgs_cellid2.append(avgs2[i:(i + 1)]) blocks_cellid2.append(b) # Make a list for the avgs etc avgs = np.zeros( 64 * (len(avgs_same) + len(avgs_cellid1) + len(avgs_cellid2))) #avgs = np.reshape(avgs, (len(avgs_same)+len(avgs_cellid1)+len(avgs_cellid2), 64)) print np.shape(avgs_same) blocks = np.zeros( len(blocks_same) + len(blocks_cellid1) + len(blocks_cellid2)) index = 0 avgs[64 * index:64 * (index + len(blocks_same))] = np.ravel( np.array(avgs_same)) blocks[index:index + len(blocks_same)] = np.array(blocks_same) index = index + len(blocks_same) avgs[64 * index:64 * (index + len(blocks_cellid1))] = np.ravel( np.array(avgs_cellid1)) blocks[index:index + len(blocks_cellid1)] = np.array(blocks_cellid1) index = index + len(blocks_cellid1) avgs[64 * index:64 * (index + len(blocks_cellid2))] = np.ravel( np.array(avgs_cellid2)) blocks[index:index + len(blocks_cellid2)] = np.array(blocks_cellid2) blocks = blocks.astype(int) # Get nodes: nodesAndKeys = self.vlsvReader.construct_velocity_cell_nodes(blocks) # Create an unstructured grid: points = nodesAndKeys[0] tets = nodesAndKeys[1] # Create a new scene self.__engine.new_scene() mayavi.mlab.set_engine(self.__engine) #CONTINUE # Create a new figure figure = mayavi.mlab.gcf(engine=self.__engine) figure.scene.disable_render = True tet_type = tvtk.Voxel().cell_type #VTK_VOXEL ug = tvtk.UnstructuredGrid(points=points) #Thissetsupthecells. ug.set_cells(tet_type, tets) #Attributedata. values = np.ravel(avgs) ug.cell_data.scalars = values ug.cell_data.scalars.name = 'avgs' d = mayavi.mlab.pipeline.add_dataset(ug) iso = mayavi.mlab.pipeline.surface(d) figure.scene.disable_render = False self.__unstructured_figures.append(figure) # Name the figure figure.name = str(cellid1) + " " + str(cellid2) mayavi.mlab.show() return True
def __generate_velocity_grid(self, cellid, iso_surface=False): '''Generates a velocity grid from a given spatial cell id :param cellid: The spatial cell's ID :param iso_surface: If true, plots the iso surface ''' # Create nodes # Get velocity blocks and avgs: blocksAndAvgs = self.vlsvReader.read_blocks(cellid) if len(blocksAndAvgs) == 0: print "CELL " + str(cellid) + " HAS NO VELOCITY BLOCK" return False # Create a new scene self.__engine.new_scene() mayavi.mlab.set_engine(self.__engine) #CONTINUE # Create a new figure figure = mayavi.mlab.gcf(engine=self.__engine) figure.scene.disable_render = True blocks = blocksAndAvgs[0] avgs = blocksAndAvgs[1] # Get nodes: nodesAndKeys = self.vlsvReader.construct_velocity_cell_nodes(blocks) # Create an unstructured grid: points = nodesAndKeys[0] tets = nodesAndKeys[1] tet_type = tvtk.Voxel().cell_type #VTK_VOXEL ug = tvtk.UnstructuredGrid(points=points) # Set up the cells ug.set_cells(tet_type, tets) # Input data values = np.ravel(avgs) ug.cell_data.scalars = values ug.cell_data.scalars.name = 'avgs' # Plot B if possible: # Read B vector and plot it: if self.vlsvReader.check_variable("B") == True: B = self.vlsvReader.read_variable(name="B", cellids=cellid) elif self.vlsvReader.check_variable("B_vol") == True: B = self.vlsvReader.read_variable(name="B_vol", cellids=cellid) else: B = self.vlsvReader.read_variable( name="background_B", cellids=cellid) + self.vlsvReader.read_variable( name="perturbed_B", cellids=cellid) points2 = np.array([[0, 0, 0]]) ug2 = tvtk.UnstructuredGrid(points=points2) ug2.point_data.vectors = [B / np.linalg.norm(B)] ug2.point_data.vectors.name = 'B_vector' #src2 = VTKDataSource(data = ug2) d2 = mayavi.mlab.pipeline.add_dataset(ug2) #mayavi.mlab.add_module(Vectors()) vec = mayavi.mlab.pipeline.vectors(d2) vec.glyph.mask_input_points = True vec.glyph.glyph.scale_factor = 1e6 vec.glyph.glyph_source.glyph_source.center = [0, 0, 0] # Visualize d = mayavi.mlab.pipeline.add_dataset(ug) if iso_surface == False: iso = mayavi.mlab.pipeline.surface(d) else: ptdata = mayavi.mlab.pipeline.cell_to_point_data(d) iso = mayavi.mlab.pipeline.iso_surface( ptdata, contours=[1e-15, 1e-14, 1e-12], opacity=0.3) figure.scene.disable_render = False self.__unstructured_figures.append(figure) # Name the figure figure.name = str(cellid) + ", " + self.variable_plotted + " = " + str( self.vlsvReader.read_variable(self.variable_plotted, cellids=cellid)) from mayavi.modules.axes import Axes axes = Axes() axes.name = 'Axes' axes.axes.fly_mode = 'none' axes.axes.number_of_labels = 8 axes.axes.font_factor = 0.5 #module_manager = self.__module_manager() # Add the label / marker: self.__engine.add_filter(axes) from mayavi.modules.outline import Outline outline = Outline() outline.name = 'Outline' self.__engine.add_filter(outline) return True
def generate_custom_velocity_grid(vlsvReader, blocks_and_values, iso_surface=False): '''Generates a velocity grid from a given spatial cell id :param vlsvReader: Some vlsv reader with a file open :param velocity_cell_map: Given velocity cell ids and values in python dict() format (see read_velocity_cells function in vlsvReader) :param iso_surface: If true, plots the iso surface # Example usage: import pytools as pt #vlsvReader = pt.vlsvfile.VlsvReader("example.vlsv") cellid = 1111 velocity_cell_map = vlsvReader.read_velocity_cells(cellid) velocity_cell_ids = velocity_cell_map.keys() velocity_cell_values = velocity_cell_map.values() velocity_cell_map[velocity_cell_ids[10]] = 3e-7 generate_custom_velocity_grid( vlsvReader, velocity_cell_map ) ''' # Create nodes # Get velocity blocks and avgs: # Get helper function: blocksAndAvgs = blocks_and_values if len(blocksAndAvgs) == 0: print("CELL " + str(cellid) + " HAS NO VELOCITY BLOCK") return False # Create a new scene #engine.new_scene() #mayavi.mlab.set_engine(engine) # Create a new figure #figure = mayavi.mlab.clf() #figure.scene.disable_render = True blocks = blocksAndAvgs[0] avgs = blocksAndAvgs[1] # Get nodes: nodesAndKeys = vlsvReader.construct_velocity_cell_nodes(blocks) # Create an unstructured grid: points = nodesAndKeys[0] tets = nodesAndKeys[1] tet_type = tvtk.Voxel().cell_type #VTK_VOXEL ug = tvtk.UnstructuredGrid(points=points) # Set up the cells ug.set_cells(tet_type, tets) # Input data values = np.ravel(avgs) ug.cell_data.scalars = values ug.cell_data.scalars.name = 'avgs' #figure.scene.disable_render = False d = mayavi.mlab.pipeline.add_dataset(ug) if iso_surface == False: iso = mayavi.mlab.pipeline.surface(d) else: ptdata = mayavi.mlab.pipeline.cell_to_point_data(d) iso = mayavi.mlab.pipeline.iso_surface(ptdata, contours=[1e-15, 1e-14, 1e-12], opacity=0.3) engine = mayavi.mlab.get_engine() from mayavi.modules.axes import Axes axes = Axes() axes.name = 'Axes' axes.axes.fly_mode = 'none' axes.axes.number_of_labels = 8 axes.axes.font_factor = 0.5 #module_manager = self.__module_manager() # Add the label / marker: engine.add_filter(axes) from mayavi.modules.outline import Outline outline = Outline() outline.name = 'Outline' engine.add_filter(outline) mayavi.mlab.show()
def createVtkObject(vmesh): print("Creating TVTK object from AMR mesh") #points = array([[0,1.2,0.6], [1,0,0], [0,1,0], [1,1,1], # tetra # [1,0,-0.5], [2,0,0], [2,1.5,0], [0,1,0], # [1,0,0], [1.5,-0.2,1], [1.6,1,1.5], [1,1,1], # Hex # ], 'f') ## The cells #cells = array([4, 0, 1, 2, 3, # tetra # 8, 4, 5, 6, 7, 8, 9, 10, 11 # hex # ]) ## The offsets for the cells, i.e. the indices where the cells start. #offset = array([0, 5]) #tetra_type = tvtk.Tetra().cell_type # VTK_TETRA == 10 #hex_type = tvtk.Hexahedron().cell_type # VTK_HEXAHEDRON == 12 #cell_types = array([tetra_type, hex_type]) ## Create the array of cells unambiguously. #cell_array = tvtk.CellArray() #cell_array.set_cells(2, cells) ## Now create the UG #ug = tvtk.UnstructuredGrid(points=points) #ug.set_cells(cell_types, offset, cell_array) #scalars = random.random(points.shape[0]) #ug.point_data.scalars = scalars #ug.point_data.scalars.name = 'scalars' #analysator #points = nodesAndKeys[0] #tets = nodesAndKeys[1] # [x,y,z] list of nodes #points = np.array([[0,0,0], # [1,0,0], # [0,1,0], # [1,1,0], # [0,0,1], # [1,0,1], # [1,1,1], # ], 'f') #cells = np.array([[0,1,2,3], # [4,5,6,7], # [4,5,6,7], points, conns, avgs = stripAmrMesh(vmesh) print(points) print(conns) #set cells & grid vox_type = tvtk.Voxel().cell_type #VTK_VOXEL ug = tvtk.UnstructuredGrid(points=points) ug.set_cells(vox_type, conns) #points = np.array([[0,0,0], [1,0,0], [0,1,0], [0,0,1], # tets # [1,0,0], [2,0,0], [1,1,0], [1,0,1], # [2,0,0], [3,0,0], [2,1,0], [2,0,1], # ], 'f') #tets = np.array([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]) #tet_type = tvtk.Tetra().cell_type #ug = tvtk.UnstructuredGrid(points=points) #ug.set_cells(tet_type, tets) #set scalar values #avgs = np.array([10.0]) values = np.ravel(avgs) ug.cell_data.scalars = values ug.cell_data.scalars.name = 'dens' #d = mayavi.mlab.pipeline.add_dataset(ug) #if iso_surface == False: # iso = mayavi.mlab.pipeline.surface(d) #else: #ptdata = mayavi.mlab.pipeline.cell_to_point_data(d) #iso = mayavi.mlab.pipeline.iso_surface(ptdata, contours=[1e-15,1e-14,1e-12], opacity=0.3) return ug