Solving FORTRAN Code Error: Is Anything Wrong?

  • Context: Comp Sci 
  • Thread starter Thread starter RJLiberator
  • Start date Start date
  • Tags Tags
    Code Fortran
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a FORTRAN code that encounters a runtime error when attempting to read data from a file. Participants explore potential issues related to the code structure, data formatting, and array declarations.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • Participants question whether the arrays MASSNUMBER2 and ABUNDANCE2 are properly declared with at least 146 members and of the correct types (INTEGER and REAL, respectively).
  • There is a suggestion to ensure that the data file "updatedata.txt" contains at least 146 records and that the format of these records matches the expected input for the READ statement.
  • One participant proposes adding the ERR=label option to the READ statement to handle potential read errors more gracefully.
  • Concerns are raised about the formatting of the "updatedata.txt" file, particularly regarding how mass numbers and abundance numbers are organized, indicating a possible mismatch with the expected input format.
  • A later reply suggests that adjusting the FORMAT and READ statements in the program might be necessary to accommodate the actual data file format.
  • Ultimately, a change to the READ statement is reported to have resolved the issue, allowing the code to run successfully.

Areas of Agreement / Disagreement

Participants express differing views on the source of the error, with some focusing on code structure and others on data formatting. The discussion reflects a lack of consensus on the best approach to resolve the issue, although a solution was found by modifying the READ statement.

Contextual Notes

Limitations include potential assumptions about the data file structure and the specific format required by FORTRAN, which may not have been fully clarified in the discussion.

RJLiberator
Gold Member
Messages
1,094
Reaction score
63

Homework Statement


Working on a FORTRAN based code for some work.
Not sure what's wrong with it.

Homework Equations

The Attempt at a Solution


The section of the code that is causing the problem is:

Code:
  OPEN( UNIT = 17,FILE='updateddata.txt',STATUS='UNKNOWN')
  DO I = 1, 146
  READ( 17, 703) MASSNUMBER2(I), ABUNDANCE2(I)
703  FORMAT( I7, ES15.8)
  WRITE (*,*) MASSNUMBER2(I), ABUNDANCE2(I)
  END DO
  CLOSE ( UNIT = 17 )

My problem is I get a run time error when running the entire code:

At line 31 of Chisquared.f (unit = 17, file='updateddata.txt')
Fortran runtime error: Bad values during floating point readMy question is: Is anything wrong in the fortran code here? Anything noticeable that would cause an error?
 
Physics news on Phys.org
RJLiberator said:

Homework Statement


Working on a FORTRAN based code for some work.
Not sure what's wrong with it.

Homework Equations

The Attempt at a Solution


The section of the code that is causing the problem is:

Code:
  OPEN( UNIT = 17,FILE='updateddata.txt',STATUS='UNKNOWN')
  DO I = 1, 146
  READ( 17, 703) MASSNUMBER2(I), ABUNDANCE2(I)
703  FORMAT( I7, ES15.8)
  WRITE (*,*) MASSNUMBER2(I), ABUNDANCE2(I)
  END DO
  CLOSE ( UNIT = 17 )

My problem is I get a run time error when running the entire code:

At line 31 of Chisquared.f (unit = 17, file='updateddata.txt')
Fortran runtime error: Bad values during floating point readMy question is: Is anything wrong in the fortran code here? Anything noticeable that would cause an error?
Make sure that the arrays MASSNUMBER and ABUNDANCE2 are each declared to have at least 146 members. Make sure MASSNUMBER is type INTEGER and ABUNDANCE2 is type REAL.

Make sure that the data file "updatedata.txt" contains at least 146 records (lines).

If you have fewer than 146 members in each array, then you might want to add the option ERR=label to the READ statement, where label is the statement number for the program to goto if a read error is encountered.

Check the format of the data file "updatedata.txt" to make sure the first 7 columns of each record contain one INTEGER type data and the next 15 columns contain only one floating point number in scientific notation.

It might be a good idea to add the FORM = 'FORMATTED' option to the OPEN statement, to make sure the program knows what type of data to expect. You can also add the option ACCESS = 'SEQUENTIAL' for a regular text file of ASCII data.

The code tags must be in ALL CAPS in order to be recognized properly.
 
  • Like
Likes   Reactions: RJLiberator
Thanks for the pretty awesome help.

So far I've tried some of the suggestions, but no cigar.

I think the problem might lie in the formatting of the updateddata.txt file.

This file has the first 2 columns as mass number until it reaches the triple digit values. Then it has 3 columns dedicated to mass number. IT then tabs over to the abundance numbers in scientific form.
I might have to reformat this to make it work in fortran, is my guess.
 
RJLiberator said:
Thanks for the pretty awesome help.

So far I've tried some of the suggestions, but no cigar.

I think the problem might lie in the formatting of the updateddata.txt file.

This file has the first 2 columns as mass number until it reaches the triple digit values. Then it has 3 columns dedicated to mass number. IT then tabs over to the abundance numbers in scientific form.
I might have to reformat this to make it work in fortran, is my guess.
It might be easier to adjust the FORMAT and READ statements in your program to adapt to the format of the data file. You can judge which is the least amount of work.
 
  • Like
Likes   Reactions: RJLiberator
So, in the end, we decided to change read to: READ( 17, *) MASSNUMBER2(I), ABUNDANCE2(I)

Amongst other small problems, this seemed to solve the problem and the code is running.

FORTRAN is rather touchy.
 
RJLiberator said:
So, in the end, we decided to change read to: READ( 17, *) MASSNUMBER2(I), ABUNDANCE2(I)

Amongst other small problems, this seemed to solve the problem and the code is running.

FORTRAN is rather touchy.
I think that you'll find that most programming languages are rather touchy. They do what you tell them to do, not necessarily what you want them to do.
 
  • Like
Likes   Reactions: RJLiberator and SteamKing

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K