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

AI Thread Summary
The Fortran runtime error encountered is due to a mismatch between the number of output items in the WRITE statement and the specified format descriptors. The original code printed 18 values, but the updated version attempts to print 36 values without adjusting the format string, leading to the error. To resolve this, the format string should be modified to accommodate the increased number of output values, such as changing the descriptor from 20e15.7 to 38e15.7. This adjustment allows the program to run without errors and produce the correct output. Understanding the intricacies of the FORMAT statement is essential for effective data printing in Fortran.
Matterwave
Science Advisor
Homework Helper
Gold Member
Messages
3,971
Reaction score
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
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?
 
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:
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?
 
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
 
I'd use 40 but to be accurate you have 1+36 values if you include the err field right?
 
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?
 
You can over specify formatted fields as then they don't get used and there's no error.
 
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. :)
 

Similar threads

Replies
5
Views
5K
Replies
9
Views
2K
Replies
2
Views
9K
Replies
2
Views
25K
Replies
54
Views
5K
Replies
16
Views
2K
Back
Top