debug off
[unres.git] / source / unres / src_MD-M / energy_p_new_barrier.F
index 4a47935..03bc765 100644 (file)
@@ -10,6 +10,8 @@ cMS$ATTRIBUTES C ::  proc_proc
 #ifdef MPI
       include "mpif.h"
       double precision weights_(n_ene)
+      integer IERR
+      integer status(MPI_STATUS_SIZE)
 #endif
       include 'COMMON.SETUP'
       include 'COMMON.IOUNITS'
@@ -25,6 +27,7 @@ cMS$ATTRIBUTES C ::  proc_proc
       include 'COMMON.CONTROL'
       include 'COMMON.TIME1'
       include 'COMMON.SPLITELE'
+      include 'COMMON.SHIELD'
 #ifdef MPI      
 c      print*,"ETOTAL Processor",fg_rank," absolute rank",myrank,
 c     & " nfgtasks",nfgtasks
@@ -55,6 +58,8 @@ C FG slaves as WEIGHTS array.
           weights_(17)=wbond
           weights_(18)=scal14
           weights_(21)=wsccor
+          weights_(22)=wtube
+
 C FG Master broadcasts the WEIGHTS_ array
           call MPI_Bcast(weights_(1),n_ene,
      &        MPI_DOUBLE_PRECISION,king,FG_COMM,IERROR)
@@ -81,6 +86,7 @@ C FG slaves receive the WEIGHTS array
           wbond=weights(17)
           scal14=weights(18)
           wsccor=weights(21)
+          wtube=weights(22)
         endif
         time_Bcast=time_Bcast+MPI_Wtime()-time00
         time_Bcastw=time_Bcastw+MPI_Wtime()-time00
@@ -146,6 +152,63 @@ C      write (iout,*) "shield_mode",shield_mode
        call set_shield_fac
       else if  (shield_mode.eq.2) then
        call set_shield_fac2
+      if (nfgtasks.gt.1) then
+C#define DEBUG
+#ifdef DEBUG
+       write(iout,*) "befor reduce fac_shield reduce"
+       do i=1,nres
+        write(2,*) "fac",itype(i),fac_shield(i),grad_shield(1,i)
+        write(2,*) "list", shield_list(1,i),ishield_list(i),
+     &  grad_shield_side(1,1,i),grad_shield_loc(1,1,i)
+       enddo
+#endif
+       call MPI_Allgatherv(fac_shield(ivec_start),ivec_count(fg_rank1),
+     &  MPI_DOUBLE_PRECISION,fac_shield(1),ivec_count(0),ivec_displ(0),
+     &  MPI_DOUBLE_PRECISION,FG_COMM,IERR)
+       call MPI_Allgatherv(shield_list(1,ivec_start),
+     &  ivec_count(fg_rank1),
+     &  MPI_I50,shield_list(1,1),ivec_count(0),
+     &  ivec_displ(0),
+     &  MPI_I50,FG_COMM,IERR)
+       call MPI_Allgatherv(ishield_list(ivec_start),
+     &  ivec_count(fg_rank1),
+     &  MPI_INTEGER,ishield_list(1),ivec_count(0),
+     &  ivec_displ(0),
+     &  MPI_INTEGER,FG_COMM,IERR)
+       call MPI_Allgatherv(grad_shield(1,ivec_start),
+     &  ivec_count(fg_rank1),
+     &  MPI_UYZ,grad_shield(1,1),ivec_count(0),
+     &  ivec_displ(0),
+     &  MPI_UYZ,FG_COMM,IERR)
+       call MPI_Allgatherv(grad_shield_side(1,1,ivec_start),
+     &  ivec_count(fg_rank1),
+     &  MPI_SHI,grad_shield_side(1,1,1),ivec_count(0),
+     &  ivec_displ(0),
+     &  MPI_SHI,FG_COMM,IERR)
+       call MPI_Allgatherv(grad_shield_loc(1,1,ivec_start),
+     &  ivec_count(fg_rank1),
+     &  MPI_SHI,grad_shield_loc(1,1,1),ivec_count(0),
+     &  ivec_displ(0),
+     &  MPI_SHI,FG_COMM,IERR)
+#ifdef DEBUG
+       write(iout,*) "after reduce fac_shield reduce"
+       do i=1,nres
+        write(2,*) "fac",itype(i),fac_shield(i),grad_shield(1,i)
+        write(2,*) "list", shield_list(1,i),ishield_list(i),
+     &  grad_shield_side(1,1,i),grad_shield_loc(1,1,i)
+       enddo
+#endif
+C#undef DEBUG
+      endif
+#ifdef DEBUG
+      do i=1,nres
+      write(iout,*) fac_shield(i),ishield_list(i),i,grad_shield(1,i)
+        do j=1,ishield_list(i)
+         write(iout,*) "grad", grad_shield_side(1,j,i),
+     &   grad_shield_loc(1,j,i)
+        enddo
+      enddo
+#endif
       endif
 c      print *,"Processor",myrank," left VEC_AND_DERIV"
       if (ipot.lt.6) then
@@ -297,6 +360,8 @@ C based on partition function
 C      print *,"przed lipidami"
       if (wliptran.gt.0) then
         call Eliptransfer(eliptran)
+      else
+       eliptran=0.0d0
       endif
 C      print *,"za lipidami"
       if (AFMlog.gt.0) then
@@ -304,6 +369,15 @@ C      print *,"za lipidami"
       else if (selfguide.gt.0) then
         call AFMvel(Eafmforce)
       endif
+      if (TUBElog.eq.1) then
+C      print *,"just before call"
+        call calctube(Etube)
+       elseif (TUBElog.eq.2) then
+        call calctube2(Etube)
+       else
+       Etube=0.0d0
+       endif
+
 #ifdef TIMING
       time_enecalc=time_enecalc+MPI_Wtime()-time00
 #endif
@@ -348,6 +422,7 @@ C
       energia(22)=eliptran
       energia(23)=Eafmforce
       energia(24)=ethetacnstr
+      energia(25)=Etube
 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"
@@ -442,6 +517,7 @@ cMS$ATTRIBUTES C ::  proc_proc
       eliptran=energia(22)
       Eafmforce=energia(23)
       ethetacnstr=energia(24)
+      Etube=energia(25)
 #ifdef SPLITELE
       etot=wsc*evdw+wscp*evdw2+welec*ees+wvdwpp*evdw1
      & +wang*ebe+wtor*etors+wscloc*escloc
@@ -449,7 +525,7 @@ cMS$ATTRIBUTES C ::  proc_proc
      & +wcorr6*ecorr6+wturn4*eello_turn4+wturn3*eello_turn3
      & +wturn6*eturn6+wel_loc*eel_loc+edihcnstr+wtor_d*etors_d
      & +wbond*estr+Uconst+wsccor*esccor+wliptran*eliptran+Eafmforce
-     & +ethetacnstr
+     & +ethetacnstr+wtube*Etube
 #else
       etot=wsc*evdw+wscp*evdw2+welec*(ees+evdw1)
      & +wang*ebe+wtor*etors+wscloc*escloc
@@ -458,7 +534,7 @@ cMS$ATTRIBUTES C ::  proc_proc
      & +wturn6*eturn6+wel_loc*eel_loc+edihcnstr+wtor_d*etors_d
      & +wbond*estr+Uconst+wsccor*esccor+wliptran*eliptran
      & +Eafmforce
-     & +ethetacnstr
+     & +ethetacnstr+wtube*Etube
 #endif
       energia(0)=etot
 c detecting NaNQ
@@ -568,6 +644,8 @@ c      enddo
      &                 +wturn3*gshieldc_t3(j,i)
      &                 +wturn4*gshieldc_t4(j,i)
      &                 +wel_loc*gshieldc_ll(j,i)
+     &                +wtube*gg_tube(j,i)
+
 
 
         enddo
@@ -591,6 +669,8 @@ c      enddo
      &                 +wcorr*gshieldc_ec(j,i)
      &                 +wturn4*gshieldc_t4(j,i)
      &                 +wel_loc*gshieldc_ll(j,i)
+     &                +wtube*gg_tube(j,i)
+
 
 
         enddo
@@ -747,11 +827,7 @@ C          print *,gradafm(1,13),"AFM"
      &                 +wturn4*gshieldc_loc_t4(j,i)
      &                 +wel_loc*gshieldc_ll(j,i)
      &                 +wel_loc*gshieldc_loc_ll(j,i)
-
-
-
-
-
+     &                +wtube*gg_tube(j,i)
 
 #else
           gradc(j,i,icg)=gradbufc(j,i)+welec*gelc(j,i)+
@@ -784,9 +860,7 @@ C          print *,gradafm(1,13),"AFM"
      &                 +wturn4*gshieldc_loc_t4(j,i)
      &                 +wel_loc*gshieldc_ll(j,i)
      &                 +wel_loc*gshieldc_loc_ll(j,i)
-
-
-
+     &                +wtube*gg_tube(j,i)
 
 
 #endif
@@ -801,6 +875,7 @@ C          print *,gradafm(1,13),"AFM"
      &                 +wturn3*gshieldx_t3(j,i)
      &                 +wturn4*gshieldx_t4(j,i)
      &                 +wel_loc*gshieldx_ll(j,i)
+     &                 +wtube*gg_tube_sc(j,i)
 
 
 
@@ -1101,6 +1176,7 @@ C------------------------------------------------------------------------
       eliptran=energia(22)
       Eafmforce=energia(23) 
       ethetacnstr=energia(24)
+      etube=energia(25)
 #ifdef SPLITELE
       write (iout,10) evdw,wsc,evdw2,wscp,ees,welec,evdw1,wvdwpp,
      &  estr,wbond,ebe,wang,
@@ -1109,6 +1185,7 @@ C------------------------------------------------------------------------
      &  ecorr5,wcorr5,ecorr6,wcorr6,eel_loc,wel_loc,eello_turn3,wturn3,
      &  eello_turn4,wturn4,eello_turn6,wturn6,esccor,wsccor,edihcnstr,
      &  ethetacnstr,ebr*nss,Uconst,eliptran,wliptran,Eafmforc,
+     &  etube,wtube,
      &  etot
    10 format (/'Virtual-chain energies:'//
      & 'EVDW=  ',1pE16.6,' WEIGHT=',1pD16.6,' (SC-SC)'/
@@ -1136,6 +1213,7 @@ C------------------------------------------------------------------------
      & 'UCONST= ',1pE16.6,' (Constraint energy)'/ 
      & 'ELT=',1pE16.6, ' WEIGHT=',1pD16.6,' (Lipid transfer energy)'/
      & 'EAFM=  ',1pE16.6,' (atomic-force microscopy)'/
+     & 'ETUBE=',1pE16.6, ' WEIGHT=',1pD16.6,' (cylindrical energy)'/
      & 'ETOT=  ',1pE16.6,' (total)')
 
 #else
@@ -1146,6 +1224,7 @@ C------------------------------------------------------------------------
      &  ecorr5,wcorr5,ecorr6,wcorr6,eel_loc,wel_loc,eello_turn3,wturn3,
      &  eello_turn4,wturn4,eello_turn6,wturn6,esccor,wsccro,edihcnstr,
      &  ethetacnstr,ebr*nss,Uconst,eliptran,wliptran,Eafmforc,
+     &  etube,wtube,
      &  etot
    10 format (/'Virtual-chain energies:'//
      & 'EVDW=  ',1pE16.6,' WEIGHT=',1pD16.6,' (SC-SC)'/
@@ -1172,6 +1251,7 @@ C------------------------------------------------------------------------
      & 'UCONST=',1pE16.6,' (Constraint energy)'/ 
      & 'ELT=',1pE16.6, ' WEIGHT=',1pD16.6,' (Lipid transfer energy)'/
      & 'EAFM=  ',1pE16.6,' (atomic-force microscopy)'/
+     & 'ETUBE=',1pE16.6, ' WEIGHT=',1pD16.6,' (cylindrical energy)'/
      & 'ETOT=  ',1pE16.6,' (total)')
 #endif
       return
@@ -4678,10 +4758,13 @@ C        else
 C        fac_shield(i)=0.4
 C        fac_shield(j)=0.6
         endif
+C         if (j.eq.78)
+C     &   write(iout,*) i,j,fac_shield(i),fac_shield(j)
         eello_turn3=eello_turn3+0.5d0*(pizda(1,1)+pizda(2,2))
      &  *fac_shield(i)*fac_shield(j)
         eello_t3=0.5d0*(pizda(1,1)+pizda(2,2))
      &  *fac_shield(i)*fac_shield(j)
+#ifdef NEWCORR
 C Derivatives in theta
         gloc(nphi+i,icg)=gloc(nphi+i,icg)
      &  +0.5d0*(gpizda1(1,1)+gpizda1(2,2))*wturn3
@@ -4689,7 +4772,7 @@ C Derivatives in theta
         gloc(nphi+i+1,icg)=gloc(nphi+i+1,icg)
      &  +0.5d0*(gpizda2(1,1)+gpizda2(2,2))*wturn3
      &   *fac_shield(i)*fac_shield(j)
-
+#endif
 
 C        if (energy_dec) write (iout,'(a6,2i5,0pf7.3)')
 C Derivatives in shield mode
@@ -11508,18 +11591,29 @@ C first for shielding is setting of function of side-chains
       include 'COMMON.IOUNITS'
       include 'COMMON.SHIELD'
       include 'COMMON.INTERACT'
+      include 'COMMON.LOCAL'
+
 C this is the squar root 77 devided by 81 the epislion in lipid (in protein)
       double precision div77_81/0.974996043d0/,
      &div4_81/0.2222222222d0/,sh_frac_dist_grad(3)
-
+  
 C the vector between center of side_chain and peptide group
        double precision pep_side(3),long,side_calf(3),
      &pept_group(3),costhet_grad(3),cosphi_grad_long(3),
      &cosphi_grad_loc(3),pep_side_norm(3),side_calf_norm(3)
+C      write(2,*) "ivec",ivec_start,ivec_end
+      do i=1,nres
+        fac_shield(i)=0.0d0
+        do j=1,3
+        grad_shield(j,i)=0.0d0
+        enddo
+      enddo
 C the line belowe needs to be changed for FGPROC>1
-      do i=1,nres-1
-      if ((itype(i).eq.ntyp1).and.itype(i+1).eq.ntyp1) cycle
+      do i=ivec_start,ivec_end
+C      do i=1,nres-1
+C      if ((itype(i).eq.ntyp1).and.itype(i+1).eq.ntyp1) cycle
       ishield_list(i)=0
+      if ((itype(i).eq.ntyp1).and.itype(i+1).eq.ntyp1) cycle
 Cif there two consequtive dummy atoms there is no peptide group between them
 C the line below has to be changed for FGPROC>1
       VolumeTotal=0.0
@@ -11552,6 +11646,7 @@ C now sscale fraction
 C       print *,buff_shield,"buff"
 C now sscale
         if (sh_frac_dist.le.0.0) cycle
+C        print *,ishield_list(i),i
 C If we reach here it means that this side chain reaches the shielding sphere
 C Lets add him to the list for gradient       
         ishield_list(i)=ishield_list(i)+1
@@ -11666,8 +11761,329 @@ C grad_shield_side is Cbeta sidechain gradient
       VolumeTotal=VolumeTotal+VofOverlap*scale_fac_dist
       enddo
       fac_shield(i)=VolumeTotal*wshield+(1.0d0-wshield)
-C      write(2,*) "TOTAL VOLUME",i,VolumeTotal,fac_shield(i)
+C      write(2,*) "TOTAL VOLUME",i,itype(i),fac_shield(i)
       enddo
       return
       end
+C-----------------------------------------------------------------------
+C-----------------------------------------------------------
+C This subroutine is to mimic the histone like structure but as well can be
+C utilizet to nanostructures (infinit) small modification has to be used to 
+C make it finite (z gradient at the ends has to be changes as well as the x,y
+C gradient has to be modified at the ends 
+C The energy function is Kihara potential 
+C E=4esp*((sigma/(r-r0))^12 - (sigma/(r-r0))^6)
+C 4eps is depth of well sigma is r_minimum r is distance from center of tube 
+C and r0 is the excluded size of nanotube (can be set to 0 if we want just a 
+C simple Kihara potential
+      subroutine calctube(Etube)
+       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'
+      double precision tub_r,vectube(3),enetube(maxres*2)
+      Etube=0.0d0
+      do i=1,2*nres
+        enetube(i)=0.0d0
+      enddo
+C first we calculate the distance from tube center
+C first sugare-phosphate group for NARES this would be peptide group 
+C for UNRES
+      do i=1,nres
+C lets ommit dummy atoms for now
+       if ((itype(i).eq.ntyp1).or.(itype(i+1).eq.ntyp1)) cycle
+C now calculate distance from center of tube and direction vectors
+      vectube(1)=mod((c(1,i)+c(1,i+1))/2.0d0,boxxsize)
+          if (vectube(1).lt.0) vectube(1)=vectube(1)+boxxsize
+      vectube(2)=mod((c(2,i)+c(2,i+1))/2.0d0,boxysize)
+          if (vectube(2).lt.0) vectube(2)=vectube(2)+boxysize
+      vectube(1)=vectube(1)-tubecenter(1)
+      vectube(2)=vectube(2)-tubecenter(2)
+
+C      print *,"x",(c(1,i)+c(1,i+1))/2.0d0,tubecenter(1)
+C      print *,"y",(c(2,i)+c(2,i+1))/2.0d0,tubecenter(2)
+
+C as the tube is infinity we do not calculate the Z-vector use of Z
+C as chosen axis
+      vectube(3)=0.0d0
+C now calculte the distance
+       tub_r=dsqrt(vectube(1)**2+vectube(2)**2+vectube(3)**2)
+C now normalize vector
+      vectube(1)=vectube(1)/tub_r
+      vectube(2)=vectube(2)/tub_r
+C calculte rdiffrence between r and r0
+      rdiff=tub_r-tubeR0
+C and its 6 power
+      rdiff6=rdiff**6.0d0
+C for vectorization reasons we will sumup at the end to avoid depenence of previous
+       enetube(i)=pep_aa_tube/rdiff6**2.0d0-pep_bb_tube/rdiff6
+C       write(iout,*) "TU13",i,rdiff6,enetube(i)
+C       print *,rdiff,rdiff6,pep_aa_tube
+C pep_aa_tube and pep_bb_tube are precomputed values A=4eps*sigma^12 B=4eps*sigma^6
+C now we calculate gradient
+       fac=(-12.0d0*pep_aa_tube/rdiff6+
+     &       6.0d0*pep_bb_tube)/rdiff6/rdiff
+C       write(iout,'(a5,i4,f12.1,3f12.5)') "TU13",i,rdiff6,enetube(i),
+C     &rdiff,fac
+
+C now direction of gg_tube vector
+        do j=1,3
+        gg_tube(j,i-1)=gg_tube(j,i-1)+vectube(j)*fac/2.0d0
+        gg_tube(j,i)=gg_tube(j,i)+vectube(j)*fac/2.0d0
+        enddo
+        enddo
+C basically thats all code now we split for side-chains (REMEMBER to sum up at the END)
+        do i=1,nres
+C Lets not jump over memory as we use many times iti
+         iti=itype(i)
+C lets ommit dummy atoms for now
+         if ((iti.eq.ntyp1)
+C in UNRES uncomment the line below as GLY has no side-chain...
+C      .or.(iti.eq.10)
+     &   ) cycle
+          vectube(1)=c(1,i+nres)
+          vectube(1)=mod(vectube(1),boxxsize)
+          if (vectube(1).lt.0) vectube(1)=vectube(1)+boxxsize
+          vectube(2)=c(2,i+nres)
+          vectube(2)=mod(vectube(2),boxysize)
+          if (vectube(2).lt.0) vectube(2)=vectube(2)+boxysize
+
+      vectube(1)=vectube(1)-tubecenter(1)
+      vectube(2)=vectube(2)-tubecenter(2)
+
+C as the tube is infinity we do not calculate the Z-vector use of Z
+C as chosen axis
+      vectube(3)=0.0d0
+C now calculte the distance
+       tub_r=dsqrt(vectube(1)**2+vectube(2)**2+vectube(3)**2)
+C now normalize vector
+      vectube(1)=vectube(1)/tub_r
+      vectube(2)=vectube(2)/tub_r
+C calculte rdiffrence between r and r0
+      rdiff=tub_r-tubeR0
+C and its 6 power
+      rdiff6=rdiff**6.0d0
+C for vectorization reasons we will sumup at the end to avoid depenence of previous
+       sc_aa_tube=sc_aa_tube_par(iti)
+       sc_bb_tube=sc_bb_tube_par(iti)
+       enetube(i+nres)=sc_aa_tube/rdiff6**2.0d0-sc_bb_tube/rdiff6
+C pep_aa_tube and pep_bb_tube are precomputed values A=4eps*sigma^12 B=4eps*sigma^6
+C now we calculate gradient
+       fac=-12.0d0*sc_aa_tube/rdiff6**2.0d0/rdiff+
+     &       6.0d0*sc_bb_tube/rdiff6/rdiff
+C now direction of gg_tube vector
+         do j=1,3
+          gg_tube_SC(j,i)=gg_tube_SC(j,i)+vectube(j)*fac
+          gg_tube(j,i-1)=gg_tube(j,i-1)+vectube(j)*fac
+         enddo
+        enddo
+        do i=1,2*nres
+          Etube=Etube+enetube(i)
+        enddo
+C        print *,"ETUBE", etube
+        return
+        end
+C TO DO 1) add to total energy
+C       2) add to gradient summation
+C       3) add reading parameters (AND of course oppening of PARAM file)
+C       4) add reading the center of tube
+C       5) add COMMONs
+C       6) add to zerograd
+
+C-----------------------------------------------------------------------
+C-----------------------------------------------------------
+C This subroutine is to mimic the histone like structure but as well can be
+C utilizet to nanostructures (infinit) small modification has to be used to 
+C make it finite (z gradient at the ends has to be changes as well as the x,y
+C gradient has to be modified at the ends 
+C The energy function is Kihara potential 
+C E=4esp*((sigma/(r-r0))^12 - (sigma/(r-r0))^6)
+C 4eps is depth of well sigma is r_minimum r is distance from center of tube 
+C and r0 is the excluded size of nanotube (can be set to 0 if we want just a 
+C simple Kihara potential
+      subroutine calctube2(Etube)
+       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'
+      double precision tub_r,vectube(3),enetube(maxres*2)
+      Etube=0.0d0
+      do i=1,2*nres
+        enetube(i)=0.0d0
+      enddo
+C first we calculate the distance from tube center
+C first sugare-phosphate group for NARES this would be peptide group 
+C for UNRES
+      do i=1,nres
+C lets ommit dummy atoms for now
+       
+       if ((itype(i).eq.ntyp1).or.(itype(i+1).eq.ntyp1)) cycle
+C now calculate distance from center of tube and direction vectors
+      vectube(1)=mod((c(1,i)+c(1,i+1))/2.0d0,boxxsize)
+          if (vectube(1).lt.0) vectube(1)=vectube(1)+boxxsize
+      vectube(2)=mod((c(2,i)+c(2,i+1))/2.0d0,boxysize)
+          if (vectube(2).lt.0) vectube(2)=vectube(2)+boxysize
+      vectube(1)=vectube(1)-tubecenter(1)
+      vectube(2)=vectube(2)-tubecenter(2)
+
+C      print *,"x",(c(1,i)+c(1,i+1))/2.0d0,tubecenter(1)
+C      print *,"y",(c(2,i)+c(2,i+1))/2.0d0,tubecenter(2)
+
+C as the tube is infinity we do not calculate the Z-vector use of Z
+C as chosen axis
+      vectube(3)=0.0d0
+C now calculte the distance
+       tub_r=dsqrt(vectube(1)**2+vectube(2)**2+vectube(3)**2)
+C now normalize vector
+      vectube(1)=vectube(1)/tub_r
+      vectube(2)=vectube(2)/tub_r
+C calculte rdiffrence between r and r0
+      rdiff=tub_r-tubeR0
+C and its 6 power
+      rdiff6=rdiff**6.0d0
+C for vectorization reasons we will sumup at the end to avoid depenence of previous
+       enetube(i)=pep_aa_tube/rdiff6**2.0d0-pep_bb_tube/rdiff6
+C       write(iout,*) "TU13",i,rdiff6,enetube(i)
+C       print *,rdiff,rdiff6,pep_aa_tube
+C pep_aa_tube and pep_bb_tube are precomputed values A=4eps*sigma^12 B=4eps*sigma^6
+C now we calculate gradient
+       fac=(-12.0d0*pep_aa_tube/rdiff6+
+     &       6.0d0*pep_bb_tube)/rdiff6/rdiff
+C       write(iout,'(a5,i4,f12.1,3f12.5)') "TU13",i,rdiff6,enetube(i),
+C     &rdiff,fac
+
+C now direction of gg_tube vector
+        do j=1,3
+        gg_tube(j,i-1)=gg_tube(j,i-1)+vectube(j)*fac/2.0d0
+        gg_tube(j,i)=gg_tube(j,i)+vectube(j)*fac/2.0d0
+        enddo
+        enddo
+C basically thats all code now we split for side-chains (REMEMBER to sum up at the END)
+        do i=1,nres
+C Lets not jump over memory as we use many times iti
+         iti=itype(i)
+C lets ommit dummy atoms for now
+         if ((iti.eq.ntyp1)
+C in UNRES uncomment the line below as GLY has no side-chain...
+     &      .or.(iti.eq.10)
+     &   ) cycle
+          vectube(1)=c(1,i+nres)
+          vectube(1)=mod(vectube(1),boxxsize)
+          if (vectube(1).lt.0) vectube(1)=vectube(1)+boxxsize
+          vectube(2)=c(2,i+nres)
+          vectube(2)=mod(vectube(2),boxysize)
+          if (vectube(2).lt.0) vectube(2)=vectube(2)+boxysize
+
+      vectube(1)=vectube(1)-tubecenter(1)
+      vectube(2)=vectube(2)-tubecenter(2)
+C THIS FRAGMENT MAKES TUBE FINITE
+        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)
+       print *,positi,bordtubebot,buftubebot,bordtubetop
+       if ((positi.gt.bordtubebot)
+     & .and.(positi.lt.bordtubetop)) then
+C the energy transfer exist
+        if (positi.lt.buftubebot) then
+         fracinbuf=1.0d0-
+     &     ((positi-bordtubebot)/tubebufthick)
+C lipbufthick is thickenes of lipid buffore
+         sstube=sscalelip(fracinbuf)
+         ssgradtube=-sscagradlip(fracinbuf)/tubebufthick
+         print *,ssgradtube, sstube,tubetranene(itype(i))
+         enetube(i+nres)=enetube(i+nres)+sstube*tubetranene(itype(i))
+C         gg_tube_SC(3,i)=gg_tube_SC(3,i)
+C     &+ssgradtube*tubetranene(itype(i))
+C         gg_tube(3,i-1)= gg_tube(3,i-1)
+C     &+ssgradtube*tubetranene(itype(i))
+C         print *,"doing sccale for lower part"
+        elseif (positi.gt.buftubetop) then
+         fracinbuf=1.0d0-
+     &((bordtubetop-positi)/tubebufthick)
+         sstube=sscalelip(fracinbuf)
+         ssgradtube=sscagradlip(fracinbuf)/tubebufthick
+         enetube(i+nres)=enetube(i+nres)+sstube*tubetranene(itype(i))
+C         gg_tube_SC(3,i)=gg_tube_SC(3,i)
+C     &+ssgradtube*tubetranene(itype(i))
+C         gg_tube(3,i-1)= gg_tube(3,i-1)
+C     &+ssgradtube*tubetranene(itype(i))
+C          print *, "doing sscalefor top part",sslip,fracinbuf
+        else
+         sstube=1.0d0
+         ssgradtube=0.0d0
+         enetube(i+nres)=enetube(i+nres)+sstube*tubetranene(itype(i))
+C         print *,"I am in true lipid"
+        endif
+        else
+C          sstube=0.0d0
+C          ssgradtube=0.0d0
+        cycle
+        endif ! if in lipid or buffor
+CEND OF FINITE FRAGMENT
+C as the tube is infinity we do not calculate the Z-vector use of Z
+C as chosen axis
+      vectube(3)=0.0d0
+C now calculte the distance
+       tub_r=dsqrt(vectube(1)**2+vectube(2)**2+vectube(3)**2)
+C now normalize vector
+      vectube(1)=vectube(1)/tub_r
+      vectube(2)=vectube(2)/tub_r
+C calculte rdiffrence between r and r0
+      rdiff=tub_r-tubeR0
+C and its 6 power
+      rdiff6=rdiff**6.0d0
+C for vectorization reasons we will sumup at the end to avoid depenence of previous
+       sc_aa_tube=sc_aa_tube_par(iti)
+       sc_bb_tube=sc_bb_tube_par(iti)
+       enetube(i+nres)=(sc_aa_tube/rdiff6**2.0d0-sc_bb_tube/rdiff6)
+     &                 *sstube+enetube(i+nres)
+C pep_aa_tube and pep_bb_tube are precomputed values A=4eps*sigma^12 B=4eps*sigma^6
+C now we calculate gradient
+       fac=(-12.0d0*sc_aa_tube/rdiff6**2.0d0/rdiff+
+     &       6.0d0*sc_bb_tube/rdiff6/rdiff)*sstube
+C now direction of gg_tube vector
+         do j=1,3
+          gg_tube_SC(j,i)=gg_tube_SC(j,i)+vectube(j)*fac
+          gg_tube(j,i-1)=gg_tube(j,i-1)+vectube(j)*fac
+         enddo
+         gg_tube_SC(3,i)=gg_tube_SC(3,i)
+     &+ssgradtube*enetube(i+nres)/sstube
+         gg_tube(3,i-1)= gg_tube(3,i-1)
+     &+ssgradtube*enetube(i+nres)/sstube
+
+        enddo
+        do i=1,2*nres
+          Etube=Etube+enetube(i)
+        enddo
+C        print *,"ETUBE", etube
+        return
+        end
+C TO DO 1) add to total energy
+C       2) add to gradient summation
+C       3) add reading parameters (AND of course oppening of PARAM file)
+C       4) add reading the center of tube
+C       5) add COMMONs
+C       6) add to zerograd