Esempio n. 1
0
def view_mlab():
    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()
Esempio n. 2
0
    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()
Esempio n. 4
0
    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
Esempio n. 5
0
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()