if(nres.gt.5) then
do j=1,3
gcart(j,nres-2)=gcart(j,nres-2)+gloc(nres-4,icg)*
& dphi(j,3,nres-1)+gloc(nres-3,icg)*dphi(j,2,nres)
& +gloc(2*nres-6,icg)*
& dtheta(j,2,nres-1)+gloc(2*nres-5,icg)*dtheta(j,1,nres)
if(nres.gt.5) then
do j=1,3
gcart(j,nres-2)=gcart(j,nres-2)+gloc(nres-4,icg)*
& dphi(j,3,nres-1)+gloc(nres-3,icg)*dphi(j,2,nres)
& +gloc(2*nres-6,icg)*
& dtheta(j,2,nres-1)+gloc(2*nres-5,icg)*dtheta(j,1,nres)