来自Fortran代码的RECTILINEAR_GRID的二进制VTK [英] Binary VTK for RECTILINEAR_GRID from fortran code

查看:290
本文介绍了来自Fortran代码的RECTILINEAR_GRID的二进制VTK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Fortran代码来生成二进制VTK格式的网格。这段代码产生一个像这样的二进制VTK文件:

 #vtk DataFile版本3.0 
vtk输出
BINARY
DATASET RECTILINEAR_GRID
DIMENSIONS 2 2 1
X_COORDINATES 2 float
^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ @ ^ @ ^ @ ^ @ ^ @
Y_COORDINATES 2 float
^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ ^ @
Z_COORDINATES 1 float
^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @

当我尝试使用ParaView打开它时,它会崩溃,并显示以下错误消息:
$ b


错误:In
/home/athanasios/OpenFOAM/ThirdParty-2.3.0/ParaView-4.1.0/VTK/IO/Legacy/vtkRectilinearGridReader.cxx,
第311行vtkRectilinearGridReader(0x379f4b0):无法识别的关键字:
...

如果我用ASCII代替上面的文件,那么它会在ParaView中正确打开,这就是打开的ASCII类似文件:

 #vtk DataFile版本3.0 
vtk输出t
ASCII
DATASET RECTILINEAR_GRID
尺寸2 2 1
X_COORDINATES 2浮动
0 1
Y_COORDINATES 2浮动
0 1
Z_COORDINATES 1 float
0

我使用以下代码在二进制VTK中生成网格格式(我给出了最低工作代码):

 程序VTKBinary 

隐式无

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

字符::缓冲区* 80,lf * 1,str1 * 8,str2 * 8,str3 * 8
整数:: ivtk = 9,int

lf = char(10)!换行字符

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

buffer ='#vtk DataFile Version 3.0'// lf;写(ivtk)trim(缓冲区)
buffer ='vtk output'// lf;写(ivtk)trim(缓冲区)
buffer ='BINARY'// lf;写(ivtk)trim(缓冲区)
buffer ='DATASET RECTILINEAR_GRID'// lf;写(ivtk)修剪(缓冲区)

! 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;写(ivtk)trim(缓冲区)
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)

结束程序VTKBinary

二进制VTK文件有什么问题?为什么退出?

解决方案

一个问题是,数组被指定为 [0。,1。] ,而不是(0。,1。),这将是一个等于一个虚数单位的复数。相同的方式 [0。] 而不是(0。)。感谢Alexander Voigt在二进制你可以使用 FLOAT ,但比您存储 real * 8 那里。它们不兼容。在那里存储 real * 4 (或更现代的 real(real32)),或者将文本 DOUBLE 而不是 FLOAT 在文件中。



符合 access = stream 要比非标准 form = binary 好得多。另外, convert = BIG_ENDIAN 是非标准的,并且不适用于许多编译器(例如,如果我正确记得它,gfortran)。 char(10)最好是 achar(10),但这是一个小问题。



顺便说一句,

  buffer ='DIMENSIONS'// str1 // str2 // str3 // lf;写(ivtk)trim(缓冲区)

可以是

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


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/athanasios/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?

解决方案

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

这篇关于来自Fortran代码的RECTILINEAR_GRID的二进制VTK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆