Fixing Fortran Runtime Error for Beginner

In summary: If an error occurs, control branches to line 8. If the end of the file is reached, line 9 is executed.Beyond what @Mark44 has pointed out: You read in the number of points at line 12 then you should use its value to control your do loop at line 17 and line 22 ie replace 1200 with N.Of course, you should check that N is in the range of [1,1200] inclusively.Using the variable "name" may not be the best choice as your syntax coloring implies its a keyword in your FORTRAN. Perhaps name1 would be a better choice.Also, in the final loop, unit 15 is never opened.
  • #1
USTCkong
4
0
Hi, I am a beginner of Fortran. Recently I try to use a fortran code for my simulation. But it always shows me the following error when execute the program: At line 18 of the file sous.f (unit =11, file = 'fluid'), fortran runtime error, End of file, Error termination. Backtrace: could not print backtrace.
The program is the following:
1579194807246.png

the file fluid.int is the following:
1.0200167 1403.0953
1.0400334 2930.1503
1.06005 3005.3186
1.0800667 3079.2313
1.1000834 3159.2497
1.1201001 3232.848
1.1401168 3307.9705
1.1601334 3381.9916
1.1801501 3445.305
1.2001668 3501.6538
1.2201835 3563.9407
1.2402002 3629.1324
1.2602168 3676.5754
1.2802335 3716.7428
1.3002502 3769.7548
1.3202669 3827.6405
1.3402836 3879.3974
1.3603003 3921.094
1.3803169 3966.1734
1.4003336 4016.8839
1.4203503 4060.472
1.440367 4100.4401
1.4603837 4143.1311
1.4804003 4185.0333
1.500417 4228.3119
1.5204337 4273.3999
1.5404504 4315.4772
1.5604671 4351.4981
1.5804837 4386.2771
1.6005004 4426.1663
1.6205171 4466.9572
1.6405338 4503.2937
1.6605505 4535.8692
1.6805671 4568.8949
1.7005838 4607.0189

I really don't know the reason. Very appreciate to help me!
 
Technology news on Phys.org
  • #2
The biggest problem I see is that you have a loop that runs 1200 times, but there aren't anywhere near that number of pairs of data items in the input file.

See https://docs.oracle.com/cd/E19957-01/805-4939/6j4m0vnat/index.html for a description of the READ statements.

Here's an example from the doc page above of how you might use READ.
Fortran:
READ( 1, 2, ERR=8, END=9, IOSTAT=N ) X, Y 
       ... 
8     WRITE( *, * ) 'I/O error # ', N, ', on 1' 
       STOP 
9     WRITE( *, * ) 'EoF on 1' 
       RETURN 
       END
In the READ statement, 1 is the unit number, 2 is (I believe) a format line. If an error occurs, control branches to line 8. If the end of the file is reached, line 9 is executed.
 
  • Like
Likes DEvens
  • #3
Beyond what @Mark44 has pointed out:

You read in the number of points at line 12 then you should use its value to control your do loop at line 17 and line 22 ie replace 1200 with N.

Of course, you should check that N is in the range of [1,1200] inclusively.

Using the variable "name" may not be the best choice as your syntax coloring implies its a keyword in your FORTRAN. Perhaps name1 would be a better choice.

Another problem, I see is when you read in your data from fileno 11 you read it into an x array and a y1 array. (line 18) The next statement reads into the same x array again and the y2 array. (line 19)

While you can do that how do you know the x arrays that are read are in fact the same arrays with the same values?
 
  • #4
jedishrfu said:
You read in the number of points at line 12 then you should use its value to control your do loop at line 17 and line 22 ie replace 1200 with N.
Good point. I overlooked that part of the code.
jedishrfu said:
Using the variable "name" may not be the best choice as your syntax coloring implies its a keyword in your FORTRAN. Perhaps name1 would be a better choice.
Not a keyword, so I don't know why the GUI treats name and name1 differently. Neither of these is a good choice for a variable name. It would be better to give these variables names that indicate what they're being used for.
jedishrfu said:
Another problem, I see is when you read in your data from fileno 11 you read it into an x array and a y1 array. (line 18) The next statement reads into the same x array again and the y2 array. (line 19)
I'm guessing this is an oversight. The names of the arrays are very inconsistent, and include x1, x, y1, y2, y, and y3. Of these, neither x1 nor y3 is used in the code shown.

I don't understand what the code is supposed to be doing in the loop that reads from units 11 and 12. Whatever value gets read into x(i) in the first read statement will be overwritten by the second read statement.

Also, in the final loop, unit 15 is never opened. Other than the default units for Fortran 77 input (unit 5) and output (unit 6), you need to open a unit before writing to it.
 
  • Like
Likes jedishrfu
  • #5
Mark44 said:
The biggest problem I see is that you have a loop that runs 1200 times, but there aren't anywhere near that number of pairs of data items in the input file.

See https://docs.oracle.com/cd/E19957-01/805-4939/6j4m0vnat/index.html for a description of the READ statements.

Here's an example from the doc page above of how you might use READ.
Fortran:
READ( 1, 2, ERR=8, END=9, IOSTAT=N ) X, Y
       ...
8     WRITE( *, * ) 'I/O error # ', N, ', on 1'
       STOP
9     WRITE( *, * ) 'EoF on 1'
       RETURN
       END
In the READ statement, 1 is the unit number, 2 is (I believe) a format line. If an error occurs, control branches to line 8. If the end of the file is reached, line 9 is executed.

Hi,

Thanks for your answers!
Here are files of fluid and bk. They are both not less than 1200.
Sorry I can't give the original txt file. It's said the file does not have an allowed extension. So I just copy it into excel. And what I do in terminal is the following:
1579214297651.png

Should I build the file of fort.11 and fort.12 before running the program?
 

Attachments

  • file.xlsx
    47.6 KB · Views: 263
  • #6
jedishrfu said:
Beyond what @Mark44 has pointed out:

You read in the number of points at line 12 then you should use its value to control your do loop at line 17 and line 22 ie replace 1200 with N.

Of course, you should check that N is in the range of [1,1200] inclusively.

Using the variable "name" may not be the best choice as your syntax coloring implies its a keyword in your FORTRAN. Perhaps name1 would be a better choice.

Another problem, I see is when you read in your data from fileno 11 you read it into an x array and a y1 array. (line 18) The next statement reads into the same x array again and the y2 array. (line 19)

While you can do that how do you know the x arrays that are read are in fact the same arrays with the same values?
Hi,

Many thanks for your answer. The N is 1200. And for the two files, the x is the same.
 
  • #7
We used to use a sentinel value as the last line of the data. Something like 999999 999999 that when read indicated there was no more data to read.

Basically it eliminates the N-value question? Let the computer do the work and you tell it what to do.

Sentinels were most useful for dealing with card decks but then I'd be giving away my age and someone will say "OK, Boomer!"
 
  • #8
USTCkong said:
Hi,

Many thanks for your answer. The N is 1200. And for the two files, the x is the same.

Its good to know you're aware of that. I would caution that you should tighten your code up and use the N value, checking it's between 1 and 1200 having it decide how much data to read and process.

It would also be a good check to verify the two x arrays or you could allow the x values to vary from file to file and use interpolations to line them up for design flexibility but I digress.
 
  • #9
jedishrfu said:
Its good to know you're aware of that. I would caution that you should tighten your code up and use the N value, checking it's between 1 and 1200 having it decide how much data to read and process.

It would also be a good check to verify the two x arrays or you could allow the x values to vary from file to file and use interpolations to line them up for design flexibility but I digress.
haha Thanks for your detailed reminder. It's tricky I didn't find obvious fault in the code, but it just couldn't give the right result.
 
  • #10
USTCkong said:
Should I build the file of fort.11 and fort.12 before running the program?
What are fort.11 and fort.12? Your source code is sous.f, which gfortran will compile and link to produce the executable, sous.exe.
USTCkong said:
The N is 1200. And for the two files, the x is the same.
If your program can't find the input files, it could be due to the files not being where the program expects them, or the files have a file extension which your program isn't using. Either of these could be why your program isn't working.

Where are your two input files? I believe they should be in the same directory as the executable, sous.exe.

Is your program using the correct names for the input files? Possibly they are fluid.txt and bk.txt, or using some other file extension such as .dat.

Also, rather than have two separate input files with the same x values, why don't you put the y values of the bk file into the fluid file?

Each line of the resulting file would look like this:
<x value> <y value from liquid> <y value from bk>
If you did this, it would simplify your code quite a lot.
 
Last edited:
  • #11
fort.11 and fort.12 are defined in lines 14 and 15, that is the old FORTRAN way of naming files. You opened a unit =number and that file was written as fort.number.
 
  • Like
Likes jedishrfu
  • #12
What time frame was that?

in my experience, while running batch fortran you would reference the number only and in the job control language a card would indicate what device actually had the data to read Ie card reader or from within the deck itself or a tape device or a disk pack.

In timesharing Fortran, I think the file name was the name you give and it didnt need a number suffix.

my experience was limited to Honeywell 6000 Fortran-y.
 
  • #13
Another problem in reading in the file names is that the dimensions of variables name and name2 are large. I believe that it will try to open a file with random memory trash filling in the unused part of the file names.

A quick temporary test is to hard-code file names into the open statements and see if the program gets farther.
 
  • #14
@jedishrfu I recently worked (within the past couple of months) with an old fortran writer, he hadn't upgraded any of his programming skills in 35+ years, no unstructured *.f90 files, plain *.f, and he didn't care. No one really looked at his development codes, he did research and wrote papers, but rarely if ever passed along his codes to anyone to use.
 
  • Like
Likes jedishrfu
  • #15
Stupid question: what happens when you comment out line 18?
 
  • #17
The error message refers to the file named "fluid", but you say that the true file name is "fluid.int". That would be a problem. You should add the option to your open statements that will only open existing files for reading, so it will not create a new file with the wrong name. You should also include the options that and will jump to some error handling code if the opens or writes cause an error.
 
  • #18
1. I strongly suggest you learn Fortran before debugging someone else's code.
2. A good way to learn file I/O is to start writing programs that only do File I/O.
 
Last edited:
  • Like
Likes jedishrfu

FAQ: Fixing Fortran Runtime Error for Beginner

What is a Fortran runtime error?

A Fortran runtime error is an error that occurs during the execution of a Fortran program. It is usually caused by a mistake or inconsistency in the code, and can lead to the program crashing or producing incorrect results.

How can I fix a Fortran runtime error?

Fixing a Fortran runtime error can be a complex process, but it usually involves carefully checking the code for any mistakes or typos. It may also require debugging tools or techniques, such as using print statements or a debugger, to identify the specific cause of the error.

What are some common causes of Fortran runtime errors?

Some common causes of Fortran runtime errors include using uninitialized variables, referencing arrays or variables out of bounds, and using incorrect data types or formats. These errors can often be avoided by writing well-structured and carefully tested code.

Is there a difference between a compile-time error and a runtime error in Fortran?

Yes, there is a difference between compile-time errors and runtime errors in Fortran. Compile-time errors occur during the compilation process, before the program is executed, and are usually caused by syntax or logic mistakes. Runtime errors, on the other hand, occur during the execution of the program and are often caused by mistakes in the code or unexpected inputs.

What are some tips for beginners to avoid Fortran runtime errors?

To avoid Fortran runtime errors, beginners should pay attention to details and carefully check their code for any mistakes before running it. It can also be helpful to use debugging tools and techniques, as well as to practice writing well-structured and tested code. Additionally, seeking guidance from more experienced programmers can also be beneficial in avoiding and fixing runtime errors.

Similar threads

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