subroutine interp_FOfields_mars_p(params,pchunk,F,Y_R,Y_PHI,Y_Z,B_X,B_Y,B_Z, &
PSIp,flag_cache)
TYPE(KORC_PARAMS), INTENT(IN) :: params
INTEGER, INTENT(IN) :: pchunk
TYPE(FIELDS), INTENT(IN) :: F
REAL(rp),DIMENSION(pchunk),INTENT(IN) :: Y_R,Y_PHI,Y_Z
REAL(rp),DIMENSION(pchunk),INTENT(OUT) :: B_X,B_Y,B_Z
REAL(rp),DIMENSION(pchunk) :: B_R,B_PHI
REAL(rp),DIMENSION(pchunk) :: B0_R,B0_PHI,B0_Z
REAL(rp),DIMENSION(pchunk) :: B1_R,B1_PHI,B1_Z
REAL(rp),DIMENSION(pchunk) :: B1Re_R,B1Re_PHI,B1Re_Z
REAL(rp),DIMENSION(pchunk) :: B1Im_R,B1Im_PHI,B1Im_Z
REAL(rp),DIMENSION(pchunk),INTENT(OUT) :: PSIp
REAL(rp),DIMENSION(pchunk) :: cP,sP
REAL(rp), DIMENSION(pchunk,3) :: A
! INTEGER(ip) :: ezerr
INTEGER :: cc
!! Particle chunk iterator.
INTEGER(is),DIMENSION(pchunk),INTENT(INOUT) :: flag_cache
REAL(rp) :: psip_conv
REAL(rp) :: amp
psip_conv=F%psip_conv
amp=F%AMP
call check_if_in_fields_domain_p(pchunk,F,Y_R,Y_PHI,Y_Z,flag_cache)
call EZspline_interp(bfield_2d%A,b1Refield_2d%R,b1Refield_2d%PHI, &
b1Refield_2d%Z,b1Imfield_2d%R,b1Imfield_2d%PHI,b1Imfield_2d%Z, &
pchunk,Y_R,Y_Z,A,B1Re_R,B1Re_PHI,B1Re_Z,B1Im_R,B1Im_PHI,B1Im_Z,ezerr)
call EZspline_error(ezerr)
!$OMP SIMD
do cc=1_idef,pchunk
PSIp(cc)=A(cc,1)
B0_R(cc) = psip_conv*A(cc,3)/Y_R(cc)
B0_PHI(cc) = -F%Bo*F%Ro/Y_R(cc)
B0_Z(cc) = -psip_conv*A(cc,2)/Y_R(cc)
cP(cc)=cos(Y_PHI(cc))
sP(cc)=sin(Y_PHI(cc))
B1_R(cc) = amp*(B1Re_R(cc)*cP(cc)-B1Im_R(cc)*sP(cc))
B1_PHI(cc) = amp*(B1Re_PHI(cc)*cP(cc)-B1Im_PHI(cc)*sP(cc))
B1_Z(cc) = amp*(B1Re_Z(cc)*cP(cc)-B1Im_Z(cc)*sP(cc))
B_R(cc) = B0_R(cc)+B1_R(cc)
B_PHI(cc) = B0_PHI(cc)+B1_PHI(cc)
B_Z(cc) = B0_Z(cc)+B1_Z(cc)
B_X(cc) = B_R(cc)*cP(cc) - B_PHI(cc)*sP(cc)
B_Y(cc) = B_R(cc)*sP(cc) + B_PHI(cc)*cP(cc)
end do
!$OMP END SIMD
!write(6,*) '(R,PHI,Z)',Y_R*params%cpp%length,Y_PHI*params%cpp%length, &
! Y_Z*params%cpp%length
!write(6,*) 'psi',PSIp*params%cpp%Bo*params%cpp%length**2
!write(6,*) 'dpsidR',A(:,2)*params%cpp%Bo*params%cpp%length
!write(6,*) 'dpsidZ',A(:,3)*params%cpp%Bo*params%cpp%length
!write(6,*) 'B0',B0_R*params%cpp%Bo,B0_PHI*params%cpp%Bo,B0_Z*params%cpp%Bo
!write(6,*) 'AMP',amp
!write(6,*) 'B1Re',B1Re_R*params%cpp%Bo,B1Re_PHI*params%cpp%Bo,B1Re_Z*params%cpp%Bo
!write(6,*) 'B1Im',B1Im_R*params%cpp%Bo,B1Im_PHI*params%cpp%Bo,B1Im_Z*params%cpp%Bo
!write(6,*) 'B1',B1_R*params%cpp%Bo,B1_PHI*params%cpp%Bo,B1_Z*params%cpp%Bo
!write(6,*) 'B',B_X*params%cpp%Bo,B_Y*params%cpp%Bo,B_Z*params%cpp%Bo
end subroutine interp_FOfields_mars_p