# VTK unstructured grid raw format in XML type

**URL:** https://discourse.vtk.org/t/vtk-unstructured-grid-raw-format-in-xml-type/434
**Category:** Support
**Created:** [March 8, 2019, 3:29pm UTC](https://discourse.vtk.org/t/vtk-unstructured-grid-raw-format-in-xml-type/434 "2019-03-08T15:29:18Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Mini\_love](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@Mini\_love](https://discourse.vtk.org/u/Mini_love)
#### Post date: [March 8, 2019, 3:29pm UTC](https://discourse.vtk.org/t/vtk-unstructured-grid-raw-format-in-xml-type/434/1 "2019-03-08T15:29:18Z")

</div>

```
  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 ?
```

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [March 9, 2019, 7:07am UTC](https://discourse.vtk.org/t/vtk-unstructured-grid-raw-format-in-xml-type/434/2 "2019-03-09T07:07:48Z")

</div>

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`:

```python
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`](http://docs.vtki.org/en/latest/) 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:

```python
>>> import vtk
>>> import vtki
>>> from vtki import examples

```

Load a sample unstructured grid

```python
>>> grid = examples.load_hexbeam()
>>> isinstance(grid, vtk.vtkUnstructuredGrid)
True
>>> grid.save('grid.vtu')
1

```

---

<div class="post-metadata">

### Author: ![Mini\_love](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@Mini\_love](https://discourse.vtk.org/u/Mini_love)
#### Post date: [March 9, 2019, 5:47pm UTC](https://discourse.vtk.org/t/vtk-unstructured-grid-raw-format-in-xml-type/434/3 "2019-03-09T17:47:58Z")

</div>

Thank you very much, Bane. I have modified my code follow your example, it works now. Thank you!

---

<div class="post-metadata">

### Author: ![Vaya\_Putra](https://discourse.vtk.org/user_avatar/discourse.vtk.org/vaya_putra/32/587_2.png) [@Vaya\_Putra](https://discourse.vtk.org/u/Vaya_Putra)
#### Post date: [July 2, 2019, 2:02pm UTC](https://discourse.vtk.org/t/vtk-unstructured-grid-raw-format-in-xml-type/434/4 "2019-07-02T14:02:38Z")

</div>

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

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [July 2, 2019, 3:45pm UTC](https://discourse.vtk.org/t/vtk-unstructured-grid-raw-format-in-xml-type/434/5 "2019-07-02T15:45:49Z")

</div>

Could you share your script and use markdown formatting, please?

---

<div class="post-metadata">

### Author: ![Mini\_love](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@Mini\_love](https://discourse.vtk.org/u/Mini_love)
#### Post date: [July 2, 2019, 4:22pm UTC](https://discourse.vtk.org/t/vtk-unstructured-grid-raw-format-in-xml-type/434/6 "2019-07-02T16:22:16Z")

</div>

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](https://drive.google.com/file/d/1_g-s6S95q7v-wgGjjo9kNLM9X6atF4G6/view?usp=sharing)

---

<div class="post-metadata">

### Author: ![Vaya\_Putra](https://discourse.vtk.org/user_avatar/discourse.vtk.org/vaya_putra/32/587_2.png) [@Vaya\_Putra](https://discourse.vtk.org/u/Vaya_Putra)
#### Post date: [July 2, 2019, 4:22pm UTC](https://discourse.vtk.org/t/vtk-unstructured-grid-raw-format-in-xml-type/434/7 "2019-07-02T16:22:22Z")

</div>

any alternative to share script? i did not understand using markdown

---

<div class="post-metadata">

### Author: ![Vaya\_Putra](https://discourse.vtk.org/user_avatar/discourse.vtk.org/vaya_putra/32/587_2.png) [@Vaya\_Putra](https://discourse.vtk.org/u/Vaya_Putra)
#### Post date: [July 2, 2019, 4:29pm UTC](https://discourse.vtk.org/t/vtk-unstructured-grid-raw-format-in-xml-type/434/8 "2019-07-02T16:29:47Z")

</div>

i tried to share from drive  
[test1.vtu](https://drive.google.com/open?id=1YNANzb2AP3knhFHT12LR44lKNn45_wkS)

---

<div class="post-metadata">

### Author: ![Mini\_love](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@Mini\_love](https://discourse.vtk.org/u/Mini_love)
#### Post date: [July 2, 2019, 4:41pm UTC](https://discourse.vtk.org/t/vtk-unstructured-grid-raw-format-in-xml-type/434/9 "2019-07-02T16:41:33Z")

</div>

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](https://vtk.org/wp-content/uploads/2015/04/file-formats.pdf)

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

---

<div class="post-metadata">

### Author: ![Mini\_love](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@Mini\_love](https://discourse.vtk.org/u/Mini_love)
#### Post date: [July 2, 2019, 4:45pm UTC](https://discourse.vtk.org/t/vtk-unstructured-grid-raw-format-in-xml-type/434/10 "2019-07-02T16:45:01Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Vaya\_Putra](https://discourse.vtk.org/user_avatar/discourse.vtk.org/vaya_putra/32/587_2.png) [@Vaya\_Putra](https://discourse.vtk.org/u/Vaya_Putra)
#### Post date: [July 2, 2019, 4:49pm UTC](https://discourse.vtk.org/t/vtk-unstructured-grid-raw-format-in-xml-type/434/11 "2019-07-02T16:49:16Z")

</div>

hi  
please tried this file  
i have same file, but the other one is from matlab code  
in coba2.vtu its worked  
[coba2.vtu](https://drive.google.com/open?id=1RRaKCA09mPvf18c9JmRI3qav7s7m-LJ4)

my plan is from matlab code give output VTU file, and another simator read this input file
