      SUBROUTINE mbpt2_t3(d_f1,d_i0,d_t11,d_t12,d_t22,d_t23,d_v2,k_f1_of
     &fset,k_i0_offset,k_t11_offset,k_t12_offset,k_t22_offset,k_t23_offs
     &et,k_v2_offset)
C     $Id$
C     This is a Fortran77 program generated by Tensor Contraction Engine v.1.0
C     Copyright (c) Battelle & Pacific Northwest National Laboratory (2002)
C     i0 ( p1 p2 p3 h9 h10 h11 )_t2f + = 1 * P( 9 ) * t2 ( p1 p2 h9 h10 )_t2 * f ( p3 h11 )_f
C     i0 ( p1 p2 p3 h9 h10 h11 )_t2f + = -1 * P( 3 ) * Sum ( h4 ) * t2 ( p1 p2 p3 h4 h9 h10 )_t2 * f ( h4 h11 )_f
C     i0 ( p1 p2 p3 h9 h10 h11 )_t2f + = 1 * P( 3 ) * Sum ( p4 ) * t2 ( p1 p2 p4 h9 h10 h11 )_t2 * f ( p3 p4 )_f
C     i0 ( p1 p2 p3 h9 h10 h11 )_ft1t1 + = 1 * P( 9 ) * t1 ( p1 p2 h9 h10 )_t1 * i1 ( p3 h11 )_ft1
C         i1 ( p1 h9 )_ft1 + = -1 * Sum ( h5 p4 ) * t1 ( p1 p4 h5 h9 )_t1 * f ( h5 p4 )_f
C     i0 ( p1 p2 p3 h9 h10 h11 )_ft1t1 + = 1 * P( 9 ) * Sum ( h6 ) * t1 ( p1 p2 h6 h9 )_t1 * i1 ( h6 p3 h10 h11 )_ft1
C         i1 ( h6 p1 h9 h10 )_ft1 + = -1 * Sum ( p4 ) * t1 ( p1 p4 h9 h10 )_t1 * f ( h6 p4 )_f
C         i1 ( h6 p1 h9 h10 )_v + = 1 * v ( h6 p1 h9 h10 )_v
C     i0 ( p1 p2 p3 h9 h10 h11 )_vt1 + = 1 * P( 9 ) * t1 ( p1 h9 )_t1 * v ( p2 p3 h10 h11 )_v
C     i0 ( p1 p2 p3 h9 h10 h11 )_vt1 + = -1 * P( 9 ) * Sum ( p4 ) * t1 ( p1 p4 h9 h10 )_t1 * v ( p2 p3 h11 p4 )_v
      IMPLICIT NONE
#include "global.fh"
#include "mafdecls.fh"
#include "util.fh"
#include "errquit.fh"
#include "tce.fh"
      INTEGER d_i0
      INTEGER k_i0_offset
      INTEGER d_t22
      INTEGER k_t22_offset
      INTEGER d_f1
      INTEGER k_f1_offset
      INTEGER d_t23
      INTEGER k_t23_offset
      INTEGER d_t12
      INTEGER k_t12_offset
      INTEGER d_i1
      INTEGER k_i1_offset
      INTEGER d_t11
      INTEGER k_t11_offset
      INTEGER d_v2
      INTEGER k_v2_offset
      INTEGER l_i1_offset
      INTEGER size_i1
      CHARACTER*255 filename
      CALL mbpt2_t3_1(d_t22,k_t22_offset,d_f1,k_f1_offset,d_i0,k_i0_offs
     &et)
      CALL mbpt2_t3_2(d_t23,k_t23_offset,d_f1,k_f1_offset,d_i0,k_i0_offs
     &et)
      CALL mbpt2_t3_3(d_t23,k_t23_offset,d_f1,k_f1_offset,d_i0,k_i0_offs
     &et)
      CALL OFFSET_mbpt2_t3_4_1(l_i1_offset,k_i1_offset,size_i1)
      CALL TCE_FILENAME('mbpt2_t3_4_1_i1',filename)
      CALL CREATEFILE(filename,d_i1,size_i1)
      CALL mbpt2_t3_4_1(d_t12,k_t12_offset,d_f1,k_f1_offset,d_i1,k_i1_of
     &fset)
      CALL RECONCILEFILE(d_i1,size_i1)
      CALL mbpt2_t3_4(d_t12,k_t12_offset,d_i1,k_i1_offset,d_i0,k_i0_offs
     &et)
      CALL DELETEFILE(d_i1)
      IF (.not.MA_POP_STACK(l_i1_offset)) CALL ERRQUIT('mbpt2_t3',-1,MA_
     &ERR)
      CALL OFFSET_mbpt2_t3_5_1(l_i1_offset,k_i1_offset,size_i1)
      CALL TCE_FILENAME('mbpt2_t3_5_1_i1',filename)
      CALL CREATEFILE(filename,d_i1,size_i1)
      CALL mbpt2_t3_5_1(d_t12,k_t12_offset,d_f1,k_f1_offset,d_i1,k_i1_of
     &fset)
      CALL mbpt2_t3_5_2(d_v2,k_v2_offset,d_i1,k_i1_offset)
      CALL RECONCILEFILE(d_i1,size_i1)
      CALL mbpt2_t3_5(d_t12,k_t12_offset,d_i1,k_i1_offset,d_i0,k_i0_offs
     &et)
      CALL DELETEFILE(d_i1)
      IF (.not.MA_POP_STACK(l_i1_offset)) CALL ERRQUIT('mbpt2_t3',-1,MA_
     &ERR)
      CALL mbpt2_t3_6(d_t11,k_t11_offset,d_v2,k_v2_offset,d_i0,k_i0_offs
     &et)
      CALL mbpt2_t3_7(d_t12,k_t12_offset,d_v2,k_v2_offset,d_i0,k_i0_offs
     &et)
      RETURN
      END
      SUBROUTINE mbpt2_t3_1(d_a,k_a_offset,d_b,k_b_offset,d_c,k_c_offset
     &)
C     $Id$
C     This is a Fortran77 program generated by Tensor Contraction Engine v.1.0
C     Copyright (c) Battelle & Pacific Northwest National Laboratory (2002)
C     i0 ( p1 p2 p3 h9 h10 h11 )_t2f + = 1 * P( 9 ) * t2 ( p1 p2 h9 h10 )_t2 * f ( p3 h11 )_f
      IMPLICIT NONE
#include "global.fh"
#include "mafdecls.fh"
#include "sym.fh"
#include "errquit.fh"
#include "tce.fh"
      INTEGER d_a
      INTEGER k_a_offset
      INTEGER d_b
      INTEGER k_b_offset
      INTEGER d_c
      INTEGER k_c_offset
      INTEGER NXTASK
      INTEGER next
      INTEGER nprocs
      INTEGER count
      INTEGER p1b
      INTEGER p2b
      INTEGER p3b
      INTEGER h9b
      INTEGER h10b
      INTEGER h11b
      INTEGER dimc
      INTEGER l_c_sort
      INTEGER k_c_sort
      INTEGER p1b_1
      INTEGER p2b_1
      INTEGER h9b_1
      INTEGER h10b_1
      INTEGER p3b_2
      INTEGER h11b_2
      INTEGER dim_common
      INTEGER dima_sort
      INTEGER dima
      INTEGER dimb_sort
      INTEGER dimb
      INTEGER l_a_sort
      INTEGER k_a_sort
      INTEGER l_a
      INTEGER k_a
      INTEGER l_b_sort
      INTEGER k_b_sort
      INTEGER l_b
      INTEGER k_b
      INTEGER l_c
      INTEGER k_c
      EXTERNAL NXTSKL
      nprocs = GA_NNODES()
      count = 0
      next = NXTASK(nprocs,1)
      DO p1b = noab+1,noab+nvab
      DO p2b = p1b,noab+nvab
      DO p3b = noab+1,noab+nvab
      DO h9b = 1,noab
      DO h10b = h9b,noab
      DO h11b = 1,noab
      IF (next.eq.count) THEN
      IF ((.not.restricted).or.(int_mb(k_spin+p1b-1)+int_mb(k_spin+p2b-1
     &)+int_mb(k_spin+p3b-1)+int_mb(k_spin+h9b-1)+int_mb(k_spin+h10b-1)+
     &int_mb(k_spin+h11b-1).ne.12)) THEN
      IF (int_mb(k_spin+p1b-1)+int_mb(k_spin+p2b-1)+int_mb(k_spin+p3b-1)
     & .eq. int_mb(k_spin+h9b-1)+int_mb(k_spin+h10b-1)+int_mb(k_spin+h11
     &b-1)) THEN
      IF (ieor(int_mb(k_sym+p1b-1),ieor(int_mb(k_sym+p2b-1),ieor(int_mb(
     &k_sym+p3b-1),ieor(int_mb(k_sym+h9b-1),ieor(int_mb(k_sym+h10b-1),in
     &t_mb(k_sym+h11b-1)))))) .eq. ieor(irrep_t2,irrep_f)) THEN
      dimc = int_mb(k_range+p1b-1) * int_mb(k_range+p2b-1) * int_mb(k_ra
     &nge+p3b-1) * int_mb(k_range+h9b-1) * int_mb(k_range+h10b-1) * int_
     &mb(k_range+h11b-1)
      IF (.not.MA_PUSH_GET(mt_dbl,dimc,'noname',l_c_sort,k_c_sort)) CALL
     & ERRQUIT('mbpt2_t3_1',0,MA_ERR)
      CALL DFILL(dimc,0.0d0,dbl_mb(k_c_sort),1)
      IF (int_mb(k_spin+p1b-1)+int_mb(k_spin+p2b-1) .eq. int_mb(k_spin+h
     &9b-1)+int_mb(k_spin+h10b-1)) THEN
      IF (ieor(int_mb(k_sym+p1b-1),ieor(int_mb(k_sym+p2b-1),ieor(int_mb(
     &k_sym+h9b-1),int_mb(k_sym+h10b-1)))) .eq. irrep_t2) THEN
      CALL TCE_RESTRICTED_4(p1b,p2b,h9b,h10b,p1b_1,p2b_1,h9b_1,h10b_1)
      CALL TCE_RESTRICTED_2(p3b,h11b,p3b_2,h11b_2)
      dim_common = 1
      dima_sort = int_mb(k_range+p1b-1) * int_mb(k_range+p2b-1) * int_mb
     &(k_range+h9b-1) * int_mb(k_range+h10b-1)
      dima = dim_common * dima_sort
      dimb_sort = int_mb(k_range+p3b-1) * int_mb(k_range+h11b-1)
      dimb = dim_common * dimb_sort
      IF ((dima .gt. 0) .and. (dimb .gt. 0)) THEN
      IF (.not.MA_PUSH_GET(mt_dbl,dima,'noname',l_a_sort,k_a_sort)) CALL
     & ERRQUIT('mbpt2_t3_1',1,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dima,'noname',l_a,k_a)) CALL ERRQUIT('
     &mbpt2_t3_1',2,MA_ERR)
      CALL GET_HASH_BLOCK(d_a,dbl_mb(k_a),dima,int_mb(k_a_offset),(h10b_
     &1 - 1 + noab * (h9b_1 - 1 + noab * (p2b_1 - noab - 1 + nvab * (p1b
     &_1 - noab - 1)))))
      CALL TCE_SORT_4(dbl_mb(k_a),dbl_mb(k_a_sort),int_mb(k_range+p1b-1)
     &,int_mb(k_range+p2b-1),int_mb(k_range+h9b-1),int_mb(k_range+h10b-1
     &),4,3,2,1,1.0d0)
      IF (.not.MA_POP_STACK(l_a)) CALL ERRQUIT('mbpt2_t3_1',3,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dimb,'noname',l_b_sort,k_b_sort)) CALL
     & ERRQUIT('mbpt2_t3_1',4,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dimb,'noname',l_b,k_b)) CALL ERRQUIT('
     &mbpt2_t3_1',5,MA_ERR)
      CALL GET_HASH_BLOCK(d_b,dbl_mb(k_b),dimb,int_mb(k_b_offset),(h11b_
     &2 - 1 + (noab+nvab) * (p3b_2 - 1)))
      CALL TCE_SORT_2(dbl_mb(k_b),dbl_mb(k_b_sort),int_mb(k_range+p3b-1)
     &,int_mb(k_range+h11b-1),2,1,1.0d0)
      IF (.not.MA_POP_STACK(l_b)) CALL ERRQUIT('mbpt2_t3_1',6,MA_ERR)
      CALL DGEMM('T','N',dima_sort,dimb_sort,dim_common,1.0d0,dbl_mb(k_a
     &_sort),dim_common,dbl_mb(k_b_sort),dim_common,1.0d0,dbl_mb(k_c_sor
     &t),dima_sort)
      IF (.not.MA_POP_STACK(l_b_sort)) CALL ERRQUIT('mbpt2_t3_1',7,MA_ER
     &R)
      IF (.not.MA_POP_STACK(l_a_sort)) CALL ERRQUIT('mbpt2_t3_1',8,MA_ER
     &R)
      END IF
      END IF
      END IF
      IF (.not.MA_PUSH_GET(mt_dbl,dimc,'noname',l_c,k_c)) CALL ERRQUIT('
     &mbpt2_t3_1',9,MA_ERR)
      IF ((p2b .le. p3b) .and. (h10b .le. h11b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+h10b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),6,5,2,4,3,1,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h11b 
     &- 1 + noab * (h10b - 1 + noab * (h9b - 1 + noab * (p3b - noab - 1 
     &+ nvab * (p2b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF ((p2b .le. p3b) .and. (h11b .le. h9b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+h10b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),6,5,2,1,4,3,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h10b 
     &- 1 + noab * (h9b - 1 + noab * (h11b - 1 + noab * (p3b - noab - 1 
     &+ nvab * (p2b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF ((p2b .le. p3b) .and. (h9b .le. h11b) .and. (h11b .le. h10b)) T
     &HEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+h10b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),6,5,2,4,1,3,-1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h10b 
     &- 1 + noab * (h11b - 1 + noab * (h9b - 1 + noab * (p3b - noab - 1 
     &+ nvab * (p2b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF ((p3b .le. p1b) .and. (h10b .le. h11b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+h10b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),2,6,5,4,3,1,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h11b 
     &- 1 + noab * (h10b - 1 + noab * (h9b - 1 + noab * (p2b - noab - 1 
     &+ nvab * (p1b - noab - 1 + nvab * (p3b - noab - 1)))))))
      END IF
      IF ((p3b .le. p1b) .and. (h11b .le. h9b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+h10b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),2,6,5,1,4,3,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h10b 
     &- 1 + noab * (h9b - 1 + noab * (h11b - 1 + noab * (p2b - noab - 1 
     &+ nvab * (p1b - noab - 1 + nvab * (p3b - noab - 1)))))))
      END IF
      IF ((p3b .le. p1b) .and. (h9b .le. h11b) .and. (h11b .le. h10b)) T
     &HEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+h10b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),2,6,5,4,1,3,-1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h10b 
     &- 1 + noab * (h11b - 1 + noab * (h9b - 1 + noab * (p2b - noab - 1 
     &+ nvab * (p1b - noab - 1 + nvab * (p3b - noab - 1)))))))
      END IF
      IF ((p1b .le. p3b) .and. (p3b .le. p2b) .and. (h10b .le. h11b)) TH
     &EN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+h10b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),6,2,5,4,3,1,-1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h11b 
     &- 1 + noab * (h10b - 1 + noab * (h9b - 1 + noab * (p2b - noab - 1 
     &+ nvab * (p3b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF ((p1b .le. p3b) .and. (p3b .le. p2b) .and. (h11b .le. h9b)) THE
     &N
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+h10b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),6,2,5,1,4,3,-1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h10b 
     &- 1 + noab * (h9b - 1 + noab * (h11b - 1 + noab * (p2b - noab - 1 
     &+ nvab * (p3b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF ((p1b .le. p3b) .and. (p3b .le. p2b) .and. (h9b .le. h11b) .and
     &. (h11b .le. h10b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+h10b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),6,2,5,4,1,3,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h10b 
     &- 1 + noab * (h11b - 1 + noab * (h9b - 1 + noab * (p2b - noab - 1 
     &+ nvab * (p3b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF (.not.MA_POP_STACK(l_c)) CALL ERRQUIT('mbpt2_t3_1',10,MA_ERR)
      IF (.not.MA_POP_STACK(l_c_sort)) CALL ERRQUIT('mbpt2_t3_1',11,MA_E
     &RR)
      END IF
      END IF
      END IF
      next = NXTASK(nprocs,1)
      END IF
      count = count + 1
      END DO
      END DO
      END DO
      END DO
      END DO
      END DO
      next = NXTASK(-nprocs,1)
      call GA_SYNC()
      RETURN
      END
      SUBROUTINE mbpt2_t3_2(d_a,k_a_offset,d_b,k_b_offset,d_c,k_c_offset
     &)
C     $Id$
C     This is a Fortran77 program generated by Tensor Contraction Engine v.1.0
C     Copyright (c) Battelle & Pacific Northwest National Laboratory (2002)
C     i0 ( p1 p2 p3 h9 h10 h11 )_t2f + = -1 * P( 3 ) * Sum ( h4 ) * t2 ( p1 p2 p3 h4 h9 h10 )_t2 * f ( h4 h11 )_f
      IMPLICIT NONE
#include "global.fh"
#include "mafdecls.fh"
#include "sym.fh"
#include "errquit.fh"
#include "tce.fh"
      INTEGER d_a
      INTEGER k_a_offset
      INTEGER d_b
      INTEGER k_b_offset
      INTEGER d_c
      INTEGER k_c_offset
      INTEGER NXTASK
      INTEGER next
      INTEGER nprocs
      INTEGER count
      INTEGER p1b
      INTEGER p2b
      INTEGER p3b
      INTEGER h9b
      INTEGER h10b
      INTEGER h11b
      INTEGER dimc
      INTEGER l_c_sort
      INTEGER k_c_sort
      INTEGER h4b
      INTEGER p1b_1
      INTEGER p2b_1
      INTEGER p3b_1
      INTEGER h9b_1
      INTEGER h10b_1
      INTEGER h4b_1
      INTEGER h4b_2
      INTEGER h11b_2
      INTEGER dim_common
      INTEGER dima_sort
      INTEGER dima
      INTEGER dimb_sort
      INTEGER dimb
      INTEGER l_a_sort
      INTEGER k_a_sort
      INTEGER l_a
      INTEGER k_a
      INTEGER l_b_sort
      INTEGER k_b_sort
      INTEGER l_b
      INTEGER k_b
      INTEGER l_c
      INTEGER k_c
      EXTERNAL NXTASK
      nprocs = GA_NNODES()
      count = 0
      next = NXTASK(nprocs,1)
      DO p1b = noab+1,noab+nvab
      DO p2b = p1b,noab+nvab
      DO p3b = p2b,noab+nvab
      DO h9b = 1,noab
      DO h10b = h9b,noab
      DO h11b = 1,noab
      IF (next.eq.count) THEN
      IF ((.not.restricted).or.(int_mb(k_spin+p1b-1)+int_mb(k_spin+p2b-1
     &)+int_mb(k_spin+p3b-1)+int_mb(k_spin+h9b-1)+int_mb(k_spin+h10b-1)+
     &int_mb(k_spin+h11b-1).ne.12)) THEN
      IF (int_mb(k_spin+p1b-1)+int_mb(k_spin+p2b-1)+int_mb(k_spin+p3b-1)
     & .eq. int_mb(k_spin+h9b-1)+int_mb(k_spin+h10b-1)+int_mb(k_spin+h11
     &b-1)) THEN
      IF (ieor(int_mb(k_sym+p1b-1),ieor(int_mb(k_sym+p2b-1),ieor(int_mb(
     &k_sym+p3b-1),ieor(int_mb(k_sym+h9b-1),ieor(int_mb(k_sym+h10b-1),in
     &t_mb(k_sym+h11b-1)))))) .eq. ieor(irrep_t2,irrep_f)) THEN
      dimc = int_mb(k_range+p1b-1) * int_mb(k_range+p2b-1) * int_mb(k_ra
     &nge+p3b-1) * int_mb(k_range+h9b-1) * int_mb(k_range+h10b-1) * int_
     &mb(k_range+h11b-1)
      IF (.not.MA_PUSH_GET(mt_dbl,dimc,'noname',l_c_sort,k_c_sort)) CALL
     & ERRQUIT('mbpt2_t3_2',0,MA_ERR)
      CALL DFILL(dimc,0.0d0,dbl_mb(k_c_sort),1)
      DO h4b = 1,noab
      IF (int_mb(k_spin+p1b-1)+int_mb(k_spin+p2b-1)+int_mb(k_spin+p3b-1)
     & .eq. int_mb(k_spin+h9b-1)+int_mb(k_spin+h10b-1)+int_mb(k_spin+h4b
     &-1)) THEN
      IF (ieor(int_mb(k_sym+p1b-1),ieor(int_mb(k_sym+p2b-1),ieor(int_mb(
     &k_sym+p3b-1),ieor(int_mb(k_sym+h9b-1),ieor(int_mb(k_sym+h10b-1),in
     &t_mb(k_sym+h4b-1)))))) .eq. irrep_t2) THEN
      CALL TCE_RESTRICTED_6(p1b,p2b,p3b,h9b,h10b,h4b,p1b_1,p2b_1,p3b_1,h
     &9b_1,h10b_1,h4b_1)
      CALL TCE_RESTRICTED_2(h4b,h11b,h4b_2,h11b_2)
      dim_common = int_mb(k_range+h4b-1)
      dima_sort = int_mb(k_range+p1b-1) * int_mb(k_range+p2b-1) * int_mb
     &(k_range+p3b-1) * int_mb(k_range+h9b-1) * int_mb(k_range+h10b-1)
      dima = dim_common * dima_sort
      dimb_sort = int_mb(k_range+h11b-1)
      dimb = dim_common * dimb_sort
      IF ((dima .gt. 0) .and. (dimb .gt. 0)) THEN
      IF (.not.MA_PUSH_GET(mt_dbl,dima,'noname',l_a_sort,k_a_sort)) CALL
     & ERRQUIT('mbpt2_t3_2',1,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dima,'noname',l_a,k_a)) CALL ERRQUIT('
     &mbpt2_t3_2',2,MA_ERR)
      IF ((h4b .le. h9b)) THEN
      CALL GET_HASH_BLOCK(d_a,dbl_mb(k_a),dima,int_mb(k_a_offset),(h10b_
     &1 - 1 + noab * (h9b_1 - 1 + noab * (h4b_1 - 1 + noab * (p3b_1 - no
     &ab - 1 + nvab * (p2b_1 - noab - 1 + nvab * (p1b_1 - noab - 1))))))
     &)
      CALL TCE_SORT_6(dbl_mb(k_a),dbl_mb(k_a_sort),int_mb(k_range+p1b-1)
     &,int_mb(k_range+p2b-1),int_mb(k_range+p3b-1),int_mb(k_range+h4b-1)
     &,int_mb(k_range+h9b-1),int_mb(k_range+h10b-1),6,5,3,2,1,4,1.0d0)
      END IF
      IF ((h9b .lt. h4b) .and. (h4b .le. h10b)) THEN
      CALL GET_HASH_BLOCK(d_a,dbl_mb(k_a),dima,int_mb(k_a_offset),(h10b_
     &1 - 1 + noab * (h4b_1 - 1 + noab * (h9b_1 - 1 + noab * (p3b_1 - no
     &ab - 1 + nvab * (p2b_1 - noab - 1 + nvab * (p1b_1 - noab - 1))))))
     &)
      CALL TCE_SORT_6(dbl_mb(k_a),dbl_mb(k_a_sort),int_mb(k_range+p1b-1)
     &,int_mb(k_range+p2b-1),int_mb(k_range+p3b-1),int_mb(k_range+h9b-1)
     &,int_mb(k_range+h4b-1),int_mb(k_range+h10b-1),6,4,3,2,1,5,-1.0d0)
      END IF
      IF ((h10b .lt. h4b)) THEN
      CALL GET_HASH_BLOCK(d_a,dbl_mb(k_a),dima,int_mb(k_a_offset),(h4b_1
     & - 1 + noab * (h10b_1 - 1 + noab * (h9b_1 - 1 + noab * (p3b_1 - no
     &ab - 1 + nvab * (p2b_1 - noab - 1 + nvab * (p1b_1 - noab - 1))))))
     &)
      CALL TCE_SORT_6(dbl_mb(k_a),dbl_mb(k_a_sort),int_mb(k_range+p1b-1)
     &,int_mb(k_range+p2b-1),int_mb(k_range+p3b-1),int_mb(k_range+h9b-1)
     &,int_mb(k_range+h10b-1),int_mb(k_range+h4b-1),5,4,3,2,1,6,1.0d0)
      END IF
      IF (.not.MA_POP_STACK(l_a)) CALL ERRQUIT('mbpt2_t3_2',3,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dimb,'noname',l_b_sort,k_b_sort)) CALL
     & ERRQUIT('mbpt2_t3_2',4,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dimb,'noname',l_b,k_b)) CALL ERRQUIT('
     &mbpt2_t3_2',5,MA_ERR)
      CALL GET_HASH_BLOCK(d_b,dbl_mb(k_b),dimb,int_mb(k_b_offset),(h11b_
     &2 - 1 + (noab+nvab) * (h4b_2 - 1)))
      CALL TCE_SORT_2(dbl_mb(k_b),dbl_mb(k_b_sort),int_mb(k_range+h4b-1)
     &,int_mb(k_range+h11b-1),2,1,1.0d0)
      IF (.not.MA_POP_STACK(l_b)) CALL ERRQUIT('mbpt2_t3_2',6,MA_ERR)
      CALL DGEMM('T','N',dima_sort,dimb_sort,dim_common,1.0d0,dbl_mb(k_a
     &_sort),dim_common,dbl_mb(k_b_sort),dim_common,1.0d0,dbl_mb(k_c_sor
     &t),dima_sort)
      IF (.not.MA_POP_STACK(l_b_sort)) CALL ERRQUIT('mbpt2_t3_2',7,MA_ER
     &R)
      IF (.not.MA_POP_STACK(l_a_sort)) CALL ERRQUIT('mbpt2_t3_2',8,MA_ER
     &R)
      END IF
      END IF
      END IF
      END DO
      IF (.not.MA_PUSH_GET(mt_dbl,dimc,'noname',l_c,k_c)) CALL ERRQUIT('
     &mbpt2_t3_2',9,MA_ERR)
      IF ((h10b .le. h11b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+h10b-1),int_mb(k_range+h9b-1),int_mb(k_range+p3b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),6,5,4,3,2,1,-1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h11b 
     &- 1 + noab * (h10b - 1 + noab * (h9b - 1 + noab * (p3b - noab - 1 
     &+ nvab * (p2b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF ((h11b .le. h9b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+h10b-1),int_mb(k_range+h9b-1),int_mb(k_range+p3b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),6,5,4,1,3,2,-1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h10b 
     &- 1 + noab * (h9b - 1 + noab * (h11b - 1 + noab * (p3b - noab - 1 
     &+ nvab * (p2b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF ((h9b .le. h11b) .and. (h11b .le. h10b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+h10b-1),int_mb(k_range+h9b-1),int_mb(k_range+p3b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),6,5,4,3,1,2,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h10b 
     &- 1 + noab * (h11b - 1 + noab * (h9b - 1 + noab * (p3b - noab - 1 
     &+ nvab * (p2b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF (.not.MA_POP_STACK(l_c)) CALL ERRQUIT('mbpt2_t3_2',10,MA_ERR)
      IF (.not.MA_POP_STACK(l_c_sort)) CALL ERRQUIT('mbpt2_t3_2',11,MA_E
     &RR)
      END IF
      END IF
      END IF
      next = NXTASK(nprocs,1)
      END IF
      count = count + 1
      END DO
      END DO
      END DO
      END DO
      END DO
      END DO
      next = NXTASK(-nprocs,1)
      call GA_SYNC()
      RETURN
      END
      SUBROUTINE mbpt2_t3_3(d_a,k_a_offset,d_b,k_b_offset,d_c,k_c_offset
     &)
C     $Id$
C     This is a Fortran77 program generated by Tensor Contraction Engine v.1.0
C     Copyright (c) Battelle & Pacific Northwest National Laboratory (2002)
C     i0 ( p1 p2 p3 h9 h10 h11 )_t2f + = 1 * P( 3 ) * Sum ( p4 ) * t2 ( p1 p2 p4 h9 h10 h11 )_t2 * f ( p3 p4 )_f
      IMPLICIT NONE
#include "global.fh"
#include "mafdecls.fh"
#include "sym.fh"
#include "errquit.fh"
#include "tce.fh"
      INTEGER d_a
      INTEGER k_a_offset
      INTEGER d_b
      INTEGER k_b_offset
      INTEGER d_c
      INTEGER k_c_offset
      INTEGER NXTASK
      INTEGER next
      INTEGER nprocs
      INTEGER count
      INTEGER p1b
      INTEGER p2b
      INTEGER p3b
      INTEGER h9b
      INTEGER h10b
      INTEGER h11b
      INTEGER dimc
      INTEGER l_c_sort
      INTEGER k_c_sort
      INTEGER p4b
      INTEGER p1b_1
      INTEGER p2b_1
      INTEGER p4b_1
      INTEGER h9b_1
      INTEGER h10b_1
      INTEGER h11b_1
      INTEGER p3b_2
      INTEGER p4b_2
      INTEGER dim_common
      INTEGER dima_sort
      INTEGER dima
      INTEGER dimb_sort
      INTEGER dimb
      INTEGER l_a_sort
      INTEGER k_a_sort
      INTEGER l_a
      INTEGER k_a
      INTEGER l_b_sort
      INTEGER k_b_sort
      INTEGER l_b
      INTEGER k_b
      INTEGER l_c
      INTEGER k_c
      EXTERNAL NXTASK
      nprocs = GA_NNODES()
      count = 0
      next = NXTASK(nprocs,1)
      DO p1b = noab+1,noab+nvab
      DO p2b = p1b,noab+nvab
      DO p3b = noab+1,noab+nvab
      DO h9b = 1,noab
      DO h10b = h9b,noab
      DO h11b = h10b,noab
      IF (next.eq.count) THEN
      IF ((.not.restricted).or.(int_mb(k_spin+p1b-1)+int_mb(k_spin+p2b-1
     &)+int_mb(k_spin+p3b-1)+int_mb(k_spin+h9b-1)+int_mb(k_spin+h10b-1)+
     &int_mb(k_spin+h11b-1).ne.12)) THEN
      IF (int_mb(k_spin+p1b-1)+int_mb(k_spin+p2b-1)+int_mb(k_spin+p3b-1)
     & .eq. int_mb(k_spin+h9b-1)+int_mb(k_spin+h10b-1)+int_mb(k_spin+h11
     &b-1)) THEN
      IF (ieor(int_mb(k_sym+p1b-1),ieor(int_mb(k_sym+p2b-1),ieor(int_mb(
     &k_sym+p3b-1),ieor(int_mb(k_sym+h9b-1),ieor(int_mb(k_sym+h10b-1),in
     &t_mb(k_sym+h11b-1)))))) .eq. ieor(irrep_t2,irrep_f)) THEN
      dimc = int_mb(k_range+p1b-1) * int_mb(k_range+p2b-1) * int_mb(k_ra
     &nge+p3b-1) * int_mb(k_range+h9b-1) * int_mb(k_range+h10b-1) * int_
     &mb(k_range+h11b-1)
      IF (.not.MA_PUSH_GET(mt_dbl,dimc,'noname',l_c_sort,k_c_sort)) CALL
     & ERRQUIT('mbpt2_t3_3',0,MA_ERR)
      CALL DFILL(dimc,0.0d0,dbl_mb(k_c_sort),1)
      DO p4b = noab+1,noab+nvab
      IF (int_mb(k_spin+p1b-1)+int_mb(k_spin+p2b-1)+int_mb(k_spin+p4b-1)
     & .eq. int_mb(k_spin+h9b-1)+int_mb(k_spin+h10b-1)+int_mb(k_spin+h11
     &b-1)) THEN
      IF (ieor(int_mb(k_sym+p1b-1),ieor(int_mb(k_sym+p2b-1),ieor(int_mb(
     &k_sym+p4b-1),ieor(int_mb(k_sym+h9b-1),ieor(int_mb(k_sym+h10b-1),in
     &t_mb(k_sym+h11b-1)))))) .eq. irrep_t2) THEN
      CALL TCE_RESTRICTED_6(p1b,p2b,p4b,h9b,h10b,h11b,p1b_1,p2b_1,p4b_1,
     &h9b_1,h10b_1,h11b_1)
      CALL TCE_RESTRICTED_2(p3b,p4b,p3b_2,p4b_2)
      dim_common = int_mb(k_range+p4b-1)
      dima_sort = int_mb(k_range+p1b-1) * int_mb(k_range+p2b-1) * int_mb
     &(k_range+h9b-1) * int_mb(k_range+h10b-1) * int_mb(k_range+h11b-1)
      dima = dim_common * dima_sort
      dimb_sort = int_mb(k_range+p3b-1)
      dimb = dim_common * dimb_sort
      IF ((dima .gt. 0) .and. (dimb .gt. 0)) THEN
      IF (.not.MA_PUSH_GET(mt_dbl,dima,'noname',l_a_sort,k_a_sort)) CALL
     & ERRQUIT('mbpt2_t3_3',1,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dima,'noname',l_a,k_a)) CALL ERRQUIT('
     &mbpt2_t3_3',2,MA_ERR)
      IF ((p4b .lt. p1b)) THEN
      CALL GET_HASH_BLOCK(d_a,dbl_mb(k_a),dima,int_mb(k_a_offset),(h11b_
     &1 - 1 + noab * (h10b_1 - 1 + noab * (h9b_1 - 1 + noab * (p2b_1 - n
     &oab - 1 + nvab * (p1b_1 - noab - 1 + nvab * (p4b_1 - noab - 1)))))
     &))
      CALL TCE_SORT_6(dbl_mb(k_a),dbl_mb(k_a_sort),int_mb(k_range+p4b-1)
     &,int_mb(k_range+p1b-1),int_mb(k_range+p2b-1),int_mb(k_range+h9b-1)
     &,int_mb(k_range+h10b-1),int_mb(k_range+h11b-1),6,5,4,3,2,1,1.0d0)
      END IF
      IF ((p1b .le. p4b) .and. (p4b .lt. p2b)) THEN
      CALL GET_HASH_BLOCK(d_a,dbl_mb(k_a),dima,int_mb(k_a_offset),(h11b_
     &1 - 1 + noab * (h10b_1 - 1 + noab * (h9b_1 - 1 + noab * (p2b_1 - n
     &oab - 1 + nvab * (p4b_1 - noab - 1 + nvab * (p1b_1 - noab - 1)))))
     &))
      CALL TCE_SORT_6(dbl_mb(k_a),dbl_mb(k_a_sort),int_mb(k_range+p1b-1)
     &,int_mb(k_range+p4b-1),int_mb(k_range+p2b-1),int_mb(k_range+h9b-1)
     &,int_mb(k_range+h10b-1),int_mb(k_range+h11b-1),6,5,4,3,1,2,-1.0d0)
      END IF
      IF ((p2b .le. p4b)) THEN
      CALL GET_HASH_BLOCK(d_a,dbl_mb(k_a),dima,int_mb(k_a_offset),(h11b_
     &1 - 1 + noab * (h10b_1 - 1 + noab * (h9b_1 - 1 + noab * (p4b_1 - n
     &oab - 1 + nvab * (p2b_1 - noab - 1 + nvab * (p1b_1 - noab - 1)))))
     &))
      CALL TCE_SORT_6(dbl_mb(k_a),dbl_mb(k_a_sort),int_mb(k_range+p1b-1)
     &,int_mb(k_range+p2b-1),int_mb(k_range+p4b-1),int_mb(k_range+h9b-1)
     &,int_mb(k_range+h10b-1),int_mb(k_range+h11b-1),6,5,4,2,1,3,1.0d0)
      END IF
      IF (.not.MA_POP_STACK(l_a)) CALL ERRQUIT('mbpt2_t3_3',3,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dimb,'noname',l_b_sort,k_b_sort)) CALL
     & ERRQUIT('mbpt2_t3_3',4,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dimb,'noname',l_b,k_b)) CALL ERRQUIT('
     &mbpt2_t3_3',5,MA_ERR)
      CALL GET_HASH_BLOCK(d_b,dbl_mb(k_b),dimb,int_mb(k_b_offset),(p4b_2
     & - 1 + (noab+nvab) * (p3b_2 - 1)))
      CALL TCE_SORT_2(dbl_mb(k_b),dbl_mb(k_b_sort),int_mb(k_range+p3b-1)
     &,int_mb(k_range+p4b-1),1,2,1.0d0)
      IF (.not.MA_POP_STACK(l_b)) CALL ERRQUIT('mbpt2_t3_3',6,MA_ERR)
      CALL DGEMM('T','N',dima_sort,dimb_sort,dim_common,1.0d0,dbl_mb(k_a
     &_sort),dim_common,dbl_mb(k_b_sort),dim_common,1.0d0,dbl_mb(k_c_sor
     &t),dima_sort)
      IF (.not.MA_POP_STACK(l_b_sort)) CALL ERRQUIT('mbpt2_t3_3',7,MA_ER
     &R)
      IF (.not.MA_POP_STACK(l_a_sort)) CALL ERRQUIT('mbpt2_t3_3',8,MA_ER
     &R)
      END IF
      END IF
      END IF
      END DO
      IF (.not.MA_PUSH_GET(mt_dbl,dimc,'noname',l_c,k_c)) CALL ERRQUIT('
     &mbpt2_t3_3',9,MA_ERR)
      IF ((p2b .le. p3b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+p3b-1)
     &,int_mb(k_range+h11b-1),int_mb(k_range+h10b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),6,5,1,4,3,2,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h11b 
     &- 1 + noab * (h10b - 1 + noab * (h9b - 1 + noab * (p3b - noab - 1 
     &+ nvab * (p2b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF ((p3b .le. p1b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+p3b-1)
     &,int_mb(k_range+h11b-1),int_mb(k_range+h10b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),1,6,5,4,3,2,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h11b 
     &- 1 + noab * (h10b - 1 + noab * (h9b - 1 + noab * (p2b - noab - 1 
     &+ nvab * (p1b - noab - 1 + nvab * (p3b - noab - 1)))))))
      END IF
      IF ((p1b .le. p3b) .and. (p3b .le. p2b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+p3b-1)
     &,int_mb(k_range+h11b-1),int_mb(k_range+h10b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),6,1,5,4,3,2,-1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h11b 
     &- 1 + noab * (h10b - 1 + noab * (h9b - 1 + noab * (p2b - noab - 1 
     &+ nvab * (p3b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF (.not.MA_POP_STACK(l_c)) CALL ERRQUIT('mbpt2_t3_3',10,MA_ERR)
      IF (.not.MA_POP_STACK(l_c_sort)) CALL ERRQUIT('mbpt2_t3_3',11,MA_E
     &RR)
      END IF
      END IF
      END IF
      next = NXTASK(nprocs,1)
      END IF
      count = count + 1
      END DO
      END DO
      END DO
      END DO
      END DO
      END DO
      next = NXTASK(-nprocs,1)
      call GA_SYNC()
      RETURN
      END
      SUBROUTINE mbpt2_t3_4(d_a,k_a_offset,d_b,k_b_offset,d_c,k_c_offset
     &)
C     $Id$
C     This is a Fortran77 program generated by Tensor Contraction Engine v.1.0
C     Copyright (c) Battelle & Pacific Northwest National Laboratory (2002)
C     i0 ( p1 p2 p3 h9 h10 h11 )_ft1t1 + = 1 * P( 9 ) * t1 ( p1 p2 h9 h10 )_t1 * i1 ( p3 h11 )_ft1
      IMPLICIT NONE
#include "global.fh"
#include "mafdecls.fh"
#include "sym.fh"
#include "errquit.fh"
#include "tce.fh"
      INTEGER d_a
      INTEGER k_a_offset
      INTEGER d_b
      INTEGER k_b_offset
      INTEGER d_c
      INTEGER k_c_offset
      INTEGER NXTASK
      INTEGER next
      INTEGER nprocs
      INTEGER count
      INTEGER p1b
      INTEGER p2b
      INTEGER p3b
      INTEGER h9b
      INTEGER h10b
      INTEGER h11b
      INTEGER dimc
      INTEGER l_c_sort
      INTEGER k_c_sort
      INTEGER p1b_1
      INTEGER p2b_1
      INTEGER h9b_1
      INTEGER h10b_1
      INTEGER p3b_2
      INTEGER h11b_2
      INTEGER dim_common
      INTEGER dima_sort
      INTEGER dima
      INTEGER dimb_sort
      INTEGER dimb
      INTEGER l_a_sort
      INTEGER k_a_sort
      INTEGER l_a
      INTEGER k_a
      INTEGER l_b_sort
      INTEGER k_b_sort
      INTEGER l_b
      INTEGER k_b
      INTEGER l_c
      INTEGER k_c
      EXTERNAL NXTASK
      nprocs = GA_NNODES()
      count = 0
      next = NXTASK(nprocs,1)
      DO p1b = noab+1,noab+nvab
      DO p2b = p1b,noab+nvab
      DO p3b = noab+1,noab+nvab
      DO h9b = 1,noab
      DO h10b = h9b,noab
      DO h11b = 1,noab
      IF (next.eq.count) THEN
      IF ((.not.restricted).or.(int_mb(k_spin+p1b-1)+int_mb(k_spin+p2b-1
     &)+int_mb(k_spin+p3b-1)+int_mb(k_spin+h9b-1)+int_mb(k_spin+h10b-1)+
     &int_mb(k_spin+h11b-1).ne.12)) THEN
      IF (int_mb(k_spin+p1b-1)+int_mb(k_spin+p2b-1)+int_mb(k_spin+p3b-1)
     & .eq. int_mb(k_spin+h9b-1)+int_mb(k_spin+h10b-1)+int_mb(k_spin+h11
     &b-1)) THEN
      IF (ieor(int_mb(k_sym+p1b-1),ieor(int_mb(k_sym+p2b-1),ieor(int_mb(
     &k_sym+p3b-1),ieor(int_mb(k_sym+h9b-1),ieor(int_mb(k_sym+h10b-1),in
     &t_mb(k_sym+h11b-1)))))) .eq. ieor(irrep_f,ieor(irrep_t1,irrep_t1))
     &) THEN
      dimc = int_mb(k_range+p1b-1) * int_mb(k_range+p2b-1) * int_mb(k_ra
     &nge+p3b-1) * int_mb(k_range+h9b-1) * int_mb(k_range+h10b-1) * int_
     &mb(k_range+h11b-1)
      IF (.not.MA_PUSH_GET(mt_dbl,dimc,'noname',l_c_sort,k_c_sort)) CALL
     & ERRQUIT('mbpt2_t3_4',0,MA_ERR)
      CALL DFILL(dimc,0.0d0,dbl_mb(k_c_sort),1)
      IF (int_mb(k_spin+p1b-1)+int_mb(k_spin+p2b-1) .eq. int_mb(k_spin+h
     &9b-1)+int_mb(k_spin+h10b-1)) THEN
      IF (ieor(int_mb(k_sym+p1b-1),ieor(int_mb(k_sym+p2b-1),ieor(int_mb(
     &k_sym+h9b-1),int_mb(k_sym+h10b-1)))) .eq. irrep_t1) THEN
      CALL TCE_RESTRICTED_4(p1b,p2b,h9b,h10b,p1b_1,p2b_1,h9b_1,h10b_1)
      CALL TCE_RESTRICTED_2(p3b,h11b,p3b_2,h11b_2)
      dim_common = 1
      dima_sort = int_mb(k_range+p1b-1) * int_mb(k_range+p2b-1) * int_mb
     &(k_range+h9b-1) * int_mb(k_range+h10b-1)
      dima = dim_common * dima_sort
      dimb_sort = int_mb(k_range+p3b-1) * int_mb(k_range+h11b-1)
      dimb = dim_common * dimb_sort
      IF ((dima .gt. 0) .and. (dimb .gt. 0)) THEN
      IF (.not.MA_PUSH_GET(mt_dbl,dima,'noname',l_a_sort,k_a_sort)) CALL
     & ERRQUIT('mbpt2_t3_4',1,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dima,'noname',l_a,k_a)) CALL ERRQUIT('
     &mbpt2_t3_4',2,MA_ERR)
      CALL GET_HASH_BLOCK(d_a,dbl_mb(k_a),dima,int_mb(k_a_offset),(h10b_
     &1 - 1 + noab * (h9b_1 - 1 + noab * (p2b_1 - noab - 1 + nvab * (p1b
     &_1 - noab - 1)))))
      CALL TCE_SORT_4(dbl_mb(k_a),dbl_mb(k_a_sort),int_mb(k_range+p1b-1)
     &,int_mb(k_range+p2b-1),int_mb(k_range+h9b-1),int_mb(k_range+h10b-1
     &),4,3,2,1,1.0d0)
      IF (.not.MA_POP_STACK(l_a)) CALL ERRQUIT('mbpt2_t3_4',3,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dimb,'noname',l_b_sort,k_b_sort)) CALL
     & ERRQUIT('mbpt2_t3_4',4,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dimb,'noname',l_b,k_b)) CALL ERRQUIT('
     &mbpt2_t3_4',5,MA_ERR)
      CALL GET_HASH_BLOCK(d_b,dbl_mb(k_b),dimb,int_mb(k_b_offset),(h11b_
     &2 - 1 + noab * (p3b_2 - noab - 1)))
      CALL TCE_SORT_2(dbl_mb(k_b),dbl_mb(k_b_sort),int_mb(k_range+p3b-1)
     &,int_mb(k_range+h11b-1),2,1,1.0d0)
      IF (.not.MA_POP_STACK(l_b)) CALL ERRQUIT('mbpt2_t3_4',6,MA_ERR)
      CALL DGEMM('T','N',dima_sort,dimb_sort,dim_common,1.0d0,dbl_mb(k_a
     &_sort),dim_common,dbl_mb(k_b_sort),dim_common,1.0d0,dbl_mb(k_c_sor
     &t),dima_sort)
      IF (.not.MA_POP_STACK(l_b_sort)) CALL ERRQUIT('mbpt2_t3_4',7,MA_ER
     &R)
      IF (.not.MA_POP_STACK(l_a_sort)) CALL ERRQUIT('mbpt2_t3_4',8,MA_ER
     &R)
      END IF
      END IF
      END IF
      IF (.not.MA_PUSH_GET(mt_dbl,dimc,'noname',l_c,k_c)) CALL ERRQUIT('
     &mbpt2_t3_4',9,MA_ERR)
      IF ((p2b .le. p3b) .and. (h10b .le. h11b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+h10b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),6,5,2,4,3,1,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h11b 
     &- 1 + noab * (h10b - 1 + noab * (h9b - 1 + noab * (p3b - noab - 1 
     &+ nvab * (p2b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF ((p2b .le. p3b) .and. (h11b .le. h9b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+h10b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),6,5,2,1,4,3,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h10b 
     &- 1 + noab * (h9b - 1 + noab * (h11b - 1 + noab * (p3b - noab - 1 
     &+ nvab * (p2b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF ((p2b .le. p3b) .and. (h9b .le. h11b) .and. (h11b .le. h10b)) T
     &HEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+h10b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),6,5,2,4,1,3,-1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h10b 
     &- 1 + noab * (h11b - 1 + noab * (h9b - 1 + noab * (p3b - noab - 1 
     &+ nvab * (p2b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF ((p3b .le. p1b) .and. (h10b .le. h11b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+h10b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),2,6,5,4,3,1,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h11b 
     &- 1 + noab * (h10b - 1 + noab * (h9b - 1 + noab * (p2b - noab - 1 
     &+ nvab * (p1b - noab - 1 + nvab * (p3b - noab - 1)))))))
      END IF
      IF ((p3b .le. p1b) .and. (h11b .le. h9b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+h10b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),2,6,5,1,4,3,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h10b 
     &- 1 + noab * (h9b - 1 + noab * (h11b - 1 + noab * (p2b - noab - 1 
     &+ nvab * (p1b - noab - 1 + nvab * (p3b - noab - 1)))))))
      END IF
      IF ((p3b .le. p1b) .and. (h9b .le. h11b) .and. (h11b .le. h10b)) T
     &HEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+h10b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),2,6,5,4,1,3,-1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h10b 
     &- 1 + noab * (h11b - 1 + noab * (h9b - 1 + noab * (p2b - noab - 1 
     &+ nvab * (p1b - noab - 1 + nvab * (p3b - noab - 1)))))))
      END IF
      IF ((p1b .le. p3b) .and. (p3b .le. p2b) .and. (h10b .le. h11b)) TH
     &EN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+h10b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),6,2,5,4,3,1,-1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h11b 
     &- 1 + noab * (h10b - 1 + noab * (h9b - 1 + noab * (p2b - noab - 1 
     &+ nvab * (p3b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF ((p1b .le. p3b) .and. (p3b .le. p2b) .and. (h11b .le. h9b)) THE
     &N
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+h10b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),6,2,5,1,4,3,-1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h10b 
     &- 1 + noab * (h9b - 1 + noab * (h11b - 1 + noab * (p2b - noab - 1 
     &+ nvab * (p3b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF ((p1b .le. p3b) .and. (p3b .le. p2b) .and. (h9b .le. h11b) .and
     &. (h11b .le. h10b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+h10b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),6,2,5,4,1,3,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h10b 
     &- 1 + noab * (h11b - 1 + noab * (h9b - 1 + noab * (p2b - noab - 1 
     &+ nvab * (p3b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF (.not.MA_POP_STACK(l_c)) CALL ERRQUIT('mbpt2_t3_4',10,MA_ERR)
      IF (.not.MA_POP_STACK(l_c_sort)) CALL ERRQUIT('mbpt2_t3_4',11,MA_E
     &RR)
      END IF
      END IF
      END IF
      next = NXTASK(nprocs,1)
      END IF
      count = count + 1
      END DO
      END DO
      END DO
      END DO
      END DO
      END DO
      next = NXTASK(-nprocs,1)
      call GA_SYNC()
      RETURN
      END
      SUBROUTINE mbpt2_t3_4_1(d_a,k_a_offset,d_b,k_b_offset,d_c,k_c_offs
     &et)
C     $Id$
C     This is a Fortran77 program generated by Tensor Contraction Engine v.1.0
C     Copyright (c) Battelle & Pacific Northwest National Laboratory (2002)
C     i1 ( p1 h9 )_ft1 + = -1 * Sum ( h5 p4 ) * t1 ( p1 p4 h5 h9 )_t1 * f ( h5 p4 )_f
      IMPLICIT NONE
#include "global.fh"
#include "mafdecls.fh"
#include "sym.fh"
#include "errquit.fh"
#include "tce.fh"
      INTEGER d_a
      INTEGER k_a_offset
      INTEGER d_b
      INTEGER k_b_offset
      INTEGER d_c
      INTEGER k_c_offset
      INTEGER NXTASK
      INTEGER next
      INTEGER nprocs
      INTEGER count
      INTEGER p1b
      INTEGER h9b
      INTEGER dimc
      INTEGER l_c_sort
      INTEGER k_c_sort
      INTEGER p4b
      INTEGER h5b
      INTEGER p1b_1
      INTEGER p4b_1
      INTEGER h9b_1
      INTEGER h5b_1
      INTEGER h5b_2
      INTEGER p4b_2
      INTEGER dim_common
      INTEGER dima_sort
      INTEGER dima
      INTEGER dimb_sort
      INTEGER dimb
      INTEGER l_a_sort
      INTEGER k_a_sort
      INTEGER l_a
      INTEGER k_a
      INTEGER l_b_sort
      INTEGER k_b_sort
      INTEGER l_b
      INTEGER k_b
      INTEGER l_c
      INTEGER k_c
      EXTERNAL NXTASK
      nprocs = GA_NNODES()
      count = 0
      next = NXTASK(nprocs,1)
      DO p1b = noab+1,noab+nvab
      DO h9b = 1,noab
      IF (next.eq.count) THEN
      IF ((.not.restricted).or.(int_mb(k_spin+p1b-1)+int_mb(k_spin+h9b-1
     &).ne.4)) THEN
      IF (int_mb(k_spin+p1b-1) .eq. int_mb(k_spin+h9b-1)) THEN
      IF (ieor(int_mb(k_sym+p1b-1),int_mb(k_sym+h9b-1)) .eq. ieor(irrep_
     &f,irrep_t1)) THEN
      dimc = int_mb(k_range+p1b-1) * int_mb(k_range+h9b-1)
      IF (.not.MA_PUSH_GET(mt_dbl,dimc,'noname',l_c_sort,k_c_sort)) CALL
     & ERRQUIT('mbpt2_t3_4_1',0,MA_ERR)
      CALL DFILL(dimc,0.0d0,dbl_mb(k_c_sort),1)
      DO p4b = noab+1,noab+nvab
      DO h5b = 1,noab
      IF (int_mb(k_spin+p1b-1)+int_mb(k_spin+p4b-1) .eq. int_mb(k_spin+h
     &9b-1)+int_mb(k_spin+h5b-1)) THEN
      IF (ieor(int_mb(k_sym+p1b-1),ieor(int_mb(k_sym+p4b-1),ieor(int_mb(
     &k_sym+h9b-1),int_mb(k_sym+h5b-1)))) .eq. irrep_t1) THEN
      CALL TCE_RESTRICTED_4(p1b,p4b,h9b,h5b,p1b_1,p4b_1,h9b_1,h5b_1)
      CALL TCE_RESTRICTED_2(h5b,p4b,h5b_2,p4b_2)
      dim_common = int_mb(k_range+p4b-1) * int_mb(k_range+h5b-1)
      dima_sort = int_mb(k_range+p1b-1) * int_mb(k_range+h9b-1)
      dima = dim_common * dima_sort
      dimb_sort = 1
      dimb = dim_common * dimb_sort
      IF ((dima .gt. 0) .and. (dimb .gt. 0)) THEN
      IF (.not.MA_PUSH_GET(mt_dbl,dima,'noname',l_a_sort,k_a_sort)) CALL
     & ERRQUIT('mbpt2_t3_4_1',1,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dima,'noname',l_a,k_a)) CALL ERRQUIT('
     &mbpt2_t3_4_1',2,MA_ERR)
      IF ((p4b .lt. p1b) .and. (h5b .le. h9b)) THEN
      CALL GET_HASH_BLOCK(d_a,dbl_mb(k_a),dima,int_mb(k_a_offset),(h9b_1
     & - 1 + noab * (h5b_1 - 1 + noab * (p1b_1 - noab - 1 + nvab * (p4b_
     &1 - noab - 1)))))
      CALL TCE_SORT_4(dbl_mb(k_a),dbl_mb(k_a_sort),int_mb(k_range+p4b-1)
     &,int_mb(k_range+p1b-1),int_mb(k_range+h5b-1),int_mb(k_range+h9b-1)
     &,4,2,3,1,-1.0d0)
      END IF
      IF ((p4b .lt. p1b) .and. (h9b .lt. h5b)) THEN
      CALL GET_HASH_BLOCK(d_a,dbl_mb(k_a),dima,int_mb(k_a_offset),(h5b_1
     & - 1 + noab * (h9b_1 - 1 + noab * (p1b_1 - noab - 1 + nvab * (p4b_
     &1 - noab - 1)))))
      CALL TCE_SORT_4(dbl_mb(k_a),dbl_mb(k_a_sort),int_mb(k_range+p4b-1)
     &,int_mb(k_range+p1b-1),int_mb(k_range+h9b-1),int_mb(k_range+h5b-1)
     &,3,2,4,1,1.0d0)
      END IF
      IF ((p1b .le. p4b) .and. (h5b .le. h9b)) THEN
      CALL GET_HASH_BLOCK(d_a,dbl_mb(k_a),dima,int_mb(k_a_offset),(h9b_1
     & - 1 + noab * (h5b_1 - 1 + noab * (p4b_1 - noab - 1 + nvab * (p1b_
     &1 - noab - 1)))))
      CALL TCE_SORT_4(dbl_mb(k_a),dbl_mb(k_a_sort),int_mb(k_range+p1b-1)
     &,int_mb(k_range+p4b-1),int_mb(k_range+h5b-1),int_mb(k_range+h9b-1)
     &,4,1,3,2,1.0d0)
      END IF
      IF ((p1b .le. p4b) .and. (h9b .lt. h5b)) THEN
      CALL GET_HASH_BLOCK(d_a,dbl_mb(k_a),dima,int_mb(k_a_offset),(h5b_1
     & - 1 + noab * (h9b_1 - 1 + noab * (p4b_1 - noab - 1 + nvab * (p1b_
     &1 - noab - 1)))))
      CALL TCE_SORT_4(dbl_mb(k_a),dbl_mb(k_a_sort),int_mb(k_range+p1b-1)
     &,int_mb(k_range+p4b-1),int_mb(k_range+h9b-1),int_mb(k_range+h5b-1)
     &,3,1,4,2,-1.0d0)
      END IF
      IF (.not.MA_POP_STACK(l_a)) CALL ERRQUIT('mbpt2_t3_4_1',3,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dimb,'noname',l_b_sort,k_b_sort)) CALL
     & ERRQUIT('mbpt2_t3_4_1',4,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dimb,'noname',l_b,k_b)) CALL ERRQUIT('
     &mbpt2_t3_4_1',5,MA_ERR)
      CALL GET_HASH_BLOCK(d_b,dbl_mb(k_b),dimb,int_mb(k_b_offset),(p4b_2
     & - 1 + (noab+nvab) * (h5b_2 - 1)))
      CALL TCE_SORT_2(dbl_mb(k_b),dbl_mb(k_b_sort),int_mb(k_range+h5b-1)
     &,int_mb(k_range+p4b-1),1,2,1.0d0)
      IF (.not.MA_POP_STACK(l_b)) CALL ERRQUIT('mbpt2_t3_4_1',6,MA_ERR)
      CALL DGEMM('T','N',dima_sort,dimb_sort,dim_common,1.0d0,dbl_mb(k_a
     &_sort),dim_common,dbl_mb(k_b_sort),dim_common,1.0d0,dbl_mb(k_c_sor
     &t),dima_sort)
      IF (.not.MA_POP_STACK(l_b_sort)) CALL ERRQUIT('mbpt2_t3_4_1',7,MA_
     &ERR)
      IF (.not.MA_POP_STACK(l_a_sort)) CALL ERRQUIT('mbpt2_t3_4_1',8,MA_
     &ERR)
      END IF
      END IF
      END IF
      END DO
      END DO
      IF (.not.MA_PUSH_GET(mt_dbl,dimc,'noname',l_c,k_c)) CALL ERRQUIT('
     &mbpt2_t3_4_1',9,MA_ERR)
      CALL TCE_SORT_2(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h9b-1)
     &,int_mb(k_range+p1b-1),2,1,-1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h9b -
     & 1 + noab * (p1b - noab - 1)))
      IF (.not.MA_POP_STACK(l_c)) CALL ERRQUIT('mbpt2_t3_4_1',10,MA_ERR)
      IF (.not.MA_POP_STACK(l_c_sort)) CALL ERRQUIT('mbpt2_t3_4_1',11,MA
     &_ERR)
      END IF
      END IF
      END IF
      next = NXTASK(nprocs,1)
      END IF
      count = count + 1
      END DO
      END DO
      next = NXTASK(-nprocs,1)
      call GA_SYNC()
      RETURN
      END
      SUBROUTINE OFFSET_mbpt2_t3_4_1(l_a_offset,k_a_offset,size)
C     $Id$
C     This is a Fortran77 program generated by Tensor Contraction Engine v.1.0
C     Copyright (c) Battelle & Pacific Northwest National Laboratory (2002)
C     i1 ( p1 h9 )_ft1
      IMPLICIT NONE
#include "global.fh"
#include "mafdecls.fh"
#include "sym.fh"
#include "errquit.fh"
#include "tce.fh"
      INTEGER l_a_offset
      INTEGER k_a_offset
      INTEGER size
      INTEGER length
      INTEGER addr
      INTEGER p1b
      INTEGER h9b
      length = 0
      DO p1b = noab+1,noab+nvab
      DO h9b = 1,noab
      IF (int_mb(k_spin+p1b-1) .eq. int_mb(k_spin+h9b-1)) THEN
      IF (ieor(int_mb(k_sym+p1b-1),int_mb(k_sym+h9b-1)) .eq. ieor(irrep_
     &f,irrep_t1)) THEN
      IF ((.not.restricted).or.(int_mb(k_spin+p1b-1)+int_mb(k_spin+h9b-1
     &).ne.4)) THEN
      length = length + 1
      END IF
      END IF
      END IF
      END DO
      END DO
      IF (.not.MA_PUSH_GET(mt_int,2*length+1,'noname',l_a_offset,k_a_off
     &set)) CALL ERRQUIT('mbpt2_t3_4_1',0,MA_ERR)
      int_mb(k_a_offset) = length
      addr = 0
      size = 0
      DO p1b = noab+1,noab+nvab
      DO h9b = 1,noab
      IF (int_mb(k_spin+p1b-1) .eq. int_mb(k_spin+h9b-1)) THEN
      IF (ieor(int_mb(k_sym+p1b-1),int_mb(k_sym+h9b-1)) .eq. ieor(irrep_
     &f,irrep_t1)) THEN
      IF ((.not.restricted).or.(int_mb(k_spin+p1b-1)+int_mb(k_spin+h9b-1
     &).ne.4)) THEN
      addr = addr + 1
      int_mb(k_a_offset+addr) = h9b - 1 + noab * (p1b - noab - 1)
      int_mb(k_a_offset+length+addr) = size
      size = size + int_mb(k_range+p1b-1) * int_mb(k_range+h9b-1)
      END IF
      END IF
      END IF
      END DO
      END DO
      RETURN
      END
      SUBROUTINE mbpt2_t3_5(d_a,k_a_offset,d_b,k_b_offset,d_c,k_c_offset
     &)
C     $Id$
C     This is a Fortran77 program generated by Tensor Contraction Engine v.1.0
C     Copyright (c) Battelle & Pacific Northwest National Laboratory (2002)
C     i0 ( p1 p2 p3 h9 h10 h11 )_ft1t1 + = 1 * P( 9 ) * Sum ( h6 ) * t1 ( p1 p2 h6 h9 )_t1 * i1 ( h6 p3 h10 h11 )_ft1
      IMPLICIT NONE
#include "global.fh"
#include "mafdecls.fh"
#include "sym.fh"
#include "errquit.fh"
#include "tce.fh"
      INTEGER d_a
      INTEGER k_a_offset
      INTEGER d_b
      INTEGER k_b_offset
      INTEGER d_c
      INTEGER k_c_offset
      INTEGER NXTASK
      INTEGER next
      INTEGER nprocs
      INTEGER count
      INTEGER p1b
      INTEGER p2b
      INTEGER p3b
      INTEGER h9b
      INTEGER h10b
      INTEGER h11b
      INTEGER dimc
      INTEGER l_c_sort
      INTEGER k_c_sort
      INTEGER h6b
      INTEGER p1b_1
      INTEGER p2b_1
      INTEGER h9b_1
      INTEGER h6b_1
      INTEGER p3b_2
      INTEGER h6b_2
      INTEGER h10b_2
      INTEGER h11b_2
      INTEGER dim_common
      INTEGER dima_sort
      INTEGER dima
      INTEGER dimb_sort
      INTEGER dimb
      INTEGER l_a_sort
      INTEGER k_a_sort
      INTEGER l_a
      INTEGER k_a
      INTEGER l_b_sort
      INTEGER k_b_sort
      INTEGER l_b
      INTEGER k_b
      INTEGER l_c
      INTEGER k_c
      EXTERNAL NXTASK
      nprocs = GA_NNODES()
      count = 0
      next = NXTASK(nprocs,1)
      DO p1b = noab+1,noab+nvab
      DO p2b = p1b,noab+nvab
      DO p3b = noab+1,noab+nvab
      DO h9b = 1,noab
      DO h10b = 1,noab
      DO h11b = h10b,noab
      IF (next.eq.count) THEN
      IF ((.not.restricted).or.(int_mb(k_spin+p1b-1)+int_mb(k_spin+p2b-1
     &)+int_mb(k_spin+p3b-1)+int_mb(k_spin+h9b-1)+int_mb(k_spin+h10b-1)+
     &int_mb(k_spin+h11b-1).ne.12)) THEN
      IF (int_mb(k_spin+p1b-1)+int_mb(k_spin+p2b-1)+int_mb(k_spin+p3b-1)
     & .eq. int_mb(k_spin+h9b-1)+int_mb(k_spin+h10b-1)+int_mb(k_spin+h11
     &b-1)) THEN
      IF (ieor(int_mb(k_sym+p1b-1),ieor(int_mb(k_sym+p2b-1),ieor(int_mb(
     &k_sym+p3b-1),ieor(int_mb(k_sym+h9b-1),ieor(int_mb(k_sym+h10b-1),in
     &t_mb(k_sym+h11b-1)))))) .eq. ieor(irrep_f,ieor(irrep_t1,irrep_t1))
     &) THEN
      dimc = int_mb(k_range+p1b-1) * int_mb(k_range+p2b-1) * int_mb(k_ra
     &nge+p3b-1) * int_mb(k_range+h9b-1) * int_mb(k_range+h10b-1) * int_
     &mb(k_range+h11b-1)
      IF (.not.MA_PUSH_GET(mt_dbl,dimc,'noname',l_c_sort,k_c_sort)) CALL
     & ERRQUIT('mbpt2_t3_5',0,MA_ERR)
      CALL DFILL(dimc,0.0d0,dbl_mb(k_c_sort),1)
      DO h6b = 1,noab
      IF (int_mb(k_spin+p1b-1)+int_mb(k_spin+p2b-1) .eq. int_mb(k_spin+h
     &9b-1)+int_mb(k_spin+h6b-1)) THEN
      IF (ieor(int_mb(k_sym+p1b-1),ieor(int_mb(k_sym+p2b-1),ieor(int_mb(
     &k_sym+h9b-1),int_mb(k_sym+h6b-1)))) .eq. irrep_t1) THEN
      CALL TCE_RESTRICTED_4(p1b,p2b,h9b,h6b,p1b_1,p2b_1,h9b_1,h6b_1)
      CALL TCE_RESTRICTED_4(p3b,h6b,h10b,h11b,p3b_2,h6b_2,h10b_2,h11b_2)
      dim_common = int_mb(k_range+h6b-1)
      dima_sort = int_mb(k_range+p1b-1) * int_mb(k_range+p2b-1) * int_mb
     &(k_range+h9b-1)
      dima = dim_common * dima_sort
      dimb_sort = int_mb(k_range+p3b-1) * int_mb(k_range+h10b-1) * int_m
     &b(k_range+h11b-1)
      dimb = dim_common * dimb_sort
      IF ((dima .gt. 0) .and. (dimb .gt. 0)) THEN
      IF (.not.MA_PUSH_GET(mt_dbl,dima,'noname',l_a_sort,k_a_sort)) CALL
     & ERRQUIT('mbpt2_t3_5',1,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dima,'noname',l_a,k_a)) CALL ERRQUIT('
     &mbpt2_t3_5',2,MA_ERR)
      IF ((h6b .le. h9b)) THEN
      CALL GET_HASH_BLOCK(d_a,dbl_mb(k_a),dima,int_mb(k_a_offset),(h9b_1
     & - 1 + noab * (h6b_1 - 1 + noab * (p2b_1 - noab - 1 + nvab * (p1b_
     &1 - noab - 1)))))
      CALL TCE_SORT_4(dbl_mb(k_a),dbl_mb(k_a_sort),int_mb(k_range+p1b-1)
     &,int_mb(k_range+p2b-1),int_mb(k_range+h6b-1),int_mb(k_range+h9b-1)
     &,4,2,1,3,1.0d0)
      END IF
      IF ((h9b .lt. h6b)) THEN
      CALL GET_HASH_BLOCK(d_a,dbl_mb(k_a),dima,int_mb(k_a_offset),(h6b_1
     & - 1 + noab * (h9b_1 - 1 + noab * (p2b_1 - noab - 1 + nvab * (p1b_
     &1 - noab - 1)))))
      CALL TCE_SORT_4(dbl_mb(k_a),dbl_mb(k_a_sort),int_mb(k_range+p1b-1)
     &,int_mb(k_range+p2b-1),int_mb(k_range+h9b-1),int_mb(k_range+h6b-1)
     &,3,2,1,4,-1.0d0)
      END IF
      IF (.not.MA_POP_STACK(l_a)) CALL ERRQUIT('mbpt2_t3_5',3,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dimb,'noname',l_b_sort,k_b_sort)) CALL
     & ERRQUIT('mbpt2_t3_5',4,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dimb,'noname',l_b,k_b)) CALL ERRQUIT('
     &mbpt2_t3_5',5,MA_ERR)
      CALL GET_HASH_BLOCK(d_b,dbl_mb(k_b),dimb,int_mb(k_b_offset),(h11b_
     &2 - 1 + noab * (h10b_2 - 1 + noab * (h6b_2 - 1 + noab * (p3b_2 - n
     &oab - 1)))))
      CALL TCE_SORT_4(dbl_mb(k_b),dbl_mb(k_b_sort),int_mb(k_range+p3b-1)
     &,int_mb(k_range+h6b-1),int_mb(k_range+h10b-1),int_mb(k_range+h11b-
     &1),4,3,1,2,1.0d0)
      IF (.not.MA_POP_STACK(l_b)) CALL ERRQUIT('mbpt2_t3_5',6,MA_ERR)
      CALL DGEMM('T','N',dima_sort,dimb_sort,dim_common,1.0d0,dbl_mb(k_a
     &_sort),dim_common,dbl_mb(k_b_sort),dim_common,1.0d0,dbl_mb(k_c_sor
     &t),dima_sort)
      IF (.not.MA_POP_STACK(l_b_sort)) CALL ERRQUIT('mbpt2_t3_5',7,MA_ER
     &R)
      IF (.not.MA_POP_STACK(l_a_sort)) CALL ERRQUIT('mbpt2_t3_5',8,MA_ER
     &R)
      END IF
      END IF
      END IF
      END DO
      IF (.not.MA_PUSH_GET(mt_dbl,dimc,'noname',l_c,k_c)) CALL ERRQUIT('
     &mbpt2_t3_5',9,MA_ERR)
      IF ((p2b .le. p3b) .and. (h9b .le. h10b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+h10b-1),int_mb(k_range+p3b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),6,5,3,4,2,1,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h11b 
     &- 1 + noab * (h10b - 1 + noab * (h9b - 1 + noab * (p3b - noab - 1 
     &+ nvab * (p2b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF ((p2b .le. p3b) .and. (h10b .le. h9b) .and. (h9b .le. h11b)) TH
     &EN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+h10b-1),int_mb(k_range+p3b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),6,5,3,2,4,1,-1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h11b 
     &- 1 + noab * (h9b - 1 + noab * (h10b - 1 + noab * (p3b - noab - 1 
     &+ nvab * (p2b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF ((p2b .le. p3b) .and. (h11b .le. h9b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+h10b-1),int_mb(k_range+p3b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),6,5,3,2,1,4,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h9b -
     & 1 + noab * (h11b - 1 + noab * (h10b - 1 + noab * (p3b - noab - 1 
     &+ nvab * (p2b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF ((p3b .le. p1b) .and. (h9b .le. h10b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+h10b-1),int_mb(k_range+p3b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),3,6,5,4,2,1,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h11b 
     &- 1 + noab * (h10b - 1 + noab * (h9b - 1 + noab * (p2b - noab - 1 
     &+ nvab * (p1b - noab - 1 + nvab * (p3b - noab - 1)))))))
      END IF
      IF ((p3b .le. p1b) .and. (h10b .le. h9b) .and. (h9b .le. h11b)) TH
     &EN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+h10b-1),int_mb(k_range+p3b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),3,6,5,2,4,1,-1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h11b 
     &- 1 + noab * (h9b - 1 + noab * (h10b - 1 + noab * (p2b - noab - 1 
     &+ nvab * (p1b - noab - 1 + nvab * (p3b - noab - 1)))))))
      END IF
      IF ((p3b .le. p1b) .and. (h11b .le. h9b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+h10b-1),int_mb(k_range+p3b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),3,6,5,2,1,4,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h9b -
     & 1 + noab * (h11b - 1 + noab * (h10b - 1 + noab * (p2b - noab - 1 
     &+ nvab * (p1b - noab - 1 + nvab * (p3b - noab - 1)))))))
      END IF
      IF ((p1b .le. p3b) .and. (p3b .le. p2b) .and. (h9b .le. h10b)) THE
     &N
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+h10b-1),int_mb(k_range+p3b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),6,3,5,4,2,1,-1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h11b 
     &- 1 + noab * (h10b - 1 + noab * (h9b - 1 + noab * (p2b - noab - 1 
     &+ nvab * (p3b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF ((p1b .le. p3b) .and. (p3b .le. p2b) .and. (h10b .le. h9b) .and
     &. (h9b .le. h11b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+h10b-1),int_mb(k_range+p3b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),6,3,5,2,4,1,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h11b 
     &- 1 + noab * (h9b - 1 + noab * (h10b - 1 + noab * (p2b - noab - 1 
     &+ nvab * (p3b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF ((p1b .le. p3b) .and. (p3b .le. p2b) .and. (h11b .le. h9b)) THE
     &N
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+h10b-1),int_mb(k_range+p3b-1),int_mb(k_range+h9b-
     &1),int_mb(k_range+p2b-1),int_mb(k_range+p1b-1),6,3,5,2,1,4,-1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h9b -
     & 1 + noab * (h11b - 1 + noab * (h10b - 1 + noab * (p2b - noab - 1 
     &+ nvab * (p3b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF (.not.MA_POP_STACK(l_c)) CALL ERRQUIT('mbpt2_t3_5',10,MA_ERR)
      IF (.not.MA_POP_STACK(l_c_sort)) CALL ERRQUIT('mbpt2_t3_5',11,MA_E
     &RR)
      END IF
      END IF
      END IF
      next = NXTASK(nprocs,1)
      END IF
      count = count + 1
      END DO
      END DO
      END DO
      END DO
      END DO
      END DO
      next = NXTASK(-nprocs,1)
      call GA_SYNC()
      RETURN
      END
      SUBROUTINE mbpt2_t3_5_1(d_a,k_a_offset,d_b,k_b_offset,d_c,k_c_offs
     &et)
C     $Id$
C     This is a Fortran77 program generated by Tensor Contraction Engine v.1.0
C     Copyright (c) Battelle & Pacific Northwest National Laboratory (2002)
C     i1 ( h6 p1 h9 h10 )_ft1 + = -1 * Sum ( p4 ) * t1 ( p1 p4 h9 h10 )_t1 * f ( h6 p4 )_f
      IMPLICIT NONE
#include "global.fh"
#include "mafdecls.fh"
#include "sym.fh"
#include "errquit.fh"
#include "tce.fh"
      INTEGER d_a
      INTEGER k_a_offset
      INTEGER d_b
      INTEGER k_b_offset
      INTEGER d_c
      INTEGER k_c_offset
      INTEGER NXTASK
      INTEGER next
      INTEGER nprocs
      INTEGER count
      INTEGER p1b
      INTEGER h6b
      INTEGER h9b
      INTEGER h10b
      INTEGER dimc
      INTEGER l_c_sort
      INTEGER k_c_sort
      INTEGER p4b
      INTEGER p1b_1
      INTEGER p4b_1
      INTEGER h9b_1
      INTEGER h10b_1
      INTEGER h6b_2
      INTEGER p4b_2
      INTEGER dim_common
      INTEGER dima_sort
      INTEGER dima
      INTEGER dimb_sort
      INTEGER dimb
      INTEGER l_a_sort
      INTEGER k_a_sort
      INTEGER l_a
      INTEGER k_a
      INTEGER l_b_sort
      INTEGER k_b_sort
      INTEGER l_b
      INTEGER k_b
      INTEGER l_c
      INTEGER k_c
      EXTERNAL NXTASK
      nprocs = GA_NNODES()
      count = 0
      next = NXTASK(nprocs,1)
      DO p1b = noab+1,noab+nvab
      DO h6b = 1,noab
      DO h9b = 1,noab
      DO h10b = h9b,noab
      IF (next.eq.count) THEN
      IF ((.not.restricted).or.(int_mb(k_spin+p1b-1)+int_mb(k_spin+h6b-1
     &)+int_mb(k_spin+h9b-1)+int_mb(k_spin+h10b-1).ne.8)) THEN
      IF (int_mb(k_spin+p1b-1)+int_mb(k_spin+h6b-1) .eq. int_mb(k_spin+h
     &9b-1)+int_mb(k_spin+h10b-1)) THEN
      IF (ieor(int_mb(k_sym+p1b-1),ieor(int_mb(k_sym+h6b-1),ieor(int_mb(
     &k_sym+h9b-1),int_mb(k_sym+h10b-1)))) .eq. ieor(irrep_f,irrep_t1)) 
     &THEN
      dimc = int_mb(k_range+p1b-1) * int_mb(k_range+h6b-1) * int_mb(k_ra
     &nge+h9b-1) * int_mb(k_range+h10b-1)
      IF (.not.MA_PUSH_GET(mt_dbl,dimc,'noname',l_c_sort,k_c_sort)) CALL
     & ERRQUIT('mbpt2_t3_5_1',0,MA_ERR)
      CALL DFILL(dimc,0.0d0,dbl_mb(k_c_sort),1)
      DO p4b = noab+1,noab+nvab
      IF (int_mb(k_spin+p1b-1)+int_mb(k_spin+p4b-1) .eq. int_mb(k_spin+h
     &9b-1)+int_mb(k_spin+h10b-1)) THEN
      IF (ieor(int_mb(k_sym+p1b-1),ieor(int_mb(k_sym+p4b-1),ieor(int_mb(
     &k_sym+h9b-1),int_mb(k_sym+h10b-1)))) .eq. irrep_t1) THEN
      CALL TCE_RESTRICTED_4(p1b,p4b,h9b,h10b,p1b_1,p4b_1,h9b_1,h10b_1)
      CALL TCE_RESTRICTED_2(h6b,p4b,h6b_2,p4b_2)
      dim_common = int_mb(k_range+p4b-1)
      dima_sort = int_mb(k_range+p1b-1) * int_mb(k_range+h9b-1) * int_mb
     &(k_range+h10b-1)
      dima = dim_common * dima_sort
      dimb_sort = int_mb(k_range+h6b-1)
      dimb = dim_common * dimb_sort
      IF ((dima .gt. 0) .and. (dimb .gt. 0)) THEN
      IF (.not.MA_PUSH_GET(mt_dbl,dima,'noname',l_a_sort,k_a_sort)) CALL
     & ERRQUIT('mbpt2_t3_5_1',1,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dima,'noname',l_a,k_a)) CALL ERRQUIT('
     &mbpt2_t3_5_1',2,MA_ERR)
      IF ((p4b .lt. p1b)) THEN
      CALL GET_HASH_BLOCK(d_a,dbl_mb(k_a),dima,int_mb(k_a_offset),(h10b_
     &1 - 1 + noab * (h9b_1 - 1 + noab * (p1b_1 - noab - 1 + nvab * (p4b
     &_1 - noab - 1)))))
      CALL TCE_SORT_4(dbl_mb(k_a),dbl_mb(k_a_sort),int_mb(k_range+p4b-1)
     &,int_mb(k_range+p1b-1),int_mb(k_range+h9b-1),int_mb(k_range+h10b-1
     &),4,3,2,1,-1.0d0)
      END IF
      IF ((p1b .le. p4b)) THEN
      CALL GET_HASH_BLOCK(d_a,dbl_mb(k_a),dima,int_mb(k_a_offset),(h10b_
     &1 - 1 + noab * (h9b_1 - 1 + noab * (p4b_1 - noab - 1 + nvab * (p1b
     &_1 - noab - 1)))))
      CALL TCE_SORT_4(dbl_mb(k_a),dbl_mb(k_a_sort),int_mb(k_range+p1b-1)
     &,int_mb(k_range+p4b-1),int_mb(k_range+h9b-1),int_mb(k_range+h10b-1
     &),4,3,1,2,1.0d0)
      END IF
      IF (.not.MA_POP_STACK(l_a)) CALL ERRQUIT('mbpt2_t3_5_1',3,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dimb,'noname',l_b_sort,k_b_sort)) CALL
     & ERRQUIT('mbpt2_t3_5_1',4,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dimb,'noname',l_b,k_b)) CALL ERRQUIT('
     &mbpt2_t3_5_1',5,MA_ERR)
      CALL GET_HASH_BLOCK(d_b,dbl_mb(k_b),dimb,int_mb(k_b_offset),(p4b_2
     & - 1 + (noab+nvab) * (h6b_2 - 1)))
      CALL TCE_SORT_2(dbl_mb(k_b),dbl_mb(k_b_sort),int_mb(k_range+h6b-1)
     &,int_mb(k_range+p4b-1),1,2,1.0d0)
      IF (.not.MA_POP_STACK(l_b)) CALL ERRQUIT('mbpt2_t3_5_1',6,MA_ERR)
      CALL DGEMM('T','N',dima_sort,dimb_sort,dim_common,1.0d0,dbl_mb(k_a
     &_sort),dim_common,dbl_mb(k_b_sort),dim_common,1.0d0,dbl_mb(k_c_sor
     &t),dima_sort)
      IF (.not.MA_POP_STACK(l_b_sort)) CALL ERRQUIT('mbpt2_t3_5_1',7,MA_
     &ERR)
      IF (.not.MA_POP_STACK(l_a_sort)) CALL ERRQUIT('mbpt2_t3_5_1',8,MA_
     &ERR)
      END IF
      END IF
      END IF
      END DO
      IF (.not.MA_PUSH_GET(mt_dbl,dimc,'noname',l_c,k_c)) CALL ERRQUIT('
     &mbpt2_t3_5_1',9,MA_ERR)
      CALL TCE_SORT_4(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h6b-1)
     &,int_mb(k_range+h10b-1),int_mb(k_range+h9b-1),int_mb(k_range+p1b-1
     &),4,1,3,2,-1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h10b 
     &- 1 + noab * (h9b - 1 + noab * (h6b - 1 + noab * (p1b - noab - 1))
     &)))
      IF (.not.MA_POP_STACK(l_c)) CALL ERRQUIT('mbpt2_t3_5_1',10,MA_ERR)
      IF (.not.MA_POP_STACK(l_c_sort)) CALL ERRQUIT('mbpt2_t3_5_1',11,MA
     &_ERR)
      END IF
      END IF
      END IF
      next = NXTASK(nprocs,1)
      END IF
      count = count + 1
      END DO
      END DO
      END DO
      END DO
      next = NXTASK(-nprocs,1)
      call GA_SYNC()
      RETURN
      END
      SUBROUTINE OFFSET_mbpt2_t3_5_1(l_a_offset,k_a_offset,size)
C     $Id$
C     This is a Fortran77 program generated by Tensor Contraction Engine v.1.0
C     Copyright (c) Battelle & Pacific Northwest National Laboratory (2002)
C     i1 ( h6 p1 h9 h10 )_ft1
      IMPLICIT NONE
#include "global.fh"
#include "mafdecls.fh"
#include "sym.fh"
#include "errquit.fh"
#include "tce.fh"
      INTEGER l_a_offset
      INTEGER k_a_offset
      INTEGER size
      INTEGER length
      INTEGER addr
      INTEGER p1b
      INTEGER h6b
      INTEGER h9b
      INTEGER h10b
      length = 0
      DO p1b = noab+1,noab+nvab
      DO h6b = 1,noab
      DO h9b = 1,noab
      DO h10b = h9b,noab
      IF (int_mb(k_spin+h6b-1)+int_mb(k_spin+p1b-1) .eq. int_mb(k_spin+h
     &9b-1)+int_mb(k_spin+h10b-1)) THEN
      IF (ieor(int_mb(k_sym+h6b-1),ieor(int_mb(k_sym+p1b-1),ieor(int_mb(
     &k_sym+h9b-1),int_mb(k_sym+h10b-1)))) .eq. ieor(irrep_f,irrep_t1)) 
     &THEN
      IF ((.not.restricted).or.(int_mb(k_spin+h6b-1)+int_mb(k_spin+p1b-1
     &)+int_mb(k_spin+h9b-1)+int_mb(k_spin+h10b-1).ne.8)) THEN
      length = length + 1
      END IF
      END IF
      END IF
      END DO
      END DO
      END DO
      END DO
      IF (.not.MA_PUSH_GET(mt_int,2*length+1,'noname',l_a_offset,k_a_off
     &set)) CALL ERRQUIT('mbpt2_t3_5_1',0,MA_ERR)
      int_mb(k_a_offset) = length
      addr = 0
      size = 0
      DO p1b = noab+1,noab+nvab
      DO h6b = 1,noab
      DO h9b = 1,noab
      DO h10b = h9b,noab
      IF (int_mb(k_spin+h6b-1)+int_mb(k_spin+p1b-1) .eq. int_mb(k_spin+h
     &9b-1)+int_mb(k_spin+h10b-1)) THEN
      IF (ieor(int_mb(k_sym+h6b-1),ieor(int_mb(k_sym+p1b-1),ieor(int_mb(
     &k_sym+h9b-1),int_mb(k_sym+h10b-1)))) .eq. ieor(irrep_f,irrep_t1)) 
     &THEN
      IF ((.not.restricted).or.(int_mb(k_spin+h6b-1)+int_mb(k_spin+p1b-1
     &)+int_mb(k_spin+h9b-1)+int_mb(k_spin+h10b-1).ne.8)) THEN
      addr = addr + 1
      int_mb(k_a_offset+addr) = h10b - 1 + noab * (h9b - 1 + noab * (h6b
     & - 1 + noab * (p1b - noab - 1)))
      int_mb(k_a_offset+length+addr) = size
      size = size + int_mb(k_range+p1b-1) * int_mb(k_range+h6b-1) * int_
     &mb(k_range+h9b-1) * int_mb(k_range+h10b-1)
      END IF
      END IF
      END IF
      END DO
      END DO
      END DO
      END DO
      RETURN
      END
      SUBROUTINE mbpt2_t3_5_2(d_a,k_a_offset,d_c,k_c_offset)
C     $Id$
C     This is a Fortran77 program generated by Tensor Contraction Engine v.1.0
C     Copyright (c) Battelle & Pacific Northwest National Laboratory (2002)
C     i1 ( h6 p1 h9 h10 )_v + = 1 * v ( h6 p1 h9 h10 )_v
      IMPLICIT NONE
#include "global.fh"
#include "mafdecls.fh"
#include "sym.fh"
#include "errquit.fh"
#include "tce.fh"
      INTEGER d_a
      INTEGER k_a_offset
      INTEGER d_c
      INTEGER k_c_offset
      INTEGER NXTASK
      INTEGER next
      INTEGER nprocs
      INTEGER count
      INTEGER p1b
      INTEGER h6b
      INTEGER h9b
      INTEGER h10b
      INTEGER dimc
      INTEGER p1b_1
      INTEGER h6b_1
      INTEGER h9b_1
      INTEGER h10b_1
      INTEGER dim_common
      INTEGER dima_sort
      INTEGER dima
      INTEGER l_a_sort
      INTEGER k_a_sort
      INTEGER l_a
      INTEGER k_a
      INTEGER l_c
      INTEGER k_c
      EXTERNAL NXTASK
      nprocs = GA_NNODES()
      count = 0
      next = NXTASK(nprocs,1)
      DO p1b = noab+1,noab+nvab
      DO h6b = 1,noab
      DO h9b = 1,noab
      DO h10b = h9b,noab
      IF (next.eq.count) THEN
      IF ((.not.restricted).or.(int_mb(k_spin+p1b-1)+int_mb(k_spin+h6b-1
     &)+int_mb(k_spin+h9b-1)+int_mb(k_spin+h10b-1).ne.8)) THEN
      IF (int_mb(k_spin+p1b-1)+int_mb(k_spin+h6b-1) .eq. int_mb(k_spin+h
     &9b-1)+int_mb(k_spin+h10b-1)) THEN
      IF (ieor(int_mb(k_sym+p1b-1),ieor(int_mb(k_sym+h6b-1),ieor(int_mb(
     &k_sym+h9b-1),int_mb(k_sym+h10b-1)))) .eq. irrep_v) THEN
      dimc = int_mb(k_range+p1b-1) * int_mb(k_range+h6b-1) * int_mb(k_ra
     &nge+h9b-1) * int_mb(k_range+h10b-1)
      CALL TCE_RESTRICTED_4(p1b,h6b,h9b,h10b,p1b_1,h6b_1,h9b_1,h10b_1)
      dim_common = 1
      dima_sort = int_mb(k_range+p1b-1) * int_mb(k_range+h6b-1) * int_mb
     &(k_range+h9b-1) * int_mb(k_range+h10b-1)
      dima = dim_common * dima_sort
      IF (dima .gt. 0) THEN
      IF (.not.MA_PUSH_GET(mt_dbl,dima,'noname',l_a_sort,k_a_sort)) CALL
     & ERRQUIT('mbpt2_t3_5_2',0,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dima,'noname',l_a,k_a)) CALL ERRQUIT('
     &mbpt2_t3_5_2',1,MA_ERR)
      IF ((h6b .le. p1b)) THEN
      if(.not.intorb) then 
      CALL GET_HASH_BLOCK(d_a,dbl_mb(k_a),dima,int_mb(k_a_offset),(h10b_
     &1 - 1 + (noab+nvab) * (h9b_1 - 1 + (noab+nvab) * (p1b_1 - 1 + (noa
     &b+nvab) * (h6b_1 - 1)))))
      else
      CALL GET_HASH_BLOCK_I(d_a,dbl_mb(k_a),dima,int_mb(k_a_offset),
     &(h10b_
     &1 - 1 + (noab+nvab) * (h9b_1 - 1 + (noab+nvab) * (p1b_1 - 1 + (noa
     &b+nvab) * (h6b_1 - 1)))),h10b_1,h9b_1,p1b_1,h6b_1)
      endif
      CALL TCE_SORT_4(dbl_mb(k_a),dbl_mb(k_a_sort),int_mb(k_range+h6b-1)
     &,int_mb(k_range+p1b-1),int_mb(k_range+h9b-1),int_mb(k_range+h10b-1
     &),4,3,1,2,1.0d0)
      END IF
      IF (.not.MA_POP_STACK(l_a)) CALL ERRQUIT('mbpt2_t3_5_2',2,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dimc,'noname',l_c,k_c)) CALL ERRQUIT('
     &mbpt2_t3_5_2',3,MA_ERR)
      CALL TCE_SORT_4(dbl_mb(k_a_sort),dbl_mb(k_c),int_mb(k_range+h10b-1
     &),int_mb(k_range+h9b-1),int_mb(k_range+h6b-1),int_mb(k_range+p1b-1
     &),4,3,2,1,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h10b 
     &- 1 + noab * (h9b - 1 + noab * (h6b - 1 + noab * (p1b - noab - 1))
     &)))
      IF (.not.MA_POP_STACK(l_c)) CALL ERRQUIT('mbpt2_t3_5_2',4,MA_ERR)
      IF (.not.MA_POP_STACK(l_a_sort)) CALL ERRQUIT('mbpt2_t3_5_2',5,MA_
     &ERR)
      END IF
      END IF
      END IF
      END IF
      next = NXTASK(nprocs,1)
      END IF
      count = count + 1
      END DO
      END DO
      END DO
      END DO
      next = NXTASK(-nprocs,1)
      call GA_SYNC()
      RETURN
      END
      SUBROUTINE mbpt2_t3_6(d_a,k_a_offset,d_b,k_b_offset,d_c,k_c_offset
     &)
C     $Id$
C     This is a Fortran77 program generated by Tensor Contraction Engine v.1.0
C     Copyright (c) Battelle & Pacific Northwest National Laboratory (2002)
C     i0 ( p1 p2 p3 h9 h10 h11 )_vt1 + = 1 * P( 9 ) * t1 ( p1 h9 )_t1 * v ( p2 p3 h10 h11 )_v
      IMPLICIT NONE
#include "global.fh"
#include "mafdecls.fh"
#include "sym.fh"
#include "errquit.fh"
#include "tce.fh"
      INTEGER d_a
      INTEGER k_a_offset
      INTEGER d_b
      INTEGER k_b_offset
      INTEGER d_c
      INTEGER k_c_offset
      INTEGER NXTASK
      INTEGER next
      INTEGER nprocs
      INTEGER count
      INTEGER p1b
      INTEGER p2b
      INTEGER p3b
      INTEGER h9b
      INTEGER h10b
      INTEGER h11b
      INTEGER dimc
      INTEGER l_c_sort
      INTEGER k_c_sort
      INTEGER p1b_1
      INTEGER h9b_1
      INTEGER p2b_2
      INTEGER p3b_2
      INTEGER h10b_2
      INTEGER h11b_2
      INTEGER dim_common
      INTEGER dima_sort
      INTEGER dima
      INTEGER dimb_sort
      INTEGER dimb
      INTEGER l_a_sort
      INTEGER k_a_sort
      INTEGER l_a
      INTEGER k_a
      INTEGER l_b_sort
      INTEGER k_b_sort
      INTEGER l_b
      INTEGER k_b
      INTEGER l_c
      INTEGER k_c
      EXTERNAL NXTASK
      nprocs = GA_NNODES()
      count = 0
      next = NXTASK(nprocs,1)
      DO p1b = noab+1,noab+nvab
      DO p2b = noab+1,noab+nvab
      DO p3b = p2b,noab+nvab
      DO h9b = 1,noab
      DO h10b = 1,noab
      DO h11b = h10b,noab
      IF (next.eq.count) THEN
      IF ((.not.restricted).or.(int_mb(k_spin+p1b-1)+int_mb(k_spin+p2b-1
     &)+int_mb(k_spin+p3b-1)+int_mb(k_spin+h9b-1)+int_mb(k_spin+h10b-1)+
     &int_mb(k_spin+h11b-1).ne.12)) THEN
      IF (int_mb(k_spin+p1b-1)+int_mb(k_spin+p2b-1)+int_mb(k_spin+p3b-1)
     & .eq. int_mb(k_spin+h9b-1)+int_mb(k_spin+h10b-1)+int_mb(k_spin+h11
     &b-1)) THEN
      IF (ieor(int_mb(k_sym+p1b-1),ieor(int_mb(k_sym+p2b-1),ieor(int_mb(
     &k_sym+p3b-1),ieor(int_mb(k_sym+h9b-1),ieor(int_mb(k_sym+h10b-1),in
     &t_mb(k_sym+h11b-1)))))) .eq. ieor(irrep_v,irrep_t1)) THEN
      dimc = int_mb(k_range+p1b-1) * int_mb(k_range+p2b-1) * int_mb(k_ra
     &nge+p3b-1) * int_mb(k_range+h9b-1) * int_mb(k_range+h10b-1) * int_
     &mb(k_range+h11b-1)
      IF (.not.MA_PUSH_GET(mt_dbl,dimc,'noname',l_c_sort,k_c_sort)) CALL
     & ERRQUIT('mbpt2_t3_6',0,MA_ERR)
      CALL DFILL(dimc,0.0d0,dbl_mb(k_c_sort),1)
      IF (int_mb(k_spin+p1b-1) .eq. int_mb(k_spin+h9b-1)) THEN
      IF (ieor(int_mb(k_sym+p1b-1),int_mb(k_sym+h9b-1)) .eq. irrep_t1) T
     &HEN
      CALL TCE_RESTRICTED_2(p1b,h9b,p1b_1,h9b_1)
      CALL TCE_RESTRICTED_4(p2b,p3b,h10b,h11b,p2b_2,p3b_2,h10b_2,h11b_2)
      dim_common = 1
      dima_sort = int_mb(k_range+p1b-1) * int_mb(k_range+h9b-1)
      dima = dim_common * dima_sort
      dimb_sort = int_mb(k_range+p2b-1) * int_mb(k_range+p3b-1) * int_mb
     &(k_range+h10b-1) * int_mb(k_range+h11b-1)
      dimb = dim_common * dimb_sort
      IF ((dima .gt. 0) .and. (dimb .gt. 0)) THEN
      IF (.not.MA_PUSH_GET(mt_dbl,dima,'noname',l_a_sort,k_a_sort)) CALL
     & ERRQUIT('mbpt2_t3_6',1,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dima,'noname',l_a,k_a)) CALL ERRQUIT('
     &mbpt2_t3_6',2,MA_ERR)
      CALL GET_HASH_BLOCK(d_a,dbl_mb(k_a),dima,int_mb(k_a_offset),(h9b_1
     & - 1 + noab * (p1b_1 - noab - 1)))
      CALL TCE_SORT_2(dbl_mb(k_a),dbl_mb(k_a_sort),int_mb(k_range+p1b-1)
     &,int_mb(k_range+h9b-1),2,1,1.0d0)
      IF (.not.MA_POP_STACK(l_a)) CALL ERRQUIT('mbpt2_t3_6',3,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dimb,'noname',l_b_sort,k_b_sort)) CALL
     & ERRQUIT('mbpt2_t3_6',4,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dimb,'noname',l_b,k_b)) CALL ERRQUIT('
     &mbpt2_t3_6',5,MA_ERR)
      if(.not.intorb) then
      CALL GET_HASH_BLOCK(d_b,dbl_mb(k_b),dimb,int_mb(k_b_offset),(h11b_
     &2 - 1 + (noab+nvab) * (h10b_2 - 1 + (noab+nvab) * (p3b_2 - 1 + (no
     &ab+nvab) * (p2b_2 - 1)))))
      else
      CALL GET_HASH_BLOCK_I(d_b,dbl_mb(k_b),dimb,int_mb(k_b_offset),
     &(h11b_
     &2 - 1 + (noab+nvab) * (h10b_2 - 1 + (noab+nvab) * (p3b_2 - 1 + (no
     &ab+nvab) * (p2b_2 - 1)))),h11b_2,h10b_2,p3b_2,p2b_2)
      endif 
      CALL TCE_SORT_4(dbl_mb(k_b),dbl_mb(k_b_sort),int_mb(k_range+p2b-1)
     &,int_mb(k_range+p3b-1),int_mb(k_range+h10b-1),int_mb(k_range+h11b-
     &1),4,3,2,1,1.0d0)
      IF (.not.MA_POP_STACK(l_b)) CALL ERRQUIT('mbpt2_t3_6',6,MA_ERR)
      CALL DGEMM('T','N',dima_sort,dimb_sort,dim_common,1.0d0,dbl_mb(k_a
     &_sort),dim_common,dbl_mb(k_b_sort),dim_common,1.0d0,dbl_mb(k_c_sor
     &t),dima_sort)
      IF (.not.MA_POP_STACK(l_b_sort)) CALL ERRQUIT('mbpt2_t3_6',7,MA_ER
     &R)
      IF (.not.MA_POP_STACK(l_a_sort)) CALL ERRQUIT('mbpt2_t3_6',8,MA_ER
     &R)
      END IF
      END IF
      END IF
      IF (.not.MA_PUSH_GET(mt_dbl,dimc,'noname',l_c,k_c)) CALL ERRQUIT('
     &mbpt2_t3_6',9,MA_ERR)
      IF ((p1b .le. p2b) .and. (h9b .le. h10b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+h10b-1),int_mb(k_range+p3b-1),int_mb(k_range+p2b-
     &1),int_mb(k_range+h9b-1),int_mb(k_range+p1b-1),6,4,3,5,2,1,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h11b 
     &- 1 + noab * (h10b - 1 + noab * (h9b - 1 + noab * (p3b - noab - 1 
     &+ nvab * (p2b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF ((p1b .le. p2b) .and. (h10b .le. h9b) .and. (h9b .le. h11b)) TH
     &EN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+h10b-1),int_mb(k_range+p3b-1),int_mb(k_range+p2b-
     &1),int_mb(k_range+h9b-1),int_mb(k_range+p1b-1),6,4,3,2,5,1,-1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h11b 
     &- 1 + noab * (h9b - 1 + noab * (h10b - 1 + noab * (p3b - noab - 1 
     &+ nvab * (p2b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF ((p1b .le. p2b) .and. (h11b .le. h9b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+h10b-1),int_mb(k_range+p3b-1),int_mb(k_range+p2b-
     &1),int_mb(k_range+h9b-1),int_mb(k_range+p1b-1),6,4,3,2,1,5,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h9b -
     & 1 + noab * (h11b - 1 + noab * (h10b - 1 + noab * (p3b - noab - 1 
     &+ nvab * (p2b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF ((p2b .le. p1b) .and. (p1b .le. p3b) .and. (h9b .le. h10b)) THE
     &N
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+h10b-1),int_mb(k_range+p3b-1),int_mb(k_range+p2b-
     &1),int_mb(k_range+h9b-1),int_mb(k_range+p1b-1),4,6,3,5,2,1,-1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h11b 
     &- 1 + noab * (h10b - 1 + noab * (h9b - 1 + noab * (p3b - noab - 1 
     &+ nvab * (p1b - noab - 1 + nvab * (p2b - noab - 1)))))))
      END IF
      IF ((p2b .le. p1b) .and. (p1b .le. p3b) .and. (h10b .le. h9b) .and
     &. (h9b .le. h11b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+h10b-1),int_mb(k_range+p3b-1),int_mb(k_range+p2b-
     &1),int_mb(k_range+h9b-1),int_mb(k_range+p1b-1),4,6,3,2,5,1,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h11b 
     &- 1 + noab * (h9b - 1 + noab * (h10b - 1 + noab * (p3b - noab - 1 
     &+ nvab * (p1b - noab - 1 + nvab * (p2b - noab - 1)))))))
      END IF
      IF ((p2b .le. p1b) .and. (p1b .le. p3b) .and. (h11b .le. h9b)) THE
     &N
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+h10b-1),int_mb(k_range+p3b-1),int_mb(k_range+p2b-
     &1),int_mb(k_range+h9b-1),int_mb(k_range+p1b-1),4,6,3,2,1,5,-1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h9b -
     & 1 + noab * (h11b - 1 + noab * (h10b - 1 + noab * (p3b - noab - 1 
     &+ nvab * (p1b - noab - 1 + nvab * (p2b - noab - 1)))))))
      END IF
      IF ((p3b .le. p1b) .and. (h9b .le. h10b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+h10b-1),int_mb(k_range+p3b-1),int_mb(k_range+p2b-
     &1),int_mb(k_range+h9b-1),int_mb(k_range+p1b-1),4,3,6,5,2,1,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h11b 
     &- 1 + noab * (h10b - 1 + noab * (h9b - 1 + noab * (p1b - noab - 1 
     &+ nvab * (p3b - noab - 1 + nvab * (p2b - noab - 1)))))))
      END IF
      IF ((p3b .le. p1b) .and. (h10b .le. h9b) .and. (h9b .le. h11b)) TH
     &EN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+h10b-1),int_mb(k_range+p3b-1),int_mb(k_range+p2b-
     &1),int_mb(k_range+h9b-1),int_mb(k_range+p1b-1),4,3,6,2,5,1,-1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h11b 
     &- 1 + noab * (h9b - 1 + noab * (h10b - 1 + noab * (p1b - noab - 1 
     &+ nvab * (p3b - noab - 1 + nvab * (p2b - noab - 1)))))))
      END IF
      IF ((p3b .le. p1b) .and. (h11b .le. h9b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+h10b-1),int_mb(k_range+p3b-1),int_mb(k_range+p2b-
     &1),int_mb(k_range+h9b-1),int_mb(k_range+p1b-1),4,3,6,2,1,5,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h9b -
     & 1 + noab * (h11b - 1 + noab * (h10b - 1 + noab * (p1b - noab - 1 
     &+ nvab * (p3b - noab - 1 + nvab * (p2b - noab - 1)))))))
      END IF
      IF (.not.MA_POP_STACK(l_c)) CALL ERRQUIT('mbpt2_t3_6',10,MA_ERR)
      IF (.not.MA_POP_STACK(l_c_sort)) CALL ERRQUIT('mbpt2_t3_6',11,MA_E
     &RR)
      END IF
      END IF
      END IF
      next = NXTASK(nprocs,1)
      END IF
      count = count + 1
      END DO
      END DO
      END DO
      END DO
      END DO
      END DO
      next = NXTASK(-nprocs,1)
      call GA_SYNC()
      RETURN
      END
      SUBROUTINE mbpt2_t3_7(d_a,k_a_offset,d_b,k_b_offset,d_c,k_c_offset
     &)
C     $Id$
C     This is a Fortran77 program generated by Tensor Contraction Engine v.1.0
C     Copyright (c) Battelle & Pacific Northwest National Laboratory (2002)
C     i0 ( p1 p2 p3 h9 h10 h11 )_vt1 + = -1 * P( 9 ) * Sum ( p4 ) * t1 ( p1 p4 h9 h10 )_t1 * v ( p2 p3 h11 p4 )_v
      IMPLICIT NONE
#include "global.fh"
#include "mafdecls.fh"
#include "sym.fh"
#include "errquit.fh"
#include "tce.fh"
      INTEGER d_a
      INTEGER k_a_offset
      INTEGER d_b
      INTEGER k_b_offset
      INTEGER d_c
      INTEGER k_c_offset
      INTEGER NXTASK
      INTEGER next
      INTEGER nprocs
      INTEGER count
      INTEGER p1b
      INTEGER p2b
      INTEGER p3b
      INTEGER h9b
      INTEGER h10b
      INTEGER h11b
      INTEGER dimc
      INTEGER l_c_sort
      INTEGER k_c_sort
      INTEGER p4b
      INTEGER p1b_1
      INTEGER p4b_1
      INTEGER h9b_1
      INTEGER h10b_1
      INTEGER p2b_2
      INTEGER p3b_2
      INTEGER h11b_2
      INTEGER p4b_2
      INTEGER dim_common
      INTEGER dima_sort
      INTEGER dima
      INTEGER dimb_sort
      INTEGER dimb
      INTEGER l_a_sort
      INTEGER k_a_sort
      INTEGER l_a
      INTEGER k_a
      INTEGER l_b_sort
      INTEGER k_b_sort
      INTEGER l_b
      INTEGER k_b
      INTEGER l_c
      INTEGER k_c
      EXTERNAL NXTASK
      nprocs = GA_NNODES()
      count = 0
      next = NXTASK(nprocs,1)
      DO p1b = noab+1,noab+nvab
      DO p2b = noab+1,noab+nvab
      DO p3b = p2b,noab+nvab
      DO h9b = 1,noab
      DO h10b = h9b,noab
      DO h11b = 1,noab
      IF (next.eq.count) THEN
      IF ((.not.restricted).or.(int_mb(k_spin+p1b-1)+int_mb(k_spin+p2b-1
     &)+int_mb(k_spin+p3b-1)+int_mb(k_spin+h9b-1)+int_mb(k_spin+h10b-1)+
     &int_mb(k_spin+h11b-1).ne.12)) THEN
      IF (int_mb(k_spin+p1b-1)+int_mb(k_spin+p2b-1)+int_mb(k_spin+p3b-1)
     & .eq. int_mb(k_spin+h9b-1)+int_mb(k_spin+h10b-1)+int_mb(k_spin+h11
     &b-1)) THEN
      IF (ieor(int_mb(k_sym+p1b-1),ieor(int_mb(k_sym+p2b-1),ieor(int_mb(
     &k_sym+p3b-1),ieor(int_mb(k_sym+h9b-1),ieor(int_mb(k_sym+h10b-1),in
     &t_mb(k_sym+h11b-1)))))) .eq. ieor(irrep_v,irrep_t1)) THEN
      dimc = int_mb(k_range+p1b-1) * int_mb(k_range+p2b-1) * int_mb(k_ra
     &nge+p3b-1) * int_mb(k_range+h9b-1) * int_mb(k_range+h10b-1) * int_
     &mb(k_range+h11b-1)
      IF (.not.MA_PUSH_GET(mt_dbl,dimc,'noname',l_c_sort,k_c_sort)) CALL
     & ERRQUIT('mbpt2_t3_7',0,MA_ERR)
      CALL DFILL(dimc,0.0d0,dbl_mb(k_c_sort),1)
      DO p4b = noab+1,noab+nvab
      IF (int_mb(k_spin+p1b-1)+int_mb(k_spin+p4b-1) .eq. int_mb(k_spin+h
     &9b-1)+int_mb(k_spin+h10b-1)) THEN
      IF (ieor(int_mb(k_sym+p1b-1),ieor(int_mb(k_sym+p4b-1),ieor(int_mb(
     &k_sym+h9b-1),int_mb(k_sym+h10b-1)))) .eq. irrep_t1) THEN
      CALL TCE_RESTRICTED_4(p1b,p4b,h9b,h10b,p1b_1,p4b_1,h9b_1,h10b_1)
      CALL TCE_RESTRICTED_4(p2b,p3b,h11b,p4b,p2b_2,p3b_2,h11b_2,p4b_2)
      dim_common = int_mb(k_range+p4b-1)
      dima_sort = int_mb(k_range+p1b-1) * int_mb(k_range+h9b-1) * int_mb
     &(k_range+h10b-1)
      dima = dim_common * dima_sort
      dimb_sort = int_mb(k_range+p2b-1) * int_mb(k_range+p3b-1) * int_mb
     &(k_range+h11b-1)
      dimb = dim_common * dimb_sort
      IF ((dima .gt. 0) .and. (dimb .gt. 0)) THEN
      IF (.not.MA_PUSH_GET(mt_dbl,dima,'noname',l_a_sort,k_a_sort)) CALL
     & ERRQUIT('mbpt2_t3_7',1,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dima,'noname',l_a,k_a)) CALL ERRQUIT('
     &mbpt2_t3_7',2,MA_ERR)
      IF ((p4b .lt. p1b)) THEN
      CALL GET_HASH_BLOCK(d_a,dbl_mb(k_a),dima,int_mb(k_a_offset),(h10b_
     &1 - 1 + noab * (h9b_1 - 1 + noab * (p1b_1 - noab - 1 + nvab * (p4b
     &_1 - noab - 1)))))
      CALL TCE_SORT_4(dbl_mb(k_a),dbl_mb(k_a_sort),int_mb(k_range+p4b-1)
     &,int_mb(k_range+p1b-1),int_mb(k_range+h9b-1),int_mb(k_range+h10b-1
     &),4,3,2,1,-1.0d0)
      END IF
      IF ((p1b .le. p4b)) THEN
      CALL GET_HASH_BLOCK(d_a,dbl_mb(k_a),dima,int_mb(k_a_offset),(h10b_
     &1 - 1 + noab * (h9b_1 - 1 + noab * (p4b_1 - noab - 1 + nvab * (p1b
     &_1 - noab - 1)))))
      CALL TCE_SORT_4(dbl_mb(k_a),dbl_mb(k_a_sort),int_mb(k_range+p1b-1)
     &,int_mb(k_range+p4b-1),int_mb(k_range+h9b-1),int_mb(k_range+h10b-1
     &),4,3,1,2,1.0d0)
      END IF
      IF (.not.MA_POP_STACK(l_a)) CALL ERRQUIT('mbpt2_t3_7',3,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dimb,'noname',l_b_sort,k_b_sort)) CALL
     & ERRQUIT('mbpt2_t3_7',4,MA_ERR)
      IF (.not.MA_PUSH_GET(mt_dbl,dimb,'noname',l_b,k_b)) CALL ERRQUIT('
     &mbpt2_t3_7',5,MA_ERR)
      IF ((h11b .le. p4b)) THEN
      if(.not.intorb) then
      CALL GET_HASH_BLOCK(d_b,dbl_mb(k_b),dimb,int_mb(k_b_offset),(p4b_2
     & - 1 + (noab+nvab) * (h11b_2 - 1 + (noab+nvab) * (p3b_2 - 1 + (noa
     &b+nvab) * (p2b_2 - 1)))))
      else
      CALL GET_HASH_BLOCK_I(d_b,dbl_mb(k_b),dimb,int_mb(k_b_offset),
     &(p4b_2
     & - 1 + (noab+nvab) * (h11b_2 - 1 + (noab+nvab) * (p3b_2 - 1 + (noa
     &b+nvab) * (p2b_2 - 1)))),p4b_2,h11b_2,p3b_2,p2b_2)
      endif
      CALL TCE_SORT_4(dbl_mb(k_b),dbl_mb(k_b_sort),int_mb(k_range+p2b-1)
     &,int_mb(k_range+p3b-1),int_mb(k_range+h11b-1),int_mb(k_range+p4b-1
     &),3,2,1,4,1.0d0)
      END IF
      IF (.not.MA_POP_STACK(l_b)) CALL ERRQUIT('mbpt2_t3_7',6,MA_ERR)
      CALL DGEMM('T','N',dima_sort,dimb_sort,dim_common,1.0d0,dbl_mb(k_a
     &_sort),dim_common,dbl_mb(k_b_sort),dim_common,1.0d0,dbl_mb(k_c_sor
     &t),dima_sort)
      IF (.not.MA_POP_STACK(l_b_sort)) CALL ERRQUIT('mbpt2_t3_7',7,MA_ER
     &R)
      IF (.not.MA_POP_STACK(l_a_sort)) CALL ERRQUIT('mbpt2_t3_7',8,MA_ER
     &R)
      END IF
      END IF
      END IF
      END DO
      IF (.not.MA_PUSH_GET(mt_dbl,dimc,'noname',l_c,k_c)) CALL ERRQUIT('
     &mbpt2_t3_7',9,MA_ERR)
      IF ((p1b .le. p2b) .and. (h10b .le. h11b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+p2b-1),int_mb(k_range+h10b-
     &1),int_mb(k_range+h9b-1),int_mb(k_range+p1b-1),6,3,2,5,4,1,-1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h11b 
     &- 1 + noab * (h10b - 1 + noab * (h9b - 1 + noab * (p3b - noab - 1 
     &+ nvab * (p2b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF ((p1b .le. p2b) .and. (h11b .le. h9b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+p2b-1),int_mb(k_range+h10b-
     &1),int_mb(k_range+h9b-1),int_mb(k_range+p1b-1),6,3,2,1,5,4,-1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h10b 
     &- 1 + noab * (h9b - 1 + noab * (h11b - 1 + noab * (p3b - noab - 1 
     &+ nvab * (p2b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF ((p1b .le. p2b) .and. (h9b .le. h11b) .and. (h11b .le. h10b)) T
     &HEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+p2b-1),int_mb(k_range+h10b-
     &1),int_mb(k_range+h9b-1),int_mb(k_range+p1b-1),6,3,2,5,1,4,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h10b 
     &- 1 + noab * (h11b - 1 + noab * (h9b - 1 + noab * (p3b - noab - 1 
     &+ nvab * (p2b - noab - 1 + nvab * (p1b - noab - 1)))))))
      END IF
      IF ((p2b .le. p1b) .and. (p1b .le. p3b) .and. (h10b .le. h11b)) TH
     &EN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+p2b-1),int_mb(k_range+h10b-
     &1),int_mb(k_range+h9b-1),int_mb(k_range+p1b-1),3,6,2,5,4,1,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h11b 
     &- 1 + noab * (h10b - 1 + noab * (h9b - 1 + noab * (p3b - noab - 1 
     &+ nvab * (p1b - noab - 1 + nvab * (p2b - noab - 1)))))))
      END IF
      IF ((p2b .le. p1b) .and. (p1b .le. p3b) .and. (h11b .le. h9b)) THE
     &N
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+p2b-1),int_mb(k_range+h10b-
     &1),int_mb(k_range+h9b-1),int_mb(k_range+p1b-1),3,6,2,1,5,4,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h10b 
     &- 1 + noab * (h9b - 1 + noab * (h11b - 1 + noab * (p3b - noab - 1 
     &+ nvab * (p1b - noab - 1 + nvab * (p2b - noab - 1)))))))
      END IF
      IF ((p2b .le. p1b) .and. (p1b .le. p3b) .and. (h9b .le. h11b) .and
     &. (h11b .le. h10b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+p2b-1),int_mb(k_range+h10b-
     &1),int_mb(k_range+h9b-1),int_mb(k_range+p1b-1),3,6,2,5,1,4,-1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h10b 
     &- 1 + noab * (h11b - 1 + noab * (h9b - 1 + noab * (p3b - noab - 1 
     &+ nvab * (p1b - noab - 1 + nvab * (p2b - noab - 1)))))))
      END IF
      IF ((p3b .le. p1b) .and. (h10b .le. h11b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+p2b-1),int_mb(k_range+h10b-
     &1),int_mb(k_range+h9b-1),int_mb(k_range+p1b-1),3,2,6,5,4,1,-1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h11b 
     &- 1 + noab * (h10b - 1 + noab * (h9b - 1 + noab * (p1b - noab - 1 
     &+ nvab * (p3b - noab - 1 + nvab * (p2b - noab - 1)))))))
      END IF
      IF ((p3b .le. p1b) .and. (h11b .le. h9b)) THEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+p2b-1),int_mb(k_range+h10b-
     &1),int_mb(k_range+h9b-1),int_mb(k_range+p1b-1),3,2,6,1,5,4,-1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h10b 
     &- 1 + noab * (h9b - 1 + noab * (h11b - 1 + noab * (p1b - noab - 1 
     &+ nvab * (p3b - noab - 1 + nvab * (p2b - noab - 1)))))))
      END IF
      IF ((p3b .le. p1b) .and. (h9b .le. h11b) .and. (h11b .le. h10b)) T
     &HEN
      CALL TCE_SORT_6(dbl_mb(k_c_sort),dbl_mb(k_c),int_mb(k_range+h11b-1
     &),int_mb(k_range+p3b-1),int_mb(k_range+p2b-1),int_mb(k_range+h10b-
     &1),int_mb(k_range+h9b-1),int_mb(k_range+p1b-1),3,2,6,5,1,4,1.0d0)
      CALL ADD_HASH_BLOCK(d_c,dbl_mb(k_c),dimc,int_mb(k_c_offset),(h10b 
     &- 1 + noab * (h11b - 1 + noab * (h9b - 1 + noab * (p1b - noab - 1 
     &+ nvab * (p3b - noab - 1 + nvab * (p2b - noab - 1)))))))
      END IF
      IF (.not.MA_POP_STACK(l_c)) CALL ERRQUIT('mbpt2_t3_7',10,MA_ERR)
      IF (.not.MA_POP_STACK(l_c_sort)) CALL ERRQUIT('mbpt2_t3_7',11,MA_E
     &RR)
      END IF
      END IF
      END IF
      next = NXTASK(nprocs,1)
      END IF
      count = count + 1
      END DO
      END DO
      END DO
      END DO
      END DO
      END DO
      next = NXTASK(-nprocs,1)
      call GA_SYNC()
      RETURN
      END
