Error 112 /undef when running fortran code

In summary, the program is attempting to simulate wind-driven circulation in a closed basin with a western boundary current. It uses the Stommel (1948) model and Arakawa C grid with a mixed explicit-implicit time scheme. The program defines various variables, including fo, beta, g, drag, density, windo, tstep, pi, L, gridstep, gridlength, ntsteps, depth, xlength, ylength, and y. It also sets boundary conditions and calculates the total energy of the perturbation from rest. The program outputs various results to files. The current issue is that when outputting array values, such as u and y, they only have seven elements instead of the expected fifty square.
  • #1
freja
7
0
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.
 
Technology news on Phys.org
  • #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.
 
  • #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 can't 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 traveled 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 that's no good if its still running within the program in the wrong shape. I have totally run out of ideas, any help much apprechiated.
 
  • #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:
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.
 
  • #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 don't 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?
 
  • #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 can't see how that doesn't 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
 
  • #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.
 
  • #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!
 
  • #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!
 

What is error 112/undef in fortran code?

Error 112/undef, also known as "undefined error," is a common error in fortran code that occurs when a variable or subroutine is not defined or declared before it is used in the code. This can be caused by a typo, missing declaration, or incorrect syntax.

How can I fix error 112/undef in my fortran code?

To fix error 112/undef, you will need to identify the variable or subroutine that is causing the error and make sure it is properly declared before it is used in the code. Check for any typos or missing declarations, and make sure the syntax is correct. Once the variable or subroutine is properly defined, the error should be resolved.

Why does error 112/undef occur in fortran code?

Error 112/undef occurs in fortran code when a variable or subroutine is not defined or declared before it is used. This can happen due to human error, such as a typo, or incorrect syntax in the code. It is important to carefully check and debug your code to prevent this error from occurring.

Is there a way to prevent error 112/undef in fortran code?

Yes, there are a few ways to prevent error 112/undef in fortran code. First, it is important to carefully check and debug your code before running it. Make sure all variables and subroutines are properly declared and defined. Additionally, using modern fortran compilers and following coding best practices can help prevent this error from occurring.

Can error 112/undef be caused by other factors besides coding errors?

While error 112/undef is typically caused by coding errors, it can also be caused by external factors such as system or memory limitations. If you have ruled out any coding errors, it is possible that the error is caused by limitations in your system. You may need to optimize your code or increase memory allocation to resolve this issue.

Similar threads

  • Programming and Computer Science
Replies
4
Views
500
  • Programming and Computer Science
Replies
1
Views
916
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
5K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
7
Views
13K
Back
Top