def exportVTK(pontos, celulas, info, arquivo): # info deve ser algum dado em formato array(ex. pressão, densidade, etc) e # nome_info string com seu rótulo (ex. "pressure") conn = np.empty(len(celulas) * 4) ctype = np.ones(len(celulas)) * VtkQuad.tid conn = np.ravel(celulas) offset = np.array([i * 4 for i in range(1, len(celulas) + 1)]) unstructuredGridToVTK(arquivo, np.ravel(pontos[:,0]), np.ravel(pontos[:,1]),np.ravel(pontos[:,2]), \ connectivity = conn, offsets = offset, cell_types = ctype, \ cellData = info, pointData = None)
def write_tris(fname, verts, tris, data=None): x = verts[:, 0].copy() # deep copy required y = verts[:, 1].copy() z = verts[:, 2].copy() ntris = tris.shape[0] conn = np.empty(3 * ntris) for i in range(ntris): conn[3 * i] = tris[i, 0] - 1 # index from zero conn[3 * i + 1] = tris[i, 1] - 1 conn[3 * i + 2] = tris[i, 2] - 1 offset = np.zeros(ntris, dtype=int) for i in range(ntris): offset[i] = 3 * (i + 1) ctype = np.full(ntris, VtkTriangle.tid) unstructuredGridToVTK(fname, x, y, z, \ connectivity=conn, offsets=offset, cell_types=ctype, \ cellData=data, pointData=None) # write out vtu file return
def evtk_fileCreation(fileLoc, xcrd, ycrd, zcrd, conn, offsets, cell_types, cellData=None, cellKeys=None, ptsData=None, ptsKeys=None): ''' Wrapper around the unstructuredGridToVTK function Export unstructured grid and associated data. Inputs: fileLoc: name of the file without extension where data should be saved. xcrd, ycrd, zcrd: 1D arrays with coordinates of the vertices of cells. It is assumed that each element has diffent number of vertices. conn: 1D array that defines the vertices associated to each element. Together with offset define the connectivity or topology of the grid. It is assumed that vertices in an element are listed consecutively. offsets: 1D array with the index of the last vertex of each element in the connectivity array. It should have length nelem, where nelem is the number of cells or elements in the grid. cell_types: 1D array with an integer that defines the cell type of each element in the grid. It should have size nelem. This should be assigned from evtk.vtk.VtkXXXX.tid, where XXXX represent the type of cell. Please check the VTK file format specification for allowed cell types. cellData: Dictionary with variables associated to each line. Keys should be the names of the variable stored in each array. All arrays must have the same number of elements. pointData: Dictionary with variables associated to each vertex. Keys should be the names of the variable stored in each array. All arrays must have the same number of elements. Output: fOut: Full path to saved file. ''' fOut = unstructuredGridToVTK(fileLoc, xcrd, ycrd, zcrd, conn, offsets, cell_types, cellData=cellData, cellKeys=cellKeys, pointData=ptsData, pointKeys=ptsKeys) return fOut
def write_tets(fname, verts, tets, data=None): x = verts[:, 0].copy() # deep copy required y = verts[:, 1].copy() z = verts[:, 2].copy() ntets = tets.shape[0] conn = np.empty(4 * ntets) for i in range(ntets): conn[4 * i] = tets[i, 0] - 1 # index from zero conn[4 * i + 1] = tets[i, 1] - 1 conn[4 * i + 2] = tets[i, 2] - 1 conn[4 * i + 3] = tets[i, 3] - 1 offset = np.zeros(ntets, dtype=int) for i in range(ntets): offset[i] = 4 * (i + 1) ctype = np.zeros(ntets) for i in range(ntets): ctype[i] = VtkTetra.tid unstructuredGridToVTK(fname, x, y, z, \ connectivity=conn, offsets=offset, cell_types=ctype, \ cellData=data, pointData=None) # write out vtu file return
def save_mesh(fname, pts, facets, facet_type=5, **kwargs): x = pts[:, 0].astype('float') y = pts[:, 1].astype('float') if pts.shape[1] >= 3: z = pts[:, 2].astype('float') else: z = np.array([0] * x.size).astype('float') connectivity = facets.flatten() ncells = facets.shape[0] offsets = 3 + 3 * np.arange(ncells) types = np.array([facet_type] * ncells) return vtk.unstructuredGridToVTK(fname, x, y, z, connectivity, offsets, types, **kwargs)
def __init__(self, path, mesh, submesh): '''Prebuild and then only receive data''' comm = mesh.mpi_comm() rank = comm.rank size = comm.size dirname, basaname = os.path.dirname(path), os.path.basename(path) if dirname: not os.path.exists(dirname) and comm.rank == 0 and os.mkdir( dirname) local_has_piece = np.zeros(comm.size, dtype=bool) if submesh.num_cells() == 0: self.write_vtu_piece = lambda data, counter, path=path, rank=rank, size=size: ( "%s_p%d_of%d_%06d.vtu" % (path, rank, size, counter)) else: local_has_piece[comm.rank] = True x, y, z = map(np.array, submesh.coordinates().T) cells = submesh.cells() ncells, nvertices_cell = cells.shape assert nvertices_cell == 3 connectivity = cells.flatten() offsets = np.cumsum(np.repeat(3, ncells)) connectivity = connectivity.astype(offsets.dtype) cell_types = VtkTriangle.tid * np.ones(ncells) self.write_vtu_piece = lambda data, counter, path=path, rank=rank, size=size: ( unstructuredGridToVTK("%s_p%d_of%d_%06d" % (path, rank, size, counter), x, y, z, connectivity, offsets, cell_types, cellData=data)) # Root will write the group file global_has_piece = sum(comm.allgather(local_has_piece), np.zeros_like(local_has_piece)) self.has_piece, = np.where(global_has_piece) # Only other piece to remember is for parallel writeing on root self.counter = 0 # Of times write_vtu_piece was called self.path = path self.world_rank = comm.rank self.world_size = comm.size # Also group file goes into the directory self.world_rank == 0 and setattr(self, 'group', VtkGroup(path))
def write_tris(fname, verts, tris, data=None): nverts = verts.shape[0] xyz = np.empty([3, nverts]) # because it needs re-ordering for i in range(nverts): for j in range(3): xyz[j, i] = verts[i, j] ntris = tris.shape[0] conn = np.empty(3 * ntris) for i in range(ntris): conn[3 * i] = tris[i, 0] - 1 # index from zero conn[3 * i + 1] = tris[i, 1] - 1 conn[3 * i + 2] = tris[i, 2] - 1 offset = np.zeros(ntris, dtype=int) for i in range(ntris): offset[i] = 3 * (i + 1) ctype = np.zeros(ntris) for i in range(ntris): ctype[i] = VtkTriangle.tid unstructuredGridToVTK(fname, \ xyz[0,:], xyz[1,:], xyz[2,:], \ connectivity=conn, offsets=offset, cell_types=ctype, \ cellData=data, pointData=None) # write out vtu file return
# Define connectivity or vertices that belongs to each element conn = np.zeros(10) conn[0], conn[1], conn[2] = 0, 1, 3 # first triangle conn[3], conn[4], conn[5] = 1, 4, 3 # second triangle conn[6], conn[7], conn[8], conn[9] = 1, 2, 5, 4 # rectangle # Define offset of last vertex of each element offset = np.zeros(3) offset[0] = 3 offset[1] = 6 offset[2] = 10 # Define cell types ctype = np.zeros(3) ctype[0], ctype[1] = VtkTriangle.tid, VtkTriangle.tid ctype[2] = VtkQuad.tid comments = ["comment 1", "comment 2"] unstructuredGridToVTK("unstructured", x, y, z, connectivity=conn, offsets=offset, cell_types=ctype, cellData=None, pointData=None, comments=comments)
def toVTK(self, dst, vals = None, text = None , default_z = None, verbose = False, comments = None): from evtk.hl import pointsToVTK, polyLinesToVTK, unstructuredGridToVTK from evtk.vtk import VtkPolygon x, y, z, pointsPerShape = self.get_xyz_lists(default_z, verbose) nshapes = len(self.shapes) import numpy as np x = np.array(x) y = np.array(y) z = np.array(z) pointsPerShape = np.array(pointsPerShape) # Export cell data if vals: cellData = {} for k, v in vals.items(): cellData[k] = np.array(v) else: cellData = None #for k, v in cellData.items(): #print(k, v) if comments: pass else: comments = [] if text: comments.append("Text fields as lists with one element for each shape follows...") for k, v in text.items(): cmt = "%s: "%k + "[" + ",".join(v) + "]" #print(cmt) comments.append(cmt) else: comments = None if verbose: print("type (%d): %s"%(self.shape_type, SHP_TYPES[type]) ) st = self.shape_type if st == TS["POINT"] or st == TS["POINTZ"]: # POINT pointsToVTK(dst, x, y, z, data = cellData, comments = comments) elif st == TS["MULTIPOINT"]: # MULTIPOINT pointsToVTK(dst, x, y, z, data = cellData, comments = comments) elif st == TS["POLYLINE"] or st == TS["POLYLINEZ"]: # POLYLINE polyLinesToVTK(dst, x, y, z, pointsPerLine = pointsPerShape, cellData = None, pointData = None) elif st == TS["POLYGON"] or st == TS["POLYGONZ"]: # POLYGON # Points should be counter clock-wise # Add a small check LATER conn = np.array([i for i in range(len(x))]) offsets = np.zeros(nshapes) cell_types = np.ones(nshapes) * VtkPolygon.tid o = 0 for s in range(nshapes): o = o + pointsPerShape[s] offsets[s] = o unstructuredGridToVTK(dst, x, y, z, conn, offsets, cell_types, cellData = cellData, pointData = None, comments = comments) else: assert False, "Not implemented for type %d"%st