Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Array Reference is out of bounds error? Fortran

  1. May 1, 2014 #1
    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 (Text):
    $ 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
     
  2. jcsd
  3. May 2, 2014 #2

    DrClaude

    User Avatar

    Staff: Mentor

    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.

    I must say that the statement
    Code (Text):
    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 (Text):
    IMPLICIT NONE
    and declare explicitly all variables.


    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).
     
  4. May 2, 2014 #3
    Thank you very much DrClaude!!


    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! :(

    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.

    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 (Text):

    $ 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
     
  5. May 2, 2014 #4

    DrClaude

    User Avatar

    Staff: Mentor

    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 :(

    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.

    Subroutines ODE2 and PATH are not in Trial.f. You need to link with the proper object file or library.
     
  6. May 2, 2014 #5

    jtbell

    User Avatar

    Staff: Mentor

    Make sure that V is declared to be the same size in all program units that use this common block.
     
  7. May 5, 2014 #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 (Text):

     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 (Text):

          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!
     
  8. May 5, 2014 #7

    DrClaude

    User Avatar

    Staff: Mentor

    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.

    Minor point: why the IF? The statement should simply be STOP.
     
  9. May 7, 2014 #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 (Text):

    /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!
     
  10. May 7, 2014 #9

    DrClaude

    User Avatar

    Staff: Mentor

    The problem is that you are not specifying which library to link to. The compilation should look like
    Code (Text):
    gfortran -g Trial.f -o Trial -lode
    where you would have a library file libode.a containg the code from ODE2.

    If ODE2 uses the Jacobian, then it needs to be properly set up if you want correct results.
     
  11. May 7, 2014 #10
    Code (Text):

    $ 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?
     
  12. May 7, 2014 #11

    DrClaude

    User Avatar

    Staff: Mentor

    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.
     
  13. May 7, 2014 #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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook