I am a newer of VTK, and I want to write a program to save my CFD results in Raw .vtu format. I find some examples from git-hub, but both of them don't work. I hope some one can help me. Do you have any Raw .vtu example ?
Questions:
- Do you have a specific programming language you are working in?
- Have you already created the
vtkUnstructuredGrid
? - Do you have a starting point of your code to do this?
Example
Here’s a demo for creating and saving a vtkUnstructuredGrid
:
import vtk
from vtk.util.numpy_support import (numpy_to_vtk, numpy_to_vtkIdTypeArray,
vtk_to_numpy)
import numpy as np
ID_TYPE = np.int32
if vtk.VTK_ID_TYPE == 12:
ID_TYPE = np.int64
offset = np.array([0, 9], np.int8)
cells = np.array([8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 10, 11, 12, 13, 14, 15])
cell_type = np.array([vtk.VTK_HEXAHEDRON, vtk.VTK_HEXAHEDRON], np.int32)
cell1 = np.array([[0, 0, 0],
[1, 0, 0],
[1, 1, 0],
[0, 1, 0],
[0, 0, 1],
[1, 0, 1],
[1, 1, 1],
[0, 1, 1]])
cell2 = np.array([[0, 0, 2],
[1, 0, 2],
[1, 1, 2],
[0, 1, 2],
[0, 0, 3],
[1, 0, 3],
[1, 1, 3],
[0, 1, 3]])
points = np.vstack((cell1, cell2)).astype(np.int32)
if offset.dtype != ID_TYPE:
offset = offset.astype(ID_TYPE)
if cells.dtype != ID_TYPE:
cells = cells.astype(ID_TYPE)
if not cells.flags['C_CONTIGUOUS']:
cells = np.ascontiguousarray(cells)
if cells.ndim != 1:
cells = cells.ravel()
if cell_type.dtype != np.uint8:
cell_type = cell_type.astype(np.uint8)
# Get number of cells
ncells = cell_type.size
# Convert to vtk arrays
cell_type = numpy_to_vtk(cell_type)
offset = numpy_to_vtkIdTypeArray(offset)
vtkcells = vtk.vtkCellArray()
vtkcells.SetCells(ncells, numpy_to_vtkIdTypeArray(cells.ravel()))
# Convert points to vtkPoints object
vtkpts = vtk.vtkPoints()
vtkpts.SetData(numpy_to_vtk(points))
# Create unstructured grid
grid = vtk.vtkUnstructuredGrid()
grid.SetPoints(vtkpts)
grid.SetCells(cell_type, offset, vtkcells)
# And save the grid
writer = vtk.vtkXMLUnstructuredGridWriter()
writer.SetDataModeToBinary()
writer.SetFileName('test.vtu')
writer.SetInputData(grid)
writer.Write()
Simpler Example
If you are working in Python, you should check out vtki
as it makes the creation of unstructured grids a bit more intuitive and handles all the file IO so you don’t need to worry about the nuances:
>>> import vtk
>>> import vtki
>>> from vtki import examples
Load a sample unstructured grid
>>> grid = examples.load_hexbeam()
>>> isinstance(grid, vtk.vtkUnstructuredGrid)
True
>>> grid.save('grid.vtu')
1
Thank you very much, Bane. I have modified my code follow your example, it works now. Thank you!
hi…
i write VTU file but i fail to visualize using paraview
do you whats wrong with my scripts
Could you share your script and use markdown formatting, please?
I just post my fortran code to output SPH particles for Paraview in Binary. I don’t use the library function. I just writing follow the manual. I hope this may help you.
Best regards.
Fortran Code
any alternative to share script? i did not understand using markdown
i tried to share from drive
test1.vtu
I download your vtu file, it was saved in ASCII, I think you can follow the vtk format file to find the errors and lots of the topics discuss about the results saving in ASCII.
!======================================================================================================
! Unstructure grid ASCII Format 2 (xml style)
!------------------------------------------------------------------------------------------------------
! Output the data
! Header of the file
write (File_index,'(a)') '<?xml version="1.0"?>'
write (File_index,'(a)') '<VTKFile type="UnstructuredGrid" version="0.1" byte_order="BigEndian">'
write (File_index,'(a)') '<UnstructuredGrid>'
!Point number
write (File_index,*) '<Piece NumberOfPoints="',Ture_total_particle_number,'" NumberOfCells="',0,'" >'
!&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
write (File_index,'(a)') '<Points>' !Points position
!Data array type (Position)
write (File_index,'(a)') '<DataArray name="Position" type="Float32" NumberOfComponents="3" format="ascii">'
if (dim==2) then
do i = 1,Ture_total_particle_number
write (File_index,'(10F10.5)') (particle_position(i,j)/NonDimension_Reference_Length,j=1,dim),0.0000
enddo
else if (dim==3) then
do i = 1,Ture_total_particle_number
write (File_index,'(10F10.5)') (particle_position(i,j)/NonDimension_Reference_Length,j=1,dim)
enddo
endif
write (File_index,'(a)') '</DataArray>'
write (File_index,'(a)') '</Points>' !End Points position
!&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
!&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
!Point data
!Velocity
write (File_index,'(a)') '<PointData Scalars="Pressure" Vectors="Velocity" >' !Data at points
write (File_index,'(a)') '<DataArray type="Float32" Name="Velocity" NumberOfComponents="3" format="ascii">'
if (dim==2) then
do i = 1,Ture_total_particle_number
write (File_index,'(10F10.5)') (particle_velocity(i,j)/velocity_0,j=1,dim),0.0000
enddo
else if (dim==3) then
do i = 1,Ture_total_particle_number
write (File_index,'(10F10.5)') (particle_velocity(i,j)/velocity_0,j=1,dim)
enddo
endif
write (File_index,'(a)') '</DataArray>'
!------------------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------------------
!Pressure
write (File_index,'(a)') '<DataArray type="Float32" Name="Pressure" format="ascii">'
do i = 1,Ture_total_particle_number
write (File_index,'(10F10.5)') (particle_press(i)-Background_Pressure)/pressure_0
enddo
write (File_index,'(a)') '</DataArray>'
!------------------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------------------
!Density (If you have other scalars, you just added them in point data)
write (File_index,'(a)') '<DataArray type="Float32" Name="Pressure" format="ascii">'
do i = 1,Ture_total_particle_number
write (File_index,'(10F10.5)') particle_rho(i)
enddo
write (File_index,'(a)') '</DataArray>'
!------------------------------------------------------------------------------------------------------
write (File_index,'(a)') '</PointData>' !End Points Data type
!&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
!------------------------------------------------------------------------------------------------------
!File end
write (File_index,'(a)') '<Cells>'
write (File_index,'(a)') '<DataArray type="Int32" Name="connectivity" format="ascii">'
write (File_index,'(a)') '</DataArray>'
write (File_index,'(a)') '<DataArray type="Int32" Name="offsets" format="ascii">'
write (File_index,'(a)') '</DataArray>'
write (File_index,'(a)') '<DataArray type="UInt8" Name="types" format="ascii">'
write (File_index,'(a)') '</DataArray>'
write (File_index,'(a)') '</Cells>'
write (File_index,'(a)') '</Piece>'
write (File_index,'(a)') '</UnstructuredGrid>'
write (File_index,'(a)') '</VTKFile>'
!------------------------------------------------------------------------------------------------------
close(File_index)
!======================================================================================================
Sorry about that I have no time to read your file carefully, so I post the source fortran code for outputting the results in ASCII format. With the help of the VTK format, I think you can get the procedures clearly and find the errors.
Best regards.
hi
please tried this file
i have same file, but the other one is from matlab code
in coba2.vtu its worked
coba2.vtu
my plan is from matlab code give output VTU file, and another simator read this input file