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

Error 112 /undef when running fortran code

  1. Feb 11, 2009 #1
    Hiya. I am able ot compile my program with no errors but when I run it I get the message:
    Error 112, Reference to undefined variable, array element or function result.

    I cannot see where I have gone wrong and more frustratingly this code was working, running producing results on the unix system at university but when I compile it on my windows system using silverfrost plato it gives this error. This is my code: (the line is bold is what it objects to)

    PROGRAM project1

    ! MTMW14 Project 1: A Simplified Model of Ocean Gyres and the Gulf Stream
    ! Date Created: 27/01/2009 Author: Freja Hunt Version 1

    ! Simulate wind driven circulation in a closed basin with a western boundary current using the Stommel (1948) model
    ! and Arakawa C grid with a mixed explicit-implicit time scheme.


    !----------------------------------------


    IMPLICIT NONE


    ! Define variables

    REAL, PARAMETER:: fo=10.0**(-4), beta=10.0**(-11), g=10.0, drag=10.0**(-6), density=1000.0,&
    & windo=0.2, tstep=100.0, pi=3.1415926535, L=10.0**(6)
    ! fo is the reference coriolis parameter, beta is the beta plane approximation that allows the coriolis parameter to vary with latitude,
    ! g is the accerleration due to gravity, drag is the linear drag coefficient, density is the density of sea water,
    ! windo is the reference wind stress, tstep is the time step (s), L is spatial extent of the basin

    INTEGER, PARAMETER:: ntsteps=1, gridstep=10000, depth=1000, xlength=L/gridstep, ylength=L/gridstep
    ! ntsteps is the number of time steps to run for, gridstep is the distance between grid points, depth is the resting depth of the fluid
    ! xlenght and ylength are the number of grid points across the basin


    REAL, DIMENSION(xlength,ylength):: u, unext, usqrd, v, vnext, vsqrd, surf, surfnext, surfsqrd, x, y
    ! u is the velocity in the x direction, v is the velocity in the y direction, surf is the surface elevation
    ! x is the distance east, y is the distance north
    REAL:: energy, energytime(ntsteps) ! Energy is the total energy of the peturbation from the resting system
    INTEGER:: i, j, n
    ! i is the x component, j is the y compentent, n is the time index


    ! Assign values to x and y arrays
    x(1,:)=0
    DO i=2, xlength
    x(i,:)=x(i-1,:)+gridstep
    END DO

    y(:,1)=L
    DO j=2, ylength
    y(:,j)=y(:,j-1)-gridstep
    END DO

    ! Assign initial values of zero to the surface elevation, u and v
    DO j=1, ylength
    DO i=1, xlength
    surf(i,j)=0
    u(i,j)=0
    v(i,j)=0
    END DO
    END DO


    !----------------------------------------


    ! Set boundary conditions
    v(1:xlength,1)=0 ! Set the northward velocity at the north boundary to zero, ie the first row is zero across all columns
    v(1:xlength,ylength)=0 ! Set the northwards velocity to zero at the south boundary, ie the lasow is zero across all columns
    u(1,1:ylength)=0 ! Set the eastwards velocity to zero at the west boundary, ie the first column is zero at all rows
    u(xlength,1:ylength)=0 ! Set the eastwards velocity to zero at the east boundary,ie the last is zero across all rows


    !----------------------------------------


    DO n = 1, ntsteps
    DO j = 2, ylength-1
    DO i = 2, xlength-1

    IF (((n/2)*2)/=n) THEN ! Define the scheme for the odd times steps

    surfnext(i,j)= surf(i,j) - depth*tstep*(((u(i+1,j)+u(i,j))/gridstep) + &
    &((v(i,j+1)-v(i,j))/gridstep))

    unext(i,j)= u(i,j) + (fo+beta*y(i,j))*tstep*v(i,j) - g*tstep* &
    & ((surfnext(i,j) - surfnext(i-1,j))/gridstep) - drag*tstep*u(i,j) + &
    & ((windo*(-cos((pi*y(i,j))/L))/(density*depth)))*tstep

    vnext(i,j)= v(i,j) - (fo+beta*y(i,j))*tstep*unext(i,j) - &
    & g*tstep*((surfnext(i,j) - surfnext(i,j-1))/gridstep) - drag*tstep*v(i,j)

    ELSE ! Define the scheme for the even time steps

    surfnext(i,j)= surf(i,j) - depth*tstep*(((u(i+1,j)+u(i,j))/gridstep) + &
    &((v(i,j+1)-v(i,j))/gridstep))

    vnext(i,j)= v(i,j) - (fo+beta*y(i,j))*tstep*u(i,j) - &
    & g*tstep*((surfnext(i,j) - surfnext(i,j-1))/gridstep) - drag*tstep*v(i,j)

    unext(i,j)= u(i,j) + (fo+beta*y(i,j))*tstep*vnext(i,j) - g*tstep* &
    & ((surfnext(i,j) - surfnext(i-1,j))/gridstep) - drag*tstep*u(i,j) + &
    & ((windo*(-cos((pi*y(i,j))/L))/(density*depth)))*tstep


    END IF


    ! Switch arrays over for next step
    surf=surfnext
    u=unext
    v=vnext

    END DO
    END DO

    ! Square variables to calculate the total energy at this time step
    DO j=1, ylength
    DO i=1, xlength
    usqrd(i,j)=u(i,j)**2
    vsqrd(i,j)=v(i,j)**2
    surfsqrd(i,j)=surf(i,j)**2
    END DO
    END DO

    ! Calculate the total energy of the peturbation from rest
    energy = 0.5*density*(depth*((sum(usqrd)) + (sum(vsqrd)))) + g*(sum(surfsqrd))

    energytime(n)=energy ! Saves total energy into an array at each time step

    END DO


    ! Write results to a file
    OPEN(UNIT=55, FILE='project1_u.dat')
    DO j=1, ylength
    WRITE(55,*) u(:,j)
    END DO
    CLOSE(55)

    OPEN(UNIT=56, FILE='project1_v.dat')
    DO j=1, ylength
    WRITE(56,*) v(:,j)
    END DO
    CLOSE(56)

    OPEN(UNIT=57, FILE='project1_surf.dat')
    DO j=1, ylength
    WRITE(57,*) surf(:,j)
    END DO
    CLOSE(57)

    OPEN(UNIT=58, FILE='project1_energy.dat')
    DO n=1, ntsteps
    WRITE(58,*) energytime(n)
    END DO
    CLOSE(58)


    END PROGRAM project1


    Any help REALLY apprechiated.
     
  2. jcsd
  3. Feb 11, 2009 #2
    Try printing to check the values of variables.
    Also, check the values of xlength and ylength. Division of a a double by an integer may not be the same as the division of an integer by another.
     
  4. Feb 13, 2009 #3
    I have checked the variables and they are the correct values, but when I output the array values, such as u and y they have the correct number of elements but they are always only seven instead of fifty square and I cant understand why.This is the most recent top part of the program:

    IMPLICIT NONE


    ! Define variables

    REAL, PARAMETER:: fo=10.0**(-4), beta=10.0**(-11), g=10.0, drag=10.0**(-6), density=1000.0,&
    & windo=0.2, tstep=200.0, pi=3.1415926535, L=1000000.0, gridstep=20000.0, gridlength=L/gridstep
    ! fo is the reference coriolis parameter, beta is the beta plane approximation that allows the coriolis parameter to vary with latitude,
    ! g is the accerleration due to gravity, drag is the linear drag coefficient, density is the density of sea water,
    ! windo is the reference wind stress, tstep is the time step, L is spatial extent of the basin, gridstep is the distance travelled across
    ! a grid square/between grid points, gridlength is the number of grids across the basin

    INTEGER, PARAMETER:: ntsteps=1, depth=1000, xlength=gridlength, ylength=gridlength
    ! ntsteps is the number of time steps to run for, gridstep is the distance between grid points, depth is the resting depth of the fluid
    ! xlenght and ylength are the number of grid points across the basin

    REAL, DIMENSION(ylength+1):: y
    REAL, DIMENSION(xlength+1,ylength+1):: u=0, unext=0, usqrd=0, v=0, vnext=0, vsqrd=0, surf=0, surfnext=0, surfsqrd=0
    ! u is the velocity in the x direction, v is the velocity in the y direction, surf is the surface elevation
    ! x is the distance east, y is the distance north
    REAL:: energy, energytime(ntsteps) ! Energy is the total energy of the peturbation from the resting system
    INTEGER:: i, j, n
    ! i is the x component, j is the y compentent, n is the time index

    DO j=1, ylength
    DO i=1, xlength
    u(i,j)=0
    END DO
    END DO

    OPEN(UNIT=54, FILE='project1_uinit.dat')
    WRITE(54,900) u
    900 FORMAT (51F12.5)
    CLOSE(54)


    y(1)=L
    DO j=2, ylength+1
    y(j)=y(j-1)-gridstep
    END DO

    OPEN(UNIT=53, FILE='project1_y.dat')
    WRITE(53,900) y
    CLOSE(53)



    The format statement is in there after write because that was the only way I could get it to proint out in the correct proportions, but thats no good if its still running within the program in the wrong shape. I have totally run out of ideas, any help much apprechiated.
     
  5. Feb 13, 2009 #4
    If you have 7 squared instead of 50 squared, there seems to be something wrong with the value of xlength, ylength or gridlength.
    I have a try to make, it's a long shot, but some compilers don't do it correctly.
    In the code:
    Code (Text):
    REAL, PARAMETER:: fo=10.0**(-4), beta=10.0**(-11), g=10.0, drag=10.0**(-6), density=1000.0,&
    & windo=0.2, tstep=200.0, pi=3.1415926535, L=1000000.0, gridstep=20000.0, gridlength=L/gridstep
    gridlength is defined in the same line as L and gridstep.
    Some one-pass compilers are not able to update the values of L and gridstep defined earlier before defining gridlength, and as a result, gives an erroneous result.
    This error is becoming rare today as compilers are no longer required to make a one-pass or two-pass compilation. It's however worth a try by declaring and defining gridlength on the following (separate) line.
     
  6. Feb 14, 2009 #5
    I gave that a try but it still does it. Funny thing is when I use the debugger it says that the arrays are 51 by 51, as they should be, so I dont understand why when it outputs it to the file it it outputs in a 372 x 7 grid unless i put the formatting in?

    Also when I increase the number of timesteps from the one it currently is to say 100 time steps I get a floating point over flow. What is this and how do I stop it?
     
  7. Feb 14, 2009 #6
    I have put initial values of zero for u, v and surf for all array elements. However when surfnext is calculated the values are nonzero, this is the code for that section, I cant see how that doesnt end up as a zero value (as its meant to)

    ! Calculate the surface elevation
    DO j = 1, ylength
    DO i = 1, xlength

    surfnext(i,j)= surf(i,j) - depth*tstep*(((u(i+1,j)+u(i,j))/gridstep) + &
    &((v(i,j+1)-v(i,j))/gridstep))

    END DO
    END DO
     
  8. Feb 14, 2009 #7
    It looks like there is either a problem of overflow, or array out-of-bounds.

    Array out of bounds can cause writing into locations where the data does not belong, and can cause many headaches hard to detect. Fortunately many compilers have options for execution time array bounds check, and many later operating systems do hardware check in case the program tries to write in the memory space of neighbours.

    You can monitor the values of the variables using the debugger for the former case, and see if the compiler has an option for array bounds check during execution. If not, you will have to monitor the array subscript as well.
     
  9. Feb 14, 2009 #8
    I sorted out the initial error by assigning the arrays initial values of zero os the columns or rows not looped over had values, which was what was causing the out of bounds error. Only problems is having sorted out that problem Iv found loads more!!
     
  10. Feb 15, 2009 #9
    Bring them on, you only have to sort them out one by one!
    Make liberal use of your debugger, which is your friend under these circumstances.
    Logical errors can be sorted out only by yourself, as you know what you would like to do.
    Feel free to post the ones you cannot resolve.
    Good Luck!!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Error 112 /undef when running fortran code
  1. Running a Fortran code (Replies: 4)

Loading...