pierwsza proba merga devela do adasko
authorAdam Sieradzan <adasko@piasek2.(none)>
Tue, 5 Nov 2013 13:37:15 +0000 (14:37 +0100)
committerAdam Sieradzan <adasko@piasek2.(none)>
Tue, 5 Nov 2013 13:37:15 +0000 (14:37 +0100)
Merge branch 'devel' into adasko

Conflicts:
.gitignore
PARAM/pot_theta_G631_DIL.parm
PARAM/pot_theta_G631_DIL_ext.parm
bin/unres/MD-M/unres_Tc_procor_oldparm_em64-D-symetr.exe
bin/unres_clustMD_MPI-oldparm
bin/wham/wham_multparm-ham_rep-oldparm
source/cluster/wham/src-M/CMakeLists.txt
source/cluster/wham/src-M/COMMON.SCCOR
source/cluster/wham/src-M/cartprint.o
source/cluster/wham/src-M/chainbuild.o
source/cluster/wham/src-M/contact.o
source/cluster/wham/src-M/convert.o
source/cluster/wham/src-M/energy_p_new.o
source/cluster/wham/src-M/geomout.o
source/cluster/wham/src-M/hc.o
source/cluster/wham/src-M/initialize_p.o
source/cluster/wham/src-M/int_from_cart1.o
source/cluster/wham/src-M/main_clust.o
source/cluster/wham/src-M/parmread.o
source/cluster/wham/src-M/permut.o
source/cluster/wham/src-M/proc_proc.o
source/cluster/wham/src-M/read_coords.o
source/cluster/wham/src-M/read_ref_str.o
source/cluster/wham/src-M/readpdb.o
source/cluster/wham/src-M/readrtns.o
source/cluster/wham/src-M/rescode.o
source/cluster/wham/src-M/setup_var.o
source/cluster/wham/src-M/wrtclust.o
source/cluster/wham/src-M/xdrf/libxdrf.a
source/cluster/wham/src/energy_p_new.F
source/unres/src_MD-M/COMMON.SCCOR
source/unres/src_MD-M/COMMON.TORSION
source/unres/src_MD-M/Makefile
source/unres/src_MD-M/cinfo.f
source/unres/src_MD-M/stochfric.F
source/unres/src_MD/COMMON.LOCAL
source/unres/src_MD/COMMON.SCCOR
source/unres/src_MD/Makefile
source/unres/src_MD/cinfo.f
source/unres/src_MD/parmread.F
source/wham/src-M/CMakeLists.txt
source/wham/src-M/cinfo.f
source/wham/src-M/include_unres/COMMON.SCCOR
source/wham/src/Makefile
source/wham/src/cinfo.f
source/wham/src/enecalc1.F
source/wham/src/energy_p_new.F
source/wham/src/include_unres/COMMON.SCCOR
source/wham/src/int_from_cart.f
source/wham/src/parmread.F

32 files changed:
1  2 
.gitignore
source/cluster/wham/src-M/energy_p_new.F
source/cluster/wham/src/energy_p_new.F
source/cluster/wham/src/initialize_p.F
source/cluster/wham/src/parmread.F
source/cluster/wham/src/read_coords.F
source/cluster/wham/src/readrtns.F
source/unres/src_MD-M/CMakeLists.txt
source/unres/src_MD-M/COMMON.CHAIN
source/unres/src_MD-M/MD_A-MTS.F
source/unres/src_MD-M/Makefile
source/unres/src_MD-M/energy_p_new_barrier.F
source/unres/src_MD-M/initialize_p.F
source/unres/src_MD-M/readpdb.F
source/unres/src_MD-M/stochfric.F
source/unres/src_MD/CMakeLists.txt
source/unres/src_MD/COMMON.SCCOR
source/unres/src_MD/energy_p_new_barrier.F
source/unres/src_MD/parmread.F
source/unres/src_MD/readpdb.F
source/unres/src_MD/readrtns.F
source/unres/src_MD/sc_move.F
source/unres/src_MD/stochfric.F
source/wham/src-M/CMakeLists.txt
source/wham/src-M/energy_p_new.F
source/wham/src/COMMON.ALLPARM
source/wham/src/enecalc1.F
source/wham/src/energy_p_new.F
source/wham/src/initialize_p.F
source/wham/src/molread_zs.F
source/wham/src/parmread.F
source/wham/src/store_parm.F

diff --combined .gitignore
@@@ -1,5 -1,6 +1,6 @@@
  # ignore compiled stuff
  *.[oa]
+ cinfo.f
  
  # ignore texteditors
  *.swp
  
  # ignore build dir
  build/
 +build2/
  
+ # latex files in documentation 
+ doc/*/*.aux
+ doc/*/*.log
  # ignored dirs form adasko
  gradcheck/
  mapcheck/
  run/
  sympcheck/
 -compinfo
 -DIL/
  bin/unres/MD/unres_ifort_MPICH_GAB_czyt.exe
 +bin/unres/MD-M/unres_Tc_procor_newparm_em64-D-symetr.exe
- DIL
++DIL/
 +compinfo
@@@ -363,9 -363,9 +363,9 @@@ cd    print *,'Entering ELJ nnt=',nnt,
        evdw=0.0D0
        evdw_t=0.0d0
        do i=iatsc_s,iatsc_e
 -        itypi=itype(i)
 -        if (itypi.eq.21) cycle
 -        itypi1=itype(i+1)
 +        itypi=iabs(itype(i))
 +        if (itypi.eq.ntyp1) cycle
 +        itypi1=iabs(itype(i+1))
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
  cd        write (iout,*) 'i=',i,' iint=',iint,' istart=',istart(i,iint),
  cd   &                  'iend=',iend(i,iint)
            do j=istart(i,iint),iend(i,iint)
 -            itypj=itype(j)
 -            if (itypj.eq.21) cycle
 +            itypj=iabs(itype(j))
 +            if (itypj.eq.ntyp1) cycle
              xj=c(1,nres+j)-xi
              yj=c(2,nres+j)-yi
              zj=c(3,nres+j)-zi
@@@ -528,9 -528,9 +528,9 @@@ c     print *,'Entering ELJK nnt=',nnt,
        evdw=0.0D0
        evdw_t=0.0d0
        do i=iatsc_s,iatsc_e
 -        itypi=itype(i)
 -        if (itypi.eq.21) cycle
 -        itypi1=itype(i+1)
 +        itypi=iabs(itype(i))
 +        if (itypi.eq.ntyp1) cycle
 +        itypi1=iabs(itype(i+1))
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
@@@ -539,8 -539,8 +539,8 @@@ C Calculate SC interaction energy
  C
          do iint=1,nint_gr(i)
            do j=istart(i,iint),iend(i,iint)
 -            itypj=itype(j)
 -            if (itypj.eq.21) cycle
 +            itypj=iabs(itype(j))
 +            if (itypj.eq.ntyp1) cycle
              xj=c(1,nres+j)-xi
              yj=c(2,nres+j)-yi
              zj=c(3,nres+j)-zi
@@@ -632,9 -632,9 +632,9 @@@ c     els
  c     endif
        ind=0
        do i=iatsc_s,iatsc_e
 -        itypi=itype(i)
 -        if (itypi.eq.21) cycle
 -        itypi1=itype(i+1)
 +        itypi=iabs(itype(i))
 +        if (itypi.eq.ntyp1) cycle
 +        itypi1=iabs(itype(i+1))
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
          do iint=1,nint_gr(i)
            do j=istart(i,iint),iend(i,iint)
              ind=ind+1
 -            itypj=itype(j)
 -            if (itypj.eq.21) cycle
 +            itypj=iabs(itype(j))
 +            if (itypj.eq.ntyp1) cycle
              dscj_inv=vbld_inv(j+nres)
              chi1=chi(itypi,itypj)
              chi2=chi(itypj,itypi)
@@@ -761,9 -761,9 +761,9 @@@ c     print *,'Entering EGB nnt=',nnt,
  c      if (icall.gt.0) lprn=.true.
        ind=0
        do i=iatsc_s,iatsc_e
 -        itypi=itype(i)
 -        if (itypi.eq.21) cycle
 -        itypi1=itype(i+1)
 +        itypi=iabs(itype(i))
 +        if (itypi.eq.ntyp1) cycle
 +        itypi1=iabs(itype(i+1))
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
          do iint=1,nint_gr(i)
            do j=istart(i,iint),iend(i,iint)
              ind=ind+1
 -            itypj=itype(j)
 -            if (itypj.eq.21) cycle
 +            itypj=iabs(itype(j))
 +            if (itypj.eq.ntyp1) cycle
              dscj_inv=vbld_inv(j+nres)
              sig0ij=sigma(itypi,itypj)
              chi1=chi(itypi,itypj)
@@@ -900,9 -900,9 +900,9 @@@ c     print *,'Entering EGB nnt=',nnt,
  c      if (icall.gt.0) lprn=.true.
        ind=0
        do i=iatsc_s,iatsc_e
 -        itypi=itype(i)
 -        if (itypi.eq.21) cycle
 -        itypi1=itype(i+1)
 +        itypi=iabs(itype(i))
 +        if (itypi.eq.ntyp1) cycle
 +        itypi1=iabs(itype(i+1))
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
          do iint=1,nint_gr(i)
            do j=istart(i,iint),iend(i,iint)
              ind=ind+1
 -            itypj=itype(j)
 -            if (itypj.eq.21) cycle
 +            itypj=iabs(itype(j))
 +            if (itypj.eq.ntyp1) cycle
              dscj_inv=vbld_inv(j+nres)
              sig0ij=sigma(itypi,itypj)
              r0ij=r0(itypi,itypj)
@@@ -1806,7 -1806,7 +1806,7 @@@ cd      write (iout,*) 'iatel_s=',iatel
          gcorr_loc(i)=0.0d0
        enddo
        do i=iatel_s,iatel_e
 -        if (itype(i).eq.21 .or. itype(i+1).eq.21) cycle
 +        if (itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1) cycle
          if (itel(i).eq.0) goto 1215
          dxi=dc(1,i)
          dyi=dc(2,i)
          num_conti=0
  c        write (iout,*) 'i',i,' ielstart',ielstart(i),' ielend',ielend(i)
          do j=ielstart(i),ielend(i)
 -          if (itype(j).eq.21 .or. itype(j+1).eq.21) cycle
 +          if (itype(j).eq.ntyp1 .or. itype(j+1).eq.ntyp1) cycle
            if (itel(j).eq.0) goto 1216
            ind=ind+1
            iteli=itel(i)
@@@ -2556,7 -2556,7 +2556,7 @@@ C Cartesian derivative
       &      +0.5d0*(pizda(1,1)+pizda(2,2))
          enddo
          endif
 -      else if (j.eq.i+3 .and. itype(i+2).ne.21) then
 +      else if (j.eq.i+3 .and. itype(i+2).ne.ntyp1) then
  CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
  C
  C               Fourth-order contributions
@@@ -2757,7 -2757,7 +2757,7 @@@ cd    print '(a)','Enter ESCP
  c      write (iout,*) 'iatscp_s=',iatscp_s,' iatscp_e=',iatscp_e,
  c     &  ' scal14',scal14
        do i=iatscp_s,iatscp_e
 -        if (itype(i).eq.21 .or. itype(i+1).eq.21) cycle
 +        if (itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1) cycle
          iteli=itel(i)
  c        write (iout,*) "i",i," iteli",iteli," nscp_gr",nscp_gr(i),
  c     &   " iscp",(iscpstart(i,j),iscpend(i,j),j=1,nscp_gr(i))
          do iint=1,nscp_gr(i)
  
          do j=iscpstart(i,iint),iscpend(i,iint)
 -          itypj=itype(j)
 -          if (itypj.eq.21) cycle
 +          itypj=iabs(itype(j))
 +          if (itypj.eq.ntyp1) cycle
  C Uncomment following three lines for SC-p interactions
  c         xj=c(1,nres+j)-xi
  c         yj=c(2,nres+j)-yi
@@@ -2880,8 -2880,7 +2880,8 @@@ 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. 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
          else
        include 'COMMON.VAR'
        include 'COMMON.IOUNITS'
        double precision erij(3),dcosom1(3),dcosom2(3),gg(3)
 -      itypi=itype(i)
 +      itypi=iabs(itype(i))
        xi=c(1,nres+i)
        yi=c(2,nres+i)
        zi=c(3,nres+i)
        dyi=dc_norm(2,nres+i)
        dzi=dc_norm(3,nres+i)
        dsci_inv=dsc_inv(itypi)
 -      itypj=itype(j)
 +      itypj=iabs(itype(j))
        dscj_inv=dsc_inv(itypj)
        xj=c(1,nres+j)-xi
        yj=c(2,nres+j)-yi
        logical energy_dec /.false./
        double precision u(3),ud(3)
        estr=0.0d0
 +      estr1=0.0d0
        do i=nnt+1,nct
 -        if (itype(i-1).eq.21 .or. itype(i).eq.21) then
 +        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)
@@@ -3047,13 -3045,13 +3047,13 @@@ c          write (iout,*) i,vbld(i),vbl
          endif
  
        enddo
 -      estr=0.5d0*AKP*estr
 +      estr=0.5d0*AKP*estr+estr1
  c
  c 09/18/07 AL: multimodal bond potential based on AM1 CA-SC PMF's included
  c
        do i=nnt,nct
 -        iti=itype(i)
 -        if (iti.ne.10 .and. iti.ne.21) then
 +        iti=iabs(itype(i))
 +        if (iti.ne.10 .and. iti.ne.ntyp1) then
            nbi=nbondterm(iti)
            if (nbi.eq.1) then
              diff=vbld(i+nres)-vbldsc0(1,iti)
@@@ -3129,23 -3127,11 +3129,23 @@@ c      write (iout,*) "nres",nre
  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.21) cycle
 +        if (itype(i-1).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)
 -        if (i.gt.3 .and. itype(i-2).ne.21) then
 +        ichir1=isign(1,itype(i-2))
 +        ichir2=isign(1,itype(i))
 +         if (itype(i-2).eq.10) ichir1=isign(1,itype(i-1))
 +         if (itype(i).eq.10) ichir2=isign(1,itype(i-1))
 +         if (itype(i-1).eq.10) then
 +          itype1=isign(10,itype(i-2))
 +          ichir11=isign(1,itype(i-2))
 +          ichir12=isign(1,itype(i-2))
 +          itype2=isign(10,itype(i))
 +          ichir21=isign(1,itype(i))
 +          ichir22=isign(1,itype(i))
 +         endif
 +        if (i.gt.3 .and. itype(i-2).ne.ntyp1) then
  #ifdef OSF
            phii=phi(i)
            icrc=0
            y(1)=0.0D0
            y(2)=0.0D0
          endif
 -        if (i.lt.nres .and. itype(i).ne.21) then
 +        if (i.lt.nres .and. itype(i).ne.ntyp1) then
  #ifdef OSF
            phii1=phi(i+1)
            icrc=0
@@@ -3182,12 -3168,8 +3182,12 @@@ C dependent on the adjacent virtual-bon
  C In following comments this theta will be referred to as t_c.
          thet_pred_mean=0.0d0
          do k=1,2
 -          athetk=athet(k,it)
 -          bthetk=bthet(k,it)
 +            athetk=athet(k,it,ichir1,ichir2)
 +            bthetk=bthet(k,it,ichir1,ichir2)
 +          if (it.eq.10) then
 +             athetk=athet(k,itype1,ichir11,ichir12)
 +             bthetk=bthet(k,itype2,ichir21,ichir22)
 +          endif
            thet_pred_mean=thet_pred_mean+athetk*y(k)+bthetk*z(k)
          enddo
  c        write (iout,*) "thet_pred_mean",thet_pred_mean
          thet_pred_mean=thet_pred_mean*ss+a0thet(it)
  c        write (iout,*) "thet_pred_mean",thet_pred_mean
  C Derivatives of the "mean" values in gamma1 and gamma2.
 -        dthetg1=(-athet(1,it)*y(2)+athet(2,it)*y(1))*ss
 -        dthetg2=(-bthet(1,it)*z(2)+bthet(2,it)*z(1))*ss
 +        dthetg1=(-athet(1,it,ichir1,ichir2)*y(2)
 +     &+athet(2,it,ichir1,ichir2)*y(1))*ss
 +         dthetg2=(-bthet(1,it,ichir1,ichir2)*z(2)
 +     &          +bthet(2,it,ichir1,ichir2)*z(1))*ss
 +         if (it.eq.10) then
 +      dthetg1=(-athet(1,itype1,ichir11,ichir12)*y(2)
 +     &+athet(2,itype1,ichir11,ichir12)*y(1))*ss
 +        dthetg2=(-bthet(1,itype2,ichir21,ichir22)*z(2)
 +     &         +bthet(2,itype2,ichir21,ichir22)*z(1))*ss
 +         endif
          if (theta(i).gt.pi-delta) then
            call theteng(pi-delta,thet_pred_mean,theta0(it),f0,fprim0,
       &         E_tc0)
        etheta=0.0D0
  c      write (iout,*) "ithetyp",(ithetyp(i),i=1,ntyp1)
        do i=ithet_start,ithet_end
 -        if (itype(i-1).eq.21) cycle
 +        if (itype(i-1).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)
 -        ityp2=ithetyp(itype(i-1))
 +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.gt.3 .and. itype(i-2).ne.21) then
 +        if (i.gt.3 .and. itype(i-2).ne.ntyp1) then
  #ifdef OSF
            phii=phi(i)
            if (phii.ne.phii) phii=150.0
  #else
            phii=phi(i)
  #endif
 -          ityp1=ithetyp(itype(i-2))
 +          ityp1=ithetyp((itype(i-2)))
            do k=1,nsingle
              cosph1(k)=dcos(k*phii)
              sinph1(k)=dsin(k*phii)
              sinph1(k)=0.0d0
            enddo 
          endif
 -        if (i.lt.nres .and. itype(i).ne.21) then
 +        if (i.lt.nres .and. itype(i).ne.ntyp1) then
  #ifdef OSF
            phii1=phi(i+1)
            if (phii1.ne.phii1) phii1=150.0
  #else
            phii1=phi(i+1)
  #endif
 -          ityp3=ithetyp(itype(i))
 +          ityp3=ithetyp((itype(i)))
            do k=1,nsingle
              cosph2(k)=dcos(k*phii1)
              sinph2(k)=dsin(k*phii1)
  c        write (iout,*) "i",i," ityp1",itype(i-2),ityp1,
  c     &   " ityp2",itype(i-1),ityp2," ityp3",itype(i),ityp3
  c        call flush(iout)
 -        ethetai=aa0thet(ityp1,ityp2,ityp3)
 +        ethetai=aa0thet(ityp1,ityp2,ityp3,iblock)
          do k=1,ndouble
            do l=1,k-1
              ccl=cosph1(l)*cosph2(k-l)
          enddo
          endif
          do k=1,ntheterm
 -          ethetai=ethetai+aathet(k,ityp1,ityp2,ityp3)*sinkt(k)
 -          dethetai=dethetai+0.5d0*k*aathet(k,ityp1,ityp2,ityp3)
 +          ethetai=ethetai+aathet(k,ityp1,ityp2,ityp3,iblock)*sinkt(k)
 +          dethetai=dethetai+0.5d0*k*aathet(k,ityp1,ityp2,ityp3,iblock)
       &      *coskt(k)
            if (lprn)
 -     &    write (iout,*) "k",k," aathet",aathet(k,ityp1,ityp2,ityp3),
 +     &    write (iout,*) "k",k," aathet",
 +     &    aathet(k,ityp1,ityp2,ityp3,iblock),
       &     " ethetai",ethetai
          enddo
          if (lprn) then
          endif
          do m=1,ntheterm2
            do k=1,nsingle
 -            aux=bbthet(k,m,ityp1,ityp2,ityp3)*cosph1(k)
 -     &         +ccthet(k,m,ityp1,ityp2,ityp3)*sinph1(k)
 -     &         +ddthet(k,m,ityp1,ityp2,ityp3)*cosph2(k)
 -     &         +eethet(k,m,ityp1,ityp2,ityp3)*sinph2(k)
 +            aux=bbthet(k,m,ityp1,ityp2,ityp3,iblock)*cosph1(k)
 +     &         +ccthet(k,m,ityp1,ityp2,ityp3,iblock)*sinph1(k)
 +     &         +ddthet(k,m,ityp1,ityp2,ityp3,iblock)*cosph2(k)
 +     &         +eethet(k,m,ityp1,ityp2,ityp3,iblock)*sinph2(k)
              ethetai=ethetai+sinkt(m)*aux
              dethetai=dethetai+0.5d0*m*aux*coskt(m)
              dephii=dephii+k*sinkt(m)*(
 -     &          ccthet(k,m,ityp1,ityp2,ityp3)*cosph1(k)-
 -     &          bbthet(k,m,ityp1,ityp2,ityp3)*sinph1(k))
 +     &          ccthet(k,m,ityp1,ityp2,ityp3,iblock)*cosph1(k)-
 +     &          bbthet(k,m,ityp1,ityp2,ityp3,iblock)*sinph1(k))
              dephii1=dephii1+k*sinkt(m)*(
 -     &          eethet(k,m,ityp1,ityp2,ityp3)*cosph2(k)-
 -     &          ddthet(k,m,ityp1,ityp2,ityp3)*sinph2(k))
 +     &          eethet(k,m,ityp1,ityp2,ityp3,iblock)*cosph2(k)-
 +     &          ddthet(k,m,ityp1,ityp2,ityp3,iblock)*sinph2(k))
              if (lprn)
       &      write (iout,*) "m",m," k",k," bbthet",
 -     &         bbthet(k,m,ityp1,ityp2,ityp3)," ccthet",
 -     &         ccthet(k,m,ityp1,ityp2,ityp3)," ddthet",
 -     &         ddthet(k,m,ityp1,ityp2,ityp3)," eethet",
 -     &         eethet(k,m,ityp1,ityp2,ityp3)," ethetai",ethetai
 +     &         bbthet(k,m,ityp1,ityp2,ityp3,iblock)," ccthet",
 +     &         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
            enddo
          enddo
          if (lprn)
          do m=1,ntheterm3
            do k=2,ndouble
              do l=1,k-1
 -              aux=ffthet(l,k,m,ityp1,ityp2,ityp3)*cosph1ph2(l,k)+
 -     &            ffthet(k,l,m,ityp1,ityp2,ityp3)*cosph1ph2(k,l)+
 -     &            ggthet(l,k,m,ityp1,ityp2,ityp3)*sinph1ph2(l,k)+
 -     &            ggthet(k,l,m,ityp1,ityp2,ityp3)*sinph1ph2(k,l)
 +              aux=ffthet(l,k,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(l,k)+
 +     &            ffthet(k,l,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(k,l)+
 +     &            ggthet(l,k,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(l,k)+
 +     &            ggthet(k,l,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(k,l)
                ethetai=ethetai+sinkt(m)*aux
                dethetai=dethetai+0.5d0*m*coskt(m)*aux
                dephii=dephii+l*sinkt(m)*(
 -     &           -ffthet(l,k,m,ityp1,ityp2,ityp3)*sinph1ph2(l,k)-
 -     &            ffthet(k,l,m,ityp1,ityp2,ityp3)*sinph1ph2(k,l)+
 -     &            ggthet(l,k,m,ityp1,ityp2,ityp3)*cosph1ph2(l,k)+
 -     &            ggthet(k,l,m,ityp1,ityp2,ityp3)*cosph1ph2(k,l))
 +     &           -ffthet(l,k,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(l,k)-
 +     &            ffthet(k,l,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(k,l)+
 +     &            ggthet(l,k,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(l,k)+
 +     &            ggthet(k,l,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(k,l))
                dephii1=dephii1+(k-l)*sinkt(m)*(
 -     &           -ffthet(l,k,m,ityp1,ityp2,ityp3)*sinph1ph2(l,k)+
 -     &            ffthet(k,l,m,ityp1,ityp2,ityp3)*sinph1ph2(k,l)+
 -     &            ggthet(l,k,m,ityp1,ityp2,ityp3)*cosph1ph2(l,k)-
 -     &            ggthet(k,l,m,ityp1,ityp2,ityp3)*cosph1ph2(k,l))
 +     &           -ffthet(l,k,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(l,k)+
 +     &            ffthet(k,l,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(k,l)+
 +     &            ggthet(l,k,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(l,k)-
 +     &            ggthet(k,l,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(k,l))
                if (lprn) then
                write (iout,*) "m",m," k",k," l",l," ffthet",
 -     &            ffthet(l,k,m,ityp1,ityp2,ityp3),
 -     &            ffthet(k,l,m,ityp1,ityp2,ityp3)," ggthet",
 -     &            ggthet(l,k,m,ityp1,ityp2,ityp3),
 -     &            ggthet(k,l,m,ityp1,ityp2,ityp3)," ethetai",ethetai
 +     &            ffthet(l,k,m,ityp1,ityp2,ityp3,iblock),
 +     &            ffthet(k,l,m,ityp1,ityp2,ityp3,iblock)," ggthet",
 +     &            ggthet(l,k,m,ityp1,ityp2,ityp3,iblock),
 +     &            ggthet(k,l,m,ityp1,ityp2,ityp3,iblock),
 +     &            " ethetai",ethetai
                write (iout,*) cosph1ph2(l,k)*sinkt(m),
       &            cosph1ph2(k,l)*sinkt(m),
       &            sinph1ph2(l,k)*sinkt(m),sinph1ph2(k,l)*sinkt(m)
@@@ -3571,9 -3540,9 +3571,9 @@@ C ALPHA and OMEGA
  c     write (iout,'(a)') 'ESC'
        do i=loc_start,loc_end
          it=itype(i)
 -        if (it.eq.21) cycle
 +        if (it.eq.ntyp1) cycle
          if (it.eq.10) goto 1
 -        nlobit=nlob(it)
 +        nlobit=nlob(iabs(it))
  c       print *,'i=',i,' it=',it,' nlobit=',nlobit
  c       write (iout,*) 'i=',i,' ssa=',ssa,' ssad=',ssad
          theti=theta(i+1)-pipol
@@@ -3728,7 -3697,7 +3728,7 @@@ C Compute the contribution to SC energ
          do iii=-1,1
  
            do j=1,nlobit
 -            expfac=dexp(bsc(j,it)-0.5D0*contr(j,iii)+emin)
 +            expfac=dexp(bsc(j,iabs(it))-0.5D0*contr(j,iii)+emin)
  cd          print *,'j=',j,' expfac=',expfac
              escloc_i=escloc_i+expfac
              do k=1,3
@@@ -3809,7 -3778,7 +3809,7 @@@ C Compute the contribution to SC energ
  
        dersc12=0.0d0
        do j=1,nlobit
 -        expfac=dexp(bsc(j,it)-0.5D0*contr(j)+emin)
 +        expfac=dexp(bsc(j,iabs(it))-0.5D0*contr(j)+emin)
          escloc_i=escloc_i+expfac
          do k=1,2
            dersc(k)=dersc(k)+Ax(k,j)*expfac
        delta=0.02d0*pi
        escloc=0.0D0
        do i=loc_start,loc_end
 -        if (itype(i).eq.21) cycle
 +        if (itype(i).eq.ntyp1) cycle
          costtab(i+1) =dcos(theta(i+1))
          sinttab(i+1) =dsqrt(1-costtab(i+1)*costtab(i+1))
          cost2tab(i+1)=dsqrt(0.5d0*(1.0d0+costtab(i+1)))
          cosfac=dsqrt(cosfac2)
          sinfac2=0.5d0/(1.0d0-costtab(i+1))
          sinfac=dsqrt(sinfac2)
 -        it=itype(i)
 +        it=iabs(itype(i))
          if (it.eq.10) goto 1
  c
  C  Compute the axes of tghe local cartesian coordinates system; store in
@@@ -3891,7 -3860,7 +3891,7 @@@ C     &   dc_norm(3,i+nres
            y_prime(j) = (dc_norm(j,i) + dc_norm(j,i-1))*sinfac
          enddo
          do j = 1,3
 -          z_prime(j) = -uz(j,i-1)
 +          z_prime(j) = -uz(j,i-1)*dsign(1.0d0,dfloat(itype(i)))
          enddo     
  c       write (2,*) "i",i
  c       write (2,*) "x_prime",(x_prime(j),j=1,3)
  C Compute the energy of the ith side cbain
  C
  c        write (2,*) "xx",xx," yy",yy," zz",zz
 -        it=itype(i)
 +        it=iabs(itype(i))
          do j = 1,65
            x(j) = sc_parmin(j,it) 
          enddo
@@@ -4102,10 -4071,8 +4102,10 @@@ c     &   (dC_norm(j,i-1),j=1,3)," vbld
           dZZ_Ci1(k)=0.0d0
           dZZ_Ci(k)=0.0d0
           do j=1,3
 -           dZZ_Ci(k)=dZZ_Ci(k)-uzgrad(j,k,2,i-1)*dC_norm(j,i+nres)
 -           dZZ_Ci1(k)=dZZ_Ci1(k)-uzgrad(j,k,1,i-1)*dC_norm(j,i+nres)
 +           dZZ_Ci(k)=dZZ_Ci(k)-uzgrad(j,k,2,i-1)
 +     &      *dsign(1.0d0,dfloat(itype(i)))*dC_norm(j,i+nres)
 +           dZZ_Ci1(k)=dZZ_Ci1(k)-uzgrad(j,k,1,i-1)
 +     & *dsign(1.0d0,dfloat(itype(i)))*dC_norm(j,i+nres)
           enddo
            
           dXX_XYZ(k)=vbld_inv(i+nres)*(x_prime(k)-xx*dC_norm(k,i+nres))
@@@ -4257,8 -4224,8 +4257,8 @@@ C Set lprn=.true. for debuggin
  c      lprn=.true.
        etors=0.0D0
        do i=iphi_start,iphi_end
 -        if (itype(i-2).eq.21 .or. itype(i-1).eq.21
 -     &      .or. itype(i).eq.21) cycle
 +        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))
          phii=phi(i)
@@@ -4342,22 -4309,17 +4342,22 @@@ C Set lprn=.true. for debuggin
  c      lprn=.true.
        etors=0.0D0
        do i=iphi_start,iphi_end
 -        if (itype(i-2).eq.21 .or. itype(i-1).eq.21
 -     &       .or. itype(i).eq.21) cycle
 +        if (itype(i-2).eq.ntyp1 .or. itype(i-1).eq.ntyp1
 +     &       .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
 +         else
 +         iblock=1
 +         endif
          itori=itortyp(itype(i-2))
          itori1=itortyp(itype(i-1))
          phii=phi(i)
          gloci=0.0D0
  C Regular cosine and sine terms
 -        do j=1,nterm(itori,itori1)
 -          v1ij=v1(j,itori,itori1)
 -          v2ij=v2(j,itori,itori1)
 +        do j=1,nterm(itori,itori1,iblock)
 +          v1ij=v1(j,itori,itori1,iblock)
 +          v2ij=v2(j,itori,itori1,iblock)
            cosphi=dcos(j*phii)
            sinphi=dsin(j*phii)
            etors=etors+v1ij*cosphi+v2ij*sinphi
@@@ -4370,7 -4332,7 +4370,7 @@@ C          [v2 cos(phi/2)+v3 sin(phi/2)
  C
          cosphi=dcos(0.5d0*phii)
          sinphi=dsin(0.5d0*phii)
 -        do j=1,nlor(itori,itori1)
 +        do j=1,nlor(itori,itori1,iblock)
            vl1ij=vlor1(j,itori,itori1)
            vl2ij=vlor2(j,itori,itori1)
            vl3ij=vlor3(j,itori,itori1)
            gloci=gloci+vl1ij*(vl3ij*cosphi-vl2ij*sinphi)*pom
          enddo
  C Subtract the constant term
 -        etors=etors-v0(itori,itori1)
 +        etors=etors-v0(itori,itori1,iblock)
          if (lprn)
       &  write (iout,'(2(a3,2x,i3,2x),2i3,6f8.3/26x,6f8.3/)')
       &  restyp(itype(i-2)),i-2,restyp(itype(i-1)),i-1,itori,itori1,
 -     &  (v1(j,itori,itori1),j=1,6),(v2(j,itori,itori1),j=1,6)
 +     &  (v1(j,itori,itori1,1),j=1,6),(v2(j,itori,itori1,1),j=1,6)
          gloc(i-3,icg)=gloc(i-3,icg)+wtor*fact*gloci
  c       write (iout,*) 'i=',i,' gloc=',gloc(i-3,icg)
   1215   continue
@@@ -4441,8 -4403,8 +4441,8 @@@ 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.21 .or. itype(i-1).eq.21
 -     &      .or. itype(i).eq.21 .or. itype(i+1).eq.21) cycle
 +        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 (itel(i-2).eq.0 .or. itel(i-1).eq.0 .or. itel(i).eq.0) 
       &     goto 1215
          itori=itortyp(itype(i-2))
          phii1=phi(i+1)
          gloci1=0.0D0
          gloci2=0.0D0
 +        iblock=1
 +        if (iabs(itype(i+1)).eq.20) iblock=2
  C Regular cosine and sine terms
 -        do j=1,ntermd_1(itori,itori1,itori2)
 -          v1cij=v1c(1,j,itori,itori1,itori2)
 -          v1sij=v1s(1,j,itori,itori1,itori2)
 -          v2cij=v1c(2,j,itori,itori1,itori2)
 -          v2sij=v1s(2,j,itori,itori1,itori2)
 +       do j=1,ntermd_1(itori,itori1,itori2,iblock)
 +          v1cij=v1c(1,j,itori,itori1,itori2,iblock)
 +          v1sij=v1s(1,j,itori,itori1,itori2,iblock)
 +          v2cij=v1c(2,j,itori,itori1,itori2,iblock)
 +          v2sij=v1s(2,j,itori,itori1,itori2,iblock)
            cosphi1=dcos(j*phii)
            sinphi1=dsin(j*phii)
            cosphi2=dcos(j*phii1)
            gloci1=gloci1+j*(v1sij*cosphi1-v1cij*sinphi1)
            gloci2=gloci2+j*(v2sij*cosphi2-v2cij*sinphi2)
          enddo
 -        do k=2,ntermd_2(itori,itori1,itori2)
 +        do k=2,ntermd_2(itori,itori1,itori2,iblock)
            do l=1,k-1
 -            v1cdij = v2c(k,l,itori,itori1,itori2)
 -            v2cdij = v2c(l,k,itori,itori1,itori2)
 -            v1sdij = v2s(k,l,itori,itori1,itori2)
 -            v2sdij = v2s(l,k,itori,itori1,itori2)
 +            v1cdij = v2c(k,l,itori,itori1,itori2,iblock)
 +            v2cdij = v2c(l,k,itori,itori1,itori2,iblock)
 +            v1sdij = v2s(k,l,itori,itori1,itori2,iblock)
 +            v2sdij = v2s(l,k,itori,itori1,itori2,iblock)
              cosphi1p2=dcos(l*phii+(k-l)*phii1)
              cosphi1m2=dcos(l*phii-(k-l)*phii1)
              sinphi1p2=dsin(l*phii+(k-l)*phii1)
@@@ -4523,48 -4483,26 +4523,48 @@@ C Set lprn=.true. for debuggin
  c      lprn=.true.
  c      write (iout,*) "EBACK_SC_COR",iphi_start,iphi_end,nterm_sccor
        esccor=0.0D0
 -      do i=iphi_start,iphi_end
 -        if (itype(i-2).eq.21 .or. itype(i-1).eq.21) cycle
 +      do i=itau_start,itau_end
 +        if (itype(i-2).eq.ntyp1 .or. itype(i-1).eq.ntyp1) cycle
          esccor_ii=0.0D0
 -        itori=itype(i-2)
 -        itori1=itype(i-1)
 +        isccori=isccortyp(itype(i-2))
 +        isccori1=isccortyp(itype(i-1))
          phii=phi(i)
 +        do intertyp=1,3 !intertyp
 +cc Added 09 May 2012 (Adasko)
 +cc  Intertyp means interaction type of backbone mainchain correlation: 
 +c   1 = SC...Ca...Ca...Ca
 +c   2 = Ca...Ca...Ca...SC
 +c   3 = SC...Ca...Ca...SCi
          gloci=0.0D0
 -        do j=1,nterm_sccor
 -          v1ij=v1sccor(j,itori,itori1)
 -          v2ij=v2sccor(j,itori,itori1)
 -          cosphi=dcos(j*phii)
 -          sinphi=dsin(j*phii)
 -          esccor=esccor+v1ij*cosphi+v2ij*sinphi
 -          gloci=gloci+j*(v2ij*cosphi-v1ij*sinphi)
 -        enddo
 +        if (((intertyp.eq.3).and.((itype(i-2).eq.10).or.
 +     &      (itype(i-1).eq.10).or.(itype(i-2).eq.ntyp1).or.
 +     &      (itype(i-1).eq.ntyp1)))
 +     &    .or. ((intertyp.eq.1).and.((itype(i-2).eq.10)
 +     &     .or.(itype(i-2).eq.ntyp1).or.(itype(i-1).eq.ntyp1)
 +     &     .or.(itype(i).eq.ntyp1)))
 +     &    .or.((intertyp.eq.2).and.((itype(i-1).eq.10).or.
 +     &      (itype(i-1).eq.ntyp1).or.(itype(i-2).eq.ntyp1).or.
 +     &      (itype(i-3).eq.ntyp1)))) cycle
 +        if ((intertyp.eq.2).and.(i.eq.4).and.(itype(1).eq.ntyp1)) cycle
 +        if ((intertyp.eq.1).and.(i.eq.nres).and.(itype(nres).eq.ntyp1))
 +     & cycle
 +       do j=1,nterm_sccor(isccori,isccori1)
 +          v1ij=v1sccor(j,intertyp,isccori,isccori1)
 +          v2ij=v2sccor(j,intertyp,isccori,isccori1)
 +          cosphi=dcos(j*tauangle(intertyp,i))
 +          sinphi=dsin(j*tauangle(intertyp,i))
 +           esccor=esccor+v1ij*cosphi+v2ij*sinphi
 +c           gloci=gloci+j*(v2ij*cosphi-v1ij*sinphi)
 +         enddo
 +c      write (iout,*) "EBACK_SC_COR",i,esccor,intertyp
 +c      gloc_sc(intertyp,i-3)=gloc_sc(intertyp,i-3)+wsccor*gloci
          if (lprn)
       &  write (iout,'(2(a3,2x,i3,2x),2i3,6f8.3/26x,6f8.3/)')
       &  restyp(itype(i-2)),i-2,restyp(itype(i-1)),i-1,itori,itori1,
 -     &  (v1sccor(j,itori,itori1),j=1,6),(v2sccor(j,itori,itori1),j=1,6)
 +     &  (v1sccor(j,1,itori,itori1),j=1,6),
 +     &  (v2sccor(j,1,itori,itori1),j=1,6)
          gsccor_loc(i-3)=gloci
 +       enddo !intertyp
        enddo
        return
        end
@@@ -6435,7 -6373,7 +6435,7 @@@ c--------------------------------------
        include 'COMMON.GEO'
        logical swap
        double precision vv(2),pizda(2,2),auxmat(2,2),auxvec(2),
-      & auxvec1(2),auxvec2(1),auxmat1(2,2)
+      & auxvec1(2),auxvec2(2),auxmat1(2,2)
        logical lprn
        common /kutas/ lprn
  CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
  #ifdef SPLITELE
        etot=wsc*evdw+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
  #else
        etot=wsc*evdw+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
        energia(18)=estr
        energia(19)=esccor
        energia(20)=edihcnstr
+ cc      if (dyn_ss) call dyn_set_nss
  c detecting NaNQ
        i=0
  #ifdef WINPGI
@@@ -218,7 -219,7 +219,7 @@@ cd        write (iout,*) i,g_corr5_loc(
       &   +wturn4*fact(3)*gel_loc_turn4(i)
       &   +wturn3*fact(2)*gel_loc_turn3(i)
       &   +wturn6*fact(5)*gel_loc_turn6(i)
-      &   +wel_loc*fact(2)*gel_loc_loc(i)+
+      &   +wel_loc*fact(2)*gel_loc_loc(i)
       &   +wsccor*fact(1)*gsccor_loc(i)
        enddo
        endif
@@@ -355,8 -356,8 +356,8 @@@ c      include "DIMENSIONS.COMPAR
  cd    print *,'Entering ELJ nnt=',nnt,' nct=',nct,' expon=',expon
        evdw=0.0D0
        do i=iatsc_s,iatsc_e
 -        itypi=itype(i)
 -        itypi1=itype(i+1)
 +        itypi=iabs(itype(i))
 +        itypi1=iabs(itype(i+1))
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
  cd        write (iout,*) 'i=',i,' iint=',iint,' istart=',istart(i,iint),
  cd   &                  'iend=',iend(i,iint)
            do j=istart(i,iint),iend(i,iint)
 -            itypj=itype(j)
 +            itypj=iabs(itype(j))
              xj=c(1,nres+j)-xi
              yj=c(2,nres+j)-yi
              zj=c(3,nres+j)-zi
@@@ -513,8 -514,8 +514,8 @@@ c      include "DIMENSIONS.COMPAR
  c     print *,'Entering ELJK nnt=',nnt,' nct=',nct,' expon=',expon
        evdw=0.0D0
        do i=iatsc_s,iatsc_e
 -        itypi=itype(i)
 -        itypi1=itype(i+1)
 +        itypi=iabs(itype(i))
 +        itypi1=iabs(itype(i+1))
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
@@@ -523,7 -524,7 +524,7 @@@ C Calculate SC interaction energy
  C
          do iint=1,nint_gr(i)
            do j=istart(i,iint),iend(i,iint)
 -            itypj=itype(j)
 +            itypj=iabs(itype(j))
              xj=c(1,nres+j)-xi
              yj=c(2,nres+j)-yi
              zj=c(3,nres+j)-zi
@@@ -611,8 -612,8 +612,8 @@@ c     els
  c     endif
        ind=0
        do i=iatsc_s,iatsc_e
 -        itypi=itype(i)
 -        itypi1=itype(i+1)
 +        itypi=iabs(itype(i))
 +        itypi1=iabs(itype(i+1))
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
          do iint=1,nint_gr(i)
            do j=istart(i,iint),iend(i,iint)
              ind=ind+1
 -            itypj=itype(j)
 +            itypj=iabs(itype(j))
              dscj_inv=vbld_inv(j+nres)
              chi1=chi(itypi,itypj)
              chi2=chi(itypj,itypi)
@@@ -723,6 -724,7 +724,7 @@@ c      include "DIMENSIONS.COMPAR
        include 'COMMON.INTERACT'
        include 'COMMON.IOUNITS'
        include 'COMMON.CALC'
+       include 'COMMON.SBRIDGE'
        logical lprn
        common /srutu/icall
        integer icant
@@@ -734,8 -736,8 +736,8 @@@ c     print *,'Entering EGB nnt=',nnt,
  c      if (icall.gt.0) lprn=.true.
        ind=0
        do i=iatsc_s,iatsc_e
 -        itypi=itype(i)
 -        itypi1=itype(i+1)
 +        itypi=iabs(itype(i))
 +        itypi1=iabs(itype(i+1))
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
@@@ -748,8 -750,14 +750,14 @@@ 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              if (energy_dec) write (iout,'(a6,2i5,0pf7.3,a3)')
+ c     &                        'evdw',i,j,evdwij,' ss'
+             ELSE
              ind=ind+1
 -            itypj=itype(j)
 +            itypj=iabs(itype(j))
              dscj_inv=vbld_inv(j+nres)
              sig0ij=sigma(itypi,itypj)
              chi1=chi(itypi,itypj)
@@@ -830,6 -838,7 +838,7 @@@ C Calculate the radial part of the grad
  C Calculate angular part of the gradient.
              call sc_grad
              endif
+             ENDIF    ! SSBOND
            enddo      ! j
          enddo        ! iint
        enddo          ! i
@@@ -854,6 -863,7 +863,7 @@@ c      include "DIMENSIONS.COMPAR
        include 'COMMON.INTERACT'
        include 'COMMON.IOUNITS'
        include 'COMMON.CALC'
+       include 'COMMON.SBRIDGE'
        common /srutu/ icall
        logical lprn
        integer icant
@@@ -865,8 -875,8 +875,8 @@@ c     print *,'Entering EGB nnt=',nnt,
  c      if (icall.gt.0) lprn=.true.
        ind=0
        do i=iatsc_s,iatsc_e
 -        itypi=itype(i)
 -        itypi1=itype(i+1)
 +        itypi=iabs(itype(i))
 +        itypi1=iabs(itype(i+1))
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
@@@ -879,8 -889,15 +889,15 @@@ C Calculate SC interaction energy
  C
          do iint=1,nint_gr(i)
            do j=istart(i,iint),iend(i,iint)
+ C in case of diagnostics    write (iout,*) "TU SZUKAJ",i,j,dyn_ss_mask(i),dyn_ss_mask(j)
+             IF (dyn_ss_mask(i).and.dyn_ss_mask(j)) THEN
+               call dyn_ssbond_ene(i,j,evdwij)
+               evdw=evdw+evdwij
+ c              if (energy_dec) write (iout,'(a6,2i5,0pf7.3,a3)')
+ c     &                        'evdw',i,j,evdwij,' ss'
+             ELSE
              ind=ind+1
 -            itypj=itype(j)
 +            itypj=iabs(itype(j))
              dscj_inv=vbld_inv(j+nres)
              sig0ij=sigma(itypi,itypj)
              r0ij=r0(itypi,itypj)
@@@ -961,6 -978,7 +978,7 @@@ C Calculate the radial part of the grad
  C Calculate angular part of the gradient.
              call sc_grad
              endif
+             ENDIF    ! dyn_ss
            enddo      ! j
          enddo        ! iint
        enddo          ! i
@@@ -2710,7 -2728,7 +2728,7 @@@ c     &   " iscp",(iscpstart(i,j),iscpe
          do iint=1,nscp_gr(i)
  
          do j=iscpstart(i,iint),iscpend(i,iint)
 -          itypj=itype(j)
 +          itypj=iabs(itype(j))
  C Uncomment following three lines for SC-p interactions
  c         xj=c(1,nres+j)-xi
  c         yj=c(2,nres+j)-yi
@@@ -2822,11 -2840,13 +2840,14 @@@ 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.
+         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
  cd          write (iout,*) "eij",eij
+         endif
          else if (ii.gt.nres .and. jj.gt.nres) then
  c Restraints from contact prediction
            dd=dist(ii,jj)
        include 'COMMON.VAR'
        include 'COMMON.IOUNITS'
        double precision erij(3),dcosom1(3),dcosom2(3),gg(3)
 -      itypi=itype(i)
 +      itypi=iabs(itype(i))
        xi=c(1,nres+i)
        yi=c(2,nres+i)
        zi=c(3,nres+i)
        dyi=dc_norm(2,nres+i)
        dzi=dc_norm(3,nres+i)
        dsci_inv=dsc_inv(itypi)
 -      itypj=itype(j)
 +      itypj=iabs(itype(j))
        dscj_inv=dsc_inv(itypj)
        xj=c(1,nres+j)-xi
        yj=c(2,nres+j)-yi
        deltat12=om2-om1+2.0d0
        cosphi=om12-om1*om2
        eij=akcm*deltad*deltad+akth*(deltat1*deltat1+deltat2*deltat2)
-      &  +akct*deltad*deltat12
+      &  +akct*deltad*deltat12+ebr
       &  +v1ss*cosphi+v2ss*cosphi*cosphi+v3ss*cosphi*cosphi*cosphi
  c      write(iout,*) i,j,"rij",rij,"d0cm",d0cm," akcm",akcm," akth",akth,
  c     &  " akct",akct," deltad",deltad," deltat",deltat1,deltat2,
  c 09/18/07 AL: multimodal bond potential based on AM1 CA-SC PMF's included
  c
        do i=nnt,nct
 -        iti=itype(i)
 +        iti=iabs(itype(i))
          if (iti.ne.10) then
            nbi=nbondterm(iti)
            if (nbi.eq.1) then
@@@ -3100,18 -3120,6 +3121,18 @@@ c      write (iout,*) ithet_start,ithet
  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)
 +        ichir1=isign(1,itype(i-2))
 +        ichir2=isign(1,itype(i))
 +         if (itype(i-2).eq.10) ichir1=isign(1,itype(i-1))
 +         if (itype(i).eq.10) ichir2=isign(1,itype(i-1))
 +         if (itype(i-1).eq.10) then
 +          itype1=isign(10,itype(i-2))
 +          ichir11=isign(1,itype(i-2))
 +          ichir12=isign(1,itype(i-2))
 +          itype2=isign(10,itype(i))
 +          ichir21=isign(1,itype(i))
 +          ichir22=isign(1,itype(i))
 +         endif
  c        if (i.gt.ithet_start .and. 
  c     &     (itel(i-1).eq.0 .or. itel(i-2).eq.0)) goto 1215
  c        if (i.gt.3 .and. (i.le.4 .or. itel(i-3).ne.0)) then
@@@ -3167,12 -3175,8 +3188,12 @@@ C dependent on the adjacent virtual-bon
  C In following comments this theta will be referred to as t_c.
          thet_pred_mean=0.0d0
          do k=1,2
 -          athetk=athet(k,it)
 -          bthetk=bthet(k,it)
 +            athetk=athet(k,it,ichir1,ichir2)
 +            bthetk=bthet(k,it,ichir1,ichir2)
 +          if (it.eq.10) then
 +             athetk=athet(k,itype1,ichir11,ichir12)
 +             bthetk=bthet(k,itype2,ichir21,ichir22)
 +          endif
            thet_pred_mean=thet_pred_mean+athetk*y(k)+bthetk*z(k)
          enddo
  c        write (iout,*) "thet_pred_mean",thet_pred_mean
          thet_pred_mean=thet_pred_mean*ss+a0thet(it)
  c        write (iout,*) "thet_pred_mean",thet_pred_mean
  C Derivatives of the "mean" values in gamma1 and gamma2.
 -        dthetg1=(-athet(1,it)*y(2)+athet(2,it)*y(1))*ss
 -        dthetg2=(-bthet(1,it)*z(2)+bthet(2,it)*z(1))*ss
 +        dthetg1=(-athet(1,it,ichir1,ichir2)*y(2)
 +     &+athet(2,it,ichir1,ichir2)*y(1))*ss
 +         dthetg2=(-bthet(1,it,ichir1,ichir2)*z(2)
 +     &          +bthet(2,it,ichir1,ichir2)*z(1))*ss
 +         if (it.eq.10) then
 +      dthetg1=(-athet(1,itype1,ichir11,ichir12)*y(2)
 +     &+athet(2,itype1,ichir11,ichir12)*y(1))*ss
 +        dthetg2=(-bthet(1,itype2,ichir21,ichir22)*z(2)
 +     &         +bthet(2,itype2,ichir21,ichir22)*z(1))*ss
 +         endif
          if (theta(i).gt.pi-delta) then
            call theteng(pi-delta,thet_pred_mean,theta0(it),f0,fprim0,
       &         E_tc0)
        etheta=0.0D0
  c      write (iout,*) "ithetyp",(ithetyp(i),i=1,ntyp1)
        do i=ithet_start,ithet_end
 +         if (itype(i-1).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)
 -        ityp2=ithetyp(itype(i-1))
 +        ityp2=ithetyp((itype(i-1)))
          do k=1,nntheterm
            coskt(k)=dcos(k*theti2)
            sinkt(k)=dsin(k*theti2)
  #else
            phii=phi(i)
  #endif
 -          ityp1=ithetyp(itype(i-2))
 +          ityp1=ithetyp((itype(i-2)))
            do k=1,nsingle
              cosph1(k)=dcos(k*phii)
              sinph1(k)=dsin(k*phii)
  #else
            phii1=phi(i+1)
  #endif
 -          ityp3=ithetyp(itype(i))
 +          ityp3=ithetyp((itype(i)))
            do k=1,nsingle
              cosph2(k)=dcos(k*phii1)
              sinph2(k)=dsin(k*phii1)
  c        write (iout,*) "i",i," ityp1",itype(i-2),ityp1,
  c     &   " ityp2",itype(i-1),ityp2," ityp3",itype(i),ityp3
  c        call flush(iout)
 -        ethetai=aa0thet(ityp1,ityp2,ityp3)
 +        ethetai=aa0thet(ityp1,ityp2,ityp3,iblock)
          do k=1,ndouble
            do l=1,k-1
              ccl=cosph1(l)*cosph2(k-l)
          enddo
          endif
          do k=1,ntheterm
 -          ethetai=ethetai+aathet(k,ityp1,ityp2,ityp3)*sinkt(k)
 -          dethetai=dethetai+0.5d0*k*aathet(k,ityp1,ityp2,ityp3)
 +          ethetai=ethetai+aathet(k,ityp1,ityp2,ityp3,iblock)*sinkt(k)
 +          dethetai=dethetai+0.5d0*k*aathet(k,ityp1,ityp2,ityp3,iblock)
       &      *coskt(k)
            if (lprn)
 -     &    write (iout,*) "k",k," aathet",aathet(k,ityp1,ityp2,ityp3),
 +     &    write (iout,*) "k",k," aathet"
 +     &       ,aathet(k,ityp1,ityp2,ityp3,iblock),
       &     " ethetai",ethetai
          enddo
          if (lprn) then
          endif
          do m=1,ntheterm2
            do k=1,nsingle
 -            aux=bbthet(k,m,ityp1,ityp2,ityp3)*cosph1(k)
 -     &         +ccthet(k,m,ityp1,ityp2,ityp3)*sinph1(k)
 -     &         +ddthet(k,m,ityp1,ityp2,ityp3)*cosph2(k)
 -     &         +eethet(k,m,ityp1,ityp2,ityp3)*sinph2(k)
 +            aux=bbthet(k,m,ityp1,ityp2,ityp3,iblock)*cosph1(k)
 +     &         +ccthet(k,m,ityp1,ityp2,ityp3,iblock)*sinph1(k)
 +     &         +ddthet(k,m,ityp1,ityp2,ityp3,iblock)*cosph2(k)
 +     &         +eethet(k,m,ityp1,ityp2,ityp3,iblock)*sinph2(k)
              ethetai=ethetai+sinkt(m)*aux
              dethetai=dethetai+0.5d0*m*aux*coskt(m)
              dephii=dephii+k*sinkt(m)*(
 -     &          ccthet(k,m,ityp1,ityp2,ityp3)*cosph1(k)-
 -     &          bbthet(k,m,ityp1,ityp2,ityp3)*sinph1(k))
 +     &          ccthet(k,m,ityp1,ityp2,ityp3,iblock)*cosph1(k)-
 +     &          bbthet(k,m,ityp1,ityp2,ityp3,iblock)*sinph1(k))
              dephii1=dephii1+k*sinkt(m)*(
 -     &          eethet(k,m,ityp1,ityp2,ityp3)*cosph2(k)-
 -     &          ddthet(k,m,ityp1,ityp2,ityp3)*sinph2(k))
 +     &          eethet(k,m,ityp1,ityp2,ityp3,iblock)*cosph2(k)-
 +     &          ddthet(k,m,ityp1,ityp2,ityp3,iblock)*sinph2(k))
              if (lprn)
       &      write (iout,*) "m",m," k",k," bbthet",
 -     &         bbthet(k,m,ityp1,ityp2,ityp3)," ccthet",
 -     &         ccthet(k,m,ityp1,ityp2,ityp3)," ddthet",
 -     &         ddthet(k,m,ityp1,ityp2,ityp3)," eethet",
 -     &         eethet(k,m,ityp1,ityp2,ityp3)," ethetai",ethetai
 +     &         bbthet(k,m,ityp1,ityp2,ityp3,iblock)," ccthet",
 +     &         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
            enddo
          enddo
          if (lprn)
          do m=1,ntheterm3
            do k=2,ndouble
              do l=1,k-1
 -              aux=ffthet(l,k,m,ityp1,ityp2,ityp3)*cosph1ph2(l,k)+
 -     &            ffthet(k,l,m,ityp1,ityp2,ityp3)*cosph1ph2(k,l)+
 -     &            ggthet(l,k,m,ityp1,ityp2,ityp3)*sinph1ph2(l,k)+
 -     &            ggthet(k,l,m,ityp1,ityp2,ityp3)*sinph1ph2(k,l)
 +              aux=ffthet(l,k,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(l,k)+
 +     &            ffthet(k,l,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(k,l)+
 +     &            ggthet(l,k,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(l,k)+
 +     &            ggthet(k,l,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(k,l)
                ethetai=ethetai+sinkt(m)*aux
                dethetai=dethetai+0.5d0*m*coskt(m)*aux
                dephii=dephii+l*sinkt(m)*(
 -     &           -ffthet(l,k,m,ityp1,ityp2,ityp3)*sinph1ph2(l,k)-
 -     &            ffthet(k,l,m,ityp1,ityp2,ityp3)*sinph1ph2(k,l)+
 -     &            ggthet(l,k,m,ityp1,ityp2,ityp3)*cosph1ph2(l,k)+
 -     &            ggthet(k,l,m,ityp1,ityp2,ityp3)*cosph1ph2(k,l))
 +     &           -ffthet(l,k,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(l,k)-
 +     &            ffthet(k,l,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(k,l)+
 +     &            ggthet(l,k,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(l,k)+
 +     &            ggthet(k,l,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(k,l))
                dephii1=dephii1+(k-l)*sinkt(m)*(
 -     &           -ffthet(l,k,m,ityp1,ityp2,ityp3)*sinph1ph2(l,k)+
 -     &            ffthet(k,l,m,ityp1,ityp2,ityp3)*sinph1ph2(k,l)+
 -     &            ggthet(l,k,m,ityp1,ityp2,ityp3)*cosph1ph2(l,k)-
 -     &            ggthet(k,l,m,ityp1,ityp2,ityp3)*cosph1ph2(k,l))
 +     &           -ffthet(l,k,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(l,k)+
 +     &            ffthet(k,l,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(k,l)+
 +     &            ggthet(l,k,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(l,k)-
 +     &            ggthet(k,l,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(k,l))
                if (lprn) then
                write (iout,*) "m",m," k",k," l",l," ffthet",
 -     &            ffthet(l,k,m,ityp1,ityp2,ityp3),
 -     &            ffthet(k,l,m,ityp1,ityp2,ityp3)," ggthet",
 -     &            ggthet(l,k,m,ityp1,ityp2,ityp3),
 -     &            ggthet(k,l,m,ityp1,ityp2,ityp3)," ethetai",ethetai
 +     &            ffthet(l,k,m,ityp1,ityp2,ityp3,iblock),
 +     &            ffthet(k,l,m,ityp1,ityp2,ityp3,iblock)," ggthet",
 +     &            ggthet(l,k,m,ityp1,ityp2,ityp3,iblock),
 +     &            ggthet(k,l,m,ityp1,ityp2,ityp3,iblock)," ethetai",
 +     &            ethetai
                write (iout,*) cosph1ph2(l,k)*sinkt(m),
       &            cosph1ph2(k,l)*sinkt(m),
       &            sinph1ph2(l,k)*sinkt(m),sinph1ph2(k,l)*sinkt(m)
@@@ -3555,7 -3546,7 +3576,7 @@@ c     write (iout,'(a)') 'ESC
        do i=loc_start,loc_end
          it=itype(i)
          if (it.eq.10) goto 1
 -        nlobit=nlob(it)
 +        nlobit=nlob(iabs(it))
  c       print *,'i=',i,' it=',it,' nlobit=',nlobit
  c       write (iout,*) 'i=',i,' ssa=',ssa,' ssad=',ssad
          theti=theta(i+1)-pipol
@@@ -3710,7 -3701,7 +3731,7 @@@ C Compute the contribution to SC energ
          do iii=-1,1
  
            do j=1,nlobit
 -            expfac=dexp(bsc(j,it)-0.5D0*contr(j,iii)+emin)
 +            expfac=dexp(bsc(j,iabs(it))-0.5D0*contr(j,iii)+emin)
  cd          print *,'j=',j,' expfac=',expfac
              escloc_i=escloc_i+expfac
              do k=1,3
@@@ -3791,7 -3782,7 +3812,7 @@@ C Compute the contribution to SC energ
  
        dersc12=0.0d0
        do j=1,nlobit
 -        expfac=dexp(bsc(j,it)-0.5D0*contr(j)+emin)
 +        expfac=dexp(bsc(j,iabs(it))-0.5D0*contr(j)+emin)
          escloc_i=escloc_i+expfac
          do k=1,2
            dersc(k)=dersc(k)+Ax(k,j)*expfac
          cosfac=dsqrt(cosfac2)
          sinfac2=0.5d0/(1.0d0-costtab(i+1))
          sinfac=dsqrt(sinfac2)
 -        it=itype(i)
 +        it=iabs(itype(i))
          if (it.eq.10) goto 1
  c
  C  Compute the axes of tghe local cartesian coordinates system; store in
@@@ -3871,7 -3862,7 +3892,7 @@@ C     &   dc_norm(3,i+nres
            y_prime(j) = (dc_norm(j,i) + dc_norm(j,i-1))*sinfac
          enddo
          do j = 1,3
 -          z_prime(j) = -uz(j,i-1)
 +          z_prime(j) = -uz(j,i-1)*dsign(1.0d0,dfloat(itype(i)))
          enddo     
  c       write (2,*) "i",i
  c       write (2,*) "x_prime",(x_prime(j),j=1,3)
            xx = xx + x_prime(j)*dc_norm(j,i+nres)
            yy = yy + y_prime(j)*dc_norm(j,i+nres)
            zz = zz + z_prime(j)*dc_norm(j,i+nres)
 +          zz = zz + z_prime(j)*dc_norm(j,i+nres)
          enddo
  
          xxtab(i)=xx
          yytab(i)=yy
          zztab(i)=zz
 +
  C
  C Compute the energy of the ith side cbain
  C
  c        write (2,*) "xx",xx," yy",yy," zz",zz
 -        it=itype(i)
 +        it=iabs(itype(i))
          do j = 1,65
            x(j) = sc_parmin(j,it) 
          enddo
@@@ -4084,10 -4073,8 +4105,10 @@@ c     &   (dC_norm(j,i-1),j=1,3)," vbld
           dZZ_Ci1(k)=0.0d0
           dZZ_Ci(k)=0.0d0
           do j=1,3
 -           dZZ_Ci(k)=dZZ_Ci(k)-uzgrad(j,k,2,i-1)*dC_norm(j,i+nres)
 -           dZZ_Ci1(k)=dZZ_Ci1(k)-uzgrad(j,k,1,i-1)*dC_norm(j,i+nres)
 +           dZZ_Ci(k)=dZZ_Ci(k)-uzgrad(j,k,2,i-1)
 +     &      *dsign(1.0d0,dfloat(itype(i)))*dC_norm(j,i+nres)
 +           dZZ_Ci1(k)=dZZ_Ci1(k)-uzgrad(j,k,1,i-1)
 +     & *dsign(1.0d0,dfloat(itype(i)))*dC_norm(j,i+nres)
           enddo
            
           dXX_XYZ(k)=vbld_inv(i+nres)*(x_prime(k)-xx*dC_norm(k,i+nres))
@@@ -4323,19 -4310,14 +4344,19 @@@ c      lprn=.true
        etors=0.0D0
        do i=iphi_start,iphi_end
          if (itel(i-2).eq.0 .or. itel(i-1).eq.0) goto 1215
 +         if (iabs(itype(i)).eq.20) then
 +         iblock=2
 +         else
 +         iblock=1
 +         endif
          itori=itortyp(itype(i-2))
          itori1=itortyp(itype(i-1))
          phii=phi(i)
          gloci=0.0D0
  C Regular cosine and sine terms
 -        do j=1,nterm(itori,itori1)
 -          v1ij=v1(j,itori,itori1)
 -          v2ij=v2(j,itori,itori1)
 +        do j=1,nterm(itori,itori1,iblock)
 +          v1ij=v1(j,itori,itori1,iblock)
 +          v2ij=v2(j,itori,itori1,iblock)
            cosphi=dcos(j*phii)
            sinphi=dsin(j*phii)
            etors=etors+v1ij*cosphi+v2ij*sinphi
@@@ -4348,7 -4330,7 +4369,7 @@@ C          [v2 cos(phi/2)+v3 sin(phi/2)
  C
          cosphi=dcos(0.5d0*phii)
          sinphi=dsin(0.5d0*phii)
 -        do j=1,nlor(itori,itori1)
 +        do j=1,nlor(itori,itori1,iblock)
            vl1ij=vlor1(j,itori,itori1)
            vl2ij=vlor2(j,itori,itori1)
            vl3ij=vlor3(j,itori,itori1)
            gloci=gloci+vl1ij*(vl3ij*cosphi-vl2ij*sinphi)*pom
          enddo
  C Subtract the constant term
 -        etors=etors-v0(itori,itori1)
 +        etors=etors-v0(itori,itori1,iblock)
          if (lprn)
       &  write (iout,'(2(a3,2x,i3,2x),2i3,6f8.3/26x,6f8.3/)')
       &  restyp(itype(i-2)),i-2,restyp(itype(i-1)),i-1,itori,itori1,
 -     &  (v1(j,itori,itori1),j=1,6),(v2(j,itori,itori1),j=1,6)
 +     &  (v1(j,itori,itori1,1),j=1,6),(v2(j,itori,itori1,1),j=1,6)
          gloc(i-3,icg)=gloc(i-3,icg)+wtor*fact*gloci
  c       write (iout,*) 'i=',i,' gloc=',gloc(i-3,icg)
   1215   continue
@@@ -4424,14 -4406,12 +4445,14 @@@ c     lprn=.true
          phii1=phi(i+1)
          gloci1=0.0D0
          gloci2=0.0D0
 +        iblock=1
 +        if (iabs(itype(i+1)).eq.20) iblock=2
  C Regular cosine and sine terms
 -        do j=1,ntermd_1(itori,itori1,itori2)
 -          v1cij=v1c(1,j,itori,itori1,itori2)
 -          v1sij=v1s(1,j,itori,itori1,itori2)
 -          v2cij=v1c(2,j,itori,itori1,itori2)
 -          v2sij=v1s(2,j,itori,itori1,itori2)
 +       do j=1,ntermd_1(itori,itori1,itori2,iblock)
 +          v1cij=v1c(1,j,itori,itori1,itori2,iblock)
 +          v1sij=v1s(1,j,itori,itori1,itori2,iblock)
 +          v2cij=v1c(2,j,itori,itori1,itori2,iblock)
 +          v2sij=v1s(2,j,itori,itori1,itori2,iblock)
            cosphi1=dcos(j*phii)
            sinphi1=dsin(j*phii)
            cosphi2=dcos(j*phii1)
            gloci1=gloci1+j*(v1sij*cosphi1-v1cij*sinphi1)
            gloci2=gloci2+j*(v2sij*cosphi2-v2cij*sinphi2)
          enddo
 -        do k=2,ntermd_2(itori,itori1,itori2)
 +        do k=2,ntermd_2(itori,itori1,itori2,iblock)
            do l=1,k-1
 -            v1cdij = v2c(k,l,itori,itori1,itori2)
 -            v2cdij = v2c(l,k,itori,itori1,itori2)
 -            v1sdij = v2s(k,l,itori,itori1,itori2)
 -            v2sdij = v2s(l,k,itori,itori1,itori2)
 +            v1cdij = v2c(k,l,itori,itori1,itori2,iblock)
 +            v2cdij = v2c(l,k,itori,itori1,itori2,iblock)
 +            v1sdij = v2s(k,l,itori,itori1,itori2,iblock)
 +            v2sdij = v2s(l,k,itori,itori1,itori2,iblock)
              cosphi1p2=dcos(l*phii+(k-l)*phii1)
              cosphi1m2=dcos(l*phii-(k-l)*phii1)
              sinphi1p2=dsin(l*phii+(k-l)*phii1)
@@@ -4514,14 -4494,14 +4535,14 @@@ c   2 = Ca...Ca...Ca...S
  c   3 = SC...Ca...Ca...SCi
          gloci=0.0D0
          if (((intertyp.eq.3).and.((itype(i-2).eq.10).or.
 -     &      (itype(i-1).eq.10).or.(itype(i-2).eq.21).or.
 -     &      (itype(i-1).eq.21)))
 +     &      (itype(i-1).eq.10).or.(itype(i-2).eq.ntyp1).or.
 +     &      (itype(i-1).eq.ntyp1)))
       &    .or. ((intertyp.eq.1).and.((itype(i-2).eq.10)
 -     &     .or.(itype(i-2).eq.21)))
 +     &     .or.(itype(i-2).eq.ntyp1)))
       &    .or.((intertyp.eq.2).and.((itype(i-1).eq.10).or.
 -     &      (itype(i-1).eq.21)))) cycle
 -        if ((intertyp.eq.2).and.(i.eq.4).and.(itype(1).eq.21)) cycle
 -        if ((intertyp.eq.1).and.(i.eq.nres).and.(itype(nres).eq.21))
 +     &      (itype(i-1).eq.ntyp1)))) cycle
 +        if ((intertyp.eq.2).and.(i.eq.4).and.(itype(1).eq.ntyp1)) cycle
 +        if ((intertyp.eq.1).and.(i.eq.nres).and.(itype(nres).eq.ntyp1))
       & cycle
          do j=1,nterm_sccor(isccori,isccori1)
            v1ij=v1sccor(j,intertyp,isccori,isccori1)
@@@ -4910,6 -4890,7 +4931,7 @@@ C This subroutine calculates multi-bod
  C Set lprn=.true. for debugging
        lprn=.false.
        eturn6=0.0d0
+       ecorr6=0.0d0
  #ifdef MPL
        n_corr=0
        n_corr1=0
@@@ -5086,10 -5067,10 +5108,10 @@@ cd                write(2,*)'wcorr6',wc
  cd                write(2,*)'ijkl',i,j,i+1,j1 
                  if (wcorr6.gt.0.0d0 .and. (j.ne.i+4 .or. j1.ne.i+3
       &               .or. wturn6.eq.0.0d0))then
- cd                  write (iout,*) '******ecorr6: i,j,i+1,j1',i,j,i+1,j1
-                   ecorr6=ecorr6+eello6(i,j,i+1,j1,jj,kk)
- cd                write (iout,*) 'ecorr',ecorr,' ecorr5=',ecorr5,
- cd     &            'ecorr6=',ecorr6
+ c                  write (iout,*) '******ecorr6: i,j,i+1,j1',i,j,i+1,j1
+ c                  ecorr6=ecorr6+eello6(i,j,i+1,j1,jj,kk)
+ c                write (iout,*) 'ecorr',ecorr,' ecorr5=',ecorr5,
+ c     &            'ecorr6=',ecorr6, wcorr6
  cd                write (iout,'(4e15.5)') sred_geom,
  cd     &          dabs(eello4(i,j,i+1,j1,jj,kk)),
  cd     &          dabs(eello5(i,j,i+1,j1,jj,kk)),
@@@ -6412,7 -6393,7 +6434,7 @@@ c--------------------------------------
        include 'COMMON.GEO'
        logical swap
        double precision vv(2),pizda(2,2),auxmat(2,2),auxvec(2),
-      & auxvec1(2),auxvec2(1),auxmat1(2,2)
+      & auxvec1(2),auxvec2(2),auxmat1(2,2)
        logical lprn
        common /kutas/ lprn
  CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
        sigii(i)=0.0D0
        rr0(i)=0.0D0
        a0thet(i)=0.0D0
 -      do j=1,2
 -        athet(j,i)=0.0D0
 -        bthet(j,i)=0.0D0
 +        do j=1,2
 +         do ichir1=-1,1
 +          do ichir2=-1,1
 +          athet(j,i,ichir1,ichir2)=0.0D0
 +          bthet(j,i,ichir1,ichir2)=0.0D0
 +          enddo
 +         enddo
          enddo
        do j=0,3
          polthet(j,i)=0.0D0
        enddo
        nlob(ntyp1)=0
        dsc(ntyp1)=0.0D0
 -      do i=1,maxtor
 +      do i=-maxtor,maxtor
        itortyp(i)=0
 -      do j=1,maxtor
 -        do k=1,maxterm
 -          v1(k,j,i)=0.0D0
 -          v2(k,j,i)=0.0D0
 -          enddo
 +       do iblock=1,2
 +        do j=-maxtor,maxtor
 +          do k=1,maxterm
 +            v1(k,j,i,iblock)=0.0D0
 +            v2(k,j,i,iblock)=0.0D0
 +           enddo
 +         enddo
          enddo
        enddo
 +      do iblock=1,2
 +       do i=-maxtor,maxtor
 +        do j=-maxtor,maxtor
 +         do k=-maxtor,maxtor
 +          do l=1,maxtermd_1
 +            v1c(1,l,i,j,k,iblock)=0.0D0
 +            v1s(1,l,i,j,k,iblock)=0.0D0
 +            v1c(2,l,i,j,k,iblock)=0.0D0
 +            v1s(2,l,i,j,k,iblock)=0.0D0
 +          enddo !l
 +          do l=1,maxtermd_2
 +           do m=1,maxtermd_2
 +            v2c(m,l,i,j,k,iblock)=0.0D0
 +            v2s(m,l,i,j,k,iblock)=0.0D0
 +           enddo !m
 +          enddo !l
 +        enddo !k
 +       enddo !j
 +      enddo !i
 +      enddo !iblock
        do i=1,maxres
        itype(i)=0
        itel(i)=0
@@@ -181,6 -155,9 +181,9 @@@ C Initialize the bridge array
        ihpb(i)=0
        jhpb(i)=0
        enddo
+       do i=1,maxres
+         dyn_ss_mask(i)=.false.
+       enddo
  C
  C Initialize timing.
  C
@@@ -240,13 -217,9 +243,13 @@@ c--------------------------------------
        include 'COMMON.NAMES'
        include 'COMMON.FFIELD'
        data restyp /
 +     &'DD' ,'DPR','DLY','DAR','DHI','DAS','DGL','DSG','DGN','DSN','DTH',
 +     &'DYY','DAL','DTY','DTR','DVA','DLE','DIL','DPN','MED','DCY','ZER',
       &'CYS','MET','PHE','ILE','LEU','VAL','TRP','TYR','ALA','GLY','THR',
       &'SER','GLN','ASN','GLU','ASP','HIS','ARG','LYS','PRO','D'/
        data onelet /
 +     &'z','p','k','r','h','d','e','n','q','s','t','g',
 +     &'a','y','w','v','l','i','f','m','c','x',
       &'C','M','F','I','L','V','W','Y','A','G','T',
       &'S','Q','N','E','D','H','R','K','P','X'/
        data potname /'LJ','LJK','BP','GB','GBV'/
@@@ -321,6 -294,7 +324,7 @@@ cd   &   (ihpb(i),jhpb(i),i=1,nss
          do ii=1,nss
            if (ihpb(ii).eq.i+nres) then
              scheck=.true.
+             if (dyn_ss) go to 10
              jj=jhpb(ii)-nres
              goto 10
            endif
@@@ -18,7 -18,7 +18,7 @@@
        include 'COMMON.SCCOR'
        include 'COMMON.SCROT'
        character*1 t1,t2,t3
 -      character*1 onelett(4) /"G","A","P","D"/
 +      character*1 onelett(-2:2) /"p","a","G","A","P"/
        logical lprint
        dimension blower(3,3,maxlob)
        double precision ip,mp
@@@ -78,47 -78,12 +78,47 @@@ C Read the parameters of the probabilit
  C of the virtual-bond valence angles theta
  C
        do i=1,ntyp
 -        read (ithep,*) a0thet(i),(athet(j,i),j=1,2),(bthet(j,i),j=1,2)
 +        read (ithep,*) a0thet(i),(athet(j,i,1,1),j=1,2),
 +     &    (bthet(j,i,1,1),j=1,2)
          read (ithep,*) (polthet(j,i),j=0,3)
        read (ithep,*) (gthet(j,i),j=1,3)
        read (ithep,*) theta0(i),sig0(i),sigc0(i)
        sigc0(i)=sigc0(i)**2
        enddo
 +      do i=1,ntyp
 +      athet(1,i,1,-1)=athet(1,i,1,1)
 +      athet(2,i,1,-1)=athet(2,i,1,1)
 +      bthet(1,i,1,-1)=-bthet(1,i,1,1)
 +      bthet(2,i,1,-1)=-bthet(2,i,1,1)
 +      athet(1,i,-1,1)=-athet(1,i,1,1)
 +      athet(2,i,-1,1)=-athet(2,i,1,1)
 +      bthet(1,i,-1,1)=bthet(1,i,1,1)
 +      bthet(2,i,-1,1)=bthet(2,i,1,1)
 +      enddo
 +      do i=-ntyp,-1
 +      a0thet(i)=a0thet(-i)
 +      athet(1,i,-1,-1)=athet(1,-i,1,1)
 +      athet(2,i,-1,-1)=-athet(2,-i,1,1)
 +      bthet(1,i,-1,-1)=bthet(1,-i,1,1)
 +      bthet(2,i,-1,-1)=-bthet(2,-i,1,1)
 +      athet(1,i,-1,1)=athet(1,-i,1,1)
 +      athet(2,i,-1,1)=-athet(2,-i,1,1)
 +      bthet(1,i,-1,1)=-bthet(1,-i,1,1)
 +      bthet(2,i,-1,1)=bthet(2,-i,1,1)
 +      athet(1,i,1,-1)=-athet(1,-i,1,1)
 +      athet(2,i,1,-1)=athet(2,-i,1,1)
 +      bthet(1,i,1,-1)=bthet(1,-i,1,1)
 +      bthet(2,i,1,-1)=-bthet(2,-i,1,1)
 +      theta0(i)=theta0(-i)
 +      sig0(i)=sig0(-i)
 +      sigc0(i)=sigc0(-i)
 +       do j=0,3
 +        polthet(j,i)=polthet(j,-i)
 +       enddo
 +       do j=1,3
 +         gthet(j,i)=gthet(j,-i)
 +       enddo
 +      enddo
        close (ithep)
        if (lprint) then
  c       write (iout,'(a)') 
@@@ -154,8 -119,7 +154,8 @@@ c       endd
       & '   b1*10^1    ','    b2*10^1   '        
          do i=1,ntyp
            write(iout,'(a3,1h&,2x,5(f8.3,1h&))') restyp(i),
 -     &        a0thet(i),(100*athet(j,i),j=1,2),(10*bthet(j,i),j=1,2)
 +     &        a0thet(i),(100*athet(j,i,1,1),j=1,2),
 +     &        (10*bthet(j,i,1,1),j=1,2)
          enddo
        write (iout,'(/a/9x,5a/79(1h-))') 
       & 'Parameters of the expression for sigma(theta_c):',
       &  ntheterm3,nsingle,ndouble
        nntheterm=max0(ntheterm,ntheterm2,ntheterm3)
        read (ithep,*) (ithetyp(i),i=1,ntyp1)
 -      do i=1,maxthetyp
 -        do j=1,maxthetyp
 -          do k=1,maxthetyp
 -            aa0thet(i,j,k)=0.0d0
 +      do i=-ntyp1,-1
 +        ithetyp(i)=-ithetyp(-i)
 +      enddo
 +      do iblock=1,2
 +      do i=-maxthetyp,maxthetyp
 +        do j=-maxthetyp,maxthetyp
 +          do k=-maxthetyp,maxthetyp
 +            aa0thet(i,j,k,iblock)=0.0d0
              do l=1,ntheterm
 -              aathet(l,i,j,k)=0.0d0
 +              aathet(l,i,j,k,iblock)=0.0d0
              enddo
              do l=1,ntheterm2
                do m=1,nsingle
 -                bbthet(m,l,i,j,k)=0.0d0
 -                ccthet(m,l,i,j,k)=0.0d0
 -                ddthet(m,l,i,j,k)=0.0d0
 -                eethet(m,l,i,j,k)=0.0d0
 +                bbthet(m,l,i,j,k,iblock)=0.0d0
 +                ccthet(m,l,i,j,k,iblock)=0.0d0
 +                ddthet(m,l,i,j,k,iblock)=0.0d0
 +                eethet(m,l,i,j,k,iblock)=0.0d0
                enddo
              enddo
              do l=1,ntheterm3
                do m=1,ndouble
                  do mm=1,ndouble
 -                 ffthet(mm,m,l,i,j,k)=0.0d0
 -                 ggthet(mm,m,l,i,j,k)=0.0d0
 +                 ffthet(mm,m,l,i,j,k,iblock)=0.0d0
 +                 ggthet(mm,m,l,i,j,k,iblock)=0.0d0
                  enddo
                enddo
              enddo
            enddo
          enddo
        enddo
 -      do i=1,nthetyp
 -        do j=1,nthetyp
 -          do k=1,nthetyp
 -            read (ithep,'(3a)') res1,res2,res3
 -            read (ithep,*) aa0thet(i,j,k)
 -            read (ithep,*)(aathet(l,i,j,k),l=1,ntheterm)
 +      enddo
 +      do iblock=1,2
 +      do i=0,nthetyp
 +        do j=-nthetyp,nthetyp
 +          do k=-nthetyp,nthetyp
 +            read (ithep,'(6a)') res1
 +            read (ithep,*) aa0thet(i,j,k,iblock)
 +            read (ithep,*)(aathet(l,i,j,k,iblock),l=1,ntheterm)
              read (ithep,*)
 -     &       ((bbthet(lll,ll,i,j,k),lll=1,nsingle),
 -     &        (ccthet(lll,ll,i,j,k),lll=1,nsingle),
 -     &        (ddthet(lll,ll,i,j,k),lll=1,nsingle),
 -     &        (eethet(lll,ll,i,j,k),lll=1,nsingle),ll=1,ntheterm2)
 +     &       ((bbthet(lll,ll,i,j,k,iblock),lll=1,nsingle),
 +     &        (ccthet(lll,ll,i,j,k,iblock),lll=1,nsingle),
 +     &        (ddthet(lll,ll,i,j,k,iblock),lll=1,nsingle),
 +     &        (eethet(lll,ll,i,j,k,iblock),lll=1,nsingle)
 +     &        ,ll=1,ntheterm2)
              read (ithep,*)
 -     &      (((ffthet(llll,lll,ll,i,j,k),ffthet(lll,llll,ll,i,j,k),
 -     &         ggthet(llll,lll,ll,i,j,k),ggthet(lll,llll,ll,i,j,k),
 +     &      (((ffthet(llll,lll,ll,i,j,k,iblock),
 +     &         ffthet(lll,llll,ll,i,j,k,iblock),
 +     &         ggthet(llll,lll,ll,i,j,k,iblock),
 +     &         ggthet(lll,llll,ll,i,j,k,iblock),
       &         llll=1,lll-1),lll=2,ndouble),ll=1,ntheterm3)
            enddo
          enddo
        do i=1,nthetyp
          do j=1,nthetyp
            do l=1,ntheterm
 -            aathet(l,i,j,nthetyp+1)=aathet(l,i,j,1)
 -            aathet(l,nthetyp+1,i,j)=aathet(l,1,i,j)
 +            aathet(l,i,j,nthetyp+1,iblock)=0.0d0
 +            aathet(l,nthetyp+1,i,j,iblock)=0.0d0
            enddo
 -          aa0thet(i,j,nthetyp+1)=aa0thet(i,j,1)
 -          aa0thet(nthetyp+1,i,j)=aa0thet(1,i,j)
 +          aa0thet(i,j,nthetyp+1,iblock)=0.0d0
 +          aa0thet(nthetyp+1,i,j,iblock)=0.0d0
          enddo
          do l=1,ntheterm
 -          aathet(l,nthetyp+1,i,nthetyp+1)=aathet(l,1,i,1)
 +          aathet(l,nthetyp+1,i,nthetyp+1,iblock)=0.0d0
          enddo
 -        aa0thet(nthetyp+1,i,nthetyp+1)=aa0thet(1,i,1)
 +        aa0thet(nthetyp+1,i,nthetyp+1,iblock)=0.0d0
        enddo
 +      enddo
 +C Substitution for D aminoacids from symmetry.
 +      do iblock=1,2
 +      do i=-nthetyp,0
 +        do j=-nthetyp,nthetyp
 +          do k=-nthetyp,nthetyp
 +           aa0thet(i,j,k,iblock)=aa0thet(-i,-j,-k,iblock)
 +           do l=1,ntheterm
 +           aathet(l,i,j,k,iblock)=aathet(l,-i,-j,-k,iblock)
 +           enddo
 +           do ll=1,ntheterm2
 +            do lll=1,nsingle
 +            bbthet(lll,ll,i,j,k,iblock)=bbthet(lll,ll,-i,-j,-k,iblock)
 +            ccthet(lll,ll,i,j,k,iblock)=-ccthet(lll,ll,-i,-j,-k,iblock)
 +            ddthet(lll,ll,i,j,k,iblock)=ddthet(lll,ll,-i,-j,-k,iblock)
 +            eethet(lll,ll,i,j,k,iblock)=-eethet(lll,ll,-i,-j,-k,iblock)
 +            enddo
 +          enddo
 +          do ll=1,ntheterm3
 +           do lll=2,ndouble
 +            do llll=1,lll-1
 +            ffthet(llll,lll,ll,i,j,k,iblock)=
 +     &      ffthet(llll,lll,ll,-i,-j,-k,iblock)
 +            ffthet(lll,llll,ll,i,j,k,iblock)=
 +     &      ffthet(lll,llll,ll,-i,-j,-k,iblock)
 +            ggthet(llll,lll,ll,i,j,k,iblock)=
 +     &      -ggthet(llll,lll,ll,-i,-j,-k,iblock)
 +            ggthet(lll,llll,ll,i,j,k,iblock)=
 +     &      -ggthet(lll,llll,ll,-i,-j,-k,iblock)
 +            enddo !ll
 +           enddo  !lll  
 +          enddo   !llll
 +         enddo    !k
 +        enddo     !j
 +       enddo      !i
 +      enddo       !iblock
 +
  C
  C Control printout of the coefficients of virtual-bond-angle potentials
  C
                write (iout,'(//4a)')
       &         'Type ',onelett(i),onelett(j),onelett(k)
                write (iout,'(//a,10x,a)') " l","a[l]"
 -              write (iout,'(i2,1pe15.5)') 0,aa0thet(i,j,k)
 +              write (iout,'(i2,1pe15.5)') 0,aa0thet(i,j,k,iblock)
                write (iout,'(i2,1pe15.5)')
 -     &           (l,aathet(l,i,j,k),l=1,ntheterm)
 +     &           (l,aathet(l,i,j,k,iblock),l=1,ntheterm)
              do l=1,ntheterm2
                write (iout,'(//2h m,4(9x,a,3h[m,i1,1h]))')
       &          "b",l,"c",l,"d",l,"e",l
                do m=1,nsingle
                  write (iout,'(i2,4(1pe15.5))') m,
 -     &          bbthet(m,l,i,j,k),ccthet(m,l,i,j,k),
 -     &          ddthet(m,l,i,j,k),eethet(m,l,i,j,k)
 +     &          bbthet(m,l,i,j,k,iblock),ccthet(m,l,i,j,k,iblock),
 +     &          ddthet(m,l,i,j,k,iblock),eethet(m,l,i,j,k,iblock)
                enddo
              enddo
              do l=1,ntheterm3
                do m=2,ndouble
                  do n=1,m-1
                    write (iout,'(i1,1x,i1,4(1pe15.5))') n,m,
 -     &              ffthet(n,m,l,i,j,k),ffthet(m,n,l,i,j,k),
 -     &              ggthet(n,m,l,i,j,k),ggthet(m,n,l,i,j,k)
 +     &              ffthet(n,m,l,i,j,k,iblock),
 +     &              ffthet(m,n,l,i,j,k,iblock),
 +     &              ggthet(n,m,l,i,j,k,iblock),
 +     &              ggthet(m,n,l,i,j,k,iblock)
                  enddo
                enddo
              enddo
          enddo  
        bsc(1,i)=0.0D0
          read(irotam,*)(censc(k,1,i),k=1,3),((blower(k,l,1),l=1,k),k=1,3)
 +        censc(1,1,-i)=censc(1,1,i)
 +        censc(2,1,-i)=censc(2,1,i)
 +        censc(3,1,-i)=-censc(3,1,i)
        do j=2,nlob(i)
          read (irotam,*) bsc(j,i)
          read (irotam,*) (censc(k,j,i),k=1,3),
       &                                 ((blower(k,l,j),l=1,k),k=1,3)
 +        censc(1,j,-i)=censc(1,j,i)
 +        censc(2,j,-i)=censc(2,j,i)
 +        censc(3,j,-i)=-censc(3,j,i)
 +C BSC is amplitude of Gaussian
          enddo
        do j=1,nlob(i)
          do k=1,3
                enddo
              gaussc(k,l,j,i)=akl
              gaussc(l,k,j,i)=akl
 +             if (((k.eq.3).and.(l.ne.3))
 +     &        .or.((l.eq.3).and.(k.ne.3))) then
 +                gaussc(k,l,j,-i)=-akl
 +                gaussc(l,k,j,-i)=-akl
 +              else
 +                gaussc(k,l,j,-i)=akl
 +                gaussc(l,k,j,-i)=akl
 +              endif
              enddo
            enddo 
        enddo
        read (itorp,*) ntortyp
        read (itorp,*) (itortyp(i),i=1,ntyp)
        write (iout,*) 'ntortyp',ntortyp
 -      do i=1,ntortyp
 -      do j=1,ntortyp
 -        read (itorp,*) nterm(i,j),nlor(i,j)
 +      do iblock=1,2
 +      do i=-ntyp,-1
 +       itortyp(i)=-itortyp(-i)
 +      enddo
 +c      write (iout,*) 'ntortyp',ntortyp
 +      do i=0,ntortyp-1
 +        do j=-ntortyp+1,ntortyp-1
 +          read (itorp,*) 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,*) 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,*) 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
            enddo
 -        do k=1,nlor(i,j)
 -          read (itorp,*) kk,vlor1(k,i,j),vlor2(k,i,j),vlor3(k,i,j) 
 +          do k=1,nlor(i,j,iblock)
 +            read (itorp,*) kk,vlor1(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 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)
 +            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
  C
  C 6/23/01 Read parameters for double torsionals
  C
 -      do i=1,ntortyp
 -        do j=1,ntortyp
 -          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)') t1,t2,t3
              if (t1.ne.onelett(i) .or. t2.ne.onelett(j) 
       &        .or. t3.ne.onelett(k)) then
       &         i,j,k,t1,t2,t3
                 stop "Error in double torsional parameter file"
              endif
 -            read (itordp,*) ntermd_1(i,j,k),ntermd_2(i,j,k)
 -            read (itordp,*) (v1c(1,l,i,j,k),l=1,ntermd_1(i,j,k))
 -            read (itordp,*) (v1s(1,l,i,j,k),l=1,ntermd_1(i,j,k))
 -            read (itordp,*) (v1c(2,l,i,j,k),l=1,ntermd_1(i,j,k))
 -            read (itordp,*) (v1s(2,l,i,j,k),l=1,ntermd_1(i,j,k))
 -            read (itordp,*) ((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,*) 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,*) (v1c(1,l,i,j,k,iblock),l=1,
 +     &         ntermd_1(i,j,k,iblock))
 +            read (itordp,*) (v1s(1,l,i,j,k,iblock),l=1,
 +     &         ntermd_1(i,j,k,iblock))
 +            read (itordp,*) (v1c(2,l,i,j,k,iblock),l=1,
 +     &         ntermd_1(i,j,k,iblock))
 +            read (itordp,*) (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,*) ((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,*) 'Constants for double torsionals'
 -      do i=1,ntortyp
 -        do j=1,ntortyp 
 -          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
              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
          enddo
        enddo
 +      enddo
        endif
  #endif
  C Read of Side-chain backbone correlation parameters
        read (isccor,*) nsccortyp
        read (isccor,*) (isccortyp(i),i=1,ntyp)
  c      write (iout,*) 'ntortyp',ntortyp
 +      do i=-ntyp,-1
 +        isccortyp(i)=-isccortyp(-i)
 +      enddo
 +      iscprol=isccortyp(20)
        maxinter=3
  cc maxinter is maximum interaction sites
        do l=1,maxinter
            read (isccor,*)
       &nterm_sccor(i,j),nlor_sccor(i,j)
            v0ijsccor=0.0d0
 -          si=-1.0d0
 -
 +          v0ijsccor1=0.0d0
 +          v0ijsccor2=0.0d0
 +          v0ijsccor3=0.0d0 
 +         si=-1.0d0
 +          nterm_sccor(-i,j)=nterm_sccor(i,j)
 +          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,*) kk,v1sccor(k,l,i,j)
       &    ,v2sccor(k,l,i,j)
 +            if (j.eq.iscprol) then
 +             if (i.eq.isccortyp(10)) then
 +             v1sccor(k,l,i,-j)=v1sccor(k,l,i,j)
 +             v2sccor(k,l,i,-j)=-v2sccor(k,l,i,j)
 +             else
 +             v1sccor(k,l,i,-j)=v1sccor(k,l,i,j)*0.5d0
 +     &                        +v2sccor(k,l,i,j)*dsqrt(0.75d0)
 +            v2sccor(k,l,i,-j)=-v2sccor(k,l,i,j)*0.5d0
 +     &                        +v1sccor(k,l,i,j)*dsqrt(0.75d0)
 +              v1sccor(k,l,-i,-j)=v1sccor(k,l,i,j)
 +              v2sccor(k,l,-i,-j)=-v2sccor(k,l,i,j)
 +              v1sccor(k,l,-i,j)=v1sccor(k,l,i,-j)
 +              v2sccor(k,l,-i,j)=-v2sccor(k,l,i,-j)
 +              endif
 +             else
 +              if (i.eq.isccortyp(10)) then
 +              v1sccor(k,l,i,-j)=v1sccor(k,l,i,j)
 +              v2sccor(k,l,i,-j)=-v2sccor(k,l,i,j)
 +              else
 +               if (j.eq.isccortyp(10)) then
 +             v1sccor(k,l,-i,j)=v1sccor(k,l,i,j)
 +             v2sccor(k,l,-i,j)=-v2sccor(k,l,i,j)
 +               else
 +             v1sccor(k,l,i,-j)=-v1sccor(k,l,i,j)
 +             v2sccor(k,l,i,-j)=-v2sccor(k,l,i,j)
 +             v1sccor(k,l,-i,-j)=v1sccor(k,l,i,j)
 +             v2sccor(k,l,-i,-j)=-v2sccor(k,l,i,j)
 +             v1sccor(k,l,-i,j)=v1sccor(k,l,i,-j)
 +             v2sccor(k,l,-i,j)=-v2sccor(k,l,i,-j)
 +                endif
 +               endif
 +            endif
              v0ijsccor=v0ijsccor+si*v1sccor(k,l,i,j)
 +            v0ijsccor1=v0ijsccor+si*v1sccor(k,l,-i,j)
 +            v0ijsccor2=v0ijsccor+si*v1sccor(k,l,i,-j)
 +            v0ijsccor3=v0ijsccor+si*v1sccor(k,l,-i,-j)
              si=-si
            enddo
            do k=1,nlor_sccor(i,j)
              v0ijsccor=v0ijsccor+vlor1sccor(k,i,j)/
       &(1+vlor3sccor(k,i,j)**2)
            enddo
 -          v0sccor(i,j)=v0ijsccor
 +           v0sccor(l,i,j)=v0ijsccor
 +           v0sccor(l,-i,j)=v0ijsccor1
 +           v0sccor(l,i,-j)=v0ijsccor2
 +           v0sccor(l,-i,-j)=v0ijsccor3
          enddo
        enddo
        enddo
@@@ -719,7 -524,7 +719,7 @@@ C 9/18/99 (AL) Read coefficients of th
  C         interaction energy of the Gly, Ala, and Pro prototypes.
  C
        read (ifourier,*) nloctyp
 -      do i=1,nloctyp
 +      do i=0,nloctyp-1
          read (ifourier,*)
          read (ifourier,*) (b(ii,i),ii=1,13)
          if (lprint) then
          endif
          B1(1,i)  = b(3,i)
          B1(2,i)  = b(5,i)
 +        B1(1,-i) = b(3,i)
 +        B1(2,-i) = -b(5,i)
          B1tilde(1,i) = b(3,i)
          B1tilde(2,i) =-b(5,i) 
 +        B1tilde(1,-i) =-b(3,i)
 +        B1tilde(2,-i) =b(5,i)
          B2(1,i)  = b(2,i)
          B2(2,i)  = b(4,i)
 +        B2(1,-i)  =b(2,i)
 +        B2(2,-i)  =-b(4,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)
 +        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)
          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)
          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)
          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)
          EE(1,1,i)= b(10,i)+b(11,i)
          EE(2,2,i)=-b(10,i)+b(11,i)
          EE(2,1,i)= b(12,i)-b(13,i)
          EE(1,2,i)= b(12,i)+b(13,i)
 +        EE(1,1,-i)= b(10,i)+b(11,i)
 +        EE(2,2,-i)=-b(10,i)+b(11,i)
 +        EE(2,1,-i)=-b(12,i)+b(13,i)
 +        EE(1,2,-i)=-b(12,i)-b(13,i)
        enddo
        if (lprint) then
        do i=1,nloctyp
  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
@@@ -1042,19 -821,12 +1042,12 @@@ 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
-       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      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
        return
        end
@@@ -218,10 -218,19 +218,19 @@@ c          call flush(iout
              call xdrfint_(ixdrf, nss, iret)
              if (iret.eq.0) goto 101
              do j=1,nss
-               call xdrfint_(ixdrf, ihpb(j), iret)
-               if (iret.eq.0) goto 101
-               call xdrfint_(ixdrf, jhpb(j), iret)
-               if (iret.eq.0) goto 101
+ cc              if (dyn_ss) then
+ cc                call xdrfint_(ixdrf, idssb(j), iret)
+ cc                if (iret.eq.0) goto 101
+ cc                call xdrfint_(ixdrf, jdssb(j), iret)
+ cc                if (iret.eq.0) goto 101
+ cc             idssb(j)=idssb(j)-nres
+ cc             jdssb(j)=jdssb(j)-nres
+ cc              else
+                 call xdrfint_(ixdrf, ihpb(j), iret)
+                 if (iret.eq.0) goto 101
+                 call xdrfint_(ixdrf, jhpb(j), iret)
+                 if (iret.eq.0) goto 101
+ cc              endif
              enddo
              call xdrffloat_(ixdrf,reini,iret)
              if (iret.eq.0) goto 101
@@@ -243,10 -252,20 +252,20 @@@ c            write (iout,*) "nss",ns
              call flush(iout)
              if (iret.eq.0) goto 101
              do k=1,nss
-               call xdrfint(ixdrf, ihpb(k), iret)
-               if (iret.eq.0) goto 101
-               call xdrfint(ixdrf, jhpb(k), iret)
-               if (iret.eq.0) goto 101
+ cc              if (dyn_ss) then
+ cc                call xdrfint(ixdrf, idssb(k), iret)
+ cc                if (iret.eq.0) goto 101
+ cc               call xdrfint(ixdrf, jdssb(k), iret)
+ cc                if (iret.eq.0) goto 101
+ cc                idssb(k)=idssb(k)-nres
+ cc                jdssb(k)=jdssb(k)-nres
+ cc              write(iout,*) "TUTU", idssb(k),jdssb(k)
+ cc              else
+                 call xdrfint(ixdrf, ihpb(k), iret)
+                 if (iret.eq.0) goto 101
+                 call xdrfint(ixdrf, jhpb(k), iret)
+                 if (iret.eq.0) goto 101
+ cc              endif
              enddo
              call xdrffloat(ixdrf,reini,iret)
              if (iret.eq.0) goto 101
              if (iret.eq.0) goto 101
  #endif
              energy(jj+1)=reini
-             entfac(jj+1)=refree
+ cc         write(iout,*) 'reini=', reini, jj+1
+             entfac(jj+1)=dble(refree)
+ cc         write(iout,*) 'refree=', refree,jj+1
              rmstb(jj+1)=rmsdev
              do k=1,nres
                do l=1,3
@@@ -397,8 -418,7 +418,8 @@@ c--------------------------------------
        enddo
        do j=nnt,nct
          itj=itype(j)
 -        if (itype(j).ne.10 .and. (vbld(nres+j)-dsc(itj)).gt.2.0d0) then
 +        if (itype(j).ne.10 .and. (vbld(nres+j)-dsc(iabs(itj))).gt.2.0d0)
 +     & then
            write (iout,*) "Conformation",jjj,jj+1
            write (iout,*) "Bad CA-SC bond length",j," ",vbld(nres+j)
            write (iout,*) "The Cartesian geometry is:"
          write (iout,*) "Reading binary file, record",iii," ii",ii
          call flush(iout)
  #endif
+         if (dyn_ss) then
+         read(icbase,rec=iii) ((allcart(j,i,ij),j=1,3),i=1,nres),
+      &    ((allcart(j,i,ij),j=1,3),i=nnt+nres,nct+nres),
+ c     &    nss_all(ij),(ihpb_all(i,ij),jhpb_all(i,ij),i=1,nss),
+      &    entfac(ii),rmstb(ii)
+         else
          read(icbase,rec=iii) ((allcart(j,i,ij),j=1,3),i=1,nres),
       &    ((allcart(j,i,ij),j=1,3),i=nnt+nres,nct+nres),
       &    nss_all(ij),(ihpb_all(i,ij),jhpb_all(i,ij),i=1,nss),
       &    entfac(ii),rmstb(ii)
+         endif
  #ifdef DEBUG
          write (iout,*) ii,iii,ij,entfac(ii)
          write (iout,'(8f10.5)') ((allcart(j,i,ij),j=1,3),i=1,nres)
          write (iout,*) "Writing binary file, record",iii," ii",ii
          call flush(iout)
  #endif
+        if (dyn_ss) then
+         write(unit_out,rec=iii) ((allcart(j,i,ij),j=1,3),i=1,nres),
+      &    ((allcart(j,i,ij),j=1,3),i=nnt+nres,nct+nres),
+ c     &    nss_all(ij),(ihpb_all(i,ij),jhpb_all(i,ij),i=1,nss_all(ij))
+      &    entfac(ii),rmstb(ii)
+         else
          write(unit_out,rec=iii) ((allcart(j,i,ij),j=1,3),i=1,nres),
       &    ((allcart(j,i,ij),j=1,3),i=nnt+nres,nct+nres),
       &    nss_all(ij),(ihpb_all(i,ij),jhpb_all(i,ij),i=1,nss_all(ij)),
       &    entfac(ii),rmstb(ii)
+         endif
  #ifdef DEBUG
          write (iout,'(8f10.5)') ((allcart(j,i,ij),j=1,3),i=1,nres)
          write (iout,'(8f10.4)') ((allcart(j,i,ij),j=1,3),i=nnt+nres,
@@@ -30,6 -30,7 +30,7 @@@
        refstr=(index(controlcard,'REFSTR').gt.0)
        write (iout,*) "REFSTR",refstr
        pdbref=(index(controlcard,'PDBREF').gt.0)
+       dyn_ss=(index(controlcard,'DYN_SS').gt.0)
        iscode=index(controlcard,'ONE_LETTER')
        tree=(index(controlcard,'MAKE_TREE').gt.0)
        min_var=(index(controlcard,'MINVAR').gt.0)
@@@ -125,9 -126,44 +126,44 @@@ C Read weights of the subsequent energ
        call reada(weightcard,'SCALSCP',scalscp,1.0d0)
        call reada(weightcard,'CUTOFF',cutoff_corr,7.0d0)
        call reada(weightcard,'DELT_CORR',delt_corr,0.5d0)
+       call reada(weightcard,'WSCCOR',wsccor,1.0D0)
+       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)
        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)
+       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,'(a,f10.2)') 'S-S depth: ',ss_depth
+       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
+       write (iout,'(2(a,f10.2))') 'ht:',ht,' eps:', eps(1,1)
        if (wcorr4.gt.0.0d0) wcorr=wcorr4
        weights(1)=wsc
        weights(2)=wscp
        weights(18)=scal14
        write (iout,10) wsc,wscp,welec,wvdwpp,wbond,wang,wscloc,wtor,
       &  wtor_d,wstrain,wel_loc,wcorr,wcorr5,wcorr6,wturn3,
-      &  wturn4,wturn6
+      &  wturn4,wturn6,wsccor
     10 format (/'Energy-term weights (unscaled):'//
       & 'WSCC=   ',f10.6,' (SC-SC)'/
       & 'WSCP=   ',f10.6,' (SC-p)'/
       & 'WCORR6= ',f10.6,' (multi-body 6th order)'/
       & 'WTURN3= ',f10.6,' (turns, 3rd order)'/
       & 'WTURN4= ',f10.6,' (turns, 4th order)'/
-      & 'WTURN6= ',f10.6,' (turns, 6th order)')
+      & 'WTURN6= ',f10.6,' (turns, 6th order)'/
+      & 'WSCCOR= ',f10.6,' (SC-backbone torsinal correalations)')
        if (wcorr4.gt.0.0d0) then
          write (iout,'(/2a/)') 'Local-electrostatic type correlation ',
       &   'between contact pairs of peptide groups'
@@@ -208,15 -245,15 +245,15 @@@ C Convert sequence to numeric cod
  
        do i=1,nres
  #ifdef PROCOR
 -        if (itype(i).eq.21 .or. itype(i+1).eq.21) then
 +        if (itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1) then
  #else
 -        if (itype(i).eq.21) then
 +        if (itype(i).eq.ntyp1) then
  #endif
            itel(i)=0
  #ifdef PROCOR
 -        else if (itype(i+1).ne.20) then
 +        else if (iabs(itype(i+1)).ne.20) then
  #else
 -        else if (itype(i).ne.20) then
 +        else if (iabs(itype(i)).ne.20) then
  #endif
            itel(i)=1
          else
        nnt=1
        nct=nres
        print *,'NNT=',NNT,' NCT=',NCT
 -      if (itype(1).eq.21) nnt=2
 -      if (itype(nres).eq.21) nct=nct-1
 +      if (itype(1).eq.ntyp1) nnt=2
 +      if (itype(nres).eq.ntyp1) nct=nct-1
        if (nstart.lt.nnt) nstart=nnt
        if (nend.gt.nct .or. nend.eq.0) nend=nct
        write (iout,*) "nstart",nstart," nend",nend
@@@ -429,6 -466,22 +466,22 @@@ c          forcon(i)=fb
          enddo
        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.
+ c          write(iout,*) i,iss(i),dyn_ss_mask(iss(i)),"ATU"
+           enddo
+       endif
+       print *, "Leaving brigde read"
        return
        end
  c----------------------------------------------------------------------------
@@@ -97,6 -97,8 +97,8 @@@ set(UNRES_MDM_SRC
  
  if (Fortran_COMPILER_NAME STREQUAL "ifort")
    set(UNRES_MDM_SRC0 ${UNRES_MDM_SRC0} prng.f ) 
+ elseif(Fortran_COMPILER_NAME STREQUAL "mpif90")
+   set(UNRES_MDM_SRC0 ${UNRES_MDM_SRC0} prng.f )
  else()
    set(UNRES_MDM_SRC0 ${UNRES_MDM_SRC0} prng_32.F )
  endif (Fortran_COMPILER_NAME STREQUAL "ifort")
@@@ -174,14 -176,12 +176,12 @@@ if (Fortran_COMPILER_NAME STREQUAL "ifo
    set(FFLAGS0 "-ip -w" ) 
    set(FFLAGS1 "-w -g -d2 -CA -CB" ) 
    set(FFLAGS2 "-w -g -00 ")
-   #set(FFLAGS3 "-c -w -O3 -ipo -ipo_obj -opt_report" )
    set(FFLAGS3 "-w -ipo " )
  elseif (Fortran_COMPILER_NAME STREQUAL "gfortran")
-   set(FFLAGS0 "-I. " ) 
-   set(FFLAGS1 "-g -I. " ) 
-   set(FFLAGS2 "-I. ")
-   #set(FFLAGS3 "-c -w -O3 -ipo -ipo_obj -opt_report" )
-   set(FFLAGS3 "-I. " )
+   set(FFLAGS0 "-std=legacy -I. " ) 
+   set(FFLAGS1 "-std=legacy -g -I. " ) 
+   set(FFLAGS2 "-std=legacy -I. ")
+   set(FFLAGS3 "-std=legacy -I. " )
  endif (Fortran_COMPILER_NAME STREQUAL "ifort")
  
  
@@@ -203,14 -203,14 +203,14 @@@ set_property(SOURCE ${UNRES_MDM_SRC3} P
  #=========================================
  if(UNRES_MD_FF STREQUAL "GAB" )
    # set preprocesor flags   
 -  set(CPPFLAGS "PROCOR -DUNRES -DISNAN -DSPLITELE -DLANG0 -DCRYST_BOND -DCRYST_THETA -DCRYST_SC" )
 +  set(CPPFLAGS "PROCOR -DUNRES -DISNAN -DSPLITELE -DLANG0 -DCRYST_BOND -DCRYST_THETA -DCRYST_SC  -DSCCORPDB" )
  
  #=========================================
  #  Settings for E0LL2Y force field
  #=========================================
  elseif(UNRES_MD_FF STREQUAL "E0LL2Y")
    # set preprocesor flags   
 -  set(CPPFLAGS "PROCOR -DUNRES -DISNAN -DSPLITELE -DLANG0" )
 +  set(CPPFLAGS "PROCOR -DUNRES -DISNAN -DSPLITELE -DLANG0 -DSCCORPDB" )
  endif(UNRES_MD_FF STREQUAL "GAB")
  
  
@@@ -235,6 -235,9 +235,9 @@@ elseif (Fortran_COMPILER_NAME STREQUAL 
  elseif (Fortran_COMPILER_NAME STREQUAL "gfortran")
    # Add old gfortran flags
    set(CPPFLAGS "${CPPFLAGS} -DG77") 
+ else (Fortran_COMPILER_NAME STREQUAL "ifort")
+   # Default preprocessor flags
+   set(CPPFLAGS "${CPPFLAGS} -DPGI")
  endif (Fortran_COMPILER_NAME STREQUAL "ifort")
  
  
@@@ -272,25 -275,41 +275,41 @@@ else(UNRES_WITH_MPI
  endif(UNRES_WITH_MPI)  
  
  #=========================================
- # cinfo.f stupid workaround for cmake
- #  - shame on me ]:)
+ # cinfo.f workaround for cmake
  #=========================================
- set_property(SOURCE compinfo.c PROPERTY CMAKE_C_FLAGS "-c" )
- add_executable(compinfo-mdm compinfo.c)
- set_target_properties(compinfo-mdm PROPERTIES OUTPUT_NAME compinfo)
- set(UNRES_CINFO_DIR "${CMAKE_CURRENT_BINARY_DIR}" )
- add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/cinfo.f 
-       COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/cinfo.f ${CMAKE_CURRENT_BINARY_DIR}/cinfo.f
-       COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/COMMON.IOUNITS ${CMAKE_CURRENT_BINARY_DIR}/COMMON.IOUNITS 
-       COMMAND ${CMAKE_CURRENT_BINARY_DIR}/compinfo | true     
-       DEPENDS compinfo-mdm )
- set_property(SOURCE ${UNRES_CINFO_DIR}/cinfo.f PROPERTY COMPILE_FLAGS ${FFLAGS0} )
+ # get the current date  
+ TODAY(DATE)
+ # generate cinfo.f
+ set(CINFO "${CMAKE_CURRENT_BINARY_DIR}/cinfo.f")
+ FILE(WRITE ${CINFO}
+ "C CMake generated file
+        subroutine cinfo
+        include 'COMMON.IOUNITS'
+        write(iout,*)'++++ Compile info ++++'
+        write(iout,*)'Version ${UNRES_MAJOR}.${UNRES_MINOR} build ${UNRES_PATCH}'
+ ")
+ CINFO_FORMAT(${CINFO} "Compiled" "${DATE}" )
+ CINFO_FORMAT(${CINFO} "Compiled by" "$ENV{USER}@$ENV{HOST}" )
+ CINFO_FORMAT(${CINFO} "OS name:" "${CMAKE_SYSTEM_NAME}" )
+ CINFO_FORMAT(${CINFO} "OS release:" "${CMAKE_SYSTEM}" )
+ CINFO_FORMAT(${CINFO} "Fortran Compiler:" "${CMAKE_Fortran_COMPILER}" )
+ CINFO_FORMAT(${CINFO} "MD Force field:" "${UNRES_MD_FF}" )
+ CINFO_FORMAT(${CINFO} "CPPFLAGS =" "${CPPFLAGS}")
+ FILE(APPEND ${CINFO} 
+ "       write(iout,*)'++++ End of compile info ++++'  
+        return 
+        end ")
+ # add include path
+ set_property(SOURCE ${CMAKE_CURRENT_BINARY_DIR}/cinfo.f PROPERTY COMPILE_FLAGS "${FFLAGS0} -I${CMAKE_CURRENT_SOURCE_DIR}")
  
  #=========================================
  # Set full unres MD-M sources
  #=========================================
- set(UNRES_MDM_SRCS ${UNRES_MDM_SRC0} ${UNRES_MDM_SRC3} ${UNRES_CINFO_DIR}/cinfo.f proc_proc.c )
+ set(UNRES_MDM_SRCS ${UNRES_MDM_SRC0} ${UNRES_MDM_SRC3} ${CMAKE_CURRENT_BINARY_DIR}/cinfo.f proc_proc.c )
  
  #=========================================
  # Build the binary
@@@ -1,15 -1,15 +1,16 @@@
        integer nres,nsup,nstart_sup,nz_start,nz_end,iz_sc,
       &  nres0,nstart_seq,chain_length,iprzes,tabperm,nperm
        double precision c,dc,dc_old,d_c_work,xloc,xrot,dc_norm,t,r,
 -     & prod,rt,dc_work,cref,crefjlee,chain_rep
 +     & prod,rt,dc_work,cref,crefjlee,chain_rep,dc_norm2
        common /chain/ c(3,maxres2+2),dc(3,0:maxres2),dc_old(3,0:maxres2),
       & xloc(3,maxres),xrot(3,maxres),dc_norm(3,0:maxres2),
 +     & dc_norm2(3,0:maxres2),
       & dc_work(MAXRES6),nres,nres0
        common /rotmat/ t(3,3,maxres),r(3,3,maxres),prod(3,3,maxres),
       &                rt(3,3,maxres) 
        common /refstruct/ cref(3,maxres2+2,maxperm),
-      &crefjlee(3,maxres2+2),
-      & nsup,nstart_sup,nstart_seq,chain_rep(3,maxres2+2,maxsym),
+      & crefjlee(3,maxres2+2),
+      & chain_rep(3,maxres2+2,maxsym),
+      & nsup,nstart_sup,nstart_seq,
       & chain_length,iprzes,tabperm(maxperm,maxsym),nperm
        common /from_zscore/ nz_start,nz_end,iz_sc
@@@ -209,7 -209,7 +209,7 @@@ c Variable time step algorithm
            enddo
          enddo
          do i=nnt,nct
 -          if (itype(i).ne.10 .and. itype(i).ne.21) then
 +          if (itype(i).ne.10 .and. itype(i).ne.ntyp1) then
              do j=1,3
                ind=ind+1
                v_work(ind)=d_t(j,i+nres)
@@@ -955,7 -955,7 +955,7 @@@ c  forces)
          enddo
        enddo
        do i=nnt,nct
 -        if (itype(i).ne.10 .and. itype(i).ne.21) then
 +        if (itype(i).ne.10 .and. itype(i).ne.ntyp1) then
            inres=i+nres
            do j=1,3
              d_t(j,inres)=d_t(j,inres)+0.5d0*d_a(j,inres)*d_time
@@@ -1005,7 -1005,7 +1005,7 @@@ c Applying velocity Verlet algorithm - 
          enddo
        enddo
        do i=nnt,nct
 -        if (itype(i).ne.10 .and. itype(i).ne.21) then
 +        if (itype(i).ne.10 .and. itype(i).ne.ntyp1) then
            inres=i+nres
            do j=1,3    
              adt=d_a_old(j,inres)*d_time
@@@ -1049,7 -1049,7 +1049,7 @@@ c  Step 2 of the velocity Verlet algori
          enddo
        enddo
        do i=nnt,nct
 -        if (itype(i).ne.10 .and. itype(i).ne.21) then
 +        if (itype(i).ne.10 .and. itype(i).ne.ntyp1) then
            inres=i+nres
            do j=1,3
              d_t(j,inres)=d_t_new(j,inres)+0.5d0*d_a(j,inres)*d_time
          ind=ind+3
        enddo
        do i=nnt,nct
 -        if (itype(i).ne.10 .and. itype(i).ne.21) then
 +        if (itype(i).ne.10 .and. itype(i).ne.ntyp1) then
            inres=i+nres
            do j=1,3    
              adt=(d_a_old(j,inres)+d_af_work(ind+j))*d_time
          ind=ind+3
        enddo
        do i=nnt,nct
 -        if (itype(i).ne.10 .and. itype(i).ne.21) then
 +        if (itype(i).ne.10 .and. itype(i).ne.ntyp1) then
            inres=i+nres
            do j=1,3
              d_t(j,inres)=d_t_new(j,inres)+(0.5d0*(d_a(j,inres)
@@@ -1260,7 -1260,6 +1260,7 @@@ c            accel(j)=aux(j)+0.5d0*(d_a
  c            if (dabs(accel(j)).gt.amax) amax=dabs(accel(j))
              if (dabs(accel(j)).gt.dabs(accel_old(j))) then
                dacc=dabs(accel(j)-accel_old(j))
 +c              write (iout,*) i,dacc
                if (dacc.gt.amax) amax=dacc
              endif
            enddo
@@@ -1279,7 -1278,7 +1279,7 @@@ c        accel(j)=aux(j
          enddo
        endif
        do i=nnt,nct
 -        if (itype(i).ne.10 .and. itype(i).ne.21) then
 +        if (itype(i).ne.10 .and. itype(i).ne.ntyp1) then
            do j=1,3 
  c            accel(j)=accel(j)+d_a(j,i+nres)-d_a_old(j,i+nres)
              accel_old(j)=accel_old(j)+d_a_old(j,i+nres)
  c          if (dabs(accel(j)).gt.amax) amax=dabs(accel(j))
            if (dabs(accel(j)).gt.dabs(accel_old(j))) then
              dacc=dabs(accel(j)-accel_old(j))
 +c            write (iout,*) "side-chain",i,dacc
              if (dacc.gt.amax) amax=dacc
            endif
          enddo
@@@ -1333,7 -1331,7 +1333,7 @@@ c            write (iout,*) "back",i,j,
            enddo
          endif
  c Side chains
 -        if (itype(i).ne.10 .and. itype(i).ne.21) then
 +        if (itype(i).ne.10 .and. itype(i).ne.ntyp1) then
            do j=1,3 
              epdriftij=
       &       dabs((d_a(j,i+nres)-d_a_old(j,i+nres))*gxcart(j,i))
@@@ -1380,7 -1378,7 +1380,7 @@@ c      write(iout,*) "fact", fac
          enddo
        enddo
        do i=nnt,nct
 -        if (itype(i).ne.10 .and. itype(i).ne.21) then
 +        if (itype(i).ne.10 .and. itype(i).ne.ntyp1) then
            inres=i+nres
            do j=1,3
              d_t(j,inres)=fact*d_t(j,inres)
@@@ -1435,8 -1433,7 +1435,8 @@@ c if the friction coefficients do not d
            stdforcp(i)=stdfp*dsqrt(gamp)
          enddo
          do i=nnt,nct
 -          stdforcsc(i)=stdfsc(itype(i))*dsqrt(gamsc(itype(i)))
 +          stdforcsc(i)=stdfsc(iabs(itype(i)))
 +     &                *dsqrt(gamsc(iabs(itype(i))))
          enddo
        endif
  c Open the pdb file for snapshotshots
@@@ -1608,7 -1605,7 +1608,7 @@@ c Removing the velocity of the center o
        call chainbuild_cart
        call kinetic(EK)
        if (tbf) then
-         call verlet_bath(EK)
+         call verlet_bath
        endif     
        kinetic_T=2.0d0/(dimen3*Rb)*EK
        if(me.eq.king.or..not.out1file)then
@@@ -1827,7 -1824,7 +1827,7 @@@ c Transfer to the d_t vecto
          enddo
        enddo
        do i=nnt,nct
 -        if (itype(i).ne.10 .and. itype(i).ne.21) then
 +        if (itype(i).ne.10 .and. itype(i).ne.ntyp1) then
            do j=1,3
              ind=ind+1
              d_t(j,i+nres)=d_t_work(ind)
@@@ -2056,7 -2053,7 +2056,7 @@@ c      endd
          ind=ind+3
        enddo
        do i=nnt,nct
 -        if (itype(i).ne.10 .and. itype(i).ne.21) then
 +        if (itype(i).ne.10 .and. itype(i).ne.ntyp1) then
            do j=1,3
              dc_work(ind+j)=dc_old(j,i+nres)
              d_t_work(ind+j)=d_t_old(j,i+nres)
          ind=ind+3
        enddo
        do i=nnt,nct
 -        if (itype(i).ne.10 .and. itype(i).ne.21) then
 +        if (itype(i).ne.10 .and. itype(i).ne.ntyp1) then
            inres=i+nres
            do j=1,3
              d_t(j,inres)=d_t_work(ind+j)
@@@ -2322,7 -2319,7 +2322,7 @@@ c      endd
          ind=ind+3
        enddo
        do i=nnt,nct
 -        if (itype(i).ne.10 .and. itype(i).ne.21) then
 +        if (itype(i).ne.10 .and. itype(i).ne.ntyp1) then
            do j=1,3
              dc_work(ind+j)=dc_old(j,i+nres)
              d_t_work(ind+j)=d_t_old(j,i+nres)
          ind=ind+3
        enddo
        do i=nnt,nct
 -        if (itype(i).ne.10 .and. itype(i).ne.21) then
 +        if (itype(i).ne.10 .and. itype(i).ne.ntyp1) then
            inres=i+nres
            do j=1,3
              dc(j,inres)=dc_work(ind+j)
@@@ -2432,7 -2429,7 +2432,7 @@@ c          ddt2=ddt2+vrand_mat2(i,j)*st
          ind=ind+3
        enddo
        do i=nnt,nct
 -        if (itype(i).ne.10 .and. itype(i).ne.21) then
 +        if (itype(i).ne.10 .and. itype(i).ne.ntyp1) then
            inres=i+nres
            do j=1,3
              d_t(j,inres)=d_t_work(ind+j)
@@@ -1,9 -1,7 +1,9 @@@
  CPPFLAGS = -DLINUX -DUNRES -DMP -DMPI \
             -DPGI -DSPLITELE -DISNAN -DAMD64 \
             -DPROCOR -DLANG0 \
 -      -DCRYST_BOND -DCRYST_THETA -DCRYST_SC 
 +           -DSCCORPDB         
 +#          -DCRYST_BOND -DCRYST_THETA -DCRYST_SC 
 +#     -DSCCORPDB 
  ## -DPROCOR
  ## -DMOMENT
  #-DCO_BIAS
@@@ -16,9 -14,8 +16,9 @@@ INSTALL_DIR = /users/software/mpich-1.2
  #FC= /usr/local/opt/intel/compiler60/ia32/bin/ifc
  FC= ifort
  
 -OPT =  -O3 -ip -w
 -
 +#OPT =  -O3 -ip -w
 +OPT = -g -CB
 +#OPT = -g
  CFLAGS = -DSGI -c
  
  FFLAGS = -c ${OPT} -I$(INSTALL_DIR)/include
@@@ -26,10 -23,10 +26,10 @@@ FFLAGS1 = -c -w -g -d2 -CA -CB -I$(INST
  FFLAGS2 = -c -w -O0 -I$(INSTALL_DIR)/include
  FFLAGSE = -c -w -O3 -ipo -ipo_obj  -opt_report -I$(INSTALL_DIR)/include
  
 -BIN = ../../../bin/unres/MD/unres_Tc_procor_oldparm_em64-D-symetr.exe
 -#LIBS = -L$(INSTALL_DIR)/lib_pgi -lmpich ../../lib/xdrf/libxdrf.a
 +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 xdrf/libxdrf.a -g -d2 -CA -CB
 +LIBS = -L$(INSTALL_DIR)/lib -lmpich ../../lib/xdrf_em64/libxdrf.a -g -d2 -CA -CB
  
  ARCH = LINUX
  PP = /lib/cpp -P
@@@ -63,7 -60,8 +63,8 @@@ unres: ${object} proc_proc.
        cc -o compinfo compinfo.c 
        ./compinfo | true
        ${FC} ${FFLAGS} cinfo.f
-       ${FC} ${OPT} -Wl,-M ${object} proc_proc.o cinfo.o ${LIBS}  -o ${BIN}
+       ${FC} ${OPT} ${object} proc_proc.o cinfo.o ${LIBS}  -o ${BIN}
+ #${FC} ${OPT} -Wl,-M ${object} proc_proc.o cinfo.o ${LIBS}  -o ${BIN}
  
  
  clean:
@@@ -131,3 -129,12 +132,12 @@@ lagrangian_lesyng.o : lagrangian_lesyng
  
  proc_proc.o: proc_proc.c
        ${CC} ${CFLAGS} proc_proc.c
+ add.o: add.f
+       ${FC} ${FFLAGS2} add.f
+ blas.o: blas.f
+       ${FC} ${FFLAGS2} blas.f
+ eigen.o: eigen.f
+       ${FC} ${FFLAGS2} eigen.f
        energia(17)=estr
        energia(20)=Uconst+Uconst_back
        energia(21)=esccor
 +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"
        call sum_energy(energia,.true.)
  c      print *," Processor",myrank," left SUM_ENERGY"
@@@ -389,14 -387,14 +389,14 @@@ cMS$ATTRIBUTES C ::  proc_pro
  #ifdef SPLITELE
        etot=wsc*evdw+wscp*evdw2+welec*ees+wvdwpp*evdw1
       & +wang*ebe+wtor*etors+wscloc*escloc
-      & +wstrain*ehpb+nss*ebr+wcorr*ecorr+wcorr5*ecorr5
+      & +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
  #else
        etot=wsc*evdw+wscp*evdw2+welec*(ees+evdw1)
       & +wang*ebe+wtor*etors+wscloc*escloc
-      & +wstrain*ehpb+nss*ebr+wcorr*ecorr+wcorr5*ecorr5
+      & +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
@@@ -436,7 -434,7 +436,7 @@@ cMS$ATTRIBUTES C ::  proc_pro
  #ifdef MPI
        include 'mpif.h'
        double precision gradbufc(3,maxres),gradbufx(3,maxres),
 -     &  glocbuf(4*maxres),gradbufc_sum(3,maxres)
 +     &  glocbuf(4*maxres),gradbufc_sum(3,maxres),gloc_scbuf(3,maxres)
  #endif
        include 'COMMON.SETUP'
        include 'COMMON.IOUNITS'
        include 'COMMON.CONTROL'
        include 'COMMON.TIME1'
        include 'COMMON.MAXGRAD'
 +      include 'COMMON.SCCOR'
  #ifdef TIMING
        time01=MPI_Wtime()
  #endif
@@@ -692,6 -689,7 +692,6 @@@ c      endd
       &   +wturn3*gel_loc_turn3(i)
       &   +wturn6*gel_loc_turn6(i)
       &   +wel_loc*gel_loc_loc(i)
 -     &   +wsccor*gsccor_loc(i)
        enddo
  #ifdef DEBUG
        write (iout,*) "gloc after adding corr"
          do i=1,4*nres
            glocbuf(i)=gloc(i,icg)
          enddo
 +#define DEBUG
 +#ifdef DEBUG
 +      write (iout,*) "gloc_sc before reduce"
 +      do i=1,nres
 +       do j=1,1
 +        write (iout,*) i,j,gloc_sc(j,i,icg)
 +       enddo
 +      enddo
 +#endif
 +#undef DEBUG
 +        do i=1,nres
 +         do j=1,3
 +          gloc_scbuf(j,i)=gloc_sc(j,i,icg)
 +         enddo
 +        enddo
          time00=MPI_Wtime()
          call MPI_Barrier(FG_COMM,IERR)
          time_barrier_g=time_barrier_g+MPI_Wtime()-time00
          call MPI_Reduce(glocbuf(1),gloc(1,icg),4*nres,
       &    MPI_DOUBLE_PRECISION,MPI_SUM,king,FG_COMM,IERR)
          time_reduce=time_reduce+MPI_Wtime()-time00
 +        call MPI_Reduce(gloc_scbuf(1,1),gloc_sc(1,1,icg),3*nres,
 +     &    MPI_DOUBLE_PRECISION,MPI_SUM,king,FG_COMM,IERR)
 +        time_reduce=time_reduce+MPI_Wtime()-time00
 +#define DEBUG
 +#ifdef DEBUG
 +      write (iout,*) "gloc_sc after reduce"
 +      do i=1,nres
 +       do j=1,1
 +        write (iout,*) i,j,gloc_sc(j,i,icg)
 +       enddo
 +      enddo
 +#endif
 +#undef DEBUG
  #ifdef DEBUG
        write (iout,*) "gloc after reduce"
        do i=1,4*nres
  c      write(iout,*)'Entering ELJ nnt=',nnt,' nct=',nct,' expon=',expon
        evdw=0.0D0
        do i=iatsc_s,iatsc_e
 -        itypi=itype(i)
 -        if (itypi.eq.21) cycle
 -        itypi1=itype(i+1)
 +        itypi=iabs(itype(i))
 +        if (itypi.eq.ntyp1) cycle
 +        itypi1=iabs(itype(i+1))
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
  cd        write (iout,*) 'i=',i,' iint=',iint,' istart=',istart(i,iint),
  cd   &                  'iend=',iend(i,iint)
            do j=istart(i,iint),iend(i,iint)
 -            itypj=itype(j)
 -            if (itypj.eq.21) cycle
 +            itypj=iabs(itype(j)) 
 +            if (itypj.eq.ntyp1) cycle
              xj=c(1,nres+j)-xi
              yj=c(2,nres+j)-yi
              zj=c(3,nres+j)-zi
  c     print *,'Entering ELJK nnt=',nnt,' nct=',nct,' expon=',expon
        evdw=0.0D0
        do i=iatsc_s,iatsc_e
 -        itypi=itype(i)
 -        if (itypi.eq.21) cycle
 -        itypi1=itype(i+1)
 +        itypi=iabs(itype(i))
 +        if (itypi.eq.ntyp1) cycle
 +        itypi1=iabs(itype(i+1))
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
@@@ -1219,8 -1189,8 +1219,8 @@@ C Calculate SC interaction energy
  C
          do iint=1,nint_gr(i)
            do j=istart(i,iint),iend(i,iint)
 -            itypj=itype(j)
 -            if (itypj.eq.21) cycle
 +            itypj=iabs(itype(j))
 +            if (itypj.eq.ntyp1) cycle
              xj=c(1,nres+j)-xi
              yj=c(2,nres+j)-yi
              zj=c(3,nres+j)-zi
@@@ -1301,9 -1271,9 +1301,9 @@@ c     els
  c     endif
        ind=0
        do i=iatsc_s,iatsc_e
 -        itypi=itype(i)
 -        if (itypi.eq.21) cycle
 -        itypi1=itype(i+1)
 +        itypi=iabs(itype(i))
 +        if (itypi.eq.ntyp1) cycle
 +        itypi1=iabs(itype(i+1))
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
          do iint=1,nint_gr(i)
            do j=istart(i,iint),iend(i,iint)
              ind=ind+1
 -            itypj=itype(j)
 -            if (itypj.eq.21) cycle
 +            itypj=iabs(itype(j))
 +            if (itypj.eq.ntyp1) cycle
  c            dscj_inv=dsc_inv(itypj)
              dscj_inv=vbld_inv(j+nres)
              chi1=chi(itypi,itypj)
@@@ -1421,9 -1391,9 +1421,9 @@@ c     print *,'Entering EGB nnt=',nnt,
  c     if (icall.eq.0) lprn=.false.
        ind=0
        do i=iatsc_s,iatsc_e
 -        itypi=itype(i)
 -        if (itypi.eq.21) cycle
 -        itypi1=itype(i+1)
 +        itypi=iabs(itype(i))
 +        if (itypi.eq.ntyp1) cycle
 +        itypi1=iabs(itype(i+1))
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
          do iint=1,nint_gr(i)
            do j=istart(i,iint),iend(i,iint)
              ind=ind+1
 -            itypj=itype(j)
 -            if (itypj.eq.21) cycle
 +            itypj=iabs(itype(j))
 +            if (itypj.eq.ntyp1) cycle
  c            dscj_inv=dsc_inv(itypj)
              dscj_inv=vbld_inv(j+nres)
  c            write (iout,*) "j",j,dsc_inv(itypj),dscj_inv,
@@@ -1566,9 -1536,9 +1566,9 @@@ c     print *,'Entering EGB nnt=',nnt,
  c     if (icall.eq.0) lprn=.true.
        ind=0
        do i=iatsc_s,iatsc_e
 -        itypi=itype(i)
 -        if (itypi.eq.21) cycle
 -        itypi1=itype(i+1)
 +        itypi=iabs(itype(i))
 +        if (itypi.eq.ntyp1) cycle
 +        itypi1=iabs(itype(i+1))
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
          do iint=1,nint_gr(i)
            do j=istart(i,iint),iend(i,iint)
              ind=ind+1
 -            itypj=itype(j)
 -            if (itypj.eq.21) cycle
 +            itypj=iabs(itype(j))
 +            if (itypj.eq.ntyp1) cycle
  c            dscj_inv=dsc_inv(itypj)
              dscj_inv=vbld_inv(j+nres)
              sig0ij=sigma(itypi,itypj)
  cd    print *,'Entering Esoft_sphere nnt=',nnt,' nct=',nct
        evdw=0.0D0
        do i=iatsc_s,iatsc_e
 -        itypi=itype(i)
 -        if (itypi.eq.21) cycle
 -        itypi1=itype(i+1)
 +        itypi=iabs(itype(i))
 +        if (itypi.eq.ntyp1) cycle
 +        itypi1=iabs(itype(i+1))
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
  cd        write (iout,*) 'i=',i,' iint=',iint,' istart=',istart(i,iint),
  cd   &                  'iend=',iend(i,iint)
            do j=istart(i,iint),iend(i,iint)
 -            itypj=itype(j)
 -            if (itypj.eq.21) cycle
 +            itypj=iabs(itype(j))
 +            if (itypj.eq.ntyp1) cycle
              xj=c(1,nres+j)-xi
              yj=c(2,nres+j)-yi
              zj=c(3,nres+j)-zi
@@@ -1896,7 -1866,7 +1896,7 @@@ cd      write(iout,*) 'In EELEC_soft_sp
        eello_turn4=0.0d0
        ind=0
        do i=iatel_s,iatel_e
 -        if (itype(i).eq.21 .or. itype(i+1).eq.21) cycle
 +        if (itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1) cycle
          dxi=dc(1,i)
          dyi=dc(2,i)
          dzi=dc(3,i)
          num_conti=0
  c        write (iout,*) 'i',i,' ielstart',ielstart(i),' ielend',ielend(i)
          do j=ielstart(i),ielend(i)
 -          if (itype(j).eq.21 .or. itype(j+1).eq.21) cycle
 +          if (itype(j).eq.ntyp1 .or. itype(j+1).eq.ntyp1) cycle
            ind=ind+1
            iteli=itel(i)
            itelj=itel(j)
@@@ -2371,11 -2341,7 +2371,11 @@@ c        if (i .gt. iatel_s+2) the
          enddo
  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))
 +          if (itype(i-1).le.ntyp) then
 +            iti1 = itortyp(itype(i-1))
 +          else
 +            iti1=ntortyp+1
 +          endif
          else
            iti1=ntortyp+1
          endif
  C Loop over i,i+2 and i,i+3 pairs of the peptide groups
  C
        do i=iturn3_start,iturn3_end
 -        if (itype(i).eq.21 .or. itype(i+1).eq.21 
 -     &  .or. itype(i+2).eq.21 .or. itype(i+3).eq.21) cycle
 +        if (itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1
 +     &  .or. itype(i+2).eq.ntyp1 .or. itype(i+3).eq.ntyp1) cycle
          dxi=dc(1,i)
          dyi=dc(2,i)
          dzi=dc(3,i)
          num_cont_hb(i)=num_conti
        enddo
        do i=iturn4_start,iturn4_end
 -        if (itype(i).eq.21 .or. itype(i+1).eq.21
 -     &    .or. itype(i+3).eq.21
 -     &    .or. itype(i+4).eq.21) cycle
 +        if (itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1
 +     &    .or. itype(i+3).eq.ntyp1
 +     &    .or. itype(i+4).eq.ntyp1) cycle
          dxi=dc(1,i)
          dyi=dc(2,i)
          dzi=dc(3,i)
          zmedi=c(3,i)+0.5d0*dzi
          num_conti=num_cont_hb(i)
          call eelecij(i,i+3,ees,evdw1,eel_loc)
 -        if (wturn4.gt.0.0d0 .and. itype(i+2).ne.21) 
 +        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 pairs of interacting peptide groups except i,i+2 and i,i+3
  c
        do i=iatel_s,iatel_e
 -        if (itype(i).eq.21 .or. itype(i+1).eq.21) cycle
 +        if (itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1) cycle
          dxi=dc(1,i)
          dyi=dc(2,i)
          dzi=dc(3,i)
@@@ -2842,7 -2808,7 +2842,7 @@@ c        write (iout,*) 'i',i,' ielstar
          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.21 .or. itype(j+1).eq.21) cycle
 +          if (itype(j).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
@@@ -2944,9 -2910,7 +2944,9 @@@ cd     &      1.0D0/dsqrt(rrmij),evdwij
  cd     &      xmedi,ymedi,zmedi,xj,yj,zj
  
            if (energy_dec) then 
 -              write (iout,'(a6,2i5,0pf7.3)') 'evdw1',i,j,evdwij
 +              write (iout,'(a6,2i5,0pf7.3,2i5,2e11.3)') 
 +     &'evdw1',i,j,evdwij
 +     &,iteli,itelj,aaa,evdw1
                write (iout,'(a6,2i5,0pf7.3)') 'ees',i,j,eesij
            endif
  
@@@ -3278,7 -3242,6 +3278,7 @@@ cd          write (iout,*) 'i',i,' j',j
  
            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)
  
            eel_loc=eel_loc+eel_loc_ij
  C Partial derivatives in virtual-bond dihedral angles gamma
  cd    print '(a)','Enter ESCP'
  cd    write (iout,*) 'iatscp_s=',iatscp_s,' iatscp_e=',iatscp_e
        do i=iatscp_s,iatscp_e
 -        if (itype(i).eq.21 .or. itype(i+1).eq.21) cycle
 +        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))
          do iint=1,nscp_gr(i)
  
          do j=iscpstart(i,iint),iscpend(i,iint)
 -          if (itype(j).eq.21) cycle
 -          itypj=itype(j)
 +          if (itype(j).eq.ntyp1) cycle
 +          itypj=iabs(itype(j))
  C Uncomment following three lines for SC-p interactions
  c         xj=c(1,nres+j)-xi
  c         yj=c(2,nres+j)-yi
  cd    print '(a)','Enter ESCP'
  cd    write (iout,*) 'iatscp_s=',iatscp_s,' iatscp_e=',iatscp_e
        do i=iatscp_s,iatscp_e
 -        if (itype(i).eq.21 .or. itype(i+1).eq.21) cycle
 +        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))
          do iint=1,nscp_gr(i)
  
          do j=iscpstart(i,iint),iscpend(i,iint)
 -          itypj=itype(j)
 -          if (itypj.eq.21) cycle
 +          itypj=iabs(itype(j))
 +          if (itypj.eq.ntyp1) cycle
  C Uncomment following three lines for SC-p interactions
  c         xj=c(1,nres+j)-xi
  c         yj=c(2,nres+j)-yi
@@@ -3965,9 -3928,8 +3965,9 @@@ C Uncomment following three lines for C
            endif
            evdwij=e1+e2
            evdw2=evdw2+evdwij
 -          if (energy_dec) write (iout,'(a6,2i5,0pf7.3)')
 -     &        'evdw2',i,j,evdwij
 +          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
@@@ -4062,8 -4024,7 +4062,8 @@@ C iii and jjj point to the residues fo
  cd        write (iout,*) "i",i," ii",ii," iii",iii," jj",jj," jjj",jjj
  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. 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
  cd          write (iout,*) "eij",eij
        include 'COMMON.VAR'
        include 'COMMON.IOUNITS'
        double precision erij(3),dcosom1(3),dcosom2(3),gg(3)
 -      itypi=itype(i)
 +      itypi=iabs(itype(i))
        xi=c(1,nres+i)
        yi=c(2,nres+i)
        zi=c(3,nres+i)
        dzi=dc_norm(3,nres+i)
  c      dsci_inv=dsc_inv(itypi)
        dsci_inv=vbld_inv(nres+i)
 -      itypj=itype(j)
 +      itypj=iabs(itype(j))
  c      dscj_inv=dsc_inv(itypj)
        dscj_inv=vbld_inv(nres+j)
        xj=c(1,nres+j)-xi
        cosphi=om12-om1*om2
        eij=akcm*deltad*deltad+akth*(deltat1*deltat1+deltat2*deltat2)
       &  +akct*deltad*deltat12
-      &  +v1ss*cosphi+v2ss*cosphi*cosphi+v3ss*cosphi*cosphi*cosphi
+      &  +v1ss*cosphi+v2ss*cosphi*cosphi+v3ss*cosphi*cosphi*cosphi+ebr
  c      write(iout,*) i,j,"rij",rij,"d0cm",d0cm," akcm",akcm," akth",akth,
  c     &  " akct",akct," deltad",deltad," deltat",deltat1,deltat2,
  c     &  " deltat12",deltat12," eij",eij 
        estr=0.0d0
        estr1=0.0d0
        do i=ibondp_start,ibondp_end
 -        if (itype(i-1).eq.21 .or. itype(i).eq.21) then
 +        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)
  c 09/18/07 AL: multimodal bond potential based on AM1 CA-SC PMF's included
  c
        do i=ibond_start,ibond_end
 -        iti=itype(i)
 -        if (iti.ne.10 .and. iti.ne.21) then
 +        iti=iabs(itype(i))
 +        if (iti.ne.10 .and. iti.ne.ntyp1) then
            nbi=nbondterm(iti)
            if (nbi.eq.1) then
              diff=vbld(i+nres)-vbldsc0(1,iti)
@@@ -4316,24 -4277,11 +4316,24 @@@ 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.21) cycle
 +        if (itype(i-1).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)
 -        if (i.gt.3 .and. itype(i-2).ne.21) then
 +        ichir1=isign(1,itype(i-2))
 +        ichir2=isign(1,itype(i))
 +         if (itype(i-2).eq.10) ichir1=isign(1,itype(i-1))
 +         if (itype(i).eq.10) ichir2=isign(1,itype(i-1))
 +         if (itype(i-1).eq.10) then
 +          itype1=isign(10,itype(i-2))
 +          ichir11=isign(1,itype(i-2))
 +          ichir12=isign(1,itype(i-2))
 +          itype2=isign(10,itype(i))
 +          ichir21=isign(1,itype(i))
 +          ichir22=isign(1,itype(i))
 +         endif
 +
 +        if (i.gt.3 .and. itype(i-2).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.21) then
 +        if (i.lt.nres .and. itype(i).ne.ntyp1) then
  #ifdef OSF
          phii1=phi(i+1)
            if (phii1.ne.phii1) phii1=150.0
@@@ -4366,27 -4314,15 +4366,27 @@@ C dependent on the adjacent virtual-bon
  C In following comments this theta will be referred to as t_c.
          thet_pred_mean=0.0d0
          do k=1,2
 -          athetk=athet(k,it)
 -          bthetk=bthet(k,it)
 -          thet_pred_mean=thet_pred_mean+athetk*y(k)+bthetk*z(k)
 +            athetk=athet(k,it,ichir1,ichir2)
 +            bthetk=bthet(k,it,ichir1,ichir2)
 +          if (it.eq.10) then
 +             athetk=athet(k,itype1,ichir11,ichir12)
 +             bthetk=bthet(k,itype2,ichir21,ichir22)
 +          endif
 +         thet_pred_mean=thet_pred_mean+athetk*y(k)+bthetk*z(k)
          enddo
          dthett=thet_pred_mean*ssd
          thet_pred_mean=thet_pred_mean*ss+a0thet(it)
  C Derivatives of the "mean" values in gamma1 and gamma2.
 -        dthetg1=(-athet(1,it)*y(2)+athet(2,it)*y(1))*ss
 -        dthetg2=(-bthet(1,it)*z(2)+bthet(2,it)*z(1))*ss
 +        dthetg1=(-athet(1,it,ichir1,ichir2)*y(2)
 +     &+athet(2,it,ichir1,ichir2)*y(1))*ss
 +         dthetg2=(-bthet(1,it,ichir1,ichir2)*z(2)
 +     &          +bthet(2,it,ichir1,ichir2)*z(1))*ss
 +         if (it.eq.10) then
 +      dthetg1=(-athet(1,itype1,ichir11,ichir12)*y(2)
 +     &+athet(2,itype1,ichir11,ichir12)*y(1))*ss
 +        dthetg2=(-bthet(1,itype2,ichir21,ichir22)*z(2)
 +     &         +bthet(2,itype2,ichir21,ichir22)*z(1))*ss
 +         endif
          if (theta(i).gt.pi-delta) then
            call theteng(pi-delta,thet_pred_mean,theta0(it),f0,fprim0,
       &         E_tc0)
        logical lprn /.false./, lprn1 /.false./
        etheta=0.0D0
        do i=ithet_start,ithet_end
 -        if (itype(i-1).eq.21) cycle
 +        if (itype(i-1).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)
 -        ityp2=ithetyp(itype(i-1))
 +        ityp2=ithetyp((itype(i-1)))
          do k=1,nntheterm
            coskt(k)=dcos(k*theti2)
            sinkt(k)=dsin(k*theti2)
          enddo
 -        if (i.gt.3 .and. itype(i-2).ne.21) then
 +        if (i.gt.3 .and. itype(i-2).ne.ntyp1) then
  #ifdef OSF
            phii=phi(i)
            if (phii.ne.phii) phii=150.0
  #else
            phii=phi(i)
  #endif
 -          ityp1=ithetyp(itype(i-2))
 +          ityp1=ithetyp((itype(i-2)))
 +C propagation of chirality for glycine type
            do k=1,nsingle
              cosph1(k)=dcos(k*phii)
              sinph1(k)=dsin(k*phii)
              sinph1(k)=0.0d0
            enddo 
          endif
 -        if (i.lt.nres .and. itype(i).ne.21) then
 +        if (i.lt.nres .and. itype(i).ne.ntyp1) then
  #ifdef OSF
            phii1=phi(i+1)
            if (phii1.ne.phii1) phii1=150.0
  #else
            phii1=phi(i+1)
  #endif
 -          ityp3=ithetyp(itype(i))
 +          ityp3=ithetyp((itype(i)))
            do k=1,nsingle
              cosph2(k)=dcos(k*phii1)
              sinph2(k)=dsin(k*phii1)
              sinph2(k)=0.0d0
            enddo
          endif  
 -        ethetai=aa0thet(ityp1,ityp2,ityp3)
 +        ethetai=aa0thet(ityp1,ityp2,ityp3,iblock)
          do k=1,ndouble
            do l=1,k-1
              ccl=cosph1(l)*cosph2(k-l)
          enddo
          endif
          do k=1,ntheterm
 -          ethetai=ethetai+aathet(k,ityp1,ityp2,ityp3)*sinkt(k)
 -          dethetai=dethetai+0.5d0*k*aathet(k,ityp1,ityp2,ityp3)
 +          ethetai=ethetai+aathet(k,ityp1,ityp2,ityp3,iblock)*sinkt(k)
 +          dethetai=dethetai+0.5d0*k*aathet(k,ityp1,ityp2,ityp3,iblock)
       &      *coskt(k)
            if (lprn)
 -     &    write (iout,*) "k",k," aathet",aathet(k,ityp1,ityp2,ityp3),
 +     &    write (iout,*) "k",k,"
 +     &     aathet",aathet(k,ityp1,ityp2,ityp3,iblock),
       &     " ethetai",ethetai
          enddo
          if (lprn) then
          endif
          do m=1,ntheterm2
            do k=1,nsingle
 -            aux=bbthet(k,m,ityp1,ityp2,ityp3)*cosph1(k)
 -     &         +ccthet(k,m,ityp1,ityp2,ityp3)*sinph1(k)
 -     &         +ddthet(k,m,ityp1,ityp2,ityp3)*cosph2(k)
 -     &         +eethet(k,m,ityp1,ityp2,ityp3)*sinph2(k)
 +            aux=bbthet(k,m,ityp1,ityp2,ityp3,iblock)*cosph1(k)
 +     &         +ccthet(k,m,ityp1,ityp2,ityp3,iblock)*sinph1(k)
 +     &         +ddthet(k,m,ityp1,ityp2,ityp3,iblock)*cosph2(k)
 +     &         +eethet(k,m,ityp1,ityp2,ityp3,iblock)*sinph2(k)
              ethetai=ethetai+sinkt(m)*aux
              dethetai=dethetai+0.5d0*m*aux*coskt(m)
              dephii=dephii+k*sinkt(m)*(
 -     &          ccthet(k,m,ityp1,ityp2,ityp3)*cosph1(k)-
 -     &          bbthet(k,m,ityp1,ityp2,ityp3)*sinph1(k))
 +     &          ccthet(k,m,ityp1,ityp2,ityp3,iblock)*cosph1(k)-
 +     &          bbthet(k,m,ityp1,ityp2,ityp3,iblock)*sinph1(k))
              dephii1=dephii1+k*sinkt(m)*(
 -     &          eethet(k,m,ityp1,ityp2,ityp3)*cosph2(k)-
 -     &          ddthet(k,m,ityp1,ityp2,ityp3)*sinph2(k))
 +     &          eethet(k,m,ityp1,ityp2,ityp3,iblock)*cosph2(k)-
 +     &          ddthet(k,m,ityp1,ityp2,ityp3,iblock)*sinph2(k))
              if (lprn)
       &      write (iout,*) "m",m," k",k," bbthet",
 -     &         bbthet(k,m,ityp1,ityp2,ityp3)," ccthet",
 -     &         ccthet(k,m,ityp1,ityp2,ityp3)," ddthet",
 -     &         ddthet(k,m,ityp1,ityp2,ityp3)," eethet",
 -     &         eethet(k,m,ityp1,ityp2,ityp3)," ethetai",ethetai
 +     &         bbthet(k,m,ityp1,ityp2,ityp3,iblock)," ccthet",
 +     &         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
            enddo
          enddo
          if (lprn)
          do m=1,ntheterm3
            do k=2,ndouble
              do l=1,k-1
 -              aux=ffthet(l,k,m,ityp1,ityp2,ityp3)*cosph1ph2(l,k)+
 -     &            ffthet(k,l,m,ityp1,ityp2,ityp3)*cosph1ph2(k,l)+
 -     &            ggthet(l,k,m,ityp1,ityp2,ityp3)*sinph1ph2(l,k)+
 -     &            ggthet(k,l,m,ityp1,ityp2,ityp3)*sinph1ph2(k,l)
 +              aux=ffthet(l,k,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(l,k)+
 +     &            ffthet(k,l,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(k,l)+
 +     &            ggthet(l,k,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(l,k)+
 +     &            ggthet(k,l,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(k,l)
                ethetai=ethetai+sinkt(m)*aux
                dethetai=dethetai+0.5d0*m*coskt(m)*aux
                dephii=dephii+l*sinkt(m)*(
 -     &           -ffthet(l,k,m,ityp1,ityp2,ityp3)*sinph1ph2(l,k)-
 -     &            ffthet(k,l,m,ityp1,ityp2,ityp3)*sinph1ph2(k,l)+
 -     &            ggthet(l,k,m,ityp1,ityp2,ityp3)*cosph1ph2(l,k)+
 -     &            ggthet(k,l,m,ityp1,ityp2,ityp3)*cosph1ph2(k,l))
 +     &           -ffthet(l,k,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(l,k)-
 +     &            ffthet(k,l,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(k,l)+
 +     &            ggthet(l,k,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(l,k)+
 +     &            ggthet(k,l,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(k,l))
                dephii1=dephii1+(k-l)*sinkt(m)*(
 -     &           -ffthet(l,k,m,ityp1,ityp2,ityp3)*sinph1ph2(l,k)+
 -     &            ffthet(k,l,m,ityp1,ityp2,ityp3)*sinph1ph2(k,l)+
 -     &            ggthet(l,k,m,ityp1,ityp2,ityp3)*cosph1ph2(l,k)-
 -     &            ggthet(k,l,m,ityp1,ityp2,ityp3)*cosph1ph2(k,l))
 +     &           -ffthet(l,k,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(l,k)+
 +     &            ffthet(k,l,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(k,l)+
 +     &            ggthet(l,k,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(l,k)-
 +     &            ggthet(k,l,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(k,l))
                if (lprn) then
                write (iout,*) "m",m," k",k," l",l," ffthet",
 -     &            ffthet(l,k,m,ityp1,ityp2,ityp3),
 -     &            ffthet(k,l,m,ityp1,ityp2,ityp3)," ggthet",
 -     &            ggthet(l,k,m,ityp1,ityp2,ityp3),
 -     &            ggthet(k,l,m,ityp1,ityp2,ityp3)," ethetai",ethetai
 +     &            ffthet(l,k,m,ityp1,ityp2,ityp3,iblock),
 +     &            ffthet(k,l,m,ityp1,ityp2,ityp3,iblock)," ggthet",
 +     &            ggthet(l,k,m,ityp1,ityp2,ityp3,iblock),
 +     &            ggthet(k,l,m,ityp1,ityp2,ityp3,iblock),
 +     &            " ethetai",ethetai
                write (iout,*) cosph1ph2(l,k)*sinkt(m),
       &            cosph1ph2(k,l)*sinkt(m),
       &            sinph1ph2(l,k)*sinkt(m),sinph1ph2(k,l)*sinkt(m)
            enddo
          enddo
  10      continue
 -        if (lprn1) write (iout,'(i2,3f8.1,9h ethetai ,f10.5)') 
 +c        lprn1=.true.
 +        if (lprn1) 
 +     &    write (iout,'(i2,3f8.1,9h ethetai ,f10.5)') 
       &   i,theta(i)*rad2deg,phii*rad2deg,
       &   phii1*rad2deg,ethetai
 +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
@@@ -4750,9 -4678,9 +4750,9 @@@ C ALPHA and OMEGA
  c     write (iout,'(a)') 'ESC'
        do i=loc_start,loc_end
          it=itype(i)
 -        if (it.eq.21) cycle
 +        if (it.eq.ntyp1) cycle
          if (it.eq.10) goto 1
 -        nlobit=nlob(it)
 +        nlobit=nlob(iabs(it))
  c       print *,'i=',i,' it=',it,' nlobit=',nlobit
  c       write (iout,*) 'i=',i,' ssa=',ssa,' ssad=',ssad
          theti=theta(i+1)-pipol
@@@ -4909,11 -4837,11 +4909,11 @@@ C Compute the contribution to SC energ
  
            do j=1,nlobit
  #ifdef OSF
 -            adexp=bsc(j,it)-0.5D0*contr(j,iii)+emin
 +            adexp=bsc(j,iabs(it))-0.5D0*contr(j,iii)+emin
              if(adexp.ne.adexp) adexp=1.0
              expfac=dexp(adexp)
  #else
 -            expfac=dexp(bsc(j,it)-0.5D0*contr(j,iii)+emin)
 +            expfac=dexp(bsc(j,iabs(it))-0.5D0*contr(j,iii)+emin)
  #endif
  cd          print *,'j=',j,' expfac=',expfac
              escloc_i=escloc_i+expfac
@@@ -4995,7 -4923,7 +4995,7 @@@ C Compute the contribution to SC energ
  
        dersc12=0.0d0
        do j=1,nlobit
 -        expfac=dexp(bsc(j,it)-0.5D0*contr(j)+emin)
 +        expfac=dexp(bsc(j,iabs(it))-0.5D0*contr(j)+emin)
          escloc_i=escloc_i+expfac
          do k=1,2
            dersc(k)=dersc(k)+Ax(k,j)*expfac
        delta=0.02d0*pi
        escloc=0.0D0
        do i=loc_start,loc_end
 -        if (itype(i).eq.21) cycle
 +        if (itype(i).eq.ntyp1) cycle
          costtab(i+1) =dcos(theta(i+1))
          sinttab(i+1) =dsqrt(1-costtab(i+1)*costtab(i+1))
          cost2tab(i+1)=dsqrt(0.5d0*(1.0d0+costtab(i+1)))
          cosfac=dsqrt(cosfac2)
          sinfac2=0.5d0/(1.0d0-costtab(i+1))
          sinfac=dsqrt(sinfac2)
 -        it=itype(i)
 +        it=iabs(itype(i))
          if (it.eq.10) goto 1
  c
  C  Compute the axes of tghe local cartesian coordinates system; store in
@@@ -5076,7 -5004,7 +5076,7 @@@ C     &   dc_norm(3,i+nres
            y_prime(j) = (dc_norm(j,i) + dc_norm(j,i-1))*sinfac
          enddo
          do j = 1,3
 -          z_prime(j) = -uz(j,i-1)
 +          z_prime(j) = -uz(j,i-1)*dsign(1.0d0,dfloat(itype(i)))
          enddo     
  c       write (2,*) "i",i
  c       write (2,*) "x_prime",(x_prime(j),j=1,3)
  C Compute the energy of the ith side cbain
  C
  c        write (2,*) "xx",xx," yy",yy," zz",zz
 -        it=itype(i)
 +        it=iabs(itype(i))
          do j = 1,65
            x(j) = sc_parmin(j,it) 
          enddo
  Cc diagnostics - remove later
          xx1 = dcos(alph(2))
          yy1 = dsin(alph(2))*dcos(omeg(2))
 -        zz1 = -dsin(alph(2))*dsin(omeg(2))
 +        zz1 = -dsign(1.0,dfloat(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
@@@ -5158,9 -5086,7 +5158,9 @@@ c     &   sumene4
  c     &   dscp1,dscp2,sumene
  c        sumene = enesc(x,xx,yy,zz,cost2tab(i+1),sint2tab(i+1))
          escloc = escloc + sumene
 -c        write (2,*) "i",i," escloc",sumene,escloc
 +c        write (2,*) "i",i," escloc",sumene,escloc,it,itype(i)
 +c     & ,zz,xx,yy
 +c#define DEBUG
  #ifdef DEBUG
  C
  C This section to check the numerical derivatives of the energy of ith side
@@@ -5204,7 -5130,6 +5204,7 @@@ C End of diagnostics section
  C        
  C Compute the gradient of esc
  C
 +c        zz=zz*dsign(1.0,dfloat(itype(i)))
          pom_s1=(1.0d0+x(63))/(0.1d0 + dscp1)**2
          pom_s16=6*(1.0d0+x(64))/(0.1d0 + dscp1**6)**2
          pom_s2=(1.0d0+x(65))/(0.1d0 + dscp2)**2
       &        +(sumene2x+sumene4x*cost2tab(i+1))*(s2+s2_6)
       &        +(pom1+pom2)*pom_dx
  #ifdef DEBUG
 -        write(2,*), "de_dxx = ", de_dxx,de_dxx_num
 +        write(2,*), "de_dxx = ", de_dxx,de_dxx_num,itype(i)
  #endif
  C
          sumene1y=x(3) + 2*x(6)*yy + x(9)*xx + x(10)*zz
       &        +(sumene2y+sumene4y*cost2tab(i+1))*(s2+s2_6)
       &        +(pom1-pom2)*pom_dy
  #ifdef DEBUG
 -        write(2,*), "de_dyy = ", de_dyy,de_dyy_num
 +        write(2,*), "de_dyy = ", de_dyy,de_dyy_num,itype(i)
  #endif
  C
          de_dzz =(x(24) +2*x(27)*zz +x(28)*xx +x(30)*yy
       &  +x(60)*xx*yy)*cost2tab(i+1)*(s2+s2_6)
       &  + ( x(14) + 2*x(17)*zz+  x(18)*xx + x(20)*yy)*(s2+s2_6)
  #ifdef DEBUG
 -        write(2,*), "de_dzz = ", de_dzz,de_dzz_num
 +        write(2,*), "de_dzz = ", de_dzz,de_dzz_num,itype(i)
  #endif
  C
          de_dt =  0.5d0*sumene3*cost2tab(i+1)*(s1+s1_6) 
       &  -0.5d0*sumene4*sint2tab(i+1)*(s2+s2_6)
       &  +pom1*pom_dt1+pom2*pom_dt2
  #ifdef DEBUG
 -        write(2,*), "de_dt = ", de_dt,de_dt_num
 +        write(2,*), "de_dt = ", de_dt,de_dt_num,itype(i)
  #endif
 +c#undef DEBUG
  c 
  C
         cossc=scalar(dc_norm(1,i),dc_norm(1,i+nres))
@@@ -5290,16 -5214,13 +5290,16 @@@ c     &   (dC_norm(j,i-1),j=1,3)," vbld
           dZZ_Ci1(k)=0.0d0
           dZZ_Ci(k)=0.0d0
           do j=1,3
 -           dZZ_Ci(k)=dZZ_Ci(k)-uzgrad(j,k,2,i-1)*dC_norm(j,i+nres)
 -           dZZ_Ci1(k)=dZZ_Ci1(k)-uzgrad(j,k,1,i-1)*dC_norm(j,i+nres)
 +           dZZ_Ci(k)=dZZ_Ci(k)-uzgrad(j,k,2,i-1)
 +     &     *dsign(1.0d0,dfloat(itype(i)))*dC_norm(j,i+nres)
 +           dZZ_Ci1(k)=dZZ_Ci1(k)-uzgrad(j,k,1,i-1)
 +     &     *dsign(1.0d0,dfloat(itype(i)))*dC_norm(j,i+nres)
           enddo
            
           dXX_XYZ(k)=vbld_inv(i+nres)*(x_prime(k)-xx*dC_norm(k,i+nres))
           dYY_XYZ(k)=vbld_inv(i+nres)*(y_prime(k)-yy*dC_norm(k,i+nres))
 -         dZZ_XYZ(k)=vbld_inv(i+nres)*(z_prime(k)-zz*dC_norm(k,i+nres))
 +         dZZ_XYZ(k)=vbld_inv(i+nres)*
 +     &   (z_prime(k)-zz*dC_norm(k,i+nres))
  c
           dt_dCi(k) = -dt_dCi(k)/sinttab(i+1)
           dt_dCi1(k)= -dt_dCi1(k)/sinttab(i+1)
@@@ -5484,8 -5405,8 +5484,8 @@@ c      lprn=.true
        etors=0.0D0
        do i=iphi_start,iphi_end
        etors_ii=0.0D0
 -        if (itype(i-2).eq.21 .or. itype(i-1).eq.21 
 -     &      .or. itype(i).eq.21) cycle
 +        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))
          phii=phi(i)
@@@ -5581,22 -5502,17 +5581,22 @@@ C Set lprn=.true. for debuggin
  c     lprn=.true.
        etors=0.0D0
        do i=iphi_start,iphi_end
 -        if (itype(i-2).eq.21 .or. itype(i-1).eq.21 
 -     &       .or. itype(i).eq.21) cycle
 +        if (itype(i-2).eq.ntyp1 .or. itype(i-1).eq.ntyp1 
 +     &       .or. itype(i).eq.ntyp1) cycle
          etors_ii=0.0D0
 +         if (iabs(itype(i)).eq.20) then
 +         iblock=2
 +         else
 +         iblock=1
 +         endif
          itori=itortyp(itype(i-2))
          itori1=itortyp(itype(i-1))
          phii=phi(i)
          gloci=0.0D0
  C Regular cosine and sine terms
 -        do j=1,nterm(itori,itori1)
 -          v1ij=v1(j,itori,itori1)
 -          v2ij=v2(j,itori,itori1)
 +        do j=1,nterm(itori,itori1,iblock)
 +          v1ij=v1(j,itori,itori1,iblock)
 +          v2ij=v2(j,itori,itori1,iblock)
            cosphi=dcos(j*phii)
            sinphi=dsin(j*phii)
            etors=etors+v1ij*cosphi+v2ij*sinphi
@@@ -5611,7 -5527,7 +5611,7 @@@ C          [v2 cos(phi/2)+v3 sin(phi/2)
  C
          cosphi=dcos(0.5d0*phii)
          sinphi=dsin(0.5d0*phii)
 -        do j=1,nlor(itori,itori1)
 +        do j=1,nlor(itori,itori1,iblock)
            vl1ij=vlor1(j,itori,itori1)
            vl2ij=vlor2(j,itori,itori1)
            vl3ij=vlor3(j,itori,itori1)
            gloci=gloci+vl1ij*(vl3ij*cosphi-vl2ij*sinphi)*pom
          enddo
  C Subtract the constant term
 -        etors=etors-v0(itori,itori1)
 +        etors=etors-v0(itori,itori1,iblock)
            if (energy_dec) write (iout,'(a6,i5,0pf7.3)')
 -     &         'etor',i,etors_ii-v0(itori,itori1)
 +     &         'etor',i,etors_ii-v0(itori,itori1,iblock)
          if (lprn)
       &  write (iout,'(2(a3,2x,i3,2x),2i3,6f8.3/26x,6f8.3/)')
       &  restyp(itype(i-2)),i-2,restyp(itype(i-1)),i-1,itori,itori1,
 -     &  (v1(j,itori,itori1),j=1,6),(v2(j,itori,itori1),j=1,6)
 +     &  (v1(j,itori,itori1,iblock),j=1,6),
 +     &  (v2(j,itori,itori1,iblock),j=1,6)
          gloc(i-3,icg)=gloc(i-3,icg)+wtor*gloci
  c       write (iout,*) 'i=',i,' gloc=',gloc(i-3,icg)
        enddo
@@@ -5681,10 -5596,9 +5681,10 @@@ C Set lprn=.true. for debuggin
        lprn=.false.
  c     lprn=.true.
        etors_d=0.0D0
 +c      write(iout,*) "a tu??"
        do i=iphid_start,iphid_end
 -        if (itype(i-2).eq.21 .or. itype(i-1).eq.21
 -     &      .or. itype(i).eq.21 .or. itype(i+1).eq.21) cycle
 +        if (itype(i-2).eq.ntyp1 .or. itype(i-1).eq.ntyp1
 +     &      .or. itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1) cycle
          itori=itortyp(itype(i-2))
          itori1=itortyp(itype(i-1))
          itori2=itortyp(itype(i))
          phii1=phi(i+1)
          gloci1=0.0D0
          gloci2=0.0D0
 +        iblock=1
 +        if (iabs(itype(i+1)).eq.20) iblock=2
 +
  C Regular cosine and sine terms
 -        do j=1,ntermd_1(itori,itori1,itori2)
 -          v1cij=v1c(1,j,itori,itori1,itori2)
 -          v1sij=v1s(1,j,itori,itori1,itori2)
 -          v2cij=v1c(2,j,itori,itori1,itori2)
 -          v2sij=v1s(2,j,itori,itori1,itori2)
 +        do j=1,ntermd_1(itori,itori1,itori2,iblock)
 +          v1cij=v1c(1,j,itori,itori1,itori2,iblock)
 +          v1sij=v1s(1,j,itori,itori1,itori2,iblock)
 +          v2cij=v1c(2,j,itori,itori1,itori2,iblock)
 +          v2sij=v1s(2,j,itori,itori1,itori2,iblock)
            cosphi1=dcos(j*phii)
            sinphi1=dsin(j*phii)
            cosphi2=dcos(j*phii1)
            gloci1=gloci1+j*(v1sij*cosphi1-v1cij*sinphi1)
            gloci2=gloci2+j*(v2sij*cosphi2-v2cij*sinphi2)
          enddo
 -        do k=2,ntermd_2(itori,itori1,itori2)
 +        do k=2,ntermd_2(itori,itori1,itori2,iblock)
            do l=1,k-1
 -            v1cdij = v2c(k,l,itori,itori1,itori2)
 -            v2cdij = v2c(l,k,itori,itori1,itori2)
 -            v1sdij = v2s(k,l,itori,itori1,itori2)
 -            v2sdij = v2s(l,k,itori,itori1,itori2)
 +            v1cdij = v2c(k,l,itori,itori1,itori2,iblock)
 +            v2cdij = v2c(l,k,itori,itori1,itori2,iblock)
 +            v1sdij = v2s(k,l,itori,itori1,itori2,iblock)
 +            v2sdij = v2s(l,k,itori,itori1,itori2,iblock)
              cosphi1p2=dcos(l*phii+(k-l)*phii1)
              cosphi1m2=dcos(l*phii-(k-l)*phii1)
              sinphi1p2=dsin(l*phii+(k-l)*phii1)
@@@ -5760,53 -5671,29 +5760,53 @@@ c        amino-acid residues
  C Set lprn=.true. for debugging
        lprn=.false.
  c      lprn=.true.
 -c      write (iout,*) "EBACK_SC_COR",iphi_start,iphi_end,nterm_sccor
 +c      write (iout,*) "EBACK_SC_COR",itau_start,itau_end
        esccor=0.0D0
 -      do i=iphi_start,iphi_end
 -        if (itype(i-2).eq.21 .or. itype(i-1).eq.21) cycle
 +      do i=itau_start,itau_end
 +        if ((itype(i-2).eq.ntyp1).or.(itype(i-1).eq.ntyp1)) cycle
          esccor_ii=0.0D0
 -        itori=itype(i-2)
 -        itori1=itype(i-1)
 +        isccori=isccortyp(itype(i-2))
 +        isccori1=isccortyp(itype(i-1))
 +c      write (iout,*) "EBACK_SC_COR",i,nterm_sccor(isccori,isccori1)
          phii=phi(i)
 +        do intertyp=1,3 !intertyp
 +cc Added 09 May 2012 (Adasko)
 +cc  Intertyp means interaction type of backbone mainchain correlation: 
 +c   1 = SC...Ca...Ca...Ca
 +c   2 = Ca...Ca...Ca...SC
 +c   3 = SC...Ca...Ca...SCi
          gloci=0.0D0
 -        do j=1,nterm_sccor
 -          v1ij=v1sccor(j,itori,itori1)
 -          v2ij=v2sccor(j,itori,itori1)
 -          cosphi=dcos(j*phii)
 -          sinphi=dsin(j*phii)
 +        if (((intertyp.eq.3).and.((itype(i-2).eq.10).or.
 +     &      (itype(i-1).eq.10).or.(itype(i-2).eq.ntyp1).or.
 +     &      (itype(i-1).eq.ntyp1)))
 +     &    .or. ((intertyp.eq.1).and.((itype(i-2).eq.10)
 +     &     .or.(itype(i-2).eq.ntyp1).or.(itype(i-1).eq.ntyp1)
 +     &     .or.(itype(i).eq.ntyp1)))
 +     &    .or.((intertyp.eq.2).and.((itype(i-1).eq.10).or.
 +     &      (itype(i-1).eq.ntyp1).or.(itype(i-2).eq.ntyp1).or.
 +     &      (itype(i-3).eq.ntyp1)))) cycle
 +        if ((intertyp.eq.2).and.(i.eq.4).and.(itype(1).eq.ntyp1)) cycle
 +        if ((intertyp.eq.1).and.(i.eq.nres).and.(itype(nres).eq.ntyp1))
 +     & cycle
 +       do j=1,nterm_sccor(isccori,isccori1)
 +          v1ij=v1sccor(j,intertyp,isccori,isccori1)
 +          v2ij=v2sccor(j,intertyp,isccori,isccori1)
 +          cosphi=dcos(j*tauangle(intertyp,i))
 +          sinphi=dsin(j*tauangle(intertyp,i))
            esccor=esccor+v1ij*cosphi+v2ij*sinphi
            gloci=gloci+j*(v2ij*cosphi-v1ij*sinphi)
          enddo
 +c      write (iout,*) "EBACK_SC_COR",i,v1ij*cosphi+v2ij*sinphi,intertyp
 +        gloc_sc(intertyp,i-3,icg)=gloc_sc(intertyp,i-3,icg)+wsccor*gloci
          if (lprn)
       &  write (iout,'(2(a3,2x,i3,2x),2i3,6f8.3/26x,6f8.3/)')
 -     &  restyp(itype(i-2)),i-2,restyp(itype(i-1)),i-1,itori,itori1,
 -     &  (v1sccor(j,itori,itori1),j=1,6),(v2sccor(j,itori,itori1),j=1,6)
 +     &  restyp(itype(i-2)),i-2,restyp(itype(i-1)),i-1,isccori,isccori1,
 +     &  (v1sccor(j,intertyp,isccori,isccori1),j=1,6)
 +     & ,(v2sccor(j,intertyp,isccori,isccori1),j=1,6)
          gsccor_loc(i-3)=gsccor_loc(i-3)+gloci
 +       enddo !intertyp
        enddo
 +
        return
        end
  c----------------------------------------------------------------------------
@@@ -80,9 -80,7 +80,9 @@@
        igeom=  8
        intin=  9
        ithep= 11
 +      ithep_pdb=51
        irotam=12
 +      irotam_pdb=52
        itorp= 13
        itordp= 23
        ielep= 14
@@@ -163,14 -161,10 +163,14 @@@ c      call memmon_print_usage(
        rr0(i)=0.0D0
        a0thet(i)=0.0D0
        do j=1,2
 -        athet(j,i)=0.0D0
 -        bthet(j,i)=0.0D0
 +         do ichir1=-1,1
 +          do ichir2=-1,1
 +          athet(j,i,ichir1,ichir2)=0.0D0
 +          bthet(j,i,ichir1,ichir2)=0.0D0
 +          enddo
 +         enddo
          enddo
 -      do j=0,3
 +        do j=0,3
          polthet(j,i)=0.0D0
          enddo
        do j=1,3
        enddo
        nlob(ntyp1)=0
        dsc(ntyp1)=0.0D0
 -      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
 +      do i=-maxtor,maxtor
 +        itortyp(i)=0
 +cc      write (iout,*) "TU DOCHODZE",i,itortyp(i)
 +       do iblock=1,2
 +        do j=-maxtor,maxtor
 +          do k=1,maxterm
 +            v1(k,j,i,iblock)=0.0D0
 +            v2(k,j,i,iblock)=0.0D0
            enddo
          enddo
 +        enddo
        enddo
 +      do iblock=1,2
 +       do i=-maxtor,maxtor
 +        do j=-maxtor,maxtor
 +         do k=-maxtor,maxtor
 +          do l=1,maxtermd_1
 +            v1c(1,l,i,j,k,iblock)=0.0D0
 +            v1s(1,l,i,j,k,iblock)=0.0D0
 +            v1c(2,l,i,j,k,iblock)=0.0D0
 +            v1s(2,l,i,j,k,iblock)=0.0D0
 +          enddo !l
 +          do l=1,maxtermd_2
 +           do m=1,maxtermd_2
 +            v2c(m,l,i,j,k,iblock)=0.0D0
 +            v2s(m,l,i,j,k,iblock)=0.0D0
 +           enddo !m
 +          enddo !l
 +        enddo !k
 +       enddo !j
 +      enddo !i
 +      enddo !iblock
 +
        do i=1,maxres
        itype(i)=0
        itel(i)=0
@@@ -281,17 -251,11 +281,17 @@@ c--------------------------------------
        include 'COMMON.NAMES'
        include 'COMMON.FFIELD'
        data restyp /
 +     &'DD','DAU','DAI','DDB','DSM','DPR','DLY','DAR','DHI','DAS','DGL',
 +     & 'DSG','DGN','DSN','DTH',
 +     &'DYY','DAL','DTY','DTR','DVA','DLE','DIL','DPN','MED','DCY','ZER',
       &'CYS','MET','PHE','ILE','LEU','VAL','TRP','TYR','ALA','GLY','THR',
 -     &'SER','GLN','ASN','GLU','ASP','HIS','ARG','LYS','PRO','D'/
 +     &'SER','GLN','ASN','GLU','ASP','HIS','ARG','LYS','PRO','SME','DBZ',
 +     &'AIB','ABU','D'/
        data onelet /
 +     &'z','z','z','z','z','p','k','r','h','d','e','n','q','s','t','g',
 +     &'a','y','w','v','l','i','f','m','c','x',
       &'C','M','F','I','L','V','W','Y','A','G','T',
 -     &'S','Q','N','E','D','H','R','K','P','X'/
 +     &'S','Q','N','E','D','H','R','K','P','z','z','z','z','X'/
        data potname /'LJ','LJK','BP','GB','GBV'/
        data ename /
       &   "EVDW SC-SC","EVDW2 SC-p","EES p-p","ECORR4 ","ECORR5 ",
@@@ -324,11 -288,11 +324,11 @@@ c--------------------------------------
        include 'COMMON.IOUNITS'
        include 'COMMON.DERIV'
        include 'COMMON.CONTACTS'
-       common /przechowalnia/ iturn3_start_all(0:MaxProcs),
-      & iturn3_end_all(0:MaxProcs),iturn4_start_all(0:MaxProcs),
-      & iturn4_end_all(0:MaxProcs),iatel_s_all(0:MaxProcs),
-      & iatel_e_all(0:MaxProcs),ielstart_all(maxres,0:MaxProcs-1),
-      & ielend_all(maxres,0:MaxProcs-1),
+       common /przechowalnia/ iturn3_start_all(0:max_fg_procs),
+      & iturn3_end_all(0:max_fg_procs),iturn4_start_all(0:max_fg_procs),
+      & iturn4_end_all(0:max_fg_procs),iatel_s_all(0:max_fg_procs),
+      &iatel_e_all(0:max_fg_procs),ielstart_all(maxres,0:max_fg_procs-1),
+      & ielend_all(maxres,0:max_fg_procs-1),
       & ntask_cont_from_all(0:max_fg_procs-1),
       & itask_cont_from_all(0:max_fg_procs-1,0:max_fg_procs-1),
       & ntask_cont_to_all(0:max_fg_procs-1),
@@@ -596,9 -560,6 +596,9 @@@ C Partition local interaction
        iphi_end=iturn3_end+2
        iturn3_start=iturn3_start-1
        iturn3_end=iturn3_end-1
 +      call int_bounds(nres-3,itau_start,itau_end)
 +      itau_start=itau_start+3
 +      itau_end=itau_end+3
        call int_bounds(nres-3,iphi1_start,iphi1_end)
        iphi1_start=iphi1_start+3
        iphi1_end=iphi1_end+3
@@@ -1127,8 -1088,6 +1127,8 @@@ c        write (iout,*) "MPI_ROTAT2",MP
        idihconstr_end=ndih_constr
        iphid_start=iphi_start
        iphid_end=iphi_end-1
 +      itau_start=4
 +      itau_end=nres
        ibond_start=2
        ibond_end=nres-1
        ibondp_start=nnt
@@@ -1150,15 -1109,16 +1150,16 @@@ c--------------------------------------
        include "COMMON.INTERACT"
        include "COMMON.SETUP"
        include "COMMON.IOUNITS"
-       integer ii,jj,itask(4),ntask_cont_to,itask_cont_to(0:MaxProcs-1)
+       integer ii,jj,itask(4),ntask_cont_to,
+      &itask_cont_to(0:max_fg_procs-1)
        logical flag
        integer iturn3_start_all,iturn3_end_all,iturn4_start_all,
       & iturn4_end_all,iatel_s_all,iatel_e_all,ielstart_all,ielend_all
-       common /przechowalnia/ iturn3_start_all(0:MaxProcs),
-      & iturn3_end_all(0:MaxProcs),iturn4_start_all(0:MaxProcs),
-      & iturn4_end_all(0:MaxProcs),iatel_s_all(0:MaxProcs),
-      & iatel_e_all(0:MaxProcs),ielstart_all(maxres,0:MaxProcs-1),
-      & ielend_all(maxres,0:MaxProcs-1)
+       common /przechowalnia/ iturn3_start_all(0:max_fg_procs),
+      & iturn3_end_all(0:max_fg_procs),iturn4_start_all(0:max_fg_procs),
+      & iturn4_end_all(0:max_fg_procs),iatel_s_all(0:max_fg_procs),
+      &iatel_e_all(0:max_fg_procs),ielstart_all(maxres,0:max_fg_procs-1),
+      & ielend_all(maxres,0:max_fg_procs-1)
        integer iproc,isent,k,l
  c Determines whether to send interaction ii,jj to other processors; a given
  c interaction can be sent to at most 2 processors.
@@@ -1240,15 -1200,15 +1241,15 @@@ c--------------------------------------
        include "COMMON.SETUP"
        include "COMMON.IOUNITS"
        integer ii,jj,itask(2),ntask_cont_from,
-      & itask_cont_from(0:MaxProcs-1)
+      & itask_cont_from(0:max_fg_procs-1)
        logical flag
        integer iturn3_start_all,iturn3_end_all,iturn4_start_all,
       & iturn4_end_all,iatel_s_all,iatel_e_all,ielstart_all,ielend_all
-       common /przechowalnia/ iturn3_start_all(0:MaxProcs),
-      & iturn3_end_all(0:MaxProcs),iturn4_start_all(0:MaxProcs),
-      & iturn4_end_all(0:MaxProcs),iatel_s_all(0:MaxProcs),
-      & iatel_e_all(0:MaxProcs),ielstart_all(maxres,0:MaxProcs-1),
-      & ielend_all(maxres,0:MaxProcs-1)
+       common /przechowalnia/ iturn3_start_all(0:max_fg_procs),
+      & iturn3_end_all(0:max_fg_procs),iturn4_start_all(0:max_fg_procs),
+      & iturn4_end_all(0:max_fg_procs),iatel_s_all(0:max_fg_procs),
+      &iatel_e_all(0:max_fg_procs),ielstart_all(maxres,0:max_fg_procs-1),
+      & ielend_all(maxres,0:max_fg_procs-1)
        integer iproc,k,l
        do iproc=fg_rank+1,nfgtasks-1
          do k=iturn3_start_all(iproc),iturn3_end_all(iproc)
@@@ -1300,7 -1260,7 +1301,7 @@@ c--------------------------------------
        subroutine add_task(iproc,ntask_cont,itask_cont)
        implicit none
        include "DIMENSIONS"
-       integer iproc,ntask_cont,itask_cont(0:MaxProcs-1)
+       integer iproc,ntask_cont,itask_cont(0:max_fg_procs-1)
        integer ii
        do ii=1,ntask_cont
          if (itask_cont(ii).eq.iproc) return
@@@ -16,7 -16,9 +16,9 @@@ C geometry
        character*3 seq,atom,res
        character*80 card
        dimension sccor(3,20)
+       double precision e1(3),e2(3),e3(3)
        integer rescode
+       logical fail
        ibeg=1
        lsecondary=.false.
        nhfrag=0
@@@ -45,7 -47,7 +47,7 @@@ crc------------------------------------
          else if (card(:3).eq.'TER') then
  C End current chain
            ires_old=ires+1 
 -          itype(ires_old)=21
 +          itype(ires_old)=ntyp1
            ibeg=2
  c          write (iout,*) "Chain ended",ires,ishift,ires_old
            if (unres_pdb) then
@@@ -78,7 -80,7 +80,7 @@@ c            write (iout,'(a80)') car
                ishift=ires-1
                if (res.ne.'GLY' .and. res.ne. 'ACE') then
                  ishift=ishift-1
 -                itype(1)=21
 +                itype(1)=ntyp1
                endif
  c              write (iout,*) "ires",ires," ibeg",ibeg," ishift",ishift
                ibeg=0          
@@@ -91,7 -93,7 +93,7 @@@ c              write (iout,*) "New chai
              ires=ires-ishift
  c            write (2,*) "ires",ires," ishift",ishift
              if (res.eq.'ACE') then
 -              ity=10
 +              itype(ires)=10
              else
                itype(ires)=rescode(ires,res,0)
              endif
@@@ -117,7 -119,7 +119,7 @@@ C syste
        nres=ires
        do i=2,nres-1
  c        write (iout,*) i,itype(i)
 -        if (itype(i).eq.21) then
 +        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
@@@ -138,11 -140,18 +140,18 @@@ C Calculate the CM of the last side cha
        nstart_sup=1
        if (itype(nres).ne.10) then
          nres=nres+1
 -        itype(nres)=21
 +        itype(nres)=ntyp1
          if (unres_pdb) then
-           c(1,nres)=c(1,nres-1)+3.8d0
-           c(2,nres)=c(2,nres-1)
-           c(3,nres)=c(3,nres-1)
+ C 2/15/2013 by Adam: corrected insertion of the last dummy residue
+           call refsys(nres-3,nres-2,nres-1,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,nres)=c(j,nres-1)-3.8d0*e2(j)
+           enddo
          else
          do j=1,3
            dcj=c(j,nres-2)-c(j,nres-3)
          c(j,nres+1)=c(j,1)
          c(j,2*nres)=c(j,nres)
        enddo
 -      if (itype(1).eq.21) then
 +      if (itype(1).eq.ntyp1) then
          nsup=nsup-1
          nstart_sup=2
          if (unres_pdb) then
-           c(1,1)=c(1,2)-3.8d0
-           c(2,1)=c(2,2)
-           c(3,1)=c(3,2)
+ C 2/15/2013 by Adam: corrected insertion of the first dummy residue
+           call refsys(2,3,4,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,1)=c(j,2)-3.8d0*e2(j)
+           enddo
          else
          do j=1,3
            dcj=c(j,4)-c(j,3)
@@@ -213,7 -229,7 +229,7 @@@ C Splits to single chain if occur
        lll=lll+1
  cc      write (iout,*) "spraw lancuchy",(c(j,i),j=1,3)
        if (i.gt.1) then
 -      if ((itype(i-1).eq.21)) then
 +      if ((itype(i-1).eq.ntyp1).and.(i.gt.2)) then
        chain_length=lll-1
        kkk=kkk+1
  c       write (iout,*) "spraw lancuchy",(c(j,i),j=1,3)
            endif
           enddo
        enddo
 +      write (iout,*) chain_length
 +      if (chain_length.eq.0) chain_length=nres
        do j=1,3
        chain_rep(j,chain_length,symetr)=chain_rep(j,chain_length,1)
        chain_rep(j,chain_length+nres,symetr)
@@@ -350,7 -364,7 +366,7 @@@ c--------------------------------------
  #endif
        do i=1,nres-1
          iti=itype(i)
 -        if (iti.ne.21 .and. itype(i+1).ne.21 .and. 
 +        if (iti.ne.ntyp1 .and. itype(i+1).ne.ntyp1 .and. 
       &      (dist(i,i+1).lt.2.0D0 .or. dist(i,i+1).gt.5.0D0)) then
            write (iout,'(a,i4)') 'Bad Cartesians for residue',i
  ctest          stop
@@@ -433,7 -447,7 +449,7 @@@ c--------------------------------------
          enddo
        enddo
        do i=2,nres-1
 -        if (itype(i).ne.10 .and. itype(i).ne.21) then
 +        if (itype(i).ne.10 .and. itype(i).ne.ntyp1) then
            do j=1,3
              dc_norm(j,i+nres)=vbld_inv(i+nres)*(c(j,i+nres)-c(j,i))
            enddo
          sinfac2=0.5d0/(1.0d0-costtab(i+1))
          sinfac=dsqrt(sinfac2)
          it=itype(i)
 -        if (it.ne.10 .and. itype(i).ne.21) then
 +        if (it.ne.10 .and. itype(i).ne.ntyp1) then
  c
  C  Compute the axes of tghe local cartesian coordinates system; store in
  c   x_prime, y_prime and z_prime 
@@@ -532,8 -546,8 +548,8 @@@ c--------------------------------------
        do i=1,nres-1
         vbld(i+1)=vbl
         vbld_inv(i+1)=1.0d0/vbld(i+1)
 -       vbld(i+1+nres)=dsc(itype(i+1))
 -       vbld_inv(i+1+nres)=dsc_inv(itype(i+1))
 +       vbld(i+1+nres)=dsc(iabs(itype(i+1)))
 +       vbld_inv(i+1+nres)=dsc_inv(iabs(itype(i+1)))
  c       print *,vbld(i+1),vbld(i+1+nres)
        enddo
        return
@@@ -39,7 -39,7 +39,7 @@@
          ind=ind+3
        enddo
        do i=nnt,nct
 -        if (itype(i).ne.10 .and. itype(i).ne.21) then
 +        if (itype(i).ne.10) then
            do j=1,3
              d_t_work(ind+j)=d_t(j,i+nres)
            enddo
@@@ -68,7 -68,7 +68,7 @@@
          ind=ind+3
        enddo
        do i=nnt,nct
 -        if (itype(i).ne.10 .and. itype(i).ne.21) then
 +        if (itype(i).ne.10) then
            do j=1,3
              friction(j,i+nres)=fric_work(ind+j)
            enddo
@@@ -185,11 -185,7 +185,11 @@@ c--------------------------------------
        enddo
        x=0.0d0         
  
 +#ifdef MPI
        time00=MPI_Wtime()
 +#else
 +      time00=tcpu()
 +#endif
  c Compute the stochastic forces acting on bodies. Store in force.
        do i=nnt,nct-1
          sig=stdforcp(i)
            force(j,i+nres)=anorm_distr(x,sig2,lowb2,highb2)
          enddo
        enddo
 +#ifdef MPI
        time_fsample=time_fsample+MPI_Wtime()-time00
 +#else
 +      time_fsample=time_fsample+tcpu()-time00
 +#endif
  c Compute the stochastic forces acting on virtual-bond vectors.
        do j=1,3
          ff(j)=0.0d0
          do j=1,3
            ff(j)=ff(j)+force(j,i)
          enddo
 -        if (itype(i+1).ne.21) then
 +        if (itype(i+1).ne.ntyp1) then
            do j=1,3
              stochforc(j,i)=stochforc(j,i)+force(j,i+nres+1)
              ff(j)=ff(j)+force(j,i+nres+1)
          stochforc(j,0)=ff(j)+force(j,nnt+nres)
        enddo
        do i=nnt,nct
 -        if (itype(i).ne.10 .and. itype(i).ne.21) then
 +        if (itype(i).ne.10) then
            do j=1,3
              stochforc(j,i+nres)=force(j,i+nres)
            enddo
          ind=ind+3
        enddo
        do i=nnt,nct
 -        if (itype(i).ne.10 .and. itype(i).ne.21) then
 +        if (itype(i).ne.10) then
            do j=1,3
              stochforcvec(ind+j)=stochforc(j,i+nres)
            enddo
  c------------------------------------------------------------------
        subroutine setup_fricmat
        implicit real*8 (a-h,o-z)
 +#ifdef MPI
        include 'mpif.h'
 +#endif
        include 'DIMENSIONS'
        include 'COMMON.VAR'
        include 'COMMON.CHAIN'
@@@ -365,11 -355,12 +365,12 @@@ c  Load the friction coefficients corre
  c  Load the friction coefficients corresponding to side chains
        m=nct-nnt
        ind=0
+       gamsc(ntyp1)=1.0d0
        do i=nnt,nct
          ind=ind+1
          ii = ind+m
          iti=itype(i)
 -        gamvec(ii)=gamsc(iti)
 +        gamvec(ii)=gamsc(iabs(iti))
        enddo
        if (surfarea) call sdarea(gamvec)
  c      if (lprn) then
@@@ -487,39 -478,22 +488,39 @@@ c      endd
        if (nfgtasks.gt.1) then
          if (fg_rank.eq.0) then
  c The matching BROADCAST for fg processors is called in ERGASTULUM
 +#ifdef MPI
            time00=MPI_Wtime()
 +#else
 +          time00=tcpu()
 +#endif
            call MPI_Bcast(10,1,MPI_INTEGER,king,FG_COMM,IERROR)
 +#ifdef MPI
            time_Bcast=time_Bcast+MPI_Wtime()-time00
 +#else
 +          time_Bcast=time_Bcast+tcpu()-time00
 +#endif
  c          print *,"Processor",myrank,
  c     &       " BROADCAST iorder in SETUP_FRICMAT"
          endif
  c     licznik=licznik+1
  c        write (iout,*) "setup_fricmat licznik",licznik
 +#ifdef MPI
          time00=MPI_Wtime()
 +#else
 +        time00=tcpu()
 +#endif
  c Scatter the friction matrix
          call MPI_Scatterv(fricmat(1,1),nginv_counts(0),
       &    nginv_start(0),MPI_DOUBLE_PRECISION,fcopy(1,1),
       &    myginv_ng_count,MPI_DOUBLE_PRECISION,king,FG_COMM,IERROR)
 -        time_scatter=time_scatter+MPI_Wtime()-time00
  #ifdef TIMING
 +#ifdef MPI
 +        time_scatter=time_scatter+MPI_Wtime()-time00
          time_scatter_fmat=time_scatter_fmat+MPI_Wtime()-time00
 +#else
 +        time_scatter=time_scatter+tcpu()-time00
 +        time_scatter_fmat=time_scatter_fmat+tcpu()-time00
 +#endif
  #endif
          do i=1,dimen
            do j=1,2*my_ng_count
        include 'COMMON.INTERACT'
        include 'COMMON.IOUNITS'
        include 'COMMON.NAMES'
-       double precision radius(maxres2),gamvec(maxres6)
+       double precision radius(maxres2),gamvec(maxres2)
        parameter (twosix=1.122462048309372981d0)
 -      logical lprn /.true./
 +      logical lprn /.false./
  c
  c     determine new friction coefficients every few SD steps
  c
@@@ -78,10 -78,17 +78,17 @@@ set(UNRES_MD_SRC
        timing.F
        thread.F 
        unres.F
+       ssMD.F
  )
  
  if(Fortran_COMPILER_NAME STREQUAL "ifort")
    set(UNRES_MD_SRC0 ${UNRES_MD_SRC0} prng.f ) 
+ elseif(Fortran_COMPILER_NAME STREQUAL "mpif90")
+   set(UNRES_MD_SRC0 ${UNRES_MD_SRC0} prng.f )
+ elseif(Fortran_COMPILER_NAME STREQUAL "f95")
+   set(UNRES_MD_SRC0 ${UNRES_MD_SRC0} prng.f )
+ elseif(Fortran_COMPILER_NAME STREQUAL "gfortran")
+   set(UNRES_MD_SRC0 ${UNRES_MD_SRC0} prng.f )
  else()
    set(UNRES_MD_SRC0 ${UNRES_MD_SRC0} prng_32.F )
  endif (Fortran_COMPILER_NAME STREQUAL "ifort")
@@@ -149,11 -156,11 +156,11 @@@ if (Fortran_COMPILER_NAME STREQUAL "ifo
    #set(FFLAGS3 "-c -w -O3 -ipo -ipo_obj -opt_report" )
    set(FFLAGS3 "-w -ipo " )
  elseif (Fortran_COMPILER_NAME STREQUAL "gfortran")
-   set(FFLAGS0 "-I. " ) 
-   set(FFLAGS1 "-g -I. " ) 
-   set(FFLAGS2 "-I. ")
+   set(FFLAGS0 "-std=legacy -I. " ) 
+   set(FFLAGS1 "-std=legacy -g -I. " ) 
+   set(FFLAGS2 "-std=legacy -I. ")
    #set(FFLAGS3 "-c -w -O3 -ipo -ipo_obj -opt_report" )
-   set(FFLAGS3 "-I. " )
+   set(FFLAGS3 "-std=legacy -I. " )
  endif (Fortran_COMPILER_NAME STREQUAL "ifort")
  
  
@@@ -175,14 -182,14 +182,14 @@@ set_property(SOURCE ${UNRES_MD_SRC3} PR
  #=========================================
  if(UNRES_MD_FF STREQUAL "GAB" )
    # set preprocesor flags   
 -  set(CPPFLAGS "PROCOR -DUNRES -DISNAN -DSPLITELE -DLANG0 -DCRYST_BOND -DCRYST_THETA -DCRYST_SC" )
 +  set(CPPFLAGS "PROCOR -DUNRES -DISNAN -DSPLITELE -DLANG0 -DCRYST_BOND -DCRYST_THETA -DCRYST_SC -DSCCORPDB" )
  
  #=========================================
  #  Settings for E0LL2Y force field
  #=========================================
  elseif(UNRES_MD_FF STREQUAL "E0LL2Y")
    # set preprocesor flags   
 -  set(CPPFLAGS "PROCOR -DUNRES -DISNAN -DSPLITELE -DLANG0" )
 +  set(CPPFLAGS "PROCOR -DUNRES -DISNAN -DSPLITELE -DLANG0 -DSCCORPDB" )
  endif(UNRES_MD_FF STREQUAL "GAB")
  
  #=========================================
@@@ -205,6 -212,9 +212,9 @@@ elseif (Fortran_COMPILER_NAME STREQUAL 
  elseif (Fortran_COMPILER_NAME STREQUAL "gfortran")
    # Add old gfortran flags
    set(CPPFLAGS "${CPPFLAGS} -DG77") 
+ else(Fortran_COMPILER_NAME STREQUAL "ifort")
+   # Default preprocessor flag
+   set(CPPFLAGS "${CPPFLAGS} -DPGI")
  endif (Fortran_COMPILER_NAME STREQUAL "ifort")
  
  #=========================================
@@@ -232,25 -242,41 +242,41 @@@ else(UNRES_WITH_MPI
  endif(UNRES_WITH_MPI)  
  
  #=========================================
- # cinfo.f stupid workaround for cmake
- #  - shame on me ]:)
+ # cinfo.f workaround for cmake
  #=========================================
- set_property(SOURCE compinfo.c PROPERTY CMAKE_C_FLAGS "-c" )
- add_executable(compinfo-md compinfo.c)
- set_target_properties(compinfo-md PROPERTIES OUTPUT_NAME compinfo)
- set(UNRES_CINFO_DIR "${CMAKE_CURRENT_BINARY_DIR}" )
- add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/cinfo.f 
-       COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/cinfo.f ${CMAKE_CURRENT_BINARY_DIR}/cinfo.f
-       COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/COMMON.IOUNITS ${CMAKE_CURRENT_BINARY_DIR}/COMMON.IOUNITS 
-       COMMAND ${CMAKE_CURRENT_BINARY_DIR}/compinfo | true     
-       DEPENDS compinfo-md )
- set_property(SOURCE ${UNRES_CINFO_DIR}/cinfo.f PROPERTY COMPILE_FLAGS ${FFLAGS0} )
+ # get the current date  
+ TODAY(DATE)
+ # generate cinfo.f
+ set(CINFO "${CMAKE_CURRENT_BINARY_DIR}/cinfo.f")
+ FILE(WRITE ${CINFO}
+ "C CMake generated file
+        subroutine cinfo
+        include 'COMMON.IOUNITS'
+        write(iout,*)'++++ Compile info ++++'
+        write(iout,*)'Version ${UNRES_MAJOR}.${UNRES_MINOR} build ${UNRES_PATCH}'
+ ")
+ CINFO_FORMAT(${CINFO} "Compiled" "${DATE}" )
+ CINFO_FORMAT(${CINFO} "Compiled by" "$ENV{USER}@$ENV{HOST}" )
+ CINFO_FORMAT(${CINFO} "OS name:" "${CMAKE_SYSTEM_NAME}" )
+ CINFO_FORMAT(${CINFO} "OS release:" "${CMAKE_SYSTEM}" )
+ CINFO_FORMAT(${CINFO} "Fortran Compiler:" "${CMAKE_Fortran_COMPILER}" )
+ CINFO_FORMAT(${CINFO} "MD Force field:" "${UNRES_MD_FF}" )
+ CINFO_FORMAT(${CINFO} "CPPFLAGS =" "${CPPFLAGS}")
+ FILE(APPEND ${CINFO} 
+ "       write(iout,*)'++++ End of compile info ++++'  
+        return 
+        end ")
+ # add include path
+ set_property(SOURCE ${CMAKE_CURRENT_BINARY_DIR}/cinfo.f PROPERTY COMPILE_FLAGS "${FFLAGS0} -I${CMAKE_CURRENT_SOURCE_DIR}")
  
  #=========================================
  # Set full unres MD sources
  #=========================================
- set(UNRES_MD_SRCS ${UNRES_MD_SRC0} ${UNRES_MD_SRC3} ${UNRES_CINFO_DIR}/cinfo.f )
+ set(UNRES_MD_SRCS ${UNRES_MD_SRC0} ${UNRES_MD_SRC3} ${CMAKE_CURRENT_BINARY_DIR}/cinfo.f )
  
  
  #=========================================
@@@ -315,7 -341,7 +341,7 @@@ export ELEPAR=$DD/electr_631Gdp.par
  export SIDEPAR=$DD/sc_GB_opt.1gab_3S_qclass5no310-shan2-sc-16-10-8k
  export FOURIER=$DD/fourier_opt.parm.1igd_hc_iter3_3
  export SCPPAR=$DD/scp.parm
- export SCCORPAR=$DD/rotcorr_AM1.parm
+ export SCCORPAR=$DD/sccor_pdb_shelly.dat
  export PATTERN=$DD/patterns.cart
  #-----------------------------------------------------------------------------
  $UNRES_BIN
@@@ -4,15 -4,14 +4,18 @@@ cc Parameters of the SCCOR ter
       &                 dcostau,dsintau,dtauangle,dcosomicron,
       &                 domicron
        integer nterm_sccor,isccortyp,nsccortyp,nlor_sccor
 -      common/sccor/v1sccor(maxterm_sccor,3,20,20),
 -     &    v2sccor(maxterm_sccor,3,20,20),
 +      common/sccor/v1sccor(maxterm_sccor,3,-ntyp:ntyp,-ntyp:ntyp),
 +     &    v2sccor(maxterm_sccor,3,-ntyp:ntyp,-ntyp:ntyp),
 +     &    v0sccor(maxterm_sccor,-ntyp:ntyp,-ntyp:ntyp),
 +     &    nterm_sccor(-ntyp:ntyp,-ntyp:ntyp),isccortyp(-ntyp:ntyp),
 +     &    nsccortyp,
 +     &    nlor_sccor(-ntyp:ntyp,-ntyp:ntyp),
       &    vlor1sccor(maxterm_sccor,20,20),
       &    vlor2sccor(maxterm_sccor,20,20),
       &    vlor3sccor(maxterm_sccor,20,20),gloc_sc(3,0:maxres2,10),
+      &    v0sccor(ntyp,ntyp),
       &    dcostau(3,3,3,maxres2),dsintau(3,3,3,maxres2),
       &    dtauangle(3,3,3,maxres2),dcosomicron(3,3,3,maxres2),
-      &    domicron(3,3,3,maxres2)
+      &    domicron(3,3,3,maxres2),
+      &    nterm_sccor(ntyp,ntyp),isccortyp(ntyp),nsccortyp,
+      &    nlor_sccor(ntyp,ntyp)
  C Calculate electrostatic (H-bonding) energy of the main chain.
  C
    107 continue
+ cmc
+ cmc Sep-06: egb takes care of dynamic ss bonds too
+ cmc
+ c      if (dyn_ss) call dyn_set_nss
  c      print *,"Processor",myrank," computed USCSC"
  #ifdef TIMING
  #ifdef MPI
        energia(23)=evdw_m
  c      print *," Processor",myrank," calls SUM_ENERGY"
        call sum_energy(energia,.true.)
+       if (dyn_ss) call dyn_set_nss
  c      print *," Processor",myrank," left SUM_ENERGY"
  #ifdef TIMING
  #ifdef MPI
@@@ -423,14 -429,14 +429,14 @@@ cMS$ATTRIBUTES C ::  proc_pro
  #ifdef SPLITELE
        etot=wsc*evdw+wscp*evdw2+welec*ees+wvdwpp*evdw1
       & +wang*ebe+wtor*etors+wscloc*escloc
-      & +wstrain*ehpb+nss*ebr+wcorr*ecorr+wcorr5*ecorr5
+      & +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
  #else
        etot=wsc*evdw+wscp*evdw2+welec*(ees+evdw1)
       & +wang*ebe+wtor*etors+wscloc*escloc
-      & +wstrain*ehpb+nss*ebr+wcorr*ecorr+wcorr5*ecorr5
+      & +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
@@@ -774,7 -780,6 +780,6 @@@ c      endd
          do i=1,4*nres
            glocbuf(i)=gloc(i,icg)
          enddo
- #define DEBUG
  #ifdef DEBUG
        write (iout,*) "gloc_sc before reduce"
        do i=1,nres
         enddo
        enddo
  #endif
- #undef DEBUG
          do i=1,nres
           do j=1,3
            gloc_scbuf(j,i)=gloc_sc(j,i,icg)
          call MPI_Reduce(gloc_scbuf(1,1),gloc_sc(1,1,icg),3*nres,
       &    MPI_DOUBLE_PRECISION,MPI_SUM,king,FG_COMM,IERR)
          time_reduce=time_reduce+MPI_Wtime()-time00
- #define DEBUG
  #ifdef DEBUG
        write (iout,*) "gloc_sc after reduce"
        do i=1,nres
         enddo
        enddo
  #endif
- #undef DEBUG
  #ifdef DEBUG
        write (iout,*) "gloc after reduce"
        do i=1,4*nres
@@@ -1056,25 -1058,25 +1058,25 @@@ C--------------------------------------
       &  edihcnstr,ebr*nss,
       &  Uconst,etot
     10 format (/'Virtual-chain energies:'//
-      & 'EVDW=  ',1pE16.6,' WEIGHT=',1pD16.6,' (SC-SC)'/
-      & 'EVDW2= ',1pE16.6,' WEIGHT=',1pD16.6,' (SC-p)'/
-      & 'EES=   ',1pE16.6,' WEIGHT=',1pD16.6,' (p-p)'/
-      & 'EVDWPP=',1pE16.6,' WEIGHT=',1pD16.6,' (p-p VDW)'/
-      & 'ESTR=  ',1pE16.6,' WEIGHT=',1pD16.6,' (stretching)'/
-      & 'EBE=   ',1pE16.6,' WEIGHT=',1pD16.6,' (bending)'/
-      & 'ESC=   ',1pE16.6,' WEIGHT=',1pD16.6,' (SC local)'/
-      & 'ETORS= ',1pE16.6,' WEIGHT=',1pD16.6,' (torsional)'/
-      & 'ETORSD=',1pE16.6,' WEIGHT=',1pD16.6,' (double torsional)'/
-      & 'EHPB=  ',1pE16.6,' WEIGHT=',1pD16.6,
+      & 'EVDW=  ',1pE16.6,' WEIGHT=',1pE16.6,' (SC-SC)'/
+      & 'EVDW2= ',1pE16.6,' WEIGHT=',1pE16.6,' (SC-p)'/
+      & 'EES=   ',1pE16.6,' WEIGHT=',1pE16.6,' (p-p)'/
+      & 'EVDWPP=',1pE16.6,' WEIGHT=',1pE16.6,' (p-p VDW)'/
+      & 'ESTR=  ',1pE16.6,' WEIGHT=',1pE16.6,' (stretching)'/
+      & 'EBE=   ',1pE16.6,' WEIGHT=',1pE16.6,' (bending)'/
+      & 'ESC=   ',1pE16.6,' WEIGHT=',1pE16.6,' (SC local)'/
+      & 'ETORS= ',1pE16.6,' WEIGHT=',1pE16.6,' (torsional)'/
+      & 'ETORSD=',1pE16.6,' WEIGHT=',1pE16.6,' (double torsional)'/
+      & 'EHPB=  ',1pE16.6,' WEIGHT=',1pE16.6,
       & ' (SS bridges & dist. cnstr.)'/
-      & 'ECORR4=',1pE16.6,' WEIGHT=',1pD16.6,' (multi-body)'/
-      & 'ECORR5=',1pE16.6,' WEIGHT=',1pD16.6,' (multi-body)'/
-      & 'ECORR6=',1pE16.6,' WEIGHT=',1pD16.6,' (multi-body)'/
-      & 'EELLO= ',1pE16.6,' WEIGHT=',1pD16.6,' (electrostatic-local)'/
-      & 'ETURN3=',1pE16.6,' WEIGHT=',1pD16.6,' (turns, 3rd order)'/
-      & 'ETURN4=',1pE16.6,' WEIGHT=',1pD16.6,' (turns, 4th order)'/
-      & 'ETURN6=',1pE16.6,' WEIGHT=',1pD16.6,' (turns, 6th order)'/
-      & 'ESCCOR=',1pE16.6,' WEIGHT=',1pD16.6,' (backbone-rotamer corr)'/
+      & 'ECORR4=',1pE16.6,' WEIGHT=',1pE16.6,' (multi-body)'/
+      & 'ECORR5=',1pE16.6,' WEIGHT=',1pE16.6,' (multi-body)'/
+      & 'ECORR6=',1pE16.6,' WEIGHT=',1pE16.6,' (multi-body)'/
+      & 'EELLO= ',1pE16.6,' WEIGHT=',1pE16.6,' (electrostatic-local)'/
+      & 'ETURN3=',1pE16.6,' WEIGHT=',1pE16.6,' (turns, 3rd order)'/
+      & 'ETURN4=',1pE16.6,' WEIGHT=',1pE16.6,' (turns, 4th order)'/
+      & 'ETURN6=',1pE16.6,' WEIGHT=',1pE16.6,' (turns, 6th order)'/
+      & 'ESCCOR=',1pE16.6,' WEIGHT=',1pE16.6,' (backbone-rotamer corr)'/
       & 'EDIHC= ',1pE16.6,' (dihedral angle constraints)'/
       & 'ESS=   ',1pE16.6,' (disulfide-bridge intrinsic energy)'/
       & 'UCONST= ',1pE16.6,' (Constraint energy)'/ 
  c      write(iout,*)'Entering ELJ nnt=',nnt,' nct=',nct,' expon=',expon
        evdw=0.0D0
        do i=iatsc_s,iatsc_e
-         itypi=iabs(itype(i))
-         itypi1=iabs(itype(i+1))
+         itypi=itype(i)
+         itypi1=itype(i+1)
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
  cd        write (iout,*) 'i=',i,' iint=',iint,' istart=',istart(i,iint),
  cd   &                  'iend=',iend(i,iint)
            do j=istart(i,iint),iend(i,iint)
-             itypj=iabs(itype(j))
+             itypj=itype(j)
              xj=c(1,nres+j)-xi
              yj=c(2,nres+j)-yi
              zj=c(3,nres+j)-zi
  c     print *,'Entering ELJK nnt=',nnt,' nct=',nct,' expon=',expon
        evdw=0.0D0
        do i=iatsc_s,iatsc_e
-         itypi=iabs(itype(i))
-         itypi1=iabs(itype(i+1))
+         itypi=itype(i)
+         itypi1=itype(i+1)
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
@@@ -1324,7 -1326,7 +1326,7 @@@ C Calculate SC interaction energy
  C
          do iint=1,nint_gr(i)
            do j=istart(i,iint),iend(i,iint)
-             itypj=iabs(itype(j))
+             itypj=itype(j)
              xj=c(1,nres+j)-xi
              yj=c(2,nres+j)-yi
              zj=c(3,nres+j)-zi
@@@ -1431,8 -1433,8 +1433,8 @@@ c     els
  c     endif
        ind=0
        do i=iatsc_s,iatsc_e
-         itypi=iabs(itype(i))
-         itypi1=iabs(itype(i+1))
+         itypi=itype(i)
+         itypi1=itype(i+1)
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
          do iint=1,nint_gr(i)
            do j=istart(i,iint),iend(i,iint)
              ind=ind+1
 -            itypj=itype(j)
 +            itypj=iabs(itype(j))
  c            dscj_inv=dsc_inv(itypj)
              dscj_inv=vbld_inv(j+nres)
              chi1=chi(itypi,itypj)
        include 'COMMON.IOUNITS'
        include 'COMMON.CALC'
        include 'COMMON.CONTROL'
+       include 'COMMON.SBRIDGE'
        logical lprn
        evdw=0.0D0
  ccccc      energy_dec=.false.
@@@ -1567,8 -1570,8 +1570,8 @@@ c     print *,'Entering EGB nnt=',nnt,
  c     if (icall.eq.0) lprn=.false.
        ind=0
        do i=iatsc_s,iatsc_e
-         itypi=iabs(itype(i))
-         itypi1=iabs(itype(i+1))
+         itypi=itype(i)
+         itypi1=itype(i+1)
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
@@@ -1584,8 -1587,14 +1587,14 @@@ 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
+               if (energy_dec) write (iout,'(a6,2i5,0pf7.3,a3)') 
+      &                        'evdw',i,j,evdwij,' ss'
+             ELSE
              ind=ind+1
-             itypj=iabs(itype(j))
+             itypj=itype(j)
  c            dscj_inv=dsc_inv(itypj)
              dscj_inv=vbld_inv(j+nres)
  c            write (iout,*) "j",j,dsc_inv(itypj),dscj_inv,
@@@ -1693,6 -1702,7 +1702,7 @@@ C Calculate angular part of the gradien
  #else
              call sc_grad
  #endif
+             ENDIF    ! dyn_ss            
            enddo      ! j
          enddo        ! iint
        enddo          ! i
@@@ -1726,8 -1736,8 +1736,8 @@@ c     print *,'Entering EGB nnt=',nnt,
  c     if (icall.eq.0) lprn=.true.
        ind=0
        do i=iatsc_s,iatsc_e
-         itypi=iabs(itype(i))
-         itypi1=iabs(itype(i+1))
+         itypi=itype(i)
+         itypi1=itype(i+1)
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
          do iint=1,nint_gr(i)
            do j=istart(i,iint),iend(i,iint)
              ind=ind+1
-             itypj=iabs(itype(j))
+             itypj=itype(j)
  c            dscj_inv=dsc_inv(itypj)
              dscj_inv=vbld_inv(j+nres)
              sig0ij=sigma(itypi,itypj)
  cd    print *,'Entering Esoft_sphere nnt=',nnt,' nct=',nct
        evdw=0.0D0
        do i=iatsc_s,iatsc_e
-         itypi=iabs(itype(i))
-         itypi1=iabs(itype(i+1))
+         itypi=itype(i)
+         itypi1=itype(i+1)
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
  cd        write (iout,*) 'i=',i,' iint=',iint,' istart=',istart(i,iint),
  cd   &                  'iend=',iend(i,iint)
            do j=istart(i,iint),iend(i,iint)
-             itypj=iabs(itype(j))
+             itypj=itype(j)
              xj=c(1,nres+j)-xi
              yj=c(2,nres+j)-yi
              zj=c(3,nres+j)-zi
@@@ -4059,7 -4069,7 +4069,7 @@@ cd    write (iout,*) 'iatscp_s=',iatscp
          do iint=1,nscp_gr(i)
  
          do j=iscpstart(i,iint),iscpend(i,iint)
-           itypj=iabs(itype(j))
+           itypj=itype(j)
  C Uncomment following three lines for SC-p interactions
  c         xj=c(1,nres+j)-xi
  c         yj=c(2,nres+j)-yi
@@@ -4153,7 -4163,7 +4163,7 @@@ cd    write (iout,*) 'iatscp_s=',iatscp
          do iint=1,nscp_gr(i)
  
          do j=iscpstart(i,iint),iscpend(i,iint)
-           itypj=iabs(itype(j))
+           itypj=itype(j)
  C Uncomment following three lines for SC-p interactions
  c         xj=c(1,nres+j)-xi
  c         yj=c(2,nres+j)-yi
@@@ -4270,10 -4280,15 +4280,15 @@@ 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.
-         if (ii.gt.nres .and. iabs(itype(iii)).eq.1 .and. 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 
            call ssbond_ene(iii,jjj,eij)
            ehpb=ehpb+2*eij
+          endif
  cd          write (iout,*) "eij",eij
          else if (ii.gt.nres .and. jj.gt.nres) then
  c Restraints from contact prediction
        include 'COMMON.VAR'
        include 'COMMON.IOUNITS'
        double precision erij(3),dcosom1(3),dcosom2(3),gg(3)
-       itypi=iabs(itype(i))
+       itypi=itype(i)
        xi=c(1,nres+i)
        yi=c(2,nres+i)
        zi=c(3,nres+i)
        dzi=dc_norm(3,nres+i)
  c      dsci_inv=dsc_inv(itypi)
        dsci_inv=vbld_inv(nres+i)
-       itypj=iabs(itype(j))
+       itypj=itype(j)
  c      dscj_inv=dsc_inv(itypj)
        dscj_inv=vbld_inv(nres+j)
        xj=c(1,nres+j)-xi
        deltat12=om2-om1+2.0d0
        cosphi=om12-om1*om2
        eij=akcm*deltad*deltad+akth*(deltat1*deltat1+deltat2*deltat2)
-      &  +akct*deltad*deltat12
+      &  +akct*deltad*deltat12+ebr
       &  +v1ss*cosphi+v2ss*cosphi*cosphi+v3ss*cosphi*cosphi*cosphi
  c      write(iout,*) i,j,"rij",rij,"d0cm",d0cm," akcm",akcm," akth",akth,
  c     &  " akct",akct," deltad",deltad," deltat",deltat1,deltat2,
  c 09/18/07 AL: multimodal bond potential based on AM1 CA-SC PMF's included
  c
        do i=ibond_start,ibond_end
-         iti=iabs(itype(i))
+         iti=itype(i)
          if (iti.ne.10) then
            nbi=nbondterm(iti)
            if (nbi.eq.1) then
@@@ -4553,19 -4568,7 +4568,7 @@@ c     write (*,'(a,i2)') 'EBEND ICG=',i
        do i=ithet_start,ithet_end
  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))
-         ichir1=isign(1,itype(i-2))
-         ichir2=isign(1,itype(i))
-         if (itype(i-2).eq.10) ichir1=isign(1,itype(i-1))
-         if (itype(i).eq.10) ichir2=isign(1,itype(i-1))
-         if (itype(i-1).eq.10) then
-          itype1=isign(10,itype(i-2))
-          ichir11=isign(1,itype(i-2))
-          ichir12=isign(1,itype(i-2))
-          itype2=isign(10,itype(i))
-          ichir21=isign(1,itype(i))
-          ichir22=isign(1,itype(i))
-         endif
+         it=itype(i-1)
          if (i.gt.3) then
  #ifdef OSF
          phii=phi(i)
@@@ -4599,27 -4602,15 +4602,15 @@@ C dependent on the adjacent virtual-bon
  C In following comments this theta will be referred to as t_c.
          thet_pred_mean=0.0d0
          do k=1,2
-           athetk=athet(k,it,ichir1,ichir2)
-           bthetk=bthet(k,it,ichir1,ichir2)
-         if (it.eq.10) then
-            athetk=athet(k,itype1,ichir11,ichir12)
-            bthetk=bthet(k,itype2,ichir21,ichir22)
-         endif
+           athetk=athet(k,it)
+           bthetk=bthet(k,it)
            thet_pred_mean=thet_pred_mean+athetk*y(k)+bthetk*z(k)
          enddo
          dthett=thet_pred_mean*ssd
          thet_pred_mean=thet_pred_mean*ss+a0thet(it)
  C Derivatives of the "mean" values in gamma1 and gamma2.
-         dthetg1=(-athet(1,it,ichir1,ichir2)*y(2)
-      &+athet(2,it,ichir1,ichir2)*y(1))*ss
-         dthetg2=(-bthet(1,it,ichir1,ichir2)*z(2)
-      &          +bthet(2,it,ichir1,ichir2)*z(1))*ss
-         if (it.eq.10) then
-       dthetg1=(-athet(1,itype1,ichir11,ichir12)*y(2)
-      &+athet(2,itype1,ichir11,ichir12)*y(1))*ss
-         dthetg2=(-bthet(1,itype2,ichir21,ichir22)*z(2)
-      &         +bthet(2,itype2,ichir21,ichir22)*z(1))*ss
-         endif
+         dthetg1=(-athet(1,it)*y(2)+athet(2,it)*y(1))*ss
+         dthetg2=(-bthet(1,it)*z(2)+bthet(2,it)*z(1))*ss
          if (theta(i).gt.pi-delta) then
            call theteng(pi-delta,thet_pred_mean,theta0(it),f0,fprim0,
       &         E_tc0)
            enddo 
          endif
          if (i.lt.nres) then
 +
 +        if (iabs(itype(i+1)).eq.20) iblock=2
 +        if (iabs(itype(i+1)).ne.20) iblock=1
  #ifdef OSF
            phii1=phi(i+1)
            if (phii1.ne.phii1) phii1=150.0
              sinph2(k)=0.0d0
            enddo
          endif  
 -        ethetai=aa0thet(ityp1,ityp2,ityp3)
 +         ethetai=aa0thet(ityp1,ityp2,ityp3,iblock)
          do k=1,ndouble
            do l=1,k-1
              ccl=cosph1(l)*cosph2(k-l)
          enddo
          endif
          do k=1,ntheterm
 -          ethetai=ethetai+aathet(k,ityp1,ityp2,ityp3)*sinkt(k)
 -          dethetai=dethetai+0.5d0*k*aathet(k,ityp1,ityp2,ityp3)
 +          ethetai=ethetai+aathet(k,ityp1,ityp2,ityp3,iblock)*sinkt(k)
 +          dethetai=dethetai+0.5d0*k*aathet(k,ityp1,ityp2,ityp3,iblock)
       &      *coskt(k)
            if (lprn)
 -     &    write (iout,*) "k",k," aathet",aathet(k,ityp1,ityp2,ityp3),
 +     &    write (iout,*) "k",k,
 +     &    "aathet",aathet(k,ityp1,ityp2,ityp3,iblock),
       &     " ethetai",ethetai
          enddo
          if (lprn) then
          endif
          do m=1,ntheterm2
            do k=1,nsingle
 -            aux=bbthet(k,m,ityp1,ityp2,ityp3)*cosph1(k)
 -     &         +ccthet(k,m,ityp1,ityp2,ityp3)*sinph1(k)
 -     &         +ddthet(k,m,ityp1,ityp2,ityp3)*cosph2(k)
 -     &         +eethet(k,m,ityp1,ityp2,ityp3)*sinph2(k)
 +            aux=bbthet(k,m,ityp1,ityp2,ityp3,iblock)*cosph1(k)
 +     &         +ccthet(k,m,ityp1,ityp2,ityp3,iblock)*sinph1(k)
 +     &         +ddthet(k,m,ityp1,ityp2,ityp3,iblock)*cosph2(k)
 +     &         +eethet(k,m,ityp1,ityp2,ityp3,iblock)*sinph2(k)
              ethetai=ethetai+sinkt(m)*aux
              dethetai=dethetai+0.5d0*m*aux*coskt(m)
              dephii=dephii+k*sinkt(m)*(
 -     &          ccthet(k,m,ityp1,ityp2,ityp3)*cosph1(k)-
 -     &          bbthet(k,m,ityp1,ityp2,ityp3)*sinph1(k))
 +     &          ccthet(k,m,ityp1,ityp2,ityp3,iblock)*cosph1(k)-
 +     &          bbthet(k,m,ityp1,ityp2,ityp3,iblock)*sinph1(k))
              dephii1=dephii1+k*sinkt(m)*(
 -     &          eethet(k,m,ityp1,ityp2,ityp3)*cosph2(k)-
 -     &          ddthet(k,m,ityp1,ityp2,ityp3)*sinph2(k))
 +     &          eethet(k,m,ityp1,ityp2,ityp3,iblock)*cosph2(k)-
 +     &          ddthet(k,m,ityp1,ityp2,ityp3,iblock)*sinph2(k))
              if (lprn)
       &      write (iout,*) "m",m," k",k," bbthet",
 -     &         bbthet(k,m,ityp1,ityp2,ityp3)," ccthet",
 -     &         ccthet(k,m,ityp1,ityp2,ityp3)," ddthet",
 -     &         ddthet(k,m,ityp1,ityp2,ityp3)," eethet",
 -     &         eethet(k,m,ityp1,ityp2,ityp3)," ethetai",ethetai
 +     &         bbthet(k,m,ityp1,ityp2,ityp3,iblock)," ccthet",
 +     &         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
            enddo
          enddo
          if (lprn)
          do m=1,ntheterm3
            do k=2,ndouble
              do l=1,k-1
 -              aux=ffthet(l,k,m,ityp1,ityp2,ityp3)*cosph1ph2(l,k)+
 -     &            ffthet(k,l,m,ityp1,ityp2,ityp3)*cosph1ph2(k,l)+
 -     &            ggthet(l,k,m,ityp1,ityp2,ityp3)*sinph1ph2(l,k)+
 -     &            ggthet(k,l,m,ityp1,ityp2,ityp3)*sinph1ph2(k,l)
 +       aux=ffthet(l,k,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(l,k)+
 +     & ffthet(k,l,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(k,l)+
 +     & ggthet(l,k,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(l,k)+
 +     & ggthet(k,l,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(k,l)
 +
                ethetai=ethetai+sinkt(m)*aux
                dethetai=dethetai+0.5d0*m*coskt(m)*aux
                dephii=dephii+l*sinkt(m)*(
 -     &           -ffthet(l,k,m,ityp1,ityp2,ityp3)*sinph1ph2(l,k)-
 -     &            ffthet(k,l,m,ityp1,ityp2,ityp3)*sinph1ph2(k,l)+
 -     &            ggthet(l,k,m,ityp1,ityp2,ityp3)*cosph1ph2(l,k)+
 -     &            ggthet(k,l,m,ityp1,ityp2,ityp3)*cosph1ph2(k,l))
 +     & -ffthet(l,k,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(l,k)-
 +     &  ffthet(k,l,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(k,l)+
 +     &  ggthet(l,k,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(l,k)+
 +     &  ggthet(k,l,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(k,l))
 +
                dephii1=dephii1+(k-l)*sinkt(m)*(
 -     &           -ffthet(l,k,m,ityp1,ityp2,ityp3)*sinph1ph2(l,k)+
 -     &            ffthet(k,l,m,ityp1,ityp2,ityp3)*sinph1ph2(k,l)+
 -     &            ggthet(l,k,m,ityp1,ityp2,ityp3)*cosph1ph2(l,k)-
 -     &            ggthet(k,l,m,ityp1,ityp2,ityp3)*cosph1ph2(k,l))
 +     &-ffthet(l,k,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(l,k)+
 +     & ffthet(k,l,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(k,l)+
 +     & ggthet(l,k,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(l,k)-
 +     & ggthet(k,l,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(k,l))
 +
                if (lprn) then
                write (iout,*) "m",m," k",k," l",l," ffthet",
 -     &            ffthet(l,k,m,ityp1,ityp2,ityp3),
 -     &            ffthet(k,l,m,ityp1,ityp2,ityp3)," ggthet",
 -     &            ggthet(l,k,m,ityp1,ityp2,ityp3),
 -     &            ggthet(k,l,m,ityp1,ityp2,ityp3)," ethetai",ethetai
 +     &            ffthet(l,k,m,ityp1,ityp2,ityp3,iblock),
 +     &            ffthet(k,l,m,ityp1,ityp2,ityp3,iblock)," ggthet",
 +     &            ggthet(l,k,m,ityp1,ityp2,ityp3,iblock),
 +     &            ggthet(k,l,m,ityp1,ityp2,ityp3,iblock),
 +     &            " ethetai",ethetai
 +
                write (iout,*) cosph1ph2(l,k)*sinkt(m),
       &            cosph1ph2(k,l)*sinkt(m),
       &            sinph1ph2(l,k)*sinkt(m),sinph1ph2(k,l)*sinkt(m)
@@@ -4984,7 -4966,7 +4975,7 @@@ c     write (iout,'(a)') 'ESC
        do i=loc_start,loc_end
          it=itype(i)
          if (it.eq.10) goto 1
-         nlobit=nlob(iabs(it))
+         nlobit=nlob(it)
  c       print *,'i=',i,' it=',it,' nlobit=',nlobit
  c       write (iout,*) 'i=',i,' ssa=',ssa,' ssad=',ssad
          theti=theta(i+1)-pipol
@@@ -5141,11 -5123,11 +5132,11 @@@ C Compute the contribution to SC energ
  
            do j=1,nlobit
  #ifdef OSF
-             adexp=bsc(j,iabs(it))-0.5D0*contr(j,iii)+emin
+             adexp=bsc(j,it)-0.5D0*contr(j,iii)+emin
              if(adexp.ne.adexp) adexp=1.0
              expfac=dexp(adexp)
  #else
-             expfac=dexp(bsc(j,iabs(it))-0.5D0*contr(j,iii)+emin)
+             expfac=dexp(bsc(j,it)-0.5D0*contr(j,iii)+emin)
  #endif
  cd          print *,'j=',j,' expfac=',expfac
              escloc_i=escloc_i+expfac
@@@ -5227,7 -5209,7 +5218,7 @@@ C Compute the contribution to SC energ
  
        dersc12=0.0d0
        do j=1,nlobit
-         expfac=dexp(bsc(j,iabs(it))-0.5D0*contr(j)+emin)
+         expfac=dexp(bsc(j,it)-0.5D0*contr(j)+emin)
          escloc_i=escloc_i+expfac
          do k=1,2
            dersc(k)=dersc(k)+Ax(k,j)*expfac
          cosfac=dsqrt(cosfac2)
          sinfac2=0.5d0/(1.0d0-costtab(i+1))
          sinfac=dsqrt(sinfac2)
 -        it=itype(i)
 +        it=iabs(itype(i))
          if (it.eq.10) goto 1
  c
  C  Compute the axes of tghe local cartesian coordinates system; store in
@@@ -5307,7 -5289,7 +5298,7 @@@ C     &   dc_norm(3,i+nres
            y_prime(j) = (dc_norm(j,i) + dc_norm(j,i-1))*sinfac
          enddo
          do j = 1,3
 -          z_prime(j) = -uz(j,i-1)
 +          z_prime(j) = -uz(j,i-1)*dsign(1.0d0,dfloat(itype(i)))
          enddo     
  c       write (2,*) "i",i
  c       write (2,*) "x_prime",(x_prime(j),j=1,3)
  C Compute the energy of the ith side cbain
  C
  c        write (2,*) "xx",xx," yy",yy," zz",zz
 -        it=itype(i)
 +        it=iabs(itype(i))
          do j = 1,65
            x(j) = sc_parmin(j,it) 
          enddo
  Cc diagnostics - remove later
          xx1 = dcos(alph(2))
          yy1 = dsin(alph(2))*dcos(omeg(2))
 -        zz1 = -dsin(alph(2))*dsin(omeg(2))
 +        zz1 = -dsign(1.0, dfloat(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
@@@ -5517,10 -5499,8 +5508,10 @@@ c     &   (dC_norm(j,i-1),j=1,3)," vbld
           dZZ_Ci1(k)=0.0d0
           dZZ_Ci(k)=0.0d0
           do j=1,3
 -           dZZ_Ci(k)=dZZ_Ci(k)-uzgrad(j,k,2,i-1)*dC_norm(j,i+nres)
 -           dZZ_Ci1(k)=dZZ_Ci1(k)-uzgrad(j,k,1,i-1)*dC_norm(j,i+nres)
 +           dZZ_Ci(k)=dZZ_Ci(k)-uzgrad(j,k,2,i-1)
 +     &     *dsign(1.0d0,dfloat(itype(i)))*dC_norm(j,i+nres)
 +           dZZ_Ci1(k)=dZZ_Ci1(k)-uzgrad(j,k,1,i-1)
 +     &     *dsign(1.0d0,dfloat(itype(i)))*dC_norm(j,i+nres)
           enddo
            
           dXX_XYZ(k)=vbld_inv(i+nres)*(x_prime(k)-xx*dC_norm(k,i+nres))
@@@ -5806,21 -5786,14 +5797,16 @@@ c     lprn=.true
        etors=0.0D0
        do i=iphi_start,iphi_end
        etors_ii=0.0D0
 +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
          itori=itortyp(itype(i-2))
          itori1=itortyp(itype(i-1))
          phii=phi(i)
          gloci=0.0D0
  C Regular cosine and sine terms
-         do j=1,nterm(itori,itori1,iblock)
-           v1ij=v1(j,itori,itori1,iblock)
-           v2ij=v2(j,itori,itori1,iblock)
+         do j=1,nterm(itori,itori1)
+           v1ij=v1(j,itori,itori1)
+           v2ij=v2(j,itori,itori1)
            cosphi=dcos(j*phii)
            sinphi=dsin(j*phii)
            etors=etors+v1ij*cosphi+v2ij*sinphi
@@@ -5835,7 -5808,7 +5821,7 @@@ C          [v2 cos(phi/2)+v3 sin(phi/2)
  C
          cosphi=dcos(0.5d0*phii)
          sinphi=dsin(0.5d0*phii)
-         do j=1,nlor(itori,itori1,iblock)
+         do j=1,nlor(itori,itori1)
            vl1ij=vlor1(j,itori,itori1)
            vl2ij=vlor2(j,itori,itori1)
            vl3ij=vlor3(j,itori,itori1)
            gloci=gloci+vl1ij*(vl3ij*cosphi-vl2ij*sinphi)*pom
          enddo
  C Subtract the constant term
-         etors=etors-v0(itori,itori1,iblock)
+         etors=etors-v0(itori,itori1)
            if (energy_dec) write (iout,'(a6,i5,0pf7.3)')
-      &         'etor',i,etors_ii-v0(itori,itori1,iblock)
+      &         'etor',i,etors_ii-v0(itori,itori1)
          if (lprn)
       &  write (iout,'(2(a3,2x,i3,2x),2i3,6f8.3/26x,6f8.3/)')
       &  restyp(itype(i-2)),i-2,restyp(itype(i-1)),i-1,itori,itori1,
-      &  (v1(j,itori,itori1,iblock),j=1,6),
-      &  (v2(j,itori,itori1,iblock),j=1,6)
+      &  (v1(j,itori,itori1),j=1,6),(v2(j,itori,itori1),j=1,6)
          gloc(i-3,icg)=gloc(i-3,icg)+wtor*gloci
  c       write (iout,*) 'i=',i,' gloc=',gloc(i-3,icg)
        enddo
@@@ -5907,22 -5879,18 +5892,20 @@@ C Set lprn=.true. for debuggin
  c     lprn=.true.
        etors_d=0.0D0
        do i=iphid_start,iphid_end
 +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
          itori=itortyp(itype(i-2))
          itori1=itortyp(itype(i-1))
          itori2=itortyp(itype(i))
-         iblock=1
-         if (iabs(itype(i+1)).eq.20) iblock=2
          phii=phi(i)
          phii1=phi(i+1)
          gloci1=0.0D0
          gloci2=0.0D0
-         do j=1,ntermd_1(itori,itori1,itori2,iblock)
-           v1cij=v1c(1,j,itori,itori1,itori2,iblock)
-           v1sij=v1s(1,j,itori,itori1,itori2,iblock)
-           v2cij=v1c(2,j,itori,itori1,itori2,iblock)
-           v2sij=v1s(2,j,itori,itori1,itori2,iblock)
+         do j=1,ntermd_1(itori,itori1,itori2)
+           v1cij=v1c(1,j,itori,itori1,itori2)
+           v1sij=v1s(1,j,itori,itori1,itori2)
+           v2cij=v1c(2,j,itori,itori1,itori2)
+           v2sij=v1s(2,j,itori,itori1,itori2)
            cosphi1=dcos(j*phii)
            sinphi1=dsin(j*phii)
            cosphi2=dcos(j*phii1)
            gloci1=gloci1+j*(v1sij*cosphi1-v1cij*sinphi1)
            gloci2=gloci2+j*(v2sij*cosphi2-v2cij*sinphi2)
          enddo
-         do k=2,ntermd_2(itori,itori1,itori2,iblock)
+         do k=2,ntermd_2(itori,itori1,itori2)
            do l=1,k-1
-             v1cdij = v2c(k,l,itori,itori1,itori2,iblock)
-             v2cdij = v2c(l,k,itori,itori1,itori2,iblock)
-             v1sdij = v2s(k,l,itori,itori1,itori2,iblock)
-             v2sdij = v2s(l,k,itori,itori1,itori2,iblock)
+             v1cdij = v2c(k,l,itori,itori1,itori2)
+             v2cdij = v2c(l,k,itori,itori1,itori2)
+             v1sdij = v2s(k,l,itori,itori1,itori2)
+             v2sdij = v2s(l,k,itori,itori1,itori2)
              cosphi1p2=dcos(l*phii+(k-l)*phii1)
              cosphi1m2=dcos(l*phii-(k-l)*phii1)
              sinphi1p2=dsin(l*phii+(k-l)*phii1)
@@@ -5983,11 -5951,10 +5966,11 @@@ c        amino-acid residues
  C Set lprn=.true. for debugging
        lprn=.false.
  c      lprn=.true.
 -c      write (iout,*) "EBACK_SC_COR",iphi_start,iphi_end,nterm_sccor
 +c     write (iout,*) "EBACK_SC_COR",itau_start,itau_end
        esccor=0.0D0
        do i=itau_start,itau_end
          esccor_ii=0.0D0
 +        if ((itype(i-2).eq.ntyp1).or.(itype(i-1).eq.ntyp1)) cycle
          isccori=isccortyp(itype(i-2))
          isccori1=isccortyp(itype(i-1))
          phii=phi(i)
@@@ -6006,16 -5973,14 +5989,16 @@@ c   2 = Ca...Ca...Ca...S
  c   3 = SC...Ca...Ca...SCi
          gloci=0.0D0
          if (((intertyp.eq.3).and.((itype(i-2).eq.10).or.
 -     &      (itype(i-1).eq.10).or.(itype(i-2).eq.21).or.
 -     &      (itype(i-1).eq.21)))
 +     &      (itype(i-1).eq.10).or.(itype(i-2).eq.ntyp1).or.
 +     &      (itype(i-1).eq.ntyp1)))
       &    .or. ((intertyp.eq.1).and.((itype(i-2).eq.10)
 -     &     .or.(itype(i-2).eq.21)))
 +     &     .or.(itype(i-2).eq.ntyp1).or.(itype(i-1).eq.ntyp1)
 +     &     .or.(itype(i).eq.ntyp1)))
       &    .or.((intertyp.eq.2).and.((itype(i-1).eq.10).or.
 -     &      (itype(i-1).eq.21)))) cycle  
 -        if ((intertyp.eq.2).and.(i.eq.4).and.(itype(1).eq.21)) cycle
 -        if ((intertyp.eq.1).and.(i.eq.nres).and.(itype(nres).eq.21))
 +     &      (itype(i-1).eq.ntyp1).or.(itype(i-2).eq.ntyp1).or.
 +     &      (itype(i-3).eq.ntyp1)))) cycle
 +        if ((intertyp.eq.2).and.(i.eq.4).and.(itype(1).eq.ntyp1)) cycle
 +        if ((intertyp.eq.1).and.(i.eq.nres).and.(itype(nres).eq.ntyp1))
       & cycle
          do j=1,nterm_sccor(isccori,isccori1)
            v1ij=v1sccor(j,intertyp,isccori,isccori1)
@@@ -6030,9 -5995,9 +6013,9 @@@ c        write (iout,*) "WTF",intertyp,
  c     &gloc_sc(intertyp,i-3,icg)
          if (lprn)
       &  write (iout,'(2(a3,2x,i3,2x),2i3,6f8.3/26x,6f8.3/)')
 -     &  restyp(itype(i-2)),i-2,restyp(itype(i-1)),i-1,itori,itori1,
 -     &  (v1sccor(j,intertyp,itori,itori1),j=1,6)
 -     & ,(v2sccor(j,intertyp,itori,itori1),j=1,6)
 +     &  restyp(itype(i-2)),i-2,restyp(itype(i-1)),i-1,isccori,isccori1,
 +     &  (v1sccor(j,intertyp,isccori,isccori1),j=1,6)
 +     & ,(v2sccor(j,intertyp,isccori,isccori1),j=1,6)
          gsccor_loc(i-3)=gsccor_loc(i-3)+gloci
         enddo !intertyp
        enddo
@@@ -8242,7 -8207,7 +8225,7 @@@ c--------------------------------------
        include 'COMMON.GEO'
        logical swap
        double precision vv(2),pizda(2,2),auxmat(2,2),auxvec(2),
-      & auxvec1(2),auxvec2(1),auxmat1(2,2)
+      & auxvec1(2),auxvec2(2),auxmat1(2,2)
        logical lprn
        common /kutas/ lprn
  CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
@@@ -28,7 -28,6 +28,6 @@@
        include 'COMMON.SETUP'
        character*1 t1,t2,t3
        character*1 onelett(4) /"G","A","P","D"/
-       character*1 toronelet(-2:2) /"p","a","G","A","P"/
        logical lprint,LaTeX
        dimension blower(3,3,maxlob)
        dimension b(13)
@@@ -103,47 -102,13 +102,13 @@@ C Read the parameters of the probabilit
  C of the virtual-bond valence angles theta
  C
        do i=1,ntyp
-         read (ithep,*,err=111,end=111) a0thet(i),(athet(j,i,1,1),j=1,2),
-      &    (bthet(j,i,1,1),j=1,2)
+         read (ithep,*,err=111,end=111) a0thet(i),(athet(j,i),j=1,2),
+      &    (bthet(j,i),j=1,2)
          read (ithep,*,err=111,end=111) (polthet(j,i),j=0,3)
        read (ithep,*,err=111,end=111) (gthet(j,i),j=1,3)
        read (ithep,*,err=111,end=111) theta0(i),sig0(i),sigc0(i)
        sigc0(i)=sigc0(i)**2
        enddo
-       do i=1,ntyp
-       athet(1,i,1,-1)=athet(1,i,1,1)
-       athet(2,i,1,-1)=athet(2,i,1,1)
-       bthet(1,i,1,-1)=-bthet(1,i,1,1)
-       bthet(2,i,1,-1)=-bthet(2,i,1,1)
-       athet(1,i,-1,1)=-athet(1,i,1,1)
-       athet(2,i,-1,1)=-athet(2,i,1,1)
-       bthet(1,i,-1,1)=bthet(1,i,1,1)
-       bthet(2,i,-1,1)=bthet(2,i,1,1)
-       enddo
-       do i=-ntyp,-1
-       a0thet(i)=a0thet(-i)
-       athet(1,i,-1,-1)=athet(1,-i,1,1)
-       athet(2,i,-1,-1)=-athet(2,-i,1,1)
-       bthet(1,i,-1,-1)=bthet(1,-i,1,1)
-       bthet(2,i,-1,-1)=-bthet(2,-i,1,1)
-       athet(1,i,-1,1)=athet(1,-i,1,1)
-       athet(2,i,-1,1)=-athet(2,-i,1,1)
-       bthet(1,i,-1,1)=-bthet(1,-i,1,1)
-       bthet(2,i,-1,1)=bthet(2,-i,1,1)
-       athet(1,i,1,-1)=-athet(1,-i,1,1)
-       athet(2,i,1,-1)=athet(2,-i,1,1)
-       bthet(1,i,1,-1)=bthet(1,-i,1,1)
-       bthet(2,i,1,-1)=-bthet(2,-i,1,1)
-       theta0(i)=theta0(-i)
-       sig0(i)=sig0(-i)
-       sigc0(i)=sigc0(-i)
-        do j=0,3
-         polthet(j,i)=polthet(j,-i)
-        enddo
-        do j=1,3
-          gthet(j,i)=gthet(j,-i)
-        enddo
-       enddo
        close (ithep)
        if (lprint) then
        if (.not.LaTeX) then
       & '        B1    ','         B2   '        
          do i=1,ntyp
            write(iout,'(a3,i4,2x,5(1pe14.5))') restyp(i),i,
-      &        a0thet(i),(athet(j,i,1,1),j=1,2),(bthet(j,i,1,1),j=1,2)
+      &        a0thet(i),(athet(j,i),j=1,2),(bthet(j,i),j=1,2)
          enddo
          write (iout,'(/a/9x,5a/79(1h-))') 
       & 'Parameters of the expression for sigma(theta_c):',
       & '   b1*10^1    ','    b2*10^1   '        
          do i=1,ntyp
            write(iout,'(a3,1h&,2x,5(f8.3,1h&))') restyp(i),
-      &        a0thet(i),(100*athet(j,i,1,1),j=1,2),
-      &        (10*bthet(j,i,1,1),j=1,2)
+      &        a0thet(i),(100*athet(j,i),j=1,2),(10*bthet(j,i),j=1,2)
          enddo
        write (iout,'(/a/9x,5a/79(1h-))') 
       & 'Parameters of the expression for sigma(theta_c):',
       &  ntheterm3,nsingle,ndouble
        nntheterm=max0(ntheterm,ntheterm2,ntheterm3)
        read (ithep,*,err=111,end=111) (ithetyp(i),i=1,ntyp1)
 -      do i=1,maxthetyp
 -        do j=1,maxthetyp
 -          do k=1,maxthetyp
 -            aa0thet(i,j,k)=0.0d0
 +      do i=-ntyp1,-1
 +        ithetyp(i)=-ithetyp(-i)
 +      enddo
 +      do iblock=1,2
 +      do i=-maxthetyp,maxthetyp
 +        do j=-maxthetyp,maxthetyp
 +          do k=-maxthetyp,maxthetyp
 +            aa0thet(i,j,k,iblock)=0.0d0
              do l=1,ntheterm
 -              aathet(l,i,j,k)=0.0d0
 +              aathet(l,i,j,k,iblock)=0.0d0
              enddo
              do l=1,ntheterm2
                do m=1,nsingle
 -                bbthet(m,l,i,j,k)=0.0d0
 -                ccthet(m,l,i,j,k)=0.0d0
 -                ddthet(m,l,i,j,k)=0.0d0
 -                eethet(m,l,i,j,k)=0.0d0
 +                bbthet(m,l,i,j,k,iblock)=0.0d0
 +                ccthet(m,l,i,j,k,iblock)=0.0d0
 +                ddthet(m,l,i,j,k,iblock)=0.0d0
 +                eethet(m,l,i,j,k,iblock)=0.0d0
                enddo
              enddo
              do l=1,ntheterm3
                do m=1,ndouble
                  do mm=1,ndouble
 -                 ffthet(mm,m,l,i,j,k)=0.0d0
 -                 ggthet(mm,m,l,i,j,k)=0.0d0
 +                 ffthet(mm,m,l,i,j,k,iblock)=0.0d0
 +                 ggthet(mm,m,l,i,j,k,iblock)=0.0d0
                  enddo
                enddo
              enddo
            enddo
          enddo
 -      enddo 
 -      do i=1,nthetyp
 -        do j=1,nthetyp
 -          do k=1,nthetyp
 -            read (ithep,'(3a)',end=111,err=111) res1,res2,res3
 -            read (ithep,*,end=111,err=111) aa0thet(i,j,k)
 -            read (ithep,*,end=111,err=111)(aathet(l,i,j,k),l=1,ntheterm)
 +      enddo
 +      enddo
 +c VAR:iblock means terminally blocking group 1=non-proline 2=proline
 +      do iblock=1,2
 +c VAR:ntethtyp is type of theta potentials type currently 0=glycine 
 +c VAR:1=non-glicyne non-proline 2=proline
 +c VAR:negative values for D-aminoacid
 +      do i=0,nthetyp
 +        do j=-nthetyp,nthetyp
 +          do k=-nthetyp,nthetyp
 +            read (ithep,'(6a)',end=111,err=111) res1
 +c VAR: aa0thet is variable describing the average value of Foureir
 +c VAR: expansion series
 +            read (ithep,*,end=111,err=111) aa0thet(i,j,k,iblock)
 +c VAR: aathet is foureir expansion in theta/2 angle for full formula
 +c VAR: look at the fitting equation in Kozlowska et al., J. Phys.:
 +Condens. Matter 19 (2007) 285203 and Sieradzan et al., unpublished
 +            read (ithep,*,end=111,err=111)
 +     &(aathet(l,i,j,k,iblock),l=1,ntheterm)
              read (ithep,*,end=111,err=111)
 -     &       ((bbthet(lll,ll,i,j,k),lll=1,nsingle),
 -     &        (ccthet(lll,ll,i,j,k),lll=1,nsingle),
 -     &        (ddthet(lll,ll,i,j,k),lll=1,nsingle),
 -     &        (eethet(lll,ll,i,j,k),lll=1,nsingle),ll=1,ntheterm2)
 +     &       ((bbthet(lll,ll,i,j,k,iblock),lll=1,nsingle),
 +     &        (ccthet(lll,ll,i,j,k,iblock),lll=1,nsingle),
 +     &        (ddthet(lll,ll,i,j,k,iblock),lll=1,nsingle),
 +     &        (eethet(lll,ll,i,j,k,iblock),lll=1,nsingle),
 +     &        ll=1,ntheterm2)
              read (ithep,*,end=111,err=111)
 -     &      (((ffthet(llll,lll,ll,i,j,k),ffthet(lll,llll,ll,i,j,k),
 -     &         ggthet(llll,lll,ll,i,j,k),ggthet(lll,llll,ll,i,j,k),
 +     &      (((ffthet(llll,lll,ll,i,j,k,iblock),
 +     &         ffthet(lll,llll,ll,i,j,k,iblock),
 +     &         ggthet(llll,lll,ll,i,j,k,iblock),
 +     &         ggthet(lll,llll,ll,i,j,k,iblock),
       &         llll=1,lll-1),lll=2,ndouble),ll=1,ntheterm3)
            enddo
          enddo
        enddo
 +
 +
  C
  C For dummy ends assign glycine-type coefficients of theta-only terms; the
  C coefficients of theta-and-gamma-dependent terms are zero.
 -C
 +C IF YOU WANT VALENCE POTENTIALS FOR DUMMY ATOM UNCOMENT BELOW (NOT
 +C RECOMENTDED AFTER VERSION 3.3)
 +c      do i=1,nthetyp
 +c        do j=1,nthetyp
 +c          do l=1,ntheterm
 +c            aathet(l,i,j,nthetyp+1,iblock)=aathet(l,i,j,1,iblock)
 +c            aathet(l,nthetyp+1,i,j,iblock)=aathet(l,1,i,j,iblock)
 +c          enddo
 +c          aa0thet(i,j,nthetyp+1,iblock)=aa0thet(i,j,1,iblock)
 +c          aa0thet(nthetyp+1,i,j,iblock)=aa0thet(1,i,j,iblock)
 +c        enddo
 +c        do l=1,ntheterm
 +c          aathet(l,nthetyp+1,i,nthetyp+1,iblock)=aathet(l,1,i,1,iblock)
 +c        enddo
 +c        aa0thet(nthetyp+1,i,nthetyp+1,iblock)=aa0thet(1,i,1,iblock)
 +c      enddo
 +c      enddo
 +C AND COMMENT THE LOOPS BELOW
        do i=1,nthetyp
          do j=1,nthetyp
            do l=1,ntheterm
 -            aathet(l,i,j,nthetyp+1)=aathet(l,i,j,1)
 -            aathet(l,nthetyp+1,i,j)=aathet(l,1,i,j)
 +            aathet(l,i,j,nthetyp+1,iblock)=0.0d0
 +            aathet(l,nthetyp+1,i,j,iblock)=0.0d0
            enddo
 -          aa0thet(i,j,nthetyp+1)=aa0thet(i,j,1)
 -          aa0thet(nthetyp+1,i,j)=aa0thet(1,i,j)
 +          aa0thet(i,j,nthetyp+1,iblock)=0.0d0
 +          aa0thet(nthetyp+1,i,j,iblock)=0.0d0
          enddo
          do l=1,ntheterm
 -          aathet(l,nthetyp+1,i,nthetyp+1)=aathet(l,1,i,1)
 +          aathet(l,nthetyp+1,i,nthetyp+1,iblock)=0.0d0
          enddo
 -        aa0thet(nthetyp+1,i,nthetyp+1)=aa0thet(1,i,1)
 +        aa0thet(nthetyp+1,i,nthetyp+1,iblock)=0.0d0
        enddo
 +      enddo
 +C TILL HERE
 +C Substitution for D aminoacids from symmetry.
 +      do iblock=1,2
 +      do i=-nthetyp,0
 +        do j=-nthetyp,nthetyp
 +          do k=-nthetyp,nthetyp
 +           aa0thet(i,j,k,iblock)=aa0thet(-i,-j,-k,iblock)
 +           do l=1,ntheterm
 +           aathet(l,i,j,k,iblock)=aathet(l,-i,-j,-k,iblock)
 +           enddo
 +           do ll=1,ntheterm2
 +            do lll=1,nsingle
 +            bbthet(lll,ll,i,j,k,iblock)=bbthet(lll,ll,-i,-j,-k,iblock)
 +            ccthet(lll,ll,i,j,k,iblock)=-ccthet(lll,ll,-i,-j,-k,iblock)
 +            ddthet(lll,ll,i,j,k,iblock)=ddthet(lll,ll,-i,-j,-k,iblock)
 +            eethet(lll,ll,i,j,k,iblock)=-eethet(lll,ll,-i,-j,-k,iblock)
 +            enddo
 +          enddo
 +          do ll=1,ntheterm3
 +           do lll=2,ndouble
 +            do llll=1,lll-1
 +            ffthet(llll,lll,ll,i,j,k,iblock)=
 +     &      ffthet(llll,lll,ll,-i,-j,-k,iblock)
 +            ffthet(lll,llll,ll,i,j,k,iblock)=
 +     &      ffthet(lll,llll,ll,-i,-j,-k,iblock)
 +            ggthet(llll,lll,ll,i,j,k,iblock)=
 +     &      -ggthet(llll,lll,ll,-i,-j,-k,iblock)
 +            ggthet(lll,llll,ll,i,j,k,iblock)=
 +     &      -ggthet(lll,llll,ll,-i,-j,-k,iblock)
 +            enddo !ll
 +           enddo  !lll  
 +          enddo   !llll
 +         enddo    !k
 +        enddo     !j
 +       enddo      !i
 +      enddo       !iblock
  C
  C Control printout of the coefficients of virtual-bond-angle potentials
  C
          do i=1,nthetyp+1
            do j=1,nthetyp+1
              do k=1,nthetyp+1
 -              write (iout,'(//4a)') 
 -     &         'Type ',onelett(i),onelett(j),onelett(k) 
 +              write (iout,'(//4a)')
 +     &         'Type ',onelett(i),onelett(j),onelett(k)
                write (iout,'(//a,10x,a)') " l","a[l]"
 -              write (iout,'(i2,1pe15.5)') 0,aa0thet(i,j,k)
 +              write (iout,'(i2,1pe15.5)') 0,aa0thet(i,j,k,iblock)
                write (iout,'(i2,1pe15.5)')
 -     &           (l,aathet(l,i,j,k),l=1,ntheterm)
 +     &           (l,aathet(l,i,j,k,iblock),l=1,ntheterm)
              do l=1,ntheterm2
 -              write (iout,'(//2h m,4(9x,a,3h[m,,i1,1h]))') 
 +              write (iout,'(//2h m,4(9x,a,3h[m,,i1,1h]))')
       &          "b",l,"c",l,"d",l,"e",l
                do m=1,nsingle
                  write (iout,'(i2,4(1pe15.5))') m,
 -     &          bbthet(m,l,i,j,k),ccthet(m,l,i,j,k),
 -     &          ddthet(m,l,i,j,k),eethet(m,l,i,j,k)
 +     &          bbthet(m,l,i,j,k,iblock),ccthet(m,l,i,j,k,iblock),
 +     &          ddthet(m,l,i,j,k,iblock),eethet(m,l,i,j,k,iblock)
                enddo
              enddo
              do l=1,ntheterm3
                do m=2,ndouble
                  do n=1,m-1
                    write (iout,'(i1,1x,i1,4(1pe15.5))') n,m,
 -     &              ffthet(n,m,l,i,j,k),ffthet(m,n,l,i,j,k),
 -     &              ggthet(n,m,l,i,j,k),ggthet(m,n,l,i,j,k)
 +     &              ffthet(n,m,l,i,j,k,iblock),
 +     &              ffthet(m,n,l,i,j,k,iblock),
 +     &              ggthet(n,m,l,i,j,k,iblock),
 +     &              ggthet(m,n,l,i,j,k,iblock)
                  enddo
                enddo
              enddo
        endif
        write (2,*) "Start reading THETA_PDB"
        do i=1,ntyp
 -        read (ithep_pdb,*,err=111,end=111) a0thet(i),(athet(j,i),j=1,2),
 -     &    (bthet(j,i),j=1,2)
 +        read (ithep_pdb,*,err=111,end=111)
 +     &     a0thet(i),(athet(j,i,1,1),j=1,2),
 +     &    (bthet(j,i,1,1),j=1,2)
          read (ithep_pdb,*,err=111,end=111) (polthet(j,i),j=0,3)
        read (ithep_pdb,*,err=111,end=111) (gthet(j,i),j=1,3)
        read (ithep_pdb,*,err=111,end=111) theta0(i),sig0(i),sigc0(i)
        sigc0(i)=sigc0(i)**2
        enddo
 +      do i=1,ntyp
 +      athet(1,i,1,-1)=athet(1,i,1,1)
 +      athet(2,i,1,-1)=athet(2,i,1,1)
 +      bthet(1,i,1,-1)=-bthet(1,i,1,1)
 +      bthet(2,i,1,-1)=-bthet(2,i,1,1)
 +      athet(1,i,-1,1)=-athet(1,i,1,1)
 +      athet(2,i,-1,1)=-athet(2,i,1,1)
 +      bthet(1,i,-1,1)=bthet(1,i,1,1)
 +      bthet(2,i,-1,1)=bthet(2,i,1,1)
 +      enddo
 +      do i=-ntyp,-1
 +      a0thet(i)=a0thet(-i)
 +      athet(1,i,-1,-1)=athet(1,-i,1,1)
 +      athet(2,i,-1,-1)=-athet(2,-i,1,1)
 +      bthet(1,i,-1,-1)=bthet(1,-i,1,1)
 +      bthet(2,i,-1,-1)=-bthet(2,-i,1,1)
 +      athet(1,i,-1,1)=athet(1,-i,1,1)
 +      athet(2,i,-1,1)=-athet(2,-i,1,1)
 +      bthet(1,i,-1,1)=-bthet(1,-i,1,1)
 +      bthet(2,i,-1,1)=bthet(2,-i,1,1)
 +      athet(1,i,1,-1)=-athet(1,-i,1,1)
 +      athet(2,i,1,-1)=athet(2,-i,1,1)
 +      bthet(1,i,1,-1)=bthet(1,-i,1,1)
 +      bthet(2,i,1,-1)=-bthet(2,-i,1,1)
 +      theta0(i)=theta0(-i)
 +      sig0(i)=sig0(-i)
 +      sigc0(i)=sigc0(-i)
 +       do j=0,3
 +        polthet(j,i)=polthet(j,-i)
 +       enddo
 +       do j=1,3
 +         gthet(j,i)=gthet(j,-i)
 +       enddo
 +      enddo
        write (2,*) "End reading THETA_PDB"
        close (ithep_pdb)
  #endif
        bsc(1,i)=0.0D0
          read(irotam,*,end=112,err=112)(censc(k,1,i),k=1,3),
       &    ((blower(k,l,1),l=1,k),k=1,3)
-         censc(1,1,-i)=censc(1,1,i)
-         censc(2,1,-i)=censc(2,1,i)
-         censc(3,1,-i)=-censc(3,1,i)
-         
        do j=2,nlob(i)
          read (irotam,*,end=112,err=112) bsc(j,i)
          read (irotam,*,end=112,err=112) (censc(k,j,i),k=1,3),
       &                                 ((blower(k,l,j),l=1,k),k=1,3)
-         censc(1,j,-i)=censc(1,j,i)
-         censc(2,j,-i)=censc(2,j,i)
-         censc(3,j,-i)=-censc(3,j,i)
- C BSC is amplitude of Gaussian
          enddo
        do j=1,nlob(i)
          do k=1,3
                enddo
              gaussc(k,l,j,i)=akl
              gaussc(l,k,j,i)=akl
-               if (((k.eq.3).and.(l.ne.3))
-      &        .or.((l.eq.3).and.(k.ne.3))) then
-                 gaussc(k,l,j,-i)=-akl
-                 gaussc(l,k,j,-i)=-akl
-               else
-                 gaussc(k,l,j,-i)=akl
-                 gaussc(l,k,j,-i)=akl
-               endif
              enddo
            enddo 
        enddo
  C Read the parameters of the probability distribution/energy expression
  C of the side chains.
  C
 +      write (2,*) "Start reading ROTAM_PDB"
 +
        do i=1,ntyp
        read (irotam_pdb,'(3x,i3,f8.3)',end=112,err=112) nlob(i),dsc(i)
          if (i.eq.10) then
        endif
        enddo
        close (irotam_pdb)
 +      write (2,*) "Ending reading ROTAM_PDB"
  #endif
        close(irotam)
  
@@@ -617,40 -450,25 +565,25 @@@ C Read torsional parameter
  C
        read (itorp,*,end=113,err=113) ntortyp
        read (itorp,*,end=113,err=113) (itortyp(i),i=1,ntyp)
-       do iblock=1,2
-       do i=-ntyp,-1
-        itortyp(i)=-itortyp(-i)
-       enddo
  c      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)
+       do i=1,ntortyp
+       do j=1,ntortyp
+         read (itorp,*,end=113,err=113) nterm(i,j),nlor(i,j)
            v0ij=0.0d0
            si=-1.0d0
-         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)
+         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)
              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,iblock)
+         do k=1,nlor(i,j)
              read (itorp,*,end=113,err=113) kk,vlor1(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,iblock)=v0ij
-           v0(-i,-j,iblock)=v0ij
+           v0(i,j)=v0ij
          enddo
        enddo
-       enddo
        close (itorp)
        if (lprint) then
        write (iout,'(/a/)') 'Torsional constants:'
          do j=1,ntortyp
              write (iout,*) 'ityp',i,' jtyp',j
              write (iout,*) 'Fourier constants'
-             do k=1,nterm(i,j,iblock)
-             write (iout,'(2(1pe15.5))') v1(k,i,j,iblock),
-      &        v2(k,i,j,iblock)
+             do k=1,nterm(i,j)
+             write (iout,'(2(1pe15.5))') v1(k,i,j),v2(k,i,j)
              enddo
              write (iout,*) 'Lorenz constants'
-             do k=1,nlor(i,j,iblock)
+             do k=1,nlor(i,j)
              write (iout,'(3(1pe15.5))') 
       &         vlor1(k,i,j),vlor2(k,i,j),vlor3(k,i,j)
              enddo
  C
  C 6/23/01 Read parameters for double torsionals
  C
-       do iblock=1,2
-       do i=0,ntortyp-1
-         do j=-ntortyp+1,ntortyp-1
-           do k=-ntortyp+1,ntortyp-1
+       do i=1,ntortyp
+         do j=1,ntortyp
+           do k=1,ntortyp
              read (itordp,'(3a1)',end=114,err=114) t1,t2,t3
++<<<<<<< HEAD
 +c              write (iout,*) "OK onelett",
 +c     &         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
++>>>>>>> devel
                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,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
+             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
        if (lprint) then
        write (iout,*) 
        write (iout,*) 'Constants for double torsionals'
++<<<<<<< HEAD
 +      do iblock=1,2
 +      do i=0,ntortyp-1
 +        do j=-ntortyp+1,ntortyp-1
 +          do k=-ntortyp+1,ntortyp-1
++=======
+       do i=1,ntortyp
+         do j=1,ntortyp 
+           do k=1,ntortyp
++>>>>>>> devel
              write (iout,*) 'ityp',i,' jtyp',j,' ktyp',k,
-      &        ' nsingle',ntermd_1(i,j,k,iblock),
-      &        ' ndouble',ntermd_2(i,j,k,iblock)
+      &        ' nsingle',ntermd_1(i,j,k),' ndouble',ntermd_2(i,j,k)
              write (iout,*)
              write (iout,*) 'Single angles:'
-             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)
+             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)
              enddo
              write (iout,*)
              write (iout,*) 'Pairs of angles:'
-             write (iout,'(3x,20i10)') (l,l=1,ntermd_2(i,j,k,iblock))
-             do l=1,ntermd_2(i,j,k,iblock)
+             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,iblock),m=1,ntermd_2(i,j,k,iblock))
+      &         l,(v2c(l,m,i,j,k),m=1,ntermd_2(i,j,k))
              enddo
              write (iout,*)
-             write (iout,'(3x,20i10)') (l,l=1,ntermd_2(i,j,k,iblock))
-             do l=1,ntermd_2(i,j,k,iblock)
+             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,iblock),m=1,ntermd_2(i,j,k,iblock)),
-      &         (v2s(l,m,-i,-j,-k,iblock),m=1,ntermd_2(i,j,k,iblock))
+      &         l,(v2s(l,m,i,j,k),m=1,ntermd_2(i,j,k))
              enddo
              write (iout,*)
            enddo
          enddo
        enddo
-       enddo
        endif
  #endif
  C Read of Side-chain backbone correlation parameters
  C Modified 11 May 2012 by Adasko
  CCC
  C
++<<<<<<< HEAD
 +      read (isccor,*,end=119,err=119) nsccortyp
 +#ifdef SCCORPDB
 +      read (isccor,*,end=119,err=119) (isccortyp(i),i=1,ntyp)
 +      do i=-ntyp,-1
 +        isccortyp(i)=-isccortyp(-i)
 +      enddo
 +      iscprol=isccortyp(20)
++=======
+       read (isccor,*,end=1113,err=1113) nsccortyp
+       read (isccor,*,end=1113,err=1113) (isccortyp(i),i=1,ntyp)
++>>>>>>> devel
  c      write (iout,*) 'ntortyp',ntortyp
        maxinter=3
  cc maxinter is maximum interaction sites
        do l=1,maxinter    
        do i=1,nsccortyp
        do j=1,nsccortyp
++<<<<<<< HEAD
 +        read (isccor,*,end=119,err=119) nterm_sccor(i,j),nlor_sccor(i,j)
++=======
+         read (isccor,*,end=1113,err=1113) nterm_sccor(i,j),
+      &             nlor_sccor(i,j)
++>>>>>>> devel
            v0ijsccor=0.0d0
 +          v0ijsccor1=0.0d0
 +          v0ijsccor2=0.0d0
 +          v0ijsccor3=0.0d0
            si=-1.0d0
 -  
 +          nterm_sccor(-i,j)=nterm_sccor(i,j)
 +          nterm_sccor(-i,-j)=nterm_sccor(i,j)
 +          nterm_sccor(i,-j)=nterm_sccor(i,j)  
          do k=1,nterm_sccor(i,j)
++<<<<<<< HEAD
 +          read (isccor,*,end=119,err=119) kk,v1sccor(k,l,i,j)
 +     &    ,v2sccor(k,l,i,j)
 +            if (j.eq.iscprol) then
 +              if (i.eq.isccortyp(10)) then
 +              v1sccor(k,l,i,-j)=v1sccor(k,l,i,j)
 +              v2sccor(k,l,i,-j)=-v2sccor(k,l,i,j)
 +              else
 +             v1sccor(k,l,i,-j)=v1sccor(k,l,i,j)*0.5d0
 +     &                        +v2sccor(k,l,i,j)*dsqrt(0.75d0)
 +             v2sccor(k,l,i,-j)=-v2sccor(k,l,i,j)*0.5d0
 +     &                        +v1sccor(k,l,i,j)*dsqrt(0.75d0)
 +             v1sccor(k,l,-i,-j)=v1sccor(k,l,i,j)
 +             v2sccor(k,l,-i,-j)=-v2sccor(k,l,i,j)
 +             v1sccor(k,l,-i,j)=v1sccor(k,l,i,-j)
 +             v2sccor(k,l,-i,j)=-v2sccor(k,l,i,-j)          
 +             endif
 +            else
 +              if (i.eq.isccortyp(10)) then
 +              v1sccor(k,l,i,-j)=v1sccor(k,l,i,j)
 +              v2sccor(k,l,i,-j)=-v2sccor(k,l,i,j)
 +              else
 +                if (j.eq.isccortyp(10)) then
 +              v1sccor(k,l,-i,j)=v1sccor(k,l,i,j)
 +              v2sccor(k,l,-i,j)=-v2sccor(k,l,i,j)
 +                else
 +             v1sccor(k,l,i,-j)=-v1sccor(k,l,i,j)
 +             v2sccor(k,l,i,-j)=-v2sccor(k,l,i,j)
 +             v1sccor(k,l,-i,-j)=v1sccor(k,l,i,j)
 +             v2sccor(k,l,-i,-j)=-v2sccor(k,l,i,j)
 +             v1sccor(k,l,-i,j)=v1sccor(k,l,i,-j)
 +             v2sccor(k,l,-i,j)=-v2sccor(k,l,i,-j)
 +            endif
 +             endif
 +             endif 
++=======
+           read (isccor,*,end=1113,err=1113) kk,v1sccor(k,l,i,j)
+      &    ,v2sccor(k,l,i,j) 
++>>>>>>> devel
              v0ijsccor=v0ijsccor+si*v1sccor(k,l,i,j)
 +            v0ijsccor1=v0ijsccor+si*v1sccor(k,l,-i,j)
 +            v0ijsccor2=v0ijsccor+si*v1sccor(k,l,i,-j)
 +            v0ijsccor3=v0ijsccor+si*v1sccor(k,l,-i,-j)
              si=-si
            enddo
          do k=1,nlor_sccor(i,j)
++<<<<<<< HEAD
 +            read (isccor,*,end=119,err=119) kk,vlor1sccor(k,i,j),
++=======
+             read (isccor,*,end=1113,err=1113) kk,vlor1sccor(k,i,j),
++>>>>>>> devel
       &        vlor2sccor(k,i,j),vlor3sccor(k,i,j) 
              v0ijsccor=v0ijsccor+vlor1sccor(k,i,j)/
       &(1+vlor3sccor(k,i,j)**2)
            enddo
 -          v0sccor(i,j)=v0ijsccor
 +          v0sccor(l,i,j)=v0ijsccor
 +          v0sccor(l,-i,j)=v0ijsccor1
 +          v0sccor(l,i,-j)=v0ijsccor2
 +          v0sccor(l,-i,-j)=v0ijsccor3  
          enddo
        enddo
        enddo
        close (isccor)
 -      
 +#else
 +      read (isccor,*,end=119,err=119) (isccortyp(i),i=1,ntyp)
 +c      write (iout,*) 'ntortyp',ntortyp
 +      maxinter=3
 +cc maxinter is maximum interaction sites
 +      do l=1,maxinter
 +      do i=1,nsccortyp
 +        do j=1,nsccortyp
 +          read (isccor,*,end=119,err=119)
 +     & nterm_sccor(i,j),nlor_sccor(i,j)
 +          v0ijsccor=0.0d0
 +          si=-1.0d0
 +
 +          do k=1,nterm_sccor(i,j)
 +            read (isccor,*,end=119,err=119) kk,v1sccor(k,l,i,j)
 +     &    ,v2sccor(k,l,i,j)
 +            v0ijsccor=v0ijsccor+si*v1sccor(k,l,i,j)
 +            si=-si
 +          enddo
 +          do k=1,nlor_sccor(i,j)
 +            read (isccor,*,end=119,err=119) kk,vlor1sccor(k,i,j),
 +     &        vlor2sccor(k,i,j),vlor3sccor(k,i,j)
 +            v0ijsccor=v0ijsccor+vlor1sccor(k,i,j)/
 +     &(1+vlor3sccor(k,i,j)**2)
 +          enddo
 +          v0sccor(i,j,iblock)=v0ijsccor
 +        enddo
 +      enddo
 +      enddo
 +      close (isccor)
 +
 +#endif      
        if (lprint) then
        write (iout,'(/a/)') 'Torsional constants:'
        do i=1,nsccortyp
          write (iout,*) "Coefficients of the cumulants"
        endif
        read (ifourier,*) nloctyp
-       do i=0,nloctyp-1
+       do i=1,nloctyp
          read (ifourier,*,end=115,err=115)
          read (ifourier,*,end=115,err=115) (b(ii),ii=1,13)
          if (lprint) then
          endif
          B1(1,i)  = b(3)
          B1(2,i)  = b(5)
  c        b1(1,i)=0.0d0
  c        b1(2,i)=0.0d0
          B1tilde(1,i) = b(3)
++<<<<<<< HEAD
 +        B1tilde(2,i) =-b(5)
 +        B1tilde(1,-i) =-b(3)
 +        B1tilde(2,-i) =b(5)
++=======
+         B1tilde(2,i) =-b(5) 
++>>>>>>> devel
  c        b1tilde(1,i)=0.0d0
  c        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)
  c        CC(1,1,i)=0.0d0
  c        CC(2,2,i)=0.0d0
  c        CC(2,1,i)=0.0d0
@@@ -949,11 -646,6 +879,6 @@@ c        CC(1,2,i)=0.0d
          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)
  c        Ctilde(1,1,i)=0.0d0
  c        Ctilde(1,2,i)=0.0d0
  c        Ctilde(2,1,i)=0.0d0
@@@ -962,10 -654,6 +887,6 @@@ c        Ctilde(2,2,i)=0.0d
          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)
  c        DD(1,1,i)=0.0d0
  c        DD(2,2,i)=0.0d0
  c        DD(2,1,i)=0.0d0
@@@ -974,11 -662,6 +895,6 @@@ c        DD(1,2,i)=0.0d
          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)
  c        Dtilde(1,1,i)=0.0d0
  c        Dtilde(1,2,i)=0.0d0
  c        Dtilde(2,1,i)=0.0d0
@@@ -987,11 -670,6 +903,6 @@@ c        Dtilde(2,2,i)=0.0d
          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)
  c        ee(1,1,i)=1.0d0
  c        ee(2,2,i)=1.0d0
  c        ee(2,1,i)=0.0d0
@@@ -1087,17 -765,9 +998,17 @@@ C----------------------- LJK potential 
        endif
        goto 50
  C---------------------- GB or BP potential -----------------------------
 -   30 read (isidep,*,end=116,err=116)((eps(i,j),j=i,ntyp),i=1,ntyp),
 -     &  (sigma0(i),i=1,ntyp),(sigii(i),i=1,ntyp),(chip(i),i=1,ntyp),
 -     &  (alp(i),i=1,ntyp)
 +   30 do i=1,ntyp
 +        read (isidep,*,end=116,err=116)(eps(i,j),j=i,ntyp)
 +       enddo
 +       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   30 read (isidep,*,end=116,err=116)((eps(i,j),j=i,ntyp),i=1,ntyp),
 +c     &  (sigma0(i),i=1,ntyp),(sigii(i),i=1,ntyp),(chip(i),i=1,ntyp),
 +c     &  (alp(i),i=1,ntyp)
  C For the GB potential convert sigma'**2 into chi'
        if (ipot.eq.4) then
        do i=1,ntyp
@@@ -1209,7 -879,7 +1120,7 @@@ c           augm(i,j)=0.5D0**(2*expon)*
  C
  C Define the SC-p interaction constants (hard-coded; old style)
  C
 -      do i=1,20
 +      do i=1,ntyp
  C "Soft" SC-p repulsion (causes helices to be too flat, but facilitates
  C helix formation)
  c       aad(i,1)=0.3D0*4.0D0**12
  
        if (lprint) then
          write (iout,*) "Parameters of SC-p interactions:"
 -        do i=1,20
 +        do i=1,ntyp
            write (iout,'(4f8.3,4e12.4)') eps_scp(i,1),rscp(i,1),
       &     eps_scp(i,2),rscp(i,2),aad(i,1),bad(i,1),aad(i,2),bad(i,2)
          enddo
@@@ -1296,6 -966,9 +1207,9 @@@ c      v3ss=0.0d
        goto 999
    113 write (iout,*) "Error reading torsional energy parameters."
        goto 999
+  1113 write (iout,*) 
+      &  "Error reading side-chain torsional energy parameters."
+       goto 999
    114 write (iout,*) "Error reading double torsional energy parameters."
        goto 999
    115 write (iout,*) 
@@@ -16,6 -16,8 +16,8 @@@ C geometry
        character*3 seq,atom,res
        character*80 card
        dimension sccor(3,20)
+       double precision e1(3),e2(3),e3(3)
+       logical fail
        integer rescode
        ibeg=1
        lsecondary=.false.
@@@ -62,7 -64,7 +64,7 @@@ C Start new residue
                ishift=ires-1
                if (res.ne.'GLY' .and. res.ne. 'ACE') then
                  ishift=ishift-1
 -                itype(1)=21
 +                itype(1)=ntyp1
                endif
                ibeg=0          
              endif
@@@ -102,11 -104,18 +104,18 @@@ C Calculate the CM of the last side cha
        nstart_sup=1
        if (itype(nres).ne.10) then
          nres=nres+1
 -        itype(nres)=21
 +        itype(nres)=ntyp1
          if (unres_pdb) then
-           c(1,nres)=c(1,nres-1)+3.8d0
-           c(2,nres)=c(2,nres-1)
-           c(3,nres)=c(3,nres-1)
+ C 2/15/2013 by Adam: corrected insertion of the last dummy residue
+           call refsys(nres-3,nres-2,nres-1,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,nres)=c(j,nres-1)-3.8d0*e2(j)
+           enddo
          else
          do j=1,3
            dcj=c(j,nres-2)-c(j,nres-3)
          c(j,nres+1)=c(j,1)
          c(j,2*nres)=c(j,nres)
        enddo
 -      if (itype(1).eq.21) then
 +      if (itype(1).eq.ntyp1) then
          nsup=nsup-1
          nstart_sup=2
          if (unres_pdb) then
-           c(1,1)=c(1,2)-3.8d0
-           c(2,1)=c(2,2)
-           c(3,1)=c(3,2)
+ C 2/15/2013 by Adam: corrected insertion of the first dummy residue
+           call refsys(2,3,4,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,1)=c(j,2)-3.8d0*e2(j)
+           enddo
          else
          do j=1,3
            dcj=c(j,4)-c(j,3)
@@@ -260,6 -276,9 +276,9 @@@ c      endi
            enddo
            iti=itype(i)
            di=dist(i,nres+i)
+ C 10/03/12 Adam: Correction for zero SC-SC bond length
+           if (itype(i).ne.10 .and. itype(i).ne.21. and. di.eq.0.0d0)
+      &     di=dsc(itype(i))
            vbld(i+nres)=di
            if (itype(i).ne.10) then
              vbld_inv(i+nres)=1.0d0/di
@@@ -411,4 -430,3 +430,3 @@@ c       print *,vbld(i+1),vbld(i+1+nres
        enddo
        return
        end
-       
@@@ -860,12 -860,36 +860,36 @@@ C 12/1/95 Added weight for the multi-bo
        call reada(weightcard,"V2SS",v2ss,7.61d0)
        call reada(weightcard,"V3SS",v3ss,13.7d0)
        call reada(weightcard,"EBR",ebr,-5.50D0)
+       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
        if(me.eq.king.or..not.out1file) then
         write (iout,*) "Parameters of the SS-bond potential:"
         write (iout,*) "D0CM",d0cm," AKCM",akcm," AKTH",akth,
       & " AKCT",akct
         write (iout,*) "V1SS",v1ss," V2SS",v2ss," V3SS",v3ss
-        write (iout,*) "EBR",ebr
+        write (iout,*) "EBR",ebr," SS_DEPTH",ss_depth
+        write (iout,*)" HT",Ht
         print *,'indpdb=',indpdb,' pdbref=',pdbref
        endif
        if (indpdb.gt.0 .or. pdbref) then
@@@ -893,6 -917,9 +917,9 @@@ c        print *,'Finished reading pdb 
          call contact(.false.,ncont_ref,icont_ref,co)
  
          if (sideadd) then 
+ C Following 2 lines for diagnostics; comment out if not needed
+          write (iout,*) "Before sideadd"
+          call intout
           if(me.eq.king.or..not.out1file)
       &    write(iout,*)'Adding sidechains'
           maxsi=1000
       &              i,' after ',nsi,' trials'
            endif
           enddo
+ C 10/03/12 Adam: Recalculate coordinates with new side chain positions
+          call chainbuild
          endif  
+ C Following 2 lines for diagnostics; comment out if not needed
+ c        write (iout,*) "After sideadd"
+ c        call intout
        endif
        if (indpdb.eq.0) then
  C Read sequence if not taken from the pdb file.
@@@ -930,8 -962,8 +962,8 @@@ C Assign initial virtual bond length
            vbld_inv(i)=vblinv
          enddo
          do i=2,nres-1
-           vbld(i+nres)=dsc(iabs(itype(i)))
-           vbld_inv(i+nres)=dsc_inv(iabs(itype(i)))
+           vbld(i+nres)=dsc(itype(i))
+           vbld_inv(i+nres)=dsc_inv(itype(i))
  c          write (iout,*) "i",i," itype",itype(i),
  c     &      " dsc",dsc(itype(i))," vbld",vbld(i),vbld(i+nres)
          enddo
@@@ -940,15 -972,15 +972,15 @@@ c      print *,nre
  c      print '(20i4)',(itype(i),i=1,nres)
        do i=1,nres
  #ifdef PROCOR
 -        if (itype(i).eq.21 .or. itype(i+1).eq.21) then
 +        if (itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1) then
  #else
 -        if (itype(i).eq.21) then
 +        if (itype(i).eq.ntyp1) then
  #endif
            itel(i)=0
  #ifdef PROCOR
-         else if (iabs(itype(i+1)).ne.20) then
+         else if (itype(i+1).ne.20) then
  #else
-         else if (iabs(itype(i)).ne.20) then
+         else if (itype(i).ne.20) then
  #endif
          itel(i)=1
          else
@@@ -1005,8 -1037,8 +1037,8 @@@ C 8/13/98 Set limits to generating the 
  #endif
        nct=nres
  cd      print *,'NNT=',NNT,' NCT=',NCT
 -      if (itype(1).eq.21) nnt=2
 -      if (itype(nres).eq.21) nct=nct-1
 +      if (itype(1).eq.ntyp1) nnt=2
 +      if (itype(nres).eq.ntyp1) nct=nct-1
        if (pdbref) then
          if(me.eq.king.or..not.out1file)
       &   write (iout,'(a,i3)') 'nsup=',nsup
@@@ -1095,8 -1127,8 +1127,8 @@@ czscore          call geom_to_var(nvar,
  c        write (iout,*) "constr_dist",constr_dist,nstart_sup,nsup
        if (constr_dist.gt.0) then
          call read_dist_constr
-         call hpb_partition
        endif
+       if (nhpb.gt.0) call hpb_partition
  c      write (iout,*) "After read_dist_constr nhpb",nhpb
  c      call flush(iout)
        if (indpdb.eq.0 .and. modecalc.ne.2 .and. modecalc.ne.4
@@@ -1151,7 -1183,6 +1183,6 @@@ C initial geometry
           enddo
           do i=2,nres-1
            omeg(i)=-120d0*deg2rad
-           if (itype(i).le.0) omeg(i)=-omeg(i)
           enddo
          else
            if(me.eq.king.or..not.out1file)
@@@ -1218,18 -1249,35 +1249,35 @@@ C Generate distance constraints, if th
          write (iout,'(/a,i3,a)') 
       &  '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)
-         if (me.eq.king.or..not.out1file)
-      &    write (iout,'(2a,i3,3a,i3,a,3f10.3)')
+           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
        if (i2ndstr.gt.0) call secstrp2dihc
  c      call geom_to_var(nvar,x)
@@@ -1293,10 -1341,12 +1341,12 @@@ C Check whether the specified bridging 
        do i=1,ns
        if (itype(iss(i)).ne.1) then
          if (me.eq.king.or..not.out1file) 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 MPI
         call MPI_Finalize(MPI_COMM_WORLD,ierror)
  C Read preformed bridges.
        if (ns.gt.0) then
        read (inp,*) nss,(ihpb(i),jhpb(i),i=1,nss)
-       write (iout,*) 'nss=',nss,' ihpb,jhpb: ',(ihpb(i),jhpb(i),i=1,nss)
+       if(fg_rank.eq.0)
+      & write(iout,*)'nss=',nss,' ihpb,jhpb: ',(ihpb(i),jhpb(i),i=1,nss)
        if (nss.gt.0) then
          nhpb=nss
  C Check if the residues involved in bridges are in the specified list of
@@@ -2435,7 -2486,7 +2486,7 @@@ c        write (iout,*) i,ifrag_(1,i),i
          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
+ c            write (iout,*) "j",j," k",k
              ddjk=dist(j,k)
              if (constr_dist.eq.1) then
              nhpb=nhpb+1
@@@ -213,7 -213,7 +213,7 @@@ c     Define what is meant by "neighbou
  
  c     Don't do glycine or ends
        i=itype(res_pick)
 -      if (i.eq.10 .or. i.eq.21) return
 +      if (i.eq.10 .or. i.eq.ntyp1) return
  
  c     Freeze everything (later will relax only selected side-chains)
        mask_r=.true.
@@@ -255,7 -255,7 +255,7 @@@ cd      print *,'new       ',(energy(k)
        n_try=0
        do while (n_try.lt.n_maxtry .and. orig_e-cur_e.lt.e_drop)
  c     Move the selected residue (don't worry if it fails)
-         call gen_side(iabs(itype(res_pick)),theta(res_pick+1),
+         call gen_side(itype(res_pick),theta(res_pick+1),
       +       alph(res_pick),omeg(res_pick),fail)
  
  c     Minimize the side-chains starting from the new arrangement
@@@ -719,8 -719,8 +719,8 @@@ c     if (icall.eq.0) lprn=.true
        do i=iatsc_s,iatsc_e
  
  
-         itypi=iabs(itype(i))
-         itypi1=iabs(itype(i+1))
+         itypi=itype(i)
+         itypi1=itype(i+1)
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
            do j=istart(i,iint),iend(i,iint)
            IF (mask_side(j).eq.1.or.mask_side(i).eq.1) THEN
              ind=ind+1
-             itypj=iabs(itype(j))
+             itypj=itype(j)
              dscj_inv=dsc_inv(itypj)
              sig0ij=sigma(itypi,itypj)
              chi1=chi(itypi,itypj)
@@@ -223,7 -223,7 +223,7 @@@ c Compute the stochastic forces acting 
          do j=1,3
            ff(j)=ff(j)+force(j,i)
          enddo
 -        if (itype(i+1).ne.21) then
 +        if (itype(i+1).ne.ntyp1) then
            do j=1,3
              stochforc(j,i)=stochforc(j,i)+force(j,i+nres+1)
              ff(j)=ff(j)+force(j,i+nres+1)
@@@ -369,7 -369,7 +369,7 @@@ c  Load the friction coefficients corre
          ind=ind+1
          ii = ind+m
          iti=itype(i)
 -        gamvec(ii)=gamsc(iti)
 +        gamvec(ii)=gamsc(iabs(iti))
        enddo
        if (surfarea) call sdarea(gamvec)
  c      if (lprn) then
        include 'COMMON.INTERACT'
        include 'COMMON.IOUNITS'
        include 'COMMON.NAMES'
-       double precision radius(maxres2),gamvec(maxres6)
+       double precision radius(maxres2),gamvec(maxres2)
        parameter (twosix=1.122462048309372981d0)
        logical lprn /.false./
  c
@@@ -98,7 -98,7 +98,7 @@@ set(UNRES_WHAM_M_PP_SR
  if (Fortran_COMPILER_NAME STREQUAL "ifort")
    set(FFLAGS0 "-g -CB -I. -I${CMAKE_CURRENT_SOURCE_DIR}/include_unres -I${MPIF_INCLUDE_DIRECTORIES}" ) 
  elseif (Fortran_COMPILER_NAME STREQUAL "gfortran")
-   set(FFLAGS0 "-g -I. -I${CMAKE_CURRENT_SOURCE_DIR}/include_unres -I${MPIF_INCLUDE_DIRECTORIES}" ) 
+   set(FFLAGS0 "-std=legacy -g -I. -I${CMAKE_CURRENT_SOURCE_DIR}/include_unres -I${MPIF_INCLUDE_DIRECTORIES}" ) 
  endif (Fortran_COMPILER_NAME STREQUAL "ifort")
  
  
@@@ -112,20 -112,24 +112,36 @@@ endif(UNRES_WITH_MPI
  set_property(SOURCE ${UNRES_WHAM_M_SRC0} PROPERTY COMPILE_FLAGS ${FFLAGS0} )
  
  #=========================================
- # WHAM preprocesor flags
+ #  Settings for GAB force field
  #=========================================
+ if(UNRES_MD_FF STREQUAL "GAB" )
+   # set preprocesor flags   
+   set(CPPFLAGS "PROCOR  -DSPLITELE -DCRYST_BOND -DCRYST_THETA -DCRYST_SC  -DSCCORPDB" )
  
++<<<<<<< HEAD
 +if(UNRES_MD_FF STREQUAL "GAB" )
 +  # set preprocesor flags   
 +  set(CPPFLAGS "PROCOR -DUNRES -DISNAN -DSPLITELE -DLANG0 -DCRYST_BOND -DCRYST_THETA -DCRYST_SC  -DSCCORPDB" )
 +
++=======
++>>>>>>> devel
  #=========================================
  #  Settings for E0LL2Y force field
  #=========================================
  elseif(UNRES_MD_FF STREQUAL "E0LL2Y")
    # set preprocesor flags   
++<<<<<<< HEAD
 +  set(CPPFLAGS "PROCOR -DUNRES -DISNAN -DSPLITELE -DLANG0 -DSCCORPDB" )
 +endif(UNRES_MD_FF STREQUAL "GAB")
++=======
+   set(CPPFLAGS "PROCOR  -DSPLITELE -DSCCORPDB" )
+ endif(UNRES_MD_FF STREQUAL "GAB")
+ #=========================================
+ # Additional flags
+ #=========================================
+ set(CPPFLAGS "${CPPFLAGS} -DUNRES -DISNAN") 
++>>>>>>> devel
  
  #=========================================
  # System specific flags
@@@ -147,6 -151,9 +163,9 @@@ elseif (Fortran_COMPILER_NAME STREQUAL 
  elseif (Fortran_COMPILER_NAME STREQUAL "gfortran")
    # Add old gfortran flags
    set(CPPFLAGS "${CPPFLAGS} -DG77") 
+ else (Fortran_COMPILER_NAME STREQUAL "ifort")
+   # Default preprocessor flags
+   set(CPPFLAGS "${CPPFLAGS} -DPGI")
  endif (Fortran_COMPILER_NAME STREQUAL "ifort")
  
  #=========================================
@@@ -173,25 -180,41 +192,41 @@@ set_property(SOURCE ${UNRES_WHAM_M_PP_S
  set(UNRES_WHAM_M_BIN "wham_${Fortran_COMPILER_NAME}.exe")
  
  #=========================================
- # cinfo.f stupid workaround for cmake
- #  - shame on me ]:)
+ # cinfo.f workaround for CMake
  #=========================================
- set_property(SOURCE compinfo.c PROPERTY CMAKE_C_FLAGS "-c" )
- add_executable(compinfo-wham-m compinfo.c)
- set_target_properties(compinfo-wham-m PROPERTIES OUTPUT_NAME compinfo)
+ # get the current date  
+ TODAY(DATE)
+ # generate cinfo.f
+ set(CINFO "${CMAKE_CURRENT_BINARY_DIR}/cinfo.f")
+ FILE(WRITE ${CINFO}
+ "C CMake generated file
+        subroutine cinfo
+        include 'COMMON.IOUNITS'
+        write(iout,*)'++++ Compile info ++++'
+        write(iout,*)'Version ${UNRES_MAJOR}.${UNRES_MINOR} build ${UNRES_PATCH}'
+ ")
+ CINFO_FORMAT(${CINFO} "Compiled" "${DATE}" )
+ CINFO_FORMAT(${CINFO} "Compiled by" "$ENV{USER}@$ENV{HOST}" )
+ CINFO_FORMAT(${CINFO} "OS name:" "${CMAKE_SYSTEM_NAME}" )
+ CINFO_FORMAT(${CINFO} "OS release:" "${CMAKE_SYSTEM}" )
+ CINFO_FORMAT(${CINFO} "Fortran Compiler:" "${CMAKE_Fortran_COMPILER}" )
+ CINFO_FORMAT(${CINFO} "MD Force field:" "${UNRES_MD_FF}" )
+ CINFO_FORMAT(${CINFO} "CPPFLAGS =" "${CPPFLAGS}")
+ FILE(APPEND ${CINFO} 
+ "       write(iout,*)'++++ End of compile info ++++'  
+        return 
+        end ")
  
- set(UNRES_CINFO_DIR "${CMAKE_CURRENT_BINARY_DIR}" )
- add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/cinfo.f 
-       COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/cinfo.f ${CMAKE_CURRENT_BINARY_DIR}/cinfo.f
-       COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/COMMON.IOUNITS ${CMAKE_CURRENT_BINARY_DIR}/COMMON.IOUNITS 
-       COMMAND ${CMAKE_CURRENT_BINARY_DIR}/compinfo | true     
-       DEPENDS compinfo-wham-m )
- set_property(SOURCE ${UNRES_CINFO_DIR}/cinfo.f PROPERTY COMPILE_FLAGS ${FFLAGS0} )
+ # set include paths
+ set_property(SOURCE ${CMAKE_CURRENT_BINARY_DIR}/cinfo.f PROPERTY COMPILE_FLAGS "${FFLAGS0} -I${CMAKE_CURRENT_SOURCE_DIR}"  )
  
  #=========================================
  # Set full unres CSA sources
  #=========================================
- set(UNRES_WHAM_M_SRCS ${UNRES_WHAM_M_SRC0} ${UNRES_CINFO_DIR}/cinfo.f proc_proc.c)
+ set(UNRES_WHAM_M_SRCS ${UNRES_WHAM_M_SRC0} ${CMAKE_CURRENT_BINARY_DIR}/cinfo.f proc_proc.c)
  
  #=========================================
  # Build the binary
       &   +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)
        enddo
        endif
        return
@@@ -367,9 -368,9 +367,9 @@@ cd    print *,'Entering ELJ nnt=',nnt,
        evdw=0.0D0
        evdw_t=0.0d0
        do i=iatsc_s,iatsc_e
 -        itypi=itype(i)
 -        if (itypi.eq.21) cycle
 -        itypi1=itype(i+1)
 +        itypi=iabs(itype(i))
 +        if (itypi.eq.ntyp1) cycle
 +        itypi1=iabs(itype(i+1))
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
  cd        write (iout,*) 'i=',i,' iint=',iint,' istart=',istart(i,iint),
  cd   &                  'iend=',iend(i,iint)
            do j=istart(i,iint),iend(i,iint)
 -            itypj=itype(j)
 -            if (itypj.eq.21) cycle
 +            itypj=iabs(itype(j))
 +            if (itypj.eq.ntyp1) cycle
              xj=c(1,nres+j)-xi
              yj=c(2,nres+j)-yi
              zj=c(3,nres+j)-zi
@@@ -540,9 -541,9 +540,9 @@@ c     print *,'Entering ELJK nnt=',nnt,
        evdw=0.0D0
        evdw_t=0.0d0
        do i=iatsc_s,iatsc_e
 -        itypi=itype(i)
 -        if (itypi.eq.21) cycle
 -        itypi1=itype(i+1)
 +        itypi=iabs(itype(i))
 +        if (itypi.eq.ntyp1) cycle
 +        itypi1=iabs(itype(i+1))
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
@@@ -551,8 -552,8 +551,8 @@@ C Calculate SC interaction energy
  C
          do iint=1,nint_gr(i)
            do j=istart(i,iint),iend(i,iint)
 -            itypj=itype(j)
 -            if (itypj.eq.21) cycle
 +            itypj=iabs(itype(j))
 +            if (itypj.eq.ntyp1) cycle
              xj=c(1,nres+j)-xi
              yj=c(2,nres+j)-yi
              zj=c(3,nres+j)-zi
@@@ -653,9 -654,9 +653,9 @@@ c     els
  c     endif
        ind=0
        do i=iatsc_s,iatsc_e
 -        itypi=itype(i)
 -        if (itypi.eq.21) cycle
 -        itypi1=itype(i+1)
 +        itypi=iabs(itype(i))
 +        if (itypi.eq.ntyp1) cycle
 +        itypi1=iabs(itype(i+1))
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
          do iint=1,nint_gr(i)
            do j=istart(i,iint),iend(i,iint)
              ind=ind+1
 -            itypj=itype(j)
 -            if (itypj.eq.21) cycle
 +            itypj=iabs(itype(j))
 +            if (itypj.eq.ntyp1) cycle
              dscj_inv=vbld_inv(j+nres)
              chi1=chi(itypi,itypj)
              chi2=chi(itypj,itypi)
@@@ -791,9 -792,9 +791,9 @@@ c     print *,'Entering EGB nnt=',nnt,
  c      if (icall.gt.0) lprn=.true.
        ind=0
        do i=iatsc_s,iatsc_e
 -        itypi=itype(i)
 -        if (itypi.eq.21) cycle
 -        itypi1=itype(i+1)
 +        itypi=iabs(itype(i))
 +        if (itypi.eq.ntyp1) cycle
 +        itypi1=iabs(itype(i+1))
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
          do iint=1,nint_gr(i)
            do j=istart(i,iint),iend(i,iint)
              ind=ind+1
 -            itypj=itype(j)
 -            if (itypj.eq.21) cycle
 +            itypj=iabs(itype(j))
 +            if (itypj.eq.ntyp1) cycle
              dscj_inv=vbld_inv(j+nres)
              sig0ij=sigma(itypi,itypj)
              chi1=chi(itypi,itypj)
@@@ -941,9 -942,9 +941,9 @@@ c     print *,'Entering EGB nnt=',nnt,
  c      if (icall.gt.0) lprn=.true.
        ind=0
        do i=iatsc_s,iatsc_e
 -        itypi=itype(i)
 -        if (itypi.eq.21) cycle
 -        itypi1=itype(i+1)
 +        itypi=iabs(itype(i))
 +        if (itypi.eq.ntyp1) cycle
 +        itypi1=iabs(itype(i+1))
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
          do iint=1,nint_gr(i)
            do j=istart(i,iint),iend(i,iint)
              ind=ind+1
 -            itypj=itype(j)
 -            if (itypj.eq.21) cycle
 +            itypj=iabs(itype(j))
 +            if (itypj.eq.ntyp1) cycle
              dscj_inv=vbld_inv(j+nres)
              sig0ij=sigma(itypi,itypj)
              r0ij=r0(itypi,itypj)
@@@ -1852,7 -1853,7 +1852,7 @@@ cd      write (iout,*) 'iatel_s=',iatel
          gcorr_loc(i)=0.0d0
        enddo
        do i=iatel_s,iatel_e
 -        if (itype(i).eq.21 .or. itype(i+1).eq.21) cycle
 +        if (itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1) cycle
          if (itel(i).eq.0) goto 1215
          dxi=dc(1,i)
          dyi=dc(2,i)
          num_conti=0
  c        write (iout,*) 'i',i,' ielstart',ielstart(i),' ielend',ielend(i)
          do j=ielstart(i),ielend(i)
 -          if (itype(j).eq.21 .or. itype(j+1).eq.21) cycle
 +          if (itype(j).eq.ntyp1 .or. itype(j+1).eq.ntyp1) cycle
            if (itel(j).eq.0) goto 1216
            ind=ind+1
            iteli=itel(i)
@@@ -1916,15 -1917,10 +1916,15 @@@ C 12/26/95 - for the evaluation of mult
            ees0ij=4.0D0+fac*fac-3.0D0*(cosb*cosb+cosg*cosg)
            ees=ees+eesij
            evdw1=evdw1+evdwij
 -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,
 -cd     &      xmedi,ymedi,zmedi,xj,yj,zj
 +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,'(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     &      xmedi,ymedi,zmedi,xj,yj,zj
  C
  C Calculate contributions to the Cartesian gradient.
  C
@@@ -2270,10 -2266,8 +2270,10 @@@ C Check the loc-el terms by numerical i
  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)
 -cd          write (iout,*) 'i',i,' j',j,' eel_loc_ij',eel_loc_ij
 -cd          write (iout,*) a22,muij(1),a23,muij(2),a32,muij(3)
 +c          write (iout,*) 'i',i,' j',j,' eel_loc_ij',eel_loc_ij
 +c          write (iout,'(a6,2i5,0pf7.3)')
 +c     &            'eelloc',i,j,eel_loc_ij
 +c          write (iout,*) a22,muij(1),a23,muij(2),a32,muij(3)
            eel_loc=eel_loc+eel_loc_ij
  C Partial derivatives in virtual-bond dihedral angles gamma
            if (calc_grad) then
@@@ -2609,7 -2603,7 +2609,7 @@@ C Cartesian derivative
       &      +0.5d0*(pizda(1,1)+pizda(2,2))
          enddo
          endif
 -      else if (j.eq.i+3 .and. itype(i+2).ne.21) then
 +      else if (j.eq.i+3 .and. itype(i+2).ne.ntyp1) then
  CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
  C
  C               Fourth-order contributions
@@@ -2810,7 -2804,7 +2810,7 @@@ cd    print '(a)','Enter ESCP
  c      write (iout,*) 'iatscp_s=',iatscp_s,' iatscp_e=',iatscp_e,
  c     &  ' scal14',scal14
        do i=iatscp_s,iatscp_e
 -        if (itype(i).eq.21 .or. itype(i+1).eq.21) cycle
 +        if (itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1) cycle
          iteli=itel(i)
  c        write (iout,*) "i",i," iteli",iteli," nscp_gr",nscp_gr(i),
  c     &   " iscp",(iscpstart(i,j),iscpend(i,j),j=1,nscp_gr(i))
          do iint=1,nscp_gr(i)
  
          do j=iscpstart(i,iint),iscpend(i,iint)
 -          itypj=itype(j)
 -          if (itypj.eq.21) cycle
 +          itypj=iabs(itype(j))
 +          if (itypj.eq.ntyp1) cycle
  C Uncomment following three lines for SC-p interactions
  c         xj=c(1,nres+j)-xi
  c         yj=c(2,nres+j)-yi
@@@ -2842,9 -2836,7 +2842,9 @@@ C Uncomment following three lines for C
              evdw2_14=evdw2_14+e1+e2
            endif
            evdwij=e1+e2
 -c          write (iout,*) i,j,evdwij
 +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
            if (calc_grad) then
  C
@@@ -2935,8 -2927,7 +2935,8 @@@ 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. 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
          else
        include 'COMMON.VAR'
        include 'COMMON.IOUNITS'
        double precision erij(3),dcosom1(3),dcosom2(3),gg(3)
 -      itypi=itype(i)
 +      itypi=iabs(itype(i))
        xi=c(1,nres+i)
        yi=c(2,nres+i)
        zi=c(3,nres+i)
        dyi=dc_norm(2,nres+i)
        dzi=dc_norm(3,nres+i)
        dsci_inv=dsc_inv(itypi)
 -      itypj=itype(j)
 +      itypj=iabs(itype(j))
        dscj_inv=dsc_inv(itypj)
        xj=c(1,nres+j)-xi
        yj=c(2,nres+j)-yi
        logical energy_dec /.false./
        double precision u(3),ud(3)
        estr=0.0d0
 -      write (iout,*) "distchainmax",distchainmax
 +      estr1=0.0d0
 +c      write (iout,*) "distchainmax",distchainmax
        do i=nnt+1,nct
 -        if (itype(i-1).eq.21 .or. itype(i).eq.21) then
 +        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)
@@@ -3104,13 -3094,13 +3104,13 @@@ c          write (iout,*) i,vbld(i),vbl
          endif
  
        enddo
 -      estr=0.5d0*AKP*estr
 +      estr=0.5d0*AKP*estr+estr1
  c
  c 09/18/07 AL: multimodal bond potential based on AM1 CA-SC PMF's included
  c
        do i=nnt,nct
 -        iti=itype(i)
 -        if (iti.ne.10 .and. iti.ne.21) then
 +        iti=iabs(itype(i))
 +        if (iti.ne.10 .and. iti.ne.ntyp1) then
            nbi=nbondterm(iti)
            if (nbi.eq.1) then
              diff=vbld(i+nres)-vbldsc0(1,iti)
@@@ -3186,24 -3176,11 +3186,24 @@@ c      write (iout,*) "nres",nre
  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.21) cycle
 +        if (itype(i-1).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)
 -        if (i.gt.3 .and. itype(i-2).ne.21) then
 +        ichir1=isign(1,itype(i-2))
 +        ichir2=isign(1,itype(i))
 +         if (itype(i-2).eq.10) ichir1=isign(1,itype(i-1))
 +         if (itype(i).eq.10) ichir2=isign(1,itype(i-1))
 +         if (itype(i-1).eq.10) then
 +          itype1=isign(10,itype(i-2))
 +          ichir11=isign(1,itype(i-2))
 +          ichir12=isign(1,itype(i-2))
 +          itype2=isign(10,itype(i))
 +          ichir21=isign(1,itype(i))
 +          ichir22=isign(1,itype(i))
 +         endif
 +
 +        if (i.gt.3 .and. itype(i-2).ne.ntyp1) then
  #ifdef OSF
            phii=phi(i)
            icrc=0
            y(1)=0.0D0
            y(2)=0.0D0
          endif
 -        if (i.lt.nres .and. itype(i).ne.21) then
 +        if (i.lt.nres .and. itype(i).ne.ntyp1) then
  #ifdef OSF
            phii1=phi(i+1)
            icrc=0
@@@ -3240,12 -3217,8 +3240,12 @@@ C dependent on the adjacent virtual-bon
  C In following comments this theta will be referred to as t_c.
          thet_pred_mean=0.0d0
          do k=1,2
 -          athetk=athet(k,it)
 -          bthetk=bthet(k,it)
 +            athetk=athet(k,it,ichir1,ichir2)
 +            bthetk=bthet(k,it,ichir1,ichir2)
 +          if (it.eq.10) then
 +             athetk=athet(k,itype1,ichir11,ichir12)
 +             bthetk=bthet(k,itype2,ichir21,ichir22)
 +          endif
            thet_pred_mean=thet_pred_mean+athetk*y(k)+bthetk*z(k)
          enddo
  c        write (iout,*) "thet_pred_mean",thet_pred_mean
          thet_pred_mean=thet_pred_mean*ss+a0thet(it)
  c        write (iout,*) "thet_pred_mean",thet_pred_mean
  C Derivatives of the "mean" values in gamma1 and gamma2.
 -        dthetg1=(-athet(1,it)*y(2)+athet(2,it)*y(1))*ss
 -        dthetg2=(-bthet(1,it)*z(2)+bthet(2,it)*z(1))*ss
 +        dthetg1=(-athet(1,it,ichir1,ichir2)*y(2)
 +     &+athet(2,it,ichir1,ichir2)*y(1))*ss
 +         dthetg2=(-bthet(1,it,ichir1,ichir2)*z(2)
 +     &          +bthet(2,it,ichir1,ichir2)*z(1))*ss
 +         if (it.eq.10) then
 +      dthetg1=(-athet(1,itype1,ichir11,ichir12)*y(2)
 +     &+athet(2,itype1,ichir11,ichir12)*y(1))*ss
 +        dthetg2=(-bthet(1,itype2,ichir21,ichir22)*z(2)
 +     &         +bthet(2,itype2,ichir21,ichir22)*z(1))*ss
 +         endif
          if (theta(i).gt.pi-delta) then
            call theteng(pi-delta,thet_pred_mean,theta0(it),f0,fprim0,
       &         E_tc0)
        etheta=0.0D0
  c      write (iout,*) "ithetyp",(ithetyp(i),i=1,ntyp1)
        do i=ithet_start,ithet_end
 -        if (itype(i-1).eq.21) cycle
 +        if (itype(i-1).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)
 -        ityp2=ithetyp(itype(i-1))
 +        ityp2=ithetyp((itype(i-1)))
          do k=1,nntheterm
            coskt(k)=dcos(k*theti2)
            sinkt(k)=dsin(k*theti2)
          enddo
 -        if (i.gt.3 .and. itype(i-2).ne.21) then
 +        if (i.gt.3 .and. itype(i-2).ne.ntyp1) then
  #ifdef OSF
            phii=phi(i)
            if (phii.ne.phii) phii=150.0
  #else
            phii=phi(i)
  #endif
 -          ityp1=ithetyp(itype(i-2))
 +          ityp1=ithetyp((itype(i-2)))
            do k=1,nsingle
              cosph1(k)=dcos(k*phii)
              sinph1(k)=dsin(k*phii)
              sinph1(k)=0.0d0
            enddo 
          endif
 -        if (i.lt.nres .and. itype(i).ne.21) then
 +        if (i.lt.nres .and. itype(i).ne.ntyp1) then
  #ifdef OSF
            phii1=phi(i+1)
            if (phii1.ne.phii1) phii1=150.0
  #else
            phii1=phi(i+1)
  #endif
 -          ityp3=ithetyp(itype(i))
 +          ityp3=ithetyp((itype(i)))
            do k=1,nsingle
              cosph2(k)=dcos(k*phii1)
              sinph2(k)=dsin(k*phii1)
  c        write (iout,*) "i",i," ityp1",itype(i-2),ityp1,
  c     &   " ityp2",itype(i-1),ityp2," ityp3",itype(i),ityp3
  c        call flush(iout)
 -        ethetai=aa0thet(ityp1,ityp2,ityp3)
 +        ethetai=aa0thet(ityp1,ityp2,ityp3,iblock)
          do k=1,ndouble
            do l=1,k-1
              ccl=cosph1(l)*cosph2(k-l)
          enddo
          endif
          do k=1,ntheterm
 -          ethetai=ethetai+aathet(k,ityp1,ityp2,ityp3)*sinkt(k)
 -          dethetai=dethetai+0.5d0*k*aathet(k,ityp1,ityp2,ityp3)
 +          ethetai=ethetai+aathet(k,ityp1,ityp2,ityp3,iblock)*sinkt(k)
 +          dethetai=dethetai+0.5d0*k*aathet(k,ityp1,ityp2,ityp3,iblock)
       &      *coskt(k)
            if (lprn)
 -     &    write (iout,*) "k",k," aathet",aathet(k,ityp1,ityp2,ityp3),
 +     &    write (iout,*) "k",k,"
 +     &      aathet",aathet(k,ityp1,ityp2,ityp3,iblock),
       &     " ethetai",ethetai
          enddo
          if (lprn) then
          endif
          do m=1,ntheterm2
            do k=1,nsingle
 -            aux=bbthet(k,m,ityp1,ityp2,ityp3)*cosph1(k)
 -     &         +ccthet(k,m,ityp1,ityp2,ityp3)*sinph1(k)
 -     &         +ddthet(k,m,ityp1,ityp2,ityp3)*cosph2(k)
 -     &         +eethet(k,m,ityp1,ityp2,ityp3)*sinph2(k)
 +            aux=bbthet(k,m,ityp1,ityp2,ityp3,iblock)*cosph1(k)
 +     &         +ccthet(k,m,ityp1,ityp2,ityp3,iblock)*sinph1(k)
 +     &         +ddthet(k,m,ityp1,ityp2,ityp3,iblock)*cosph2(k)
 +     &         +eethet(k,m,ityp1,ityp2,ityp3,iblock)*sinph2(k)
              ethetai=ethetai+sinkt(m)*aux
              dethetai=dethetai+0.5d0*m*aux*coskt(m)
              dephii=dephii+k*sinkt(m)*(
 -     &          ccthet(k,m,ityp1,ityp2,ityp3)*cosph1(k)-
 -     &          bbthet(k,m,ityp1,ityp2,ityp3)*sinph1(k))
 +     &          ccthet(k,m,ityp1,ityp2,ityp3,iblock)*cosph1(k)-
 +     &          bbthet(k,m,ityp1,ityp2,ityp3,iblock)*sinph1(k))
              dephii1=dephii1+k*sinkt(m)*(
 -     &          eethet(k,m,ityp1,ityp2,ityp3)*cosph2(k)-
 -     &          ddthet(k,m,ityp1,ityp2,ityp3)*sinph2(k))
 +     &          eethet(k,m,ityp1,ityp2,ityp3,iblock)*cosph2(k)-
 +     &          ddthet(k,m,ityp1,ityp2,ityp3,iblock)*sinph2(k))
              if (lprn)
       &      write (iout,*) "m",m," k",k," bbthet",
 -     &         bbthet(k,m,ityp1,ityp2,ityp3)," ccthet",
 -     &         ccthet(k,m,ityp1,ityp2,ityp3)," ddthet",
 -     &         ddthet(k,m,ityp1,ityp2,ityp3)," eethet",
 -     &         eethet(k,m,ityp1,ityp2,ityp3)," ethetai",ethetai
 +     &         bbthet(k,m,ityp1,ityp2,ityp3,iblock)," ccthet",
 +     &         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
            enddo
          enddo
          if (lprn)
          do m=1,ntheterm3
            do k=2,ndouble
              do l=1,k-1
 -              aux=ffthet(l,k,m,ityp1,ityp2,ityp3)*cosph1ph2(l,k)+
 -     &            ffthet(k,l,m,ityp1,ityp2,ityp3)*cosph1ph2(k,l)+
 -     &            ggthet(l,k,m,ityp1,ityp2,ityp3)*sinph1ph2(l,k)+
 -     &            ggthet(k,l,m,ityp1,ityp2,ityp3)*sinph1ph2(k,l)
 +              aux=ffthet(l,k,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(l,k)+
 +     &            ffthet(k,l,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(k,l)+
 +     &            ggthet(l,k,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(l,k)+
 +     &            ggthet(k,l,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(k,l)
                ethetai=ethetai+sinkt(m)*aux
                dethetai=dethetai+0.5d0*m*coskt(m)*aux
                dephii=dephii+l*sinkt(m)*(
 -     &           -ffthet(l,k,m,ityp1,ityp2,ityp3)*sinph1ph2(l,k)-
 -     &            ffthet(k,l,m,ityp1,ityp2,ityp3)*sinph1ph2(k,l)+
 -     &            ggthet(l,k,m,ityp1,ityp2,ityp3)*cosph1ph2(l,k)+
 -     &            ggthet(k,l,m,ityp1,ityp2,ityp3)*cosph1ph2(k,l))
 +     &           -ffthet(l,k,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(l,k)-
 +     &            ffthet(k,l,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(k,l)+
 +     &            ggthet(l,k,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(l,k)+
 +     &            ggthet(k,l,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(k,l))
                dephii1=dephii1+(k-l)*sinkt(m)*(
 -     &           -ffthet(l,k,m,ityp1,ityp2,ityp3)*sinph1ph2(l,k)+
 -     &            ffthet(k,l,m,ityp1,ityp2,ityp3)*sinph1ph2(k,l)+
 -     &            ggthet(l,k,m,ityp1,ityp2,ityp3)*cosph1ph2(l,k)-
 -     &            ggthet(k,l,m,ityp1,ityp2,ityp3)*cosph1ph2(k,l))
 +     &           -ffthet(l,k,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(l,k)+
 +     &            ffthet(k,l,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(k,l)+
 +     &            ggthet(l,k,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(l,k)-
 +     &            ggthet(k,l,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(k,l))
                if (lprn) then
                write (iout,*) "m",m," k",k," l",l," ffthet",
 -     &            ffthet(l,k,m,ityp1,ityp2,ityp3),
 -     &            ffthet(k,l,m,ityp1,ityp2,ityp3)," ggthet",
 -     &            ggthet(l,k,m,ityp1,ityp2,ityp3),
 -     &            ggthet(k,l,m,ityp1,ityp2,ityp3)," ethetai",ethetai
 +     &            ffthet(l,k,m,ityp1,ityp2,ityp3,iblock),
 +     &            ffthet(k,l,m,ityp1,ityp2,ityp3,iblock)," ggthet",
 +     &            ggthet(l,k,m,ityp1,ityp2,ityp3,iblock),
 +     &            ggthet(k,l,m,ityp1,ityp2,ityp3,iblock),
 +     &            " ethetai",ethetai
                write (iout,*) cosph1ph2(l,k)*sinkt(m),
       &            cosph1ph2(k,l)*sinkt(m),
       &            sinph1ph2(l,k)*sinkt(m),sinph1ph2(k,l)*sinkt(m)
@@@ -3628,9 -3589,9 +3628,9 @@@ C ALPHA and OMEGA
  c     write (iout,'(a)') 'ESC'
        do i=loc_start,loc_end
          it=itype(i)
 -        if (it.eq.21) cycle
 +        if (it.eq.ntyp1) cycle
          if (it.eq.10) goto 1
 -        nlobit=nlob(it)
 +        nlobit=nlob(iabs(it))
  c       print *,'i=',i,' it=',it,' nlobit=',nlobit
  c       write (iout,*) 'i=',i,' ssa=',ssa,' ssad=',ssad
          theti=theta(i+1)-pipol
@@@ -3785,7 -3746,7 +3785,7 @@@ C Compute the contribution to SC energ
          do iii=-1,1
  
            do j=1,nlobit
 -            expfac=dexp(bsc(j,it)-0.5D0*contr(j,iii)+emin)
 +            expfac=dexp(bsc(j,iabs(it))-0.5D0*contr(j,iii)+emin)
  cd          print *,'j=',j,' expfac=',expfac
              escloc_i=escloc_i+expfac
              do k=1,3
@@@ -3866,7 -3827,7 +3866,7 @@@ C Compute the contribution to SC energ
  
        dersc12=0.0d0
        do j=1,nlobit
 -        expfac=dexp(bsc(j,it)-0.5D0*contr(j)+emin)
 +        expfac=dexp(bsc(j,iabs(it))-0.5D0*contr(j)+emin)
          escloc_i=escloc_i+expfac
          do k=1,2
            dersc(k)=dersc(k)+Ax(k,j)*expfac
        delta=0.02d0*pi
        escloc=0.0D0
        do i=loc_start,loc_end
 -        if (itype(i).eq.21) cycle
 +        if (itype(i).eq.ntyp1) cycle
          costtab(i+1) =dcos(theta(i+1))
          sinttab(i+1) =dsqrt(1-costtab(i+1)*costtab(i+1))
          cost2tab(i+1)=dsqrt(0.5d0*(1.0d0+costtab(i+1)))
          cosfac=dsqrt(cosfac2)
          sinfac2=0.5d0/(1.0d0-costtab(i+1))
          sinfac=dsqrt(sinfac2)
 -        it=itype(i)
 +        it=iabs(itype(i))
          if (it.eq.10) goto 1
  c
  C  Compute the axes of tghe local cartesian coordinates system; store in
@@@ -3948,7 -3909,7 +3948,7 @@@ C     &   dc_norm(3,i+nres
            y_prime(j) = (dc_norm(j,i) + dc_norm(j,i-1))*sinfac
          enddo
          do j = 1,3
 -          z_prime(j) = -uz(j,i-1)
 +          z_prime(j) = -uz(j,i-1)*dsign(1.0d0,dfloat(itype(i)))
          enddo     
  c       write (2,*) "i",i
  c       write (2,*) "x_prime",(x_prime(j),j=1,3)
  C Compute the energy of the ith side cbain
  C
  c        write (2,*) "xx",xx," yy",yy," zz",zz
 -        it=itype(i)
 +        it=iabs(itype(i))
          do j = 1,65
            x(j) = sc_parmin(j,it) 
          enddo
  Cc diagnostics - remove later
          xx1 = dcos(alph(2))
          yy1 = dsin(alph(2))*dcos(omeg(2))
 -        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
@@@ -4031,8 -3992,6 +4031,8 @@@ c     &   dscp1,dscp2,sumen
  c        sumene = enesc(x,xx,yy,zz,cost2tab(i+1),sint2tab(i+1))
          escloc = escloc + sumene
  c        write (2,*) "escloc",escloc
 +c        write (2,*) "i",i," escloc",sumene,escloc,it,itype(i),
 +c     &  zz,xx,yy
          if (.not. calc_grad) goto 1
  #ifdef DEBUG
  C
@@@ -4161,10 -4120,8 +4161,10 @@@ c     &   (dC_norm(j,i-1),j=1,3)," vbld
           dZZ_Ci1(k)=0.0d0
           dZZ_Ci(k)=0.0d0
           do j=1,3
 -           dZZ_Ci(k)=dZZ_Ci(k)-uzgrad(j,k,2,i-1)*dC_norm(j,i+nres)
 -           dZZ_Ci1(k)=dZZ_Ci1(k)-uzgrad(j,k,1,i-1)*dC_norm(j,i+nres)
 +           dZZ_Ci(k)=dZZ_Ci(k)-uzgrad(j,k,2,i-1)
 +     & *dsign(1.0d0,dfloat(itype(i)))*dC_norm(j,i+nres)
 +           dZZ_Ci1(k)=dZZ_Ci1(k)-uzgrad(j,k,1,i-1)
 +     &  *dsign(1.0d0,dfloat(itype(i)))*dC_norm(j,i+nres)
           enddo
            
           dXX_XYZ(k)=vbld_inv(i+nres)*(x_prime(k)-xx*dC_norm(k,i+nres))
@@@ -4316,8 -4273,8 +4316,8 @@@ C Set lprn=.true. for debuggin
  c      lprn=.true.
        etors=0.0D0
        do i=iphi_start,iphi_end
 -        if (itype(i-2).eq.21 .or. itype(i-1).eq.21
 -     &      .or. itype(i).eq.21) cycle
 +        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))
          phii=phi(i)
@@@ -4401,22 -4358,17 +4401,22 @@@ C Set lprn=.true. for debuggin
  c      lprn=.true.
        etors=0.0D0
        do i=iphi_start,iphi_end
 -        if (itype(i-2).eq.21 .or. itype(i-1).eq.21
 -     &       .or. itype(i).eq.21) cycle
 +        if (itype(i-2).eq.ntyp1 .or. itype(i-1).eq.ntyp1
 +     &       .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
 +         else
 +         iblock=1
 +         endif
          itori=itortyp(itype(i-2))
          itori1=itortyp(itype(i-1))
          phii=phi(i)
          gloci=0.0D0
  C Regular cosine and sine terms
 -        do j=1,nterm(itori,itori1)
 -          v1ij=v1(j,itori,itori1)
 -          v2ij=v2(j,itori,itori1)
 +        do j=1,nterm(itori,itori1,iblock)
 +          v1ij=v1(j,itori,itori1,iblock)
 +          v2ij=v2(j,itori,itori1,iblock)
            cosphi=dcos(j*phii)
            sinphi=dsin(j*phii)
            etors=etors+v1ij*cosphi+v2ij*sinphi
@@@ -4429,24 -4381,22 +4429,24 @@@ C          [v2 cos(phi/2)+v3 sin(phi/2)
  C
          cosphi=dcos(0.5d0*phii)
          sinphi=dsin(0.5d0*phii)
 -        do j=1,nlor(itori,itori1)
 +        do j=1,nlor(itori,itori1,iblock)
            vl1ij=vlor1(j,itori,itori1)
            vl2ij=vlor2(j,itori,itori1)
            vl3ij=vlor3(j,itori,itori1)
            pom=vl2ij*cosphi+vl3ij*sinphi
            pom1=1.0d0/(pom*pom+1.0d0)
            etors=etors+vl1ij*pom1
 +c          if (energy_dec) etors_ii=etors_ii+
 +c     &                vl1ij*pom1
            pom=-pom*pom1*pom1
            gloci=gloci+vl1ij*(vl3ij*cosphi-vl2ij*sinphi)*pom
          enddo
  C Subtract the constant term
 -        etors=etors-v0(itori,itori1)
 +        etors=etors-v0(itori,itori1,iblock)
          if (lprn)
       &  write (iout,'(2(a3,2x,i3,2x),2i3,6f8.3/26x,6f8.3/)')
       &  restyp(itype(i-2)),i-2,restyp(itype(i-1)),i-1,itori,itori1,
 -     &  (v1(j,itori,itori1),j=1,6),(v2(j,itori,itori1),j=1,6)
 +     &  (v1(j,itori,itori1,1),j=1,6),(v2(j,itori,itori1,1),j=1,6)
          gloc(i-3,icg)=gloc(i-3,icg)+wtor*fact*gloci
  c       write (iout,*) 'i=',i,' gloc=',gloc(i-3,icg)
   1215   continue
@@@ -4502,8 -4452,8 +4502,8 @@@ 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.21 .or. itype(i-1).eq.21
 -     &      .or. itype(i).eq.21 .or. itype(i+1).eq.21) cycle
 +        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 (itel(i-2).eq.0 .or. itel(i-1).eq.0 .or. itel(i).eq.0) 
       &     goto 1215
          itori=itortyp(itype(i-2))
          phii1=phi(i+1)
          gloci1=0.0D0
          gloci2=0.0D0
 +        iblock=1
 +        if (iabs(itype(i+1)).eq.20) iblock=2
  C Regular cosine and sine terms
 -        do j=1,ntermd_1(itori,itori1,itori2)
 -          v1cij=v1c(1,j,itori,itori1,itori2)
 -          v1sij=v1s(1,j,itori,itori1,itori2)
 -          v2cij=v1c(2,j,itori,itori1,itori2)
 -          v2sij=v1s(2,j,itori,itori1,itori2)
 +        do j=1,ntermd_1(itori,itori1,itori2,iblock)
 +          v1cij=v1c(1,j,itori,itori1,itori2,iblock)
 +          v1sij=v1s(1,j,itori,itori1,itori2,iblock)
 +          v2cij=v1c(2,j,itori,itori1,itori2,iblock)
 +          v2sij=v1s(2,j,itori,itori1,itori2,iblock)
            cosphi1=dcos(j*phii)
            sinphi1=dsin(j*phii)
            cosphi2=dcos(j*phii1)
            gloci1=gloci1+j*(v1sij*cosphi1-v1cij*sinphi1)
            gloci2=gloci2+j*(v2sij*cosphi2-v2cij*sinphi2)
          enddo
 -        do k=2,ntermd_2(itori,itori1,itori2)
 +        do k=2,ntermd_2(itori,itori1,itori2,iblock)
            do l=1,k-1
 -            v1cdij = v2c(k,l,itori,itori1,itori2)
 -            v2cdij = v2c(l,k,itori,itori1,itori2)
 -            v1sdij = v2s(k,l,itori,itori1,itori2)
 -            v2sdij = v2s(l,k,itori,itori1,itori2)
 +            v1cdij = v2c(k,l,itori,itori1,itori2,iblock)
 +            v2cdij = v2c(l,k,itori,itori1,itori2,iblock)
 +            v1sdij = v2s(k,l,itori,itori1,itori2,iblock)
 +            v2sdij = v2s(l,k,itori,itori1,itori2,iblock)
              cosphi1p2=dcos(l*phii+(k-l)*phii1)
              cosphi1m2=dcos(l*phii-(k-l)*phii1)
              sinphi1p2=dsin(l*phii+(k-l)*phii1)
              gloci1=gloci1+l*(v1sdij*cosphi1p2+v2sdij*cosphi1m2
       &        -v1cdij*sinphi1p2-v2cdij*sinphi1m2)
              gloci2=gloci2+(k-l)*(v1sdij*cosphi1p2-v2sdij*cosphi1m2
 -     &        -v1cdij*sinphi1p2+v2cdij*sinphi1m2) 
 +     &        -v1cdij*sinphi1p2+v2cdij*sinphi1m2)
            enddo
          enddo
          gloc(i-3,icg)=gloc(i-3,icg)+wtor_d*fact2*gloci1
@@@ -4584,49 -4532,26 +4584,49 @@@ C Set lprn=.true. for debuggin
  c      lprn=.true.
  c      write (iout,*) "EBACK_SC_COR",iphi_start,iphi_end,nterm_sccor
        esccor=0.0D0
 -      do i=iphi_start,iphi_end
 -        if (itype(i-2).eq.21 .or. itype(i-1).eq.21) cycle
 +      do i=itau_start,itau_end
 +        if ((itype(i-2).eq.ntyp1).or.(itype(i-1).eq.ntyp1)) cycle
          esccor_ii=0.0D0
 -        itori=itype(i-2)
 -        itori1=itype(i-1)
 +        isccori=isccortyp(itype(i-2))
 +        isccori1=isccortyp(itype(i-1))
          phii=phi(i)
 +        do intertyp=1,3 !intertyp
 +cc Added 09 May 2012 (Adasko)
 +cc  Intertyp means interaction type of backbone mainchain correlation: 
 +c   1 = SC...Ca...Ca...Ca
 +c   2 = Ca...Ca...Ca...SC
 +c   3 = SC...Ca...Ca...SCi
          gloci=0.0D0
 -        do j=1,nterm_sccor
 -          v1ij=v1sccor(j,itori,itori1)
 -          v2ij=v2sccor(j,itori,itori1)
 -          cosphi=dcos(j*phii)
 -          sinphi=dsin(j*phii)
 -          esccor=esccor+v1ij*cosphi+v2ij*sinphi
 -          gloci=gloci+j*(v2ij*cosphi-v1ij*sinphi)
 -        enddo
 +        if (((intertyp.eq.3).and.((itype(i-2).eq.10).or.
 +     &      (itype(i-1).eq.10).or.(itype(i-2).eq.ntyp1).or.
 +     &      (itype(i-1).eq.ntyp1)))
 +     &    .or. ((intertyp.eq.1).and.((itype(i-2).eq.10)
 +     &     .or.(itype(i-2).eq.ntyp1).or.(itype(i-1).eq.ntyp1)
 +     &     .or.(itype(i).eq.ntyp1)))
 +     &    .or.((intertyp.eq.2).and.((itype(i-1).eq.10).or.
 +     &      (itype(i-1).eq.ntyp1).or.(itype(i-2).eq.ntyp1).or.
 +     &      (itype(i-3).eq.ntyp1)))) cycle
 +        if ((intertyp.eq.2).and.(i.eq.4).and.(itype(1).eq.ntyp1)) cycle
 +        if ((intertyp.eq.1).and.(i.eq.nres).and.(itype(nres).eq.ntyp1))
 +     & cycle
 +       do j=1,nterm_sccor(isccori,isccori1)
 +          v1ij=v1sccor(j,intertyp,isccori,isccori1)
 +          v2ij=v2sccor(j,intertyp,isccori,isccori1)
 +          cosphi=dcos(j*tauangle(intertyp,i))
 +          sinphi=dsin(j*tauangle(intertyp,i))
 +           esccor=esccor+v1ij*cosphi+v2ij*sinphi
 +           gloci=gloci+j*(v2ij*cosphi-v1ij*sinphi)
 +         enddo
 +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
          if (lprn)
       &  write (iout,'(2(a3,2x,i3,2x),2i3,6f8.3/26x,6f8.3/)')
       &  restyp(itype(i-2)),i-2,restyp(itype(i-1)),i-1,itori,itori1,
 -     &  (v1sccor(j,itori,itori1),j=1,6),(v2sccor(j,itori,itori1),j=1,6)
 -        gsccor_loc(i-3)=gloci
 +     &  (v1sccor(j,1,itori,itori1),j=1,6)
 +     &  ,(v2sccor(j,1,itori,itori1),j=1,6)
 +c        gsccor_loc(i-3)=gloci
 +       enddo !intertyp
        enddo
        return
        end
@@@ -4735,9 -4660,9 +4735,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=num_cont_hb(atom)
        do i=1,num_kont
          do k=1,7
@@@ -4760,10 -4685,9 +4760,10 @@@ 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
@@@ -6502,7 -6426,7 +6502,7 @@@ c--------------------------------------
        include 'COMMON.GEO'
        logical swap
        double precision vv(2),pizda(2,2),auxmat(2,2),auxvec(2),
-      & auxvec1(2),auxvec2(1),auxmat1(2,2)
+      & auxvec1(2),auxvec2(2),auxmat1(2,2)
        logical lprn
        common /kutas/ lprn
  CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
@@@ -3,12 -3,10 +3,12 @@@
       & vbldsc0_all(maxbondterm,ntyp,max_parm),
       & aksc_all(maxbondterm,ntyp,max_parm),
       & abond0_all(maxbondterm,ntyp,max_parm),
 -     & a0thet_all(ntyp,max_parm),athet_all(2,ntyp,max_parm),
 -     & bthet_all(2,ntyp,max_parm),polthet_all(0:3,ntyp,max_parm),
 -     & gthet_all(3,ntyp,max_parm),theta0_all(ntyp,max_parm),
 -     & sig0_all(ntyp,max_parm),sigc0_all(ntyp,max_parm),
 +     & a0thet_all(-ntyp:ntyp,max_parm),
 +     & athet_all(2,-ntyp:ntyp,-1:1,-1:1,max_parm),
 +     & bthet_all(2,-ntyp:ntyp,-1:1,-1:1,max_parm),
 +     & polthet_all(0:3,-ntyp:ntyp,max_parm),
 +     & gthet_all(3,-ntyp:ntyp,max_parm),theta0_all(-ntyp:ntyp,max_parm),
 +     & sig0_all(-ntyp:ntyp,max_parm),sigc0_all(-ntyp:ntyp,max_parm),
       & aa0thet_all(maxthetyp1,maxthetyp1,maxthetyp1,max_parm),
       & aathet_all(maxtheterm,maxthetyp1,maxthetyp1,maxthetyp1,max_parm),
       & bbthet_all(maxsingle,maxtheterm2,maxthetyp1,maxthetyp1,
       & ggthet_all(maxdouble,maxdouble,maxtheterm3,maxthetyp1,maxthetyp1,
       &  maxthetyp1,max_parm),
       & dsc_all(ntyp1,max_parm),bsc_all(maxlob,ntyp,max_parm),
 -     & censc_all(3,maxlob,ntyp,max_parm),
 -     & gaussc_all(3,3,maxlob,ntyp,max_parm),dsc0_all(ntyp1,max_parm),
 +     & censc_all(3,maxlob,-ntyp:ntyp,max_parm),
 +     & gaussc_all(3,3,maxlob,-ntyp:ntyp,max_parm),
 +     & dsc0_all(ntyp1,max_parm),
       & sc_parmin_all(65,ntyp,max_parm),
 -     & v0_all(maxtor,maxtor,max_parm),
 -     & v1_all(maxterm,maxtor,maxtor,max_parm),
 -     & v2_all(maxterm,maxtor,maxtor,max_parm),
 +     & v0_all(-maxtor:maxtor,-maxtor:maxtor,2,max_parm),
 +     & v1_all(maxterm,-maxtor:maxtor,-maxtor:maxtor,2,max_parm),
 +     & v2_all(maxterm,-maxtor:maxtor,-maxtor:maxtor,2,max_parm),
       & vlor1_all(maxlor,maxtor,maxtor,max_parm),
       & vlor2_all(maxlor,maxtor,maxtor,max_parm),
       & vlor3_all(maxlor,maxtor,maxtor,max_parm),
 -     & v1c_all(2,maxtermd_1,maxtor,maxtor,maxtor,max_parm),
 -     & v1s_all(2,maxtermd_1,maxtor,maxtor,maxtor,max_parm),
 -     & v2c_all(maxtermd_2,maxtermd_2,maxtor,maxtor,maxtor,max_parm),
 -     & v2s_all(maxtermd_2,maxtermd_2,maxtor,maxtor,maxtor,max_parm),
 -     & b1_all(2,maxtor,max_parm),b2_all(2,maxtor,max_parm),
 -     & cc_all(2,2,maxtor,max_parm),dd_all(2,2,maxtor,max_parm),
 -     & ee_all(2,2,maxtor,max_parm),ctilde_all(2,2,maxtor,max_parm),
 -     & dtilde_all(2,2,maxtor,max_parm),b1tilde_all(2,maxtor,max_parm),
 +     & v1c_all(2,maxtermd_1,-maxtor:maxtor,-maxtor:maxtor,
 +     & -maxtor:maxtor,2,max_parm),
 +     & v1s_all(2,maxtermd_1,-maxtor:maxtor,-maxtor:maxtor,
 +     & -maxtor:maxtor,2,max_parm),
 +     & v2c_all(maxtermd_2,maxtermd_2,-maxtor:maxtor,
 +     & -maxtor:maxtor,-maxtor:maxtor,2,max_parm),
 +     & v2s_all(maxtermd_2,maxtermd_2,-maxtor:maxtor,-maxtor:maxtor,
 +     & -maxtor:maxtor,2,max_parm),
 +     & b1_all(2,-maxtor:maxtor,max_parm),
 +     & b2_all(2,-maxtor:maxtor,max_parm),
 +     & cc_all(2,2,-maxtor:maxtor,max_parm),
 +     & dd_all(2,2,-maxtor:maxtor,max_parm),
 +     & ee_all(2,2,-maxtor:maxtor,max_parm),
 +     & ctilde_all(2,2,-maxtor:maxtor,max_parm),
 +     & dtilde_all(2,2,-maxtor:maxtor,max_parm),
 +     & b1tilde_all(2,-maxtor:maxtor,max_parm),
       & app_all(2,2,max_parm),bpp_all(2,2,max_parm),
       & ael6_all(2,2,max_parm),ael3_all(2,2,max_parm),
       & aad_all(ntyp,2,max_parm),bad_all(ntyp,2,max_parm),
       & sigma_all(ntyp,ntyp,max_parm),r0_all(ntyp,ntyp,max_parm),
       & chi_all(ntyp,ntyp,max_parm),chip_all(ntyp,max_parm),
       & alp_all(ntyp,max_parm),ebr_all(max_parm),d0cm_all(max_parm),
+      & ss_depth_all(max_parm),ht_all(max_parm),
       & akcm_all(max_parm),akth_all(max_parm),akct_all(max_parm),
       & v1ss_all(max_parm),v2ss_all(max_parm),v3ss_all(max_parm),
       & v1sccor_all(maxterm_sccor,3,ntyp,ntyp,max_parm),
       & v2sccor_all(maxterm_sccor,3,ntyp,ntyp,max_parm)
 -      integer nlob_all(ntyp1,max_parm),nlor_all(maxtor,maxtor,max_parm),
 -     & nterm_all(maxtor,maxtor,max_parm),
 -     & ntermd1_all(maxtor,maxtor,maxtor,max_parm),
 -     & ntermd2_all(maxtor,maxtor,maxtor,max_parm),
 +      integer nlob_all(ntyp1,max_parm),
 +     & nlor_all(-maxtor:maxtor,-maxtor:maxtor,2,max_parm),
 +     & nterm_all(-maxtor:maxtor,-maxtor:maxtor,2,max_parm),
 +     & ntermd1_all(-maxtor:maxtor,-maxtor:maxtor,
 +     & -maxtor:maxtor,2,max_parm),
 +     & ntermd2_all(-maxtor:maxtor,-maxtor:maxtor,
 +     & -maxtor:maxtor,2,max_parm),
       & nbondterm_all(ntyp,max_parm),nthetyp_all(max_parm),
       & ithetyp_all(ntyp1,max_parm),ntheterm_all(max_parm),
       & ntheterm2_all(max_parm),ntheterm3_all(max_parm),
@@@ -85,6 -72,7 +86,7 @@@
       & ctilde_all,dtilde_all,b1tilde_all,app_all,bpp_all,ael6_all,
       & ael3_all,aad_all,bad_all,aa_all,bb_all,augm_all,
       & eps_all,sigma_all,r0_all,chi_all,chip_all,alp_all,ebr_all,
+      & ss_depth_all,ht_all,
       & d0cm_all,akcm_all,akth_all,akct_all,v1ss_all,v2ss_all,v3ss_all,
       & v1sccor_all,v2sccor_all,nbondterm_all,
       & nlob_all,nlor_all,nterm_all,ntermd1_all,ntermd2_all,
@@@ -75,6 -75,7 +75,7 @@@
       &    ((csingle(l,k+nres),l=1,3),k=nnt,nct),
       &    nss,(ihpb(k),jhpb(k),k=1,nss),
       &    eini,efree,rmsdev,(q(j,iii+1),j=1,nQ),iR,ib,ipar
+ cc       write(iout,*), 'NAWEJ',i,eini
           if (indpdb.gt.0) then
             do k=1,nres
               do l=1,3
@@@ -194,12 -195,12 +195,16 @@@ c        call pdbout(ii+1,beta_h(ib,ipa
       &         " the value read in: ",energia(0),eini," point",
       &         iii+1,indstart(me1)+iii," T",
       &         1.0d0/(1.987D-3*beta_h(ib,ipar))
-               errmsg_count=errmsg_count+1
+              call enerprint(energia(0),fT)
+              call pdbout(iii+1,beta_h(ib,ipar),
+      &                   eini,energia(0),0.0d0,rmsdev)
+              write (iout,*)
  
+               errmsg_count=errmsg_count+1
 +              call pdbout(indstart(me1)+iii,
 +     & 1.0d0/(1.987D-3*beta_h(ib,ipar)),
 +     &energia(0),eini,0.0d0,0.0d0)
 +              call enerprint(energia(0),fT)
                if (errmsg_count.gt.maxerrmsg_count) 
       &          write (iout,*) "Too many warning messages"
                if (einicheck.gt.1) then
            do k=1,21
              enetb(k,iii+1,iparm)=energia(k)
            enddo
+ c          write(iout,*) "iCHUJ TU STRZELI",i,iii,entfac(i)
+ c          call enerprint(energia(0),fT)
  #ifdef DEBUG
            write (iout,'(2i5,f10.1,3e15.5)') i,iii,
       &     1.0d0/(beta_h(ib,ipar)*1.987D-3),energia(0),eini,efree
- c          call enerprint(energia(0),fT)
- #endif
- #ifdef DEBUG
+           call enerprint(energia(0),fT)
            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)
            write (iout,'(8f10.4)') (vbld(k),k=nnt+1,nct)
            write (iout,'(8f10.5)') (q(k,iii+1),k=1,nQ)
            write (iout,'(f10.5,i10)') rmsdev,iscor
            call enerprint(energia(0),fT)
+         write(liczba,'(bz,i3.3)') me
+         nazwa="test"//liczba//".pdb"
+         write (iout,*) "pdb file",nazwa
+         open (ipdb,file=nazwa,position="append")
          call pdbout(ii+1,beta_h(ib,ipar),efree,energia(0),0.0d0,rmsdev)
+         close(ipdb)
  #endif
          endif
  
@@@ -437,12 -443,22 +447,22 @@@ c--------------------------------------
  #else
        do i=1,ntot(islice)
  #endif
+ cc        if (dyn_ss) then
+ cc        read(ientout,rec=i,err=101)
+ cc     &    ((csingle(l,k),l=1,3),k=1,nres),
+ cc     &    ((csingle(l,k+nres),l=1,3),k=nnt,nct),
+ cc     &    nss,(idssb(k),jdssb(k),k=1,nss),
+ cc     &    eini,efree,rmsdev,(q(k,i),k=1,nQ),iR,ib,iparm
+ cc        idssb(k)=idssb(k)-nres
+ cc        jdssb(k)=jdssb(k)-nres
+ cc        else
          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),
       &    nss,(ihpb(k),jhpb(k),k=1,nss),
       &    eini,efree,rmsdev,(q(k,i),k=1,nQ),iR,ib,iparm
- c        write (iout,*) iR,ib,iparm,eini,efree
+ cc         endif
+ cc        write (iout,*) 'CC', iR,ib,iparm,eini,efree
          do j=1,2*nres
            do k=1,3
              c(k,j)=csingle(k,j)
          iscore=0
          if (indpdb.gt.0) then
            call conf_compar(i,.false.,.true.)
-         endif
+         endif 
+ c        if (dyn_ss) then
          if (bxfile .or.cxfile .or. ensembles.gt.0) write (ientin,rec=i)  
       &    ((csingle(l,k),l=1,3),k=1,nres),
       &    ((csingle(l,k+nres),l=1,3),k=nnt,nct),
       &    nss,(ihpb(k),jhpb(k),k=1,nss),
  c     &    potE(i,iparm),-entfac(i),rms_nat,iscore 
       &    potE(i,nparmset),-entfac(i),rms_nat,iscore 
- c        write (iout,'(2i5,3e15.5)') i,me,potE(i,iparm),-entfac(i)
+ c        else
+           if (bxfile .or.cxfile .or. ensembles.gt.0) write
+      &     (ientin,rec=i)
+      &    ((csingle(l,k),l=1,3),k=1,nres),
+      &    ((csingle(l,k+nres),l=1,3),k=nnt,nct),
+      &    nss,(ihpb(k),jhpb(k),k=1,nss),
+ c     &    potE(i,iparm),-entfac(i),rms_nat,iscore
+      &    potE(i,nparmset),-entfac(i),rms_nat,iscore
+ c        endif
+         write (iout,'(2i5,3e15.5)') i,me,potE(i,iparm),-entfac(i)
  #ifndef MPI
          if (cxfile) call cxwrite(ixdrf,csingle,potE(i,nparmset),
       &    -entfac(i),rms_nat,iscore)
@@@ -538,17 -564,37 +568,37 @@@ c        write (iout,*) "j",j," indstar
  c        call flush(iout)
          do i=indstart(j),indend(j)
            iii = iii+1
+ cc          if (dyn_ss) then
+ cc          read(ientin,rec=iii,err=101)
+ cc     &      ((csingle(l,k),l=1,3),k=1,nres),
+ cc     &      ((csingle(l,k+nres),l=1,3),k=nnt,nct),
+ cc     &      nss,(idssb(k),jdssb(k),k=1,nss),
+ cc    &      eini,efree,rmsdev,iscor
+ cc          idssb(k)=idssb(k)-nres
+ cc          jdssb(k)=jdssb(k)-nres
+ cc          else
            read(ientin,rec=iii,err=101)
       &      ((csingle(l,k),l=1,3),k=1,nres),
       &      ((csingle(l,k+nres),l=1,3),k=nnt,nct),
       &      nss,(ihpb(k),jhpb(k),k=1,nss),
       &      eini,efree,rmsdev,iscor
+ cc          endif
            if (bxfile .or. ensembles.gt.0) then
-             write (ientout,rec=i)
+ cc          if (dyn_ss) then
+ cc            write (ientout,rec=i)
+ cc     &        ((csingle(l,k),l=1,3),k=1,nres),
+ cc     &        ((csingle(l,k+nres),l=1,3),k=nnt,nct),
+ cc     &        nss,(idssb(k)+nres,jdssb(k)+nres,k=1,nss),
+ cc    &        eini,efree,rmsdev,iscor
+ cc           else
+                         write (ientout,rec=i)
       &        ((csingle(l,k),l=1,3),k=1,nres),
       &        ((csingle(l,k+nres),l=1,3),k=nnt,nct),
       &        nss,(ihpb(k),jhpb(k),k=1,nss),
       &        eini,efree,rmsdev,iscor
+ cc           write(iout,*) "W poszukiwaniu zlotych galotow"
+ cc           write(iout,*) "efree=",efree,iii
+ cc           endif
            endif
            if(cxfile)call cxwrite(ixdrf,csingle,eini,efree,rmsdev,iscor)
  #ifdef DEBUG
@@@ -642,6 -688,7 +692,7 @@@ c      call flush(iout
  
  c      write (iout,*) "itmp",itmp
  c      call flush(iout)
+ c       write (iout,*) "CNZ",eini,dyn_ss
  #if (defined(AIX) && !defined(JUBL))
        call xdrf3dfcoord_(ixdrf, xoord, itmp, prec, iret)
  
@@@ -649,8 -696,13 +700,13 @@@ c      write (iout,*) "xdrf3dfcoord
  c      call flush(iout)
        call xdrfint_(ixdrf, nss, iret)
        do j=1,nss
+ cc       if (dyn_ss) then
+ cc       call xdrfint_(ixdrf, idssb(j)+nres, iret)
+ cc        call xdrfint_(ixdrf, jdssb(j)+nres, iret)
+ cc       else
          call xdrfint_(ixdrf, ihpb(j), iret)
          call xdrfint_(ixdrf, jhpb(j), iret)
+ cc       endif
        enddo
        call xdrffloat_(ixdrf,real(eini),iret) 
        call xdrffloat_(ixdrf,real(efree),iret) 
  
        call xdrfint(ixdrf, nss, iret)
        do j=1,nss
+ cc       if (dyn_ss) then
+ cc        call xdrfint(ixdrf, idssb(j), iret)
+ cc        call xdrfint(ixdrf, jdssb(j), iret)
+ cc        idssb(j)=idssb(j)-nres
+ cc       jdssb(j)=jdssb(j)-nres
+ cc       else
          call xdrfint(ixdrf, ihpb(j), iret)
          call xdrfint(ixdrf, jhpb(j), iret)
+ cc       endif
        enddo
        call xdrffloat(ixdrf,real(eini),iret) 
-       call xdrffloat(ixdrf,real(efree),iret) 
+       call xdrffloat(ixdrf,real(efree),iret)
        call xdrffloat(ixdrf,real(rmsdev),iret) 
        call xdrfint(ixdrf,iscor,iret) 
  #endif
@@@ -730,8 -789,7 +793,8 @@@ c--------------------------------------
        enddo
        do j=nnt,nct
          itj=itype(j)
 -        if (itype(j).ne.10 .and. (vbld(nres+j)-dsc(itj)).gt.2.0d0) then
 +        if (itype(j).ne.10 .and.(vbld(nres+j)-dsc(iabs(itj))).gt.2.0d0) 
 +     & then
            if (iprint.gt.0) 
       &    write (iout,*) "Bad CA-SC bond length",j," ",vbld(nres+j),
       &     " for conformation",ii
@@@ -107,7 -107,7 +107,7 @@@ 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
        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
        energia(19)=esccor
        energia(20)=edihcnstr
        energia(21)=evdw_t
+ c      if (dyn_ss) call dyn_set_nss
  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)
        enddo
        endif
        return
@@@ -368,9 -369,8 +369,9 @@@ cd    print *,'Entering ELJ nnt=',nnt,
        evdw=0.0D0
        evdw_t=0.0d0
        do i=iatsc_s,iatsc_e
 -        itypi=itype(i)
 -        itypi1=itype(i+1)
 +        itypi=iabs(itype(i))
 +        if (itypi.eq.ntyp1) cycle
 +        itypi1=iabs(itype(i+1))
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
  cd        write (iout,*) 'i=',i,' iint=',iint,' istart=',istart(i,iint),
  cd   &                  'iend=',iend(i,iint)
            do j=istart(i,iint),iend(i,iint)
 -            itypj=itype(j)
 +            itypj=iabs(itype(j))
 +            if (itypj.eq.ntyp1) cycle
              xj=c(1,nres+j)-xi
              yj=c(2,nres+j)-yi
              zj=c(3,nres+j)-zi
@@@ -541,9 -540,8 +542,9 @@@ c     print *,'Entering ELJK nnt=',nnt,
        evdw=0.0D0
        evdw_t=0.0d0
        do i=iatsc_s,iatsc_e
 -        itypi=itype(i)
 -        itypi1=itype(i+1)
 +        itypi=iabs(itype(i))
 +        if (itypi.eq.ntyp1) cycle
 +        itypi1=iabs(itype(i+1))
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
@@@ -552,8 -550,7 +553,8 @@@ C Calculate SC interaction energy
  C
          do iint=1,nint_gr(i)
            do j=istart(i,iint),iend(i,iint)
 -            itypj=itype(j)
 +            itypj=iabs(itype(j))
 +            if (itypj.eq.ntyp1) cycle
              xj=c(1,nres+j)-xi
              yj=c(2,nres+j)-yi
              zj=c(3,nres+j)-zi
@@@ -654,8 -651,8 +655,8 @@@ c     els
  c     endif
        ind=0
        do i=iatsc_s,iatsc_e
 -        itypi=itype(i)
 -        itypi1=itype(i+1)
 +        itypi=iabs(itype(i))
 +        itypi1=iabs(itype(i+1))
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
          do iint=1,nint_gr(i)
            do j=istart(i,iint),iend(i,iint)
              ind=ind+1
 -            itypj=itype(j)
 +            itypj=iabs(itype(j))
              dscj_inv=vbld_inv(j+nres)
              chi1=chi(itypi,itypj)
              chi2=chi(itypj,itypi)
        include 'COMMON.ENEPS'
        include 'COMMON.IOUNITS'
        include 'COMMON.CALC'
+       include 'COMMON.SBRIDGE'
        logical lprn
        common /srutu/icall
        integer icant
@@@ -790,8 -788,8 +792,8 @@@ c     print *,'Entering EGB nnt=',nnt,
  c      if (icall.gt.0) lprn=.true.
        ind=0
        do i=iatsc_s,iatsc_e
 -        itypi=itype(i)
 -        itypi1=itype(i+1)
 +        itypi=iabs(itype(i))
 +        itypi1=iabs(itype(i+1))
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
@@@ -804,8 -802,23 +806,23 @@@ C Calculate SC interaction energy
  C
          do iint=1,nint_gr(i)
            do j=istart(i,iint),iend(i,iint)
+ C in case of diagnostics    write (iout,*) "TU SZUKAJ",i,j,dyn_ss_mask(i),dyn_ss_mask(j)
+ C /06/28/2013 Adasko: In case of dyn_ss - dynamic disulfide bond
+ C formation no electrostatic interactions should be calculated. If it
+ C would be allowed NaN would appear
+             IF (dyn_ss_mask(i).and.dyn_ss_mask(j)) THEN
+ C /06/28/2013 Adasko: dyn_ss_mask is logical statement wheather this Cys
+ C residue can or cannot form disulfide bond. There is still bug allowing
+ C Cys...Cys...Cys bond formation
+               call dyn_ssbond_ene(i,j,evdwij)
+ C /06/28/2013 Adasko: dyn_ssbond_ene is dynamic SS bond foration energy
+ C function in ssMD.F
+               evdw=evdw+evdwij
+ c              if (energy_dec) write (iout,'(a6,2i5,0pf7.3,a3)')
+ c     &                        'evdw',i,j,evdwij,' ss'
+             ELSE
              ind=ind+1
 -            itypj=itype(j)
 +            itypj=iabs(itype(j))
              dscj_inv=vbld_inv(j+nres)
              sig0ij=sigma(itypi,itypj)
              chi1=chi(itypi,itypj)
@@@ -870,6 -883,7 +887,7 @@@ c--------------------------------------
  c            write (iout,*) "i",i," j",j," itypi",itypi," itypj",itypj,
  c     &         " ij",ij," eneps",aux*e1/dabs(eps(itypi,itypj)),
  c     &         aux*e2/eps(itypi,itypj)
+ c       write (iout,'(a6,2i5,0pf7.3)') 'evdw',i,j,evdwij
              if (lprn) then
              sigm=dabs(aa(itypi,itypj)/bb(itypi,itypj))**(1.0D0/6.0D0)
              epsi=bb(itypi,itypj)**2/aa(itypi,itypj)
@@@ -893,6 -907,7 +911,7 @@@ C Calculate the radial part of the grad
  C Calculate angular part of the gradient.
              call sc_grad
              endif
+             ENDIF    ! dyn_ss
            enddo      ! j
          enddo        ! iint
        enddo          ! i
@@@ -935,8 -950,8 +954,8 @@@ c     print *,'Entering EGB nnt=',nnt,
  c      if (icall.gt.0) lprn=.true.
        ind=0
        do i=iatsc_s,iatsc_e
 -        itypi=itype(i)
 -        itypi1=itype(i+1)
 +        itypi=iabs(itype(i))
 +        itypi1=iabs(itype(i+1))
          xi=c(1,nres+i)
          yi=c(2,nres+i)
          zi=c(3,nres+i)
          do iint=1,nint_gr(i)
            do j=istart(i,iint),iend(i,iint)
              ind=ind+1
 -            itypj=itype(j)
 +            itypj=iabs(itype(j))
              dscj_inv=vbld_inv(j+nres)
              sig0ij=sigma(itypi,itypj)
              r0ij=r0(itypi,itypj)
@@@ -2789,7 -2804,7 +2808,7 @@@ c     &   " iscp",(iscpstart(i,j),iscpe
          do iint=1,nscp_gr(i)
  
          do j=iscpstart(i,iint),iscpend(i,iint)
 -          itypj=itype(j)
 +          itypj=iabs(itype(j))
  C Uncomment following three lines for SC-p interactions
  c         xj=c(1,nres+j)-xi
  c         yj=c(2,nres+j)-yi
@@@ -2901,10 -2916,12 +2920,13 @@@ 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.
-         if (ii.gt.nres .and. iabs(itype(iii)).eq.1 .and.
+         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
          else if (ii.gt.nres .and. jj.gt.nres) then
  c Restraints from contact prediction
        include 'COMMON.VAR'
        include 'COMMON.IOUNITS'
        double precision erij(3),dcosom1(3),dcosom2(3),gg(3)
 -      itypi=itype(i)
 +      itypi=iabs(itype(i))
        xi=c(1,nres+i)
        yi=c(2,nres+i)
        zi=c(3,nres+i)
        dyi=dc_norm(2,nres+i)
        dzi=dc_norm(3,nres+i)
        dsci_inv=dsc_inv(itypi)
 -      itypj=itype(j)
 +      itypj=iabs(itype(j))
        dscj_inv=dsc_inv(itypj)
        xj=c(1,nres+j)-xi
        yj=c(2,nres+j)-yi
        deltat12=om2-om1+2.0d0
        cosphi=om12-om1*om2
        eij=akcm*deltad*deltad+akth*(deltat1*deltat1+deltat2*deltat2)
-      &  +akct*deltad*deltat12
+      &  +akct*deltad*deltat12+ebr
+ c     &  +akct*deltad*deltat12
       &  +v1ss*cosphi+v2ss*cosphi*cosphi+v3ss*cosphi*cosphi*cosphi
- c      write(iout,*) i,j,"rij",rij,"d0cm",d0cm," akcm",akcm," akth",akth,
- c     &  " akct",akct," deltad",deltad," deltat",deltat1,deltat2,
- c     &  " deltat12",deltat12," eij",eij 
+       write(iout,*) i,j,"rij",rij,"d0cm",d0cm," akcm",akcm," akth",akth,
+      &  " akct",akct," deltad",deltad," deltat",deltat1,deltat2,
+      &  " deltat12",deltat12," eij",eij,"ebr",ebr
        ed=2*akcm*deltad+akct*deltat12
        pom1=akct*deltad
        pom2=v1ss+2*v2ss*cosphi+3*v3ss*cosphi*cosphi
        include 'COMMON.FFIELD'
        include 'COMMON.CONTROL'
        double precision u(3),ud(3)
+       logical :: lprn=.false.
        estr=0.0d0
        do i=nnt+1,nct
          diff = vbld(i)-vbldp0
  c 09/18/07 AL: multimodal bond potential based on AM1 CA-SC PMF's included
  c
        do i=nnt,nct
 -        iti=itype(i)
 +        iti=iabs(itype(i))
          if (iti.ne.10) then
            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
+             if (lprn)
+      &      write (iout,*) 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
              do j=1,3
                gradbx(j,i)=AKSC(1,iti)*diff*dc(j,i+nres)/vbld(i+nres)
                usum=usum+uprod1
                usumsqder=usumsqder+ud(j)*uprod2
              enddo
- c            write (iout,*) i,iti,vbld(i+nres),(vbldsc0(j,iti),
- c     &      AKSC(j,iti),abond0(j,iti),u(j),j=1,nbi)
+             if (lprn)
+      &      write (iout,*) i,iti,vbld(i+nres),(vbldsc0(j,iti),
+      &      AKSC(j,iti),abond0(j,iti),u(j),j=1,nbi)
              estr=estr+uprod/usum
              do j=1,3
               gradbx(j,i)=usumsqder/(usum*usum)*dc(j,i+nres)/vbld(i+nres)
@@@ -3180,18 -3201,6 +3206,18 @@@ c      write (iout,*) ithet_start,ithet
  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)
 +        ichir1=isign(1,itype(i-2))
 +        ichir2=isign(1,itype(i))
 +         if (itype(i-2).eq.10) ichir1=isign(1,itype(i-1))
 +         if (itype(i).eq.10) ichir2=isign(1,itype(i-1))
 +         if (itype(i-1).eq.10) then
 +          itype1=isign(10,itype(i-2))
 +          ichir11=isign(1,itype(i-2))
 +          ichir12=isign(1,itype(i-2))
 +          itype2=isign(10,itype(i))
 +          ichir21=isign(1,itype(i))
 +          ichir22=isign(1,itype(i))
 +         endif
  c        if (i.gt.ithet_start .and. 
  c     &     (itel(i-1).eq.0 .or. itel(i-2).eq.0)) goto 1215
  c        if (i.gt.3 .and. (i.le.4 .or. itel(i-3).ne.0)) then
@@@ -3247,12 -3256,8 +3273,12 @@@ C dependent on the adjacent virtual-bon
  C In following comments this theta will be referred to as t_c.
          thet_pred_mean=0.0d0
          do k=1,2
 -          athetk=athet(k,it)
 -          bthetk=bthet(k,it)
 +            athetk=athet(k,it,ichir1,ichir2)
 +            bthetk=bthet(k,it,ichir1,ichir2)
 +          if (it.eq.10) then
 +             athetk=athet(k,itype1,ichir11,ichir12)
 +             bthetk=bthet(k,itype2,ichir21,ichir22)
 +          endif
            thet_pred_mean=thet_pred_mean+athetk*y(k)+bthetk*z(k)
          enddo
  c        write (iout,*) "thet_pred_mean",thet_pred_mean
          thet_pred_mean=thet_pred_mean*ss+a0thet(it)
  c        write (iout,*) "thet_pred_mean",thet_pred_mean
  C Derivatives of the "mean" values in gamma1 and gamma2.
 -        dthetg1=(-athet(1,it)*y(2)+athet(2,it)*y(1))*ss
 -        dthetg2=(-bthet(1,it)*z(2)+bthet(2,it)*z(1))*ss
 +        dthetg1=(-athet(1,it,ichir1,ichir2)*y(2)
 +     &+athet(2,it,ichir1,ichir2)*y(1))*ss
 +         dthetg2=(-bthet(1,it,ichir1,ichir2)*z(2)
 +     &          +bthet(2,it,ichir1,ichir2)*z(1))*ss
 +         if (it.eq.10) then
 +      dthetg1=(-athet(1,itype1,ichir11,ichir12)*y(2)
 +     &+athet(2,itype1,ichir11,ichir12)*y(1))*ss
 +        dthetg2=(-bthet(1,itype2,ichir21,ichir22)*z(2)
 +     &         +bthet(2,itype2,ichir21,ichir22)*z(1))*ss
 +         endif
          if (theta(i).gt.pi-delta) then
            call theteng(pi-delta,thet_pred_mean,theta0(it),f0,fprim0,
       &         E_tc0)
        etheta=0.0D0
  c      write (iout,*) "ithetyp",(ithetyp(i),i=1,ntyp1)
        do i=ithet_start,ithet_end
 +        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)
 -        ityp2=ithetyp(itype(i-1))
 +        ityp2=ithetyp((itype(i-1)))
          do k=1,nntheterm
            coskt(k)=dcos(k*theti2)
            sinkt(k)=dsin(k*theti2)
  #else
            phii=phi(i)
  #endif
 -          ityp1=ithetyp(itype(i-2))
 +          ityp1=ithetyp(iabs(itype(i-2)))
            do k=1,nsingle
              cosph1(k)=dcos(k*phii)
              sinph1(k)=dsin(k*phii)
  #else
            phii1=phi(i+1)
  #endif
 -          ityp3=ithetyp(itype(i))
 +          ityp3=ithetyp((itype(i)))
            do k=1,nsingle
              cosph2(k)=dcos(k*phii1)
              sinph2(k)=dsin(k*phii1)
  c        write (iout,*) "i",i," ityp1",itype(i-2),ityp1,
  c     &   " ityp2",itype(i-1),ityp2," ityp3",itype(i),ityp3
  c        call flush(iout)
 -        ethetai=aa0thet(ityp1,ityp2,ityp3)
 +        ethetai=aa0thet(ityp1,ityp2,ityp3,iblock)
          do k=1,ndouble
            do l=1,k-1
              ccl=cosph1(l)*cosph2(k-l)
          enddo
          endif
          do k=1,ntheterm
 -          ethetai=ethetai+aathet(k,ityp1,ityp2,ityp3)*sinkt(k)
 -          dethetai=dethetai+0.5d0*k*aathet(k,ityp1,ityp2,ityp3)
 +          ethetai=ethetai+aathet(k,ityp1,ityp2,ityp3,iblock)*sinkt(k)
 +          dethetai=dethetai+0.5d0*k*aathet(k,ityp1,ityp2,ityp3,iblock)
       &      *coskt(k)
            if (lprn)
 -     &    write (iout,*) "k",k," aathet",aathet(k,ityp1,ityp2,ityp3),
 +     &    write (iout,*) "k",k," aathet",aathet(k,ityp1,ityp2,ityp3,
 +     &      iblock),
       &     " ethetai",ethetai
          enddo
          if (lprn) then
          endif
          do m=1,ntheterm2
            do k=1,nsingle
 -            aux=bbthet(k,m,ityp1,ityp2,ityp3)*cosph1(k)
 -     &         +ccthet(k,m,ityp1,ityp2,ityp3)*sinph1(k)
 -     &         +ddthet(k,m,ityp1,ityp2,ityp3)*cosph2(k)
 -     &         +eethet(k,m,ityp1,ityp2,ityp3)*sinph2(k)
 +            aux=bbthet(k,m,ityp1,ityp2,ityp3,iblock)*cosph1(k)
 +     &         +ccthet(k,m,ityp1,ityp2,ityp3,iblock)*sinph1(k)
 +     &         +ddthet(k,m,ityp1,ityp2,ityp3,iblock)*cosph2(k)
 +     &         +eethet(k,m,ityp1,ityp2,ityp3,iblock)*sinph2(k)
              ethetai=ethetai+sinkt(m)*aux
              dethetai=dethetai+0.5d0*m*aux*coskt(m)
              dephii=dephii+k*sinkt(m)*(
 -     &          ccthet(k,m,ityp1,ityp2,ityp3)*cosph1(k)-
 -     &          bbthet(k,m,ityp1,ityp2,ityp3)*sinph1(k))
 +     &          ccthet(k,m,ityp1,ityp2,ityp3,iblock)*cosph1(k)-
 +     &          bbthet(k,m,ityp1,ityp2,ityp3,iblock)*sinph1(k))
              dephii1=dephii1+k*sinkt(m)*(
 -     &          eethet(k,m,ityp1,ityp2,ityp3)*cosph2(k)-
 -     &          ddthet(k,m,ityp1,ityp2,ityp3)*sinph2(k))
 +     &          eethet(k,m,ityp1,ityp2,ityp3,iblock)*cosph2(k)-
 +     &          ddthet(k,m,ityp1,ityp2,ityp3,iblock)*sinph2(k))
              if (lprn)
       &      write (iout,*) "m",m," k",k," bbthet",
 -     &         bbthet(k,m,ityp1,ityp2,ityp3)," ccthet",
 -     &         ccthet(k,m,ityp1,ityp2,ityp3)," ddthet",
 -     &         ddthet(k,m,ityp1,ityp2,ityp3)," eethet",
 -     &         eethet(k,m,ityp1,ityp2,ityp3)," ethetai",ethetai
 +     &         bbthet(k,m,ityp1,ityp2,ityp3,iblock)," ccthet",
 +     &         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
            enddo
          enddo
          if (lprn)
          do m=1,ntheterm3
            do k=2,ndouble
              do l=1,k-1
 -              aux=ffthet(l,k,m,ityp1,ityp2,ityp3)*cosph1ph2(l,k)+
 -     &            ffthet(k,l,m,ityp1,ityp2,ityp3)*cosph1ph2(k,l)+
 -     &            ggthet(l,k,m,ityp1,ityp2,ityp3)*sinph1ph2(l,k)+
 -     &            ggthet(k,l,m,ityp1,ityp2,ityp3)*sinph1ph2(k,l)
 +              aux=ffthet(l,k,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(l,k)+
 +     &            ffthet(k,l,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(k,l)+
 +     &            ggthet(l,k,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(l,k)+
 +     &            ggthet(k,l,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(k,l)
                ethetai=ethetai+sinkt(m)*aux
                dethetai=dethetai+0.5d0*m*coskt(m)*aux
                dephii=dephii+l*sinkt(m)*(
 -     &           -ffthet(l,k,m,ityp1,ityp2,ityp3)*sinph1ph2(l,k)-
 -     &            ffthet(k,l,m,ityp1,ityp2,ityp3)*sinph1ph2(k,l)+
 -     &            ggthet(l,k,m,ityp1,ityp2,ityp3)*cosph1ph2(l,k)+
 -     &            ggthet(k,l,m,ityp1,ityp2,ityp3)*cosph1ph2(k,l))
 +     &           -ffthet(l,k,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(l,k)-
 +     &            ffthet(k,l,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(k,l)+
 +     &            ggthet(l,k,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(l,k)+
 +     &            ggthet(k,l,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(k,l))
                dephii1=dephii1+(k-l)*sinkt(m)*(
 -     &           -ffthet(l,k,m,ityp1,ityp2,ityp3)*sinph1ph2(l,k)+
 -     &            ffthet(k,l,m,ityp1,ityp2,ityp3)*sinph1ph2(k,l)+
 -     &            ggthet(l,k,m,ityp1,ityp2,ityp3)*cosph1ph2(l,k)-
 -     &            ggthet(k,l,m,ityp1,ityp2,ityp3)*cosph1ph2(k,l))
 +     &           -ffthet(l,k,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(l,k)+
 +     &            ffthet(k,l,m,ityp1,ityp2,ityp3,iblock)*sinph1ph2(k,l)+
 +     &            ggthet(l,k,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(l,k)-
 +     &            ggthet(k,l,m,ityp1,ityp2,ityp3,iblock)*cosph1ph2(k,l))
                if (lprn) then
                write (iout,*) "m",m," k",k," l",l," ffthet",
 -     &            ffthet(l,k,m,ityp1,ityp2,ityp3),
 -     &            ffthet(k,l,m,ityp1,ityp2,ityp3)," ggthet",
 -     &            ggthet(l,k,m,ityp1,ityp2,ityp3),
 -     &            ggthet(k,l,m,ityp1,ityp2,ityp3)," ethetai",ethetai
 +     &            ffthet(l,k,m,ityp1,ityp2,ityp3,iblock),
 +     &            ffthet(k,l,m,ityp1,ityp2,ityp3,iblock)," ggthet",
 +     &            ggthet(l,k,m,ityp1,ityp2,ityp3,iblock),
 +     &            ggthet(k,l,m,ityp1,ityp2,ityp3,iblock)," ethetai",
 +     &            ethetai
                write (iout,*) cosph1ph2(l,k)*sinkt(m),
       &            cosph1ph2(k,l)*sinkt(m),
       &            sinph1ph2(l,k)*sinkt(m),sinph1ph2(k,l)*sinkt(m)
@@@ -3635,7 -3628,7 +3661,7 @@@ c     write (iout,'(a)') 'ESC
        do i=loc_start,loc_end
          it=itype(i)
          if (it.eq.10) goto 1
 -        nlobit=nlob(it)
 +        nlobit=nlob(iabs(it))
  c       print *,'i=',i,' it=',it,' nlobit=',nlobit
  c       write (iout,*) 'i=',i,' ssa=',ssa,' ssad=',ssad
          theti=theta(i+1)-pipol
@@@ -3790,7 -3783,7 +3816,7 @@@ C Compute the contribution to SC energ
          do iii=-1,1
  
            do j=1,nlobit
 -            expfac=dexp(bsc(j,it)-0.5D0*contr(j,iii)+emin)
 +            expfac=dexp(bsc(j,iabs(it))-0.5D0*contr(j,iii)+emin)
  cd          print *,'j=',j,' expfac=',expfac
              escloc_i=escloc_i+expfac
              do k=1,3
@@@ -3871,7 -3864,7 +3897,7 @@@ C Compute the contribution to SC energ
  
        dersc12=0.0d0
        do j=1,nlobit
 -        expfac=dexp(bsc(j,it)-0.5D0*contr(j)+emin)
 +        expfac=dexp(bsc(j,iabs(it))-0.5D0*contr(j)+emin)
          escloc_i=escloc_i+expfac
          do k=1,2
            dersc(k)=dersc(k)+Ax(k,j)*expfac
          cosfac=dsqrt(cosfac2)
          sinfac2=0.5d0/(1.0d0-costtab(i+1))
          sinfac=dsqrt(sinfac2)
 -        it=itype(i)
 +        it=iabs(itype(i))
          if (it.eq.10) goto 1
  c
  C  Compute the axes of tghe local cartesian coordinates system; store in
@@@ -3952,7 -3945,7 +3978,7 @@@ C     &   dc_norm(3,i+nres
            y_prime(j) = (dc_norm(j,i) + dc_norm(j,i-1))*sinfac
          enddo
          do j = 1,3
 -          z_prime(j) = -uz(j,i-1)
 +          z_prime(j) = -uz(j,i-1)*dsign(1.0d0,dfloat(itype(i)))
          enddo     
  c       write (2,*) "i",i
  c       write (2,*) "x_prime",(x_prime(j),j=1,3)
  C Compute the energy of the ith side cbain
  C
  c        write (2,*) "xx",xx," yy",yy," zz",zz
 -        it=itype(i)
 +        it=iabs(itype(i))
          do j = 1,65
            x(j) = sc_parmin(j,it) 
          enddo
  Cc diagnostics - remove later
          xx1 = dcos(alph(2))
          yy1 = dsin(alph(2))*dcos(omeg(2))
 -        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
@@@ -4036,7 -4029,8 +4062,8 @@@ c        sumene = enesc(x,xx,yy,zz,cost
          escloc = escloc + sumene
  c        write (2,*) "escloc",escloc
          if (.not. calc_grad) goto 1
- #ifdef DEBUG
+ #ifdef DEBUG2
  C
  C This section to check the numerical derivatives of the energy of ith side
  C chain in xx, yy, zz, and theta. Use the -DDEBUG compiler option or insert
@@@ -4163,11 -4157,8 +4190,11 @@@ c     &   (dC_norm(j,i-1),j=1,3)," vbld
           dZZ_Ci1(k)=0.0d0
           dZZ_Ci(k)=0.0d0
           do j=1,3
 -           dZZ_Ci(k)=dZZ_Ci(k)-uzgrad(j,k,2,i-1)*dC_norm(j,i+nres)
 -           dZZ_Ci1(k)=dZZ_Ci1(k)-uzgrad(j,k,1,i-1)*dC_norm(j,i+nres)
 +            dZZ_Ci(k)=dZZ_Ci(k)-uzgrad(j,k,2,i-1)
 +     & *dsign(1.0d0,dfloat(itype(i)))*dC_norm(j,i+nres)
 +            dZZ_Ci1(k)=dZZ_Ci1(k)-uzgrad(j,k,1,i-1)
 +     &  *dsign(1.0d0,dfloat(itype(i)))*dC_norm(j,i+nres)
 +
           enddo
            
           dXX_XYZ(k)=vbld_inv(i+nres)*(x_prime(k)-xx*dC_norm(k,i+nres))
@@@ -4403,19 -4394,14 +4430,19 @@@ c      lprn=.true
        etors=0.0D0
        do i=iphi_start,iphi_end
          if (itel(i-2).eq.0 .or. itel(i-1).eq.0) goto 1215
 +         if (iabs(itype(i)).eq.20) then
 +         iblock=2
 +         else
 +         iblock=1
 +         endif
          itori=itortyp(itype(i-2))
          itori1=itortyp(itype(i-1))
          phii=phi(i)
          gloci=0.0D0
  C Regular cosine and sine terms
 -        do j=1,nterm(itori,itori1)
 -          v1ij=v1(j,itori,itori1)
 -          v2ij=v2(j,itori,itori1)
 +        do j=1,nterm(itori,itori1,iblock)
 +          v1ij=v1(j,itori,itori1,iblock)
 +          v2ij=v2(j,itori,itori1,iblock)
            cosphi=dcos(j*phii)
            sinphi=dsin(j*phii)
            etors=etors+v1ij*cosphi+v2ij*sinphi
@@@ -4428,7 -4414,7 +4455,7 @@@ C          [v2 cos(phi/2)+v3 sin(phi/2)
  C
          cosphi=dcos(0.5d0*phii)
          sinphi=dsin(0.5d0*phii)
 -        do j=1,nlor(itori,itori1)
 +        do j=1,nlor(itori,itori1,iblock)
            vl1ij=vlor1(j,itori,itori1)
            vl2ij=vlor2(j,itori,itori1)
            vl3ij=vlor3(j,itori,itori1)
            gloci=gloci+vl1ij*(vl3ij*cosphi-vl2ij*sinphi)*pom
          enddo
  C Subtract the constant term
 -        etors=etors-v0(itori,itori1)
 +        etors=etors-v0(itori,itori1,iblock)
          if (lprn)
       &  write (iout,'(2(a3,2x,i3,2x),2i3,6f8.3/26x,6f8.3/)')
       &  restyp(itype(i-2)),i-2,restyp(itype(i-1)),i-1,itori,itori1,
 -     &  (v1(j,itori,itori1),j=1,6),(v2(j,itori,itori1),j=1,6)
 +     &  (v1(j,itori,itori1,1),j=1,6),(v2(j,itori,itori1,1),j=1,6)
          gloc(i-3,icg)=gloc(i-3,icg)+wtor*fact*gloci
  c       write (iout,*) 'i=',i,' gloc=',gloc(i-3,icg)
   1215   continue
@@@ -4504,26 -4490,16 +4531,24 @@@ c     lprn=.true
          itori=itortyp(itype(i-2))
          itori1=itortyp(itype(i-1))
          itori2=itortyp(itype(i))
- c        iblock=1
- c        if (iabs(itype(i+1)).eq.20) iblock=2
          phii=phi(i)
          phii1=phi(i+1)
          gloci1=0.0D0
          gloci2=0.0D0
 +        iblock=1
 +        if (iabs(itype(i+1)).eq.20) iblock=2
  C Regular cosine and sine terms
 -        do j=1,ntermd_1(itori,itori1,itori2)
 -          v1cij=v1c(1,j,itori,itori1,itori2)
 -          v1sij=v1s(1,j,itori,itori1,itori2)
 -          v2cij=v1c(2,j,itori,itori1,itori2)
 -          v2sij=v1s(2,j,itori,itori1,itori2)
 +c c       do j=1,ntermd_1(itori,itori1,itori2,iblock)
 +c          v1cij=v1c(1,j,itori,itori1,itori2,iblock)
 +c          v1sij=v1s(1,j,itori,itori1,itori2,iblock)
 +c          v2cij=v1c(2,j,itori,itori1,itori2,iblock)
 +c          v2sij=v1s(2,j,itori,itori1,itori2,iblock)
 +       do j=1,ntermd_1(itori,itori1,itori2,iblock)
 +          v1cij=v1c(1,j,itori,itori1,itori2,iblock)
 +          v1sij=v1s(1,j,itori,itori1,itori2,iblock)
 +          v2cij=v1c(2,j,itori,itori1,itori2,iblock)
 +          v2sij=v1s(2,j,itori,itori1,itori2,iblock)
 +
            cosphi1=dcos(j*phii)
            sinphi1=dsin(j*phii)
            cosphi2=dcos(j*phii1)
            gloci1=gloci1+j*(v1sij*cosphi1-v1cij*sinphi1)
            gloci2=gloci2+j*(v2sij*cosphi2-v2cij*sinphi2)
          enddo
 -        do k=2,ntermd_2(itori,itori1,itori2)
 +        do k=2,ntermd_2(itori,itori1,itori2,iblock)
            do l=1,k-1
 -            v1cdij = v2c(k,l,itori,itori1,itori2)
 -            v2cdij = v2c(l,k,itori,itori1,itori2)
 -            v1sdij = v2s(k,l,itori,itori1,itori2)
 -            v2sdij = v2s(l,k,itori,itori1,itori2)
 +            v1cdij = v2c(k,l,itori,itori1,itori2,iblock)
 +            v2cdij = v2c(l,k,itori,itori1,itori2,iblock)
 +            v1sdij = v2s(k,l,itori,itori1,itori2,iblock)
 +            v2sdij = v2s(l,k,itori,itori1,itori2,iblock)
              cosphi1p2=dcos(l*phii+(k-l)*phii1)
              cosphi1m2=dcos(l*phii-(k-l)*phii1)
              sinphi1p2=dsin(l*phii+(k-l)*phii1)
@@@ -4585,12 -4561,12 +4610,12 @@@ c        amino-acid residues
  C Set lprn=.true. for debugging
        lprn=.false.
  c      lprn=.true.
- c      write (iout,*) "EBACK_SC_COR",iphi_start,iphi_end,nterm_sccor
+ c      write (iout,*) "EBACK_SC_COR",itau_start,itau_end,nterm_sccor
        esccor=0.0D0
        do i=itau_start,itau_end
          esccor_ii=0.0D0
 -        isccori=isccortyp(itype(i-2))
 -        isccori1=isccortyp(itype(i-1))
 +        isccori=isccortyp((itype(i-2)))
 +        isccori1=isccortyp((itype(i-1)))
          phii=phi(i)
  cccc  Added 9 May 2012
  cc Tauangle is torsional engle depending on the value of first digit 
@@@ -4607,14 -4583,14 +4632,14 @@@ c   2 = Ca...Ca...Ca...S
  c   3 = SC...Ca...Ca...SCi
          gloci=0.0D0
          if (((intertyp.eq.3).and.((itype(i-2).eq.10).or.
 -     &      (itype(i-1).eq.10).or.(itype(i-2).eq.21).or.
 -     &      (itype(i-1).eq.21)))
 +     &      (itype(i-1).eq.10).or.(itype(i-2).eq.ntyp1).or.
 +     &      (itype(i-1).eq.ntyp1)))
       &    .or. ((intertyp.eq.1).and.((itype(i-2).eq.10)
 -     &     .or.(itype(i-2).eq.21)))
 +     &     .or.(itype(i-2).eq.ntyp1)))
       &    .or.((intertyp.eq.2).and.((itype(i-1).eq.10).or.
 -     &      (itype(i-1).eq.21)))) cycle
 -        if ((intertyp.eq.2).and.(i.eq.4).and.(itype(1).eq.21)) cycle
 -        if ((intertyp.eq.1).and.(i.eq.nres).and.(itype(nres).eq.21))
 +     &      (itype(i-1).eq.ntyp1)))) cycle
 +        if ((intertyp.eq.2).and.(i.eq.4).and.(itype(1).eq.ntyp1)) cycle
 +        if ((intertyp.eq.1).and.(i.eq.nres).and.(itype(nres).eq.ntyp1))
       & cycle
          do j=1,nterm_sccor(isccori,isccori1)
            v1ij=v1sccor(j,intertyp,isccori,isccori1)
@@@ -4632,7 -4608,7 +4657,7 @@@ c     &gloc_sc(intertyp,i-3,icg
       &  restyp(itype(i-2)),i-2,restyp(itype(i-1)),i-1,itori,itori1,
       &  (v1sccor(j,intertyp,itori,itori1),j=1,6)
       & ,(v2sccor(j,intertyp,itori,itori1),j=1,6)
 -        gsccor_loc(i-3)=gsccor_loc(i-3)+gloci
 +c        gsccor_loc(i-3)=gsccor_loc(i-3)+gloci
         enddo !intertyp
        enddo
  c        do i=1,nres
@@@ -4745,9 -4721,9 +4770,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=num_cont_hb(atom)
        do i=1,num_kont
          do k=1,7
@@@ -4770,10 -4746,9 +4795,10 @@@ 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
@@@ -6508,7 -6483,7 +6533,7 @@@ c--------------------------------------
        include 'COMMON.GEO'
        logical swap
        double precision vv(2),pizda(2,2),auxmat(2,2),auxvec(2),
-      & auxvec1(2),auxvec2(1),auxmat1(2,2)
+      & auxvec1(2),auxvec2(2),auxmat1(2,2)
        logical lprn
        common /kutas/ lprn
  CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
@@@ -42,9 -42,7 +42,9 @@@
        igeom=  8
        intin=  9
        ithep= 11
 +      ithep_pdb=51
        irotam=12
 +      irotam_pdb=52
        itorp= 13
        itordp= 23
        ielep= 14
        sigii(i)=0.0D0
        rr0(i)=0.0D0
        a0thet(i)=0.0D0
 -      do j=1,2
 -        athet(j,i)=0.0D0
 -        bthet(j,i)=0.0D0
 +        do j=1,2
 +         do ichir1=-1,1
 +          do ichir2=-1,1
 +          athet(j,i,ichir1,ichir2)=0.0D0
 +          bthet(j,i,ichir1,ichir2)=0.0D0
 +          enddo
 +         enddo
          enddo
        do j=0,3
          polthet(j,i)=0.0D0
        enddo
        nlob(ntyp1)=0
        dsc(ntyp1)=0.0D0
 -      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
 +      do i=-maxtor,maxtor
 +        itortyp(i)=0
 +       do iblock=1,2
 +        do j=-maxtor,maxtor
 +          do k=1,maxterm
 +            v1(k,j,i,iblock)=0.0D0
 +            v2(k,j,i,iblock)=0.0D0
            enddo
          enddo
 +        enddo
        enddo
 +      do iblock=1,2
 +       do i=-maxtor,maxtor
 +        do j=-maxtor,maxtor
 +         do k=-maxtor,maxtor
 +          do l=1,maxtermd_1
 +            v1c(1,l,i,j,k,iblock)=0.0D0
 +            v1s(1,l,i,j,k,iblock)=0.0D0
 +            v1c(2,l,i,j,k,iblock)=0.0D0
 +            v1s(2,l,i,j,k,iblock)=0.0D0
 +          enddo !l
 +          do l=1,maxtermd_2
 +           do m=1,maxtermd_2
 +            v2c(m,l,i,j,k,iblock)=0.0D0
 +            v2s(m,l,i,j,k,iblock)=0.0D0
 +           enddo !m
 +          enddo !l
 +        enddo !k
 +       enddo !j
 +      enddo !i
 +      enddo !iblock
        do i=1,maxres
        itype(i)=0
        itel(i)=0
@@@ -187,6 -159,9 +187,9 @@@ C Initialize the bridge array
        ihpb(i)=0
        jhpb(i)=0
        enddo
+       do i=1,maxres
+         dyn_ss_mask(i)=.false.
+       enddo
  C
  C Initialize timing.
  C
@@@ -247,13 -222,9 +250,13 @@@ c--------------------------------------
        include 'COMMON.WEIGHTS'
        include 'COMMON.FFIELD'
        data restyp /
 +     &'DD' ,'DPR','DLY','DAR','DHI','DAS','DGL','DSG','DGN','DSN','DTH',
 +     &'DYY','DAL','DTY','DTR','DVA','DLE','DIL','DPN','MED','DCY','ZER',
       &'CYS','MET','PHE','ILE','LEU','VAL','TRP','TYR','ALA','GLY','THR',
       &'SER','GLN','ASN','GLU','ASP','HIS','ARG','LYS','PRO','D'/
        data onelet /
 +     &'z','p','k','r','h','d','e','n','q','s','t','g',
 +     &'a','y','w','v','l','i','f','m','c','x',
       &'C','M','F','I','L','V','W','Y','A','G','T',
       &'S','Q','N','E','D','H','R','K','P','X'/
        data potname /'LJ','LJK','BP','GB','GBV'/
@@@ -328,6 -299,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) go to 10
          do ii=1,nss
            if (ihpb(ii).eq.i+nres) then
              scheck=.true.
@@@ -381,7 -353,7 +385,7 @@@ cd      write (iout,*) 'i=',i,' scheck=
            nint_gr(i)=1
            istart(i,1)=i+1
            iend(i,1)=nct
 -          ind_scint=int_scint+nct-i
 +          ind_scint=ind_scint+nct-i
  #endif
          endif
  #ifdef MPL
@@@ -497,7 -469,7 +501,7 @@@ cd        write (iout,*) 'i.gt.nct-iscp
          endif 
        enddo ! i
  #endif
-       if (lprint) then
+               if (lprint) then
          write (iout,'(a)') 'SC-p interaction array:'
          do i=iatscp_s,iatscp_e
            write (iout,'(i3,2(2x,2i3))') 
@@@ -52,15 -52,15 +52,15 @@@ C Convert sequence to numeric cod
        write (iout,'(20i4)') (itype(i),i=1,nres)
        do i=1,nres-1
  #ifdef PROCOR
 -        if (itype(i).eq.21 .or. itype(i+1).eq.21) then
 +        if (itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1) then
  #else
 -        if (itype(i).eq.21) then
 +        if (itype(i).eq.ntyp1) then
  #endif
            itel(i)=0
  #ifdef PROCOR
 -        else if (itype(i+1).ne.20) then
 +        else if (iabs(itype(i+1)).ne.20) then
  #else
 -        else if (itype(i).ne.20) then
 +        else if (iabs(itype(i)).ne.20) then
  #endif
            itel(i)=1
          else
  
        nnt=1
        nct=nres
 -      if (itype(1).eq.21) nnt=2
 -      if (itype(nres).eq.21) nct=nct-1
 +      if (itype(1).eq.ntyp1) nnt=2
 +      if (itype(nres).eq.ntyp1) nct=nct-1
        write(iout,*) 'NNT=',NNT,' NCT=',NCT
  c Read distance restraints
        if (constr_dist.gt.0) then
+         if (refstr) call read_ref_structure(*11)
          call read_dist_constr
          call hpb_partition
        endif
        endif
        write (iout,'(a)')
        return
+    11 stop "Error reading reference structure"
        end
  c-----------------------------------------------------------------------------
        logical function seq_comp(itypea,itypeb,length)
@@@ -202,6 -204,25 +204,25 @@@ C bridging residues
          enddo
        endif
        endif
+       if (ns.gt.0.and.dyn_ss) then
+ C /06/28/2013 Adasko:ns is number of Cysteins bonded also called half of
+ C the bond
+           do i=nss+1,nhpb
+ C /06/28/2013 Adasko: nss number of full SS bonds
+             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.
+ C /06/28/2013 Adasko: dyn_ss_mask which Cysteins can form disulfidebond
+ c          write(iout,*) i,iss(i),dyn_ss_mask(iss(i)),"ATU"
+           enddo
+       endif
        return
        end
  c------------------------------------------------------------------------------
@@@ -23,7 -23,6 +23,6 @@@
        include 'COMMON.FREE'
        character*1 t1,t2,t3
        character*1 onelett(4) /"G","A","P","D"/
-       character*1 toronelet(-2:2)/"p","a","G","A","P"/
        logical lprint
        dimension blower(3,3,maxlob)
        character*800 controlcard
@@@ -52,10 -51,31 +51,31 @@@ C Assign virtual-bond lengt
          key = wname(i)(:ilen(wname(i)))
          call reada(controlcard,key(:ilen(key)),ww(i),1.0d0)
        enddo
+       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)
+ c      dyn_ss=(index(controlcard,'DYN_SS').gt.0)
        write (iout,*) "iparm",iparm," myparm",myparm
- c If reading not own parameters, skip assignment
+ c      do i=1,maxres
+ c        dyn_ss_mask(i)=.false.
+ c      enddo
+       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(me.eq.king.or..not.out1file) then
+ c       print *,'indpdb=',indpdb,' pdbref=',pdbref
+ c      endif
+ c If reading not own parameters, skip assignment
+ cc      write(iout,*) "KURWA", ww(15)
        if (iparm.eq.myparm .or. .not.separate_parset) then
  
  c
        wtor=ww(13)
        wtor_d=ww(14)
        wvdwpp=ww(16)
+       wstrain=ww(15)
        wbond=ww(18)
        wsccor=ww(19)
  
        endif
+ cc      write(iout,*) "KURWA", wstrain,akcm,akth,wsc,dyn_ss
  
        call card_concat(controlcard,.false.)
  
@@@ -102,7 -124,7 +124,7 @@@ c Return if not own parameter
        call reads(controlcard,"TORDPAR",tordname_t,tordname)
        open (itordp,file=tordname_t,status='old')
        rewind(itordp)
 -      call reads(controlcard,"SCCORAR",sccorname_t,sccorname)
 +      call reads(controlcard,"SCCORPAR",sccorname_t,sccorname)
        open (isccor,file=sccorname_t,status='old')
        rewind(isccor)
        call reads(controlcard,"FOURIER",fouriername_t,fouriername)
@@@ -194,47 -216,12 +216,47 @@@ C Read the parameters of the probabilit
  C of the virtual-bond valence angles theta
  C
        do i=1,ntyp
 -        read (ithep,*) a0thet(i),(athet(j,i),j=1,2),(bthet(j,i),j=1,2)
 +        read (ithep,*) a0thet(i),(athet(j,i,1,1),j=1,2),
 +     &    (bthet(j,i,1,1),j=1,2)
          read (ithep,*) (polthet(j,i),j=0,3)
        read (ithep,*) (gthet(j,i),j=1,3)
        read (ithep,*) theta0(i),sig0(i),sigc0(i)
        sigc0(i)=sigc0(i)**2
        enddo
 +      do i=1,ntyp
 +      athet(1,i,1,-1)=athet(1,i,1,1)
 +      athet(2,i,1,-1)=athet(2,i,1,1)
 +      bthet(1,i,1,-1)=-bthet(1,i,1,1)
 +      bthet(2,i,1,-1)=-bthet(2,i,1,1)
 +      athet(1,i,-1,1)=-athet(1,i,1,1)
 +      athet(2,i,-1,1)=-athet(2,i,1,1)
 +      bthet(1,i,-1,1)=bthet(1,i,1,1)
 +      bthet(2,i,-1,1)=bthet(2,i,1,1)
 +      enddo
 +      do i=-ntyp,-1
 +      a0thet(i)=a0thet(-i)
 +      athet(1,i,-1,-1)=athet(1,-i,1,1)
 +      athet(2,i,-1,-1)=-athet(2,-i,1,1)
 +      bthet(1,i,-1,-1)=bthet(1,-i,1,1)
 +      bthet(2,i,-1,-1)=-bthet(2,-i,1,1)
 +      athet(1,i,-1,1)=athet(1,-i,1,1)
 +      athet(2,i,-1,1)=-athet(2,-i,1,1)
 +      bthet(1,i,-1,1)=-bthet(1,-i,1,1)
 +      bthet(2,i,-1,1)=bthet(2,-i,1,1)
 +      athet(1,i,1,-1)=-athet(1,-i,1,1)
 +      athet(2,i,1,-1)=athet(2,-i,1,1)
 +      bthet(1,i,1,-1)=bthet(1,-i,1,1)
 +      bthet(2,i,1,-1)=-bthet(2,-i,1,1)
 +      theta0(i)=theta0(-i)
 +      sig0(i)=sig0(-i)
 +      sigc0(i)=sigc0(-i)
 +       do j=0,3
 +        polthet(j,i)=polthet(j,-i)
 +       enddo
 +       do j=1,3
 +         gthet(j,i)=gthet(j,-i)
 +       enddo
 +      enddo
        close (ithep)
        if (lprint) then
  c       write (iout,'(a)') 
@@@ -270,8 -257,7 +292,8 @@@ c       endd
       & '   b1*10^1    ','    b2*10^1   '        
          do i=1,ntyp
            write(iout,'(a3,1h&,2x,5(f8.3,1h&))') restyp(i),
 -     &        a0thet(i),(100*athet(j,i),j=1,2),(10*bthet(j,i),j=1,2)
 +     &        a0thet(i),(100*athet(j,i,1,1),j=1,2),
 +     &        (10*bthet(j,i,1,1),j=1,2)
          enddo
        write (iout,'(/a/9x,5a/79(1h-))') 
       & 'Parameters of the expression for sigma(theta_c):',
       &  ntheterm3,nsingle,ndouble
        nntheterm=max0(ntheterm,ntheterm2,ntheterm3)
        read (ithep,*) (ithetyp(i),i=1,ntyp1)
 -      do i=1,maxthetyp
 -        do j=1,maxthetyp
 -          do k=1,maxthetyp
 -            aa0thet(i,j,k)=0.0d0
 +      do i=-ntyp1,-1
 +        ithetyp(i)=-ithetyp(-i)
 +      enddo
 +c      write (iout,*) "tu dochodze"
 +      do iblock=1,2
 +      do i=-maxthetyp,maxthetyp
 +        do j=-maxthetyp,maxthetyp
 +          do k=-maxthetyp,maxthetyp
 +            aa0thet(i,j,k,iblock)=0.0d0
              do l=1,ntheterm
 -              aathet(l,i,j,k)=0.0d0
 +              aathet(l,i,j,k,iblock)=0.0d0
              enddo
              do l=1,ntheterm2
                do m=1,nsingle
 -                bbthet(m,l,i,j,k)=0.0d0
 -                ccthet(m,l,i,j,k)=0.0d0
 -                ddthet(m,l,i,j,k)=0.0d0
 -                eethet(m,l,i,j,k)=0.0d0
 +                bbthet(m,l,i,j,k,iblock)=0.0d0
 +                ccthet(m,l,i,j,k,iblock)=0.0d0
 +                ddthet(m,l,i,j,k,iblock)=0.0d0
 +                eethet(m,l,i,j,k,iblock)=0.0d0
                enddo
              enddo
              do l=1,ntheterm3
                do m=1,ndouble
                  do mm=1,ndouble
 -                 ffthet(mm,m,l,i,j,k)=0.0d0
 -                 ggthet(mm,m,l,i,j,k)=0.0d0
 +                 ffthet(mm,m,l,i,j,k,iblock)=0.0d0
 +                 ggthet(mm,m,l,i,j,k,iblock)=0.0d0
                  enddo
                enddo
              enddo
            enddo
          enddo
 +      enddo 
        enddo
 -      do i=1,nthetyp
 -        do j=1,nthetyp
 -          do k=1,nthetyp
 -            read (ithep,'(3a)') res1,res2,res3
 -            read (ithep,*) aa0thet(i,j,k)
 -            read (ithep,*)(aathet(l,i,j,k),l=1,ntheterm)
 +
 +      do iblock=1,2
 +      do i=0,nthetyp
 +        do j=-nthetyp,nthetyp
 +          do k=-nthetyp,nthetyp
 +            read (ithep,'(6a)') res1
 +            read (ithep,*) aa0thet(i,j,k,iblock)
 +            read (ithep,*)(aathet(l,i,j,k,iblock),l=1,ntheterm)
              read (ithep,*)
 -     &       ((bbthet(lll,ll,i,j,k),lll=1,nsingle),
 -     &        (ccthet(lll,ll,i,j,k),lll=1,nsingle),
 -     &        (ddthet(lll,ll,i,j,k),lll=1,nsingle),
 -     &        (eethet(lll,ll,i,j,k),lll=1,nsingle),ll=1,ntheterm2)
 +     &       ((bbthet(lll,ll,i,j,k,iblock),lll=1,nsingle),
 +     &        (ccthet(lll,ll,i,j,k,iblock),lll=1,nsingle),
 +     &        (ddthet(lll,ll,i,j,k,iblock),lll=1,nsingle),
 +     &        (eethet(lll,ll,i,j,k,iblock),lll=1,nsingle)
 +     &        ,ll=1,ntheterm2)
              read (ithep,*)
 -     &      (((ffthet(llll,lll,ll,i,j,k),ffthet(lll,llll,ll,i,j,k),
 -     &         ggthet(llll,lll,ll,i,j,k),ggthet(lll,llll,ll,i,j,k),
 +     &      (((ffthet(llll,lll,ll,i,j,k,iblock),
 +     &      ffthet(lll,llll,ll,i,j,k,iblock),
 +     &         ggthet(llll,lll,ll,i,j,k,iblock)
 +     &        ,ggthet(lll,llll,ll,i,j,k,iblock),
       &         llll=1,lll-1),lll=2,ndouble),ll=1,ntheterm3)
            enddo
          enddo
        do i=1,nthetyp
          do j=1,nthetyp
            do l=1,ntheterm
 -            aathet(l,i,j,nthetyp+1)=aathet(l,i,j,1)
 -            aathet(l,nthetyp+1,i,j)=aathet(l,1,i,j)
 +            aathet(l,i,j,nthetyp+1,iblock)=0.0d0
 +            aathet(l,nthetyp+1,i,j,iblock)=0.0d0
            enddo
 -          aa0thet(i,j,nthetyp+1)=aa0thet(i,j,1)
 -          aa0thet(nthetyp+1,i,j)=aa0thet(1,i,j)
 +          aa0thet(i,j,nthetyp+1,iblock)=0.0d0
 +          aa0thet(nthetyp+1,i,j,iblock)=0.0d0
          enddo
          do l=1,ntheterm
 -          aathet(l,nthetyp+1,i,nthetyp+1)=aathet(l,1,i,1)
 +          aathet(l,nthetyp+1,i,nthetyp+1,iblock)=0.0d0
          enddo
 -        aa0thet(nthetyp+1,i,nthetyp+1)=aa0thet(1,i,1)
 +        aa0thet(nthetyp+1,i,nthetyp+1,iblock)=0.0d0
        enddo
 +      enddo
 +C Substitution for D aminoacids from symmetry.
 +      do iblock=1,2
 +      do i=-nthetyp,0
 +        do j=-nthetyp,nthetyp
 +          do k=-nthetyp,nthetyp
 +           aa0thet(i,j,k,iblock)=aa0thet(-i,-j,-k,iblock)
 +           do l=1,ntheterm
 +           aathet(l,i,j,k,iblock)=aathet(l,-i,-j,-k,iblock)
 +           enddo
 +           do ll=1,ntheterm2
 +            do lll=1,nsingle
 +            bbthet(lll,ll,i,j,k,iblock)=bbthet(lll,ll,-i,-j,-k,iblock)
 +            ccthet(lll,ll,i,j,k,iblock)=-ccthet(lll,ll,-i,-j,-k,iblock)
 +            ddthet(lll,ll,i,j,k,iblock)=ddthet(lll,ll,-i,-j,-k,iblock)
 +            eethet(lll,ll,i,j,k,iblock)=-eethet(lll,ll,-i,-j,-k,iblock)
 +            enddo
 +          enddo
 +          do ll=1,ntheterm3
 +           do lll=2,ndouble
 +            do llll=1,lll-1
 +            ffthet(llll,lll,ll,i,j,k,iblock)=
 +     &      ffthet(llll,lll,ll,-i,-j,-k,iblock)
 +            ffthet(lll,llll,ll,i,j,k,iblock)=
 +     &      ffthet(lll,llll,ll,-i,-j,-k,iblock)
 +            ggthet(llll,lll,ll,i,j,k,iblock)=
 +     &      -ggthet(llll,lll,ll,-i,-j,-k,iblock)
 +            ggthet(lll,llll,ll,i,j,k,iblock)=
 +     &      -ggthet(lll,llll,ll,-i,-j,-k,iblock)
 +            enddo !ll
 +           enddo  !lll  
 +          enddo   !llll
 +         enddo    !k
 +        enddo     !j
 +       enddo      !i
 +      enddo       !iblock
  C
  C Control printout of the coefficients of virtual-bond-angle potentials
  C
                write (iout,'(//4a)')
       &         'Type ',onelett(i),onelett(j),onelett(k)
                write (iout,'(//a,10x,a)') " l","a[l]"
 -              write (iout,'(i2,1pe15.5)') 0,aa0thet(i,j,k)
 +              write (iout,'(i2,1pe15.5)') 0,aa0thet(i,j,k,iblock)
                write (iout,'(i2,1pe15.5)')
 -     &           (l,aathet(l,i,j,k),l=1,ntheterm)
 +     &           (l,aathet(l,i,j,k,iblock),l=1,ntheterm)
              do l=1,ntheterm2
                write (iout,'(//2h m,4(9x,a,3h[m,i1,1h]))')
       &          "b",l,"c",l,"d",l,"e",l
                do m=1,nsingle
                  write (iout,'(i2,4(1pe15.5))') m,
 -     &          bbthet(m,l,i,j,k),ccthet(m,l,i,j,k),
 -     &          ddthet(m,l,i,j,k),eethet(m,l,i,j,k)
 +     &          bbthet(m,l,i,j,k,iblock),ccthet(m,l,i,j,k,iblock),
 +     &          ddthet(m,l,i,j,k,iblock),eethet(m,l,i,j,k,iblock)
                enddo
              enddo
              do l=1,ntheterm3
                do m=2,ndouble
                  do n=1,m-1
                    write (iout,'(i1,1x,i1,4(1pe15.5))') n,m,
 -     &              ffthet(n,m,l,i,j,k),ffthet(m,n,l,i,j,k),
 -     &              ggthet(n,m,l,i,j,k),ggthet(m,n,l,i,j,k)
 +     &              ffthet(n,m,l,i,j,k,iblock),
 +     &              ffthet(m,n,l,i,j,k,iblock),
 +     &              ggthet(n,m,l,i,j,k,iblock),
 +     &              ggthet(m,n,l,i,j,k,iblock)
                  enddo
                enddo
              enddo
          enddo  
        bsc(1,i)=0.0D0
          read(irotam,*)(censc(k,1,i),k=1,3),((blower(k,l,1),l=1,k),k=1,3)
 +         censc(1,1,-i)=censc(1,1,i)
 +         censc(2,1,-i)=censc(2,1,i)
 +         censc(3,1,-i)=-censc(3,1,i)
        do j=2,nlob(i)
          read (irotam,*) bsc(j,i)
          read (irotam,*) (censc(k,j,i),k=1,3),
       &                                 ((blower(k,l,j),l=1,k),k=1,3)
 +          censc(1,j,-i)=censc(1,j,i)
 +          censc(2,j,-i)=censc(2,j,i)
 +          censc(3,j,-i)=-censc(3,j,i)
          enddo
        do j=1,nlob(i)
          do k=1,3
                enddo
              gaussc(k,l,j,i)=akl
              gaussc(l,k,j,i)=akl
 +               if (((k.eq.3).and.(l.ne.3))
 +     &        .or.((l.eq.3).and.(k.ne.3))) then
 +                 gaussc(k,l,j,-i)=-akl
 +                 gaussc(l,k,j,-i)=-akl
 +               else
 +                 gaussc(k,l,j,-i)=akl
 +                 gaussc(l,k,j,-i)=akl
 +               endif
              enddo
            enddo 
        enddo
@@@ -589,35 -512,24 +611,35 @@@ C Read torsional parameter
  C
        read (itorp,*) ntortyp
        read (itorp,*) (itortyp(i),i=1,ntyp)
 -      write (iout,*) 'ntortyp',ntortyp
 -      do i=1,ntortyp
 -      do j=1,ntortyp
 -        read (itorp,*) nterm(i,j),nlor(i,j)
 +      do iblock=1,2
 +      do i=-ntyp,-1
 +       itortyp(i)=-itortyp(-i)
 +      enddo
 +c      write (iout,*) 'ntortyp',ntortyp
 +      do i=0,ntortyp-1
 +        do j=-ntortyp+1,ntortyp-1
 +          read (itorp,*) 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,*) 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,*) 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
            enddo
 -        do k=1,nlor(i,j)
 +        do k=1,nlor(i,j,iblock)
            read (itorp,*) kk,vlor1(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 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)
 +            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
  C
  C 6/23/01 Read parameters for double torsionals
  C
 -      do i=1,ntortyp
 -        do j=1,ntortyp
 -          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)') 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
                 stop "Error in double torsional parameter file"
              endif
 -            read (itordp,*) ntermd_1(i,j,k),ntermd_2(i,j,k)
 -            read (itordp,*) (v1c(1,l,i,j,k),l=1,ntermd_1(i,j,k))
 -            read (itordp,*) (v1s(1,l,i,j,k),l=1,ntermd_1(i,j,k))
 -            read (itordp,*) (v1c(2,l,i,j,k),l=1,ntermd_1(i,j,k))
 -            read (itordp,*) (v1s(2,l,i,j,k),l=1,ntermd_1(i,j,k))
 -            read (itordp,*) ((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,*) 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,*) (v1c(1,l,i,j,k,iblock),l=1,
 +     &         ntermd_1(i,j,k,iblock))
 +            read (itordp,*) (v1s(1,l,i,j,k,iblock),l=1,
 +     &         ntermd_1(i,j,k,iblock))
 +            read (itordp,*) (v1c(2,l,i,j,k,iblock),l=1,
 +     &         ntermd_1(i,j,k,iblock))
 +            read (itordp,*) (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,*) ((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,*) 'Constants for double torsionals'
 -      do i=1,ntortyp
 -        do j=1,ntortyp 
 -          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
              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)
 +            do l=1,ntermd_1(i,j,k,iblock)
                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)
 +     &           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)
              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,'(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),m=1,ntermd_2(i,j,k))
 +     &         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,'(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),m=1,ntermd_2(i,j,k))
 +     &         l,(v2s(l,m,i,j,k,iblock),m=1,ntermd_2(i,j,k,iblock))
              enddo
              write (iout,*)
            enddo
          enddo
        enddo
 +      enddo
        endif
  #endif
  C Read of Side-chain backbone correlation parameters
@@@ -732,10 -611,6 +754,10 @@@ CC
  C
        read (isccor,*) nsccortyp
        read (isccor,*) (isccortyp(i),i=1,ntyp)
 +      do i=-ntyp,-1
 +        isccortyp(i)=-isccortyp(-i)
 +       enddo
 +       iscprol=isccortyp(20)
  c      write (iout,*) 'ntortyp',ntortyp
        maxinter=3
  cc maxinter is maximum interaction sites
          do j=1,nsccortyp
            read (isccor,*) nterm_sccor(i,j),nlor_sccor(i,j)
            v0ijsccor=0.0d0
 +          v0ijsccor1=0.0d0
 +          v0ijsccor2=0.0d0
 +          v0ijsccor3=0.0d0
            si=-1.0d0
 -
 +          nterm_sccor(-i,j)=nterm_sccor(i,j)
 +          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,*) kk,v1sccor(k,l,i,j)
       &    ,v2sccor(k,l,i,j)
 +             if (j.eq.iscprol) then
 +             if (i.eq.isccortyp(10)) then
 +             v1sccor(k,l,i,-j)=v1sccor(k,l,i,j)
 +             v2sccor(k,l,i,-j)=-v2sccor(k,l,i,j)
 +             else
 +              v1sccor(k,l,i,-j)=v1sccor(k,l,i,j)*0.5d0
 +     &                        +v2sccor(k,l,i,j)*dsqrt(0.75d0)
 +             v2sccor(k,l,i,-j)=-v2sccor(k,l,i,j)*0.5d0
 +     &                        +v1sccor(k,l,i,j)*dsqrt(0.75d0)
 +             v1sccor(k,l,-i,-j)=v1sccor(k,l,i,j)
 +             v2sccor(k,l,-i,-j)=-v2sccor(k,l,i,j)
 +             v1sccor(k,l,-i,j)=v1sccor(k,l,i,-j)
 +             v2sccor(k,l,-i,j)=-v2sccor(k,l,i,-j)
 +             endif
 +            else
 +             if (i.eq.isccortyp(10)) then
 +             v1sccor(k,l,i,-j)=v1sccor(k,l,i,j)
 +             v2sccor(k,l,i,-j)=-v2sccor(k,l,i,j)
 +             else
 +               if (j.eq.isccortyp(10)) then
 +             v1sccor(k,l,-i,j)=v1sccor(k,l,i,j)
 +             v2sccor(k,l,-i,j)=-v2sccor(k,l,i,j)
 +               else
 +             v1sccor(k,l,i,-j)=-v1sccor(k,l,i,j)
 +             v2sccor(k,l,i,-j)=-v2sccor(k,l,i,j)
 +             v1sccor(k,l,-i,-j)=v1sccor(k,l,i,j)
 +             v2sccor(k,l,-i,-j)=-v2sccor(k,l,i,j)
 +             v1sccor(k,l,-i,j)=v1sccor(k,l,i,-j)
 +             v2sccor(k,l,-i,j)=-v2sccor(k,l,i,-j)
 +                endif
 +               endif
 +             endif
              v0ijsccor=v0ijsccor+si*v1sccor(k,l,i,j)
 +            v0ijsccor1=v0ijsccor+si*v1sccor(k,l,-i,j)
 +            v0ijsccor2=v0ijsccor+si*v1sccor(k,l,i,-j)
 +            v0ijsccor3=v0ijsccor+si*v1sccor(k,l,-i,-j)
              si=-si
            enddo
            do k=1,nlor_sccor(i,j)
              v0ijsccor=v0ijsccor+vlor1sccor(k,i,j)/
       &(1+vlor3sccor(k,i,j)**2)
            enddo
 -          v0sccor(i,j)=v0ijsccor
 +          v0sccor(l,i,j)=v0ijsccor
 +          v0sccor(l,-i,j)=v0ijsccor1
 +          v0sccor(l,i,-j)=v0ijsccor2
 +          v0sccor(l,-i,-j)=v0ijsccor3   
          enddo
        enddo
        enddo
@@@ -830,96 -662,39 +852,96 @@@ C 9/18/99 (AL) Read coefficients of th
  C         interaction energy of the Gly, Ala, and Pro prototypes.
  C
        read (ifourier,*) nloctyp
 -      do i=1,nloctyp
 +      do i=0,nloctyp-1
          read (ifourier,*)
 -        read (ifourier,*) (b(ii,i),ii=1,13)
 +        read (ifourier,*) (b(ii),ii=1,13)
          if (lprint) then
          write (iout,*) 'Type',i
 -        write (iout,'(a,i2,a,f10.5)') ('b(',ii,')=',b(ii,i),ii=1,13)
 +        write (iout,'(a,i2,a,f10.5)') ('b(',ii,')=',b(ii),ii=1,13)
          endif
 -        B1(1,i)  = b(3,i)
 -        B1(2,i)  = b(5,i)
 -        B1tilde(1,i) = b(3,i)
 -        B1tilde(2,i) =-b(5,i) 
 -        B2(1,i)  = b(2,i)
 -        B2(2,i)  = b(4,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)
 -        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)
 -        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)
 -        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)
 -        EE(1,1,i)= b(10,i)+b(11,i)
 -        EE(2,2,i)=-b(10,i)+b(11,i)
 -        EE(2,1,i)= b(12,i)-b(13,i)
 -        EE(1,2,i)= b(12,i)+b(13,i)
 +        B1(1,i)  = b(3)
 +        B1(2,i)  = b(5)
 +        B1(1,-i) = b(3)
 +        B1(2,-i) = -b(5)
 +c        b1(1,i)=0.0d0
 +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)=0.0d0
 +c        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)
 +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)
 +
 +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)
 +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)
 +
 +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)
 +
 +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
        if (lprint) then
        do i=1,nloctyp
@@@ -1117,8 -892,27 +1139,27 @@@ c           augm(i,j)=0.5D0**(2*expon)*
          enddo
        enddo
  C
- C Define the SC-p interaction constants
+ C Define the SC-p interaction constants and SS bond potentials
  C
+       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,*) "Parameters of the SS-bond potential:"
+        write (iout,*) "D0CM",d0cm," AKCM",akcm," AKTH",akth,
+      & " AKCT",akct
+        write (iout,*) "V1SS",v1ss," V2SS",v2ss," V3SS",v3ss
+        write (iout,*) "EBR",ebr," SS_DEPTH",ss_depth
+        write (iout,*)" HT",Ht
  #ifdef OLDSCP
        do i=1,20
  C "Soft" SC-p repulsion (causes helices to be too flat, but facilitates 
  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
@@@ -1178,21 -972,21 +1219,21 @@@ 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
  
-       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      if (lprint) 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
        end
@@@ -19,7 -19,7 +19,7 @@@
        include 'COMMON.SCROT'
        include 'COMMON.SCCOR'
        include 'COMMON.ALLPARM'
 -      integer i,j,k,l,m,mm,iparm
 +      integer i,j,k,l,m,mm,iparm,ichir1,ichir2,iblock,iii
  
  c Store weights
        ww_all(1,iparm)=wsc
@@@ -53,15 -53,11 +53,15 @@@ c Store bond parameter
        enddo
  c Store bond angle parameters
  #ifdef CRYST_THETA
 -      do i=1,ntyp
 +      do i=-ntyp,ntyp
          a0thet_all(i,iparm)=a0thet(i)
 +        do ichir1=-1,1
 +        do ichir2=-1,1
          do j=1,2
 -          athet_all(j,i,iparm)=athet(j,i)
 -          bthet_all(j,i,iparm)=bthet(j,i)
 +          athet_all(j,i,ichir1,ichir2,iparm)=athet(j,i,ichir1,ichir2)
 +          bthet_all(j,i,ichir1,ichir2,iparm)=bthet(j,i,ichir1,ichir2)
 +        enddo
 +        enddo
          enddo
          do j=0,3
            polthet_all(j,i,iparm)=polthet(j,i)
  #endif
  #ifdef CRYST_SC
  c Store the sidechain rotamer parameters
 -      do i=1,ntyp
 -        nlob_all(i,iparm)=nlob(i)
 -        do j=1,nlob(i)
 -          bsc_all(j,i,iparm)=bsc(j,i)
 +      do i=-ntyp,ntyp
 +       iii=iabs(i)
 +       if (i.eq.0) cycle
 +        nlob_all(iii,iparm)=nlob(iii)
 +        do j=1,nlob(iii)
 +          bsc_all(j,iii,iparm)=bsc(j,iii)
            do k=1,3
              censc_all(k,j,i,iparm)=censc(k,j,i)
            enddo
        enddo
  #endif
  c Store the torsional parameters
 -      do i=1,ntortyp
 -        do j=1,ntortyp
 -          v0_all(i,j,iparm)=v0(i,j)
 -          nterm_all(i,j,iparm)=nterm(i,j)
 -          nlor_all(i,j,iparm)=nlor(i,j)
 -          do k=1,nterm(i,j)
 -            v1_all(k,i,j,iparm)=v1(k,i,j)
 -            v2_all(k,i,j,iparm)=v2(i,i,j)
 -          enddo
 -          do k=1,nlor(i,j)
 -            vlor1_all(k,i,j,iparm)=vlor1(k,i,j)
 -            vlor2_all(k,i,j,iparm)=vlor2(k,i,j)
 -            vlor3_all(k,i,j,iparm)=vlor3(k,i,j)
 -          enddo
 -        enddo
 -      enddo  
 +      do iblock=1,2
 +      do i=-ntortyp+1,ntortyp-1
 +        do j=-ntortyp+1,ntortyp-1
 +          v0_all(i,j,iblock,iparm)=v0(i,j,iblock)
 +          nterm_all(i,j,iblock,iparm)=nterm(i,j,iblock)
 +          nlor_all(i,j,iblock,iparm)=nlor(i,j,iblock)
 +          do k=1,nterm(i,j,iblock)
 +            v1_all(k,i,j,iblock,iparm)=v1(k,i,j,iblock)
 +            v2_all(k,i,j,iblock,iparm)=v2(k,i,j,iblock)
 +           enddo
 +          do k=1,nlor(i,j,iblock)
 +             vlor1_all(k,i,j,iparm)=vlor1(k,i,j)
 +             vlor2_all(k,i,j,iparm)=vlor2(k,i,j)
 +             vlor3_all(k,i,j,iparm)=vlor3(k,i,j)
 +           enddo
 +         enddo
 +      enddo
 +       enddo  
  c Store the double torsional parameters
 -      do i=1,ntortyp
 -        do j=1,ntortyp
 -          do k=1,ntortyp
 -            ntermd1_all(i,j,k,iparm)=ntermd_1(i,j,k)
 -            ntermd2_all(i,j,k,iparm)=ntermd_2(i,j,k)
 -            do l=1,ntermd_1(i,j,k)
 -              v1c_all(1,l,i,j,k,iparm)=v1c(1,l,i,j,k)
 -              v1c_all(2,l,i,j,k,iparm)=v1c(2,l,i,j,k)
 -              v2c_all(1,l,i,j,k,iparm)=v2c(1,l,i,j,k)
 -              v2c_all(2,l,i,j,k,iparm)=v2c(2,l,i,j,k)
 -            enddo
 -            do l=1,ntermd_2(i,j,k)
 -              do m=1,ntermd_2(i,j,k)
 -                v2s_all(l,m,i,j,k,iparm)=v2s(l,m,i,j,k)
 -              enddo
 -            enddo
 -          enddo
 -        enddo
 +      do iblock=1,2
 +      do i=-ntortyp+1,ntortyp-1
 +        do j=-ntortyp+1,ntortyp-1
 +          do k=-ntortyp+1,ntortyp-1
 +            ntermd1_all(i,j,k,iblock,iparm)=ntermd_1(i,j,k,iblock)
 +            ntermd2_all(i,j,k,iblock,iparm)=ntermd_2(i,j,k,iblock)
 +            do l=1,ntermd_1(i,j,k,iblock)
 +              v1c_all(1,l,i,j,k,iblock,iparm)=v1c(1,l,i,j,k,iblock)
 +              v1c_all(2,l,i,j,k,iblock,iparm)=v1c(2,l,i,j,k,iblock)
 +              v2c_all(1,l,i,j,k,iblock,iparm)=v2c(1,l,i,j,k,iblock)
 +              v2c_all(2,l,i,j,k,iblock,iparm)=v2c(2,l,i,j,k,iblock)
 +             enddo
 +            do l=1,ntermd_2(i,j,k,iblock)
 +              do m=1,ntermd_2(i,j,k,iblock)
 +                v2s_all(l,m,i,j,k,iblock,iparm)=v2s(l,m,i,j,k,iblock)
 +               enddo
 +             enddo
 +           enddo
 +         enddo
 +       enddo
        enddo
  c Store parameters of the cumulants
 -      do i=1,nloctyp
 +      do i=-nloctyp,nloctyp
          do j=1,2
            b1_all(j,i,iparm)=b1(j,i)
            b1tilde_all(j,i,iparm)=b1tilde(j,i)
@@@ -227,6 -217,8 +227,8 @@@ c Store the SCp parameter
          enddo
        enddo
  c Store disulfide-bond parameters
+       ht_all(iparm)=ht
+       ss_depth_all(iparm)=ss_depth
        ebr_all(iparm)=ebr
        d0cm_all(iparm)=d0cm
        akcm_all(iparm)=akcm
        include 'COMMON.SCROT'
        include 'COMMON.SCCOR'
        include 'COMMON.ALLPARM'
 -      integer i,j,k,l,m,mm,iparm
 +      integer i,j,k,l,m,mm,iparm,ichir1,ichir2,iblock,iii
  
  c Restore weights
        wsc=ww_all(1,iparm)
@@@ -306,17 -298,12 +308,17 @@@ c Restore bond parameter
        enddo
  c Restore bond angle parameters
  #ifdef CRYST_THETA
 -      do i=1,ntyp
 +      do i=-ntyp,ntyp
          a0thet(i)=a0thet_all(i,iparm)
 +
 +        do ichir1=-1,1
 +        do ichir2=-1,1
          do j=1,2
 -          athet(j,i)=athet_all(j,i,iparm)
 -          bthet(j,i)=bthet_all(j,i,iparm)
 -        enddo
 +          athet(j,i,ichir1,ichir2)=athet_all(j,i,ichir1,ichir2,iparm)
 +          bthet(j,i,ichir1,ichir2)=bthet_all(j,i,ichir1,ichir2,iparm)
 +       enddo
 +       enddo
 +       enddo
          do j=0,3
            polthet(j,i)=polthet_all(j,i,iparm)
          enddo
  #endif
  c Restore the sidechain rotamer parameters
  #ifdef CRYST_SC
 -      do i=1,ntyp
 -        nlob(i)=nlob_all(i,iparm)
 -        do j=1,nlob(i)
 -          bsc(j,i)=bsc_all(j,i,iparm)
 +      do i=-ntyp,ntyp
 +        if (i.eq.0) cycle
 +        iii=iabs(i)
 +        nlob(iii)=nlob_all(iii,iparm)
 +        do j=1,nlob(iii)
 +          bsc(j,iii)=bsc_all(j,iii,iparm)
            do k=1,3
              censc(k,j,i)=censc_all(k,j,i,iparm)
            enddo
        enddo
  #endif
  c Restore the torsional parameters
 -      do i=1,ntortyp
 -        do j=1,ntortyp
 -          v0(i,j)=v0_all(i,j,iparm)
 -          nterm(i,j)=nterm_all(i,j,iparm)
 -          nlor(i,j)=nlor_all(i,j,iparm)
 -          do k=1,nterm(i,j)
 -            v1(k,i,j)=v1_all(k,i,j,iparm)
 -            v2(i,i,j)=v2_all(k,i,j,iparm)
 -          enddo
 -          do k=1,nlor(i,j)
 -            vlor1(k,i,j)=vlor1_all(k,i,j,iparm)
 -            vlor2(k,i,j)=vlor2_all(k,i,j,iparm)
 -            vlor3(k,i,j)=vlor3_all(k,i,j,iparm)
 -          enddo
 -        enddo
 -      enddo  
 +      do iblock=1,2
 +      do i=-ntortyp+1,ntortyp-1
 +        do j=-ntortyp+1,ntortyp-1
 +          v0(i,j,iblock)=v0_all(i,j,iblock,iparm)
 +          nterm(i,j,iblock)=nterm_all(i,j,iblock,iparm)
 +          nlor(i,j,iblock)=nlor_all(i,j,iblock,iparm)
 +          do k=1,nterm(i,j,iblock)
 +            v1(k,i,j,iblock)=v1_all(k,i,j,iblock,iparm)
 +            v2(k,i,j,iblock)=v2_all(k,i,j,iblock,iparm)
 +           enddo
 +          do k=1,nlor(i,j,iblock)
 +             vlor1(k,i,j)=vlor1_all(k,i,j,iparm)
 +             vlor2(k,i,j)=vlor2_all(k,i,j,iparm)
 +             vlor3(k,i,j)=vlor3_all(k,i,j,iparm)
 +           enddo
 +         enddo
 +       enddo  
 +      enddo
  c Restore the double torsional parameters
 -      do i=1,ntortyp
 -        do j=1,ntortyp
 -          do k=1,ntortyp
 -            ntermd_1(i,j,k)=ntermd1_all(i,j,k,iparm)
 -            ntermd_2(i,j,k)=ntermd2_all(i,j,k,iparm)
 -            do l=1,ntermd_1(i,j,k)
 -              v1c(1,l,i,j,k)=v1c_all(1,l,i,j,k,iparm)
 -              v1c(2,l,i,j,k)=v1c_all(2,l,i,j,k,iparm)
 -              v2c(1,l,i,j,k)=v2c_all(1,l,i,j,k,iparm)
 -              v2c(2,l,i,j,k)=v2c_all(2,l,i,j,k,iparm)
 -            enddo
 -            do l=1,ntermd_2(i,j,k)
 -              do m=1,ntermd_2(i,j,k)
 -                v2s(l,m,i,j,k)=v2s_all(l,m,i,j,k,iparm)
 -              enddo
 -            enddo
 -          enddo
 -        enddo
 +      do iblock=1,2
 +      do i=-ntortyp+1,ntortyp-1
 +        do j=-ntortyp+1,ntortyp-1
 +          do k=-ntortyp+1,ntortyp-1
 +            ntermd_1(i,j,k,iblock)=ntermd1_all(i,j,k,iblock,iparm)
 +            ntermd_2(i,j,k,iblock)=ntermd2_all(i,j,k,iblock,iparm)
 +            do l=1,ntermd_1(i,j,k,iblock)
 +              v1c(1,l,i,j,k,iblock)=v1c_all(1,l,i,j,k,iblock,iparm)
 +              v1c(2,l,i,j,k,iblock)=v1c_all(2,l,i,j,k,iblock,iparm)
 +              v2c(1,l,i,j,k,iblock)=v2c_all(1,l,i,j,k,iblock,iparm)
 +              v2c(2,l,i,j,k,iblock)=v2c_all(2,l,i,j,k,iblock,iparm)
 +             enddo
 +            do l=1,ntermd_2(i,j,k,iblock)
 +              do m=1,ntermd_2(i,j,k,iblock)
 +                v2s(l,m,i,j,k,iblock)=v2s_all(l,m,i,j,k,iblock,iparm)
 +               enddo
 +             enddo
 +           enddo
 +         enddo
 +       enddo
        enddo
  c Restore parameters of the cumulants
 -      do i=1,nloctyp
 +      do i=-nloctyp,nloctyp
          do j=1,2
            b1(j,i)=b1_all(j,i,iparm)
            b1tilde(j,i)=b1tilde_all(j,i,iparm)
@@@ -481,6 -462,8 +483,8 @@@ c Restore the SCp parameter
          enddo
        enddo
  c Restore disulfide-bond parameters
+       ht=ht_all(iparm)
+       ss_depth=ss_depth_all(iparm) 
        ebr=ebr_all(iparm)
        d0cm=d0cm_all(iparm)
        akcm=akcm_all(iparm)