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

  • Context: Fortran 
  • Thread starter Thread starter nikhillaghave
  • Start date Start date
  • Tags Tags
    Fortran
Click For Summary
SUMMARY

The discussion centers on implementing checkpointing in Fortran applications using both direct and sequential access file methods. The user, Nikhil, successfully utilized direct access with unformatted files to maintain precision when writing and reading an array of real*8 numbers. However, when switching to sequential access with formatted output, precision was lost, leading to incorrect results upon data retrieval. The key takeaway is that binary files retain full precision, while formatted files may round results, impacting data integrity.

PREREQUISITES
  • Understanding of Fortran I/O operations
  • Familiarity with direct and sequential file access methods
  • Knowledge of data types in Fortran, specifically real*8
  • Concept of formatted versus unformatted file writing
NEXT STEPS
  • Research Fortran binary file handling for precision retention
  • Explore the differences between formatted and unformatted I/O in Fortran
  • Learn about the implications of record length in direct access files
  • Investigate methods to convert formatted output to maintain precision
USEFUL FOR

This discussion is beneficial for Fortran developers, particularly those working on applications requiring precise data storage and retrieval, as well as anyone interested in file I/O operations in programming.

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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
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