SUBROUTINE surface_2AP(v,x)
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
real*8 acos,tanh,v1,v2,v3b
data delper/0.075d0/
data pswitch/25d0/
data dphi/0.02d0/
data phio/2.96705972839036d0/ ! 170* degrees
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


      phiswitch=0.5d0*(1.d0+tanh((phi-phio)/(phio*dphi)))

      call Vhamil_2AP_CIV(E,r)
      call TRI3B_2AP_CIV(r(1),r(2),r(3),v3b)
      IF(r(1).lt.4.0d0.and.E(1).gt.1d-2)v3b=0d0
      v1=E(2)+v3b

      call Vhamil_2AP_C2V(E,r)
      call TRI3B_2AP_C2V(r(1),r(2),r(3),v3b)
      IF(r(1).lt.4.0d0.and.E(1).gt.1d-2)v3b=0d0
      V2=E(2)+V3b

      v=v2*(1.d0-phiswitch)+v1*phiswitch

return
END      

      
