def single_type_ug(): """Simple example showing how to create an unstructured grid consisting of cells of a single type. """ points = 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 = 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) return ug
def _readData(self): from enthought.tvtk.api import tvtk import tables import numpy filename = "../results/strikeslip_%s_%04dm.h5" % (shape, res) h5 = tables.openFile(filename, 'r') cells = h5.root.topology.cells[:] (ncells, ncorners) = cells.shape vertices = h5.root.geometry.vertices[:] / 1e+3 (nvertices, spaceDim) = vertices.shape disp = h5.root.solution.snapshot0.displacements[:] h5.close() if shape == "tet4": assert(spaceDim == 3) assert(ncorners == 4) cellType = tvtk.Tetra().cell_type elif shape == "hex8": assert(spaceDim == 3) assert(ncorners == 8) cellType = tvtk.Hexahedron().cell_type else: raise ValueError("Unknown shape '%s'." % shape) data = tvtk.UnstructuredGrid() data.points = vertices data.set_cells(cellType, cells) data.point_data.vectors = disp data.point_data.vectors.name = "Displacement [m]" return data
def _readMesh(self): from enthought.tvtk.api import tvtk import tables import numpy filename = "../meshes/reverseslip_%s_%04dm.h5" % (shape, res) h5 = tables.openFile(filename, 'r') cells = h5.root.topology.cells[:] (ncells, ncorners) = cells.shape vertices = h5.root.geometry.vertices[:] / 1e+3 (nvertices, spaceDim) = vertices.shape h5.close() if shape == "tet4": assert (spaceDim == 3) assert (ncorners == 4) cellType = tvtk.Tetra().cell_type else: raise ValueError("Unknown shape '%s'." % shape) data = tvtk.UnstructuredGrid() data.points = vertices data.set_cells(cellType, cells) return data
def _read_ele_file(self): """ Loads data from {basename}.ele, and inserts triangle/tetrahedron cells and cell scalars into the unstructured grid. """ file_name = '%s.ele' % self._basename # Load all data. all_data = self._get_data(file_name) # Grab values from the first line of data file. tet_tri, nodes_per_tet_tri, attributes = map(int, all_data[0:3]) # Reshape remainder of array. data_array = all_data[3:].reshape(tet_tri, 1 + nodes_per_tet_tri + attributes) nodes_array = data_array[:, 1:(nodes_per_tet_tri + 1)] - self._numbered_from if (nodes_per_tet_tri == 3): cell_type = tvtk.Triangle().cell_type else: cell_type = tvtk.Tetra().cell_type self._grid.set_cells(cell_type, nodes_array) for i in range(attributes): attribute_array = data_array[:, (i + nodes_per_tet_tri + 1):(i + nodes_per_tet_tri + 2)] self._add_attribute_array(attribute_array, i, 'cell')
def mixed_type_ug(): """A slightly more complex example of how to generate an unstructured grid with different cell types. Returns a created unstructured grid. """ points = array( [ [0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1], # tetra [2, 0, 0], [3, 0, 0], [3, 1, 0], [2, 1, 0], [2, 0, 1], [3, 0, 1], [3, 1, 1], [2, 1, 1], # Hex ], 'f') # shift the points so we can show both. points[:, 1] += 2.0 # 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) # Now just set the cell types and reuse the ug locations and cells. ug.set_cells(cell_types, offset, cell_array) return ug
def unstructured_grid(): 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) # Now just set the cell types and reuse the ug locations and cells. 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' return ug
def make_data(): points = N.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 = N.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) # Setup the point attributes. temp = N.random.random(12) v = N.random.randn(12, 3) ten = N.random.randn(12, 9) a = tvtk.FloatArray(name='p') a.from_array(N.random.randn(12)) ug.point_data.add_array(a) ug.point_data.scalars = temp ug.point_data.scalars.name = 't' ug.point_data.vectors = v ug.point_data.vectors.name = 'v' ug.point_data.tensors = ten ug.point_data.tensors.name = 'ten' # Setup the cell attributes. temp = N.random.random(3) v = N.random.randn(3, 3) ten = N.random.randn(3, 9) ug.cell_data.scalars = temp ug.cell_data.scalars.name = 't' ug.cell_data.vectors = v ug.cell_data.vectors.name = 'v' ug.cell_data.tensors = ten ug.cell_data.tensors.name = 'ten' return ug
def _readData(self): from enthought.tvtk.api import tvtk import tables import numpy filename = "../projection/pylith_%s_%04dm.h5" % (shape, res) h5 = tables.openFile(filename, 'r') cells = h5.root.topology.cells[:] (ncells, ncorners) = cells.shape vertices = h5.root.geometry.vertices[:] / 1e+3 (nvertices, spaceDim) = vertices.shape error = h5.root.difference.pylith_1_0_analytic.snapshot0.displacements[:] disp = h5.root.projection.data.pylith_1_0.snapshot0.displacements[:] dispE = h5.root.projection.data.analytic.snapshot0.displacements[:] h5.close() if shape == "tet4": assert(spaceDim == 3) assert(ncorners == 4) cellType = tvtk.Tetra().cell_type elif shape == "hex8": assert(spaceDim == 3) assert(ncorners == 8) cellType = tvtk.Hexahedron().cell_type else: raise ValueError("Unknown shape '%s'." % shape) errorLog10 = numpy.log10(error) data = tvtk.UnstructuredGrid() data.points = vertices data.set_cells(cellType, cells) data.cell_data.scalars = errorLog10 data.cell_data.scalars.name = "log10(Error [m])" return data
def mshplot3D(mesh, scale=[1, 1, 1], elm_ids=[], labels=[0, 0, 0]): ''' Plot 3D meshes (and optionally label vertices, elements, and edges) @param mesh A 3D mesh object @param elm_ids Numpy array of elements that will be plotted. If left empty, all elements are plotted. @param labels (\c bool) List of three elements indicateting what should be labeled. By default, nothing is labeled labels[0]=True labels vertices labels[1]=True labels elements labels[2]=True labels edges @param scale (\c int) List indicating the scaling of the vertices -- this is used to scale the z-direction for a thin mesh. By default, there is no scaling, i.e. scale = [1, 1, 1] @see msh.mesh.Mesh3D @author Matt Ueckermann ''' if type(elm_ids) is not int: if len(elm_ids) == 0: elm_ids = np.arange(len(mesh.elm)) #Figure out how many vertices are in each element type n_vert_in_type = [len(int_el_pqr(element=element, dim=3)[0]) \ for element in mesh.u_elm_type] #Now create the TVTK data-structures for the 'cells' and 'offsets' #cells give [#verts, vert1id, vert2id...,vertnid, #verts ...] #offsets gives the id's in the cells list where #verts are listed. So above # it is [0, n+1] cells = np.array([], dtype=int) offset = np.array([], dtype=int) for elm, elm_type in zip(mesh.elm[elm_ids, :], mesh.elm_type[elm_ids]): n_vt = n_vert_in_type[elm_type] offset = np.append(offset, len(cells)) cells = np.append(cells, [n_vt] + elm[:n_vt].tolist()) #Also have to create a list of element-types -- to do that we have to #convert from my numbering system to the TVTK numbering system if mesh.dim == 3: type_convert = np.array([tvtk.Tetra().cell_type,\ tvtk.Hexahedron().cell_type, tvtk.Wedge().cell_type]) elif mesh.dim == 2: type_convert = np.array([tvtk.Triangle().cell_type,\ tvtk.Quad().cell_type]) cell_types = mesh.u_elm_type[mesh.elm_type[elm_ids]] cell_types = type_convert[cell_types] #To help visualize the mesh, we color it be the distance from the origin if mesh.dim == 3: x, y, z = mesh.vert[:].T elif mesh.dim == 2: x, y = mesh.vert[:, :2].T z = np.zeros_like(x) dist = np.sqrt(x**2 + y**2 + z**2) #and we scale the x, y, z, coordinates according the the assigned 'scale' points = np.column_stack((x * scale[0], y * scale[1], z * scale[2])) #Now we create the data-structures cell_array = tvtk.CellArray() cell_array.set_cells(len(offset), cells) ug = tvtk.UnstructuredGrid(points=points) ug.set_cells(cell_types, offset, cell_array) ug.point_data.scalars = dist.ravel() ug.point_data.scalars.name = 'Distance from Origin' #Next we set the new data-structures as a mayavi source src = sources.vtk_data_source.VTKDataSource(data=ug) #Extract the edges from the grid edges = mlab.pipeline.extract_edges(src) #Use a shorter name for the elements elm = mesh.elm[elm_ids, :] if any(labels) and len(elm) > 20: string = "WARNING:\nAre you sure you want to label more than 20" + \ "elements in 3D? -- Labels are very memory" + \ "(apparently -- who would have guessed?) \nY(es) to proceed:" answer = raw_input(string) if answer.lower() not in ['yes', 'y', 'yes']: labels = [0, 0, 0] #Next add labels if desired: maxdist = np.max(dist) / 2. if labels[0]: #Vertex labels for i in xrange(len(offset)): for vnum in elm[i, :]: if vnum >= 0: mlab.text3d(points[vnum, 0], points[vnum, 1], \ points[vnum, 2], '%d' % vnum, \ color=(0, 0, 0), scale=0.02*maxdist) if labels[1]: #element labels for i in xrange(len(offset)): if elm_ids[i] < 0: elm_label = mesh.numelm + elm_ids[i] else: elm_label = elm_ids[i] x = np.mean(points[elm[i, :cells[offset[i]]], 0]) y = np.mean(points[elm[i, :cells[offset[i]]], 1]) z = np.mean(points[elm[i, :cells[offset[i]]], 2]) mlab.text3d(x, y, z, '%d' % elm_label, color=(0.3, 0.3, 0.9), \ scale=0.03*maxdist) if labels[2]: #edge labels if len(elm_ids) < len(mesh.elm): elm2ed = connect_elm2ed(mesh.elm2elm[:], mesh.ed2ed) ed_ids = np.unique(elm2ed[elm_ids]) ed_ids = ed_ids[ed_ids >= 0] ed2ed = mesh.ed2ed[ed_ids, :] else: ed_ids = np.arange(len(mesh.ed2ed)) ed2ed = mesh.ed2ed[:] for i in xrange(len(ed2ed)): n = 8 if ed2ed[i, -1] < 0: n = 7 x = np.mean(points[ed2ed[i, 4:n], 0]) y = np.mean(points[ed2ed[i, 4:n], 1]) z = np.mean(points[ed2ed[i, 4:n], 2]) mlab.text3d(x, y, z, '%d' % ed_ids[i], color=(0.3, 0.9, 0.3), \ scale=0.03*maxdist) #And plot them! mlab.pipeline.surface(edges, opacity=0.4, line_width=2) mlab.axes() mlab.title('3D mesh', size=0.5, height=0.95) #mlab.show() return cells, offset, cell_types