Software Archive
Read-only legacy content
17061 Discussions

Common Statement

Intel_C_Intel
Employee
915 Views
How come if I put the same Common header (ex. "Common / bubspec / xlambda, xin, c, rbc, bub") in two different procedures, the values still change? Is there something I am forgetting to do?
0 Kudos
9 Replies
Steven_L_Intel1
Employee
915 Views
I don't understand what you mean by "the values still change" What behavior are you expecting? The idea of COMMON is that a common block is shared among all the program units which declare it. You must make sure that the common block is declared exactly the same way in each program unit.

Steve
0 Kudos
Intel_C_Intel
Employee
915 Views
My main goal is to use those variables declared in the common statement, as global variables. Is that the only way, or is there a simpler way to reach my goal?
0 Kudos
Steven_L_Intel1
Employee
915 Views
Well, that's one way. Another is to declare your variables in a MODULE which you then USE where necessary.

What behavior do you see that you don't expect?

Steve
0 Kudos
Intel_C_Intel
Employee
915 Views
I have the Common header in 2 different procedures, and I read the value in in the first procedure, and use it for calculations in the second. But when I go to use the variable in the second procedure, its value that was declared in the first procedure is lost, and is set to '0'. What am I doing wrong, or what am I forgetting to do?
0 Kudos
Steven_L_Intel1
Employee
915 Views
Without seeing your actual code, I don't know what you're doing wrong. Can you post a short (10-20 line) example that demonstrates the problem? Bracket your code in the post with
 and 
.

Steve
0 Kudos
Intel_C_Intel
Employee
915 Views
First Procedure

      INTEGER FUNCTION zref_TF_D1_CB(S)  
      !DEC$ attributes C::zref_TF_D1_CB  
      LOGICAL*1 S(1)  
      CHARACTER*256 S2  
      INTEGER*4 zref  
      Common / refqnt / zref  
      INCLUDE 'FGLUE.INC'  
      INCLUDE 'ADDGLBL.INC'  
      INCLUDE 'GUIDEFS.INC'  
      INCLUDE 'GUI.INC'  
      zref_TF_D1_CB = 1  
      Call TecUtilLockOn()  
      CALL FExtGetCharFromCString(S,S2,ILen)  
      READ(S2(1:ILen),*, IOSTAT=iErr) zref  
      Call TecUtilLockOff()  
      RETURN  
      END


Second Procedure

	Subroutine AssignData ()  
	Integer*4 rgas, unref, tobs, itwod, xref, yref, zref, iseed  
	common / refqnt / yref,zref,sigman,sigmat,pv,s,unref,Fr,we,re  
...  
	open(7,file='D:jpfCavatCavatlarry.inp',status='unknown')  
	write (7,*) xref  
	write (7,*) yref  
	write (7,*) zref  
	close (7)  
...  
0 Kudos
Steven_L_Intel1
Employee
915 Views
Ah, that explains it.

Your COMMON statement must be the same in every routine. A COMMON declares a shared memory region and the variables are laid out in the region in the order you list them in the COMMON statement. If you have different variable lists, the variables appear to be in different places.

Usually, one puts COMMON statements in an INCLUDE file and INCLUDEs it in each routine. As I mentioned earlier, the modern alternative is to have a MODULE which declares the variables, and then you USE the module where needed.

Steve
0 Kudos
Intel_C_Intel
Employee
915 Views
Read in Procedure
      INTEGER FUNCTION zref_TF_D1_CB(S) 
      !DEC$ attributes C::zref_TF_D1_CB 
      LOGICAL*1 S(1) 
      CHARACTER*256 S2 
      INTEGER*4 zref 
      Include 'Common.fi' 
      INCLUDE 'FGLUE.INC' 
      INCLUDE 'ADDGLBL.INC' 
      INCLUDE 'GUIDEFS.INC' 
      INCLUDE 'GUI.INC' 
      zref_TF_D1_CB = 1 
      Call TecUtilLockOn() 
      CALL FExtGetCharFromCString(S,S2,ILen) 
      READ(S2(1:ILen),*, IOSTAT=iErr) zref 
      Call TecUtilLockOff() 
      RETURN 
      END 


Write out Procedure

	Subroutine AssignData () 
	Include 'Common.fi' 
	open(7,file='D:jpfCavatCavatlarry.inp',status='unknown') 
	write (7,*) zref 
	close (7) 
	Return 
	End 


Common.fi

 	Common / turbulence / iturb   
	Common / Param / tobs, iseed 
	Common / bubspec / xlambda, xin, c, rbc, bub 
	Common / bubspec / bubcount 
	Common / refqnt / rref, uref, dref,vref,aref,pinf,tref,rgas,xref 
	common / refqnt / yref,zref,sigman,sigmat,pv,unref,Fr,we,re 
	Common / testing / IMax, JMax, KMax, NumZones, NumVars, Length 


I did what you said and put the common statements in the INCLUDE file and INCLUDEd it in each routine, but the variable is still changing. Ex. It reads in the value '8', and prints out ' 1.1210388E-44'

Sorry if I am just making something simple complicated
Jake
0 Kudos
Steven_L_Intel1
Employee
916 Views
Add explicit type declarations for all the COMMON variables in Common.fi and remove them, where they appear, in the routines. You have, for example, Zref declared as Integer in the Read In procedure, but it's not typed at all in the Write procedure, so it is REAL by default.

Get into the habit of putting IMPLICIT NONE just after the SUBROUTINE, FUNCTION or PROGRAM statement throughout your code, and explicitly declare everything. It will save you many headaches.

Steve
0 Kudos
Reply