Array Reference is out of bounds error? Fortran

In summary, The conversation was about debugging the compilation of a Fortran program and the importance of focusing on the first error and correcting it. It was also mentioned that implicit variables are not a good programming practice and should be avoided. Lastly, the conversation touched upon some new errors that were encountered after resolving the initial ones.
  • #1
adishpatel
22
0
So, I have a bunch of errors which need resolving here but majority are based on the "out of bounds" error and hence the topic title :). Would be great if you are able to help me resolve the errors. Quite a newbie with fortran. So please bear with any silly questions that I may throw at you.

Code:
$ make Trial1
gfortran    Trial1.f   -o Trial1
Trial1.f:16.24:

      INTEGER IER,IW,K,J
                        1
Error: Symbol 'j' at (1) already has basic type of INTEGER
Trial1.f:47.11:

      IW = 1500
           1
Warning: Extension: Conversion from INTEGER(4) to LOGICAL(4) at (1)
Trial1.f:164.12:

      DO 18,  K=1,1000
            1
Error: Loop variable at (1) must be INTEGER
Trial1.f:267.10:

      Fxr(2) = (Ex(2)/psi)*(FR(2)/ER(2))
          1
Warning: Array reference at (1) is out of bounds (2 > 1) in dimension 1
Trial1.f:268.10:

      Fyr(2) = (Ey(2)/phi)*(FR(2)/ER(2))
          1
Warning: Array reference at (1) is out of bounds (2 > 1) in dimension 1
Trial1.f:283.30:

      F(3) = (-S(2)-2.0d0*Fxr(2)-FT(2)*X(5))/mw
                              1
Warning: Array reference at (1) is out of bounds (2 > 1) in dimension 1
Trial1.f:284.32:

      F(4) = (-S(4)-2.0d0*a*Fyr(2))/Iwy
                                1
Warning: Array reference at (1) is out of bounds (2 > 1) in dimension 1
<builtin>: recipe for target 'Trial1' failed
make: *** [Trial1] Error 1

The complete code can be found at: sysden.com/Trial.f
 
Technology news on Phys.org
  • #2
When debugging the compilation of a program, I find it a very good practice to focus on the first error and correct it, and then recompile the program, as subsequent errors can depend on the first one.

Case in point:
adishpatel said:
Trial1.f:16.24:

INTEGER IER,IW,K,J
1
Error: Symbol 'j' at (1) already has basic type of INTEGER
Trial1.f:47.11:

IW = 1500
1
Warning: Extension: Conversion from INTEGER(4) to LOGICAL(4) at (1)
The warning on IW is due to the fact that the error in declaring J meant that IW was not declared as in integer. You are declaring J in lines 6 and 16, so remove one of them.

I must say that the statement
Code:
IMPLICIT LOGICAL (A-Z)
is one of the weirdest I have ever seen. Logical variables are rarely used in Fortran, so why make all variables logical by default? Anyway, implicit variables make debugging diffucult and are not a good programming practice. Use
Code:
IMPLICIT NONE
and declare explicitly all variables.


adishpatel said:
Trial1.f:267.10:

Fxr(2) = (Ex(2)/psi)*(FR(2)/ER(2))
1
Warning: Array reference at (1) is out of bounds (2 > 1) in dimension 1
Variables Fxf, Fyf, Fxr, and Fyr have all been declared as arrays of size 1 (which is legal, but strange). Therefore, you can't assing a value to Fxr(2).
 
  • Like
Likes 1 person
  • #3
Thank you very much DrClaude!


DrClaude said:
When debugging the compilation of a program, I find it a very good practice to focus on the first error and correct it, and then recompile the program, as subsequent errors can depend on the first one.

Case in point:

The warning on IW is due to the fact that the error in declaring J meant that IW was not declared as in integer. You are declaring J in lines 6 and 16, so remove one of them.
Not sure how I missed out on catching that, but that was such a stupid mistake on my end. It was right in front of me! :(

I must say that the statement
Code:
IMPLICIT LOGICAL (A-Z)
is one of the weirdest I have ever seen. Logical variables are rarely used in Fortran, so why make all variables logical by default? Anyway, implicit variables make debugging diffucult and are not a good programming practice. Use
Code:
IMPLICIT NONE
and declare explicitly all variables.
The Logical (A-Z) statement was added based on a journal I was going through. It helped me build up the code until now. However, I understand your point there. Changing it to IMPLICIT NONE also helped me get rid of the "Error: Loop variable at (1) must be INTEGER" alongside.

Variables Fxf, Fyf, Fxr, and Fyr have all been declared as arrays of size 1 (which is legal, but strange). Therefore, you can't assing a value to Fxr(2).

I see! I figured that they should have been Fxr(1) and Fyr(1) instead :). Your input helped me spot that. Thank you once again.



All the old errors seem to have disappeared and a few more have showed up :(

Code:
$ make Trial1
gfortran    Trial1.f   -o Trial1
Trial1.f:219.20:

      COMMON /PARAM/ V
                    1
Warning: Named COMMON block 'param' at (1) shall be of the same size as elsewhere (8 vs 16 bytes)
Trial1.f:302.20:

      COMMON /EXTRA/ mw,mf,k1,k2,k3,k4,k5,k6
                    1
Warning: Named COMMON block 'extra' at (1) shall be of the same size as elsewhere (64 vs 360 bytes)
Trial1.f:301.20:

      COMMON /PARAM/ V
                    1
Warning: Named COMMON block 'param' at (1) shall be of the same size as elsewhere (8 vs 16 bytes)
/tmp/cceH8Y8s.o:Trial1.f:(.text+0x800): undefined reference to `ode2_'
/tmp/cceH8Y8s.o:Trial1.f:(.text+0x800): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `ode2_'
/tmp/cceH8Y8s.o:Trial1.f:(.text+0xa46): undefined reference to `path_'
/tmp/cceH8Y8s.o:Trial1.f:(.text+0xa46): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `path_'
/tmp/cceH8Y8s.o:Trial1.f:(.text+0xb2e): undefined reference to `path_'
/tmp/cceH8Y8s.o:Trial1.f:(.text+0xb2e): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `path_'
/tmp/cceH8Y8s.o:Trial1.f:(.text+0xce0): undefined reference to `path_'
/tmp/cceH8Y8s.o:Trial1.f:(.text+0xce0): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `path_'
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/../../../../x86_64-pc-cygwin/bin/ld: /tmp/cceH8Y8s.o: bad reloc address 0x0 in section `.pdata'
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/../../../../x86_64-pc-cygwin/bin/ld: final link failed: Invalid operation
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'Trial1' failed
make: *** [Trial1] Error 1

I searched for "COMMON /PARAM/ V" throughout the document and no other common parameter is mentioned in the same statement. They all are of the same size?

The program as been updated at the same URL: http://sysden.com/Trial.f
 
  • #4
adishpatel said:
I see! I figured that they should have been Fxr(1) and Fyr(1) instead :).
Why do you declare arrays of size 1 instead of using scalars?

Your input helped me spot that. Thank you once again.



All the old errors seem to have disappeared and a few more have showed up :(

adishpatel said:
Code:
Trial1.f:219.20:

      COMMON /PARAM/ V
                    1
Warning: Named COMMON block 'param' at (1) shall be of the same size as elsewhere (8 vs 16 bytes)
In line 11, you declare the common block PARAM with the variable C, which is an array of size 2, while V in line 219 is a scalar.

You have a similar problem with the common block EXTRA, where in line 302 it is not the same size as declared previously in the main program and in subroutine FCN.

adishpatel said:
Code:
/tmp/cceH8Y8s.o:Trial1.f:(.text+0x800): undefined reference to `ode2_'
/tmp/cceH8Y8s.o:Trial1.f:(.text+0x800): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `ode2_'
/tmp/cceH8Y8s.o:Trial1.f:(.text+0xa46): undefined reference to `path_'
/tmp/cceH8Y8s.o:Trial1.f:(.text+0xa46): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `path_'
Subroutines ODE2 and PATH are not in Trial.f. You need to link with the proper object file or library.
 
  • #5
adishpatel said:
TI searched for "COMMON /PARAM/ V" throughout the document and no other common parameter is mentioned in the same statement. They all are of the same size?

Make sure that V is declared to be the same size in all program units that use this common block.
 
  • #6
Thank you DrClaude and jtbell! You guys have been great!

I have left a few arrays with size 1 for future use as there will be other formulas added in. Will simply update the array accordingly :)

So, after quite some effort and error solving that appeared on the console straight away, I have managed to fix the code and compile it using the commands:

make "FC=gfortran" Trail

which creates a .exe file and generates an output. However, it seems that I am getting NaN's?

Code:
 T =  0.1225D+02    X(1) =                 NaN    X(2) =                 NaN
 T =  0.1226D+02    X(1) =                 NaN    X(2) =                 NaN
 T =  0.1228D+02    X(1) =                 NaN    X(2) =                 NaN
 T =  0.1229D+02    X(1) =                 NaN    X(2) =                 NaN
 T =  0.1230D+02    X(1) =                 NaN    X(2) =                 NaN
 T =  0.1232D+02    X(1) =                 NaN    X(2) =                 NaN
 T =  0.1233D+02    X(1) =                 NaN    X(2) =                 NaN
 T =  0.1234D+02    X(1) =                 NaN    X(2) =                 NaN
 T =  0.1235D+02    X(1) =                 NaN    X(2) =                 NaN
 T =  0.1237D+02    X(1) =                 NaN    X(2) =                 NaN
 T =  0.1238D+02    X(1) =                 NaN    X(2) =                 NaN
 T =  0.1239D+02    X(1) =                 NaN    X(2) =                 NaN
 T =  0.1240D+02    X(1) =                 NaN    X(2) =                 NaN
 T =  0.1242D+02    X(1) =                 NaN    X(2) =                 NaN
 T =  0.1243D+02    X(1) =                 NaN    X(2) =                 NaN
 T =  0.1244D+02    X(1) =                 NaN    X(2) =                 NaN
 T =  0.1245D+02    X(1) =                 NaN    X(2) =                 NaN
 T =  0.1247D+02    X(1) =                 NaN    X(2) =                 NaN
 T =  0.1248D+02    X(1) =                 NaN    X(2) =                 NaN
 T =  0.1249D+02    X(1) =                 NaN    X(2) =                 NaN
 T =  0.1250D+02    X(1) =                 NaN    X(2) =                 NaN
 T =  0.1252D+02    X(1) =                 NaN    X(2) =                 NaN
 T =  0.1253D+02    X(1) =                 NaN    X(2) =                 NaN
 T =  0.1254D+02    X(1) =                 NaN    X(2) =                 NaN
 T =  0.1255D+02    X(1) =                 NaN    X(2) =                 NaN
 T =  0.1257D+02    X(1) =                 NaN    X(2) =                 NaN

Any way to figure out what is causing it? Some sort of debugging tool that executes the code line by line and give an output helping me figure out what exactly is causing the results to be NaN?

I am currently using Cygwin with a custom library to execute my code if that is of any use.

I have also tried to tamper with the code here which governs the ouput but that hasn't quite helped in any way:
Code:
      INDEX=1
      T=0.0D+00
   17 FORMAT(' T =',D12.4,'    X(1) =',D20.10,'    X(2) =',D20.10)
      DO 18,  K=1,1000
         TEND = K*TPER/500
         CALL ODE2(FCN,JAC,IJAC,N,T,X,TEND,INDEX,IER)
         IF (X(1).GT.1.0D+01) GOTO 18
         WRITE(6,17) T,X(11),X(12)
         WRITE(3,*) T,X(11),X(12)
   18 CONTINUE
      IF (.TRUE.) STOP

Updated program is available as usual at http://sysden.com/Trial.f

Thank you once again!
 
  • #7
adishpatel said:
Any way to figure out what is causing it? Some sort of debugging tool that executes the code line by line and give an output helping me figure out what exactly is causing the results to be NaN?
You can use the likes of gdb to do some debugging, but I am not sure that this would help you here. If ODE2 is a library routine, the problem lies most probably with the system of ODEs you are trying to solve. What do the results look like before the NaNs appear? Is the result close to an overflow?

Looking at the program more closer, I see that the Jacobian is not defined properly. The arrays in JAC are much too small, and this might be causing you some problems.

adishpatel said:
Code:
      IF (.TRUE.) STOP
Minor point: why the IF? The statement should simply be STOP.
 
  • #8
I have managed to resolve the issue with the help of my tutor. There was a minor issue with the formula. Different papers/journals had different formulas which caused me to not being able to produce the results.

However, on the matter of ODE2, I have a separate Makefile which directs the .f file to look for ODE2 in the specific library. It compiles fine when running make, but when I was trying to compile it for gdb using the command,

gfortran -g Trial.f -o Trial

and that would throw out errors:
Code:
/tmp/cciA5Yig.o: In function `proeq':
/home/jatiN/trail1.f:172: undefined reference to `ode2_'
/home/jatiN/trail1.f:172:(.text+0x848): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `ode2_'
collect2: error: ld returned 1 exit status

I tried modifying the path of the library by using

export LD_LIBRARY_PATH="/home/jatiN/lib"

but that was a no go. As I have the results in hand now, it is not of extreme importance to debug it. However, if this could be resolved with some changes, then might as well look into it :)

Also, I did notice (as you pointed out) that the program had array of 2 for X() under the Subroutine JAC. Changed it to 14 now. It didn't cause any results to change, however it should help in any further coding work.

Thank you Dr.Claude for all the help so far! You have helped me a lot!
 
  • #9
adishpatel said:
However, on the matter of ODE2, I have a separate Makefile which directs the .f file to look for ODE2 in the specific library. It compiles fine when running make, but when I was trying to compile it for gdb using the command,

gfortran -g Trial.f -o Trial

and that would throw out errors:
Code:
/tmp/cciA5Yig.o: In function `proeq':
/home/jatiN/trail1.f:172: undefined reference to `ode2_'
/home/jatiN/trail1.f:172:(.text+0x848): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `ode2_'
collect2: error: ld returned 1 exit status

I tried modifying the path of the library by using

export LD_LIBRARY_PATH="/home/jatiN/lib"
The problem is that you are not specifying which library to link to. The compilation should look like
Code:
gfortran -g Trial.f -o Trial -lode
where you would have a library file libode.a containg the code from ODE2.

adishpatel said:
Also, I did notice (as you pointed out) that the program had array of 2 for X() under the Subroutine JAC. Changed it to 14 now. It didn't cause any results to change, however it should help in any further coding work.
If ODE2 uses the Jacobian, then it needs to be properly set up if you want correct results.
 
  • #10
Code:
$ gfortran -g Trial.f -o Trial -lode
/usr/bin/ld: cannot find -lode
collect2: error: ld returned 1 exit status

I created the missing directories (bin and ld) and added the file named libode.a with the code of ode2.f inside it and received the following error?
 
  • #11
adishpatel said:
Code:
$ gfortran -g Trial.f -o Trial -lode
/usr/bin/ld: cannot find -lode
collect2: error: ld returned 1 exit status

I created the missing directories (bin and ld) and added the file named libode.a with the code of ode2.f inside it and received the following error?
Look, I don't know where the code for ODE2 and PATH resides, -lode was just an example. It's up to you to figure out where they should be linked from.
 
  • #12
Oh okay.. I thought that was a way to link the library. My bad, sorry about that. I'll figure that you. Thank you very much for all the help you have provided. I highly appreciate it.
 

Related to Array Reference is out of bounds error? Fortran

1. What is an "Array Reference is out of bounds" error in Fortran?

An "Array Reference is out of bounds" error in Fortran occurs when a program attempts to access an array element that does not exist. This could be due to a variety of reasons such as a typo in array dimension or index, or the program trying to access an element outside the declared size of the array.

2. How can I identify and debug an "Array Reference is out of bounds" error in Fortran?

The best way to identify and debug an "Array Reference is out of bounds" error in Fortran is to use a debugger. Most Fortran compilers come with a built-in debugger that can help pinpoint the exact line of code where the error occurs. Additionally, you can also use print statements to track the values of array dimensions and indices to identify the cause of the error.

3. Can a "Array Reference is out of bounds" error cause a program to crash?

Yes, an "Array Reference is out of bounds" error can cause a program to crash. This is because the program is trying to access memory that it does not have access to, which can result in a segmentation fault or other types of runtime errors. It is important to handle these errors carefully to avoid program crashes.

4. How can I prevent "Array Reference is out of bounds" errors in Fortran?

To prevent "Array Reference is out of bounds" errors in Fortran, it is important to carefully declare and initialize array dimensions and indices. Make sure to double check for typos and errors when declaring arrays, and always keep track of the size and indices being used. Additionally, using bounds checking options provided by some Fortran compilers can also help catch these errors during compilation.

5. Are there any alternative ways to handle "Array Reference is out of bounds" errors in Fortran?

Yes, there are alternative ways to handle "Array Reference is out of bounds" errors in Fortran. One way is to use the "ASSOCIATE" statement to associate a pointer with the array and perform bounds checking on the pointer instead of the array itself. Another way is to use the "ERROR STOP" statement to stop the program and print an error message when an array reference is out of bounds, allowing for easier identification and debugging.

Similar threads

  • Programming and Computer Science
Replies
4
Views
635
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
19
Views
5K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
5
Views
7K
Back
Top