      subroutine testgendet
      use module
      !
      ! Generation de tous les determinants, y compris ceux non lies
      ! la subroutine adrdet doit reconnaitre quels sont les lies a
      ! conserver
      !
      implicit real(8) (a-h,o-x,z),logical(1)(y)
      integer,dimension(:,:),allocatable :: nsta,nstb,nspa,nspb
      dimension yc(32),it(2),ip(2)
      dimension ica(maxorbcas),icb(maxorbcas)
      common/bid/ibid(0:2,0:2,0:2,0:2)
      allocatable ydet(:)
      character(16) cara,carb
      character(80) aa
      allocate (ydet(ndet))
      yw=.false.
      yw1=.false.
      if(yw)print*,'testgendet ndet',ndet
      ydet=.false.
      ibid=0
      ndetg=0
      ndetr=0
      ! boucle sur les det CAS
      do ic=1,ndetc
         yc=.false.
         i=ibinc(ic)
         do k=1,noac
            yc(k)=(i==ibset(i,k-1))
            yc(k+noac)=(i==ibset(i,k+16-1))
         enddo
         if(yw)print'(16l1,2x,16l1)',yc(1:noac+noac)
         nca=-nocac0
         do k=1,noac
            if(yc(k)) nca=nca+1
         enddo
         ncb=-nocbc0
         do k=1,noac
            if(yc(k+noac)) ncb=ncb+1
         enddo
         if(w2) then
            jdist=0
            if(sel==2) jdist=icaldist(ic,jbin,ibinc,noac)
            cara=' '
            carb=' '
            write(cara(1:noac),'(16l1)') yc(1:noac)
            write(carb(1:noac),'(16l1)') yc(noac+1:noac+noac)
            print'(a,i8,2x,2a,i4)','Testgendet det CAS',
     *      ic,cara,carb,jdist
         endif
         kk=0
         do k=1,noac
            if(yc(k)) then
               kk=kk+1
               ica(kk)=numac(k)
            endif
         enddo
         kk=0
         do k=1,noac
            if(yc(k+noac)) then
               kk=kk+1
               icb(kk)=-numac(k)
            endif
         enddo
         if(sel/=0) then
            idist=icaldist(ic,jbin,ibinc,noac)
!           call adrc(yc,iadrc,irang)
!           print'(a,8l1,4i8)','idist,irang,reor_sel(irang),iadrc',
!     *     yc(1:noac+noac),idist,irang,reor_sel(irang),iadrc
         else
!           call adrc(yc,iadrc,irang)
!           print*,'irang,iadrc',yc(1:noac+noac),irang,iadrc
         endif
!      enddo
!      stop
!      do
         if(yw)print*,'  Determinants                       '//
     *   'isdimtp(icl) iadrc iadrtp-corr corr   iadr'

         ! Excitations a partir du det CAS
         do nt=0,2
            do np=0,2
               if(igener==1) then
                  if(nt>1.or.np>1) cycle
               else
                  if(nt+np>igener) cycle
               endif
               if(sel/=0) then
                  call lim_sel(nt,np,idist,ibon)
                  if(ibon==0) cycle
               endif
               do nta=0,nt
                  if(nta>ninac) cycle
                  ntb=nt-nta
                  if(ntb>ninac) cycle
                  do npa=0,np
                     if(npa>nvirt) cycle
                     if(nca+npa-nta.ne.0) cycle
                     npb=np-npa
                     if(npb>nvirt) cycle
                     if(ncb+npb-ntb.ne.0) cycle
                     if(yw)print*,'accepte',nta,ntb,npa,npb
                     ntta=1
                     nttb=1
                     ntpa=1
                     ntpb=1
                     ntta1=1
                     nttb1=1
                     ntpa1=1
                     ntpb1=1
                     if(nta==1) ntta1=ninac
                     if(nta==2) ntta1=ninac*(ninac-1)/2
                     if(ntb==1) nttb1=ninac
                     if(ntb==2) nttb1=ninac*(ninac-1)/2
                     if(npa==1) ntpa1=nvirt
                     if(npa==2) ntpa1=nvirt*(nvirt-1)/2
                     if(npb==1) ntpb1=nvirt
                     if(npb==2) ntpb1=nvirt*(nvirt-1)/2
                     if(nta.ne.0) then
                        allocate(nsta(nta,ntta1))
                        call combin(nsta,ninac,nta,ntta,nta,ntta1)
                     endif
                     if(ntb.ne.0) then
                        allocate(nstb(ntb,nttb1))
                        !print*,ninac,ntb,nttb1,ntb,nttb1
                        call combin(nstb,ninac,ntb,nttb,ntb,nttb1)
                     endif
                     if(npa.ne.0) then
                        allocate(nspa(npa,ntpa1))
                        call combin(nspa,nvirt,npa,ntpa,npa,ntpa1)
                     endif
                     if(npb.ne.0) then
                        allocate(nspb(npb,ntpb1))
                        !print*,ninac,npb,ntpb1,npb,ntpb1
                        call combin(nspb,nvirt,npb,ntpb,npb,ntpb1)
                     endif
              if(yw)print*,'nvirt',nvirt,npa,npb,ntpa1,ntpb1,ntpa,ntpb
                  !   print*,'ntta,ntta1',ntta,ntta1
                  !   print*,'nttb,nttb1',nttb,nttb1
                  !   print*,'ntpa,ntpa1',ntpa,ntpa1
                  !   print*,'ntpb,ntpb1',ntpb,ntpb1
                     if(ntta.ne.ntta1) stop
                     if(nttb.ne.nttb1) stop
                     if(ntpa.ne.ntpa1) stop
                     if(ntpb.ne.ntpb1) stop
                     do kta=1,ntta
                        jt=0
                        do k=1,nta
                           jt=jt+1
                           it(jt)=numin(nsta(k,kta))
                        enddo
                        do ktb=1,nttb
                           jt1=jt
                           do k=1,ntb
                              jt1=jt1+1
                              it(jt1)=-numin(nstb(k,ktb))
                           enddo
                           do kpa=1,ntpa
                              jp=0
                              do k=1,npa
                                 jp=jp+1
                                 ip(jp)=numvir(nspa(k,kpa))
                              enddo
                              do kpb=1,ntpb
                                 jp1=jp
                                 do k=1,npb
                                    jp1=jp1+1
                                    ip(jp1)=-numvir(nspb(k,kpb))
                                 enddo
                                 ndetg=ndetg+1
                                 !print*,'it,ip',it,'  ',ip
                                 call adrdet(yc,jt1,it,jp1,ip,iadr)
                                 if((mprint(7)==1.or.yw1).and.iadr.ne.0)
     *                           then
                                    aa=' '
                                    aa(1:5)='iadr='
                                    kp=5
                                    write(aa(kp+1:kp+10),'(i10)') iadr
                                    kp=kp+10
                                    aa(6:15)=adjustl(aa(6:15))
                                    write(aa(kp+1:kp+noac),'(32l1)')
     *                              yc(1:noac)
                                    kp=kp+noac+1
                                    write(aa(kp+1:kp+noac),
     *                              '(32l1)') yc(noac+1:noac+noac)
                                    kp=kp+noac+1
                                    write(aa(kp+1:kp+5*jt1),'(2i5)')
     *                              it(1:jt1)
                                    kp=kp+5*jt1+2
                                    aa(kp+1:kp+3)='-->'
                                    kp=kp+4
                                    write(aa(kp+1:kp+5*jp1),'(2i5)')
     *                              ip(1:jp1)
                                    if(gener=='SEL+S0')print*,trim(aa)
                                 endif
                                 if(iadr==0) then
                                         !print*,'gen gd',ygendet
                                    if(ygendet)
     *                              call pie(yc,jt1,it,jp1,ip)
                                    cycle
                                 endif
                                 if(iadr<0.or.iadr>ndet) then
                                    print*,'adresses hors limites',iadr
                                    stop
                                 endif
                                 if(ydet(iadr)) then
                                    write(75,*)'det deja adresse',iadr,
     *                              ' it=',it(1:jt1),' ip=',ip(1:jp1)
     *                              ,'$'
                                    call scril
                                    stop
                                 endif
                                 ndetr=ndetr+1
                                 ydet(iadr)=.true.
                              enddo
                           enddo
                        enddo
                     enddo
                     if(nta.ne.0)deallocate(nsta)
                     if(ntb.ne.0)deallocate(nstb)
                     if(npa.ne.0)deallocate(nspa)
                     if(npb.ne.0)deallocate(nspb)
                  enddo
               enddo
            enddo
         enddo
      enddo
      print*,'nombre de det generes sans consideration de localite'
     *,ndetg
      print*,'nombre de det generes retenus                       '
     *,ndetr
      open(51,file='NDET',status='unknown')
      write(51,*) ndetg,ndetr
      close(51)
      if(ndetr.ne.ndet) then
         print*,'mauvais nombre de determinants',ndetr,ndet
         print'(80l1)', ydet
         do k=1,ndet
            if(.not.ydet(k)) print*,'F ',k
         enddo
         stop
      endif
      deallocate (ydet)
      return
      end
