Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to terminate a do loop?

I am writing a very basic Fortran Code to create the Ising model. However, I am stuck at the very last step - repeat the calculation until the most stable condition is achieved.

do
!Calculation (omitted here)
!Making a decision
if (E1 /= -64) then             !not yet stable
    if(dE > 0.0) then       
    call seed
    call random_number(prob)        ! random generate a number which is 0 <= Prob <= 1      
    Print *, prob
        if(Prob < exp(-dE/T)) then
                        !do nothing as the flip was made
        else
        mat(b,c) = -mat(b,c)        !flip the sign back, i.e. reject the change; only accept with probability of exp(-dE/T)
        end if
    else
    end if                  !Since the sign has changed already, if dE<0, the move must be accepted. Therefore no additional move should be taken
else
end do
end if
end do

Apparently, Fortran doesn't like the second last end do statement as I didn't defined do at that particularly. All I want is to exit the do loop once E1 == -64.

like image 983
Kiss in the rain Avatar asked Mar 14 '23 23:03

Kiss in the rain


1 Answers

In Fortran you can exit a do loop at any time using the exit statement.

  do
    ...
    if (condition) exit
  end do

If your do loop is labelled, use the label in the exit statement too

outer: do
  inner: do
    ...
    if (condition) exit outer
  end do inner
end do outer

Fortran 2008 allows the exit statement for other constructs too.

like image 106
Vladimir F Героям слава Avatar answered Mar 20 '23 10:03

Vladimir F Героям слава