Binary VTK for RECTILINEAR_GRID from fortran code

2.7k views Asked by At

I am having a fortran code to generate a grid in binary VTK format. This code produces a binary VTK file like this one:

# vtk DataFile Version 3.0
vtk output
BINARY
DATASET RECTILINEAR_GRID
DIMENSIONS        2       2       1
X_COORDINATES        2  float
^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
Y_COORDINATES        2  float
^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
Z_COORDINATES        1  float
^@^@^@^@^@^@^@^@

When, I try to open it with ParaView it crashes with the following error message:

ERROR: In /home/user/OpenFOAM/ThirdParty-2.3.0/ParaView-4.1.0/VTK/IO/Legacy/vtkRectilinearGridReader.cxx, line 311 vtkRectilinearGridReader (0x379f4b0): Unrecognized keyword: ...

If I write the above file in ASCII instead, then it opens properly in ParaView, that's the ASCII analogous file that opens:

# vtk DataFile Version 3.0
vtk output
ASCII
DATASET RECTILINEAR_GRID
DIMENSIONS 2 2 1
X_COORDINATES 2 float
0 1
Y_COORDINATES 2 float
0 1
Z_COORDINATES 1 float
0

I am using the following code to generate the grid in binary VTK format (I am giving a minimum working code):

program VTKBinary

implicit none

real*8    :: x(2) = (0., 1.)
real*8    :: y(2) = (0., 1.)
real*8    :: z(1) = (0.)

character :: buffer*80, lf*1, str1*8, str2*8, str3*8
integer   :: ivtk = 9, int

lf = char(10) ! line feed character

open(unit=ivtk,file='test_bin.vtk',form='binary',convert='BIG_ENDIAN')

buffer = '# vtk DataFile Version 3.0'//lf  ; write(ivtk) trim(buffer)
buffer = 'vtk output'//lf                  ; write(ivtk) trim(buffer)
buffer = 'BINARY'//lf                      ; write(ivtk) trim(buffer)
buffer = 'DATASET RECTILINEAR_GRID'//lf    ; write(ivtk) trim(buffer)

! WRITE GRID
write(str1(1:8),'(i8)') size(x)
write(str2(1:8),'(i8)') size(y)
write(str3(1:8),'(i8)') size(z)
buffer = 'DIMENSIONS '//str1//str2//str3//lf  ; write(ivtk) trim(buffer)
buffer = 'X_COORDINATES '//str1//'  float'//lf ; write(ivtk) trim(buffer)
write(ivtk) x
buffer = lf//'Y_COORDINATES '//str2//'  float'//lf  ; write(ivtk) trim(buffer)
write(ivtk) y
buffer = lf//'Z_COORDINATES '//str3//'  float'//lf  ; write(ivtk) trim(buffer)
write(ivtk) z

close(ivtk)

end program VTKBinary

What is wrong with the binary VTK file? why it exits?

1

There are 1 answers

3
Vladimir F Героям слава On BEST ANSWER

One issue is, that arrays are specified as [0., 1.], not as (0., 1.), that would be a complex number equal to one imaginary unit i. The same way [0.] instead of (0.). Thanks to Alexander Voigt in binary vtk for Rectilinear_grid from fortran code can not worked by paraview, for pointing out the issue.

You state that you use FLOAT, but than you store real*8 there. They are not compatible. Either store real*4 (or a more modern real(real32) ) there, or place the text DOUBLE instead of FLOAT in the file.

Note, the stand conforming access=stream is much better than the non-standard form=binary. Also, convert=BIG_ENDIAN is non-standard and will not work with many compilers (e.g., gfortran, if I recall it correctly). The char(10) is better to be achar(10) but that is a minor issue.

BTW,

buffer = 'DIMENSIONS '//str1//str2//str3//lf  ; write(ivtk) trim(buffer)

can just be

write(ivtk) 'DIMENSIONS '//str1//str2//str3//lf