Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array's Declaration 1

Status
Not open for further replies.

Arpene

Programmer
Apr 20, 2010
6
DE
Hi guys

I'm breaking my head with the following problem:

A main file and a subroutine (saved in another file) share a module. This module was defined in the main file and is used in the subroutine.
If I call, in the subroutine, the values of a matrix or array declared in the module, I have no problem up to an array. In this case the when I try to build the project I get the error "This name has not been declared as an array", altough it has been indeed declared in the module as an array.

With only these few information can you guy give some hint?

Thanks in advance
 
Show us your code please and give us the exact error message without interpreting it !

For the moment, I just suggest :
- to save the module in its own file
- to put the subroutine in a module
- to put in the main file only the main program.
 
Hi FJacq

Thanks for your reply.


#################################
This is the main file:
#################################


module workvar


implicit doubleprecision (A-H,O-Z)

Parameter N=100,
# Nstg=100,
# Rcte=8.3145

doubleprecision Psys,
# Tsysc,
# Tsys

Integer NC


Character(50) Simname,
# Compname(10)

dimension doubleprecision
# XIJ(N,Nstg)
dimension doubleprecision
# gamma(N),
# R(N),
# Q(N),
# A(N,N),
# Tao(N,N),
# cPsi(N),
# cPhi(N),
# Xcomp(N),
# cTermL(N),
# clngamcom(N),
# clngamres(N)



Parameter
# Z=10

endmodule workvar


c Begins the code


Program col_NEQM_Krishna

use workvar


Xcomp(1)=0.274
Xcomp(2)=0.1090
Xcomp(3)=1-(Xcomp(1)+Xcomp(2))

call openfiles
Call subread
call gammai


end program




subroutine subread

use workvar

call rdstar
read(100,*)Simname
call rdstar
read(100,*)NC
call rdstar

do i=1,NC
read(100,*)Compname(i)
end do


call rdstar
read(100,*)Psys
call rdstar
read(100,*)Tsysc
Tsys=Tsysc+273.15d0
call rdstar
do i=1,NC
read(100,*)index,R(i),Q(i)
end do
call rdstar

do i=1,nc
read(100,*)A(i,1),A(i,2),A(i,3)
end do

end subroutine subread


subroutine rdstar


character*1 ctest

do i1 = 1, 1000
read (100, '(a1)') ctest
if (ctest .ne. '*') goto 20
end do
20 backspace (100)
return
end



subroutine openfiles
open(100,file='X_Input001.txt') open(200,file='X_Output001.txt')

end



#################################
This is the subroutine:
#################################



Subroutine gammai

use workvar

implicit doubleprecision (A-H,O-Z)

Dimension sumpsitao1(N),sumpsitao2(N),sumpsitao3(N)




sumq=0.d0
sumr=0.d0

Do i=1,nc
sumq=sumq+Q(i)*Xcomp(i)
sumr=sumr+R(i)*Xcomp(i)
end do



Do i=1,nc
cPsi(i)=Q(i)*Xcomp(i)/sumq
cPhi(i)=R(i)*Xcomp(i)/sumr
end do



Do i=1,nc
cTermL(i)=(Z/2)*(R(i)-Q(i))-(R(i)-1.d0)
end do


do i=1,NC
do j=1,NC
Tao(i,j)=exp(-A(i,j)/(Rcte*tsys))
enddo
enddo


sumxl=0

do i=1,NC
sumxl=sumxl+xcomp(i)*cterml(i)
end do



do i=1,NC

term1=log(cphi(i)/xcomp(i))
term2=(z/2)*Q(i)*log(cpsi(i)/cphi(i))+cterml(i)
term3=-(cPhi(i)/xcomp(i))*sumxl

clngamcom(i)=term1+term2+term3

end do

do i=1,NC

sumpsitao1(i)=0
sumpsitao2(i)=0
sumpsitao3(i)=0

end do

do i=1,NC
do j=1,NC
sumpsitao1(i)=sumpsitao1(i)+cpsi(j)*tao(j,i)
end do
end do




do i=1,NC
do j=1,NC
do k=1,NC
sumpsitao3(k)=0
end do

do l=1,NC
sumpsitao3(l)=sumpsitao3(l)+cpsi(l)*tao(l,j)
end do

sumpsitao2(i)=sumpsitao2(i)+(cpsi(j)*tao(j,i)/sumpsitao3(j))

end do
end do




do i=1,NC
clngamres(i)=Q(i)*(1.d0-log(sumpsitao1(i))-sumpsitao2(i))
enddo



do i=1,NC

gamma(i)=exp(clngamres(i)+clngamcom(i))

enddo





do i=1,NC
write(*,*)'sumpsitao1',i,sumpsitao1(i)
end do

write(*,*)

do i=1,NC
write(*,*)'sumpsitao2',i,sumpsitao2(i)
end do
write(*,*)

do i=1,NC
write(*,*)'sumpsitao3',i,sumpsitao3(i)
end do
write(*,*)

do i=1,NC
write(*,*)'gamma',i,gamma(i)
end do
write(*,*)




endsubroutine


#################################
This is the input file (input.txt)
#################################

*======================================================================
*
* Name of simulation (Without spaces and slashes "/"):
*
*----------------------------------------------------------------------
Simulation-NEq-Distillation
*
*======================================================================
*
* Number of components.
*
*----------------------------------------------------------------------
3
*======================================================================
*
* Name of components (Without spaces and slashes "/"):
*
*----------------------------------------------------------------------
Acetone-[1]
Methanol-[2]
Water-[3]
*======================================================================
*
* System's Pressure [Pa].
*
*----------------------------------------------------------------------
101300
*======================================================================
*
* System's Temperature. [°C]
*
*----------------------------------------------------------------------
62.30d0
*======================================================================
*
* Parameters of UNIQUAC method.
*
*======================================================================
*
* Index R(I) Q(I)
*
*----------------------------------------------------------------------
1 2.5735d0 2.3360d0
2 1.4311d0 1.4320d0
3 0.9200d0 1.4000d0
*======================================================================
*
* Aij=(Uij-Uii)
*
*----------------------------------------------------------------------
00000.000 2122.249 4706.087
-673.605 0000.000 -1042.758
-394.757 6542.625 0000.000

#################################
And last but not least..this is the error!!
#################################

--------------------Configuration: teste_tips - Win32 Debug--------------------
Compiling Fortran...
D:\teste_tips\sub.for
D:\teste_tips\sub.for(103) : Error: This name has not been declared as an array. [GAMMA]
gamma(i)=exp(clngamres(i)+clngamcom(i))
--------^
Error executing df.exe.

teste_tips.exe - 1 error(s), 0 warning(s)


#################################
Notice that gamma has been declared in the module, but somehow it doesn't recognize it.


THANKS A LOT!!!


 
Your trouble comes from the declaration of arrays in the module :

Code:
      dimension doubleprecision    
     #                gamma(N)

The syntax is incorrect and does not create the array gamma but the array doubleprecisiongamma

To correct that, you just need to delete either the keyword DIMENSION or the keyword DOUBLEPRECISION. Personally, I would delete DIMENSION.

Now, after looking into your programming, I would add several advices in addition to the three ones of my previous post :
- IMPLICIT NONE instead of IMPLICIT DOUBLE PRECISION (A-H,O-Z) is much more efficient to avoid mistakes in variable names,
- use the free format (suffix .f90 for files) instead of the fixed one (.f or .for suffixes). With the fixed format, the space is not a delimiter. This is the reason why you have declared in fact an array with a very strange name.
 
Thanks for the hints. They were really healfull.
 
Hi FJacq

Another quick question:

What is the advantage of writing a subroutine or function inside a module? And Why "implicit none" is better than "implicit double precision" ?

Thanks

Artur
 
Developing functions and subroutines inside modules is one of the main improvements of F90 over F77.

A module has several goals :
- isolating associated things in one entity, the module,
- making these entities available for another part only if that part USE the module,
- enabling the compiler to check many things,
- factorizing declarations common to all subroutines and functions belonging to the module.

When a part (main program, other module, subroutine or function) uses a module, then automatically, the compiler is able to compare the calling statements with the definition of routines (they are provided by the *.mod files generated by the compiler when compiling modules). This feature deletes one of the main source of bugs of the FORTRAN-77 : a mismatch actual/dummy argument.

Additionally, the module replaces efficiently the COMMON and the BLOCK DATA.

So a well programmed F9x code is composed exclusively of modules plus one independent source file containing the main program.

About IMPLICIT NONE versus other implicit instructions, the reason again is simple : discovering mistakes as soon as possible (i.e. if possible during the compilation phase).

Look at the following example :

Code:
PROGRAM test
  IMPLICIT DOUBLE PRECISION (A-H,O-Z)
  INTEGER a(2),i
  a=0
  DO i=1,2
    a(j)=i+1
  ENDDO
  WRITE(*,*) a
END PROGRAM

Compile the code and run. The result will be very strange. I get, with ifort-11 on Linux :

Code:
$ ifort t.f90
$ ./a.out
           0           0


Let us replace now the implicit statement by IMPLICIT NONE and compile again. We get :

Code:
$ ifort t.f90
t.f90(6): error #6404: This name does not have a type, and must have an explicit type.   [J]
    a(j)=i+1
------^
compilation aborted for t.f90 (code 1)

Immediately, one find the mistake : the typo "j" instead of "i".

If you are in a hurry and you don't want to declare all the variables, you will surely prefer the first implicit statement. But this is always a very bad bet : do not forget that the programming time (writing instruction) is always negligible compared to the time needed for locating and correcting mistakes (more than 90% for me).

This is why all experienced programmers always introduce IMPLICIT NONE at the top of each module. This implicit statement will be applied to all subroutines and functions of the modules : you don't need to repeat !

In the same way, I rarely use "USE" statements within subroutines or functions : I group the USE statements at the top of each module. So the module has also another additional interest : reducing the number of instructions in avoiding repetitions.

A last advice with the USE statement : I suggest to precise the ONLY clause which enable to list all what you need from the used module. This is very convenient for the reader who immediately knows the origin of each symbol.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top