VTK unstructured grid raw format in XML type

  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:

  1. Do you have a specific programming language you are working in?
  2. Have you already created the vtkUnstructuredGrid?
  3. 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!

1 Like

hi…
i write VTU file but i fail to visualize using paraview
do you whats wrong with my scripts

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 50.00 50.00 80.00 80.00 80.00 50.00 50.00 80.00 80.00 80.00 50.00 50.00 80.00 80.00 80.00 50.00 50.00 80.00 80.00 80.00 50.00 50.00 80.00 80.00 80.00 200.00 200.00 200.00 200.00 200.00 200.00 200.00 200.00 200.00 200.00 200.00 200.00 200.00 200.00 200.00 380.00 380.00 380.00 380.00 380.00 380.00 380.00 380.00 380.00 380.00 0 0 0 1 0 0 2 0 0 3 0 0 4 0 0 5 0 0 5 1 0 4 1 0 3 1 0 2 1 0 1 1 0 0 1 0 5 2 0 4 2 0 3 2 0 2 2 0 1 2 0 0 2 0 5 3 0 4 3 0 3 3 0 2 3 0 1 3 0 0 3 0 5 4 0 4 4 0 3 4 0 2 4 0 1 4 0 0 4 0 5 5 0 4 5 0 3 5 0 2 5 0 1 5 0 0 5 0 0 0 1 1 0 1 2 0 1 3 0 1 4 0 1 5 0 1 5 1 1 4 1 1 3 1 1 2 1 1 1 1 1 0 1 1 5 2 1 4 2 1 3 2 1 2 2 1 1 2 1 0 2 1 5 3 1 4 3 1 3 3 1 2 3 1 1 3 1 0 3 1 5 4 1 4 4 1 3 4 1 2 4 1 1 4 1 0 4 1 5 5 1 4 5 1 3 5 1 2 5 1 1 5 1 0 5 1 0 1 10 11 36 37 46 47 1 2 9 10 37 38 45 46 2 3 8 9 38 39 44 45 3 4 7 8 39 40 43 44 4 5 6 7 40 41 42 43 11 10 16 17 47 46 52 53 10 9 15 16 46 45 51 52 9 8 14 15 45 44 50 51 8 7 13 14 44 43 49 50 7 6 12 13 43 42 48 49 17 16 22 23 53 52 58 59 16 15 21 22 52 51 57 58 15 14 20 21 51 50 56 57 14 13 19 20 50 49 55 56 13 12 18 19 49 48 54 55 23 22 28 29 59 58 64 65 22 21 27 28 58 57 63 64 21 20 26 27 57 56 62 63 20 19 25 26 56 55 61 62 19 18 24 25 55 54 60 61 29 28 34 35 65 64 70 71 28 27 33 34 64 63 69 70 27 26 32 33 63 62 68 69 26 25 31 32 62 61 67 68 25 24 30 31 61 60 66 67 DataArray type="Int64" Name="offsets" format="ascii" RangeMin="8" RangeMax="200" 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120 128 136 144 152 160 168 176 184 192 200 DataArray> 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12

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.

vtk formats file

    !======================================================================================================
    ! 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