********************************************************************************
*                                                                              *
*  2DHF version 1-2003                                                         *
*  Copyright (C) 1996  Jacek Kobus, Leif Laaksonen, Dage Sundholm              *
*                                                                              *
*  This software may be used and distributed according to the terms            *
*  of the GNU General Public License, see README and COPYING.                  *
*                                                                              *
********************************************************************************
c ### relax ###
c
c     Controls relaxation of Coulomb potentials, exchange potentials
c     and orbitals
c
      subroutine  relax (iorb,cw_sor,psi,pot,excp,
     &     borb,bpot,d,e,f0,f1,f2,f3,f4,g,wjac1,wjac2,
     &     wgt2,lhs,rhs,wk00,wk0,wk1,wk2)
      implicit integer*4 (i-n)
      implicit real*8 (a-h,o-z)
      real*8 lhs
      integer*4 cw_sor		

      include 'commons8.inc'
      dimension psi(*),pot(*),excp(*)
      dimension wjac1(*),wjac2(*),borb(*),bpot(*),d(*),e(*),
     &     f0(*),f1(*),f2(*),f3(*),f4(*),g(*),wgt2(*),
     &     lhs(*),rhs(*),wk00(*),wk0(*),wk1(*),wk2(*)

c     when ipoiss=3 then mcsor method is used to solve poisson's eqs for
c     potentials

      if (exlcoul.eq.0.d0) then
         if (ipoiss.eq.1) then
            call relcoul1 (iorb,cw_sor,psi,pot,excp,bpot,d,f3,g,
     &           lhs,rhs,wk2)
         elseif (ipoiss.eq.2.or.ipoiss.eq.3) then
            call relcoul2 (iorb,cw_sor,psi,pot,excp,bpot,d,f3,g,
     &           lhs,rhs,wk2)
         endif	  
      endif

c     process the exchange potentials
      
      if (islat.eq.0) then
         if (exlexp.eq.0.d0.or.exlexp.eq.2.d0) then
            if (ipoiss.eq.1) then
               call relexch1 (iorb,cw_sor,psi,pot,excp,bpot,d,e,
     &              f3,g,lhs,rhs,wk2)
            elseif (ipoiss.eq.2.or.ipoiss.eq.3) then
               call relexch2 (iorb,cw_sor,psi,pot,excp,bpot,d,e,
     &              f3,g,lhs,rhs,wk2)
            endif
	 endif
      endif


      if (exlorb.eq.0.d0) then
         if (idbg(90).ne.0) call tail(psi)
         if (ipoiss.eq.1.or.ipoiss.eq.3) then
            call relorb1(iorb,cw_sor,psi,pot,excp,borb,d,e,
     &		        f0,f1,f2,f4,wgt2,lhs,rhs,wk00,wk0,wk2)
         elseif (ipoiss.eq.2) then
            call relorb2(iorb,cw_sor,psi,pot,excp,borb,d,e,
     &           f0,f1,f2,f4,wgt2,lhs,rhs,wk00,wk0,wk2)
         endif
      endif

      return
      end
