anchor.diff:        do i = 0, k-1
            if (count(ilabeling2==i)/=count(ilabeling1==i)) then
               write(13,'("Concentrations don''t match for color ",i1)') i
  -            write(*,'("Concentrations don''t match for color ",i1)') i
               cycle compareloop
            endif
         enddo
  -      write(*,'("Concentrations match for structures ",2(i6,1x))') strN, strN2
         write(13,'("Concentrations match for structures ",2(i6,1x))') strN, strN2
  -      write(*,'(2(14i1,/))') ilabeling1, ilabeling2
  +
  +!! Aug 12 This block of 30 lines was moved from above. We only need to check for L1/=L2 after everything else matches.
  +   ! We do this even though there are no duplicates to remove. The routine also generates the vectors subscripts for the label permutations and we need these to generate the list of equivalent labelings
  +
  +! dRotList1 is used in following two calls because, at this point, we've established that they would be the same, and it's more efficient to find dRotList1 outside of the compare loop
  +   call remove_duplicate_lattices(HNFin,LatDim2,pLV2,rot,shift,dset2,dRotList1,HNFout,fixOp,&
  +                                  LattRotList2,sLVlist,degen_list,eps)
  +   open(17,file="debug_rotation_permutations.out")
  +   ! Get the list of label permutations
  +   call get_rotation_perms_lists(pLV2,HNFout,L2,SNF,fixOp,LattRotList2,dRotList1,eps)
  +   write(17,'("Rots Indx:",/,8(24(i3,1x),/))') LattRotList2(1)%RotIndx(:)
  +   write(17,'("Permutation group (trans+rot):")')
  +   nP = size(LattRotList2(1)%perm,1)
  +   do ip = 1, nP
  +      write(17,'("Perm #",i3,":",1x,200(i2,1x))') ip,LattRotList2(1)%perm(ip,:)
  +   enddo
  +   call find_equivalent_labelings(ilabeling2,LattRotList2,qLabel,rotProdLab)
  +   do i = 1,size(qLabel,1)
  +!      write(*,'("pL: ",i3,2x,14i1)')i,qLabel(i,:)
  +   enddo
         if (any(L1/=L2)) then ! We must find the automorphism that connects the two different groups and adjust ilabeling2 accordingly
  -         if (associated(g1)) deallocate(g1)
  -         if (associated(g2)) deallocate(g2)
  +         if (associated(g1)) deallocate(g1); if (associated(g2)) deallocate(g2)
            !1) build the member list
            call make_member_list(diag,g1) ! This routine allocates g1
  -         ! do i = 1,3
  -         !    write(*,'("g1: ",20(i2,1x))') g1(i,:)
  -         ! enddo
  -         ! print*
  -
            !2) find the group automorphism
            call matrix_inverse(real(L2(:,:,1),dp),L2inv,err)
  -         if (err) stop "L1inv is not defined"
  +         if (err) stop "L2inv is not defined"
            T = matmul(L1(:,:,1),L2inv)
  -         ! do i = 1,3
  -         !    write(*,'(" T:  ",3(f7.3,1x))') T(i,:)
  -         ! enddo
  -
  +          ! do i = 1,3
  +          !    write(*,'(" T:  ",3(f7.3,1x))') T(i,:)
  +          ! enddo
            allocate(g2(3,size(g1,2)))
  -         g2 = -1
  +         !g2 = -1
            g2 = nint(matmul(T,g1))
            g2 = modulo(g2,spread(diag,2,n))
            !if (any(g2==-1)) stop "Bug in group member mapping"
  
positive.code:          !    write(*,'("g2: ",20(i2,1x))') g2(i,:)
           ! enddo
           ! print*
           do i = 1,size(qLabel,1)
              write(*,'("qL:  ",20(i1))') qLabel(i,:)
           enddo
           print*
  
           ! Next, find the rearrangement between the two groups (the automorphism)
           if (allocated(automorphism)) deallocate(automorphism)
           allocate(automorphism(n2))
           call find_permutation_of_group(g1,g2,automorphism)
           !3) change ilabeling2 into the equivalent ilabeling1
           write(*,'("automorphism: ",2x,7i1)') automorphism
           write(*,'("before:  ",14i1)') ilabeling2
           do i = 0, n*nD2-1, n
  !            write(*,'("i  ",i2," vs: ",7i2)')i,(automorphism+i)
              ilabeling2(1+i:n+i) = ilabeling2(automorphism+i)
           enddo
           write(*,'("after:   ",14i1)') ilabeling2
           !4) proceed as normal
        endif
           foundLab = .false.
  !July 29         nL = size(pLabel,1)
           !outer: !do iL = 1, nL
              ! GLWH July 2019. As of today, this logic is wrong from earlier attempts. We do not want to loop over all permutations of both the file1 and file2 label. It is sufficient, now that we have the self-complementary equiatomic stuff sorted out, to just check each file2 labeling with all permutations of the file1 labeling (or the other way around.)
  
  
              ! We need to loop over all permutations (to get the full orbit) because we do not know
              ! which member of the orbit is the representative.
              ! Also, for 50-50 labelings that are self-complementary (more generally, any
              ! "equiatomic" labeling may be self-complementary), we also need to check the
              ! complement of each labeling because any of the complements may appear in either list.
              ! GLWH 2019, see email chain, subject: "Monk?", with Rod Forcade starting May 16 2019
              do jL = 1, size(qlabel,1)
                 ilabeling2 = qLabel(jL,:)
                 !July29 write(*,'("plabel: ",14(i1))') plabel(iL,:)
                 write(*,'("qlabel: ",14(i1))') qlabel(jL,:)
                 if(all(ilabeling2==ilabeling1)) then
                    foundLab = .true.
                    print*,"MATCH"
                    if (match/=0) then
                       write(*,'("Additional match found: ",i6," == ",i6)') strN, strN2
                        write(*,'("In file 2, structure ",i6)') strN2
                        write(*,'("was equivalent to ",i6)') match
                        write(*,'("in file 1")')
                       stop "BUG! Found more than one match in struct_enum.out file"
                    endif
        !            match = iStr2
                    match = strN2
                    exit
                 endif
                 !if equiatomic then check for complements. I think it's OK to not check permutations (we've already done that above). We just need to check for different complements of the same labeling...
                 if (equiatomic) then ! check complements as well
                    do iLP = 2, nLP ! Loop over all permutations of the
                                    ! labels (stored in 'labPerms'). Start at 2 since
                                    ! we want to skip the identity. We already checked the unpermuted case in the preceding `if` statement.
                    do i = 0,k-1
                       where  (ilabeling2==i) tempLab = labPerms(iLP,i+1)
                    enddo
                    write(*,'("before: ",14i1)') ilabeling2
                    write(*,'("after:  ",14i1)') tempLab
  
                       ! do jp = 1, size(ilabeling2) ! For each digit in the labeling,
                       !                             ! permute the label (label exchange)
                       !    write(*,'(20i2)') labPerms(iLP,:)
                          !stop "still haven't written this part"
                          !ilabeling2(jp) = labPerms(a(jp)+1,iLP)
                       ! enddo
                       if(all(ilabeling2==tempLab)) then
                          foundLab = .true.
                          print*,"MATCH (equiatomic)"
                          if (match/=0) then
                             write(*,'("Additional match found: ",i6," == ",i6)') strN, strN2
                             write(*,'("In file 2, structure ",i6)') strN2
                             write(*,'("was equivalent to ",i6)') match
                             write(*,'("in file 1")')
                             stop "BUG! Found more than one match in struct_enum.out file"
                          endif
              !            match = iStr2
                          match = strN2
  !                        exit outer
                       endif
                    enddo
                 endif
              enddo
           if(.not. foundLab) then
              write(13,'("Labeling didn''t match for str #:",i8)') strN
           else
              write(13,'("Structure number:",i8," in file2 is a match to ",i8," in file1!")') strN2, strN
           endif
           !if (match/=0) exit
     enddo compareloop
     close(f2)
     if(match==0) then
        write(13,'("Match for str #:",i8," in file 1 was not found in file 2")') strN
        write(*,'("Match for str #:",i8," in file 1 was not found in file 2")') strN
        stop
     else
        write(13,'("Structure number:",i8," is a match to # ",i9," in file 2.")') strN, match
        write(*,'("Structure number:",i8," is a match to # ",i9," in file 2.")') strN, match
     endif
     deallocate(ilabeling2)
     deallocate(ilabeling1)
  enddo
  close(13)
  
  endprogram compare_two_struct_enum
  
negative.code: integer, pointer, dimension(:,:) :: label, g1, g2, dlabel, qLabel
  integer :: LatDim1, LatDim2, match, nD1, nD2, Nmin, Nmax, k, ioerr
  integer :: strN, sizeN, n, pgOps, diag(3), a, b, c, d, e, f, i, jl, j
  integer :: iP, nP, lc, iStr2, HNFtest(3,3), hdgenfact, labdgen, totdgen
  integer :: strN2, hnfN2, n2, pgOps2, diag2(3), idx, f1, f2, iLP, nLP
  integer, allocatable :: ilabeling1(:), ilabeling2(:), atomType(:), automorphism(:)
  integer, allocatable :: tempLab(:)
  real(dp) :: eps
  logical full, HNFmatch, foundLab, err, equiatomic
  character(maxLabLength)   :: labeling ! List, 0..k-1, of the atomic type at each site
  type(RotPermList)         :: dRotList1
  type(RotPermList),pointer :: LattRotList2(:)
  type(opList), pointer     :: fixOp(:)
  integer, pointer          :: crange(:,:), degen_list(:), rotProdLab(:), labPerms(:,:)
  
  