time array out of bounds in modelling?

╄→尐↘猪︶ㄣ 提交于 2019-12-04 21:13:48

I've got some time now to elaborate on my earlier comment. (Note that I use italics denote terms you might care to read about.)

The error you report is a run-time error, not one that the compiler is able to see at compile-time. If you don't understand the difference between run-time (ie when the code executes) and compile-time (ie when the compiler turns your sources into executable code) do some research. Furthermore it's evident that you (or someone) has instructed the compiler to create a version of the code which checks that array element accesses are within array bounds. This is a very important safety feature when testing new software, but imposes a performance penalty when the code executes so many codes are, once they've passed their tests, compiled without this checking.

I don't know what compiler you're using but look at its documentation to find an option that turns on array bounds checking at run-time.

The error message is quite explicit -- at some point in your code it has tried to access element 141 of an array with only 140 elements. We can't tell you how this has happened, probably not even if we saw your entire code. This kind of thing often happens when data is loaded that doesn't conform to the programmer's expectations. It also often happens when programmers make off-by-one errors in writing loops. We might spot that from looking at your whole code, but you're in a much better position to do that than we are.

You write

but my collegue has been able to run the model with no issues, which was strange to me. He compiled the model with some different settings in the Makefile, I don't know if this matters,

Well, yes, this matters, it matters a lot. If you write code that accesses element 141 of an array with 140 elements Fortran, like many other compiled languages, will happily access the next location in memory after element 140. In general you haven't a clue what data the program is interfering with. If you are lucky the next location in memory is outside the address space the operating system has allocated to the program and the operating system stops the program immediately and reports a segmentation fault.

If you're unlucky the program carries on blithely reading from, and writing to, element 141, whatever the heck it is.

I speculate that your colleague has not implemented array-bounds checking for his version of the code. It's up to you whether or not you tell him his code's (very probably) broken.

So what do you do about it ? You debug the program. You can do this in a variety of ways, the easiest of which is (in my opinion) to insert some write statements to print out variable values at critical points in the code to test your assumptions about what values they might, can, or actually do take. More difficult, but worth the initial effort in terms of future problem-solving, would be to run the code under the control of a debugger. There are several good debuggers available for Fortran programs on all major platforms.

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