如何获取多个输入并为每个输入运行程序

时间:2017-04-08 16:49:32

标签: fortran

以下是自我避免随机游走的程序。该程序工作正常,但我需要做一个小修改,但我不知道如何。

目前,该计划会收到nns作为输入,然后计算距离(dis)。我希望程序能够收到多个n并计算每个n的距离。

当前输出示例

n = 100 ns = 100 dis = 10.8

我希望程序输出

  n = 100 ns = 100 dis = 10.8 

  n = 200 ns = 100 dis = 11.6

继续n的所有输入值。

这可以通过每次使用不同的n运行程序来完成,但我需要一次运行。

PROGRAM Two_dimensional_Self_Avoiding__Random_Walks

  implicit none

  integer, dimension(:,:), allocatable :: lattice 
  integer :: i,x,y,xt,yt,id,step,xx, ns,n
  real :: r,dis,dis2,square,d,d2
  Logical :: terminate,newsite
  print*, ' Enter ns and n '
  read(*,*) ns,n 
  allocate(lattice(-n:n,-n:n)) 

  CALL RANDOM_SEED()
  dis = 0.0; dis2 = 0.0              ! intial values for end to end distance 
  CALL walks()                          ! self avoiding walks
  dis = dis/float(ns); dis2 = dis2/float(ns)
  print*,ns,n,dis,dis2


CONTAINS
  SUBROUTINE walks


    DO i = 1,ns

    lattice = 0; x = 0; y = 0
    step = 0; terminate = .FALSE.
        !do ii = 1, n

    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(10,*),step
 !print*, x,y
    write(7,*) x,y
    ENDDO

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

    write(8,*) step, d, d2

!enddo
    ENDDO

    END SUBROUTINE walks


END PROGRAM Two_dimensional_Self_Avoiding__Random_Walks

0 个答案:

没有答案