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. 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:
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. Is there a way to tell fortran I want to read real binary data into a complex variable array?
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
write(2) CFL%re
write(2) W%re
You still need the #ifdef
, but you don't need another array. 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. Or storage association of procedure arguments to do the same.