How can I debug a Fortran READ/WRITE statement with an implicit DO loop?

假装没事ソ 提交于 2020-02-06 02:43:11

问题


The Fortran program I am working is encountering a runtime error when processing an input file.

At line 182 of file ../SOURCE_FILE.f90 (unit = 1, file = 'INPUT_FILE.1')
Fortran runtime error: Bad value during integer read

Looking to line 182 I see a READ statement with an implicit/implied DO loop:

182:    READ(IT4, 310 )((IPPRM2(IP,I),IP=1,NP),I=1,16)    ! read 6 integers
183:    READ(IT4, 320 )((PPARM2(IP,I),IP=1,NP),I=1,14)    ! read 5 reals

Format statement:

310 FORMAT(1X,6I12)

When I reach this code in the debugger NP has a value of 2. I has a value of 6, and IP has a value of 67. I think I and IP should be reinitialized in the loop.

My problem is that when I try to step through in the debugger once I get to the READ statement it seems to execute and then throw the error. I'm not sure how to follow it as it reads. I tried stepping into the function, but it seems like that may be a difficult route to take since I am unfamiliar with the gfortran library. The input file looks OK, I think it should be read just fine. This makes me think this READ statement isn't looping as intended.

I am completely new to Fortran and implicit DO loops like this, but from what I can gather line 182 should read in 6 integers according to the format string #310. However, when I arrive NP has a value of 2 which makes me think it will only try to read 2 integers 16 times.

How can I debug this read statement to examine the values read into IPPARM as they are read from the file? Will I have to step through the Fortran library?

Any tips that can clear up my confusion regarding these implicit loops would be appreciated!

Thanks!

NOTE: I'm using gfortran/gcc and gdb on Linux.


回答1:


Is there any reason you need specific formatting on the read? I would use READ(IT4, *) where feasible...

Later versions of gfortran support unlimited format reads (see link http://fortranwiki.org/fortran/show/Fortran+2008+status)

Then it may be helpful to specify

310 FORMAT("*(1X,6I12)")

Or for older compilers

310 FORMAT(1000(1X,6I12))



回答2:


The variables IP and I are loop indices and so they are reinitialized by the loop. With NP=2 the first statement is going to read a total of 32 integers -- it is contributing to the determination the list of items to read. The format determines how they are read. With "1X,6I12" they will be read as 6 integers per line of the input file. When the first 6 of the requested 32 integers is read fron a line/record, Fortran will consider that line/record completed and advance to the next record.

With a format of "1X,6I12" the integers must be precisely arranged in the file. There should be a single blank, then the integers should each be right-justified in fields of 12 columns. If they get out of alignment you could get the wrong value read or a runtime error.



来源:https://stackoverflow.com/questions/11523763/how-can-i-debug-a-fortran-read-write-statement-with-an-implicit-do-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!