      subroutine locnats
      use module
      use mod_script
      IMPLICIT REAL*8(A-H,O-X,Z),logical*1(y)
      character(3) cc
      yw=.false.
!
! Number of density matrices
!
      if(ypiste) print*,'entree locnats'
      if(density=='CASDI') then
         nmat_locnats=nmat
      else if(density=='RODYALL') then
         nmat_locnats=netattot
      endif
      if(yw)print*,'nmat_locnats',nmat_locnats
!
! Mix with preceding Density Matrix
!
      if(alpha.ne.1.d0) call mixDM(nmat_locnats)
!
! locnats data
!
      call nameld(namel,SEUIL,'SEUIL',8)
      open(1,file='IN_locnats',status='unknown')
      write(1,'(3a)')" &locn prefix='",trim(prefix),"'"
      if(any(frozen(1:nsym)/=0))
     *write(1,'(a,8(i4,'',''))')' ngel=',frozen(1:nsym)
      if(any(delete(1:nsym)/=0))
     *write(1,'(a,8(i4,'',''))')' ndel=',delete(1:nsym)
!      if(yfermi_donne)
!     *write(1,'(a,8(i4,'',''))')' fermi=',fermi(1:nsym)
      write(1,'(a,i3)')' nmat=',nmat_locnats
      write(1,'(a)')' densmat='
      cc='000'
      do k=1,min(9,nmat_locnats)
         write(cc(3:3),'(i1)') k
         write(1,'(a)')' ''DENS'//cc//''''
      enddo
      do k=10,min(99,nmat_locnats)
         write(cc(2:3),'(i2)') k
         write(1,'(a)')' ''DENS'//cc//''''
      enddo
      if(density=='CASDI') then
         write(1,'(a,8(i3,'',''))')' netat=',netat(1:nmat)
      else if(density=='RODYALL') then
         write(1,'(a,8(i3,'',''))')' netat=',(1,k=1,nmat_locnats)
      endif
      if(density=='CASDI') then
         write(1,'(a,8(i3,'',''))')' metat=',(metat(k),k=1,netat(1))
      else if(density=='RODYALL') then
         write(1,'(a,8(i3,'',''))')' metat=',(1,k=1,netat(1))
      endif
!     do kk=2,nmat_locnats
!        write(1,'(a,8(i3,'',''))') (metat(k),k=sum(netat(1:kk-1)),
!    *   sum(netat(1:kk-1)))
!     enddo
      write(1,'(a,8(f4.2,'',''))')' coef=',coef
      write(1,'(a)')' orbv=''INPORB'',orbn=''LOCORB2'''
      !call aldon('LOCN',1)
      write(1,'(a,l1)')' yhier=',yhier
      write(1,'(a,l1)')' yreloc=',yreloc
      write(1,'(a,d20.12)')' alpha=',alpha
      write(1,'(a)')' /'
      if(iter==1) call display_data(1,'LOCNATS')
      close(1)
!
! locnats script
!
      write(cc,'(i3)') iter
      cc=adjustl(cc)
      open(10,file='xx_locnats',status='unknown')
      write(10,'(2a)') trim(locnat),
     *' < IN_locnats  '//r//' locnatout'//trim(cc)
      write(10,'(a)') 'cp LOCORB2 LOCORB2_'//trim(cc)
      if(r.ne.'#')write(10,'(a)') 'cp locnatout'//trim(cc)//
     *' LOCNAT.OUT'
      close(10)
!
! locnats exec
!
      call system('chmod +x xx_locnats')
      call system('./xx_locnats')
      if(ypiste) print*,'sortie locnats'
!
! Search maximal extradiagonal value of density matrix
!
      call maxvaldens(' locnatout'//trim(cc))
      return
      end
       subroutine maxvaldens(filout)
      use module
      IMPLICIT REAL*8(A-H,O-X,Z),logical*1(y)
      character(*) filout
      character(132) aa
      if(index(r,'#')/=0) return
      open(99,file=filout,status='old')
      do
         read(99,'(a132)') aa
         if(index(aa,'mat densite  valeur max hors mat block diag')>0)
     *   then
            aa(1:44)=' '
            read(aa,*) convdm
            exit
         endif
      enddo
      do
         read(99,'(a132)',end=1) aa
         if(index(aa,'mat densite  valeur max hors mat block diag')>0)
     *   then
            aa(1:44)=' '
            read(aa,*) convdm1
            convdm=max(convdm,convdm1)
         endif
      enddo
 1    continue
      close(99)
      end
         
      
       subroutine mixDM(nmat_locnats)
      use module
      use mod_script
      IMPLICIT REAL*8(A-H,O-X,Z),logical*1(y)
      character(3) cc
      character(8),save :: filDMr,filDMw
      allocatable dens1(:,:),dens2(:,:)
      yw=.false.
      if(ypiste) print*,'entree mixDM'
      norb=sum(isym(1:nsym))
!
! Mix density matrices of the current iterations with those of preceding
! iterations
!
      if(iter>1) then

         allocate(dens1(norb,norb))
         allocate(dens2(norb,norb))
         if(mod(iter,2)==0) then
            filDMw='DENS_old2'
            filDMr='DENS_old1'
         else
            filDMw='DENS_old2'
            filDMr='DENS_old1'
         endif
         open(99,file=filDMw,status='unknown',form='unformatted')
         open(96,file=filDMr,status='unknown',form='unformatted')

         do k=1,nmat_locnats

            write(cc,'(i3.3)') k
            open(97,file='DENS'//cc,status='old',form='unformatted')

!     read old density density matrix
            read(96) dens1

!     read current density matrix
            read(97) dens2

!     mix
            dens2=(1.d0-alpha)*dens1+alpha*dens2

!     write average density matrix...
!     ... as current DM to continue calculation
            rewind 97
            write(97) dens2
            close(97)

!     ... as old DM for next iteration
            write(99) dens2

         enddo

         deallocate(dens1)
         deallocate(dens2)
         close(99)
         close(96)

      else if(iter==1) then

         allocate(dens1(norb,norb))
         filDMw='DENS_old1'
         open(99,file=filDMw,status='unknown',form='unformatted')

         do k=1,nmat_locnats

            write(cc,'(i3.3)') k
            open(97,file='DENS'//cc,status='old',form='unformatted')
!  write DM as old DM for next iteration
!     read current density density matrix
            read(97) dens1
!     write it as old DM for next iteration
            write(99) dens1
            close(97)
         enddo
         deallocate(dens1)
      endif
      close(99)
      close(97)
      end
