首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Fortran中使用原始二进制数据编写.vtu文件(截图)

在Fortran中使用原始二进制数据编写.vtu文件(截图)
EN

Stack Overflow用户
提问于 2020-05-03 11:38:33
回答 1查看 936关注 0票数 2

我正在尝试编写一个Fortran子程序,它输出一个原始二进制数据的.vtu文件。我成功地编写了一个.vtk二进制文件和.vtu ascii文件,但是在这种文件中,我不知道问题在哪里。下面是我的子程序、生成的.vtu文件和截视图的错误消息。

代码语言:javascript
复制
MODULE OUTPUT
IMPLICIT NONE
CONTAINS
function itoa(i) result(res)
  character(:),ALLOCATABLE :: res
  INTEGER,intent(in) :: i
  character(range(i)+2) :: tmp
  write(tmp,'(i0)') i
  res = trim(tmp)
end function
SUBROUTINE print_vtu_binary_appended(step,number_of_particles,system_name,position,velocity,radius)
use iso_fortran_env
implicit none
real(kind=real64), intent(in), dimension(number_of_particles,3) :: position, velocity
real(kind=real64), intent(in), dimension(number_of_particles) :: radius
integer(kind=int32), intent(in) :: step, number_of_particles
integer(kind=int32), dimension(6) :: offset
integer(kind=int32) :: vtu, print_number=0
character(len=*), intent(in) :: system_name

offset(1) = 0
offset(2) = offset(1) + 4 + SIZEOF(position)
offset(3) = offset(2) + 4 + SIZEOF(velocity)
offset(4) = offset(3) + 4 + SIZEOF(radius)
offset(5) = offset(4) + 4
offset(6) = offset(5) + 4
OPEN(newunit=vtu, action='write', access='stream', STATUS='new', form='unformatted', FILE='./'//TRIM(system_name)//itoa(print_number)//'.vtu')
WRITE(vtu)'<?xml version="1.0"?>'//NEW_LINE('A')
WRITE(vtu)'<VTKFile type="UnstructuredGrid" version="0.1" byte_order="LittleEndian">'//NEW_LINE('A')
WRITE(vtu)'<UnstructuredGrid>'//NEW_LINE('A')
WRITE(vtu)'<Piece NumberOfPoints="'//itoa(number_of_particles)//'" NumberOfCells="0">'//NEW_LINE('A')
WRITE(vtu)'<Points>'//NEW_LINE('A')
WRITE(vtu)'<DataArray name="Position" type="Float64" NumberOfComponents="3" format="appended" offset="'//itoa(offset(1))//'">'//NEW_LINE('A')
WRITE(vtu)'</DataArray>'//NEW_LINE('A')
WRITE(vtu)'</Points>'//NEW_LINE('A')
WRITE(vtu)'<PointData>'//NEW_LINE('A')
WRITE(vtu)'<DataArray type="Float64" Name="Velocity" NumberOfComponents="3" format="appended" offset="'//itoa(offset(2))//'">'//NEW_LINE('A')
WRITE(vtu)'</DataArray>'//NEW_LINE('A')
WRITE(vtu)'<DataArray type="Float64" Name="Radius" format="appended" offset="'//itoa(offset(3))//'" >'//NEW_LINE('A')
WRITE(vtu)'</DataArray>'//NEW_LINE('A')
WRITE(vtu)'</PointData>'//NEW_LINE('A')
WRITE(vtu)'<Cells>'//NEW_LINE('A')
WRITE(vtu)'<DataArray type="Int32" Name="connectivity" format="appended" offset="'//itoa(offset(4))//'">'//NEW_LINE('A')
WRITE(vtu)'</DataArray>'//NEW_LINE('A')
WRITE(vtu)'<DataArray type="Int32" Name="offsets" format="appended" offset="'//itoa(offset(5))//'">'//NEW_LINE('A')
WRITE(vtu)'</DataArray>'//NEW_LINE('A')
WRITE(vtu)'<DataArray type="UInt8" Name="types" format="appended" offset="'//itoa(offset(6))//'">'//NEW_LINE('A')
WRITE(vtu)'</DataArray>'//NEW_LINE('A')
WRITE(vtu)'</Cells>'//NEW_LINE('A')
WRITE(vtu)'</Piece>'//NEW_LINE('A')
WRITE(vtu)'</UnstructuredGrid>'//NEW_LINE('A')
WRITE(vtu)'<AppendedData encoding="raw">'//NEW_LINE('A')
WRITE(vtu)char(95),offset(1),position
WRITE(vtu)offset(2),velocity
WRITE(vtu)offset(3),radius
WRITE(vtu)offset(4),offset(5),offset(6)
WRITE(vtu)NEW_LINE('A')//'</AppendedData>'//NEW_LINE('A')
WRITE(vtu)'</VTKFile>'
CLOSE(unit=vtu)
print_number = print_number + 1
END SUBROUTINE print_vtu_binary_appended
END MODULE OUTPUT
program test
use iso_fortran_env
use output
implicit none
integer(kind=int32) :: step=1, number_of_particles=2
real(kind=real64), dimension(2,3) :: position, velocity
real(kind=real64), dimension(2) :: radius
character(len=14) :: system_name='random_numbers'
position = reshape((/ 1.0754_real64, 2.0683_real64, 3.2479_real64, 4.08642_real64, 5.46906_real64, 6.36974_real64 /), shape(position))
velocity = reshape((/ 3.0754_real64, 7.0683_real64, 10.2479_real64, 72.08642_real64, 83.46906_real64, 22.36974_real64 /), shape(velocity))
radius = reshape((/ 0.0000754_real64, 0.00083_real64 /), shape(radius))
CALL print_vtu_binary_appended(step,number_of_particles,system_name,position,velocity,radius)
end program test

生成的xml文件示例:

代码语言:javascript
复制
<?xml version="1.0"?>
<VTKFile type="UnstructuredGrid" version="0.1" byte_order="LittleEndian">
<UnstructuredGrid>
<Piece NumberOfPoints="8" NumberOfCells="0">
<Points>
<DataArray name="Position" type="Float64" NumberOfComponents="3" format="appended" offset="0">
</DataArray>
</Points>
<PointData>
<DataArray type="Float64" Name="Velocity" NumberOfComponents="3" format="appended" offset="196">
</DataArray>
<DataArray type="Float64" Name="Radius" format="appended" offset="392" >
</DataArray>
</PointData>
<Cells>
<DataArray type="Int32" Name="connectivity" format="appended" offset="460">
</DataArray>
<DataArray type="Int32" Name="offsets" format="appended" offset="464">
</DataArray>
<DataArray type="UInt8" Name="types" format="appended" offset="468">
</DataArray>
</Cells>
</Piece>
</UnstructuredGrid>
<AppendedData encoding="raw">
...
</AppendedData>
</VTKFile>

Paraview中的错误:

C:\bbd\ecd3383f\build\superbuild\paraview\src\VTK\IO\XML\vtkXMLUnstructuredDataReader.cxx,错误:在

第466行中,vtkXMLUnstructuredGridReader (000001DDC1DD5090):无法从片0中的点读取点数组。元素中的数据数组可能太短.

EN

回答 1

Stack Overflow用户

发布于 2020-05-04 15:36:18

我猜想这个问题可能是由结束记录引起的,我建议使用"char(10)“而不是内部的"new_line”。

在我的库(https://github.com/szaghi/VTKFortran)中,我使用

character(1), parameter :: end_rec = char(10) !< End-character for binary-record finalize.

编辑

很抱歉猜错了,我认为"char(10)“更安全,因为使用"new_line”,您可以选择哪种类型,因此我假设不同数量的字节终止,这是我的错。

读取您的代码,您使用“偏移量”来跟踪附加的数据偏移量和附加部分中每个数据数组的字节数。我认为这是不安全的:如果您将偏移量定义从int64更改为int32类型,则您的测试工作在我当前的体系结构中(GNUFortran9.3.0,Linux4.19.0-6-AMD 64,Parview5.8.0)。

还请注意,您的itao函数对于传递的参数"i“没有一个善意的定义,但是您同时传递了int64和int32。

我建议将偏移量和字节号的定义分开,但对于小文件,定义偏移量,因为int32似乎对我适用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61573813

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档