简体   繁体   English

使用二进制读写将真实数据读入 Fortran 中的复数变量

[英]Reading Real data into Complex variables in Fortran using binary read and write

I have a code where I have to do a great deal of file I/O and as such would like a pretty efficient way of dealing with the following problem.我有一个代码,我必须在其中执行大量文件 I/O,因此想要一种非常有效的方法来处理以下问题。 For the sake of convenience, my code always write to restart files only the real part, coded as follows:为了方便起见,我的代码总是只写重启文件的真实部分,编码如下:

OPEN(UNIT = 2, FILE = filename, STATUS = 'UNKNOWN', form = 'unformatted')
    write(2) CFL
    write(2) W
CLOSE(2)

When I run my code in the real analysis mode, reading these files is trivial:当我在实分析模式下运行我的代码时,读取这些文件是微不足道的:

OPEN(UNIT = 2, FILE = filename, STATUS = 'OLD', form = 'unformatted')
    read(2) CFL
    read(2) W
CLOSE(2)

But when I run in complex I have to do this somewhat clumsy hack:但是当我在复杂的环境中运行时,我必须做这个有点笨拙的 hack:

open(UNIT = funit, FILE = filename, STATUS = 'old', form='unformatted')
#ifdef COMPLEX_ON
        read(funit) CFL
        read(funit) W_t
        CFL_in = CFL
        W = W_t
#else                                                                                                                                                                   
        read(funit) CFL_in
        read(funit) W
#endif
close(funit)

This means that now I need two copies of the data, and if I use implied do-loops like I did previously, I can do this hack one entry at a time and the copy to the W array, but this is a slower way of handling the I/O.这意味着现在我需要数据的两个副本,如果我像以前那样使用隐含的 do 循环,我可以一次破解一个条目并复制到 W 数组,但这是一种较慢的方法处理 I/O。 Is there a way to tell fortran I want to read real binary data into a complex variable array?有没有办法告诉 fortran 我想将真正的二进制数据读入一个复杂的变量数组?

Thanks.谢谢。

If your compiler is very up to date, or even claims to be Fortran 2008 conforming, you could use one of the least supported F2008 features and read just the real part如果您的编译器是最新的,或者甚至声称符合 Fortran 2008,您可以使用受支持最少的 F2008 功能之一并阅读真正的部分

write(2) CFL%re
write(2) W%re

You still need the #ifdef , but you don't need another array.您仍然需要#ifdef ,但不需要另一个数组。 However, the compile may well be in need to create a temporary array anyway.但是,无论如何编译很可能需要创建一个临时数组。

Else you could use the plain old equivalence to view the complex arrayas a real array with an extra dimension.否则,您可以使用普通的旧equivalence将复杂数组视为具有额外维度的真实数组。 Or storage association of procedure arguments to do the same.或者存储过程参数的关联来做同样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM