增加2D自我避免随机游走的步骤

时间:2017-04-23 16:20:43

标签: fortran

我正在尝试增加以下Fortran自行避免随机游走程序中的可能步骤数。

增加步数可以使方形平均距离更准确

我会提供您的解决方案和建议。

PROGRAM Two_dimensional_Self_Avoiding__Random_Walks

  implicit none

  integer, dimension(:,:), allocatable :: lattice 
  integer, dimension(1:46):: na
  integer :: i,x,y,xt,yt,id,step,xx, ns,n,ii,III,choice
  real :: r,dis,dis2,square,d,d2
  Logical :: terminate,newsite


  CALL RANDOM_SEED()
             ! intial values for end to end distance 


  read(*,*) choice 
if (choice == 1) then 

  print*, ' Enter ns and n '
  read(*,*) ns 

na = (/(III, III=5, 50, 1)/) 

  do ii= 1, 46

 dis = 0.0; dis2 = 0.0 

 n = na(ii)

  allocate(lattice(-n:n,-n:n)) 

  CALL walks()                          ! self avoiding walks
IF (ALLOCATED (lattice)) DEALLOCATE (lattice)

enddo 

elseif (choice == 2) then 


 print*, ' Enter ns and n '
  read(*,*) ns , n

dis = 0.0; dis2 = 0.0 
 allocate(lattice(-n:n,-n:n)) 
CALL walks()    

endif




CONTAINS
  SUBROUTINE walks

    DO i = 1,ns

    lattice = 0; x = 0; y = 0
    step = 0; terminate = .FALSE.


    DO WHILE ((.NOT. terminate) .AND. (step <= n))
    xt = x; yt = y
    xx = lattice(x+1,y)+lattice(x-1,y) &
    +lattice(x,y+1)+lattice(x,y-1)

    IF (xx == 4) THEN
    terminate = .TRUE.
    ELSE
    newsite = .FALSE.

    DO WHILE (.NOT. newsite)
    CALL RANDOM_NUMBER(r)
    id = INT(r*4.0)

    IF (id == 0) THEN
    x = xt + 1; y = yt
    ELSEIF (id == 1) THEN
    x = xt - 1; y = yt
    ELSEIF (id == 2) THEN
    x = xt; y = yt + 1
    ELSEIF (id == 3) THEN
    x = xt; y = yt - 1 
    ENDIF

    IF (lattice(x,y) == 0) newsite = .TRUE.

    ENDDO

    step = step + 1; lattice(x,y) = 1
    ENDIF



    write(7,*) x,y

    ENDDO



    write(10,*),step

    square = float(x**2+y**2)
    dis = dis + sqrt(square); dis2 = dis2 + square
    d = dis/ns; d2=dis2/ns




    ENDDO
    write(11,*), ns,n, d, d2
     print*, ns,n, d, d2
    END SUBROUTINE walks


END PROGRAM Two_dimensional_Self_Avoiding__Random_Walks

0 个答案:

没有答案