def saveVtkUnstructuredGrid(vtkGrid, file_name): """Writes vtkGrid to file. (.vtu XML format). To create a vtkGrid from a PRST grid: from prst.plotting import createVtkUnstructuredGrid vtkGrid = createVtkUnstructuredGrid(G) """ try: from tvtk.api import tvtk except: prst.log.error("Couldn't import tvtk") return w = tvtk.XMLUnstructuredGridWriter(input=vtkGrid, file_name=file_name) return w.write()
def UnstructuredGrid(x, y, z, data, OutFileName): #mlab.options.backend = 'envisage' #------------ Writing .vti files ---------------------- # Make the data. points = np.array([x, y, z]).T scalars = np.array(data) # Now create the UG. ug = tvtk.UnstructuredGrid(points=points) ug.point_data.scalars = scalars ug.point_data.scalars.name = 'Data' fileOut = OutFileName + '.vtu' print fileOut w = tvtk.XMLUnstructuredGridWriter(input=ug, file_name=fileOut) w.write()
def test_unique_name(self): "Does the get_unique_id method work." t = tvtk.XMLUnstructuredGridWriter() tape = self.tape self.assertEqual(tape._get_unique_name(t), 'xml_unstructured_grid_writer') self.assertEqual(tape._get_unique_name(t), 'xml_unstructured_grid_writer1') t = Toy() self.assertEqual(tape._get_unique_name(t), 'toy') t = (1, 2) self.assertEqual(tape._get_unique_name(t), 'tuple0') l = [1, 2] self.assertEqual(tape._get_unique_name(l), 'list0') d = {'a': 1} self.assertEqual(tape._get_unique_name(d), 'dict0') self.assertEqual(tape._get_unique_name(1), 'int0')
def vtk_write(self, file_name): writer = tvtk.XMLUnstructuredGridWriter() writer.set_input(self.to_vtk()) writer.file_name = file_name writer.write()
class ResultData(object): #定义写入类 #tvtk的写入类,与vtk的写入类很类似,在mayavi中,不能直接用vtk类,而要用由python 对vtk封装后的 tvtk,如果需要读数据,也可以用类似的API writer = tvtk.XMLUnstructuredGridWriter() def __init__(self): ''' 初始化结果数据,为画图做准备 以unstrgrid开头的数据属于 vtk的非结构网格数据,这种数据可以保存到本地,但是不能直接用来绘图 需要通过VTKDataSource 作为接口,生成mayavi可以渲染的数据源 以vtkdatasource开头的数据属于经过了VTKDataSource函数处理过的数据,可以被mayavi渲染 ''' self.address = 'H:\GitHub\Topology-optimization-of-structure-via-simp-method' self.index = [] self.U = np.zeros((global_variable.NODE_COUNTS, 1)) self.stress = np.zeros((global_variable.NODE_COUNTS, 1)) self.strain = np.zeros((global_variable.NODE_COUNTS, 1)) self.density = np.ones((global_variable.ELEMENT_COUNTS, 1)) #unstructuredgrid 类型数据 self.unstrgrid_mesh = None self.unstrgrid_density = None self.unstrgrid_stress = None self.unstrgrid_strain = None self.unstrgrid_displacement = None #vtksource类型数据 self.vtkdatasource_mesh = None self.vtkdatasource_displacement = None self.vtkdatasource_stress = None self.vtkdatasource_strain = None self.vtkdatasource_density = None #初始化unstructuredgrid 与 vtkdatasource self.initialize() #以用户自定义值 初始化#初始化unstructuredgrid 与 vtkdatasource def initialize(self): self.unstrgrid_mesh = self.generate_unstrgrid_mesh() self.unstrgrid_density = self.generate_unstrgrid_mesh() self.unstrgrid_stress = self.generate_unstrgrid_mesh() self.unstrgrid_strain = self.generate_unstrgrid_mesh() self.unstrgrid_displacement = self.generate_unstrgrid_mesh() self.write_unstructured_data() self.vtkdatasource() def undate_ansys_date(self, U, stress, strain, x): self.U = U self.stress = stress self.strain = strain self.density = x #生成非结构网格数据 def write_unstructured_data(self, loop=-1): ''' 设置if条件语句是为了第一次初始化的结果不写入本地文件 ''' # 存储位移数据 self.update_unstrgrid_displacement(self.U, 0.05, 0) if loop != -1: name = 'displacement_0' + str(loop + 1) + '.vtu' #设置写入的文件 self.writer.set_input_data(self.unstrgrid_displacement) #设置写入文件的名称 self.writer.file_name = self.address + '\displacement\\' + name #开始写入 self.writer.write() # 应力数据 self.update_unstrgrid_stress(self.stress) if loop != -1: name = 'stress_0' + str(loop) + '.vtu' self.writer.set_input_data(self.unstrgrid_stress) self.writer.file_name = self.address + '\stress\\' + name self.writer.write() # 应变数据 self.update_unstrgrid_strain(self.strain) if loop != -1: name = 'strain_0' + str(loop) + '.vtu' self.writer.set_input_data(self.unstrgrid_strain) self.writer.file_name = self.address + '\strain\\' + name self.writer.write() # 密度数据 self.update_unstrgrid_density(self.density) if loop != -1: name = 'density_0' + str(loop) + '.vtu' self.writer.set_input_data(self.unstrgrid_density) self.writer.file_name = self.address + '\\density\\' + name self.writer.write() #生成vtkdata类型数据 def vtkdatasource(self): self.vtkdatasource_mesh = VTKDataSource(data=self.unstrgrid_mesh, name='Geometry') self.vtkdatasource_displacement = VTKDataSource( data=self.unstrgrid_displacement, name='DisplacementData') self.vtkdatasource_stress = VTKDataSource(data=self.unstrgrid_stress, name='StessData') self.vtkdatasource_strain = VTKDataSource(data=self.unstrgrid_strain, name='StrainData') self.vtkdatasource_density = VTKDataSource(data=self.unstrgrid_density, name='DensiytData') #生成网格数据,filter参数是为了过滤我们不想显示的密度单元,1表示全部显示,0表示全部不显示 def generate_unstrgrid_mesh(self, filter=0): points = global_variable.NODE_COORDINATES self.index = where(self.density >= (1.0 - filter))[0].tolist() cells = (global_variable.ELEMENT_ATTRIBUTES[self.index, :] - 1) if global_variable.GRID_TYPE == 'Polygon': cell_tpye = tvtk.Polygon().cell_type if global_variable.GRID_TYPE == 'Hexahedron': cell_tpye = tvtk.Hexahedron().cell_type grid = tvtk.UnstructuredGrid(points=points) grid.set_cells(cell_tpye, cells) return grid #生成密度数据 def update_unstrgrid_density(self, density): self.unstrgrid_density.cell_data.scalars = density[self.index] self.unstrgrid_density.cell_data.scalars.name = 'density' #生成位移数据,scale1表示单纯位移图形的位移缩放因子,scale2用于应力、应变、密度图形的位移缩放 def update_unstrgrid_displacement(self, U, scale1=0.05, scale2=0): if U.shape[1] == 2: U = np.column_stack((U, zeros(U.shape[0]))) self.unstrgrid_displacement.points = global_variable.NODE_COORDINATES + scale1 * U self.unstrgrid_displacement.point_data.scalars = sqrt(sum(U**2, axis=1)) self.unstrgrid_displacement.point_data.scalars.name = 'USUM' self.unstrgrid_density.points = global_variable.NODE_COORDINATES + scale2 * U self.unstrgrid_strain.points = global_variable.NODE_COORDINATES + scale2 * U self.unstrgrid_stress.points = global_variable.NODE_COORDINATES + scale2 * U #生成应力数据 def update_unstrgrid_stress(self, stress): self.unstrgrid_stress.point_data.scalars = stress self.unstrgrid_stress.point_data.scalars.name = 'stress' #生成应变数据 def update_unstrgrid_strain(self, strain): self.unstrgrid_strain.point_data.scalars = strain self.unstrgrid_strain.point_data.scalars.name = 'strain'
def save_xml(ug, file_name): """Shows how you can save the unstructured grid dataset to a VTK XML file.""" w = tvtk.XMLUnstructuredGridWriter(input=ug, file_name=file_name) w.write()
pd = tvtk.PointData() """ pd.scalars = v pd.scalars.name = 'v' ug.point_data.add_array(pd.scalars) """ pd.vectors = uvw.tolist() pd.vectors.name = 'Disp' ug.point_data.add_array(pd.vectors) cdJg = tvtk.CellData() cdJg.scalars = Jg cdJg.scalars.name='J_g' ug.cell_data.add_array(cdJg.scalars) cdPID = tvtk.CellData() cdPID.scalars = PropID cdPID.scalars.name='PropID' ug.cell_data.add_array(cdPID.scalars) cdState = tvtk.CellData() cdState.scalars = State cdState.scalars.name='State' ug.cell_data.add_array(cdState.scalars) filename = mc.folderPostProcess+'/paraview_'+'{0:04d}'.format(dc.step)+'.vtu' w = tvtk.XMLUnstructuredGridWriter(input = ug, file_name=(filename)) w.write()
writer = tvtk.XMLStructuredGridWriter(input=sgrid, file_name=os.path.join( run_dir, "vtk", "test.vts")) writer.write() # cell-based mesh (use unstructured mesh, polydata is for surfaces and not # volumes) sdims, spts, vertices = smesh_cells(gchemgrid.e_lon_4x5, gchemgrid.e_lat_4x5, gchemgrid.e_km_geos5_r * 1e3, lev_scale, lev_offset, True) sgrid = tvtk.UnstructuredGrid(points=spts) tet_type = tvtk.Hexahedron().cell_type # cells = hexahedrons sgrid.set_cells(tet_type, vertices) ijavg = ctm_f.filter(category=field_category) for tracer in ijavg: print "export diagnostic %s to VTK" % tracer.full_name vals_array = tracer.values * units_conv[mr_units] #ar = sgrid.cell_data.scalars = np.ravel(vals_array.transpose()) #sgrid.cell_data.scalars.name = tracer.full_name ar = sgrid.cell_data.add_array(np.ravel(vals_array.transpose())) sgrid.cell_data.get_array(ar).name = tracer.full_name outfile = os.path.join(run_dir, "vtk", "test_ijavg_cell.vtu") if os.path.exists(outfile): os.remove(outfile) writer = tvtk.XMLUnstructuredGridWriter(input=sgrid, file_name=outfile) writer.write()