Merge branch 'devel' into AFM
authorAdam Sieradzan <adasko@piasek4.chem.univ.gda.pl>
Thu, 6 Aug 2015 08:39:41 +0000 (10:39 +0200)
committerAdam Sieradzan <adasko@piasek4.chem.univ.gda.pl>
Thu, 6 Aug 2015 08:39:41 +0000 (10:39 +0200)
Conflicts:
.gitignore
PARAM/pot_theta_G631_DIL.parm
bin/unres/MD/unres-mult-symetr_ifort_MPICH_E0LL2Y.exe
bin/unres/MD/unres_Tc_procor_oldparm_em64-D-symetr.exe
bin/unres/MD/unres_ifort_MPICH_GAB.exe
bin/xdrf2ang
bin/xdrf2pdb
bin/xdrf2pdb-m
source/cluster/wham/src-M/energy_p_new.F
source/maxlik/src_CSA/CMakeLists.txt
source/unres/src_CSA_DiL/CMakeLists.txt
source/unres/src_CSA_DiL/Makefile
source/unres/src_CSA_DiL/csa.F
source/unres/src_MD-M/COMMON.CONTROL
source/unres/src_MD-M/DIMENSIONS
source/unres/src_MD-M/Makefile
source/unres/src_MD-M/cinfo.f
source/unres/src_MD-M/energy_p_new_barrier.F
source/unres/src_MD-M/parmread.F
source/unres/src_MD-M/refsys.f
source/unres/src_MD/COMMON.TORSION
source/wham/src-M/DIMENSIONS
source/wham/src-M/energy_p_new.F
source/wham/src-M/readrtns.F
source/xdrfpdb/src/Makefile

29 files changed:
1  2 
.gitignore
bin/unres/MD/unres-mult-symetr_ifort_MPICH_E0LL2Y.exe
bin/unres/MD/unres_ifort_MPICH_GAB.exe
source/cluster/wham/src-M/energy_p_new.F
source/cluster/wham/src-M/parmread.F
source/cluster/wham/src-M/readrtns.F
source/unres/src_CSA/CMakeLists.txt
source/unres/src_MD-M/COMMON.CONTROL
source/unres/src_MD-M/DIMENSIONS
source/unres/src_MD-M/Makefile
source/unres/src_MD-M/checkder_p.F
source/unres/src_MD-M/energy_p_new_barrier.F
source/unres/src_MD-M/geomout.F
source/unres/src_MD-M/initialize_p.F
source/unres/src_MD-M/intcartderiv.F
source/unres/src_MD-M/parmread.F
source/unres/src_MD-M/readpdb.F
source/unres/src_MD-M/readrtns_CSA.F
source/unres/src_MD-M/refsys.f
source/unres/src_MD-M/ssMD.F
source/unres/src_MD-M/unres.F
source/unres/src_MD/initialize_p.F
source/unres/src_MD/parmread.F
source/unres/src_MD/ssMD.F
source/wham/src-M/enecalc1.F
source/wham/src-M/energy_p_new.F
source/wham/src-M/initialize_p.F
source/wham/src-M/parmread.F
source/wham/src-M/readrtns.F

diff --combined .gitignore
@@@ -12,15 -12,10 +12,11 @@@ cinfo.
  # ignore build dir
  build/
  build2/
- <<<<<<< HEAD
  build_prere/
  period_build/
  period_build2/
  build_*/
- =======
- build_*/
 +period_*/
- >>>>>>> master
  
  # latex files in documentation 
  doc/*/*.aux
@@@ -36,9 -31,3 +32,9 @@@ bin/unres/MD/unres_ifort_MPICH_GAB_czyt
  bin/unres/MD-M/unres_Tc_procor_newparm_em64-D-symetr.exe
  DIL/
  compinfo
 +period_build
 +build_devel
 +build_theta
 +build_new
 +build_prere/
 +period_build2
diff --combined bin/unres/MD/unres-mult-symetr_ifort_MPICH_E0LL2Y.exe
index a440770,3128072..0000000
deleted file mode 100755,100755
Binary files differ
diff --combined bin/unres/MD/unres_ifort_MPICH_GAB.exe
index 2f98745,c31777f..0000000
deleted file mode 100755,100755
Binary files differ
@@@ -67,7 -67,7 +67,7 @@@ cd    print *,'EHPB exitted succesfully
  C
  C Calculate the virtual-bond-angle energy.
  C
-       call ebend(ebe)
+       call ebend(ebe,ethetacnstr)
  cd    print *,'Bend energy finished.'
  C
  C Calculate the SC local energy.
@@@ -107,24 -107,23 +107,23 @@@ c      write (iout,*) "ft(6)",fact(6),
        etot=wsc*(evdw+fact(6)*evdw_t)+wscp*evdw2+welec*fact(1)*ees
       & +wvdwpp*evdw1
       & +wang*ebe+wtor*fact(1)*etors+wscloc*escloc
-      & +wstrain*ehpb+nss*ebr+wcorr*fact(3)*ecorr+wcorr5*fact(4)*ecorr5
+      & +wstrain*ehpb+wcorr*fact(3)*ecorr+wcorr5*fact(4)*ecorr5
       & +wcorr6*fact(5)*ecorr6+wturn4*fact(3)*eello_turn4
       & +wturn3*fact(2)*eello_turn3+wturn6*fact(5)*eturn6
       & +wel_loc*fact(2)*eel_loc+edihcnstr+wtor_d*fact(2)*etors_d
-      & +wbond*estr+wsccor*fact(1)*esccor
+      & +wbond*estr+wsccor*fact(1)*esccor+ethetacnstr
  #else
        etot=wsc*(evdw+fact(6)*evdw_t)+wscp*evdw2
       & +welec*fact(1)*(ees+evdw1)
       & +wang*ebe+wtor*fact(1)*etors+wscloc*escloc
-      & +wstrain*ehpb+nss*ebr+wcorr*fact(3)*ecorr+wcorr5*fact(4)*ecorr5
+      & +wstrain*ehpb+wcorr*fact(3)*ecorr+wcorr5*fact(4)*ecorr5
       & +wcorr6*fact(5)*ecorr6+wturn4*fact(3)*eello_turn4
       & +wturn3*fact(2)*eello_turn3+wturn6*fact(5)*eturn6
       & +wel_loc*fact(2)*eel_loc+edihcnstr+wtor_d*fact(2)*etors_d
-      & +wbond*estr+wsccor*fact(1)*esccor
+      & +wbond*estr+wsccor*fact(1)*esccor+ethetacnstr
  #endif
        energia(0)=etot
        energia(1)=evdw
- c      call enerprint(energia(0),frac)
  #ifdef SCP14
        energia(2)=evdw2-evdw2_14
        energia(17)=evdw2_14
        energia(19)=esccor
        energia(20)=edihcnstr
        energia(21)=evdw_t
+       energia(24)=ethetacnstr
  c detecting NaNQ
  #ifdef ISNAN
  #ifdef AIX
       &   +wturn3*fact(2)*gel_loc_turn3(i)
       &   +wturn6*fact(5)*gel_loc_turn6(i)
       &   +wel_loc*fact(2)*gel_loc_loc(i)
-      &   +wsccor*fact(1)*gsccor_loc(i)
+ c     &   +wsccor*fact(1)*gsccor_loc(i)
+ c ROZNICA Z WHAMem
        enddo
        endif
+       if (dyn_ss) call dyn_set_nss
        return
        end
  C------------------------------------------------------------------------
        esccor=energia(19)
        edihcnstr=energia(20)
        estr=energia(18)
+       ethetacnstr=energia(24)
  #ifdef SPLITELE
        write (iout,10) evdw,wsc,evdw2,wscp,ees,welec*fact(1),evdw1,
       &  wvdwpp,
       &  ecorr,wcorr*fact(3),ecorr5,wcorr5*fact(4),ecorr6,wcorr6*fact(5),
       &  eel_loc,wel_loc*fact(2),eello_turn3,wturn3*fact(2),
       &  eello_turn4,wturn4*fact(3),eello_turn6,wturn6*fact(5),
-      &  esccor,wsccor*fact(1),edihcnstr,ebr*nss,etot
+      &  esccor,wsccor*fact(1),edihcnstr,ethetacnstr,ebr*nss,etot
     10 format (/'Virtual-chain energies:'//
       & 'EVDW=  ',1pE16.6,' WEIGHT=',1pD16.6,' (SC-SC)'/
       & 'EVDW2= ',1pE16.6,' WEIGHT=',1pD16.6,' (SC-p)'/
       & 'ETURN6=',1pE16.6,' WEIGHT=',1pD16.6,' (turns, 6th order)'/
       & 'ESCCOR=',1pE16.6,' WEIGHT=',1pD16.6,' (backbone-rotamer corr)'/
       & 'EDIHC= ',1pE16.6,' (dihedral angle constraints)'/
+      & 'ETHETC= ',1pE16.6,' (valence angle constraints)'/
       & 'ESS=   ',1pE16.6,' (disulfide-bridge intrinsic energy)'/ 
       & 'ETOT=  ',1pE16.6,' (total)')
  #else
       &  ecorr6,wcorr6*fact(5),eel_loc,wel_loc*fact(2),
       &  eello_turn3,wturn3*fact(2),eello_turn4,wturn4*fact(3),
       &  eello_turn6,wturn6*fact(5),esccor*fact(1),wsccor,
-      &  edihcnstr,ebr*nss,etot
+      &  edihcnstr,ethetacnstr,ebr*nss,etot
     10 format (/'Virtual-chain energies:'//
       & 'EVDW=  ',1pE16.6,' WEIGHT=',1pD16.6,' (SC-SC)'/
       & 'EVDW2= ',1pE16.6,' WEIGHT=',1pD16.6,' (SC-p)'/
       & 'ETURN6=',1pE16.6,' WEIGHT=',1pD16.6,' (turns, 6th order)'/
       & 'ESCCOR=',1pE16.6,' WEIGHT=',1pD16.6,' (backbone-rotamer corr)'/
       & 'EDIHC= ',1pE16.6,' (dihedral angle constraints)'/
+      & 'ETHETC= ',1pE16.6,' (valence angle constraints)'/
       & 'ESS=   ',1pE16.6,' (disulfide-bridge intrinsic energy)'/ 
       & 'ETOT=  ',1pE16.6,' (total)')
  #endif
        integer icant
        external icant
  cd    print *,'Entering ELJ nnt=',nnt,' nct=',nct,' expon=',expon
+ c ROZNICA DODANE Z WHAM
+ c      do i=1,210
+ c        do j=1,2
+ c          eneps_temp(j,i)=0.0d0
+ c        enddo
+ c      enddo
+ cROZNICA
        evdw=0.0D0
        evdw_t=0.0d0
        do i=iatsc_s,iatsc_e
@@@ -393,6 -406,11 +406,11 @@@ c           write (iout,*)'i=',i,' j=',
              e2=fac*bb(itypi,itypj)
              evdwij=e1+e2
              ij=icant(itypi,itypj)
+ c ROZNICA z WHAM
+ c            eneps_temp(1,ij)=eneps_temp(1,ij)+e1/dabs(eps0ij)
+ c            eneps_temp(2,ij)=eneps_temp(2,ij)+e2/eps0ij
+ c
  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)/)')
        include 'COMMON.INTERACT'
        include 'COMMON.IOUNITS'
        include 'COMMON.CALC'
+       include 'COMMON.SBRIDGE'
        logical lprn
        common /srutu/icall
        integer icant
        external icant
 -      logical energy_dec /.true./
 +      integer xshift,yshift,zshift
++      logical energy_dec /.false./
  c     print *,'Entering EGB nnt=',nnt,' nct=',nct,' expon=',expon
        evdw=0.0D0
        evdw_t=0.0d0
@@@ -768,12 -787,6 +788,12 @@@ c      if (icall.gt.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
          dxi=dc_norm(1,nres+i)
          dyi=dc_norm(2,nres+i)
          dzi=dc_norm(3,nres+i)
@@@ -783,6 -796,38 +803,38 @@@ C Calculate SC interaction energy
  C
          do iint=1,nint_gr(i)
            do j=istart(i,iint),iend(i,iint)
+             IF (dyn_ss_mask(i).and.dyn_ss_mask(j)) THEN
+ c              write(iout,*) "PRZED ZWYKLE", evdwij
+               call dyn_ssbond_ene(i,j,evdwij)
+ c              write(iout,*) "PO ZWYKLE", evdwij
+               evdw=evdw+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)
+ C search over all next residues
+               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)
+ c               if(evdwij_przed_tri.ne.evdwij) then
+ c                 write (iout,*) "TRI:", evdwij, evdwij_przed_tri
+ 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',i,j,evdwij,'tss'
+               endif!dyn_ss_mask(k)
+              enddo! k
+             ELSE
              ind=ind+1
              itypj=iabs(itype(j))
              if (itypj.eq.ntyp1) cycle
@@@ -807,55 -852,15 +859,55 @@@ c           chip12=0.0D
  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)
 +          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
              dxj=dc_norm(1,nres+j)
              dyj=dc_norm(2,nres+j)
              dzj=dc_norm(3,nres+j)
  c            write (iout,*) i,j,xj,yj,zj
              rrij=1.0D0/(xj*xj+yj*yj+zj*zj)
              rij=dsqrt(rrij)
 +            sss=sscale((1.0d0/rij)/sigma(itypi,itypj))
 +            sssgrad=sscagrad((1.0d0/rij)/sigma(itypi,itypj))
 +            if (sss.le.0.0d0) cycle
  C Calculate angle-dependent terms of energy and contributions to their
  C derivatives.
              call sc_angular
@@@ -878,9 -883,9 +930,9 @@@ c--------------------------------------
              eps3der=evdwij*eps2rt
              evdwij=evdwij*eps2rt*eps3rt
              if (bb(itypi,itypj).gt.0) then
 -              evdw=evdw+evdwij
 +              evdw=evdw+evdwij*sss
              else
 -              evdw_t=evdw_t+evdwij
 +              evdw_t=evdw_t+evdwij*sss
              endif
              ij=icant(itypi,itypj)
              aux=eps1*eps2rt**2*eps3rt**2
@@@ -904,7 -909,6 +956,7 @@@ C Calculate gradient components
              fac=-expon*(e1+evdwij)*rij_shift
              sigder=fac*sigder
              fac=rij*fac
 +            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
  C Calculate angular part of the gradient.
              call sc_grad
              endif
+             ENDIF    ! dyn_ss            
            enddo      ! j
          enddo        ! iint
        enddo          ! i
@@@ -1854,15 -1859,7 +1907,15 @@@ cd      write (iout,*) 'iatel_s=',iatel
          gcorr_loc(i)=0.0d0
        enddo
        do i=iatel_s,iatel_e
 -        if (itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1) cycle
 +          if (i.eq.1) then
 +           if (itype(i).eq.ntyp1.or. itype(i+1).eq.ntyp1
 +     &  .or. itype(i+2).eq.ntyp1) cycle
 +          else
 +        if (itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1
 +     &  .or. itype(i+2).eq.ntyp1
 +     &  .or. itype(i-1).eq.ntyp1
 +     &) cycle
 +         endif
          if (itel(i).eq.0) goto 1215
          dxi=dc(1,i)
          dyi=dc(2,i)
          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
          num_conti=0
  c        write (iout,*) 'i',i,' ielstart',ielstart(i),' ielend',ielend(i)
          do j=ielstart(i),ielend(i)
 -          if (itype(j).eq.ntyp1 .or. itype(j+1).eq.ntyp1) cycle
 +          if (j.eq.1) then
 +           if (itype(j).eq.ntyp1 .or. itype(j+1).eq.ntyp1
 +     & .or.itype(j+2).eq.ntyp1
 +     &) cycle
 +          else
 +          if (itype(j).eq.ntyp1 .or. itype(j+1).eq.ntyp1
 +     & .or.itype(j+2).eq.ntyp1
 +     & .or.itype(j-1).eq.ntyp1
 +     &) cycle
 +         endif
            if (itel(j).eq.0) goto 1216
            ind=ind+1
            iteli=itel(i)
@@@ -1913,50 -1895,10 +1966,50 @@@ C End diagnostic
            dx_normj=dc_norm(1,j)
            dy_normj=dc_norm(2,j)
            dz_normj=dc_norm(3,j)
 -          xj=c(1,j)+0.5D0*dxj-xmedi
 -          yj=c(2,j)+0.5D0*dyj-ymedi
 -          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
 +      dist_init=(xj-xi)**2+(yj-yi)**2+(zj-zi)**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
 +
            rij=xj*xj+yj*yj+zj*zj
 +            sss=sscale(sqrt(rij))
 +            sssgrad=sscagrad(sqrt(rij))
            rrmij=1.0D0/rij
            rij=dsqrt(rij)
            rmij=1.0D0/rij
@@@ -1980,7 -1922,7 +2033,7 @@@ c          write (iout,*) "i",i,iteli,
  C 12/26/95 - for the evaluation of multi-body H-bonding interactions
            ees0ij=4.0D0+fac*fac-3.0D0*(cosb*cosb+cosg*cosg)
            ees=ees+eesij
 -          evdw1=evdw1+evdwij
 +          evdw1=evdw1+evdwij*sss
  cd          write(iout,'(2(2i3,2x),7(1pd12.4)/2(3(1pd12.4),5x)/)')
  cd     &      iteli,i,itelj,j,aaa,bbb,ael6i,ael3i,
  cd     &      1.0D0/dsqrt(rrmij),evdwij,eesij,
  C Calculate contributions to the Cartesian gradient.
  C
  #ifdef SPLITELE
 -          facvdw=-6*rrmij*(ev1+evdwij) 
 +          facvdw=-6*rrmij*(ev1+evdwij)*sss
            facel=-3*rrmij*(el1+eesij)
            fac1=fac
            erij(1)=xj*rmij
                gelc(l,k)=gelc(l,k)+ggg(l)
              enddo
            enddo
 -          ggg(1)=facvdw*xj
 -          ggg(2)=facvdw*yj
 -          ggg(3)=facvdw*zj
 +C          ggg(1)=facvdw*xj
 +C          ggg(2)=facvdw*yj
 +C          ggg(3)=facvdw*zj
 +          if (sss.gt.0.0) then
 +          ggg(1)=facvdw*xj+sssgrad*rmij*evdwij*xj
 +          ggg(2)=facvdw*yj+sssgrad*rmij*evdwij*yj
 +          ggg(3)=facvdw*zj+sssgrad*rmij*evdwij*zj
 +          else
 +          ggg(1)=0.0
 +          ggg(2)=0.0
 +          ggg(3)=0.0
 +          endif
            do k=1,3
              ghalf=0.5D0*ggg(k)
              gvdwpp(k,i)=gvdwpp(k,i)+ghalf
              enddo
            enddo
  #else
 -          facvdw=ev1+evdwij 
 +          facvdw=(ev1+evdwij)*sss
            facel=el1+eesij  
            fac1=fac
            fac=-3*rrmij*(facvdw+facvdw+facel)
@@@ -2885,13 -2818,6 +2938,13 @@@ c     &   " iscp",(iscpstart(i,j),iscpe
          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))
 +C    Returning the ith atom to box
 +          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
  
          do iint=1,nscp_gr(i)
  
@@@ -2903,72 -2829,26 +2956,72 @@@ c         xj=c(1,nres+j)-x
  c         yj=c(2,nres+j)-yi
  c         zj=c(3,nres+j)-zi
  C Uncomment following three lines for Ca-p interactions
 -          xj=c(1,j)-xi
 -          yj=c(2,j)-yi
 -          zj=c(3,j)-zi
 +          xj=c(1,j)
 +          yj=c(2,j)
 +          zj=c(3,j)
 +C returning the jth atom to box
 +          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
 +C Finding the closest jth atom
 +      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
 +
            rrij=1.0D0/(xj*xj+yj*yj+zj*zj)
 +C sss is scaling function for smoothing the cutoff gradient otherwise
 +C the gradient would not be continuouse
 +          sss=sscale(1.0d0/(dsqrt(rrij)))
 +          if (sss.le.0.0d0) cycle
 +          sssgrad=sscagrad(1.0d0/(dsqrt(rrij)))
            fac=rrij**expon2
            e1=fac*fac*aad(itypj,iteli)
            e2=fac*bad(itypj,iteli)
            if (iabs(j-i) .le. 2) then
              e1=scal14*e1
              e2=scal14*e2
 -            evdw2_14=evdw2_14+e1+e2
 +            evdw2_14=evdw2_14+(e1+e2)*sss
            endif
            evdwij=e1+e2
  c          write (iout,*) i,j,evdwij
 -          evdw2=evdw2+evdwij
 +          evdw2=evdw2+evdwij*sss
            if (calc_grad) then
  C
  C Calculate contributions to the gradient in the virtual-bond and SC vectors.
  C
 -          fac=-(evdwij+e1)*rrij
 +           fac=-(evdwij+e1)*rrij*sss
 +           fac=fac+(evdwij)*sssgrad*dsqrt(rrij)/expon
            ggg(1)=xj*fac
            ggg(2)=yj*fac
            ggg(3)=zj*fac
        include 'COMMON.DERIV'
        include 'COMMON.VAR'
        include 'COMMON.INTERACT'
+       include 'COMMON.CONTROL'
        dimension ggg(3)
        ehpb=0.0D0
  cd    print *,'edis: nhpb=',nhpb,' fbr=',fbr
@@@ -3053,11 -2934,42 +3107,42 @@@ C iii and jjj point to the residues fo
          endif
  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
+ C          call ssbond_ene(iii,jjj,eij)
+ C          ehpb=ehpb+2*eij
+ C        else
+        if (.not.dyn_ss .and. i.le.nss) then
+          if (ii.gt.nres .and. iabs(itype(iii)).eq.1 .and.
+      & iabs(itype(jjj)).eq.1) then
            call ssbond_ene(iii,jjj,eij)
            ehpb=ehpb+2*eij
-         else
+            endif !ii.gt.neres
+         else if (ii.gt.nres .and. jj.gt.nres) then
+ c Restraints from contact prediction
+           dd=dist(ii,jj)
+           if (constr_dist.eq.11) then
+ C            ehpb=ehpb+fordepth(i)**4.0d0
+ C     &          *rlornmr1(dd,dhpb(i),dhpb1(i),forcon(i))
+             ehpb=ehpb+fordepth(i)**4.0d0
+      &          *rlornmr1(dd,dhpb(i),dhpb1(i),forcon(i))
+             fac=fordepth(i)**4.0d0
+      &          *rlornmr1prim(dd,dhpb(i),dhpb1(i),forcon(i))/dd
+ C          write (iout,'(a6,2i5,3f8.3)') "edisl",ii,jj,
+ C     &    ehpb,fordepth(i),dd
+ C             print *,"TUTU"
+ C            write(iout,*) ehpb,"atu?"
+ C            ehpb,"tu?"
+ C            fac=fordepth(i)**4.0d0
+ C     &          *rlornmr1prim(dd,dhpb(i),dhpb1(i),forcon(i))/dd
+            else !constr_dist.eq.11
+           if (dhpb1(i).gt.0.0d0) then
+             ehpb=ehpb+2*forcon(i)*gnmr1(dd,dhpb(i),dhpb1(i))
+             fac=forcon(i)*gnmr1prim(dd,dhpb(i),dhpb1(i))/dd
+ c            write (iout,*) "beta nmr",
+ c     &        dd,2*forcon(i)*gnmr1(dd,dhpb(i),dhpb1(i))
+           else !dhpb(i).gt.0.00
  C Calculate the distance between the two points and its difference from the
  C target distance.
          dd=dist(ii,jj)
  C Evaluate gradient.
  C
          fac=waga*rdis/dd
+         endif !dhpb(i).gt.0
+         endif
  cd      print *,'i=',i,' ii=',ii,' jj=',jj,' dhpb=',dhpb(i),' dd=',dd,
  cd   &   ' waga=',waga,' fac=',fac
          do j=1,3
@@@ -3084,6 -2998,53 +3171,53 @@@ C Cartesian gradient in the SC vectors 
              ghpbx(j,jjj)=ghpbx(j,jjj)+ggg(j)
            enddo
          endif
+         else !ii.gt.nres
+ C          write(iout,*) "before"
+           dd=dist(ii,jj)
+ C          write(iout,*) "after",dd
+           if (constr_dist.eq.11) then
+             ehpb=ehpb+fordepth(i)**4.0d0
+      &          *rlornmr1(dd,dhpb(i),dhpb1(i),forcon(i))
+             fac=fordepth(i)**4.0d0
+      &          *rlornmr1prim(dd,dhpb(i),dhpb1(i),forcon(i))/dd
+ C            ehpb=ehpb+fordepth(i)**4*rlornmr1(dd,dhpb(i),dhpb1(i))
+ C            fac=fordepth(i)**4*rlornmr1prim(dd,dhpb(i),dhpb1(i))/dd
+ C            print *,ehpb,"tu?"
+ C            write(iout,*) ehpb,"btu?",
+ C     & dd,dhpb(i),dhpb1(i),fordepth(i),forcon(i)
+ C          write (iout,'(a6,2i5,3f8.3)') "edisl",ii,jj,
+ C     &    ehpb,fordepth(i),dd
+            else
+           if (dhpb1(i).gt.0.0d0) then
+             ehpb=ehpb+2*forcon(i)*gnmr1(dd,dhpb(i),dhpb1(i))
+             fac=forcon(i)*gnmr1prim(dd,dhpb(i),dhpb1(i))/dd
+ c            write (iout,*) "alph nmr",
+ c     &        dd,2*forcon(i)*gnmr1(dd,dhpb(i),dhpb1(i))
+           else
+             rdis=dd-dhpb(i)
+ C Get the force constant corresponding to this distance.
+             waga=forcon(i)
+ C Calculate the contribution to energy.
+             ehpb=ehpb+waga*rdis*rdis
+ c            write (iout,*) "alpha reg",dd,waga*rdis*rdis
+ C
+ C Evaluate gradient.
+ C
+             fac=waga*rdis/dd
+           endif
+           endif
+         do j=1,3
+           ggg(j)=fac*(c(j,jj)-c(j,ii))
+         enddo
+ cd      print '(i3,3(1pe14.5))',i,(ggg(j),j=1,3)
+ C If this is a SC-SC distance, we need to calculate the contributions to the
+ C Cartesian gradient in the SC vectors (ghpbx).
+         if (iii.lt.ii) then
+           do j=1,3
+             ghpbx(j,iii)=ghpbx(j,iii)-ggg(j)
+             ghpbx(j,jjj)=ghpbx(j,jjj)+ggg(j)
+           enddo
+         endif
          do j=iii,jjj-1
            do k=1,3
              ghpbc(k,j)=ghpbc(k,j)+ggg(k)
          enddo
          endif
        enddo
-       ehpb=0.5D0*ehpb
+       if (constr_dist.ne.11) ehpb=0.5D0*ehpb
        return
        end
  C--------------------------------------------------------------------------
        estr=0.0d0
        estr1=0.0d0
        do i=nnt+1,nct
 -        if (itype(i-1).eq.ntyp1 .or. itype(i).eq.ntyp1) then
 -          estr1=estr1+gnmr1(vbld(i),-1.0d0,distchainmax)
 -          do j=1,3
 -          gradb(j,i-1)=gnmr1prim(vbld(i),-1.0d0,distchainmax)
 -     &      *dc(j,i-1)/vbld(i)
 -          enddo
 -          if (energy_dec) write(iout,*)
 -     &       "estr1",i,gnmr1(vbld(i),-1.0d0,distchainmax)
 -        else
 +        if (itype(i-1).eq.ntyp1 .and. itype(i).eq.ntyp1) cycle
 +C          estr1=estr1+gnmr1(vbld(i),-1.0d0,distchainmax)
 +C          do j=1,3
 +C          gradb(j,i-1)=gnmr1prim(vbld(i),-1.0d0,distchainmax)
 +C     &      *dc(j,i-1)/vbld(i)
 +C          enddo
 +C          if (energy_dec) write(iout,*)
 +C     &       "estr1",i,vbld(i),distchainmax,
 +C     &       gnmr1(vbld(i),-1.0d0,distchainmax)
 +C        else
 +         if (itype(i-1).eq.ntyp1 .or. itype(i).eq.ntyp1) then
 +        diff = vbld(i)-vbldpDUM
 +         else
            diff = vbld(i)-vbldp0
  c          write (iout,*) i,vbld(i),vbldp0,diff,AKP*diff*diff
 +         endif
            estr=estr+diff*diff
            do j=1,3
              gradb(j,i-1)=AKP*diff*dc(j,i-1)/vbld(i)
            enddo
 -        endif
 -
 +C        endif
 +C        write (iout,'(a7,i5,4f7.3)')
 +C     &     "estr bb",i,vbld(i),vbldp0,diff,AKP*diff*diff
        enddo
        estr=0.5d0*AKP*estr+estr1
  c
@@@ -3279,7 -3234,7 +3413,7 @@@ c     &      AKSC(j,iti),abond0(j,iti),
        end
  #ifdef CRYST_THETA
  C--------------------------------------------------------------------------
-       subroutine ebend(etheta)
+       subroutine ebend(etheta,ethetacnstr)
  C
  C Evaluate the virtual-bond-angle energy given the virtual-bond dihedral
  C angles gamma and its derivatives in consecutive thetas and gammas.
        include 'COMMON.IOUNITS'
        include 'COMMON.NAMES'
        include 'COMMON.FFIELD'
+       include 'COMMON.TORCNSTR'
        common /calcthet/ term1,term2,termm,diffak,ratak,
       & ak,aktc,termpre,termexp,sigc,sig0i,time11,time12,sigcsq,
       & delthe0,sig0inv,sigtc,sigsqtc,delthec,it
        double precision y(2),z(2)
        delta=0.02d0*pi
-       time11=dexp(-2*time)
-       time12=1.0d0
+ c      time11=dexp(-2*time)
+ c      time12=1.0d0
        etheta=0.0D0
  c      write (iout,*) "nres",nres
  c     write (*,'(a,i2)') 'EBEND ICG=',icg
  c      write (iout,*) ithet_start,ithet_end
        do i=ithet_start,ithet_end
 -        if (itype(i-1).eq.ntyp1) cycle
 +        if (i.le.2) cycle
 +        if ((itype(i-1).eq.ntyp1).or.itype(i-2).eq.ntyp1
 +     &  .or.itype(i).eq.ntyp1) cycle
  C Zero the energy function and its derivative at 0 or pi.
          call splinthet(theta(i),0.5d0*delta,ss,ssd)
          it=itype(i-1)
            ichir21=isign(1,itype(i))
            ichir22=isign(1,itype(i))
           endif
 -        if (i.gt.3 .and. itype(i-2).ne.ntyp1) then
 +         if (i.eq.3) then
 +          y(1)=0.0D0
 +          y(2)=0.0D0
 +          else
 +        if (i.gt.3 .and. itype(i-3).ne.ntyp1) then
  #ifdef OSF
            phii=phi(i)
-           icrc=0
-           call proc_proc(phii,icrc)
+ c          icrc=0
+ c          call proc_proc(phii,icrc)
            if (icrc.eq.1) phii=150.0
  #else
            phii=phi(i)
            y(1)=0.0D0
            y(2)=0.0D0
          endif
 -        if (i.lt.nres .and. itype(i).ne.ntyp1) then
 +        endif
 +        if (i.lt.nres .and. itype(i+1).ne.ntyp1) then
  #ifdef OSF
            phii1=phi(i+1)
-           icrc=0
-           call proc_proc(phii1,icrc)
+ c          icrc=0
+ c          call proc_proc(phii1,icrc)
            if (icrc.eq.1) phii1=150.0
            phii1=pinorm(phii1)
            z(1)=cos(phii1)
@@@ -3418,9 -3367,37 +3553,37 @@@ c     &    rad2deg*phii,rad2deg*phii1,e
          if (i.gt.3) gloc(i-3,icg)=gloc(i-3,icg)+wang*E_tc*dthetg1
          if (i.lt.nres) gloc(i-2,icg)=gloc(i-2,icg)+wang*E_tc*dthetg2
          gloc(nphi+i-2,icg)=wang*(E_theta+E_tc*dthett)
-  1215   continue
+ c 1215   continue
        enddo
  C Ufff.... We've done all this!!! 
+ C now constrains
+       ethetacnstr=0.0d0
+ C      print *,ithetaconstr_start,ithetaconstr_end,"TU"
+       do i=1,ntheta_constr
+         itheta=itheta_constr(i)
+         thetiii=theta(itheta)
+         difi=pinorm(thetiii-theta_constr0(i))
+         if (difi.gt.theta_drange(i)) then
+           difi=difi-theta_drange(i)
+           ethetacnstr=ethetacnstr+0.25d0*for_thet_constr(i)*difi**4
+           gloc(itheta+nphi-2,icg)=gloc(itheta+nphi-2,icg)
+      &    +for_thet_constr(i)*difi**3
+         else if (difi.lt.-drange(i)) then
+           difi=difi+drange(i)
+           ethetacnstr=ethetacnstr+0.25d0*for_thet_constr(i)*difi**4
+           gloc(itheta+nphi-2,icg)=gloc(itheta+nphi-2,icg)
+      &    +for_thet_constr(i)*difi**3
+         else
+           difi=0.0
+         endif
+ C       if (energy_dec) then
+ C        write (iout,'(a6,2i5,4f8.3,2e14.5)') "ethetc",
+ C     &    i,itheta,rad2deg*thetiii,
+ C     &    rad2deg*theta_constr0(i),  rad2deg*theta_drange(i),
+ C     &    rad2deg*difi,0.25d0*for_thet_constr(i)*difi**4,
+ C     &    gloc(itheta+nphi-2,icg)
+ C        endif
+       enddo
        return
        end
  C---------------------------------------------------------------------------
@@@ -3533,7 -3510,7 +3696,7 @@@ C "Thank you" to MAPLE (probably spare
        end
  #else
  C--------------------------------------------------------------------------
-       subroutine ebend(etheta)
+       subroutine ebend(etheta,ethetacnstr)
  C
  C Evaluate the virtual-bond-angle energy given the virtual-bond dihedral
  C angles gamma and its derivatives in consecutive thetas and gammas.
        include 'COMMON.NAMES'
        include 'COMMON.FFIELD'
        include 'COMMON.CONTROL'
+       include 'COMMON.TORCNSTR'
        double precision coskt(mmaxtheterm),sinkt(mmaxtheterm),
       & cosph1(maxsingle),sinph1(maxsingle),cosph2(maxsingle),
       & sinph2(maxsingle),cosph1ph2(maxdouble,maxdouble),
        etheta=0.0D0
  c      write (iout,*) "ithetyp",(ithetyp(i),i=1,ntyp1)
        do i=ithet_start,ithet_end
 +        if (i.le.2) cycle
 +        if ((itype(i-1).eq.ntyp1).or.itype(i-2).eq.ntyp1
 +     &  .or.itype(i).eq.ntyp1) cycle
+ c        if (itype(i-1).eq.ntyp1) cycle
 -        if ((itype(i-1).eq.ntyp1).or.(itype(i-2).eq.ntyp1).or.
 -     &(itype(i).eq.ntyp1)) cycle
          if (iabs(itype(i+1)).eq.20) iblock=2
          if (iabs(itype(i+1)).ne.20) iblock=1
          dethetai=0.0d0
          dephii=0.0d0
          dephii1=0.0d0
          theti2=0.5d0*theta(i)
 -CC Ta zmina jest niewlasciwa
          ityp2=ithetyp((itype(i-1)))
          do k=1,nntheterm
            coskt(k)=dcos(k*theti2)
            sinkt(k)=dsin(k*theti2)
          enddo
 +        if (i.eq.3) then
 +          phii=0.0d0
 +          ityp1=nthetyp+1
 +          do k=1,nsingle
 +            cosph1(k)=0.0d0
 +            sinph1(k)=0.0d0
 +          enddo
 +        else
          if (i.gt.3 .and. itype(i-3).ne.ntyp1) then
  #ifdef OSF
            phii=phi(i)
            enddo
          else
            phii=0.0d0
-           ityp1=nthetyp+1
+ c          ityp1=nthetyp+1
            do k=1,nsingle
+             ityp1=ithetyp((itype(i-2)))
              cosph1(k)=0.0d0
              sinph1(k)=0.0d0
            enddo 
          endif
 +        endif
          if (i.lt.nres .and. itype(i+1).ne.ntyp1) then
  #ifdef OSF
            phii1=phi(i+1)
            enddo
          else
            phii1=0.0d0
-           ityp3=nthetyp+1
+ c          ityp3=nthetyp+1
+           ityp3=ithetyp((itype(i)))
            do k=1,nsingle
              cosph2(k)=0.0d0
              sinph2(k)=0.0d0
@@@ -3736,7 -3708,36 +3903,36 @@@ c        call flush(iout
          etheta=etheta+ethetai
          if (i.gt.3) gloc(i-3,icg)=gloc(i-3,icg)+wang*dephii
          if (i.lt.nres) gloc(i-2,icg)=gloc(i-2,icg)+wang*dephii1
-         gloc(nphi+i-2,icg)=wang*dethetai
+ c        gloc(nphi+i-2,icg)=wang*dethetai
+         gloc(nphi+i-2,icg)=gloc(nphi+i-2,icg)+wang*dethetai
+       enddo
+ C now constrains
+       ethetacnstr=0.0d0
+ C      print *,ithetaconstr_start,ithetaconstr_end,"TU"
+       do i=1,ntheta_constr
+         itheta=itheta_constr(i)
+         thetiii=theta(itheta)
+         difi=pinorm(thetiii-theta_constr0(i))
+         if (difi.gt.theta_drange(i)) then
+           difi=difi-theta_drange(i)
+           ethetacnstr=ethetacnstr+0.25d0*for_thet_constr(i)*difi**4
+           gloc(itheta+nphi-2,icg)=gloc(itheta+nphi-2,icg)
+      &    +for_thet_constr(i)*difi**3
+         else if (difi.lt.-drange(i)) then
+           difi=difi+drange(i)
+           ethetacnstr=ethetacnstr+0.25d0*for_thet_constr(i)*difi**4
+           gloc(itheta+nphi-2,icg)=gloc(itheta+nphi-2,icg)
+      &    +for_thet_constr(i)*difi**3
+         else
+           difi=0.0
+         endif
+ C       if (energy_dec) then
+ C        write (iout,'(a6,2i5,4f8.3,2e14.5)') "ethetc",
+ C     &    i,itheta,rad2deg*thetiii,
+ C     &    rad2deg*theta_constr0(i),  rad2deg*theta_drange(i),
+ C     &    rad2deg*difi,0.25d0*for_thet_constr(i)*difi**4,
+ C     &    gloc(itheta+nphi-2,icg)
+ C        endif
        enddo
        return
        end
@@@ -4127,7 -4128,8 +4323,8 @@@ c        write (2,*) "xx",xx," yy",yy,
  Cc diagnostics - remove later
          xx1 = dcos(alph(2))
          yy1 = dsin(alph(2))*dcos(omeg(2))
-         zz1 = -dsin(alph(2))*dsin(omeg(2))
+ c        zz1 = -dsin(alph(2))*dsin(omeg(2))
+         zz1 = -dsign(1.0d0,itype(i))*dsin(alph(2))*dsin(omeg(2))
          write(2,'(3f8.1,3f9.3,1x,3f9.3)') 
       &    alph(2)*rad2deg,omeg(2)*rad2deg,theta(3)*rad2deg,xx,yy,zz,
       &    xx1,yy1,zz1
@@@ -4502,12 -4504,12 +4699,12 @@@ c       write (iout,*) 'i=',i,' gloc=',
          difi=phii-phi0(i)
          if (difi.gt.drange(i)) then
            difi=difi-drange(i)
-           edihcnstr=edihcnstr+0.25d0*ftors*difi**4
-           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors*difi**3
+           edihcnstr=edihcnstr+0.25d0*ftors(i)*difi**4
+           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors(i)*difi**3
          else if (difi.lt.-drange(i)) then
            difi=difi+drange(i)
-           edihcnstr=edihcnstr+0.25d0*ftors*difi**4
-           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors*difi**3
+           edihcnstr=edihcnstr+0.25d0*ftors(i)*difi**4
+           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors(i)*difi**3
          endif
  !        write (iout,'(2i5,2f8.3,2e14.5)') i,itori,rad2deg*phii,
  !     &    rad2deg*difi,0.25d0*ftors*difi**4,gloc(itori-3,icg)
@@@ -4538,9 -4540,8 +4735,9 @@@ C Set lprn=.true. for debuggin
  c      lprn=.true.
        etors=0.0D0
        do i=iphi_start,iphi_end
 -        if (itype(i-2).eq.ntyp1 .or. itype(i-1).eq.ntyp1
 -     &       .or. itype(i).eq.ntyp1) cycle
 +        if (i.le.2) cycle
 +        if (itype(i-2).eq.ntyp1.or. itype(i-1).eq.ntyp1
 +     &      .or. itype(i).eq.ntyp1 .or. itype(i-3).eq.ntyp1) cycle
          if (itel(i-2).eq.0 .or. itel(i-1).eq.0) goto 1215
           if (iabs(itype(i)).eq.20) then
           iblock=2
@@@ -4596,14 -4597,14 +4793,14 @@@ c       write (iout,*) 'i=',i,' gloc=',
          edihi=0.0d0
          if (difi.gt.drange(i)) then
            difi=difi-drange(i)
-           edihcnstr=edihcnstr+0.25d0*ftors*difi**4
-           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors*difi**3
-           edihi=0.25d0*ftors*difi**4
+           edihcnstr=edihcnstr+0.25d0*ftors(i)*difi**4
+           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors(i)*difi**3
+           edihi=0.25d0*ftors(i)*difi**4
          else if (difi.lt.-drange(i)) then
            difi=difi+drange(i)
-           edihcnstr=edihcnstr+0.25d0*ftors*difi**4
-           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors*difi**3
-           edihi=0.25d0*ftors*difi**4
+           edihcnstr=edihcnstr+0.25d0*ftors(i)*difi**4
+           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors(i)*difi**3
+           edihi=0.25d0*ftors(i)*difi**4
          else
            difi=0.0d0
          endif
@@@ -4638,10 -4639,8 +4835,10 @@@ C Set lprn=.true. for debuggin
  c     lprn=.true.
        etors_d=0.0D0
        do i=iphi_start,iphi_end-1
 -        if (itype(i-2).eq.ntyp1 .or. itype(i-1).eq.ntyp1
 -     &      .or. itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1) cycle
 +        if (i.le.3) cycle
 +         if ((itype(i-2).eq.ntyp1).or.itype(i-3).eq.ntyp1.or.
 +     &  (itype(i-1).eq.ntyp1).or.(itype(i).eq.ntyp1).or.
 +     &  (itype(i+1).eq.ntyp1)) cycle
          if (itel(i-2).eq.0 .or. itel(i-1).eq.0 .or. itel(i).eq.0) 
       &     goto 1215
          itori=itortyp(itype(i-2))
@@@ -4723,7 -4722,7 +4920,7 @@@ c      lprn=.true
  c      write (iout,*) "EBACK_SC_COR",iphi_start,iphi_end,nterm_sccor
        esccor=0.0D0
        do i=itau_start,itau_end
-         if (itype(i-2).eq.ntyp1 .or. itype(i-1).eq.ntyp1) cycle
+         if ((itype(i-2).eq.ntyp1).or.(itype(i-1).eq.ntyp1)) cycle
          esccor_ii=0.0D0
          isccori=isccortyp(itype(i-2))
          isccori1=isccortyp(itype(i-1))
@@@ -4897,9 -4896,9 +5094,9 @@@ c--------------------------------------
        integer dimen1,dimen2,atom,indx
        double precision buffer(dimen1,dimen2)
        double precision zapas 
 -      common /contacts_hb/ zapas(3,20,maxres,7),
 -     &         facont_hb(20,maxres),ees0p(20,maxres),ees0m(20,maxres),
 -     &         num_cont_hb(maxres),jcont_hb(20,maxres)
 +      common /contacts_hb/ zapas(3,ntyp,maxres,7),
 +     &     facont_hb(ntyp,maxres),ees0p(ntyp,maxres),ees0m(ntyp,maxres),
 +     &         num_cont_hb(maxres),jcont_hb(ntyp,maxres)
        num_kont=buffer(1,indx+26)
        num_kont_old=num_cont_hb(atom)
        num_cont_hb(atom)=num_kont+num_kont_old
@@@ -5432,7 -5431,11 +5629,11 @@@ C--------------------------------------
       &  auxmat(2,2)
        iti1 = itortyp(itype(i+1))
        if (j.lt.nres-1) then
-         itj1 = itortyp(itype(j+1))
+         if (itype(j).le.ntyp) then
+           itj1 = itortyp(itype(j+1))
+         else
+           itj1=ntortyp+1
+         endif
        else
          itj1=ntortyp+1
        endif
@@@ -5520,14 -5523,16 +5721,16 @@@ cd      if (i.ne.2 .or. j.ne.4 .or. k.n
        enddo 
        if (l.eq.j+1) then
  C parallel orientation of the two CA-CA-CA frames.
-         if (i.gt.1) then
+ c        if (i.gt.1) then
+         if (i.gt.1 .and. itype(i).le.ntyp) then
            iti=itortyp(itype(i))
          else
            iti=ntortyp+1
          endif
          itk1=itortyp(itype(k+1))
          itj=itortyp(itype(j))
-         if (l.lt.nres-1) then
+ c        if (l.lt.nres-1) then
+         if (l.lt.nres-1 .and. itype(l+1).le.ntyp) then
            itl1=itortyp(itype(l+1))
          else
            itl1=ntortyp+1
@@@ -5673,7 -5678,8 +5876,8 @@@ C Calculate the Cartesian derivatives o
  C End vectors
        else
  C Antiparallel orientation of the two CA-CA-CA frames.
-         if (i.gt.1) then
+ c        if (i.gt.1) then
+         if (i.gt.1 .and. itype(i).le.ntyp) then
            iti=itortyp(itype(i))
          else
            iti=ntortyp+1
          itk1=itortyp(itype(k+1))
          itl=itortyp(itype(l))
          itj=itortyp(itype(j))
-         if (j.lt.nres-1) then
+ c        if (j.lt.nres-1) then
+         if (j.lt.nres-1 .and. itype(j+1).le.ntyp) then
            itj1=itortyp(itype(j+1))
          else 
            itj1=ntortyp+1
  C 4/7/01 AL Component s1 was removed, because it pertains to the respective 
  C           energy moment and not to the cluster cumulant.
        iti=itortyp(itype(i))
-       if (j.lt.nres-1) then
+ c      if (j.lt.nres-1) then
+       if (j.lt.nres-1 .and. itype(j+1).le.ntyp) then
          itj1=itortyp(itype(j+1))
        else
          itj1=ntortyp+1
        endif
        itk=itortyp(itype(k))
        itk1=itortyp(itype(k+1))
-       if (l.lt.nres-1) then
+ c      if (l.lt.nres-1) then
+       if (l.lt.nres-1 .and. itype(l+1).le.ntyp) then
          itl1=itortyp(itype(l+1))
        else
          itl1=ntortyp+1
@@@ -6959,13 -6968,15 +7166,15 @@@ C           energy moment and not to th
  cd      write (2,*) 'eello_graph4: wturn6',wturn6
        iti=itortyp(itype(i))
        itj=itortyp(itype(j))
-       if (j.lt.nres-1) then
+ c      if (j.lt.nres-1) then
+       if (j.lt.nres-1 .and. itype(j+1).le.ntyp) then
          itj1=itortyp(itype(j+1))
        else
          itj1=ntortyp+1
        endif
        itk=itortyp(itype(k))
-       if (k.lt.nres-1) then
+ c      if (k.lt.nres-1) then
+       if (k.lt.nres-1 .and. itype(k+1).le.ntyp) then
          itk1=itortyp(itype(k+1))
        else
          itk1=ntortyp+1
@@@ -7644,34 -7655,4 +7853,34 @@@ C--------------------------------------
        scalar=sc
        return
        end
 +C-----------------------------------------------------------------------
 +      double precision function sscale(r)
 +      double precision r,gamm
 +      include "COMMON.SPLITELE"
 +      if(r.lt.r_cut-rlamb) then
 +        sscale=1.0d0
 +      else if(r.le.r_cut.and.r.ge.r_cut-rlamb) then
 +        gamm=(r-(r_cut-rlamb))/rlamb
 +        sscale=1.0d0+gamm*gamm*(2*gamm-3.0d0)
 +      else
 +        sscale=0d0
 +      endif
 +      return
 +      end
 +C-----------------------------------------------------------------------
 +C-----------------------------------------------------------------------
 +      double precision function sscagrad(r)
 +      double precision r,gamm
 +      include "COMMON.SPLITELE"
 +      if(r.lt.r_cut-rlamb) then
 +        sscagrad=0.0d0
 +      else if(r.le.r_cut.and.r.ge.r_cut-rlamb) then
 +        gamm=(r-(r_cut-rlamb))/rlamb
 +        sscagrad=gamm*(6*gamm-6.0d0)/rlamb
 +      else
 +        sscagrad=0.0d0
 +      endif
 +      return
 +      end
 +C-----------------------------------------------------------------------
  
@@@ -34,8 -34,8 +34,8 @@@ C Assign virtual-bond lengt
        vblinv=1.0D0/vbl
        vblinv2=vblinv*vblinv
  #ifdef CRYST_BOND
 -      read (ibond,*) vbldp0,akp
 -      do i=1,ntyp
 +      read (ibond,*) vbldp0,vbldpdum,akp
 +       do i=1,ntyp
          nbondterm(i)=1
          read (ibond,*) vbldsc0(1,i),aksc(1,i)
          dsc(i) = vbldsc0(1,i)
@@@ -46,7 -46,7 +46,7 @@@
          endif
        enddo
  #else
 -      read (ibond,*) ijunk,vbldp0,akp,rjunk
 +       read (ibond,*) ijunk,vbldp0,vbldpdum,akp,rjunk
        do i=1,ntyp
          read (ibond,*) nbondterm(i),(vbldsc0(j,i),aksc(j,i),abond0(j,i),
       &   j=1,nbondterm(i))
  C
  C Define the constants of the disulfide bridge
  C
-       ebr=-5.50D0
+ C      ebr=-5.50D0
  c
  c Old arbitrary potential - commented out.
  c
@@@ -1040,19 -1040,19 +1040,19 @@@ c Constants of the disulfide-bond poten
  c energy surface of diethyl disulfide.
  c A. Liwo and U. Kozlowska, 11/24/03
  c
-       D0CM = 3.78d0
-       AKCM = 15.1d0
-       AKTH = 11.0d0
-       AKCT = 12.0d0
-       V1SS =-1.08d0
-       V2SS = 7.61d0
-       V3SS = 13.7d0
+ C      D0CM = 3.78d0
+ C      AKCM = 15.1d0
+ C      AKTH = 11.0d0
+ C      AKCT = 12.0d0
+ C      V1SS =-1.08d0
+ C      V2SS = 7.61d0
+ C      V3SS = 13.7d0
  
-       write (iout,'(/a)') "Disulfide bridge parameters:"
-       write (iout,'(a,f10.2)') 'S-S bridge energy: ',ebr
-       write (iout,'(2(a,f10.2))') 'd0cm:',d0cm,' akcm:',akcm
-       write (iout,'(2(a,f10.2))') 'akth:',akth,' akct:',akct
-       write (iout,'(3(a,f10.2))') 'v1ss:',v1ss,' v2ss:',v2ss,
-      & ' v3ss:',v3ss
+ C      write (iout,'(/a)') "Disulfide bridge parameters:"
+ C      write (iout,'(a,f10.2)') 'S-S bridge energy: ',ebr
+ C      write (iout,'(2(a,f10.2))') 'd0cm:',d0cm,' akcm:',akcm
+ C      write (iout,'(2(a,f10.2))') 'akth:',akth,' akct:',akct
+ C      write (iout,'(3(a,f10.2))') 'v1ss:',v1ss,' v2ss:',v2ss,
+ C     & ' v3ss:',v3ss
        return
        end
        include 'COMMON.FFIELD'
        include 'COMMON.FREE'
        include 'COMMON.INTERACT'
 +      include "COMMON.SPLITELE"
        character*320 controlcard,ucase
  #ifdef MPL
        include 'COMMON.INFO'
  #endif
-       integer i
+       integer i,i1,i2,it1,it2
  
        read (INP,'(a80)') titel
        call card_concat(controlcard)
        call readi(controlcard,'RESCALE',rescale_mode,2)
        call reada(controlcard,'DISTCHAINMAX',distchainmax,50.0d0)
        write (iout,*) "DISTCHAINMAX",distchainmax
 +C Reading the dimensions of box in x,y,z coordinates
 +      call reada(controlcard,'BOXX',boxxsize,100.0d0)
 +      call reada(controlcard,'BOXY',boxysize,100.0d0)
 +      call reada(controlcard,'BOXZ',boxzsize,100.0d0)
 +c Cutoff range for interactions
 +      call reada(controlcard,"R_CUT",r_cut,15.0d0)
 +      call reada(controlcard,"LAMBDA",rlamb,0.3d0)
        call readi(controlcard,'PDBOUT',outpdb,0)
        call readi(controlcard,'MOL2OUT',outmol2,0)
        refstr=(index(controlcard,'REFSTR').gt.0)
        pdbref=(index(controlcard,'PDBREF').gt.0)
        iscode=index(controlcard,'ONE_LETTER')
        tree=(index(controlcard,'MAKE_TREE').gt.0)
+       with_dihed_constr = index(controlcard,"WITH_DIHED_CONSTR").gt.0
+       call readi(controlcard,'CONSTR_DIST',constr_dist,0)
+       write (iout,*) "with_dihed_constr ",with_dihed_constr,
+      & " CONSTR_DIST",constr_dist
+       with_theta_constr = index(controlcard,"WITH_THETA_CONSTR").gt.0
+       write (iout,*) "with_theta_constr ",with_theta_constr
+       call flush(iout)
        min_var=(index(controlcard,'MINVAR').gt.0)
        plot_tree=(index(controlcard,'PLOT_TREE').gt.0)
        punch_dist=(index(controlcard,'PUNCH_DIST').gt.0)
        include 'COMMON.CONTROL'
        include 'COMMON.CONTACTS'
        include 'COMMON.TIME1'
+       include 'COMMON.TORCNSTR'
  #ifdef MPL
        include 'COMMON.INFO'
  #endif
        double precision x(maxvar)
        integer itype_pdb(maxres)
        logical seq_comp
-       integer i,j,kkk
+       integer i,j,kkk,i1,i2,it1,it2
  C
  C Body
  C
@@@ -134,6 -134,51 +142,51 @@@ C Read weights of the subsequent energ
        call reada(weightcard,'CUTOFF',cutoff_corr,7.0d0)
        call reada(weightcard,'DELT_CORR',delt_corr,0.5d0)
        if (index(weightcard,'SOFT').gt.0) ipot=6
+       call reada(weightcard,"D0CM",d0cm,3.78d0)
+       call reada(weightcard,"AKCM",akcm,15.1d0)
+       call reada(weightcard,"AKTH",akth,11.0d0)
+       call reada(weightcard,"AKCT",akct,12.0d0)
+       call reada(weightcard,"V1SS",v1ss,-1.08d0)
+       call reada(weightcard,"V2SS",v2ss,7.61d0)
+       call reada(weightcard,"V3SS",v3ss,13.7d0)
+       call reada(weightcard,"EBR",ebr,-5.50D0)
+       call reada(weightcard,"ATRISS",atriss,0.301D0)
+       call reada(weightcard,"BTRISS",btriss,0.021D0)
+       call reada(weightcard,"CTRISS",ctriss,1.001D0)
+       call reada(weightcard,"DTRISS",dtriss,1.001D0)
+       write (iout,*) "ATRISS=", atriss
+       write (iout,*) "BTRISS=", btriss
+       write (iout,*) "CTRISS=", ctriss
+       write (iout,*) "DTRISS=", dtriss
+       dyn_ss=(index(weightcard,'DYN_SS').gt.0)
+       do i=1,maxres
+         dyn_ss_mask(i)=.false.
+       enddo
+       do i=1,maxres-1
+         do j=i+1,maxres
+           dyn_ssbond_ij(i,j)=1.0d300
+         enddo
+       enddo
+       call reada(weightcard,"HT",Ht,0.0D0)
+       if (dyn_ss) then
+         ss_depth=ebr/wsc-0.25*eps(1,1)
+         Ht=Ht/wsc-0.25*eps(1,1)
+         akcm=akcm*wstrain/wsc
+         akth=akth*wstrain/wsc
+         akct=akct*wstrain/wsc
+         v1ss=v1ss*wstrain/wsc
+         v2ss=v2ss*wstrain/wsc
+         v3ss=v3ss*wstrain/wsc
+       else
+         ss_depth=ebr/wstrain-0.25*eps(1,1)*wsc/wstrain
+       endif
+       write (iout,'(/a)') "Disulfide bridge parameters:"
+       write (iout,'(a,f10.2)') 'S-S bridge energy: ',ebr
+       write (iout,'(2(a,f10.2))') 'd0cm:',d0cm,' akcm:',akcm
+       write (iout,'(2(a,f10.2))') 'akth:',akth,' akct:',akct
+       write (iout,'(3(a,f10.2))') 'v1ss:',v1ss,' v2ss:',v2ss,
+      & ' v3ss:',v3ss
  C 12/1/95 Added weight for the multi-body term WCORR
        call reada(weightcard,'WCORRH',wcorr,1.0D0)
        if (wcorr4.gt.0.0d0) wcorr=wcorr4
@@@ -241,6 -286,66 +294,66 @@@ C Convert sequence to numeric cod
  
        print *,'Call Read_Bridge.'
        call read_bridge
+ C this fragment reads diheadral constrains
+       if (with_dihed_constr) then
+       read (inp,*) ndih_constr
+       if (ndih_constr.gt.0) then
+ C        read (inp,*) ftors
+ C        write (iout,*) 'FTORS',ftors
+ C ftors is the force constant for torsional quartic constrains
+         read (inp,*) (idih_constr(i),phi0(i),drange(i),ftors(i),
+      &                i=1,ndih_constr)
+         write (iout,*)
+      &   'There are',ndih_constr,' constraints on phi angles.'
+         do i=1,ndih_constr
+           write (iout,'(i5,3f8.3)') idih_constr(i),phi0(i),drange(i),
+      &  ftors(i)
+         enddo
+         do i=1,ndih_constr
+           phi0(i)=deg2rad*phi0(i)
+           drange(i)=deg2rad*drange(i)
+         enddo
+       endif ! endif ndif_constr.gt.0
+       endif ! with_dihed_constr
+       if (with_theta_constr) then
+ C with_theta_constr is keyword allowing for occurance of theta constrains
+       read (inp,*) ntheta_constr
+ C ntheta_constr is the number of theta constrains
+       if (ntheta_constr.gt.0) then
+ C        read (inp,*) ftors
+         read (inp,*) (itheta_constr(i),theta_constr0(i),
+      &  theta_drange(i),for_thet_constr(i),
+      &  i=1,ntheta_constr)
+ C the above code reads from 1 to ntheta_constr 
+ C itheta_constr(i) residue i for which is theta_constr
+ C theta_constr0 the global minimum value
+ C theta_drange is range for which there is no energy penalty
+ C for_thet_constr is the force constant for quartic energy penalty
+ C E=k*x**4 
+ C        if(me.eq.king.or..not.out1file)then
+          write (iout,*)
+      &   'There are',ntheta_constr,' constraints on phi angles.'
+          do i=1,ntheta_constr
+           write (iout,'(i5,3f8.3)') itheta_constr(i),theta_constr0(i),
+      &    theta_drange(i),
+      &    for_thet_constr(i)
+          enddo
+ C        endif
+         do i=1,ntheta_constr
+           theta_constr0(i)=deg2rad*theta_constr0(i)
+           theta_drange(i)=deg2rad*theta_drange(i)
+         enddo
+ C        if(me.eq.king.or..not.out1file)
+ C     &   write (iout,*) 'FTORS',ftors
+ C        do i=1,ntheta_constr
+ C          ii = itheta_constr(i)
+ C          thetabound(1,ii) = phi0(i)-drange(i)
+ C          thetabound(2,ii) = phi0(i)+drange(i)
+ C        enddo
+       endif ! ntheta_constr.gt.0
+       endif! with_theta_constr
        nnt=1
        nct=nres
        print *,'NNT=',NNT,' NCT=',NCT
@@@ -320,6 -425,45 +433,45 @@@ c      endi
          endif
          call contact(.true.,ncont_ref,icont_ref)
        endif
+        if (ns.gt.0) then
+ C        write (iout,'(/a,i3,a)')
+ C     &  'The chain contains',ns,' disulfide-bridging cysteines.'
+         write (iout,'(20i4)') (iss(i),i=1,ns)
+        if (dyn_ss) then
+           write(iout,*)"Running with dynamic disulfide-bond formation"
+        else
+         write (iout,'(/a/)') 'Pre-formed links are:'
+         do i=1,nss
+           i1=ihpb(i)-nres
+           i2=jhpb(i)-nres
+           it1=itype(i1)
+           it2=itype(i2)
+           write (iout,'(2a,i3,3a,i3,a,3f10.3)')
+      &    restyp(it1),'(',i1,') -- ',restyp(it2),'(',i2,')',dhpb(i),
+      &    ebr,forcon(i)
+         enddo
+         write (iout,'(a)')
+        endif
+       endif
+       if (ns.gt.0.and.dyn_ss) then
+           do i=nss+1,nhpb
+             ihpb(i-nss)=ihpb(i)
+             jhpb(i-nss)=jhpb(i)
+             forcon(i-nss)=forcon(i)
+             dhpb(i-nss)=dhpb(i)
+           enddo
+           nhpb=nhpb-nss
+           nss=0
+           call hpb_partition
+           do i=1,ns
+             dyn_ss_mask(iss(i))=.true.
+           enddo
+       endif
+ c Read distance restraints
+       if (constr_dist.gt.0) then
+         call read_dist_constr
+         call hpb_partition
+       endif
        return
        end
  c-----------------------------------------------------------------------------
@@@ -364,10 -508,12 +516,12 @@@ C Check whether the specified bridging 
        do i=1,ns
        if (itype(iss(i)).ne.1) then
          write (iout,'(2a,i3,a)') 
-      &   'Do you REALLY think that the residue ',restyp(iss(i)),i,
+      &   'Do you REALLY think that the residue ',
+      &    restyp(itype(iss(i))),i,
       &   ' can form a disulfide bridge?!!!'
          write (*,'(2a,i3,a)') 
-      &   'Do you REALLY think that the residue ',restyp(iss(i)),i,
+      &   'Do you REALLY think that the residue ',
+      &   restyp(itype(iss(i))),i,
       &   ' can form a disulfide bridge?!!!'
  #ifdef MPL
         call mp_stopall(error_msg)
@@@ -408,8 -554,8 +562,8 @@@ C bridging residues
            enddo
            write (iout,'(a,i3,a)') 'Pair',i,' contains unknown cystine.'
     20     continue
-           dhpb(i)=dbr
-           forcon(i)=fbr
+ C          dhpb(i)=dbr
+ C          forcon(i)=fbr
          enddo
          do i=1,nss
            ihpb(i)=ihpb(i)+nres
@@@ -490,6 -636,25 +644,25 @@@ c--------------------------------------
        read (rekord(iread:),*) wartosc
        return
        end
+ C----------------------------------------------------------------------
+       subroutine multreadi(rekord,lancuch,tablica,dim,default)
+       implicit none
+       integer dim,i
+       integer tablica(dim),default
+       character*(*) rekord,lancuch
+       character*80 aux
+       integer ilen,iread
+       external ilen
+       do i=1,dim
+         tablica(i)=default
+       enddo
+       iread=index(rekord,lancuch(:ilen(lancuch))//"=")
+       if (iread.eq.0) return
+       iread=iread+ilen(lancuch)+1
+       read (rekord(iread:),*,end=10,err=10) (tablica(i),i=1,dim)
+    10 return
+       end
  c----------------------------------------------------------------------------
        subroutine card_concat(card)
        include 'DIMENSIONS'
  #endif
        return
        end
+       subroutine read_dist_constr
+       implicit real*8 (a-h,o-z)
+       include 'DIMENSIONS'
+ #ifdef MPI
+       include 'mpif.h'
+ #endif
+       include 'COMMON.CONTROL'
+       include 'COMMON.CHAIN'
+       include 'COMMON.IOUNITS'
+       include 'COMMON.SBRIDGE'
+       integer ifrag_(2,100),ipair_(2,100)
+       double precision wfrag_(100),wpair_(100)
+       character*500 controlcard
+       logical lprn /.true./
+       write (iout,*) "Calling read_dist_constr"
+ C      write (iout,*) "nres",nres," nstart_sup",nstart_sup," nsup",nsup
+ C      call flush(iout)
+       write(iout,*) "TU sie wywalam?"
+       call card_concat(controlcard)
+       write (iout,*) controlcard
+       call flush(iout)
+       call readi(controlcard,"NFRAG",nfrag_,0)
+       call readi(controlcard,"NPAIR",npair_,0)
+       call readi(controlcard,"NDIST",ndist_,0)
+       call reada(controlcard,'DIST_CUT',dist_cut,5.0d0)
+       call multreadi(controlcard,"IFRAG",ifrag_(1,1),2*nfrag_,0)
+       call multreadi(controlcard,"IPAIR",ipair_(1,1),2*npair_,0)
+       call multreada(controlcard,"WFRAG",wfrag_(1),nfrag_,0.0d0)
+       call multreada(controlcard,"WPAIR",wpair_(1),npair_,0.0d0)
+       write (iout,*) "NFRAG",nfrag_," NPAIR",npair_," NDIST",ndist_
+       write (iout,*) "IFRAG"
+       do i=1,nfrag_
+         write (iout,*) i,ifrag_(1,i),ifrag_(2,i),wfrag_(i)
+       enddo
+       write (iout,*) "IPAIR"
+       do i=1,npair_
+         write (iout,*) i,ipair_(1,i),ipair_(2,i),wpair_(i)
+       enddo
+       call flush(iout)
+       do i=1,nfrag_
+         if (ifrag_(1,i).lt.nstart_sup) ifrag_(1,i)=nstart_sup
+         if (ifrag_(2,i).gt.nstart_sup+nsup-1)
+      &    ifrag_(2,i)=nstart_sup+nsup-1
+ c        write (iout,*) i,ifrag_(1,i),ifrag_(2,i),wfrag_(i)
+         call flush(iout)
+         if (wfrag_(i).gt.0.0d0) then
+         do j=ifrag_(1,i),ifrag_(2,i)-1
+           do k=j+1,ifrag_(2,i)
+             write (iout,*) "j",j," k",k
+             ddjk=dist(j,k)
+             if (constr_dist.eq.1) then
+               nhpb=nhpb+1
+               ihpb(nhpb)=j
+               jhpb(nhpb)=k
+               dhpb(nhpb)=ddjk
+               forcon(nhpb)=wfrag_(i) 
+             else if (constr_dist.eq.2) then
+               if (ddjk.le.dist_cut) then
+                 nhpb=nhpb+1
+                 ihpb(nhpb)=j
+                 jhpb(nhpb)=k
+                 dhpb(nhpb)=ddjk
+                 forcon(nhpb)=wfrag_(i) 
+               endif
+             else
+               nhpb=nhpb+1
+               ihpb(nhpb)=j
+               jhpb(nhpb)=k
+               dhpb(nhpb)=ddjk
+               forcon(nhpb)=wfrag_(i)*dexp(-0.5d0*(ddjk/dist_cut)**2)
+             endif
+             if (lprn)
+      &      write (iout,'(a,3i5,f8.2,1pe12.2)') "+dist.constr ",
+      &       nhpb,ihpb(nhpb),jhpb(nhpb),dhpb(nhpb),forcon(nhpb)
+           enddo
+         enddo
+         endif
+       enddo
+       do i=1,npair_
+         if (wpair_(i).gt.0.0d0) then
+         ii = ipair_(1,i)
+         jj = ipair_(2,i)
+         if (ii.gt.jj) then
+           itemp=ii
+           ii=jj
+           jj=itemp
+         endif
+         do j=ifrag_(1,ii),ifrag_(2,ii)
+           do k=ifrag_(1,jj),ifrag_(2,jj)
+             nhpb=nhpb+1
+             ihpb(nhpb)=j
+             jhpb(nhpb)=k
+             forcon(nhpb)=wpair_(i)
+             dhpb(nhpb)=dist(j,k)
+             write (iout,'(a,3i5,f8.2,f10.1)') "+dist.constr ",
+      &       nhpb,ihpb(nhpb),jhpb(nhpb),dhpb(nhpb),forcon(nhpb)
+           enddo
+         enddo
+         endif
+       enddo 
+       do i=1,ndist_
+         if (constr_dist.eq.11) then
+         read (inp,*) ihpb(nhpb+1),jhpb(nhpb+1),dhpb(i),dhpb1(i),
+      &     ibecarb(i),forcon(nhpb+1),fordepth(nhpb+1)
+         fordepth(nhpb+1)=fordepth(nhpb+1)/forcon(nhpb+1)
+ C        write (iout,'(a,3i5,f8.2,f10.1)') "+dist.constr ",
+ C     &     nhpb,ihpb(nhpb),jhpb(nhpb),dhpb(nhpb),forcon(nhpb)
+         else
+         read (inp,*) ihpb(nhpb+1),jhpb(nhpb+1),forcon(nhpb+1)
+         endif
+         if (forcon(nhpb+1).gt.0.0d0) then
+           nhpb=nhpb+1
+           if (ibecarb(i).gt.0) then
+             ihpb(i)=ihpb(i)+nres
+             jhpb(i)=jhpb(i)+nres
+           endif
+           if (dhpb(nhpb).eq.0.0d0)
+      &       dhpb(nhpb)=dist(ihpb(nhpb),jhpb(nhpb))
+ C          dhpb(nhpb)=dist(ihpb(nhpb),jhpb(nhpb))
+           write (iout,'(a,3i5,f8.2,f10.1)') "+dist.constr ",
+      &     nhpb,ihpb(nhpb),jhpb(nhpb),dhpb(nhpb),forcon(nhpb)
+         endif
+ C      endif
+       enddo
+       call hpb_partition
+       call flush(iout)
+       return
+       end
@@@ -18,7 -18,7 +18,7 @@@ set(UNRES_CSA_SRC
        contact.f
        convert.f
        cored.f
 -      csa.f
 +      csa.F
        dfa.F
        diff12.f
        distfit.f
@@@ -73,7 -73,6 +73,7 @@@ set(UNRES_CSA_PP_SR
        cartder.F
        chainbuild.F
        checkder_p.F
 +      csa.F
        dfa.F
        econstr_local.F
        energy_p_new_barrier.F
@@@ -136,19 -135,19 +136,19 @@@ set_property(SOURCE ${UNRES_CSA_SRC3} P
  #=========================================
  
  if(UNRES_CSA_FF STREQUAL "CASP3" )
 -  set(CPPFLAGS "PROCOR -DUNRES -DISNAN -DSPLITELE -DLANG0 -DCRYST_TOR -DCRYST_BOND -DCRYST_THETA -DCRYST_SC -DMOMENT" )
 +  set(CPPFLAGS "UNRES -DISNAN -DSPLITELE -DLANG0 -DMOMENT -DCRYST_BOND -DCRYST_THETA -DCRYST_SC -DCRYST_TOR" )
  elseif(UNRES_CSA_FF STREQUAL "ALPHA")
 -  set(CPPFLAGS "PROCOR -DUNRES -DISNAN -DSPLITELE -DLANG0 -DMOMENT -DCRYST_BOND -DCRYST_THETA -DCRYST_SC" )
 +  set(CPPFLAGS "UNRES -DISNAN -DSPLITELE -DLANG0 -DMOMENT -DCRYST_BOND -DCRYST_THETA -DCRYST_SC" )
  elseif(UNRES_CSA_FF STREQUAL "BETA")
 -  set(CPPFLAGS "PROCOR -DUNRES -DISNAN -DSPLITELE -DLANG0 -DMOMENT -DCRYST_BOND -DCRYST_THETA -DCRYST_SC" )
 +  set(CPPFLAGS "UNRES -DISNAN -DSPLITELE -DLANG0 -DMOMENT -DCRYST_BOND -DCRYST_THETA -DCRYST_SC" )
  elseif(UNRES_CSA_FF STREQUAL "ALPHABETA")
 -  set(CPPFLAGS "PROCOR -DUNRES -DISNAN -DSPLITELE -DLANG0 -DMOMENT -DCRYST_BOND -DCRYST_THETA -DCRYST_SC" )
 +  set(CPPFLAGS "UNRES -DISNAN -DSPLITELE -DLANG0 -DMOMENT -DCRYST_BOND -DCRYST_THETA -DCRYST_SC" )
  elseif(UNRES_CSA_FF STREQUAL "CASP5")
 -  set(CPPFLAGS "PROCOR -DUNRES -DISNAN -DSPLITELE -DLANG0 -DCRYST_BOND -DCRYST_THETA -DCRYST_SC" )
 +  set(CPPFLAGS "UNRES -DISNAN -DSPLITELE -DLANG0 -DMOMENT -DCRYST_BOND -DCRYST_THETA -DCRYST_SC" )
  elseif(UNRES_CSA_FF STREQUAL "3P")
 -  set(CPPFLAGS "PROCOR -DUNRES -DISNAN -DSPLITELE -DLANG0 -DCRYST_BOND -DCRYST_THETA -DCRYST_SC" )
 +  set(CPPFLAGS "UNRES -DISNAN -DSPLITELE -DLANG0 -DCRYST_BOND -DCRYST_THETA -DCRYST_SC" )
  elseif(UNRES_CSA_FF STREQUAL "4P")
 -  set(CPPFLAGS "PROCOR -DUNRES -DISNAN -DSPLITELE -DLANG0 -DCRYST_BOND -DCRYST_THETA -DCRYST_SC" )
 +  set(CPPFLAGS "UNRES -DISNAN -DSPLITELE -DLANG0 -DCRYST_BOND -DCRYST_THETA -DCRYST_SC" )
  endif(UNRES_CSA_FF STREQUAL "CASP3")
  
  #=========================================
@@@ -250,7 -249,7 +250,7 @@@ target_link_libraries( UNRES_BIN-CSA ${
  #=========================================
  # Install Path
  #=========================================
- install(TARGETS UNRES_BIN-CSA DESTINATION ${CMAKE_INSTALL_PREFIX})
+ install(TARGETS UNRES_BIN-CSA DESTINATION ${CMAKE_INSTALL_PREFIX}/unres/CSA)
  
  
  #=========================================
@@@ -1,14 -1,14 +1,15 @@@
        integer modecalc,iscode,indpdb,indback,indphi,iranconf,icheckgrad,
 -     & inprint,i2ndstr,mucadyn,constr_dist,symetr
 +     & inprint,i2ndstr,mucadyn,constr_dist,symetr,AFMlog,selfguide
        logical minim,refstr,pdbref,outpdb,outmol2,overlapsc,energy_dec,
       &                 sideadd,lsecondary,read_cart,unres_pdb,
       &                 vdisulf,searchsc,lmuca,dccart,extconf,out1file,
-      &                 gnorm_check,gradout,split_ene
+      &                 gnorm_check,gradout,split_ene,with_theta_constr
        common /cntrl/ modecalc,iscode,indpdb,indback,indphi,iranconf,
       & icheckgrad,minim,i2ndstr,refstr,pdbref,outpdb,outmol2,iprint,
       & overlapsc,energy_dec,sideadd,lsecondary,read_cart,unres_pdb
       & ,vdisulf,searchsc,lmuca,dccart,mucadyn,extconf,out1file,
-      & constr_dist,gnorm_check,gradout,split_ene,symetr,AFMlog,
-      & selfguide
++     & selfguide,AFMlog,
+      & constr_dist,gnorm_check,gradout,split_ene,with_theta_constr,
+      & symetr
  C... minim = .true. means DO minimization.
  C... energy_dec = .true. means print energy decomposition matrix
@@@ -6,17 -6,17 +6,17 @@@
  ********************************************************************************
  C Max. number of processors.
        integer maxprocs
 -      parameter (maxprocs=2048)
 +      parameter (maxprocs=1028)
  C Max. number of fine-grain processors
        integer max_fg_procs
  c      parameter (max_fg_procs=maxprocs)
 -      parameter (max_fg_procs=512)
 +      parameter (max_fg_procs=256)
  C Max. number of coarse-grain processors
        integer max_cg_procs
        parameter (max_cg_procs=maxprocs)
  C Max. number of AA residues
        integer maxres
 -      parameter (maxres=1200)
 +      parameter (maxres=600)
  C Appr. max. number of interaction sites
        integer maxres2,maxres6,mmaxres2
        parameter (maxres2=2*maxres,maxres6=6*maxres)
@@@ -28,7 -28,7 +28,7 @@@ C Max number of symetric chain
        parameter (maxperm=120) 
  C Max. number of variables
        integer maxvar
-       parameter (maxvar=4*maxres)
+       parameter (maxvar=6*maxres)
  C Max. number of groups of interactions that a given SC is involved in
        integer maxint_gr
        parameter (maxint_gr=2)
@@@ -41,7 -41,7 +41,7 @@@ C Max. number of SC contact
        parameter (maxcont=12*maxres)
  C Max. number of contacts per residue
        integer maxconts
 -      parameter (maxconts=maxres/4)
 +      parameter (maxconts=maxres)
  c      parameter (maxconts=50)
  C Number of AA types (at present only natural AA's will be handled
        integer ntyp,ntyp1
@@@ -95,7 -95,7 +95,7 @@@ C Max. number of conformations in the p
        parameter (max_pool=10)
  C Number of energy components
        integer n_ene,n_ene2
-       parameter (n_ene=23,n_ene2=2*n_ene)
+       parameter (n_ene=25,n_ene2=2*n_ene)
  C Number of threads in deformation
        integer max_thread,max_thread2
        parameter (max_thread=4,max_thread2=2*max_thread)     
@@@ -1,84 -1,98 +1,97 @@@
- FC = ifort
+ #INSTALL_DIR = /usr/local/mpich-1.2.0
+ INSTALL_DIR = /users/software/mpich-1.2.7p1_intel-10.1_em64_ssh
+ #
+ #FC= /usr/local/opt/intel/compiler60/ia32/bin/ifc
+ FC= ifort
+ OPT =  -O3 -ip -w
+ #OPT = -g -CB
+ #OPT = -g
+ CFLAGS = -DSGI -c
  FFLAGS = -c ${OPT} -I$(INSTALL_DIR)/include
  FFLAGS1 = -c -w -g -d2 -CA -CB -I$(INSTALL_DIR)/include
 -FFLAGS2 = -c -w -O0 -I$(INSTALL_DIR)/include
 +FFLAGS2 = -c -w -g -O0 -I$(INSTALL_DIR)/include
  FFLAGSE = -c -w -O3 -ipo -ipo_obj  -opt_report -I$(INSTALL_DIR)/include
  
- CC = cc
- CFLAGS = -DLINUX -DPGI -c
- OPT =  -O3 -ip -w
- # -Mvect <---slows down
- #        -Minline=name:matmat2 <---false convergence
- LIBS = -Lxdrf -lxdrf
- #-DMOMENT
- #-DCO_BIAS
- #-DCRYST_TOR
- #-DDEBUG
+ #BIN = ../../../bin/unres/MD-M/unres_Tc_procor_newparm_em64-D-symetr.exe
+ #LIBS = -L$(INSTALL_DIR)/lib_pgi -lmpich xdrf/libxdrf.a
+ #LIBS = -L$(INSTALL_DIR)/lib_ifort -lmpich xdrf/libxdrf.a
+ LIBS = -L$(INSTALL_DIR)/lib -lmpich ../../lib/xdrf_em64/libxdrf.a 
  
  ARCH = LINUX
  PP = /lib/cpp -P
  
- all: 
-       @echo "Specify force field: GAB, 4P or E0LL2Y"
+ all: GAB
  
  .SUFFIXES: .F
  .F.o:
 -      ${FC} ${FFLAGS}  ${CPPFLAGS} $*.F
 -
 +      ${FC} ${FFLAGS} ${CPPFLAGS} $*.F
  
  object = unres.o arcos.o cartprint.o chainbuild.o convert.o initialize_p.o \
          matmult.o readrtns_CSA.o parmread.o gen_rand_conf.o printmat.o map.o \
          pinorm.o randgens.o rescode.o intcor.o timing.o misc.o intlocal.o \
          cartder.o checkder_p.o econstr_local.o energy_p_new_barrier.o \
        energy_p_new-sep_barrier.o gradient_p.o minimize_p.o sumsld.o \
 -        cored.o rmdd.o geomout.o readpdb.o permut.o regularize.o thread.o fitsq.o mcm.o \
 +        cored.o rmdd.o geomout.o readpdb.o regularize.o thread.o fitsq.o mcm.o \
          mc.o bond_move.o refsys.o check_sc_distr.o check_bond.o contact.o djacob.o \
 -        eigen.o blas.o add.o entmcm.o minim_mcmf.o \
 -        together.o csa.o minim_jlee.o shift.o diff12.o bank.o newconf.o ran.o \
 -        indexx.o MP.o compare_s1.o prng_32.o \
 -        test.o banach.o distfit.o rmsd.o elecont.o dihed_cons.o \
 +        eigen.o blas.o add.o entmcm.o \
 +        MP.o compare_s1.o \
 +        banach.o rmsd.o elecont.o dihed_cons.o \
          sc_move.o local_move.o \
          intcartderiv.o lagrangian_lesyng.o\
        stochfric.o kinetic_lesyng.o MD_A-MTS.o moments.o int_to_cart.o \
 -        surfatom.o sort.o muca_md.o MREMD.o rattle.o gauss.o energy_split-sep.o \
 -      q_measure.o gnmr1.o ssMD.o
 +        surfatom.o sort.o muca_md.o rattle.o gauss.o energy_split-sep.o \
 +        q_measure.o gnmr1.o test.o ssMD.o permut.o distfit.o checkvar.o
 +
 +no_option:
  
- GAB: CPPFLAGS = -DPROCOR -DLINUX -DPGI -DAMD64 -DUNRES -DISNAN \
-       -DSPLITELE -DLANG0 -DCRYST_BOND -DCRYST_THETA -DCRYST_SC
- GAB: BIN = ../../../bin/unres/MD/unres-mult_ifort_single_GAB.exe
- GAB: ${object} xdrf/libxdrf.a
+ GAB: CPPFLAGS = -DPROCOR -DLINUX -DPGI -DUNRES -DISNAN -DMP -DMPI -DAMD64 \
+       -DSPLITELE -DLANG0 -DCRYST_BOND -DCRYST_THETA -DCRYST_SC\
+         -DSCCORPDB
+ GAB: BIN = ../../../bin/unres/MD-M/unres_ifort_MPICH_GAB.exe
+ GAB: ${object} ../../lib/xdrf_em64/libxdrf.a
+       cc -o compinfo compinfo.c
+       ./compinfo | true
+       ${FC} ${FFLAGS} cinfo.f
+       ${FC} ${OPT} ${object} cinfo.o ${LIBS}  -o ${BIN}
+ E0LL2Y: CPPFLAGS = -DPROCOR -DLINUX -DPGI -DUNRES -DISNAN -DMP -DMPI -DAMD64 \
+       -DSPLITELE -DLANG0
+ E0LL2Y: BIN = ../../../bin/unres/MD-M/unres_ifort_MPICH_E0LL2Y.exe
+ E0LL2Y: ${object} ../../lib/xdrf_em64/libxdrf.a
        cc -o compinfo compinfo.c
        ./compinfo | true
        ${FC} ${FFLAGS} cinfo.f
        ${FC} ${OPT} ${object} cinfo.o ${LIBS}  -o ${BIN}
  
+ ../../lib/xdrf_em64/libxdrf.a:
+       cd ../../lib/xdrf_em64 && make
 +4P: CPPFLAGS = -DLINUX -DPGI -DAMD64 -DUNRES -DISNAN \
 +      -DSPLITELE -DLANG0 -DCRYST_BOND -DCRYST_THETA -DCRYST_SC
 +4P: BIN = ../../../bin/unres/MD/unres-mult_ifort_single_4P.exe
 +4P: ${object} xdrf/libxdrf.a
 +      cc -o compinfo compinfo.c
 +      ./compinfo | true
 +      ${FC} ${FFLAGS} cinfo.f
 +      ${FC} ${OPT} ${object} cinfo.o ${LIBS}  -o ${BIN}
  
 -clean:
 -      /bin/rm *.o
 -
 -newconf.o: newconf.f
 -      ${FC} ${FFLAGS} ${CPPFLAGS} newconf.f
 -
 -bank.o: bank.F
 -      ${FC} ${FFLAGS} ${CPPFLAGS} bank.F
 -
 -diff12.o: diff12.f
 -      ${FC} ${FFLAGS} ${CPPFLAGS} diff12.f
 -
 -csa.o: csa.f
 -      ${FC} ${FFLAGS} ${CPPFLAGS} csa.f
 -
 -shift.o: shift.F
 -      ${FC} ${FFLAGS} ${CPPFLAGS} shift.F
 +E0LL2Y: CPPFLAGS = -DPROCOR -DLINUX -DPGI -DAMD64 -DUNRES -DISNAN \
 +      -DSPLITELE -DLANG0
 +E0LL2Y: BIN = ../../../bin/unres/MD/unres-mult_ifort_single_E0LL2Y.exe
 +E0LL2Y: ${object} xdrf/libxdrf.a
 +      cc -o compinfo compinfo.c
 +      ./compinfo | true
 +      ${FC} ${FFLAGS} cinfo.f
 +      ${FC} ${OPT} ${object} cinfo.o ${LIBS}  -o ${BIN}
  
 -ran.o: ran.f
 -      ${FC} ${FFLAGS} ${CPPFLAGS} ran.f
 +xdrf/libxdrf.a:
 +      cd xdrf && make
  
 -together.o: together.F
 -      ${FC} ${FFLAGS} ${CPPFLAGS} together.F
 +clean:
 +      /bin/rm -f *.o && /bin/rm -f compinfo && cd xdrf && make clean
  
  test.o: test.F
        ${FC} ${FFLAGS} ${CPPFLAGS} test.F
@@@ -101,6 -115,9 +114,6 @@@ cartder.o : cartder.
  readpdb.o : readpdb.F
        ${FC} ${FFLAGS2} ${CPPFLAGS} readpdb.F
  
 -permut.o : permut.F
 -      ${FC} ${FFLAGS2} ${CPPFLAGS} permut.F
 -
  sumsld.o : sumsld.f
        ${FC} ${FFLAGS} ${CPPFLAGS} sumsld.f
          
@@@ -110,29 -127,20 +123,29 @@@ cored.o : cored.
  rmdd.o : rmdd.f
        ${FC} ${FFLAGS} ${CPPFLAGS} rmdd.f
  
 -energy_p_new.o : energy_p_new.F
 -      ${FC} ${FFLAGSE} ${CPPFLAGS} energy_p_new.F
 +energy_p_new_barrier.o : energy_p_new_barrier.F
 +      ${FC} ${FFLAGSE} ${CPPFLAGS} energy_p_new_barrier.F
 +
 +gradient_p.o : gradient_p.F
 +      ${FC} ${FFLAGSE} ${CPPFLAGS} gradient_p.F
 +
 +energy_p_new-sep_barrier.o : energy_p_new-sep_barrier.F
 +      ${FC} ${FFLAGSE} ${CPPFLAGS} energy_p_new-sep_barrier.F
  
  lagrangian_lesyng.o : lagrangian_lesyng.F
        ${FC} ${FFLAGSE} ${CPPFLAGS} lagrangian_lesyng.F
  
 -proc_proc.o: proc_proc.c
 -      ${CC} ${CFLAGS} proc_proc.c
 +MD_A-MTS.o : MD_A-MTS.F
 +      ${FC} ${FFLAGSE} ${CPPFLAGS} MD_A-MTS.F
  
 -add.o: add.f
 -      ${FC} ${FFLAGS2} add.f
 +blas.o : blas.f
 +      ${FC} ${FFLAGS1} blas.f
  
 -blas.o: blas.f
 -      ${FC} ${FFLAGS2} blas.f
 +add.o : add.f
 +      ${FC} ${FFLAGS1} add.f
  
 -eigen.o: eigen.f
 +eigen.o : eigen.f
        ${FC} ${FFLAGS2} eigen.f
 +
 +proc_proc.o: proc_proc.c
 +      ${CC} ${CFLAGS} proc_proc.c
@@@ -272,16 -272,17 +272,17 @@@ C Check the gradient of the energy in C
        integer uiparm(1)
        double precision urparm(1)
        external fdum
 -      r_cut=2.0d0
 -      rlambd=0.3d0
 +c      r_cut=2.0d0
 +c      rlambd=0.3d0
        icg=1
        nf=0
        nfl=0                
+       print *,"ATU 3"
        call intout
  c      call intcartderiv
  c      call checkintcartgrad
        call zerograd
-       aincr=1.0D-5
+       aincr=8.0D-6
        write(iout,*) 'Calling CHECK_ECARTINT.'
        nf=0
        icall=0
        endif
        write (iout,'(/a/)') 'Gradient in virtual-bond and SC vectors'
        do i=0,nres
+         print *,i
          do j=1,3
          xx(j)=c(j,i+nres)
          ddc(j)=dc(j,i) 
@@@ -395,7 -397,6 +397,7 @@@ c            write (iout,*) "etot11",et
  c          write(iout,'(2i5,2(a,f15.10))')i,j," etot",etot," etot1",etot1
          dc(j,i)=ddc(j)-aincr
            call chainbuild_cart
 +C          print *,c(j,i)
  c          call int_from_cart1(.false.)
            if (.not.split_ene) then
              call etotal(energia1(0))
@@@ -507,8 -508,10 +509,10 @@@ c--------------------------------------
  #else
        do i=2,nres
  #endif
+ C        print *,i
          dnorm1=dist(i-1,i)
-         dnorm2=dist(i,i+1) 
+         dnorm2=dist(i,i+1)
+ C        print *,i,dnorm1,dnorm2 
        do j=1,3
          c(j,maxres2)=0.5D0*(2*c(j,i)+(c(j,i-1)-c(j,i))/dnorm1
       &     +(c(j,i+1)-c(j,i))/dnorm2)
          endif
          endif
          omeg(i)=beta(nres+i,i,maxres2,i+1)
+ C        print *,omeg(i)
          alph(i)=alpha(nres+i,i,maxres2)
+ C        print *,alph(i)
          theta(i+1)=alpha(i-1,i,i+1)
          vbld(i)=dist(i-1,i)
+ C        print *,vbld(i)
          vbld_inv(i)=1.0d0/vbld(i)
          vbld(nres+i)=dist(nres+i,i)
+ C            print *,vbld(i+nres)
          if (itype(i).ne.10) then
            vbld_inv(nres+i)=1.0d0/vbld(nres+i)
          else
@@@ -24,7 -24,6 +24,7 @@@ cMS$ATTRIBUTES C ::  proc_pro
        include 'COMMON.MD'
        include 'COMMON.CONTROL'
        include 'COMMON.TIME1'
 +      include 'COMMON.SPLITELE'
  #ifdef MPI      
  c      print*,"ETOTAL Processor",fg_rank," absolute rank",myrank,
  c     & " nfgtasks",nfgtasks
@@@ -99,7 -98,6 +99,7 @@@ c      endi
  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)
@@@ -113,7 -111,6 +113,7 @@@ C Berne-Pechukas potential (dilated LJ
        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)
@@@ -137,13 -134,6 +137,13 @@@ c      print *,"Processor",myrank," com
  #ifdef TIMING
        time_vec=time_vec+MPI_Wtime()-time01
  #endif
 +C Introduction of shielding effect first for each peptide group
 +C the shielding factor is set this factor is describing how each
 +C peptide group is shielded by side-chains
 +C the matrix - shield_fac(i) the i index describe the ith between i and i+1
 +      if (shield_mode.gt.0) then
 +       call set_shield_fac
 +      endif
  c      print *,"Processor",myrank," left VEC_AND_DERIV"
        if (ipot.lt.6) then
  #ifdef SPLITELE
              eello_turn4=0.0d0
           endif
        else
 -c        write (iout,*) "Soft-spheer ELEC potential"
 -        call eelec_soft_sphere(ees,evdw1,eel_loc,eello_turn3,
 -     &   eello_turn4)
 +        write (iout,*) "Soft-spheer ELEC potential"
 +c        call eelec_soft_sphere(ees,evdw1,eel_loc,eello_turn3,
 +c     &   eello_turn4)
        endif
  c      print *,"Processor",myrank," computed UELEC"
  C
  C Calculate the virtual-bond-angle energy.
  C
        if (wang.gt.0d0) then
-         call ebend(ebe)
+         call ebend(ebe,ethetacnstr)
        else
          ebe=0
+         ethetacnstr=0
        endif
  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
        else
          esccor=0.0d0
        endif
 +C      print *,"PRZED MULIt"
  c      print *,"Processor",myrank," computed Usccorr"
  C 
  C 12/1/95 Multi-body terms
@@@ -272,19 -261,6 +273,19 @@@ C  after the equilibration tim
           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
        energia(17)=estr
        energia(20)=Uconst+Uconst_back
        energia(21)=esccor
 -C      energia(22)=eliptrans (the energy for lipid transfere implemented in lipid branch)
 -C      energia(23)= ... (energy for AFM, steered molecular dynamics)
 +      energia(22)=eliptran
 +      energia(23)=Eafmforce
+       energia(24)=ethetacnstr
  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"
@@@ -419,23 -396,22 +421,26 @@@ cMS$ATTRIBUTES C ::  proc_pro
        estr=energia(17)
        Uconst=energia(20)
        esccor=energia(21)
 +      eliptran=energia(22)
 +      Eafmforce=energia(23)
+       ethetacnstr=energia(24)
 -
  #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+ethetacnstr
 +     & +wbond*estr+Uconst+wsccor*esccor+wliptran*eliptran+Eafmforce
++     & +ethetacnstr
  #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+ethetacnstr
 +     & +wbond*estr+Uconst+wsccor*esccor+wliptran*eliptran
 +     & +Eafmforce
++     & +ethetacnstr
  #endif
        energia(0)=etot
  c detecting NaNQ
@@@ -471,10 -447,9 +476,10 @@@ cMS$ATTRIBUTES C ::  proc_pro
  #endif
  #ifdef MPI
        include 'mpif.h'
 -      double precision gradbufc(3,maxres),gradbufx(3,maxres),
 -     &  glocbuf(4*maxres),gradbufc_sum(3,maxres),gloc_scbuf(3,maxres)
  #endif
 +      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'
@@@ -527,7 -502,7 +532,7 @@@ c      endd
        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))+
       &                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))+
       &                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
        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
@@@ -615,7 -584,7 +620,7 @@@ c      endd
        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
        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
        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
@@@ -673,7 -642,7 +678,7 @@@ c      endd
        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)+
       &                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)+
       &                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
@@@ -1013,17 -976,17 +1018,18 @@@ C--------------------------------------
        estr=energia(17)
        Uconst=energia(20)
        esccor=energia(21)
 +      eliptran=energia(22)
 +      Eafmforce=energia(23) 
+       ethetacnstr=energia(24)
  #ifdef SPLITELE
        write (iout,10) evdw,wsc,evdw2,wscp,ees,welec,evdw1,wvdwpp,
       &  estr,wbond,ebe,wang,
       &  escloc,wscloc,etors,wtor,etors_d,wtor_d,ehpb,wstrain,
       &  ecorr,wcorr,
       &  ecorr5,wcorr5,ecorr6,wcorr6,eel_loc,wel_loc,eello_turn3,wturn3,
--     &  eello_turn4,wturn4,eello_turn6,wturn6,esccor,wsccor,
-      &  edihcnstr,ebr*nss,
-      &  Uconst,eliptran,wliptran,Eafmforce,etot
 -     &  edihcnstr,
 -     &  ethetacnstr,ebr*nss,
 -     &  Uconst,etot
++     &  eello_turn4,wturn4,eello_turn6,wturn6,esccor,wsccro,edihcnstr,
++     &  ethetacnstr,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)'/
       & 'ETURN6=',1pE16.6,' WEIGHT=',1pD16.6,' (turns, 6th order)'/
       & 'ESCCOR=',1pE16.6,' WEIGHT=',1pD16.6,' (backbone-rotamer corr)'/
       & 'EDIHC= ',1pE16.6,' (dihedral angle constraints)'/
+      & 'ETHETC= ',1pE16.6,' (valence 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,
       &  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,eliptran,wliptran,Eafmforc,etot
 -     &  ethetacnstr,
 -     &  ebr*nss,Uconst,etot
++     &  ethetacnstr,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)'/
       & 'ETURN6=',1pE16.6,' WEIGHT=',1pD16.6,' (turns, 6th order)'/
       & 'ESCCOR=',1pE16.6,' WEIGHT=',1pD16.6,' (backbone-rotamer corr)'/
       & 'EDIHC= ',1pE16.6,' (dihedral angle constraints)'/
+      & 'ETHETC= ',1pE16.6,' (valence 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
@@@ -1137,14 -1098,13 +1146,14 @@@ C Change 12/1/95 to calculate four-bod
  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
              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)
@@@ -1416,18 -1375,17 +1425,18 @@@ C Calculate the angle-dependent terms o
              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,
        include 'COMMON.IOUNITS'
        include 'COMMON.CALC'
        include 'COMMON.CONTROL'
 +      include 'COMMON.SPLITELE'
        include 'COMMON.SBRIDGE'
        logical lprn
 -
 -c      write(iout,*) "Jestem w egb(evdw)"
 +      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.
        ind=0
 +C the loop over all 27 posible neigbours (for xshift=0,yshift=0,zshift=0
 +C we have the original box)
 +C      do xshift=-1,1
 +C      do yshift=-1,1
 +C      do zshift=-1,1
        do i=iatsc_s,iatsc_e
          itypi=iabs(itype(i))
          if (itypi.eq.ntyp1) cycle
          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
 +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
 +
          dxi=dc_norm(1,nres+i)
          dyi=dc_norm(2,nres+i)
          dzi=dc_norm(3,nres+i)
          do iint=1,nint_gr(i)
            do j=istart(i,iint),iend(i,iint)
              IF (dyn_ss_mask(i).and.dyn_ss_mask(j)) THEN
+ c              write(iout,*) "PRZED ZWYKLE", evdwij
                call dyn_ssbond_ene(i,j,evdwij)
+ c              write(iout,*) "PO ZWYKLE", evdwij
                evdw=evdw+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) 
+ C search over all next residues
+               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)
+ c               if(evdwij_przed_tri.ne.evdwij) then
+ c                 write (iout,*) "TRI:", evdwij, evdwij_przed_tri
+ 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',i,j,evdwij,'tss'
+               endif!dyn_ss_mask(k)
+              enddo! k
              ELSE
              ind=ind+1
              itypj=iabs(itype(j))
@@@ -1604,118 -1520,17 +1640,118 @@@ c           chip12=0.0D
  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)
 +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
 +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)
 +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)/sigma(itypi,itypj))
 +            sssgrad=sscagrad((1.0d0/rij)/sigma(itypi,itypj))
 +             
 +c            write (iout,'(a7,4f8.3)') 
 +c    &      "ssscale",sss,((1.0d0/rij)/sigma(itypi,itypj)),r_cut,rlamb
 +            if (sss.gt.0.0d0) then
  C Calculate angle-dependent terms of energy and contributions to their
  C derivatives.
              call sc_angular
@@@ -1736,24 -1551,18 +1772,24 @@@ cd     &        rij_shift,1.0D0/rij,sig
  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
 +            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,
@@@ -1770,32 -1579,17 +1806,32 @@@ C Calculate gradient components
              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/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
              ENDIF    ! dyn_ss            
            enddo      ! j
          enddo        ! iint
        enddo          ! i
 +C      enddo          ! zshift
 +C      enddo          ! yshift
 +C      enddo          ! xshift
  c      write (iout,*) "Number of loop steps in EGB:",ind
  cccc      energy_dec=.false.
        return
@@@ -1832,41 -1626,6 +1868,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)
@@@ -1903,74 -1662,9 +1939,74 @@@ c           chip12=0.0D
  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)
@@@ -1991,8 -1685,8 +2027,8 @@@ C I hate to put IF's in the loops, but 
  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
              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),
@@@ -2016,7 -1710,6 +2052,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
@@@ -2104,7 -1797,6 +2140,7 @@@ C--------------------------------------
        include 'COMMON.CALC'
        include 'COMMON.IOUNITS'
        double precision dcosom1(3),dcosom2(3)
 +cc      print *,'sss=',sss
        eom1=eps2der*eps2rt_om1-2.0D0*alf1*eps3der+sigder*sigsq_om1
        eom2=eps2der*eps2rt_om2+2.0D0*alf2*eps3der+sigder*sigsq_om2
        eom12=evdwij*eps1_om12+eps2der*eps2rt_om12
@@@ -2123,16 -1815,16 +2159,16 @@@ c      write (iout,*) "eom1",eom1," eom
          dcosom2(k)=rij*(dc_norm(k,nres+j)-om2*erij(k))
        enddo
        do k=1,3
 -        gg(k)=gg(k)+eom1*dcosom1(k)+eom2*dcosom2(k)
 +        gg(k)=(gg(k)+eom1*dcosom1(k)+eom2*dcosom2(k))*sss
        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
 -        gvdwx(k,j)=gvdwx(k,j)+gg(k)
 +     &            +eom1*(erij(k)-om1*dc_norm(k,nres+i)))*dsci_inv*sss
 +        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
 +     &            +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))
  c     &            +eom1*(erij(k)-om1*dc_norm(k,nres+i)))*dsci_inv
  c        write (iout,*)(eom12*(dc_norm(k,nres+i)-om12*dc_norm(k,nres+j))
@@@ -2147,8 -1839,8 +2183,8 @@@ cgrad          gvdwc(l,k)=gvdwc(l,k)+gg
  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
        include 'COMMON.VECTORS'
        include 'COMMON.FFIELD'
        dimension ggg(3)
 -cd      write(iout,*) 'In EELEC_soft_sphere'
 +C      write(iout,*) 'In EELEC_soft_sphere'
        ees=0.0D0
        evdw1=0.0D0
        eel_loc=0.0d0 
          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
          num_conti=0
  c        write (iout,*) 'i',i,' ielstart',ielstart(i),' ielend',ielend(i)
          do j=ielstart(i),ielend(i)
            dxj=dc(1,j)
            dyj=dc(2,j)
            dzj=dc(3,j)
 -          xj=c(1,j)+0.5D0*dxj-xmedi
 -          yj=c(2,j)+0.5D0*dyj-ymedi
 -          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
 +      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
            rij=xj*xj+yj*yj+zj*zj
 +            sss=sscale(sqrt(rij))
 +            sssgrad=sscagrad(sqrt(rij))
            if (rij.lt.r0ijsq) then
              evdw1ij=0.25d0*(rij-r0ijsq)**2
              fac=rij-r0ijsq
              evdw1ij=0.0d0
              fac=0.0d0
            endif
 -          evdw1=evdw1+evdw1ij
 +          evdw1=evdw1+evdw1ij*sss
  C
  C Calculate contributions to the Cartesian gradient.
  C
 -          ggg(1)=fac*xj
 -          ggg(2)=fac*yj
 -          ggg(3)=fac*zj
 +          ggg(1)=fac*xj*sssgrad
 +          ggg(2)=fac*yj*sssgrad
 +          ggg(3)=fac*zj*sssgrad
            do k=1,3
              gvdwpp(k,i)=gvdwpp(k,i)-ggg(k)
              gvdwpp(k,j)=gvdwpp(k,j)+ggg(k)
  C Compute the virtual-bond-torsional-angle dependent quantities needed
  C to calculate the el-loc multibody terms of various order.
  C
 +c      write(iout,*) 'nphi=',nphi,nres
 +#ifdef PARMAT
 +      do i=ivec_start+2,ivec_end+2
 +#else
 +      do i=3,nres+1
 +#endif
 +#ifdef NEWCORR
 +        if (i.gt. nnt+2 .and. i.lt.nct+2) then
 +          iti = itortyp(itype(i-2))
 +        else
 +          iti=ntortyp+1
 +        endif
 +c        if (i.gt. iatel_s+1 .and. i.lt.iatel_e+4) then
 +        if (i.gt. nnt+1 .and. i.lt.nct+1) then
 +          iti1 = itortyp(itype(i-1))
 +        else
 +          iti1=ntortyp+1
 +        endif
 +c        write(iout,*),i
 +        b1(1,i-2)=bnew1(1,1,iti)*dsin(theta(i-1)/2.0)
 +     &           +bnew1(2,1,iti)*dsin(theta(i-1))
 +     &           +bnew1(3,1,iti)*dcos(theta(i-1)/2.0)
 +        gtb1(1,i-2)=bnew1(1,1,iti)*dcos(theta(i-1)/2.0d0)/2.0d0
 +     &             +bnew1(2,1,iti)*dcos(theta(i-1))
 +     &             -bnew1(3,1,iti)*dsin(theta(i-1)/2.0d0)/2.0d0
 +c     &           +bnew1(3,1,iti)*sin(alpha(i))*cos(beta(i))
 +c     &*(cos(theta(i)/2.0)
 +        b2(1,i-2)=bnew2(1,1,iti)*dsin(theta(i-1)/2.0)
 +     &           +bnew2(2,1,iti)*dsin(theta(i-1))
 +     &           +bnew2(3,1,iti)*dcos(theta(i-1)/2.0)
 +c     &           +bnew2(3,1,iti)*sin(alpha(i))*cos(beta(i))
 +c     &*(cos(theta(i)/2.0)
 +        gtb2(1,i-2)=bnew2(1,1,iti)*dcos(theta(i-1)/2.0d0)/2.0d0
 +     &             +bnew2(2,1,iti)*dcos(theta(i-1))
 +     &             -bnew2(3,1,iti)*dsin(theta(i-1)/2.0d0)/2.0d0
 +c        if (ggb1(1,i).eq.0.0d0) then
 +c        write(iout,*) 'i=',i,ggb1(1,i),
 +c     &bnew1(1,1,iti)*cos(theta(i)/2.0)/2.0,
 +c     &bnew1(2,1,iti)*cos(theta(i)),
 +c     &bnew1(3,1,iti)*sin(theta(i)/2.0)/2.0
 +c        endif
 +        b1(2,i-2)=bnew1(1,2,iti)
 +        gtb1(2,i-2)=0.0
 +        b2(2,i-2)=bnew2(1,2,iti)
 +        gtb2(2,i-2)=0.0
 +        EE(1,1,i-2)=eenew(1,iti)*dcos(theta(i-1))
 +        EE(1,2,i-2)=eeold(1,2,iti)
 +        EE(2,1,i-2)=eeold(2,1,iti)
 +        EE(2,2,i-2)=eeold(2,2,iti)
 +        gtEE(1,1,i-2)=-eenew(1,iti)*dsin(theta(i-1))
 +        gtEE(1,2,i-2)=0.0d0
 +        gtEE(2,2,i-2)=0.0d0
 +        gtEE(2,1,i-2)=0.0d0
 +c        EE(2,2,iti)=0.0d0
 +c        EE(1,2,iti)=0.5d0*eenew(1,iti)
 +c        EE(2,1,iti)=0.5d0*eenew(1,iti)
 +c        b1(2,iti)=bnew1(1,2,iti)*sin(alpha(i))*sin(beta(i))
 +c        b2(2,iti)=bnew2(1,2,iti)*sin(alpha(i))*sin(beta(i))
 +       b1tilde(1,i-2)=b1(1,i-2)
 +       b1tilde(2,i-2)=-b1(2,i-2)
 +       b2tilde(1,i-2)=b2(1,i-2)
 +       b2tilde(2,i-2)=-b2(2,i-2)
 +c       write (iout,*) 'i=',i-2,gtb1(2,i-2),gtb1(1,i-2)
 +c       write(iout,*)  'b1=',b1(1,i-2)
 +c       write (iout,*) 'theta=', theta(i-1)
 +       enddo
 +#else
 +        b1(1,i-2)=b(3,iti)
 +        b1(2,i-2)=b(5,iti)
 +        b2(1,i-2)=b(2,iti)
 +        b2(2,i-2)=b(4,iti)
 +       b1tilde(1,i-2)=b1(1,i-2)
 +       b1tilde(2,i-2)=-b1(2,i-2)
 +       b2tilde(1,i-2)=b2(1,i-2)
 +       b2tilde(2,i-2)=-b2(2,i-2)
 +        EE(1,2,i-2)=eeold(1,2,iti)
 +        EE(2,1,i-2)=eeold(2,1,iti)
 +        EE(2,2,i-2)=eeold(2,2,iti)
 +        EE(1,1,i-2)=eeold(1,1,iti)
 +      enddo
 +#endif
  #ifdef PARMAT
        do i=ivec_start+2,ivec_end+2
  #else
@@@ -2815,13 -2381,13 +2851,13 @@@ c        if (i.gt. iatel_s+2 .and. i.lt
          if (i.gt. nnt+2 .and. i.lt.nct+2) then
            iti = itortyp(itype(i-2))
          else
 -          iti=ntortyp+1
 +          iti=ntortyp
          endif
  c        if (i.gt. iatel_s+1 .and. i.lt.iatel_e+4) then
          if (i.gt. nnt+1 .and. i.lt.nct+1) then
            iti1 = itortyp(itype(i-1))
          else
 -          iti1=ntortyp+1
 +          iti1=ntortyp
          endif
  cd        write (iout,*) '*******i',i,' iti1',iti
  cd        write (iout,*) 'b1',b1(:,iti)
@@@ -2829,18 -2395,8 +2865,18 @@@ cd        write (iout,*) 'b2',b2(:,iti
  cd        write (iout,*) 'Ug',Ug(:,:,i-2)
  c        if (i .gt. iatel_s+2) then
          if (i .gt. nnt+2) then
 -          call matvec2(Ug(1,1,i-2),b2(1,iti),Ub2(1,i-2))
 -          call matmat2(EE(1,1,iti),Ug(1,1,i-2),EUg(1,1,i-2))
 +          call matvec2(Ug(1,1,i-2),b2(1,i-2),Ub2(1,i-2))
 +#ifdef NEWCORR
 +          call matvec2(Ug(1,1,i-2),gtb2(1,i-2),gUb2(1,i-2))
 +c          write (iout,*) Ug(1,1,i-2),gtb2(1,i-2),gUb2(1,i-2),"chuj"
 +#endif
 +c          write(iout,*) "co jest kurwa", iti, EE(1,1,iti),EE(2,1,iti),
 +c     &    EE(1,2,iti),EE(2,2,iti)
 +          call matmat2(EE(1,1,i-2),Ug(1,1,i-2),EUg(1,1,i-2))
 +          call matmat2(gtEE(1,1,i-2),Ug(1,1,i-2),gtEUg(1,1,i-2))
 +c          write(iout,*) "Macierz EUG",
 +c     &    eug(1,1,i-2),eug(1,2,i-2),eug(2,1,i-2),
 +c     &    eug(2,2,i-2)
            if (wcorr4.gt.0.0d0 .or. wcorr5.gt.0.0d0 .or. wcorr6.gt.0.0d0) 
       &    then
            call matmat2(CC(1,1,iti),Ug(1,1,i-2),CUg(1,1,i-2))
              enddo
            enddo
          endif
 -        call matvec2(Ugder(1,1,i-2),b2(1,iti),Ub2der(1,i-2))
 -        call matmat2(EE(1,1,iti),Ugder(1,1,i-2),EUgder(1,1,i-2))
 +        call matvec2(Ugder(1,1,i-2),b2(1,i-2),Ub2der(1,i-2))
 +        call matmat2(EE(1,1,i-2),Ugder(1,1,i-2),EUgder(1,1,i-2))
          do k=1,2
            muder(k,i-2)=Ub2der(k,i-2)
          enddo
@@@ -2872,15 -2428,15 +2908,15 @@@ c        if (i.gt. iatel_s+1 .and. i.lt
            if (itype(i-1).le.ntyp) then
              iti1 = itortyp(itype(i-1))
            else
 -            iti1=ntortyp+1
 +            iti1=ntortyp
            endif
          else
 -          iti1=ntortyp+1
 +          iti1=ntortyp
          endif
          do k=1,2
 -          mu(k,i-2)=Ub2(k,i-2)+b1(k,iti1)
 +          mu(k,i-2)=Ub2(k,i-2)+b1(k,i-1)
          enddo
 -cd        write (iout,*) 'mu ',mu(:,i-2)
 +c        write (iout,*) 'mu ',mu(:,i-2),i-2
  cd        write (iout,*) 'mu1',mu1(:,i-2)
  cd        write (iout,*) 'mu2',mu2(:,i-2)
          if (wcorr4.gt.0.0d0 .or. wcorr5.gt.0.0d0 .or.wcorr6.gt.0.0d0)
          call matvec2(Ctilde(1,1,iti1),obrot_der(1,i-2),Ctobrder(1,i-2))
          call matvec2(Dtilde(1,1,iti),obrot2_der(1,i-2),Dtobr2der(1,i-2))
  C Vectors and matrices dependent on a single virtual-bond dihedral.
 -        call matvec2(DD(1,1,iti),b1tilde(1,iti1),auxvec(1))
 +        call matvec2(DD(1,1,iti),b1tilde(1,i-1),auxvec(1))
          call matvec2(Ug2(1,1,i-2),auxvec(1),Ug2Db1t(1,i-2)) 
          call matvec2(Ug2der(1,1,i-2),auxvec(1),Ug2Db1tder(1,i-2)) 
          call matvec2(CC(1,1,iti1),Ub2(1,i-2),CUgb2(1,i-2))
        include 'COMMON.VECTORS'
        include 'COMMON.FFIELD'
        include 'COMMON.TIME1'
 +      include 'COMMON.SPLITELE'
        dimension ggg(3),gggp(3),gggm(3),erij(3),dcosb(3),dcosg(3),
       &          erder(3,3),uryg(3,3),urzg(3,3),vryg(3,3),vrzg(3,3)
        double precision acipa(2,2),agg(3,4),aggi(3,4),aggi1(3,4),
 -     &    aggj(3,4),aggj1(3,4),a_temp(2,2),muij(4)
 +     &    aggj(3,4),aggj1(3,4),a_temp(2,2),muij(4),gmuij(4)
        common /locel/ a_temp,agg,aggi,aggi1,aggj,aggj1,a22,a23,a32,a33,
       &    dxi,dyi,dzi,dx_normi,dy_normi,dz_normi,xmedi,ymedi,zmedi,
       &    num_conti,j1,j2
@@@ -3287,23 -2842,9 +3323,23 @@@ c 9/27/08 AL Split the interaction loo
  C
  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
 -     &  .or. itype(i+2).eq.ntyp1 .or. itype(i+3).eq.ntyp1) cycle
 +C changes suggested by Ana to avoid out of bounds
 +     & .or.((i+4).gt.nres)
 +     & .or.((i-1).le.0)
 +C end of changes by Ana
 +     &  .or. itype(i+2).eq.ntyp1
 +     &  .or. itype(i+3).eq.ntyp1) cycle
 +        if(i.gt.1)then
 +          if(itype(i-1).eq.ntyp1)cycle
 +        end if
 +        if(i.LT.nres-3)then
 +          if (itype(i+4).eq.ntyp1) cycle
 +        end if
          dxi=dc(1,i)
          dyi=dc(2,i)
          dzi=dc(3,i)
          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
          num_conti=0
          call eelecij(i,i+2,ees,evdw1,eel_loc)
          if (wturn3.gt.0.0d0) call eturn3(i,eello_turn3)
          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)
 +     & .or.((i-1).le.0)
 +C end of changes suggested by Ana
       &    .or. itype(i+3).eq.ntyp1
 -     &    .or. itype(i+4).eq.ntyp1) cycle
 +     &    .or. itype(i+4).eq.ntyp1
 +     &    .or. itype(i+5).eq.ntyp1
 +     &    .or. itype(i).eq.ntyp1
 +     &    .or. itype(i-1).eq.ntyp1
 +     &                             ) cycle
          dxi=dc(1,i)
          dyi=dc(2,i)
          dzi=dc(3,i)
          xmedi=c(1,i)+0.5d0*dxi
          ymedi=c(2,i)+0.5d0*dyi
          zmedi=c(3,i)+0.5d0*dzi
 +C Return atom into box, boxxsize is size of box in x dimension
 +c  194   continue
 +c        if (xmedi.gt.((0.5d0)*boxxsize)) xmedi=xmedi-boxxsize
 +c        if (xmedi.lt.((-0.5d0)*boxxsize)) xmedi=xmedi+boxxsize
 +C Condition for being inside the proper box
 +c        if ((xmedi.gt.((0.5d0)*boxxsize)).or.
 +c     &       (xmedi.lt.((-0.5d0)*boxxsize))) then
 +c        go to 194
 +c        endif
 +c  195   continue
 +c        if (ymedi.gt.((0.5d0)*boxysize)) ymedi=ymedi-boxysize
 +c        if (ymedi.lt.((-0.5d0)*boxysize)) ymedi=ymedi+boxysize
 +C Condition for being inside the proper box
 +c        if ((ymedi.gt.((0.5d0)*boxysize)).or.
 +c     &       (ymedi.lt.((-0.5d0)*boxysize))) then
 +c        go to 195
 +c        endif
 +c  196   continue
 +c        if (zmedi.gt.((0.5d0)*boxzsize)) zmedi=zmedi-boxzsize
 +c        if (zmedi.lt.((-0.5d0)*boxzsize)) zmedi=zmedi+boxzsize
 +C Condition for being inside the proper box
 +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
 +
          num_conti=num_cont_hb(i)
 +c        write(iout,*) "JESTEM W PETLI"
          call eelecij(i,i+3,ees,evdw1,eel_loc)
          if (wturn4.gt.0.0d0 .and. itype(i+2).ne.ntyp1) 
       &   call eturn4(i,eello_turn4)
          num_cont_hb(i)=num_conti
        enddo   ! i
 +C Loop over all neighbouring boxes
 +C      do xshift=-1,1
 +C      do yshift=-1,1
 +C      do zshift=-1,1
  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 (itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1) cycle
 +        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)
 +     & .or.((i-1).le.0)
 +C end of changes by Ana
 +     &  .or. itype(i+2).eq.ntyp1
 +     &  .or. itype(i-1).eq.ntyp1
 +     &                ) cycle
          dxi=dc(1,i)
          dyi=dc(2,i)
          dzi=dc(3,i)
          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
 +C          xmedi=xmedi+xshift*boxxsize
 +C          ymedi=ymedi+yshift*boxysize
 +C          zmedi=zmedi+zshift*boxzsize
 +
 +C Return tom into box, boxxsize is size of box in x dimension
 +c  164   continue
 +c        if (xmedi.gt.((xshift+0.5d0)*boxxsize)) xmedi=xmedi-boxxsize
 +c        if (xmedi.lt.((xshift-0.5d0)*boxxsize)) xmedi=xmedi+boxxsize
 +C Condition for being inside the proper box
 +c        if ((xmedi.gt.((xshift+0.5d0)*boxxsize)).or.
 +c     &       (xmedi.lt.((xshift-0.5d0)*boxxsize))) then
 +c        go to 164
 +c        endif
 +c  165   continue
 +c        if (ymedi.gt.((yshift+0.5d0)*boxysize)) ymedi=ymedi-boxysize
 +c        if (ymedi.lt.((yshift-0.5d0)*boxysize)) ymedi=ymedi+boxysize
 +C Condition for being inside the proper box
 +c        if ((ymedi.gt.((yshift+0.5d0)*boxysize)).or.
 +c     &       (ymedi.lt.((yshift-0.5d0)*boxysize))) then
 +c        go to 165
 +c        endif
 +c  166   continue
 +c        if (zmedi.gt.((zshift+0.5d0)*boxzsize)) zmedi=zmedi-boxzsize
 +c        if (zmedi.lt.((zshift-0.5d0)*boxzsize)) zmedi=zmedi+boxzsize
 +cC Condition for being inside the proper box
 +c        if ((zmedi.gt.((zshift+0.5d0)*boxzsize)).or.
 +c     &       (zmedi.lt.((zshift-0.5d0)*boxzsize))) then
 +c        go to 166
 +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)
 -          if (itype(j).eq.ntyp1.or. itype(j+1).eq.ntyp1) cycle
 +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)
 +     & .or.((j-1).le.0)
 +C end of changes by Ana
 +     & .or.itype(j+2).eq.ntyp1
 +     & .or.itype(j-1).eq.ntyp1
 +     &) cycle
            call eelecij(i,j,ees,evdw1,eel_loc)
          enddo ! j
          num_cont_hb(i)=num_conti
        enddo   ! i
 +C     enddo   ! zshift
 +C      enddo   ! yshift
 +C      enddo   ! xshift
 +
  c      write (iout,*) "Number of loop steps in EELEC:",ind
  cd      do i=1,nres
  cd        write (iout,'(i3,3f10.5,5x,3f10.5)') 
@@@ -3498,12 -2931,10 +3534,12 @@@ C--------------------------------------
        include 'COMMON.VECTORS'
        include 'COMMON.FFIELD'
        include 'COMMON.TIME1'
 +      include 'COMMON.SPLITELE'
        dimension ggg(3),gggp(3),gggm(3),erij(3),dcosb(3),dcosg(3),
       &          erder(3,3),uryg(3,3),urzg(3,3),vryg(3,3),vrzg(3,3)
        double precision acipa(2,2),agg(3,4),aggi(3,4),aggi1(3,4),
 -     &    aggj(3,4),aggj1(3,4),a_temp(2,2),muij(4)
 +     &    aggj(3,4),aggj1(3,4),a_temp(2,2),muij(4),gmuij1(4),gmuji1(4),
 +     &    gmuij2(4),gmuji2(4)
        common /locel/ a_temp,agg,aggi,aggi1,aggj,aggj1,a22,a23,a32,a33,
       &    dxi,dyi,dzi,dx_normi,dy_normi,dz_normi,xmedi,ymedi,zmedi,
       &    num_conti,j1,j2
@@@ -3534,84 -2965,10 +3570,84 @@@ c          ind=ind+
            dx_normj=dc_norm(1,j)
            dy_normj=dc_norm(2,j)
            dz_normj=dc_norm(3,j)
 -          xj=c(1,j)+0.5D0*dxj-xmedi
 -          yj=c(2,j)+0.5D0*dyj-ymedi
 -          zj=c(3,j)+0.5D0*dzj-zmedi
 +C          xj=c(1,j)+0.5D0*dxj-xmedi
 +C          yj=c(2,j)+0.5D0*dyj-ymedi
 +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
 +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(sqrt(rij))
 +            sssgrad=sscagrad(sqrt(rij))
 +c            if (sss.gt.0.0d0) then  
            rrmij=1.0D0/rij
            rij=dsqrt(rij)
            rmij=1.0D0/rij
@@@ -3627,15 -2984,14 +3663,15 @@@ c 4/26/02 - AL scaling down 1,4 repulsi
            ev2=bbb*r6ij
            fac3=ael6i*r6ij
            fac4=ael3i*r3ij
 -          evdwij=ev1+ev2
 +          evdwij=(ev1+ev2)
            el1=fac3*(4.0D0+fac*fac-3.0D0*(cosb*cosb+cosg*cosg))
            el2=fac4*fac       
 -          eesij=el1+el2
 +C MARYSIA
 +          eesij=(el1+el2)
  C 12/26/95 - for the evaluation of multi-body H-bonding interactions
            ees0ij=4.0D0+fac*fac-3.0D0*(cosb*cosb+cosg*cosg)
            ees=ees+eesij
 -          evdw1=evdw1+evdwij
 +          evdw1=evdw1+evdwij*sss
  cd          write(iout,'(2(2i3,2x),7(1pd12.4)/2(3(1pd12.4),5x)/)')
  cd     &      iteli,i,itelj,j,aaa,bbb,ael6i,ael3i,
  cd     &      1.0D0/dsqrt(rrmij),evdwij,eesij,
  C Calculate contributions to the Cartesian gradient.
  C
  #ifdef SPLITELE
 -          facvdw=-6*rrmij*(ev1+evdwij)
 +          facvdw=-6*rrmij*(ev1+evdwij)*sss
            facel=-3*rrmij*(el1+eesij)
            fac1=fac
            erij(1)=xj*rmij
@@@ -3682,15 -3038,9 +3718,15 @@@ cgrad            do l=1,
  cgrad              gelc(l,k)=gelc(l,k)+ggg(l)
  cgrad            enddo
  cgrad          enddo
 -          ggg(1)=facvdw*xj
 -          ggg(2)=facvdw*yj
 -          ggg(3)=facvdw*zj
 +          if (sss.gt.0.0) then
 +          ggg(1)=facvdw*xj+sssgrad*rmij*evdwij*xj
 +          ggg(2)=facvdw*yj+sssgrad*rmij*evdwij*yj
 +          ggg(3)=facvdw*zj+sssgrad*rmij*evdwij*zj
 +          else
 +          ggg(1)=0.0
 +          ggg(2)=0.0
 +          ggg(3)=0.0
 +          endif
  c          do k=1,3
  c            ghalf=0.5D0*ggg(k)
  c            gvdwpp(k,i)=gvdwpp(k,i)+ghalf
@@@ -3710,9 -3060,8 +3746,9 @@@ cgrad              gvdwpp(l,k)=gvdwpp(l
  cgrad            enddo
  cgrad          enddo
  #else
 -          facvdw=ev1+evdwij 
 -          facel=el1+eesij  
 +C MARYSIA
 +          facvdw=(ev1+evdwij)*sss
 +          facel=(el1+eesij)
            fac1=fac
            fac=-3*rrmij*(facvdw+facvdw+facel)
            erij(1)=xj*rmij
@@@ -3743,9 -3092,9 +3779,9 @@@ cgrad              gelc(l,k)=gelc(l,k)+
  cgrad            enddo
  cgrad          enddo
  c 9/28/08 AL Gradient compotents will be summed only at the end
 -          ggg(1)=facvdw*xj
 -          ggg(2)=facvdw*yj
 -          ggg(3)=facvdw*zj
 +          ggg(1)=facvdw*xj+sssgrad*rmij*evdwij*xj
 +          ggg(2)=facvdw*yj+sssgrad*rmij*evdwij*yj
 +          ggg(3)=facvdw*zj+sssgrad*rmij*evdwij*zj
            do k=1,3
              gvdwpp(k,j)=gvdwpp(k,j)+ggg(k)
              gvdwpp(k,i)=gvdwpp(k,i)-ggg(k)
@@@ -3784,16 -3133,14 +3820,16 @@@ cgrad            endd
  cgrad          enddo
            do k=1,3
              gelc(k,i)=gelc(k,i)
 -     &               +(ecosa*(dc_norm(k,j)-cosa*dc_norm(k,i))
 -     &               + ecosb*(erij(k)-cosb*dc_norm(k,i)))*vbld_inv(i+1)
 +     &           +(ecosa*(dc_norm(k,j)-cosa*dc_norm(k,i))
 +     &           + ecosb*(erij(k)-cosb*dc_norm(k,i)))*vbld_inv(i+1)
              gelc(k,j)=gelc(k,j)
 -     &               +(ecosa*(dc_norm(k,i)-cosa*dc_norm(k,j))
 -     &               + ecosg*(erij(k)-cosg*dc_norm(k,j)))*vbld_inv(j+1)
 +     &           +(ecosa*(dc_norm(k,i)-cosa*dc_norm(k,j))
 +     &           + ecosg*(erij(k)-cosg*dc_norm(k,j)))*vbld_inv(j+1)
              gelc_long(k,j)=gelc_long(k,j)+ggg(k)
              gelc_long(k,i)=gelc_long(k,i)-ggg(k)
            enddo
 +C MARYSIA
 +c          endif !sscale
            IF (wel_loc.gt.0.0d0 .or. wcorr4.gt.0.0d0 .or. wcorr5.gt.0.0d0
       &        .or. wcorr6.gt.0.0d0 .or. wturn3.gt.0.0d0 
       &        .or. wturn4.gt.0.0d0 .or. wturn6.gt.0.0d0) THEN
@@@ -3804,7 -3151,6 +3840,7 @@@ C   Fourier series in the angles lambda
  C   Macromolecules, 1974, 7, 797-806 for definition). This correlation terms
  C   are computed for EVERY pair of non-contiguous peptide groups.
  C
 +
            if (j.lt.nres-1) then
              j1=j+1
              j2=j-1
              j2=j-2
            endif
            kkk=0
 +          lll=0
            do k=1,2
              do l=1,2
                kkk=kkk+1
                muij(kkk)=mu(k,i)*mu(l,j)
 +c              write(iout,*) 'mumu=', mu(k,i),mu(l,j),i,j,k,l
 +#ifdef NEWCORR
 +             gmuij1(kkk)=gtb1(k,i+1)*mu(l,j)
 +c             write(iout,*) 'k=',k,i,gtb1(k,i+1),gtb1(k,i+1)*mu(l,j)
 +             gmuij2(kkk)=gUb2(k,i)*mu(l,j)
 +             gmuji1(kkk)=mu(k,i)*gtb1(l,j+1)
 +c             write(iout,*) 'l=',l,j,gtb1(l,j+1),gtb1(l,j+1)*mu(k,i)
 +             gmuji2(kkk)=mu(k,i)*gUb2(l,j)
 +#endif
              enddo
            enddo  
  cd         write (iout,*) 'EELEC: i',i,' j',j
@@@ -3992,59 -3328,11 +4028,59 @@@ cgrad            endi
  C Contribution to the local-electrostatic energy coming from the i-j pair
            eel_loc_ij=a22*muij(1)+a23*muij(2)+a32*muij(3)
       &     +a33*muij(4)
 +c          write (iout,*) 'i',i,' j',j,itype(i),itype(j),
 +c     &                     ' eel_loc_ij',eel_loc_ij
 +c          write(iout,*) 'muije=',muij(1),muij(2),muij(3),muij(4)
 +C Calculate patrial derivative for theta angle
 +#ifdef NEWCORR
 +         geel_loc_ij=a22*gmuij1(1)
 +     &     +a23*gmuij1(2)
 +     &     +a32*gmuij1(3)
 +     &     +a33*gmuij1(4)         
 +c         write(iout,*) "derivative over thatai"
 +c         write(iout,*) a22*gmuij1(1), a23*gmuij1(2) ,a32*gmuij1(3),
 +c     &   a33*gmuij1(4) 
 +         gloc(nphi+i,icg)=gloc(nphi+i,icg)+
 +     &      geel_loc_ij*wel_loc
 +c         write(iout,*) "derivative over thatai-1" 
 +c         write(iout,*) a22*gmuij2(1), a23*gmuij2(2) ,a32*gmuij2(3),
 +c     &   a33*gmuij2(4)
 +         geel_loc_ij=
 +     &     a22*gmuij2(1)
 +     &     +a23*gmuij2(2)
 +     &     +a32*gmuij2(3)
 +     &     +a33*gmuij2(4)
 +         gloc(nphi+i-1,icg)=gloc(nphi+i-1,icg)+
 +     &      geel_loc_ij*wel_loc
 +c  Derivative over j residue
 +         geel_loc_ji=a22*gmuji1(1)
 +     &     +a23*gmuji1(2)
 +     &     +a32*gmuji1(3)
 +     &     +a33*gmuji1(4)
 +c         write(iout,*) "derivative over thataj" 
 +c         write(iout,*) a22*gmuji1(1), a23*gmuji1(2) ,a32*gmuji1(3),
 +c     &   a33*gmuji1(4)
 +
 +        gloc(nphi+j,icg)=gloc(nphi+j,icg)+
 +     &      geel_loc_ji*wel_loc
 +         geel_loc_ji=
 +     &     +a22*gmuji2(1)
 +     &     +a23*gmuji2(2)
 +     &     +a32*gmuji2(3)
 +     &     +a33*gmuji2(4)
 +c         write(iout,*) "derivative over thataj-1"
 +c         write(iout,*) a22*gmuji2(1), a23*gmuji2(2) ,a32*gmuji2(3),
 +c     &   a33*gmuji2(4)
 +         gloc(nphi+j-1,icg)=gloc(nphi+j-1,icg)+
 +     &      geel_loc_ji*wel_loc
 +#endif
  cd          write (iout,*) 'i',i,' j',j,' eel_loc_ij',eel_loc_ij
  
            if (energy_dec) write (iout,'(a6,2i5,0pf7.3)')
       &            'eelloc',i,j,eel_loc_ij
 -c              write (iout,*) a22,muij(1),a23,muij(2),a32,muij(3)
 +c           if (eel_loc_ij.ne.0)
 +c     &      write (iout,'(a4,2i4,8f9.5)')'chuj',
 +c     &     i,j,a22,muij(1),a23,muij(2),a32,muij(3),a33,muij(4)
  
            eel_loc=eel_loc+eel_loc_ij
  C Partial derivatives in virtual-bond dihedral angles gamma
@@@ -4072,14 -3360,14 +4108,14 @@@ cgrad            endd
  cgrad          enddo
  C Remaining derivatives of eello
            do l=1,3
 -            gel_loc(l,i)=gel_loc(l,i)+aggi(l,1)*muij(1)+
 -     &          aggi(l,2)*muij(2)+aggi(l,3)*muij(3)+aggi(l,4)*muij(4)
 -            gel_loc(l,i+1)=gel_loc(l,i+1)+aggi1(l,1)*muij(1)+
 -     &          aggi1(l,2)*muij(2)+aggi1(l,3)*muij(3)+aggi1(l,4)*muij(4)
 -            gel_loc(l,j)=gel_loc(l,j)+aggj(l,1)*muij(1)+
 -     &          aggj(l,2)*muij(2)+aggj(l,3)*muij(3)+aggj(l,4)*muij(4)
 -            gel_loc(l,j1)=gel_loc(l,j1)+aggj1(l,1)*muij(1)+
 -     &          aggj1(l,2)*muij(2)+aggj1(l,3)*muij(3)+aggj1(l,4)*muij(4)
 +            gel_loc(l,i)=gel_loc(l,i)+(aggi(l,1)*muij(1)+
 +     &        aggi(l,2)*muij(2)+aggi(l,3)*muij(3)+aggi(l,4)*muij(4))
 +            gel_loc(l,i+1)=gel_loc(l,i+1)+(aggi1(l,1)*muij(1)+
 +     &     aggi1(l,2)*muij(2)+aggi1(l,3)*muij(3)+aggi1(l,4)*muij(4))
 +            gel_loc(l,j)=gel_loc(l,j)+(aggj(l,1)*muij(1)+
 +     &       aggj(l,2)*muij(2)+aggj(l,3)*muij(3)+aggj(l,4)*muij(4))
 +            gel_loc(l,j1)=gel_loc(l,j1)+(aggj1(l,1)*muij(1)+
 +     &     aggj1(l,2)*muij(2)+aggj1(l,3)*muij(3)+aggj1(l,4)*muij(4))
            enddo
            ENDIF
  C Change 12/26/95 to calculate four-body contributions to H-bonding energy
@@@ -4292,9 -3580,7 +4328,9 @@@ C Third- and fourth-order contribution
        dimension ggg(3)
        double precision auxmat(2,2),auxmat1(2,2),auxmat2(2,2),pizda(2,2),
       &  e1t(2,2),e2t(2,2),e3t(2,2),e1tder(2,2),e2tder(2,2),e3tder(2,2),
 -     &  e1a(2,2),ae3(2,2),ae3e2(2,2),auxvec(2),auxvec1(2)
 +     &  e1a(2,2),ae3(2,2),ae3e2(2,2),auxvec(2),auxvec1(2),gpizda1(2,2),
 +     &  gpizda2(2,2),auxgmat1(2,2),auxgmatt1(2,2),
 +     &  auxgmat2(2,2),auxgmatt2(2,2)
        double precision agg(3,4),aggi(3,4),aggi1(3,4),
       &    aggj(3,4),aggj1(3,4),a_temp(2,2),auxmat3(2,2)
        common /locel/ a_temp,agg,aggi,aggi1,aggj,aggj1,a22,a23,a32,a33,
  CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC   
  cd        call checkint_turn3(i,a_temp,eello_turn3_num)
          call matmat2(EUg(1,1,i+1),EUg(1,1,i+2),auxmat(1,1))
 +c auxalary matices for theta gradient
 +c auxalary matrix for i+1 and constant i+2
 +        call matmat2(gtEUg(1,1,i+1),EUg(1,1,i+2),auxgmat1(1,1))
 +c auxalary matrix for i+2 and constant i+1
 +        call matmat2(EUg(1,1,i+1),gtEUg(1,1,i+2),auxgmat2(1,1))
          call transpose2(auxmat(1,1),auxmat1(1,1))
 +        call transpose2(auxgmat1(1,1),auxgmatt1(1,1))
 +        call transpose2(auxgmat2(1,1),auxgmatt2(1,1))
          call matmat2(a_temp(1,1),auxmat1(1,1),pizda(1,1))
 +        call matmat2(a_temp(1,1),auxgmatt1(1,1),gpizda1(1,1))
 +        call matmat2(a_temp(1,1),auxgmatt2(1,1),gpizda2(1,1))
          eello_turn3=eello_turn3+0.5d0*(pizda(1,1)+pizda(2,2))
 +C Derivatives in theta
 +        gloc(nphi+i,icg)=gloc(nphi+i,icg)
 +     &  +0.5d0*(gpizda1(1,1)+gpizda1(2,2))*wturn3
 +        gloc(nphi+i+1,icg)=gloc(nphi+i+1,icg)
 +     &  +0.5d0*(gpizda2(1,1)+gpizda2(2,2))*wturn3
 +
          if (energy_dec) write (iout,'(a6,2i5,0pf7.3)')
       &          'eturn3',i,j,0.5d0*(pizda(1,1)+pizda(2,2))
  cd        write (2,*) 'i,',i,' j',j,'eello_turn3',
@@@ -4409,11 -3680,7 +4445,11 @@@ C Third- and fourth-order contribution
        dimension ggg(3)
        double precision auxmat(2,2),auxmat1(2,2),auxmat2(2,2),pizda(2,2),
       &  e1t(2,2),e2t(2,2),e3t(2,2),e1tder(2,2),e2tder(2,2),e3tder(2,2),
 -     &  e1a(2,2),ae3(2,2),ae3e2(2,2),auxvec(2),auxvec1(2)
 +     &  e1a(2,2),ae3(2,2),ae3e2(2,2),auxvec(2),auxvec1(2),auxgvec(2),
 +     &  auxgEvec1(2),auxgEvec2(2),auxgEvec3(2),
 +     &  gte1t(2,2),gte2t(2,2),gte3t(2,2),
 +     &  gte1a(2,2),gtae3(2,2),gtae3e2(2,2), ae3gte2(2,2),
 +     &  gtEpizda1(2,2),gtEpizda2(2,2),gtEpizda3(2,2)
        double precision agg(3,4),aggi(3,4),aggi1(3,4),
       &    aggj(3,4),aggj1(3,4),a_temp(2,2),auxmat3(2,2)
        common /locel/ a_temp,agg,aggi,aggi1,aggj,aggj1,a22,a23,a32,a33,
  CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC   
  cd        call checkint_turn4(i,a_temp,eello_turn4_num)
  c        write (iout,*) "eturn4 i",i," j",j," j1",j1," j2",j2
 +c        write(iout,*)"WCHODZE W PROGRAM"
          a_temp(1,1)=a22
          a_temp(1,2)=a23
          a_temp(2,1)=a32
@@@ -4445,100 -3711,32 +4481,100 @@@ c        write(iout,*) "iti1",iti1," it
          call transpose2(EUg(1,1,i+1),e1t(1,1))
          call transpose2(Eug(1,1,i+2),e2t(1,1))
          call transpose2(Eug(1,1,i+3),e3t(1,1))
 +C Ematrix derivative in theta
 +        call transpose2(gtEUg(1,1,i+1),gte1t(1,1))
 +        call transpose2(gtEug(1,1,i+2),gte2t(1,1))
 +        call transpose2(gtEug(1,1,i+3),gte3t(1,1))
          call matmat2(e1t(1,1),a_temp(1,1),e1a(1,1))
 +c       eta1 in derivative theta
 +        call matmat2(gte1t(1,1),a_temp(1,1),gte1a(1,1))
          call matvec2(e1a(1,1),Ub2(1,i+3),auxvec(1))
 -        s1=scalar2(b1(1,iti2),auxvec(1))
 +c       auxgvec is derivative of Ub2 so i+3 theta
 +        call matvec2(e1a(1,1),gUb2(1,i+3),auxgvec(1)) 
 +c       auxalary matrix of E i+1
 +        call matvec2(gte1a(1,1),Ub2(1,i+3),auxgEvec1(1))
 +c        s1=0.0
 +c        gs1=0.0    
 +        s1=scalar2(b1(1,i+2),auxvec(1))
 +c derivative of theta i+2 with constant i+3
 +        gs23=scalar2(gtb1(1,i+2),auxvec(1))
 +c derivative of theta i+2 with constant i+2
 +        gs32=scalar2(b1(1,i+2),auxgvec(1))
 +c derivative of E matix in theta of i+1
 +        gsE13=scalar2(b1(1,i+2),auxgEvec1(1))
 +
          call matmat2(a_temp(1,1),e3t(1,1),ae3(1,1))
 +c       ea31 in derivative theta
 +        call matmat2(a_temp(1,1),gte3t(1,1),gtae3(1,1))
          call matvec2(ae3(1,1),Ub2(1,i+2),auxvec(1)) 
 -        s2=scalar2(b1(1,iti1),auxvec(1))
 +c auxilary matrix auxgvec of Ub2 with constant E matirx
 +        call matvec2(ae3(1,1),gUb2(1,i+2),auxgvec(1))
 +c auxilary matrix auxgEvec1 of E matix with Ub2 constant
 +        call matvec2(gtae3(1,1),Ub2(1,i+2),auxgEvec3(1))
 +
 +c        s2=0.0
 +c        gs2=0.0
 +        s2=scalar2(b1(1,i+1),auxvec(1))
 +c derivative of theta i+1 with constant i+3
 +        gs13=scalar2(gtb1(1,i+1),auxvec(1))
 +c derivative of theta i+2 with constant i+1
 +        gs21=scalar2(b1(1,i+1),auxgvec(1))
 +c derivative of theta i+3 with constant i+1
 +        gsE31=scalar2(b1(1,i+1),auxgEvec3(1))
 +c        write(iout,*) gs1,gs2,'i=',i,auxgvec(1),gUb2(1,i+2),gtb1(1,i+2),
 +c     &  gtb1(1,i+1)
          call matmat2(ae3(1,1),e2t(1,1),ae3e2(1,1))
 +c two derivatives over diffetent matrices
 +c gtae3e2 is derivative over i+3
 +        call matmat2(gtae3(1,1),e2t(1,1),gtae3e2(1,1))
 +c ae3gte2 is derivative over i+2
 +        call matmat2(ae3(1,1),gte2t(1,1),ae3gte2(1,1))
          call matmat2(ae3e2(1,1),e1t(1,1),pizda(1,1))
 +c three possible derivative over theta E matices
 +c i+1
 +        call matmat2(ae3e2(1,1),gte1t(1,1),gtEpizda1(1,1))
 +c i+2
 +        call matmat2(ae3gte2(1,1),e1t(1,1),gtEpizda2(1,1))
 +c i+3
 +        call matmat2(gtae3e2(1,1),e1t(1,1),gtEpizda3(1,1))
          s3=0.5d0*(pizda(1,1)+pizda(2,2))
 +
 +        gsEE1=0.5d0*(gtEpizda1(1,1)+gtEpizda1(2,2))
 +        gsEE2=0.5d0*(gtEpizda2(1,1)+gtEpizda2(2,2))
 +        gsEE3=0.5d0*(gtEpizda3(1,1)+gtEpizda3(2,2))
 +
          eello_turn4=eello_turn4-(s1+s2+s3)
 -        if (energy_dec) write (iout,'(a6,2i5,0pf7.3)')
 -     &      'eturn4',i,j,-(s1+s2+s3)
 +c             write(iout,*)'chujOWO', auxvec(1),b1(1,iti2)
 +        if (energy_dec) write (iout,'(a6,2i5,0pf7.3,3f7.3)')
 +     &      'eturn4',i,j,-(s1+s2+s3),s1,s2,s3
  cd        write (2,*) 'i,',i,' j',j,'eello_turn4',-(s1+s2+s3),
  cd     &    ' eello_turn4_num',8*eello_turn4_num
 +#ifdef NEWCORR
 +        gloc(nphi+i,icg)=gloc(nphi+i,icg)
 +     &                  -(gs13+gsE13+gsEE1)*wturn4
 +        gloc(nphi+i+1,icg)= gloc(nphi+i+1,icg)
 +     &                    -(gs23+gs21+gsEE2)*wturn4
 +        gloc(nphi+i+2,icg)= gloc(nphi+i+2,icg)
 +     &                    -(gs32+gsE31+gsEE3)*wturn4
 +c         gloc(nphi+i+1,icg)=gloc(nphi+i+1,icg)-
 +c     &   gs2
 +#endif
 +        if (energy_dec) write (iout,'(a6,2i5,0pf7.3)')
 +     &      'eturn4',i,j,-(s1+s2+s3)
 +c        write (iout,*) 'i,',i,' j',j,'eello_turn4',-(s1+s2+s3),
 +c     &    ' eello_turn4_num',8*eello_turn4_num
  C Derivatives in gamma(i)
          call transpose2(EUgder(1,1,i+1),e1tder(1,1))
          call matmat2(e1tder(1,1),a_temp(1,1),auxmat(1,1))
          call matvec2(auxmat(1,1),Ub2(1,i+3),auxvec(1))
 -        s1=scalar2(b1(1,iti2),auxvec(1))
 +        s1=scalar2(b1(1,i+2),auxvec(1))
          call matmat2(ae3e2(1,1),e1tder(1,1),pizda(1,1))
          s3=0.5d0*(pizda(1,1)+pizda(2,2))
          gel_loc_turn4(i)=gel_loc_turn4(i)-(s1+s3)
  C Derivatives in gamma(i+1)
          call transpose2(EUgder(1,1,i+2),e2tder(1,1))
          call matvec2(ae3(1,1),Ub2der(1,i+2),auxvec(1)) 
 -        s2=scalar2(b1(1,iti1),auxvec(1))
 +        s2=scalar2(b1(1,i+1),auxvec(1))
          call matmat2(ae3(1,1),e2tder(1,1),auxmat(1,1))
          call matmat2(auxmat(1,1),e1t(1,1),pizda(1,1))
          s3=0.5d0*(pizda(1,1)+pizda(2,2))
  C Derivatives in gamma(i+2)
          call transpose2(EUgder(1,1,i+3),e3tder(1,1))
          call matvec2(e1a(1,1),Ub2der(1,i+3),auxvec(1))
 -        s1=scalar2(b1(1,iti2),auxvec(1))
 +        s1=scalar2(b1(1,i+2),auxvec(1))
          call matmat2(a_temp(1,1),e3tder(1,1),auxmat(1,1))
          call matvec2(auxmat(1,1),Ub2(1,i+2),auxvec(1)) 
 -        s2=scalar2(b1(1,iti1),auxvec(1))
 +        s2=scalar2(b1(1,i+1),auxvec(1))
          call matmat2(auxmat(1,1),e2t(1,1),auxmat3(1,1))
          call matmat2(auxmat3(1,1),e1t(1,1),pizda(1,1))
          s3=0.5d0*(pizda(1,1)+pizda(2,2))
@@@ -4564,10 -3762,10 +4600,10 @@@ C Derivatives of this turn contribution
              a_temp(2,2)=agg(l,4)
              call matmat2(e1t(1,1),a_temp(1,1),e1a(1,1))
              call matvec2(e1a(1,1),Ub2(1,i+3),auxvec(1))
 -            s1=scalar2(b1(1,iti2),auxvec(1))
 +            s1=scalar2(b1(1,i+2),auxvec(1))
              call matmat2(a_temp(1,1),e3t(1,1),ae3(1,1))
              call matvec2(ae3(1,1),Ub2(1,i+2),auxvec(1)) 
 -            s2=scalar2(b1(1,iti1),auxvec(1))
 +            s2=scalar2(b1(1,i+1),auxvec(1))
              call matmat2(ae3(1,1),e2t(1,1),ae3e2(1,1))
              call matmat2(ae3e2(1,1),e1t(1,1),pizda(1,1))
              s3=0.5d0*(pizda(1,1)+pizda(2,2))
@@@ -4583,10 -3781,10 +4619,10 @@@ C Remaining derivatives of this turn co
            a_temp(2,2)=aggi(l,4)
            call matmat2(e1t(1,1),a_temp(1,1),e1a(1,1))
            call matvec2(e1a(1,1),Ub2(1,i+3),auxvec(1))
 -          s1=scalar2(b1(1,iti2),auxvec(1))
 +          s1=scalar2(b1(1,i+2),auxvec(1))
            call matmat2(a_temp(1,1),e3t(1,1),ae3(1,1))
            call matvec2(ae3(1,1),Ub2(1,i+2),auxvec(1)) 
 -          s2=scalar2(b1(1,iti1),auxvec(1))
 +          s2=scalar2(b1(1,i+1),auxvec(1))
            call matmat2(ae3(1,1),e2t(1,1),ae3e2(1,1))
            call matmat2(ae3e2(1,1),e1t(1,1),pizda(1,1))
            s3=0.5d0*(pizda(1,1)+pizda(2,2))
            a_temp(2,2)=aggi1(l,4)
            call matmat2(e1t(1,1),a_temp(1,1),e1a(1,1))
            call matvec2(e1a(1,1),Ub2(1,i+3),auxvec(1))
 -          s1=scalar2(b1(1,iti2),auxvec(1))
 +          s1=scalar2(b1(1,i+2),auxvec(1))
            call matmat2(a_temp(1,1),e3t(1,1),ae3(1,1))
            call matvec2(ae3(1,1),Ub2(1,i+2),auxvec(1)) 
 -          s2=scalar2(b1(1,iti1),auxvec(1))
 +          s2=scalar2(b1(1,i+1),auxvec(1))
            call matmat2(ae3(1,1),e2t(1,1),ae3e2(1,1))
            call matmat2(ae3e2(1,1),e1t(1,1),pizda(1,1))
            s3=0.5d0*(pizda(1,1)+pizda(2,2))
            a_temp(2,2)=aggj(l,4)
            call matmat2(e1t(1,1),a_temp(1,1),e1a(1,1))
            call matvec2(e1a(1,1),Ub2(1,i+3),auxvec(1))
 -          s1=scalar2(b1(1,iti2),auxvec(1))
 +          s1=scalar2(b1(1,i+2),auxvec(1))
            call matmat2(a_temp(1,1),e3t(1,1),ae3(1,1))
            call matvec2(ae3(1,1),Ub2(1,i+2),auxvec(1)) 
 -          s2=scalar2(b1(1,iti1),auxvec(1))
 +          s2=scalar2(b1(1,i+1),auxvec(1))
            call matmat2(ae3(1,1),e2t(1,1),ae3e2(1,1))
            call matmat2(ae3e2(1,1),e1t(1,1),pizda(1,1))
            s3=0.5d0*(pizda(1,1)+pizda(2,2))
            a_temp(2,2)=aggj1(l,4)
            call matmat2(e1t(1,1),a_temp(1,1),e1a(1,1))
            call matvec2(e1a(1,1),Ub2(1,i+3),auxvec(1))
 -          s1=scalar2(b1(1,iti2),auxvec(1))
 +          s1=scalar2(b1(1,i+2),auxvec(1))
            call matmat2(a_temp(1,1),e3t(1,1),ae3(1,1))
            call matvec2(ae3(1,1),Ub2(1,i+2),auxvec(1)) 
 -          s2=scalar2(b1(1,iti1),auxvec(1))
 +          s2=scalar2(b1(1,i+1),auxvec(1))
            call matmat2(ae3(1,1),e2t(1,1),ae3e2(1,1))
            call matmat2(ae3e2(1,1),e1t(1,1),pizda(1,1))
            s3=0.5d0*(pizda(1,1)+pizda(2,2))
        r0_scp=4.5d0
  cd    print '(a)','Enter ESCP'
  cd    write (iout,*) 'iatscp_s=',iatscp_s,' iatscp_e=',iatscp_e
 +C      do xshift=-1,1
 +C      do yshift=-1,1
 +C      do zshift=-1,1
        do i=iatscp_s,iatscp_e
          if (itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1) cycle
          iteli=itel(i)
          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))
 -
 +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 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
 +cC 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
 +C          xi=xi+xshift*boxxsize
 +C          yi=yi+yshift*boxysize
 +C          zi=zi+zshift*boxzsize
          do iint=1,nscp_gr(i)
  
          do j=iscpstart(i,iint),iscpend(i,iint)
@@@ -4747,75 -3909,10 +4783,75 @@@ c         xj=c(1,nres+j)-x
  c         yj=c(2,nres+j)-yi
  c         zj=c(3,nres+j)-zi
  C Uncomment following three lines for Ca-p interactions
 -          xj=c(1,j)-xi
 -          yj=c(2,j)-yi
 -          zj=c(3,j)-zi
 +          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
 +C          xj=xj-xi
 +C          yj=yj-yi
 +C          zj=zj-zi
            rij=xj*xj+yj*yj+zj*zj
 +
            r0ij=r0_scp
            r0ijsq=r0ij*r0ij
            if (rij.lt.r0ijsq) then
@@@ -4866,9 -3963,6 +4902,9 @@@ cgrad          endd
  
          enddo ! iint
        enddo ! i
 +C      enddo !zshift
 +C      enddo !yshift
 +C      enddo !xshift
        return
        end
  C-----------------------------------------------------------------------------
        include 'COMMON.FFIELD'
        include 'COMMON.IOUNITS'
        include 'COMMON.CONTROL'
 +      include 'COMMON.SPLITELE'
        dimension ggg(3)
        evdw2=0.0D0
        evdw2_14=0.0d0
 +c        print *,boxxsize,boxysize,boxzsize,'wymiary pudla'
  cd    print '(a)','Enter ESCP'
  cd    write (iout,*) 'iatscp_s=',iatscp_s,' iatscp_e=',iatscp_e
 +C      do xshift=-1,1
 +C      do yshift=-1,1
 +C      do zshift=-1,1
        do i=iatscp_s,iatscp_e
          if (itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1) cycle
          iteli=itel(i)
          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
          do iint=1,nscp_gr(i)
  
          do j=iscpstart(i,iint),iscpend(i,iint)
@@@ -4952,97 -4005,27 +4988,97 @@@ c         xj=c(1,nres+j)-x
  c         yj=c(2,nres+j)-yi
  c         zj=c(3,nres+j)-zi
  C Uncomment following three lines for Ca-p interactions
 -          xj=c(1,j)-xi
 -          yj=c(2,j)-yi
 -          zj=c(3,j)-zi
 +          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
 +c          print *,xj,yj,zj,'polozenie j'
            rrij=1.0D0/(xj*xj+yj*yj+zj*zj)
 +c          print *,rrij
 +          sss=sscale(1.0d0/(dsqrt(rrij)))
 +c          print *,r_cut,1.0d0/dsqrt(rrij),sss,'tu patrz'
 +c          if (sss.eq.0) print *,'czasem jest OK'
 +          if (sss.le.0.0d0) cycle
 +          sssgrad=sscagrad(1.0d0/(dsqrt(rrij)))
            fac=rrij**expon2
            e1=fac*fac*aad(itypj,iteli)
            e2=fac*bad(itypj,iteli)
            if (iabs(j-i) .le. 2) then
              e1=scal14*e1
              e2=scal14*e2
 -            evdw2_14=evdw2_14+e1+e2
 +            evdw2_14=evdw2_14+(e1+e2)*sss
            endif
            evdwij=e1+e2
 -          evdw2=evdw2+evdwij
 +          evdw2=evdw2+evdwij*sss
            if (energy_dec) write (iout,'(a6,2i5,0pf7.3,2i3,3e11.3)')
       &        'evdw2',i,j,evdwij,iteli,itypj,fac,aad(itypj,iteli),
       &       bad(itypj,iteli)
  C
  C Calculate contributions to the gradient in the virtual-bond and SC vectors.
  C
 -          fac=-(evdwij+e1)*rrij
 +          fac=-(evdwij+e1)*rrij*sss
 +          fac=fac+(evdwij)*sssgrad*dsqrt(rrij)/expon
            ggg(1)=xj*fac
            ggg(2)=yj*fac
            ggg(3)=zj*fac
@@@ -5077,14 -4060,10 +5113,14 @@@ cgrad          endd
              gvdwc_scpp(k,i)=gvdwc_scpp(k,i)-ggg(k)
              gvdwc_scp(k,j)=gvdwc_scp(k,j)+ggg(k)
            enddo
 -        enddo
 +c        endif !endif for sscale cutoff
 +        enddo ! j
  
          enddo ! iint
        enddo ! i
 +c      enddo !zshift
 +c      enddo !yshift
 +c      enddo !xshift
        do i=1,nct
          do j=1,3
            gvdwc_scp(j,i)=expon*gvdwc_scp(j,i)
        include 'COMMON.VAR'
        include 'COMMON.INTERACT'
        include 'COMMON.IOUNITS'
+       include 'COMMON.CONTROL'
        dimension ggg(3)
        ehpb=0.0D0
+       do i=1,3
+        ggg(i)=0.0d0
+       enddo
+ C      write (iout,*) ,"link_end",link_end,constr_dist
  cd      write(iout,*)'edis: nhpb=',nhpb,' fbr=',fbr
  cd      write(iout,*)'link_start=',link_start,' link_end=',link_end
        if (link_end.eq.0) return
@@@ -5138,33 -4122,88 +5179,90 @@@ c        write (iout,*) "i",i," ii",ii,
  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.
 +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
  C 15/02/13 CC dynamic SSbond - additional check
-          if (ii.gt.nres 
-      &       .and. itype(iii).eq.1 .and. itype(jjj).eq.1) then 
+          if (ii.gt.nres .and. iabs(itype(iii)).eq.1 .and.
+      & iabs(itype(jjj)).eq.1) then
            call ssbond_ene(iii,jjj,eij)
            ehpb=ehpb+2*eij
           endif
  cd          write (iout,*) "eij",eij
+ cd   &   ' waga=',waga,' fac=',fac
+         else if (ii.gt.nres .and. jj.gt.nres) then
+ c Restraints from contact prediction
+           dd=dist(ii,jj)
+           if (constr_dist.eq.11) then
+             ehpb=ehpb+fordepth(i)**4.0d0
+      &          *rlornmr1(dd,dhpb(i),dhpb1(i),forcon(i))
+             fac=fordepth(i)**4.0d0
+      &          *rlornmr1prim(dd,dhpb(i),dhpb1(i),forcon(i))/dd
+           if (energy_dec) write (iout,'(a6,2i5,3f8.3)') "edisl",ii,jj,
+      &    ehpb,fordepth(i),dd
+            else
+           if (dhpb1(i).gt.0.0d0) then
+             ehpb=ehpb+2*forcon(i)*gnmr1(dd,dhpb(i),dhpb1(i))
+             fac=forcon(i)*gnmr1prim(dd,dhpb(i),dhpb1(i))/dd
+ c            write (iout,*) "beta nmr",
+ c     &        dd,2*forcon(i)*gnmr1(dd,dhpb(i),dhpb1(i))
+           else
+             dd=dist(ii,jj)
+             rdis=dd-dhpb(i)
+ C Get the force constant corresponding to this distance.
+             waga=forcon(i)
+ C Calculate the contribution to energy.
+             ehpb=ehpb+waga*rdis*rdis
+ c            write (iout,*) "beta reg",dd,waga*rdis*rdis
+ C
+ C Evaluate gradient.
+ C
+             fac=waga*rdis/dd
+           endif
+           endif
+           do j=1,3
+             ggg(j)=fac*(c(j,jj)-c(j,ii))
+           enddo
+           do j=1,3
+             ghpbx(j,iii)=ghpbx(j,iii)-ggg(j)
+             ghpbx(j,jjj)=ghpbx(j,jjj)+ggg(j)
+           enddo
+           do k=1,3
+             ghpbc(k,jjj)=ghpbc(k,jjj)+ggg(k)
+             ghpbc(k,iii)=ghpbc(k,iii)-ggg(k)
+           enddo
          else
  C Calculate the distance between the two points and its difference from the
  C target distance.
            dd=dist(ii,jj)
+           if (constr_dist.eq.11) then
+             ehpb=ehpb+fordepth(i)**4.0d0
+      &           *rlornmr1(dd,dhpb(i),dhpb1(i),forcon(i))
+             fac=fordepth(i)**4.0d0
+      &           *rlornmr1prim(dd,dhpb(i),dhpb1(i),forcon(i))/dd
+           if (energy_dec) write (iout,'(a6,2i5,3f8.3)') "edisl",ii,jj,
+      &    ehpb,fordepth(i),dd
+            else   
+           if (dhpb1(i).gt.0.0d0) then
+             ehpb=ehpb+2*forcon(i)*gnmr1(dd,dhpb(i),dhpb1(i))
+             fac=forcon(i)*gnmr1prim(dd,dhpb(i),dhpb1(i))/dd
+ c            write (iout,*) "alph nmr",
+ c     &        dd,2*forcon(i)*gnmr1(dd,dhpb(i),dhpb1(i))
+           else
              rdis=dd-dhpb(i)
  C Get the force constant corresponding to this distance.
              waga=forcon(i)
  C Calculate the contribution to energy.
              ehpb=ehpb+waga*rdis*rdis
+ c            write (iout,*) "alpha reg",dd,waga*rdis*rdis
  C
  C Evaluate gradient.
  C
              fac=waga*rdis/dd
- cd      print *,'i=',i,' ii=',ii,' jj=',jj,' dhpb=',dhpb(i),' dd=',dd,
- cd   &   ' waga=',waga,' fac=',fac
+           endif
+           endif
              do j=1,3
                ggg(j)=fac*(c(j,jj)-c(j,ii))
              enddo
@@@ -5188,7 -4227,7 +5286,7 @@@ cgrad        endd
            enddo
          endif
        enddo
-       ehpb=0.5D0*ehpb
+       if (constr_dist.ne.11) ehpb=0.5D0*ehpb
        return
        end
  C--------------------------------------------------------------------------
        estr=0.0d0
        estr1=0.0d0
        do i=ibondp_start,ibondp_end
 -        if (itype(i-1).eq.ntyp1 .or. itype(i).eq.ntyp1) then
 -          estr1=estr1+gnmr1(vbld(i),-1.0d0,distchainmax)
 -          do j=1,3
 -          gradb(j,i-1)=gnmr1prim(vbld(i),-1.0d0,distchainmax)
 -     &      *dc(j,i-1)/vbld(i)
 -          enddo
 -          if (energy_dec) write(iout,*) 
 -     &       "estr1",i,gnmr1(vbld(i),-1.0d0,distchainmax)
 -        else
 +        if (itype(i-1).eq.ntyp1 .and. itype(i).eq.ntyp1) cycle
 +c          estr1=estr1+gnmr1(vbld(i),-1.0d0,distchainmax)
 +c          do j=1,3
 +c          gradb(j,i-1)=gnmr1prim(vbld(i),-1.0d0,distchainmax)
 +c     &      *dc(j,i-1)/vbld(i)
 +c          enddo
 +c          if (energy_dec) write(iout,*) 
 +c     &       "estr1",i,gnmr1(vbld(i),-1.0d0,distchainmax)
 +c        else
 +C       Checking if it involves dummy (NH3+ or COO-) group
 +         if (itype(i-1).eq.ntyp1 .or. itype(i).eq.ntyp1) then
 +C YES   vbldpDUM is the equlibrium length of spring for Dummy atom
 +        diff = vbld(i)-vbldpDUM
 +         else
 +C NO    vbldp0 is the equlibrium lenght of spring for peptide group
          diff = vbld(i)-vbldp0
 -        if (energy_dec) write (iout,*) 
 +         endif 
 +        if (energy_dec)    write (iout,'(a7,i5,4f7.3)') 
       &     "estr bb",i,vbld(i),vbldp0,diff,AKP*diff*diff
          estr=estr+diff*diff
          do j=1,3
            gradb(j,i-1)=AKP*diff*dc(j,i-1)/vbld(i)
          enddo
  c        write (iout,'(i5,3f10.5)') i,(gradb(j,i-1),j=1,3)
 -        endif
 +c        endif
        enddo
        estr=0.5d0*AKP*estr+estr1
  c
            nbi=nbondterm(iti)
            if (nbi.eq.1) then
              diff=vbld(i+nres)-vbldsc0(1,iti)
 -            if (energy_dec) write (iout,*) 
 +            if (energy_dec)  write (iout,*) 
       &      "estr sc",i,iti,vbld(i+nres),vbldsc0(1,iti),diff,
       &      AKSC(1,iti),AKSC(1,iti)*diff*diff
              estr=estr+0.5d0*AKSC(1,iti)*diff*diff
        end 
  #ifdef CRYST_THETA
  C--------------------------------------------------------------------------
-       subroutine ebend(etheta)
+       subroutine ebend(etheta,ethetacnstr)
  C
  C Evaluate the virtual-bond-angle energy given the virtual-bond dihedral
  C angles gamma and its derivatives in consecutive thetas and gammas.
        include 'COMMON.NAMES'
        include 'COMMON.FFIELD'
        include 'COMMON.CONTROL'
+       include 'COMMON.TORCNSTR'
        common /calcthet/ term1,term2,termm,diffak,ratak,
       & ak,aktc,termpre,termexp,sigc,sig0i,time11,time12,sigcsq,
       & delthe0,sig0inv,sigtc,sigsqtc,delthec,it
@@@ -5406,8 -4439,7 +5505,8 @@@ c      time12=1.0d
        etheta=0.0D0
  c     write (*,'(a,i2)') 'EBEND ICG=',icg
        do i=ithet_start,ithet_end
 -        if (itype(i-1).eq.ntyp1) cycle
 +        if ((itype(i-1).eq.ntyp1).or.itype(i-2).eq.ntyp1
 +     &  .or.itype(i).eq.ntyp1) cycle
  C Zero the energy function and its derivative at 0 or pi.
          call splinthet(theta(i),0.5d0*delta,ss,ssd)
          it=itype(i-1)
            ichir22=isign(1,itype(i))
           endif
  
 -        if (i.gt.3 .and. itype(i-2).ne.ntyp1) then
 +        if (i.gt.3 .and. itype(i-3).ne.ntyp1) then
  #ifdef OSF
          phii=phi(i)
            if (phii.ne.phii) phii=150.0
            y(1)=0.0D0
            y(2)=0.0D0
          endif
 -        if (i.lt.nres .and. itype(i).ne.ntyp1) then
 +        if (i.lt.nres .and. itype(i+1).ne.ntyp1) then
  #ifdef OSF
          phii1=phi(i+1)
            if (phii1.ne.phii1) phii1=150.0
            z(1)=cos(phii1)
  #else
            phii1=phi(i+1)
 -          z(1)=dcos(phii1)
  #endif
 +          z(1)=dcos(phii1)
            z(2)=dsin(phii1)
          else
            z(1)=0.0D0
@@@ -5464,7 -4496,6 +5563,7 @@@ C In following comments this theta wil
               bthetk=bthet(k,itype2,ichir21,ichir22)
            endif
           thet_pred_mean=thet_pred_mean+athetk*y(k)+bthetk*z(k)
 +c         write(iout,*) 'chuj tu', y(k),z(k)
          enddo
          dthett=thet_pred_mean*ssd
          thet_pred_mean=thet_pred_mean*ss+a0thet(it)
@@@ -5501,12 -4532,40 +5600,40 @@@ C Derivatives of the "mean" values in g
       &        E_theta,E_tc)
          endif
          etheta=etheta+ethetai
 -        if (energy_dec) write (iout,'(a6,i5,0pf7.3)')
 -     &      'ebend',i,ethetai
 +        if (energy_dec) write (iout,'(a6,i5,0pf7.3,f7.3,i5)')
 +     &      'ebend',i,ethetai,theta(i),itype(i)
          if (i.gt.3) gloc(i-3,icg)=gloc(i-3,icg)+wang*E_tc*dthetg1
          if (i.lt.nres) gloc(i-2,icg)=gloc(i-2,icg)+wang*E_tc*dthetg2
 -        gloc(nphi+i-2,icg)=wang*(E_theta+E_tc*dthett)
 +        gloc(nphi+i-2,icg)=wang*(E_theta+E_tc*dthett)+gloc(nphi+i-2,icg)
        enddo
+       ethetacnstr=0.0d0
+ C      print *,ithetaconstr_start,ithetaconstr_end,"TU"
+       do i=ithetaconstr_start,ithetaconstr_end
+         itheta=itheta_constr(i)
+         thetiii=theta(itheta)
+         difi=pinorm(thetiii-theta_constr0(i))
+         if (difi.gt.theta_drange(i)) then
+           difi=difi-theta_drange(i)
+           ethetacnstr=ethetcnstr+0.25d0*for_thet_constr(i)*difi**4
+           gloc(itheta+nphi-2,icg)=gloc(itheta+nphi-2,icg)
+      &    +for_thet_constr(i)*difi**3
+         else if (difi.lt.-drange(i)) then
+           difi=difi+drange(i)
+           ethetacnstr=ethetcnstr+0.25d0*for_thet_constr(i)*difi**4
+           gloc(itheta+nphi-2,icg)=gloc(itheta+nphi-2,icg)
+      &    +for_thet_constr(i)*difi**3
+         else
+           difi=0.0
+         endif
+        if (energy_dec) then
+         write (iout,'(a6,2i5,4f8.3,2e14.5)') "ethetc",
+      &    i,itheta,rad2deg*thetiii,
+      &    rad2deg*theta_constr0(i),  rad2deg*theta_drange(i),
+      &    rad2deg*difi,0.25d0*for_thet_constr(i)*difi**4,
+      &    gloc(itheta+nphi-2,icg)
+         endif
+       enddo
  C Ufff.... We've done all this!!! 
        return
        end
@@@ -5523,8 -4582,7 +5650,8 @@@ C--------------------------------------
  C Calculate the contributions to both Gaussian lobes.
  C 6/6/97 - Deform the Gaussians using the factor of 1/(1+time)
  C The "polynomial part" of the "standard deviation" of this part of 
 -C the distribution.
 +C the distributioni.
 +ccc        write (iout,*) thetai,thet_pred_mean
          sig=polthet(3,it)
          do j=2,0,-1
            sig=sig*thet_pred_mean+polthet(j,it)
@@@ -5554,7 -4612,6 +5681,7 @@@ C Following variable is sigma(t_c)**(-2
          delthe0=thetai-theta0i
          term1=-0.5D0*sigcsq*delthec*delthec
          term2=-0.5D0*sig0inv*delthe0*delthe0
 +C        write (iout,*)'term1',term1,term2,sigcsq,delthec,sig0inv,delthe0
  C Following fuzzy logic is to avoid underflows in dexp and subsequent INFs and
  C NaNs in taking the logarithm. We extract the largest exponent which is added
  C to the energy (this being the log of the distribution) at the end of energy
@@@ -5582,7 -4639,6 +5709,7 @@@ C Contribution of the bending energy fr
  C the sum of the contributions from the two lobes and the pre-exponential
  C factor. Simple enough, isn't it?
          ethetai=(-dlog(termexp)-termm+dlog(termpre))
 +C       write (iout,*) 'termexp',termexp,termm,termpre,i
  C NOW the derivatives!!!
  C 6/6/97 Take into account the deformation.
          E_theta=(delthec*sigcsq*term1
@@@ -5623,7 -4679,7 +5750,7 @@@ C "Thank you" to MAPLE (probably spare
        end
  #else
  C--------------------------------------------------------------------------
-       subroutine ebend(etheta)
+       subroutine ebend(etheta,ethetacnstr)
  C
  C Evaluate the virtual-bond-angle energy given the virtual-bond dihedral
  C angles gamma and its derivatives in consecutive thetas and gammas.
        include 'COMMON.NAMES'
        include 'COMMON.FFIELD'
        include 'COMMON.CONTROL'
+       include 'COMMON.TORCNSTR'
        double precision coskt(mmaxtheterm),sinkt(mmaxtheterm),
       & cosph1(maxsingle),sinph1(maxsingle),cosph2(maxsingle),
       & sinph2(maxsingle),cosph1ph2(maxdouble,maxdouble),
        logical lprn /.false./, lprn1 /.false./
        etheta=0.0D0
        do i=ithet_start,ithet_end
 -        if ((itype(i-1).eq.ntyp1).or.(itype(i-2).eq.ntyp1).or.
 -     &(itype(i).eq.ntyp1)) cycle
 +c        print *,i,itype(i-1),itype(i),itype(i-2)
 +        if ((itype(i-1).eq.ntyp1).or.itype(i-2).eq.ntyp1
 +     &  .or.itype(i).eq.ntyp1) cycle
- C In current verion the ALL DUMMY ATOM POTENTIALS ARE OFF
+ C        print *,i,theta(i)
          if (iabs(itype(i+1)).eq.20) iblock=2
          if (iabs(itype(i+1)).ne.20) iblock=1
          dethetai=0.0d0
            coskt(k)=dcos(k*theti2)
            sinkt(k)=dsin(k*theti2)
          enddo
+ C        print *,ethetai
 -
          if (i.gt.3 .and. itype(i-3).ne.ntyp1) then
  #ifdef OSF
            phii=phi(i)
@@@ -5680,8 -4737,8 +5808,8 @@@ C propagation of chirality for glycine 
            enddo
          else
            phii=0.0d0
-           ityp1=nthetyp+1
            do k=1,nsingle
+           ityp1=ithetyp((itype(i-2)))
              cosph1(k)=0.0d0
              sinph1(k)=0.0d0
            enddo 
            enddo
          else
            phii1=0.0d0
-           ityp3=nthetyp+1
+           ityp3=ithetyp((itype(i)))
            do k=1,nsingle
              cosph2(k)=0.0d0
              sinph2(k)=0.0d0
          enddo
          write(iout,*) "ethetai",ethetai
          endif
+ C       print *,ethetai
          do m=1,ntheterm2
            do k=1,nsingle
              aux=bbthet(k,m,ityp1,ityp2,ityp3,iblock)*cosph1(k)
       &         ccthet(k,m,ityp1,ityp2,ityp3,iblock)," ddthet",
       &         ddthet(k,m,ityp1,ityp2,ityp3,iblock)," eethet",
       &         eethet(k,m,ityp1,ityp2,ityp3,iblock)," ethetai",ethetai
+ C        print *,"tu",cosph1(k),sinph1(k),cosph2(k),sinph2(k)
            enddo
          enddo
+ C        print *,"cosph1", (cosph1(k), k=1,nsingle)
+ C        print *,"cosph2", (cosph2(k), k=1,nsingle)
+ C        print *,"sinph1", (sinph1(k), k=1,nsingle)
+ C        print *,"sinph2", (sinph2(k), k=1,nsingle)
          if (lprn)
       &  write(iout,*) "ethetai",ethetai
+ C        print *,"tu",cosph1(k),sinph1(k),cosph2(k),sinph2(k)
          do m=1,ntheterm3
            do k=2,ndouble
              do l=1,k-1
          enddo
  10      continue
  c        lprn1=.true.
+ C        print *,ethetai
          if (lprn1) 
       &    write (iout,'(i2,3f8.1,9h ethetai ,f10.5)') 
       &   i,theta(i)*rad2deg,phii*rad2deg,
@@@ -5818,8 -4883,37 +5954,37 @@@ c        lprn1=.false
          etheta=etheta+ethetai
          if (i.gt.3) gloc(i-3,icg)=gloc(i-3,icg)+wang*dephii
          if (i.lt.nres) gloc(i-2,icg)=gloc(i-2,icg)+wang*dephii1
-         gloc(nphi+i-2,icg)=wang*dethetai+gloc(nphi+i-2,icg)
+         gloc(nphi+i-2,icg)=gloc(nphi+i-2,icg)+wang*dethetai
        enddo
+ C now constrains
+       ethetacnstr=0.0d0
+ C      print *,ithetaconstr_start,ithetaconstr_end,"TU"
+       do i=ithetaconstr_start,ithetaconstr_end
+         itheta=itheta_constr(i)
+         thetiii=theta(itheta)
+         difi=pinorm(thetiii-theta_constr0(i))
+         if (difi.gt.theta_drange(i)) then
+           difi=difi-theta_drange(i)
+           ethetacnstr=ethetacnstr+0.25d0*for_thet_constr(i)*difi**4
+           gloc(itheta+nphi-2,icg)=gloc(itheta+nphi-2,icg)
+      &    +for_thet_constr(i)*difi**3
+         else if (difi.lt.-drange(i)) then
+           difi=difi+drange(i)
+           ethetacnstr=ethetacnstr+0.25d0*for_thet_constr(i)*difi**4
+           gloc(itheta+nphi-2,icg)=gloc(itheta+nphi-2,icg)
+      &    +for_thet_constr(i)*difi**3
+         else
+           difi=0.0
+         endif
+        if (energy_dec) then
+         write (iout,'(a6,2i5,4f8.3,2e14.5)') "ethetc",
+      &    i,itheta,rad2deg*thetiii,
+      &    rad2deg*theta_constr0(i),  rad2deg*theta_drange(i),
+      &    rad2deg*difi,0.25d0*for_thet_constr(i)*difi**4,
+      &    gloc(itheta+nphi-2,icg)
+         endif
+       enddo
        return
        end
  #endif
@@@ -6584,9 -5678,9 +6749,9 @@@ c      lprn=.true
        do i=iphi_start,iphi_end
        etors_ii=0.0D0
          if (itype(i-2).eq.ntyp1.or. itype(i-1).eq.ntyp1
 -     &      .or. itype(i).eq.ntyp1) cycle
 -      itori=itortyp(itype(i-2))
 -      itori1=itortyp(itype(i-1))
 +     &      .or. itype(i).eq.ntyp1 .or. itype(i-3).eq.ntyp1) cycle
 +        itori=itortyp(itype(i-2))
 +        itori1=itortyp(itype(i-1))
          phii=phi(i)
          gloci=0.0D0
  C Proline-Proline pair is a special case...
@@@ -6639,12 -5733,12 +6804,12 @@@ c       write (iout,*) 'i=',i,' gloc=',
          difi=phii-phi0(i)
          if (difi.gt.drange(i)) then
            difi=difi-drange(i)
-           edihcnstr=edihcnstr+0.25d0*ftors*difi**4
-           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors*difi**3
+           edihcnstr=edihcnstr+0.25d0*ftors(i)*difi**4
+           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors(i)*difi**3
          else if (difi.lt.-drange(i)) then
            difi=difi+drange(i)
-           edihcnstr=edihcnstr+0.25d0*ftors*difi**4
-           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors*difi**3
+           edihcnstr=edihcnstr+0.25d0*ftors(i)**difi**4
+           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors(i)*difi**3
          endif
  !        write (iout,'(2i5,2f8.3,2e14.5)') i,itori,rad2deg*phii,
  !     &    rad2deg*difi,0.25d0*ftors*difi**4,gloc(itori-3,icg)
@@@ -6680,15 -5774,8 +6845,15 @@@ C Set lprn=.true. for debuggin
  c     lprn=.true.
        etors=0.0D0
        do i=iphi_start,iphi_end
 -        if (itype(i-2).eq.ntyp1 .or. itype(i-1).eq.ntyp1 
 -     &       .or. itype(i).eq.ntyp1) cycle
 +C ANY TWO ARE DUMMY ATOMS in row CYCLE
 +c        if (((itype(i-3).eq.ntyp1).and.(itype(i-2).eq.ntyp1)).or.
 +c     &      ((itype(i-2).eq.ntyp1).and.(itype(i-1).eq.ntyp1))  .or.
 +c     &      ((itype(i-1).eq.ntyp1).and.(itype(i).eq.ntyp1))) cycle
 +        if (itype(i-2).eq.ntyp1.or. itype(i-1).eq.ntyp1
 +     &      .or. itype(i).eq.ntyp1 .or. itype(i-3).eq.ntyp1) cycle
 +C In current verion the ALL DUMMY ATOM POTENTIALS ARE OFF
 +C For introducing the NH3+ and COO- group please check the etor_d for reference
 +C and guidance
          etors_ii=0.0D0
           if (iabs(itype(i)).eq.20) then
           iblock=2
@@@ -6750,18 -5837,21 +6915,21 @@@ c      do i=1,ndih_const
          difi=pinorm(phii-phi0(i))
          if (difi.gt.drange(i)) then
            difi=difi-drange(i)
-           edihcnstr=edihcnstr+0.25d0*ftors*difi**4
-           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors*difi**3
+           edihcnstr=edihcnstr+0.25d0*ftors(i)*difi**4
+           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors(i)*difi**3
          else if (difi.lt.-drange(i)) then
            difi=difi+drange(i)
-           edihcnstr=edihcnstr+0.25d0*ftors*difi**4
-           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors*difi**3
+           edihcnstr=edihcnstr+0.25d0*ftors(i)*difi**4
+           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors(i)*difi**3
          else
            difi=0.0
          endif
- cd        write (iout,'(2i5,4f8.3,2e14.5)') i,itori,rad2deg*phii,
- cd     &    rad2deg*phi0(i),  rad2deg*drange(i),
- cd     &    rad2deg*difi,0.25d0*ftors*difi**4,gloc(itori-3,icg)
+        if (energy_dec) then
+         write (iout,'(a6,2i5,4f8.3,2e14.5)') "edihc",
+      &    i,itori,rad2deg*phii,
+      &    rad2deg*phi0(i),  rad2deg*drange(i),
+      &    rad2deg*difi,0.25d0*ftors(i)*difi**4,gloc(itori-3,icg)
+         endif
        enddo
  cd       write (iout,*) 'edihcnstr',edihcnstr
        return
@@@ -6789,15 -5879,8 +6957,15 @@@ c     lprn=.true
        etors_d=0.0D0
  c      write(iout,*) "a tu??"
        do i=iphid_start,iphid_end
 -        if (itype(i-2).eq.ntyp1 .or. itype(i-1).eq.ntyp1
 -     &      .or. itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1) cycle
 +C ANY TWO ARE DUMMY ATOMS in row CYCLE
 +C        if (((itype(i-3).eq.ntyp1).and.(itype(i-2).eq.ntyp1)).or.
 +C     &      ((itype(i-2).eq.ntyp1).and.(itype(i-1).eq.ntyp1)).or.
 +C     &      ((itype(i-1).eq.ntyp1).and.(itype(i).eq.ntyp1))  .or.
 +C     &      ((itype(i).eq.ntyp1).and.(itype(i+1).eq.ntyp1))) cycle
 +         if ((itype(i-2).eq.ntyp1).or.itype(i-3).eq.ntyp1.or.
 +     &  (itype(i-1).eq.ntyp1).or.(itype(i).eq.ntyp1).or.
 +     &  (itype(i+1).eq.ntyp1)) cycle
 +C In current verion the ALL DUMMY ATOM POTENTIALS ARE OFF
          itori=itortyp(itype(i-2))
          itori1=itortyp(itype(i-1))
          itori2=itortyp(itype(i))
          gloci2=0.0D0
          iblock=1
          if (iabs(itype(i+1)).eq.20) iblock=2
 +C Iblock=2 Proline type
 +C ADASKO: WHEN PARAMETERS FOR THIS TYPE OF BLOCKING GROUP IS READY UNCOMMENT
 +C CHECK WEATHER THERE IS NECCESITY FOR iblock=3 for COO-
 +C        if (itype(i+1).eq.ntyp1) iblock=3
 +C The problem of NH3+ group can be resolved by adding new parameters please note if there
 +C IS or IS NOT need for this
 +C IF Yes uncomment below and add to parmread.F appropriate changes and to v1cij and so on
 +C        is (itype(i-3).eq.ntyp1) ntblock=2
 +C        ntblock is N-terminal blocking group
  
  C Regular cosine and sine terms
          do j=1,ntermd_1(itori,itori1,itori2,iblock)
 +C Example of changes for NH3+ blocking group
 +C       do j=1,ntermd_1(itori,itori1,itori2,iblock,ntblock)
 +C          v1cij=v1c(1,j,itori,itori1,itori2,iblock,ntblock)
            v1cij=v1c(1,j,itori,itori1,itori2,iblock)
            v1sij=v1s(1,j,itori,itori1,itori2,iblock)
            v2cij=v1c(2,j,itori,itori1,itori2,iblock)
@@@ -7936,15 -7007,15 +8104,15 @@@ C--------------------------------------
        if (j.lt.nres-1) then
          itj1 = itortyp(itype(j+1))
        else
 -        itj1=ntortyp+1
 +        itj1=ntortyp
        endif
        do iii=1,2
          dipi(iii,1)=Ub2(iii,i)
          dipderi(iii)=Ub2der(iii,i)
 -        dipi(iii,2)=b1(iii,iti1)
 +        dipi(iii,2)=b1(iii,i+1)
          dipj(iii,1)=Ub2(iii,j)
          dipderj(iii)=Ub2der(iii,j)
 -        dipj(iii,2)=b1(iii,itj1)
 +        dipj(iii,2)=b1(iii,j+1)
        enddo
        kkk=0
        do iii=1,2
@@@ -8026,14 -7097,14 +8194,14 @@@ C parallel orientation of the two CA-CA
          if (i.gt.1) then
            iti=itortyp(itype(i))
          else
 -          iti=ntortyp+1
 +          iti=ntortyp
          endif
          itk1=itortyp(itype(k+1))
          itj=itortyp(itype(j))
          if (l.lt.nres-1) then
            itl1=itortyp(itype(l+1))
          else
 -          itl1=ntortyp+1
 +          itl1=ntortyp
          endif
  C A1 kernel(j+1) A2T
  cd        do iii=1,2
@@@ -8124,26 -7195,26 +8292,26 @@@ C They are needed only when the fifth- 
  C indluded.
          IF (wcorr5.gt.0.0d0 .or. wcorr6.gt.0.0d0) THEN
          call transpose2(AEA(1,1,1),auxmat(1,1))
 -        call matvec2(auxmat(1,1),b1(1,iti),AEAb1(1,1,1))
 +        call matvec2(auxmat(1,1),b1(1,i),AEAb1(1,1,1))
          call matvec2(auxmat(1,1),Ub2(1,i),AEAb2(1,1,1))
          call matvec2(auxmat(1,1),Ub2der(1,i),AEAb2derg(1,2,1,1))
          call transpose2(AEAderg(1,1,1),auxmat(1,1))
 -        call matvec2(auxmat(1,1),b1(1,iti),AEAb1derg(1,1,1))
 +        call matvec2(auxmat(1,1),b1(1,i),AEAb1derg(1,1,1))
          call matvec2(auxmat(1,1),Ub2(1,i),AEAb2derg(1,1,1,1))
 -        call matvec2(AEA(1,1,1),b1(1,itk1),AEAb1(1,2,1))
 -        call matvec2(AEAderg(1,1,1),b1(1,itk1),AEAb1derg(1,2,1))
 +        call matvec2(AEA(1,1,1),b1(1,k+1),AEAb1(1,2,1))
 +        call matvec2(AEAderg(1,1,1),b1(1,k+1),AEAb1derg(1,2,1))
          call matvec2(AEA(1,1,1),Ub2(1,k+1),AEAb2(1,2,1))
          call matvec2(AEAderg(1,1,1),Ub2(1,k+1),AEAb2derg(1,1,2,1))
          call matvec2(AEA(1,1,1),Ub2der(1,k+1),AEAb2derg(1,2,2,1))
          call transpose2(AEA(1,1,2),auxmat(1,1))
 -        call matvec2(auxmat(1,1),b1(1,itj),AEAb1(1,1,2))
 +        call matvec2(auxmat(1,1),b1(1,j),AEAb1(1,1,2))
          call matvec2(auxmat(1,1),Ub2(1,j),AEAb2(1,1,2))
          call matvec2(auxmat(1,1),Ub2der(1,j),AEAb2derg(1,2,1,2))
          call transpose2(AEAderg(1,1,2),auxmat(1,1))
 -        call matvec2(auxmat(1,1),b1(1,itj),AEAb1derg(1,1,2))
 +        call matvec2(auxmat(1,1),b1(1,j),AEAb1derg(1,1,2))
          call matvec2(auxmat(1,1),Ub2(1,j),AEAb2derg(1,1,1,2))
 -        call matvec2(AEA(1,1,2),b1(1,itl1),AEAb1(1,2,2))
 -        call matvec2(AEAderg(1,1,2),b1(1,itl1),AEAb1derg(1,2,2))
 +        call matvec2(AEA(1,1,2),b1(1,l+1),AEAb1(1,2,2))
 +        call matvec2(AEAderg(1,1,2),b1(1,l+1),AEAb1derg(1,2,2))
          call matvec2(AEA(1,1,2),Ub2(1,l+1),AEAb2(1,2,2))
          call matvec2(AEAderg(1,1,2),Ub2(1,l+1),AEAb2derg(1,1,2,2))
          call matvec2(AEA(1,1,2),Ub2der(1,l+1),AEAb2derg(1,2,2,2))
@@@ -8152,20 -7223,20 +8320,20 @@@ C Calculate the Cartesian derivatives o
            do kkk=1,5
              do lll=1,3
                call transpose2(AEAderx(1,1,lll,kkk,iii,1),auxmat(1,1))
 -              call matvec2(auxmat(1,1),b1(1,iti),
 +              call matvec2(auxmat(1,1),b1(1,i),
       &          AEAb1derx(1,lll,kkk,iii,1,1))
                call matvec2(auxmat(1,1),Ub2(1,i),
       &          AEAb2derx(1,lll,kkk,iii,1,1))
 -              call matvec2(AEAderx(1,1,lll,kkk,iii,1),b1(1,itk1),
 +              call matvec2(AEAderx(1,1,lll,kkk,iii,1),b1(1,k+1),
       &          AEAb1derx(1,lll,kkk,iii,2,1))
                call matvec2(AEAderx(1,1,lll,kkk,iii,1),Ub2(1,k+1),
       &          AEAb2derx(1,lll,kkk,iii,2,1))
                call transpose2(AEAderx(1,1,lll,kkk,iii,2),auxmat(1,1))
 -              call matvec2(auxmat(1,1),b1(1,itj),
 +              call matvec2(auxmat(1,1),b1(1,j),
       &          AEAb1derx(1,lll,kkk,iii,1,2))
                call matvec2(auxmat(1,1),Ub2(1,j),
       &          AEAb2derx(1,lll,kkk,iii,1,2))
 -              call matvec2(AEAderx(1,1,lll,kkk,iii,2),b1(1,itl1),
 +              call matvec2(AEAderx(1,1,lll,kkk,iii,2),b1(1,l+1),
       &          AEAb1derx(1,lll,kkk,iii,2,2))
                call matvec2(AEAderx(1,1,lll,kkk,iii,2),Ub2(1,l+1),
       &          AEAb2derx(1,lll,kkk,iii,2,2))
@@@ -8179,7 -7250,7 +8347,7 @@@ C Antiparallel orientation of the two C
          if (i.gt.1) then
            iti=itortyp(itype(i))
          else
 -          iti=ntortyp+1
 +          iti=ntortyp
          endif
          itk1=itortyp(itype(k+1))
          itl=itortyp(itype(l))
          if (j.lt.nres-1) then
            itj1=itortyp(itype(j+1))
          else 
 -          itj1=ntortyp+1
 +          itj1=ntortyp
          endif
  C A2 kernel(j-1)T A1T
          call kernel(aa1(1,1),aa2t(1,1),a_chuj_der(1,1,1,1,jj,i),
@@@ -8262,26 -7333,26 +8430,26 @@@ C indluded
          IF (wcorr5.gt.0.0d0 .or. wcorr6.gt.0.0d0 .or.
       &    (wturn6.gt.0.0d0 .and. j.eq.i+4 .and. l.eq.i+3)) THEN
          call transpose2(AEA(1,1,1),auxmat(1,1))
 -        call matvec2(auxmat(1,1),b1(1,iti),AEAb1(1,1,1))
 +        call matvec2(auxmat(1,1),b1(1,i),AEAb1(1,1,1))
          call matvec2(auxmat(1,1),Ub2(1,i),AEAb2(1,1,1))
          call matvec2(auxmat(1,1),Ub2der(1,i),AEAb2derg(1,2,1,1))
          call transpose2(AEAderg(1,1,1),auxmat(1,1))
 -        call matvec2(auxmat(1,1),b1(1,iti),AEAb1derg(1,1,1))
 +        call matvec2(auxmat(1,1),b1(1,i),AEAb1derg(1,1,1))
          call matvec2(auxmat(1,1),Ub2(1,i),AEAb2derg(1,1,1,1))
 -        call matvec2(AEA(1,1,1),b1(1,itk1),AEAb1(1,2,1))
 -        call matvec2(AEAderg(1,1,1),b1(1,itk1),AEAb1derg(1,2,1))
 +        call matvec2(AEA(1,1,1),b1(1,k+1),AEAb1(1,2,1))
 +        call matvec2(AEAderg(1,1,1),b1(1,k+1),AEAb1derg(1,2,1))
          call matvec2(AEA(1,1,1),Ub2(1,k+1),AEAb2(1,2,1))
          call matvec2(AEAderg(1,1,1),Ub2(1,k+1),AEAb2derg(1,1,2,1))
          call matvec2(AEA(1,1,1),Ub2der(1,k+1),AEAb2derg(1,2,2,1))
          call transpose2(AEA(1,1,2),auxmat(1,1))
 -        call matvec2(auxmat(1,1),b1(1,itj1),AEAb1(1,1,2))
 +        call matvec2(auxmat(1,1),b1(1,j+1),AEAb1(1,1,2))
          call matvec2(auxmat(1,1),Ub2(1,l),AEAb2(1,1,2))
          call matvec2(auxmat(1,1),Ub2der(1,l),AEAb2derg(1,2,1,2))
          call transpose2(AEAderg(1,1,2),auxmat(1,1))
 -        call matvec2(auxmat(1,1),b1(1,itl),AEAb1(1,1,2))
 +        call matvec2(auxmat(1,1),b1(1,l),AEAb1(1,1,2))
          call matvec2(auxmat(1,1),Ub2(1,l),AEAb2derg(1,1,1,2))
 -        call matvec2(AEA(1,1,2),b1(1,itj1),AEAb1(1,2,2))
 -        call matvec2(AEAderg(1,1,2),b1(1,itj1),AEAb1derg(1,2,2))
 +        call matvec2(AEA(1,1,2),b1(1,j+1),AEAb1(1,2,2))
 +        call matvec2(AEAderg(1,1,2),b1(1,j+1),AEAb1derg(1,2,2))
          call matvec2(AEA(1,1,2),Ub2(1,j),AEAb2(1,2,2))
          call matvec2(AEAderg(1,1,2),Ub2(1,j),AEAb2derg(1,1,2,2))
          call matvec2(AEA(1,1,2),Ub2der(1,j),AEAb2derg(1,2,2,2))
@@@ -8290,20 -7361,20 +8458,20 @@@ C Calculate the Cartesian derivatives o
            do kkk=1,5
              do lll=1,3
                call transpose2(AEAderx(1,1,lll,kkk,iii,1),auxmat(1,1))
 -              call matvec2(auxmat(1,1),b1(1,iti),
 +              call matvec2(auxmat(1,1),b1(1,i),
       &          AEAb1derx(1,lll,kkk,iii,1,1))
                call matvec2(auxmat(1,1),Ub2(1,i),
       &          AEAb2derx(1,lll,kkk,iii,1,1))
 -              call matvec2(AEAderx(1,1,lll,kkk,iii,1),b1(1,itk1),
 +              call matvec2(AEAderx(1,1,lll,kkk,iii,1),b1(1,k+1),
       &          AEAb1derx(1,lll,kkk,iii,2,1))
                call matvec2(AEAderx(1,1,lll,kkk,iii,1),Ub2(1,k+1),
       &          AEAb2derx(1,lll,kkk,iii,2,1))
                call transpose2(AEAderx(1,1,lll,kkk,iii,2),auxmat(1,1))
 -              call matvec2(auxmat(1,1),b1(1,itl),
 +              call matvec2(auxmat(1,1),b1(1,l),
       &          AEAb1derx(1,lll,kkk,iii,1,2))
                call matvec2(auxmat(1,1),Ub2(1,l),
       &          AEAb2derx(1,lll,kkk,iii,1,2))
 -              call matvec2(AEAderx(1,1,lll,kkk,iii,2),b1(1,itj1),
 +              call matvec2(AEAderx(1,1,lll,kkk,iii,2),b1(1,j+1),
       &          AEAb1derx(1,lll,kkk,iii,2,2))
                call matvec2(AEAderx(1,1,lll,kkk,iii,2),Ub2(1,j),
       &          AEAb2derx(1,lll,kkk,iii,2,2))
@@@ -8600,7 -7671,7 +8768,7 @@@ C Contribution from graph I
        call matmat2(auxmat(1,1),AEA(1,1,1),pizda(1,1))
        vv(1)=pizda(1,1)+pizda(2,2)
        vv(2)=pizda(2,1)-pizda(1,2)
 -      eello5_2=scalar2(AEAb1(1,2,1),b1(1,itk))
 +      eello5_2=scalar2(AEAb1(1,2,1),b1(1,k))
       & -0.5d0*scalar2(vv(1),Ctobr(1,k))
  C Explicit gradient in virtual-dihedral angles.
        g_corr5_loc(k-1)=g_corr5_loc(k-1)
        vv(2)=pizda(2,1)-pizda(1,2)
        if (l.eq.j+1) then
          g_corr5_loc(l-1)=g_corr5_loc(l-1)
 -     &   +ekont*(scalar2(AEAb1derg(1,2,1),b1(1,itk))
 +     &   +ekont*(scalar2(AEAb1derg(1,2,1),b1(1,k))
       &   -0.5d0*scalar2(vv(1),Ctobr(1,k)))
        else
          g_corr5_loc(j-1)=g_corr5_loc(j-1)
 -     &   +ekont*(scalar2(AEAb1derg(1,2,1),b1(1,itk))
 +     &   +ekont*(scalar2(AEAb1derg(1,2,1),b1(1,k))
       &   -0.5d0*scalar2(vv(1),Ctobr(1,k)))
        endif
  C Cartesian gradient
              vv(1)=pizda(1,1)+pizda(2,2)
              vv(2)=pizda(2,1)-pizda(1,2)
              derx(lll,kkk,iii)=derx(lll,kkk,iii)
 -     &       +scalar2(AEAb1derx(1,lll,kkk,iii,2,1),b1(1,itk))
 +     &       +scalar2(AEAb1derx(1,lll,kkk,iii,2,1),b1(1,k))
       &       -0.5d0*scalar2(vv(1),Ctobr(1,k))
            enddo
          enddo
@@@ -8681,7 -7752,7 +8849,7 @@@ cd1110    continu
          call matmat2(auxmat(1,1),AEA(1,1,2),pizda(1,1))
          vv(1)=pizda(1,1)+pizda(2,2)
          vv(2)=pizda(2,1)-pizda(1,2)
 -        eello5_4=scalar2(AEAb1(1,2,2),b1(1,itl))
 +        eello5_4=scalar2(AEAb1(1,2,2),b1(1,l))
       &   -0.5d0*scalar2(vv(1),Ctobr(1,l))
  C Explicit gradient in virtual-dihedral angles.
          g_corr5_loc(l-1)=g_corr5_loc(l-1)
          vv(1)=pizda(1,1)+pizda(2,2)
          vv(2)=pizda(2,1)-pizda(1,2)
          g_corr5_loc(k-1)=g_corr5_loc(k-1)
 -     &   +ekont*(scalar2(AEAb1derg(1,2,2),b1(1,itl))
 +     &   +ekont*(scalar2(AEAb1derg(1,2,2),b1(1,l))
       &   -0.5d0*scalar2(vv(1),Ctobr(1,l)))
  C Cartesian gradient
          do iii=1,2
                vv(1)=pizda(1,1)+pizda(2,2)
                vv(2)=pizda(2,1)-pizda(1,2)
                derx(lll,kkk,iii)=derx(lll,kkk,iii)
 -     &         +scalar2(AEAb1derx(1,lll,kkk,iii,2,2),b1(1,itl))
 +     &         +scalar2(AEAb1derx(1,lll,kkk,iii,2,2),b1(1,l))
       &         -0.5d0*scalar2(vv(1),Ctobr(1,l))
              enddo
            enddo
@@@ -8754,7 -7825,7 +8922,7 @@@ C Contribution from graph I
          call matmat2(auxmat(1,1),AEA(1,1,2),pizda(1,1))
          vv(1)=pizda(1,1)+pizda(2,2)
          vv(2)=pizda(2,1)-pizda(1,2)
 -        eello5_4=scalar2(AEAb1(1,2,2),b1(1,itj))
 +        eello5_4=scalar2(AEAb1(1,2,2),b1(1,j))
       &   -0.5d0*scalar2(vv(1),Ctobr(1,j))
  C Explicit gradient in virtual-dihedral angles.
          g_corr5_loc(j-1)=g_corr5_loc(j-1)
          vv(1)=pizda(1,1)+pizda(2,2)
          vv(2)=pizda(2,1)-pizda(1,2)
          g_corr5_loc(k-1)=g_corr5_loc(k-1)
 -     &   +ekont*(scalar2(AEAb1derg(1,2,2),b1(1,itj))
 +     &   +ekont*(scalar2(AEAb1derg(1,2,2),b1(1,j))
       &   -0.5d0*scalar2(vv(1),Ctobr(1,j)))
  C Cartesian gradient
          do iii=1,2
                vv(1)=pizda(1,1)+pizda(2,2)
                vv(2)=pizda(2,1)-pizda(1,2)
                derx(lll,kkk,3-iii)=derx(lll,kkk,3-iii)
 -     &         +scalar2(AEAb1derx(1,lll,kkk,iii,2,2),b1(1,itj))
 +     &         +scalar2(AEAb1derx(1,lll,kkk,iii,2,2),b1(1,j))
       &         -0.5d0*scalar2(vv(1),Ctobr(1,j))
              enddo
            enddo
@@@ -8838,9 -7909,9 +9006,9 @@@ cd        ghalf=0.0d
  cold        ghalf=0.5d0*eel5*eij*gacont_hbr(ll,kk,k)
  cgrad        ghalf=0.5d0*ggg2(ll)
  cd        ghalf=0.0d0
-         gradcorr5(ll,k)=gradcorr5(ll,k)+ghalf+ekont*derx(ll,2,2)
+         gradcorr5(ll,k)=gradcorr5(ll,k)+ekont*derx(ll,2,2)
          gradcorr5(ll,k+1)=gradcorr5(ll,k+1)+ekont*derx(ll,3,2)
-         gradcorr5(ll,l)=gradcorr5(ll,l)+ghalf+ekont*derx(ll,4,2)
+         gradcorr5(ll,l)=gradcorr5(ll,l)+ekont*derx(ll,4,2)
          gradcorr5(ll,l1)=gradcorr5(ll,l1)+ekont*derx(ll,5,2)
          gradcorr5_long(ll,l)=gradcorr5_long(ll,l)+gradcorr5kl
          gradcorr5_long(ll,k)=gradcorr5_long(ll,k)-gradcorr5kl
@@@ -9056,8 -8127,8 +9224,8 @@@ CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
        vv1(1)=pizda1(1,1)-pizda1(2,2)
        vv1(2)=pizda1(1,2)+pizda1(2,1)
        s4=0.5d0*scalar2(vv1(1),Dtobr2(1,i))
 -      vv(1)=AEAb1(1,2,imat)*b1(1,itk)-AEAb1(2,2,imat)*b1(2,itk)
 -      vv(2)=AEAb1(1,2,imat)*b1(2,itk)+AEAb1(2,2,imat)*b1(1,itk)
 +      vv(1)=AEAb1(1,2,imat)*b1(1,k)-AEAb1(2,2,imat)*b1(2,k)
 +      vv(2)=AEAb1(1,2,imat)*b1(2,k)+AEAb1(2,2,imat)*b1(1,k)
        s5=scalar2(vv(1),Dtobr2(1,i))
  cd      write (2,*) 's1',s1,' s2',s2,' s3',s3,' s4', s4,' s5',s5
        eello6_graph1=-0.5d0*(s1+s2+s3+s4+s5)
        call matmat2(AEAderg(1,1,imat),auxmat(1,1),pizda1(1,1))
        vv1(1)=pizda1(1,1)-pizda1(2,2)
        vv1(2)=pizda1(1,2)+pizda1(2,1)
 -      vv(1)=AEAb1derg(1,2,imat)*b1(1,itk)-AEAb1derg(2,2,imat)*b1(2,itk)
 -      vv(2)=AEAb1derg(1,2,imat)*b1(2,itk)+AEAb1derg(2,2,imat)*b1(1,itk)
 +      vv(1)=AEAb1derg(1,2,imat)*b1(1,k)-AEAb1derg(2,2,imat)*b1(2,k)
 +      vv(2)=AEAb1derg(1,2,imat)*b1(2,k)+AEAb1derg(2,2,imat)*b1(1,k)
        if (l.eq.j+1) then
          g_corr6_loc(l-1)=g_corr6_loc(l-1)
       & +ekont*(-0.5d0*(scalar2(AEAb1derg(1,2,imat),CUgb2(1,i))
              vv1(1)=pizda1(1,1)-pizda1(2,2)
              vv1(2)=pizda1(1,2)+pizda1(2,1)
              s4=0.5d0*scalar2(vv1(1),Dtobr2(1,i))
 -            vv(1)=AEAb1derx(1,lll,kkk,iii,2,imat)*b1(1,itk)
 -     &       -AEAb1derx(2,lll,kkk,iii,2,imat)*b1(2,itk)
 -            vv(2)=AEAb1derx(1,lll,kkk,iii,2,imat)*b1(2,itk)
 -     &       +AEAb1derx(2,lll,kkk,iii,2,imat)*b1(1,itk)
 +            vv(1)=AEAb1derx(1,lll,kkk,iii,2,imat)*b1(1,k)
 +     &       -AEAb1derx(2,lll,kkk,iii,2,imat)*b1(2,k)
 +            vv(2)=AEAb1derx(1,lll,kkk,iii,2,imat)*b1(2,k)
 +     &       +AEAb1derx(2,lll,kkk,iii,2,imat)*b1(1,k)
              s5=scalar2(vv(1),Dtobr2(1,i))
              derx(lll,kkk,ind)=derx(lll,kkk,ind)-0.5d0*(s1+s2+s3+s4+s5)
            enddo
@@@ -9341,22 -8412,22 +9509,22 @@@ C           energy moment and not to th
        if (j.lt.nres-1) then
          itj1=itortyp(itype(j+1))
        else
 -        itj1=ntortyp+1
 +        itj1=ntortyp
        endif
        itk=itortyp(itype(k))
        itk1=itortyp(itype(k+1))
        if (l.lt.nres-1) then
          itl1=itortyp(itype(l+1))
        else
 -        itl1=ntortyp+1
 +        itl1=ntortyp
        endif
  #ifdef MOMENT
        s1=dip(4,jj,i)*dip(4,kk,k)
  #endif
 -      call matvec2(AECA(1,1,1),b1(1,itk1),auxvec(1))
 -      s2=0.5d0*scalar2(b1(1,itk),auxvec(1))
 -      call matvec2(AECA(1,1,2),b1(1,itl1),auxvec(1))
 -      s3=0.5d0*scalar2(b1(1,itj1),auxvec(1))
 +      call matvec2(AECA(1,1,1),b1(1,k+1),auxvec(1))
 +      s2=0.5d0*scalar2(b1(1,k),auxvec(1))
 +      call matvec2(AECA(1,1,2),b1(1,l+1),auxvec(1))
 +      s3=0.5d0*scalar2(b1(1,j+1),auxvec(1))
        call transpose2(EE(1,1,itk),auxmat(1,1))
        call matmat2(auxmat(1,1),AECA(1,1,1),pizda(1,1))
        vv(1)=pizda(1,1)+pizda(2,2)
@@@ -9371,13 -8442,13 +9539,13 @@@ cd     & "sum",-(s2+s3+s4
  #endif
  c      eello6_graph3=-s4
  C Derivatives in gamma(k-1)
 -      call matvec2(AECAderg(1,1,2),b1(1,itl1),auxvec(1))
 -      s3=0.5d0*scalar2(b1(1,itj1),auxvec(1))
 +      call matvec2(AECAderg(1,1,2),b1(1,l+1),auxvec(1))
 +      s3=0.5d0*scalar2(b1(1,j+1),auxvec(1))
        s4=-0.25d0*scalar2(vv(1),Ctobrder(1,k))
        g_corr6_loc(k-1)=g_corr6_loc(k-1)-ekont*(s3+s4)
  C Derivatives in gamma(l-1)
 -      call matvec2(AECAderg(1,1,1),b1(1,itk1),auxvec(1))
 -      s2=0.5d0*scalar2(b1(1,itk),auxvec(1))
 +      call matvec2(AECAderg(1,1,1),b1(1,k+1),auxvec(1))
 +      s2=0.5d0*scalar2(b1(1,k),auxvec(1))
        call matmat2(auxmat(1,1),AECAderg(1,1,1),pizda(1,1))
        vv(1)=pizda(1,1)+pizda(2,2)
        vv(2)=pizda(2,1)-pizda(1,2)
@@@ -9394,12 -8465,12 +9562,12 @@@ C Cartesian derivatives
                s1=dip(4,jj,i)*dipderx(lll,kkk,4,kk,k)
              endif
  #endif
 -            call matvec2(AECAderx(1,1,lll,kkk,iii,1),b1(1,itk1),
 +            call matvec2(AECAderx(1,1,lll,kkk,iii,1),b1(1,k+1),
       &        auxvec(1))
 -            s2=0.5d0*scalar2(b1(1,itk),auxvec(1))
 -            call matvec2(AECAderx(1,1,lll,kkk,iii,2),b1(1,itl1),
 +            s2=0.5d0*scalar2(b1(1,k),auxvec(1))
 +            call matvec2(AECAderx(1,1,lll,kkk,iii,2),b1(1,l+1),
       &        auxvec(1))
 -            s3=0.5d0*scalar2(b1(1,itj1),auxvec(1))
 +            s3=0.5d0*scalar2(b1(1,j+1),auxvec(1))
              call matmat2(auxmat(1,1),AECAderx(1,1,lll,kkk,iii,1),
       &        pizda(1,1))
              vv(1)=pizda(1,1)+pizda(2,2)
@@@ -9460,19 -8531,19 +9628,19 @@@ cd      write (2,*) 'eello_graph4: wtur
        if (j.lt.nres-1) then
          itj1=itortyp(itype(j+1))
        else
 -        itj1=ntortyp+1
 +        itj1=ntortyp
        endif
        itk=itortyp(itype(k))
        if (k.lt.nres-1) then
          itk1=itortyp(itype(k+1))
        else
 -        itk1=ntortyp+1
 +        itk1=ntortyp
        endif
        itl=itortyp(itype(l))
        if (l.lt.nres-1) then
          itl1=itortyp(itype(l+1))
        else
 -        itl1=ntortyp+1
 +        itl1=ntortyp
        endif
  cd      write (2,*) 'eello6_graph4:','i',i,' j',j,' k',k,' l',l
  cd      write (2,*) 'iti',iti,' itj',itj,' itj1',itj1,' itk',itk,
@@@ -9487,11 -8558,11 +9655,11 @@@ cd     & ' itl',itl,' itl1',itl
        call matvec2(AECA(1,1,imat),Ub2(1,k),auxvec(1))
        s2=0.5d0*scalar2(Ub2(1,i),auxvec(1))
        if (j.eq.l+1) then
 -        call matvec2(ADtEA1(1,1,3-imat),b1(1,itj1),auxvec1(1))
 -        s3=-0.5d0*scalar2(b1(1,itj),auxvec1(1))
 +        call matvec2(ADtEA1(1,1,3-imat),b1(1,j+1),auxvec1(1))
 +        s3=-0.5d0*scalar2(b1(1,j),auxvec1(1))
        else
 -        call matvec2(ADtEA1(1,1,3-imat),b1(1,itl1),auxvec1(1))
 -        s3=-0.5d0*scalar2(b1(1,itl),auxvec1(1))
 +        call matvec2(ADtEA1(1,1,3-imat),b1(1,l+1),auxvec1(1))
 +        s3=-0.5d0*scalar2(b1(1,l),auxvec1(1))
        endif
        call transpose2(EUg(1,1,k),auxmat(1,1))
        call matmat2(AECA(1,1,imat),auxmat(1,1),pizda(1,1))
@@@ -9515,11 -8586,11 +9683,11 @@@ C Derivatives in gamma(i-1
  #endif
          s2=0.5d0*scalar2(Ub2der(1,i),auxvec(1))
          if (j.eq.l+1) then
 -          call matvec2(ADtEA1derg(1,1,1,3-imat),b1(1,itj1),auxvec1(1))
 -          s3=-0.5d0*scalar2(b1(1,itj),auxvec1(1))
 +          call matvec2(ADtEA1derg(1,1,1,3-imat),b1(1,j+1),auxvec1(1))
 +          s3=-0.5d0*scalar2(b1(1,j),auxvec1(1))
          else
 -          call matvec2(ADtEA1derg(1,1,1,3-imat),b1(1,itl1),auxvec1(1))
 -          s3=-0.5d0*scalar2(b1(1,itl),auxvec1(1))
 +          call matvec2(ADtEA1derg(1,1,1,3-imat),b1(1,l+1),auxvec1(1))
 +          s3=-0.5d0*scalar2(b1(1,l),auxvec1(1))
          endif
          s4=0.25d0*scalar2(vv(1),Dtobr2der(1,i))
          if (wturn6.gt.0.0d0 .and. k.eq.l+4 .and. i.eq.j+2) then
@@@ -9548,11 -8619,11 +9716,11 @@@ C Derivatives in gamma(k-1
        call matvec2(AECA(1,1,imat),Ub2der(1,k),auxvec1(1))
        s2=0.5d0*scalar2(Ub2(1,i),auxvec1(1))
        if (j.eq.l+1) then
 -        call matvec2(ADtEA1derg(1,1,2,3-imat),b1(1,itj1),auxvec1(1))
 -        s3=-0.5d0*scalar2(b1(1,itj),auxvec1(1))
 +        call matvec2(ADtEA1derg(1,1,2,3-imat),b1(1,j+1),auxvec1(1))
 +        s3=-0.5d0*scalar2(b1(1,j),auxvec1(1))
        else
 -        call matvec2(ADtEA1derg(1,1,2,3-imat),b1(1,itl1),auxvec1(1))
 -        s3=-0.5d0*scalar2(b1(1,itl),auxvec1(1))
 +        call matvec2(ADtEA1derg(1,1,2,3-imat),b1(1,l+1),auxvec1(1))
 +        s3=-0.5d0*scalar2(b1(1,l),auxvec1(1))
        endif
        call transpose2(EUgder(1,1,k),auxmat1(1,1))
        call matmat2(AECA(1,1,imat),auxmat1(1,1),pizda(1,1))
@@@ -9618,12 -8689,12 +9786,12 @@@ C Cartesian derivatives
              s2=0.5d0*scalar2(Ub2(1,i),auxvec(1))
              if (j.eq.l+1) then
                call matvec2(ADtEA1derx(1,1,lll,kkk,iii,3-imat),
 -     &          b1(1,itj1),auxvec(1))
 -              s3=-0.5d0*scalar2(b1(1,itj),auxvec(1))
 +     &          b1(1,j+1),auxvec(1))
 +              s3=-0.5d0*scalar2(b1(1,j),auxvec(1))
              else
                call matvec2(ADtEA1derx(1,1,lll,kkk,iii,3-imat),
 -     &          b1(1,itl1),auxvec(1))
 -              s3=-0.5d0*scalar2(b1(1,itl),auxvec(1))
 +     &          b1(1,l+1),auxvec(1))
 +              s3=-0.5d0*scalar2(b1(1,l),auxvec(1))
              endif
              call matmat2(AECAderx(1,1,lll,kkk,iii,imat),auxmat(1,1),
       &        pizda(1,1))
@@@ -9723,12 -8794,12 +9891,12 @@@ cd      write (2,*) 'eello6_5',eello6_
  #ifdef MOMENT
        call transpose2(AEA(1,1,1),auxmat(1,1))
        call matmat2(EUg(1,1,i+1),auxmat(1,1),auxmat(1,1))
 -      ss1=scalar2(Ub2(1,i+2),b1(1,itl))
 +      ss1=scalar2(Ub2(1,i+2),b1(1,l))
        s1 = (auxmat(1,1)+auxmat(2,2))*ss1
  #endif
 -      call matvec2(EUg(1,1,i+2),b1(1,itl),vtemp1(1))
 +      call matvec2(EUg(1,1,i+2),b1(1,l),vtemp1(1))
        call matvec2(AEA(1,1,1),vtemp1(1),vtemp1(1))
 -      s2 = scalar2(b1(1,itk),vtemp1(1))
 +      s2 = scalar2(b1(1,k),vtemp1(1))
  #ifdef MOMENT
        call transpose2(AEA(1,1,2),atemp(1,1))
        call matmat2(atemp(1,1),EUg(1,1,i+4),atemp(1,1))
        call matmat2(achuj_temp(1,1),EUg(1,1,i+2),gtemp(1,1))
        call matmat2(gtemp(1,1),EUg(1,1,i+3),gtemp(1,1)) 
        call matvec2(a_chuj(1,1,jj,i),Ub2(1,i+4),vtemp4(1)) 
 -      ss13 = scalar2(b1(1,itk),vtemp4(1))
 +      ss13 = scalar2(b1(1,k),vtemp4(1))
        s13 = (gtemp(1,1)+gtemp(2,2))*ss13
  #endif
  c      write (2,*) 's1,s2,s8,s12,s13',s1,s2,s8,s12,s13
@@@ -9777,12 -8848,12 +9945,12 @@@ C Derivatives in gamma(i+3
  #ifdef MOMENT
        call transpose2(AEA(1,1,1),auxmatd(1,1))
        call matmat2(EUg(1,1,i+1),auxmatd(1,1),auxmatd(1,1))
 -      ss1d=scalar2(Ub2der(1,i+2),b1(1,itl))
 +      ss1d=scalar2(Ub2der(1,i+2),b1(1,l))
        s1d = (auxmatd(1,1)+auxmatd(2,2))*ss1d
  #endif
 -      call matvec2(EUgder(1,1,i+2),b1(1,itl),vtemp1d(1))
 +      call matvec2(EUgder(1,1,i+2),b1(1,l),vtemp1d(1))
        call matvec2(AEA(1,1,1),vtemp1d(1),vtemp1d(1))
 -      s2d = scalar2(b1(1,itk),vtemp1d(1))
 +      s2d = scalar2(b1(1,k),vtemp1d(1))
  #ifdef MOMENT
        call matvec2(Ug2der(1,1,i+2),dd(1,1,itk1),vtemp2d(1))
        s8d = -(atemp(1,1)+atemp(2,2))*scalar2(cc(1,1,itl),vtemp2d(1))
@@@ -9830,9 -8901,9 +9998,9 @@@ C Derivatives in gamma(i+5
        call matmat2(EUg(1,1,i+1),auxmatd(1,1),auxmatd(1,1))
        s1d = (auxmatd(1,1)+auxmatd(2,2))*ss1
  #endif
 -      call matvec2(EUg(1,1,i+2),b1(1,itl),vtemp1d(1))
 +      call matvec2(EUg(1,1,i+2),b1(1,l),vtemp1d(1))
        call matvec2(AEAderg(1,1,1),vtemp1d(1),vtemp1d(1))
 -      s2d = scalar2(b1(1,itk),vtemp1d(1))
 +      s2d = scalar2(b1(1,k),vtemp1d(1))
  #ifdef MOMENT
        call transpose2(AEA(1,1,2),atempd(1,1))
        call matmat2(atempd(1,1),EUgder(1,1,i+4),atempd(1,1))
        s12d = scalar2(Ub2(1,i+2),vtemp3d(1))
  #ifdef MOMENT
        call matvec2(a_chuj(1,1,jj,i),Ub2der(1,i+4),vtemp4d(1)) 
 -      ss13d = scalar2(b1(1,itk),vtemp4d(1))
 +      ss13d = scalar2(b1(1,k),vtemp4d(1))
        s13d = (gtemp(1,1)+gtemp(2,2))*ss13d
  #endif
  c      s1d=0.0d0
@@@ -9866,10 -8937,10 +10034,10 @@@ C Cartesian derivative
              call matmat2(EUg(1,1,i+1),auxmatd(1,1),auxmatd(1,1))
              s1d = (auxmatd(1,1)+auxmatd(2,2))*ss1
  #endif
 -            call matvec2(EUg(1,1,i+2),b1(1,itl),vtemp1(1))
 +            call matvec2(EUg(1,1,i+2),b1(1,l),vtemp1(1))
              call matvec2(AEAderx(1,1,lll,kkk,iii,1),vtemp1(1),
       &          vtemp1d(1))
 -            s2d = scalar2(b1(1,itk),vtemp1d(1))
 +            s2d = scalar2(b1(1,k),vtemp1d(1))
  #ifdef MOMENT
              call transpose2(AEAderx(1,1,lll,kkk,iii,2),atempd(1,1))
              call matmat2(atempd(1,1),EUg(1,1,i+4),atempd(1,1))
@@@ -9913,7 -8984,7 +10081,7 @@@ c      s13d=0.0d
            derx_turn(lll,kkk,2) = derx_turn(lll,kkk,2)-0.5d0*s13d
            call matvec2(a_chuj_der(1,1,lll,kkk,jj,i),Ub2(1,i+4),
       &      vtemp4d(1)) 
 -          ss13d = scalar2(b1(1,itk),vtemp4d(1))
 +          ss13d = scalar2(b1(1,k),vtemp4d(1))
            s13d = (gtemp(1,1)+gtemp(2,2))*ss13d
            derx_turn(lll,kkk,1) = derx_turn(lll,kkk,1)-0.5d0*s13d
          enddo
@@@ -10149,199 -9220,4 +10317,199 @@@ crc      print *,((prod(i,j),i=1,2),j=1
  
        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
  
@@@ -97,7 -97,7 +97,7 @@@ cmodel      write (iunit,'(a5,i6)') 'MO
        ires=0
        do i=nnt,nct
          iti=itype(i)
 -        if (iti.eq.ntyp1) then
 +        if ((iti.eq.ntyp1).and.((itype(i+1)).eq.ntyp1)) then
            ichain=ichain+1
            ires=0
            write (iunit,'(a)') 'TER'
          ires=ires+1
          iatom=iatom+1
          ica(i)=iatom
 +        if (iti.ne.ntyp1) then
          write (iunit,10) iatom,restyp(iti),chainid(ichain),
       &     ires,(c(j,i),j=1,3),vtot(i)
          if (iti.ne.10) then
            write (iunit,20) iatom,restyp(iti),chainid(ichain),
       &      ires,(c(j,nres+i),j=1,3),
       &      vtot(i+nres)
 +         endif
          endif
          endif
        enddo
@@@ -425,6 -423,7 +425,7 @@@ c--------------------------------------
        include 'COMMON.SBRIDGE'
        include 'COMMON.DISTFIT'
        include 'COMMON.MD'
+       include 'COMMON.REMD'
        include 'COMMON.SETUP'
        integer itime
        double precision energia(0:n_ene)
        open(istat,file=statname,access="append")
  #endif
  #endif
 +       if (AFMlog.gt.0) then
 +       if (refstr) then
 +         call rms_nac_nnc(rms,frac,frac_nn,co,.false.)
 +          write (line1,'(i10,f15.2,3f12.3,f7.2,2f6.3,4f12.3,i5,$)')
 +     &          itime,totT,EK,potE,totE,
 +     &          rms,frac,frac_nn,kinetic_T,t_bath,gyrate(),
 +     &          potEcomp(23),me
 +          format1="a133"
 +         else
 +C          print *,'A CHUJ',potEcomp(23)
 +          write (line1,'(i10,f15.2,7f12.3,i5,$)')
 +     &           itime,totT,EK,potE,totE,
 +     &           kinetic_T,t_bath,gyrate(),
 +     &           potEcomp(23),me
 +          format1="a114"
 +        endif
 +       else if (selfguide.gt.0) then
 +       distance=0.0
 +       do j=1,3
 +       distance=distance+(c(j,afmend)-c(j,afmbeg))**2
 +       enddo
 +       distance=dsqrt(distance)
 +       if (refstr) then
 +         call rms_nac_nnc(rms,frac,frac_nn,co,.false.)
 +          write (line1,'(i10,f15.2,3f12.3,f7.2,2f6.3,f12.3,f10.1,2f8.2,
 +     &    f9.2,i5,$)')
 +     &          itime,totT,EK,potE,totE,
 +     &          rms,frac,frac_nn,kinetic_T,t_bath,gyrate(),
 +     &          distance,potEcomp(23),me
 +          format1="a133"
 +C          print *,"CHUJOWO"
 +         else
 +C          print *,'A CHUJ',potEcomp(23)
 +          write (line1,'(i10,f15.2,8f12.3,i5,$)')
 +     &           itime,totT,EK,potE,totE,
 +     &           kinetic_T,t_bath,gyrate(),
 +     &           distance,potEcomp(23),me
 +          format1="a114"
 +        endif
 +       else
         if (refstr) then
           call rms_nac_nnc(rms,frac,frac_nn,co,.false.)
            write (line1,'(i10,f15.2,3f12.3,f7.2,4f6.3,3f12.3,i5,$)')
       &           amax,kinetic_T,t_bath,gyrate(),me
            format1="a114"
          endif
 +        endif
          if(usampl.and.totT.gt.eq_time) then
             write(line2,'(i5,2f9.4,300f7.4)') iset,uconst,uconst_back,
       &      (qfrag(ii1),ii1=1,nfrag),(qpair(ii2),ii2=1,npair),
             line2=' '
          endif
          if (print_compon) then
+           if(itime.eq.0) then
+            write(format,'(a1,a4,a1,a4,a10)') "(",format1,",",format2,
+      &                                                     ",20a12)"
+            write (istat,format) "#","",
+      &      (ename(print_order(i)),i=1,nprint_ene)
+           endif
            write(format,'(a1,a4,a1,a4,a10)') "(",format1,",",format2,
       &                                                     ",20f12.3)"
            write (istat,format) line1,line2,
        icsa_in=40
  crc for ifc error 118
        icsa_pdb=42
 +C Lipidic input file for parameters range 60-79
 +      iliptranpar=60
 +C input file for transfer sidechain and peptide group inside the
 +C lipidic environment if lipid is implicite
 +
 +C DNA input files for parameters range 80-99
 +C Suger input files for parameters range 100-119
 +C All-atom input files for parameters range 120-149
  C
  C Set default weights of the energy terms.
  C
@@@ -154,10 -146,8 +154,10 @@@ c      call memmon_print_usage(
        enddo
        do i=1,ntyp
        do j=1,ntyp
 -        aa(i,j)=0.0D0
 -        bb(i,j)=0.0D0
 +        aa_aq(i,j)=0.0D0
 +        bb_aq(i,j)=0.0D0
 +          aa_lip(i,j)=0.0D0
 +          bb_lip(i,j)=0.0D0
          augm(i,j)=0.0D0
          sigma(i,j)=0.0D0
          r0(i,j)=0.0D0
@@@ -255,32 -245,6 +255,32 @@@ C Initialize the bridge array
        ihpb(i)=0
        jhpb(i)=0
        enddo
 +C Initialize correlation arrays
 +      do i=1,maxres
 +       do k=1,2
 +        b1(k,i)=0.0
 +        b2(k,i)=0.0
 +        b1tilde(k,i)=0.0
 +c        b2tilde(k,i)=0.0
 +        do j=1,2
 +C        CC(j,k,i)=0.0
 +C        Ctilde(j,k,i)=0.0
 +C        DD(j,k,i)=0.0
 +C        Dtilde(j,k,i)=0.0
 +        EE(j,k,i)=0.0
 +        enddo
 +       enddo
 +      enddo
 +      do i=-maxtor,maxtor
 +       do k=1,2
 +        do j=1,2
 +        CC(j,k,i)=0.0
 +        Ctilde(j,k,i)=0.0
 +        DD(j,k,i)=0.0
 +        Dtilde(j,k,i)=0.0
 +        enddo
 +      enddo
 +      enddo
  C
  C Initialize timing.
  C
  C Initialize constants used to split the energy into long- and short-range
  C components
  C
 -      r_cut=2.0d0
 -      rlamb=0.3d0
 +C      r_cut=2.0d0
 +C      rlamb=0.3d0
  #ifndef SPLITELE
        nprint_ene=nprint_ene-1
  #endif
@@@ -380,7 -344,7 +380,7 @@@ C... to deal with by current processor
          itask_cont_from(i)=fg_rank
          itask_cont_to(i)=fg_rank
        enddo
-       lprint=.false.
+       lprint=energy_dec
        if (lprint)
       &write (iout,*) 'INIT_INT_TABLE nres=',nres,' nnt=',nnt,' nct=',nct
        n_sc_int_tot=(nct-nnt+1)*(nct-nnt)/2-nss
@@@ -479,6 -443,7 +479,7 @@@ c            write (iout,*) 'jj=nct
        iatsc_s=nnt
        iatsc_e=nct-1
  #endif
+       if (iatsc_s.eq.0) iatsc_s=1
  #ifdef MPI
        if (lprint) write (*,*) 'Processor',fg_rank,' CG Group',kolor,
       &   ' absolute rank',myrank,' iatsc_s=',iatsc_s,' iatsc_e=',iatsc_e
@@@ -611,6 -576,7 +612,7 @@@ cd        write (iout,*) 'i.gt.nct-iscp
          endif 
        enddo ! i
  #endif
+       if (iatscp_s.eq.0) iatscp_s=1
        if (lprint) then
          write (iout,'(a)') 'SC-p interaction array:'
          do i=iatscp_s,iatscp_e
@@@ -652,8 -618,6 +654,8 @@@ C Partition local interaction
        call int_bounds(nct-nnt,ibondp_start,ibondp_end) 
        ibondp_start=ibondp_start+nnt
        ibondp_end=ibondp_end+nnt
 +      call int_bounds(nres,ilip_start,ilip_end)
 +      ilip_start=ilip_start
        call int_bounds1(nres-1,ivec_start,ivec_end) 
  c      print *,"Processor",myrank,fg_rank,fg_rank1,
  c     &  " ivec_start",ivec_start," ivec_end",ivec_end
        else
          call int_bounds(ndih_constr,idihconstr_start,idihconstr_end)
        endif
+       if (ntheta_constr.eq.0) then
+         idihconstr_start=1
+         idihconstr_end=0
+       else
+         call int_bounds
+      &  (ntheta_constr,ithetaconstr_start,ithetaconstr_end)
+       endif
  c      nsumgrad=(nres-nnt)*(nres-nnt+1)/2
  c      nlen=nres-nnt+1
        nsumgrad=(nres-nnt)*(nres-nnt+1)/2
       & ' ivec_start',ivec_start,' ivec_end',ivec_end,
       & ' iset_start',iset_start,' iset_end',iset_end,
       & ' idihconstr_start',idihconstr_start,' idihconstr_end',
-      &   idihconstr_end
+      &   idihconstr_end,
+      & ' ithetaconstr_start',ithetaconstr_start,' ithetaconstr_end',
+      &   ithetaconstr_end
         write (*,*) 'Processor:',fg_rank,myrank,' igrad_start',
       &   igrad_start,' igrad_end',igrad_end,' ngrad_start',ngrad_start,
       &   ' ngrad_end',ngrad_end
@@@ -1164,6 -1138,8 +1176,8 @@@ c        write (iout,*) "MPI_ROTAT2",MP
        iphi1_end=nres
        idihconstr_start=1
        idihconstr_end=ndih_constr
+       ithetaconstr_start=1
+       ithetaconstr_end=ntheta_constr
        iphid_start=iphi_start
        iphid_end=iphi_end-1
        itau_start=4
        ibond_start=2
        ibond_end=nres-1
        ibondp_start=nnt
 -      ibondp_end=nct-1
 +C      ibondp_end=nct-1
 +      ibondp_end=nct
        ivec_start=1
        ivec_end=nres-1
        iset_start=3
        iset_end=nres+1
        iint_start=2
        iint_end=nres-1
 +      ilip_start=1
 +      ilip_end=nres
  #endif
        return
        end 
@@@ -48,12 -48,10 +48,12 @@@ c We need dtheta(:,:,i-1) to compute dp
          do j=1,3
            dcostheta(j,1,i)=-(dc_norm(j,i-1)+cost*dc_norm(j,i-2))/
       &          vbld(i-1)
 -          if (itype(i-1).ne.ntyp1) dtheta(j,1,i)=-dcostheta(j,1,i)/sint
 +c          if (itype(i-1).ne.ntyp1)
 +          dtheta(j,1,i)=-dcostheta(j,1,i)/sint
            dcostheta(j,2,i)=-(dc_norm(j,i-2)+cost*dc_norm(j,i-1))/
       &          vbld(i)
 -          if (itype(i-1).ne.ntyp1) dtheta(j,2,i)=-dcostheta(j,2,i)/sint
 +c          if (itype(i-1).ne.ntyp1)
 +          dtheta(j,2,i)=-dcostheta(j,2,i)/sint
          enddo
        enddo
  #if defined(MPI) && defined(PARINTDER)
@@@ -101,8 -99,7 +101,8 @@@ c conventional formulas around 0 and 18
  #else
        do i=4,nres      
  #endif
 -c        if (itype(i-1).eq.21 .or. itype(i-2).eq.21 ) cycle
 +c        if (itype(i-2).eq.ntyp1.or. itype(i-1).eq.ntyp1
 +c     &      .or. itype(i).eq.ntyp1 .or. itype(i-3).eq.ntyp1) cycle
  c the conventional case
          sint=dsin(theta(i))
                sint1=dsin(theta(i-1))
@@@ -127,8 -124,8 +127,8 @@@ c    Obtaining the gamma derivatives fr
              ctgt=cost/sint
              ctgt1=cost1/sint1
              cosg_inv=1.0d0/cosg
 -            if (itype(i-1).ne.ntyp1 .and. itype(i-2).ne.ntyp1) then
 -          dsinphi(j,1,i)=-sing*ctgt1*dtheta(j,1,i-1)
 +c            if (itype(i-1).ne.ntyp1 .and. itype(i-2).ne.ntyp1) then
 +      dsinphi(j,1,i)=-sing*ctgt1*dtheta(j,1,i-1)
       &        -(fac0*vp1(j)+sing*dc_norm(j,i-3))*vbld_inv(i-2)
              dphi(j,1,i)=cosg_inv*dsinphi(j,1,i)
              dsinphi(j,2,i)=
       &        +(fac0*vp3(j)-sing*dc_norm(j,i-1))*vbld_inv(i)
  c     &        +(fac0*vp3(j)-sing*dc_norm(j,i-1))*vbld_inv(i-1)
              dphi(j,3,i)=cosg_inv*dsinphi(j,3,i)
 -            endif
 +c            endif
  c Bug fixed 3/24/05 (AL)
         enddo                                              
  c   Obtaining the gamma derivatives from cosine derivative
          else
             do j=1,3
 -           if (itype(i-1).ne.ntyp1 .and. itype(i-2).ne.ntyp1) then
 +c           if (itype(i-1).ne.ntyp1 .and. itype(i-2).ne.ntyp1) then
             dcosphi(j,1,i)=fac1*dcostheta(j,1,i-1)+fac3*
       &           dcostheta(j,1,i-1)-fac0*(dc_norm(j,i-1)-scalp*
       &     dc_norm(j,i-3))/vbld(i-2)
       &           dcostheta(j,2,i)-fac0*(dc_norm(j,i-3)-scalp*
       &     dc_norm(j,i-1))/vbld(i)
             dphi(j,3,i)=-1/sing*dcosphi(j,3,i)       
 -           endif
 +c           endif
           enddo
          endif                                                                                          
        enddo
  Calculate derivative of Tauangle
+       do i=1,nres-1
+        do j=1,3
+         dc_norm2(j,i+nres)=-dc_norm(j,i+nres)
+        enddo
+       enddo
  #ifdef PARINTDER
        do i=itau_start,itau_end
  #else
@@@ -695,7 -697,7 +700,7 @@@ c     Check omega gradien
        enddo
        return
        end
 -
 +c------------------------------------------------------------
        subroutine chainbuild_cart
        implicit real*8 (a-h,o-z)
        include 'DIMENSIONS'
@@@ -739,7 -741,6 +744,7 @@@ c        call flush(iout
  #endif
        do j=1,3
          c(j,1)=dc(j,0)
 +c        c(j,1)=c(j,1)
        enddo
        do i=2,nres
          do j=1,3
            c(j,i+nres)=c(j,i)+dc(j,i+nres)
          enddo
        enddo
 +C      print *,'tutu'
  c      write (iout,*) "CHAINBUILD_CART"
  c      call cartprint
        call int_from_cart1(.false.)
@@@ -31,7 -31,7 +31,7 @@@
        character*1 toronelet(-2:2) /"p","a","G","A","P"/
        logical lprint,LaTeX
        dimension blower(3,3,maxlob)
 -      dimension b(13)
 +C      dimension b(13)
        character*3 lancuch,ucase
  C
  C For printing parameters after they are read set the following in the UNRES
@@@ -59,7 -59,7 +59,7 @@@ c Read the virtual-bond parameters, mas
  c and Stokes' radii of the peptide group and side chains
  c
  #ifdef CRYST_BOND
 -      read (ibond,*) vbldp0,akp,mp,ip,pstok
 +      read (ibond,*) vbldp0,vbldpdum,akp,mp,ip,pstok
        do i=1,ntyp
          nbondterm(i)=1
          read (ibond,*) vbldsc0(1,i),aksc(1,i),msc(i),isc(i),restok(i)
@@@ -71,8 -71,9 +71,9 @@@
          endif
        enddo
  #else
 -      read (ibond,*) junk,vbldp0,akp,rjunk,mp,ip,pstok
 +      read (ibond,*) junk,vbldp0,vbldpdum,akp,rjunk,mp,ip,pstok
        do i=1,ntyp
+       print *,i
          read (ibond,*) nbondterm(i),(vbldsc0(j,i),aksc(j,i),abond0(j,i),
       &   j=1,nbondterm(i)),msc(i),isc(i),restok(i)
          dsc(i) = vbldsc0(1,i)
            enddo
          enddo
        endif
 +C reading lipid parameters
 +       read(iliptranpar,*) pepliptran
 +       do i=1,ntyp
 +       read(iliptranpar,*) liptranene(i)
 +       enddo
 +       close(iliptranpar)
  #ifdef CRYST_THETA
  C
  C Read the parameters of the probability distribution/energy expression 
          write (iout,*) "Coefficients of the cumulants"
        endif
        read (ifourier,*) nloctyp
 +
        do i=0,nloctyp-1
          read (ifourier,*,end=115,err=115)
 -        read (ifourier,*,end=115,err=115) (b(ii),ii=1,13)
 +        read (ifourier,*,end=115,err=115) (b(ii,i),ii=1,13)
 +#ifdef NEWCORR
 +        read (ifourier,*,end=115,err=115) (bnew1(ii,1,i),ii=1,3)
 +        read (ifourier,*,end=115,err=115) (bnew2(ii,1,i),ii=1,3)
 +        read (ifourier,*,end=115,err=115) (bnew1(ii,2,i),ii=1,1)
 +        read (ifourier,*,end=115,err=115) (bnew2(ii,2,i),ii=1,1)
 +        read (ifourier,*,end=115,err=115) (eenew(ii,i),ii=1,1)
 +#endif 
          if (lprint) then
          write (iout,*) 'Type',i
 -        write (iout,'(a,i2,a,f10.5)') ('b(',ii,')=',b(ii),ii=1,13)
 +        write (iout,'(a,i2,a,f10.5)') ('b(',ii,')=',b(ii,i),ii=1,13)
          endif
 -        B1(1,i)  = b(3)
 -        B1(2,i)  = b(5)
 -        B1(1,-i) = b(3)
 -        B1(2,-i) = -b(5)
 +c        B1(1,i)  = b(3)
 +c        B1(2,i)  = b(5)
 +c        B1(1,-i) = b(3)
 +c        B1(2,-i) = -b(5)
  c        b1(1,i)=0.0d0
  c        b1(2,i)=0.0d0
 +c        B1tilde(1,i) = b(3)
 +c        B1tilde(2,i) =-b(5)
 +c        B1tilde(1,-i) =-b(3)
 +c        B1tilde(2,-i) =b(5)
 +c        b1tilde(1,i)=0.0d0
 +c        b1tilde(2,i)=0.0d0
 +c        B2(1,i)  = b(2)
 +c        B2(2,i)  = b(4)
 +c        B2(1,-i)  =b(2)
 +c        B2(2,-i)  =-b(4)
+         B1tilde(1,i) = b(3)
+         B1tilde(2,i) =-b(5)
+         B1tilde(1,-i) =-b(3)
+         B1tilde(2,-i) =b(5)
+         b1tilde(1,i)=0.0d0
+         b1tilde(2,i)=0.0d0
+         B2(1,i)  = b(2)
+         B2(2,i)  = b(4)
+         B2(1,-i)  =b(2)
+         B2(2,-i)  =-b(4)
  
  c        b2(1,i)=0.0d0
  c        b2(2,i)=0.0d0
 -        CC(1,1,i)= b(7)
 -        CC(2,2,i)=-b(7)
 -        CC(2,1,i)= b(9)
 -        CC(1,2,i)= b(9)
 -        CC(1,1,-i)= b(7)
 -        CC(2,2,-i)=-b(7)
 -        CC(2,1,-i)=-b(9)
 -        CC(1,2,-i)=-b(9)
 +        CC(1,1,i)= b(7,i)
 +        CC(2,2,i)=-b(7,i)
 +        CC(2,1,i)= b(9,i)
 +        CC(1,2,i)= b(9,i)
 +        CC(1,1,-i)= b(7,i)
 +        CC(2,2,-i)=-b(7,i)
 +        CC(2,1,-i)=-b(9,i)
 +        CC(1,2,-i)=-b(9,i)
  c        CC(1,1,i)=0.0d0
  c        CC(2,2,i)=0.0d0
  c        CC(2,1,i)=0.0d0
  c        CC(1,2,i)=0.0d0
 -        Ctilde(1,1,i)=b(7)
 -        Ctilde(1,2,i)=b(9)
 -        Ctilde(2,1,i)=-b(9)
 -        Ctilde(2,2,i)=b(7)
 -        Ctilde(1,1,-i)=b(7)
 -        Ctilde(1,2,-i)=-b(9)
 -        Ctilde(2,1,-i)=b(9)
 -        Ctilde(2,2,-i)=b(7)
 +        Ctilde(1,1,i)=b(7,i)
 +        Ctilde(1,2,i)=b(9,i)
 +        Ctilde(2,1,i)=-b(9,i)
 +        Ctilde(2,2,i)=b(7,i)
 +        Ctilde(1,1,-i)=b(7,i)
 +        Ctilde(1,2,-i)=-b(9,i)
 +        Ctilde(2,1,-i)=b(9,i)
 +        Ctilde(2,2,-i)=b(7,i)
  
  c        Ctilde(1,1,i)=0.0d0
  c        Ctilde(1,2,i)=0.0d0
  c        Ctilde(2,1,i)=0.0d0
  c        Ctilde(2,2,i)=0.0d0
 -        DD(1,1,i)= b(6)
 -        DD(2,2,i)=-b(6)
 -        DD(2,1,i)= b(8)
 -        DD(1,2,i)= b(8)
 -        DD(1,1,-i)= b(6)
 -        DD(2,2,-i)=-b(6)
 -        DD(2,1,-i)=-b(8)
 -        DD(1,2,-i)=-b(8)
 +        DD(1,1,i)= b(6,i)
 +        DD(2,2,i)=-b(6,i)
 +        DD(2,1,i)= b(8,i)
 +        DD(1,2,i)= b(8,i)
 +        DD(1,1,-i)= b(6,i)
 +        DD(2,2,-i)=-b(6,i)
 +        DD(2,1,-i)=-b(8,i)
 +        DD(1,2,-i)=-b(8,i)
  c        DD(1,1,i)=0.0d0
  c        DD(2,2,i)=0.0d0
  c        DD(2,1,i)=0.0d0
  c        DD(1,2,i)=0.0d0
 -        Dtilde(1,1,i)=b(6)
 -        Dtilde(1,2,i)=b(8)
 -        Dtilde(2,1,i)=-b(8)
 -        Dtilde(2,2,i)=b(6)
 -        Dtilde(1,1,-i)=b(6)
 -        Dtilde(1,2,-i)=-b(8)
 -        Dtilde(2,1,-i)=b(8)
 -        Dtilde(2,2,-i)=b(6)
 +        Dtilde(1,1,i)=b(6,i)
 +        Dtilde(1,2,i)=b(8,i)
 +        Dtilde(2,1,i)=-b(8,i)
 +        Dtilde(2,2,i)=b(6,i)
 +        Dtilde(1,1,-i)=b(6,i)
 +        Dtilde(1,2,-i)=-b(8,i)
 +        Dtilde(2,1,-i)=b(8,i)
 +        Dtilde(2,2,-i)=b(6,i)
  
  c        Dtilde(1,1,i)=0.0d0
  c        Dtilde(1,2,i)=0.0d0
  c        Dtilde(2,1,i)=0.0d0
  c        Dtilde(2,2,i)=0.0d0
 -        EE(1,1,i)= b(10)+b(11)
 -        EE(2,2,i)=-b(10)+b(11)
 -        EE(2,1,i)= b(12)-b(13)
 -        EE(1,2,i)= b(12)+b(13)
 -        EE(1,1,-i)= b(10)+b(11)
 -        EE(2,2,-i)=-b(10)+b(11)
 -        EE(2,1,-i)=-b(12)+b(13)
 -        EE(1,2,-i)=-b(12)-b(13)
 -
 +        EEold(1,1,i)= b(10,i)+b(11,i)
 +        EEold(2,2,i)=-b(10,i)+b(11,i)
 +        EEold(2,1,i)= b(12,i)-b(13,i)
 +        EEold(1,2,i)= b(12,i)+b(13,i)
 +        EEold(1,1,-i)= b(10,i)+b(11,i)
 +        EEold(2,2,-i)=-b(10,i)+b(11,i)
 +        EEold(2,1,-i)=-b(12,i)+b(13,i)
 +        EEold(1,2,-i)=-b(12,i)-b(13,i)
 +        write(iout,*) "TU DOCHODZE"
 +        print *,"JESTEM"
  c        ee(1,1,i)=1.0d0
  c        ee(2,2,i)=1.0d0
  c        ee(2,1,i)=0.0d0
  c        ee(1,2,i)=0.0d0
  c        ee(2,1,i)=ee(1,2,i)
        enddo
 +c      lprint=.true.
        if (lprint) then
        do i=1,nloctyp
          write (iout,*) 'Type',i
          enddo
          write(iout,*) 'EE'
          do j=1,2
 -          write (iout,'(2f10.5)') EE(j,1,i),EE(j,2,i)
 +          write (iout,'(2f10.5)') EEold(j,1,i),EEold(j,2,i)
          enddo
        enddo
        endif
 +c      lprint=.false.
  
  C 
  C Read electrostatic-interaction parameters
       & ', exponents are ',expon,2*expon 
        goto (10,20,30,30,40) ipot
  C----------------------- LJ potential ---------------------------------
-    10 read (isidep,*,end=116,err=116)((eps(i,j),j=i,ntyp),i=1,ntyp),
+    10 read (isidep,*,end=117,err=117)((eps(i,j),j=i,ntyp),i=1,ntyp),
       &   (sigma0(i),i=1,ntyp)
        if (lprint) then
        write (iout,'(/a/)') 'Parameters of the LJ potential:'
        endif
        goto 50
  C----------------------- LJK potential --------------------------------
-    20 read (isidep,*,end=116,err=116)((eps(i,j),j=i,ntyp),i=1,ntyp),
+    20 read (isidep,*,end=117,err=117)((eps(i,j),j=i,ntyp),i=1,ntyp),
       &  (sigma0(i),i=1,ntyp),(rr0(i),i=1,ntyp)
        if (lprint) then
        write (iout,'(/a/)') 'Parameters of the LJK potential:'
        goto 50
  C---------------------- GB or BP potential -----------------------------
     30 do i=1,ntyp
-        read (isidep,*,end=116,err=116)(eps(i,j),j=i,ntyp)
+        read (isidep,*,end=117,err=117)(eps(i,j),j=i,ntyp)
        enddo
 -      read (isidep,*,end=117,err=117)(sigma0(i),i=1,ntyp)
 -      read (isidep,*,end=117,err=117)(sigii(i),i=1,ntyp)
 -      read (isidep,*,end=117,err=117)(chip(i),i=1,ntyp)
 -      read (isidep,*,end=117,err=117)(alp(i),i=1,ntyp)
 +      read (isidep,*,end=116,err=116)(sigma0(i),i=1,ntyp)
 +      read (isidep,*,end=116,err=116)(sigii(i),i=1,ntyp)
 +      read (isidep,*,end=116,err=116)(chip(i),i=1,ntyp)
 +      read (isidep,*,end=116,err=116)(alp(i),i=1,ntyp)
 +C now we start reading lipid
 +      do i=1,ntyp
 +       read (isidep,*,end=1161,err=1161)(epslip(i,j),j=i,ntyp)
 +       print *,"WARNING!!"
 +       do j=1,ntyp
 +       epslip(i,j)=epslip(i,j)+0.05d0
 +       enddo
 +      enddo
  C For the GB potential convert sigma'**2 into chi'
        if (ipot.eq.4) then
        do i=1,ntyp
        endif
        goto 50
  C--------------------- GBV potential -----------------------------------
-    40 read (isidep,*,end=116,err=116)((eps(i,j),j=i,ntyp),i=1,ntyp),
+    40 read (isidep,*,end=117,err=117)((eps(i,j),j=i,ntyp),i=1,ntyp),
       &  (sigma0(i),i=1,ntyp),(rr0(i),i=1,ntyp),(sigii(i),i=1,ntyp),
       &  (chip(i),i=1,ntyp),(alp(i),i=1,ntyp)
        if (lprint) then
@@@ -1160,7 -1136,6 +1171,7 @@@ C Calculate the "working" parameters o
        do i=2,ntyp
          do j=1,i-1
          eps(i,j)=eps(j,i)
 +          epslip(i,j)=epslip(j,i)
          enddo
        enddo
        do i=1,ntyp
          epsij=eps(i,j)
          sigeps=dsign(1.0D0,epsij)
          epsij=dabs(epsij)
 -        aa(i,j)=epsij*rrij*rrij
 -        bb(i,j)=-sigeps*epsij*rrij
 -        aa(j,i)=aa(i,j)
 -        bb(j,i)=bb(i,j)
 +          aa_aq(i,j)=epsij*rrij*rrij
 +          bb_aq(i,j)=-sigeps*epsij*rrij
 +          aa_aq(j,i)=aa_aq(i,j)
 +          bb_aq(j,i)=bb_aq(i,j)
 +          epsijlip=epslip(i,j)
 +          sigeps=dsign(1.0D0,epsijlip)
 +          epsijlip=dabs(epsijlip)
 +          aa_lip(i,j)=epsijlip*rrij*rrij
 +          bb_lip(i,j)=-sigeps*epsijlip*rrij
 +          aa_lip(j,i)=aa_lip(i,j)
 +          bb_lip(j,i)=bb_lip(i,j)
          if (ipot.gt.2) then
            sigt1sq=sigma0(i)**2
            sigt2sq=sigma0(j)**2
@@@ -1232,7 -1200,7 +1243,7 @@@ c           augm(i,j)=0.5D0**(2*expon)*
            endif
          if (lprint) then
              write (iout,'(2(a3,2x),3(1pe10.3),5(0pf8.3))') 
 -     &      restyp(i),restyp(j),aa(i,j),bb(i,j),augm(i,j),
 +     &      restyp(i),restyp(j),aa_aq(i,j),bb_aq(i,j),augm(i,j),
       &      sigma(i,j),r0(i,j),chi(i,j),chi(j,i)
          endif
          enddo
@@@ -1289,7 -1257,7 +1300,7 @@@ c      lprint=.false
  C
  C Define the constants of the disulfide bridge
  C
-       ebr=-5.50D0
+ C      ebr=-12.00D0
  c
  c Old arbitrary potential - commented out.
  c
@@@ -1300,13 -1268,13 +1311,13 @@@ c Constants of the disulfide-bond poten
  c energy surface of diethyl disulfide.
  c A. Liwo and U. Kozlowska, 11/24/03
  c
-       D0CM = 3.78d0
-       AKCM = 15.1d0
-       AKTH = 11.0d0
-       AKCT = 12.0d0
-       V1SS =-1.08d0
-       V2SS = 7.61d0
-       V3SS = 13.7d0
+ C      D0CM = 3.78d0
+ C      AKCM = 15.1d0
+ C      AKTH = 11.0d0
+ C      AKCT = 12.0d0
+ C      V1SS =-1.08d0
+ C      V2SS = 7.61d0
+ C      V3SS = 13.7d0
  c      akcm=0.0d0
  c      akth=0.0d0
  c      akct=0.0d0
@@@ -1314,14 -1282,14 +1325,14 @@@ c      v1ss=0.0d
  c      v2ss=0.0d0
  c      v3ss=0.0d0
        
-       if(me.eq.king) then
-       write (iout,'(/a)') "Disulfide bridge parameters:"
-       write (iout,'(a,f10.2)') 'S-S bridge energy: ',ebr
-       write (iout,'(2(a,f10.2))') 'd0cm:',d0cm,' akcm:',akcm
-       write (iout,'(2(a,f10.2))') 'akth:',akth,' akct:',akct
-       write (iout,'(3(a,f10.2))') 'v1ss:',v1ss,' v2ss:',v2ss,
-      &  ' v3ss:',v3ss
-       endif
+ C      if(me.eq.king) then
+ C      write (iout,'(/a)') "Disulfide bridge parameters:"
+ C      write (iout,'(a,f10.2)') 'S-S bridge energy: ',ebr
+ C      write (iout,'(2(a,f10.2))') 'd0cm:',d0cm,' akcm:',akcm
+ C      write (iout,'(2(a,f10.2))') 'akth:',akth,' akct:',akct
+ C      write (iout,'(3(a,f10.2))') 'v1ss:',v1ss,' v2ss:',v2ss,
+ C     &  ' v3ss:',v3ss
+ C      endif
        return
    111 write (iout,*) "Error reading bending energy parameters."
        goto 999
        goto 999
    116 write (iout,*) "Error reading electrostatic energy parameters."
        goto 999
 + 1161 write (iout,*) "Error reading electrostatic energy parameters.Lip"
 +      goto 999
    117 write (iout,*) "Error reading side chain interaction parameters."
        goto 999
    118 write (iout,*) "Error reading SCp interaction parameters."
@@@ -17,11 -17,8 +17,11 @@@ C geometry
        character*80 card
        dimension sccor(3,20)
        double precision e1(3),e2(3),e3(3)
 -      integer rescode
 +      integer rescode,iterter(maxres)
        logical fail
 +      do i=1,maxres
 +         iterter(i)=0
 +      enddo
        ibeg=1
        lsecondary=.false.
        nhfrag=0
@@@ -49,13 -46,10 +49,13 @@@ crc------------------------------------
            goto 10
          else if (card(:3).eq.'TER') then
  C End current chain
 -          ires_old=ires+1 
 +          ires_old=ires+2
 +          itype(ires_old-1)=ntyp1 
 +          iterter(ires_old-1)=1
            itype(ires_old)=ntyp1
 +          iterter(ires_old)=1
            ibeg=2
 -c          write (iout,*) "Chain ended",ires,ishift,ires_old
 +          write (iout,*) "Chain ended",ires,ishift,ires_old
            if (unres_pdb) then
              do j=1,3
                dc(j,ires)=sccor(j,iii)
@@@ -80,7 -74,7 +80,7 @@@ C Calculate the CM of the preceding res
              endif
  C Start new residue.
  c            write (iout,'(a80)') card
 -            read (card(24:26),*) ires
 +            read (card(23:26),*) ires
              read (card(18:20),'(a3)') res
              if (ibeg.eq.1) then
                ishift=ires-1
@@@ -124,55 -118,15 +124,55 @@@ C Calculate dummy residue coordinates i
  C system
        nres=ires
        do i=2,nres-1
 -c        write (iout,*) i,itype(i)
 -        if (itype(i).eq.ntyp1) then
 -c          write (iout,*) "dummy",i,itype(i)
 -          do j=1,3
 -            c(j,i)=((c(j,i-1)+c(j,i+1))/2+2*c(j,i-1)-c(j,i-2))/2
 -c            c(j,i)=(c(j,i-1)+c(j,i+1))/2
 -            dc(j,i)=c(j,i)
 -          enddo
 -        endif
 +        write (iout,*) i,itype(i),itype(i+1)
 +        if (itype(i).eq.ntyp1.and.iterter(i).eq.1) then
 +         if (itype(i+1).eq.ntyp1.and.iterter(i+1).eq.1 ) then
 +C 16/01/2014 by Adasko: Adding to dummy atoms in the chain
 +C first is connected prevous chain (itype(i+1).eq.ntyp1)=true
 +C second dummy atom is conected to next chain itype(i+1).eq.ntyp1=false
 +           if (unres_pdb) then
 +C 2/15/2013 by Adam: corrected insertion of the last dummy residue
 +            print *,i,'tu dochodze'
 +            call refsys(i-3,i-2,i-1,e1,e2,e3,fail)
 +            if (fail) then
 +              e2(1)=0.0d0
 +              e2(2)=1.0d0
 +              e2(3)=0.0d0
 +            endif !fail
 +            print *,i,'a tu?'
 +            do j=1,3
 +             c(j,i)=c(j,i-1)-1.9d0*e2(j)
 +            enddo
 +           else   !unres_pdb
 +           do j=1,3
 +             dcj=(c(j,i-2)-c(j,i-3))/2.0
 +            if (dcj.eq.0) dcj=1.23591524223
 +             c(j,i)=c(j,i-1)+dcj
 +             c(j,nres+i)=c(j,i)
 +           enddo     
 +          endif   !unres_pdb
 +         else     !itype(i+1).eq.ntyp1
 +          if (unres_pdb) then
 +C 2/15/2013 by Adam: corrected insertion of the first dummy residue
 +            call refsys(i+1,i+2,i+3,e1,e2,e3,fail)
 +            if (fail) then
 +              e2(1)=0.0d0
 +              e2(2)=1.0d0
 +              e2(3)=0.0d0
 +            endif
 +            do j=1,3
 +              c(j,i)=c(j,i+1)-1.9d0*e2(j)
 +            enddo
 +          else !unres_pdb
 +           do j=1,3
 +            dcj=(c(j,i+3)-c(j,i+2))/2.0
 +            if (dcj.eq.0) dcj=1.23591524223
 +            c(j,i)=c(j,i+1)-dcj
 +            c(j,nres+i)=c(j,i)
 +           enddo
 +          endif !unres_pdb
 +         endif !itype(i+1).eq.ntyp1
 +        endif  !itype.eq.ntyp1
        enddo
  C Calculate the CM of the last side chain.
        if (unres_pdb) then
@@@ -196,12 -150,11 +196,12 @@@ C 2/15/2013 by Adam: corrected insertio
              e2(3)=0.0d0
            endif
            do j=1,3
 -            c(j,nres)=c(j,nres-1)-3.8d0*e2(j)
 +            c(j,nres)=c(j,nres-1)-1.9d0*e2(j)
            enddo
          else
          do j=1,3
 -          dcj=c(j,nres-2)-c(j,nres-3)
 +          dcj=(c(j,nres-2)-c(j,nres-3))/2.0
 +            if (dcj.eq.0) dcj=1.23591524223
            c(j,nres)=c(j,nres-1)+dcj
            c(j,2*nres)=c(j,nres)
          enddo
@@@ -228,11 -181,11 +228,11 @@@ C 2/15/2013 by Adam: corrected insertio
              e2(3)=0.0d0
            endif
            do j=1,3
 -            c(j,1)=c(j,2)-3.8d0*e2(j)
 +            c(j,1)=c(j,2)-1.9d0*e2(j)
            enddo
          else
          do j=1,3
 -          dcj=c(j,4)-c(j,3)
 +          dcj=(c(j,4)-c(j,3))/2.0
            c(j,1)=c(j,2)-dcj
            c(j,nres+1)=c(j,1)
          enddo
@@@ -246,6 -199,7 +246,7 @@@ C Calculate internal coordinates
       &    (c(j,nres+ires),j=1,3)
         enddo
        endif
+ C      print *,"before int_from_cart"
        call int_from_cart(.true.,.false.)
        call sc_loc_geom(.true.)
        do i=1,nres
@@@ -369,6 -323,7 +370,6 @@@ cc enddia
           hfrag(i,j)=hfrag(i,j)-ishift
          enddo
        enddo
 -
        return
        end
  c---------------------------------------------------------------------------
@@@ -436,6 -391,7 +437,7 @@@ c          vbld(nres)=3.8d
  c          vbld_inv(nres)=1.0d0/vbld(2)
  c        endif
  c      endif
+       print *,"A TU2"
        if (lside) then
          do i=2,nres-1
            do j=1,3
@@@ -8,7 -8,6 +8,7 @@@
        include 'COMMON.CONTROL'
        include 'COMMON.SBRIDGE'
        include 'COMMON.IOUNITS'
 +      include 'COMMON.SPLITELE'
        logical file_exist
  C Read force-field parameters except weights
        call parmread
@@@ -80,7 -79,6 +80,7 @@@
        include 'COMMON.FFIELD'
        include 'COMMON.INTERACT'
        include 'COMMON.SETUP'
 +      include 'COMMON.SPLITELE'
        COMMON /MACHSW/ KDIAG,ICORFL,IXDR
        character*8 diagmeth(0:3) /'Library','EVVRSP','Givens','Jacobi'/
        character*80 ucase
@@@ -98,6 -96,10 +98,10 @@@ c      print *,"Processor",me," fg_rank
  C Set up the time limit (caution! The time must be input in minutes!)
        read_cart=index(controlcard,'READ_CART').gt.0
        call readi(controlcard,'CONSTR_DIST',constr_dist,0)
+ C this variable with_theta_constr is the variable which allow to read and execute the
+ C constrains on theta angles WITH_THETA_CONSTR is the keyword
+       with_theta_constr = index(controlcard,"WITH_THETA_CONSTR").gt.0
+       write (iout,*) "with_theta_constr ",with_theta_constr
        call readi(controlcard,'SYM',symetr,1)
        call reada(controlcard,'TIMLIM',timlim,960.0D0) ! default 16 hours
        unres_pdb = index(controlcard,'UNRES_PDB') .gt. 0
        refstr=pdbref .or. (index(controlcard,'REFSTR').gt.0)
        indpdb=index(controlcard,'PDBSTART')
        extconf=(index(controlcard,'EXTCONF').gt.0)
 +      AFMlog=(index(controlcard,'AFM'))
 +      selfguide=(index(controlcard,'SELFGUIDE'))
 +      print *,'AFMlog',AFMlog,selfguide,"KUPA"
        call readi(controlcard,'IPRINT',iprint,0)
        call readi(controlcard,'MAXGEN',maxgen,10000)
        call readi(controlcard,'MAXOVERLAP',maxoverlap,1000)
@@@ -218,34 -217,7 +222,34 @@@ cfmc        modecalc=1
        i2ndstr=index(controlcard,'USE_SEC_PRED')
        gradout=index(controlcard,'GRADOUT').gt.0
        gnorm_check=index(controlcard,'GNORM_CHECK').gt.0
 +C  DISTCHAINMAX become obsolete for periodic boundry condition
        call reada(controlcard,'DISTCHAINMAX',distchainmax,5.0d0)
 +C Reading the dimensions of box in x,y,z coordinates
 +      call reada(controlcard,'BOXX',boxxsize,100.0d0)
 +      call reada(controlcard,'BOXY',boxysize,100.0d0)
 +      call reada(controlcard,'BOXZ',boxzsize,100.0d0)
 +c Cutoff range for interactions
 +      call reada(controlcard,"R_CUT",r_cut,15.0d0)
 +      call reada(controlcard,"LAMBDA",rlamb,0.3d0)
 +      call reada(controlcard,"LIPTHICK",lipthick,0.0d0)
 +      call reada(controlcard,"LIPAQBUF",lipbufthick,0.0d0)
 +      if (lipthick.gt.0.0d0) then
 +       bordliptop=(boxzsize+lipthick)/2.0
 +       bordlipbot=bordliptop-lipthick
 +C      endif
 +      if ((bordliptop.gt.boxzsize).or.(borlipbot.lt.0.0)) 
 +     & write(iout,*) "WARNING WRONG SIZE OF LIPIDIC PHASE"
 +      buflipbot=bordlipbot+lipbufthick
 +      bufliptop=bordliptop-lipbufthick
 +      if ((lipbufthick*2.0d0).gt.lipthick)
 +     &write(iout,*) "WARNING WRONG SIZE OF LIP AQ BUF"
 +      endif
 +      write(iout,*) "bordliptop=",bordliptop
 +      write(iout,*) "bordlipbot=",bordlipbot
 +      write(iout,*) "bufliptop=",bufliptop
 +      write(iout,*) "buflipbot=",buflipbot
 +
 +
        if (me.eq.king .or. .not.out1file ) 
       &  write (iout,*) "DISTCHAINMAX",distchainmax
        
        ntime_split0=ntime_split
        call readi(controlcard,"MAXTIME_SPLIT",maxtime_split,64)
        ntime_split0=ntime_split
 -      call reada(controlcard,"R_CUT",r_cut,2.0d0)
 -      call reada(controlcard,"LAMBDA",rlamb,0.3d0)
 +c      call reada(controlcard,"R_CUT",r_cut,2.0d0)
 +c      call reada(controlcard,"LAMBDA",rlamb,0.3d0)
        rest = index(controlcard,"REST").gt.0
        tbf = index(controlcard,"TBF").gt.0
        usampl = index(controlcard,"USAMPL").gt.0
        integer rescode
        double precision x(maxvar)
        character*256 pdbfile
-       character*320 weightcard
+       character*400 weightcard
        character*80 weightcard_t,ucase
        dimension itype_pdb(maxres)
        common /pizda/ itype_pdb
@@@ -608,7 -580,6 +612,7 @@@ C Read weights of the subsequent energ
         call reada(weightcard,'CUTOFF',cutoff_corr,7.0d0)
         call reada(weightcard,'DELT_CORR',delt_corr,0.5d0)
         call reada(weightcard,'TEMP0',temp0,300.0d0)
 +       call reada(weightcard,'WLT',wliptran,0.0D0)
         if (index(weightcard,'SOFT').gt.0) ipot=6
  C 12/1/95 Added weight for the multi-body term WCORR
         call reada(weightcard,'WCORRH',wcorr,1.0D0)
        call reada(weightcard,"V2SS",v2ss,7.61d0)
        call reada(weightcard,"V3SS",v3ss,13.7d0)
        call reada(weightcard,"EBR",ebr,-5.50D0)
+       call reada(weightcard,"ATRISS",atriss,0.301D0)
+       call reada(weightcard,"BTRISS",btriss,0.021D0)
+       call reada(weightcard,"CTRISS",ctriss,1.001D0)
+       call reada(weightcard,"DTRISS",dtriss,1.001D0)
+       write (iout,*) "ATRISS=", atriss
+       write (iout,*) "BTRISS=", btriss
+       write (iout,*) "CTRISS=", ctriss
+       write (iout,*) "DTRISS=", dtriss
        dyn_ss=(index(weightcard,'DYN_SS').gt.0)
        do i=1,maxres
          dyn_ss_mask(i)=.false.
          v2ss=v2ss*wstrain/wsc
          v3ss=v3ss*wstrain/wsc
        else
-         ss_depth=ebr/wstrain-0.25*eps(1,1)*wsc/wstrain
+         if (wstrain.ne.0.0) then
+          ss_depth=ebr/wstrain-0.25*eps(1,1)*wsc/wstrain
+         else
+           ss_depth=0.0
+         endif
        endif
  
        if(me.eq.king.or..not.out1file) then
    33    write (iout,'(a)') 'Error opening PDB file.'
          stop
    34    continue
- c        print *,'Begin reading pdb data'
+ c        write (iout,*) 'Begin reading pdb data'
+ c        call flush(iout)
          call readpdb
- c        print *,'Finished reading pdb data'
+ c        write (iout,*) 'Finished reading pdb data'
+ c        call flush(iout)
          if(me.eq.king.or..not.out1file)
       &   write (iout,'(a,i3,a,i3)')'nsup=',nsup,
       &   ' nstart_sup=',nstart_sup
@@@ -847,27 -832,78 +865,78 @@@ C 8/13/98 Set limits to generating the 
        enddo
        read (inp,*) ndih_constr
        if (ndih_constr.gt.0) then
-         read (inp,*) ftors
-         read (inp,*) (idih_constr(i),phi0(i),drange(i),i=1,ndih_constr)
+ C        read (inp,*) ftors
+         read (inp,*) (idih_constr(i),phi0(i),drange(i),ftors(i),
+      &  i=1,ndih_constr)
          if(me.eq.king.or..not.out1file)then
           write (iout,*) 
       &   'There are',ndih_constr,' constraints on phi angles.'
           do i=1,ndih_constr
-           write (iout,'(i5,2f8.3)') idih_constr(i),phi0(i),drange(i)
+           write (iout,'(i5,3f8.3)') idih_constr(i),phi0(i),drange(i),
+      &    ftors(i)
           enddo
          endif
          do i=1,ndih_constr
            phi0(i)=deg2rad*phi0(i)
            drange(i)=deg2rad*drange(i)
          enddo
-         if(me.eq.king.or..not.out1file)
-      &   write (iout,*) 'FTORS',ftors
+ C        if(me.eq.king.or..not.out1file)
+ C     &   write (iout,*) 'FTORS',ftors
          do i=1,ndih_constr
            ii = idih_constr(i)
            phibound(1,ii) = phi0(i)-drange(i)
            phibound(2,ii) = phi0(i)+drange(i)
          enddo 
        endif
+ C first setting the theta boundaries to 0 to pi
+ C this mean that there is no energy penalty for any angle occuring this can be applied 
+ C for generate random conformation but is not implemented in this way
+ C      do i=1,nres
+ C        thetabound(1,i)=0
+ C        thetabound(2,i)=pi
+ C      enddo
+ C begin reading theta constrains this is quartic constrains allowing to 
+ C have smooth second derivative 
+       if (with_theta_constr) then
+ C with_theta_constr is keyword allowing for occurance of theta constrains
+       read (inp,*) ntheta_constr
+ C ntheta_constr is the number of theta constrains
+       if (ntheta_constr.gt.0) then
+ C        read (inp,*) ftors
+         read (inp,*) (itheta_constr(i),theta_constr0(i),
+      &  theta_drange(i),for_thet_constr(i),
+      &  i=1,ntheta_constr)
+ C the above code reads from 1 to ntheta_constr 
+ C itheta_constr(i) residue i for which is theta_constr
+ C theta_constr0 the global minimum value
+ C theta_drange is range for which there is no energy penalty
+ C for_thet_constr is the force constant for quartic energy penalty
+ C E=k*x**4 
+         if(me.eq.king.or..not.out1file)then
+          write (iout,*)
+      &   'There are',ntheta_constr,' constraints on phi angles.'
+          do i=1,ntheta_constr
+           write (iout,'(i5,3f8.3)') itheta_constr(i),theta_constr0(i),
+      &    theta_drange(i),
+      &    for_thet_constr(i)
+          enddo
+         endif
+         do i=1,ntheta_constr
+           theta_constr0(i)=deg2rad*theta_constr0(i)
+           theta_drange(i)=deg2rad*theta_drange(i)
+         enddo
+ C        if(me.eq.king.or..not.out1file)
+ C     &   write (iout,*) 'FTORS',ftors
+ C        do i=1,ntheta_constr
+ C          ii = itheta_constr(i)
+ C          thetabound(1,ii) = phi0(i)-drange(i)
+ C          thetabound(2,ii) = phi0(i)+drange(i)
+ C        enddo
+       endif ! ntheta_constr.gt.0
+       endif! with_theta_constr
+ C
+ C      with_dihed_constr = index(controlcard,"WITH_DIHED_CONSTR").gt.0
+ C      write (iout,*) "with_dihed_constr ",with_dihed_constr
        nnt=1
  #ifdef MPI
        if (me.eq.king) then
@@@ -954,11 -990,12 +1023,13 @@@ czscore          call geom_to_var(nvar,
            enddo
            call contact(.true.,ncont_ref,icont_ref,co)
          endif
- c        write (iout,*) "constr_dist",constr_dist,nstart_sup,nsup
+         endif
+         print *, "A TU"
+         write (iout,*) "constr_dist",constr_dist,nstart_sup,nsup
          call flush(iout)
          if (constr_dist.gt.0) call read_dist_constr
          write (iout,*) "After read_dist_constr nhpb",nhpb
 +        if ((AFMlog.gt.0).or.(selfguide.gt.0)) call read_afminp
          call hpb_partition
          if(me.eq.king.or..not.out1file)
       &   write (iout,*) 'Contact order:',co
       &     restyp(itype(icont_ref(2,i))),' ',icont_ref(2,i)
          enddo
          endif
-       endif
+ C      endif
        if (indpdb.eq.0 .and. modecalc.ne.2 .and. modecalc.ne.4
       &    .and. modecalc.ne.8 .and. modecalc.ne.9 .and. 
       &    modecalc.ne.10) then
@@@ -1071,7 -1108,18 +1142,7 @@@ C initial geometry
     40       continue
            endif
  #else
 -          do itrial=1,100
 -            itmp=1
 -            call gen_rand_conf(itmp,*30)
 -            goto 40
 -   30       write (iout,*) 'Failed to generate random conformation',
 -     &        ', itrial=',itrial
 -            write (*,*) 'Failed to generate random conformation',
 -     &        ', itrial=',itrial
 -          enddo
 -          write (iout,'(a,i3,a)') 'Processor:',me,
 -     &      ' error in generating random conformation.'
 -          write (*,'(a,i3,a)') 'Processor:',me,
 +          write (*,'(a)') 
       &      ' error in generating random conformation.'
            stop
     40     continue
@@@ -1147,6 -1195,7 +1218,7 @@@ cd    write (iout,'(i4,f10.5)') (i,rad2
       &  write (iout,'(//80(1h*)/20x,a,i4,a/80(1h*)//)') 
       &  'Processor',myrank,': end reading molecular data.'
  #endif
+       print *,"A TU?"
        return
        end
  c--------------------------------------------------------------------------
@@@ -1996,8 -2045,6 +2068,8 @@@ C Get parameter filenames and open the 
        open (ielep,file=elename,status='old',readonly)
        call getenv_loc('SIDEPAR',sidename)
        open (isidep,file=sidename,status='old',readonly)
 +      call getenv_loc('LIPTRANPAR',liptranname)
 +      open (iliptranpar,file=liptranname,status='old',action='read')
  #ifndef CRYST_SC
        call getenv_loc('ROTPARPDB',rotname_pdb)
        open (irotam_pdb,file=rotname_pdb,status='old',action='read')
@@@ -2072,7 -2119,7 +2144,7 @@@ c      print *,"Processor",myrank," fg_
        mol2name=prefix(:lenpre)//'_'//pot(:lenpot)//'.mol2'
        statname=prefix(:lenpre)//'_'//pot(:lenpot)//'.stat'
        if (lentmp.gt.0)
 -     &  call copy_to_tmp(pref_orig(:ile(pref_orig))//'_'//pot(:lenpot)//
 +     &  call copy_to_tmp(pref_orig(:ilen(pref_orig))//'_'//pot(:lenpot)
       &    //'.stat')
        rest2name=prefix(:ilen(prefix))//'.rst'
        if(usampl) then 
@@@ -2202,7 -2249,6 +2274,7 @@@ c--------------------------------------
        include 'COMMON.MD'
        open(irest2,file=rest2name,status='unknown')
        read(irest2,*) totT,EK,potE,totE,t_bath
 +      totTafm=totT
        do i=1,2*nres
           read(irest2,'(3e15.5)') (d_t(j,i),j=1,3)
        enddo
@@@ -2258,36 -2304,6 +2330,36 @@@ c--------------------------------------
        enddo
        return
        end
 +C---------------------------------------------------------------------------
 +      subroutine read_afminp
 +            implicit real*8 (a-h,o-z)
 +      include 'DIMENSIONS'
 +#ifdef MPI
 +      include 'mpif.h'
 +#endif
 +      include 'COMMON.SETUP'
 +      include 'COMMON.CONTROL'
 +      include 'COMMON.CHAIN'
 +      include 'COMMON.IOUNITS'
 +      include 'COMMON.SBRIDGE'
 +      character*320 afmcard
 +      print *, "wchodze"
 +      call card_concat(afmcard)
 +      call readi(afmcard,"BEG",afmbeg,0)
 +      call readi(afmcard,"END",afmend,0)
 +      call reada(afmcard,"FORCE",forceAFMconst,0.0d0)
 +      call reada(afmcard,"VEL",velAFMconst,0.0d0)
 +      print *,'FORCE=' ,forceAFMconst
 +CCCC NOW PROPERTIES FOR AFM
 +       distafminit=0.0d0
 +       do i=1,3
 +        distafminit=(c(i,afmend)-c(i,afmbeg))**2+distafminit
 +       enddo
 +        distafminit=dsqrt(distafminit)
 +        print *,'initdist',distafminit
 +      return
 +      end
 +
  c-------------------------------------------------------------------------------
        subroutine read_dist_constr
        implicit real*8 (a-h,o-z)
        integer ifrag_(2,100),ipair_(2,100)
        double precision wfrag_(100),wpair_(100)
        character*500 controlcard
- c      write (iout,*) "Calling read_dist_constr"
+       print *, "WCHODZE"
+       write (iout,*) "Calling read_dist_constr"
  c      write (iout,*) "nres",nres," nstart_sup",nstart_sup," nsup",nsup
  c      call flush(iout)
        call card_concat(controlcard)
@@@ -2397,11 -2414,30 +2470,30 @@@ c            write (iout,*) "j",j," k",
          enddo
          endif
        enddo 
+       print *,ndist_
        do i=1,ndist_
-         read (inp,*) ihpb(nhpb+1),jhpb(nhpb+1),forcon(nhpb+1)
+         if (constr_dist.eq.11) then
+         read (inp,*) ihpb(nhpb+1),jhpb(nhpb+1),dhpb(i),dhpb1(i),
+      &     ibecarb(i),forcon(nhpb+1),fordepth(nhpb+1)
+         fordepth(nhpb+1)=fordepth(nhpb+1)/forcon(nhpb+1)
+         else
+ C        print *,"in else"
+         read (inp,*) ihpb(nhpb+1),jhpb(nhpb+1),dhpb(i),dhpb1(i),
+      &     ibecarb(i),forcon(nhpb+1)
+         endif
          if (forcon(nhpb+1).gt.0.0d0) then
            nhpb=nhpb+1
-           dhpb(nhpb)=dist(ihpb(nhpb),jhpb(nhpb))
+           if (ibecarb(i).gt.0) then
+             ihpb(i)=ihpb(i)+nres
+             jhpb(i)=jhpb(i)+nres
+           endif
+           if (dhpb(nhpb).eq.0.0d0)
+      &       dhpb(nhpb)=dist(ihpb(nhpb),jhpb(nhpb))
+         endif
+ C        read (inp,*) ihpb(nhpb+1),jhpb(nhpb+1),forcon(nhpb+1)
+ C        if (forcon(nhpb+1).gt.0.0d0) then
+ C          nhpb=nhpb+1
+ C          dhpb(nhpb)=dist(ihpb(nhpb),jhpb(nhpb))
  #ifdef MPI
            if (.not.out1file .or. me.eq.king)
       &    write (iout,'(a,3i5,f8.2,f10.1)') "+dist.constr ",
            write (iout,'(a,3i5,f8.2,f10.1)') "+dist.constr ",
       &     nhpb,ihpb(nhpb),jhpb(nhpb),dhpb(nhpb),forcon(nhpb)
  #endif
-         endif
        enddo
        call flush(iout)
        return
@@@ -1,30 -1,25 +1,31 @@@
        subroutine refsys(i2,i3,i4,e1,e2,e3,fail)
 +c This subroutine calculates unit vectors of a local reference system
 +c defined by atoms (i2), (i3), and (i4). The x axis is the axis from
+       implicit real*8 (a-h,o-z)
+       include 'DIMENSIONS'
+ c this subroutine calculates unity vectors of a local reference system
+ c defined by atoms (i2), (i3), and (i4). the x axis is the axis from
  c atom (i3) to atom (i2), and the xy plane is the plane defined by atoms
  c (i2), (i3), and (i4). z axis is directed according to the sign of the
- c vector product (i3)-(i2) and (i3)-(i4). Sets fail to .true. if atoms
+ c vector product (i3)-(i2) and (i3)-(i4). sets fail to .true. if atoms
  c (i2) and (i3) or (i3) and (i4) coincide or atoms (i2), (i3), and (i4)
- c form a linear fragment. Returns vectors e1, e2, and e3.
-       implicit real*8 (a-h,o-z)
-       include 'DIMENSIONS'
+ c form a linear fragment. returns vectors e1, e2, and e3.
        logical fail
        double precision e1(3),e2(3),e3(3)
        double precision u(3),z(3)
        include 'COMMON.IOUNITS'
-       include 'COMMON.CHAIN'
 -      include "COMMON.CHAIN"
 -      data coinc /1.0d-13/,align /1.0d-13/
 +      double precision coinc/1.0D-13/,align /1.0D-13/
 +c      print *,'just initialize'
        fail=.false.
 -      s1=0.0d0
 -      s2=0.0d0
 +c      print *,fail
 +      s1=0.0
 +      s2=0.0
 +      print *,s1,s2
        do 1 i=1,3
 +      print *, i2,i3,i4
        zi=c(i,i2)-c(i,i3)
        ui=c(i,i4)-c(i,i3)
 +      print *,zi,ui
        s1=s1+zi*zi
        s2=s2+ui*ui
        z(i)=zi
        if (s1.gt.coinc) goto 2
        write (iout,1000) i2,i3,i1
        fail=.true.
- c     do 3 i=1,3
- c   3 c(i,i1)=0.0D0
        return
      2 if (s2.gt.coinc) goto 4
        write(iout,1000) i3,i4,i1
        fail=.true.
-       do 5 i=1,3
-     5 c(i,i1)=0.0D0
        return
 +      print *,'two if pass'
      4 s1=1.0/s1
        s2=1.0/s2
        v1=z(2)*u(3)-z(3)*u(2)
        v2=z(3)*u(1)-z(1)*u(3)
        v3=z(1)*u(2)-z(2)*u(1)
-       anorm=dsqrt(v1*v1+v2*v2+v3*v3)
+       anorm=sqrt(v1*v1+v2*v2+v3*v3)
        if (anorm.gt.align) goto 6
        write (iout,1010) i2,i3,i4,i1
        fail=.true.
- c     do 7 i=1,3
- c   7 c(i,i1)=0.0D0
        return
-     6 anorm=1.0D0/anorm
+     6 anorm=1.0/anorm
        e3(1)=v1*anorm
        e3(2)=v2*anorm
        e3(3)=v3*anorm
        e2(1)=e1(3)*e3(2)-e1(2)*e3(3)
        e2(2)=e1(1)*e3(3)-e1(3)*e3(1)
        e2(3)=e1(2)*e3(1)-e1(1)*e3(2)
-       print *,'just before leave'
 - 1000 format (/1x,' * * * error - atoms',i4,' and',i4,' coincide.')
 - 1010 format (/1x,' * * * error - atoms',2(i4,2h, ),i4,' form a linear')
 + 1000 format (/1x,' * * * Error - atoms',i4,' and',i4,' coincide.',
 +     1 'coordinates of atom',i4,' are set to zero.')
 + 1010 format (/1x,' * * * Error - atoms',2(i4,2h, ),i4,' form a linear',
 +     1 ' fragment. coordinates of atom',i4,' are set to zero.')
        return
        end
@@@ -150,119 -150,11 +150,119 @@@ c-------END TESTING COD
        dyi=dc_norm(2,nres+i)
        dzi=dc_norm(3,nres+i)
        dsci_inv=vbld_inv(i+nres)
 -
 +        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-
 +     &        ((positi-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-positi)/lipbufthick)
 +         sslipi=sscalelip(fracinbuf)
 +         ssgradlipi=sscagradlip(fracinbuf)/lipbufthick
 +        else
 +         sslipi=1.0d0
 +         ssgradlipi=0.0
 +        endif
 +       else
 +         sslipi=0.0d0
 +         ssgradlipi=0.0
 +       endif
        itypj=itype(j)
 -      xj=c(1,nres+j)-c(1,nres+i)
 -      yj=c(2,nres+j)-c(2,nres+i)
 -      zj=c(3,nres+j)-c(3,nres+i)
 +            xj=c(1,nres+j)
 +            yj=c(2,nres+j)
 +            zj=c(3,nres+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
 +       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-
 +     &        ((positi-bordlipbot)/lipbufthick)
 +C lipbufthick is thickenes of lipid buffore
 +         sslipj=sscalelip(fracinbuf)
 +         ssgradlipj=-sscagradlip(fracinbuf)/lipbufthick
 +        elseif (zi.gt.bufliptop) then
 +         fracinbuf=1.0d0-((bordliptop-positi)/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
 +
 +      dist_init=(xj-xi)**2+(yj-yi)**2+(zj-zi)**2
 +      xj_safe=xj
 +      yj_safe=yj
 +      zj_safe=zj
 +      subchap=0
 +      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     xj=c(1,nres+j)-c(1,nres+i)
 +C      yj=c(2,nres+j)-c(2,nres+i)
 +C      zj=c(3,nres+j)-c(3,nres+i)
        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)  ! sc_angular needs rij to really be the inverse
 +            sss=sscale((1.0d0/rij)/sigma(itypi,itypj))
 +            sssgrad=sscagrad((1.0d0/rij)/sigma(itypi,itypj))
  c     The following are set in sc_angular
  c      erij(1)=xj*rij
  c      erij(2)=yj*rij
@@@ -297,9 -187,9 +297,9 @@@ c      om12=dxi*dxj+dyi*dyj+dzi*dz
  
        ljXs=sig-sig0ij
        ljA=eps1*eps2rt**2*eps3rt**2
 -      ljB=ljA*bb(itypi,itypj)
 -      ljA=ljA*aa(itypi,itypj)
 -      ljxm=ljXs+(-2.0D0*aa(itypi,itypj)/bb(itypi,itypj))**(1.0D0/6.0D0)
 +      ljB=ljA*bb
 +      ljA=ljA*aa
 +      ljxm=ljXs+(-2.0D0*aa/bb)**(1.0D0/6.0D0)
  
        ssXs=d0cm
        deltat1=1.0d0-om1
@@@ -333,7 -223,7 +333,7 @@@ c-------TESTING COD
  c     Stop and plot energy and derivative as a function of distance
        if (checkstop) then
          ssm=ssC-0.25D0*ssB*ssB/ssA
 -        ljm=-0.25D0*ljB*bb(itypi,itypj)/aa(itypi,itypj)
 +        ljm=-0.25D0*ljB*bb/aa
          if (ssm.lt.ljm .and.
       &       dabs(rij-0.5d0*(ssxm+ljxm)).lt.0.35d0*(ljxm-ssxm)) then
            nicheck=1000
@@@ -358,18 -248,17 +358,18 @@@ c-------END TESTING COD
          havebond=.false.
          ljd=rij-ljXs
          fac=(1.0D0/ljd)**expon
 -        e1=fac*fac*aa(itypi,itypj)
 -        e2=fac*bb(itypi,itypj)
 +        e1=fac*fac*aa
 +        e2=fac*bb
          eij=eps1*eps2rt*eps3rt*(e1+e2)
          eps2der=eij*eps3rt
          eps3der=eij*eps2rt
 -        eij=eij*eps2rt*eps3rt
 +        eij=eij*eps2rt*eps3rt*sss
  
          sigder=-sig/sigsq
          e1=e1*eps1*eps2rt**2*eps3rt**2
          ed=-expon*(e1+eij)/ljd
          sigder=ed*sigder
 +        ed=ed+eij/sss*sssgrad/sigma(itypi,itypj)*rij
          eom1=eps2der*eps2rt_om1-2.0D0*alf1*eps3der+sigder*sigsq_om1
          eom2=eps2der*eps2rt_om2+2.0D0*alf2*eps3der+sigder*sigsq_om2
          eom12=eij*eps1_om12+eps2der*eps2rt_om12
          havebond=.true.
          ssd=rij-ssXs
          eij=ssA*ssd*ssd+ssB*ssd+ssC
 -
 +        eij=eij*sss        
          ed=2*akcm*ssd+akct*deltat12
 +        ed=ed+eij/sss*sssgrad/sigma(itypi,itypj)*rij
          pom1=akct*ssd
          pom2=v1ss+2*v2ss*cosphi+3*v3ss*cosphi*cosphi
          eom1=-2*akth*deltat1-pom1-om2*pom2
@@@ -421,15 -309,13 +421,15 @@@ c-------FIRST METHOD, DISCONTINUOUS SEC
            fac1=deltasq_inv*fac*(xm-rij)
            fac2=deltasq_inv*fac*(rij-ssxm)
            ed=delta_inv*(Ht*hd2-ssm*hd1)
 +          eij=eij*sss
 +          ed=ed+eij/sss*sssgrad/sigma(itypi,itypj)*rij
            eom1=fac1*d_ssxm(1)+fac2*d_xm(1)+h1*d_ssm(1)
            eom2=fac1*d_ssxm(2)+fac2*d_xm(2)+h1*d_ssm(2)
            eom12=fac1*d_ssxm(3)+fac2*d_xm(3)+h1*d_ssm(3)
          else
            havebond=.false.
 -          ljm=-0.25D0*ljB*bb(itypi,itypj)/aa(itypi,itypj)
 -          d_ljm(1)=-0.5D0*bb(itypi,itypj)/aa(itypi,itypj)*ljB
 +          ljm=-0.25D0*ljB*bb/aa
 +          d_ljm(1)=-0.5D0*bb/aa*ljB
            d_ljm(2)=d_ljm(1)*(0.5D0*eps2rt_om2/eps2rt+alf2/eps3rt)
            d_ljm(3)=d_ljm(1)*(0.5D0*eps1_om12+0.5D0*eps2rt_om12/eps2rt-
       +         alf12/eps3rt)
            fac1=deltasq_inv*fac*(ljxm-rij)
            fac2=deltasq_inv*fac*(rij-xm)
            ed=delta_inv*(ljm*hd2-Ht*hd1)
 +          eij=eij*sss
 +          ed=ed+eij/sss*sssgrad/sigma(itypi,itypj)*rij
            eom1=fac1*d_xm(1)+fac2*d_ljxm(1)+h2*d_ljm(1)
            eom2=fac1*d_xm(2)+fac2*d_ljxm(2)+h2*d_ljm(2)
            eom12=fac1*d_xm(3)+fac2*d_ljxm(3)+h2*d_ljm(3)
@@@ -549,8 -433,6 +549,8 @@@ c-------TESTING COD
          checkstop=.false.
        endif
  c-------END TESTING CODE
 +            gg_lipi(3)=ssgradlipi*eij
 +            gg_lipj(3)=ssgradlipj*eij
  
        do k=1,3
          dcosom1(k)=(dc_norm(k,nres+i)-om1*erij(k))/rij
          gg(k)=ed*erij(k)+eom1*dcosom1(k)+eom2*dcosom2(k)
        enddo
        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
 -        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
        enddo
@@@ -574,8 -456,8 +574,8 @@@ cgrad        endd
  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(k)
 +        gvdwc(l,j)=gvdwc(l,j)+gg(l)+gg_lipj(k)
        enddo
  
        return
@@@ -650,7 -532,7 +650,7 @@@ c     Local variable
       &     allihpb(maxdim),alljhpb(maxdim),
       &     newnss,newihpb(maxdim),newjhpb(maxdim)
        logical found
-       integer i_newnss(max_fg_procs),displ(max_fg_procs)
+       integer i_newnss(max_fg_procs),displ(0:max_fg_procs)
        integer g_newihpb(maxdim),g_newjhpb(maxdim),g_newnss
  
        allnss=0
@@@ -742,9 -624,9 +742,9 @@@ cmc      write(iout,*)"NEWNSS ",newnss,
          enddo
  #ifndef CLUST
  #ifndef WHAM
-         if (.not.found.and.fg_rank.eq.0) 
-      &      write(iout,'(a15,f12.2,f8.1,2i5)')
-      &       "SSBOND_BREAK",totT,t_bath,idssb(i),jdssb(i)
+ c        if (.not.found.and.fg_rank.eq.0) 
+ c     &      write(iout,'(a15,f12.2,f8.1,2i5)')
+ c     &       "SSBOND_BREAK",totT,t_bath,idssb(i),jdssb(i)
  #endif
  #endif
        enddo
          enddo
  #ifndef CLUST
  #ifndef WHAM
-         if (.not.found.and.fg_rank.eq.0) 
-      &      write(iout,'(a15,f12.2,f8.1,2i5)')
-      &       "SSBOND_FORM",totT,t_bath,newihpb(i),newjhpb(i)
+ c        if (.not.found.and.fg_rank.eq.0) 
+ c     &      write(iout,'(a15,f12.2,f8.1,2i5)')
+ c     &       "SSBOND_FORM",totT,t_bath,newihpb(i),newjhpb(i)
  #endif
  #endif
        enddo
        return
        end
  
- c----------------------------------------------------------------------------
- #ifdef WHAM
-       subroutine read_ssHist
-       implicit none
- c     Includes
-       include 'DIMENSIONS'
-       include "DIMENSIONS.FREE"
-       include 'COMMON.FREE'
  
- c     Local variables
-       integer i,j
-       character*80 controlcard
-       do i=1,dyn_nssHist
-         call card_concat(controlcard,.true.)
-         read(controlcard,*)
-      &       dyn_ssHist(i,0),(dyn_ssHist(i,j),j=1,2*dyn_ssHist(i,0))
-       enddo
-       return
-       end
- #endif
- c----------------------------------------------------------------------------
- C-----------------------------------------------------------------------------
- C-----------------------------------------------------------------------------
- C-----------------------------------------------------------------------------
- C-----------------------------------------------------------------------------
  C-----------------------------------------------------------------------------
  C-----------------------------------------------------------------------------
  C-----------------------------------------------------------------------------
@@@ -2067,3 -1918,151 +2036,151 @@@ c$$$      retur
  c$$$      end                                                               
  c$$$
  c$$$C-----------------------------------------------------------------------------
+ c$$$C-----------------------------------------------------------------------------
+          subroutine triple_ssbond_ene(resi,resj,resk,eij)
+       include 'DIMENSIONS'
+       include 'COMMON.SBRIDGE'
+       include 'COMMON.CHAIN'
+       include 'COMMON.DERIV'
+       include 'COMMON.LOCAL'
+       include 'COMMON.INTERACT'
+       include 'COMMON.VAR'
+       include 'COMMON.IOUNITS'
+       include 'COMMON.CALC'
+ #ifndef CLUST
+ #ifndef WHAM
+       include 'COMMON.MD'
+ #endif
+ #endif
+ c     External functions
+       double precision h_base
+       external h_base
+ c     Input arguments
+       integer resi,resj,resk
+ c     Output arguments
+       double precision eij,eij1,eij2,eij3
+ c     Local variables
+       logical havebond
+ c      integer itypi,itypj,k,l
+       double precision rrij,ssd,deltat1,deltat2,deltat12,cosphi
+       double precision rrik,rrjk,rik,rjk,xi,xk,yi,yk,zi,zk,xij,yij,zij
+       double precision xik,yik,zik,xjk,yjk,zjk
+       double precision sig0ij,ljd,sig,fac,e1,e2
+       double precision dcosom1(3),dcosom2(3),ed
+       double precision pom1,pom2
+       double precision ljA,ljB,ljXs
+       double precision d_ljB(1:3)
+       double precision ssA,ssB,ssC,ssXs
+       double precision ssxm,ljxm,ssm,ljm
+       double precision d_ssxm(1:3),d_ljxm(1:3),d_ssm(1:3),d_ljm(1:3)
+       if (dtriss.eq.0) return
+       i=resi
+       j=resj
+       k=resk
+ C      write(iout,*) resi,resj,resk
+       itypi=itype(i)
+       dxi=dc_norm(1,nres+i)
+       dyi=dc_norm(2,nres+i)
+       dzi=dc_norm(3,nres+i)
+       dsci_inv=vbld_inv(i+nres)
+       xi=c(1,nres+i)
+       yi=c(2,nres+i)
+       zi=c(3,nres+i)
+       itypj=itype(j)
+       xj=c(1,nres+j)
+       yj=c(2,nres+j)
+       zj=c(3,nres+j)
+       
+       dxj=dc_norm(1,nres+j)
+       dyj=dc_norm(2,nres+j)
+       dzj=dc_norm(3,nres+j)
+       dscj_inv=vbld_inv(j+nres)
+       itypk=itype(k)
+       xk=c(1,nres+k)
+       yk=c(2,nres+k)
+       zk=c(3,nres+k)
+       
+       dxk=dc_norm(1,nres+k)
+       dyk=dc_norm(2,nres+k)
+       dzk=dc_norm(3,nres+k)
+       dscj_inv=vbld_inv(k+nres)
+       xij=xj-xi
+       xik=xk-xi
+       xjk=xk-xj
+       yij=yj-yi
+       yik=yk-yi
+       yjk=yk-yj
+       zij=zj-zi
+       zik=zk-zi
+       zjk=zk-zj
+       rrij=(xij*xij+yij*yij+zij*zij)
+       rij=dsqrt(rrij)  ! sc_angular needs rij to really be the inverse
+       rrik=(xik*xik+yik*yik+zik*zik)
+       rik=dsqrt(rrik)
+       rrjk=(xjk*xjk+yjk*yjk+zjk*zjk)
+       rjk=dsqrt(rrjk)
+ C there are three combination of distances for each trisulfide bonds
+ C The first case the ith atom is the center
+ C Energy function is E=d/(a*(x-y)**2+b*(x+y)**2+c) where x is first
+ C distance y is second distance the a,b,c,d are parameters derived for
+ C this problem d parameter was set as a penalty currenlty set to 1.
+       eij1=dtriss/(atriss*(rij-rik)**2+btriss*(rij+rik)**2+ctriss)
+ C second case jth atom is center
+       eij2=dtriss/(atriss*(rij-rjk)**2+btriss*(rij+rjk)**2+ctriss)
+ C the third case kth atom is the center
+       eij3=dtriss/(atriss*(rik-rjk)**2+btriss*(rik+rjk)**2+ctriss)
+ C      eij2=0.0
+ C      eij3=0.0
+ C      eij1=0.0
+       eij=eij1+eij2+eij3
+ C      write(iout,*)i,j,k,eij
+ C The energy penalty calculated now time for the gradient part 
+ C derivative over rij
+       fac=-eij1**2/dtriss*(2.0*atriss*(rij-rik)+2.0*btriss*(rij+rik))
+      &-eij2**2/dtriss*(2.0*atriss*(rij-rjk)+2.0*btriss*(rij+rjk))  
+             gg(1)=xij*fac/rij
+             gg(2)=yij*fac/rij
+             gg(3)=zij*fac/rij
+       do m=1,3
+         gvdwx(m,i)=gvdwx(m,i)-gg(m)
+         gvdwx(m,j)=gvdwx(m,j)+gg(m)
+       enddo
+       do l=1,3
+         gvdwc(l,i)=gvdwc(l,i)-gg(l)
+         gvdwc(l,j)=gvdwc(l,j)+gg(l)
+       enddo
+ C now derivative over rik
+       fac=-eij1**2/dtriss*(-2.0*atriss*(rij-rik)+2.0*btriss*(rij+rik))
+      &-eij3**2/dtriss*(2.0*atriss*(rik-rjk)+2.0*btriss*(rik+rjk))
+             gg(1)=xik*fac/rik
+             gg(2)=yik*fac/rik
+             gg(3)=zik*fac/rik
+       do m=1,3
+         gvdwx(m,i)=gvdwx(m,i)-gg(m)
+         gvdwx(m,k)=gvdwx(m,k)+gg(m)
+       enddo
+       do l=1,3
+         gvdwc(l,i)=gvdwc(l,i)-gg(l)
+         gvdwc(l,k)=gvdwc(l,k)+gg(l)
+       enddo
+ C now derivative over rjk
+       fac=-eij2**2/dtriss*(-2.0*atriss*(rij-rjk)+2.0*btriss*(rij+rjk))-
+      &eij3**2/dtriss*(-2.0*atriss*(rik-rjk)+2.0*btriss*(rik+rjk))
+             gg(1)=xjk*fac/rjk
+             gg(2)=yjk*fac/rjk
+             gg(3)=zjk*fac/rjk
+       do m=1,3
+         gvdwx(m,j)=gvdwx(m,j)-gg(m)
+         gvdwx(m,k)=gvdwx(m,k)+gg(m)
+       enddo
+       do l=1,3
+         gvdwc(l,j)=gvdwc(l,j)-gg(l)
+         gvdwc(l,k)=gvdwc(l,k)+gg(l)
+       enddo
+       return
+       end
@@@ -56,7 -56,6 +56,6 @@@ c      call memmon_print_usage(
        if (me.eq.king) call cinfo
  C Read force field parameters and job setup data
        call readrtns
-       call flush(iout)
  C
        if (me.eq.king .or. .not. out1file) then
         write (iout,'(2a/)') 
@@@ -103,12 -102,7 +102,12 @@@ C Fine-grain slaves just do energy and 
        else if (modecalc.eq.12) then
          call exec_MD
        else if (modecalc.eq.14) then
 +#ifdef MPI
          call exec_MREMD
 +#else
 +        write (iout,*) "Need a parallel version to run MREMD."
 +        stop
 +#endif
        else
          write (iout,'(a)') 'This calculation type is not supported',
       &   ModeCalc
@@@ -137,14 -131,19 +136,20 @@@ c--------------------------------------
        include 'COMMON.SETUP'
        include 'COMMON.CONTROL'
        include 'COMMON.IOUNITS'
-       if (me.eq.king .or. .not. out1file)
-      &   write (iout,*) "Calling chainbuild"
+ c      if (me.eq.king .or. .not. out1file) then
+ c        write (iout,*) "Calling chainbuild"
+ c        call flush(iout)
+ c      endif
        call chainbuild
+ c      if (me.eq.king .or. .not. out1file) then
+ c        write (iout,*) "Calling MD"
+ c        call flush(iout)
+ c      endif
        call MD
        return
        end
  c---------------------------------------------------------------------------
 +#ifdef MPI
        subroutine exec_MREMD
        include 'DIMENSIONS'
  #ifdef MPI
        endif
        return
        end
 +#endif
  c---------------------------------------------------------------------------
        subroutine exec_eeval_or_minim
        implicit real*8 (a-h,o-z)
        double precision energy(0:n_ene)
        double precision energy_long(0:n_ene),energy_short(0:n_ene)
        double precision varia(maxvar)
 -      if (indpdb.eq.0) call chainbuild
 +      if (indpdb.eq.0)     call chainbuild
 +      print *,'dc',c(1,1)
 +      if (indpdb.ne.0) then
 +      dc(1,0)=c(1,1)
 +      dc(2,0)=c(2,1)
 +      dc(3,0)=c(3,1)
 +      endif
 +#ifdef MPI
        time00=MPI_Wtime()
 +#else
 +      time00=tcpu()
 +#endif
        call chainbuild_cart
 +      print *,'dc',dc(1,0),dc(2,0),dc(3,0)
        if (split_ene) then
         print *,"Processor",myrank," after chainbuild"
         icall=1
         call enerprint(energy(0))
        endif
        call etotal(energy(0))
 +#ifdef MPI
        time_ene=MPI_Wtime()-time00
 +#else 
 +      time_ene=tcpu()-time00
 +#endif
        write (iout,*) "Time for energy evaluation",time_ene
        print *,"after etotal"
        etota = energy(0)
        etot =etota
        call enerprint(energy(0))
        call hairpin(.true.,nharp,iharp)
 +        print *,'after hairpin'
        call secondary2(.true.)
 +        print *,'after secondary'
        if (minim) then
  crc overlap test
          if (overlapsc) then 
  
          if (dccart) then
            print *, 'Calling MINIM_DC'
 +#ifdef MPI
            time1=MPI_WTIME()
 +#else
 +          time1=tcpu()
 +#endif
            call minim_dc(etot,iretcode,nfun)
          else
            if (indpdb.ne.0) then 
            endif
            call geom_to_var(nvar,varia)
            print *,'Calling MINIMIZE.'
 +#ifdef MPI
            time1=MPI_WTIME()
 +#else
 +          time1=tcpu()
 +#endif
            call minimize(etot,varia,iretcode,nfun)
          endif
          print *,'SUMSL return code is',iretcode,' eval ',nfun
 +#ifdef MPI
          evals=nfun/(MPI_WTIME()-time1)
 +#else
 +        evals=nfun/(tcpu()-time1)
 +#endif
          print *,'# eval/s',evals
          print *,'refstr=',refstr
 -        call hairpin(.true.,nharp,iharp)
 +        call hairpin(.false.,nharp,iharp)
 +        print *,'after hairpin'
          call secondary2(.true.)
 +        print *,'after secondary'
          call etotal(energy(0))
          etot = energy(0)
          call enerprint(energy(0))
@@@ -649,7 -616,7 +654,7 @@@ c Broadcast the order to compute intern
        endif
        do while (.not. eof)
            if (read_cart) then
 -            read (intin,'(e15.10,e15.5)',end=1100,err=1100) time,ene
 +            read (intin,'(e15.10,e15.5)',end=11,err=11) time,ene
              call read_x(intin,*11)
  #ifdef MPI
  c Broadcast the order to compute internal coordinates to the slaves.
  #endif
              call int_from_cart1(.false.)
            else
 -            read (intin,'(i5)',end=1100,err=1100) iconf
 +            read (intin,'(i5)',end=11,err=11) iconf
              call read_angles(intin,*11)
              call geom_to_var(nvar,varia)
              call chainbuild
@@@ -709,6 -676,7 +714,7 @@@ c--------------------------------------
        include 'COMMON.SBRIDGE'
        common /srutu/ icall
        double precision energy(0:max_ene)
+       print *,"A TU?"
  c      do i=2,nres
  c        vbld(i)=vbld(i)+ran_number(-0.1d0,0.1d0)
  c        if (itype(i).ne.10) 
@@@ -734,10 -702,14 +740,14 @@@ c      endd
        totT=1.d0
        eq_time=0.0d0
        call read_fragments
+       print *, "AFTER read fragments"
        call chainbuild_cart
+       print *,"chainbuild_cart"
        call cartprint
+       print *,"After cartprint"
        call intout
        icall=1
+       print *,"before ETOT"
        call etotal(energy(0))
        etot = energy(0)
        call enerprint(energy(0))
        print *,'icheckgrad=',icheckgrad
        goto (10,20,30) icheckgrad
    10  call check_ecartint
+       call check_ecartint
        return
    20  call check_cartgrad
        return
@@@ -195,17 -195,15 +195,17 @@@ c      call memmon_print_usage(
        enddo
        nlob(ntyp1)=0
        dsc(ntyp1)=0.0D0
 +      do iblock=1,2
        do i=1,maxtor
        itortyp(i)=0
        do j=1,maxtor
          do k=1,maxterm
 -          v1(k,j,i)=0.0D0
 -          v2(k,j,i)=0.0D0
 +          v1(k,j,i,iblock)=0.0D0
 +          v2(k,j,i,iblock)=0.0D0
            enddo
          enddo
        enddo
 +      enddo
        do i=1,maxres
        itype(i)=0
        itel(i)=0
@@@ -318,7 -316,7 +318,7 @@@ C... to deal with by current processor
          itask_cont_from(i)=fg_rank
          itask_cont_to(i)=fg_rank
        enddo
-       lprint=.false.
+       lprint=energy_dec
        if (lprint)
       &write (iout,*) 'INIT_INT_TABLE nres=',nres,' nnt=',nnt,' nct=',nct
        n_sc_int_tot=(nct-nnt+1)*(nct-nnt)/2-nss
@@@ -417,6 -415,7 +417,7 @@@ c            write (iout,*) 'jj=nct
        iatsc_s=nnt
        iatsc_e=nct-1
  #endif
+       if (iatsc_s.eq.0) iatsc_s=1
  #ifdef MPI
        if (lprint) write (*,*) 'Processor',fg_rank,' CG Group',kolor,
       &   ' absolute rank',myrank,' iatsc_s=',iatsc_s,' iatsc_e=',iatsc_e
@@@ -549,6 -548,7 +550,7 @@@ cd        write (iout,*) 'i.gt.nct-iscp
          endif 
        enddo ! i
  #endif
+       if (iatscp_s.eq.0) iatscp_s=1
        if (lprint) then
          write (iout,'(a)') 'SC-p interaction array:'
          do i=iatscp_s,iatscp_e
@@@ -210,10 -210,9 +210,11 @@@ C Kozlowska et al., J. Phys.: Condens. 
  C
        read (ithep,*,err=111,end=111) nthetyp,ntheterm,ntheterm2,
       &  ntheterm3,nsingle,ndouble
+ C      print *, "tu"
        nntheterm=max0(ntheterm,ntheterm2,ntheterm3)
 +      write(iout,*) "I am here",ntyp1
        read (ithep,*,err=111,end=111) (ithetyp(i),i=1,ntyp1)
 +C      write(iout,*) "I am herew"
        do i=-ntyp1,-1
          ithetyp(i)=-ithetyp(-i)
        enddo
@@@ -251,6 -250,7 +252,7 @@@ c VAR:ntethtyp is type of theta potenti
  c VAR:1=non-glicyne non-proline 2=proline
  c VAR:negative values for D-aminoacid
        do i=0,nthetyp
+ C        print *,i
          do j=-nthetyp,nthetyp
            do k=-nthetyp,nthetyp
              read (ithep,'(6a)',end=111,err=111) res1
@@@ -604,54 -604,38 +606,54 @@@ C Read torsional parameter
  C
        read (itorp,*,end=113,err=113) ntortyp
        read (itorp,*,end=113,err=113) (itortyp(i),i=1,ntyp)
 -c      write (iout,*) 'ntortyp',ntortyp
 -      do i=1,ntortyp
 -      do j=1,ntortyp
 -        read (itorp,*,end=113,err=113) nterm(i,j),nlor(i,j)
 +      do iblock=1,2
 +      do i=-ntyp,-1
 +       itortyp(i)=-itortyp(-i)
 +      enddo
 +      write (iout,*) 'ntortyp',ntortyp
 +      do i=0,ntortyp-1
 +        do j=-ntortyp+1,ntortyp-1
 +          read (itorp,*,end=113,err=113) nterm(i,j,iblock),
 +     &          nlor(i,j,iblock)
 +          nterm(-i,-j,iblock)=nterm(i,j,iblock)
 +          nlor(-i,-j,iblock)=nlor(i,j,iblock)
            v0ij=0.0d0
            si=-1.0d0
 -        do k=1,nterm(i,j)
 -          read (itorp,*,end=113,err=113) kk,v1(k,i,j),v2(k,i,j) 
 -            v0ij=v0ij+si*v1(k,i,j)
 +          do k=1,nterm(i,j,iblock)
 +            read (itorp,*,end=113,err=113) kk,v1(k,i,j,iblock),
 +     &      v2(k,i,j,iblock)
 +            v1(k,-i,-j,iblock)=v1(k,i,j,iblock)
 +            v2(k,-i,-j,iblock)=-v2(k,i,j,iblock)
 +            v0ij=v0ij+si*v1(k,i,j,iblock)
              si=-si
 +c         write(iout,*) i,j,k,iblock,nterm(i,j,iblock)
 +c         write(iout,*) v1(k,-i,-j,iblock),v1(k,i,j,iblock),
 +c      &v2(k,-i,-j,iblock),v2(k,i,j,iblock)
            enddo
 -        do k=1,nlor(i,j)
 +          do k=1,nlor(i,j,iblock)
              read (itorp,*,end=113,err=113) kk,vlor1(k,i,j),
 -     &        vlor2(k,i,j),vlor3(k,i,j) 
 +     &        vlor2(k,i,j),vlor3(k,i,j)
              v0ij=v0ij+vlor1(k,i,j)/(1+vlor3(k,i,j)**2)
            enddo
 -          v0(i,j)=v0ij
 +          v0(i,j,iblock)=v0ij
 +          v0(-i,-j,iblock)=v0ij
          enddo
        enddo
 +      enddo
        close (itorp)
        if (lprint) then
 -      write (iout,'(/a/)') 'Torsional constants:'
 -      do i=1,ntortyp
 -        do j=1,ntortyp
 +        write (iout,'(/a/)') 'Torsional constants:'
 +        do i=1,ntortyp
 +          do j=1,ntortyp
              write (iout,*) 'ityp',i,' jtyp',j
              write (iout,*) 'Fourier constants'
 -            do k=1,nterm(i,j)
 -            write (iout,'(2(1pe15.5))') v1(k,i,j),v2(k,i,j)
 +            do k=1,nterm(i,j,iblock)
 +              write (iout,'(2(1pe15.5))') v1(k,i,j,iblock),
 +     &        v2(k,i,j,iblock)
              enddo
              write (iout,*) 'Lorenz constants'
 -            do k=1,nlor(i,j)
 -            write (iout,'(3(1pe15.5))') 
 +            do k=1,nlor(i,j,iblock)
 +              write (iout,'(3(1pe15.5))')
       &         vlor1(k,i,j),vlor2(k,i,j),vlor3(k,i,j)
              enddo
            enddo
  C
  C 6/23/01 Read parameters for double torsionals
  C
 -      do i=1,ntortyp
 -        do j=1,ntortyp
 -          do k=1,ntortyp
 +C      do i=1,ntortyp
 +C        do j=1,ntortyp
 +C          do k=1,ntortyp
 +      do iblock=1,2
 +      do i=0,ntortyp-1
 +        do j=-ntortyp+1,ntortyp-1
 +          do k=-ntortyp+1,ntortyp-1
              read (itordp,'(3a1)',end=114,err=114) t1,t2,t3
- c              write (iout,*) "OK onelett",
- c     &         i,j,k,t1,t2,t3
+               write (iout,*) "OK onelett",
+      &         i,j,k,t1,t2,t3
  
-             if (t1.ne.toronelet(i) .or. t2.ne.toronelet(j) 
-      &        .or. t3.ne.toronelet(k)) then
+             if (t1.ne.onelett(i) .or. t2.ne.onelett(j) 
+      &        .or. t3.ne.onelett(k)) then
                write (iout,*) "Error in double torsional parameter file",
       &         i,j,k,t1,t2,t3
  #ifdef MPI
  #endif
                 stop "Error in double torsional parameter file"
              endif
 -            read (itordp,*,end=114,err=114) ntermd_1(i,j,k),
 -     &         ntermd_2(i,j,k)
 -            read (itordp,*,end=114,err=114) (v1c(1,l,i,j,k),l=1,
 -     &         ntermd_1(i,j,k))
 -            read (itordp,*,end=114,err=114) (v1s(1,l,i,j,k),l=1,
 -     &         ntermd_1(i,j,k))
 -            read (itordp,*,end=114,err=114) (v1c(2,l,i,j,k),l=1,
 -     &         ntermd_1(i,j,k))
 -            read (itordp,*,end=114,err=114) (v1s(2,l,i,j,k),l=1,
 -     &         ntermd_1(i,j,k))
 -            read (itordp,*,end=114,err=114) ((v2c(l,m,i,j,k),
 -     &         v2c(m,l,i,j,k),v2s(l,m,i,j,k),v2s(m,l,i,j,k),
 -     &         m=1,l-1),l=1,ntermd_2(i,j,k))
 -          enddo
 -        enddo
 -      enddo
 +           read (itordp,*,end=114,err=114) ntermd_1(i,j,k,iblock),
 +     &         ntermd_2(i,j,k,iblock)
 +            ntermd_1(-i,-j,-k,iblock)=ntermd_1(i,j,k,iblock)
 +            ntermd_2(-i,-j,-k,iblock)=ntermd_2(i,j,k,iblock)
 +            read (itordp,*,end=114,err=114) (v1c(1,l,i,j,k,iblock),l=1,
 +     &         ntermd_1(i,j,k,iblock))
 +            read (itordp,*,end=114,err=114)(v1s(1,l,i,j,k,iblock),l=1,
 +     &         ntermd_1(i,j,k,iblock))
 +            read (itordp,*,end=114,err=114)(v1c(2,l,i,j,k,iblock),l=1,
 +     &         ntermd_1(i,j,k,iblock))
 +            read (itordp,*,end=114,err=114)(v1s(2,l,i,j,k,iblock),l=1,
 +     &         ntermd_1(i,j,k,iblock))
 +C Martix of D parameters for one dimesional foureir series
 +            do l=1,ntermd_1(i,j,k,iblock)
 +             v1c(1,l,-i,-j,-k,iblock)=v1c(1,l,i,j,k,iblock)
 +             v1s(1,l,-i,-j,-k,iblock)=-v1s(1,l,i,j,k,iblock)
 +             v1c(2,l,-i,-j,-k,iblock)=v1c(2,l,i,j,k,iblock)
 +             v1s(2,l,-i,-j,-k,iblock)=-v1s(2,l,i,j,k,iblock)
 +c            write(iout,*) "whcodze" ,
 +c     & v1s(2,l,-i,-j,-k,iblock),v1s(2,l,i,j,k,iblock)
 +            enddo
 +            read (itordp,*,end=114,err=114) ((v2c(l,m,i,j,k,iblock),
 +     &         v2c(m,l,i,j,k,iblock),v2s(l,m,i,j,k,iblock),
 +     &         v2s(m,l,i,j,k,iblock),
 +     &         m=1,l-1),l=1,ntermd_2(i,j,k,iblock))
 +C Martix of D parameters for two dimesional fourier series
 +            do l=1,ntermd_2(i,j,k,iblock)
 +             do m=1,l-1
 +             v2c(l,m,-i,-j,-k,iblock)=v2c(l,m,i,j,k,iblock)
 +             v2c(m,l,-i,-j,-k,iblock)=v2c(m,l,i,j,k,iblock)
 +             v2s(l,m,-i,-j,-k,iblock)=-v2s(l,m,i,j,k,iblock)
 +             v2s(m,l,-i,-j,-k,iblock)=-v2s(m,l,i,j,k,iblock)
 +             enddo!m
 +            enddo!l
 +          enddo!k
 +        enddo!j
 +      enddo!i
 +      enddo!iblock
        if (lprint) then
 -      write (iout,*) 
 +      write (iout,*)
        write (iout,*) 'Constants for double torsionals'
        do iblock=1,2
        do i=0,ntortyp-1
          do j=-ntortyp+1,ntortyp-1
            do k=-ntortyp+1,ntortyp-1
              write (iout,*) 'ityp',i,' jtyp',j,' ktyp',k,
 -     &        ' nsingle',ntermd_1(i,j,k),' ndouble',ntermd_2(i,j,k)
 +     &        ' nsingle',ntermd_1(i,j,k,iblock),
 +     &        ' ndouble',ntermd_2(i,j,k,iblock)
              write (iout,*)
              write (iout,*) 'Single angles:'
 -            do l=1,ntermd_1(i,j,k)
 -              write (iout,'(i5,2f10.5,5x,2f10.5)') l,
 -     &           v1c(1,l,i,j,k),v1s(1,l,i,j,k),
 -     &           v1c(2,l,i,j,k),v1s(2,l,i,j,k)
 +            do l=1,ntermd_1(i,j,k,iblock)
 +              write (iout,'(i5,2f10.5,5x,2f10.5,5x,2f10.5)') l,
 +     &           v1c(1,l,i,j,k,iblock),v1s(1,l,i,j,k,iblock),
 +     &           v1c(2,l,i,j,k,iblock),v1s(2,l,i,j,k,iblock),
 +     &           v1s(1,l,-i,-j,-k,iblock),v1s(2,l,-i,-j,-k,iblock)
              enddo
              write (iout,*)
              write (iout,*) 'Pairs of angles:'
 -            write (iout,'(3x,20i10)') (l,l=1,ntermd_2(i,j,k))
 -            do l=1,ntermd_2(i,j,k)
 -              write (iout,'(i5,20f10.5)') 
 -     &         l,(v2c(l,m,i,j,k),m=1,ntermd_2(i,j,k))
 +            write (iout,'(3x,20i10)') (l,l=1,ntermd_2(i,j,k,iblock))
 +            do l=1,ntermd_2(i,j,k,iblock)
 +              write (iout,'(i5,20f10.5)')
 +     &         l,(v2c(l,m,i,j,k,iblock),m=1,ntermd_2(i,j,k,iblock))
              enddo
              write (iout,*)
 -            write (iout,'(3x,20i10)') (l,l=1,ntermd_2(i,j,k))
 -            do l=1,ntermd_2(i,j,k)
 -              write (iout,'(i5,20f10.5)') 
 -     &         l,(v2s(l,m,i,j,k),m=1,ntermd_2(i,j,k))
 +            write (iout,'(3x,20i10)') (l,l=1,ntermd_2(i,j,k,iblock))
 +            do l=1,ntermd_2(i,j,k,iblock)
 +              write (iout,'(i5,20f10.5)')
 +     &         l,(v2s(l,m,i,j,k,iblock),m=1,ntermd_2(i,j,k,iblock)),
 +     &         (v2s(l,m,-i,-j,-k,iblock),m=1,ntermd_2(i,j,k,iblock))
              enddo
              write (iout,*)
            enddo
@@@ -763,13 -718,12 +765,13 @@@ CC
  C
        read (isccor,*,end=1113,err=1113) nsccortyp
  #ifdef SCCORPDB
 +      write (iout,*) "Tu wchodze"
        read (isccor,*,end=1113,err=1113) (isccortyp(i),i=1,ntyp)
        do i=-ntyp,-1
          isccortyp(i)=-isccortyp(-i)
        enddo
        iscprol=isccortyp(20)
 -c      write (iout,*) 'ntortyp',ntortyp
 +C      write (iout,*) 'ntortyp',ntortyp
        maxinter=3
  cc maxinter is maximum interaction sites
        do l=1,maxinter    
          do j=1,nsccortyp
           read (isccor,*,end=1113,err=1113) nterm_sccor(i,j),
       &             nlor_sccor(i,j)
+           print *,i,j,l
            v0ijsccor=0.0d0
            v0ijsccor1=0.0d0
            v0ijsccor2=0.0d0
            nterm_sccor(-i,-j)=nterm_sccor(i,j)
            nterm_sccor(i,-j)=nterm_sccor(i,j)  
            do k=1,nterm_sccor(i,j)
 -           read (isccor,*,end=119,err=119) kk,v1sccor(k,l,i,j)
 +           read (isccor,*,end=1113,err=1113) kk,v1sccor(k,l,i,j)
       &    ,v2sccor(k,l,i,j)
 +c            write(iout,*) "k=",kk
              if (j.eq.iscprol) then
                if (i.eq.isccortyp(10)) then
                v1sccor(k,l,i,-j)=v1sccor(k,l,i,j)
@@@ -844,7 -798,6 +847,7 @@@ C     &    ,v2sccor(k,l,i,j
        enddo
        close (isccor)
  #else
 +      write(iout,*) "a tu nie wchodze"
        read (isccor,*,end=119,err=119) (isccortyp(i),i=1,ntyp)
  c      write (iout,*) 'ntortyp',ntortyp
        maxinter=3
@@@ -916,8 -869,8 +919,8 @@@ c        b1(1,i)=0.0d
  c        b1(2,i)=0.0d0
          B1tilde(1,i) = b(3)
          B1tilde(2,i) =-b(5)
-         B1tilde(1,-i) =-b(3)
-         B1tilde(2,-i) =b(5)
+ C        B1tilde(1,-i) =-b(3)
+ C        B1tilde(2,-i) =b(5)
  c        b1tilde(1,i)=0.0d0
  c        b1tilde(2,i)=0.0d0
          B2(1,i)  = b(2)
@@@ -589,7 -589,7 +589,7 @@@ cmc      write(iout,*)"ALLNSS ",allnss,
       &    MPI_INTEGER,MPI_SUM,king,FG_COMM,IERR)
          call MPI_Gather(newnss,1,MPI_INTEGER,
       &                  i_newnss,1,MPI_INTEGER,king,FG_COMM,IERR)
-         displ(0)=0
+ C        displ(0)=0
          do i=1,nfgtasks-1,1
            displ(i)=i_newnss(i-1)+displ(i-1)
          enddo
@@@ -655,41 -655,6 +655,6 @@@ cmc      write(iout,*)"NEWNSS ",newnss,
        return
        end
  
- c----------------------------------------------------------------------------
- #ifdef WHAM
-       subroutine read_ssHist
-       implicit none
- c     Includes
-       include 'DIMENSIONS'
-       include "DIMENSIONS.FREE"
-       include 'COMMON.FREE'
- c     Local variables
-       integer i,j
-       character*80 controlcard
-       do i=1,dyn_nssHist
-         call card_concat(controlcard,.true.)
-         read(controlcard,*)
-      &       dyn_ssHist(i,0),(dyn_ssHist(i,j),j=1,2*dyn_ssHist(i,0))
-       enddo
-       return
-       end
- #endif
- c----------------------------------------------------------------------------
- C-----------------------------------------------------------------------------
- C-----------------------------------------------------------------------------
- C-----------------------------------------------------------------------------
- C-----------------------------------------------------------------------------
- C-----------------------------------------------------------------------------
- C-----------------------------------------------------------------------------
- C-----------------------------------------------------------------------------
  
  c$$$c-----------------------------------------------------------------------------
  c$$$
@@@ -1949,150 -1914,3 +1914,150 @@@ c$$$      retur
  c$$$      end                                                               
  c$$$
  c$$$C-----------------------------------------------------------------------------
 +         subroutine triple_ssbond_ene(resi,resj,resk,eij)
 +      include 'DIMENSIONS'
 +      include 'COMMON.SBRIDGE'
 +      include 'COMMON.CHAIN'
 +      include 'COMMON.DERIV'
 +      include 'COMMON.LOCAL'
 +      include 'COMMON.INTERACT'
 +      include 'COMMON.VAR'
 +      include 'COMMON.IOUNITS'
 +      include 'COMMON.CALC'
 +#ifndef CLUST
 +#ifndef WHAM
 +      include 'COMMON.MD'
 +#endif
 +#endif
 +
 +c     External functions
 +      double precision h_base
 +      external h_base
 +
 +c     Input arguments
 +      integer resi,resj,resk
 +
 +c     Output arguments
 +      double precision eij,eij1,eij2,eij3
 +
 +c     Local variables
 +      logical havebond
 +c      integer itypi,itypj,k,l
 +      double precision rrij,ssd,deltat1,deltat2,deltat12,cosphi
 +      double precision rrik,rrjk,rik,rjk,xi,xk,yi,yk,zi,zk,xij,yij,zij
 +      double precision xik,yik,zik,xjk,yjk,zjk
 +      double precision sig0ij,ljd,sig,fac,e1,e2
 +      double precision dcosom1(3),dcosom2(3),ed
 +      double precision pom1,pom2
 +      double precision ljA,ljB,ljXs
 +      double precision d_ljB(1:3)
 +      double precision ssA,ssB,ssC,ssXs
 +      double precision ssxm,ljxm,ssm,ljm
 +      double precision d_ssxm(1:3),d_ljxm(1:3),d_ssm(1:3),d_ljm(1:3)
 +
 +      i=resi
 +      j=resj
 +      k=resk
 +C      write(iout,*) resi,resj,resk
 +      itypi=itype(i)
 +      dxi=dc_norm(1,nres+i)
 +      dyi=dc_norm(2,nres+i)
 +      dzi=dc_norm(3,nres+i)
 +      dsci_inv=vbld_inv(i+nres)
 +      xi=c(1,nres+i)
 +      yi=c(2,nres+i)
 +      zi=c(3,nres+i)
 +
 +      itypj=itype(j)
 +      xj=c(1,nres+j)
 +      yj=c(2,nres+j)
 +      zj=c(3,nres+j)
 +      
 +      dxj=dc_norm(1,nres+j)
 +      dyj=dc_norm(2,nres+j)
 +      dzj=dc_norm(3,nres+j)
 +      dscj_inv=vbld_inv(j+nres)
 +      itypk=itype(k)
 +      xk=c(1,nres+k)
 +      yk=c(2,nres+k)
 +      zk=c(3,nres+k)
 +      
 +      dxk=dc_norm(1,nres+k)
 +      dyk=dc_norm(2,nres+k)
 +      dzk=dc_norm(3,nres+k)
 +      dscj_inv=vbld_inv(k+nres)
 +      xij=xj-xi
 +      xik=xk-xi
 +      xjk=xk-xj
 +      yij=yj-yi
 +      yik=yk-yi
 +      yjk=yk-yj
 +      zij=zj-zi
 +      zik=zk-zi
 +      zjk=zk-zj
 +      rrij=(xij*xij+yij*yij+zij*zij)
 +      rij=dsqrt(rrij)  ! sc_angular needs rij to really be the inverse
 +      rrik=(xik*xik+yik*yik+zik*zik)
 +      rik=dsqrt(rrik)
 +      rrjk=(xjk*xjk+yjk*yjk+zjk*zjk)
 +      rjk=dsqrt(rrjk)
 +C there are three combination of distances for each trisulfide bonds
 +C The first case the ith atom is the center
 +C Energy function is E=d/(a*(x-y)**2+b*(x+y)**2+c) where x is first
 +C distance y is second distance the a,b,c,d are parameters derived for
 +C this problem d parameter was set as a penalty currenlty set to 1.
 +      eij1=dtriss/(atriss*(rij-rik)**2+btriss*(rij+rik)**2+ctriss)
 +C second case jth atom is center
 +      eij2=dtriss/(atriss*(rij-rjk)**2+btriss*(rij+rjk)**2+ctriss)
 +C the third case kth atom is the center
 +      eij3=dtriss/(atriss*(rik-rjk)**2+btriss*(rik+rjk)**2+ctriss)
 +C      eij2=0.0
 +C      eij3=0.0
 +C      eij1=0.0
 +      eij=eij1+eij2+eij3
 +C      write(iout,*)i,j,k,eij
 +C The energy penalty calculated now time for the gradient part 
 +C derivative over rij
 +      fac=-eij1**2/dtriss*(2.0*atriss*(rij-rik)+2.0*btriss*(rij+rik))
 +     &-eij2**2/dtriss*(2.0*atriss*(rij-rjk)+2.0*btriss*(rij+rjk))  
 +            gg(1)=xij*fac/rij
 +            gg(2)=yij*fac/rij
 +            gg(3)=zij*fac/rij
 +      do m=1,3
 +        gvdwx(m,i)=gvdwx(m,i)-gg(m)
 +        gvdwx(m,j)=gvdwx(m,j)+gg(m)
 +      enddo
 +      do l=1,3
 +        gvdwc(l,i)=gvdwc(l,i)-gg(l)
 +        gvdwc(l,j)=gvdwc(l,j)+gg(l)
 +      enddo
 +C now derivative over rik
 +      fac=-eij1**2/dtriss*(-2.0*atriss*(rij-rik)+2.0*btriss*(rij+rik))
 +     &-eij3**2/dtriss*(2.0*atriss*(rik-rjk)+2.0*btriss*(rik+rjk))
 +            gg(1)=xik*fac/rik
 +            gg(2)=yik*fac/rik
 +            gg(3)=zik*fac/rik
 +      do m=1,3
 +        gvdwx(m,i)=gvdwx(m,i)-gg(m)
 +        gvdwx(m,k)=gvdwx(m,k)+gg(m)
 +      enddo
 +      do l=1,3
 +        gvdwc(l,i)=gvdwc(l,i)-gg(l)
 +        gvdwc(l,k)=gvdwc(l,k)+gg(l)
 +      enddo
 +C now derivative over rjk
 +      fac=-eij2**2/dtriss*(-2.0*atriss*(rij-rjk)+2.0*btriss*(rij+rjk))-
 +     &eij3**2/dtriss*(-2.0*atriss*(rik-rjk)+2.0*btriss*(rik+rjk))
 +            gg(1)=xjk*fac/rjk
 +            gg(2)=yjk*fac/rjk
 +            gg(3)=zjk*fac/rjk
 +      do m=1,3
 +        gvdwx(m,j)=gvdwx(m,j)-gg(m)
 +        gvdwx(m,k)=gvdwx(m,k)+gg(m)
 +      enddo
 +      do l=1,3
 +        gvdwc(l,j)=gvdwc(l,j)-gg(l)
 +        gvdwc(l,k)=gvdwc(l,k)+gg(l)
 +      enddo
 +      return
 +      end
@@@ -35,7 -35,7 +35,7 @@@
        double precision tole /1.0d-1/
        integer i,itj,ii,iii,j,k,l,licz
        integer ir,ib,ipar,iparm
-       integer iscor,islice
+       integer iscor,islice,scount_buff(0:99)
        real*4 csingle(3,maxres2)
        double precision energ
        double precision temp
@@@ -60,8 -60,6 +60,8 @@@
            enddo
          enddo
        enddo
 +      write (iout,*) "indstart(me1),indend(me1)"
 +     &,indstart(me1),indend(me1)
        do i=indstart(me1),indend(me1)
  #else
        do iparm=1,nParmSet
@@@ -73,7 -71,6 +73,7 @@@
        enddo
        do i=1,ntot
  #endif
 +
          read(ientout,rec=i,err=101) 
       &    ((csingle(l,k),l=1,3),k=1,nres),
       &    ((csingle(l,k+nres),l=1,3),k=nnt,nct),
@@@ -157,17 -154,14 +157,17 @@@ c     &   " kfac",kfac,"quot",quot," fT
       &      wcorr,wcorr5,wcorr6,wturn4,wturn3,wturn6,wel_loc,
       &      wtor_d,wsccor,wbond
  #endif
 +C        write (iout,*) "tuz przed energia"
          call etotal(energia(0),fT)
 +C        write (iout,*) "tuz za energia"
 +
  #ifdef DEBUG
          write (iout,*) "Conformation",i
            write (iout,'(8f10.5)') ((c(l,k),l=1,3),k=1,nres)
            write (iout,'(8f10.5)') ((c(l,k+nres),l=1,3),k=nnt,nct)
          call enerprint(energia(0),fT)
          write (iout,'(2i5,21f8.2)') i,iparm,(energia(k),k=1,21)
-         write (iout,*) "ftors",ftors
+         write (iout,*) "ftors(1)",ftors(1)
          call briefout(i,energia(0))
          temp=1.0d0/(beta_h(ib,ipar)*1.987D-3)
          write (iout,*) "temp", temp
@@@ -226,7 -220,6 +226,7 @@@ c              call intou
                endif
              endif
            endif
 +C          write (iout,*) "Czy tu dochodze"
            potE(iii+1,iparm)=energia(0)
            do k=1,21
              enetb(k,iii+1,iparm)=energia(k)
@@@ -277,12 -270,15 +277,15 @@@ c     &   " snk",snk_p(iR,ib,ipar
    121   continue
        enddo   
  #ifdef MPI
-       scount(me)=iii 
-       write (iout,*) "Me",me," scount",scount(me)
+       scount_buff(me)=iii 
+       write (iout,*) "Me",me," scount_buff",scount_buff(me)
        call flush(iout)
  c  Master gathers updated numbers of conformations written by all procs.
-       call MPI_AllGather( scount(me), 1, MPI_INTEGER, scount(0), 1, 
+ c      call MPI_AllGather(MPI_IN_PLACE,1,MPI_DATATYPE_NULL,scount(0),1,
+ c     &  MPI_INTEGER, WHAM_COMM, IERROR)
+       call MPI_AllGather( scount_buff(me), 1, MPI_INTEGER, scount(0), 1,
       &  MPI_INTEGER, WHAM_COMM, IERROR)
        indstart(0)=1
        indend(0)=scount(0)
        do i=1, Nprocs-1
@@@ -368,7 -364,7 +371,7 @@@ c--------------------------------------
        double precision energ
        integer ilen,iroof
        external ilen,iroof
-       integer ir,ib,iparm
+       integer ir,ib,iparm, scount_buff(0:99)
        integer isecstr(maxres)
        write (licz2,'(bz,i2.2)') islice
        call opentmp(islice,ientout,bprotfile_temp)
@@@ -673,8 -669,13 +676,13 @@@ c      write (iout,*) "xdrf3dfcoord
  c      call flush(iout)
        call xdrfint_(ixdrf, nss, iret)
        do j=1,nss
-         call xdrfint_(ixdrf, ihpb(j), iret)
-         call xdrfint_(ixdrf, jhpb(j), iret)
+            if (dyn_ss) then
+             call xdrfint(ixdrf, idssb(j)+nres, iret)
+             call xdrfint(ixdrf, jdssb(j)+nres, iret)
+            else
+             call xdrfint_(ixdrf, ihpb(j), iret)
+             call xdrfint_(ixdrf, jhpb(j), iret)
+            endif
        enddo
        call xdrffloat_(ixdrf,real(eini),iret) 
        call xdrffloat_(ixdrf,real(efree),iret) 
  
        call xdrfint(ixdrf, nss, iret)
        do j=1,nss
-         call xdrfint(ixdrf, ihpb(j), iret)
-         call xdrfint(ixdrf, jhpb(j), iret)
+            if (dyn_ss) then
+             call xdrfint(ixdrf, idssb(j)+nres, iret)
+             call xdrfint(ixdrf, jdssb(j)+nres, iret)
+            else
+             call xdrfint(ixdrf, ihpb(j), iret)
+             call xdrfint(ixdrf, jhpb(j), iret)
+            endif
        enddo
        call xdrffloat(ixdrf,real(eini),iret) 
        call xdrffloat(ixdrf,real(efree),iret) 
@@@ -44,13 -44,11 +44,13 @@@ C Gay-Berne potential (shifted LJ, angu
        goto 106
  C Gay-Berne-Vorobjev potential (shifted LJ, angular dependence).
    105 call egbv(evdw,evdw_t)
 +C      write(iout,*) 'po elektostatyce'
  C
  C Calculate electrostatic (H-bonding) energy of the main chain.
  C
 -  106 call eelec(ees,evdw1,eel_loc,eello_turn3,eello_turn4)
 -C
 +  106  call eelec(ees,evdw1,eel_loc,eello_turn3,eello_turn4)
 +C            write(iout,*) 'po eelec'
 +
  C Calculate excluded-volume interaction energy between peptide groups
  C and side chains.
  C
@@@ -58,9 -56,8 +58,9 @@@
  c
  c Calculate the bond-stretching energy
  c
 +
        call ebond(estr)
 -c      write (iout,*) "estr",estr
 +C       write (iout,*) "estr",estr
  C 
  C Calculate the disulfide-bridge and other energy and the contributions
  C from other distance constraints.
@@@ -70,13 -67,13 +70,14 @@@ cd    print *,'EHPB exitted succesfully
  C
  C Calculate the virtual-bond-angle energy.
  C
-       call ebend(ebe)
 +C      print *,'Bend energy finished.'
+       call ebend(ebe,ethetacnstr)
+ cd    print *,'Bend energy finished.'
  C
  C Calculate the SC local energy.
  C
        call esc(escloc)
 -cd    print *,'SCLOC energy finished.'
 +C       print *,'SCLOC energy finished.'
  C
  C Calculate the virtual-bond torsional energy.
  C
@@@ -110,20 -107,20 +111,20 @@@ c      write (iout,*) "ft(6)",fact(6),
        etot=wsc*(evdw+fact(6)*evdw_t)+wscp*evdw2+welec*fact(1)*ees
       & +wvdwpp*evdw1
       & +wang*ebe+wtor*fact(1)*etors+wscloc*escloc
-      & +wstrain*ehpb+nss*ebr+wcorr*fact(3)*ecorr+wcorr5*fact(4)*ecorr5
+      & +wstrain*ehpb+wcorr*fact(3)*ecorr+wcorr5*fact(4)*ecorr5
       & +wcorr6*fact(5)*ecorr6+wturn4*fact(3)*eello_turn4
       & +wturn3*fact(2)*eello_turn3+wturn6*fact(5)*eturn6
       & +wel_loc*fact(2)*eel_loc+edihcnstr+wtor_d*fact(2)*etors_d
-      & +wbond*estr+wsccor*fact(1)*esccor
+      & +wbond*estr+wsccor*fact(1)*esccor+ethetacnstr
  #else
        etot=wsc*(evdw+fact(6)*evdw_t)+wscp*evdw2
       & +welec*fact(1)*(ees+evdw1)
       & +wang*ebe+wtor*fact(1)*etors+wscloc*escloc
-      & +wstrain*ehpb+nss*ebr+wcorr*fact(3)*ecorr+wcorr5*fact(4)*ecorr5
+      & +wstrain*ehpb+wcorr*fact(3)*ecorr+wcorr5*fact(4)*ecorr5
       & +wcorr6*fact(5)*ecorr6+wturn4*fact(3)*eello_turn4
       & +wturn3*fact(2)*eello_turn3+wturn6*fact(5)*eturn6
       & +wel_loc*fact(2)*eel_loc+edihcnstr+wtor_d*fact(2)*etors_d
-      & +wbond*estr+wsccor*fact(1)*esccor
+      & +wbond*estr+wsccor*fact(1)*esccor+ethetacnstr
  #endif
        energia(0)=etot
        energia(1)=evdw
        energia(19)=esccor
        energia(20)=edihcnstr
        energia(21)=evdw_t
+       energia(24)=ethetacnstr
  c detecting NaNQ
  #ifdef ISNAN
  #ifdef AIX
       &   +wturn3*fact(2)*gel_loc_turn3(i)
       &   +wturn6*fact(5)*gel_loc_turn6(i)
       &   +wel_loc*fact(2)*gel_loc_loc(i)
+ c     &   +wsccor*fact(1)*gsccor_loc(i)
+ c BYLA ROZNICA Z CLUSTER< OSTATNIA LINIA DODANA
        enddo
        endif
+       if (dyn_ss) call dyn_set_nss
        return
        end
  C------------------------------------------------------------------------
        esccor=energia(19)
        edihcnstr=energia(20)
        estr=energia(18)
+       ethetacnstr=energia(24)
  #ifdef SPLITELE
        write (iout,10) evdw,wsc,evdw2,wscp,ees,welec*fact(1),evdw1,
       &  wvdwpp,
       &  ecorr,wcorr*fact(3),ecorr5,wcorr5*fact(4),ecorr6,wcorr6*fact(5),
       &  eel_loc,wel_loc*fact(2),eello_turn3,wturn3*fact(2),
       &  eello_turn4,wturn4*fact(3),eello_turn6,wturn6*fact(5),
-      &  esccor,wsccor*fact(1),edihcnstr,ebr*nss,etot
+      &  esccor,wsccor*fact(1),edihcnstr,ethetacnstr,ebr*nss,etot
     10 format (/'Virtual-chain energies:'//
       & 'EVDW=  ',1pE16.6,' WEIGHT=',1pD16.6,' (SC-SC)'/
       & 'EVDW2= ',1pE16.6,' WEIGHT=',1pD16.6,' (SC-p)'/
       & 'ETURN6=',1pE16.6,' WEIGHT=',1pD16.6,' (turns, 6th order)'/
       & 'ESCCOR=',1pE16.6,' WEIGHT=',1pD16.6,' (backbone-rotamer corr)'/
       & 'EDIHC= ',1pE16.6,' (dihedral angle constraints)'/
+      & 'ETHETC= ',1pE16.6,' (valence angle constraints)'/
       & 'ESS=   ',1pE16.6,' (disulfide-bridge intrinsic energy)'/ 
       & 'ETOT=  ',1pE16.6,' (total)')
  #else
       &  ecorr6,wcorr6*fact(5),eel_loc,wel_loc*fact(2),
       &  eello_turn3,wturn3*fact(2),eello_turn4,wturn4*fact(3),
       &  eello_turn6,wturn6*fact(5),esccor*fact(1),wsccor,
-      &  edihcnstr,ebr*nss,etot
+      &  edihcnstr,ethetacnstr,ebr*nss,etot
     10 format (/'Virtual-chain energies:'//
       & 'EVDW=  ',1pE16.6,' WEIGHT=',1pD16.6,' (SC-SC)'/
       & 'EVDW2= ',1pE16.6,' WEIGHT=',1pD16.6,' (SC-p)'/
       & 'ETURN6=',1pE16.6,' WEIGHT=',1pD16.6,' (turns, 6th order)'/
       & 'ESCCOR=',1pE16.6,' WEIGHT=',1pD16.6,' (backbone-rotamer corr)'/
       & 'EDIHC= ',1pE16.6,' (dihedral angle constraints)'/
+      & 'ETHETC= ',1pE16.6,' (valence angle constraints)'/
       & 'ESS=   ',1pE16.6,' (disulfide-bridge intrinsic energy)'/ 
       & 'ETOT=  ',1pE16.6,' (total)')
  #endif
        integer icant
        external icant
  cd    print *,'Entering ELJ nnt=',nnt,' nct=',nct,' expon=',expon
+ c ROZNICA z cluster
        do i=1,210
          do j=1,2
            eneps_temp(j,i)=0.0d0
          enddo
        enddo
+ cROZNICA
        evdw=0.0D0
        evdw_t=0.0d0
        do i=iatsc_s,iatsc_e
@@@ -400,8 -407,11 +411,11 @@@ c           write (iout,*)'i=',i,' j=',
              e2=fac*bb(itypi,itypj)
              evdwij=e1+e2
              ij=icant(itypi,itypj)
+ c ROZNICA z cluster
              eneps_temp(1,ij)=eneps_temp(1,ij)+e1/dabs(eps0ij)
              eneps_temp(2,ij)=eneps_temp(2,ij)+e2/eps0ij
+ c
  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)/)')
        include 'COMMON.ENEPS'
        include 'COMMON.IOUNITS'
        include 'COMMON.CALC'
+       include 'COMMON.SBRIDGE'
        logical lprn
        common /srutu/icall
        integer icant
@@@ -800,14 -811,6 +815,14 @@@ c      if (icall.gt.0) lprn=.true
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
 +C returning the ith atom to box
 +          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
 +
          dxi=dc_norm(1,nres+i)
          dyi=dc_norm(2,nres+i)
          dzi=dc_norm(3,nres+i)
@@@ -817,6 -820,26 +832,26 @@@ C Calculate SC interaction energy
  C
          do iint=1,nint_gr(i)
            do j=istart(i,iint),iend(i,iint)
+             IF (dyn_ss_mask(i).and.dyn_ss_mask(j)) THEN
+               call dyn_ssbond_ene(i,j,evdwij)
+               evdw=evdw+evdwij
+ C            write (iout,'(a6,2i5,0pf7.3,a3,2f10.3)')
+ C     &                        'evdw',i,j,evdwij,' ss',evdw,evdw_t
+ C triple bond artifac removal
+              do k=j+1,iend(i,iint)
+ C search over all next residues
+               if (dyn_ss_mask(k)) then
+ C check if they are cysteins
+ C              write(iout,*) 'k=',k
+               call triple_ssbond_ene(i,j,k,evdwij)
+ C call the energy function that removes the artifical triple disulfide
+ C bond the soubroutine is located in ssMD.F
+               evdw=evdw+evdwij
+ C             write (iout,'(a6,2i5,0pf7.3,a3,2f10.3)')
+ C     &                        'evdw',i,j,evdwij,'tss',evdw,evdw_t
+               endif!dyn_ss_mask(k)
+              enddo! k
+             ELSE
              ind=ind+1
              itypj=iabs(itype(j))
              if (itypj.eq.ntyp1) cycle
@@@ -841,59 -864,15 +876,59 @@@ c           chip12=0.0D
  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)
 +C returning jth atom to box
 +          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 checking the distance
 +      dist_init=(xj-xi)**2+(yj-yi)**2+(zj-zi)**2
 +      xj_safe=xj
 +      yj_safe=yj
 +      zj_safe=zj
 +      subchap=0
 +C finding the closest
 +      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)
  c            write (iout,*) i,j,xj,yj,zj
              rrij=1.0D0/(xj*xj+yj*yj+zj*zj)
              rij=dsqrt(rrij)
 +            sss=sscale((1.0d0/rij)/sigma(itypi,itypj))
 +            sssgrad=sscagrad((1.0d0/rij)/sigma(itypi,itypj))
 +            if (sss.le.0.0) cycle
  C Calculate angle-dependent terms of energy and contributions to their
  C derivatives.
              call sc_angular
@@@ -916,9 -895,9 +951,9 @@@ c--------------------------------------
              eps3der=evdwij*eps2rt
              evdwij=evdwij*eps2rt*eps3rt
              if (bb(itypi,itypj).gt.0) then
 -              evdw=evdw+evdwij
 +              evdw=evdw+evdwij*sss
              else
 -              evdw_t=evdw_t+evdwij
 +              evdw_t=evdw_t+evdwij*sss
              endif
              ij=icant(itypi,itypj)
              aux=eps1*eps2rt**2*eps3rt**2
@@@ -947,7 -926,6 +982,7 @@@ C Calculate gradient components
              fac=-expon*(e1+evdwij)*rij_shift
              sigder=fac*sigder
              fac=rij*fac
 +            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
  C Calculate angular part of the gradient.
              call sc_grad
              endif
+ C            write(iout,*)  "partial sum", evdw, evdw_t
+             ENDIF    ! dyn_ss            
            enddo      ! j
          enddo        ! iint
        enddo          ! i
@@@ -1901,22 -1881,14 +1938,22 @@@ cd      endd
        do i=1,nres
          num_cont_hb(i)=0
        enddo
 -cd      print '(a)','Enter EELEC'
 -cd      write (iout,*) 'iatel_s=',iatel_s,' iatel_e=',iatel_e
 +C      print '(a)','Enter EELEC'
 +C      write (iout,*) 'iatel_s=',iatel_s,' iatel_e=',iatel_e
        do i=1,nres
          gel_loc_loc(i)=0.0d0
          gcorr_loc(i)=0.0d0
        enddo
        do i=iatel_s,iatel_e
 -        if (itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1) cycle
 +          if (i.eq.1) then 
 +           if (itype(i).eq.ntyp1.or. itype(i+1).eq.ntyp1
 +     &  .or. itype(i+2).eq.ntyp1) cycle
 +          else
 +        if (itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1
 +     &  .or. itype(i+2).eq.ntyp1
 +     &  .or. itype(i-1).eq.ntyp1
 +     &) cycle
 +         endif
          if (itel(i).eq.0) goto 1215
          dxi=dc(1,i)
          dyi=dc(2,i)
          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
          num_conti=0
 -c        write (iout,*) 'i',i,' ielstart',ielstart(i),' ielend',ielend(i)
 +C        write (iout,*) 'i',i,' ielstart',ielstart(i),' ielend',ielend(i)
          do j=ielstart(i),ielend(i)
 -          if (itype(j).eq.ntyp1 .or. itype(j+1).eq.ntyp1) cycle
 +          if (j.eq.1) then
 +           if (itype(j).eq.ntyp1 .or. itype(j+1).eq.ntyp1
 +     & .or.itype(j+2).eq.ntyp1
 +     &) cycle  
 +          else     
 +          if (itype(j).eq.ntyp1 .or. itype(j+1).eq.ntyp1
 +     & .or.itype(j+2).eq.ntyp1
 +     & .or.itype(j-1).eq.ntyp1
 +     &) cycle
 +         endif
 +C
 +C) cycle
            if (itel(j).eq.0) goto 1216
            ind=ind+1
            iteli=itel(i)
@@@ -1969,49 -1924,10 +2006,49 @@@ C End diagnostic
            dx_normj=dc_norm(1,j)
            dy_normj=dc_norm(2,j)
            dz_normj=dc_norm(3,j)
 -          xj=c(1,j)+0.5D0*dxj-xmedi
 -          yj=c(2,j)+0.5D0*dyj-ymedi
 -          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
 +      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
            rij=xj*xj+yj*yj+zj*zj
 +            sss=sscale(sqrt(rij))
 +            sssgrad=sscagrad(sqrt(rij))
            rrmij=1.0D0/rij
            rij=dsqrt(rij)
            rmij=1.0D0/rij
@@@ -2035,12 -1951,12 +2072,12 @@@ c          write (iout,*) "i",i,iteli,
  C 12/26/95 - for the evaluation of multi-body H-bonding interactions
            ees0ij=4.0D0+fac*fac-3.0D0*(cosb*cosb+cosg*cosg)
            ees=ees+eesij
 -          evdw1=evdw1+evdwij
 +          evdw1=evdw1+evdwij*sss
  c             write (iout,'(a6,2i5,0pf7.3,2i5,2e11.3)') 
  c     &'evdw1',i,j,evdwij
  c     &,iteli,itelj,aaa,evdw1
  
 -c              write (iout,'(a6,2i5,0pf7.3)') 'ees',i,j,eesij
 +C              write (iout,'(a6,2i5,0pf7.3)') 'ees',i,j,eesij
  c          write(iout,'(2(2i3,2x),7(1pd12.4)/2(3(1pd12.4),5x)/)')
  c     &      iteli,i,itelj,j,aaa,bbb,ael6i,ael3i,
  c     &      1.0D0/dsqrt(rrmij),evdwij,eesij,
  C Calculate contributions to the Cartesian gradient.
  C
  #ifdef SPLITELE
 -          facvdw=-6*rrmij*(ev1+evdwij) 
 +          facvdw=-6*rrmij*(ev1+evdwij)*sss
            facel=-3*rrmij*(el1+eesij)
            fac1=fac
            erij(1)=xj*rmij
                gelc(l,k)=gelc(l,k)+ggg(l)
              enddo
            enddo
 -          ggg(1)=facvdw*xj
 -          ggg(2)=facvdw*yj
 -          ggg(3)=facvdw*zj
 +C          ggg(1)=facvdw*xj
 +C          ggg(2)=facvdw*yj
 +C          ggg(3)=facvdw*zj
 +          if (sss.gt.0.0) then
 +          ggg(1)=facvdw*xj+sssgrad*rmij*evdwij*xj
 +          ggg(2)=facvdw*yj+sssgrad*rmij*evdwij*yj
 +          ggg(3)=facvdw*zj+sssgrad*rmij*evdwij*zj
 +          else
 +          ggg(1)=0.0
 +          ggg(2)=0.0
 +          ggg(3)=0.0
 +          endif
            do k=1,3
              ghalf=0.5D0*ggg(k)
              gvdwpp(k,i)=gvdwpp(k,i)+ghalf
              enddo
            enddo
  #else
 -          facvdw=ev1+evdwij 
 +          facvdw=(ev1+evdwij)*sss
            facel=el1+eesij  
            fac1=fac
            fac=-3*rrmij*(facvdw+facvdw+facel)
@@@ -2947,13 -2854,7 +2984,13 @@@ c     &   " iscp",(iscpstart(i,j),iscpe
          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))
 -
 +C Returning the ith atom to box
 +          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
          do iint=1,nscp_gr(i)
  
          do j=iscpstart(i,iint),iscpend(i,iint)
@@@ -2964,73 -2865,28 +3001,73 @@@ c         xj=c(1,nres+j)-x
  c         yj=c(2,nres+j)-yi
  c         zj=c(3,nres+j)-zi
  C Uncomment following three lines for Ca-p interactions
 -          xj=c(1,j)-xi
 -          yj=c(2,j)-yi
 -          zj=c(3,j)-zi
 +          xj=c(1,j)
 +          yj=c(2,j)
 +          zj=c(3,j)
 +C returning the jth atom to box
 +          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
 +C Finding the closest jth atom
 +      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
            rrij=1.0D0/(xj*xj+yj*yj+zj*zj)
 +C sss is scaling function for smoothing the cutoff gradient otherwise
 +C the gradient would not be continuouse
 +          sss=sscale(1.0d0/(dsqrt(rrij)))
 +          if (sss.le.0.0d0) cycle
 +          sssgrad=sscagrad(1.0d0/(dsqrt(rrij)))
            fac=rrij**expon2
            e1=fac*fac*aad(itypj,iteli)
            e2=fac*bad(itypj,iteli)
            if (iabs(j-i) .le. 2) then
              e1=scal14*e1
              e2=scal14*e2
 -            evdw2_14=evdw2_14+e1+e2
 +            evdw2_14=evdw2_14+(e1+e2)*sss
            endif
            evdwij=e1+e2
  c          write (iout,'(a6,2i5,0pf7.3,2i3,3e11.3)')
  c     &        'evdw2',i,j,evdwij,iteli,itypj,fac,aad(itypj,iteli),
  c     &       bad(itypj,iteli)
 -          evdw2=evdw2+evdwij
 +          evdw2=evdw2+evdwij*sss
            if (calc_grad) then
  C
  C Calculate contributions to the gradient in the virtual-bond and SC vectors.
  C
 -          fac=-(evdwij+e1)*rrij
 +          fac=-(evdwij+e1)*rrij*sss
 +          fac=fac+(evdwij)*sssgrad*dsqrt(rrij)/expon
            ggg(1)=xj*fac
            ggg(2)=yj*fac
            ggg(3)=zj*fac
        include 'COMMON.DERIV'
        include 'COMMON.VAR'
        include 'COMMON.INTERACT'
+       include 'COMMON.CONTROL'
+       include 'COMMON.IOUNITS'
        dimension ggg(3)
        ehpb=0.0D0
  cd    print *,'edis: nhpb=',nhpb,' fbr=',fbr
  cd    print *,'link_start=',link_start,' link_end=',link_end
+ C      write(iout,*) link_end, "link_end"
        if (link_end.eq.0) return
        do i=link_start,link_end
  C If ihpb(i) and jhpb(i) > NRES, this is a SC-SC distance, otherwise a
@@@ -3115,25 -2974,98 +3155,98 @@@ C iii and jjj point to the residues fo
          endif
  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. 
+ C        if (ii.gt.nres .and. iabs(itype(iii)).eq.1 .and. 
+ C     & iabs(itype(jjj)).eq.1) then
+ C       write(iout,*) constr_dist,"const"
+        if (.not.dyn_ss .and. i.le.nss) then
+          if (ii.gt.nres .and. iabs(itype(iii)).eq.1 .and.
       & iabs(itype(jjj)).eq.1) then
            call ssbond_ene(iii,jjj,eij)
            ehpb=ehpb+2*eij
-         else
- C Calculate the distance between the two points and its difference from the
- C target distance.
-         dd=dist(ii,jj)
-         rdis=dd-dhpb(i)
+            endif !ii.gt.neres
+         else if (ii.gt.nres .and. jj.gt.nres) then
+ c Restraints from contact prediction
+           dd=dist(ii,jj)
+           if (constr_dist.eq.11) then
+ C            ehpb=ehpb+fordepth(i)**4.0d0
+ C     &          *rlornmr1(dd,dhpb(i),dhpb1(i),forcon(i))
+             ehpb=ehpb+fordepth(i)**4.0d0
+      &          *rlornmr1(dd,dhpb(i),dhpb1(i),forcon(i))
+             fac=fordepth(i)**4.0d0
+      &          *rlornmr1prim(dd,dhpb(i),dhpb1(i),forcon(i))/dd
+ C          write (iout,'(a6,2i5,3f8.3)') "edisl",ii,jj,
+ C     &    ehpb,fordepth(i),dd
+ C            write(iout,*) ehpb,"atu?"
+ C            ehpb,"tu?"
+ C            fac=fordepth(i)**4.0d0
+ C     &          *rlornmr1prim(dd,dhpb(i),dhpb1(i),forcon(i))/dd
+            else
+           if (dhpb1(i).gt.0.0d0) then
+             ehpb=ehpb+2*forcon(i)*gnmr1(dd,dhpb(i),dhpb1(i))
+             fac=forcon(i)*gnmr1prim(dd,dhpb(i),dhpb1(i))/dd
+ c            write (iout,*) "beta nmr",
+ c     &        dd,2*forcon(i)*gnmr1(dd,dhpb(i),dhpb1(i))
+           else
+             dd=dist(ii,jj)
+             rdis=dd-dhpb(i)
  C Get the force constant corresponding to this distance.
-         waga=forcon(i)
+             waga=forcon(i)
  C Calculate the contribution to energy.
-         ehpb=ehpb+waga*rdis*rdis
+             ehpb=ehpb+waga*rdis*rdis
+ c            write (iout,*) "beta reg",dd,waga*rdis*rdis
  C
  C Evaluate gradient.
  C
-         fac=waga*rdis/dd
- cd      print *,'i=',i,' ii=',ii,' jj=',jj,' dhpb=',dhpb(i),' dd=',dd,
- cd   &   ' waga=',waga,' fac=',fac
+             fac=waga*rdis/dd
+           endif !end dhpb1(i).gt.0
+           endif !end const_dist=11
+           do j=1,3
+             ggg(j)=fac*(c(j,jj)-c(j,ii))
+           enddo
+           do j=1,3
+             ghpbx(j,iii)=ghpbx(j,iii)-ggg(j)
+             ghpbx(j,jjj)=ghpbx(j,jjj)+ggg(j)
+           enddo
+           do k=1,3
+             ghpbc(k,jjj)=ghpbc(k,jjj)+ggg(k)
+             ghpbc(k,iii)=ghpbc(k,iii)-ggg(k)
+           enddo
+         else !ii.gt.nres
+ C          write(iout,*) "before"
+           dd=dist(ii,jj)
+ C          write(iout,*) "after",dd
+           if (constr_dist.eq.11) then
+             ehpb=ehpb+fordepth(i)**4.0d0
+      &          *rlornmr1(dd,dhpb(i),dhpb1(i),forcon(i))
+             fac=fordepth(i)**4.0d0
+      &          *rlornmr1prim(dd,dhpb(i),dhpb1(i),forcon(i))/dd
+ C            ehpb=ehpb+fordepth(i)**4*rlornmr1(dd,dhpb(i),dhpb1(i))
+ C            fac=fordepth(i)**4*rlornmr1prim(dd,dhpb(i),dhpb1(i))/dd
+ C            print *,ehpb,"tu?"
+ C            write(iout,*) ehpb,"btu?",
+ C     & dd,dhpb(i),dhpb1(i),fordepth(i),forcon(i)
+ C          write (iout,'(a6,2i5,3f8.3)') "edisl",ii,jj,
+ C     &    ehpb,fordepth(i),dd
+            else   
+           if (dhpb1(i).gt.0.0d0) then
+             ehpb=ehpb+2*forcon(i)*gnmr1(dd,dhpb(i),dhpb1(i))
+             fac=forcon(i)*gnmr1prim(dd,dhpb(i),dhpb1(i))/dd
+ c            write (iout,*) "alph nmr",
+ c     &        dd,2*forcon(i)*gnmr1(dd,dhpb(i),dhpb1(i))
+           else
+             rdis=dd-dhpb(i)
+ C Get the force constant corresponding to this distance.
+             waga=forcon(i)
+ C Calculate the contribution to energy.
+             ehpb=ehpb+waga*rdis*rdis
+ c            write (iout,*) "alpha reg",dd,waga*rdis*rdis
+ C
+ C Evaluate gradient.
+ C
+             fac=waga*rdis/dd
+           endif
+           endif
          do j=1,3
            ggg(j)=fac*(c(j,jj)-c(j,ii))
          enddo
@@@ -3153,7 -3085,7 +3266,7 @@@ C Cartesian gradient in the SC vectors 
          enddo
          endif
        enddo
-       ehpb=0.5D0*ehpb
+       if (constr_dist.ne.11) ehpb=0.5D0*ehpb
        return
        end
  C--------------------------------------------------------------------------
        estr1=0.0d0
  c      write (iout,*) "distchainmax",distchainmax
        do i=nnt+1,nct
 -        if (itype(i-1).eq.ntyp1 .or. itype(i).eq.ntyp1) then
 -          estr1=estr1+gnmr1(vbld(i),-1.0d0,distchainmax)
 -          do j=1,3
 -          gradb(j,i-1)=gnmr1prim(vbld(i),-1.0d0,distchainmax)
 -     &      *dc(j,i-1)/vbld(i)
 -          enddo
 -          if (energy_dec) write(iout,*)
 -     &       "estr1",i,vbld(i),distchainmax,
 -     &       gnmr1(vbld(i),-1.0d0,distchainmax)
 -        else
 +        if (itype(i-1).eq.ntyp1 .and. itype(i).eq.ntyp1) cycle
 +C          estr1=estr1+gnmr1(vbld(i),-1.0d0,distchainmax)
 +C          do j=1,3
 +C          gradb(j,i-1)=gnmr1prim(vbld(i),-1.0d0,distchainmax)
 +C     &      *dc(j,i-1)/vbld(i)
 +C          enddo
 +C          if (energy_dec) write(iout,*)
 +C     &       "estr1",i,vbld(i),distchainmax,
 +C     &       gnmr1(vbld(i),-1.0d0,distchainmax)
 +C        else
 +         if (itype(i-1).eq.ntyp1 .or. itype(i).eq.ntyp1) then
 +        diff = vbld(i)-vbldpDUM
 +         else
            diff = vbld(i)-vbldp0
  c          write (iout,*) i,vbld(i),vbldp0,diff,AKP*diff*diff
 +         endif
            estr=estr+diff*diff
            do j=1,3
              gradb(j,i-1)=AKP*diff*dc(j,i-1)/vbld(i)
            enddo
 -        endif
 -
 +C        endif
 +C        write (iout,'(a7,i5,4f7.3)')
 +C     &     "estr bb",i,vbld(i),vbldp0,diff,AKP*diff*diff
        enddo
        estr=0.5d0*AKP*estr+estr1
  c
            nbi=nbondterm(iti)
            if (nbi.eq.1) then
              diff=vbld(i+nres)-vbldsc0(1,iti)
 -c            write (iout,*) i,iti,vbld(i+nres),vbldsc0(1,iti),diff,
 -c     &      AKSC(1,iti),AKSC(1,iti)*diff*diff
 +C            write (iout,*) i,iti,vbld(i+nres),vbldsc0(1,iti),diff,
 +C     &      AKSC(1,iti),AKSC(1,iti)*diff*diff
              estr=estr+0.5d0*AKSC(1,iti)*diff*diff
              do j=1,3
                gradbx(j,i)=AKSC(1,iti)*diff*dc(j,i+nres)/vbld(i+nres)
@@@ -3342,7 -3269,7 +3455,7 @@@ c     &      AKSC(j,iti),abond0(j,iti),
        end
  #ifdef CRYST_THETA
  C--------------------------------------------------------------------------
-       subroutine ebend(etheta)
+       subroutine ebend(etheta,ethetacnstr)
  C
  C Evaluate the virtual-bond-angle energy given the virtual-bond dihedral
  C angles gamma and its derivatives in consecutive thetas and gammas.
        include 'COMMON.IOUNITS'
        include 'COMMON.NAMES'
        include 'COMMON.FFIELD'
+       include 'COMMON.TORCNSTR'
        common /calcthet/ term1,term2,termm,diffak,ratak,
       & ak,aktc,termpre,termexp,sigc,sig0i,time11,time12,sigcsq,
       & delthe0,sig0inv,sigtc,sigsqtc,delthec,it
        double precision y(2),z(2)
        delta=0.02d0*pi
-       time11=dexp(-2*time)
-       time12=1.0d0
+ c      time11=dexp(-2*time)
+ c      time12=1.0d0
        etheta=0.0D0
  c      write (iout,*) "nres",nres
  c     write (*,'(a,i2)') 'EBEND ICG=',icg
  c      write (iout,*) ithet_start,ithet_end
        do i=ithet_start,ithet_end
 -        if (itype(i-1).eq.ntyp1) cycle
 +C        if (itype(i-1).eq.ntyp1) cycle
 +        if (i.le.2) cycle
 +        if ((itype(i-1).eq.ntyp1).or.itype(i-2).eq.ntyp1
 +     &  .or.itype(i).eq.ntyp1) cycle
  C Zero the energy function and its derivative at 0 or pi.
          call splinthet(theta(i),0.5d0*delta,ss,ssd)
          it=itype(i-1)
            ichir21=isign(1,itype(i))
            ichir22=isign(1,itype(i))
           endif
 +         if (i.eq.3) then
 +          y(1)=0.0D0
 +          y(2)=0.0D0
 +          else
  
 -        if (i.gt.3 .and. itype(i-2).ne.ntyp1) then
 +        if (i.gt.3 .and. itype(i-3).ne.ntyp1) then
  #ifdef OSF
            phii=phi(i)
-           icrc=0
-           call proc_proc(phii,icrc)
+ c          icrc=0
+ c          call proc_proc(phii,icrc)
            if (icrc.eq.1) phii=150.0
  #else
            phii=phi(i)
            y(1)=0.0D0
            y(2)=0.0D0
          endif
 -        if (i.lt.nres .and. itype(i).ne.ntyp1) then
 +        endif
 +        if (i.lt.nres .and. itype(i+1).ne.ntyp1) then
  #ifdef OSF
            phii1=phi(i+1)
-           icrc=0
-           call proc_proc(phii1,icrc)
+ c          icrc=0
+ c          call proc_proc(phii1,icrc)
            if (icrc.eq.1) phii1=150.0
            phii1=pinorm(phii1)
            z(1)=cos(phii1)
@@@ -3478,14 -3398,39 +3592,41 @@@ C Derivatives of the "mean" values in g
       &        E_theta,E_tc)
          endif
          etheta=etheta+ethetai
 +c         write (iout,'(a6,i5,0pf7.3,f7.3,i5)')
 +c     &      'ebend',i,ethetai,theta(i),itype(i)
  c        write (iout,'(2i3,3f8.3,f10.5)') i,it,rad2deg*theta(i),
  c     &    rad2deg*phii,rad2deg*phii1,ethetai
          if (i.gt.3) gloc(i-3,icg)=gloc(i-3,icg)+wang*E_tc*dthetg1
          if (i.lt.nres) gloc(i-2,icg)=gloc(i-2,icg)+wang*E_tc*dthetg2
          gloc(nphi+i-2,icg)=wang*(E_theta+E_tc*dthett)
-  1215   continue
+ c 1215   continue
+       enddo
+       ethetacnstr=0.0d0
+ C      print *,ithetaconstr_start,ithetaconstr_end,"TU"
+       do i=1,ntheta_constr
+         itheta=itheta_constr(i)
+         thetiii=theta(itheta)
+         difi=pinorm(thetiii-theta_constr0(i))
+         if (difi.gt.theta_drange(i)) then
+           difi=difi-theta_drange(i)
+           ethetacnstr=ethetacnstr+0.25d0*for_thet_constr(i)*difi**4
+           gloc(itheta+nphi-2,icg)=gloc(itheta+nphi-2,icg)
+      &    +for_thet_constr(i)*difi**3
+         else if (difi.lt.-drange(i)) then
+           difi=difi+drange(i)
+           ethetacnstr=ethetacnstr+0.25d0*for_thet_constr(i)*difi**4
+           gloc(itheta+nphi-2,icg)=gloc(itheta+nphi-2,icg)
+      &    +for_thet_constr(i)*difi**3
+         else
+           difi=0.0
+         endif
+ C       if (energy_dec) then
+ C        write (iout,'(a6,2i5,4f8.3,2e14.5)') "ethetc",
+ C     &    i,itheta,rad2deg*thetiii,
+ C     &    rad2deg*theta_constr0(i),  rad2deg*theta_drange(i),
+ C     &    rad2deg*difi,0.25d0*for_thet_constr(i)*difi**4,
+ C     &    gloc(itheta+nphi-2,icg)
+         endif
        enddo
  C Ufff.... We've done all this!!! 
        return
@@@ -3600,7 -3545,7 +3741,7 @@@ C "Thank you" to MAPLE (probably spare
        end
  #else
  C--------------------------------------------------------------------------
-       subroutine ebend(etheta)
+       subroutine ebend(etheta,ethetacnstr)
  C
  C Evaluate the virtual-bond-angle energy given the virtual-bond dihedral
  C angles gamma and its derivatives in consecutive thetas and gammas.
        include 'COMMON.NAMES'
        include 'COMMON.FFIELD'
        include 'COMMON.CONTROL'
+       include 'COMMON.TORCNSTR'
        double precision coskt(mmaxtheterm),sinkt(mmaxtheterm),
       & cosph1(maxsingle),sinph1(maxsingle),cosph2(maxsingle),
       & sinph2(maxsingle),cosph1ph2(maxdouble,maxdouble),
        etheta=0.0D0
  c      write (iout,*) "ithetyp",(ithetyp(i),i=1,ntyp1)
        do i=ithet_start,ithet_end
 -c        if (itype(i-1).eq.ntyp1) cycle
 -        if ((itype(i-1).eq.ntyp1).or.(itype(i-2).eq.ntyp1).or.
 -     &(itype(i).eq.ntyp1)) cycle
 +C         if (i.eq.2) cycle
 +C        if (itype(i-1).eq.ntyp1) cycle
 +        if (i.le.2) cycle
 +        if ((itype(i-1).eq.ntyp1).or.itype(i-2).eq.ntyp1
 +     &  .or.itype(i).eq.ntyp1) cycle
          if (iabs(itype(i+1)).eq.20) iblock=2
          if (iabs(itype(i+1)).ne.20) iblock=1
          dethetai=0.0d0
            coskt(k)=dcos(k*theti2)
            sinkt(k)=dsin(k*theti2)
          enddo
 +        if (i.eq.3) then 
 +          phii=0.0d0
 +          ityp1=nthetyp+1
 +          do k=1,nsingle
 +            cosph1(k)=0.0d0
 +            sinph1(k)=0.0d0
 +          enddo
 +        else
          if (i.gt.3 .and. itype(i-3).ne.ntyp1) then
  #ifdef OSF
            phii=phi(i)
            enddo
          else
            phii=0.0d0
-           ityp1=nthetyp+1
+ c          ityp1=nthetyp+1
            do k=1,nsingle
+             ityp1=ithetyp((itype(i-2)))
              cosph1(k)=0.0d0
              sinph1(k)=0.0d0
            enddo 
          endif
 +        endif
          if (i.lt.nres .and. itype(i+1).ne.ntyp1) then
  #ifdef OSF
            phii1=phi(i+1)
            enddo
          else
            phii1=0.0d0
-           ityp3=nthetyp+1
+ c          ityp3=nthetyp+1
+           ityp3=ithetyp((itype(i)))
            do k=1,nsingle
              cosph2(k)=0.0d0
              sinph2(k)=0.0d0
@@@ -3805,7 -3742,36 +3949,36 @@@ c        call flush(iout
          etheta=etheta+ethetai
          if (i.gt.3) gloc(i-3,icg)=gloc(i-3,icg)+wang*dephii
          if (i.lt.nres) gloc(i-2,icg)=gloc(i-2,icg)+wang*dephii1
-         gloc(nphi+i-2,icg)=wang*dethetai
+ c        gloc(nphi+i-2,icg)=wang*dethetai
+         gloc(nphi+i-2,icg)=gloc(nphi+i-2,icg)+wang*dethetai
+       enddo
+ C now constrains
+       ethetacnstr=0.0d0
+ C      print *,ithetaconstr_start,ithetaconstr_end,"TU"
+       do i=1,ntheta_constr
+         itheta=itheta_constr(i)
+         thetiii=theta(itheta)
+         difi=pinorm(thetiii-theta_constr0(i))
+         if (difi.gt.theta_drange(i)) then
+           difi=difi-theta_drange(i)
+           ethetacnstr=ethetacnstr+0.25d0*for_thet_constr(i)*difi**4
+           gloc(itheta+nphi-2,icg)=gloc(itheta+nphi-2,icg)
+      &    +for_thet_constr(i)*difi**3
+         else if (difi.lt.-drange(i)) then
+           difi=difi+drange(i)
+           ethetacnstr=ethetacnstr+0.25d0*for_thet_constr(i)*difi**4
+           gloc(itheta+nphi-2,icg)=gloc(itheta+nphi-2,icg)
+      &    +for_thet_constr(i)*difi**3
+         else
+           difi=0.0
+         endif
+ C       if (energy_dec) then
+ C        write (iout,'(a6,2i5,4f8.3,2e14.5)') "ethetc",
+ C     &    i,itheta,rad2deg*thetiii,
+ C     &    rad2deg*theta_constr0(i),  rad2deg*theta_drange(i),
+ C     &    rad2deg*difi,0.25d0*for_thet_constr(i)*difi**4,
+ C     &    gloc(itheta+nphi-2,icg)
+ C        endif
        enddo
        return
        end
@@@ -3833,14 -3799,14 +4006,14 @@@ C ALPHA and OMEGA
        common /sccalc/ time11,time12,time112,theti,it,nlobit
        delta=0.02d0*pi
        escloc=0.0D0
 -c     write (iout,'(a)') 'ESC'
 +C      write (iout,*) 'ESC'
        do i=loc_start,loc_end
          it=itype(i)
          if (it.eq.ntyp1) cycle
          if (it.eq.10) goto 1
          nlobit=nlob(iabs(it))
  c       print *,'i=',i,' it=',it,' nlobit=',nlobit
 -c       write (iout,*) 'i=',i,' ssa=',ssa,' ssad=',ssad
 +C        write (iout,*) 'i=',i,' ssa=',ssa,' ssad=',ssad
          theti=theta(i+1)-pipol
          x(1)=dtan(theti)
          x(2)=alph(i)
@@@ -3876,8 -3842,8 +4049,8 @@@ c        write (iout,*) "i",i," x",x(1)
              dersc(k)=ss*dersc(k)+(1.0d0-ss)*dersc0(k)
            enddo
            dersc(2)=dersc(2)+ssd*(escloci-esclocbi)
 -c         write (iout,*) 'i=',i,x(2)*rad2deg,escloci0,escloci,
 -c    &             esclocbi,ss,ssd
 +          write (iout,*) 'i=',i,x(2)*rad2deg,escloci0,escloci,
 +     &             esclocbi,ss,ssd
            escloci=ss*escloci+(1.0d0-ss)*esclocbi
  c         escloci=esclocbi
  c         write (iout,*) escloci
            enddo
            dersc(2)=dersc(2)+ssd*(escloci-esclocbi)
  c         write (iout,*) 'i=',i,x(2)*rad2deg,escloci0,escloci,
 -c    &             esclocbi,ss,ssd
 +c     &             esclocbi,ss,ssd
            escloci=ss*escloci+(1.0d0-ss)*esclocbi
 -c         write (iout,*) escloci
 +C         write (iout,*) 'i=',i, escloci
          else
            call enesc(x,escloci,dersc,ddummy,.false.)
          endif
  
          escloc=escloc+escloci
 -c        write (iout,*) 'i=',i,' escloci=',escloci,' dersc=',dersc
 +C        write (iout,*) 'i=',i,' escloci=',escloci,' dersc=',dersc
 +            write (iout,'(a6,i5,0pf7.3)')
 +     &     'escloc',i,escloci
  
          gloc(nphi+i-1,icg)=gloc(nphi+i-1,icg)+
       &   wscloc*dersc(1)
@@@ -4575,15 -4539,16 +4748,16 @@@ c       write (iout,*) 'i=',i,' gloc=',
          difi=phii-phi0(i)
          if (difi.gt.drange(i)) then
            difi=difi-drange(i)
-           edihcnstr=edihcnstr+0.25d0*ftors*difi**4
-           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors*difi**3
+           edihcnstr=edihcnstr+0.25d0*ftors(i)*difi**4
+           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors(i)*difi**3
          else if (difi.lt.-drange(i)) then
            difi=difi+drange(i)
-           edihcnstr=edihcnstr+0.25d0*ftors*difi**4
-           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors*difi**3
+           edihcnstr=edihcnstr+0.25d0*ftors(i)*difi**4
+           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors(i)*difi**3
          endif
- !        write (iout,'(2i5,2f8.3,2e14.5)') i,itori,rad2deg*phii,
- !     &    rad2deg*difi,0.25d0*ftors*difi**4,gloc(itori-3,icg)
+ C        write (iout,'(a6,2i5,2f8.3,2e14.5)') "edih",
+ C     &    i,itori,rad2deg*phii,
+ C     &    rad2deg*difi,0.25d0*ftors(i)*difi**4,gloc(itori-3,icg)
        enddo
  !      write (iout,*) 'edihcnstr',edihcnstr
        return
@@@ -4611,11 -4576,8 +4785,11 @@@ C Set lprn=.true. for debuggin
  c      lprn=.true.
        etors=0.0D0
        do i=iphi_start,iphi_end
 -        if (itype(i-2).eq.ntyp1 .or. itype(i-1).eq.ntyp1
 -     &       .or. itype(i).eq.ntyp1) cycle
 +        if (i.le.2) cycle
 +        if (itype(i-2).eq.ntyp1.or. itype(i-1).eq.ntyp1
 +     &      .or. itype(i).eq.ntyp1 .or. itype(i-3).eq.ntyp1) cycle
 +C        if (itype(i-2).eq.ntyp1 .or. itype(i-1).eq.ntyp1
 +C     &       .or. itype(i).eq.ntyp1) cycle
          if (itel(i-2).eq.0 .or. itel(i-1).eq.0) goto 1215
           if (iabs(itype(i)).eq.20) then
           iblock=2
@@@ -4673,21 -4635,24 +4847,24 @@@ c       write (iout,*) 'i=',i,' gloc=',
          edihi=0.0d0
          if (difi.gt.drange(i)) then
            difi=difi-drange(i)
-           edihcnstr=edihcnstr+0.25d0*ftors*difi**4
-           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors*difi**3
-           edihi=0.25d0*ftors*difi**4
+           edihcnstr=edihcnstr+0.25d0*ftors(i)*difi**4
+           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors(i)*difi**3
+           edihi=0.25d0*ftors(i)*difi**4
          else if (difi.lt.-drange(i)) then
            difi=difi+drange(i)
-           edihcnstr=edihcnstr+0.25d0*ftors*difi**4
-           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors*difi**3
-           edihi=0.25d0*ftors*difi**4
+           edihcnstr=edihcnstr+0.25d0*ftors(i)*difi**4
+           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors(i)*difi**3
+           edihi=0.25d0*ftors(i)*difi**4
          else
            difi=0.0d0
          endif
+         write (iout,'(a6,2i5,2f8.3,2e14.5)') "edih",
+      &    i,itori,rad2deg*phii,
+      &    rad2deg*difi,0.25d0*ftors(i)*difi**4
  c        write (iout,'(2i5,4f10.5,e15.5)') i,itori,phii,phi0(i),difi,
  c     &    drange(i),edihi
  !        write (iout,'(2i5,2f8.3,2e14.5)') i,itori,rad2deg*phii,
- !     &    rad2deg*difi,0.25d0*ftors*difi**4,gloc(itori-3,icg)
+ !     &    rad2deg*difi,0.25d0*ftors(i)*difi**4,gloc(itori-3,icg)
        enddo
  !      write (iout,*) 'edihcnstr',edihcnstr
        return
@@@ -4715,12 -4680,8 +4892,12 @@@ C Set lprn=.true. for debuggin
  c     lprn=.true.
        etors_d=0.0D0
        do i=iphi_start,iphi_end-1
 -        if (itype(i-2).eq.ntyp1.or. itype(i-1).eq.ntyp1
 -     &      .or. itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1) cycle
 +        if (i.le.3) cycle
 +C        if (itype(i-2).eq.ntyp1.or. itype(i-1).eq.ntyp1
 +C     &      .or. itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1) cycle
 +         if ((itype(i-2).eq.ntyp1).or.itype(i-3).eq.ntyp1.or.
 +     &  (itype(i-1).eq.ntyp1).or.(itype(i).eq.ntyp1).or.
 +     &  (itype(i+1).eq.ntyp1)) cycle
          if (itel(i-2).eq.0 .or. itel(i-1).eq.0 .or. itel(i).eq.0) 
       &     goto 1215
          itori=itortyp(itype(i-2))
@@@ -4834,6 -4795,7 +5011,7 @@@ c   3 = SC...Ca...Ca...SC
             esccor=esccor+v1ij*cosphi+v2ij*sinphi
             gloci=gloci+j*(v2ij*cosphi-v1ij*sinphi)
           enddo
+ C      write (iout,*)"EBACK_SC_COR",esccor,i
  c      write (iout,*) "EBACK_SC_COR",i,v1ij*cosphi+v2ij*sinphi,intertyp,
  c     & nterm_sccor(isccori,isccori1),isccori,isccori1
  c        gloc_sc(intertyp,i-3,icg)=gloc_sc(intertyp,i-3,icg)+wsccor*gloci
@@@ -7729,34 -7691,4 +7907,34 @@@ C--------------------------------------
        scalar=sc
        return
        end
 +C-----------------------------------------------------------------------
 +      double precision function sscale(r)
 +      double precision r,gamm
 +      include "COMMON.SPLITELE"
 +      if(r.lt.r_cut-rlamb) then
 +        sscale=1.0d0
 +      else if(r.le.r_cut.and.r.ge.r_cut-rlamb) then
 +        gamm=(r-(r_cut-rlamb))/rlamb
 +        sscale=1.0d0+gamm*gamm*(2*gamm-3.0d0)
 +      else
 +        sscale=0d0
 +      endif
 +      return
 +      end
 +C-----------------------------------------------------------------------
 +C-----------------------------------------------------------------------
 +      double precision function sscagrad(r)
 +      double precision r,gamm
 +      include "COMMON.SPLITELE"
 +      if(r.lt.r_cut-rlamb) then
 +        sscagrad=0.0d0
 +      else if(r.le.r_cut.and.r.ge.r_cut-rlamb) then
 +        gamm=(r-(r_cut-rlamb))/rlamb
 +        sscagrad=gamm*(6*gamm-6.0d0)/rlamb
 +      else
 +        sscagrad=0.0d0
 +      endif
 +      return
 +      end
 +C-----------------------------------------------------------------------
  
@@@ -184,6 -184,7 +184,7 @@@ C Initialize the bridge array
        do i=1,maxres
        ihpb(i)=0
        jhpb(i)=0
+         dyn_ss_mask(i)=.false.
        enddo
  C
  C Initialize timing.
@@@ -261,17 -262,19 +262,19 @@@ c--------------------------------------
       &   "EVDW SC-SC","EVDW2 SC-p","EES p-p","ECORR4 ","ECORR5 ",
       &   "ECORR6 ","EELLO ","ETURN3 ","ETURN4 ","ETURN6 ",
       &   "EBE bend","ESC SCloc","ETORS ","ETORSD ","EHPB","EVDWPP",
-      &   "EVDW2_14","ESTR","ESCCOR","EDIHC","EVDW_T"/
+      &   "EVDW2_14","ESTR","ESCCOR","EDIHC","EVDW_T","ELIPTRAN",
+      &   "EAFM","ETHETC"/
        data wname /
       &   "WSC","WSCP","WELEC","WCORR","WCORR5","WCORR6","WEL_LOC",
       &   "WTURN3","WTURN4","WTURN6","WANG","WSCLOC","WTOR","WTORD",
-      &   "WHPB","WVDWPP","WSCP14","WBOND","WSCCOR","WDIHC","WSC"/
+      &   "WHPB","WVDWPP","WSCP14","WBOND","WSCCOR","WDIHC","WSC",
+      &   "WLIPTRAN","WAFM","WTHETC"/
        data ww0 /1.0d0,1.0d0,1.0d0,1.0d0,1.0d0,1.0d0,1.0d0,1.0d0,1.0d0,
       &    1.0d0,1.0d0,1.0d0,1.0d0,1.0d0,1.0d0,1.0d0,0.4d0,1.0d0,1.0d0,
-      &    0.0d0,0.0/
-       data nprint_ene /21/
+      &    0.0d0,0.0,0.0d0,0.0d0,0.0d0/
+       data nprint_ene /22/
        data print_order /1,2,3,18,11,12,13,14,4,5,6,7,8,9,10,19,
-      &  16,15,17,20,21/
+      &  16,15,17,20,21,24,22,23/
        end 
  c---------------------------------------------------------------------------
        subroutine init_int_table
@@@ -328,6 -331,7 +331,7 @@@ cd    write (iout,*) 'ns=',ns,' nss=',n
  cd   &   (ihpb(i),jhpb(i),i=1,nss)
        do i=nnt,nct-1
          scheck=.false.
+         if (dyn_ss) goto 10
          do ii=1,nss
            if (ihpb(ii).eq.i+nres) then
              scheck=.true.
@@@ -537,7 -541,7 +541,7 @@@ C Partition local interaction
  #else
        loc_start=2
        loc_end=nres-1
 -      ithet_start=3 
 +      ithet_start=3
        ithet_end=nres
        iphi_start=nnt+3
        iphi_end=nct
@@@ -35,6 -35,8 +35,8 @@@
        character*16 key
        integer iparm
        double precision ip,mp
+       character*6 res1
+ C      write (iout,*) "KURWA"
  C
  C Body
  C
@@@ -55,6 -57,66 +57,66 @@@ C Assign virtual-bond lengt
  
        write (iout,*) "iparm",iparm," myparm",myparm
  c If reading not own parameters, skip assignment
+       call reada(controlcard,"D0CM",d0cm,3.78d0)
+       call reada(controlcard,"AKCM",akcm,15.1d0)
+       call reada(controlcard,"AKTH",akth,11.0d0)
+       call reada(controlcard,"AKCT",akct,12.0d0)
+       call reada(controlcard,"V1SS",v1ss,-1.08d0)
+       call reada(controlcard,"V2SS",v2ss,7.61d0)
+       call reada(controlcard,"V3SS",v3ss,13.7d0)
+       call reada(controlcard,"EBR",ebr,-5.50D0)
+       call reada(controlcard,"DTRISS",dtriss,1.0D0)
+       call reada(controlcard,"ATRISS",atriss,0.3D0)
+       call reada(controlcard,"BTRISS",btriss,0.02D0)
+       call reada(controlcard,"CTRISS",ctriss,1.0D0)
+       dyn_ss=(index(controlcard,'DYN_SS').gt.0)
+       write(iout,*) "ATRISS",atriss
+       write(iout,*) "BTRISS",btriss
+       write(iout,*) "CTRISS",ctriss
+       write(iout,*) "DTRISS",dtriss
+ C      do i=1,maxres
+ C        dyn_ss_mask(i)=.false.
+ C      enddo
+ C      ebr=-12.0D0
+ c
+ c Old arbitrary potential - commented out.
+ c
+ c      dbr= 4.20D0
+ c      fbr= 3.30D0
+ c
+ c Constants of the disulfide-bond potential determined based on the RHF/6-31G**
+ c energy surface of diethyl disulfide.
+ c A. Liwo and U. Kozlowska, 11/24/03
+ c
+       D0CM = 3.78d0
+       AKCM = 15.1d0
+       AKTH = 11.0d0
+       AKCT = 12.0d0
+       V1SS =-1.08d0
+       V2SS = 7.61d0
+       V3SS = 13.7d0
+       do i=1,maxres-1
+         do j=i+1,maxres
+           dyn_ssbond_ij(i,j)=1.0d300
+         enddo
+       enddo
+       call reada(controlcard,"HT",Ht,0.0D0)
+ C      if (dyn_ss) then
+ C        ss_depth=ebr/wsc-0.25*eps(1,1)
+ C        write(iout,*) HT,wsc,eps(1,1),'KURWA'
+ C        Ht=Ht/wsc-0.25*eps(1,1)
+        
+ C        akcm=akcm*whpb/wsc
+ C        akth=akth*whpb/wsc
+ C        akct=akct*whpb/wsc
+ C        v1ss=v1ss*whpb/wsc
+ C        v2ss=v2ss*whpb/wsc
+ C        v3ss=v3ss*whpb/wsc
+ C      else
+ C        ss_depth=ebr/whpb-0.25*eps(1,1)*wsc/whpb
+ C      endif
  
        if (iparm.eq.myparm .or. .not.separate_parset) then
  
        wvdwpp=ww(16)
        wbond=ww(18)
        wsccor=ww(19)
+       whpb=ww(15)
+       wstrain=ww(15)
        endif
  
        call card_concat(controlcard,.false.)
@@@ -150,7 -213,7 +213,7 @@@ c Read the virtual-bond parameters, mas
  c and Stokes' radii of the peptide group and side chains
  c
  #ifdef CRYST_BOND
 -      read (ibond,*) vbldp0,akp
 +      read (ibond,*) vbldp0,vbldpdum,akp
        do i=1,ntyp
          nbondterm(i)=1
          read (ibond,*) vbldsc0(1,i),aksc(1,i)
          endif
        enddo
  #else
 -      read (ibond,*) ijunk,vbldp0,akp,rjunk
 +      read (ibond,*) ijunk,vbldp0,vbldpdum,akp,rjunk
        do i=1,ntyp
          read (ibond,*) nbondterm(i),(vbldsc0(j,i),aksc(j,i),abond0(j,i),
       &   j=1,nbondterm(i))
  C Read the parameters of Utheta determined from ab initio surfaces
  C Kozlowska et al., J. Phys.: Condens. Matter 19 (2007) 285203
  C
- c      write (iout,*) "tu dochodze"
+       write (iout,*) "tu dochodze"
        read (ithep,*) nthetyp,ntheterm,ntheterm2,
       &  ntheterm3,nsingle,ndouble
        nntheterm=max0(ntheterm,ntheterm2,ntheterm3)
        do i=-ntyp1,-1
          ithetyp(i)=-ithetyp(-i)
        enddo
- c      write (iout,*) "tu dochodze"
+       write (iout,*) "tu dochodze"
        do iblock=1,2
        do i=-maxthetyp,maxthetyp
          do j=-maxthetyp,maxthetyp
          enddo
        enddo
        enddo
+ C      write (iout,*) "KURWA1"
        do iblock=1,2
        do i=0,nthetyp
          do j=-nthetyp,nthetyp
            do k=-nthetyp,nthetyp
              read (ithep,'(6a)') res1
+             write(iout,*) res1,i,j,k
              read (ithep,*) aa0thet(i,j,k,iblock)
              read (ithep,*)(aathet(l,i,j,k,iblock),l=1,ntheterm)
              read (ithep,*)
            enddo
          enddo
        enddo
+ C       write(iout,*) "KURWA1.1"
  C
  C For dummy ends assign glycine-type coefficients of theta-only terms; the
  C coefficients of theta-and-gamma-dependent terms are zero.
          aa0thet(nthetyp+1,i,nthetyp+1,iblock)=0.0d0
        enddo
        enddo
+ C       write(iout,*) "KURWA1.5"
  C Substitution for D aminoacids from symmetry.
        do iblock=1,2
        do i=-nthetyp,0
        call flush(iout)
        endif
  #endif
+ C      write(iout,*) 'KURWA2'
  #ifdef CRYST_SC
  C
  C Read the parameters of the probability distribution/energy expression
        enddo
  #endif
        close(irotam)
+ C      write (iout,*) 'KURWAKURWA'
  #ifdef CRYST_TOR
  C
  C Read torsional parameters in old format
  C
  C Define the constants of the disulfide bridge
  C
-       ebr=-5.50D0
+ C      ebr=-12.0D0
  c
  c Old arbitrary potential - commented out.
  c
@@@ -1202,21 -1270,36 +1270,36 @@@ c Constants of the disulfide-bond poten
  c energy surface of diethyl disulfide.
  c A. Liwo and U. Kozlowska, 11/24/03
  c
-       D0CM = 3.78d0
-       AKCM = 15.1d0
-       AKTH = 11.0d0
-       AKCT = 12.0d0
-       V1SS =-1.08d0
-       V2SS = 7.61d0
-       V3SS = 13.7d0
+ C      D0CM = 3.78d0
+ C      AKCM = 15.1d0
+ C      AKTH = 11.0d0
+ C      AKCT = 12.0d0
+ C      V1SS =-1.08d0
+ C      V2SS = 7.61d0
+ C      V3SS = 13.7d0
+       write (iout,*) dyn_ss,'dyndyn'
+       if (dyn_ss) then
+         ss_depth=ebr/wsc-0.25*eps(1,1)
+ C        write(iout,*) akcm,whpb,wsc,'KURWA'
+         Ht=Ht/wsc-0.25*eps(1,1)
  
-       if (lprint) then
+         akcm=akcm*whpb/wsc
+         akth=akth*whpb/wsc
+         akct=akct*whpb/wsc
+         v1ss=v1ss*whpb/wsc
+         v2ss=v2ss*whpb/wsc
+         v3ss=v3ss*whpb/wsc
+       else
+         ss_depth=ebr/whpb-0.25*eps(1,1)*wsc/whpb
+       endif
+ C      if (lprint) then
        write (iout,'(/a)') "Disulfide bridge parameters:"
        write (iout,'(a,f10.2)') 'S-S bridge energy: ',ebr
        write (iout,'(2(a,f10.2))') 'd0cm:',d0cm,' akcm:',akcm
        write (iout,'(2(a,f10.2))') 'akth:',akth,' akct:',akct
        write (iout,'(3(a,f10.2))') 'v1ss:',v1ss,' v2ss:',v2ss,
       & ' v3ss:',v3ss
-       endif
+ C      endif
        return
        end
@@@ -17,7 -17,7 +17,8 @@@
        include "COMMON.FREE"
        include "COMMON.CONTROL"
        include "COMMON.ENERGIES"
 +      include "COMMON.SPLITELE"
+       include "COMMON.SBRIDGE"
        character*800 controlcard
        integer i,j,k,ii,n_ene_found
        integer ind,itype1,itype2,itypf,itypsc,itypp
        call readi(controlcard,"RESCALE",rescale_mode,1)
        check_conf=index(controlcard,"NO_CHECK_CONF").eq.0
        call reada(controlcard,'DISTCHAINMAX',distchainmax,50.0d0)
 +       call reada(controlcard,'BOXX',boxxsize,100.0d0)
 +       call reada(controlcard,'BOXY',boxysize,100.0d0)
 +       call reada(controlcard,'BOXZ',boxzsize,100.0d0)
 +c Cutoff range for interactions
 +      call reada(controlcard,"R_CUT",r_cut,15.0d0)
 +      call reada(controlcard,"LAMBDA",rlamb,0.3d0)
        call readi(controlcard,'SYM',symetr,1)
        write (iout,*) "DISTCHAINMAX",distchainmax
        write (iout,*) "delta",delta
        zscfile=index(controlcard,"ZSCFILE").gt.0
        with_dihed_constr = index(controlcard,"WITH_DIHED_CONSTR").gt.0
        write (iout,*) "with_dihed_constr ",with_dihed_constr
+       with_theta_constr = index(controlcard,"WITH_THETA_CONSTR").gt.0
+       write (iout,*) "with_theta_constr ",with_theta_constr
        call readi(controlcard,'CONSTR_DIST',constr_dist,0)
+       dyn_ss=index(controlcard,"DYN_SS").gt.0
        return
        end
  c------------------------------------------------------------------------------
@@@ -407,7 -404,7 +411,7 @@@ c--------------------------------------
        external ilen,iroof
        double precision rmsdev,energia(0:max_ene),efree,eini,temp
        double precision prop(maxQ)
-       integer ntot_all(maxslice,0:maxprocs-1)
+       integer ntot_all(maxslice,0:maxprocs-1), maxslice_buff
        integer iparm,ib,iib,ir,nprop,nthr,npars
        double precision etot,time
        integer ixdrf,iret 
@@@ -538,7 -535,13 +542,13 @@@ c DA scratchfile
  
  #ifdef MPI
  c Check if everyone has the same number of conformations
-       call MPI_Allgather(stot(1),maxslice,MPI_INTEGER,
+ c      call MPI_ALLgather(MPI_IN_PLACE,stot(1),MPI_DATATYPE_NULL,
+ c     &  ntot_all(1,0),maxslice,MPI_INTEGER,MPI_Comm_World,IERROR)
+       maxslice_buff=maxslice
+       call MPI_Allgather(stot(1),maxslice_buff,MPI_INTEGER,
       &  ntot_all(1,0),maxslice,MPI_INTEGER,MPI_Comm_World,IERROR)
        lerr=.false.
        do i=0,nprocs-1