Author Topic: Exit Loop  (Read 9528 times)

Alex77

  • New Member
  • *
  • Posts: 5
Exit Loop
« on: January 01, 2017, 09:32:24 AM »
Dear all,

Does anyone know if there is something similar to the flag ljump, that allows to skip the remaining commands in a Loop,Next  loop?
I want to implement a stopping criterion for a staggered solution scheme. Thanks a lot!

Cheers,

Alex

Prof. R.L. Taylor

  • Administrator
  • FEAP Guru
  • *****
  • Posts: 2649
Re: Exit Loop
« Reply #1 on: January 01, 2017, 02:03:13 PM »
There are many options to using staggers, thus, there is no built in method to exit all the staggers.

If you can describe the algorithm you are using we may have some ideas.  One is to write a user macro that monitors the behavior of the individual staggers and when all converge sets the loop counters to a maximum.  That is the way we exit a loop now.

Alex77

  • New Member
  • *
  • Posts: 5
Re: Exit Loop
« Reply #2 on: January 01, 2017, 11:18:38 PM »
Dear Prof. Taylor,

Thanks for your reply. My algorithm works like this

Code: [Select]
PARTition
 1,1,0
 0,0,1
 1,1,1

macr
 ...
 STAG,allo
 ...
 dt,,tt
 part,,1
 tran,expl
 mass lump
 form accel
 loop,step,km
  loop,ohne,ko
   part,,1
   tran,expl
   time,expl,tf,0.9
   loop,STAG,10
    STAG,set,0.0
    part,,1
    tran,centr
    loop,,1
     mati
     form,expl
    next
   part,,2
   tran,off
   loop,iter,15
    mati
    tang
    form
    solve
   next,iter
  Monolithic
  STAG,prin
  STAG,chec,1.0E-3    !should exit loop/stag if tolerance 1.0E-3 is reached
  next,stag
 next,ohne
next,step

end

The relevant part of the user macro STAG is

Code: [Select]
        if (pcomp(lct,'chec',4)) then
          call pgetd('USER5',beg1,len1,prec1,found1)
          if(found1) then
            if ((abs(hr(beg1)-hr(beg1+1))/abs(hr(beg1))).le.ctl(1)) then                     
              hr(beg1+1)=hr(beg1)   ! --> converged Etotal_old=Etotal
              ???                       ! Here I want to exit the STAG loop.
            else
              hr(beg1+1)=hr(beg1)   ! --> not converged Etotal_old=Etotal
            endif
          else
            write(iow,*),'USER5 NOT ALLOCATED - ERROR!'
          endif
        endif

STAG checks whether the solution is converged in the sense that the relative change of some global energy is small enough.
I am just not sure how to set the relevant loop counter to exit the STAG loop. Thanks for your help!


EDIT:

I have realized that the algorithm can actually be implemented by setting (and resetting) the flag ljump correctly:

Code: [Select]
loop,stag,100
  part,,1
  tran,centr
  loop,,1
    mati
    form,expl
  next
  part,,2
  tran,off
  loop,iter,15
   mati
   tang
   form
   solve
  next,iter
  PART,,3
  STAG,set,0.0
  RESId           
  STAG,chec,1.0E-3
  next,stag
  STAG,NOJU

and

   
Code: [Select]
     if (pcomp(lct,'chec',4)) then
          call pgetd('USER5',beg1,len1,prec1,found1)
          if(found1) then
            encur=hr(beg1)
            enold=hr(beg1+1)
            if ((abs(encur-enold)/abs(enold)).le.ctl(1)) then                     
              hr(beg1+1)=encur   ! --> converged Etotal_old=Etotal
              ljump=.true.
            else
              hr(beg1+1)=encur   ! --> not converged Etotal_old=Etotal
            endif
          else
            write(iow,*),'USER5 NOT ALLOCATED - ERROR4!'
          endif
        endif
               
        if (pcomp(lct,'noju',4)) then
          call pgetd('USER5',beg1,len1,prec1,found1)
          if(found1) then
           ljump=.false.
          else
            write(iow,*),'USER5 NOT ALLOCATED - ERROR4a!'
          endif
        endif

Seems to work so far. Thank you!
« Last Edit: January 02, 2017, 07:00:20 AM by Alex77 »

FEAP_Admin

  • Administrator
  • FEAP Guru
  • *****
  • Posts: 993
Re: Exit Loop
« Reply #3 on: January 02, 2017, 09:04:53 AM »
Alex,  Did you try google?!

The answer is 'exit', which will exit the current loop.  'cycle' is an other good command to know about; it skip the rest of the loop and continues with the next loop iteration.

Alex77

  • New Member
  • *
  • Posts: 5
Re: Exit Loop
« Reply #4 on: January 02, 2017, 11:14:48 AM »
I did... I am still not sure how the Fortran commands?  "exit" and "cycle" would help to solve my problem, but thank you anyway.

Prof. R.L. Taylor

  • Administrator
  • FEAP Guru
  • *****
  • Posts: 2649
Re: Exit Loop
« Reply #5 on: January 02, 2017, 01:25:22 PM »
Looking at your algoirthm I note you set the transient algorithm every time you enter a partition.  You should not need to do this feap should keep track of which algorithm belongs to each partition. (npart.f does this).

I will look more at your coding for the exit and see if we should build it into the next release.  In your case one partition is explicit and thus, I presume, it converges by definition.  So the only thing to monitor is the implicit part.  But if both were implicit then there could be a need for additional checks.

I hope all continues to work for you now.

aza

  • Jr. Member
  • **
  • Posts: 11
Re: Exit Loop
« Reply #6 on: January 20, 2017, 06:56:46 AM »
Dear Alex and everyone,

I also want to exit a LOOP-NEXT-loop and found your suggestion concerning this problem. In my case, it does not work...
In my procedure, I have a RMDL loop which should be exited if a stopping criterion is fulfilled:

Code: [Select]
!...
time
loop,rmdl,10
  loop,,12
    tang,,1
  next
  rmdl,,1,5,10       ! if stopping criterion is fulfilled exit loop here
next,rmdl
plot,wipe
!...

The stopping criterion is evaluated in the material subroutine, and if it is fulfilled, a common variable named maxloop is set to -1. So I tried to set ljump=.true. if maxloop=-1 in the user macro RMDL. This does not have any visible effect.
Is there anything else you did to make it work or did I misunderstand anything?

I looked at pmacr2 and tried to understand the "Conventional loop checks" there, but I am not sure where and how the variables ct(1,l) and ct(1,n) are set. They seem to be important when deciding if a loop is exited or not.

Does anyone have another hint how I could exit the RMDL loop?

Thanks,
aza

Prof. R.L. Taylor

  • Administrator
  • FEAP Guru
  • *****
  • Posts: 2649
Re: Exit Loop
« Reply #7 on: January 20, 2017, 08:35:50 AM »
Each LOOP-NEXT is assigned a level indicator "lv" (basically the entire batch set is a LOOP-NEXT at level = 1).  The LOOP command is assicated with a CT(1:3,n), where "n" is the number of the command in the BATCh set, and the NEXT is also associated with its CT(1:3,m) . So the data is organized as:

LOOP,,num  --> CT(1:3,n)
   .....
NEXT          --> CT(1:3,m)

We set the values in CT(2,m) to be the n (the location where the matching LOOP is located.  CT(1,n) is set to num the number of
times the loop is to be computed.  For each LOOP we also set  lvs(lv) = n and lve(lv) = m.  We are counting in CT(1,m) the number of times we reach the NEXT statement and testing if it is smaller than CT(2,n).  Thus the simplest way to extit the loop is merely to set CT(1,m) = CT(1,n).  You get the value of m from lvs(lv) and the value of n from lve(lv).

The management for jump is more complicated and not needed for your case. 

aza

  • Jr. Member
  • **
  • Posts: 11
Re: Exit Loop
« Reply #8 on: January 23, 2017, 01:29:09 AM »
Your explanation was very helpful to me, Prof. Taylor. Many thanks!

hamidattar

  • New Member
  • *
  • Posts: 4
Re: Exit Loop
« Reply #9 on: May 22, 2017, 06:52:22 AM »
Thank you Alex77 for the interesting question and thank you Prof. Taylor for the helpful answer.

Following Prof. Taylor's answer I could manage the problem by implementing the following subroutine:

Code: [Select]

  include 'ldata.h'
  include 'comnds.h'

  subroutine endLoop()
    ct(1, lve(lv)) = ct(1, lvs(lv))
  end subroutine endLoop

Calling 'endLoop()' will not *exit* the loop immediately, but the current loop will end after reaching its 'next' command.

Prof. R.L. Taylor

  • Administrator
  • FEAP Guru
  • *****
  • Posts: 2649
Re: Exit Loop
« Reply #10 on: May 22, 2017, 09:38:14 AM »
This seems a dangerous thing to use in general.  You may cause unpredictable errors in solution of problems using various looping strategies.

Maybe using the IF-ELSE-ENDI commands would be better?