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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with using "cycle"

Status
Not open for further replies.

zamaniM

Programmer
Jul 10, 2011
12
Hi.
This code includes an Iteration process. The problem is in the section 4 and using "cycle".with cycle the output files are empty,but with using GO TO they aren't empty but all outputs are zero.how should i use 'cycle' and why they are zero?



!**************************************************************** 1 *******************************************************************************************
REAL ::i,j,d_time
REAL ,DIMENSION ( 14 )::a_ROC
REAL ,DIMENSION (6:21,14)::OX_P,OX_diff_P,d_cum,ROC_P,RP_P
REAL ,DIMENSION ( 25,14 )::K_1,K_3
REAL ,DIMENSION ( 25,14 )::K_2,K_4,K_5,K_6,K_7
REAL ,DIMENSION ( 6:21,14)::Initial_NO,Initial_NO2,Initia_oz
REAL ,DIMENSION (6:21,14)::OZ_P,NO_P,NO2_P,OZ_P_NEW,NO_P_NEW,NO2_P_NEW
REAL ,DIMENSION (7:21,14)::Er_NO,Er_NO2,Er_Oz,POS_Er_NO,POS_Er_NO2,POS_Er_Oz, Relative_POS_NO, Relative_POS_NO2, Relative_POS_oz, SUM_Relative
!****************************************************************************************************************************************************************


d_time=60

!******************************* 2 ***********************************
OPEN(1,file='k_1.txt',status='old')
OPEN(2,file='K_2.txt',status='old')
OPEN(3,file='K_3.txt',status='old')
OPEN(4,file='K_4.txt',status='old')
OPEN(5,file='K_5.txt',status='old')
OPEN(6,file='K_6.txt',status='old')
OPEN(7,file='K_7.txt',status='old')
OPEN(8,file='a_ROC.txt',status='old')
OPEN(9,file='Initial Try NO.txt',status='old')
OPEN(10,file='Initial Try NO2.txt',status='old')
OPEN(11,file='Initial Try oz.txt',status='old')
OPEN(12,file='d_cum.txt',status='old')
OPEN(13,file='ROC_Prediction.txt')
OPEN(14,file='RP_Prediction.txt')
OPEN(15,file='NO_P.txt')
OPEN(16,file='NO2_P.txt')
OPEN(17,file='OZ_P.txt')
OPEN(18,file='Er_NO.txt')
OPEN(19,file='Er_NO2.txt')
OPEN(20,file='Er_Oz.txt')
OPEN(21,file='POS_Er_NO.txt')
OPEN(22,file='POS_Er_NO2.txt')
OPEN(23,file='POS_Er_Oz.txt')
OPEN(24,file='SUM_Relative.txt')



!**********************************************************************

!******************************** 3 **********************************
DO i=1,25
READ(1,*) (K_1(i,j),j=1,14)
READ(2,*) (K_2(i,j),j=1,14)
READ(3,*) (K_3(i,j),j=1,14)
READ(4,*) (K_4(i,j),j=1,14)
READ(5,*) (K_5(i,j),j=1,14)
READ(6,*) (K_6(i,j),j=1,14)
READ(7,*) (K_7(i,j),j=1,14)
END DO

DO j=1,14
READ(8,*) (a_ROC(j))
END DO

DO i=6,21
READ(9,*) (Initial_NO(i,j),j=1,14)
READ(10,*)(Initial_NO2(i,j),j=1,14)
READ(11,*)(Initia_oz(i,j),j=1,14)
END DO

DO i=6,21
READ(12,*)(d_cum(i,j),j=1,14)
END DO

DO i=6,21
DO j=1,14
NO_P_NEW(i,j)=Initial_NO(i,j)
NO2_P_NEW(i,j)=Initial_NO2(i,j)
OZ_P_NEW(i,j)=Initia_oz(i,j)
END DO
END DO
!******************************************************************************


!************************************************************* 4 ****************************************************************************
DO j=1,14
OX_P(6,j)=OZ_P_NEW(6,j)-(2*NO_P_NEW(6,j))-NO2_P_NEW(6,j)
OX_diff_P(6,j)=0
ROC_P(6,j)=0
RP_P(6,j)=0
END DO


DO j=1,14
DO i=7,21
DO
NO_P(i,j)= NO_P_NEW(i,j)
NO2_P(i,j)=NO2_P_NEW(i,j)
OZ_P(i,j)= OZ_P_NEW(i,j)
OX_P(i,j)=OZ_P(i,j)-(2*NO_P(i,j))-NO2_P(i,j)
OX_diff_P(i,j)= OX_P(i,j)- OX_P(i-1,j)
ROC_P(i,j)=OX_diff_P(i,j)/(a_ROC(j)*d_cum(i,j))
RP_P(i,j)=K_1(i,j)*ROC_P(i,j)/((K_2(i,j)*NO_P(i,j))+(2*K_6(i,j)*NO2_P(i,j)))

OZ_P_NEW(i,j)=(((k_3(i-1,j)*NO2_P(i-1,j))-(K_4(i-1,j)*NO_P(i-1,j)*OZ_P(i-1,j)))*d_time )+OZ_P(i-1,j)
NO_P_NEW(i,j)=(((K_3(i-1,j)*NO2_P(i-1,j))-(K_4(i-1,j)*NO_P(i-1,j)*OZ_P(i-1,j))-(K_2(i-1,j)*RP_P(i-1,j)*NO_P(i-1,j)))*d_time)+NO_P(i-1,j)
NO2_P_NEW(i,j)=(((K_2(i-1,j)*RP_P(i-1,j)*NO_P(i-1,j))+(K_2(i-1,j)*NO_P(i-1,j)*OZ_P(i-1,j))-(K_3(i-1,j)* NO2_P(i-1,j))-(K_6(i-1,j)*RP_P(i-1,j)* NO2_P(i-1,j))-(K_7(i-1,j)*RP_P(i-1,j)*NO2_P(i-1,j)))*d_time)+NO2_P(i-1,j)

Er_NO(i,j)=NO_P_NEW(i,j)-NO_P(i,j)
Er_NO2(i,j)=NO2_P_NEW(i,j)-NO2_P(i,j)
Er_Oz(i,j)=OZ_P_NEW(i,j)-OZ_P(i,j)

POS_Er_NO(i,j)=ABS(Er_NO(i,j))
POS_Er_NO2(i,j)=ABS(Er_NO2(i,j))
POS_Er_Oz(i,j)=ABS(Er_Oz(i,j))

Relative_POS_NO(i,j)=(Er_NO(i,j))/NO_P_NEW(i,j)
Relative_POS_NO2(i,j)=(Er_NO2(i,j))/NO2_P_NEW(i,j)
Relative_POS_oz(i,j)=(Er_Oz(i,j))/OZ_P_NEW(i,j)

SUM_Relative(i,j)=Relative_POS_NO(i,j)+Relative_POS_NO2(i,j)+ Relative_POS_oz(i,j)

IF( SUM_Relative(i,j)>1 ) CYCLE
NO_P(i,j)= NO_P_NEW(i,j)
NO2_P(i,j)=NO2_P_NEW(i,j)
OZ_P(i,j)= OZ_P_NEW(i,j)

END DO
END DO
END DO

!********************************************************************************************************************************************************

DO i=6,21
WRITE(13,"(14(1x,1pe14.7))")(ROC_P(i,j),j=1,14)
WRITE(14,"(14(1x,1pe14.7))")(RP_P(i,j),j=1,14)
WRITE(15,"(14(1x,1pe14.7))")(NO_P(i,j),j=1,14 )
WRITE(16,"(14(1x,1pe14.7))")(NO2_P(i,j),j=1,14)
WRITE(17,"(14(1x,1pe14.7))")(OZ_P(i,j),j=1,14 )
END DO

DO i=7,21
WRITE(18,"(14(1x,1pe14.7))")(Er_NO(i,j),j=1,14)
WRITE(19,"(14(1x,1pe14.7))")(Er_NO2(i,j),j=1,14)
WRITE(20,"(14(1x,1pe14.7))")(Er_Oz(i,j),j=1,14 )
WRITE(21,"(14(1x,1pe14.7))")(POS_Er_NO(i,j),j=1,14)
WRITE(22,"(14(1x,1pe14.7))")(POS_Er_NO2(i,j),j=1,14)
WRITE(23,"(14(1x,1pe14.7))")(POS_Er_Oz(i,j),j=1,14 )
WRITE(24,"(14(1x,1pe14.7))")(SUM_Relative(i,j),j=1,14)

END DO


END PROGRAM Chemical_Production


 
As far as I understand your code, the statement

IF( SUM_Relative(i,j)>1 ) CYCLE

just terminates the execution of the inner loop for this cycle. That means, the loop will just skip execution of the next three statements and then begin the next cycle.

As I do not see any condition that would end the inner loop, this will cycle until you abort the program and would of course never reach the last two loops where you write your results.

I assume, what you wanted to achieve was to terminate this inner loop altogether when the condition in the if-clause is met. If so, you should use EXIT instead of CYCLE. Then the loop starting

DO i=7,21

would continue with the next i.

If you want to close termination of the three loops alltogether when the if-clause is met - that is what you do when you use goto with an adress outside of the loops - you should name your loops and use this name in the exit statement:

loop_1: do j = 1, 14
loop_2: do i = 7, 21
loop_3: do
.
.
if (condition) exit loop_1
.
.
enddo loop_3
enddo loop_2
enddo loop_1


Norbert

The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Dear Gummibaer,
Thank for your help. In fact , I want to correct initial try of OZ _p, NO_p and NO2_P from OZ _p_NEW ,NO _p_NEW and NO2 _p_NEW. So, the 3th loop (based on your nomination) should continue for each I and j until SUM_Relative(i,j) become less than 1.ٍElse,it should continue with OZ _p_NEW ,NO _p_NEW and NO2 _p_NEW. Then 2 other loops should start and repeat this procedure. I don't want close 3 loop together .also ,I've used EXIT instead of CYCLE, but I've had still empty outputs .So, what is the solution?

DO j=1,14
DO i=7,21
DO
.
.
IF( SUM_Relative(i,j)<1 ) EXIT
NO_P(i,j)= NO_P_NEW(i,j)
NO2_P(i,j)=NO2_P_NEW(i,j)
OZ_P(i,j)= OZ_P_NEW(i,j)
END DO
END DO
END DO
 
Does your prog terminate as you intend it to ? Or do you have to abort execution ? Reason for my question: if SUM_Relative () does not converge to a value below 1, the inner loop will never terminate and your prog never reach the read-statements.
I would recommend that you put the open-statements for your outputfiles (BTW: do you really need every value in a differeferent file ??) after the do-loops. That way you can check if your iteration came to an end by checking if the files were created.

Norbert


The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
i change the condition value and output files are not empty, but they are irrational. I should change condition term.
thank so much.
 
Maybe one tip:
you could establish a counter for the innermost loop and terminate execution after a reasonable number of cycles

Code:
integer j
.
.
.
do ...
    do ...
       j = 0                                           ! nullify counter for every new start of the inner loop
       do
          j = j + 1                                    ! count the cycles
          if (j .gt. 1000) then                        ! terminate with errormessage after too many loops
             write (somewhere, *) 'iteration terminated'
             exit
          endif
          .
          .
          if (condition met) exit
       enddo
    enddo
enddo

The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Thanks dear Gummibaer. Your guidances were so helpful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top