SUBROUTINE surface_1AP(v,x)
!--------------------------------
! This Potential should be used for any scattering energies below 1.5eV. Zero energy is defined
! as the 2S+2S+2S three body dissociation limit.
!    Written by X. Li@University of Oklahoma
!    Supervisor: G. A. Parker
!    See Journal of Chemical Phyiscs 129. Page 124305 (2008)
!--------------------------------
IMPLICIT NONE
REAL*8 r(3),v,phi,x(3),phio,E(37),phiswitch
integer r2s
real*8 delper,s1switch,dphi,pswitch,s1,vli3_at,VF,temp,big
real*8 acos,tanh,v1,v2,v3b,vli2
data delper/0.075d0/
data pswitch/25d0/
data dphi/0.02d0/
data phio/2.96705972839036d0/ ! 170* degrees
data big/1d-10/
save

r(1)=maxval(x)
r(3)=minval(x)
r(2)=max(min(x(1),x(2)),min(x(2),x(3)),min(x(1),x(3)))
temp=(r(2)**2+r(3)**2-r(1)**2)/(2d0*r(2)*r(3))
IF(abs(temp-1d0).lt.1d-10)THEN
phi=acos(1d0)
ELSEIF(abs(temp+1d0).lt.1d-10)THEN
phi=acos(-1d0)
ELSE
phi=acos(temp)
ENDIF

!     No three-body fit at C_{iv} geometries
      phiswitch=0.5d0*(1.d0+tanh((phi-phio)/(phio*dphi)))
      call Vhamil_1AP_CIV(E,r)
      v1=E(1)

!     At C_{2v} geometries
      s1=(r(1)+r(2)+r(3))/dsqrt(3.0d0)
      s1switch=0.5d0*(1.d0+tanh((s1-pswitch)/(pswitch*delper)))
      call Vhamil_1AP_C2V(E,r)
      call TRI3B_1AP_C2V_3(r(1),r(2),r(3),v3b)
 
!     Make sure the highly repulsive wall is okay    
      IF(r(1).lt.4.0d0.and.E(1).gt.1d-2)THEN
      v3b=0d0
      ENDIF
  
      V2=E(1)+V3b*(1.d0-s1switch)
      v=v2*(1.d0-phiswitch)+v1*phiswitch

!     Make sure the asymptotic region is correct
      CALL vli3_F(R,VF)
      V=V*(1d0-s1switch)+VF*s1switch

return
END      

      
