Silverfrost Forums

Welcome to our forums

Array definition and write problem

6 Oct 2011 2:43 (Edited: 6 Oct 2011 10:35) #9049

I am having a problem I define a 8x11 arrays FX(j,nrpm) etc I assign values in a double loop. But when I try to write the same array out to file, slightly further down the code, using another double loop reversing the order, using j and n as the loop variables it complains that they are undefined variables etc?? I am of course now using 11x8 in the reversal.

here is a subset of the code in question its part of a bigger subroutine loop where NRPM is incremented - note I am reversing the order its written out ie I want J tabled with NRPM (N) incremented, then stepping J+1 increment then NRPM incremented again.

DO J=1,8
DO M=2,512
IF(ABS(THETA(2,M)-(RPM*J/120)).LT.0.01)THEN
FX(J,NRPM)=ABS(GENG(31,M))
FY(J,NRPM)=ABS(GENG(32,M))
FZ(J,NRPM)=ABS(GENG(33,M))
MX(J,NRPM)=ABS(GENG(34,M))
MY(J,NRPM)=ABS(GENG(35,M))
MZ(J,NRPM)=ABS(GENG(36,M))
ENDIF
ENDDO
ENDDO    
IF(NRPM.EQ.11)THEN    
DO J=1,8

DO N=1,NRPM
ESP=RPMIN+(N-1)*RPMI  
EO=J*0.5        
  WRITE(*,*)ESP,EO,ESP*j/120,FX(J,N),FY(J,N),FZ(J,N),MX(J,N),MY(J,N),MZ(J,N)
ENDDO
ENDDO
ENDIF

Basically it does not recognise the FX(J,N) etc variables? error 112 reference to undefined variable etc...

6 Oct 2011 3:08 #9050

Where you assign values to FX as in FX(J,NRPM)=ABS(GENG(31,M)) the second index is fixed at NRPM so for the loop M=2,512 you are just continuously overwriting the same cell.

Whilst when you try to write out the array contents you are attempting to do this with the second index varying from 1 to NRPM. Cells with indeces 1 to NRPM-1 have not been populated, hence the error message.

6 Oct 2011 3:27 #9051

Quoted from JohnHorspool Where you assign values to FX as in FX(J,NRPM)=ABS(GENG(31,M)) the second index is fixed at NRPM so for the loop M=2,512 you are just continuously overwriting the same cell.

Whilst when you try to write out the array contents you are attempting to do this with the second index varying from 1 to NRPM. Cells with indeces 1 to NRPM-1 have not been populated, hence the error message.

Thanks I'll consider more what you write, but the NRPM is incremented outside of this section in a subroutine as as I think I tried to explain?

Further example here is an output at the foot of the define array variable:

FX(J,NRPM)=ABS(GENG(31,M))
FY(J,NRPM)=ABS(GENG(32,M))
FZ(J,NRPM)=ABS(GENG(33,M))
MX(J,NRPM)=ABS(GENG(34,M))
MY(J,NRPM)=ABS(GENG(35,M))
MZ(J,NRPM)=ABS(GENG(36,M))
PRINT *,j,nrpm, fz(j,nrpm)

The output is correct it assigns the right values it increments NRPM etc its just that I cannot reproduce them further down which is weird

6 Oct 2011 3:45 #9052

if i try to print out two Fz array values beyond the initial do loops its again says its undefined, so its losing its values not sure why?

FX(J,NRPM)=ABS(GENG(31,M))
FY(J,NRPM)=ABS(GENG(32,M))
FZ(J,NRPM)=ABS(GENG(33,M))
MX(J,NRPM)=ABS(GENG(34,M))
MY(J,NRPM)=ABS(GENG(35,M))
MZ(J,NRPM)=ABS(GENG(36,M))

! PRINT *,j,nrpm, fz(j,nrpm) - assigns ok here !! ENDIF ENDDO ENDDO print *,fz(1,1),fz(1,2) - undefined here !!!

7 Oct 2011 11:00 #9053

Your code is very difficult to understand.

Can you provide details of what you are trying to do (rather than code that doesn't work)?

*Sometimes, the only way to make progress is to throw away what you have and start again. *

Whatever the value of NRPM is (but actually it must be 11 to get the printout), you are only setting up one column of the arrays FX, FY etc.

You therefore get an error when you try to access the other columns since they are undefined.

7 Oct 2011 11:40 #9054

There is nothing in this block of code that increments the value of NRPM !

DO J=1,8 
DO M=2,512 
IF(ABS(THETA(2,M)-(RPM*J/120)).LT.0.01)THEN 
FX(J,NRPM)=ABS(GENG(31,M)) 
FY(J,NRPM)=ABS(GENG(32,M)) 
FZ(J,NRPM)=ABS(GENG(33,M)) 
MX(J,NRPM)=ABS(GENG(34,M)) 
MY(J,NRPM)=ABS(GENG(35,M)) 
MZ(J,NRPM)=ABS(GENG(36,M)) 
ENDIF 
ENDDO 
ENDDO 
7 Oct 2011 1:17 #9055

Quoted from JohnHorspool There is nothing in this block of code that increments the value of NRPM !

DO J=1,8 
DO M=2,512 
IF(ABS(THETA(2,M)-(RPM*J/120)).LT.0.01)THEN 
FX(J,NRPM)=ABS(GENG(31,M)) 
FY(J,NRPM)=ABS(GENG(32,M)) 
FZ(J,NRPM)=ABS(GENG(33,M)) 
MX(J,NRPM)=ABS(GENG(34,M)) 
MY(J,NRPM)=ABS(GENG(35,M)) 
MZ(J,NRPM)=ABS(GENG(36,M)) 
ENDIF 
ENDDO 
ENDDO 

Correct, it is beyond this LOOP its a subroutine etc a return then increments NRPM....believe me it does there is no problem there it goes from 1 to 11 correctly.

Its so weird I can get the arrays to keep the j values but it only seems to store the last NRPM value 11...why, when I seeded 8x11 at the start surely array values for both subscripts J,N should stay? I realise it starts to write out at when NRPM=11 but I am re-setting from N=1,NRPM etc to pick up all the array values...

Further I have created a simpler programme swopping the array order and it works..see here:

program Test REAL,dimension (15,15)::FX,FY,FZ NRPM=0 100 NRPM=NRPM+1 DO J=1,3 FX(J,NRPM)=0.2NRPM+J FY(J,NRPM)=0.5NRPM+J FZ(J,NRPM)=0.7NRPM+J
PRINT ,J0.5,NRPM
1000,FX(J,NRPM),FY(J,NRPM),FZ(J,NRPM) ENDDO print *
IF(NRPM.EQ.4)THEN DO J=1,3 DO N=1,4
WRITE(,)J0.5,N1000,FX(J,N),FY(J,N),FZ(J,N) ENDDO ENDDO STOP ENDIF GOTO 100 END

7 Oct 2011 1:41 #9056

If the code you have provided in your first post is in a subroutine then the values you set up for a given value of NRPM will be lost when the subroutine exits (depending on whether you used the SAVE attribute or SAVE statement or not), so when the next call is made with NRPM incremented an 'undefined' error will occur.

Can we see all the code or enough to work out what you have done?

We need to see where and how each variable is declared, and what the scope of each variable is.

Its almost certainly an error in the code, rather than a FTN95 error.

7 Oct 2011 1:51 #9057

In the last program you posted you have a STOP between the last END DO and the END IF.

But you don't have this in the first code!!

Perhaps you have started again!

7 Oct 2011 2:46 #9060

Quoted from davidb If the code you have provided in your first post is in a subroutine then the values you set up for a given value of NRPM will be lost when the subroutine exits (depending on whether you used the SAVE attribute or SAVE statement or not), so when the next call is made with NRPM incremented an 'undefined' error will occur.

Can we see all the code or enough to work out what you have done?

We need to see where and how each variable is declared, and what the scope of each variable is.

Its almost certainly an error in the code, rather than a FTN95 error.

I think you may be right because the only array value combinations that 'stay unaltered' are for J,11 ie valid for all J values but it only recognises the last NRPM increment which is 11 and its NRPM that gets incremented in the sub routine...I could cut and paste the code buts its massive can you suggest a save command I need to do etc

7 Oct 2011 2:49 #9061

Quoted from davidb In the last program you posted you have a STOP between the last END DO and the END IF.

But you don't have this in the first code!!

Perhaps you have started again!

Point taken but its not this that was simply a convenience for that program, but maybe this simple program without a subroutine is evidence again that is the SR thats re-setting things!!!

7 Oct 2011 2:57 #9062

Don't post it if its very big.

Try

real, save :: FX(10,11), FY(10,11), ...

etc but use the dimensions you have now.

or you can add the line

save FX, FY, ...

7 Oct 2011 3:07 #9063

I modified your program so it has a subroutine. Does your real program look like this.

SUBROUTINE XXX(NRPM)
INTEGER NRPM
REAL,dimension (15,15), SAVE ::FX,FY,FZ
DO J=1,3
FX(J,NRPM)=0.2*NRPM+J
FY(J,NRPM)=0.5*NRPM+J
FZ(J,NRPM)=0.7*NRPM+J
PRINT *,J*0.5,NRPM*1000,FX(J,NRPM),FY(J,NRPM),FZ(J,NRPM)
ENDDO
print *
IF(NRPM.EQ.4)THEN
DO J=1,3
DO N=1,4
WRITE(*,*)J*0.5,N*1000,FX(J,N),FY(J,N),FZ(J,N)
ENDDO
ENDDO
! Note RETURN IN HERE
RETURN
ENDIF
END SUBROUTINE XXX

program Test
NRPM=0
100 NRPM=NRPM+1
   CALL XXX(NRPM)
IF (NRPM < 4) GOTO 100
END
7 Oct 2011 3:24 #9064

Hi David if you were near by I'd hug you!!!

I have spent hours on this....yes your right I have got round it using common block etc, ok old fashioned I know but much of my programme is based on them. Have not looked at this programme for ages so a little rusty I guess....

Thanks again David have a nice weekend.

ps have just seen your save command might give that a whirl too it looks simpler than common block approach etc

7 Oct 2011 3:28 #9065

Good to know you have a way forward 😃

7 Oct 2011 3:34 #9066

Yes the save command alot more elegant....

Thanks again.

9 Oct 2011 1:17 #9070

In your original post, a more robust approach to finding the best values from the array GENG could be:

! defind for NRPM
!
DO J=1,8 
! find best value for M in the range 2:512
 m = 2
 x = RPM*J/120
 DO n=3,512 
  IF( ABS(THETA(2,n)-x) < ABS(THETA(2,M)-x) )  M = n
 end do
 FX(J,NRPM) = ABS(GENG(31,M)) 
 FY(J,NRPM) = ABS(GENG(32,M)) 
 FZ(J,NRPM) = ABS(GENG(33,M)) 
 MX(J,NRPM) = ABS(GENG(34,M)) 
 MY(J,NRPM) = ABS(GENG(35,M)) 
 MZ(J,NRPM) = ABS(GENG(36,M)) 
ENDDO 

I assume that NRPM steps from 1 to 11 outside this slice of code, so that all values of the arrays have been defined

John

9 Oct 2011 4:41 #9071

Quoted from JohnCampbell In your original post, a more robust approach to finding the best values from the array GENG could be:

! defind for NRPM
!
DO J=1,8 
! find best value for M in the range 2:512
 m = 2
 x = RPM*J/120
 DO n=3,512 
  IF( ABS(THETA(2,n)-x) < ABS(THETA(2,M)-x) )  M = n
 end do
 FX(J,NRPM) = ABS(GENG(31,M)) 
 FY(J,NRPM) = ABS(GENG(32,M)) 
 FZ(J,NRPM) = ABS(GENG(33,M)) 
 MX(J,NRPM) = ABS(GENG(34,M)) 
 MY(J,NRPM) = ABS(GENG(35,M)) 
 MZ(J,NRPM) = ABS(GENG(36,M)) 
ENDDO 

I assume that NRPM steps from 1 to 11 outside this slice of code, so that all values of the arrays have been defined

John

Hi John,

Not sure I follow above my code is merely stepping through to assign array values at discrete intervals that coinicide with 1/2,1,1.5,2,..etc from the 512 frequency spectra geng variables

Please login to reply.