      logical function tce_ccsd_lambda_response_driver(title,thresh,
     &        maxiter,
     &        d_yr1,k_yr1_offset,size_yr1,d_yr2,k_yr2_offset,size_yr2,
     &        d_y1,k_y1_offset,d_y2,k_y2_offset,
     &        d_tr1,k_tr1_offset,size_tr1,d_tr2,k_tr2_offset,size_tr2,
     &        d_t1,k_t1_offset,size_t1,d_t2,k_t2_offset,size_t2,
     &        d_f1,k_f1_offset,d_v2,k_v2_offset,d_o1,k_o1_offset,omega)
c
c $Id: tce_ccsd_lambda_response_driver.F 19706 2010-10-29 17:52:31Z d3y133 $
c 
c Main routine for many-electron theory calculations.
c Some of the subroutines have been generated by 
c operator/tensor contraction engines.
c
c Written by Jeff Hammond, January 2008.
c
      implicit none
#include "mafdecls.fh"
#include "tcgmsg.fh"
#include "global.fh"
#include "bas.fh"
#include "geom.fh"
#include "sym.fh"
#include "util.fh"
#include "msgids.fh"
#include "stdio.fh"
#include "sf.fh"
#include "inp.fh"
#include "errquit.fh"
#include "tce.fh"
c#include "tce_main.fh"
c#include "tce_prop.fh"
c#include "tce_ints.fh"
c#include "tce_amps.fh"
c#include "tce_diis.fh"
c
c     CI, CC, & MBPT
c
      integer iter,maxiter
      logical nodezero         ! True if node 0
      logical recompf          ! True if recompute Fock
      double precision cpu     ! CPU sec counter
      double precision wall    ! WALL sec counter
      integer irrep
      integer irrep_g
      integer d_yr1             ! SF handle for yr1 amplitudes
      integer k_yr1_offset      ! Offset for yr1 file
      integer size_yr1          ! File size in doubles
      integer d_yr2             ! SF handle for yr2 amplitudes
      integer k_yr2_offset      ! Offset for yr2 file
      integer size_yr2          ! File size in doubles
      integer d_yrr1            ! SF handle for yrr1 amplitudes
      integer d_yrr2            ! SF handle for yrr2 amplitudes
      integer d_tr1             ! SF handle for tr1 amplitudes
      integer k_tr1_offset      ! Offset for tr1 file
      integer size_tr1          ! File size in doubles
      integer d_tr2             ! SF handle for tr2 amplitudes
      integer k_tr2_offset      ! Offset for tr2 file
      integer size_tr2          ! File size in doubles
      integer d_rr1             ! SF handle for rr1 amplitudes
      integer d_rr2             ! SF handle for rr2 amplitudes
      integer d_o1
      integer k_o1_offset
      integer size_o1
      integer d_f1             ! SF handle for MO 1e integrals
      integer k_f1_offset      ! Offset for 1e integral file
      integer size_1e          ! File size in doubles
      integer d_v2             ! SF handle for MO 2e integrals
      integer k_v2_offset      ! Offset for 2e integral file
      integer size_2e          ! File size in doubles
      integer d_t1             ! SF handle for t1 amplitudes
      integer k_t1_offset      ! Offset for t1 file
      integer size_t1          ! File size in doubles
      integer d_t2             ! SF handle for t2 amplitudes
      integer k_t2_offset      ! Offset for t2 file
      integer size_t2          ! File size in doubles
      integer d_y1             ! Lambda 1 file
      integer d_y2             ! Lambda 2 file
      integer k_y1_offset      ! Offset for Lambda 1 file
      integer size_y1          ! File size in doubles
      integer k_y2_offset      ! Offset for Lambda 2 file
      integer size_y2          ! File size in doubles
      double precision rr1      ! Residual t1
      double precision rr2      ! Residual t2
      double precision residual! Largest residual
      double precision thresh
      double precision ddotfile
      double precision omega
      external ddotfile
      integer dummy            ! Dummy argument for DIIS
      character*8 yrr1filename ! File name stub
      data yrr1filename/'yrr1  '/
      character*8 yrr2filename ! File name stub
      data yrr2filename/'yrr2  '/
      character*255 filename
      character*20 title
      character*4 irrepname
      irrep_g=0
      nodezero=(ga_nodeid().eq.0)
      call tce_diis_init()
      do iter=1,maxiter
        cpu=-util_cpusec()
        wall=-util_wallsec()
        if (nodezero.and.(iter.eq.1))
     &    write(LuOut,9400) title
        call tce_filename(yrr1filename,filename)
        call createfile(filename,d_yrr1,size_yr1)
        call tce_zero(d_yrr1,size_yr1)
        call daxpyfile(1,omega,d_yr1,d_yrr1,size_yr1)
        irrep_y=irrep_g ! using Lambda(0) amplitudes
        call ccsd_y_o1(d_yrr1,d_o1,d_t1,d_t2,
     1       d_y1,d_y2,k_yr1_offset,
     2       k_o1_offset,k_t1_offset,k_t2_offset,
     3       k_y1_offset,k_y2_offset)
        irrep_y=irrep_g ! using Lambda(0) amplitudes
        call ccsd_y_tr1(d_f1,d_yrr1,d_t1,d_t2,d_v2,
     1       d_tr1,d_tr2,d_y1,
     2       d_y2,k_f1_offset,k_yr1_offset,
     3       k_t1_offset,k_t2_offset,k_v2_offset,
     4       k_tr1_offset,k_tr2_offset,
     5       k_y1_offset,k_y2_offset)
        irrep_y=irrep_d ! using Lambda(1) amplitudes
        call eomccsd_y1(d_f1,d_yrr1,d_t1,d_t2,d_v2,
     1       d_yr1,d_yr2,k_f1_offset,
     2       k_yr1_offset,k_t1_offset,k_t2_offset,
     3       k_v2_offset,k_yr1_offset,k_yr2_offset)
        call tce_filename(yrr2filename,filename)
        call createfile(filename,d_yrr2,size_yr2)
        call tce_zero(d_yrr2,size_yr2)
        call daxpyfile(1,omega,d_yr2,d_yrr2,size_yr2)
        irrep_y=irrep_g ! using Lambda(0) amplitudes
        call ccsd_y_o2(d_yrr2,d_o1,d_t1,
     1       d_y1,d_y2,k_yr2_offset,
     2       k_o1_offset,k_t1_offset,
     3       k_y1_offset,k_y2_offset)
        irrep_y=irrep_g ! using Lambda(0) amplitudes
        call ccsd_y_tr2(d_f1,d_yrr2,d_t1,d_v2,
     1       d_tr1,d_tr2,d_y1,
     2       d_y2,k_f1_offset,k_yr2_offset,
     3       k_t1_offset,k_v2_offset,
     4       k_tr1_offset,k_tr2_offset,
     5       k_y1_offset,k_y2_offset)
        irrep_y=irrep_d ! using Lambda(1) amplitudes
        call eomccsd_y2(d_f1,d_yrr2,d_t1,d_t2,d_v2,
     1       d_yr1,d_yr2,k_f1_offset,
     2       k_yr2_offset,k_t1_offset,k_t2_offset,
     3       k_v2_offset,k_yr1_offset,k_yr2_offset)
        call reconcilefile(d_yrr1,size_yr1)
        call reconcilefile(d_yrr2,size_yr2)
        rr1 = ddotfile(d_yrr1,d_yrr1,size_yr1)
        rr2 = ddotfile(d_yrr2,d_yrr2,size_yr2)
        residual = max(rr1,rr2)
        cpu=cpu+util_cpusec()
        wall=wall+util_wallsec()
        if (nodezero) write(LuOut,9420) iter,residual,cpu,wall
        if (residual .lt. thresh) then
          if (nodezero) write(LuOut,9410)
          call deletefile(d_yrr2)
          call deletefile(d_yrr1)
          call tce_diis_tidy()
          tce_ccsd_lambda_response_driver=.true.
          return
        endif
        irrep_y=irrep_d
        call tce_diis2(.true.,iter,.true.,.true.,.false.,.false.,
     1       d_yrr1,d_yr1,k_yr1_offset,size_yr1,
     3       d_yrr2,d_yr2,k_yr2_offset,size_yr2,
     5       dummy,dummy,dummy,dummy,
     6       dummy,dummy,dummy,dummy)
        call deletefile(d_yrr2)
        call deletefile(d_yrr1)
        if (nodezero) call util_flush(LuOut)
      enddo
      call tce_diis_tidy()
      tce_ccsd_lambda_response_driver=.false.
      return  
c
c     ======
c     Format
c     ======
c
 9000 format(1x,A,' file size   = ',i16)
 9010 format(1x,A,' file name   = ',A)
 9090 format(1x,A,' file handle = ',i10)
 9020 format(1x,'Cpu & wall time / sec',2f15.1)
 9480 format(1x,'Cpu & wall time / sec for ',A,2f15.1)
 9050 format(/,1x,A,' iterations',/,
     1  1x,'--------------------------------------------------------',/
     2  1x,'Iter          Residuum       Correlation     Cpu    Wall',/
     3  1x,'--------------------------------------------------------')
 9060 format(
     1  1x,'--------------------------------------------------------',/
     2  1x,'Iterations converged')
 9070 format(1x,A,' correlation energy / hartree = ',f25.15)
 9080 format(1x,A,' total energy / hartree       = ',f25.15)
 9100 format(1x,i4,2f18.13,2f8.1)
 9120 format(1x,A)
 9250 format(1x,'Ground-state symmetry is ',A4)
 9210 format(/,1x,'Iteration ',i3,' using ',i4,' trial vectors')
 9230 format(1x,f17.13,f18.13,f11.5,2f8.1)
 9240 format(1x,
     1'--------------------------------------------------------------'
     2,/,1x,'Iterations converged')
 9310 format(1x,A,' ground state energy / hartree  =',f25.15)
 9400 format(/,1x,A,' iterations',/,
     1  1x,'--------------------------------------',/
     2  1x,'Iter          Residuum     Cpu    Wall',/
     3  1x,'--------------------------------------')
 9410 format(
     1  1x,'--------------------------------------',/
     2  1x,'Iterations converged')
 9420 format(1x,i4,f18.13,2f8.1)
 9440 format(1x,A3,' axis ( ',A4,'symmetry)')
      end
