      subroutine smetsweep ! Bernd Berg May 18 2009. 
C Gaussian heatbath sweep for scalar phi^4 field.
      include '../../Libs/Fortran/implicit.sta'
      include '../../Libs/Fortran/constants.par'
      include '../../Libs/Fortran_par/lat.par'
      include '../../Libs/Fortran_par/mc.par'   
      parameter(cutoff=ten**3,twelve=12*one)
      include '../../Libs/Fortran_par/common_lat.f'
      include '../../Libs/Scalar_par/common_scalar.f'
      samin=sact
      samax=sact
      acpt=zero
      if(xla.le.zero) then 
        if(xla.lt.zero) stop "smetsweep: error xla.lt.zero."
        if(xm2.le.zero) 
     &    stop "smetsweep: error xla.eq.zero.and.xm2.le.zero."
        dphi=four/sqrt(two*xmfactor)
      else
        xxla=max(xla,em12) ! To avoid warning meesages for xla=zero.
        q=-twelve/xxla
        p=xmfactor/xxla
        x01=-p/2+sqrt((p/2)**2-q)
        dphi=sqrt(x01)
      endif
      do is=1,ms ! Loop over sites.
        samin=min(samin,sact)
        samax=min(samax,sact)
        phinn=zero
        do id=1,nd
          phinn=phinn+phi(ipf(id,is))
          phinn=phinn+phi(ipb(id,is))
        enddo
        call smetupdt(phinn,xmfactor,xla,dphi,phi(is),phisum,
     &                sactnn,sact2,sact,acpt)
        if((sact/ms).gt.cutoff) goto 1
      enddo
      sacpt=sacpt+acpt/ms ! Acceptance rate for the sweep.
      sa_min=min(samin,sa_min)
      sa_max=min(samax,sa_max)
      return
1     write(iuo,'(/,"smetsweep routine: Action too large is =",I9)') is
      write(iuo,'(2X,"sact/ms,phisum/ms =",2G15.6)') sact/ms,phisum/ms
      stop "smetsweep routine: Action too large."
      end
