Merge branch 'AFM' into multichain
[unres.git] / source / unres / src_MD-M / energy_p_new_barrier.F
index 439fb81..3192819 100644 (file)
@@ -99,6 +99,7 @@ c      endif
 C 
 C Compute the side-chain and electrostatic interaction energy
 C
+C      print *,ipot
       goto (101,102,103,104,105,106) ipot
 C Lennard-Jones potential.
   101 call elj(evdw)
@@ -112,6 +113,7 @@ C Berne-Pechukas potential (dilated LJ, angular dependence).
       goto 107
 C Gay-Berne potential (shifted LJ, angular dependence).
   104 call egb(evdw)
+C      print *,"bylem w egb"
       goto 107
 C Gay-Berne-Vorobjev potential (shifted LJ, angular dependence).
   105 call egbv(evdw)
@@ -199,6 +201,7 @@ c      print *,"Processor",myrank," computed UB"
 C
 C Calculate the SC local energy.
 C
+C      print *,"TU DOCHODZE?"
       call esc(escloc)
 c      print *,"Processor",myrank," computed USC"
 C
@@ -229,6 +232,7 @@ C
       else
         esccor=0.0d0
       endif
+C      print *,"PRZED MULIt"
 c      print *,"Processor",myrank," computed Usccorr"
 C 
 C 12/1/95 Multi-body terms
@@ -261,6 +265,19 @@ C  after the equilibration time
          Uconst=0.0d0
          Uconst_back=0.0d0
       endif
+C 01/27/2015 added by adasko
+C the energy component below is energy transfer into lipid environment 
+C based on partition function
+C      print *,"przed lipidami"
+      if (wliptran.gt.0) then
+        call Eliptransfer(eliptran)
+      endif
+C      print *,"za lipidami"
+      if (AFMlog.gt.0) then
+        call AFMforce(Eafmforce)
+      else if (selfguide.gt.0) then
+        call AFMvel(Eafmforce)
+      endif
 #ifdef TIMING
       time_enecalc=time_enecalc+MPI_Wtime()-time00
 #endif
@@ -302,6 +319,8 @@ C
       energia(17)=estr
       energia(20)=Uconst+Uconst_back
       energia(21)=esccor
+      energia(22)=eliptran
+      energia(23)=Eafmforce
 c    Here are the energies showed per procesor if the are more processors 
 c    per molecule then we sum it up in sum_energy subroutine 
 c      print *," Processor",myrank," calls SUM_ENERGY"
@@ -393,20 +412,23 @@ cMS$ATTRIBUTES C ::  proc_proc
       estr=energia(17)
       Uconst=energia(20)
       esccor=energia(21)
+      eliptran=energia(22)
+      Eafmforce=energia(23)
 #ifdef SPLITELE
       etot=wsc*evdw+wscp*evdw2+welec*ees+wvdwpp*evdw1
      & +wang*ebe+wtor*etors+wscloc*escloc
      & +wstrain*ehpb+wcorr*ecorr+wcorr5*ecorr5
      & +wcorr6*ecorr6+wturn4*eello_turn4+wturn3*eello_turn3
      & +wturn6*eturn6+wel_loc*eel_loc+edihcnstr+wtor_d*etors_d
-     & +wbond*estr+Uconst+wsccor*esccor
+     & +wbond*estr+Uconst+wsccor*esccor+wliptran*eliptran+Eafmforce
 #else
       etot=wsc*evdw+wscp*evdw2+welec*(ees+evdw1)
      & +wang*ebe+wtor*etors+wscloc*escloc
      & +wstrain*ehpb+wcorr*ecorr+wcorr5*ecorr5
      & +wcorr6*ecorr6+wturn4*eello_turn4+wturn3*eello_turn3
      & +wturn6*eturn6+wel_loc*eel_loc+edihcnstr+wtor_d*etors_d
-     & +wbond*estr+Uconst+wsccor*esccor
+     & +wbond*estr+Uconst+wsccor*esccor+wliptran*eliptran
+     & +Eafmforce
 #endif
       energia(0)=etot
 c detecting NaNQ
@@ -443,8 +465,9 @@ cMS$ATTRIBUTES C ::  proc_proc
 #ifdef MPI
       include 'mpif.h'
 #endif
-      double precision gradbufc(3,maxres),gradbufx(3,maxres),
-     &  glocbuf(4*maxres),gradbufc_sum(3,maxres),gloc_scbuf(3,maxres)
+      double precision gradbufc(3,-1:maxres),gradbufx(3,-1:maxres),
+     & glocbuf(4*maxres),gradbufc_sum(3,-1:maxres)
+     & ,gloc_scbuf(3,-1:maxres)
       include 'COMMON.SETUP'
       include 'COMMON.IOUNITS'
       include 'COMMON.FFIELD'
@@ -497,7 +520,7 @@ c      enddo
       call flush(iout)
 #endif
 #ifdef SPLITELE
-      do i=1,nct
+      do i=0,nct
         do j=1,3
           gradbufc(j,i)=wsc*gvdwc(j,i)+
      &                wscp*(gvdwc_scp(j,i)+gvdwc_scpp(j,i))+
@@ -508,10 +531,13 @@ c      enddo
      &                wcorr6*gradcorr6_long(j,i)+
      &                wturn6*gcorr6_turn_long(j,i)+
      &                wstrain*ghpbc(j,i)
+     &                +wliptran*gliptranc(j,i)
+     &                +gradafm(j,i)
+
         enddo
       enddo 
 #else
-      do i=1,nct
+      do i=0,nct
         do j=1,3
           gradbufc(j,i)=wsc*gvdwc(j,i)+
      &                wscp*(gvdwc_scp(j,i)+gvdwc_scpp(j,i))+
@@ -523,6 +549,9 @@ c      enddo
      &                wcorr6*gradcorr6_long(j,i)+
      &                wturn6*gcorr6_turn_long(j,i)+
      &                wstrain*ghpbc(j,i)
+     &                +wliptran*gliptranc(j,i)
+     &                +gradafm(j,i)
+
         enddo
       enddo 
 #endif
@@ -536,7 +565,7 @@ c      enddo
       enddo
       call flush(iout)
 #endif
-      do i=1,nres
+      do i=0,nres
         do j=1,3
           gradbufc_sum(j,i)=gradbufc(j,i)
         enddo
@@ -579,7 +608,7 @@ c      enddo
       do j=1,3
         gradbufc(j,nres-1)=gradbufc_sum(j,nres)
       enddo
-      do i=nres-2,nnt,-1
+      do i=nres-2,-1,-1
         do j=1,3
           gradbufc(j,i)=gradbufc(j,i+1)+gradbufc_sum(j,i+1)
         enddo
@@ -600,7 +629,7 @@ c      enddo
       enddo
       call flush(iout)
 #endif
-      do i=1,nres
+      do i=-1,nres
         do j=1,3
           gradbufc_sum(j,i)=gradbufc(j,i)
           gradbufc(j,i)=0.0d0
@@ -609,7 +638,7 @@ c      enddo
       do j=1,3
         gradbufc(j,nres-1)=gradbufc_sum(j,nres)
       enddo
-      do i=nres-2,nnt,-1
+      do i=nres-2,-1,-1
         do j=1,3
           gradbufc(j,i)=gradbufc(j,i+1)+gradbufc_sum(j,i+1)
         enddo
@@ -637,7 +666,7 @@ c      enddo
       do k=1,3
         gradbufc(k,nres)=0.0d0
       enddo
-      do i=1,nct
+      do i=-1,nct
         do j=1,3
 #ifdef SPLITELE
           gradc(j,i,icg)=gradbufc(j,i)+welec*gelc(j,i)+
@@ -658,6 +687,8 @@ c      enddo
      &                wturn6*gcorr6_turn(j,i)+
      &                wsccor*gsccorc(j,i)
      &               +wscloc*gscloc(j,i)
+     &               +wliptran*gliptranc(j,i)
+     &                +gradafm(j,i)
 #else
           gradc(j,i,icg)=gradbufc(j,i)+welec*gelc(j,i)+
      &                wel_loc*gel_loc(j,i)+
@@ -677,12 +708,16 @@ c      enddo
      &                wturn6*gcorr6_turn(j,i)+
      &                wsccor*gsccorc(j,i)
      &               +wscloc*gscloc(j,i)
+     &               +wliptran*gliptranc(j,i)
+     &                +gradafm(j,i)
+
 #endif
           gradx(j,i,icg)=wsc*gvdwx(j,i)+wscp*gradx_scp(j,i)+
      &                  wbond*gradbx(j,i)+
      &                  wstrain*ghpbx(j,i)+wcorr*gradxorr(j,i)+
      &                  wsccor*gsccorx(j,i)
      &                 +wscloc*gsclocx(j,i)
+     &                 +wliptran*gliptranx(j,i)
         enddo
       enddo 
 #ifdef DEBUG
@@ -971,6 +1006,8 @@ C------------------------------------------------------------------------
       estr=energia(17)
       Uconst=energia(20)
       esccor=energia(21)
+      eliptran=energia(22)
+      Eafmforce=energia(23) 
 #ifdef SPLITELE
       write (iout,10) evdw,wsc,evdw2,wscp,ees,welec,evdw1,wvdwpp,
      &  estr,wbond,ebe,wang,
@@ -979,7 +1016,7 @@ C------------------------------------------------------------------------
      &  ecorr5,wcorr5,ecorr6,wcorr6,eel_loc,wel_loc,eello_turn3,wturn3,
      &  eello_turn4,wturn4,eello_turn6,wturn6,esccor,wsccor,
      &  edihcnstr,ebr*nss,
-     &  Uconst,etot
+     &  Uconst,eliptran,wliptran,Eafmforce,etot
    10 format (/'Virtual-chain energies:'//
      & 'EVDW=  ',1pE16.6,' WEIGHT=',1pD16.6,' (SC-SC)'/
      & 'EVDW2= ',1pE16.6,' WEIGHT=',1pD16.6,' (SC-p)'/
@@ -1003,7 +1040,10 @@ C------------------------------------------------------------------------
      & 'EDIHC= ',1pE16.6,' (dihedral angle constraints)'/
      & 'ESS=   ',1pE16.6,' (disulfide-bridge intrinsic energy)'/
      & 'UCONST= ',1pE16.6,' (Constraint energy)'/ 
+     & 'ELT=',1pE16.6, ' WEIGHT=',1pD16.6,' (Lipid transfer energy)'/
+     & 'EAFM=  ',1pE16.6,' (atomic-force microscopy)'/
      & 'ETOT=  ',1pE16.6,' (total)')
+
 #else
       write (iout,10) evdw,wsc,evdw2,wscp,ees,welec,
      &  estr,wbond,ebe,wang,
@@ -1011,7 +1051,7 @@ C------------------------------------------------------------------------
      &  ecorr,wcorr,
      &  ecorr5,wcorr5,ecorr6,wcorr6,eel_loc,wel_loc,eello_turn3,wturn3,
      &  eello_turn4,wturn4,eello_turn6,wturn6,esccor,wsccro,edihcnstr,
-     &  ebr*nss,Uconst,etot
+     &  ebr*nss,Uconst,eliptran,wliptran,Eafmforc,etot
    10 format (/'Virtual-chain energies:'//
      & 'EVDW=  ',1pE16.6,' WEIGHT=',1pD16.6,' (SC-SC)'/
      & 'EVDW2= ',1pE16.6,' WEIGHT=',1pD16.6,' (SC-p)'/
@@ -1034,6 +1074,8 @@ C------------------------------------------------------------------------
      & 'EDIHC= ',1pE16.6,' (dihedral angle constraints)'/
      & 'ESS=   ',1pE16.6,' (disulfide-bridge intrinsic energy)'/
      & 'UCONST=',1pE16.6,' (Constraint energy)'/ 
+     & 'ELT=',1pE16.6, ' WEIGHT=',1pD16.6,' (Lipid transfer energy)'/
+     & 'EAFM=  ',1pE16.6,' (atomic-force microscopy)'/
      & 'ETOT=  ',1pE16.6,' (total)')
 #endif
       return
@@ -1088,13 +1130,14 @@ C Change 12/1/95 to calculate four-body interactions
 c           write (iout,*)'i=',i,' j=',j,' itypi=',itypi,' itypj=',itypj
             eps0ij=eps(itypi,itypj)
             fac=rrij**expon2
-            e1=fac*fac*aa(itypi,itypj)
-            e2=fac*bb(itypi,itypj)
+C have you changed here?
+            e1=fac*fac*aa
+            e2=fac*bb
             evdwij=e1+e2
 cd          sigm=dabs(aa(itypi,itypj)/bb(itypi,itypj))**(1.0D0/6.0D0)
 cd          epsi=bb(itypi,itypj)**2/aa(itypi,itypj)
 cd          write (iout,'(2(a3,i3,2x),6(1pd12.4)/2(3(1pd12.4),5x)/)')
-cd   &        restyp(itypi),i,restyp(itypj),j,aa(itypi,itypj),
+cd   &        restyp(itypi),i,restyp(itypj),j,a(itypi,itypj),
 cd   &        bb(itypi,itypj),1.0D0/dsqrt(rrij),evdwij,epsi,sigm,
 cd   &        (c(k,i),k=1,3),(c(k,j),k=1,3)
             evdw=evdw+evdwij
@@ -1238,8 +1281,9 @@ C
             rij=1.0D0/r_inv_ij 
             r_shift_inv=1.0D0/(rij+r0(itypi,itypj)-sigma(itypi,itypj))
             fac=r_shift_inv**expon
-            e1=fac*fac*aa(itypi,itypj)
-            e2=fac*bb(itypi,itypj)
+C have you changed here?
+            e1=fac*fac*aa
+            e2=fac*bb
             evdwij=e_augm+e1+e2
 cd          sigm=dabs(aa(itypi,itypj)/bb(itypi,itypj))**(1.0D0/6.0D0)
 cd          epsi=bb(itypi,itypj)**2/aa(itypi,itypj)
@@ -1365,17 +1409,18 @@ C Calculate the angle-dependent terms of energy & contributions to derivatives.
             call sc_angular
 C Calculate whole angle-dependent part of epsilon and contributions
 C to its derivatives
+C have you changed here?
             fac=(rrij*sigsq)**expon2
-            e1=fac*fac*aa(itypi,itypj)
-            e2=fac*bb(itypi,itypj)
+            e1=fac*fac*aa
+            e2=fac*bb
             evdwij=eps1*eps2rt*eps3rt*(e1+e2)
             eps2der=evdwij*eps3rt
             eps3der=evdwij*eps2rt
             evdwij=evdwij*eps2rt*eps3rt
             evdw=evdw+evdwij
             if (lprn) then
-            sigm=dabs(aa(itypi,itypj)/bb(itypi,itypj))**(1.0D0/6.0D0)
-            epsi=bb(itypi,itypj)**2/aa(itypi,itypj)
+            sigm=dabs(aa/bb)**(1.0D0/6.0D0)
+            epsi=bb**2/aa
 cd            write (iout,'(2(a3,i3,2x),15(0pf7.3))')
 cd     &        restyp(itypi),i,restyp(itypj),j,
 cd     &        epsi,sigm,chi1,chi2,chip1,chip2,
@@ -1425,7 +1470,7 @@ C
       integer xshift,yshift,zshift
       evdw=0.0D0
 ccccc      energy_dec=.false.
-c     print *,'Entering EGB nnt=',nnt,' nct=',nct,' expon=',expon
+C      print *,'Entering EGB nnt=',nnt,' nct=',nct,' expon=',expon
       evdw=0.0D0
       lprn=.false.
 c     if (icall.eq.0) lprn=.false.
@@ -1473,6 +1518,35 @@ c        endif
           if (yi.lt.0) yi=yi+boxysize
           zi=mod(zi,boxzsize)
           if (zi.lt.0) zi=zi+boxzsize
+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
+
 C          xi=xi+xshift*boxxsize
 C          yi=yi+yshift*boxysize
 C          zi=zi+zshift*boxzsize
@@ -1557,6 +1631,36 @@ c        endif
           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
+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
@@ -1625,18 +1729,24 @@ cd     &        rij_shift,1.0D0/rij,sig,sig0ij,sigsq,1-dsqrt(sigsq)
 c---------------------------------------------------------------
             rij_shift=1.0D0/rij_shift 
             fac=rij_shift**expon
-            e1=fac*fac*aa(itypi,itypj)
-            e2=fac*bb(itypi,itypj)
+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
+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(itypi,itypj)/bb(itypi,itypj))**(1.0D0/6.0D0)
-            epsi=bb(itypi,itypj)**2/aa(itypi,itypj)
+            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,
@@ -1658,12 +1768,20 @@ c     &      evdwij,fac,sigma(itypi,itypj),expon
             fac=fac+evdwij/sss*sssgrad/sigma(itypi,itypj)*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
+C            gg_lipi(3)=0.0d0
+C            gg_lipj(3)=0.0d0
             gg(1)=xj*fac
             gg(2)=yj*fac
             gg(3)=zj*fac
 C Calculate angular part of the gradient.
             call sc_grad
-            endif    ! sss
+            endif
             ENDIF    ! dyn_ss            
           enddo      ! j
         enddo        ! iint
@@ -1707,6 +1825,41 @@ c     if (icall.eq.0) lprn=.true.
         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
+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
+
         dxi=dc_norm(1,nres+i)
         dyi=dc_norm(2,nres+i)
         dzi=dc_norm(3,nres+i)
@@ -1743,9 +1896,74 @@ 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
+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
+C      if (aa.ne.aa_aq(itypi,itypj)) write(63,'2e10.5') 
+C     &(aa-aa_aq(itypi,itypj)),(bb-bb_aq(itypi,itypj))
+      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)
@@ -1766,8 +1984,8 @@ C I hate to put IF's in the loops, but here don't have another choice!!!!
 c---------------------------------------------------------------
             rij_shift=1.0D0/rij_shift 
             fac=rij_shift**expon
-            e1=fac*fac*aa(itypi,itypj)
-            e2=fac*bb(itypi,itypj)
+            e1=fac*fac*aa
+            e2=fac*bb
             evdwij=eps1*eps2rt*eps3rt*(e1+e2)
             eps2der=evdwij*eps3rt
             eps3der=evdwij*eps2rt
@@ -1776,8 +1994,8 @@ c---------------------------------------------------------------
             evdwij=evdwij*eps2rt*eps3rt
             evdw=evdw+evdwij+e_augm
             if (lprn) then
-            sigm=dabs(aa(itypi,itypj)/bb(itypi,itypj))**(1.0D0/6.0D0)
-            epsi=bb(itypi,itypj)**2/aa(itypi,itypj)
+            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),
@@ -1791,6 +2009,7 @@ C Calculate gradient components.
             fac=-expon*(e1+evdwij)*rij_shift
             sigder=fac*sigder
             fac=rij*fac-2*expon*rrij*e_augm
+            fac=fac+evdwij/sss*sssgrad/sigma(itypi,itypj)*rij
 C Calculate the radial part of the gradient
             gg(1)=xj*fac
             gg(2)=yj*fac
@@ -1901,10 +2120,10 @@ c      write (iout,*) "eom1",eom1," eom2",eom2," eom12",eom12
       enddo 
 c      write (iout,*) "gg",(gg(k),k=1,3)
       do k=1,3
-        gvdwx(k,i)=gvdwx(k,i)-gg(k)
+        gvdwx(k,i)=gvdwx(k,i)-gg(k)+gg_lipi(k)
      &            +(eom12*(dc_norm(k,nres+j)-om12*dc_norm(k,nres+i))
      &            +eom1*(erij(k)-om1*dc_norm(k,nres+i)))*dsci_inv*sss
-        gvdwx(k,j)=gvdwx(k,j)+gg(k)
+        gvdwx(k,j)=gvdwx(k,j)+gg(k)+gg_lipj(k)
      &            +(eom12*(dc_norm(k,nres+i)-om12*dc_norm(k,nres+j))
      &            +eom2*(erij(k)-om2*dc_norm(k,nres+j)))*dscj_inv*sss
 c        write (iout,*)(eom12*(dc_norm(k,nres+j)-om12*dc_norm(k,nres+i))
@@ -1921,8 +2140,8 @@ cgrad          gvdwc(l,k)=gvdwc(l,k)+gg(l)
 cgrad        enddo
 cgrad      enddo
       do l=1,3
-        gvdwc(l,i)=gvdwc(l,i)-gg(l)
-        gvdwc(l,j)=gvdwc(l,j)+gg(l)
+        gvdwc(l,i)=gvdwc(l,i)-gg(l)+gg_lipi(l)
+        gvdwc(l,j)=gvdwc(l,j)+gg(l)+gg_lipj(l)
       enddo
       return
       end
@@ -3063,6 +3282,8 @@ C Loop over i,i+2 and i,i+3 pairs of the peptide groups
 C
 C 14/01/2014 TURN3,TUNR4 does no go under periodic boundry condition
       do i=iturn3_start,iturn3_end
+        if (i.le.1) cycle
+C        write(iout,*) "tu jest i",i
         if (itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1
 C changes suggested by Ana to avoid out of bounds
      & .or.((i+4).gt.nres)
@@ -3097,6 +3318,7 @@ C end of changes by Ana
         num_cont_hb(i)=num_conti
       enddo
       do i=iturn4_start,iturn4_end
+        if (i.le.1) cycle
         if (itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1
 C changes suggested by Ana to avoid out of bounds
      & .or.((i+5).gt.nres)
@@ -3164,6 +3386,7 @@ c
 c Loop over all pairs of interacting peptide groups except i,i+2 and i,i+3
 c
       do i=iatel_s,iatel_e
+        if (i.le.1) cycle
         if (itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1
 C changes suggested by Ana to avoid out of bounds
      & .or.((i+2).gt.nres)
@@ -3220,7 +3443,8 @@ c        endif
 c        write (iout,*) 'i',i,' ielstart',ielstart(i),' ielend',ielend(i)
         num_conti=num_cont_hb(i)
         do j=ielstart(i),ielend(i)
-c          write (iout,*) i,j,itype(i),itype(j)
+C          write (iout,*) i,j
+         if (j.le.1) cycle
           if (itype(j).eq.ntyp1.or. itype(j+1).eq.ntyp1
 C changes suggested by Ana to avoid out of bounds
      & .or.((j+2).gt.nres)
@@ -4907,8 +5131,8 @@ c        write (iout,*) "i",i," ii",ii," iii",iii," jj",jj," jjj",jjj,
 c     &    dhpb(i),dhpb1(i),forcon(i)
 C 24/11/03 AL: SS bridges handled separately because of introducing a specific
 C    distance and angle dependent SS bond potential.
-        if (ii.gt.nres .and. iabs(itype(iii)).eq.1 .and.
-     & iabs(itype(jjj)).eq.1) then
+C        if (ii.gt.nres .and. iabs(itype(iii)).eq.1 .and.
+C     & iabs(itype(jjj)).eq.1) then
 cmc        if (ii.gt.nres .and. itype(iii).eq.1 .and. itype(jjj).eq.1) then
 C 18/07/06 MC: Use the convention that the first nss pairs are SS bonds
         if (.not.dyn_ss .and. i.le.nss) then
@@ -4956,7 +5180,6 @@ cgrad        enddo
             ghpbc(k,iii)=ghpbc(k,iii)-ggg(k)
           enddo
         endif
-       endif
       enddo
       ehpb=0.5D0*ehpb
       return
@@ -9919,4 +10142,199 @@ crc      print *,((prod(i,j),i=1,2),j=1,2)
 
       return
       end
+CCC----------------------------------------------
+      subroutine Eliptransfer(eliptran)
+      implicit real*8 (a-h,o-z)
+      include 'DIMENSIONS'
+      include 'COMMON.GEO'
+      include 'COMMON.VAR'
+      include 'COMMON.LOCAL'
+      include 'COMMON.CHAIN'
+      include 'COMMON.DERIV'
+      include 'COMMON.NAMES'
+      include 'COMMON.INTERACT'
+      include 'COMMON.IOUNITS'
+      include 'COMMON.CALC'
+      include 'COMMON.CONTROL'
+      include 'COMMON.SPLITELE'
+      include 'COMMON.SBRIDGE'
+C this is done by Adasko
+C      print *,"wchodze"
+C structure of box:
+C      water
+C--bordliptop-- buffore starts
+C--bufliptop--- here true lipid starts
+C      lipid
+C--buflipbot--- lipid ends buffore starts
+C--bordlipbot--buffore ends
+      eliptran=0.0
+      do i=ilip_start,ilip_end
+C       do i=1,1
+        if (itype(i).eq.ntyp1) cycle
+
+        positi=(mod(((c(3,i)+c(3,i+1))/2.0d0),boxzsize))
+        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 ((positi.gt.bordlipbot)
+     &.and.(positi.lt.bordliptop)) then
+C the energy transfer exist
+        if (positi.lt.buflipbot) then
+C what fraction I am in
+         fracinbuf=1.0d0-
+     &        ((positi-bordlipbot)/lipbufthick)
+C lipbufthick is thickenes of lipid buffore
+         sslip=sscalelip(fracinbuf)
+         ssgradlip=-sscagradlip(fracinbuf)/lipbufthick
+         eliptran=eliptran+sslip*pepliptran
+         gliptranc(3,i)=gliptranc(3,i)+ssgradlip*pepliptran/2.0d0
+         gliptranc(3,i-1)=gliptranc(3,i-1)+ssgradlip*pepliptran/2.0d0
+C         gliptranc(3,i-2)=gliptranc(3,i)+ssgradlip*pepliptran
+
+C        print *,"doing sccale for lower part"
+C         print *,i,sslip,fracinbuf,ssgradlip
+        elseif (positi.gt.bufliptop) then
+         fracinbuf=1.0d0-((bordliptop-positi)/lipbufthick)
+         sslip=sscalelip(fracinbuf)
+         ssgradlip=sscagradlip(fracinbuf)/lipbufthick
+         eliptran=eliptran+sslip*pepliptran
+         gliptranc(3,i)=gliptranc(3,i)+ssgradlip*pepliptran/2.0d0
+         gliptranc(3,i-1)=gliptranc(3,i-1)+ssgradlip*pepliptran/2.0d0
+C         gliptranc(3,i-2)=gliptranc(3,i)+ssgradlip*pepliptran
+C          print *, "doing sscalefor top part"
+C         print *,i,sslip,fracinbuf,ssgradlip
+        else
+         eliptran=eliptran+pepliptran
+C         print *,"I am in true lipid"
+        endif
+C       else
+C       eliptran=elpitran+0.0 ! I am in water
+       endif
+       enddo
+C       print *, "nic nie bylo w lipidzie?"
+C now multiply all by the peptide group transfer factor
+C       eliptran=eliptran*pepliptran
+C now the same for side chains
+CV       do i=1,1
+       do i=ilip_start,ilip_end
+        if (itype(i).eq.ntyp1) cycle
+        positi=(mod(c(3,i+nres),boxzsize))
+        if (positi.le.0) positi=positi+boxzsize
+C       print *,mod(c(3,i+nres),boxzsize),bordlipbot,bordliptop
+c for each residue check if it is in lipid or lipid water border area
+C       respos=mod(c(3,i+nres),boxzsize)
+C       print *,positi,bordlipbot,buflipbot
+       if ((positi.gt.bordlipbot)
+     & .and.(positi.lt.bordliptop)) then
+C the energy transfer exist
+        if (positi.lt.buflipbot) then
+         fracinbuf=1.0d0-
+     &     ((positi-bordlipbot)/lipbufthick)
+C lipbufthick is thickenes of lipid buffore
+         sslip=sscalelip(fracinbuf)
+         ssgradlip=-sscagradlip(fracinbuf)/lipbufthick
+         eliptran=eliptran+sslip*liptranene(itype(i))
+         gliptranx(3,i)=gliptranx(3,i)
+     &+ssgradlip*liptranene(itype(i))
+         gliptranc(3,i-1)= gliptranc(3,i-1)
+     &+ssgradlip*liptranene(itype(i))
+C         print *,"doing sccale for lower part"
+        elseif (positi.gt.bufliptop) then
+         fracinbuf=1.0d0-
+     &((bordliptop-positi)/lipbufthick)
+         sslip=sscalelip(fracinbuf)
+         ssgradlip=sscagradlip(fracinbuf)/lipbufthick
+         eliptran=eliptran+sslip*liptranene(itype(i))
+         gliptranx(3,i)=gliptranx(3,i)
+     &+ssgradlip*liptranene(itype(i))
+         gliptranc(3,i-1)= gliptranc(3,i-1)
+     &+ssgradlip*liptranene(itype(i))
+C          print *, "doing sscalefor top part",sslip,fracinbuf
+        else
+         eliptran=eliptran+liptranene(itype(i))
+C         print *,"I am in true lipid"
+        endif
+        endif ! if in lipid or buffor
+C       else
+C       eliptran=elpitran+0.0 ! I am in water
+       enddo
+       return
+       end
+C---------------------------------------------------------
+C AFM soubroutine for constant force
+       subroutine AFMforce(Eafmforce)
+       implicit real*8 (a-h,o-z)
+      include 'DIMENSIONS'
+      include 'COMMON.GEO'
+      include 'COMMON.VAR'
+      include 'COMMON.LOCAL'
+      include 'COMMON.CHAIN'
+      include 'COMMON.DERIV'
+      include 'COMMON.NAMES'
+      include 'COMMON.INTERACT'
+      include 'COMMON.IOUNITS'
+      include 'COMMON.CALC'
+      include 'COMMON.CONTROL'
+      include 'COMMON.SPLITELE'
+      include 'COMMON.SBRIDGE'
+      real*8 diffafm(3)
+      dist=0.0d0
+      Eafmforce=0.0d0
+      do i=1,3
+      diffafm(i)=c(i,afmend)-c(i,afmbeg)
+      dist=dist+diffafm(i)**2
+      enddo
+      dist=dsqrt(dist)
+      Eafmforce=-forceAFMconst*(dist-distafminit)
+      do i=1,3
+      gradafm(i,afmend-1)=-forceAFMconst*diffafm(i)/dist
+      gradafm(i,afmbeg-1)=forceAFMconst*diffafm(i)/dist
+      enddo
+C      print *,'AFM',Eafmforce
+      return
+      end
+C---------------------------------------------------------
+C AFM subroutine with pseudoconstant velocity
+       subroutine AFMvel(Eafmforce)
+       implicit real*8 (a-h,o-z)
+      include 'DIMENSIONS'
+      include 'COMMON.GEO'
+      include 'COMMON.VAR'
+      include 'COMMON.LOCAL'
+      include 'COMMON.CHAIN'
+      include 'COMMON.DERIV'
+      include 'COMMON.NAMES'
+      include 'COMMON.INTERACT'
+      include 'COMMON.IOUNITS'
+      include 'COMMON.CALC'
+      include 'COMMON.CONTROL'
+      include 'COMMON.SPLITELE'
+      include 'COMMON.SBRIDGE'
+      real*8 diffafm(3)
+C Only for check grad COMMENT if not used for checkgrad
+C      totT=3.0d0
+C--------------------------------------------------------
+C      print *,"wchodze"
+      dist=0.0d0
+      Eafmforce=0.0d0
+      do i=1,3
+      diffafm(i)=c(i,afmend)-c(i,afmbeg)
+      dist=dist+diffafm(i)**2
+      enddo
+      dist=dsqrt(dist)
+      Eafmforce=0.5d0*forceAFMconst
+     & *(distafminit+totTafm*velAFMconst-dist)**2
+C      Eafmforce=-forceAFMconst*(dist-distafminit)
+      do i=1,3
+      gradafm(i,afmend-1)=-forceAFMconst*
+     &(distafminit+totTafm*velAFMconst-dist)
+     &*diffafm(i)/dist
+      gradafm(i,afmbeg-1)=forceAFMconst*
+     &(distafminit+totTafm*velAFMconst-dist)
+     &*diffafm(i)/dist
+      enddo
+C      print *,'AFM',Eafmforce,totTafm*velAFMconst,dist
+      return
+      end