Why am I getting a Fortran runtime error when trying to print more values?

In summary, the code around line 2057 is trying to print out xsum and xsumbar which are two nflavor by nflavor matrices, but is instead printing an xsum that has been expanded to a nflavor2 by nflavor2 matrix (nflavor2=nflavor*2).
  • #1
Matterwave
Science Advisor
Gold Member
3,971
329
Hi guys, I've been tinkering with this code for a while now, as you probably have seen from my other threads. Right now I am working on changing the code around so that it actually writes out the correct output instead of the old output.

I am getting this runtime error:
Code:
At line 2057 of file bulb-new.F90 (unit = 6, file = 'stdout')
Fortran runtime error: Expected INTEGER for item 26 in formatted transfer, got REAL
(a1,f15.10,i5,1p,20e15.7)
           ^

The code that is around line 2057 is this:
Code:
write(6,'(a1,f15.10,i5,1p,20e15.7)')'x',r,iter,dr,err, &
  &  xsum(1:nflavor2,1:nflavor2)
  open(unit=7,file='status')
  write(7,'(a1,f15.10,i5,1p,20e15.7)')'x',r,iter,dr,err, &
  &  xsum(1:nflavor2,1:nflavor2)
  close(7)

Now, I basically just copy-pasted this code from a previous iteration, so I don't know really what all those formatting things mean. The previous iteration worked fine and did not give this error. The part that I copy pasted looks like this in the previous iteration:

Code:
write(6,'(a1,f15.10,i5,1p,20e15.7)')'x',r,iter,dr,err, &
  &  xsum(1:nflavor,1:nflavor),xbarsum(1:nflavor,1:nflavor)
  open(unit=7,file='status')
  write(7,'(a1,f15.10,i5,1p,20e15.7)')'x',r,iter,dr,err, &
  &  xsum(1:nflavor,1:nflavor),xbarsum(1:nflavor,1:nflavor)
  close(7)

As you can see, it is nearly identical except instead of printing xsum and xsumbar which are two nflavor by nflavor matrices, I have replaced it with printing an xsum that has been expanded to a nflavor2 by nflavor2 matrix (nflavor2=nflavor*2). So instead of printing 18 numbers, it should print 36 numbers.

Can anyone shed some light on what's happening? Maybe having a bigger matrix to print out is a problem?
 
Technology news on Phys.org
  • #2
Not sure if this is right but the 20e15.7 format code maybe it needs to be increased?

Doesn't the 20 represent the number of columns of 15.7 formatted numbers?
 
  • #3
jedishrfu said:
Not sure if this is right but the 20e15.7 format code maybe it needs to be increased?

Doesn't the 20 represent th number of columns of 15.7 formatted number?

What do you mean by columns of 15.7? I really have no idea what that means...I definitely will look it up, but, the little carat in the terminal was pointing at the "i5" so I thought the error was there?
The problem is, the online resources on formatting the write statement...is not super helping me all that much here lol.
 
Last edited:
  • #4
As I look at the formatting I see a1 for the 'x' and f15.10 for the r and i5 for the iter and 1p for dr and error and xsum are covered by the 20f15.7 and you said the original printed 18 values and so now you've doubled that to 36 so you haven't increased the format code for those added fields right?
 
  • #5
jedishrfu said:
As I look at the formatting I see a1 for the 'x' and f15.10 for the r and i5 for the iter and 1p for dr and error and xsum are covered by the 20f15.7 and you said the original printed 18 values and so now you've doubled that to 36 so you haven't increased the format code for those added fields right?

Yeah, I did not mess around with the formatting at all. The original has xsum+xsumbar being 18 values, and now I have a 36 valued xsum to print. I tried expanding to 36 and now the code is running...so...we'll see...? but maybe I should change it to 38? o_O
 
  • #6
I'd use 40 but to be accurate you have 1+36 values if you include the err field right?
 
  • #7
jedishrfu said:
I'd use 40 but to be accurate you have 1+36 values if you include the err field right?

How do you get 40 from 36+1? o_O

What I guessed was before there were 18 values, and there was a "20" there, so I was guessing the write statement was taking into account 2 extra slots...for something...so I guessed 36+2=38 is what I should have.

But, now that I think about it, shouldn't the original code have 19? 1 for err and 18 for the 18 values inside xsum and xsumbar?
 
  • #8
You can over specify formatted fields as then they don't get used and there's no error.
 
  • #9
jedishrfu said:
You can over specify formatted fields as then they don't get used and there's no error.

Ah! Thanks for that info! I will definitely put a little room for error then. I stopped the run and there was definitely some errors in the writing because I could no longer open the output file in gedit lol.
 
  • #10
Yes, isn't programming wonderful? The simplest change can result in hours of trying to find why it didn't work.
 
  • #11
jedishrfu said:
Yes, isn't programming wonderful? The simplest change can result in hours of trying to find why it didn't work.

I have to say that I am learning quite a bit of programming from working this code...but it's definitely not my favorite thing in the world...
 
  • #12
I am getting this runtime error:
Code:
At line 2057 of file bulb-new.F90 (unit = 6, file = 'stdout')
Fortran runtime error: Expected INTEGER for item 26 in formatted transfer, got REAL
(a1,f15.10,i5,1p,20e15.7)
           ^

The code that is around line 2057 is this:
Code:
write(6,'(a1,f15.10,i5,1p,20e15.7)')'x',r,iter,dr,err, &
  &  xsum(1:nflavor2,1:nflavor2)
  open(unit=7,file='status')
  write(7,'(a1,f15.10,i5,1p,20e15.7)')'x',r,iter,dr,err, &
  &  xsum(1:nflavor2,1:nflavor2)
  close(7)

Now, I basically just copy-pasted this code from a previous iteration, so I don't know really what all those formatting things mean. The previous iteration worked fine and did not give this error. The part that I copy pasted looks like this in the previous iteration:

Code:
write(6,'(a1,f15.10,i5,1p,20e15.7)')'x',r,iter,dr,err, &
  &  xsum(1:nflavor,1:nflavor),xbarsum(1:nflavor,1:nflavor)
  open(unit=7,file='status')
  write(7,'(a1,f15.10,i5,1p,20e15.7)')'x',r,iter,dr,err, &
  &  xsum(1:nflavor,1:nflavor),xbarsum(1:nflavor,1:nflavor)
  close(7)

As you can see, it is nearly identical except instead of printing xsum and xsumbar which are two nflavor by nflavor matrices, I have replaced it with printing an xsum that has been expanded to a nflavor2 by nflavor2 matrix (nflavor2=nflavor*2). So instead of printing 18 numbers, it should print 36 numbers.

Can anyone shed some light on what's happening? Maybe having a bigger matrix to print out is a problem?

I think you have to be careful and make sure the number of items in the output list of the WRITE statement matches the edit descriptors in the corresponding format string.

For example, you have specified 20 e15.7 fields to output the values of the XSUM and XBARSUM arrays. If you increase the number of array items you wish to output by changing the dimension of these arrays without changing the number of fields allocated in the format string, the WRITE statement will attempt to output the extra data by going back to the initial edit descriptor in the format string. This may not cause a problem until you try to write a floating point number using an I5 descriptor, which I think is why you are getting this particular error message.

Based on your comments about this problem, it is not clear if you are intending to print out all the values in the XSUM and XBARSUM arrays or just certain values. Normally, an implied DO loop would be used to print the values in an array in whole or in part.

Since your format string contains descriptors for printing single variables, the method normally used to force the formatting of an implied DO loop to start at a particular location in the format string is to enclose the repeated edit descriptors within another set of parentheses, in order to keep the format string from defaulting back to the initial edit descriptor if the number of output variables exceeds the number of edit descriptors.

For example,

Code:
write(6,'(a1,f15.10,i5,1p,(20e15.7))')'x',r,iter,dr,err, &
  &  xsum(1:nflavor,1:nflavor),xbarsum(1:nflavor,1:nflavor)
  open(unit=7,file='status')
  write(7,'(a1,f15.10,i5,1p,(20e15.7))')'x',r,iter,dr,err, &
  &  xsum(1:nflavor,1:nflavor),xbarsum(1:nflavor,1:nflavor)
  close(7)

adding the extra set of parentheses around the 20E15.7 edit descriptor means that when 20 fields have been printed and more values are to be output, an additional set of 20 fields with the E15.7 descriptor will be printed, rather than rewinding all the way back to the A1 descriptor.

The FORMAT statement is one of the trickier statements to master in learning Fortran, but it can provide a flexible means of printing formatted data when it is learned. It might be better to experiment with a small program which does nothing but print various output so that you can get a feel for how the various features of the FORMAT statement interact with corresponding READ or WRITE statements. It may also help to use numbered FORMAT statements which stand separately from the READ or WRITE statements where the input or output is done.
 
  • #13
SteamKing said:
I think you have to be careful and make sure the number of items in the output list of the WRITE statement matches the edit descriptors in the corresponding format string.

For example, you have specified 20 e15.7 fields to output the values of the XSUM and XBARSUM arrays. If you increase the number of array items you wish to output by changing the dimension of these arrays without changing the number of fields allocated in the format string, the WRITE statement will attempt to output the extra data by going back to the initial edit descriptor in the format string. This may not cause a problem until you try to write a floating point number using an I5 descriptor, which I think is why you are getting this particular error message.

Based on your comments about this problem, it is not clear if you are intending to print out all the values in the XSUM and XBARSUM arrays or just certain values. Normally, an implied DO loop would be used to print the values in an array in whole or in part.

Since your format string contains descriptors for printing single variables, the method normally used to force the formatting of an implied DO loop to start at a particular location in the format string is to enclose the repeated edit descriptors within another set of parentheses, in order to keep the format string from defaulting back to the initial edit descriptor if the number of output variables exceeds the number of edit descriptors.

For example,

Code:
write(6,'(a1,f15.10,i5,1p,(20e15.7))')'x',r,iter,dr,err, &
  &  xsum(1:nflavor,1:nflavor),xbarsum(1:nflavor,1:nflavor)
  open(unit=7,file='status')
  write(7,'(a1,f15.10,i5,1p,(20e15.7))')'x',r,iter,dr,err, &
  &  xsum(1:nflavor,1:nflavor),xbarsum(1:nflavor,1:nflavor)
  close(7)

adding the extra set of parentheses around the 20E15.7 edit descriptor means that when 20 fields have been printed and more values are to be output, an additional set of 20 fields with the E15.7 descriptor will be printed, rather than rewinding all the way back to the A1 descriptor.

The FORMAT statement is one of the trickier statements to master in learning Fortran, but it can provide a flexible means of printing formatted data when it is learned. It might be better to experiment with a small program which does nothing but print various output so that you can get a feel for how the various features of the FORMAT statement interact with corresponding READ or WRITE statements. It may also help to use numbered FORMAT statements which stand separately from the READ or WRITE statements where the input or output is done.

This is useful information. Thanks! By changing 20 to 38, the code runs fine now and prints the correct values. :)
 

FAQ: Why am I getting a Fortran runtime error when trying to print more values?

What is a runtime error in FORTRAN?

A runtime error in FORTRAN is an error that occurs during the execution of a program. It is caused by a mistake or unexpected event that the program was not designed to handle, and it can cause the program to crash or produce incorrect results.

How do I fix a runtime error in FORTRAN?

To fix a runtime error in FORTRAN, you will need to identify the source of the error by debugging your code. This can involve using tools such as a debugger or print statements to track the program's execution and identify where the error is occurring. Once you have identified the source of the error, you can then make changes to your code to correct the error.

What are some common causes of runtime errors in FORTRAN?

Some common causes of runtime errors in FORTRAN include division by zero, accessing an array element outside of its bounds, and using uninitialized variables. Other potential causes include passing incorrect data types to subroutines or functions, and attempting to read or write to a file that does not exist.

How can I prevent runtime errors in FORTRAN?

To prevent runtime errors in FORTRAN, it is important to thoroughly test and debug your code before running it. This involves checking for potential errors and edge cases, such as dividing by zero or using uninitialized variables, and handling them appropriately. It is also helpful to use good programming practices, such as properly declaring and initializing variables, to reduce the likelihood of runtime errors.

Is there a way to automatically catch and handle runtime errors in FORTRAN?

Yes, there are tools and techniques available for automatically catching and handling runtime errors in FORTRAN. These include using exception handling mechanisms, such as the Fortran 2003 "stop" statement, and using debuggers or other runtime error detection tools. Additionally, some programming languages, such as MATLAB, have built-in error handling functionality that can be used when programming in FORTRAN.

Similar threads

Replies
5
Views
4K
Replies
9
Views
1K
Replies
4
Views
2K
Replies
3
Views
2K
Replies
2
Views
8K
Replies
2
Views
24K
Replies
54
Views
5K
Replies
16
Views
2K
Replies
4
Views
4K
Back
Top