      subroutine diff1(b,ab)
      parameter(maxsym=8,maxorbcas=16,maxda3=300)
      include 'maxorb.prm'
      IMPLICIT REAL*8(A-H,O-X,Z),logical*1(y)
c
c    |-*-*-*-*-*-*-*-|-*-*-*-*-*-*-*-|
c    |       b       |       ab      |
c
      common/dim/iadri ; integer(8) iadri
      common/opt/ihab,calcul
      character*3 calcul
      common/sym/ itsym(maxorb*2),its(maxsym,maxsym),nsym
      common /orb/ enuc,eref,norb,noca,nocb
     *,iorb(2*maxorb),ninac,nvirt,noca0
      dimension ntv(maxorbcas+2),npv(maxorbcas+2)
      dimension ica(maxorbcas),icb(maxorbcas),it(2),ip(2)
      common/l40/ hmono(maxorb*(maxorb+1)/2),aj(maxorb*(maxorb+1)/2)
     *,ak(maxorb*(maxorb+1)/2),f(maxorb*(maxorb+1)/2),e0
     *,ajk(maxorb*(maxorb+1)/2),escf
      character*4 hab
      common/infdav/ twcas,alam(maxda3),tau,twvec,twess,sint,sint4,sh
     *,hab,N,nvec,ngao,niter,iprec,lvec(maxda3),nfrag,ipresc,irest
     *,jrest
     *,iunit,ihprec,ihvec,ifiess,NESSAI,nf8,maxda2,bidia,ywvec,ytana
     *,yhmono,ydens,ywcas
      logical*1 bidia
      common/adr/iadr0(maxsym),nbint
      dimension b(iadri),ab(iadri)
      common /prep/ nca0,ncb0,ic0(maxorbcas),icamun(maxorb+maxorb)
     *,numt(maxorb),nump(maxorb),nv
     *,gener,yoc(maxorb+maxorb),yrefc(maxorbcas),yac(maxorb)
     *,ygmono,ygddci,ygddc2,ygcas,ygtous
      character*4 gener
      common/test/itest(100)
      common /cs/ noac,nelac,numac(maxorbcas),itype(maxorb),is0,ms2
c
c-----------------------------------------------------------
c     elements de matrice a une difference
c-----------------------------------------------------------
c
      jcpu=itemps(idum)
      yw=.false.
      yw1=.false.
      if(itest(16).eq.1.and.nelac.ge.2.and.calcul.eq.'MD') goto 100
      if(yw)write(6,*)'debut diff1',norb
      kdet=0
      call rew61
 1    continue
c      read(61,end=100)ne
c      if(ne.ne.0) read(61)(ntv(k),k=1,ne),(npv(k),k=1,ne)
      call venondloc(ne,ntv,npv,ierr)
      if(ierr.eq.1) goto 100
      kdet=kdet+1
      call trad(ne,ntv,npv,nca,ica,ncb,icb,nt,it,ispt,np,ip,ispp,ierr)
      coef=1.d0
      call adrdet
     *(nca,ica,ncb,icb,nt,it,ispt,np,ip,ispp,iadr1,
     *mhdum,mtdum)
      if(iadr1.ne.kdet) then
         print*,'diff1 kdet,iadr1',kdet,iadr1
         stop
      endif
      b0=b(kdet)
      if(yw)write(6,*)' di de depart',(ntv(k),k=1,ne),'  ',
     *(npv(k),k=1,ne)
      if(yw)write(6,*)'1 diff   adresse du di de depart',iadr1
c     write(68,*)'1 diff   adresse du di de depart',iadr1
      if(yw)write(6,*)'avant diff0 '
      if(calcul.eq.'M')
     *call diff0(nca,ica,ncb,icb,nt,it,ispt,np,ip,ispp,iadr1,e)
      if(yw)write(6,*)'apres diff0 '
      if(itest(16).eq.1.and.nelac.ge.2) goto 1
      if(itest(16).eq.1.and.nelac.eq.0) then
         if(ne.eq.1) then
c--         interaction mono/fond
            b0=b(kdet)
            mini=iorb(ntv(1))
            maxi=iorb(npv(1))
            a=f(maxi*(maxi-1)/2+mini)
            isgn=1-2*mod(noca-mini,2)
            if(calcul.eq.'M') then
      if(yw)write(6,*)'avant stover ds diff1 '
               call stover(kdet,1,isgn*a)
      if(yw)write(6,*)'apres stover ds diff1 '
            else
               aa=a*isgn
               ab(1)=ab(1)+aa*b0
               ab(kdet)=ab(kdet)+aa*b(1)
            endif
         endif
         goto 1
      endif
      if(itest(16).eq.1.and.nelac.eq.1) then
c--         interactions <..a|..b> ou <..A|..b> ou <..A|..B>
c           on doit calculer l'int F
c--         interactions <iCa|iCb> ou <iCA|iCb> ou <iCA|iCB>
c           on a deja calcule la moitie de F
         if(nt.eq.1) coef=0.5d0
      endif
c     print*,'coef',coef,nt
      if(itest(16).eq.1.and.nt.eq.2) goto 1
      if(yw)write(6,*)'avant diff1c '
      call diff1c(nca,ica,ncb,icb,nt,it,ispt,np,ip,ispp,iadr1
     *,b,ab,coef)
      if(ygcas) goto 1
      if(yw)write(6,*)'avant diff1p '
c     print*,'appel diff1p'
      call diff1p(nca,ica,ncb,icb,nt,it,ispt,np,ip,ispp,iadr1
     *,b,ab,coef)
      call diff1cp(nca,ica,ncb,icb,nt,it,ispt,np,ip,ispp,iadr1,
     *b,ab,coef)
      if(itest(16).eq.1) goto 1
      if(yw)write(6,*)'avant diff1tp '
      call diff1tp(nca,ica,ncb,icb,nt,it,ispt,np,ip,ispp,iadr1,
     *b,ab)
      if(yw)write(6,*)'avant diff1t '
      call diff1t(nca,ica,ncb,icb,nt,it,ispt,np,ip,ispp,iadr1
     *,b,ab)
      if(yw)write(6,*)'avant diff1tc '
      call diff1tc(nca,ica,ncb,icb,nt,it,ispt,np,ip,ispp,iadr1,
     *b,ab)
      goto 1
 100  continue
      jcpu=itemps(idum)-jcpu
      if(ytana.or.yw)write(6,*)'1 difference   cpu',jcpu
      return
      end
