pdbread & boxshift
[unres.git] / source / unres / src-HCD-5D / energy_p_new_barrier.F
index ba7cbd8..ddc2a4d 100644 (file)
@@ -115,6 +115,7 @@ c        call chainbuild_cart
       if (nfgtasks.gt.1) then
         call MPI_Bcast(itime_mat,1,MPI_INT,king,FG_COMM,IERROR)
       endif
+c      write (iout,*) "itime_mat",itime_mat," imatupdate",imatupdate
       if (mod(itime_mat,imatupdate).eq.0) then
         call make_SCp_inter_list
         call make_SCSC_inter_list
@@ -1476,6 +1477,7 @@ C
      & sigij,r0ij,rcut,sqrij,sss1,sssgrad1
       double precision fcont,fprimcont
       double precision sscale,sscagrad
+      double precision boxshift
 c      write(iout,*)'Entering ELJ nnt=',nnt,' nct=',nct,' expon=',expon
       evdw=0.0D0
 c      do i=iatsc_s,iatsc_e
@@ -1488,6 +1490,7 @@ c      do i=iatsc_s,iatsc_e
         xi=c(1,nres+i)
         yi=c(2,nres+i)
         zi=c(3,nres+i)
+        call to_box(xi,yi,zi)
 C Change 12/1/95
         num_conti=0
 C
@@ -1499,9 +1502,13 @@ cd   &                  'iend=',iend(i,iint)
 c          do j=istart(i,iint),iend(i,iint)
             itypj=iabs(itype(j)) 
             if (itypj.eq.ntyp1) cycle
-            xj=c(1,nres+j)-xi
-            yj=c(2,nres+j)-yi
-            zj=c(3,nres+j)-zi
+            xj=c(1,nres+j)
+            yj=c(2,nres+j)
+            zj=c(3,nres+j)
+            call to_box(xj,yj,zj)
+            xj=boxshift(xj-xi,boxxsize)
+            yj=boxshift(yj-yi,boxysize)
+            zj=boxshift(zj-zi,boxzsize)
 C Change 12/1/95 to calculate four-body interactions
             rij=xj*xj+yj*yj+zj*zj
             rrij=1.0D0/rij
@@ -1649,6 +1656,7 @@ C
      & fac_augm,e_augm,r_inv_ij,r_shift_inv,sss1,sssgrad1
       logical scheck
       double precision sscale,sscagrad
+      double precision boxshift
 c     print *,'Entering ELJK nnt=',nnt,' nct=',nct,' expon=',expon
       evdw=0.0D0
 c      do i=iatsc_s,iatsc_e
@@ -1661,6 +1669,7 @@ c      do i=iatsc_s,iatsc_e
         xi=c(1,nres+i)
         yi=c(2,nres+i)
         zi=c(3,nres+i)
+        call to_box(xi,yi,zi)
 C
 C Calculate SC interaction energy.
 C
@@ -1668,9 +1677,13 @@ c        do iint=1,nint_gr(i)
 c          do j=istart(i,iint),iend(i,iint)
             itypj=iabs(itype(j))
             if (itypj.eq.ntyp1) cycle
-            xj=c(1,nres+j)-xi
-            yj=c(2,nres+j)-yi
-            zj=c(3,nres+j)-zi
+            xj=c(1,nres+j)
+            yj=c(2,nres+j)
+            zj=c(3,nres+j)
+            call to_box(xj,yj,zj)
+            xj=boxshift(xj-xi,boxxsize)
+            yj=boxshift(yj-yi,boxysize)
+            zj=boxshift(zj-zi,boxzsize)
             rrij=1.0D0/(xj*xj+yj*yj+zj*zj)
             fac_augm=rrij**expon
             e_augm=augm(itypi,itypj)*fac_augm
@@ -1748,6 +1761,7 @@ C
       double precision eps0ij,epsi,sigm,fac,e1,e2,rrij,xi,yi,zi,
      & sss1,sssgrad1
       double precision sscale,sscagrad
+      double precision boxshift
 c     double precision rrsave(maxdim)
       logical lprn
       evdw=0.0D0
@@ -1769,6 +1783,7 @@ c      do i=iatsc_s,iatsc_e
         xi=c(1,nres+i)
         yi=c(2,nres+i)
         zi=c(3,nres+i)
+        call to_box(xi,yi,zi)
         dxi=dc_norm(1,nres+i)
         dyi=dc_norm(2,nres+i)
         dzi=dc_norm(3,nres+i)
@@ -1803,9 +1818,13 @@ c           chip12=0.0D0
 c           alf1=0.0D0
 c           alf2=0.0D0
 c           alf12=0.0D0
-            xj=c(1,nres+j)-xi
-            yj=c(2,nres+j)-yi
-            zj=c(3,nres+j)-zi
+            xj=c(1,nres+j)
+            yj=c(2,nres+j)
+            zj=c(3,nres+j)
+            call to_box(xj,yj,zj)
+            xj=boxshift(xj-xi,boxxsize)
+            yj=boxshift(yj-yi,boxysize)
+            zj=boxshift(zj-zi,boxzsize)
             dxj=dc_norm(1,nres+j)
             dyj=dc_norm(2,nres+j)
             dzj=dc_norm(3,nres+j)
       include 'COMMON.SPLITELE'
       include 'COMMON.SBRIDGE'
       logical lprn
-      integer xshift,yshift,zshift,subchap
       double precision evdw
       integer itypi,itypj,itypi1,iint,ind,ikont
       double precision eps0ij,epsi,sigm,fac,e1,e2,rrij,xi,yi,zi
       double precision fracinbuf,sslipi,evdwij_przed_tri,sig0ij,
-     & sslipj,ssgradlipj,ssgradlipi,dist_init,xj_safe,yj_safe,zj_safe,
-     & xj_temp,yj_temp,zj_temp,dist_temp,sig,rij_shift,faclip
+     & sslipj,ssgradlipj,ssgradlipi,sig,rij_shift,faclip
       double precision dist,sscale,sscagrad,sscagradlip,sscalelip
+      double precision boxshift
       evdw=0.0D0
 ccccc      energy_dec=.false.
 C      print *,'Entering EGB nnt=',nnt,' nct=',nct,' expon=',expon
@@ -1912,66 +1930,14 @@ c      do i=iatsc_s,iatsc_e
         xi=c(1,nres+i)
         yi=c(2,nres+i)
         zi=c(3,nres+i)
-C Return atom into box, boxxsize is size of box in x dimension
-c  134   continue
-c        if (xi.gt.((xshift+0.5d0)*boxxsize)) xi=xi-boxxsize
-c        if (xi.lt.((xshift-0.5d0)*boxxsize)) xi=xi+boxxsize
-C Condition for being inside the proper box
-c        if ((xi.gt.((xshift+0.5d0)*boxxsize)).or.
-c     &       (xi.lt.((xshift-0.5d0)*boxxsize))) then
-c        go to 134
-c        endif
-c  135   continue
-c        if (yi.gt.((yshift+0.5d0)*boxysize)) yi=yi-boxysize
-c        if (yi.lt.((yshift-0.5d0)*boxysize)) yi=yi+boxysize
-C Condition for being inside the proper box
-c        if ((yi.gt.((yshift+0.5d0)*boxysize)).or.
-c     &       (yi.lt.((yshift-0.5d0)*boxysize))) then
-c        go to 135
-c        endif
-c  136   continue
-c        if (zi.gt.((zshift+0.5d0)*boxzsize)) zi=zi-boxzsize
-c        if (zi.lt.((zshift-0.5d0)*boxzsize)) zi=zi+boxzsize
-C Condition for being inside the proper box
-c        if ((zi.gt.((zshift+0.5d0)*boxzsize)).or.
-c     &       (zi.lt.((zshift-0.5d0)*boxzsize))) then
-c        go to 136
-c        endif
-          xi=mod(xi,boxxsize)
-          if (xi.lt.0) xi=xi+boxxsize
-          yi=mod(yi,boxysize)
-          if (yi.lt.0) yi=yi+boxysize
-          zi=mod(zi,boxzsize)
-          if (zi.lt.0) zi=zi+boxzsize
+        call to_box(xi,yi,zi)
 C define scaling factor for lipids
 
 C        if (positi.le.0) positi=positi+boxzsize
 C        print *,i
 C first for peptide groups
 c for each residue check if it is in lipid or lipid water border area
-       if ((zi.gt.bordlipbot)
-     &.and.(zi.lt.bordliptop)) then
-C the energy transfer exist
-        if (zi.lt.buflipbot) then
-C what fraction I am in
-         fracinbuf=1.0d0-
-     &        ((zi-bordlipbot)/lipbufthick)
-C lipbufthick is thickenes of lipid buffore
-         sslipi=sscalelip(fracinbuf)
-         ssgradlipi=-sscagradlip(fracinbuf)/lipbufthick
-        elseif (zi.gt.bufliptop) then
-         fracinbuf=1.0d0-((bordliptop-zi)/lipbufthick)
-         sslipi=sscalelip(fracinbuf)
-         ssgradlipi=sscagradlip(fracinbuf)/lipbufthick
-        else
-         sslipi=1.0d0
-         ssgradlipi=0.0
-        endif
-       else
-         sslipi=0.0d0
-         ssgradlipi=0.0
-       endif
-
+        call lipid_layer(xi,yi,zi,sslipi,ssgradlipi)
 C          xi=xi+xshift*boxxsize
 C          yi=yi+yshift*boxysize
 C          zi=zi+zshift*boxzsize
@@ -1998,15 +1964,15 @@ c              write(iout,*) "PO ZWYKLE", evdwij
               if (energy_dec) write (iout,'(a6,2i5,0pf7.3,a3)') 
      &                        'evdw',i,j,evdwij,' ss'
 C triple bond artifac removal
-             do k=j+1,iend(i,iint) 
+              do k=j+1,iend(i,iint) 
 C search over all next residues
-              if (dyn_ss_mask(k)) then
+                if (dyn_ss_mask(k)) then
 C check if they are cysteins
 C              write(iout,*) 'k=',k
 
 c              write(iout,*) "PRZED TRI", evdwij
-               evdwij_przed_tri=evdwij
-              call triple_ssbond_ene(i,j,k,evdwij)
+                  evdwij_przed_tri=evdwij
+                  call triple_ssbond_ene(i,j,k,evdwij)
 c               if(evdwij_przed_tri.ne.evdwij) then
 c                 write (iout,*) "TRI:", evdwij, evdwij_przed_tri
 c               endif
@@ -2014,30 +1980,30 @@ c               endif
 c              write(iout,*) "PO TRI", evdwij
 C call the energy function that removes the artifical triple disulfide
 C bond the soubroutine is located in ssMD.F
-              evdw=evdw+evdwij             
-              if (energy_dec) write (iout,'(a6,2i5,0pf7.3,a3)')
+                  evdw=evdw+evdwij             
+                  if (energy_dec) write (iout,'(a6,2i5,0pf7.3,a3)')
      &                        'evdw',i,j,evdwij,'tss'
-              endif!dyn_ss_mask(k)
-             enddo! k
+                endif!dyn_ss_mask(k)
+              enddo! k
             ELSE
-            ind=ind+1
-            itypj=iabs(itype(j))
-            if (itypj.eq.ntyp1) cycle
+              ind=ind+1
+              itypj=iabs(itype(j))
+              if (itypj.eq.ntyp1) cycle
 c            dscj_inv=dsc_inv(itypj)
-            dscj_inv=vbld_inv(j+nres)
+              dscj_inv=vbld_inv(j+nres)
 c            write (iout,*) "j",j,dsc_inv(itypj),dscj_inv,
 c     &       1.0d0/vbld(j+nres)
 c            write (iout,*) "i",i," j", j," itype",itype(i),itype(j)
-            sig0ij=sigma(itypi,itypj)
-            chi1=chi(itypi,itypj)
-            chi2=chi(itypj,itypi)
-            chi12=chi1*chi2
-            chip1=chip(itypi)
-            chip2=chip(itypj)
-            chip12=chip1*chip2
-            alf1=alp(itypi)
-            alf2=alp(itypj)
-            alf12=0.5D0*(alf1+alf2)
+              sig0ij=sigma(itypi,itypj)
+              chi1=chi(itypi,itypj)
+              chi2=chi(itypj,itypi)
+              chi12=chi1*chi2
+              chip1=chip(itypi)
+              chip2=chip(itypj)
+              chip12=chip1*chip2
+              alf1=alp(itypi)
+              alf2=alp(itypj)
+              alf12=0.5D0*(alf1+alf2)
 C For diagnostics only!!!
 c           chi1=0.0D0
 c           chi2=0.0D0
@@ -2048,191 +2014,112 @@ c           chip12=0.0D0
 c           alf1=0.0D0
 c           alf2=0.0D0
 c           alf12=0.0D0
-            xj=c(1,nres+j)
-            yj=c(2,nres+j)
-            zj=c(3,nres+j)
-C Return atom J into box the original box
-c  137   continue
-c        if (xj.gt.((0.5d0)*boxxsize)) xj=xj-boxxsize
-c        if (xj.lt.((-0.5d0)*boxxsize)) xj=xj+boxxsize
-C Condition for being inside the proper box
-c        if ((xj.gt.((0.5d0)*boxxsize)).or.
-c     &       (xj.lt.((-0.5d0)*boxxsize))) then
-c        go to 137
-c        endif
-c  138   continue
-c        if (yj.gt.((0.5d0)*boxysize)) yj=yj-boxysize
-c        if (yj.lt.((-0.5d0)*boxysize)) yj=yj+boxysize
-C Condition for being inside the proper box
-c        if ((yj.gt.((0.5d0)*boxysize)).or.
-c     &       (yj.lt.((-0.5d0)*boxysize))) then
-c        go to 138
-c        endif
-c  139   continue
-c        if (zj.gt.((0.5d0)*boxzsize)) zj=zj-boxzsize
-c        if (zj.lt.((-0.5d0)*boxzsize)) zj=zj+boxzsize
-C Condition for being inside the proper box
-c        if ((zj.gt.((0.5d0)*boxzsize)).or.
-c     &       (zj.lt.((-0.5d0)*boxzsize))) then
-c        go to 139
-c        endif
-          xj=mod(xj,boxxsize)
-          if (xj.lt.0) xj=xj+boxxsize
-          yj=mod(yj,boxysize)
-          if (yj.lt.0) yj=yj+boxysize
-          zj=mod(zj,boxzsize)
-          if (zj.lt.0) zj=zj+boxzsize
-       if ((zj.gt.bordlipbot)
-     &.and.(zj.lt.bordliptop)) then
-C the energy transfer exist
-        if (zj.lt.buflipbot) then
-C what fraction I am in
-         fracinbuf=1.0d0-
-     &        ((zj-bordlipbot)/lipbufthick)
-C lipbufthick is thickenes of lipid buffore
-         sslipj=sscalelip(fracinbuf)
-         ssgradlipj=-sscagradlip(fracinbuf)/lipbufthick
-        elseif (zj.gt.bufliptop) then
-         fracinbuf=1.0d0-((bordliptop-zj)/lipbufthick)
-         sslipj=sscalelip(fracinbuf)
-         ssgradlipj=sscagradlip(fracinbuf)/lipbufthick
-        else
-         sslipj=1.0d0
-         ssgradlipj=0.0
-        endif
-       else
-         sslipj=0.0d0
-         ssgradlipj=0.0
-       endif
-      aa=aa_lip(itypi,itypj)*(sslipi+sslipj)/2.0d0
-     &  +aa_aq(itypi,itypj)*(2.0d0-sslipi-sslipj)/2.0d0
-      bb=bb_lip(itypi,itypj)*(sslipi+sslipj)/2.0d0
-     &  +bb_aq(itypi,itypj)*(2.0d0-sslipi-sslipj)/2.0d0
+              xj=c(1,nres+j)
+              yj=c(2,nres+j)
+              zj=c(3,nres+j)
+              call to_box(xj,yj,zj)
+              call lipid_layer(xj,yj,zj,sslipj,ssgradlipj)
+              aa=aa_lip(itypi,itypj)*(sslipi+sslipj)/2.0d0
+     &          +aa_aq(itypi,itypj)*(2.0d0-sslipi-sslipj)/2.0d0
+              bb=bb_lip(itypi,itypj)*(sslipi+sslipj)/2.0d0
+     &          +bb_aq(itypi,itypj)*(2.0d0-sslipi-sslipj)/2.0d0
 C      write(iout,*) "tu,", i,j,aa_lip(itypi,itypj),bb_lip(itypi,itypj)
 C      if (aa.ne.aa_aq(itypi,itypj)) write(63,'(2e10.5)')
 C     &(aa-aa_aq(itypi,itypj)),(bb-bb_aq(itypi,itypj))
 C      if (ssgradlipj.gt.0.0d0) print *,"??WTF??"
 C      print *,sslipi,sslipj,bordlipbot,zi,zj
-      dist_init=(xj-xi)**2+(yj-yi)**2+(zj-zi)**2
-      xj_safe=xj
-      yj_safe=yj
-      zj_safe=zj
-      subchap=0
-      do xshift=-1,1
-      do yshift=-1,1
-      do zshift=-1,1
-          xj=xj_safe+xshift*boxxsize
-          yj=yj_safe+yshift*boxysize
-          zj=zj_safe+zshift*boxzsize
-          dist_temp=(xj-xi)**2+(yj-yi)**2+(zj-zi)**2
-          if(dist_temp.lt.dist_init) then
-            dist_init=dist_temp
-            xj_temp=xj
-            yj_temp=yj
-            zj_temp=zj
-            subchap=1
-          endif
-       enddo
-       enddo
-       enddo
-       if (subchap.eq.1) then
-          xj=xj_temp-xi
-          yj=yj_temp-yi
-          zj=zj_temp-zi
-       else
-          xj=xj_safe-xi
-          yj=yj_safe-yi
-          zj=zj_safe-zi
-       endif
-            dxj=dc_norm(1,nres+j)
-            dyj=dc_norm(2,nres+j)
-            dzj=dc_norm(3,nres+j)
+              xj=boxshift(xj-xi,boxxsize)
+              yj=boxshift(yj-yi,boxysize)
+              zj=boxshift(zj-zi,boxzsize)
+              dxj=dc_norm(1,nres+j)
+              dyj=dc_norm(2,nres+j)
+              dzj=dc_norm(3,nres+j)
 C            xj=xj-xi
 C            yj=yj-yi
 C            zj=zj-zi
 c            write (iout,*) "dcnorj",dxi*dxi+dyi*dyi+dzi*dzi
 c            write (iout,*) "j",j," dc_norm",
 c     &       dc_norm(1,nres+j),dc_norm(2,nres+j),dc_norm(3,nres+j)
-            rrij=1.0D0/(xj*xj+yj*yj+zj*zj)
-            rij=dsqrt(rrij)
-            sss=sscale(1.0d0/rij,r_cut_int)
+              rrij=1.0D0/(xj*xj+yj*yj+zj*zj)
+              rij=dsqrt(rrij)
+              sss=sscale(1.0d0/rij,r_cut_int)
 c            write (iout,'(a7,4f8.3)') 
 c    &      "ssscale",sss,((1.0d0/rij)/sigma(itypi,itypj)),r_cut,rlamb
-            if (sss.eq.0.0d0) cycle
-            sssgrad=sscagrad(1.0d0/rij,r_cut_int)
+              if (sss.eq.0.0d0) cycle
+              sssgrad=sscagrad(1.0d0/rij,r_cut_int)
 C Calculate angle-dependent terms of energy and contributions to their
 C derivatives.
-            call sc_angular
-            sigsq=1.0D0/sigsq
-            sig=sig0ij*dsqrt(sigsq)
-            rij_shift=1.0D0/rij-sig+sig0ij
+              call sc_angular
+              sigsq=1.0D0/sigsq
+              sig=sig0ij*dsqrt(sigsq)
+              rij_shift=1.0D0/rij-sig+sig0ij
 c for diagnostics; uncomment
 c            rij_shift=1.2*sig0ij
 C I hate to put IF's in the loops, but here don't have another choice!!!!
-            if (rij_shift.le.0.0D0) then
-              evdw=1.0D20
+              if (rij_shift.le.0.0D0) then
+                evdw=1.0D20
 cd              write (iout,'(2(a3,i3,2x),17(0pf7.3))')
 cd     &        restyp(itypi),i,restyp(itypj),j,
 cd     &        rij_shift,1.0D0/rij,sig,sig0ij,sigsq,1-dsqrt(sigsq) 
-              return
-            endif
-            sigder=-sig*sigsq
+                return
+              endif
+              sigder=-sig*sigsq
 c---------------------------------------------------------------
-            rij_shift=1.0D0/rij_shift 
-            fac=rij_shift**expon
+              rij_shift=1.0D0/rij_shift 
+              fac=rij_shift**expon
 C here to start with
 C            if (c(i,3).gt.
-            faclip=fac
-            e1=fac*fac*aa
-            e2=fac*bb
-            evdwij=eps1*eps2rt*eps3rt*(e1+e2)
-            eps2der=evdwij*eps3rt
-            eps3der=evdwij*eps2rt
+              faclip=fac
+              e1=fac*fac*aa
+              e2=fac*bb
+              evdwij=eps1*eps2rt*eps3rt*(e1+e2)
+              eps2der=evdwij*eps3rt
+              eps3der=evdwij*eps2rt
 C       write(63,'(2i3,2e10.3,2f10.5)') i,j,aa,bb, evdwij,
 C     &((sslipi+sslipj)/2.0d0+
 C     &(2.0d0-sslipi-sslipj)/2.0d0)
 c            write (iout,*) "sigsq",sigsq," sig",sig," eps2rt",eps2rt,
 c     &        " eps3rt",eps3rt," eps1",eps1," e1",e1," e2",e2
-            evdwij=evdwij*eps2rt*eps3rt
-            evdw=evdw+evdwij*sss
-            if (lprn) then
-            sigm=dabs(aa/bb)**(1.0D0/6.0D0)
-            epsi=bb**2/aa
-            write (iout,'(2(a3,i3,2x),17(0pf7.3))')
-     &        restyp(itypi),i,restyp(itypj),j,
-     &        epsi,sigm,chi1,chi2,chip1,chip2,
-     &        eps1,eps2rt**2,eps3rt**2,sig,sig0ij,
-     &        om1,om2,om12,1.0D0/rij,1.0D0/rij_shift,
-     &        evdwij
-            endif
+              evdwij=evdwij*eps2rt*eps3rt
+              evdw=evdw+evdwij*sss
+              if (lprn) then
+                sigm=dabs(aa/bb)**(1.0D0/6.0D0)
+                epsi=bb**2/aa
+                write (iout,'(2(a3,i3,2x),17(0pf7.3))')
+     &           restyp(itypi),i,restyp(itypj),j,
+     &           epsi,sigm,chi1,chi2,chip1,chip2,
+     &           eps1,eps2rt**2,eps3rt**2,sig,sig0ij,
+     &           om1,om2,om12,1.0D0/rij,1.0D0/rij_shift,
+     &           evdwij
+              endif
 
-            if (energy_dec) write (iout,'(a,2i5,3f10.5)') 
+              if (energy_dec) write (iout,'(a,2i5,3f10.5)') 
      &                    'r sss evdw',i,j,1.0d0/rij,sss,evdwij
 
 C Calculate gradient components.
-            e1=e1*eps1*eps2rt**2*eps3rt**2
-            fac=-expon*(e1+evdwij)*rij_shift
-            sigder=fac*sigder
-            fac=rij*fac
+              e1=e1*eps1*eps2rt**2*eps3rt**2
+              fac=-expon*(e1+evdwij)*rij_shift
+              sigder=fac*sigder
+              fac=rij*fac
 c            print '(2i4,6f8.4)',i,j,sss,sssgrad*
 c     &      evdwij,fac,sigma(itypi,itypj),expon
-            fac=fac+evdwij*sssgrad/sss*rij
+              fac=fac+evdwij*sssgrad/sss*rij
 c            fac=0.0d0
 C Calculate the radial part of the gradient
-            gg_lipi(3)=eps1*(eps2rt*eps2rt)
-     &       *(eps3rt*eps3rt)*sss/2.0d0*(faclip*faclip*
-     &        (aa_lip(itypi,itypj)-aa_aq(itypi,itypj))
-     &       +faclip*(bb_lip(itypi,itypj)-bb_aq(itypi,itypj)))
-            gg_lipj(3)=ssgradlipj*gg_lipi(3)
-            gg_lipi(3)=gg_lipi(3)*ssgradlipi
+              gg_lipi(3)=eps1*(eps2rt*eps2rt)
+     &          *(eps3rt*eps3rt)*sss/2.0d0*(faclip*faclip*
+     &           (aa_lip(itypi,itypj)-aa_aq(itypi,itypj))
+     &          +faclip*(bb_lip(itypi,itypj)-bb_aq(itypi,itypj)))
+              gg_lipj(3)=ssgradlipj*gg_lipi(3)
+              gg_lipi(3)=gg_lipi(3)*ssgradlipi
 C            gg_lipi(3)=0.0d0
 C            gg_lipj(3)=0.0d0
-            gg(1)=xj*fac
-            gg(2)=yj*fac
-            gg(3)=zj*fac
+              gg(1)=xj*fac
+              gg(2)=yj*fac
+              gg(3)=zj*fac
 C Calculate angular part of the gradient.
 c            call sc_grad_scale(sss)
-            call sc_grad
+              call sc_grad
             ENDIF    ! dyn_ss            
 c          enddo      ! j
 c        enddo        ! iint
@@ -2262,7 +2149,7 @@ C
       include 'COMMON.IOUNITS'
       include 'COMMON.CALC'
       include 'COMMON.SPLITELE'
-      integer xshift,yshift,zshift,subchap
+      double precision boxshift
       integer icall
       common /srutu/ icall
       logical lprn
@@ -2271,8 +2158,7 @@ C
       double precision eps0ij,epsi,sigm,fac,e1,e2,rrij,r0ij,
      & xi,yi,zi,fac_augm,e_augm
       double precision fracinbuf,sslipi,evdwij_przed_tri,sig0ij,
-     & sslipj,ssgradlipj,ssgradlipi,dist_init,xj_safe,yj_safe,zj_safe,
-     & xj_temp,yj_temp,zj_temp,dist_temp,sig,rij_shift,faclip,sssgrad1
+     & sslipj,ssgradlipj,ssgradlipi,sig,rij_shift,faclip,sssgrad1
       double precision dist,sscale,sscagrad,sscagradlip,sscalelip
       evdw=0.0D0
 c     print *,'Entering EGB nnt=',nnt,' nct=',nct,' expon=',expon
@@ -2290,41 +2176,14 @@ c      do i=iatsc_s,iatsc_e
         xi=c(1,nres+i)
         yi=c(2,nres+i)
         zi=c(3,nres+i)
-          xi=mod(xi,boxxsize)
-          if (xi.lt.0) xi=xi+boxxsize
-          yi=mod(yi,boxysize)
-          if (yi.lt.0) yi=yi+boxysize
-          zi=mod(zi,boxzsize)
-          if (zi.lt.0) zi=zi+boxzsize
+        call to_box(xi,yi,zi)
 C define scaling factor for lipids
 
 C        if (positi.le.0) positi=positi+boxzsize
 C        print *,i
 C first for peptide groups
 c for each residue check if it is in lipid or lipid water border area
-       if ((zi.gt.bordlipbot)
-     &.and.(zi.lt.bordliptop)) then
-C the energy transfer exist
-        if (zi.lt.buflipbot) then
-C what fraction I am in
-         fracinbuf=1.0d0-
-     &        ((zi-bordlipbot)/lipbufthick)
-C lipbufthick is thickenes of lipid buffore
-         sslipi=sscalelip(fracinbuf)
-         ssgradlipi=-sscagradlip(fracinbuf)/lipbufthick
-        elseif (zi.gt.bufliptop) then
-         fracinbuf=1.0d0-((bordliptop-zi)/lipbufthick)
-         sslipi=sscalelip(fracinbuf)
-         ssgradlipi=sscagradlip(fracinbuf)/lipbufthick
-        else
-         sslipi=1.0d0
-         ssgradlipi=0.0
-        endif
-       else
-         sslipi=0.0d0
-         ssgradlipi=0.0
-       endif
-
+        call lipid_layer(xi,yi,zi,sslipi,ssgradlipi)
         dxi=dc_norm(1,nres+i)
         dyi=dc_norm(2,nres+i)
         dzi=dc_norm(3,nres+i)
@@ -2361,131 +2220,77 @@ c           chip12=0.0D0
 c           alf1=0.0D0
 c           alf2=0.0D0
 c           alf12=0.0D0
-C            xj=c(1,nres+j)-xi
-C            yj=c(2,nres+j)-yi
-C            zj=c(3,nres+j)-zi
-          xj=mod(xj,boxxsize)
-          if (xj.lt.0) xj=xj+boxxsize
-          yj=mod(yj,boxysize)
-          if (yj.lt.0) yj=yj+boxysize
-          zj=mod(zj,boxzsize)
-          if (zj.lt.0) zj=zj+boxzsize
-       if ((zj.gt.bordlipbot)
-     &.and.(zj.lt.bordliptop)) then
-C the energy transfer exist
-        if (zj.lt.buflipbot) then
-C what fraction I am in
-         fracinbuf=1.0d0-
-     &        ((zj-bordlipbot)/lipbufthick)
-C lipbufthick is thickenes of lipid buffore
-         sslipj=sscalelip(fracinbuf)
-         ssgradlipj=-sscagradlip(fracinbuf)/lipbufthick
-        elseif (zj.gt.bufliptop) then
-         fracinbuf=1.0d0-((bordliptop-zj)/lipbufthick)
-         sslipj=sscalelip(fracinbuf)
-         ssgradlipj=sscagradlip(fracinbuf)/lipbufthick
-        else
-         sslipj=1.0d0
-         ssgradlipj=0.0
-        endif
-       else
-         sslipj=0.0d0
-         ssgradlipj=0.0
-       endif
-      aa=aa_lip(itypi,itypj)*(sslipi+sslipj)/2.0d0
-     &  +aa_aq(itypi,itypj)*(2.0d0-sslipi-sslipj)/2.0d0
-      bb=bb_lip(itypi,itypj)*(sslipi+sslipj)/2.0d0
-     &  +bb_aq(itypi,itypj)*(2.0d0-sslipi-sslipj)/2.0d0
+           xj=c(1,nres+j)
+           yj=c(2,nres+j)
+           zj=c(3,nres+j)
+           call to_box(xj,yj,zj)
+           call lipid_layer(xj,yj,zj,sslipj,ssgradlipj)
+           aa=aa_lip(itypi,itypj)*(sslipi+sslipj)/2.0d0
+     &       +aa_aq(itypi,itypj)*(2.0d0-sslipi-sslipj)/2.0d0
+           bb=bb_lip(itypi,itypj)*(sslipi+sslipj)/2.0d0
+     &       +bb_aq(itypi,itypj)*(2.0d0-sslipi-sslipj)/2.0d0
 C      if (aa.ne.aa_aq(itypi,itypj)) write(63,'2e10.5') 
 C     &(aa-aa_aq(itypi,itypj)),(bb-bb_aq(itypi,itypj))
 C      write(iout,*) "tu,", i,j,aa,bb,aa_lip(itypi,itypj),sslipi,sslipj
-      dist_init=(xj-xi)**2+(yj-yi)**2+(zj-zi)**2
-      xj_safe=xj
-      yj_safe=yj
-      zj_safe=zj
-      subchap=0
-      do xshift=-1,1
-      do yshift=-1,1
-      do zshift=-1,1
-          xj=xj_safe+xshift*boxxsize
-          yj=yj_safe+yshift*boxysize
-          zj=zj_safe+zshift*boxzsize
-          dist_temp=(xj-xi)**2+(yj-yi)**2+(zj-zi)**2
-          if(dist_temp.lt.dist_init) then
-            dist_init=dist_temp
-            xj_temp=xj
-            yj_temp=yj
-            zj_temp=zj
-            subchap=1
-          endif
-       enddo
-       enddo
-       enddo
-       if (subchap.eq.1) then
-          xj=xj_temp-xi
-          yj=yj_temp-yi
-          zj=zj_temp-zi
-       else
-          xj=xj_safe-xi
-          yj=yj_safe-yi
-          zj=zj_safe-zi
-       endif
-            dxj=dc_norm(1,nres+j)
-            dyj=dc_norm(2,nres+j)
-            dzj=dc_norm(3,nres+j)
-            rrij=1.0D0/(xj*xj+yj*yj+zj*zj)
-            rij=dsqrt(rrij)
-            sss=sscale(1.0d0/rij,r_cut_int)
-            if (sss.eq.0.0d0) cycle
-            sssgrad=sscagrad(1.0d0/rij,r_cut_int)
+           xj=boxshift(xj-xi,boxxsize)
+           yj=boxshift(yj-yi,boxysize)
+           zj=boxshift(zj-zi,boxzsize)
+           dxj=dc_norm(1,nres+j)
+           dyj=dc_norm(2,nres+j)
+           dzj=dc_norm(3,nres+j)
+           rrij=1.0D0/(xj*xj+yj*yj+zj*zj)
+           rij=dsqrt(rrij)
+           sss=sscale(1.0d0/rij,r_cut_int)
+           if (sss.eq.0.0d0) cycle
+           sssgrad=sscagrad(1.0d0/rij,r_cut_int)
 C Calculate angle-dependent terms of energy and contributions to their
 C derivatives.
-            call sc_angular
-            sigsq=1.0D0/sigsq
-            sig=sig0ij*dsqrt(sigsq)
-            rij_shift=1.0D0/rij-sig+r0ij
+           call sc_angular
+           sigsq=1.0D0/sigsq
+           sig=sig0ij*dsqrt(sigsq)
+           rij_shift=1.0D0/rij-sig+r0ij
 C I hate to put IF's in the loops, but here don't have another choice!!!!
-            if (rij_shift.le.0.0D0) then
-              evdw=1.0D20
-              return
-            endif
-            sigder=-sig*sigsq
+           if (rij_shift.le.0.0D0) then
+             evdw=1.0D20
+             return
+           endif
+           sigder=-sig*sigsq
 c---------------------------------------------------------------
-            rij_shift=1.0D0/rij_shift 
-            fac=rij_shift**expon
-            e1=fac*fac*aa
-            e2=fac*bb
-            evdwij=eps1*eps2rt*eps3rt*(e1+e2)
-            eps2der=evdwij*eps3rt
-            eps3der=evdwij*eps2rt
-            fac_augm=rrij**expon
-            e_augm=augm(itypi,itypj)*fac_augm
-            evdwij=evdwij*eps2rt*eps3rt
-            evdw=evdw+evdwij+e_augm
-            if (lprn) then
-            sigm=dabs(aa/bb)**(1.0D0/6.0D0)
-            epsi=bb**2/aa
-            write (iout,'(2(a3,i3,2x),17(0pf7.3))')
+           rij_shift=1.0D0/rij_shift 
+           fac=rij_shift**expon
+           e1=fac*fac*aa
+           e2=fac*bb
+           evdwij=eps1*eps2rt*eps3rt*(e1+e2)
+           eps2der=evdwij*eps3rt
+           eps3der=evdwij*eps2rt
+           fac_augm=rrij**expon
+           e_augm=augm(itypi,itypj)*fac_augm
+           evdwij=evdwij*eps2rt*eps3rt
+           evdw=evdw+evdwij+e_augm
+           if (lprn) then
+             sigm=dabs(aa/bb)**(1.0D0/6.0D0)
+             epsi=bb**2/aa
+             write (iout,'(2(a3,i3,2x),17(0pf7.3))')
      &        restyp(itypi),i,restyp(itypj),j,
      &        epsi,sigm,sig,(augm(itypi,itypj)/epsi)**(1.0D0/12.0D0),
      &        chi1,chi2,chip1,chip2,
      &        eps1,eps2rt**2,eps3rt**2,
      &        om1,om2,om12,1.0D0/rij,1.0D0/rij_shift,
      &        evdwij+e_augm
-            endif
+           endif
 C Calculate gradient components.
-            e1=e1*eps1*eps2rt**2*eps3rt**2
-            fac=-expon*(e1+evdwij)*rij_shift
-            sigder=fac*sigder
-            fac=rij*fac-2*expon*rrij*e_augm
-            fac=fac+(evdwij+e_augm)*sssgrad/sss*rij
+           e1=e1*eps1*eps2rt**2*eps3rt**2
+           fac=-expon*(e1+evdwij)*rij_shift
+           sigder=fac*sigder
+           fac=rij*fac-2*expon*rrij*e_augm
+           fac=fac+(evdwij+e_augm)*sssgrad/sss*rij
 C Calculate the radial part of the gradient
-            gg(1)=xj*fac
-            gg(2)=yj*fac
-            gg(3)=zj*fac
+           gg(1)=xj*fac
+           gg(2)=yj*fac
+           gg(3)=zj*fac
 C Calculate angular part of the gradient.
 c            call sc_grad_scale(sss)
-            call sc_grad
+           call sc_grad
 c          enddo      ! j
 c        enddo        ! iint
       enddo          ! i
@@ -2636,6 +2441,7 @@ C
       include 'COMMON.IOUNITS'
 c      include 'COMMON.CONTACTS'
       dimension gg(3)
+      double precision boxshift
 cd    print *,'Entering Esoft_sphere nnt=',nnt,' nct=',nct
       evdw=0.0D0
 c      do i=iatsc_s,iatsc_e
@@ -2648,6 +2454,7 @@ c      do i=iatsc_s,iatsc_e
         xi=c(1,nres+i)
         yi=c(2,nres+i)
         zi=c(3,nres+i)
+        call to_box(xi,yi,zi)
 C
 C Calculate SC interaction energy.
 C
@@ -2657,9 +2464,9 @@ cd   &                  'iend=',iend(i,iint)
 c          do j=istart(i,iint),iend(i,iint)
             itypj=iabs(itype(j))
             if (itypj.eq.ntyp1) cycle
-            xj=c(1,nres+j)-xi
-            yj=c(2,nres+j)-yi
-            zj=c(3,nres+j)-zi
+            xj=boxshift(c(1,nres+j)-xi,boxxsize)
+            yj=boxshift(c(2,nres+j)-yi,boxysize)
+            zj=boxshift(c(3,nres+j)-zi,boxzsize)
             rij=xj*xj+yj*yj+zj*zj
 c           write (iout,*)'i=',i,' j=',j,' itypi=',itypi,' itypj=',itypj
             r0ij=r0(itypi,itypj)
@@ -2716,7 +2523,7 @@ c      include 'COMMON.CONTACTS'
       include 'COMMON.VECTORS'
       include 'COMMON.FFIELD'
       dimension ggg(3)
-      integer xshift,yshift,zshift
+      double precision boxshift
 C      write(iout,*) 'In EELEC_soft_sphere'
       ees=0.0D0
       evdw1=0.0D0
@@ -2732,12 +2539,7 @@ C      write(iout,*) 'In EELEC_soft_sphere'
         xmedi=c(1,i)+0.5d0*dxi
         ymedi=c(2,i)+0.5d0*dyi
         zmedi=c(3,i)+0.5d0*dzi
-          xmedi=mod(xmedi,boxxsize)
-          if (xmedi.lt.0) xmedi=xmedi+boxxsize
-          ymedi=mod(ymedi,boxysize)
-          if (ymedi.lt.0) ymedi=ymedi+boxysize
-          zmedi=mod(zmedi,boxzsize)
-          if (zmedi.lt.0) zmedi=zmedi+boxzsize
+        call to_box(xmedi,ymedi,zmedi)
         num_conti=0
 c        write (iout,*) 'i',i,' ielstart',ielstart(i),' ielend',ielend(i)
         do j=ielstart(i),ielend(i)
@@ -2754,43 +2556,10 @@ c        write (iout,*) 'i',i,' ielstart',ielstart(i),' ielend',ielend(i)
           xj=c(1,j)+0.5D0*dxj
           yj=c(2,j)+0.5D0*dyj
           zj=c(3,j)+0.5D0*dzj
-          xj=mod(xj,boxxsize)
-          if (xj.lt.0) xj=xj+boxxsize
-          yj=mod(yj,boxysize)
-          if (yj.lt.0) yj=yj+boxysize
-          zj=mod(zj,boxzsize)
-          if (zj.lt.0) zj=zj+boxzsize
-      dist_init=(xj-xmedi)**2+(yj-ymedi)**2+(zj-zmedi)**2
-      xj_safe=xj
-      yj_safe=yj
-      zj_safe=zj
-      isubchap=0
-      do xshift=-1,1
-      do yshift=-1,1
-      do zshift=-1,1
-          xj=xj_safe+xshift*boxxsize
-          yj=yj_safe+yshift*boxysize
-          zj=zj_safe+zshift*boxzsize
-          dist_temp=(xj-xi)**2+(yj-yi)**2+(zj-zi)**2
-          if(dist_temp.lt.dist_init) then
-            dist_init=dist_temp
-            xj_temp=xj
-            yj_temp=yj
-            zj_temp=zj
-            isubchap=1
-          endif
-       enddo
-       enddo
-       enddo
-       if (isubchap.eq.1) then
-          xj=xj_temp-xmedi
-          yj=yj_temp-ymedi
-          zj=zj_temp-zmedi
-       else
-          xj=xj_safe-xmedi
-          yj=yj_safe-ymedi
-          zj=zj_safe-zmedi
-       endif
+          call to_box(xj,yj,zj)
+          xj=boxshift(xj-xmedi,boxxsize)
+          yj=boxshift(yj-ymedi,boxysize)
+          zj=boxshift(zj-zmedi,boxzsize)
           rij=xj*xj+yj*yj+zj*zj
             sss=sscale(sqrt(rij),r_cut_int)
             sssgrad=sscagrad(sqrt(rij),r_cut_int)
@@ -3786,12 +3555,7 @@ c        end if
         xmedi=c(1,i)+0.5d0*dxi
         ymedi=c(2,i)+0.5d0*dyi
         zmedi=c(3,i)+0.5d0*dzi
-          xmedi=mod(xmedi,boxxsize)
-          if (xmedi.lt.0) xmedi=xmedi+boxxsize
-          ymedi=mod(ymedi,boxysize)
-          if (ymedi.lt.0) ymedi=ymedi+boxysize
-          zmedi=mod(zmedi,boxzsize)
-          if (zmedi.lt.0) zmedi=zmedi+boxzsize
+        call to_box(xmedi,ymedi,zmedi)
         num_conti=0
         call eelecij(i,i+2,ees,evdw1,eel_loc)
         if (wturn3.gt.0.0d0) call eturn3(i,eello_turn3)
@@ -3846,13 +3610,7 @@ c        if ((zmedi.gt.((0.5d0)*boxzsize)).or.
 c     &       (zmedi.lt.((-0.5d0)*boxzsize))) then
 c        go to 196
 c        endif
-          xmedi=mod(xmedi,boxxsize)
-          if (xmedi.lt.0) xmedi=xmedi+boxxsize
-          ymedi=mod(ymedi,boxysize)
-          if (ymedi.lt.0) ymedi=ymedi+boxysize
-          zmedi=mod(zmedi,boxzsize)
-          if (zmedi.lt.0) zmedi=zmedi+boxzsize
-
+        call to_box(xmedi,ymedi,zmedi)
 #ifdef FOURBODY
         num_conti=num_cont_hb(i)
 #endif
@@ -3895,12 +3653,7 @@ c     &  .or. itype(i-1).eq.ntyp1
         xmedi=c(1,i)+0.5d0*dxi
         ymedi=c(2,i)+0.5d0*dyi
         zmedi=c(3,i)+0.5d0*dzi
-          xmedi=mod(xmedi,boxxsize)
-          if (xmedi.lt.0) xmedi=xmedi+boxxsize
-          ymedi=mod(ymedi,boxysize)
-          if (ymedi.lt.0) ymedi=ymedi+boxysize
-          zmedi=mod(zmedi,boxzsize)
-          if (zmedi.lt.0) zmedi=zmedi+boxzsize
+        call to_box(xmedi,ymedi,zmedi)
 C          xmedi=xmedi+xshift*boxxsize
 C          ymedi=ymedi+yshift*boxysize
 C          zmedi=zmedi+zshift*boxzsize
@@ -3940,7 +3693,7 @@ c        do j=ielstart(i),ielend(i)
 C          do j=16,17
 C          write (iout,*) i,j
 C         if (j.le.1) cycle
-          if (itype(j).eq.ntyp1.or. itype(j+1).eq.ntyp1
+        if (itype(j).eq.ntyp1.or. itype(j+1).eq.ntyp1
 C changes suggested by Ana to avoid out of bounds
 c     & .or.((j+2).gt.nres)
 c     & .or.((j-1).le.0)
@@ -3948,7 +3701,7 @@ C end of changes by Ana
 c     & .or.itype(j+2).eq.ntyp1
 c     & .or.itype(j-1).eq.ntyp1
      &) cycle
-          call eelecij(i,j,ees,evdw1,eel_loc)
+        call eelecij(i,j,ees,evdw1,eel_loc)
 c        enddo ! j
 #ifdef FOURBODY
         num_cont_hb(i)=num_conti
@@ -4017,10 +3770,9 @@ C-------------------------------------------------------------------------------
      &  ecosa1,ecosb1,ecosg1,ecosa2,ecosb2,ecosg2,ecosap,ecosbp,
      &  ecosgp,ecosam,ecosbm,ecosgm,ghalf,rlocshield
       double precision a22,a23,a32,a33,geel_loc_ij,geel_loc_ji
-      double precision dist_init,xj_safe,yj_safe,zj_safe,
-     &  xj_temp,yj_temp,zj_temp,dist_temp,xmedi,ymedi,zmedi
+      double precision xmedi,ymedi,zmedi
       double precision sscale,sscagrad,scalar
-
+      double precision boxshift
 c 4/26/02 - AL scaling factor for 1,4 repulsive VDW interactions
 #ifdef MOMENT
       double precision scal_el /1.0d0/
@@ -4032,7 +3784,6 @@ C 13-go grudnia roku pamietnego...
       double precision unmat(3,3) /1.0d0,0.0d0,0.0d0,
      &                   0.0d0,1.0d0,0.0d0,
      &                   0.0d0,0.0d0,1.0d0/
-       integer xshift,yshift,zshift
 c          time00=MPI_Wtime()
 cd      write (iout,*) "eelecij",i,j
 c          ind=ind+1
@@ -4055,73 +3806,12 @@ C          zj=c(3,j)+0.5D0*dzj-zmedi
           xj=c(1,j)+0.5D0*dxj
           yj=c(2,j)+0.5D0*dyj
           zj=c(3,j)+0.5D0*dzj
-          xj=mod(xj,boxxsize)
-          if (xj.lt.0) xj=xj+boxxsize
-          yj=mod(yj,boxysize)
-          if (yj.lt.0) yj=yj+boxysize
-          zj=mod(zj,boxzsize)
-          if (zj.lt.0) zj=zj+boxzsize
-          if ((zj.lt.0).or.(xj.lt.0).or.(yj.lt.0)) write (*,*) "CHUJ"
-      dist_init=(xj-xmedi)**2+(yj-ymedi)**2+(zj-zmedi)**2
-      xj_safe=xj
-      yj_safe=yj
-      zj_safe=zj
-      isubchap=0
-      do xshift=-1,1
-      do yshift=-1,1
-      do zshift=-1,1
-          xj=xj_safe+xshift*boxxsize
-          yj=yj_safe+yshift*boxysize
-          zj=zj_safe+zshift*boxzsize
-          dist_temp=(xj-xmedi)**2+(yj-ymedi)**2+(zj-zmedi)**2
-          if(dist_temp.lt.dist_init) then
-            dist_init=dist_temp
-            xj_temp=xj
-            yj_temp=yj
-            zj_temp=zj
-            isubchap=1
-          endif
-       enddo
-       enddo
-       enddo
-       if (isubchap.eq.1) then
-          xj=xj_temp-xmedi
-          yj=yj_temp-ymedi
-          zj=zj_temp-zmedi
-       else
-          xj=xj_safe-xmedi
-          yj=yj_safe-ymedi
-          zj=zj_safe-zmedi
-       endif
+          call to_box(xj,yj,zj)
+          xj=boxshift(xj-xmedi,boxxsize)
+          yj=boxshift(yj-ymedi,boxysize)
+          zj=boxshift(zj-zmedi,boxzsize)
 C        if ((i+3).lt.j) then !this condition keeps for turn3 and turn4 not subject to PBC
 c  174   continue
-c        if (xj.gt.((0.5d0)*boxxsize)) xj=xj-boxxsize
-c        if (xj.lt.((-0.5d0)*boxxsize)) xj=xj+boxxsize
-C Condition for being inside the proper box
-c        if ((xj.gt.((0.5d0)*boxxsize)).or.
-c     &       (xj.lt.((-0.5d0)*boxxsize))) then
-c        go to 174
-c        endif
-c  175   continue
-c        if (yj.gt.((0.5d0)*boxysize)) yj=yj-boxysize
-c        if (yj.lt.((-0.5d0)*boxysize)) yj=yj+boxysize
-C Condition for being inside the proper box
-c        if ((yj.gt.((0.5d0)*boxysize)).or.
-c     &       (yj.lt.((-0.5d0)*boxysize))) then
-c        go to 175
-c        endif
-c  176   continue
-c        if (zj.gt.((0.5d0)*boxzsize)) zj=zj-boxzsize
-c        if (zj.lt.((-0.5d0)*boxzsize)) zj=zj+boxzsize
-C Condition for being inside the proper box
-c        if ((zj.gt.((0.5d0)*boxzsize)).or.
-c     &       (zj.lt.((-0.5d0)*boxzsize))) then
-c        go to 176
-c        endif
-C        endif !endPBC condintion
-C        xj=xj-xmedi
-C        yj=yj-ymedi
-C        zj=zj-zmedi
           rij=xj*xj+yj*yj+zj*zj
 
           sss=sscale(dsqrt(rij),r_cut_int)
@@ -5569,7 +5259,7 @@ C
       include 'COMMON.IOUNITS'
       include 'COMMON.CONTROL'
       dimension ggg(3)
-      integer xshift,yshift,zshift
+      double precision boxshift
       evdw2=0.0D0
       evdw2_14=0.0d0
       r0_scp=4.5d0
@@ -5612,12 +5302,7 @@ c        if ((zi.gt.((zshift+0.5d0)*boxzsize)).or.
 c     &       (zi.lt.((zshift-0.5d0)*boxzsize))) then
 c        go to 136
 c        endif
-          xi=mod(xi,boxxsize)
-          if (xi.lt.0) xi=xi+boxxsize
-          yi=mod(yi,boxysize)
-          if (yi.lt.0) yi=yi+boxysize
-          zi=mod(zi,boxzsize)
-          if (zi.lt.0) zi=zi+boxzsize
+          call to_box(xi,yi,zi)
 C          xi=xi+xshift*boxxsize
 C          yi=yi+yshift*boxysize
 C          zi=zi+zshift*boxzsize
@@ -5634,67 +5319,10 @@ C Uncomment following three lines for Ca-p interactions
           xj=c(1,j)
           yj=c(2,j)
           zj=c(3,j)
-c  174   continue
-c        if (xj.gt.((0.5d0)*boxxsize)) xj=xj-boxxsize
-c        if (xj.lt.((-0.5d0)*boxxsize)) xj=xj+boxxsize
-C Condition for being inside the proper box
-c        if ((xj.gt.((0.5d0)*boxxsize)).or.
-c     &       (xj.lt.((-0.5d0)*boxxsize))) then
-c        go to 174
-c        endif
-c  175   continue
-c        if (yj.gt.((0.5d0)*boxysize)) yj=yj-boxysize
-c        if (yj.lt.((-0.5d0)*boxysize)) yj=yj+boxysize
-cC Condition for being inside the proper box
-c        if ((yj.gt.((0.5d0)*boxysize)).or.
-c     &       (yj.lt.((-0.5d0)*boxysize))) then
-c        go to 175
-c        endif
-c  176   continue
-c        if (zj.gt.((0.5d0)*boxzsize)) zj=zj-boxzsize
-c        if (zj.lt.((-0.5d0)*boxzsize)) zj=zj+boxzsize
-C Condition for being inside the proper box
-c        if ((zj.gt.((0.5d0)*boxzsize)).or.
-c     &       (zj.lt.((-0.5d0)*boxzsize))) then
-c        go to 176
-          xj=mod(xj,boxxsize)
-          if (xj.lt.0) xj=xj+boxxsize
-          yj=mod(yj,boxysize)
-          if (yj.lt.0) yj=yj+boxysize
-          zj=mod(zj,boxzsize)
-          if (zj.lt.0) zj=zj+boxzsize
-      dist_init=(xj-xi)**2+(yj-yi)**2+(zj-zi)**2
-      xj_safe=xj
-      yj_safe=yj
-      zj_safe=zj
-      subchap=0
-      do xshift=-1,1
-      do yshift=-1,1
-      do zshift=-1,1
-          xj=xj_safe+xshift*boxxsize
-          yj=yj_safe+yshift*boxysize
-          zj=zj_safe+zshift*boxzsize
-          dist_temp=(xj-xi)**2+(yj-yi)**2+(zj-zi)**2
-          if(dist_temp.lt.dist_init) then
-            dist_init=dist_temp
-            xj_temp=xj
-            yj_temp=yj
-            zj_temp=zj
-            subchap=1
-          endif
-       enddo
-       enddo
-       enddo
-       if (subchap.eq.1) then
-          xj=xj_temp-xi
-          yj=yj_temp-yi
-          zj=zj_temp-zi
-       else
-          xj=xj_safe-xi
-          yj=yj_safe-yi
-          zj=zj_safe-zi
-       endif
-c c       endif
+          call to_box(xj,yj,zj)
+          xj=boxshift(xj-xi,boxxsize)
+          yj=boxshift(yj-yi,boxysize)
+          zj=boxshift(zj-zi,boxzsize)
 C          xj=xj-xi
 C          yj=yj-yi
 C          zj=zj-zi
@@ -5716,32 +5344,6 @@ C
           ggg(1)=xj*fac
           ggg(2)=yj*fac
           ggg(3)=zj*fac
-cgrad          if (j.lt.i) then
-cd          write (iout,*) 'j<i'
-C Uncomment following three lines for SC-p interactions
-c           do k=1,3
-c             gradx_scp(k,j)=gradx_scp(k,j)+ggg(k)
-c           enddo
-cgrad          else
-cd          write (iout,*) 'j>i'
-cgrad            do k=1,3
-cgrad              ggg(k)=-ggg(k)
-C Uncomment following line for SC-p interactions
-c             gradx_scp(k,j)=gradx_scp(k,j)-ggg(k)
-cgrad            enddo
-cgrad          endif
-cgrad          do k=1,3
-cgrad            gvdwc_scp(k,i)=gvdwc_scp(k,i)-0.5D0*ggg(k)
-cgrad          enddo
-cgrad          kstart=min0(i+1,j)
-cgrad          kend=max0(i-1,j-1)
-cd        write (iout,*) 'i=',i,' j=',j,' kstart=',kstart,' kend=',kend
-cd        write (iout,*) ggg(1),ggg(2),ggg(3)
-cgrad          do k=kstart,kend
-cgrad            do l=1,3
-cgrad              gvdwc_scp(l,k)=gvdwc_scp(l,k)-ggg(l)
-cgrad            enddo
-cgrad          enddo
           do k=1,3
             gvdwc_scpp(k,i)=gvdwc_scpp(k,i)-ggg(k)
             gvdwc_scp(k,j)=gvdwc_scp(k,j)+ggg(k)
       include 'COMMON.IOUNITS'
       include 'COMMON.CONTROL'
       include 'COMMON.SPLITELE'
-      integer xshift,yshift,zshift
       double precision ggg(3)
       integer i,iint,j,k,iteli,itypj,subchap,ikont
       double precision xi,yi,zi,xj,yj,zj,rrij,sss1,sssgrad1,
      & fac,e1,e2,rij
       double precision evdw2,evdw2_14,evdwij
-      double precision xj_safe,yj_safe,zj_safe,xj_temp,yj_temp,zj_temp,
-     & dist_temp, dist_init
       double precision sscale,sscagrad
+      double precision boxshift
       evdw2=0.0D0
       evdw2_14=0.0d0
 c        print *,boxxsize,boxysize,boxzsize,'wymiary pudla'
@@ -5801,43 +5401,7 @@ c      do i=iatscp_s,iatscp_e
         xi=0.5D0*(c(1,i)+c(1,i+1))
         yi=0.5D0*(c(2,i)+c(2,i+1))
         zi=0.5D0*(c(3,i)+c(3,i+1))
-          xi=mod(xi,boxxsize)
-          if (xi.lt.0) xi=xi+boxxsize
-          yi=mod(yi,boxysize)
-          if (yi.lt.0) yi=yi+boxysize
-          zi=mod(zi,boxzsize)
-          if (zi.lt.0) zi=zi+boxzsize
-c          xi=xi+xshift*boxxsize
-c          yi=yi+yshift*boxysize
-c          zi=zi+zshift*boxzsize
-c        print *,xi,yi,zi,'polozenie i'
-C Return atom into box, boxxsize is size of box in x dimension
-c  134   continue
-c        if (xi.gt.((xshift+0.5d0)*boxxsize)) xi=xi-boxxsize
-c        if (xi.lt.((xshift-0.5d0)*boxxsize)) xi=xi+boxxsize
-C Condition for being inside the proper box
-c        if ((xi.gt.((xshift+0.5d0)*boxxsize)).or.
-c     &       (xi.lt.((xshift-0.5d0)*boxxsize))) then
-c        go to 134
-c        endif
-c  135   continue
-c          print *,xi,boxxsize,"pierwszy"
-
-c        if (yi.gt.((yshift+0.5d0)*boxysize)) yi=yi-boxysize
-c        if (yi.lt.((yshift-0.5d0)*boxysize)) yi=yi+boxysize
-C Condition for being inside the proper box
-c        if ((yi.gt.((yshift+0.5d0)*boxysize)).or.
-c     &       (yi.lt.((yshift-0.5d0)*boxysize))) then
-c        go to 135
-c        endif
-c  136   continue
-c        if (zi.gt.((zshift+0.5d0)*boxzsize)) zi=zi-boxzsize
-c        if (zi.lt.((zshift-0.5d0)*boxzsize)) zi=zi+boxzsize
-C Condition for being inside the proper box
-c        if ((zi.gt.((zshift+0.5d0)*boxzsize)).or.
-c     &       (zi.lt.((zshift-0.5d0)*boxzsize))) then
-c        go to 136
-c        endif
+        call to_box(xi,yi,zi)
 c        do iint=1,nscp_gr(i)
 
 c        do j=iscpstart(i,iint),iscpend(i,iint)
@@ -5851,68 +5415,10 @@ C Uncomment following three lines for Ca-p interactions
           xj=c(1,j)
           yj=c(2,j)
           zj=c(3,j)
-          xj=mod(xj,boxxsize)
-          if (xj.lt.0) xj=xj+boxxsize
-          yj=mod(yj,boxysize)
-          if (yj.lt.0) yj=yj+boxysize
-          zj=mod(zj,boxzsize)
-          if (zj.lt.0) zj=zj+boxzsize
-c  174   continue
-c        if (xj.gt.((0.5d0)*boxxsize)) xj=xj-boxxsize
-c        if (xj.lt.((-0.5d0)*boxxsize)) xj=xj+boxxsize
-C Condition for being inside the proper box
-c        if ((xj.gt.((0.5d0)*boxxsize)).or.
-c     &       (xj.lt.((-0.5d0)*boxxsize))) then
-c        go to 174
-c        endif
-c  175   continue
-c        if (yj.gt.((0.5d0)*boxysize)) yj=yj-boxysize
-c        if (yj.lt.((-0.5d0)*boxysize)) yj=yj+boxysize
-cC Condition for being inside the proper box
-c        if ((yj.gt.((0.5d0)*boxysize)).or.
-c     &       (yj.lt.((-0.5d0)*boxysize))) then
-c        go to 175
-c        endif
-c  176   continue
-c        if (zj.gt.((0.5d0)*boxzsize)) zj=zj-boxzsize
-c        if (zj.lt.((-0.5d0)*boxzsize)) zj=zj+boxzsize
-C Condition for being inside the proper box
-c        if ((zj.gt.((0.5d0)*boxzsize)).or.
-c     &       (zj.lt.((-0.5d0)*boxzsize))) then
-c        go to 176
-c        endif
-CHERE IS THE CALCULATION WHICH MIRROR IMAGE IS THE CLOSEST ONE
-      dist_init=(xj-xi)**2+(yj-yi)**2+(zj-zi)**2
-      xj_safe=xj
-      yj_safe=yj
-      zj_safe=zj
-      subchap=0
-      do xshift=-1,1
-      do yshift=-1,1
-      do zshift=-1,1
-          xj=xj_safe+xshift*boxxsize
-          yj=yj_safe+yshift*boxysize
-          zj=zj_safe+zshift*boxzsize
-          dist_temp=(xj-xi)**2+(yj-yi)**2+(zj-zi)**2
-          if(dist_temp.lt.dist_init) then
-            dist_init=dist_temp
-            xj_temp=xj
-            yj_temp=yj
-            zj_temp=zj
-            subchap=1
-          endif
-       enddo
-       enddo
-       enddo
-       if (subchap.eq.1) then
-          xj=xj_temp-xi
-          yj=yj_temp-yi
-          zj=zj_temp-zi
-       else
-          xj=xj_safe-xi
-          yj=yj_safe-yi
-          zj=zj_safe-zi
-       endif
+          call to_box(xj,yj,zj)
+          xj=boxshift(xj-xi,boxxsize)
+          yj=boxshift(yj-yi,boxysize)
+          zj=boxshift(zj-zi,boxzsize)
 c          print *,xj,yj,zj,'polozenie j'
           rrij=1.0D0/(xj*xj+yj*yj+zj*zj)
 c          print *,rrij
@@ -13638,3 +13144,103 @@ C-----------------------------------------------------------------------
       endif
       return
       end
+c------------------------------------------------------------------------
+      double precision function boxshift(x,boxsize)
+      implicit none
+      double precision x,boxsize
+      double precision xtemp
+      xtemp=dmod(x,boxsize)
+      if (dabs(xtemp-boxsize).lt.dabs(xtemp)) then
+        boxshift=xtemp-boxsize
+      else if (dabs(xtemp+boxsize).lt.dabs(xtemp)) then
+        boxshift=xtemp+boxsize
+      else
+        boxshift=xtemp
+      endif
+      return
+      end
+c--------------------------------------------------------------------------
+      subroutine closest_img(xi,yi,zi,xj,yj,zj)
+      include 'DIMENSIONS'
+      include 'COMMON.CHAIN'
+      integer xshift,yshift,zshift,subchap
+      double precision dist_init,xj_safe,yj_safe,zj_safe,
+     & xj_temp,yj_temp,zj_temp,dist_temp
+      xj_safe=xj
+      yj_safe=yj
+      zj_safe=zj
+      dist_init=(xj-xi)**2+(yj-yi)**2+(zj-zi)**2
+      subchap=0
+      do xshift=-1,1
+        do yshift=-1,1
+          do zshift=-1,1
+            xj=xj_safe+xshift*boxxsize
+            yj=yj_safe+yshift*boxysize
+            zj=zj_safe+zshift*boxzsize
+            dist_temp=(xj-xi)**2+(yj-yi)**2+(zj-zi)**2
+            if(dist_temp.lt.dist_init) then
+              dist_init=dist_temp
+              xj_temp=xj
+              yj_temp=yj
+              zj_temp=zj
+              subchap=1
+            endif
+          enddo
+        enddo
+      enddo
+      if (subchap.eq.1) then
+        xj=xj_temp-xi
+        yj=yj_temp-yi
+        zj=zj_temp-zi
+      else
+        xj=xj_safe-xi
+        yj=yj_safe-yi
+        zj=zj_safe-zi
+      endif
+      return
+      end
+c--------------------------------------------------------------------------
+      subroutine to_box(xi,yi,zi)
+      implicit none
+      include 'DIMENSIONS'
+      include 'COMMON.CHAIN'
+      double precision xi,yi,zi
+      xi=dmod(xi,boxxsize)
+      if (xi.lt.0.0d0) xi=xi+boxxsize
+      yi=dmod(yi,boxysize)
+      if (yi.lt.0.0d0) yi=yi+boxysize
+      zi=dmod(zi,boxzsize)
+      if (zi.lt.0.0d0) zi=zi+boxzsize
+      return
+      end
+c--------------------------------------------------------------------------
+      subroutine lipid_layer(xi,yi,zi,sslipi,ssgradlipi)
+      implicit none
+      include 'DIMENSIONS'
+      include 'COMMON.CHAIN'
+      double precision xi,yi,zi,sslipi,ssgradlipi
+      double precision fracinbuf
+      double precision sscalelip,sscagradlip
+
+      if ((zi.gt.bordlipbot).and.(zi.lt.bordliptop)) then
+C the energy transfer exist
+        if (zi.lt.buflipbot) then
+C what fraction I am in
+          fracinbuf=1.0d0-((zi-bordlipbot)/lipbufthick)
+C lipbufthick is thickenes of lipid buffore
+          sslipi=sscalelip(fracinbuf)
+          ssgradlipi=-sscagradlip(fracinbuf)/lipbufthick
+        elseif (zi.gt.bufliptop) then
+          fracinbuf=1.0d0-((bordliptop-zi)/lipbufthick)
+          sslipi=sscalelip(fracinbuf)
+          ssgradlipi=sscagradlip(fracinbuf)/lipbufthick
+        else
+          sslipi=1.0d0
+          ssgradlipi=0.0
+        endif
+      else
+        sslipi=0.0d0
+        ssgradlipi=0.0
+      endif
+      return
+      end