      subroutine scalar_init ! Bernd Berg Mar 28 2009. 
C Initialization of scalar field MCMC simulation.
      include '../../Libs/Fortran/implicit.sta'
      character cd*1,clx*3,clt*3
      include '../../Libs/Fortran/constants.par'
      include '../../Libs/Fortran_par/lat.par'
      include '../../Libs/Fortran_par/mc.par'
      parameter(miuo=-iuo)
      include '../../Libs/Fortran_par/common_lat.f'
      include '../../Libs/Scalar_par/common_scalar.f'
      dimension ix(nd)
      ltest=.true.
      ltest=.false.
C     if(ltest) stop "scalar_init: ltest."
C
      write(iuo,'(/,2X,"scalar_init: ")')
      write(iuo,'(4x,"parameters (lat.dat,lat.par):")')
      write(iuo,'(6x,"lattice dimension:",I10)') nd
      write(iuo,'(6x,"# lattice sites:  ",I10)') ms
      write(iuo,'(6x,"nla:",8I6)') nla
      write(iuo,'(6x,"mlink,np:",2I12)') mlink,np
      ns=nsfun(nla,nd)
      if(ns.ne.ms) stop "scalar_init: lat.par and lat.dat mismatch."
      write(iuo,'(4x,"parameters (mc.par):")')
      write(iuo,'(6x,"nequi:         ",4I10)') nequi
      write(iuo,'(6x,"nrpt,nmeas,nsw:",3I10)') nrpt,nmeas,nsw
      write(iuo,'(6x,"xm,xm2,xmfactor =",5X,3F12.6)') xm,xm2,xmfactor
      a2=sqrt(two*xmfactor)
      write(iuo,'(6x,"a2=sqrt(2*xmfactor) =",F10.6)') a2
      write(iuo,'(6x,"phi^4 coupling =",3X,F12.6)') xla
      write(iuo,'(6x,"iuo,iud1,iseed1,iseed2:",2I4,2I7)') 
     &                iuo,iud1,iseed1,iseed2
      if(istart.lt.2) then
        write(iuo,'(4X,"Ordered start istart, xstart:",I3,F10.6)') 
     &                                istart, xstart
      else
        write(iuo,'(4X,"Disordered start istart, xstart:",I3,F10.6)') 
     &                                istart, xstart
      endif
C
      write(cd,'(I1.1)') nd
      write(clx,'(I3.3)') nla(1)
      write(clt,'(I3.3)') nla(nd)
c Prepare file name:
      write(cfile,'(A,"d",A,"x",A,"t")') cd,clx,clt
      write(iuo,'(4X,"Data filename: ",2A,".D")') cfln,cfile
C
      inquire(file=cfln//".bak",exist=lexist)
      if(lexist) then 
        write(iuo,'(" scalar_init: Continuation not implemented.")')
      stop "scalar_init: Continuation not implemented."
      endif
C
      call rmaset(miuo,iud1,iseed1,iseed2,"no file") 
      call ranmar(xr) ! First random number.
      write(iuo,'(4X,"Initial randon number xr = ",F12.6)') xr
      sacpt=zero      ! For acceptance rate.
      call lat_init(ms,nd,ipf,ipb,nla,ix) ! lattice.
      phisum=ms*xstart ! Initial value for ordered start.
      call scalar_start(phi,xmfactor,ms,istart,phisum) ! Start configuration.
      call scalar_act(phi,xmfactor,xla,ipf,ms,nd,
     &  sactnn,sact2,sact,phisum)  ! Action.
      write(iuo,'(4X,"Initial mean field =",8X,G15.6)') phisum/ms
      write(iuo,'(4X,"Action per site sact/ms =",3X,G15.6)') sact/ms
c     print*,phi
      sa_min=sact
      sa_max=sact
C
C Initialized lowest momentum arrays for Fourier transformations:
      write(iuo,'(4X,"First momentum FT arrays xkr, xki:")')
      do i=1,ml
        xkr(i)=cos((i-1)*(tpi/ml)) ! Real parts.
        xki(i)=sin((i-1)*(tpi/ml)) ! Imaginary parts.
        write(iuo,'(I6,2G15.6)') i,xkr(i),xki(i)
      enddo
C
      write(iuo,'(2X,"scalar_init: initialization done.",/)')
      return
      end
