我正在编写一个非常基本的Fortran代码来创建Ising模型.但是,我陷入了最后一步 - 重复计算直到达到最稳定的状态.
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
显然,Fortran并不喜欢最后一个end do
声明,因为我没有do
特别定义.我想要的只是退出do
循环一次E1 == -64
.
在Fortran中,您可以使用该exit
语句随时退出do循环.
do ... if (condition) exit end do
如果标记了do循环,也请使用exit语句中的标签
outer: do inner: do ... if (condition) exit outer end do inner end do outer
Fortran 2008也允许其他构造的exit语句.