Fortran Fortran: help with I/O in Direct Access and Seq. access

  • Thread starter Thread starter nikhillaghave
  • Start date Start date
  • Tags Tags
    Fortran
Click For Summary
The discussion centers on implementing checkpointing in an application to save and restore history data, specifically an array of real*8 numbers where precision is crucial. Initially, the user successfully utilized direct access unformatted files, maintaining precision during read and write operations. However, upon switching to sequential formatted files for human readability, precision issues arose, leading to incorrect results upon data restoration. The user seeks clarification on the differences in data storage between direct and sequential access methods, particularly regarding precision loss in formatted output. A key point raised is that binary files preserve full precision, while formatted output may round values, impacting data integrity. The user is looking for guidance on achieving consistent precision in sequential I/O similar to that of direct I/O.
nikhillaghave
Messages
2
Reaction score
0
Hello,

I am implementing simple check-pointing in an application so that the history is saved to a file and upon restart the data is read from this file.
-> The history file contains an array of real*8 numbers and precision is very important in the program.

I first implemented this using direct access unformatted file and it worked fine. Basically the default free-format specifiers used by fortran had the same precision while writing and reading the data.
Code:
C Write
         open(unit,file='hist.dat',
     +        access='DIRECT',RECL=recordlength)
         write(unit,REC=NF),(X(I),I=1,N),F,NF
         close(unit)

C READ
         open(unit,file='hist.dat',status='old',
     +        ACCESS='DIRECT',RECL=recordlength)

         read(unit,REC=NF),
     +        (X(i),i=1,N),F,NF

         close(unit)

I wanted the history file to be human readable/editable, so I made the file sequential and used the free-format write(unit,*) and read(unit,*) for writing and reading the data. Since the defaults worked in direct access file, i assumed it should work in sequential too, but the precision became incorrect and as a result the history file does not work the way it should and gives incorrect results on restarts. The data looks the same to the naked eye, but some where the precision is lost.

Code:
C write
     open(unit,file='hist.dat',
     +        access='APPEND')
         write(unit,*) (X(I),I=1,N),F,NF
         close(unit)

C read
     open(unit,file='hist.dat')
            read(unit,*)
     +           (X(i),i=1,N),F,NF

           close(unit)

Can some one explain why this is happening? is it because direct access files store data according to the record length?

How can make the seq. I/O format specifiers exactly the same as the direct I/O ?

Can someone point me in the right direction as I am clueless at this point. Thank You.

Nikhil
 
Technology news on Phys.org
I don't think the problem is direct or sequential, it is formatted versus binary.

A binary file retains full precision. It is not human readable.

Formatted output is human readable. It may round the results.
 
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K