      PROGRAM qmwalk
      IMPLICIT NONE
      REAL*8 Esum, Vave, Vref, binsize, ds, dt, psi(-500:500), x(2000)
      INTEGER IR_, N, N0, i, imcs, mcs, nequil
      DATA psi, x/3001*0.0D0/
      CALL GWopen(IR_, 0)
      CALL initial(x,N,N0,Vref,binsize,ds,dt,mcs,nequil,Esum)
      DO i = 1, nequil                      ! equilibration
         CALL walk(x,N,N0,Vref,Vave,ds,dt)
      END DO
      DO imcs = 1, mcs
         CALL walk(x,N,N0,Vref,Vave,ds,dt)
         CALL mydata(x,psi,N,Vref,Vave,Esum,imcs,binsize)
      END DO
      CALL plot_psi(psi,binsize)
      CALL GWquit(IR_)
      END
C
      SUBROUTINE initial(x,N,N0,Vref,binsize,ds,dt,mcs,nequil,Esum)
      IMPLICIT NONE
      REAL*8 width, x(2000), Vref, binsize, ds, dt, Esum, V
      REAL dmy, rnd
      INTEGER IR_, i, N, N0, mcs, nequil
      dmy = rnd(-1)
      WRITE(*,'(A,$)') 'desired number of walkers = '
      READ(*,*) N0
      WRITE(*,'(A,$)') 'step length = '
      READ(*,*) ds
      dt = ds*ds                            ! time step
      WRITE(*,'(A,$)') 'number of Monte Carlo steps per walker = '
      READ(*,*) mcs
      nequil = int(0.4D0*mcs)
*     choose initial number of walkers equal to desired number
      N = N0
*     choose initial positions of walkers at random
      width = 1                             ! initial width of region for walkers
      Vref = 0
      DO i = 1, N
         x(i) = (2*rnd(0) - 1)*width
         Vref = Vref + V(x(i))
      END DO
      Vref = Vref/N
      binsize = 2*ds                        ! binsize for psi array
      Esum = 0
      CALL GWclear(IR_, -1)
      WRITE(*,'(4A12)') 'MC steps', 'N', 'E', 'Vref'
      WRITE(*,*)
      END
C
      SUBROUTINE walk(x,N,N0,Vref,Vave,ds,dt)
      IMPLICIT NONE
      REAL*8 Nin, Vsum, dV, potential, x(2000), Vref, Vave, ds, dt, V
      REAL rnd
      INTEGER i, N, N0
      Nin = N                               ! # walkers at beginning of trial
      Vsum = 0
      DO i = Nin, 1, -1
         IF(rnd(0) .LT. 0.5D0) THEN
            x(i) = x(i) + ds
         ELSE
            x(i) = x(i) - ds
         END IF
         potential = V(x(i))                ! potential at x
         dV = potential - Vref
         IF(dV .LT. 0) THEN
            IF(rnd(0) .LT. -dV*dt) THEN
               N = N + 1
               x(N) = x(i)                  ! new walker
*              factor of 2 since two walkers at x
               Vsum = Vsum + 2*potential
            ELSE
               Vsum = Vsum + potential      ! only do old walker
            END IF
         ELSE
            IF(rnd(0) .LT. dV*dt) THEN
               x(i) = x(N)
               N = N - 1
            ELSE
               Vsum = Vsum + potential
            END IF
         END IF
      END DO
      Vave = Vsum/N                         ! mean potential
      Vref = Vave - (N - N0)/(N0*dt)        ! new reference energy
      END
C
      SUBROUTINE mydata(x,psi,N,Vref,Vave,Esum,imcs,binsize)
      IMPLICIT NONE
      REAL*8 x(2000), psi(-500:500), Vref, Vave, Esum, binsize
      INTEGER bin, i, N, imcs
*     accumulate data
      Esum = Esum + Vave
*     bin walkers
      DO i = 1, N
         bin = int(x(i)/binsize)
         psi(bin) = psi(bin) + 1
      END DO
      IF(mod(imcs,10) .EQ. 0) THEN
         WRITE(*, '(I12,I12,F12.3,F12.3)') 
     +            imcs, N, Esum/imcs, Vref
      END IF
      END
C
      SUBROUTINE plot_psi(psi,binsize)
      IMPLICIT NONE
      REAL*8 P, norm, pmax, sum, ymax, psi(-500:500), binsize
      INTEGER IR_, i, imax
      LOGICAL Ltmp1_
      i = 0
      pmax = psi(i)*psi(i)
      sum = pmax
      Ltmp1_ = .TRUE.
      DO WHILE(Ltmp1_)
         i = i + 1
         P = psi(i)*psi(i) + psi(-i)*psi(-i)
         IF(P .GT. pmax) pmax = P
         sum = sum + P
         Ltmp1_ = P .NE. 0
      END DO
      imax = i
      norm = sqrt(sum*binsize)
      ymax = sqrt(pmax)/norm
      CALL GWindow(IR_, REAL(-imax), REAL(-0.1D0*ymax), REAL(imax)
     +, REAL(1.1D0*ymax))
      CALL GWline(IR_, REAL(-imax), 0.0, REAL(imax), 0.0)
      DO i = -imax, imax
         CALL GWline(IR_, REAL(i-1), REAL(psi(i-1)/norm), REAL(i)
     +   , REAL(psi(i)/norm))
      END DO
      END
C
      FUNCTION V(x)
      IMPLICIT NONE
      REAL*8 V, x
      V = 0.5D0*x*x
      END
