Fortran Problem with initial state in FORTRAN

AI Thread Summary
The discussion revolves around a Fortran code that encounters a compilation error, specifically error 112, which indicates a reference to an undefined variable or function. The user seeks assistance in resolving this issue. Key points include the identification of potential problems in the code, such as the use of the 'FMT=I' in the WRITE statement and the need for proper syntax in Fortran 95, including avoiding common blocks and line-numbered DO loops. Additionally, the importance of defining the function RANDOM_NORMAL_MOD is highlighted, as its absence could lead to the error. Suggestions are made to improve code efficiency by opening and closing files outside of loops. Ultimately, the user reports that they have resolved the issue, indicating a successful outcome from the advice received.
Jamil
Messages
8
Reaction score
0
I have created an initial state code with perodic boundary conditions and normal distribution of velocities. I not know where is the problem, when I compile the computer detect the 112 error. Could anyone help me please? Thanks!
the code is:
************************************************************
Code:
COMMON / BLOCK1 / RX, RY, RZ, VX, VY, VZ, PVX, PVY, PVZ
!En este fichero programaremos el estado inicial de nuestro sistema.
INTEGER (SELECTED_INT_KIND(2)) N !Número de moléculas
PARAMETER (N=125)
INTEGER (SELECTED_INT_KIND(4)) I !Átomo I-ésimo
REAL RX(N), RY(N), RZ(N) !Posiciones de los distintos átomos
REAL VX(N), VY(N), VZ(N) !Velocidades de los distintos átomos
REAL PVX(N), PVY(N), PVZ(N) !GAUSSIANA EVALUADA (PROBABILIDAD)
REAL VMEDIAX, VMEDIAY, VMEDIAZ
REAL VTOTAL2, ECINETICA !VTORAL2 corresponde al módulo de la velocidad media al cuadrado, ECINETICA es la energía cinética por partículas
REAL L
TRED=2.5
DENSIDAD=0.2
L=(N/DENSIDAD)**(1/3)
VMEDIAX=0
VMEDIAY=0
VMEDIAZ=0
VMEDIA2=0
ECINETICA=0

!El sistema de coordenadas cartesianas estará centrado en el cubo,
!de tal forma que las variables  RX, RY, RZ estarán dentro del intervalo
![-L/2,L/2]
   DO 100 I=1,N
   IF (RX(I)> L/2) RX(I)=RX(I)-L
  IF (RX(I)< -L/2) RX(I)=RX(I)+L 
   IF (RY(I)< L/2) RY(I)=RY(I)-L
  IF (RY(I)> -L/2) RY(I)=RY(I)+L
   IF (RZ(I)< L/2) RZ(I)=RZ(I)-L
  IF (RZ(I)> -L/2) RZ(I)=RZ(I)+L
100 ENDDO

!Las velocidades se medirán en unidades (epsilon/n)**0.5
!De este modo el argumento de la exponencial será la velocidad reducida al
!cuadrado entre la temperatura reducida
   DO 200 I=1,N
     VX(I)= RANDOM_NORMAL_MOD (-VX(I)/TRED,(TRED/2)**0.5)
     VY(I)= RANDOM_NORMAL_MOD (-VY(I)/TRED,(TRED/2)**0.5)
     VZ(I)= RANDOM_NORMAL_MOD (-VZ(I)/TRED,(TRED/2)**0.5)
  PVX(I)= (((2/TRED)**0.5)/(2*2.1416)**0.5)*EXP(-((VX(I))**2)/TRED)
  PVY(I)= (((2/TRED)**0.5)/(2*2.1416)**0.5)*EXP(-((VY(I))**2)/TRED)
  PVZ(I)= (((2/TRED)**0.5)/(2*2.1416)**0.5)*EXP(-((VZ(I))**2)/TRED)
  OPEN(10,FILE='DATOS.DAT',STATUS='OLD')
  WRITE (10,FMT=I)'PROBABILIDADESX' , PVX(I), VX(I)
  WRITE (10,FMT=I)'PROBABILIDADESY' , PVY(I), VY(I)
  WRITE (10,FMT=I)'PROBABILIDADESZ' , PVZ(I), VZ(I)
  CLOSE(10)
  VMEDIAX= VMEDIAX+VX(I)
  VMEDIAY= VMEDIAY+VY(I)
  VMEDIAZ= VMEDIAZ+VZ(I)
  VTOTAL2= VTOTAL2 + (VX(I))**2 + (VY(I))**2 + (VZ(I))**2
200 ENDDO
VMEDIA2=VMEDIA2/N
ECINETICA=0.5*VTOTAL2 !Es la energía cinética por partículas y por unidad de masa, la cual corresponde a la masa de las moléculas de nuestro sistema.
PRINT *, "El valor medio de la velocidad a lo largo del eje x es: ", VMEDIAX
PRINT *, "El valor medio de la velocidad a lo largo del eje Y es: ", VMEDIAY
PRINT *, "El valor medio de la velocidad a lo largo del eje Z es: ", VMEDIAZ
PRINT *, "El valor de la energía cinética media por partículas y por unidad de masa es: " , ECINETICA

END
************************************************
Thank you!
 
Technology news on Phys.org
Jamil said:
when I compile the computer detect the 112 error.

What is a 112 error? Or at least, which compiler are you using?

(The meaning of error codes depends on which compiler is being used.)
 
error 112: reference to undefined variable, array element or function result.
Thank you jtbell!
 
I don't get such an error when trying to compile your code. The only problem is the use of 'FMT=I' in WRITE (10,FMT=I).
 
the problem is in :
Code:
IF (RX(I) >  L*0.5) RX(I)=RX(I)-L
IF (RX(I) < -L*0.5) RX(I)=RX(I)+L
IF (RY(I) >  L*0.5) RY(I)=RY(I)-L
IF (RY(I) < -L*0.5) RY(I)=RY(I)+L
IF (RZ(I) >  L*0.5) RZ(I)=RZ(I)-L
IF (RZ(I) < -L*0.5) RZ(I)=RZ(I)+L
but I don't know why.
o_O
 
You appear to be using a function named RANDOM_NORMAL_MOD. Where is it defined?
 
are you using Fortran 77? Because I don't think " < " is part of it...you would need to use " .LT. ", instead.

Please indicate what compiler you are using and please copy and paste the error message, a few lines if need be.
 
I am using FORTRAN 95, and the error message is: ''Error 112, reference to undefined variable, array element or function result (/UNDEF)''
Thanks for your interest! :smile:
 
The function named RANDOM_NORMAL_MOD is defined in other page.
 
  • #10
Does the error message tell you which line of your code it refers to?
 
  • #11
Yeap...that is precisely why I explicitly asked Jamil to copy and paste the compiler output, it most probably prints a line with where it thinks the problem is and the following line with a caret " ^ " pointing to the variable or something.

Jamil:

If you are using Fortran 95, then, use Fortran 95:
  • Add proper "program pname" and corresponding "end program pname"
  • Stop using common blocks
  • Stop using using numbers to refer to lines. For example, do-loops like "DO 200 I=1,N " are no longer necessary. Do-loops used to be done with "do 200 . . . 200 continue" ; nowadays, it is done with " do . . . enddo " ... so, your use of "200 enddo" is in fact a mix of styles!
  • You do not need to open and close the file inside the loop...you can open it right before the loop and close it right after...much more efficient
  • When asking for help, you should follow instructions...if you have not solved the problem and you do not know how to interpret the compiler error message, do as I asked and paste the compiler output and see if we can help further. When pasting, make sure to also paste the command line you are using to compile...you keep saying RANDOM_NORMAL_MOD is somewhere else...where? and how does this program know about it?
 
  • Like
Likes Mark44
  • #12
gsal said:
Yeap...that is precisely why I explicitly asked Jamil to copy and paste the compiler output, it most probably prints a line with where it thinks the problem is and the following line with a caret " ^ " pointing to the variable or something.

Jamil:

If you are using Fortran 95, then, use Fortran 95:
  • Add proper "program pname" and corresponding "end program pname"
  • Stop using common blocks
  • Stop using using numbers to refer to lines. For example, do-loops like "DO 200 I=1,N " are no longer necessary. Do-loops used to be done with "do 200 . . . 200 continue" ; nowadays, it is done with " do . . . enddo " ... so, your use of "200 enddo" is in fact a mix of styles!
  • You do not need to open and close the file inside the loop...you can open it right before the loop and close it right after...much more efficient
  • When asking for help, you should follow instructions...if you have not solved the problem and you do not know how to interpret the compiler error message, do as I asked and paste the compiler output and see if we can help further. When pasting, make sure to also paste the command line you are using to compile...you keep saying RANDOM_NORMAL_MOD is somewhere else...where? and how does this program know about it?
Very good points, all, and especially the advice to move the file open/close outside the loop. It is very inefficient to open and close the file 125 times.
 
  • #13
Thank you for you help! I have solved the problem! :smile:
 

Similar threads

Back
Top