Solving FORTRAN Code Error: Is Anything Wrong?

  • Comp Sci
  • Thread starter RJLiberator
  • Start date
  • Tags
    Code Fortran
In summary: It's important to double check all your code and make sure that it's formatted correctly and that all the variables are declared and used correctly.In summary, the conversation discusses troubleshooting a FORTRAN code that is producing a runtime error. The user is working on a code for some work and is unsure of what is causing the error. They identify the section of the code that is causing the problem and ask for suggestions on what might be causing the error. Suggestions include checking the arrays and data file for proper formatting and declaring the variables correctly. In the end, it is determined that adjusting the READ statement to adapt to the format of the data file solves the problem. The conversation also highlights the importance of being meticulous and double checking all code when working with programming
  • #1
RJLiberator
Gold Member
1,095
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
  • #2
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 RJLiberator
  • #3
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.
 
  • #4
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 RJLiberator
  • #5
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.
 
  • #6
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 RJLiberator and SteamKing

FAQ: Solving FORTRAN Code Error: Is Anything Wrong?

1. What is FORTRAN code error?

FORTRAN (FORmula TRANslator) is a programming language commonly used in scientific and engineering applications. A FORTRAN code error occurs when the code does not run as expected due to logical or syntactical mistakes.

2. How can I identify a FORTRAN code error?

The most common way to identify a FORTRAN code error is through compiling and running the code. The compiler will provide error messages indicating the line and type of error. Additionally, debugging tools such as print statements or step-through debugging can be used to pinpoint the error.

3. What are some common mistakes that lead to FORTRAN code errors?

Some common mistakes that can lead to FORTRAN code errors include using incorrect syntax, misspelling variable names, forgetting to declare variables, and using incorrect data types. It is important to carefully review the code and use proper coding conventions to prevent these errors.

4. How can I fix a FORTRAN code error?

To fix a FORTRAN code error, it is important to first identify the specific error and its location in the code. Once identified, the code can be carefully reviewed and corrected. It may also be helpful to consult documentation or seek assistance from other programmers.

5. How can I prevent FORTRAN code errors in the future?

To prevent FORTRAN code errors in the future, it is important to follow coding conventions and best practices, use proper syntax, carefully review the code before running it, and utilize debugging tools. It may also be helpful to regularly test and troubleshoot the code to catch any errors early on.

Similar threads

Replies
7
Views
2K
Replies
8
Views
3K
Replies
3
Views
4K
Replies
13
Views
3K
Replies
1
Views
2K
Replies
2
Views
2K
Replies
5
Views
2K
Back
Top