def write_vtp_header(path, prefix, vertices, connectivity, offsets, nPoints, nPolygons, variable_list, outType, xtime=None): vtkFile = VtkFile("{}/{}".format(path, prefix), VtkPolyData) #if xtime is not None: # vtkFile.openElement(str("metadata")) # vtkFile.openElement(str("xtime")) # vtkFile.xml.addText(str(xtime)) # vtkFile.closeElement(str("xtime")) # vtkFile.closeElement(str("metadata")) vtkFile.openElement(vtkFile.ftype.name) vtkFile.openPiece(npoints=nPoints, npolys=nPolygons) vtkFile.openElement(str("Points")) vtkFile.addData(str("points"), vertices) vtkFile.closeElement(str("Points")) vtkFile.openElement(str("Polys")) vtkFile.addData(str("connectivity"), connectivity) vtkFile.addData(str("offsets"), offsets) vtkFile.closeElement(str("Polys")) vtkFile.openData(str("Point"), scalars='solution') for var in variable_list: if var in vec_vars: vtkFile.addHeader('x'+var, outType, nPoints, 1) vtkFile.addHeader('y'+var, outType, nPoints, 1) vtkFile.addHeader('z'+var, outType, nPoints, 1) else: vtkFile.addHeader(var, outType, nPoints, 1) vtkFile.closeData(str("Point")) vtkFile.closePiece() vtkFile.closeElement(vtkFile.ftype.name) vtkFile.appendData(vertices) vtkFile.appendData(connectivity) vtkFile.appendData(offsets) return vtkFile
#w.addData("Passive", passive) #w.closeData("Point") # Cell data #pressure = np.zeros([nx, ny, nz], dtype="float64", order='F') if mhd: w.openData("Cell", scalars = ("Density","RhoNeutros","GasPressure"), vectors = ("Velocity","Magnetic")) else: w.openData("Cell", scalars = "Density", vectors = ("Velocity")) w.addData("Density", rrho) w.addData("Velocity", (vvx,vvy,vvz)) w.addData("GasPressure",ppgas) if mhd: w.addData("Magnetic", (bbx,bby,bbz)) w.addData("RhoNeutros", rrho_n) w.closeData("Cell") # Coordinates of cell vertices w.openElement("Coordinates") w.addData("x_coordinates", x); w.addData("y_coordinates", y); w.addData("z_coordinates", z); w.closeElement("Coordinates"); w.closePiece() w.closeGrid() #w.appendData(data = passive) w.appendData(data = rrho) w.appendData(data = (vvx,vvy,vvz)) w.appendData(data = ppgas)
x = np.arange(0, lx + 0.1 * dx, dx, dtype='float64') y = np.arange(0, ly + 0.1 * dy, dy, dtype='float64') z = np.arange(0, lz + 0.1 * dz, dz, dtype='float64') start, end = (0, 0, 0), (nx, ny, nz) w = VtkFile("./evtk_test", VtkRectilinearGrid) w.openGrid(start=start, end=end) w.openPiece(start=start, end=end) # Point data temp = np.random.rand(npoints) vx = vy = vz = np.zeros([nx + 1, ny + 1, nz + 1], dtype="float64", order='F') w.openData("Point", scalars="Temperature", vectors="Velocity") w.addData("Temperature", temp) w.addData("Velocity", (vx, vy, vz)) w.closeData("Point") # Cell data pressure = np.zeros([nx, ny, nz], dtype="float64", order='F') w.openData("Cell", scalars="Pressure") w.addData("Pressure", pressure) w.closeData("Cell") # Coordinates of cell vertices w.openElement("Coordinates") w.addData("x_coordinates", x) w.addData("y_coordinates", y) w.addData("z_coordinates", z) w.closeElement("Coordinates") w.closePiece()
#Add to write file. for step in times: filename = new_data + '/' + str(step) w = VtkFile(filename, VtkStructuredGrid) #evtk_test0 w.openGrid(start=start, end=end) w.openPiece(start=start, end=end) w.openData("Point", scalars=scalar_vars_string, vectors="Velocity") for key in scalar_vars: w.addData(str(key), np.array(f[key][step])) vx = np.array(f['u'][step]) vy = np.array(f['v'][step]) vz = np.array(f['w'][step]) w.addData("Velocity", (vx, vy, vz)) w.closeData("Point") # Coordinates of cell vertices w.openElement("Points") w.addData("points", (X, Y, Z)) w.closeElement("Points") w.closePiece() w.closeGrid() # Need to modify parameters for key in scalar_vars: w.appendData(data=np.array(f[key][step])) w.appendData(data=(vx, vy, vz)) w.appendData((X, Y, Z)) w.save()
end=None, npoints=npoints, ncells=None, nverts=None, nlines=None, nstrips=None, npolys=npolys) # Point data vtkBlocks.openElement("Points") vtkBlocks.addHeader("points", vertices.dtype.name, len(pointIDs), 3) vtkBlocks.closeElement("Points") vtkBlocks.openData("Point", scalars="pointIDs") # vtkBlocks.addData("pointIDs", pointIDs) vtkBlocks.addHeader("pointIDs", pointIDs.dtype.name, len(pointIDs), 1) vtkBlocks.closeData("Point") # Cell data vtkBlocks.openData("Cell", normals="normals") vtkBlocks.addHeader("normals", normals.dtype.name, npolys, 3) vtkBlocks.closeData("Cell") # Poly data vtkBlocks.openElement("Polys") vtkBlocks.addHeader("connectivity", connectivity.dtype.name, len(connectivity), 1) vtkBlocks.addHeader("offsets", offsets.dtype.name, len(offsets), 1) # vtkBlocks.addData("connectivity", connectivity) # vtkBlocks.addData("offsets", offsets) vtkBlocks.closeElement("Polys")
normals = np.asarray(data[i].normals) connectivity = np.asarray(data[i].connectivity) offsets = np.asarray(data[i].offsets) vtkBlocks.openPiece(start = None, end = None, npoints = npoints, ncells = None, nverts = None, nlines = None, nstrips = None, npolys = npolys) # Point data vtkBlocks.openElement("Points") vtkBlocks.addHeader("points", vertices.dtype.name, len(pointIDs), 3) vtkBlocks.closeElement("Points") vtkBlocks.openData("Point", scalars = "pointIDs") # vtkBlocks.addData("pointIDs", pointIDs) vtkBlocks.addHeader("pointIDs", pointIDs.dtype.name, len(pointIDs), 1) vtkBlocks.closeData("Point") # Cell data vtkBlocks.openData("Cell", normals = "normals") vtkBlocks.addHeader("normals", normals.dtype.name, npolys, 3) vtkBlocks.closeData("Cell") # Poly data vtkBlocks.openElement("Polys") vtkBlocks.addHeader("connectivity", connectivity.dtype.name, len(connectivity), 1) vtkBlocks.addHeader("offsets", offsets.dtype.name, len(offsets), 1) # vtkBlocks.addData("connectivity", connectivity) # vtkBlocks.addData("offsets", offsets) vtkBlocks.closeElement("Polys") vtkBlocks.closePiece()
new_data = new_dir + '/data' os.system('mkdir -p ' + new_dir) os.system('mkdir -p ' + new_data) for step in times: filename = new_data + '/' + str(step) w = VtkFile(filename, VtkStructuredGrid) #evtk_test0 w.openGrid(start=start, end=end) w.openPiece(start=start, end=end) w.openData('Point', scalars=scalar_vars_string, vectors='Velocity') for key in scalar_vars: w.addData(str(key), np.array(f[key][step])) vx = np.array(f['u'][step]) vy = np.array(f['v'][step]) vz = np.array(f['w'][step]) w.addData('Velocity', (vx, vy, vz)) w.closeData('Point') w.openElement('Points') w.addData('points', (X, Y, Z)) w.closeElement('Points') w.closePiece() w.closeGrid() for key in scalar_vars: w.appendData(data=np.array(f[key][step])) w.appendData(data=(vx, vy, vz)) w.appendData((X, Y, Z)) w.save() print('file: ' + filename + ' added') g = VtkGroup(new_dir + '/group') for step in times: g.addFile(filepath=new_data + '/' + str(step) + '.vts', sim_time=float(step))
def write_vtp_header(path, prefix, active_var_index, var_indices, variable_list, all_dim_vals, vertices, connectivity, offsets, nPoints, nPolygons, outType, cellData=True, pointData=False, xtime=None): # {{{ vtkFile = VtkFile("{}/{}".format(path, prefix), VtkPolyData) if xtime is not None: vtkFile.openElement(str("metadata")) vtkFile.openElement(str("xtime")) vtkFile.xml.addText(str(xtime)) vtkFile.closeElement(str("xtime")) vtkFile.closeElement(str("metadata")) vtkFile.openElement(vtkFile.ftype.name) vtkFile.openPiece(npoints=nPoints, npolys=nPolygons) vtkFile.openElement(str("Points")) vtkFile.addData(str("points"), vertices) vtkFile.closeElement(str("Points")) vtkFile.openElement(str("Polys")) vtkFile.addData(str("connectivity"), connectivity) vtkFile.addData(str("offsets"), offsets) vtkFile.closeElement(str("Polys")) if (cellData): vtkFile.openData( str("Cell"), scalars=[str(var) for var in variable_list[active_var_index]]) for iVar in var_indices: var_name = variable_list[iVar] (out_var_names, dim_list) = \ get_hyperslab_name_and_dims(var_name, all_dim_vals[var_name]) for out_var_name in out_var_names: vtkFile.addHeader(str(out_var_name), outType, nPolygons, 1) vtkFile.closeData(str("Cell")) if (pointData): vtkFile.openData( str("Point"), scalars=[str(var) for var in variable_list[active_var_index]]) for iVar in var_indices: var_name = variable_list[iVar] (out_var_names, dim_list) = \ get_hyperslab_name_and_dims(var_name, all_dim_vals[var_name]) for out_var_name in out_var_names: vtkFile.addHeader(str(out_var_name), outType, nPoints, 1) vtkFile.closeData(str("Point")) vtkFile.closePiece() vtkFile.closeElement(vtkFile.ftype.name) vtkFile.appendData(vertices) vtkFile.appendData(connectivity) vtkFile.appendData(offsets) return vtkFile # }}}
def gridToVTK_with_start(path, x, y, z, cellData=None, pointData=None, fieldData=None, start=None): """ Write data values as a rectilinear or rectangular grid. Parameters ---------- path : str name of the file without extension where data should be saved. x : array-like x coordinate axis. y : array-like y coordinate axis. z : array-like z coordinate axis. cellData : dict, optional dictionary containing arrays with cell centered data. Keys should be the names of the data arrays. Arrays must have the same dimensions in all directions and must contain only scalar data. pointData : dict, optional dictionary containing arrays with node centered data. Keys should be the names of the data arrays. Arrays must have same dimension in each direction and they should be equal to the dimensions of the cell data plus one and must contain only scalar data. fieldData : dict, optional dictionary with variables associated with the field. Keys should be the names of the variable stored in each array. start : tuple, optional start position of data extent. Returns ------- str Full path to saved file. Notes ----- coordinates of the nodes of the grid. They can be 1D or 3D depending if the grid should be saved as a rectilinear or logically structured grid, respectively. Arrays should contain coordinates of the nodes of the grid. If arrays are 1D, then the grid should be Cartesian, i.e. faces in all cells are orthogonal. If arrays are 3D, then the grid should be logically structured with hexahedral cells. In both cases the arrays dimensions should be equal to the number of nodes of the grid. """ import numpy as np from pyevtk.vtk import VtkFile, VtkStructuredGrid # Extract dimensions if start is None: start = (0, 0, 0) s = x.shape end = (start[0]+s[0]-1, start[1]+s[1]-1, start[2]+s[2]-1) w = VtkFile(path, VtkStructuredGrid) w.openGrid(start=start, end=end) w.openPiece(start=start, end=end) w.openElement("Points") w.addData("coordinates", (x, y, z)) w.closeElement("Points") # Point data if pointData: keys = list(pointData.keys()) w.openData("Point", scalars=keys[0]) for key in keys: data = pointData[key] w.addData(key, data) w.closeData("Point") # Cell data if cellData: keys = list(cellData.keys()) w.openData("Cell", scalars=keys[0]) for key in keys: data = cellData[key] w.addData(key, data) w.closeData("Cell") # Field data # https://www.visitusers.org/index.php?title=Time_and_Cycle_in_VTK_files#XML_VTK_files if fieldData: keys = list(fieldData.keys()) w.openData("Field") # no attributes in FieldData for key in keys: data = fieldData[key] w.addData(key, data) w.closeData("Field") w.closePiece() w.closeGrid() # Write coordinates w.appendData((x, y, z)) # Write data if pointData is not None: keys = list(pointData.keys()) for key in keys: data = pointData[key] w.appendData(data) if cellData is not None: keys = list(cellData.keys()) for key in keys: data = cellData[key] w.appendData(data) if fieldData is not None: keys = list(fieldData.keys()) for key in keys: data = fieldData[key] w.appendData(data) w.save() return w.getFileName()