add.f
arcos.f
aveint.F
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/aveint.F: No such file or directory
aveint_new.F
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/aveint_new.F: No such file or directory
aveint_period.F
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/aveint_period.F: No such file or directory
banach.f
bank.F
976c976
<         call chainbuild
---
>         call chainbuild_extconf
bkedis.F
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/bkedis.F: No such file or directory
bkread_dist_constr.F
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/bkread_dist_constr.F: No such file or directory
blas.f
block_partition.F
bond_move.f
box.F
10a11
>       mhalfboxsize = -halfboxsize
31c32
<             output=output-boxsize
---
>         output=output-boxsize
34c35
<             output=output+boxsize
---
>         output=output+boxsize
44c45
<         output=vec
---
>       output=vec
53a55,123
> 
>       subroutine wrap_coord(v,o,crd)
>       implicit none
>       include 'COMMON.NEWBOX'
>       double precision v,o
>       integer crd
>       if(v.gt.halfboxsize(crd)) then
>         o=v-boxsize(crd)
>       else if(v.lt.-halfboxsize(crd)) then
>         o=v+boxsize(crd)
>       else
>         o=v
>       endif
>       end
> 
> 
>       double precision function wrap_coordx(v)
>       implicit none
>       include 'COMMON.NEWBOX'
>       double precision v
>       !if(v.gt.halfboxsize(1)) then
>       !  wrap_coordx = v-boxsize(1)
>       !else if(v.lt.mhalfboxsize(1)) then
>       !  wrap_coordx = v+boxsize(1)
>       !else
>       !  wrap_coordx = v
>       !endif
>       if(abs(v).gt.halfboxsize(1)) then
>         wrap_coordx=v-sign(boxsize(1),v)
>       else
>         wrap_coordx=v
>       endif
>       end
> 
>       double precision function wrap_coordy(v)
>       implicit none
>       include 'COMMON.NEWBOX'
>       double precision v
> !     if(v.gt.halfboxsize(2)) then
> !       wrap_coordy = v-boxsize(2)
> !     else if(v.lt.mhalfboxsize(2)) then
> !       wrap_coordy = v+boxsize(2)
> !     else
> !       wrap_coordy = v
> !     endif
>       if(abs(v).gt.halfboxsize(2)) then
>         wrap_coordy=v-sign(boxsize(2),v)
>       else
>         wrap_coordy=v
>       endif
>       end
> 
>       double precision function wrap_coordz(v)
>       implicit none
>       include 'COMMON.NEWBOX'
>       double precision v
> !     if(v.gt.halfboxsize(3)) then
> !       wrap_coordz = v-boxsize(3)
> !     else if(v.lt.mhalfboxsize(3)) then
> !       wrap_coordz = v+boxsize(3)
> !     else
> !       wrap_coordz = v
> !     endif
>       if(abs(v).gt.halfboxsize(3)) then
>         wrap_coordz=v-sign(boxsize(3),v)
>       else
>         wrap_coordz=v
>       endif
>       end
calc_bondvec.F
16a17
> !     include 'COMMON.OMP'
22a24,26
> #ifdef FNLOG
>       write(*,*)'calc_bondvec cpu'
> #endif
34c38,43
<         vbld_inv(i)=1.0d0/vbld(i)
---
>         !vbld_inv(i)=1.0d0/vbld(i)
>         if (vbld(i).ne.0.0d0) then
>           vbld_inv(i)=1.0d0/vbld(i)
>         else
>           vbld_inv(i)=0.0d0
>         endif
40a50,54
> #ifdef VBLDLOG
>         write(*,'(a,i4,2f18.10)')'vi',i,vbld(i),vbld_inv(i)
>         write(*,'(a,i4,2f18.10)')'vi',i+nres,
>      &    vbld(i+nres),vbld_inv(i+nres)
> #endif
48a63,68
> #ifdef VBLDLOG
>       write(*,'(a,i4,3f18.10)')'dcn',1,dc_norm(:,1)
> #endif
> !     soa_dc_norm_x(1)=dc_norm(1,1)
> !     soa_dc_norm_y(2)=dc_norm(2,1)
> !     soa_dc_norm_z(3)=dc_norm(3,1)
57a78,88
> #ifdef VBLDLOG
>         write(*,'(a,i4,3f18.10)')'dcn',i,dc_norm(:,i)
>         write(*,'(a,i4,3f18.10)')'dcn',i+nres,dc_norm(:,i+nres)
> #endif
> 
> !       soa_dc_norm_x(i)=dc_norm(1,i)
> !       soa_dc_norm_y(i)=dc_norm(2,i)
> !       soa_dc_norm_z(i)=dc_norm(3,i)
> !       soa_dc_norm_x(i+nres)=dc_norm(1,i+nres)
> !       soa_dc_norm_y(i+nres)=dc_norm(2,i+nres)
> !       soa_dc_norm_z(i+nres)=dc_norm(3,i+nres)
cart2intgrad.F
cart2intgrad-new.F
cartder.F
27c27
< * andgles alph and omega:
---
> * angles alph and omega:
cartprint.f
chainbuild.F
chainbuild-new.F
22d21
<       
28,29c27
< 
< C     
---
> C
35a34,38
> #ifdef MPI
>       include 'mpif.h'
>       double precision time00,time01
>       integer ierr,ierror
> #endif
43a47,48
>       include 'COMMON.SETUP'
>       include 'COMMON.TIME1'
52c57,82
< 
---
> #ifdef MPI
>       if (nfgtasks.gt.1) then
> c        write (iout,*) "BCAST in chainbuild_extconf"
> c        call flush(iout)
> c Broadcast the order to build the chain and compute internal
> coordinates
> c to the slaves. The slaves receive the order in ERGASTULUM.
>         time00=MPI_Wtime()
>         if (fg_rank.eq.0)
>      &    call MPI_Bcast(14,1,MPI_INTEGER,king,FG_COMM,IERROR)
>         time_bcast7=time_bcast7+MPI_Wtime()-time00
>         time01=MPI_Wtime()
>         call MPI_Bcast(vbld(1),2*nres,MPI_DOUBLE_PRECISION,
>      &    king,FG_COMM,IERROR)
>         call MPI_Bcast(theta(1),nres,MPI_DOUBLE_PRECISION,
>      &    king,FG_COMM,IERROR)
>         call MPI_Bcast(phi(1),nres,MPI_DOUBLE_PRECISION,
>      &    king,FG_COMM,IERROR)
>         call MPI_Bcast(alph(1),nres,MPI_DOUBLE_PRECISION,
>      &    king,FG_COMM,IERROR)
>         call MPI_Bcast(omeg(1),nres,MPI_DOUBLE_PRECISION,
>      &    king,FG_COMM,IERROR)
>         time_bcast=time_bcast+MPI_Wtime()-time00
>         time_bcastc=time_bcastc+MPI_Wtime()-time01
>       endif
> #endif
506,514c536,541
<            if (itype(i).eq.ntyp1 .and. itype(i-1).eq.ntyp1) then
<              difference=abs(c(j,i-1)-c(j,i))
< C             print *,'diff', difference
<              if (difference.gt.boxxsize/2.0) then
<                 if (c(j,i-1).gt.c(j,i)) then
<                   nojumpval=1
<                  else
<                    nojumpval=-1
<                  endif
---
>           if (itype(i).eq.ntyp1 .and. itype(i-1).eq.ntyp1) then
>             difference=abs(c(j,i-1)-c(j,i))
> C           print *,'diff', difference
>             if (difference.gt.boxxsize/2.0) then
>               if (c(j,i-1).gt.c(j,i)) then
>                 nojumpval=1
516,517c543
<               nojumpval=0
<               endif
---
>                 nojumpval=-1
519,522c545,552
<               c(j,i)=c(j,i)+nojumpval*boxxsize
<               c(j,i+nres)=c(j,i+nres)+nojumpval*boxxsize
<          enddo
<        nojumpval=0
---
>             else
>               nojumpval=0
>             endif
>           endif
>           c(j,i)=c(j,i)+nojumpval*boxxsize
>           c(j,i+nres)=c(j,i+nres)+nojumpval*boxxsize
>         enddo
>         nojumpval=0
524,531c554,558
<            if (itype(i).eq.ntyp1 .and. itype(i-1).eq.ntyp1) then
<              difference=abs(c(j,i-1)-c(j,i))
<              if (difference.gt.boxxsize/2.0) then
<                 if (c(j,i-1).gt.c(j,i)) then
<                   nojumpval=1
<                  else
<                    nojumpval=-1
<                  endif
---
>           if (itype(i).eq.ntyp1 .and. itype(i-1).eq.ntyp1) then
>             difference=abs(c(j,i-1)-c(j,i))
>             if (difference.gt.boxxsize/2.0) then
>               if (c(j,i-1).gt.c(j,i)) then
>                 nojumpval=1
533c560
<               nojumpval=0
---
>                 nojumpval=-1
535,538c562,568
<              endif
<               c(j,i)=c(j,i)+nojumpval*boxxsize
<               c(j,i+nres)=c(j,i+nres)+nojumpval*boxxsize
<          enddo
---
>             else
>               nojumpval=0
>             endif
>           endif
>           c(j,i)=c(j,i)+nojumpval*boxxsize
>           c(j,i+nres)=c(j,i+nres)+nojumpval*boxxsize
>         enddo
chain_symmetry.F
check_bond.f
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/check_bond.f: No such file or directory
checkder_p.F
92a93
> #ifdef VIRTUAL
97,101d97
< #ifdef MPI
<       include 'mpif.h'
<       integer ierror
<       include 'COMMON.SETUP'
< #endif
111d106
<       include 'COMMON.TORCNSTR'
124c119
<       integer i,j,k,nf,ic,ip
---
>       integer i,j,k,nf
130,141d124
< c     local variable to check gradient of time-averaged calculation of internals
<        
< #ifdef CHECK_GRAD_AVE      
<       include 'COMMON.NMR'
<       include 'COMMON.AVNLOC'
<       include 'COMMON.RESTARTED'
<       integer navint_temp
<       navint_temp=1
< !      double precision temp
< #endif
< c      write(iout,*),"start check grad" 
< c      call flush(iout)
150d132
< c      call flush(iout)
161c143
<       call geom_to_var(nvar,x)
---
> c      call geom_to_var(nvar,x)
163,190c145,146
< #ifdef CHECK_GRAD_AVE
< c        write(iout,*),"before call cp1",nsteps
< c        write (iout,*) "link_start_peak",link_start_peak,
< c     &   " link_end_peak",link_end_peak
<         if (nfgtasks.gt.1) 
<      &    call MPI_Bcast(15,1,MPI_INTEGER,king,FG_COMM,IERROR)
<         call save_nmrave_info
< c        do ic=1,npeak
< c          do ip=ipeak(1,ic),ipeak(2,ic)
< c          do j=1,2
< c            dnmr_xave_safe(j,ip)=dnmr_xave(j,ip)
< c          enddo
< c          enddo
< c        enddo
< c        theta_xave_safe(:,ithetaconstr_start:ithetaconstr_end)=
< c     &    theta_xave(:,ithetaconstr_start:ithetaconstr_end)
< c        cosphi_xave_safe(:,idihconstr_start:idihconstr_end)=
< c     &    cosphi_xave(:,idihconstr_start:idihconstr_end)
< c        sinphi_xave_safe(:,idihconstr_start:idihconstr_end)=
< c     &    sinphi_xave(:,idihconstr_start:idihconstr_end)
< c        nsteps_save = nsteps
< c        icalls_save = icalls
< c        idiv_tau_save=idiv_tau
< c        tau_temp_save=tau_temp
< c        write (iout,*) "icalls before",icalls
<         icalls(:)=icalls(:)+navint_temp
< c        write (iout,*) "icalls after",icalls
< #endif
---
> c        write (iout,*) "Before calling energy"
> c        call flush(iout)
191a148,149
> c        write (iout,*) "After calling energy"
> c        call flush(iout)
193,217d150
< #ifdef CHECK_GRAD_AVE
< c        write (iout,*) "link_start_peak",link_start_peak,
< c     &   " link_end_peak",link_end_peak
<         if (nfgtasks.gt.1) 
<      &    call MPI_Bcast(16,1,MPI_INTEGER,king,FG_COMM,IERROR)
<         call restore_nmrave_info
< c        do ic=link_start_peak,link_end_peak
< c          do ip=ipeak(1,ic),ipeak(2,ic)
< c          dnmr_xave(:,ip)=dnmr_xave_safe(:,ip)
< c          enddo
< c        enddo
< c        theta_xave(:,ithetaconstr_start:ithetaconstr_end)=
< c     &    theta_xave_safe(:,ithetaconstr_start:ithetaconstr_end)
< c        cosphi_xave(:,idihconstr_start:idihconstr_end)=
< c     &    cosphi_xave_safe(:,idihconstr_start:idihconstr_end)
< c        sinphi_xave(:,idihconstr_start:idihconstr_end)=
< c     &    sinphi_xave_safe(:,idihconstr_start:idihconstr_end)
< c        nsteps = nsteps_save
< c        icalls(:) = icalls_save(:)+navint_temp
< c        idiv_tau=idiv_tau_save
< c        tau_temp=tau_temp_save
< c        write (iout,*) "icalls reset",icalls
< c        write(iout,*),"after call cp1",nsteps
< c        write(iout,*),"after call cp1",nsteps
< #endif
246a180
>         call make_soa_dc_norm
250c184
< c        call flush(iout)
---
>         call flush(iout)
274c208
< c        call flush(iout)
---
>         call flush(iout)
326,332d259
< !#ifdef CHECK_GRAD_AVE
< !        do ic=link_start_peak,link_end_peak
< !          dnmr_xave_safe(:,ic)=dnmr_xave(:,ic)
< !        enddo
< !        nsteps_save = nsteps
< c        write(iout,*),"before call cp2",nsteps
< !#endif
335,356d261
< #ifdef CHECK_GRAD_AVE
<             if (nfgtasks.gt.1) 
<      &        call MPI_Bcast(16,1,MPI_INTEGER,king,FG_COMM,IERROR)
<             call restore_nmrave_info
< c            do ic=link_start_peak,link_end_peak
< c              do ip=ipeak(1,ic),ipeak(2,ic)
< c              dnmr_xave(:,ip)=dnmr_xave_safe(:,ip)
< c              enddo
< c            enddo
< c            theta_xave(:,ithetaconstr_start:ithetaconstr_end)=
< c     &       theta_xave_safe(:,ithetaconstr_start:ithetaconstr_end)
< c            cosphi_xave(:,idihconstr_start:idihconstr_end)=
< c     &       cosphi_xave_safe(:,idihconstr_start:idihconstr_end)
< c            sinphi_xave(:,idihconstr_start:idihconstr_end)=
< c     &       sinphi_xave_safe(:,idihconstr_start:idihconstr_end)
< c            nsteps = nsteps_save
< c            icalls(:) = icalls_save(:)+navint_temp
< c            idiv_tau=idiv_tau_save
< c            tau_temp=tau_temp_save
< c            write (iout,*) "icalls reset",icalls
< c           write(iout,*),"after call cp2",nsteps
< #endif
372,378d276
< !#ifdef CHECK_GRAD_AVE
< !         do ic=link_start_peak,link_end_peak
< !          dnmr_xave_safe(:,ic)=dnmr_xave(:,ic)
< !         enddo
< !         nsteps_save = nsteps
< c        write(iout,*),"before call cp3",nsteps
< !#endif
382,406d279
< #ifdef  CHECK_GRAD_AVE
< #ifndef SCALE_ERESTR
<             ggg(j)=ggg(j)*scal_force(1)
< #endif
<             if (nfgtasks.gt.1) 
<      &        call MPI_Bcast(16,1,MPI_INTEGER,king,FG_COMM,IERROR)
<             call restore_nmrave_info
< c            do ic=link_start_peak,link_end_peak
< c              do ip=ipeak(1,ic),ipeak(2,ic)
< c              dnmr_xave(:,ip)=dnmr_xave_safe(:,ip)
< c              enddo
< c            enddo
< c            theta_xave(:,ithetaconstr_start:ithetaconstr_end)=
< c     &       theta_xave_safe(:,ithetaconstr_start:ithetaconstr_end)
< c            cosphi_xave(:,idihconstr_start:idihconstr_end)=
< c     &       cosphi_xave_safe(:,idihconstr_start:idihconstr_end)
< c            sinphi_xave(:,idihconstr_start:idihconstr_end)=
< c     &       sinphi_xave_safe(:,idihconstr_start:idihconstr_end)
< c            nsteps = nsteps_save
< c            icalls(:) = icalls_save(:)+navint_temp
< c            idiv_tau=idiv_tau_save
< c            tau_temp=tau_temp_save
< c            write (iout,*) "icalls reset",icalls
< c           write(iout,*),"after call cp3",nsteps
< #endif
411c284
< 	    ggg(j)=(etot11-etot21)/(2*aincr)
---
>             ggg(j)=(etot11-etot21)/(2*aincr)
434,440d306
< !#ifdef  CHECK_GRAD_AVE
< !         do ic=link_start_peak,link_end_peak
< !          dnmr_xave_safe(:,ic)=dnmr_xave(:,ic)
< !         enddo
< !         nsteps_save = nsteps
< !c        write(iout,*),"before call cp4",nsteps
< !#endif
443,464d308
< #ifdef  CHECK_GRAD_AVE
<             if (nfgtasks.gt.1) 
<      &        call MPI_Bcast(16,1,MPI_INTEGER,king,FG_COMM,IERROR)
<             call restore_nmrave_info
< c            do ic=link_start_peak,link_end_peak
< c              do ip=ipeak(1,ic),ipeak(2,ic)
< c              dnmr_xave(:,ip)=dnmr_xave_safe(:,ip)
< c              enddo
< c            enddo
< c            theta_xave(:,ithetaconstr_start:ithetaconstr_end)=
< c     &        theta_xave_safe(:,ithetaconstr_start:ithetaconstr_end)
< c            cosphi_xave(:,idihconstr_start:idihconstr_end)=
< c     &        cosphi_xave_safe(:,idihconstr_start:idihconstr_end)
< c            sinphi_xave(:,idihconstr_start:idihconstr_end)=
< c     &        sinphi_xave_safe(:,idihconstr_start:idihconstr_end)
< c            nsteps = nsteps_save
< c            icalls(:) = icalls_save(:)+navint_temp
< c            idiv_tau=idiv_tau_save
< c            tau_temp=tau_temp_save
< c            write (iout,*) "icalls reset",icalls
< c           write(iout,*),"after call cp4",nsteps
< #endif
485,491d328
< !#ifdef  CHECK_GRAD_AVE
< !         do ic=link_start_peak,link_end_peak
< !          dnmr_xave_safe(:,ic)=dnmr_xave(:,ic)
< !         enddo
< !         nsteps_save = nsteps
< !c        write(iout,*),"before call cp5",nsteps
< !#endif
495,519d331
< #ifdef  CHECK_GRAD_AVE
< #ifndef SCALE_ERESTR
<             ggg(j+3)=ggg(j+3)*scal_force(1)
< #endif
<             if (nfgtasks.gt.1) 
<      &        call MPI_Bcast(16,1,MPI_INTEGER,king,FG_COMM,IERROR)
<             call restore_nmrave_info
< c            do ic=link_start_peak,link_end_peak
< c              do ip=ipeak(1,ic),ipeak(2,ic)
< c              dnmr_xave(:,ip)=dnmr_xave_safe(:,ip)
< c              enddo
< c            enddo
< c            theta_xave(:,ithetaconstr_start:ithetaconstr_end)=
< c     &       theta_xave_safe(:,ithetaconstr_start:ithetaconstr_end)
< c            cosphi_xave(:,idihconstr_start:idihconstr_end)=
< c     &       cosphi_xave_safe(:,idihconstr_start:idihconstr_end)
< c            sinphi_xave(:,idihconstr_start:idihconstr_end)=
< c     &       sinphi_xave_safe(:,idihconstr_start:idihconstr_end)
< c            nsteps = nsteps_save
< c            icalls(:) = icalls_save(:)+navint_temp
< c            idiv_tau=idiv_tau_save
< c            tau_temp=tau_temp_save
< c            write (iout,*) "icalls reset",icalls
< c           write(iout,*),"after call cp5",nsteps
< #endif
524c336
< 	    ggg(j+3)=(etot11-etot21)/(2*aincr)
---
>             ggg(j+3)=(etot11-etot21)/(2*aincr)
547a360,678
> #else
>       subroutine check_ecartint
> C Check the gradient of the energy in Cartesian coordinates.
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CONTROL'
>       include 'COMMON.CHAIN'
>       include 'COMMON.DERIV'
>       include 'COMMON.IOUNITS'
>       include 'COMMON.VAR'
>       include 'COMMON.CONTACTS'
>       include 'COMMON.MD'
>       include 'COMMON.LOCAL'
>       include 'COMMON.SPLITELE'
>       include 'COMMON.INTERACT'
>       integer icall
>       common /srutu/ icall
>       double precision ggg(6),ggg1(6),cc(3),cc1(3),xx(3),
>      & x(maxvar),g(maxvar)
>       double precision dcnorm_safe(3),dxnorm_safe(3)
>       double precision grad_s(6,0:maxres),grad_s1(6,0:maxres)
>       double precision phi_temp(maxres),theta_temp(maxres),
>      &  alph_temp(maxres),omeg_temp(maxres)
>       double precision energia(0:n_ene),energia1(0:n_ene)
>       integer uiparm(1)
>       double precision urparm(1)
>       external fdum
>       integer i,j,k,nf
>       double precision etot,etot1,etot2,etot11,etot12,etot21,etot22
>       double precision dist,alpha,beta
>       double precision debug_div
>       common /checkecartintcommon/ x,g,grad_s,grad_s1,phi_temp,
>      &                             theta_temp,alph_temp,omeg_temp
> c      r_cut=2.0d0
> c      rlambd=0.3d0
>       icg=1
>       nf=0
>       nfl=0
> c      print *,"ATU 3"
>       call int_from_cart1(.false.)
>       call intout
> c      call intcartderiv
> c      call checkintcartgrad
> c      call zerograd
> c      aincr=8.0D-7
> c      aincr=1.0D-7
>       print '("Calling CHECK_ECARTINT",1pd12.3)',aincr
>       write (iout,'("Calling CHECK_ECARTINT",1pd12.3)') aincr
>       call cartprint
>       nf=0
>       icall=0
> c      call geom_to_var(nvar,x)
>       if (.not.split_ene) then
>         write (iout,*) "Before calling energy"
>         call flush(iout)
>         call etotal(energia(0))
>         write (iout,*) "After calling energy"
>         call flush(iout)
>         etot=energia(0)
>         call enerprint(energia(0))
>         write (iout,*) "enter cartgrad"
>         call flush(iout)
>         call cartgrad
> c#ifdef VIRTUAL
> c Transform the gradient to Cartesians
> c        call grad_transform
> c#endif
>         write (iout,*) "exit cartgrad"
>         call flush(iout)
>         icall =1
>         write (iout,'(//27(1h*)," Checking energy gradient ",27(1h*))')
>         write (iout,'(//4x,3a12,3x,3a12)')"gcart_x","gcart_y","gcart_z",
>      &     "gxcart_x","gxcart_y","gxcart_z"
>         do i=1,nres
>           write (iout,'(i4,3e12.4,3x,3e12.4)') i,(gcart(j,i),j=1,3),
>      &      (gxcart(j,i),j=1,3)
>         enddo
>         do j=1,3
>           grad_s(j,0)=gcart(j,0)
>         enddo
>         do i=1,nres
>           do j=1,3
>             grad_s(j,i)=gcart(j,i)
>             grad_s(j+3,i)=gxcart(j,i)
>           enddo
>         enddo
>       else
> !- split gradient check
> c        call zerograd
>         next_RESPA=.true.
>         last_split=.true.
>         call place_coords_in_box
>         call make_soa_dc_norm
>         call list_update
>         call etotal_short(energia(0))
>         call enerprint(energia(0))
>         call flush(iout)
> c        write (iout,*) "enter cartgrad"
> c        call flush(iout)
>         call cartgrad
> c        write (iout,*) "exit cartgrad"
> c        call flush(iout)
>         icall =1
>         write (iout,*) "shortrange grad"
>         do i=1,nres
>           write (iout,'(i4,3e12.4,3x,3e12.4)') i,(gcart(j,i),j=1,3),
>      &    (gxcart(j,i),j=1,3)
>         enddo
>         do j=1,3
>           grad_s(j,0)=gcart(j,0)
>         enddo
>         do i=1,nres
>           do j=1,3
>             grad_s(j,i)=gcart(j,i)
>             grad_s(j+3,i)=gxcart(j,i)
>           enddo
>         enddo
> c        call zerograd
>         call etotal_long(energia(0))
>         call enerprint(energia(0))
>         call flush(iout)
> c        write (iout,*) "enter cartgrad"
> c        call flush(iout)
>         call cartgrad
> c        write (iout,*) "exit cartgrad"
> c        call flush(iout)
>         icall =1
>         write (iout,*) "longrange grad"
>         do i=1,nres
>           write (iout,'(i4,3e12.4,3x,3e12.4)') i,(gcart(j,i),j=1,3),
>      &    (gxcart(j,i),j=1,3)
>         enddo
>         do j=1,3
>           grad_s1(j,0)=gcart(j,0)
>         enddo
>         do i=1,nres
>           do j=1,3
>             grad_s1(j,i)=gcart(j,i)
>             grad_s1(j+3,i)=gxcart(j,i)
>           enddo
>         enddo
>       endif
> c      write (iout,*) "Vector dc"
> c      do i=0,nres
> c        write (iout,'(i5,2(3f10.5,5x))')
> c     &     i,(dc(j,i),j=1,3),(dc(j,i+nres),j=1,3)
> c      enddo
> c      write (iout,*) "Coordinates after chainbuild_cart"
> c      call chainbuild_cart
> c      call cartprint
>       write (iout,'(/a/)') 'Gradient in Calpha and SC coordinates'
>       do i=1,nres
> c        print *,i
>         if (itype(i).eq.ntyp1) cycle
>         do j=1,3
>           xx(j)=c(j,i+nres)
>           cc(j)=c(j,i)
> c          if (i.gt.1) then
> c            if (itype(i-1).eq.ntyp1 .and. itype(i).ne.ntyp1) then
> c              cc1(j)=c(j,i-1)
> c            endif
> c          endif
> c          if (i.lt.nres) then
> c            if (itype(i+1).eq.ntyp1 .and. itype(i).ne.ntyp1) then
> c              cc1(j)=c(j,i+1)
> c            endif
> c          endif
> c          do k=1,3
> c            dcnorm_safe(k)=dc_norm(k,i)
> c            dxnorm_safe(k)=dc_norm(k,i+nres)
> c          enddo
>         enddo
>         do j=1,3
>           c(j,i)=cc(j)+aincr
> c          if (i.gt.1) then
> c            if (itype(i-1).eq.ntyp1 .and. itype(i).ne.ntyp1) then
> c              c(j,i-1)=cc1(j)+aincr
> c              c(j,i-1)=c(j,i)-dc(j,i-1)
> c            endif
> c          endif
> c          if (i.lt.nres) then
> c            if (itype(i+1).eq.ntyp1 .and. itype(i).ne.ntyp1) then
> c              c(j,i+1)=cc1(j)+aincr
> c              c(j,i+1)=c(j,i)+dc(j,i)
> c            endif
> c          endif
>           call calculate_dc
> #ifdef MPI
> c Broadcast the order to compute internal coordinates to the slaves.
> c          if (nfgtasks.gt.1)
> c     &      call MPI_Bcast(6,1,MPI_INTEGER,king,FG_COMM,IERROR)
> #endif
> c          call int_from_cart1(.false.)
>           if (.not.split_ene) then
>             call etotal(energia1(0))
> c            call enerprint(energia1(0))
>             etot1=energia1(0)
>           else
> !- split gradient
>             call etotal_short(energia1(0))
>             etot11=energia1(0)
>             call etotal_long(energia1(0))
>             etot12=energia1(0)
> c            write (iout,*) "etot11",etot11," etot12",etot12
>           endif
> !- end split gradient
> c          write(iout,'(2i5,2(a,e15.5))')i,j," etot",etot," etot1",etot1
>           c(j,i)=cc(j)-aincr
> c          if (i.gt.1) then
> c            if (itype(i-1).eq.ntyp1 .and. itype(i).ne.ntyp1) then
> c              c(j,i-1)=cc1(j)-aincr
> c              c(j,i-1)=c(j,i)-dc(j,i-1)
> c            endif
> c          endif
> c          if (i.lt.nres) then
> c            if (itype(i+1).eq.ntyp1 .and. itype(i).ne.ntyp1) then
> c              c(j,i+1)=cc1(j)-aincr
> c              c(j,i+1)=c(j,i)+dc(j,i)
> c            endif
> c          endif
>           call calculate_dc
> C          print *,c(j,i)
> c          call int_from_cart1(.false.)
>           if (.not.split_ene) then
>             call etotal(energia1(0))
> c            call enerprint(energia1(0))
>             etot2=energia1(0)
>             ggg(j)=(etot1-etot2)/(2*aincr)
>           else
> !- split gradient
>             call etotal_short(energia1(0))
>             etot21=energia1(0)
>             ggg(j)=(etot11-etot21)/(2*aincr)
>             call etotal_long(energia1(0))
>             etot22=energia1(0)
>             ggg1(j)=(etot12-etot22)/(2*aincr)
> !- end split gradient
> c            write (iout,*) "etot21",etot21," etot22",etot22
>           endif
> c          write(iout,'(2i5,2(a,e15.5))')i,j," etot",etot," etot2",etot2
>           c(j,i)=cc(j)
> c          if (i.gt.1) then
> c            if (itype(i-1).eq.ntyp1 .and. itype(i).ne.ntyp1) then
> c              c(j,i-1)=cc1(j)
> c            endif
> c          endif
> c          if (i.lt.nres) then
> c            if (itype(i+1).eq.ntyp1 .and. itype(i).ne.ntyp1) then
> c              c(j,i+1)=cc1(j)
> c            endif
> c          endif
>           call calculate_dc
>         enddo
>         do j=1,3
>           c(j,i+nres)=xx(j)+aincr
>           call calculate_dc
> c          write (iout,*) "i",i," j",j," dxnorm+ and dxnorm"
> c          write (iout,'(3f15.10)') (dc_norm(k,i+nres),k=1,3)
> c          write (iout,'(3f15.10)') (dxnorm_safe(k),k=1,3)
> c          write (iout,*) "dxnormnorm",dsqrt(
> c     &  dc_norm(1,i+nres)**2+dc_norm(2,i+nres)**2+dc_norm(3,i+nres)**2)
> c          write (iout,*) "dxnormnormsafe",dsqrt(
> c     &      dxnorm_safe(1)**2+dxnorm_safe(2)**2+dxnorm_safe(3)**2)
> c          write (iout,*)
>           if (.not.split_ene) then
>             call etotal(energia1(0))
>             etot1=energia1(0)
>           else
> !- split gradient
>             call etotal_short(energia1(0))
>             etot11=energia1(0)
>             call etotal_long(energia1(0))
>             etot12=energia1(0)
>           endif
> !- end split gradient
> c          write(iout,'(2i5,2(a,f15.10))')i,j," etot",etot," etot1",etot1
>           c(j,i+nres)=xx(j)-aincr
>           call calculate_dc
> c          write (iout,*) "i",i," j",j," dxnorm- and dxnorm"
> c          write (iout,'(3f15.10)') (dc_norm(k,i+nres),k=1,3)
> c          write (iout,'(3f15.10)') (dxnorm_safe(k),k=1,3)
> c          write (iout,*)
> c          write (iout,*) "dxnormnorm",dsqrt(
> c     &  dc_norm(1,i+nres)**2+dc_norm(2,i+nres)**2+dc_norm(3,i+nres)**2)
> c          write (iout,*) "dxnormnormsafe",dsqrt(
> c     &      dxnorm_safe(1)**2+dxnorm_safe(2)**2+dxnorm_safe(3)**2)
>           if (.not.split_ene) then
>             call etotal(energia1(0))
>             etot2=energia1(0)
>             ggg(j+3)=(etot1-etot2)/(2*aincr)
>           else
> !- split gradient
>             call etotal_short(energia1(0))
>             etot21=energia1(0)
>             ggg(j+3)=(etot11-etot21)/(2*aincr)
>             call etotal_long(energia1(0))
>             etot22=energia1(0)
>             ggg1(j+3)=(etot12-etot22)/(2*aincr)
> !- end split gradient
>           endif
> c          write(iout,'(2i5,2(a,f15.10))')i,j," etot",etot," etot2",etot2
>           c(j,i+nres)=xx(j)
>           call calculate_dc
>         enddo
>         write (iout,'(i3,6(1pe12.5)/3x,6(1pe12.5)/3x,6(1pe12.5)/)')
>      &   i,(ggg(k),k=1,6),(grad_s(k,i),k=1,6),
>      &     (debug_div(ggg(k),grad_s(k,i)),k=1,6)
>         if (split_ene) then
>           write (iout,'(i3,6(1pe12.5)/3x,6(1pe12.5)/3x,6(1pe12.5)/)')
>      &   i,(ggg1(k),k=1,6),(grad_s1(k,i),k=1,6),(ggg1(k)/grad_s1(k,i),
>      &   k=1,6)
>          write (iout,'(i3,6(1pe12.5)/3x,6(1pe12.5)/3x,6(1pe12.5)/)')
>      &   i,(ggg(k)+ggg1(k),k=1,6),(grad_s(k,i)+grad_s1(k,i),k=1,6),
>      &   ((ggg(k)+ggg1(k))/(grad_s(k,i)+grad_s1(k,i)),k=1,6)
>         endif
>       enddo
>       return
>       end
> #endif
578a710,712
> #ifdef FNLOG
>       write(*,*)'int_from_cart1 cpu'
> #endif
612,627c746,761
<          !tauangle(1,i+1)=beta(c(1,i-1+nres),c(1,i-1),c(1,i),c(1,i+1))
<          call beta_2(c(1,i-1+nres),c(1,i-1),c(1,i),c(1,i+1),
<      &               tauangle(1,i+1),pom)
<          cos_tauangle1(i+1)=pom
<          !sin_tauangle1(i+1)=dsqrt(1.0d0 - pom*pom)
<          sin_tauangle1(i+1)=sign(dsqrt(1.0d0-pom*pom),tauangle(1,i+1))
<          !omicron(1,i)=alpha(c(1,i-2),c(1,i-1),c(1,i-1+nres))
<          pom=cos_alpha(c(1,i-2),c(1,i-1),c(1,i-1+nres))
<          cos_omicron(1,i)=pom
<          sin_omicron(1,i)=dsqrt(1.0d0 - pom*pom)
<          omicron(1,i)=arcos(pom)
<          !omicron(2,i)=alpha(c(1,i-1+nres),c(1,i-1),c(1,i))
<          pom=cos_alpha(c(1,i-1+nres),c(1,i-1),c(1,i))
<          cos_omicron(2,i)=pom
<          sin_omicron(2,i)=dsqrt(1.0d0 - pom*pom)
<          omicron(2,i)=arcos(pom)
---
>           !tauangle(1,i+1)=beta(c(1,i-1+nres),c(1,i-1),c(1,i),c(1,i+1))
>           call beta_2(c(1,i-1+nres),c(1,i-1),c(1,i),c(1,i+1),
>      &      tauangle(1,i+1),pom)
>           cos_tauangle1(i+1)=pom
>           !sin_tauangle1(i+1)=dsqrt(1.0d0 - pom*pom)
>           sin_tauangle1(i+1)=sign(dsqrt(1.0d0-pom*pom),tauangle(1,i+1))
>           !omicron(1,i)=alpha(c(1,i-2),c(1,i-1),c(1,i-1+nres))
>           pom=cos_alpha(c(1,i-2),c(1,i-1),c(1,i-1+nres))
>           cos_omicron(1,i)=pom
>           sin_omicron(1,i)=dsqrt(1.0d0 - pom*pom)
>           omicron(1,i)=arcos(pom)
>           !omicron(2,i)=alpha(c(1,i-1+nres),c(1,i-1),c(1,i))
>           pom=cos_alpha(c(1,i-1+nres),c(1,i-1),c(1,i))
>           cos_omicron(2,i)=pom
>           sin_omicron(2,i)=dsqrt(1.0d0 - pom*pom)
>           omicron(2,i)=arcos(pom)
658a793,795
> !       write(*,'(a,i4,2f18.10)')'vi',i,vbld(i),vbld_inv(i)
> !       write(*,'(a,i4,2f18.10)')'vi',i+nres,
> !    &    vbld(i+nres),vbld_inv(i+nres)
716,718c853
<         !do j=1,3
<         !  dc_norm(j,i)=dc(j,i)*vbld_inv(i+1)
<         !enddo
---
>         !write(*,'(a,i4,3f18.10)')'dcn',i,dc_norm(:,i)
724,726c859
<         !do j=1,3
<         !  dc_norm(j,i+nres)=dc(j,i+nres)*vbld_inv(i+nres)
<         !enddo
---
>         !write(*,'(a,i4,3f18.10)')'dcn',i+nres,dc_norm(:,i+nres)
783c916
<       call chainbuild
---
>       call chainbuild_extconf
check_sc_distr.f
cinfo.f
5,7c5,7
<       write(iout,*)'Branch homology'
<       write(iout,*)'Commit 3d6f9e7811a3341955e6fdda721ab6de34efe252'
<       write(iout,*)'compiled Thu Aug 07 08:18:12 2025'
---
>       write(iout,*)'Branch main'
>       write(iout,*)'Commit d274a81c105df18225a5ecca022a6a102488af4a'
>       write(iout,*)'compiled Fri Jun 21 12:53:30 2024'
12a13,16
>       write(iout,*)'FORTRAN_DEBUG_FLAGS='
>       write(iout,*)'CUDA_DEBUG_FLAGS=-DNDEBUG '
>       write(iout,*)'FORTRAN_DEBUG_FLAGS=-g'
>       write(iout,*)'CUDA_DEBUG_FLAGS=-g -lineinfo'
14c18,37
<       write(iout,*)'EXTRAOPTS=-qopenmp -march=native \'
---
>       write(iout,*)'CUDACC:=nvcc'
>       write(iout,*)'CUDACC=nvcc -ccbin $(SITE_CC)'
>       write(iout,*)'CUDA_TARGETS:=$(if $(CUDA_TARGETS),$(CUDA_TARGE...'
>       write(iout,*)'CUDALINK=$(CUDACC) -dlink'
>       write(iout,*)'CUDAOPTS=-O3 $(CUDA_TARGETS) --compiler-options...'
>       write(iout,*)'CUDALIBS=-L/usr/local/cuda/lib64 -lcudart -lcud...'
>       write(iout,*)'EXTRAOPTS=-qopenmp -march=core-avx2 \'
>       write(iout,*)'cuda_fortran:=cuda.o cuda_md.o numa.o'
>       write(iout,*)'pre_cuda_object= \'
>       write(iout,*)'cuda_object:=cuda_linked.o'
>       write(iout,*)'accelerator_object=${cuda_object} ${pre_cuda_ob...'
>       write(iout,*)'EXTRAOPTS:=${EXTRAOPTS} -D WITH_CUDA'
>       write(iout,*)'ACCELERATORLIBS=${CUDALIBS}'
>       write(iout,*)'accelerator_object='
>       write(iout,*)'pre_accelerator_object='
>       write(iout,*)'ACCELERATORLIBS='
>       write(iout,*)'OPTIMIZE3=-O3 -ipo'
>       write(iout,*)'OPTIMIZE2=-O2 -ip'
>       write(iout,*)'OPTIMIZE3=-O3'
>       write(iout,*)'OPTIMIZE2=-O2'
23c46,47
<       write(iout,*)'LIBS = -lmpi xdrf/libxdrf.a xdrf/libxdrf.a ${SI...'
---
>       write(iout,*)'LIBS = xdrf/libxdrf.a ${SITE_LIBS} ${ACCELERATO...'
>       write(iout,*)'MPILIBS=-lmpi'
26c50
<       write(iout,*)'OUTPUTDIR = ../../../bin'
---
>       write(iout,*)'OUTPUTDIR = ../bin'
30a55
>       write(iout,*)'NEWCORR5D: LIBS:=${LIBS} ${MPILIBS}'
35d59
<       write(iout,*)'SINGLE: LIBS = xdrf/libxdrf.a'
38a63,64
>       write(iout,*)'RSTTORSB: CPPFLAGS = -DPROCOR -DLINUX -DPGI -DA...'
>       write(iout,*)'RSTTORSB: RSTTORSBBIN = ${OUTPUTDIR}/rsttorsb.exe'
compare_s1.F
contact_cp.F
48c48
<        call chainbuild
---
>        call chainbuild_extconf
200c200
<         call chainbuild
---
>         call chainbuild_extconf
341c341
<          call chainbuild
---
>          call chainbuild_extconf
355c355
<        call chainbuild
---
>        call chainbuild_extconf
402c402
<          call chainbuild
---
>          call chainbuild_extconf
contact.F
2c2,4
<       implicit real*8 (a-h,o-z)
---
>       use omp_lib
>       use omp_data
>       implicit none
9c11,15
<       real*8 facont /1.569D0/  ! facont = (2/(1-sqrt(1-1/4)))**(1/6)
---
>       include 'COMMON.TIME1'
>       real*8 facont
>       parameter (facont=1.569D0)   ! facont = (2/(1-sqrt(1-1/4)))**(1/6)
>       integer kkk
>       parameter (kkk=3)
11a18,21
>       integer i,j,iti,itj,my_thread,contcount,base,i1,i2,it1,it2
>       integer allcontcount
>       integer*8 th_co(8,max_fg_threads),jsum
>       double precision co,rcomp
12a23,27
>       double precision dist_sq
>       double precision time0,timer_value
> 
>       time0=timer_value()
> 
14,17c29,32
<       kkk=3
<       co=0.0d0
< c!$OMP PARALLEL DO SCHEDULE(DYNAMIC) DEFAULT(SHARED)
< c!$OMP& REDUCTION(+:co,ncont) PRIVATE(iti,j,itj,rcomp)
---
>       allcontcount=0
>       th_co=0
> !$OMP PARALLEL DO SCHEDULE(DYNAMIC) DEFAULT(SHARED)
> !$OMP& PRIVATE(j,iti,itj,rcomp,contcount,my_thread,base,jsum)
18a34,36
>         my_thread=omp_get_thread_num()+1
>         contcount=0
>         jsum=0
35,38c53,56
<             ncont=ncont+1
<             co = co + dfloat(iabs(i-j))
<             icont(1,ncont)=i
<             icont(2,ncont)=j
---
>             jsum=jsum+iabs(i-j)
>             contcount=contcount+1
>             workspace(my_thread).item.list_ranges(2*contcount-1)=i
>             workspace(my_thread).item.list_ranges(2*contcount-0)=j
40a59,89
>         th_co(1,my_thread)=th_co(1,my_thread)+jsum
> !$OMP ATOMIC CAPTURE
>         base=allcontcount
>         allcontcount=allcontcount+contcount
> !$OMP END ATOMIC
>         do j=1,contcount
>           icont(1,base+j)=workspace(my_thread).item.list_ranges(j*2-1)
>           icont(2,base+j)=workspace(my_thread).item.list_ranges(j*2-0)
>         enddo
> 
>       enddo ! do i=nnt+kkk,nct
> 
>       ncont=allcontcount
> 
>       if (lprint) then
>         write (iout,'(a)') 'Contact map:'
>         do i=1,ncont
>           i1=icont(1,i)
>           i2=icont(2,i)
>           it1=itype(i1)
>           it2=itype(i2)
>           write (iout,'(i5,2x,a,i5,2x,a,i4)')
>      &     i,restyp(it1),i1,restyp(it2),i2
>         enddo
>         write (iout,*) "End contact map"
>         call flush(iout)
>       endif
> 
>       co=0.0d0
>       do i=1,omp_get_max_threads()
>         co=co+dfloat(th_co(1,i))
41a91,184
>       if (ncont*nres.ne.0) then
>         co=co/dfloat(nres*ncont)
>       endif
> 
>       time_contact=time_contact+timer_value()-time0
> 
>       return
>       end
> 
> C----------------------------------------------------------------------
>       subroutine contact_with_fract(lprint,ncont,icont,ref_c,
>      &  icont_shift,co,fract,fract_nn)
>       use omp_lib
>       use omp_data
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.IOUNITS'
>       include 'COMMON.CHAIN'
>       include 'COMMON.INTERACT'
>       include 'COMMON.FFIELD'
>       include 'COMMON.NAMES'
>       include 'COMMON.TIME1'
>       real*8 facont
>       parameter (facont=1.569D0)   ! facont = (2/(1-sqrt(1-1/4)))**(1/6)
>       integer kkk
>       parameter (kkk=3)
>       integer ncont,ncont_ref,nmatch,icont_shift
>       integer icont(2,maxcont)
>       integer i,j,iti,itj,my_thread,contcount,refcount,matchcount,base
>       integer i1,i2,it1,it2
>       integer allcontcount
>       integer*8 th_co(8,max_fg_threads),jsum
>       double precision ref_c(3,maxres2+2)
>       double precision co,rcomp
>       logical lprint
>       logical match
>       double precision dist_sq
>       double precision fract,fract_nn
>       double precision time0,timer_value
> 
>       time0=timer_value()
> 
>       allcontcount=0
>       th_co=0
> !$OMP PARALLEL DO SCHEDULE(DYNAMIC) DEFAULT(SHARED)
> !$OMP& PRIVATE(
> !$OMP&   j,iti,itj,rcomp,contcount,my_thread,base,jsum,
> !$OMP&   match,refcount,matchcount)
>       do i=nnt+kkk,nct
>         my_thread=omp_get_thread_num()+1
>         contcount=0
>         refcount=0
>         matchcount=0
>         jsum=0
>         iti=iabs(itype(i))
>         if (iti.eq.ntyp1) cycle
>         do j=nnt,i-kkk
>           itj=iabs(itype(j))
>           if (itj.eq.ntyp1) cycle
>           if (ipot.ne.4) then
>             rcomp=facont*sigmaii(iti,itj)
>           else
>             rcomp=facont*sigma(iti,itj)
>           endif
>           match=.false.
>           rcomp=rcomp*rcomp
>           if (dist_sq(c(1,nres+i),c(1,nres+j)).lt.rcomp) then
>             jsum=jsum+iabs(i-j)
>             contcount=contcount+1
>             workspace(my_thread).item.list_ranges(2*contcount-1)=i
>             workspace(my_thread).item.list_ranges(2*contcount-0)=j
>             match=.true.
>           endif
>           if (dist_sq(ref_c(1,nres+i+icont_shift),
>      &                ref_c(1,nres+j+icont_shift)).lt.rcomp) then
>             refcount=refcount+1
>             if (match) matchcount=matchcount+1
>           endif
>         enddo
>         th_co(1,my_thread)=th_co(1,my_thread)+jsum
>         th_co(2,my_thread)=th_co(2,my_thread)+refcount
>         th_co(3,my_thread)=th_co(3,my_thread)+matchcount
> !$OMP ATOMIC CAPTURE
>         base=allcontcount
>         allcontcount=allcontcount+contcount
> !$OMP END ATOMIC
>         do j=1,contcount
>           icont(1,base+j)=workspace(my_thread).item.list_ranges(j*2-1)
>           icont(2,base+j)=workspace(my_thread).item.list_ranges(j*2-0)
>         enddo
> 
>       enddo ! do i=nnt+kkk,nct
> 
>       ncont=allcontcount
56,60c199,221
< !     co = 0.0d0
< !     do i=1,ncont
< !       co = co + dfloat(iabs(icont(1,i)-icont(2,i)))
< !     enddo
<       co = co / (nres*ncont)
---
> 
>       co=0.0d0
>       ncont_ref=0
>       nmatch=0
>       do i=1,omp_get_max_threads()
>         co=co+dfloat(th_co(1,i))
>         ncont_ref=ncont_ref+th_co(2,i)
>         nmatch=nmatch+th_co(3,i)
>       enddo
>       if (ncont*nres.ne.0) then
>         co=co/dfloat(nres*ncont)
>       endif
> 
>       fract=dfloat(nmatch)/dfloat(ncont_ref)
> 
>       if (ncont.ne.0) then
>         fract_nn=dfloat(ncont-nmatch)/dfloat(ncont)
>       else
>         fract_nn=0
>       endif
> 
>       time_contact=time_contact+timer_value()-time0
> 
62a224,239
> 
> C----------------------------------------------------------------------
>       subroutine save_contact_ref
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       include 'COMMON.INTERACT'
>       include 'COMMON.CONTACTS'
>       integer i
>       !icont_ref_c(:,1:2*nres)=c(:,1:2*nres)
>       do i=1,nres
>         icont_ref_c(:,nres+i)=c(:,nres+i)
>       enddo
>       return
>       end
> 
108c285,289
<       contact_fract_nn=dfloat(ncont-nmatch)/dfloat(ncont)
---
>       if (ncont.ne.0) then
>         contact_fract_nn=dfloat(ncont-nmatch)/dfloat(ncont)
>       else
>         contact_fract_nn=0
>       endif
convert.f
cored.f
csa.f
324a325
>       call save_contact_ref
dfa.F
diff12.f
dihed_cons.F
djacob.f
ebend_kcc.F
21d20
<       logical lprn
29a29
>       logical lprn
121c121
<       etheta=0
---
>       etheta=0.0d0
econstr_local.F
econstr_qlike.F
econstrq-PMF.F
edis_aveave.F
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/edis_aveave.F: No such file or directory
edis_aveave-orig-Aug-4-2025.F
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/edis_aveave-orig-Aug-4-2025.F: No such file or directory
edis_aveave-orig.F
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/edis_aveave-orig.F: No such file or directory
edis_aveave-test.F
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/edis_aveave-test.F: No such file or directory
edis_dev.F
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/edis_dev.F: No such file or directory
edis.F
1a2,3
>       use omp_lib
>       use omp_data
5d6
< c      implicit real*8 (a-h,o-z)
12a14
>       include 'COMMON.FFIELD'
22,25d23
<       include 'COMMON.MD'
<       include 'COMMON.NAMES'
<       include 'COMMON.AVNLOC'
<       include 'COMMON.RESTARTED'
28,30c26,29
<       double precision rvec(3)
<       double precision ddtab(200)
<       integer isortkey(200)
---
>       !double precision rvec(3)
>       !double precision ddtab(200)
>       !integer isortkey(200)
>       double precision dgxi(3),dgxj(3),dgci(3),dgcj(3),dehpb
33c32
<       integer icount,iatom
---
>       !integer icount
35a35
>       double precision ehpb1,ehpb2,ehpb3
37,57c37,46
<      & rlornmr2,rlornmr2prim,gamber,gamberprim
<       double precision time00,time01
<       double precision dnow
<       double precision denom,dravdr,dravdr_max
<       integer INCFLG
<       double precision r0HH /2.0d0/
<       double precision epsHH /0.01d0/
<       double precision ehpbprim
<       double precision scaltau,scal_force_temp
<       icount=0
< #ifdef TIMING_ENE
< #ifdef MPI
<       time00=MPI_Wtime()
< #else
<       time00=tcpu()
< #endif
< #endif
<       ehpb=0.0D0
<       do i=1,3
<        ggg(i)=0.0d0
<       enddo
---
>      & rlornmr2,rlornmr2prim
>       integer my_thread,threads_used,from,to,link_low
>       double precision gg1(3),gg2(3),gg3(3)
>       double precision time01
>       !icount=0
>       ehpb=0.0d0
>       ehpb1=0.0d0
>       ehpb2=0.0d0
>       ehpb3=0.0d0
>       ggg(:)=0.0d0
59a49,54
> #ifdef EDISLOG
>       write(*,*)'cpu edis',restr_on_coord,constr_dist,
>      &  link_end,link_end_peak
> #endif
>       threads_used=omp_get_max_threads()
>       ! restr_on_coord requires constr_type/constr_dist=5
61a57,58
> !$OMP PARALLEL DO DEFAULT(SHARED) REDUCTION(+:ehpb1)
> !$OMP& PRIVATE(ecoor,j)
68a66,69
> #ifdef EDISLOG
>         write(*,'(a,i6,3f18.12)')'EDISca',i,
>      &    bfac(i)*(c(:,i)-cref(:,i))
> #endif
73a75,78
> #ifdef EDISLOG
>           write(*,'(a,i6,3f18.12)')'EDIScb',i,
>      &      bfac(i)*(c(:,i+nres)-cref(:,i+nres))
> #endif
77c82,86
<         ehpb=ehpb+0.5d0*bfac(i)*ecoor
---
>         ehpb1=ehpb1+0.5d0*bfac(i)*ecoor
> #ifdef EDISLOG
>         write(*,'(a,i6,3f18.12)')'EDIScc',i,
>      &    0.5d0*bfac(i)*ecoor,ecoor,bfac(i)
> #endif
78a88,92
> #ifdef EDISLOG
>         write(*,'(a,1f18.12)')'EDIScz',ehpb1
> #endif
> 
>       ehpb=ehpb+ehpb1
108a123,124
> 
> !=======================================================================
111,117c127,128
<       INCFLG=1
< c      write (iout,*) "atimeave",atimeave
<       if (atimeave.gt.0) then
<         call force_scale(1)
< #ifdef DEBUG
<         write (iout,*) "nsteps",nsteps(1),"tau_temp",tau_temp(1),
<      &  " scaling",scal_force(1)
---
> #ifdef EDISLOG
>       write(*,*)'ilink_start_peak loop',link_start_peak,link_end_peak
119,120c130,135
<       endif
<       dravdr_max=0.0d0
---
> !$OMP PARALLEL DO DEFAULT(SHARED)
> !$OMP& REDUCTION(+:ehpb2)
> !$OMP& PRIVATE(
> !$OMP&   j,k,l,ip,ii,jj,iti,itj,my_thread,
> !$OMP&   ehpb_peak,dd,ggg,aux,fac,slopefac,ggg_peak,gg1,gg2,gg3
> !$OMP& )
123,126c138,139
< #ifdef DEBUG
<         write(iout,*)"i",i," link_end_peak",link_end_peak," ipeak",
<      &   ipeak(1,i),ipeak(2,i)
< #endif
---
> c        print *,"i",i," link_end_peak",link_end_peak," ipeak",
> c     &   ipeak(1,i),ipeak(2,i)
129c142
<           ip = ipeak(1,i)
---
>           ip=ipeak(1,i)
135,165c148,150
< c
< c Prevent protons from clashing add LJ potential to ehpb 25/10/2023
< c
< c           ehpb=ehpb+epsHH*((r0HH/dd)**12-2*(r0HH/dd)**6)
< c           ehpb=ehpb+epsHH*(r0HH/dd)**12
< c Calculate derivative of LJ potential      
< c           ehpbprim=-12*epsHH*((r0HH/dd)**12-(r0HH/dd)**6)/dd**2
< c           ehpbprim=-12*epsHH*(r0HH/dd)**12/dd**2
< c          ehpbprim=0.0d0
< c--------------Print out pdb file if dd smaller than 1
<           if (dd.lt.1.0d0 ) then
<             write (iout,*) 
<      &        "nsteps(1)",nsteps(1)," ii",ii," jj",jj," dd",dd
< #ifdef PrintPDB
<             call pdbout(0.0d0,'BAD COORDS NMR',iout)
<             write(iout,'(a)') "REMARK reconstructed proton positions"
<             iatom=0
<             do k=nnt+1,nct
<               iatom=iatom+1
<               write (iout,'(4hATOM,i7,2x,a3,1x,a3,2x,i4,4x,3f8.3)')
<      &              iatom,"CA",restyp(itype(k)),k-nnt+1,(c(j,k),j=1,3)
<               iatom=iatom+1
<               write (iout,'(4hATOM,i7,2x,a3,1x,a3,2x,i4,4x,3f8.3)')
<      &          iatom,"H",restyp(itype(k)),k-nnt+1,(hpos(j,k-1,0),j=1,3)
<               iatom=iatom+1
<               write (iout,'(4hATOM,i7,2x,a3,1x,a3,2x,i4,4x,3f8.3)')
<      &          iatom,"HA",restyp(itype(k)),k-nnt+1,(hpos(j,k,1),j=1,3)
<               iatom=iatom+1
<               write (iout,'(4hATOM,i7,2x,a3,1x,a3,2x,i4,4x,3f8.3)')
<      &          iatom,"HB",restyp(itype(k)),k-nnt+1,(hpos(j,k,2),j=1,3)
<             enddo
---
> #ifdef EDISLOG
>           write(*,'(a,6i5)')'EDIS0',i,ip,ii,jj,iti,itj
>           write(*,'(a,i5,4f18.12)')'EDIS1',i,dd,ggg(:)
167,232d151
<           endif 
<           if (atimeave.gt.0) then
<             dnow=dd
< #ifdef DEBUG
< c          write (iout,'(a3,i3,2f13.10,2f17.14)'),"before",i,dnow,
< c     &    dd,dnmr_xave(1,i), dnmr_xave(2,i)
< c          write (iout,'(a3,i3,2f15.5)'),"before",i,dnow,
<      
< #endif
< c
< c---------------Call AVEINT subroutine from AMBER---------------------
< c          call AVEINT(dnmr_xave(1,ip),dnmr_xave0(ip),dnow,TAU,d_time0,
< c     &         NAVINT,3,1,INCFLG,1,dd,DENOM) 
< c     &       INCFLG
< c          write (iout,*) "nsteps",nsteps," TAU_temp",TAU_temp
<             if (restarted) then
< c Calculate the averages from restart information
<               call AVEINT(dnmr_xave(1,ip),dnmr_xave0(ip),dnow,TAU,
<      &         d_time0,NAVINT,3,1,INCFLG,2,dd,DENOM) 
<             else
< c Update the averages
<               call AVEINT(dnmr_xave(1,ip),dnmr_xave0(ip),dnow,TAU,
<      &         d_time0,NAVINT,3,1,INCFLG,1,dd,DENOM) 
<             endif
< c--------------Calcualte derivative factors of time-ave--------------
< c----------F=∂E/∂x=(∂E/∂r̄)(∂r̄/∂r(t))(∂r(t)/∂x)
<             if (icalls(1).lt.navint) then
<               DRAVDR=1.0d0
<             else if (ider_ave(1).eq.1) then
<               DRAVDR=0.5d0
<             else 
<               DRAVDR=0.5d0*(dd/dnow)**4*NAVINT*d_time0/DENOM
< c
< c Scaling factor of the forces
< c
<               dravdr = dravdr*scal_force(1)
<               if (dravdr.gt.dravdr_max) dravdr_max=dravdr
<             endif
< c
<             INCFLG=0
< #define DEBUG
< #ifdef DEBUG
<             if (ip.eq.3 .and. mod(nsteps(1),ntwe).eq.0) then
<               write (iout,'(a3,i10,i5,2f10.5,2f17.14,e20.10,f10.5)')
<      &        "co",nsteps(1),ip,dnow,
<      &        dd,dnmr_xave(1,ip),dnmr_xave(2,ip),dnmr_xave0(ip),DENOM 
<               write (iout,'(i5,3(1x,a,f10.5))') i,"dd/dnow",dd/dnow,
<      &       "d_time0/DENOM",d_time0/DENOM,"dravdr",dravdr
<             endif
< c          if (mod(nsteps(1),ntwe).eq.0) then
< c          write (iout,'(a3,i3,2f13.10,3f17.14)'),"co",i,dnow,
< c     &    dd,dnmr_xave(1,i), dnmr_xave(2,i), DENOM 
< c           write (iout,'(a3,1x,i8,1x,3(i2,1x),6(f10.6,1x),1x,i5)'),
< c     &      "nmr",nsteps(1),i,ii,jj,
< c     &      dnow,dd,dhpb_peak(ip),dhpb1_peak(ip),DENOM,DRAVDR,NAVINT
< c          endif
< #endif
< #undef DEBUG
< c          elseif (atimeave.eq.0) then
<            
< c           write (iout,'(a3,1x,3(i2,1x),3(f10.6,1x))'),
< c     &      "nmr",i,ii,jj,
< c     &      dd,dhpb_peak(ip),dhpb1_peak(ip)
<             
<           endif
< c---DRAVDR=((dd/dnow)*(1+IPOWER))*d_time0/DENOM---IPOWER=3 for NOE distance
234,235d152
< c#define NOWYLORENZ
< c#define AMBER
263a181,183
> #ifdef EDISLOG
>           write(*,'(a,i5,3f18.12)')'EDIS2',i,aux,fac,slope_peak
> #endif
278,291d197
< #elif defined (AMBER)
<           aux=gamber(dd,dhpb2_peak(ip),dhpb_peak(ip),dhpb1_peak(ip),
<      &        dhpb3_peak(ip),
<      &        forcon_peak(ip),fordepth_peak(ip))
<           fac=gamberprim(dd,dhpb2_peak(ip),dhpb_peak(ip),dhpb1_peak(ip),
<      &        dhpb3_peak(ip),
<      &        forcon_peak(ip),fordepth_peak(ip))
< #ifdef DEBUG
<           write(iout,'(a4,i3,1x,9(a7,f12.4,1x))') "ip",ip," dd",dd,
<      &        " d1",dhpb2_peak(ip),
<      &        " d2",dhpb_peak(ip)," d3",dhpb1_peak(ip),
<      &        " d4",dhpb3_peak(ip)," k2",forcon_peak(ip),
<      &        " k3",fordepth_peak(ip)," penalty",aux," grad",fac
< #endif
313,337c219,224
< #ifdef AMBER
<           ehpb=ehpb+aux
<           if (atimeave.gt.0) then
<             fac=fac/dnow*DRAVDR
<           else
<             fac=fac/dd
<           endif
< #else
<           ehpb=ehpb+fordepth_peak(ipeak(1,i))*aux
<      &      *scal_force(1)
<           if (atimeave.gt.0) then
< c            fac=fordepth_peak(ipeak(1,i))*fac/dnow*DRAVDR
<             fac=fordepth_peak(ipeak(1,i))*fac*DRAVDR
<           else
< c            fac=fordepth_peak(ipeak(1,i))*fac/dd
<             fac=fordepth_peak(ipeak(1,i))*fac
<           endif
< #endif
< c--------------------time-averaged modifed for fac---------------------
< c          if(atimeave.gt.0) fac=fordepth_peak(ipeak(1,i))*fac/dd
< c          should modified above line (190) or not?
< c          write (iout,*) "fac",fac," DRAVDR",DRAVDR
< c-------------adding gradient for LJ potential------Adam-Co25/10/2023--
< c          fac = fac + ehpbprim
< c---------------------------------------------------------------------
---
> #ifdef EDISLOG
>           write(*,'(a,3i5,a,3f18.12)')'EDISa',i,ii,jj,' e',
>      &      fordepth_peak(ipeak(1,i))*aux
> #endif
>           ehpb2=ehpb2+fordepth_peak(ipeak(1,i))*aux
>           fac=fordepth_peak(ipeak(1,i))*fac/dd
340a228,233
> #ifdef EDISLOG
>           write(*,'(a,3i5,a,3f18.12)')'EDISa',i,ii,jj,' a',ggg
> #endif
> 
>           my_thread=omp_get_thread_num()+1
> 
342,344c235,249
<             do j=1,3
<               ghpbx(j,ii)=ghpbx(j,ii)-ggg(j)*protpos(iti,itype(ii))
<             enddo
---
> !            do j=1,3
> !              ghpbx(j,ii)=ghpbx(j,ii)-ggg(j)*protpos(iti,itype(ii))
> !            enddo
>             aux=protpos(iti,itype(ii))
>             workspace(my_thread).item.dgradxx(ii)=
>      &        workspace(my_thread).item.dgradxx(ii)-ggg(1)*aux*wstrain
>             workspace(my_thread).item.dgradxy(ii)=
>      &        workspace(my_thread).item.dgradxy(ii)-ggg(2)*aux*wstrain
>             workspace(my_thread).item.dgradxz(ii)=
>      &        workspace(my_thread).item.dgradxz(ii)-ggg(3)*aux*wstrain
> 
> #ifdef EDISLOG
>             write(*,'(a,3i5,a,4f18.12)')'EDISa1',i,ii,jj,'  ',
>      &        -ggg(:)*protpos(iti,itype(ii)),protpos(iti,itype(ii))
> #endif
345a251
> 
347,349c253,267
<             do j=1,3
<               ghpbx(j,jj)=ghpbx(j,jj)+ggg(j)*protpos(itj,itype(jj))
<             enddo
---
> !            do j=1,3
> !              ghpbx(j,jj)=ghpbx(j,jj)+ggg(j)*protpos(itj,itype(jj))
> !            enddo
>             aux=protpos(itj,itype(jj))
>             workspace(my_thread).item.dgradxx(jj)=
>      &        workspace(my_thread).item.dgradxx(jj)+ggg(1)*aux*wstrain
>             workspace(my_thread).item.dgradxy(jj)=
>      &        workspace(my_thread).item.dgradxy(jj)+ggg(2)*aux*wstrain
>             workspace(my_thread).item.dgradxz(jj)=
>      &        workspace(my_thread).item.dgradxz(jj)+ggg(3)*aux*wstrain
> 
> #ifdef EDISLOG
>             write(*,'(a,3i5,a,4f18.12)')'EDISa2',i,ii,jj,'  ',
>      &        ggg(:)*protpos(itj,itype(jj)),protpos(itj,itype(jj))
> #endif
351,354c269,286
<           do k=1,3
<             ghpbc(k,jj)=ghpbc(k,jj)+ggg(k)
<             ghpbc(k,ii)=ghpbc(k,ii)-ggg(k)
<           enddo
---
> !          do k=1,3
> !            ghpbc(k,jj)=ghpbc(k,jj)+ggg(k)
> !            ghpbc(k,ii)=ghpbc(k,ii)-ggg(k)
> !          enddo
>           workspace(my_thread).item.dgradbufcx(ii)=
>      &      workspace(my_thread).item.dgradbufcx(ii)-wstrain*ggg(1)
>           workspace(my_thread).item.dgradbufcy(ii)=
>      &      workspace(my_thread).item.dgradbufcy(ii)-wstrain*ggg(2)
>           workspace(my_thread).item.dgradbufcz(ii)=
>      &      workspace(my_thread).item.dgradbufcz(ii)-wstrain*ggg(3)
> 
>           workspace(my_thread).item.dgradbufcx(jj)=
>      &      workspace(my_thread).item.dgradbufcx(jj)+wstrain*ggg(1)
>           workspace(my_thread).item.dgradbufcy(jj)=
>      &      workspace(my_thread).item.dgradbufcy(jj)+wstrain*ggg(2)
>           workspace(my_thread).item.dgradbufcz(jj)=
>      &      workspace(my_thread).item.dgradbufcz(jj)+wstrain*ggg(3)
> 
355a288,290
>             gg1(:)=0.0d0
>             gg2(:)=0.0d0
>             gg3(:)=0.0d0
358,362c293,300
<                 ghpbdc(k,ii-1)=ghpbdc(k,ii-1)-dhpos_dc(l,k,1,ii,iti)
<      &               *ggg(l)
<                 ghpbdc(k,ii)=ghpbdc(k,ii)-dhpos_dc(l,k,2,ii,iti)*ggg(l)
<                 ghpbdc(k,ii+1)=ghpbdc(k,ii+1)-dhpos_dc(l,k,3,ii,iti)
<      &               *ggg(l)
---
> !                ghpbdc(k,ii-1)=ghpbdc(k,ii-1)-dhpos_dc(l,k,1,ii,iti)
> !     &               *ggg(l)
> !                ghpbdc(k,ii)=ghpbdc(k,ii)-dhpos_dc(l,k,2,ii,iti)*ggg(l)
> !                ghpbdc(k,ii+1)=ghpbdc(k,ii+1)-dhpos_dc(l,k,3,ii,iti)
> !     &               *ggg(l)
>                 gg1(k)=gg1(k)-dhpos_dc(l,k,1,ii,iti)*ggg(l)
>                 gg2(k)=gg2(k)-dhpos_dc(l,k,2,ii,iti)*ggg(l)
>                 gg3(k)=gg3(k)-dhpos_dc(l,k,3,ii,iti)*ggg(l)
364a303,339
> !            do k=1,3
> !              ghpbdc(k,ii-1)=ghpbdc(k,ii-1)+gg1(k)
> !              ghpbdc(k,ii)=ghpbdc(k,ii)+gg2(k)
> !              ghpbdc(k,ii+1)=ghpbdc(k,ii+1)+gg3(k)
> !            enddo
>             workspace(my_thread).item.dgradcx(ii-1)=
>      &        workspace(my_thread).item.dgradcx(ii-1)+wstrain*gg1(1)
>             workspace(my_thread).item.dgradcy(ii-1)=
>      &        workspace(my_thread).item.dgradcy(ii-1)+wstrain*gg1(2)
>             workspace(my_thread).item.dgradcz(ii-1)=
>      &        workspace(my_thread).item.dgradcz(ii-1)+wstrain*gg1(3)
> 
>             workspace(my_thread).item.dgradcx(ii)=
>      &        workspace(my_thread).item.dgradcx(ii)+wstrain*gg2(1)
>             workspace(my_thread).item.dgradcy(ii)=
>      &        workspace(my_thread).item.dgradcy(ii)+wstrain*gg2(2)
>             workspace(my_thread).item.dgradcz(ii)=
>      &        workspace(my_thread).item.dgradcz(ii)+wstrain*gg2(3)
> 
>             workspace(my_thread).item.dgradcx(ii+1)=
>      &        workspace(my_thread).item.dgradcx(ii+1)+wstrain*gg3(1)
>             workspace(my_thread).item.dgradcy(ii+1)=
>      &        workspace(my_thread).item.dgradcy(ii+1)+wstrain*gg3(2)
>             workspace(my_thread).item.dgradcz(ii+1)=
>      &        workspace(my_thread).item.dgradcz(ii+1)+wstrain*gg3(3)
> 
> #ifdef EDISLOG
>             write(*,'(a,4i4,i2,9f12.6)')'EDISa3a',i,ii,jj,iti,1,
>      &        dhpos_dc(:,:,1,ii,iti)
>             write(*,'(a,4i4,i2,9f12.6)')'EDISa3a',i,ii,jj,iti,2,
>      &        dhpos_dc(:,:,2,ii,iti)
>             write(*,'(a,4i4,i2,9f12.6)')'EDISa3a',i,ii,jj,iti,3,
>      &        dhpos_dc(:,:,3,ii,iti)
>             write(*,'(a,3i5,a,3f18.12)')'EDISa3',i,ii,jj,'  ',gg1(:)
>             write(*,'(a,3i5,a,3f18.12)')'EDISa4',i,ii,jj,'  ',gg2(:)
>             write(*,'(a,3i5,a,3f18.12)')'EDISa5',i,ii,jj,'  ',gg3(:)
> #endif
366a342,344
>             gg1(:)=0.0d0
>             gg2(:)=0.0d0
>             gg3(:)=0.0d0
369,373c347,354
<                 ghpbdc(k,jj-1)=ghpbdc(k,jj-1)+dhpos_dc(l,k,1,jj,itj)
<      &                *ggg(l)
<                 ghpbdc(k,jj)=ghpbdc(k,jj)+dhpos_dc(l,k,2,jj,itj)*ggg(l)
<                 ghpbdc(k,jj+1)=ghpbdc(k,jj+1)+dhpos_dc(l,k,3,jj,itj)
<      &                *ggg(l)
---
> !                ghpbdc(k,jj-1)=ghpbdc(k,jj-1)+dhpos_dc(l,k,1,jj,itj)
> !     &                *ggg(l)
> !                ghpbdc(k,jj)=ghpbdc(k,jj)+dhpos_dc(l,k,2,jj,itj)*ggg(l)
> !                ghpbdc(k,jj+1)=ghpbdc(k,jj+1)+dhpos_dc(l,k,3,jj,itj)
> !     &                *ggg(l)
>                 gg1(k)=gg1(k)+dhpos_dc(l,k,1,jj,itj)*ggg(l)
>                 gg2(k)=gg2(k)+dhpos_dc(l,k,2,jj,itj)*ggg(l)
>                 gg3(k)=gg3(k)+dhpos_dc(l,k,3,jj,itj)*ggg(l)
375a357,382
>             workspace(my_thread).item.dgradcx(jj-1)=
>      &        workspace(my_thread).item.dgradcx(jj-1)+wstrain*gg1(1)
>             workspace(my_thread).item.dgradcy(jj-1)=
>      &        workspace(my_thread).item.dgradcy(jj-1)+wstrain*gg1(2)
>             workspace(my_thread).item.dgradcz(jj-1)=
>      &        workspace(my_thread).item.dgradcz(jj-1)+wstrain*gg1(3)
> 
>             workspace(my_thread).item.dgradcx(jj)=
>      &        workspace(my_thread).item.dgradcx(jj)+wstrain*gg2(1)
>             workspace(my_thread).item.dgradcy(jj)=
>      &        workspace(my_thread).item.dgradcy(jj)+wstrain*gg2(2)
>             workspace(my_thread).item.dgradcz(jj)=
>      &        workspace(my_thread).item.dgradcz(jj)+wstrain*gg2(3)
> 
>             workspace(my_thread).item.dgradcx(jj+1)=
>      &        workspace(my_thread).item.dgradcx(jj+1)+wstrain*gg3(1)
>             workspace(my_thread).item.dgradcy(jj+1)=
>      &        workspace(my_thread).item.dgradcy(jj+1)+wstrain*gg3(2)
>             workspace(my_thread).item.dgradcz(jj+1)=
>      &        workspace(my_thread).item.dgradcz(jj+1)+wstrain*gg3(3)
> 
> #ifdef EDISLOG
>             write(*,'(a,3i5,a,3f18.12)')'EDISa6',i,ii,jj,'  ',gg1(:)
>             write(*,'(a,3i5,a,3f18.12)')'EDISa7',i,ii,jj,'  ',gg2(:)
>             write(*,'(a,3i5,a,3f18.12)')'EDISa8',i,ii,jj,'  ',gg3(:)
> #endif
377a385
>             aux=0.0d0
379,380c387,389
<               ghpbdphi(ii-1)=ghpbdphi(ii-1)-
<      &          dhpos_gamma(k,ii,iti)*ggg(k)
---
> !              ghpbdphi(ii-1)=ghpbdphi(ii-1)-
> !     &          dhpos_gamma(k,ii,iti)*ggg(k)
>               aux=aux-dhpos_gamma(k,ii,iti)*ggg(k)
381a391,395
> #ifdef EDISLOG
>             write(*,'(a,3i5,a,1f18.12)')'EDISa9',i,ii,jj,'  ',aux
> #endif
>             workspace(my_thread).item.dgloc(ii-1)=
>      &        workspace(my_thread).item.dgloc(ii-1)+wstrain*aux
382a397
> 
383a399
>             aux=0.0d0
385,387c401,409
<               ghpbdphi(jj-1)=ghpbdphi(jj-1)+
<      &          dhpos_gamma(k,jj,itj)*ggg(k)
<             enddo
---
> !              ghpbdphi(jj-1)=ghpbdphi(jj-1)+
> !     &          dhpos_gamma(k,jj,itj)*ggg(k)
>               aux=aux+dhpos_gamma(k,jj,itj)*ggg(k)
>             enddo
>             workspace(my_thread).item.dgloc(jj-1)=
>      &        workspace(my_thread).item.dgloc(jj-1)+wstrain*aux
> #ifdef EDISLOG
>             write(*,'(a,3i5,a,1f18.12)')'EDISaa',i,ii,jj,'  ',aux
> #endif
432,465d453
<           if (atimeave.gt.0) then
<             dnow=dd
< #ifdef DEBUG
<             write (iout,'(a3,i3,2f14.10,2f17.14)'),"before",i,dnow,
<      &      dd,dnmr_xave(1,ip), dnmr_xave(2,ip)
< #endif
<             if (restarted) then
< c Calculate the averages from restart informaton
<               call AVEINT(dnmr_xave(1,ip),dnmr_xave0(ip),dnow,TAU,
<      &         d_time0,NAVINT,3,1,INCFLG,2,dd,DENOM) 
<             else
< c Update the averages
<               call AVEINT(dnmr_xave(1,ip),dnmr_xave0(ip),dnow,TAU,
<      &         d_time0,NAVINT,3,1,INCFLG,1,dd,DENOM)
<             endif
< #ifdef DEBUG
<             write (iout,'(a3,i3,2f14.10,2f17.14)'),"after",i,dnow,
<      &      dd,dnmr_xave(1,ip), dnmr_xave(2,ip)
< #endif
<             if (icalls(1).lt.navint) then
<               DRAVDR=1.0d0
<             else if (ider_ave(1).eq.1) then
<               DRAVDR=0.5d0
<             else 
<               DRAVDR=0.5d0*(dd/dnow)**4*NAVINT*d_time0/DENOM
< c
< c Scaling factor of the forces
< c
<               dravdr = dravdr*scal_force(1)
<               if (dravdr.gt.dravdr_max) dravdr_max=dravdr
<             endif
< c          DRAVDR=0.5d0*(dd/dnow)**4*d_time0/DENOM
<             INCFLG=0
<           endif
484,485c472
< c     &        forcon_peak(ip))*aux/dd
<      &        forcon_peak(ip))*aux
---
>      &        forcon_peak(ip))*aux/dd
488,489c475
< c     &        forcon_peak(ip),slope_peak)*aux/dd
<      &        forcon_peak(ip),slope_peak)*aux
---
>      &        forcon_peak(ip),slope_peak)*aux/dd
507,513c493
<             if (atimeave.gt.0) then
< c              fac=fac*aux/dnow*DRAVDR
<               fac=fac*aux*DRAVDR
<             else
< c              fac=fac*aux/dd
<               fac=fac*aux
<             endif
---
>             fac=fac*aux/dd
532,538c512
<             if (atimeave.gt.0) then
< c              fac=fac*aux/dnow*DRAVDR
<               fac=fac*aux*DRAVDR
<             else
< c              fac=fac*aux/dd
<               fac=fac*aux
<             endif
---
>             fac=fac*aux/dd
541,555d514
< #elif defined (AMBER)
<           aux=gamber(dd,dhpb2_peak(ip),dhpb_peak(ip),dhpb1_peak(ip),
<      &        dhpb3_peak(ip),
<      &        forcon_peak(ip),fordepth_peak(ip))
<           aux=dexp(-scal_peak*aux)
<           ehpb_peak=ehpb_peak+aux
<           fac=gamberprim(dd,dhpb2_peak(ip),dhpb_peak(ip),dhpb1_peak(ip),
<      &        dhpb3_peak(ip),forcon_peak(ip),fordepth_peak(ip))
<           if (atimeave.gt.0) then
< c            fac=fac*aux/dnow*DRAVDR
<             fac=fac*aux*DRAVDR
<           else
< c            fac=fac*aux/dd
<             fac=fac*aux
<           endif
572,578c531
<               if (atimeave.gt.0) then
< c                fac=fac*aux/dnow*DRAVDR
<                 fac=fac*aux*DRAVDR
<               else
< c                fac=fac*aux/dd
<                 fac=fac*dd
<               endif
---
>               fac=fac*aux/dd
585,586c538,539
<      &      "edisL",i,ii,iti,jj,itj,dd,dhpb_peak(ip),dhpb1_peak(ip),
<      &      forcon_peak(ip),fordepth_peak(ip),aux
---
>      &        "edisL",i,ii,iti,jj,itj,dd,dhpb_peak(ip),dhpb1_peak(ip),
>      &        forcon_peak(ip),fordepth_peak(ip),aux
594,601c547
<               if (atimeave.gt.0) then
< !                fac=fac*aux/dnow*DRAVDR
<                 fac=fac*aux*DRAVDR
<               else
< !                fac=fac*aux/dd
<                 fac=fac*aux
<               endif
<           endif
---
>               fac=fac*aux/dd
609a556
> 
612,613c559,560
<      & -fordepth_peak(ipeak(1,i))*dlog(ehpb_peak)/scal_peak,
<      &  " scal_peak",scal_peak
---
>      &    -fordepth_peak(ipeak(1,i))*dlog(ehpb_peak)/scal_peak,
>      &    " scal_peak",scal_peak
617c564,565
<         ehpb=ehpb-fordepth_peak(ipeak(1,i))*dlog(ehpb_peak)/scal_peak
---
> 
>         ehpb2=ehpb2-fordepth_peak(ipeak(1,i))*dlog(ehpb_peak)/scal_peak
629a578,580
> #ifdef EDISLOG
>           write(*,'(a,4i5,a,3f18.12)')'EDISa',i,ip,ii,jj,' b',ggg
> #endif
640,642c591,603
<             do j=1,3
<               ghpbx(j,ii)=ghpbx(j,ii)-ggg(j)*protpos(iti,itype(ii))
<             enddo
---
> !            do j=1,3
> !              ghpbx(j,ii)=ghpbx(j,ii)-ggg(j)*protpos(iti,itype(ii))
> !            enddo
>             workspace(my_thread).item.dgradxx(ii)=
>      &        workspace(my_thread).item.dgradxx(ii)-
>      &        ggg(1)*protpos(iti,itype(ii))*wstrain
>             workspace(my_thread).item.dgradxy(ii)=
>      &        workspace(my_thread).item.dgradxy(ii)-
>      &        ggg(2)*protpos(iti,itype(ii))*wstrain
>             workspace(my_thread).item.dgradxz(ii)=
>      &        workspace(my_thread).item.dgradxz(ii)-
>      &        ggg(3)*protpos(iti,itype(ii))*wstrain
> 
645,652c606,635
<             do j=1,3
<               ghpbx(j,jj)=ghpbx(j,jj)+ggg(j)*protpos(itj,itype(jj))
<             enddo
<           endif
<           do k=1,3
<             ghpbc(k,jj)=ghpbc(k,jj)+ggg(k)
<             ghpbc(k,ii)=ghpbc(k,ii)-ggg(k)
<           enddo
---
> !            do j=1,3
> !              ghpbx(j,jj)=ghpbx(j,jj)+ggg(j)*protpos(itj,itype(jj))
> !            enddo
>             workspace(my_thread).item.dgradxx(jj)=
>      &        workspace(my_thread).item.dgradxx(jj)+
>      &        ggg(1)*protpos(itj,itype(jj))*wstrain
>             workspace(my_thread).item.dgradxy(jj)=
>      &        workspace(my_thread).item.dgradxy(jj)+
>      &        ggg(2)*protpos(itj,itype(jj))*wstrain
>             workspace(my_thread).item.dgradxz(jj)=
>      &        workspace(my_thread).item.dgradxz(jj)+
>      &        ggg(3)*protpos(itj,itype(jj))*wstrain
>           endif
> !          do k=1,3
> !            ghpbc(k,jj)=ghpbc(k,jj)+ggg(k)
> !            ghpbc(k,ii)=ghpbc(k,ii)-ggg(k)
> !          enddo
>            workspace(my_thread).item.dgradbufcx(jj)=
>      &       workspace(my_thread).item.dgradbufcx(jj)+ggg(1)*wstrain
>            workspace(my_thread).item.dgradbufcy(jj)=
>      &       workspace(my_thread).item.dgradbufcy(jj)+ggg(2)*wstrain
>            workspace(my_thread).item.dgradbufcz(jj)=
>      &       workspace(my_thread).item.dgradbufcz(jj)+ggg(3)*wstrain
> 
>            workspace(my_thread).item.dgradbufcx(ii)=
>      &       workspace(my_thread).item.dgradbufcx(ii)-ggg(1)*wstrain
>            workspace(my_thread).item.dgradbufcy(ii)=
>      &       workspace(my_thread).item.dgradbufcy(ii)-ggg(2)*wstrain
>            workspace(my_thread).item.dgradbufcz(ii)=
>      &       workspace(my_thread).item.dgradbufcz(ii)-ggg(3)*wstrain
684a668,670
>             gg1(:)=0.0d0
>             gg2(:)=0.0d0
>             gg3(:)=0.0d0
687,691c673,680
<                 ghpbdc(k,ii-1)=ghpbdc(k,ii-1)-dhpos_dc(l,k,1,ii,iti)
<      &               *ggg(l)
<                 ghpbdc(k,ii)=ghpbdc(k,ii)-dhpos_dc(l,k,2,ii,iti)*ggg(l)
<                 ghpbdc(k,ii+1)=ghpbdc(k,ii+1)-dhpos_dc(l,k,3,ii,iti)
<      &               *ggg(l)
---
> !                ghpbdc(k,ii-1)=ghpbdc(k,ii-1)-dhpos_dc(l,k,1,ii,iti)
> !     &               *ggg(l)
> !                ghpbdc(k,ii)=ghpbdc(k,ii)-dhpos_dc(l,k,2,ii,iti)*ggg(l)
> !                ghpbdc(k,ii+1)=ghpbdc(k,ii+1)-dhpos_dc(l,k,3,ii,iti)
> !     &               *ggg(l)
>                 gg1(k)=gg1(k)-dhpos_dc(l,k,1,ii,iti)*ggg(l)
>                 gg2(k)=gg2(k)-dhpos_dc(l,k,2,ii,iti)*ggg(l)
>                 gg3(k)=gg3(k)-dhpos_dc(l,k,3,ii,iti)*ggg(l)
693a683,702
>             workspace(my_thread).item.dgradcx(ii-1)=
>      &        workspace(my_thread).item.dgradcx(ii-1)+gg1(1)*wstrain
>             workspace(my_thread).item.dgradcy(ii-1)=
>      &        workspace(my_thread).item.dgradcy(ii-1)+gg1(2)*wstrain
>             workspace(my_thread).item.dgradcz(ii-1)=
>      &        workspace(my_thread).item.dgradcz(ii-1)+gg1(3)*wstrain
> 
>             workspace(my_thread).item.dgradcx(ii)=
>      &        workspace(my_thread).item.dgradcx(ii)+gg2(1)*wstrain
>             workspace(my_thread).item.dgradcy(ii)=
>      &        workspace(my_thread).item.dgradcy(ii)+gg2(2)*wstrain
>             workspace(my_thread).item.dgradcz(ii)=
>      &        workspace(my_thread).item.dgradcz(ii)+gg2(3)*wstrain
> 
>             workspace(my_thread).item.dgradcx(ii+1)=
>      &        workspace(my_thread).item.dgradcx(ii+1)+gg3(1)*wstrain
>             workspace(my_thread).item.dgradcy(ii+1)=
>      &        workspace(my_thread).item.dgradcy(ii+1)+gg3(2)*wstrain
>             workspace(my_thread).item.dgradcz(ii+1)=
>      &        workspace(my_thread).item.dgradcz(ii+1)+gg3(3)*wstrain
694a704
> 
695a706,708
>             gg1(:)=0.0d0
>             gg2(:)=0.0d0
>             gg3(:)=0.0d0
698,702c711,718
<                 ghpbdc(k,jj-1)=ghpbdc(k,jj-1)+dhpos_dc(l,k,1,jj,itj)
<      &                *ggg(l)
<                 ghpbdc(k,jj)=ghpbdc(k,jj)+dhpos_dc(l,k,2,jj,itj)*ggg(l)
<                 ghpbdc(k,jj+1)=ghpbdc(k,jj+1)+dhpos_dc(l,k,3,jj,itj)
<      &                *ggg(l)
---
> !               ghpbdc(k,jj-1)=ghpbdc(k,jj-1)+dhpos_dc(l,k,1,jj,itj)
> !    &                *ggg(l)
> !               ghpbdc(k,jj)=ghpbdc(k,jj)+dhpos_dc(l,k,2,jj,itj)*ggg(l)
> !               ghpbdc(k,jj+1)=ghpbdc(k,jj+1)+dhpos_dc(l,k,3,jj,itj)
> !    &                *ggg(l)
>                 gg1(k)=gg1(k)+dhpos_dc(l,k,1,jj,itj)*ggg(l)
>                 gg2(k)=gg2(k)+dhpos_dc(l,k,2,jj,itj)*ggg(l)
>                 gg3(k)=gg3(k)+dhpos_dc(l,k,3,jj,itj)*ggg(l)
704a721,740
>             workspace(my_thread).item.dgradcx(jj-1)=
>      &        workspace(my_thread).item.dgradcx(jj-1)+gg1(1)*wstrain
>             workspace(my_thread).item.dgradcy(jj-1)=
>      &        workspace(my_thread).item.dgradcy(jj-1)+gg1(2)*wstrain
>             workspace(my_thread).item.dgradcz(jj-1)=
>      &        workspace(my_thread).item.dgradcz(jj-1)+gg1(3)*wstrain
> 
>             workspace(my_thread).item.dgradcx(jj)=
>      &        workspace(my_thread).item.dgradcx(jj)+gg2(1)*wstrain
>             workspace(my_thread).item.dgradcy(jj)=
>      &        workspace(my_thread).item.dgradcy(jj)+gg2(2)*wstrain
>             workspace(my_thread).item.dgradcz(jj)=
>      &        workspace(my_thread).item.dgradcz(jj)+gg2(3)*wstrain
> 
>             workspace(my_thread).item.dgradcx(jj+1)=
>      &        workspace(my_thread).item.dgradcx(jj+1)+gg3(1)*wstrain
>             workspace(my_thread).item.dgradcy(jj+1)=
>      &        workspace(my_thread).item.dgradcy(jj+1)+gg3(2)*wstrain
>             workspace(my_thread).item.dgradcz(jj+1)=
>      &        workspace(my_thread).item.dgradcz(jj+1)+gg3(3)*wstrain
705a742
> 
706a744
>             aux=0.0d0
708,709c746,748
<               ghpbdphi(ii-1)=ghpbdphi(ii-1)-
<      &          dhpos_gamma(k,ii,iti)*ggg(k)
---
> !             ghpbdphi(ii-1)=ghpbdphi(ii-1)-
> !    &          dhpos_gamma(k,ii,iti)*ggg(k)
>               aux=aux-dhpos_gamma(k,ii,iti)*ggg(k)
710a750,751
>             workspace(my_thread).item.dgloc(ii-1)=
>      &        workspace(my_thread).item.dgloc(ii-1)+wstrain*aux
712a754
>             aux=0.0d0
714,715c756,758
<               ghpbdphi(jj-1)=ghpbdphi(jj-1)+
<      &          dhpos_gamma(k,jj,itj)*ggg(k)
---
> !             ghpbdphi(jj-1)=ghpbdphi(jj-1)+
> !    &          dhpos_gamma(k,jj,itj)*ggg(k)
>               aux=aux+dhpos_gamma(k,jj,itj)*ggg(k)
716a760,761
>             workspace(my_thread).item.dgloc(jj-1)=
>      &        workspace(my_thread).item.dgloc(jj-1)+wstrain*aux
720c765,769
<         endif
---
>         endif  ! if (ipeak(1,i).eq.ipeak(2,i)) ... else ...
> 
>       enddo  ! do i=link_start_peak,link_end_peak
> 
>       ehpb=ehpb+ehpb2
722,758d770
<       enddo
< c      do i=1,nres
< c        do j=1,3
< c          ghpbc(j,i)=ghpbc(j,i)*0.5d0
< c        enddo
< c      enddo
< c      do i=1,nres
< c        do j=1,3
< c          ghpbx(j,i)=ghpbx(j,i)*0.5d0
< c        enddo
< c      enddo
< c      do i=0,nres
< c        do j=1,3
< c          ghpbdc(j,i)=ghpbdc(j,i)*0.5d0
< c        enddo
< c      enddo
< c      do i=1,nres
< c        ghpbdphi(i)=ghpbdphi(i)*0.5d0
< c      enddo
<       if (ider_ave(1).ne.1 .and. dravdr_limit.gt.0.0d0) then
< #ifdef DEBUG
<       write (iout,*) "dravdr_max=",dravdr_max
< #endif
<         if (dravdr_max.gt.dravdr_limit) then
<           scal_force_temp=dravdr_limit/dravdr_max
< #define DEBUG
< #ifdef DEBUG
<           write(iout,*)nsteps(1)," Average distance forces re-rescaled",
<      &    dravdr_max,dravdr_limit,scal_force_temp
< #endif
< #undef DEBUG
<           ghpbc(:,:nres)=ghpbc(:,:nres)*scal_force_temp
<           ghpbx(:,:nres)=ghpbx(:,:nres)*scal_force_temp
<           ghpbdc(:,:nres)=ghpbdc(:,:nres)*scal_force_temp
<           ghpbdphi(:nres)=ghpbdphi(:nres)*scal_force_temp
<         endif
<       endif
765a778,779
> 
> !=======================================================================
767c781,797
<       do i=link_start,link_end
---
>       if (.not.dyn_ss) then
>         link_low=max(link_start,nss+1)
>       else
>         link_low=link_start
>       endif
> #ifdef EDISLOG
>       write(*,*)'ilink_start loop',link_low,link_end
> #endif
> !$OMP PARALLEL DO DEFAULT(SHARED)
> !$OMP& REDUCTION(+:ehpb3)
> !$OMP& PRIVATE(
> !$OMP&   j,k,ii,jj,iti,itj,iii,jjj,my_thread,
> !$OMP&   eij,dd,fac,ehpbi,xdis,expdis,aux,rdis,waga,ggg,
> !$OMP&   dgxi,dgxj,dgci,dgcj,dehpb
> !$OMP& )
>       !do i=link_start,link_end
>       do i=link_low,link_end
771a802,807
>         dgci=0.0d0
>         dgcj=0.0d0
>         dgxi=0.0d0
>         dgxj=0.0d0
>         dehpb=0.0d0
>         fac=0.0d0 !TODO: bo constr_type==13 nie ustawia fac
785,801d820
< C 24/11/03 AL: SS bridges handled separately because of introducing a specific
< C    distance and angle dependent SS bond potential.
< C        if (ii.gt.nres .and. iabs(itype(iii)).eq.1 .and.
< C     & iabs(itype(jjj)).eq.1) then
< cmc        if (ii.gt.nres .and. itype(iii).eq.1 .and. itype(jjj).eq.1) then
< C 18/07/06 MC: Use the convention that the first nss pairs are SS bonds
<         if (.not.dyn_ss .and. i.le.nss) then
< C 15/02/13 CC dynamic SSbond - additional check
<           if (ii.gt.nres .and. iabs(itype(iii)).eq.1 .and.
<      &        iabs(itype(jjj)).eq.1) then
<            call ssbond_ene(iii,jjj,eij)
<            ehpb=ehpb+eij
<          endif
< cd          write (iout,*) "eij",eij
< cd   &   ' waga=',waga,' fac=',fac
< !        else if (ii.gt.nres .and. jj.gt.nres) then
<         else
804,821c823,849
<           dd=dist(c(1,ii),c(1,jj))
<           if (irestr_type(i).eq.11) then
<             if (slope.eq.0.0d0) then
<               ehpbi=fordepth(i)!**4.0d0
<      &           *rlornmr1(dd,dhpb(i),dhpb1(i),forcon(i))
<               fac=fordepth(i)!**4.0d0
<      &           *rlornmr1prim(dd,dhpb(i),dhpb1(i),forcon(i))/dd
<             else
<               ehpbi=fordepth(i)!**4.0d0
<      &           *rlornmr2(dd,dhpb(i),dhpb1(i),forcon(i),slope)
<               fac=fordepth(i)!**4.0d0
<      &           *rlornmr2prim(dd,dhpb(i),dhpb1(i),forcon(i),slope)/dd
<             endif
<             ehpb=ehpb+ehpbi
<             if (energy_dec) write (iout,'(a6,2i5,6f10.3,i5)')
<      &        "edisL",ii,jj,dd,dhpb(i),dhpb1(i),forcon(i),fordepth(i),
<      &        ehpbi,irestr_type(i)
<           else if (irestr_type(i).eq.10) then
---
>         dd=dist(c(1,ii),c(1,jj))
> #ifdef EDISLOG
>         write(*,'(a,2i5,a,i4,1f18.12)')'EDISe',ii,jj,' 2',
>      &    irestr_type(i),dd
> #endif
>         if (irestr_type(i).eq.11) then
>           if (slope.eq.0.0d0) then
>             ehpbi=fordepth(i)!**4.0d0
>      &         *rlornmr1(dd,dhpb(i),dhpb1(i),forcon(i))
>             fac=fordepth(i)!**4.0d0
>      &         *rlornmr1prim(dd,dhpb(i),dhpb1(i),forcon(i))/dd
>           else
>             ehpbi=fordepth(i)!**4.0d0
>      &         *rlornmr2(dd,dhpb(i),dhpb1(i),forcon(i),slope)
>             fac=fordepth(i)!**4.0d0
>      &         *rlornmr2prim(dd,dhpb(i),dhpb1(i),forcon(i),slope)/dd
>           endif
> #ifdef EDISLOG
>           write(*,'(a,2i5,a,4f18.12)')'EDISe',ii,jj,' b',ehpbi,
>      &      fac,1.0d0/dd,slope
> #endif
>           !ehpb=ehpb+ehpbi
>           dehpb=dehpb+ehpbi
>           if (energy_dec) write (iout,'(a6,2i5,6f10.3,i5)')
>      &      "edisL",ii,jj,dd,dhpb(i),dhpb1(i),forcon(i),fordepth(i),
>      &      ehpbi,irestr_type(i)
>         else if (irestr_type(i).eq.10) then
823,841c851,882
<             xdis = 0.5d0*(dd/forcon(i))**2
<             expdis = dexp(-xdis)
< c            aux=(dhpb(i)+dhpb1(i)*xdis)*expdis+fordepth(i)
<             aux=(dhpb(i)+dhpb1(i)*xdis*xdis)*expdis+fordepth(i)
< c            write (iout,*)"HERE: xdis",xdis," expdis",expdis," aux",aux,
< c     &          " wboltzd",wboltzd
<             ehpb=ehpb-wboltzd*xlscore(i)*dlog(aux)
< c            fac=-wboltzd*(dhpb1(i)*(1.0d0-xdis)-dhpb(i))
<             fac=-wboltzd*xlscore(i)*(dhpb1(i)*(2.0d0-xdis)*xdis-dhpb(i))
<      &           *expdis/(aux*forcon(i)**2)
<             if (energy_dec) write(iout,'(a6,2i5,8f15.8,i5)') 
<      &        "edisX",ii,jj,dd,dhpb(i),dhpb1(i),forcon(i),fordepth(i),
<      &        xlscore(i),wboltzd,-wboltzd*xlscore(i)*dlog(aux),
<      &       irestr_type(i)
<           else if (irestr_type(i).eq.13) then
< c            write (iout,*) "edis:",i,ihpb(i),jhpb(i),irestr_type(i)
<             call Xlinkene(ihpb(i),jhpb(i),ibecarb(i),irestr_type(i),eij)
<             ehpb=ehpb+eij
<           else if (irestr_type(i).eq.2) then
---
>           xdis = 0.5d0*(dd/forcon(i))**2
>           expdis = dexp(-xdis)
> c          aux=(dhpb(i)+dhpb1(i)*xdis)*expdis+fordepth(i)
>           aux=(dhpb(i)+dhpb1(i)*xdis*xdis)*expdis+fordepth(i)
> c          write (iout,*)"HERE: xdis",xdis," expdis",expdis," aux",aux,
> c     &        " wboltzd",wboltzd
>           !ehpb=ehpb-wboltzd*xlscore(i)*dlog(aux)
>           dehpb=dehpb-wboltzd*xlscore(i)*dlog(aux)
> c          fac=-wboltzd*(dhpb1(i)*(1.0d0-xdis)-dhpb(i))
>           fac=-wboltzd*xlscore(i)*(dhpb1(i)*(2.0d0-xdis)*xdis-dhpb(i))
>      &         *expdis/(aux*forcon(i)**2)
> #ifdef EDISLOG
>           write(*,'(a,2i5,a,4f18.12)')'EDISe',ii,jj,' d',
>      &      -wboltzd*xlscore(i)*dlog(aux),fac,xdis,aux
> #endif
>           if (energy_dec) write(iout,'(a6,2i5,8f15.8,i5)') 
>      &      "edisX",ii,jj,dd,dhpb(i),dhpb1(i),forcon(i),fordepth(i),
>      &      xlscore(i),wboltzd,-wboltzd*xlscore(i)*dlog(aux),
>      &     irestr_type(i)
>         else if (irestr_type(i).eq.13) then
> c          write (iout,*) "edis:",i,ihpb(i),jhpb(i),irestr_type(i)
>           ! updates ghpbc, ghpbx
>           !call Xlinkene(ihpb(i),jhpb(i),ibecarb(i),irestr_type(i),eij)
> !         call Xlinkene(ii,jj,ibecarb(i),irestr_type(i),eij)
>           call Xlinkene2(ihpb(i),jhpb(i),ibecarb(i),irestr_type(i),
>      &      eij,dgci,dgcj,dgxi,dgxj)
> #ifdef EDISLOG
>           write(*,'(a,2i5,a,1f18.12)')'EDISe',ii,jj,' e',eij
> #endif
>           !ehpb=ehpb+eij
>           dehpb=dehpb+eij
>         else if (irestr_type(i).eq.2) then
843,849c884,893
< c-------------------21/3/2023---Co implemented amber well potential-----
<             ehpbi=forcon(i)*gnmr1(dd,dhpb(i),dhpb1(i))
<             fac=forcon(i)*gnmr1prim(dd,dhpb(i),dhpb1(i))/dd
<             ehpb=ehpb+ehpbi
<             if (energy_dec) write(iout,'(a6,2i5,5f10.3,i5)') 
<      &      "edisQ",ii,jj,dd,dhpb(i),dhpb1(i),forcon(i),ehpbi
<           else
---
>           ehpbi=forcon(i)*gnmr1(dd,dhpb(i),dhpb1(i))
>           fac=forcon(i)*gnmr1prim(dd,dhpb(i),dhpb1(i))/dd
> #ifdef EDISLOG
>           write(*,'(a,2i5,a,2f18.12)')'EDISe',ii,jj,' f',ehpbi,fac
> #endif
>           !ehpb=ehpb+ehpbi
>           dehpb=dehpb+ehpbi
>           if (energy_dec) write(iout,'(a6,2i5,5f10.3,i5)') 
>      &    "edisQ",ii,jj,dd,dhpb(i),dhpb1(i),forcon(i),ehpbi
>         else
851c895
<             rdis=dd-dhpb(i)
---
>           rdis=dd-dhpb(i)
853c897
<             waga=forcon(i)
---
>           waga=forcon(i)
855,858c899,903
<             ehpb=ehpb+0.5d0*waga*rdis*rdis
<             if (energy_dec) write(iout,'(a6,2i5,5f10.3,i5)') 
<      &      "edisS",ii,jj,dd,dhpb(i),dhpb1(i),forcon(i),
<      &       0.5d0*waga*rdis*rdis,irestr_type(i)
---
>           !ehpb=ehpb+0.5d0*waga*rdis*rdis
>           dehpb=dehpb+0.5d0*waga*rdis*rdis
>           if (energy_dec) write(iout,'(a6,2i5,5f10.3,i5)') 
>      &    "edisS",ii,jj,dd,dhpb(i),dhpb1(i),forcon(i),
>      &     0.5d0*waga*rdis*rdis,irestr_type(i)
862,863c907,912
<             fac=waga*rdis/dd
<           endif
---
>           fac=waga*rdis/dd
> #ifdef EDISLOG
>           write(*,'(a,2i5,a,2f18.12)')'EDISe',ii,jj,' g',
>      &      0.5d0*waga*rdis*rdis,fac
> #endif
>         endif
865,867c914,919
<           do j=1,3
<             ggg(j)=fac*(c(j,jj)-c(j,ii))
<           enddo
---
>         do j=1,3
>           ggg(j)=fac*(c(j,jj)-c(j,ii))
>         enddo
> #ifdef EDISLOG
>         write(*,'(a,2i5,a,3f18.12)')'EDISe',ii,jj,' G',ggg
> #endif
871,883c923,932
<           if (iii.lt.ii) then
<             do j=1,3
<               ghpbx(j,iii)=ghpbx(j,iii)-ggg(j)
<             enddo
<           endif
<           if (jjj.lt.jj) then
<             do j=1,3
<               ghpbx(j,jjj)=ghpbx(j,jjj)+ggg(j)
<             enddo
<           endif
<           do k=1,3
<             ghpbc(k,jjj)=ghpbc(k,jjj)+ggg(k)
<             ghpbc(k,iii)=ghpbc(k,iii)-ggg(k)
---
>         if (iii.lt.ii) then
>           do j=1,3
>             !ghpbx(j,iii)=ghpbx(j,iii)-ggg(j)
>             dgxi(j)=dgxi(j)-ggg(j)
>           enddo
>         endif
>         if (jjj.lt.jj) then
>           do j=1,3
>             !ghpbx(j,jjj)=ghpbx(j,jjj)+ggg(j)
>             dgxj(j)=dgxj(j)+ggg(j)
885a935,991
>         do k=1,3
>           !ghpbc(k,jjj)=ghpbc(k,jjj)+ggg(k)
>           dgcj(k)=dgcj(k)+ggg(k)
>           !ghpbc(k,iii)=ghpbc(k,iii)-ggg(k)
>           dgci(k)=dgci(k)-ggg(k)
>         enddo
> 
>         ehpb3=ehpb3+dehpb
> 
> #ifdef EDISLOG
>         write(*,'(a,4i5,a,3f18.12)')'EDISe',ii,jj,iii,jjj,' W',dgci(:)
>         write(*,'(a,4i5,a,3f18.12)')'EDISe',ii,jj,iii,jjj,' X',dgcj(:)
>         write(*,'(a,4i5,a,3f18.12)')'EDISe',ii,jj,iii,jjj,' Y',dgxi(:)
>         write(*,'(a,4i5,a,3f18.12)')'EDISe',ii,jj,iii,jjj,' Z',dgxj(:)
> #endif
>         
> !        ghpbc(1,iii)=ghpbc(1,iii)+dgci(1)
> !        ghpbc(2,iii)=ghpbc(2,iii)+dgci(2)
> !        ghpbc(3,iii)=ghpbc(3,iii)+dgci(3)
>         my_thread=omp_get_thread_num()+1
>         workspace(my_thread).item.dgradbufcx(iii)=
>      &    workspace(my_thread).item.dgradbufcx(iii)+wstrain*dgci(1)
>         workspace(my_thread).item.dgradbufcy(iii)=
>      &    workspace(my_thread).item.dgradbufcy(iii)+wstrain*dgci(2)
>         workspace(my_thread).item.dgradbufcz(iii)=
>      &    workspace(my_thread).item.dgradbufcz(iii)+wstrain*dgci(3)
>         
> !        ghpbc(1,jjj)=ghpbc(1,jjj)+dgcj(1)
> !        ghpbc(2,jjj)=ghpbc(2,jjj)+dgcj(2)
> !        ghpbc(3,jjj)=ghpbc(3,jjj)+dgcj(3)
>         workspace(my_thread).item.dgradbufcx(jjj)=
>      &    workspace(my_thread).item.dgradbufcx(jjj)+wstrain*dgcj(1)
>         workspace(my_thread).item.dgradbufcy(jjj)=
>      &    workspace(my_thread).item.dgradbufcy(jjj)+wstrain*dgcj(2)
>         workspace(my_thread).item.dgradbufcz(jjj)=
>      &    workspace(my_thread).item.dgradbufcz(jjj)+wstrain*dgcj(3)
>         
> !        ghpbx(1,iii)=ghpbx(1,iii)+dgxi(1)
> !        ghpbx(2,iii)=ghpbx(2,iii)+dgxi(2)
> !        ghpbx(3,iii)=ghpbx(3,iii)+dgxi(3)
>         my_thread=omp_get_thread_num()+1
>         workspace(my_thread).item.dgradxx(iii)=
>      &    workspace(my_thread).item.dgradxx(iii)+dgxi(1)*wstrain
>         workspace(my_thread).item.dgradxy(iii)=
>      &    workspace(my_thread).item.dgradxy(iii)+dgxi(2)*wstrain
>         workspace(my_thread).item.dgradxz(iii)=
>      &    workspace(my_thread).item.dgradxz(iii)+dgxi(3)*wstrain
>         
> !        ghpbx(1,jjj)=ghpbx(1,jjj)+dgxj(1)
> !        ghpbx(2,jjj)=ghpbx(2,jjj)+dgxj(2)
> !        ghpbx(3,jjj)=ghpbx(3,jjj)+dgxj(3)
>         workspace(my_thread).item.dgradxx(jjj)=
>      &    workspace(my_thread).item.dgradxx(jjj)+dgxj(1)*wstrain
>         workspace(my_thread).item.dgradxy(jjj)=
>      &    workspace(my_thread).item.dgradxy(jjj)+dgxj(2)*wstrain
>         workspace(my_thread).item.dgradxz(jjj)=
>      &    workspace(my_thread).item.dgradxz(jjj)+dgxj(3)*wstrain
887,893c993,994
< #ifdef TIMING_ENE
< #ifdef MPI
<       time_edis=time_edis+MPI_Wtime()-time00
< #else 
<       time_edis=time_edis+tcpu()-time00
< #endif
< #endif
---
> 
>       ehpb=ehpb+ehpb3
895,949d995
<       return
<       end
< c----------------------------------------------------------------------------
<       double precision function scaltau(tau,ndiv_tau,idiv_tau)
<       integer nstep,ndiv_tau,idiv_tau
<       double precision tau
<       double precision scal_tau
<       if (idiv_tau.gt.ndiv_tau) then
<         scal_tau=1.0d0
<       else
<         scal_tau=idiv_tau/(ndiv_tau+0.0d0) 
<       endif
<       scaltau=scal_tau
<       return
<       end
< c----------------------------------------------------------------------------
<       subroutine force_scale(itrestr)
<       implicit none
<       integer itrestr
<       include "DIMENSIONS"
<       include "COMMON.MD"
<       include "COMMON.NMR"
<       include "COMMON.AVNLOC"
<       include "COMMON.RESTARTED"
<       include "COMMON.IOUNITS"
<       double precision scal_fac
<       double precision scaltau
<       external scaltau
<       if (ider_ave(itrestr).eq.1) then 
<         scal_force(itrestr)=1.0
<       else 
< c
< c Scaling tau
< c
<         if (nsteps(itrestr).eq.0) then
<           scal_force(itrestr)=1.0
<           idiv_tau(itrestr)=0
<         endif
<         if (mod(nsteps(itrestr),it_interval).eq.0) then
<           if (idiv_tau(itrestr).gt.0) 
<      &      scal_force(itrestr)=tau_temp(itrestr)/(d_time0*NAVINT)
<      &       *(1.0d0-exp(-1.0d0))
<           idiv_tau(itrestr)=idiv_tau(itrestr)+1
<           scal_fac = scaltau(tau,ndiv_tau,idiv_tau(itrestr))
<           tau_temp(itrestr) = tau*scal_fac
< #ifdef DEBUG
<           write(iout,"(2(a,1x,i10),1x,2(a,f12.5,1x))")
<      &    "nsteps",nsteps(itrestr)," idiv_tau",idiv_tau(itrestr),
<      &    " scal_fac",scal_fac,
<      &    " tau_temp",tau_temp(itrestr)," scal_force",
<      &     scal_force(itrestr)
< c          write (iout,*) "NAVINT",navint
< #endif
<         endif
<       endif
edis-stara.F
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/edis-stara.F: No such file or directory
eelec.F
32a33
>       include 'COMMON.OMP'
48d48
< 
49a50
> 
62a64
>       max_threads_used=1
83a86
> 
139,140c142,145
<         call eelecij(eelecij_work_turn3,eelecij_work_size_turn3,2,
<      &               threads_used,scal_el)
---
> !       call eelecij(eelecij_work_turn3,eelecij_work_size_turn3,2,
> !    &               threads_used,scal_el)
>         call eelecij_delta(eelecij_blocks_2,num_eelecij_blocks_2,2,
>      &                     threads_used,scal_el)
142,143c147,150
<         call eelecij_nowl(eelecij_work_turn3,eelecij_work_size_turn3,2,
<      &                    threads_used,scal_el)
---
> !       call eelecij_nowl(eelecij_work_turn3,eelecij_work_size_turn3,2,
> !    &                    threads_used,scal_el)
>         call eelecij_delta_nowl(eelecij_blocks_2,num_eelecij_blocks_2,2,
>      &                          threads_used,scal_el)
165,166c172,175
<         call eelecij(eelecij_work_turn4,eelecij_work_size_turn4,3,
<      &               threads_used,1.0d0)
---
> !       call eelecij(eelecij_work_turn4,eelecij_work_size_turn4,3,
> !    &               threads_used,1.0d0)
>         call eelecij_delta(eelecij_blocks_3,num_eelecij_blocks_3,3,
>      &                     threads_used,1.0d0)
168,169c177,180
<         call eelecij_nowl(eelecij_work_turn4,eelecij_work_size_turn4,3,
<      &                    threads_used,1.0d0)
---
> !       call eelecij_nowl(eelecij_work_turn4,eelecij_work_size_turn4,3,
> !    &                    threads_used,1.0d0)
>         call eelecij_delta_nowl(eelecij_blocks_3,num_eelecij_blocks_3,3,
>      &                          threads_used,1.0d0)
176c187
<         call eelecij2(threads_used)
---
>         call eelecij(threads_used)
178c189
<         call eelecij2_nowl(threads_used)
---
>         call eelecij_nowl(threads_used)
183a195,197
> 
>       eelec_group_results=.true.
> 
191c205
< C-------------------------------------------------------------------------------
---
> C-----------------------------------------------------------------------
199c213
< C-------------------------------------------------------------------------------
---
> C-----------------------------------------------------------------------
209c223
< C-------------------------------------------------------------------------------
---
> C-----------------------------------------------------------------------
216c230
< C-------------------------------------------------------------------------------
---
> C-----------------------------------------------------------------------
231c245
< C-------------------------------------------------------------------------------
---
> C-----------------------------------------------------------------------
265a280,291
> !     if (i.lt.j-2) then
> !     write(*,'(a,2i4,3f18.12)')'AGGIJ0i',i,j,cp(:,i)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJ0j',i,j,cp(:,j)
> !     write(*,'(a,2i4,2i4)')'AGGIJ1 ',i,j,iteli,itelj
> !     write(*,'(a,2i4,3f18.12)')'AGGIJ2 ',i,j,rij,rmij,fac
> !     write(*,'(a,2i4,3f18.12)')'AGGIJ3 ',i,j,erij(:)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJ3a',i,j,uy(:,2,i)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJ3b',i,j,uy(:,1,j)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJ3c',i,j,uz(:,i)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJ3d',i,j,uz(:,j-1)
> !     endif
> 
277a304,309
> !     if (i.lt.j-2) then
> !     write(*,'(a,2i4,4f18.12)')'AGGIJ4 ',i,j,ury,vry,urz,vrz
> !     write(*,'(a,2i4,4f18.12)')'AGGIJ5 ',i,j,
> !    &  uryvry,uryvrz,urzvry,urzvrz
> !     write(*,'(a,2i4,4f18.12)')'AGGIJ6 ',i,j,a22,a23,a32,a33
> !     endif
279a312,314
> !     if (i.lt.j-2) then
> !     write(*,'(a,2i4,3f18.12)')'AGGIJ7 ',i,j,ggg
> !     endif
291a327,332
> !     if (i.lt.j-2) then
> !     write(*,'(a,2i4,3f18.12)')'AGGIJ81',i,j,agg(:,1)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJ82',i,j,agg(:,2)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJ83',i,j,agg(:,3)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJ84',i,j,agg(:,4)
> !     endif
306a348,353
> !     if (i.lt.j-2) then
> !     write(*,'(a,2i4,3f18.12)')'AGGIJ91',i,j,uyjvry(:)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJ92',i,j,uzjvrz(:)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJ93',i,j,uyiury(:)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJ94',i,j,uziurz(:)
> !     endif
328a376,403
> !     if (i.lt.j-2) then
> !     write(*,'(a,2i4,3f18.12)')'AGGIJA1',i,j,aggi(:,1)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJA2',i,j,aggi(:,2)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJA3',i,j,aggi(:,3)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJA4',i,j,aggi(:,4)
> 
> !     write(*,'(a,2i4,3f18.12)')'AGGIJB1',i,j,aggi1(:,1)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJB2',i,j,aggi1(:,2)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJB3',i,j,aggi1(:,3)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJB4',i,j,aggi1(:,4)
> 
> !     write(*,'(a,2i4,3f18.12)')'AGGIJC1',i,j,aggj(:,1)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJC2',i,j,aggj(:,2)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJC3',i,j,aggj(:,3)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJC4',i,j,aggj(:,4)
> 
> !     write(*,'(a,2i4,3f18.12)')'AGGIJD1',i,j,aggj1(:,1)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJD2',i,j,aggj1(:,2)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJD3',i,j,aggj1(:,3)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJD4',i,j,aggj1(:,4)
> 
> !     write(*,'(a,2i4,3f18.12)')'AGGIJE1',i,j,uygrad(:,1,1,1,j)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJE2',i,j,uygrad(:,2,1,1,j)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJE3',i,j,uygrad(:,3,1,1,j)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJF1',i,j,uzgrad(:,1,1,j-1)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJF2',i,j,uzgrad(:,2,1,j-1)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJF3',i,j,uzgrad(:,3,1,j-1)
> !     endif
333a409
>       ! jedak to, bo inaczej energia jest niestabilna
335a412,433
> !     if (i.lt.j-2) then
> !     write(*,'(a,2i4,3f18.12)')'AGGIJV1',i,j,aggi(:,1)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJV2',i,j,aggi(:,2)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJV3',i,j,aggi(:,3)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJV4',i,j,aggi(:,4)
> 
> !     write(*,'(a,2i4,3f18.12)')'AGGIJW1',i,j,aggi1(:,1)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJW2',i,j,aggi1(:,2)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJW3',i,j,aggi1(:,3)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJW4',i,j,aggi1(:,4)
> 
> !     write(*,'(a,2i4,3f18.12)')'AGGIJX1',i,j,aggj(:,1)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJX2',i,j,aggj(:,2)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJX3',i,j,aggj(:,3)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJX4',i,j,aggj(:,4)
> 
> !     write(*,'(a,2i4,3f18.12)')'AGGIJY1',i,j,aggj1(:,1)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJY2',i,j,aggj1(:,2)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJY3',i,j,aggj1(:,3)
> !     write(*,'(a,2i4,3f18.12)')'AGGIJY4',i,j,aggj1(:,4)
> !     endif
> 
339a438,441
> !     if (i.lt.j-2) then
> !     write(*,'(a,2i4,4f18.12)')'AGGIJZ ',i,j,
> !    &  axx(1,1),axx(1,2),axx(2,1),axx(2,2)
> !     endif
349,683c451
< C-------------------------------------------------------------------------------
< !      subroutine eelecij(eelecij_work,num_items,offset,
< !     &                   threads_used,scal_el)
< !#ifdef _OPENMP
< !      use omp_lib
< !#endif
< !      implicit none
< !      include 'DIMENSIONS'
< !      include 'COMMON.CHAIN'
< !      include 'COMMON.SPLITELE'
< !#ifdef SHIELD
< !      include 'COMMON.EELEC.SHIELD'
< !#else
< !      include 'COMMON.EELEC'
< !#endif
< !      include 'COMMON.LIPID'
< !      integer num_items,threads_used,offset
< !      double precision xyzj(3),rij,scal_el
< !      integer my_thread,range_lo,range_hi,from,to
< !      integer lasti,lastj,i,j,item,seq_idx,nseq_idx
< !      record /eelecij_work_chunk/ seq_work
< !      record /eelecij_work_chunk/ nseq_work
< !      !record /eelecij_workitem/ eelecij_work(num_items)
< !      integer eelecij_work(num_items)
< !      record /eelec_thread_data/ th_data
< !      logical full
< !      double precision scalar
< !
< !      !range_lo=1
< !      !range_hi=num_items
< !
< !#ifdef _OPENMP
< !      threads_used=omp_get_max_threads()
< !      ! ensure at least 4 elements per thread to avoid overlap
< !      !if(range_hi-range_lo+1 .lt. 4*threads_used) then
< !      !  threads_used=1
< !      !  my_thread=1
< !      !endif
< !#else
< !      threads_used=1
< !#endif
< !!$OMP PARALLEL
< !!$OMP& NUM_THREADS(threads_used) DEFAULT(SHARED)
< !!$OMP& PRIVATE(
< !!$OMP&   i,j,xyzj,rij,lasti,lastj,
< !!$OMP&   seq_work,seq_idx,nseq_work,nseq_idx,full,
< !!$OMP&   my_thread,item,from,to,th_data
< !!$OMP& )
< !#ifdef _OPENMP
< !      my_thread=omp_get_thread_num()+1
< !#else
< !      my_thread=1
< !#endif
< !      call eelec_fill_thread_data(th_data)
< !
< !      !from=range_lo+((range_hi-range_lo)/threads_used)*(my_thread-1)
< !      !to=range_lo+((range_hi-range_lo)/threads_used)*my_thread-1
< !      !if(my_thread.eq.threads_used) to=range_hi
< !      call split_work_for_threads(from,to,1,num_items,
< !     &                            my_thread,threads_used)
< !
< !
< !      seq_idx=0
< !      nseq_idx=0
< !      lasti=0
< !      lastj=0
< !
< !      do item=from,to
< !        !i=eelecij_work(item).i
< !        !j=eelecij_work(item).j
< !        i=eelecij_work(item)
< !        j=i+offset
< !        LOG_STAT(300,1)
< !        call wrapvec(cp(:,j)-cp(:,i),xyzj)
< !        rij=scalar(xyzj,xyzj)
< !        if(rij.ge.r_cut_int_sq) cycle
< !        LOG_STAT(301,1)
< !
< !        if(i.ne.lasti) then
< !          if(seq_idx.gt.0) then
< !!DIR$ NOINLINE
< !            call eelecij_do_work_seq(seq_work,lasti,
< !     &                               seq_work.j(1),
< !     &                               seq_work.j(seq_idx),
< !     &                               eelecij_value(my_thread),
< !     &                               eelecij_array(my_thread),
< !     &                               th_data,scal_el)
< !            LOG_STAT(302,seq_idx)
< !            seq_idx=0
< !          endif
< !          if(nseq_idx.gt.0) then
< !!DIR$ NOINLINE
< !            call eelecij_do_work_nonseq(nseq_work,
< !     &                                  lasti,1,nseq_idx,
< !     &                                  eelecij_value(my_thread),
< !     &                                  eelecij_array(my_thread),
< !     &                                  th_data,scal_el)
< !            LOG_STAT(303,nseq_idx)
< !            nseq_idx=0
< !          endif
< !          lasti=i
< !          lastj=-1
< !        endif
< !
< !        if((j.eq.lastj+1).or.(seq_idx.eq.0)) then
< !          call eelec_queue_work(seq_work,seq_idx,i,j,
< !     &                          xyzj,rij,full)
< !          lastj=j
< !          if(full) then
< !!DIR$ NOINLINE
< !            call eelecij_do_work_seq(seq_work,lasti,
< !     &                               seq_work.j(1),
< !     &                               seq_work.j(seq_idx),
< !     &                               eelecij_value(my_thread),
< !     &                               eelecij_array(my_thread),
< !     &                               th_data,scal_el)
< !            LOG_STAT(302,seq_idx)
< !            seq_idx=0
< !            lastj=-1
< !          endif
< !        else
< !          if((seq_idx.lt.simd_seq_cutoff) .and.
< !     &       (nseq_idx+seq_idx.lt.eelec_buffer_size)) then
< !            LOG_STAT(304,seq_idx)
< !            call eelecij_move_work(seq_work,seq_idx,
< !     &                             nseq_work,nseq_idx)
< !          else
< !!DIR$ NOINLINE
< !            call eelecij_do_work_seq(seq_work,lasti,
< !     &                               seq_work.j(1),
< !     &                               seq_work.j(seq_idx),
< !     &                               eelecij_value(my_thread),
< !     &                               eelecij_array(my_thread),
< !     &                               th_data,scal_el)
< !            LOG_STAT(302,seq_idx)
< !            seq_idx=0
< !          endif
< !          call eelec_queue_work(nseq_work,nseq_idx,i,j,
< !     &                          xyzj,rij,full)
< !          lastj=j
< !          if(full) then
< !!DIR$ NOINLINE
< !            call eelecij_do_work_nonseq(nseq_work,lasti,
< !     &                                  1,nseq_idx,
< !     &                                  eelecij_value(my_thread),
< !     &                                  eelecij_array(my_thread),
< !     &                                  th_data,scal_el)
< !            LOG_STAT(303,nseq_idx)
< !            nseq_idx=0
< !            lastj=-1
< !          endif
< !        endif
< !      enddo
< !
< !      if(seq_idx.gt.0) then
< !!DIR$ NOINLINE
< !        call eelecij_do_work_seq(seq_work,lasti,
< !     &                           seq_work.j(1),
< !     &                           seq_work.j(seq_idx),
< !     &                           eelecij_value(my_thread),
< !     &                           eelecij_array(my_thread),
< !     &                           th_data,scal_el)
< !        LOG_STAT(302,seq_idx)
< !      endif
< !      if(nseq_idx.gt.0) then
< !!DIR$ NOINLINE
< !        call eelecij_do_work_nonseq(nseq_work,lasti,
< !     &                              1,nseq_idx,
< !     &                              eelecij_value(my_thread),
< !     &                              eelecij_array(my_thread),
< !     &                              th_data,scal_el)
< !        LOG_STAT(303,nseq_idx)
< !      endif
< !!$OMP END PARALLEL
< !      return
< !      end
< !C-------------------------------------------------------------------------------
< !      subroutine eelecij2(threads_used)
< !#ifdef _OPENMP
< !      use omp_lib
< !#endif
< !      implicit none
< !      include 'DIMENSIONS'
< !      include 'COMMON.CHAIN'
< !      include 'COMMON.INTERACT'
< !      include 'COMMON.SPLITELE'
< !#ifdef SHIELD
< !      include 'COMMON.EELEC.SHIELD'
< !#else
< !      include 'COMMON.EELEC'
< !#endif
< !      include 'COMMON.LIPID'
< !      integer threads_used
< !      integer from_blk,to_blk,from_j,to_j
< !      double precision xyzj(3),rij
< !      integer my_thread,from,to
< !      integer lastj,i,j,ikont,jblock,item,seq_idx,nseq_idx
< !      integer from_ik,to_ik,first_blk,last_blk,first_j,last_j
< !      record /eelecij_work_chunk/ seq_work
< !      record /eelecij_work_chunk/ nseq_work
< !      record /eelec_thread_data/ th_data
< !      logical full,limited
< !      double precision scalar
< !
< !      threads_used = pp_threads
< !
< !!$OMP PARALLEL IF(threads_used.gt.1)
< !!$OMP& NUM_THREADS(threads_used) DEFAULT(SHARED)
< !!$OMP& PRIVATE(
< !!$OMP&   i,j,ikont,jblock,xyzj,rij,lastj,from_j,to_j,
< !!$OMP&   seq_work,seq_idx,nseq_work,nseq_idx,full,
< !!$OMP&   from_ik,to_ik,first_blk,last_blk,first_j,last_j,
< !!$OMP&   my_thread,item,from,to,from_blk,to_blk,th_data
< !!$OMP& )
< !#ifdef _OPENMP
< !      my_thread=omp_get_thread_num()+1
< !#else
< !      my_thread=1
< !#endif
< !      call eelec_fill_thread_data(th_data)
< !
< !      seq_idx=0
< !      nseq_idx=0
< !      lastj=0
< !
< !      from_ik=pp_from_ik(my_thread)
< !      to_ik=pp_to_ik(my_thread)
< !      first_blk=pp_first_blk(my_thread)
< !      last_blk=pp_last_blk(my_thread)
< !      first_j=pp_first_j(my_thread)
< !      last_j=pp_last_j(my_thread)
< !
< !      do ikont=from_ik,to_ik
< !        i=newcontlistppi(1,ikont)
< !        from_blk=newcontlistppi(2,ikont-1)+1
< !        to_blk=newcontlistppi(2,ikont)
< !
< !        if(ikont.eq.from_ik) from_blk=first_blk
< !        if(ikont.eq.to_ik) to_blk=last_blk
< !
< !        lastj=-1
< !
< !        do jblock=from_blk,to_blk
< !          from_j=newcontlistppj(1,jblock)
< !          to_j=newcontlistppj(2,jblock)
< !          if((ikont.eq.from_ik).and.(jblock.eq.from_blk)) from_j=first_j
< !          if((ikont.eq.to_ik).and.(jblock.eq.to_blk)) to_j=last_j
< !
< !          do j=from_j,to_j
< !            LOG_STAT(300,1)
< !            call wrapvec(cp(:,j)-cp(:,i),xyzj)
< !            rij=scalar(xyzj,xyzj)
< !            if(rij.ge.r_cut_int_sq) cycle
< !            LOG_STAT(301,1)
< !
< !            if((j.eq.lastj+1).or.(seq_idx.eq.0)) then
< !              call eelec_queue_work(seq_work,seq_idx,i,j,
< !     &                              xyzj,rij,full)
< !              lastj=j
< !              if(full) then
< !!DIR$ NOINLINE
< !                call eelecij_do_work_seq(seq_work,i,
< !     &                                   seq_work.j(1),
< !     &                                   seq_work.j(seq_idx),
< !     &                                   eelecij_value(my_thread),
< !     &                                   eelecij_array(my_thread),
< !     &                                   th_data,1.0d0)
< !                LOG_STAT(302,seq_idx)
< !                seq_idx=0
< !                lastj=-1
< !              endif
< !            else
< !              if((seq_idx.lt.simd_seq_cutoff) .and.
< !     &           (nseq_idx+seq_idx.lt.eelec_buffer_size)) then
< !                LOG_STAT(304,seq_idx)
< !                call eelecij_move_work(seq_work,seq_idx,
< !     &                                 nseq_work,nseq_idx)
< !              else
< !!DIR$ NOINLINE
< !                call eelecij_do_work_seq(seq_work,i,
< !     &                                   seq_work.j(1),
< !     &                                   seq_work.j(seq_idx),
< !     &                                   eelecij_value(my_thread),
< !     &                                   eelecij_array(my_thread),
< !     &                                   th_data,1.0d0)
< !                LOG_STAT(302,seq_idx)
< !                seq_idx=0
< !              endif
< !              call eelec_queue_work(nseq_work,nseq_idx,
< !     &                              i,j,xyzj,rij,
< !     &                              full)
< !              lastj=j
< !              if(full) then
< !!DIR$ NOINLINE
< !                call eelecij_do_work_nonseq(nseq_work,i,
< !     &                                      1,nseq_idx,
< !     &                                      eelecij_value(my_thread),
< !     &                                      eelecij_array(my_thread),
< !     &                                      th_data,1.0d0)
< !                LOG_STAT(303,nseq_idx)
< !                nseq_idx=0
< !                lastj=-1
< !              endif
< !            endif
< !          enddo  ! j
< !        enddo  ! jblock
< !
< !        if(seq_idx.gt.0) then
< !!DIR$ NOINLINE
< !          call eelecij_do_work_seq(seq_work,i,
< !     &                             seq_work.j(1),
< !     &                             seq_work.j(seq_idx),
< !     &                             eelecij_value(my_thread),
< !     &                             eelecij_array(my_thread),
< !     &                             th_data,1.0d0)
< !          LOG_STAT(302,seq_idx)
< !          seq_idx=0
< !        endif
< !        if(nseq_idx.gt.0) then
< !!DIR$ NOINLINE
< !          call eelecij_do_work_nonseq(nseq_work,i,
< !     &                                1,nseq_idx,
< !     &                                eelecij_value(my_thread),
< !     &                                eelecij_array(my_thread),
< !     &                                th_data,1.0d0)
< !          LOG_STAT(303,nseq_idx)
< !          nseq_idx=0
< !        endif
< !      enddo  ! ikont
< !!$OMP END PARALLEL
< !
< !      return
< !      end
< 
< C-----------------------------------------------------------------------------
---
> C-----------------------------------------------------------------------
699a468
>       include 'COMMON.OMP'
724c493,494
<       faclipij=(lipid_cache(i).ss+lipid_cache(j).ss)*0.5d0*lipscale
---
>       !faclipij=(lipid_cache(i).ss+lipid_cache(j).ss)*0.5d0*lipscale
>       faclipij=lipid_cache_ss(i)+lipid_cache_ss(j)
725a496
> !     write(*,'(a,1i4,1f18.12)')'ET3 1 ',i,faclipij
739a511
> !     write(*,'(a,1i4,4f18.12)')'ET3 2 ',i,auxmat(1,:),auxmat(2,:)
741a514
> !     write(*,'(a,i4,2f18.12)')'ET3 3 ',i,eello_t3,faclipij
748a522
> !     write(*,'(a,1i4,1f18.12)')'ET3 4 ',i,eello_t3*faclipij
756a531
> !     write(*,'(a,1i4,4f18.12)')'ET3 5 ',i,auxgmat1(1,:),auxgmat1(2,:)
763a539,540
> !     write(*,'(a,1i4,1f18.12)')'ET3 6 ',i,gloc_theta1
> 
765a543
> !     write(*,'(a,1i4,4f18.12)')'ET3 7 ',i,auxgmat2(1,:),auxgmat2(2,:)
773a552
> !     write(*,'(a,1i4,1f18.12)')'ET3 8 ',i,gloc_theta2
837a617,618
> !     write(*,'(a,1i4,4f18.12)')'ET3 9 ',i,gtcosEUg(1,:),gtcosEUg(2,:)
> !     write(*,'(a,1i4,4f18.12)')'ET3 A ',i,gtsinEUg(1,:),gtsinEUg(2,:)
846a628
> !     write(*,'(a,1i4,4f18.12)')'ET3 B ',i,auxmat2(1,:),auxmat2(2,:)
854a637,638
> !     write(*,'(a,1i4,1f18.12)')'ET3 C ',i,gel_loc_turn3_cos
> 
855a640
> !     write(*,'(a,1i4,4f18.12)')'ET3 D ',i,auxmat2(1,:),auxmat2(2,:)
863a649
> !     write(*,'(a,1i4,1f18.12)')'ET3 E ',i,gel_loc_turn3_sin
888a675,676
> !     write(*,'(a,2i4,1f18.12)')'ET3 F ',i,l,
> !    &    +trace_ae(a_temp,auxmat1)*faclipij
903a692,694
> !     write(*,'(a,2i4,1f18.12)')'ET3 G ',i,l,
> !    &    +trace_ae(a_temp,auxmat1)*faclipij
> 
917a709,711
> !     write(*,'(a,2i4,1f18.12)')'ET3 H ',i,l,
> !    &    +trace_ae(a_temp,auxmat1)*faclipij
> 
931a726,728
> !     write(*,'(a,2i4,1f18.12)')'ET3 I ',i,l,
> !    &    +trace_ae(a_temp,auxmat1)*faclipij
> 
935c732,733
<      &  lipid_cache(i).ssgrad*eello_t3/4.0d0*lipscale
---
> !    &  lipid_cache(i).ssgrad*eello_t3/4.0d0*lipscale
>      &  lipid_cache_ssgrad(i)*eello_t3*0.5d0
938c736,737
<      &  lipid_cache(j).ssgrad*eello_t3/4.0d0*lipscale
---
> !    &  lipid_cache(j).ssgrad*eello_t3/4.0d0*lipscale
>      &  lipid_cache_ssgrad(j)*eello_t3*0.5d0
941c740,741
<      &  lipid_cache(i).ssgrad*eello_t3/4.0d0*lipscale
---
> !    &  lipid_cache(i).ssgrad*eello_t3/4.0d0*lipscale
>      &  lipid_cache_ssgrad(i)*eello_t3*0.5d0
944c744,750
<      &  lipid_cache(j).ssgrad*eello_t3/4.0d0*lipscale
---
> !    &  lipid_cache(j).ssgrad*eello_t3/4.0d0*lipscale
>      &  lipid_cache_ssgrad(j)*eello_t3*0.5d0
> 
> !     write(*,'(a,1i4,2f18.12)')'ET3 J ',i,
> !    &  lipid_cache_ssgrad(i)*eello_t3*0.5d0,
> !    &  lipid_cache_ssgrad(j)*eello_t3*0.5d0
> 
988a795,811
> 
> !     write(*,'(a,1i4,3f18.12)')'ET3 K ',i,
> !    &  +gel_loc_turn3_cos*dcgs1s2tab(:,1,i+3)
> !    &  +gel_loc_turn3_sin*dsgs1s2tab(:,1,i+3)
> !    &  +gloc_theta1*dcosttab(:,1,i+2)
> 
> !     write(*,'(a,1i4,3f18.12)')'ET3 L ',i,
> !    & +gel_loc_turn3_cos*dcgs1s2tab(:,2,i+3)
> !    & +gel_loc_turn3_sin*dsgs1s2tab(:,2,i+3)
> !    & +gloc_theta1*dcosttab(:,2,i+2)
> !    & +gloc_theta2*dcosttab(:,1,i+3)
> 
> !     write(*,'(a,1i4,3f18.12)')'ET3 M ',i,
> !    & +gel_loc_turn3_cos*dcgs1s2tab(:,3,i+3)
> !    & +gel_loc_turn3_sin*dsgs1s2tab(:,3,i+3)
> !    & +gloc_theta2*dcosttab(:,2,i+3)
> 
999c822
< C-------------------------------------------------------------------------------
---
> C-----------------------------------------------------------------------
1015a839
>       include 'COMMON.OMP'
1041c865,866
<       faclipij=(lipid_cache(i).ss+lipid_cache(j).ss)*0.5d0*lipscale
---
>       !faclipij=(lipid_cache(i).ss+lipid_cache(j).ss)*0.5d0*lipscale
>       faclipij=lipid_cache_ss(i)+lipid_cache_ss(j)
1057a883,884
> !     write(*,'(a,i4,i4,3f18.12)')'ET41 ',i,j,
> !    &  faclipij,sint3inv,sinttab(i+3)
1067a895,901
> !     write(*,'(a,i4,4f18.12)')'ET41a',i,
> !    &  E3tuE2t(1,1),E3tuE2t(1,2),E3tuE2t(2,1),E3tuE2t(2,2)
> !     write(*,'(a,i4,4f18.12)')'ET41b',i,
> !    &  e3tue2tue1t(1,1),e3tue2tue1t(1,2),
> !    &  e3tue2tue1t(2,1),e3tue2tue1t(2,2)
> !     write(*,'(a,i4,4f18.12)')'ET41c',i,
> !    &  a_temp(1,1),a_temp(1,2),a_temp(2,1),a_temp(2,2)
1071a906
> !     write(*,'(a,i4,1f18.12)')'ET42 ',i,s3
1082a918,929
> !     write(*,'(a,i4,3f18.12)')'ET43 ',i,gsEE1,gsEE2,gsEE3
> !     write(*,'(a,i4,4f18.12)')'ET431',i,
> !    &  E2tuE1t(1,1),E2tuE1t(1,2),E2tuE1t(2,1),E2tuE1t(2,2)
> !     write(*,'(a,i4,4f18.12)')'ET43a',i,
> !    &  e3tue2tugte1t(1,1),e3tue2tugte1t(1,2),
> !    &  e3tue2tugte1t(2,1),e3tue2tugte1t(2,2)
> !     write(*,'(a,i4,4f18.12)')'ET43b',i,
> !    &  e3tugte2tue1t(1,1),e3tugte2tue1t(1,2),
> !    &  e3tugte2tue1t(2,1),e3tugte2tue1t(2,2)
> !     write(*,'(a,i4,4f18.12)')'ET43c',i,
> !    &  gte3tue2tue1t(1,1),gte3tue2tue1t(1,2),
> !    &  gte3tue2tue1t(2,1),gte3tue2tue1t(2,2)
1095a943,944
> !     write(*,'(a,i4,2f18.12)')'ET44 ',i,eello_t4,faclipij
> 
1156a1006,1007
> !     write(*,'(a,i4,3f18.12)')'ET45 ',i,
> !    &  gloc_theta1,gloc_theta2,gloc_theta3
1182a1034,1035
> !     write(*,'(a,i4,2f18.12)')'ET46 ',i,
> !    &  gel_loc_turn4_phicos1,gel_loc_turn4_phisin1
1210a1064,1065
> !     write(*,'(a,i4,2f18.12)')'ET47 ',i,
> !    &  gel_loc_turn4_phicos2,gel_loc_turn4_phisin2
1224a1080,1082
> !     write(*,'(a,i4,i4,1f18.12)')'ET481',i,l,
> !    &     -trace_ae(a_temp,e3tue2tue1t)*sint3inv
> !    &     *faclipij
1236a1095,1097
> !     write(*,'(a,i4,i4,1f18.12)')'ET482',i,l,
> !    &    -trace_ae(a_temp,e3tue2tue1t)*sint3inv
> !    &    *faclipij
1248a1110,1112
> !     write(*,'(a,i4,i4,1f18.12)')'ET483',i,l,
> !    &    -trace_ae(a_temp,e3tue2tue1t)*sint3inv
> !    &    *faclipij
1260a1125,1127
> !     write(*,'(a,i4,i4,1f18.12)')'ET484',i,l,
> !    &    -trace_ae(a_temp,e3tue2tue1t)*sint3inv
> !    &    *faclipij
1264c1131,1132
<      &    lipid_cache(i).ssgrad*eello_t4/4.0d0*lipscale
---
> !    &    lipid_cache(i).ssgrad*eello_t4/4.0d0*lipscale
>      &    lipid_cache_ssgrad(i)*eello_t4*0.5d0
1266c1134,1135
<      &    lipid_cache(j).ssgrad*eello_t4/4.0d0*lipscale
---
> !    &    lipid_cache(j).ssgrad*eello_t4/4.0d0*lipscale
>      &    lipid_cache_ssgrad(j)*eello_t4*0.5d0
1268c1137,1138
<      &    lipid_cache(i).ssgrad*eello_t4/4.0d0*lipscale
---
> !    &    lipid_cache(i).ssgrad*eello_t4/4.0d0*lipscale
>      &    lipid_cache_ssgrad(i)*eello_t4*0.5d0
1270c1140,1143
<      &    lipid_cache(j).ssgrad*eello_t4/4.0d0*lipscale
---
> !    &    lipid_cache(j).ssgrad*eello_t4/4.0d0*lipscale
>      &    lipid_cache_ssgrad(j)*eello_t4*0.5d0
> !     write(*,'(a,i4,3f18.12)')'ET49 ',i,
> !    &  lipid_cache_ssgrad(i),lipid_cache_ssgrad(j),eello_t4
1289a1163,1165
> 
> !     write(*,'(a,i4,3f18.12)')'ET4Y ',i,eello_t4_theta
> 
1311a1188,1210
> 
> !     write(*,'(a,i4,3f18.12)')'ET4Z1',i,
> !    & +gel_loc_turn4_phicos1*dcgs1s2tab(:,1,i+3)
> !    & +gel_loc_turn4_phisin1*dsgs1s2tab(:,1,i+3)
> !    & +gloc_theta1*dcosttab(:,1,i+2)
> !     write(*,'(a,i4,3f18.12)')'ET4Z2',i,
> !    & +gel_loc_turn4_phicos1*dcgs1s2tab(:,2,i+3)
> !    & +gel_loc_turn4_phisin1*dsgs1s2tab(:,2,i+3)
> !    & +gel_loc_turn4_phicos2*dcgs1s2tab(:,1,i+4)
> !    & +gel_loc_turn4_phisin2*dsgs1s2tab(:,1,i+4)
> !    & +gloc_theta1*dcosttab(:,2,i+2)
> !    & +(gloc_theta2+eello_t4_theta)*dcosttab(:,1,i+3)
> !     write(*,'(a,i4,3f18.12)')'ET4Z3',i,
> !    & +gel_loc_turn4_phicos1*dcgs1s2tab(:,3,i+3)
> !    & +gel_loc_turn4_phisin1*dsgs1s2tab(:,3,i+3)
> !    & +gel_loc_turn4_phicos2*dcgs1s2tab(:,2,i+4)
> !    & +gel_loc_turn4_phisin2*dsgs1s2tab(:,2,i+4)
> !    & +(gloc_theta2+eello_t4_theta)*dcosttab(:,2,i+3)
> !    & +gloc_theta3*dcosttab(:,1,i+4)
> !     write(*,'(a,i4,3f18.12)')'ET4Z4',i,
> !    & +gel_loc_turn4_phicos2*dcgs1s2tab(:,3,i+4)
> !    & +gel_loc_turn4_phisin2*dsgs1s2tab(:,3,i+4)
> !    & +gloc_theta3*dcosttab(:,2,i+4)
1322,1323c1221,1222
< C---------------------------------------------------------------------
<       subroutine clear_eelec_values(num_threads)
---
> C-----------------------------------------------------------------------
>       subroutine clear_eelec_values(threads_used)
1325a1225
>       include 'COMMON.OMP'
1331,1332c1231,1232
<       integer i,num_threads
<       do i=1,num_threads
---
>       integer i,threads_used
>       do i=1,threads_used
1337c1237
< C-------------------------------------------------------------------------------
---
> C-----------------------------------------------------------------------
1340a1241
>       include 'COMMON.OMP'
1352c1253
< C-------------------------------------------------------------------------------
---
> C-----------------------------------------------------------------------
1355a1257
>       include 'COMMON.OMP'
1371,1372c1273,1274
< C-------------------------------------------------------------------------------
<       subroutine sum_eelec_values(ees,evdw1,eel_loc,num_threads)
---
> C-----------------------------------------------------------------------
>       subroutine sum_eelec_values(ees,evdw1,eel_loc,threads_used)
1374a1277
>       include 'COMMON.OMP'
1381,1382c1284,1285
<       integer i,num_threads
<       do i=1,num_threads
---
>       integer i,threads_used
>       do i=1,threads_used
1392,1393c1295,1298
< C-------------------------------------------------------------------------------
<       subroutine clear_eelec_arrays(num_threads)
---
> 
> C-----------------------------------------------------------------------
>       subroutine clear_eelec_arrays(my_thread)
>       use omp_data
1398a1304
>       include 'COMMON.OMP'
1404,1433c1310,1336
<       integer i,j,num_threads
<       do i=1,num_threads
<         do j=-1,nres
<           eelecij_array(i).gvdwppx(j)=0.0d0
<           eelecij_array(i).gvdwppy(j)=0.0d0
<           eelecij_array(i).gvdwppz(j)=0.0d0
<           eelecij_array(i).gelc_longx(j)=0.0d0
<           eelecij_array(i).gelc_longy(j)=0.0d0
<           eelecij_array(i).gelc_longz(j)=0.0d0
<           eelecij_array(i).gelcx(j)=0.0d0
<           eelecij_array(i).gelcy(j)=0.0d0
<           eelecij_array(i).gelcz(j)=0.0d0
<           eelecij_array(i).gel_loc_longx(j)=0.0d0
<           eelecij_array(i).gel_loc_longy(j)=0.0d0
<           eelecij_array(i).gel_loc_longz(j)=0.0d0
<           eelecij_array(i).gel_locx(j)=0.0d0
<           eelecij_array(i).gel_locy(j)=0.0d0
<           eelecij_array(i).gel_locz(j)=0.0d0
<         enddo
<         eelecij_array(i).gel_locx(-2)=0.0d0
<         eelecij_array(i).gel_locy(-2)=0.0d0
<         eelecij_array(i).gel_locz(-2)=0.0d0
<         eelecij_array(i).gel_locx(nres+1)=0.0d0
<         eelecij_array(i).gel_locy(nres+1)=0.0d0
<         eelecij_array(i).gel_locz(nres+1)=0.0d0
<         eelecij_array(i).gel_locx(nres+2)=0.0d0
<         eelecij_array(i).gel_locy(nres+2)=0.0d0
<         eelecij_array(i).gel_locz(nres+2)=0.0d0
<       enddo
<       do i=1,num_threads
---
>       integer my_thread
>       workspace(my_thread).item.dgradcx(-1:nres)=0.0d0
>       workspace(my_thread).item.dgradcy(-1:nres)=0.0d0
>       workspace(my_thread).item.dgradcz(-1:nres)=0.0d0
> !     workspace(my_thread).item.gelcx(-1:nres)=0.0d0
> !     workspace(my_thread).item.gelcy(-1:nres)=0.0d0
> !     workspace(my_thread).item.gelcz(-1:nres)=0.0d0
>       workspace(my_thread).item.gel_locx(-2:nres+2)=0.0d0
>       workspace(my_thread).item.gel_locy(-2:nres+2)=0.0d0
>       workspace(my_thread).item.gel_locz(-2:nres+2)=0.0d0
>       return
>       end
> 
> C-----------------------------------------------------------------------
>       subroutine clear_common_eelec_arrays(threads_used)
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       include 'COMMON.OMP'
> #ifdef SHIELD
>       include 'COMMON.EELEC.SHIELD'
> #else
>       include 'COMMON.EELEC'
> #endif
>       integer i,j,threads_used
> 
>       do i=1,threads_used
1447,1449c1350,1351
< C-------------------------------------------------------------------------------
<       subroutine sum_eelec_arrays(num_threads)
< #ifdef _OPENMP
---
> C-----------------------------------------------------------------------
>       subroutine sum_eelec_arrays(threads_used)
1451c1353
< #endif
---
>       use omp_data
1459a1362,1363
>       include 'COMMON.TORSION'
>       include 'COMMON.OMP'
1466c1370
<       integer i,j,num_threads,j1,from,to,from2,to2,range_lo,range_hi
---
>       integer i,j,j1,from,to,range_lo,range_hi
1467a1372,1374
>       double precision tmuder(3,3)
>       double precision cost1,cost2,gggt(3)
>       integer iti,iti1
1473,1480d1379
< #ifdef _OPENMP
<       threads_used=omp_get_max_threads()
<       ! ensure at least 4 elements per thread to avoid overlap
<       !if(range_hi-range_lo+1 .lt. 4*threads_used) threads_used=1
< #else
<       threads_used=1
< #endif
< 
1483c1382,1383
< !$OMP& PRIVATE(from,to,from2,to2,i,j,ggg,ggg1,ggg2,ggg3,res,g,h)
---
> !$OMP& PRIVATE(from,to,i,j,ggg,ggg1,ggg2,ggg3,res,g,h,
> !$OMP&         iti,iti1,gggt,tmuder,cost1,cost2)
1489,1492d1388
<       !from=range_lo+((range_hi-range_lo)/threads_used)*(my_thread-1)
<       !to=range_lo+((range_hi-range_lo)/threads_used)*my_thread-1
<       !if(my_thread.eq.threads_used) to=range_hi
<       call split_work_for_threads(from,to,0,nres,my_thread,threads_used)
1494c1390,1392
<       gel_loc(:,from:to)=0.0d0
---
> c     skip index 0 as it is not used in sum_gradient
> !     call split_work_for_threads(from,to,0,nres,my_thread,threads_used)
>       call split_work_for_threads(from,to,1,nres,my_thread,threads_used)
1496,1499c1394
<       from2=from
<       if(my_thread.eq.1) from2=0
<       to2=to
<       if(my_thread.eq.threads_used) to2=range_hi-1
---
>       !gel_loc(:,from:to)=0.0d0
1501,1508c1396,1411
<       do j=from,to
<         ggg=0.0d0
<         do i=1,num_threads
<           ggg(1)=ggg(1)+eelecij_array(i).gvdwppx(j)
<           ggg(2)=ggg(2)+eelecij_array(i).gvdwppy(j)
<           ggg(3)=ggg(3)+eelecij_array(i).gvdwppz(j)
<         enddo
<         gvdwpp(:,j)=ggg
---
>       !from2=from
>       !if(my_thread.eq.1) from2=0
>       !to2=to
>       !if(my_thread.eq.threads_used) to2=range_hi-1
> 
>       ggg1=0.0d0
>       ggg2=0.0d0
>       ggg3=0.0d0
>       do i=1,threads_used
>           ggg2(1)=ggg2(1)+workspace(i).item.gel_locx(from-1)
>           ggg2(2)=ggg2(2)+workspace(i).item.gel_locy(from-1)
>           ggg2(3)=ggg2(3)+workspace(i).item.gel_locz(from-1)
> 
>           ggg1(1)=ggg1(1)+workspace(i).item.gel_locx(from)
>           ggg1(2)=ggg1(2)+workspace(i).item.gel_locy(from)
>           ggg1(3)=ggg1(3)+workspace(i).item.gel_locz(from)
1512,1519c1415,1426
<         ggg=0.0d0
<         do i=1,num_threads
<           ggg(1)=ggg(1)+eelecij_array(i).gelc_longx(j)
<           ggg(2)=ggg(2)+eelecij_array(i).gelc_longy(j)
<           ggg(3)=ggg(3)+eelecij_array(i).gelc_longz(j)
<         enddo
<         gelc_long(:,j)=ggg
<       enddo
---
> !       ggg=0.0d0
> !       do i=1,threads_used
> !         ggg(1)=ggg(1)+workspace(i).item.gelcx(j)
> !         ggg(2)=ggg(2)+workspace(i).item.gelcy(j)
> !         ggg(3)=ggg(3)+workspace(i).item.gelcz(j)
> !       enddo
> !       if(j.le.0) then
> !         res(:)=0.0d0
> !       else
> !         !res(:)=ggg*vbld_inv(j+1)*welec
> !         res(:)=ggg*(welec*vbld_inv(j+1))
> !       endif
1521,1526c1428,1444
<       do j=from,to
<         ggg=0.0d0
<         do i=1,num_threads
<           ggg(1)=ggg(1)+eelecij_array(i).gelcx(j)
<           ggg(2)=ggg(2)+eelecij_array(i).gelcy(j)
<           ggg(3)=ggg(3)+eelecij_array(i).gelcz(j)
---
>         ggg3=ggg2
>         ggg2=ggg1
>         ggg1=0.0d0
> !       ggg2=0.0d0
> !       ggg3=0.0d0
>         do i=1,threads_used
>           ggg1(1)=ggg1(1)+workspace(i).item.gel_locx(j+1)
>           ggg1(2)=ggg1(2)+workspace(i).item.gel_locy(j+1)
>           ggg1(3)=ggg1(3)+workspace(i).item.gel_locz(j+1)
> 
> !         ggg2(1)=ggg2(1)+workspace(i).item.gel_locx(j)
> !         ggg2(2)=ggg2(2)+workspace(i).item.gel_locy(j)
> !         ggg2(3)=ggg2(3)+workspace(i).item.gel_locz(j)
> 
> !         ggg3(1)=ggg3(1)+workspace(i).item.gel_locx(j-1)
> !         ggg3(2)=ggg3(2)+workspace(i).item.gel_locy(j-1)
> !         ggg3(3)=ggg3(3)+workspace(i).item.gel_locz(j-1)
1528,1532d1445
<         if(j.le.0) then
<           gelc(:,j)=0.0d0
<         else
<           gelc(:,j)=ggg*vbld_inv(j+1)
<         endif
1534,1544c1447
<         ggg=0.0d0
<         do i=1,num_threads
<           if (isnan(eelecij_array(i).gel_loc_longx(j))) 
<      &      print *,"i",i," j",j," NaN in eelecij_array",i,j
<           ggg(1)=ggg(1)+eelecij_array(i).gel_loc_longx(j)
<           ggg(2)=ggg(2)+eelecij_array(i).gel_loc_longy(j)
<           ggg(3)=ggg(3)+eelecij_array(i).gel_loc_longz(j)
<         enddo
<         if (isnan(ggg(1))) print *,"i",i," j",j," 2 NaN in ggg"
<         gel_loc_long(:,j)=ggg
< c        if(isnan(gel_loc_long(1,j)))print *,"i",i," j",j," 2 NaN in ggg"
---
>         call scale_by_muder(j,ggg1,ggg2,ggg3,ggg)
1546,1609c1449,1450
<         ggg1=0.0d0
<         ggg2=0.0d0
<         ggg3=0.0d0
<         !TODO: 3 times more summing than necessary
<         do i=1,num_threads
<           ggg1(1)=ggg1(1)+eelecij_array(i).gel_locx(j+1)
<           ggg1(2)=ggg1(2)+eelecij_array(i).gel_locy(j+1)
<           ggg1(3)=ggg1(3)+eelecij_array(i).gel_locz(j+1)
< 
<           ggg2(1)=ggg2(1)+eelecij_array(i).gel_locx(j)
<           ggg2(2)=ggg2(2)+eelecij_array(i).gel_locy(j)
<           ggg2(3)=ggg2(3)+eelecij_array(i).gel_locz(j)
< 
<           ggg3(1)=ggg3(1)+eelecij_array(i).gel_locx(j-1)
<           ggg3(2)=ggg3(2)+eelecij_array(i).gel_locy(j-1)
<           ggg3(3)=ggg3(3)+eelecij_array(i).gel_locz(j-1)
<         enddo
<         !gel_loc(:,j)=ggg2
< 
<         gel_loc(:,j)=0.0d0
<         gel_loc(1,j)=gel_loc(1,j)+scalar(ggg1,muder(:,1,1,j+1))
<         gel_loc(2,j)=gel_loc(2,j)+scalar(ggg1,muder(:,2,1,j+1))
<         gel_loc(3,j)=gel_loc(3,j)+scalar(ggg1,muder(:,3,1,j+1))
< 
<         gel_loc(1,j)=gel_loc(1,j)+scalar(ggg2,muder(:,1,2,j))
<         gel_loc(2,j)=gel_loc(2,j)+scalar(ggg2,muder(:,2,2,j))
<         gel_loc(3,j)=gel_loc(3,j)+scalar(ggg2,muder(:,3,2,j))
< 
<         gel_loc(1,j)=gel_loc(1,j)+scalar(ggg3,muder(:,1,3,j-1))
<         gel_loc(2,j)=gel_loc(2,j)+scalar(ggg3,muder(:,2,3,j-1))
<         gel_loc(3,j)=gel_loc(3,j)+scalar(ggg3,muder(:,3,3,j-1))
<       enddo
< 
< !$OMP BARRIER
<       do i=1,num_threads
<         eelecij_array(i).gvdwppx(from:to)=0.0d0
<         eelecij_array(i).gvdwppy(from:to)=0.0d0
<         eelecij_array(i).gvdwppz(from:to)=0.0d0
< 
<         eelecij_array(i).gelc_longx(from:to)=0.0d0
<         eelecij_array(i).gelc_longy(from:to)=0.0d0
<         eelecij_array(i).gelc_longz(from:to)=0.0d0
< 
<         eelecij_array(i).gelcx(from:to)=0.0d0
<         eelecij_array(i).gelcy(from:to)=0.0d0
<         eelecij_array(i).gelcz(from:to)=0.0d0
< 
<         eelecij_array(i).gel_loc_longx(from:to)=0.0d0
<         eelecij_array(i).gel_loc_longy(from:to)=0.0d0
<         eelecij_array(i).gel_loc_longz(from:to)=0.0d0
< 
<         eelecij_array(i).gel_locx(from:to)=0.0d0
<         eelecij_array(i).gel_locy(from:to)=0.0d0
<         eelecij_array(i).gel_locz(from:to)=0.0d0
< 
<         if (my_thread.eq.1) then
<           eelecij_array(i).gel_locx(0)=0.0d0
<           eelecij_array(i).gel_locy(0)=0.0d0
<           eelecij_array(i).gel_locz(0)=0.0d0
< 
<           eelecij_array(i).gel_locx(nres+1)=0.0d0
<           eelecij_array(i).gel_locy(nres+1)=0.0d0
<           eelecij_array(i).gel_locz(nres+1)=0.0d0
<         endif
---
> !       omp_dgradc(:,j)=res(:)+ggg(:)*(wel_loc*vbld_inv(j+1))
>         omp_dgradc(:,j)=omp_dgradc(:,j)+ggg(:)*(wel_loc*vbld_inv(j+1))
1612,1657d1452
<       return
<       end
< C-----------------------------------------------------------------------------
<       subroutine eelec_queue_work(chunk,idx,i,j,xyzj,rij,full)
<       implicit none
<       include 'DIMENSIONS'
<       include 'COMMON.CHAIN'
<       include 'COMMON.INTERACT'
< #ifdef SHIELD
<       include 'COMMON.SHIELD'
<       include 'COMMON.EELEC.SHIELD'
< #else
<       include 'COMMON.EELEC'
< #endif
<       include 'COMMON.LIPID'
< c 4/26/02 - AL scaling factor for 1,4 repulsive VDW interactions
<       integer idx,i,j,iteli,itelj
<       double precision xyzj(3),rij
<       logical full
<       record /eelecij_work_chunk/ chunk
< 
<       !iteli=itel(i)
<       !itelj=itel(j)
<       !if (j.eq.i+2 .and. itelj.eq.2) iteli=2
< 
<       idx=idx+1
<       chunk.j(idx)=j
<       chunk.x(idx)=xyzj(1)
<       chunk.y(idx)=xyzj(2)
<       chunk.z(idx)=xyzj(3)
<       chunk.r(idx)=rij
<       !chunk.aaa(idx)=app(iteli,itelj)
<       !chunk.bbb(idx)=bpp(iteli,itelj)
<       !chunk.ael6(idx)=ael6(iteli,itelj)
<       !chunk.ael3(idx)=ael3(iteli,itelj)
<       !chunk.sqrt_ael6(idx)=sqrt_ael6(iteli,itelj)
<       chunk.lipid_j_ss(idx)=lipid_cache(j).ss
<       chunk.lipid_j_ssgrad(idx)=lipid_cache(j).ssgrad
<       !if (j.eq.i+2) then
<       !  chunk.scal_el(idx)=scal_el
<       !else
<       !  chunk.scal_el(idx)=1.0d0
<       !endif
< #ifdef SHIELD
<       chunk.fac_shield_j(idx)=fac_shield(j)
< #endif
1659,1690c1454,1464
<       full=idx.eq.eelec_buffer_size
<       return
<       end
< C-----------------------------------------------------------------------------
<       subroutine eelecij_move_work(from,from_idx,to,to_idx)
<       implicit none
<       include 'DIMENSIONS'
< #ifdef SHIELD
<       include 'COMMON.EELEC.SHIELD'
< #else
<       include 'COMMON.EELEC'
< #endif
<       integer from_idx,to_idx,idx
<       record /eelecij_work_chunk/ from,to
<       do idx=1,from_idx
<         to_idx=to_idx+1
<         to.j(to_idx)=from.j(idx)
<         to.x(to_idx)=from.x(idx)
<         to.y(to_idx)=from.y(idx)
<         to.z(to_idx)=from.z(idx)
<         to.r(to_idx)=from.r(idx)
<         !to.aaa(to_idx)=from.aaa(idx)
<         !to.bbb(to_idx)=from.bbb(idx)
<         !to.ael6(to_idx)=from.ael6(idx)
<         !to.ael3(to_idx)=from.ael3(idx)
<         !to.sqrt_ael6(to_idx)=from.sqrt_ael6(idx)
<         to.lipid_j_ss(to_idx)=from.lipid_j_ss(idx)
<         to.lipid_j_ssgrad(to_idx)=from.lipid_j_ssgrad(idx)
<         !to.scal_el(to_idx)=from.scal_el(idx)
< #ifdef SHIELD
<         to.fac_shield_j(to_idx)=from.fac_shield_j(idx)
< #endif
---
> !$OMP PARALLEL NUM_THREADS(threads_used) DEFAULT(SHARED)
> !$OMP& PRIVATE(my_thread)
>       my_thread=omp_get_thread_num()+1
> 
> !     workspace(my_thread).item.gelcx(1:nres)=0.0d0
> !     workspace(my_thread).item.gelcy(1:nres)=0.0d0
> !     workspace(my_thread).item.gelcz(1:nres)=0.0d0
>       workspace(my_thread).item.gel_locx(0:nres+1)=0.0d0
>       workspace(my_thread).item.gel_locy(0:nres+1)=0.0d0
>       workspace(my_thread).item.gel_locz(0:nres+1)=0.0d0
> !$OMP END PARALLEL
1692,1693d1465
<       enddo
<       from_idx=0
1701,1702c1473,1475
<       INCLUDE 'DIMENSIONS'
<       INCLUDE 'COMMON.CHAIN'
---
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       include 'COMMON.OMP'
1748a1522,1570
> C-----------------------------------------------------------------------------
> !      subroutine calculate_turns4(doturn,eello_turn4)
> !#ifdef _OPENMP
> !      use omp_lib
> !#endif
> !      include 'DIMENSIONS'
> !      include 'COMMON.CHAIN'
> !      include 'COMMON.OMP'
> !#ifdef SHIELD
> !      include 'COMMON.EELEC.SHIELD'
> !#else
> !      include 'COMMON.EELEC'
> !#endif
> !      double precision eello_turn4
> !      double precision th_eello_turn(8,max_fg_threads)
> !      integer*1 doturn(maxres)
> !      integer i,threads_used,range_lo,range_hi,my_thread,from,to
> !
> !!     write(*,*)'calculate_turns4'
> !
> !      !range_lo=1
> !      !range_hi=nres
> !      th_eello_turn=0.0d0
> !
> !#ifdef _OPENMP
> !      threads_used=omp_get_max_threads()
> !#else
> !      threads_used=1
> !#endif
> !!$OMP PARALLEL IF(threads_used.gt.1) NUM_THREADS(threads_used)
> !!$OMP& DEFAULT(SHARED) PRIVATE(i,my_thread,from,to)
> !#ifdef _OPENMP
> !      my_thread=omp_get_thread_num()+1
> !#else
> !      my_thread=1
> !#endif
> !      call split_work_for_threads(from,to,1,nres,my_thread,threads_used)
> !
> !      do i=from,to
> !        if(and(doturn(i),2).ne.0) then
> !          call eturn4(i,th_eello_turn(2,my_thread),my_thread)
> !        endif
> !      enddo
> !!$OMP END PARALLEL
> !
> !      eello_turn4=sum(th_eello_turn(2,1:threads_used))
> !      call sum_eturn4_arrays(threads_used)
> !      return
> !      end
1750c1572
<       subroutine sum_eturn_arrays(num_threads)
---
>       subroutine sum_eturn_arrays(threads_used)
1758a1581
>       include 'COMMON.OMP'
1764c1587
<       integer i,j,num_threads
---
>       integer i,j,threads_used
1768c1591
<         do j=1,num_threads
---
>         do j=1,threads_used
1799c1622
<         do j=1,num_threads
---
>         do j=1,threads_used
1810c1633
<         do j=1,num_threads
---
>         do j=1,threads_used
1817c1640
<         do j=1,num_threads
---
>         do j=1,threads_used
1824c1647,1718
<         do j=1,num_threads
---
>         do j=1,threads_used
>           ggg=ggg+eelecij_turn(i,j).gshieldc_t4
>           eelecij_turn(i,j).gshieldc_t4=0.0d0
>         enddo
>         gshieldc_t4(:,i)=gshieldc_t4(:,i)+ggg
> 
>         ggg=0.0d0
>         do j=1,threads_used
>           ggg=ggg+eelecij_turn(i,j).gshieldx_t4
>           eelecij_turn(i,j).gshieldx_t4=0.0d0
>         enddo
>         gshieldx_t4(:,i)=gshieldx_t4(:,i)+ggg
>         enddo
> #endif
> 
>       enddo
> !$OMP END PARALLEL DO
> 
>       return
>       end
> 
> 
>       subroutine sum_eturn4_arrays(threads_used)
> #ifdef _OPENMP
>       use omp_lib
> #endif
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.VAR'
>       include 'COMMON.CHAIN'
>       include 'COMMON.DERIV'
>       include 'COMMON.OMP'
> #ifdef SHIELD
>       include 'COMMON.EELEC.SHIELD'
> #else
>       include 'COMMON.EELEC'
> #endif
>       integer i,j,threads_used
>       double precision ggg(3,2),g(3)
> 
>       do i=-1,0
>         do j=1,threads_used
>           gcorr4_turn(:,i)=
>      &               gcorr4_turn(:,i)+eelecij_turn(i,j).gcorr4_turn
> #ifdef SHIELD
>           gshieldc_t4(:,i)=
>      &               gshieldc_t4(:,i)+eelecij_turn(i,j).gshieldc_t4
>           gshieldx_t4(:,i)=
>      &               gshieldx_t4(:,i)+eelecij_turn(i,j).gshieldx_t4
> #endif
> 
>           eelecij_turn(i,j).gcorr4_turn=0.0d0
> #ifdef SHIELD
>           eelecij_turn(i,j).gshieldc_t4=0.0d0
>           eelecij_turn(i,j).gshieldx_t4=0.0d0
> #endif
>         enddo
>       enddo
> 
> !$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(j,ggg,g)
>       do i=1,nres
>         ggg(:,1)=0.0d0
>         ggg(:,2)=0.0d0
>         do j=1,threads_used
>           ggg(:,2)=ggg(:,2)+eelecij_turn(i,j).gcorr4_turn
>           eelecij_turn(i,j).gcorr4_turn=0.0d0
>         enddo
>         gcorr4_turn(:,i)=gcorr4_turn(:,i)+ggg(:,2)
> 
> #ifdef SHIELD
>         ggg=0.0d0
>         do j=1,threads_used
1831c1725
<         do j=1,num_threads
---
>         do j=1,threads_used
1877a1772
>       include 'COMMON.OMP'
1897a1793,1795
> #ifdef _OPENMP
>       use omp_lib
> #endif
1903a1802
>       include 'COMMON.OMP'
1909c1808,1811
<       integer k,i
---
>       integer k,i,last,blk,count3,count4
>       integer threads_used,current_thread,work_left
>       integer count3_per_thread,count3_tails
>       integer count4_per_thread,count4_tails
1910a1813,1821
> #ifdef _OPENMP
>       threads_used=omp_get_max_threads()
> #else
>       threads_used=1
> #endif
> 
>       eelecij_doturn(1:nres)=0
> 
>       count3=0
1911a1823,1845
>       do i=iturn3_start,iturn3_end
>         if(itype(i).eq.ntyp1
>      &     .or. itype(i+1).eq.ntyp1
>      &     .or. itype(i+2).eq.ntyp1
>      &     .or. itype(i+3).eq.ntyp1) cycle
>         count3=count3+1
>       enddo
> 
>       count4=0
>       do i=iturn4_start,iturn4_end
>         if(i.lt.1) 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
>         count4=count4+1
>       enddo
> 
>       count3_per_thread=count3/threads_used
>       count3_tails=count3-count3_per_thread*threads_used
> 
>       count4_per_thread=count4/threads_used
>       count4_tails=count4-count4_per_thread*threads_used
1913a1848,1857
>       blk=0
>       last=-1
>       current_thread=0
>       work_left=0
> 
>       first_block_2(1:threads_used)=1
>       last_block_2(1:threads_used)=0
>       first_block_3(1:threads_used)=1
>       last_block_3(1:threads_used)=0
> 
1915,1916d1858
< c        if (i.le.1) cycle
< C        write(iout,*) "tu jest i",i
1922,1923d1863
<         !eelecij_work_turn3(k).i=i
<         !eelecij_work_turn3(k).j=i+2
1924a1865,1883
> 
>         if(i.eq.last+1 .and. work_left.gt.0) then
>           eelecij_blocks_2(blk*2+1)=i
>           work_left=work_left-1
>         else
>           blk=blk+1
>           eelecij_blocks_2(blk*2+0)=i
>           eelecij_blocks_2(blk*2+1)=i
>           if(work_left.le.0) then
>             current_thread=current_thread+1
>             first_block_2(current_thread)=blk
>             work_left=count3_per_thread
>             if(current_thread.le.count3_tails) work_left=work_left+1
>             work_left=work_left-1
>           endif
>           last_block_2(current_thread)=blk
>         endif
>         last=i
> 
1927a1887
> 
1928a1889
>       num_eelecij_blocks_2=blk
1930a1892,1896
>       blk=0
>       last=-1
>       current_thread=0
>       work_left=0
> 
1936a1903
> 
1938,1939d1904
<         !eelecij_work_turn4(k).i=i
<         !eelecij_work_turn4(k).j=i+3
1940a1906,1924
> 
>         if(i.eq.last+1 .and. work_left.gt.0) then
>           eelecij_blocks_3(blk*2+1)=i
>           work_left=work_left-1
>         else
>           blk=blk+1
>           eelecij_blocks_3(blk*2+0)=i
>           eelecij_blocks_3(blk*2+1)=i
>           if(work_left.le.0) then
>             current_thread=current_thread+1
>             first_block_3(current_thread)=blk
>             work_left=count4_per_thread
>             if(current_thread.le.count4_tails) work_left=work_left+1
>             work_left=work_left-1
>           endif
>           last_block_3(current_thread)=blk
>         endif
>         last=i
> 
1943a1928
> 
1944a1930
>       num_eelecij_blocks_3=blk
1948c1934
< C=======================================================================
---
> C-----------------------------------------------------------------------
1949a1936
> #define EELECIJ_DELTA      eelecij_delta
1951,1953c1938,1940
< #define EELECIJ2           eelecij2
< #define EELEC_SEQ          eelecij_do_work_seq
< #define EELEC_NSEQ         eelecij_do_work_nonseq
---
> #define EELECIJ_WORK_SEQ   eelecij_do_work_seq
> #define EELECIJ_WORK_NSEQ  eelecij_do_work_nseq
> #define EELECIJ_WORK_PAIR  eelecij_do_work_pair
1955,1970c1942,1966
< #define EELEC_WORK_FN      EELEC_SEQ
< #define EELEC_MAP_J(seq)   seq
< #define EELEC_MAP_IDX(seq) seq-from+1
< #include"eelec_work.F"
< #undef EELEC_MAP_IDX
< #undef EELEC_MAP_J
< #undef EELEC_WORK_FN
< #define EELEC_WORK_FN      EELEC_NSEQ
< #define EELEC_MAP_J(seq)   chunk.j(seq)
< #define EELEC_MAP_IDX(seq) seq
< #include"eelec_work.F"
< #undef EELEC_MAP_IDX
< #undef EELEC_MAP_J
< #undef EELEC_WORK_FN
< #undef EELEC_NSEQ
< #undef EELEC_SEQ
---
> 
> !#define EELECIJ_WORK_FN    EELECIJ_WORK_SEQ
> !#define EELECIJ_MAP_J(seq) seq
> !#include"eelec_work.F"
> !#undef EELECIJ_MAP_J
> !#undef EELECIJ_WORK_FN
> !
> !#define EELECIJ_WORK_FN    EELECIJ_WORK_NSEQ
> !#define EELECIJ_MAP_J(seq) jindices(seq)
> !#include"eelec_work.F"
> !#undef EELECIJ_MAP_J
> !#undef EELECIJ_WORK_FN
> !
> !#define EELECIJ_WORK_FN     EELECIJ_WORK_PAIR
> !#define EELECIJ_MAP_I(seq) seq
> !#define EELECIJ_MAP_J(seq) (seq+basei)
> !#include"eelec_work.F"
> !#undef EELECIJ_MAP_J
> !#undef EELECIJ_MAP_I
> !#undef EELECIJ_WORK_FN
> 
> #undef EELECIJ_WORK_SEQ
> #undef EELECIJ_WORK_NSEQ
> #undef EELECIJ_WORK_PAIR
> #undef EELECIJ_DELTA
1972c1968
< #undef EELECIJ2
---
> 
1974a1971
> #define EELECIJ_DELTA      eelecij_delta_nowl
1976,1978c1973,1975
< #define EELECIJ2           eelecij2_nowl
< #define EELEC_SEQ          eelecij_do_work_seq_nowl
< #define EELEC_NSEQ         eelecij_do_work_nonseq_nowl
---
> #define EELECIJ_WORK_SEQ   eelecij_do_work_seq_nowl
> #define EELECIJ_WORK_NSEQ  eelecij_do_work_nseq_nowl
> #define EELECIJ_WORK_PAIR  eelecij_do_work_pair_nowl
1980,1995c1977,2001
< #define EELEC_WORK_FN      EELEC_SEQ
< #define EELEC_MAP_J(seq)   seq
< #define EELEC_MAP_IDX(seq) seq-from+1
< #include"eelec_work.F"
< #undef EELEC_MAP_IDX
< #undef EELEC_MAP_J
< #undef EELEC_WORK_FN
< #define EELEC_WORK_FN      EELEC_NSEQ
< #define EELEC_MAP_J(seq)   chunk.j(seq)
< #define EELEC_MAP_IDX(seq) seq
< #include"eelec_work.F"
< #undef EELEC_MAP_IDX
< #undef EELEC_MAP_J
< #undef EELEC_WORK_FN
< #undef EELEC_NSEQ
< #undef EELEC_SEQ
---
> 
> !#define EELECIJ_WORK_FN    EELECIJ_WORK_SEQ
> !#define EELECIJ_MAP_J(seq) seq
> !#include"eelec_work.F"
> !#undef EELECIJ_MAP_J
> !#undef EELECIJ_WORK_FN
> !
> !#define EELECIJ_WORK_FN    EELECIJ_WORK_NSEQ
> !#define EELECIJ_MAP_J(seq) jindices(seq)
> !#include"eelec_work.F"
> !#undef EELECIJ_MAP_J
> !#undef EELECIJ_WORK_FN
> !
> !#define EELECIJ_WORK_FN    EELECIJ_WORK_PAIR
> !#define EELECIJ_MAP_I(seq) seq
> !#define EELECIJ_MAP_J(seq) (seq+basei)
> !#include"eelec_work.F"
> !#undef EELECIJ_MAP_J
> !#undef EELECIJ_MAP_I
> !#undef EELECIJ_WORK_FN
> 
> #undef EELECIJ_WORK_SEQ
> #undef EELECIJ_WORK_NSEQ
> #undef EELECIJ_WORK_PAIR
> #undef EELECIJ_DELTA
1997d2002
< #undef EELECIJ2
eelec_prep.F
22c22,28
<       double precision auxvec(3),auxvec1(3),auxmat(2,2),mudertemp(3,3)
---
>       include 'COMMON.OMP'
> #ifndef SHIELD
>       include 'COMMON.EELEC'
> #else
>       include 'COMMON.EELEC.SHIELD'
> #endif
>       double precision auxvec(3),auxvec1(3),mudertemp(3,3)
23a30
>       double precision inv_sint1,inv_sint1cub
25c32,33
<       integer i,ii,iti,iti1,innt,inct,j,k,l
---
>       double precision b1i1(2),gtb1i1(2),b2i(2),gtb2i(2)
>       integer i,ii,ii1,iti,iti1,innt,inct,j,k,l
26a35
> !     write(*,*)'set_matrices'
31,32c40,45
< c      write(iout,*) 'nphi=',nphi,nres
< c      write(iout,*) "itype2loc",itype2loc
---
>       mu(:,nres)=0.0d0
>       soa_mu_x(nres)=0.0d0
>       soa_mu_y(nres)=0.0d0
>       soa_mu_z(nres)=0.0d0
> !     muder(:,:,:,-1:0)=0.0d0
> !     muder(:,:,:,nres:nres+1)=0.0d0
34,35c47,50
< !$OMP& PRIVATE(k,l,ii,iti,iti1,innt,inct,
< !$OMP&         sint1,cost1,sint1sq,sint1cub)
---
> !$OMP& PRIVATE(j,k,cost1,cost2,sint1,sint2,aux,aux1,aux2,
> !$OMP&         auxvec,auxvec1,mudertemp,b1i1,gtb1i1,b2i,gtb2i,
> !$OMP&         sint1sq,sint1cub,inv_sint1,inv_sint1cub,
> !$OMP&         ii,iti,iti1)
37,53c52,121
<         ii=ireschain(i)
< c       write (iout,*) "i",i,i-2," ii",ii
<         if (ii.eq.0) cycle
<         innt=chain_border(1,ii)
<         inct=chain_border(2,ii)
< c       write (iout,*) "i",i,i-2," ii",ii," innt",innt," inct",inct
< c       if (i.gt. nnt+2 .and. i.lt.nct+2) then 
<         if (i.gt. innt .and. i.lt.inct) then 
<           iti=itype2loc(itype(i))
<         else
<           iti=nloctyp
<         endif
< c       if (i.gt. iatel_s+1 .and. i.lt.iatel_e+4) then
<         if (i.gt.innt-1 .and. i.lt.inct-1) then 
<           iti1=itype2loc(itype(i+1))
<         else
<           iti1=nloctyp
---
>         if (abs_itype(i).ne.ntyp1.and.abs_itype(i+1).ne.ntyp1) then
>           cost1=costtab(i+1)
>           cost2=costtab(i+2)
>           sint1=sinttab(i+1)
>           sint2=sinttab(i+2)
> 
>           ii=ireschain(i)
>           iti=iti_tab(i)
>           iti1=iti_tab(i+1)
> 
>           if (ii.ne.0) then
>             sint1sq=sint1*sint1
>             sint1cub=sint1sq*sint1
>             inv_sint1=1.0d0/sint1
>             inv_sint1cub=1.0d0/sint1cub
>             do k=1,2
>               do l=1,2
>                 EE(l,k,i)=(eenew(1,l,k,iti)+eenew(2,l,k,iti)*cost1)
>      &            *sint1
>                 gtEE(l,k,i)=eenew(2,l,k,iti)*sint1-
>      &            (eenew(1,l,k,iti)+eenew(2,l,k,iti)*cost1)*cost1
>      &            *inv_sint1
>               enddo
>             enddo
>             EE(1,1,i)=EE(1,1,i)+e0new(1,iti)*cost1*inv_sint1
>             EE(1,2,i)=EE(1,2,i)+(e0new(2,iti)+e0new(3,iti)*cost1)
>      &        *inv_sint1
>             EE(2,1,i)=EE(2,1,i)+(e0new(2,iti)*cost1+e0new(3,iti))
>      &        *inv_sint1
>             EE(2,2,i)=EE(2,2,i)-e0new(1,iti)*inv_sint1
>             gtEE(1,1,i)=gtEE(1,1,i)+e0new(1,iti)*inv_sint1cub
>             gtEE(1,2,i)=gtEE(1,2,i)+e0new(3,iti)*inv_sint1+
>      &         (e0new(2,iti)+e0new(3,iti)*cost1)*cost1*inv_sint1cub
>             gtEE(2,1,i)=gtEE(2,1,i)+e0new(2,iti)*inv_sint1+
>      &         (e0new(3,iti)+e0new(2,iti)*cost1)*cost1*inv_sint1cub
>             gtEE(2,2,i)=gtEE(2,2,i)-e0new(1,iti)*cost1*inv_sint1cub
>           endif
> 
>           b1i1(1)=bnew1(1,1,iti1)
>      &      +(bnew1(2,1,iti1)+bnew1(3,1,iti1)*cost2)*cost2
>           b1i1(2)=bnew1(1,2,iti1)
>      &      +(bnew1(2,2,iti1)+bnew1(3,2,iti1)*cost2)*cost2
> 
> !         gtb1i1(1)=bnew1(2,1,iti1)+2*bnew1(3,1,iti1)*cost2
> !         gtb1i1(2)=bnew1(2,2,iti1)+2*bnew1(3,2,iti1)*cost2
> 
>           b2i(1)=bnew2(1,1,iti)
>      &      +(bnew2(2,1,iti)+bnew2(3,1,iti)*cost1)*cost1
>           b2i(2)=bnew2(1,2,iti)
>      &      +(bnew2(2,2,iti)+bnew2(3,2,iti)*cost1)*cost1
> 
> !         gtb2i(1)=bnew2(2,1,iti)+2*bnew2(3,1,iti)*cost1
> !         gtb2i(2)=bnew2(2,2,iti)+2*bnew2(3,2,iti)*cost1
> 
> c          write (iout,*) "i",i," uz_dc(:,i)",uz_dc(:,i),
> c     &     " uz_dc(:,i-1)",uz_dc(:,i-1)
>           mu(:,i)=b2i(1)*dC_norm(:,i-1)+b1i1(1)*dC_norm(:,i+1)
>      &            +(b2i(1)*cost1+b1i1(1)*cost2)*dC_norm(:,i)
>      &             -b2i(2)*uz_dc(:,i-1)-b1i1(2)*uz_dc(:,i)
>           soa_mu_x(i)=mu(1,i)
>           soa_mu_y(i)=mu(2,i)
>           soa_mu_z(i)=mu(3,i)
> !C Derivatives of mu(:,i) in dC(:,i-1)
> 
>           Ug(1,1,i)=-cphsth1sth2tab(i+2)
>           Ug(1,2,i)=-sphsth1sth2tab(i+2)
>           Ug(2,1,i)=-sphsth1sth2tab(i+2)
>           Ug(2,2,i)= cphsth1sth2tab(i+2)
>           call matmat2(EE(1,1,i),Ug(1,1,i),EUg(1,1,i))
>           call matmat2(gtEE(1,1,i),Ug(1,1,i),gtEUg(1,1,i))
55,99c123,131
< c       write(iout,*),"i",i,i-2," iti",itype(i-2),iti,
< c     &  " iti1",itype(i-1),iti1
< c 11/8/2021 AL: Change to the derivatives in cos(theta), not in theta.
<         cost1=costtab(i+1)
<         sint1=sinttab(i+1)
<         sint1sq=sint1*sint1
<         sint1cub=sint1sq*sint1
< c       write (iout,*) "bnew1",i,iti
< c       write (iout,*) (bnew1(k,1,iti),k=1,3)
< c       write (iout,*) (bnew1(k,2,iti),k=1,3)
< c       write (iout,*) "bnew2",i,iti
< c       write (iout,*) (bnew2(k,1,iti),k=1,3)
< c       write (iout,*) (bnew2(k,2,iti),k=1,3)
<         do k=1,2
<           b1(k,i)=bnew1(1,k,iti)
<      &      +(bnew1(2,k,iti)+bnew1(3,k,iti)*cost1)*cost1
<           gtb1(k,i)=bnew1(2,k,iti)+2*bnew1(3,k,iti)*cost1
<           b2(k,i)=bnew2(1,k,iti)
<      &      +(bnew2(2,k,iti)+bnew2(3,k,iti)*cost1)*cost1
<           gtb2(k,i)=bnew2(2,k,iti)+2*bnew2(3,k,iti)*cost1
<         enddo
<         do k=1,2
<           do l=1,2
< c            aux=eenew(1,l,k,iti)+eenew(2,l,k,iti)*cost1
< c            EE(l,k,i)=sint1sq*aux
< c            gtEE(l,k,i)=aux-2*cost1*eenew(2,l,k,iti)
<             EE(l,k,i)=(eenew(1,l,k,iti)+eenew(2,l,k,iti)*cost1)*sint1
<             gtEE(l,k,i)=eenew(2,l,k,iti)*sint1-
<      &        (eenew(1,l,k,iti)+eenew(2,l,k,iti)*cost1)*cost1/sint1
<           enddo
<         enddo
<         EE(1,1,i)=EE(1,1,i)+e0new(1,iti)*cost1/sint1
<         EE(1,2,i)=EE(1,2,i)+(e0new(2,iti)+e0new(3,iti)*cost1)/sint1
<         EE(2,1,i)=EE(2,1,i)+(e0new(2,iti)*cost1+e0new(3,iti))/sint1
<         EE(2,2,i)=EE(2,2,i)-e0new(1,iti)/sint1
<         gtEE(1,1,i)=gtEE(1,1,i)+e0new(1,iti)/sint1cub
<         gtEE(1,2,i)=gtEE(1,2,i)+e0new(3,iti)/sint1+
<      &     (e0new(2,iti)+e0new(3,iti)*cost1)*cost1/sint1cub
<         gtEE(2,1,i)=gtEE(2,1,i)+e0new(2,iti)/sint1+
<      &     (e0new(3,iti)+e0new(2,iti)*cost1)*cost1/sint1cub
<         gtEE(2,2,i)=gtEE(2,2,i)-e0new(1,iti)*cost1/sint1cub
< c        b1tilde(1,i-2)=b1(1,i-2)
< c        b1tilde(2,i-2)=-b1(2,i-2)
< c        b2tilde(1,i-2)=b2(1,i-2)
< c        b2tilde(2,i-2)=-b2(2,i-2)
---
>       enddo
> #ifdef DEBUG
>       write (iout,*) "Vectors mu"
>       do i=1,nres-1
>         write (iout,'(2hmu,i3,3f8.1,12f10.5)') i,rad2deg*theta(i+2),
>      &   rad2deg*theta(i+3),rad2deg*phi(i+3),mu(1,i),mu(2,i),mu(3,i),
>      &      ((ee(l,k,i),l=1,2),k=1,2)
>       enddo
> #endif
101,104c133,152
<         write (iout,*) 'i=',i,gtb1(2,i),gtb1(1,i)
<         write(iout,*)  'b1=',(b1(k,i),k=1,2)
<         write(iout,*)  'b2=',(b2(k,i),k=1,2)
<         write (iout,*) 'theta=', theta(i)
---
>       write (iout,*) "Arrays EE UG and EUG"
>       do i=1,nres-1
>         write (iout,'(i5,4f10.5,5x,4f10.5,5x,4f10.5)') i,
>      &   ((ee(l,k,i),l=1,2),k=1,2),
>      &   ((ug(l,k,i),l=1,2),k=1,2),
>      &   ((eug(l,k,i),l=1,2),k=1,2)
>       enddo
> #endif
>       return
>       end
> C-----------------------------------------------------------------------
>       subroutine set_matrices_scale
>       use omp_lib
>       implicit none
>       include 'DIMENSIONS'
> #ifdef MPI
>       include "mpif.h"
>       include "COMMON.SETUP"
>       integer IERR
>       integer status(MPI_STATUS_SIZE)
106,107c154,254
<       enddo ! i
<       mu(:,:nres)=0.0d0
---
>       include 'COMMON.IOUNITS'
>       include 'COMMON.GEO'
>       include 'COMMON.VAR'
>       include 'COMMON.LOCAL'
>       include 'COMMON.CHAIN'
>       include 'COMMON.DERIV'
>       include 'COMMON.INTERACT'
>       include 'COMMON.CORRMAT'
>       include 'COMMON.TORSION'
>       include 'COMMON.VECTORS'
>       include 'COMMON.FFIELD'
>       include 'COMMON.OMP'
>       double precision auxvec(3),auxvec1(3),mudertemp(3,3)
>       double precision sint1,cost1,sint1sq,sint1cub,sint2,cost2
>       double precision inv_sint1,inv_sint1cub
>       double precision aux,aux1,aux2
>       double precision b1i1(2),gtb1i1(2),b2i(2),gtb2i(2)
>       integer i,ii,ii1,iti,iti1,innt,inct,j,k,l
> 
> C
> C Compute the virtual-bond-torsional-angle dependent quantities needed
> C to calculate the el-loc multibody terms of various order.
> C
> c      write(iout,*) 'nphi=',nphi,nres
> c      write(iout,*) "itype2loc",itype2loc
> !!!$OMP PARALLEL DO DEFAULT(SHARED)
> !!!$OMP& PRIVATE(k,l,ii,iti,iti1,innt,inct,
> !!!$OMP&         sint1,cost1,sint1sq,sint1cub)
> !!      do i=1,nres-1
> !!        ii=ireschain(i)
> !!c       write (iout,*) "i",i,i-2," ii",ii
> !!        if (ii.eq.0) cycle
> !!        innt=chain_border(1,ii)
> !!        inct=chain_border(2,ii)
> !!c       write (iout,*) "i",i,i-2," ii",ii," innt",innt," inct",inct
> !!c       if (i.gt. nnt+2 .and. i.lt.nct+2) then 
> !!        if (i.gt. innt .and. i.lt.inct) then 
> !!          iti=itype2loc(itype(i))
> !!        else
> !!          iti=nloctyp
> !!        endif
> !!c       if (i.gt. iatel_s+1 .and. i.lt.iatel_e+4) then
> !!        if (i.gt.innt-1 .and. i.lt.inct-1) then 
> !!          iti1=itype2loc(itype(i+1))
> !!        else
> !!          iti1=nloctyp
> !!        endif
> !!c       write(iout,*),"i",i,i-2," iti",itype(i-2),iti,
> !!c     &  " iti1",itype(i-1),iti1
> !!c 11/8/2021 AL: Change to the derivatives in cos(theta), not in theta.
> !!        cost1=costtab(i+1)
> !!        sint1=sinttab(i+1)
> !!        sint1sq=sint1*sint1
> !!        sint1cub=sint1sq*sint1
> !!c       write (iout,*) "bnew1",i,iti
> !!c       write (iout,*) (bnew1(k,1,iti),k=1,3)
> !!c       write (iout,*) (bnew1(k,2,iti),k=1,3)
> !!c       write (iout,*) "bnew2",i,iti
> !!c       write (iout,*) (bnew2(k,1,iti),k=1,3)
> !!c       write (iout,*) (bnew2(k,2,iti),k=1,3)
> !!        do k=1,2
> !!!         b1(k,i)=bnew1(1,k,iti)
> !!!    &      +(bnew1(2,k,iti)+bnew1(3,k,iti)*cost1)*cost1
> !!!         gtb1(k,i)=bnew1(2,k,iti)+2*bnew1(3,k,iti)*cost1
> !!!         b2(k,i)=bnew2(1,k,iti)
> !!!    &      +(bnew2(2,k,iti)+bnew2(3,k,iti)*cost1)*cost1
> !!!         gtb2(k,i)=bnew2(2,k,iti)+2*bnew2(3,k,iti)*cost1
> !!        enddo
> !!        do k=1,2
> !!          do l=1,2
> !!c            aux=eenew(1,l,k,iti)+eenew(2,l,k,iti)*cost1
> !!c            EE(l,k,i)=sint1sq*aux
> !!c            gtEE(l,k,i)=aux-2*cost1*eenew(2,l,k,iti)
> !!            EE(l,k,i)=(eenew(1,l,k,iti)+eenew(2,l,k,iti)*cost1)*sint1
> !!            gtEE(l,k,i)=eenew(2,l,k,iti)*sint1-
> !!     &        (eenew(1,l,k,iti)+eenew(2,l,k,iti)*cost1)*cost1/sint1
> !!          enddo
> !!        enddo
> !!        EE(1,1,i)=EE(1,1,i)+e0new(1,iti)*cost1/sint1
> !!        EE(1,2,i)=EE(1,2,i)+(e0new(2,iti)+e0new(3,iti)*cost1)/sint1
> !!        EE(2,1,i)=EE(2,1,i)+(e0new(2,iti)*cost1+e0new(3,iti))/sint1
> !!        EE(2,2,i)=EE(2,2,i)-e0new(1,iti)/sint1
> !!        gtEE(1,1,i)=gtEE(1,1,i)+e0new(1,iti)/sint1cub
> !!        gtEE(1,2,i)=gtEE(1,2,i)+e0new(3,iti)/sint1+
> !!     &     (e0new(2,iti)+e0new(3,iti)*cost1)*cost1/sint1cub
> !!        gtEE(2,1,i)=gtEE(2,1,i)+e0new(2,iti)/sint1+
> !!     &     (e0new(3,iti)+e0new(2,iti)*cost1)*cost1/sint1cub
> !!        gtEE(2,2,i)=gtEE(2,2,i)-e0new(1,iti)*cost1/sint1cub
> !!c        b1tilde(1,i-2)=b1(1,i-2)
> !!c        b1tilde(2,i-2)=-b1(2,i-2)
> !!c        b2tilde(1,i-2)=b2(1,i-2)
> !!c        b2tilde(2,i-2)=-b2(2,i-2)
> !!#ifdef DEBUG
> !!        !write (iout,*) 'i=',i,gtb1(2,i),gtb1(1,i)
> !!        !write(iout,*)  'b1=',(b1(k,i),k=1,2)
> !!        !write(iout,*)  'b2=',(b2(k,i),k=1,2)
> !!        !write (iout,*) 'theta=', theta(i)
> !!#endif
> !!      enddo ! i
>       !mu(:,:nres)=0.0d0
>       mu(:,nres)=0.0d0
109c256,258
<       muder(:,:,:,-1:nres+1)=0.0d0
---
>       !muder(:,:,:,-1:nres+1)=0.0d0
>       muder(:,:,:,-1:0)=0.0d0
>       muder(:,:,:,nres:nres+1)=0.0d0
116c265,267
< !$OMP&         auxvec,auxvec1,mudertemp)
---
> !$OMP&         auxvec,auxvec1,mudertemp,b1i1,gtb1i1,b2i,gtb2i,
> !$OMP&         sint1sq,sint1cub,inv_sint1,inv_sint1cub,
> !$OMP&         ii,iti,iti1)
120c271,272
<         if (iabs(itype(i)).ne.ntyp1.and.iabs(itype(i+1)).ne.ntyp1) then
---
> !       if (iabs(itype(i)).ne.ntyp1.and.iabs(itype(i+1)).ne.ntyp1) then
>         if (abs_itype(i).ne.ntyp1.and.abs_itype(i+1).ne.ntyp1) then
124a277,345
> 
>           !ii=ireschain(i)
>           !innt=chain_border(1,ii)
>           !inct=chain_border(2,ii)
>           !if (i.gt. innt .and. i.lt.inct) then 
>           !  iti=itype2loc(itype(i))
>           !else
>           !  iti=nloctyp
>           !endif
>           ii=ireschain(i)
>           iti=iti_tab(i)
> 
>           !ii1=ireschain(i+1)
>           !innt=chain_border(1,ii1)
>           !inct=chain_border(2,ii1)
>           !if (i+1.gt. innt .and. i+1.lt.inct) then 
>           !  iti1=itype2loc(itype(i+1))
>           !else
>           !  iti1=nloctyp
>           !endif
>           iti1=iti_tab(i+1)
> 
>           if (ii.ne.0) then
>             sint1sq=sint1*sint1
>             sint1cub=sint1sq*sint1
>             inv_sint1=1.0d0/sint1
>             inv_sint1cub=1.0d0/sint1cub
>             do k=1,2
>               do l=1,2
> c               aux=eenew(1,l,k,iti)+eenew(2,l,k,iti)*cost1
> c               EE(l,k,i)=sint1sq*aux
> c               gtEE(l,k,i)=aux-2*cost1*eenew(2,l,k,iti)
>                 EE(l,k,i)=(eenew(1,l,k,iti)+eenew(2,l,k,iti)*cost1)
>      &            *sint1
>                 gtEE(l,k,i)=eenew(2,l,k,iti)*sint1-
>      &            (eenew(1,l,k,iti)+eenew(2,l,k,iti)*cost1)*cost1
>      &            *inv_sint1
>               enddo
>             enddo
>             EE(1,1,i)=EE(1,1,i)+e0new(1,iti)*cost1*inv_sint1
>             EE(1,2,i)=EE(1,2,i)+(e0new(2,iti)+e0new(3,iti)*cost1)
>      &        *inv_sint1
>             EE(2,1,i)=EE(2,1,i)+(e0new(2,iti)*cost1+e0new(3,iti))
>      &        *inv_sint1
>             EE(2,2,i)=EE(2,2,i)-e0new(1,iti)*inv_sint1
>             gtEE(1,1,i)=gtEE(1,1,i)+e0new(1,iti)*inv_sint1cub
>             gtEE(1,2,i)=gtEE(1,2,i)+e0new(3,iti)*inv_sint1+
>      &         (e0new(2,iti)+e0new(3,iti)*cost1)*cost1*inv_sint1cub
>             gtEE(2,1,i)=gtEE(2,1,i)+e0new(2,iti)*inv_sint1+
>      &         (e0new(3,iti)+e0new(2,iti)*cost1)*cost1*inv_sint1cub
>             gtEE(2,2,i)=gtEE(2,2,i)-e0new(1,iti)*cost1*inv_sint1cub
>           endif
> 
>           b1i1(1)=bnew1(1,1,iti1)
>      &      +(bnew1(2,1,iti1)+bnew1(3,1,iti1)*cost2)*cost2
>           b1i1(2)=bnew1(1,2,iti1)
>      &      +(bnew1(2,2,iti1)+bnew1(3,2,iti1)*cost2)*cost2
> 
>           gtb1i1(1)=bnew1(2,1,iti1)+2*bnew1(3,1,iti1)*cost2
>           gtb1i1(2)=bnew1(2,2,iti1)+2*bnew1(3,2,iti1)*cost2
> 
>           b2i(1)=bnew2(1,1,iti)
>      &      +(bnew2(2,1,iti)+bnew2(3,1,iti)*cost1)*cost1
>           b2i(2)=bnew2(1,2,iti)
>      &      +(bnew2(2,2,iti)+bnew2(3,2,iti)*cost1)*cost1
> 
>           gtb2i(1)=bnew2(2,1,iti)+2*bnew2(3,1,iti)*cost1
>           gtb2i(2)=bnew2(2,2,iti)+2*bnew2(3,2,iti)*cost1
> 
127,129c348,350
<           mu(:,i)=b2(1,i)*dC_norm(:,i-1)+b1(1,i+1)*dC_norm(:,i+1)
<      &            +(b2(1,i)*cost1+b1(1,i+1)*cost2)*dC_norm(:,i)
<      &             -b2(2,i)*uz_dc(:,i-1)-b1(2,i+1)*uz_dc(:,i)
---
>           mu(:,i)=b2i(1)*dC_norm(:,i-1)+b1i1(1)*dC_norm(:,i+1)
>      &            +(b2i(1)*cost1+b1i1(1)*cost2)*dC_norm(:,i)
>      &             -b2i(2)*uz_dc(:,i-1)-b1i1(2)*uz_dc(:,i)
131,133c352,354
<           aux=vbld_inv(i)*b2(1,i)
<           aux1=cost1*gtb2(1,i)+b2(1,i)
<           aux2=gtb2(1,i)
---
>           aux=vbld_inv(i)*b2i(1)
>           aux1=cost1*gtb2i(1)+b2i(1)
>           aux2=gtb2i(1)
136,137c357,358
<           auxvec=vbld_inv(i)*b2(2,i)*dC_norm(:,i-1)
<           auxvec1=vbld_inv(i)*b2(2,i)*dC_norm(:,i)
---
>           auxvec=vbld_inv(i)*b2i(2)*dC_norm(:,i-1)
>           auxvec1=vbld_inv(i)*b2i(2)*dC_norm(:,i)
140c361
<           muder(:,:,1,i)=muder(:,:,1,i)-mudertemp(:,:)
---
>           muder(:,:,1,i)=-mudertemp(:,:)
141a363,365
>           auxvec=aux1*dC_norm(:,i)
>      &          +aux2*dC_norm(:,i-1)
>      &          -gtb2i(2)*uz_dc(:,i-1)
146,148c370,375
<      &         dC_norm(k,i)*dcosttab(j,1,i+1)*aux1+
<      &         dC_norm(k,i-1)*dcosttab(j,1,i+1)*aux2-
<      &         gtb2(2,i)*uz_dc(k,i-1)*dcosttab(j,1,i+1)
---
> !    &         dC_norm(k,i)*dcosttab(j,1,i+1)*aux1+
> !    &         dC_norm(k,i-1)*dcosttab(j,1,i+1)*aux2-
> !    &         gtb2i(2)*uz_dc(k,i-1)*dcosttab(j,1,i+1)
> !-   &         (dC_norm(k,i)*aux1+dC_norm(k,i-1)*aux2
> !-   &          -gtb2i(2)*uz_dc(k,i-1))*dcosttab(j,1,i+1)
>      &         auxvec(k)*dcosttab(j,1,i+1)
151a379
> 
153,155c381,383
<           aux=vbld_inv(i+2)*b1(1,i+1)
<           aux1=cost2*gtb1(1,i+1)+b1(1,i+1)
<           aux2=gtb1(1,i+1)
---
>           aux=vbld_inv(i+2)*b1i1(1)
>           aux1=cost2*gtb1i1(1)+b1i1(1)
>           aux2=gtb1i1(1)
158,159c386,387
<           auxvec=-vbld_inv(i+2)*b1(2,i+1)*dC_norm(:,i+1)
<           auxvec1=vbld_inv(i+2)*b1(2,i+1)*dC_norm(:,i)
---
>           auxvec=-vbld_inv(i+2)*b1i1(2)*dC_norm(:,i+1)
>           auxvec1=vbld_inv(i+2)*b1i1(2)*dC_norm(:,i)
162c390
<           muder(:,:,3,i)=muder(:,:,3,i)+mudertemp(:,:)
---
>           muder(:,:,3,i)=mudertemp(:,:)
165a394,396
>           auxvec=aux1*dC_norm(:,i)
>      &          +aux2*dC_norm(:,i+1)
>      &          -gtb1i1(2)*uz_dc(:,i)
170,172c401,406
<      &         dC_norm(k,i)*dcosttab(j,2,i+2)*aux1+
<      &         dC_norm(k,i+1)*dcosttab(j,2,i+2)*aux2-
<      &         gtb1(2,i+1)*uz_dc(k,i)*dcosttab(j,2,i+2)
---
> !    &         dC_norm(k,i)*dcosttab(j,2,i+2)*aux1+
> !    &         dC_norm(k,i+1)*dcosttab(j,2,i+2)*aux2-
> !    &         gtb1i1(2)*uz_dc(k,i)*dcosttab(j,2,i+2)
> !-   &         (dC_norm(k,i)*aux1+dC_norm(k,i+1)*aux2
> !-   &          -gtb1i1(2)*uz_dc(k,i))*dcosttab(j,2,i+2)
>      &         auxvec(k)*dcosttab(j,2,i+2)
175a410
> 
177,179c412,414
<           aux=vbld_inv(i+1)*(b2(1,i)*cost1+b1(1,i+1)*cost2)
<           aux1=cost1*gtb2(1,i)+b2(1,i)
<           aux2=cost2*gtb1(1,i+1)+b1(1,i+1)
---
>           aux=vbld_inv(i+1)*(b2i(1)*cost1+b1i1(1)*cost2)
>           aux1=cost1*gtb2i(1)+b2i(1)
>           aux2=cost2*gtb1i1(1)+b1i1(1)
181,182c416,417
<           auxvec=-vbld_inv(i+1)*b2(2,i)*dC_norm(:,i)
<           auxvec1=vbld_inv(i+1)*b2(2,i)*dC_norm(:,i-1)
---
>           auxvec=-vbld_inv(i+1)*b2i(2)*dC_norm(:,i)
>           auxvec1=vbld_inv(i+1)*b2i(2)*dC_norm(:,i-1)
185,187c420,422
<           muder(:,:,2,i)=muder(:,:,2,i)+mudertemp(:,:)
<           auxvec=vbld_inv(i+1)*b1(2,i+1)*dC_norm(:,i)
<           auxvec1=vbld_inv(i+1)*b1(2,i+1)*dC_norm(:,i+1)
---
>           muder(:,:,2,i)=mudertemp(:,:)
>           auxvec=vbld_inv(i+1)*b1i1(2)*dC_norm(:,i)
>           auxvec1=vbld_inv(i+1)*b1i1(2)*dC_norm(:,i+1)
191a427,433
>           
>           auxvec=aux1*dC_norm(:,i)
>      &          +gtb2i(1)*dC_norm(:,i-1)
>      &          -gtb2i(2)*uz_dc(:,i-1)
>           auxvec1=aux2*dC_norm(:,i)
>      &           +gtb1i1(1)*dC_norm(:,i+1)
>      &           -gtb1i1(2)*uz_dc(:,i)
195,201c437,446
<      &         gtb2(1,i)*dC_norm(k,i-1)*dcosttab(j,2,i+1)-
<      &         dC_norm(k,i)*dC_norm(j,i)*aux+
<      &         dC_norm(k,i)*dcosttab(j,2,i+1)*aux1+
<      &         dC_norm(k,i)*dcosttab(j,1,i+2)*aux2+
<      &         gtb1(1,i+1)*dC_norm(k,i+1)*dcosttab(j,1,i+2)-
<      &         gtb2(2,i)*uz_dc(k,i-1)*dcosttab(j,2,i+1)-
<      &         gtb1(2,i+1)*uz_dc(k,i)*dcosttab(j,1,i+2)
---
> !    &         gtb2i(1)*dC_norm(k,i-1)*dcosttab(j,2,i+1)-
> !    &         dC_norm(k,i)*dC_norm(j,i)*aux+
> !    &         dC_norm(k,i)*dcosttab(j,2,i+1)*aux1+
> !    &         dC_norm(k,i)*dcosttab(j,1,i+2)*aux2+
> !    &         gtb1i1(1)*dC_norm(k,i+1)*dcosttab(j,1,i+2)-
> !    &         gtb2i(2)*uz_dc(k,i-1)*dcosttab(j,2,i+1)-
> !    &         gtb1i1(2)*uz_dc(k,i)*dcosttab(j,1,i+2)
>      &         -dC_norm(k,i)*dC_norm(j,i)*aux
>      &         +auxvec(k)*dcosttab(j,2,i+1)
>      &         +auxvec1(k)*dcosttab(j,1,i+2)
204a450
> 
218,220c464,466
<      &       -b2(1,i),b2(2,i),b1(1,i+1),b1(2,i+1),
<      &       dsqrt(b2(1,i)**2+b2(2,i)**2)
<      &      +dsqrt(b1(1,i+1)**2+b1(2,i+1)**2),
---
>      &       -b2i(1),b2i(2),b1i1(1),b1i1(2),
>      &       dsqrt(b2i(1)**2+b2i(2)**2)
>      &      +dsqrt(b1i1(1)**2+b1i1(2)**2),
294a541,819
> !! ALTERNATE IMPLEMENTATION TO CHECK IF I UNDERSTAND IT CORRECLTY
> !! ungrad = unorm * (I - u * u^t) * ugrad
> !!
> !!                   |1 0 0|   |.|          |       |
> !! ungrad = unorm * (|0 1 0| - |u|*|.u.|) * | ugrad |
> !!                   |0 0 1|   |.|          |       |
> !!
> !!    double precision u(3),ugrad(3,3),unorm,ungrad(3,3)
> !!    double precision uu(3,3)
> !!    integer i,j,k
> !!    do i=1,3
> !!      do j=1,3
> !!        uu(i,j)=-u(i)*u(j)
> !!      enddo
> !!      uu(i,i)=uu(i,i)+1.0d0
> !!    enddo
> !!    do i=1,3
> !!      do j=1,3
> !!        ungrad(i,j)=0.0d0
> !!        do k=1,3
> !!          ungrad(i,j)=ungrad(i,j)+uu(i,k)*ugrad(k,j)
> !!        enddo
> !!        ungrad(i,j)=ungrad(i,j)*unorm
> !!      enddo
> !!    enddo
>       return
>       end
> C-----------------------------------------------------------------------
>       subroutine scale_by_muder1(i,cost1,iti,ggg,gggo)
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       include 'COMMON.LOCAL'
>       include 'COMMON.TORSION'
>       double precision ggg(3),gggo(3)
>       double precision cost1,b2i1,b2i2,gtb2i1,gtb2i2
>       double precision aux,aux1,aux2,s1,s2,vi
>       integer i,iti
>       double precision scalar
> 
>       b2i1=bnew2(1,1,iti)+(bnew2(2,1,iti)+bnew2(3,1,iti)*cost1)*cost1
>       b2i2=bnew2(1,2,iti)+(bnew2(2,2,iti)+bnew2(3,2,iti)*cost1)*cost1
> 
>       call vecpr(ggg(:),dc_norm(:,i),gggo(:))
> 
>       gtb2i1=bnew2(2,1,iti)+2*bnew2(3,1,iti)*cost1
>       gtb2i2=bnew2(2,2,iti)+2*bnew2(3,2,iti)*cost1
>       
>       aux=scalar(dc_norm(:,i-1),ggg(:))
>       aux1=scalar(dc_norm(:,i),ggg(:))
>       aux2=-scalar(dc_norm(:,i-1),gggo(:))
> 
>       s2=(cost1*gtb2i1+b2i1)*aux1+gtb2i1*aux-gtb2i2*aux2
> 
>       s1=b2i2*aux2-b2i1*aux
> 
>       s1=s1-cost1*s2
>       vi=vbld_inv(i)
> 
>       gggo(1)=vi*(b2i2*gggo(1)+dc_norm(1,i-1)*s1
>      &  -dc_norm(1,i)*s2+b2i1*ggg(1))
>       gggo(2)=vi*(b2i2*gggo(2)+dc_norm(2,i-1)*s1
>      &  -dc_norm(2,i)*s2+b2i1*ggg(2))
>       gggo(3)=vi*(b2i2*gggo(3)+dc_norm(3,i-1)*s1
>      &  -dc_norm(3,i)*s2+b2i1*ggg(3))
>       return
>       end
> C-----------------------------------------------------------------------
>       subroutine scale_by_muder3(i,cost2,iti,ggg,gggo)
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       include 'COMMON.LOCAL'
>       include 'COMMON.TORSION'
>       double precision ggg(3),gggo(3)
>       double precision cost2,b1i1(2),gtb1i1(2),dcn1(3)
>       double precision aux1
>       double precision nig,ni1g,dcg,vi,s1,s2
>       double precision scalar
>       integer i,iti
> 
>       b1i1(1)=bnew1(1,1,iti)+(bnew1(2,1,iti)+bnew1(3,1,iti)*cost2)*cost2
>       b1i1(2)=bnew1(1,2,iti)+(bnew1(2,2,iti)+bnew1(3,2,iti)*cost2)*cost2
>       gtb1i1(1)=bnew1(2,1,iti)+2*bnew1(3,1,iti)*cost2
>       gtb1i1(2)=bnew1(2,2,iti)+2*bnew1(3,2,iti)*cost2
> 
>       aux1=cost2*gtb1i1(1)+b1i1(1)
> 
>       dcn1(:)=dc_norm(:,i+1)
> 
>       call vecpr(dc_norm(:,i),ggg(:),gggo(:))
> 
>       nig=scalar(dc_norm(:,i),ggg(:))
>       ni1g=scalar(dc_norm(:,i+1),ggg(:))
> 
>       vi=vbld_inv(i+2)
> 
>       dcg=-scalar(dc_norm(:,i+1),gggo(:))
>       s1=aux1*nig+gtb1i1(1)*ni1g-gtb1i1(2)*dcg
>       s2=b1i1(2)*dcg-b1i1(1)*ni1g-cost2*s1
> 
>       gggo(1)=vi*
>      &  (b1i1(2)*gggo(1)+dcn1(1)*s2-dc_norm(1,i)*s1+ggg(1)*b1i1(1))
>       gggo(2)=vi*
>      &  (b1i1(2)*gggo(2)+dcn1(2)*s2-dc_norm(2,i)*s1+ggg(2)*b1i1(1))
>       gggo(3)=vi*
>      &  (b1i1(2)*gggo(3)+dcn1(3)*s2-dc_norm(3,i)*s1+ggg(3)*b1i1(1))
>       return
>       end
> C-----------------------------------------------------------------------
>       subroutine scale_by_muder2(i,cost1,cost2,iti,iti1,ggg,gggo)
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       include 'COMMON.LOCAL'
>       include 'COMMON.TORSION'
>       double precision ggg(3),gggo(3)
>       double precision cost1,cost2
>       double precision b1i1(2),gtb1i1(2),b2i(2),gtb2i(2)
>       double precision aux,aux1,aux2
>       double precision dcn(3),dcm(3),dcp(3)
>       double precision g_np(3),g_nm(3)
>       double precision ng,npg,nmg,uz1g,uzg,s,av1,av2
>       double precision vi
>       integer i,iti,iti1
>       double precision scalar
> 
>       vi = vbld_inv(i+1)
> 
>       b2i(1)=bnew2(1,1,iti)
>      &  +(bnew2(2,1,iti)+bnew2(3,1,iti)*cost1)*cost1
>       b2i(2)=bnew2(1,2,iti)
>      &  +(bnew2(2,2,iti)+bnew2(3,2,iti)*cost1)*cost1
> 
>       b1i1(1)=bnew1(1,1,iti1)
>      &  +(bnew1(2,1,iti1)+bnew1(3,1,iti1)*cost2)*cost2
>       b1i1(2)=bnew1(1,2,iti1)
>      &  +(bnew1(2,2,iti1)+bnew1(3,2,iti1)*cost2)*cost2
> 
>       gtb1i1(1)=bnew1(2,1,iti1)+2*bnew1(3,1,iti1)*cost2
>       gtb1i1(2)=bnew1(2,2,iti1)+2*bnew1(3,2,iti1)*cost2
>       gtb2i(1)=bnew2(2,1,iti)+2*bnew2(3,1,iti)*cost1
>       gtb2i(2)=bnew2(2,2,iti)+2*bnew2(3,2,iti)*cost1
>  
>       aux=b2i(1)*cost1+b1i1(1)*cost2
>       aux1=cost1*gtb2i(1)+b2i(1)
>       aux2=cost2*gtb1i1(1)+b1i1(1)
> 
>       dcn(:)=dc_norm(:,i)
>       dcm(:)=dc_norm(:,i-1)
>       dcp(:)=dc_norm(:,i+1)
> 
>       ng=scalar(dc_norm(:,i),ggg)
>       npg=scalar(dc_norm(:,i+1),ggg)
>       nmg=scalar(dc_norm(:,i-1),ggg)
> 
>       call vecpr(ggg(:),dc_norm(:,i-1),g_nm(:))
>       call vecpr(ggg(:),dc_norm(:,i+1),g_np(:))
>       uzg=-scalar(g_np(:),dc_norm(:,i))
>       uz1g=scalar(g_nm(:),dc_norm(:,i))
> 
>       s=b2i(2)*uz1g+b1i1(2)*uzg-ng*aux
>       av2=aux1*ng+gtb2i(1)*nmg-gtb2i(2)*uz1g
>       av1=aux2*ng+gtb1i1(1)*npg-gtb1i1(2)*uzg
> 
>       gggo(:)=-b2i(2)*g_nm(:)+b1i1(2)*g_np(:)
> 
>       s=s-av1*cost2-av2*cost1
>       gggo(1)=vi*(gggo(1)+dcn(1)*s-av2*dcm(1)-av1*dcp(1)+aux*ggg(1))
>       gggo(2)=vi*(gggo(2)+dcn(2)*s-av2*dcm(2)-av1*dcp(2)+aux*ggg(2))
>       gggo(3)=vi*(gggo(3)+dcn(3)*s-av2*dcm(3)-av1*dcp(3)+aux*ggg(3))
>       return
>       end
> C-----------------------------------------------------------------------
>       subroutine scale_by_muder(i,ggg1,ggg2,ggg3,gggo)
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       include 'COMMON.LOCAL'
>       include 'COMMON.TORSION'
>       include 'COMMON.OMP'
>       include 'COMMON.VAR'
>       double precision ggg1(3),ggg2(3),ggg3(3),gggo(3)
>       double precision cost1,cost2
>       integer i,iti,itj
> 
>       double precision aux,vi
>       double precision dcn(3),dcm(3),dcp(3)
> 
>       double precision b1i1,b1i2,b1j1,b1j2
>       double precision b2i1,b2i2,b2j1,b2j2
>       double precision gtb1i1,gtb1i2,gtb1j1,gtb1j2
>       double precision gtb2i1,gtb2i2,gtb2j1,gtb2j2
> 
>       double precision sn,sp,sm
>       double precision ng1,npg1
>       double precision ng2,npg2,nmg2
>       double precision ng3,nmg3
> 
>       double precision g1_np(3),g2_np(3),g2_nm(3),g3_nm(3)
>       double precision tp1,tp2p,tp2m,tp3
> 
>       double precision scalar
> 
>       iti=iti_tab(i)
>       itj=iti_tab(i+1)
>       cost1=costtab(i+1)
>       cost2=costtab(i+2)
>       !vi=vbld_inv(i+1)
>       dcn(:)=dc_norm(:,i)
>       dcm(:)=dc_norm(:,i-1)
>       dcp(:)=dc_norm(:,i+1)
> 
>       !write(*,'(a,i3,a,2i3,3f16.13)')'H',i,' 1 ',iti,itj,cost1,cost2,vi
>       !write(*,'(a,i3,a,3f18.15)')'H',i,' 2 ',dcm
>       !write(*,'(a,i3,a,3f18.15)')'H',i,' 3 ',dcn
>       !write(*,'(a,i3,a,3f18.15)')'H',i,' 4 ',dcp
> 
>       b1i1=bnew1(1,1,iti)+(bnew1(2,1,iti)+bnew1(3,1,iti)*cost1)*cost1
>       b1i2=bnew1(1,2,iti)+(bnew1(2,2,iti)+bnew1(3,2,iti)*cost1)*cost1
>       gtb1i1=bnew1(2,1,iti)+2*bnew1(3,1,iti)*cost1
>       gtb1i2=bnew1(2,2,iti)+2*bnew1(3,2,iti)*cost1
> 
>       b1j1=bnew1(1,1,itj)+(bnew1(2,1,itj)+bnew1(3,1,itj)*cost2)*cost2
>       b1j2=bnew1(1,2,itj)+(bnew1(2,2,itj)+bnew1(3,2,itj)*cost2)*cost2
>       gtb1j1=bnew1(2,1,itj)+2*bnew1(3,1,itj)*cost2
>       gtb1j2=bnew1(2,2,itj)+2*bnew1(3,2,itj)*cost2
> 
>       b2i1=bnew2(1,1,iti)+(bnew2(2,1,iti)+bnew2(3,1,iti)*cost1)*cost1
>       b2i2=bnew2(1,2,iti)+(bnew2(2,2,iti)+bnew2(3,2,iti)*cost1)*cost1
>       gtb2i1=bnew2(2,1,iti)+2*bnew2(3,1,iti)*cost1
>       gtb2i2=bnew2(2,2,iti)+2*bnew2(3,2,iti)*cost1
> 
>       b2j1=bnew2(1,1,itj)+(bnew2(2,1,itj)+bnew2(3,1,itj)*cost2)*cost2
>       b2j2=bnew2(1,2,itj)+(bnew2(2,2,itj)+bnew2(3,2,itj)*cost2)*cost2
>       gtb2j1=bnew2(2,1,itj)+2*bnew2(3,1,itj)*cost2
>       gtb2j2=bnew2(2,2,itj)+2*bnew2(3,2,itj)*cost2
> 
>       ng1=scalar(dcn(:),ggg1(:))
>       npg1=scalar(dcp(:),ggg1(:))
> 
>       ng2=scalar(dcn(:),ggg2(:))
>       npg2=scalar(dcp(:),ggg2(:))
>       nmg2=scalar(dcm(:),ggg2(:))
> 
>       nmg3=scalar(dcm(:),ggg3(:))
>       ng3=scalar(dcn(:),ggg3(:))
> 
>       call vecpr(ggg1(:),dcp(:),g1_np(:))
>       tp1=scalar(dcn(:),g1_np(:))
> 
>       call vecpr(ggg2(:),dcm(:),g2_nm(:))
>       call vecpr(ggg2(:),dcp(:),g2_np(:))
>       tp2p=scalar(dcn(:),g2_np(:))
>       tp2m=scalar(dcn(:),g2_nm(:))
> 
>       call vecpr(ggg3(:),dcm(:),g3_nm(:))
>       tp3=scalar(dcn(:),g3_nm(:))
> 
>       aux=b2i1*cost1+b1j1*cost2
> 
>       sp= (cost2*gtb2j1+b2j1)*npg1+gtb2j1*ng1 +gtb2j2*tp1
>      &   +(cost2*gtb1j1+b1j1)*ng2 +gtb1j1*npg2+gtb1j2*tp2p
> 
>       sm= (cost1*gtb2i1+b2i1)*ng2 +gtb2i1*nmg2-gtb2i2*tp2m
>      &   +(cost1*gtb1i1+b1i1)*nmg3+gtb1i1*ng3 -gtb1i2*tp3
> 
>       sn=-b2j2*tp1-b1j2*tp2p
>      &   +b2i2*tp2m+b1i2*tp3
>      &   -b2j1*ng1-aux*ng2-b1i1*ng3
>      &   -sp*cost2-sm*cost1
> 
>       gggo(:)= b2j2*g1_np(:)+b1j2*g2_np(:)
>      &        -b2i2*g2_nm(:)-b1i2*g3_nm(:)
>      &        +sn*dcn(:)-sm*dcm(:)-sp*dcp(:)
>      &        +b2j1*ggg1(:)+aux*ggg2(:)+b1i1*ggg3(:)
> 
>       !moved outside
>       !gggo(:)=vi*gggo(:)
eelec_scale_save.F
36a37,38
>       include 'COMMON.OMP'
>       include 'COMMON.EELEC'
47,48c49,50
<       integer*1 doturn(maxres)
<       common /eelecscalecommon/doturn
---
>       !integer*1 doturn(maxres)
>       !common /eelecscalecommon/doturn
82c84,85
<         call set_matrices
---
>         !call set_matrices
>         call set_matrices_scale
106c109
<       tail_agg=(wturn3.gt.0.0d0)
---
>       !tail_agg=(wturn3.gt.0.0d0)
114c117
<         if (tail_agg) doturn(i)=or(doturn(i),1)
---
>         !if (tail_agg) doturn(i)=or(doturn(i),1)
121c124
<         tail_agg=(wturn4.gt.0.0d0 .and. itype(i+2).ne.ntyp1)
---
>         !tail_agg=(wturn4.gt.0.0d0 .and. itype(i+2).ne.ntyp1)
125c128
<         if (tail_agg) doturn(i)=or(doturn(i),2)
---
>         !if (tail_agg) doturn(i)=or(doturn(i),2)
127c130,131
<       call calculate_turns(doturn,eello_turn3,eello_turn4)
---
>       !call calculate_turns(doturn,eello_turn3,eello_turn4)
>       call calculate_turns(eelecij_doturn,eello_turn3,eello_turn4)
133a138
> 
499,500d503
<           eel_loc_ij=eel_loc_ij
<      &    *sss1*faclipij
503a507,508
>           eel_loc_ij=eel_loc_ij
>      &    *sss1*faclipij
590a596,602
> !       write(*,'(a,2i5,3f18.12)')'SAI',i,j,cp(:,i)
> !       write(*,'(a,2i5,3f18.12)')'SAJ',i,j,cp(:,j)
> !       write(*,'(a,2i5,3f18.12)')'SBI',i,j,mu(:,i)
> !       write(*,'(a,2i5,3f18.12)')'SBJ',i,j,mu(:,j)
> !       write(*,'(a,2i5,3f18.12)')'SC',i,j,fac,rij,faclipij
> !       write(*,'(a,2i5,3f18.12)')'SD',i,j,sss1,auxvec
> !       write(*,'(a,2i5,3f18.12)')'SE',i,j,eel_loc_ij,muier,mujer
eelec_t.F
1,3c1,2
<       subroutine EELECIJ(eelecij_work,num_items,offset,
<      &                   threads_used,scal_el)
< #ifdef _OPENMP
---
>       subroutine EELECIJ_DELTA(ranges,num_items,offset,
>      &                         threads_used,scal_el)
5c4
< #endif
---
>       use omp_data
8,9c7
<       include 'COMMON.CHAIN'
<       include 'COMMON.SPLITELE'
---
>       include 'COMMON.OMP'
15,24c13,17
<       include 'COMMON.LIPID'
<       include 'COMMON.NEWBOX'
<       integer num_items,threads_used,offset
<       double precision xyzj(3),rij,scal_el
<       integer my_thread,range_lo,range_hi,from,to
<       integer lasti,lastj,i,j,item,seq_idx,nseq_idx
<       double precision box(3),halfbox(3)
<       record /eelecij_work_chunk/ seq_work
<       record /eelecij_work_chunk/ nseq_work
<       integer eelecij_work(num_items)
---
>       integer num_items,offset
>       integer threads_used,my_thread
>       double precision scal_el
>       integer item,from,to
>       integer ranges(maxres*2)
26,27d18
<       logical full
<       double precision scalar
36,41c27
< !$OMP& PRIVATE(
< !$OMP&   i,j,xyzj,rij,lasti,lastj,
< !$OMP&   seq_work,seq_idx,nseq_work,nseq_idx,full,
< !$OMP&   box,halfbox,
< !$OMP&   my_thread,item,from,to,th_data
< !$OMP& )
---
> !$OMP& PRIVATE(my_thread,item,from,to,th_data)
47d32
<       call eelec_fill_thread_data(th_data)
49,58c34
<       call split_work_for_threads(from,to,1,num_items,
<      &                            my_thread,threads_used)
< 
< 
<       seq_idx=0
<       nseq_idx=0
<       lasti=0
<       lastj=0
<       box=boxsize
<       halfbox=halfboxsize
---
>       call eelec_fill_thread_data(th_data)
59a36,42
>       if(offset.eq.2) then
>         from=first_block_2(my_thread)
>         to=last_block_2(my_thread)
>       else
>         from=first_block_3(my_thread)
>         to=last_block_3(my_thread)
>       endif
61,137c44,47
<         !i=eelecij_work(item).i
<         !j=eelecij_work(item).j
<         i=eelecij_work(item)
<         j=i+offset
<         LOG_STAT(300,1)
<         !call wrapvec(cp(:,j)-cp(:,i),xyzj)
<         call wrapvec2(cp(:,j)-cp(:,i),xyzj,halfbox,box)
<         rij=scalar(xyzj,xyzj)
<         if(rij.ge.r_cut_int_sq) cycle
<         LOG_STAT(301,1)
< 
<         if(i.ne.lasti) then
<           if(seq_idx.gt.0) then
<             call EELEC_SEQ(seq_work,lasti,
<      &                     seq_work.j(1),seq_work.j(seq_idx),
<      &                     eelecij_value(my_thread),
<      &                     eelecij_array(my_thread),
<      &                     th_data,scal_el)
<             LOG_STAT(302,seq_idx)
<             seq_idx=0
<           endif
<           if(nseq_idx.gt.0) then
<             call EELEC_NSEQ(nseq_work,lasti,
<      &                      1,nseq_idx,
<      &                      eelecij_value(my_thread),
<      &                      eelecij_array(my_thread),
<      &                      th_data,scal_el)
<             LOG_STAT(303,nseq_idx)
<             nseq_idx=0
<           endif
<           lasti=i
<           lastj=-1
<         endif
< 
<         if((j.eq.lastj+1).or.(seq_idx.eq.0)) then
<           call eelec_queue_work(seq_work,seq_idx,i,j,
<      &                          xyzj,rij,full)
<           lastj=j
<           if(full) then
<             call EELEC_SEQ(seq_work,lasti,
<      &                     seq_work.j(1),seq_work.j(seq_idx),
<      &                     eelecij_value(my_thread),
<      &                     eelecij_array(my_thread),
<      &                     th_data,scal_el)
<             LOG_STAT(302,seq_idx)
<             seq_idx=0
<             lastj=-1
<           endif
<         else
<           if((seq_idx.lt.simd_seq_cutoff) .and.
<      &       (nseq_idx+seq_idx.lt.eelec_buffer_size)) then
<             LOG_STAT(304,seq_idx)
<             call eelecij_move_work(seq_work,seq_idx,
<      &                             nseq_work,nseq_idx)
<           else
<             call EELEC_SEQ(seq_work,lasti,
<      &                     seq_work.j(1),seq_work.j(seq_idx),
<      &                     eelecij_value(my_thread),
<      &                     eelecij_array(my_thread),
<      &                     th_data,scal_el)
<             LOG_STAT(302,seq_idx)
<             seq_idx=0
<           endif
<           call eelec_queue_work(nseq_work,nseq_idx,i,j,
<      &                          xyzj,rij,full)
<           lastj=j
<           if(full) then
<             call EELEC_NSEQ(nseq_work,lasti,
<      &                      1,nseq_idx,
<      &                      eelecij_value(my_thread),
<      &                      eelecij_array(my_thread),
<      &                      th_data,scal_el)
<             LOG_STAT(303,nseq_idx)
<             nseq_idx=0
<             lastj=-1
<           endif
<         endif
---
>         call EELECIJ_WORK_PAIR(
>      &    offset,ranges(item*2+0),ranges(item*2+1),
>      &    eelecij_value(my_thread),workspace(my_thread).item,
>      &    th_data,scal_el)
139,155d48
< 
<       if(seq_idx.gt.0) then
<         call EELEC_SEQ(seq_work,lasti,
<      &                 seq_work.j(1),seq_work.j(seq_idx),
<      &                 eelecij_value(my_thread),
<      &                 eelecij_array(my_thread),
<      &                 th_data,scal_el)
<         LOG_STAT(302,seq_idx)
<       endif
<       if(nseq_idx.gt.0) then
<         call EELEC_NSEQ(nseq_work,lasti,
<      &                  1,nseq_idx,
<      &                  eelecij_value(my_thread),
<      &                  eelecij_array(my_thread),
<      &                  th_data,scal_el)
<         LOG_STAT(303,nseq_idx)
<       endif
156a50
> 
158a53
> 
160,161c55
<       subroutine EELECIJ2(threads_used)
< #ifdef _OPENMP
---
>       subroutine EELECIJ(threads_used)
163c57
< #endif
---
>       use omp_data
166d59
<       include 'COMMON.CHAIN'
168c61
<       include 'COMMON.SPLITELE'
---
>       include 'COMMON.OMP'
174,176c67
<       include 'COMMON.LIPID'
<       include 'COMMON.NEWBOX'
<       integer threads_used
---
>       integer threads_used,my_thread
178,180d68
<       double precision xyzj(3),rij
<       integer my_thread,from,to
<       integer lastj,i,j,ikont,jblock,item,seq_idx,nseq_idx
182,184c70
<       double precision box(3),halfbox(3)
<       record /eelecij_work_chunk/ seq_work
<       record /eelecij_work_chunk/ nseq_work
---
>       integer i,ikont,jblock
186,187d71
<       logical full,limited
<       double precision scalar
194,195c78
< !$OMP&   i,j,ikont,jblock,xyzj,rij,lastj,from_j,to_j,
< !$OMP&   seq_work,seq_idx,nseq_work,nseq_idx,full,
---
> !$OMP&   i,ikont,jblock,from_j,to_j,
197,198c80
< !$OMP&   box,halfbox,
< !$OMP&   my_thread,item,from,to,from_blk,to_blk,th_data
---
> !$OMP&   my_thread,from_blk,to_blk,th_data
207,218c89,101
<       seq_idx=0
<       nseq_idx=0
<       lastj=0
< 
<       from_ik=pp_from_ik(my_thread)
<       to_ik=pp_to_ik(my_thread)
<       first_blk=pp_first_blk(my_thread)
<       last_blk=pp_last_blk(my_thread)
<       first_j=pp_first_j(my_thread)
<       last_j=pp_last_j(my_thread)
<       box=boxsize
<       halfbox=halfboxsize
---
>       ! interactions in ranges
>       !from_ik=pp_from_ik(my_thread)
>       !to_ik=pp_to_ik(my_thread)
>       !first_blk=pp_first_blk(my_thread)
>       !last_blk=pp_last_blk(my_thread)
>       !first_j=pp_first_j(my_thread)
>       !last_j=pp_last_j(my_thread)
>       from_ik=pp_range(my_thread).rng_start
>       to_ik=pp_range(my_thread).rng_end
>       first_blk=pp_range(my_thread).rng_first
>       last_blk=pp_range(my_thread).rng_last
>       first_j=pp_range(my_thread).rng_first_j
>       last_j=pp_range(my_thread).rng_last_j
228,229d110
<         lastj=-1
< 
233,234c114,115
<           if((ikont.eq.from_ik).and.(jblock.eq.from_blk)) from_j=first_j
<           if((ikont.eq.to_ik).and.(jblock.eq.to_blk)) to_j=last_j
---
>           if(jblock.eq.first_blk) from_j=first_j
>           if(jblock.eq.last_blk) to_j=last_j
236,288c117,121
<           do j=from_j,to_j
<             LOG_STAT(300,1)
<             !call wrapvec(cp(:,j)-cp(:,i),xyzj)
<             call wrapvec2(cp(:,j)-cp(:,i),xyzj,halfbox,box)
<             rij=scalar(xyzj,xyzj)
<             if(rij.ge.r_cut_int_sq) cycle
<             LOG_STAT(301,1)
< 
<             if((j.eq.lastj+1).or.(seq_idx.eq.0)) then
<               call eelec_queue_work(seq_work,seq_idx,i,j,
<      &                              xyzj,rij,full)
<               lastj=j
<               if(full) then
<                 call EELEC_SEQ(seq_work,i,
<      &                         seq_work.j(1),seq_work.j(seq_idx),
<      &                         eelecij_value(my_thread),
<      &                         eelecij_array(my_thread),
<      &                         th_data,1.0d0)
<                 LOG_STAT(302,seq_idx)
<                 seq_idx=0
<                 lastj=-1
<               endif
<             else
<               if((seq_idx.lt.simd_seq_cutoff) .and.
<      &           (nseq_idx+seq_idx.lt.eelec_buffer_size)) then
<                 LOG_STAT(304,seq_idx)
<                 call eelecij_move_work(seq_work,seq_idx,
<      &                                 nseq_work,nseq_idx)
<               else
<                 call EELEC_SEQ(seq_work,i,
<      &                         seq_work.j(1),seq_work.j(seq_idx),
<      &                         eelecij_value(my_thread),
<      &                         eelecij_array(my_thread),
<      &                         th_data,1.0d0)
<                 LOG_STAT(302,seq_idx)
<                 seq_idx=0
<               endif
<               call eelec_queue_work(nseq_work,nseq_idx,
<      &                              i,j,xyzj,rij,
<      &                              full)
<               lastj=j
<               if(full) then
<                 call EELEC_NSEQ(nseq_work,i,
<      &                          1,nseq_idx,
<      &                          eelecij_value(my_thread),
<      &                          eelecij_array(my_thread),
<      &                          th_data,1.0d0)
<                 LOG_STAT(303,nseq_idx)
<                 nseq_idx=0
<                 lastj=-1
<               endif
<             endif
<           enddo  ! j
---
> !DIR$ NOINLINE
>           call EELECIJ_WORK_SEQ(
>      &      i,from_j,to_j,
>      &      eelecij_value(my_thread),workspace(my_thread).item,
>      &      th_data,1.0d0)
290,308d122
< 
<         if(seq_idx.gt.0) then
<           call EELEC_SEQ(seq_work,i,
<      &                   seq_work.j(1),seq_work.j(seq_idx),
<      &                   eelecij_value(my_thread),
<      &                   eelecij_array(my_thread),
<      &                   th_data,1.0d0)
<           LOG_STAT(302,seq_idx)
<           seq_idx=0
<         endif
<         if(nseq_idx.gt.0) then
<           call EELEC_NSEQ(nseq_work,i,
<      &                    1,nseq_idx,
<      &                    eelecij_value(my_thread),
<      &                    eelecij_array(my_thread),
<      &                    th_data,1.0d0)
<           LOG_STAT(303,nseq_idx)
<           nseq_idx=0
<         endif
309a124,144
> 
>       ! interactions in separate pairs
>       from_ik=pp_range(my_thread).sep_start
>       to_ik=pp_range(my_thread).sep_end
>       first_j=pp_range(my_thread).sep_first
>       last_j=pp_range(my_thread).sep_last
> 
>       do i=from_ik,to_ik
>         from_j=ppseplisti(i-1)+1
>         to_j=ppseplisti(i)
> 
>         if(i.eq.from_ik) from_j=first_j
>         if(i.eq.to_ik) to_j=last_j
>         if (from_j.gt.to_j) cycle
> 
> !DIR$ NOINLINE
>         call EELECIJ_WORK_NSEQ(
>      &    i,from_j,to_j,
>      &    eelecij_value(my_thread),workspace(my_thread).item,
>      &    th_data,1.0d0)
>       enddo  ! i
313a149,169
> 
> C-----------------------------------------------------------------------
> #define EELECIJ_WORK_FN     EELECIJ_WORK_SEQ
> #define EELECIJ_MAP_J(seq)  seq
> #include"eelec_work.F"
> #undef EELECIJ_MAP_J
> #undef EELECIJ_WORK_FN
> 
> #define EELECIJ_WORK_FN     EELECIJ_WORK_NSEQ
> #define EELECIJ_MAP_J(seq)  ppseplistj(seq)
> #include"eelec_work.F"
> #undef EELECIJ_MAP_J
> #undef EELECIJ_WORK_FN
> 
> #define EELECIJ_WORK_FN     EELECIJ_WORK_PAIR
> #define EELECIJ_MAP_I(seq)  seq
> #define EELECIJ_MAP_J(seq)  (seq+basei)
> #include"eelec_work.F"
> #undef EELECIJ_MAP_J
> #undef EELECIJ_MAP_I
> #undef EELECIJ_WORK_FN
eelec_work.F
1,2c1,3
<       subroutine EELEC_WORK_FN(chunk,i,from,to,i_value,output,
<      &                         th_data,scal_el)
---
>       subroutine EELECIJ_WORK_FN(basei,from,to,i_value,output,
>      &                           th_data,scal_el)
>       use omp_data
19a21
>       include 'COMMON.OMP'
27d28
<       record /eelecij_work_chunk/ chunk
29c30
<       record /eelecij_array_delta/ output
---
>       type(workspace_type) output
31c32
<       integer seq,from,to,i,j,idx
---
>       integer seq,from,to,basei,i,j,idx
34c35
<       double precision ggg(3),gggi(3),erij(3),dcosb(3),dcosg(3)
---
>       double precision erij(3)!,dcosb(3),dcosg(3)
36c37
<       double precision ggg1(3),ggg3(3),ggg4(3),auxvec(3)
---
>       double precision auxveci(3),auxvecj(3)
41,47c42,47
<       double precision aux,auxa,auxb,auxg,gamm,
<      &  rij,r3ij,r6ij,cosa,cosb,cosg,fac,ev1,ev2,fac3,fac4,
<      &  evdwij,el1,el2,eesij,facvdw,facel,fac1,ecosa,
<      &  ecosb,ecosg,ury,urz,vry,vrz,facr,eel_loc_ij,
<      &  eel_loc_ij_o,eel_loc_ij_1
<       double precision muimuj,muier,mujer
<       double precision geel_loc_delta
---
>       double precision aux,auxa,auxb,auxg,aux2,gamm,gamm2,
>      &  esss,esssgrad,esss_rmij_3,fac,fac1,fac3,fac4,
>      &  rij,r3ij,r6ij,cosa,cosb,cosg,ev1,ev2,evdwij,
>      &  el1,el2,eesij,facvdw,facel,
>      &  ecosa,ecosb,ecosg,eel_loc_ij,eel_loc_ij_o
>       double precision muier,mujer
49c49,50
<       double precision dc_norm_i(3),dc_norm_j(3)
---
>       double precision dc_norm_i(3),dc_norm_j(3),cp_i(3)
>       double precision gradj(3),erij_f
52,56c53,54
<       double precision d_gvdwppx,d_gvdwppy,d_gvdwppz
<       double precision d_gelc_longx,d_gelc_longy,d_gelc_longz
<       double precision d_gelc(3)
<       double precision d_gel_loc_longx,d_gel_loc_longy,d_gel_loc_longz
<       double precision d_auxvec(3)
---
>       double precision gradix,gradiy,gradiz,gradilip
>       double precision d_gelc(3),d_auxvec(3)
57a56,57
>       double precision droff,irlamb2,cutoffsq,rij2
>       double precision gg_lip,erb,erg,vbld_invi,vbld_invj
59,64c59,63
<       d_gvdwppx=0.0d0
<       d_gvdwppy=0.0d0
<       d_gvdwppz=0.0d0
<       d_gelc_longx=0.0d0
<       d_gelc_longy=0.0d0
<       d_gelc_longz=0.0d0
---
> 
>       gradix=0.0d0
>       gradiy=0.0d0
>       gradiz=0.0d0
>       gradilip=0.0d0
66,68d64
<       d_gel_loc_longx=0.0d0
<       d_gel_loc_longy=0.0d0
<       d_gel_loc_longz=0.0d0
73c69,83
< 
---
>       
>       cutoffsq=(r_cut_int-rlamb)*(r_cut_int-rlamb)
>       droff=r_dropoff*irlamb
>       irlamb2=irlamb*6.0d0
> 
> #ifndef EELECIJ_MAP_I
>       i=basei
>       cp_i(1)=soa_cp_x(i)
>       cp_i(2)=soa_cp_y(i)
>       cp_i(3)=soa_cp_z(i)
>       dc_norm_i(1)=soa_dc_norm_x(i)
>       dc_norm_i(2)=soa_dc_norm_y(i)
>       dc_norm_i(3)=soa_dc_norm_z(i)
>       lipid_i_ss=lipid_cache_ss(i)
>       lipid_i_ssgrad=lipid_cache_ssgrad(i)
75,78d84
< 
<       dc_norm_i=dc_norm(:,i)
<       lipid_i_ss=lipid_cache(i).ss
<       lipid_i_ssgrad=lipid_cache(i).ssgrad
81a88
> #endif
85,89c92,96
< !$OMP& REDUCTION(+:d_gvdwppx,d_gvdwppy,d_gvdwppz,d_gelc,
< !$OMP&             d_gelc_longx,d_gelc_longy,d_gelc_longz,
< !$OMP&             d_gel_loc_longx,d_gel_loc_longy,d_gel_loc_longz,
< !$OMP&             d_auxvec,
< !$OMP&             d_ees,d_evdw1,d_eel_loc)
---
> !$OMP& REDUCTION(+:d_gelc,
> #ifndef EELECIJ_MAP_I
> !$OMP&             gradix,gradiy,gradiz,gradilip,
> #endif
> !$OMP&             d_auxvec,d_ees,d_evdw1,d_eel_loc)
94c101,108
< !$OMP&   j,k,xyzj,erij,rij,rmij,rrmij,r3ij,r6ij,dc_norm_j,
---
> #ifdef EELECIJ_MAP_I
> !$OMP&   i,cp_i,dc_norm_i,lipid_i_ss,lipid_i_ssgrad,
> !$OMP&   gradix,gradiy,gradiz,gradilip,
> #ifdef SHIELD
> !$OMP&   fac_shield_i,
> #endif
> #endif
> !$OMP&   j,k,xyzj,erij,rij,rij2,rmij,rrmij,r3ij,r6ij,dc_norm_j,
96,101c110,115
< !$OMP&   ggg,gggi,ggg1,ggg3,ggg4,gelc_long_di,gelc_long_dj,
< !$OMP&   evdwij,el1,el2,eesij,facvdw,facel,
< !$OMP&   dcosb,dcosg,ecosa,ecosb,ecosg,ury,urz,vry,vrz,eel_loc_ij,
< !$OMP&   faclipij,faclipij2,sss,sssgrad,gamm,aux,auxa,auxb,auxg,
< !$OMP&   eiteli,itelj,muimuj,muier,mujer,auxvec,
< !$OMP&   eel_loc_ij_o,geel_loc_delta
---
> !$OMP&   gelc_long_di,gelc_long_dj,
> !$OMP&   evdwij,el1,el2,eesij,facvdw,facel,esss_rmij_3,
> !$OMP&   ecosa,ecosb,ecosg,eel_loc_ij,
> !$OMP&   faclipij,faclipij2,esss,esssgrad,gamm,aux,auxa,auxb,auxg,aux2,
> !$OMP&   eiteli,itelj,muier,mujer,auxveci,auxvecj,gamm2,erb,erg,
> !$OMP&   eel_loc_ij_o,gg_lip,gradj,erij_f,vbld_invi,vbld_invj
104,113c118,150
<         j=EELEC_MAP_J(seq)
<         idx=EELEC_MAP_IDX(seq)
<         xyzj(1)=chunk.x(idx)
<         xyzj(2)=chunk.y(idx)
<         xyzj(3)=chunk.z(idx)
<         rij=chunk.r(idx)
< #ifdef NAN_DETECT
<         if (isnan(rij)) print *,"eelec_work: NaN in rij",
<      &   idx,rij,xyzj(1),xyzj(2),xyzj(3)
< #endif
---
> #ifdef EELECIJ_MAP_I
>         i=EELECIJ_MAP_I(seq)
>         cp_i(1)=soa_cp_x(i)
>         cp_i(2)=soa_cp_y(i)
>         cp_i(3)=soa_cp_z(i)
>         dc_norm_i(1)=soa_dc_norm_x(i)
>         dc_norm_i(2)=soa_dc_norm_y(i)
>         dc_norm_i(3)=soa_dc_norm_z(i)
>         lipid_i_ss=lipid_cache_ss(i)
>         lipid_i_ssgrad=lipid_cache_ssgrad(i)
>         iteli=itel(i)
>         gradix=0.0d0
>         gradiy=0.0d0
>         gradiz=0.0d0
>         gradilip=0.0d0
> #ifdef SHIELD
>         fac_shield_i=fac_shield(i)
> #endif
> #endif
>         j=EELECIJ_MAP_J(seq)
> 
>         !write(*,*)'eelec pair',i,j
>         !call wrapvec(cp(:,j)-cp_i,xyzj)
>         !call wrapvec2(cp(:,j)-cp(:,i),xyzj,halfbox,box)
>         call wrapvec((/
>      &    soa_cp_x(j)-cp_i(1),
>      &    soa_cp_y(j)-cp_i(2),
>      &    soa_cp_z(j)-cp_i(3)
>      &    /),xyzj)
>         rij2=scalar(xyzj,xyzj)
> 
> !       write(*,'(a,2i4,i2,3f18.12)')'hst:e',i,j,1,cp_i(:)
> 
115a153,154
> #ifdef EELECIJ_MAP_I
>         ! j will equal i+2 only when offset is 2
116a156
> #endif
118,120c158,163
<         aux=(lipid_i_ss+chunk.lipid_j_ss(idx))*0.5d0
<         faclipij=aux*lipscale+1.0d0
<         faclipij2=aux*lipscale**2+1.0d0
---
>         !aux=(lipid_i_ss+lipid_cache(j).ss)*0.5d0
>         !faclipij=aux*lipscale+1.0d0
>         !faclipij2=aux*lipscale**2+1.0d0
>         aux=lipid_i_ss+lipid_cache_ss(j)
>         faclipij=aux+1.0d0
>         faclipij2=aux*lipscale+1.0d0
124,126c167,179
<         rrmij=1.0D0/rij
<         rij=dsqrt(rij)
<         rmij=1.0D0/rij
---
>         !rij=dsqrt(rij2)
>         !rmij=1.0D0/rij
>         !rrmij=rmij*rmij
>         !r3ij=rrmij*rmij
>         !r6ij=r3ij*r3ij  
> 
>         !rrmij=1.0d0/rij2
>         !rmij=dsqrt(rrmij)
>         rmij=real(1.0/sqrt(real(rij2,4)),8)    ! low precision square root
>         rmij=rmij*(1.5d0-0.5d0*rij2*rmij*rmij) ! with single Newton step
>         rrmij=rmij*rmij
> 
>         rij=rij2*rmij
128c181
<         r6ij=r3ij*r3ij  
---
>         r6ij=rrmij*rrmij*rrmij
131,145c184,202
<         if(rij.lt.r_cut_int-rlamb) then
<           sss=1.0d0
<           sssgrad=0.0d0
<         else
<           gamm=(rij-(r_cut_int-rlamb))*irlamb
<           sss=1.0d0+gamm*gamm*(2*gamm-3.0d0)
<           sssgrad=gamm*(6*gamm-6.0d0)*irlamb
<         endif
< #ifdef NAN_DETECT
<         if (isnan(sss)) print *,"eelec_work: NaN in sss",
<      &    gamm,rij,r_cut_int,rlamb,irlamb,sss
< #endif
<         dc_norm_j(1)=dc_norm(1,j)
<         dc_norm_j(2)=dc_norm(2,j)
<         dc_norm_j(3)=dc_norm(3,j)
---
> !       if(rij.lt.r_cut_int-rlamb) then
> !         sss=1.0d0
> !         sssgrad=0.0d0
> !       else
> !         gamm=(rij-(r_cut_int-rlamb))*irlamb
> !         sss=1.0d0+gamm*gamm*(2*gamm-3.0d0)
> !         sssgrad=gamm*(6*gamm-6.0d0)*irlamb
> !       endif
>         !gamm=max(0.0d0,rij*irlamb-droff)
>         !gamm=min(1.0d0,gamm)
>         !gamm2=gamm*gamm
>         !esss=1.0d0+gamm2*(2.0d0*gamm-3.0d0)
>         !esssgrad=(gamm2-gamm)*irlamb2
> 
>         dc_norm_j(1)=soa_dc_norm_x(j)
>         dc_norm_j(2)=soa_dc_norm_y(j)
>         dc_norm_j(3)=soa_dc_norm_z(j)
> 
>         erij=xyzj*rmij
148,150c205,207
<         cosb=scalar(xyzj,dc_norm_i)*rmij
<         cosg=scalar(xyzj,dc_norm_j)*rmij
<         fac=cosa-3.0D0*cosb*cosg
---
>         cosb=scalar(erij,dc_norm_i)
>         cosg=scalar(erij,dc_norm_j)
>         fac=cosa-3.0d0*cosb*cosg
151a209,217
>         ev2=th_data.bbb(itelj,eiteli)*r6ij
>         fac4=th_data.ael3(itelj,eiteli)*r3ij
> #ifdef EELEC_WITH_WEL_LOC
>         fac1=th_data.sqrt_ael6(itelj,eiteli)*r3ij
>         fac3=-fac1*fac1
> #else
>         fac3=th_data.ael6(itelj,eiteli)*r6ij
> #endif
> #ifdef EELECIJ_MAP_I
155,157c221
<         ev2=th_data.bbb(itelj,eiteli)*r6ij
<         fac3=th_data.ael6(itelj,eiteli)*r6ij
<         fac4=th_data.ael3(itelj,eiteli)*r3ij
---
> #endif
159c223
<         el1=fac3*(4.0D0+fac*fac-3.0D0*(cosb*cosb+cosg*cosg))
---
>         el1=fac3*(4.0d0+fac*fac-3.0d0*(cosb*cosb+cosg*cosg))
167,168c231
<         !fsj=fac_shield(j)
<         fsj=chunk.fac_shield_j(idx)
---
>         fsj=fac_shield(j)
192c255,265
<         aux=sss*faclipij2
---
>         esss=1.0d0
>         esssgrad=0.0d0
>         if(rij2.gt.cutoffsq) then
>           gamm=max(0.0d0,rij*irlamb-droff)
>           gamm=min(1.0d0,gamm)
>           gamm2=gamm*gamm
>           esss=1.0d0+gamm2*(2.0d0*gamm-3.0d0)
>           esssgrad=(gamm2-gamm)*irlamb2
>         endif
> 
>         aux=esss*faclipij2
194a268,271
> #ifdef EELECLOG
>         write(*,'(a,i4,i4,3f18.12)')'ELa',i,j,rij,eesij*aux,evdwij*aux
> #endif
>         !write(*,'(a,2i6,3f18.12)')'E ',i,j,evdwij,eesij,esss
204c281
<      &      'evdw1',i,j,evdwij,iteli,itelj,aaa,d_evdw1,sss,rij
---
>      &      'evdw1',i,j,evdwij,iteli,itelj,aaa,d_evdw1,esss,rij
206c283
<      &      fsi,fsj,lipid_i_ss,lipid_cache(j).ss,faclipij,
---
>      &      fsi,fsj,lipid_i_ss,lipid_cache_ss(j),faclipij,
215,218c292,295
<         facvdw=-6*rrmij*(ev1+evdwij)*sss
<         facel=-3*rrmij*(el1+eesij)
<         fac1=fac
<         erij=xyzj*rmij
---
>         !facvdw=-6*rrmij*(ev1+evdwij)*esss
>         !facel=-3*rrmij*(el1+eesij)
>         !fac1=fac
>         !erij=xyzj*rmij
257c334
<      &                  *2.0*sss
---
>      &                  *2.0*esss
260c337
<      &                  +grad_shield_loc(k,ilist,j)*eesij/fsj*2.0*sss
---
>      &                  +grad_shield_loc(k,ilist,j)*eesij/fsj*2.0*esss
267c344
<      &               grad_shield(k,i)*eesij/fsi*2.0*sss
---
>      &               grad_shield(k,i)*eesij/fsi*2.0*esss
269c346
<      &               grad_shield(k,j)*eesij/fsj*2.0*sss
---
>      &               grad_shield(k,j)*eesij/fsj*2.0*esss
271c348
<      &               grad_shield(k,i)*eesij/fsi*2.0*sss
---
>      &               grad_shield(k,i)*eesij/fsi*2.0*esss
273c350
<      &               grad_shield(k,j)*eesij/fsj*2.0*sss
---
>      &               grad_shield(k,j)*eesij/fsj*2.0*esss
277c354
<         lipid_j_ssgrad=chunk.lipid_j_ssgrad(idx)
---
>         !lipid_j_ssgrad=lipid_cache(j).ssgrad*0.5d0*lipscale
279,280c356,360
<         facvdw=(facvdw+sssgrad*rmij*evdwij)*faclipij2
<         ggg=xyzj*facvdw
---
>         esss_rmij_3=3.0d0*esss*rmij
>         !facvdw=(facvdw+esssgrad*rmij*evdwij)*faclipij2
>         
>         facvdw=(esssgrad*evdwij-2*esss_rmij_3*(ev1+evdwij))*faclipij2
>         !ggg=xyzj*facvdw
282,293c362,378
<         auxg=sss*0.5d0*lipscale**2
<         aux=evdwij*auxg
<         output.gvdwppx(j)=output.gvdwppx(j)+ggg(1)
<         output.gvdwppy(j)=output.gvdwppy(j)+ggg(2)
<         output.gvdwppz(j)=output.gvdwppz(j)+(ggg(3)+lipid_j_ssgrad*aux)
<         d_gvdwppx=d_gvdwppx-ggg(1)
<         d_gvdwppy=d_gvdwppy-ggg(2)
<         d_gvdwppz=d_gvdwppz-(ggg(3)-lipid_i_ssgrad*aux)
< 
<         aux=(facel*sss+rmij*sssgrad*eesij)*faclipij2
<         ggg=xyzj*aux
<         eesij=eesij*auxg
---
>         auxg=esss*0.5d0*lipscale*lipscale
>         !aux=evdwij*auxg
> 
>         ! gvdwpp(j)
>         !output.dgradcx(j)=output.dgradcx(j)+wvdwpp*(ggg(1))
>         !output.dgradcy(j)=output.dgradcy(j)+wvdwpp*(ggg(2))
>         !output.dgradcz(j)=output.dgradcz(j)+wvdwpp*(ggg(3)+lipid_j_ssgrad*aux)
>         !erij_f=wvdwpp*facvdw*rij
>         erij_f=wvdwpp*facvdw
> 
>         !gg_lip=wvdwpp*aux
>         gg_lip=(wvdwpp*evdwij+welec*eesij)*auxg
> 
>         !aux=(facel*esss+rmij*esssgrad*eesij)*faclipij2
>         aux2=(esssgrad*eesij-esss_rmij_3*(el1+eesij))*faclipij2
>         !ggg=xyzj*aux
>         !eesij=eesij*auxg
299,300c384,385
< C-SPLITELE:        fac=-3*rrmij*(facvdw+facvdw+facel)*sss
< C-SPLITELE:     &      +(evdwij+eesij)*sssgrad*rrmij
---
> C-SPLITELE:        fac=-3*rrmij*(facvdw+facvdw+facel)*esss
> C-SPLITELE:     &      +(evdwij+eesij)*esssgrad*rrmij
320,322c405,407
< C-SPLITELE:        ggg(1)=facvdw*xyzj(1)+sssgrad*rmij*evdwij*xyzj(1)
< C-SPLITELE:        ggg(2)=facvdw*xyzj(2)+sssgrad*rmij*evdwij*xyzj(2)
< C-SPLITELE:        ggg(3)=facvdw*xyzj(3)+sssgrad*rmij*evdwij*xyzj(3)
---
> C-SPLITELE:        ggg(1)=facvdw*xyzj(1)+esssgrad*rmij*evdwij*xyzj(1)
> C-SPLITELE:        ggg(2)=facvdw*xyzj(2)+esssgrad*rmij*evdwij*xyzj(2)
> C-SPLITELE:        ggg(3)=facvdw*xyzj(3)+esssgrad*rmij*evdwij*xyzj(3)
328c413
< C-SPLITELE:     &    sss*lipid_cache(j).ssgrad*evdwij/2.0d0*lipscale**2
---
> C-SPLITELE:     &    esss*lipid_cache(j).ssgrad*evdwij/2.0d0*lipscale**2
330c415
< C-SPLITELE:     &    sss*lipid_cache(i).ssgrad*evdwij/2.0d0*lipscale**2
---
> C-SPLITELE:     &    esss*lipid_cache(i).ssgrad*evdwij/2.0d0*lipscale**2
335,336c420,428
<         ecosa=2.0D0*fac3*fac1+fac4
<         fac3=-6.0D0*fac3
---
> 
>         auxa=esss*faclipij2
> #ifdef SHIELD
>            *(fsij*fsij)
> #endif
>         !ecosa=2.0d0*fac3*fac1+fac4
>         ecosa=(2.0d0*fac3*fac+fac4)*auxa
>         !fac3=-6.0d0*fac3
>         fac3=-6.0d0*fac3*auxa
340,341c432,433
<         dcosb=dc_norm_i-erij*cosb
<         dcosg=dc_norm_j-erij*cosg
---
>         !dcosb=dc_norm_i-erij*cosb
>         !dcosg=dc_norm_j-erij*cosg
346,352c438,444
<         aux=sss*faclipij2
< #ifdef SHIELD
<            *(fsij*fsij)
< #endif
<         ecosa=ecosa*aux
<         ecosb=ecosb*aux
<         ecosg=ecosg*aux
---
> !        aux=esss*faclipij2
> !#ifdef SHIELD
> !           *(fsij*fsij)
> !#endif
> !        ecosa=ecosa*aux
> !        ecosb=ecosb*aux
> !        ecosg=ecosg*aux
353a446
>         aux=rmij*welec
358,372c451,462
<         !ggg=(ecosb*dcosb+ecosg*dcosg)*rmij*aux
<         gggi=(ecosb*rmij)*dcosb+(ecosg*rmij)*dcosg
< !       ggg=rmij*(ecosb*dc_norm_i+ecosg*dc_norm_j       ! TO TEST
< !    &            -(ecosg*cosg+ecosb*cosb)*erij)
< 
<         ggg=ggg+gggi
< 
<         output.gelc_longx(j)=output.gelc_longx(j)+ggg(1)
<         output.gelc_longy(j)=output.gelc_longy(j)+ggg(2)
<         output.gelc_longz(j)=output.gelc_longz(j)+ggg(3)
<      &                      +lipid_j_ssgrad*eesij
<         d_gelc_longx=d_gelc_longx-ggg(1)
<         d_gelc_longy=d_gelc_longy-ggg(2)
<         d_gelc_longz=d_gelc_longz-ggg(3)
<      &                      +lipid_i_ssgrad*eesij
---
>         ! gelc_long(j)
> !        output.dgradcx(j)=output.dgradcx(j)+welec*(ggg(1))
> !        output.dgradcy(j)=output.dgradcy(j)+welec*(ggg(2))
> !        output.dgradcz(j)=output.dgradcz(j)
> !     &              +welec*(ggg(3)+lipid_j_ssgrad*eesij)
>         erb=ecosb*aux
>         erg=ecosg*aux
>         !erij_f=erij_f+welec*(rij*aux2-erb*cosb-erg*cosg)
>         erij_f=erij_f+welec*aux2-erb*cosb-erg*cosg
>         gradj=erb*dc_norm_i+erg*dc_norm_j
> 
>         !gg_lip=gg_lip+welec*eesij
373a464
>         ! gelc
376a468,481
> !       output.gelcx(j)=output.gelcx(j)
> !    &    +ecosa*dc_norm_i(1)+ecosg*erij(1)-dc_norm_j(1)*auxg
> !       output.gelcy(j)=output.gelcy(j)
> !    &    +ecosa*dc_norm_i(2)+ecosg*erij(2)-dc_norm_j(2)*auxg
> !       output.gelcz(j)=output.gelcz(j)
> !    &    +ecosa*dc_norm_i(3)+ecosg*erij(3)-dc_norm_j(3)*auxg
>         vbld_invj=vbld_inv(j+1)*welec
>         output.dgradcx(j)=output.dgradcx(j)+vbld_invj*
>      &    (ecosa*dc_norm_i(1)+ecosg*erij(1)-dc_norm_j(1)*auxg)
>         output.dgradcy(j)=output.dgradcy(j)+vbld_invj*
>      &    (ecosa*dc_norm_i(2)+ecosg*erij(2)-dc_norm_j(2)*auxg)
>         output.dgradcz(j)=output.dgradcz(j)+vbld_invj*
>      &    (ecosa*dc_norm_i(3)+ecosg*erij(3)-dc_norm_j(3)*auxg)
> 
380,388c485
< !       output(j).gelc=output(j).gelc
< !    &    +ecosa*dc_norm_i+ecosg*erij-dc_norm_j*auxg
<         output.gelcx(j)=output.gelcx(j)
<      &    +ecosa*dc_norm_i(1)+ecosg*erij(1)-dc_norm_j(1)*auxg
<         output.gelcy(j)=output.gelcy(j)
<      &    +ecosa*dc_norm_i(2)+ecosg*erij(2)-dc_norm_j(2)*auxg
<         output.gelcz(j)=output.gelcz(j)
<      &    +ecosa*dc_norm_i(3)+ecosg*erij(3)-dc_norm_j(3)*auxg
< 
---
> #ifndef EELECIJ_MAP_I
389a487,501
> #else
> !       output.gelcx(i)=output.gelcx(i)
> !    &    +ecosa*dc_norm_j(1)+ecosb*erij(1)-dc_norm_i(1)*auxb
> !       output.gelcy(i)=output.gelcy(i)
> !    &    +ecosa*dc_norm_j(2)+ecosb*erij(2)-dc_norm_i(2)*auxb
> !       output.gelcz(i)=output.gelcz(i)
> !    &    +ecosa*dc_norm_j(3)+ecosb*erij(3)-dc_norm_i(3)*auxb
>         vbld_invi=vbld_inv(i+1)*welec
>         output.dgradcx(i)=output.dgradcx(i)+vbld_invi*
>      &    (ecosa*dc_norm_j(1)+ecosb*erij(1)-dc_norm_i(1)*auxb)
>         output.dgradcy(i)=output.dgradcy(i)+vbld_invi*
>      &    (ecosa*dc_norm_j(2)+ecosb*erij(2)-dc_norm_i(2)*auxb)
>         output.dgradcz(i)=output.dgradcz(i)+vbld_invi*
>      &    (ecosa*dc_norm_j(3)+ecosb*erij(3)-dc_norm_i(3)*auxb)
> #endif
407d518
<         fac=th_data.sqrt_ael6(itelj,eiteli)*r3ij
409,411c520,532
<           muimuj=scalar(mu(:,i),mu(:,j))
<           muier=scalar(mu(:,i),erij(:))
<           mujer=scalar(mu(:,j),erij(:))
---
>           !muimuj=scalar(mu(:,i),mu(:,j))
>           !muier=scalar(mu(:,i),erij(:))
>           !mujer=scalar(mu(:,j),erij(:))
>           muier=3.0d0*
>      &     (soa_mu_x(i)*erij(1)+soa_mu_y(i)*erij(2)+soa_mu_z(i)*erij(3))
>           mujer=3.0d0*
>      &     (soa_mu_x(j)*erij(1)+soa_mu_y(j)*erij(2)+soa_mu_z(j)*erij(3))
>           auxveci(1)=soa_mu_x(i)-muier*erij(1)
>           auxveci(2)=soa_mu_y(i)-muier*erij(2)
>           auxveci(3)=soa_mu_z(i)-muier*erij(3)
>           auxvecj(1)=soa_mu_x(j)-mujer*erij(1)
>           auxvecj(2)=soa_mu_y(j)-mujer*erij(2)
>           auxvecj(3)=soa_mu_z(j)-mujer*erij(3)
413c534,536
<           eel_loc_ij_o=fac*(muimuj-3.0d0*muier*mujer)
---
>           !eel_loc_ij_o=fac*(muimuj-3.0d0*muier*mujer)
>           eel_loc_ij_o=
>      &      fac1*(scalar(auxveci,auxvecj)-(2.0d0/3.0d0)*muier*mujer)
414a538
> 
431c555
<           auxg=sss*faclipij
---
>           auxg=esss*faclipij
434d557
< #else
436,437d558
< c AL 9/22/2022 The eel_loc_ij_1 factor introduced for better numerical stability
<           eel_loc_ij_1 = eel_loc_ij*faclipij
439d559
< 
494,498c614,618
<           auxvec=fac*(mu(:,j)-3.0d0*mujer*erij)
<      &         *sss*faclipij
< #ifdef SHIELD
<      &         *fsij
< #endif
---
> !         auxvec=fac*(mu(:,j)-3.0d0*mujer*erij)
> !    &         *esss*faclipij
>           auxg=auxg*fac1
> 
>           !auxvec=auxg*(mu(:,j)-3.0d0*mujer*erij)
510,515c630,635
<           d_auxvec=d_auxvec+auxvec
< 
<           auxvec=fac*(mu(:,i)-3.0*muier*erij)
<      &         *sss*faclipij
< #ifdef SHIELD
<      &         *fsij
---
> #ifndef EELECIJ_MAP_I
>           d_auxvec=d_auxvec+auxg*auxvecj
> #else
>           output.gel_locx(i)=output.gel_locx(i)+auxg*auxvecj(1)
>           output.gel_locy(i)=output.gel_locy(i)+auxg*auxvecj(2)
>           output.gel_locz(i)=output.gel_locz(i)+auxg*auxvecj(3)
516a637,641
> 
> !         auxvec=fac*(mu(:,i)-3.0d0*muier*erij)
> !    &         *esss*faclipij
>           !auxvec=auxg*(mu(:,i)-3.0d0*muier*erij)
> 
529,531c654,656
<           output.gel_locx(j)=output.gel_locx(j)+auxvec(1)
<           output.gel_locy(j)=output.gel_locy(j)+auxvec(2)
<           output.gel_locz(j)=output.gel_locz(j)+auxvec(3)
---
>           output.gel_locx(j)=output.gel_locx(j)+auxg*auxveci(1)
>           output.gel_locy(j)=output.gel_locy(j)+auxg*auxveci(2)
>           output.gel_locz(j)=output.gel_locz(j)+auxg*auxveci(3)
541,552c666,691
< c AL 2/22/2022 Changed for better numerical stability
< c          aux=eel_loc_ij/sss*sssgrad*rmij
< #ifdef NAN_DETECT
<           if (isnan(sss)) print *,"eelec_work 1: NaN in sss"
< #endif
<           aux=eel_loc_ij_1*sssgrad*rmij
<           ggg=xyzj*aux
<           ggg=ggg-3.0d0*rmij*(eel_loc_ij*erij
<      &        +fac*sss*(
<      &          muier*mu(:,j)+mujer*mu(:,i)
<      &          -2.0d0*muier*mujer*erij)
<      &       )*faclipij
---
> !         eel_loc_ij=eel_loc_ij_o
> !         auxg=esss*faclipij
> !#ifdef SHIELD
> !              *fsij
> !#endif
> !         eel_loc_ij=eel_loc_ij*auxg
> 
>           !aux=eel_loc_ij/esss*esssgrad*rmij
>           !aux=eel_loc_ij*esss_loc3*rmij
> 
> !          eel_loc_ij_o=fac*(muimuj-3.0d0*muier*mujer)
> !          aux=eel_loc_ij_o*faclipij*esssgrad*rmij
> !#ifdef SHIELD
> !              *fsij
> !#endif
> !          ggg=xyzj*aux
> !          ggg=ggg-3.0d0*rmij*(eel_loc_ij*erij
> !     &        +fac*esss*(
> !     &          muier*mu(:,j)+mujer*mu(:,i)
> !     &          -2.0d0*muier*mujer*erij)
> !     &       )*faclipij
> !#ifdef SHIELD
> !     &       *fsij
> !#endif
> 
>           auxg=wel_loc*faclipij
556,563c695,721
< #ifdef NAN_DETECT
<           if (isnan(sss)) print *,"eelec_work 2: NaN in sss"
<           if (isnan(ggg(1)).or.isnan(ggg(2)).or.isnan(ggg(3))) 
<      &        print *,"eelec_work: NaN in ggg",i,j,ggg," aux",aux,
<      &          " sss",sss," sssgrad",sssgrad," muier",muier,
<      &          " mujer",mujer," mui",mu(:,i)," muj",mu(:,j),
<      &          " xyzj",xyzj
< #endif
---
>           !fac=fac*esss
>           !rmij=rmij*3.0d0
>           !aux=rmij*fac*faclipij
>           aux=esss_rmij_3*fac1*auxg*(1.0d0/3.0d0)
>           !auxg=muier*mujer
> !         aux2=(eel_loc_ij_o*esssgrad-rmij*(eel_loc_ij-2.0d0*fac*auxg))
> !    &        *faclipij
> !#ifdef SHIELD
> !     &       *fsij
> !#endif
> 
>           aux2=auxg*eel_loc_ij_o*
>      &      (esssgrad
>      &       -esss_rmij_3*faclipij
> #ifdef SHIELD
>      &        *fsij
> #endif
>      &       )
>      &       -(4.0d0/3.0d0)*aux*muier*mujer
> 
> !          ggg=(
> !!    &        (eel_loc_ij_o*esssgrad-rmij*(eel_loc_ij-2.0d0*fac*auxg)
> !!    &        )*faclipij*erij
> !!    &        aux2*erij
> !     &        -muier*mu(:,j)
> !     &        -mujer*mu(:,i)
> !     &        )
566,584c724,745
<           aux=eel_loc_ij_o*0.5d0*lipscale/faclipij
< #ifdef NAN_DETECT
<           if (isnan(aux)) print *,"NaN in lipid",i,j
< #endif
<           output.gel_loc_longx(j)=output.gel_loc_longx(j)+ggg(1)
<           output.gel_loc_longy(j)=output.gel_loc_longy(j)+ggg(2)
<           output.gel_loc_longz(j)=output.gel_loc_longz(j)+
<      &                           +(ggg(3)+lipid_j_ssgrad*aux)
<           d_gel_loc_longx=d_gel_loc_longx-ggg(1)
<           d_gel_loc_longy=d_gel_loc_longy-ggg(2)
<           d_gel_loc_longz=d_gel_loc_longz-
<      &                     (ggg(3)-lipid_i_ssgrad*aux)
< #ifdef NAN_DETECT
<           if (isnan(output.gel_loc_longx(j)).or.
<      &        isnan(output.gel_loc_longy(j)).or.
<      &        isnan(output.gel_loc_longz(j)))print*,"NaN in output",i,j,
<      &        output.gel_loc_longx(j),output.gel_loc_longy(j),
<      &        output.gel_loc_longz(j) 
< #endif
---
>           !aux=eel_loc_ij_o*0.5d0*lipscale/faclipij
> !       faclipij=lipid_cache(i).ss+lipid_cache(j).ss+1.0d0
>           !aux=eel_loc_ij_o/faclipij
> 
>           ! gel_loc_long(j)
> !          output.dgradcx(j)=output.dgradcx(j)+wel_loc*(ggg(1))
> !          output.dgradcy(j)=output.dgradcy(j)+wel_loc*(ggg(2))
> !          output.dgradcz(j)=output.dgradcz(j)
> !     &      +wel_loc*(ggg(3)+lipid_j_ssgrad*aux)
>           !gradj=-(muier*mu(:,j)+mujer*mu(:,i))*wel_loc
> !         gradj=-(muier*auxvecj+mujer*auxveci+6.0d0*muier*mujer*erij)
> !    &       *(aux*wel_loc)
>           !gradj=-(muier*auxvecj+mujer*auxveci)*(aux*wel_loc)
>           muier=muier*aux
>           mujer=mujer*aux
>           gradj=gradj-muier*auxvecj-mujer*auxveci
>           !erij_f=erij_f+wel_loc*(aux2-6.0d0*muier*mujer*aux)
>           erij_f=erij_f+aux2
> 
>           !gg_lip=gg_lip+wel_loc*aux
>           gg_lip=gg_lip+wel_loc*eel_loc_ij_o/faclipij
> 
587a749,777
>           !gradj=gradj+erij_f*erij+dci_f*dc_norm_i+dcj_f*dc_norm_j
>           gradj=gradj+erij_f*erij
> 
>           lipid_j_ssgrad=lipid_cache_ssgrad(j)
> !         output.dgradcx(j)=output.dgradcx(j)+gradj(1)
> !         output.dgradcy(j)=output.dgradcy(j)+gradj(2)
> !         output.dgradcz(j)=output.dgradcz(j)+
> !    &      gradj(3)+lipid_j_ssgrad*gg_lip
>           output.dgradbufc2x(j)=output.dgradbufc2x(j)+gradj(1)
>           output.dgradbufc2y(j)=output.dgradbufc2y(j)+gradj(2)
>           output.dgradbufc2z(j)=output.dgradbufc2z(j)+
>      &      gradj(3)+lipid_j_ssgrad*gg_lip
> 
>           gradix=gradix-gradj(1)
>           gradiy=gradiy-gradj(2)
>           gradiz=gradiz-gradj(3)
> 
> #ifdef EELECLOG
>           write(*,'(a,i4,i4,3f18.12)')'ELz',i,j,gradj(:)
> #endif
> 
> #ifdef EELECIJ_MAP_I
>           output.dgradbufc2x(i)=output.dgradbufc2x(i)+gradix
>           output.dgradbufc2y(i)=output.dgradbufc2y(i)+gradiy
>           output.dgradbufc2z(i)=output.dgradbufc2z(i)+
>      &      gradiz+lipid_i_ssgrad*gg_lip
> #else
>           gradilip=gradilip+gg_lip
> #endif
589,608d778
< #ifdef NAN_DETECT
<       if (isnan(d_gel_loc_longx).or.isnan(d_gel_loc_longy).or.
<      &  isnan(d_gel_loc_longz)) print *,"NaN in d_gel_loc",i,j,
<      &  d_gel_loc_longx,d_gel_loc_longy,d_gel_loc_longz
< #endif
<       output.gvdwppx(i)=output.gvdwppx(i)+d_gvdwppx
<       output.gvdwppy(i)=output.gvdwppy(i)+d_gvdwppy
<       output.gvdwppz(i)=output.gvdwppz(i)+d_gvdwppz
< 
<       output.gelc_longx(i)=output.gelc_longx(i)+d_gelc_longx
<       output.gelc_longy(i)=output.gelc_longy(i)+d_gelc_longy
<       output.gelc_longz(i)=output.gelc_longz(i)+d_gelc_longz
< 
<       output.gelcx(i)=output.gelcx(i)+d_gelc(1)
<       output.gelcy(i)=output.gelcy(i)+d_gelc(2)
<       output.gelcz(i)=output.gelcz(i)+d_gelc(3)
< 
<       output.gel_loc_longx(i)=output.gel_loc_longx(i)+d_gel_loc_longx
<       output.gel_loc_longy(i)=output.gel_loc_longy(i)+d_gel_loc_longy
<       output.gel_loc_longz(i)=output.gel_loc_longz(i)+d_gel_loc_longz
609a780,796
> #ifndef EELECIJ_MAP_I
>       ! gvdwpp + gelc_long + gel_loc_long
>       output.dgradbufc2x(i)=output.dgradbufc2x(i)+gradix
>       output.dgradbufc2y(i)=output.dgradbufc2y(i)+gradiy
>       output.dgradbufc2z(i)=output.dgradbufc2z(i)+gradiz+
>      &  lipid_i_ssgrad*gradilip
> 
> !     output.gelcx(i)=output.gelcx(i)+d_gelc(1)
> !     output.gelcy(i)=output.gelcy(i)+d_gelc(2)
> !     output.gelcz(i)=output.gelcz(i)+d_gelc(3)
>       ! gelc
>       vbld_invi=vbld_inv(i+1)*welec
>       output.dgradcx(i)=output.dgradcx(i)+d_gelc(1)*vbld_invi
>       output.dgradcy(i)=output.dgradcy(i)+d_gelc(2)*vbld_invi
>       output.dgradcz(i)=output.dgradcz(i)+d_gelc(3)*vbld_invi
> 
>       ! gel_loc
612a800
> #endif
egb.F
0a1
> #include"timers"
2c3,7
< 
---
> C-----------------------------------------------------------------------
> C
> C This subroutine calculates the interaction energy of nonbonded side chains
> C assuming the Gay-Berne potential of interaction.
> C
4a10,14
> #ifdef MPI
>       include"mpif.h"
> #else
>       double precision tcpu
> #endif
5a16
>       include 'COMMON.TIME1'
6a18,19
>       !include 'COMMON.SBRIDGE'  !!!
>       include 'COMMON.OMP'
7a21,26
>       double precision time00
>       !integer i,j               !!!
>       !integer egb_cnt
>       !common /egbstaticdata/ egb_cnt
>       !write(*,*)'egb_start',egb_cnt
>       !egb_cnt=egb_cnt+1
9c28
<         call egb_6(evdw)
---
>         call egb_6
11c30
<         call egb_n(evdw)
---
>         call egb_n
12a32,40
> 
>       call egb_sum_results(evdw)
>       egb_group_results=.true.
> 
>       !do i=1,ns
>       !  do j=1,ns
>       !    write(*,*)'SSB',i,j,dyn_ssbond_ij(i,j)
>       !  enddo
>       !enddo
14a43
> 
16c45
<       subroutine egb_queue_work(chunk,idx,j,xyzj,rij,full)
---
>       subroutine clear_egb_values(threads_used)
18a48
>       include 'COMMON.OMP'
20,30c50,78
<       integer idx,j
<       double precision xyzj(3),rij
<       logical full
<       record /egb_work_chunk/ chunk
<       idx=idx+1
<       chunk.j(idx)=j
<       chunk.x(idx)=xyzj(1)
<       chunk.y(idx)=xyzj(2)
<       chunk.z(idx)=xyzj(3)
<       chunk.r(idx)=rij
<       full=idx.eq.egb_buffer_size
---
>       integer threads_used
>       egb_accum(1:threads_used).evdw=0.0d0
>       return
>       end
> 
> C-----------------------------------------------------------------------
>       subroutine clear_egb_arrays(my_thread)
>       use omp_data
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       !include 'COMMON.EGB'
>       integer my_thread
>       !do i=1,nres
>         !egb_result(j).gvdwcx(i)=0.0d0
>         !egb_result(j).gvdwcy(i)=0.0d0
>         !egb_result(j).gvdwcz(i)=0.0d0
>         !egb_result(j).gvdwxx(i)=0.0d0
>         !egb_result(j).gvdwxy(i)=0.0d0
>         !egb_result(j).gvdwxz(i)=0.0d0
>         !egb_result(j).lip_delta(i)=0.0d0
>       !workspace(my_thread).item.gvdwcx(1:nres)=0.0d0
>       !workspace(my_thread).item.gvdwcy(1:nres)=0.0d0
>       !workspace(my_thread).item.gvdwcz(1:nres)=0.0d0
>       !workspace(my_thread).item.gvdwxx(1:nres)=0.0d0
>       !workspace(my_thread).item.gvdwxy(1:nres)=0.0d0
>       !workspace(my_thread).item.gvdwxz(1:nres)=0.0d0
>       !workspace(my_thread).item.lip_delta(1:nres)=0.0d0
>       !enddo
32a81
> 
34c83,85
<       subroutine egb_move_work(from,from_idx,to,to_idx)
---
>       subroutine egb_sum_results(evdw)
>       use omp_lib
>       use omp_data
36a88,91
>       include 'COMMON.CHAIN'
>       include 'COMMON.FFIELD'
>       include 'COMMON.OMP'
>       include 'COMMON.LIPID'
38,46c93,157
<       integer from_idx,to_idx,i
<       record /egb_work_chunk/ from,to
<       do i=1,from_idx
<         to_idx=to_idx+1
<         to.j(to_idx)=from.j(i)
<         to.x(to_idx)=from.x(i)
<         to.y(to_idx)=from.y(i)
<         to.z(to_idx)=from.z(i)
<         to.r(to_idx)=from.r(i)
---
>       double precision evdw
>       integer i,j,my_thread,threads_used
>       double precision g,gg(3),ggx(3)
> 
>       threads_used=max(scsc_threads,dynss_threads)
> 
> !!$OMP PARALLEL DO PRIVATE(g,gg,ggx,j)
> !      do i=1,nres
> !        ggx=0.0d0
> !        g=0.0d0
> !        do j=1,threads_used
> !
> !!         ggx(1)=ggx(1)+workspace(j).item.gvdwxx(i)
> !!         ggx(2)=ggx(2)+workspace(j).item.gvdwxy(i)
> !!         ggx(3)=ggx(3)+workspace(j).item.gvdwxz(i)
> !
> !          g=g+workspace(j).item.lip_delta(i)
> !        enddo
> !
> !        !dsci_inv=vbld_inv(i+nres)
> !        !if(dsci_inv.gt.1.0d20) then
> !        !  dsci_inv=0.0d0
> !        !endif
> !
> !        !gg(3)=gg(3)+lipid_cache_ssgrad(nres+i)*g
> !        !gvdwc(:,i)=gg
> !        !gvdwx(:,i)=gg+ggx*dsci_inv
> !        !gvdwc(:,i)=gg
> !!       gvdwc(3,i)=gvdwc(3,i)+lipid_cache_ssgrad(nres+i)*g
> !        workspace(1).item.dgradbufcz(i)=workspace(1).item.dgradbufcz(i)
> !     &                            +wsc*lipid_cache_ssgrad(nres+i)*g
> !        !gvdwx(:,i)=ggx*dsci_inv
> !        !gvdwx(:,i)=ggx
> !        !gvdwx(3,i)=gvdwx(3,i)+lipid_cache_ssgrad(nres+i)*g
> !!       omp_dgradx(1,i)=omp_dgradx(1,i)+wsc*ggx(1)
> !!       omp_dgradx(2,i)=omp_dgradx(2,i)+wsc*ggx(2)
> !!       omp_dgradx(3,i)=omp_dgradx(3,i)+wsc*(ggx(3)
> !!    &    +lipid_cache_ssgrad(nres+i)*g)
> !        omp_dgradx(3,i)=omp_dgradx(3,i)+wsc*(
> !     &    +lipid_cache_ssgrad(nres+i)*g)
> !
> !!     write(*,'(a,i4,3f18.12)')'GC',i,
> !!    &  workspace(1).item.dgradbufcx(i)/wsc,
> !!    &  workspace(1).item.dgradbufcy(i)/wsc,
> !!    &  workspace(1).item.dgradbufcz(i)/wsc
> !!     write(*,'(a,i4,3f18.12)')'GX',i,omp_dgradx(:,i)/wsc
> !      enddo
> !!$OMP END PARALLEL DO
> 
> !!$OMP PARALLEL NUM_THREADS(threads_used) DEFAULT(SHARED)
> !!$OMP& PRIVATE(my_thread)
> !      my_thread=omp_get_thread_num()+1
> !      !workspace(my_thread).item.gvdwcx(1:nres)=0.0d0
> !      !workspace(my_thread).item.gvdwcy(1:nres)=0.0d0
> !      !workspace(my_thread).item.gvdwcz(1:nres)=0.0d0
> !      !workspace(my_thread).item.gvdwxx(1:nres)=0.0d0
> !      !workspace(my_thread).item.gvdwxy(1:nres)=0.0d0
> !      !workspace(my_thread).item.gvdwxz(1:nres)=0.0d0
> !      !workspace(my_thread).item.lip_delta(1:nres)=0.0d0
> !!$OMP END PARALLEL
> 
>       evdw=0.0d0
>       do i=1,threads_used
>         evdw=evdw+egb_accum(i).evdw
>         egb_accum(i).evdw=0.0d0
48c159
<       from_idx=0
---
> 
50a162
> 
52c164
<       subroutine clear_egb_arrays(num_threads)
---
>       subroutine init_egb
55a168,169
>       include 'COMMON.INTERACT'
>       include 'COMMON.OMP'
57,63c171,192
<       integer num_threads,i,j
<       egb_accum(:).evdw=0.0d0
<       do j=1,num_threads
<         do i=1,nres
<           egb_result(i,j).lip_delta=0.0d0
<           egb_result(i,j).gvdwc=0.0d0
<           egb_result(i,j).gvdwx=0.0d0
---
>       integer j,itypi,itypj
> 
>       do itypi=1,ntyp
>         do j=1,nres
>           itypj=abs_itype(j)
>           if (itypj.eq.ntyp1 .or. itypi.eq.ntyp1) then
>             egb_data2d(j,1,itypi)=0.0d0
>             egb_data2d(j,2,itypi)=0.0d0
>             egb_data2d(j,3,itypi)=0.0d0
>             egb_data2d(j,4,itypi)=0.0d0
>             egb_data2d(j,5,itypi)=0.0d0
>             egb_data2d(j,6,itypi)=0.0d0
>             egb_data2d(j,7,itypi)=0.0d0
>           else
>             egb_data2d(j,1,itypi)=sigma(itypi,itypj)
>             egb_data2d(j,2,itypi)=chi(itypi,itypj)
>             egb_data2d(j,3,itypi)=chi(itypj,itypi)
>             egb_data2d(j,4,itypi)=aa_lipaq(itypj,itypi)
>             egb_data2d(j,5,itypi)=bb_lipaq(itypj,itypi)
>             egb_data2d(j,6,itypi)=aa_aq(itypi,itypj)
>             egb_data2d(j,7,itypi)=bb_aq(itypi,itypj)
>           endif
66,67c195,206
<       return
<       end
---
>       do j=1,nres
>         itypj=abs_itype(j)
>         if (itypj.eq.ntyp1) then
>           egb_data1d(j,1)=0.0d0
>           egb_data1d(j,2)=0.0d0
>         else
>           egb_data1d(j,1)=chip(itypj)
>           egb_data1d(j,2)=alp(itypj)
>         endif
>       enddo
>       end subroutine
> 
70c209
< #define EGB_POW(x,out)     out=x*x*x*x*x*x
---
> #define EGB_POW(x,out)     out=(x*x*x)*(x*x*x)
74,89d212
< #define EGB_MAP_J(seq)     seq
< !#define EGB_MAP_IDX(seq)  seq-chunk.j(1)+1
< #define EGB_MAP_IDX(seq)   seq-from+1
< #define EGB_WORK_FN        EGB_WORK_SEQ
< #include"egb_work.F"
< #undef EGB_MAP_IDX
< #undef EGB_MAP_J
< #undef EGB_WORK_FN
< #define EGB_WORK_FN        EGB_WORK_NSEQ
< #define EGB_MAP_J(seq)     chunk.j(seq)
< #define EGB_MAP_IDX(seq)   seq
< #include"egb_work.F"
< #undef EGB_MAP_IDX
< #undef EGB_MAP_J
< #undef EGB_WORK_FN
< #undef EGB_WORK_SEQ
90a214
> #undef EGB_WORK_SEQ
94,97c218,221
< #define EGB_FN            egb_n
< #define EGB_POW(x,out)    out=x**expon
< #define EGB_WORK_SEQ      egb_n_do_work_seq
< #define EGB_WORK_NSEQ     egb_n_do_work_nseq
---
> #define EGB_FN             egb_n
> #define EGB_POW(x,out)     out=x**expon
> #define EGB_WORK_SEQ       egb_n_do_work_seq
> #define EGB_WORK_NSEQ      egb_n_do_work_nseq
99,114d222
< #define EGB_MAP_J(seq)    seq
< !#define EGB_MAP_IDX(seq) seq-chunk.j(1)+1
< #define EGB_MAP_IDX(seq)  seq-from+1
< #define EGB_WORK_FN       EGB_WORK_SEQ
< #include"egb_work.F"
< #undef EGB_MAP_IDX
< #undef EGB_MAP_J
< #undef EGB_WORK_FN
< #define EGB_WORK_FN       EGB_WORK_NSEQ
< #define EGB_MAP_J(seq)    chunk.j(seq)
< #define EGB_MAP_IDX(seq)  seq
< #include"egb_work.F"
< #undef EGB_MAP_IDX
< #undef EGB_MAP_J
< #undef EGB_WORK_FN
< #undef EGB_WORK_SEQ
115a224
> #undef EGB_WORK_SEQ
egb_t.F
1,2c1
<       subroutine EGB_FN(evdw)
< #ifdef _OPENMP
---
>       subroutine EGB_FN
4,8c3
< #endif
< C
< C This subroutine calculates the interaction energy of nonbonded side chains
< C assuming the Gay-Berne potential of interaction.
< C
---
>       use omp_data
16a12
>       include 'COMMON.FFIELD'
18c14
<       !include 'COMMON.EELEC'
---
>       include 'COMMON.OMP'
25,28c21
<       double precision evdw,evdwij,rij,xyzj(3),evdwij_przed_tri
<       integer i,j,k,lastj,seq_idx,nseq_idx,ikont,jblock
<       record /egb_work_chunk/ seq_work,nseq_work
<       double precision scalar
---
>       integer i,j,ikont,jblock
32c25
<       double precision g,gg(3),ggx(3),dsci_inv
---
>       double precision g,gg(3),ggx(3)
34d26
<       logical full,limited
37c29
< C      print *,'Entering EGB nnt=',nnt,' nct=',nct,' expon=',expon
---
> c      print *,'Entering EGB nnt=',nnt,' nct=',nct,' expon=',expon
42,45d33
< c     if (icall.eq.0) lprn=.false.
< C the loop over all 27 posible neigbours (for xshift=0,yshift=0,zshift=0
< C we have the original box)
< c     do i=iatsc_s,iatsc_e
50,52c38
< !$OMP& PRIVATE(
< !$OMP&   i,j,ikont,jblock,k,seq_work,nseq_work,seq_idx,nseq_idx,
< !$OMP&   lastj,evdwij,evdwij_przed_tri,rij,xyzj,full,
---
> !$OMP& PRIVATE(i,ikont,jblock,
57d42
< #ifdef _OPENMP
59,63d43
< #else
<       my_thread=1
< #endif
<       seq_idx=0
<       nseq_idx=0
65,70c45,56
<       from_ik=scsc_from_ik(my_thread)
<       to_ik=scsc_to_ik(my_thread)
<       first_blk=scsc_first_blk(my_thread)
<       last_blk=scsc_last_blk(my_thread)
<       first_j=scsc_first_j(my_thread)
<       last_j=scsc_last_j(my_thread)
---
>       !from_ik=scsc_from_ik(my_thread)
>       !to_ik=scsc_to_ik(my_thread)
>       !first_blk=scsc_first_blk(my_thread)
>       !last_blk=scsc_last_blk(my_thread)
>       !first_j=scsc_first_j(my_thread)
>       !last_j=scsc_last_j(my_thread)
>       from_ik=scsc_range(my_thread).rng_start
>       to_ik=scsc_range(my_thread).rng_end
>       first_blk=scsc_range(my_thread).rng_first
>       last_blk=scsc_range(my_thread).rng_last
>       first_j=scsc_range(my_thread).rng_first_j
>       last_j=scsc_range(my_thread).rng_last_j
83,84d68
<         lastj=-1
< 
88,196c72,75
<           if((ikont.eq.from_ik).and.(jblock.eq.from_blk)) from_j=first_j
<           if((ikont.eq.to_ik).and.(jblock.eq.to_blk)) to_j=last_j
< 
<           do j=from_j,to_j
< C define scaling factor for lipids
< 
< C           if (positi.le.0) positi=positi+boxzsize
< C           print *,i
< C first for peptide groups
< c for each residue check if it is in lipid or lipid water border area
< C
< C Calculate SC interaction energy.
< C
<             IF (dyn_ss_mask(i).and.dyn_ss_mask(j)) THEN
< 
< c             write(iout,*) "PRZED ZWYKLE", evdwij
<               call dyn_ssbond_ene(i,j,evdwij)
< c             write(iout,*) "PO ZWYKLE", evdwij
< c             call flush(iout)
< 
<               !evdw=evdw+evdwij
<               egb_accum(my_thread).evdw=egb_accum(my_thread).evdw+evdwij
< #ifdef DEBUG
<               if (energy_dec) write (iout,'(a6,2i5,0pf7.3,a3)') 
<      &                        'evdw',i,j,evdwij,' ss'
< #endif
< C triple bond artifac removal
< c             do k=j+1,iend(i,iint) 
<               do k=j+1,nct
< C search over all next residues
<                 if (dyn_ss_mask(k)) then
< C check if they are cysteins
< C                 write(iout,*) 'k=',k
< 
< c                 write(iout,*) "PRZED TRI", evdwij
<                   evdwij_przed_tri=evdwij
<                   call triple_ssbond_ene(i,j,k,evdwij)
< c                 if(evdwij_przed_tri.ne.evdwij) then
< c                   write (iout,*) "TRI:", evdwij, evdwij_przed_tri
< c                 endif
< 
< c                 write(iout,*) "PO TRI", evdwij
< C call the energy function that removes the artifical triple disulfide
< C bond the soubroutine is located in ssMD.F
<                   !evdw=evdw+evdwij             
<                   egb_accum(my_thread).evdw=
<      &                              egb_accum(my_thread).evdw+evdwij
< #ifdef DEBUG
<                   if (energy_dec) write (iout,'(a6,2i5,0pf7.3,a3)')
<      &                        'evdw',i,j,evdwij,'tss'
< #endif
<                 endif!dyn_ss_mask(k)
<               enddo! k
<             ELSE
< 
<               LOG_STAT(200, 1)
<               !call wrapvec(c_tobox(:,nres+j)-c_tobox(:,nres+i),xyzj)
<               call wrapvec2(c_tobox(:,nres+j)-c_tobox(:,nres+i),xyzj,
<      &                      halfbox,box)
<               rij=scalar(xyzj,xyzj)
<               if(rij.ge.r_cut_int_sq) cycle
<               LOG_STAT(201, 1)
< 
<               if((j.ne.lastj+1).and.(seq_idx.gt.0)) then
<                 if((seq_idx.lt.simd_seq_cutoff) .and.
<      &             (nseq_idx+seq_idx.lt.egb_buffer_size)) then
<                   LOG_STAT(204, seq_idx)
<                   call egb_move_work(seq_work,seq_idx,nseq_work,
<      &                               nseq_idx)
<                 else
<                   LOG_STAT(202, seq_idx)
<                   call EGB_WORK_SEQ(seq_work,i,
<      &                              seq_work.j(1),seq_work.j(seq_idx),
<      &                              egb_accum(my_thread).evdw,
<      &                              egb_result(:,my_thread))
<                   seq_idx=0
<                 endif
<                 call egb_queue_work(nseq_work,nseq_idx,j,xyzj,rij,full)
<                 lastj=j
<                 if(full) then
<                   LOG_STAT(203, nseq_idx)
<                   call EGB_WORK_NSEQ(nseq_work,i,
<      &                               1,nseq_idx,
<      &                               egb_accum(my_thread).evdw,
<      &                               egb_result(:,my_thread))
<                   nseq_idx=0
<                   lastj=-1
<                 endif
<               else
<                 call egb_queue_work(seq_work,seq_idx,j,xyzj,rij,full)
<                 lastj=j
<                 if(full) then
<                   LOG_STAT(202, seq_idx)
<                   call EGB_WORK_SEQ(seq_work,i,
<      &                              seq_work.j(1),seq_work.j(seq_idx),
<      &                              egb_accum(my_thread).evdw,
<      &                              egb_result(:,my_thread))
<                   seq_idx=0
<                   lastj=-1
<                 endif
<               endif
<             ENDIF  ! dyn_ss            
<           enddo    ! j
<         enddo      ! jblock
< 
<         if(seq_idx.gt.0) then
<           LOG_STAT(202, seq_idx)
<           call EGB_WORK_SEQ(seq_work,i,
<      &                      seq_work.j(1),seq_work.j(seq_idx),
---
>           if(jblock.eq.first_blk) from_j=first_j
>           if(jblock.eq.last_blk) to_j=last_j
> !DIR$ NOINLINE          ! inlining this breaks calculations
>           call EGB_WORK_SEQ(i,from_j,to_j,
198,228c77
<      &                      egb_result(:,my_thread))
<           seq_idx=0
<         endif
<         if(nseq_idx.gt.0) then
<           LOG_STAT(203, nseq_idx)
<           call EGB_WORK_NSEQ(nseq_work,i,
<      &                       1,nseq_idx,
<      &                       egb_accum(my_thread).evdw,
<      &                       egb_result(:,my_thread))
<           nseq_idx=0
<         endif
<       enddo        ! i
< 
< !$OMP END PARALLEL
< 
< !$OMP PARALLEL DO PRIVATE(g,gg,ggx,j,dsci_inv)
<       do i=1,nres
<         gg=0.0d0
<         ggx=0.0d0
<         g=0.0d0
<         do j=1,threads_used
<           gg=gg+egb_result(i,j).gvdwc
<           egb_result(i,j).gvdwc=0.0d0
< !       enddo
< !       do j=1,threads_used
<           ggx=ggx+egb_result(i,j).gvdwx
<           egb_result(i,j).gvdwx=0.0d0
< !       enddo
< !       do j=1,threads_used
<           g=g+egb_result(i,j).lip_delta
<           egb_result(i,j).lip_delta=0.0d0
---
>      &                      workspace(my_thread).item)
230,238d78
< 
<         dsci_inv=vbld_inv(i+nres)
<         if(dsci_inv.gt.1.0d20) then
<           dsci_inv=0.0d0
<         endif
< 
<         gg(3)=gg(3)+lipid_cache(nres+i).ssgrad*g
<         gvdwc(:,i)=gg
<         gvdwx(:,i)=gg+ggx*dsci_inv
240d79
< !$OMP END PARALLEL DO
242,245c81,97
<       evdw=0.0D0
<       do i=1,threads_used
<         evdw=evdw+egb_accum(i).evdw
<         egb_accum(i).evdw=0.0d0
---
>       from_ik=scsc_range(my_thread).sep_start
>       to_ik=scsc_range(my_thread).sep_end
>       first_j=scsc_range(my_thread).sep_first
>       last_j=scsc_range(my_thread).sep_last
> 
>       do i=from_ik,to_ik
>         from_j=scscseplisti(i-1)+1
>         to_j=scscseplisti(i)
> 
>         if(i.eq.from_ik) from_j=first_j
>         if(i.eq.to_ik) to_j=last_j
>         if (from_j.gt.to_j) cycle
> 
> !DIR$ NOINLINE
>         call EGB_WORK_NSEQ(i,from_j,to_j,
>      &                    egb_accum(my_thread).evdw,
>      &                    workspace(my_thread).item)
247,248c99
< 
<       return
---
> !$OMP END PARALLEL
249a101,115
> 
> C-----------------------------------------------------------------------
> #define EGB_WORK_FN        EGB_WORK_SEQ
> #define EGB_MAP_J(seq)     seq
> #include"egb_work.F"
> #undef EGB_MAP_J
> #undef EGB_WORK_FN
> 
> #define EGB_WORK_FN        EGB_WORK_NSEQ
> #define EGB_MAP_J(seq)     scscseplistj(seq)
> #define EGB_NSEQ
> #include"egb_work.F"
> #undef EGB_NSEQ
> #undef EGB_MAP_J
> #undef EGB_WORK_FN
egb_work.F
1c1,2
<       subroutine EGB_WORK_FN(chunk,i,from,to,evdw,output)
---
>       subroutine EGB_WORK_FN(i,from,to,evdw,output)
>       use omp_data
11c12
<       include 'COMMON.EGB'
---
>       include 'COMMON.FFIELD'
13a15,16
>       include 'COMMON.EGB'
>       include 'COMMON.NEWBOX'
15,17c18,21
<       record /egb_work_chunk/ chunk
<       record /egb_result_item/ output(maxres)
<       integer from,to,seq,idx,i,j,item,itypi,itypj
---
>       type(workspace_type) output
>       integer from,to,i,j,itypi,itypj,seq
>       double precision box(3),halfbox(3)
>       double precision c_i(3),xyzj(3),erij(3),gg(3)
19,32c23,34
<       double precision epsi,sigm,fac,e1,e2
<       double precision sig0ij,sig,rij_shift,faclip
<       double precision sslipij
<       double precision scalar
<       double precision xyzj(3),gamm,irij,gg_lip,eps23rt
<       double precision erij(3),gg(3),rij,
<      & chi1,chi2,chi12,chip1,chip2,chip12,alf1,alf2,alf12,om1,om2,om12,
<      & om1om2,chiom1,chiom2,chiom12,chipom1,chipom2,chipom12,eps1,
<      & faceps1,faceps1_inv,eps1_om12,facsig,sigsq,sigsq_om1,sigsq_om2,
<      & sigsq_om12,facp,facp_inv,facp1,eps2rt,eps2rt_om1,eps2rt_om2,
<      & eps3rt,eom1,eom2,eom12,evdwij,eps2der,eps3der,sigder,reom1,reom2
<       double precision d_evdw
<       double precision d_lip_delta_i
<       double precision d_gvdwx_i(3),d_gvdwx_dcnorm_i,d_gvdwc_i(3)
---
>       double precision fac,e1,e2
>       double precision sig0ij,sig,rij_shift,faclip,sslipij
>       double precision rij,gamm,irij,gg_lip,eps23rt,
>      &  chi1,chi2,chi12,chip1,chip2,chip12,alf1,alf2,alf12,om1,om2,om12,
>      &  chiom1,chiom2,chiom12,chipom1,chipom2,chipom12,eps1,
>      &  faceps1,faceps1_inv,eps1_om12,sigsq,sigsq_om1,sigsq_om2,
>      &  sigsq_om12,facp,facp_inv,eps2rt,eps2rt_om1,eps2rt_om2,
>      &  eps3rt,eom1,eom2,eom12,evdwij,eps2der,eps3der,sigder,
>      &  reom1,reom2,aal,bbl,aae,bbe,x0,r012m1,facx0,
>      &  afacx0,bfacx0,abfacx0,ABfacsig,aux,rij2
>       double precision d_gvdwc_i(3),d_gvdwx_i(3),d_gvdwx_dcnorm_i
>       double precision d_evdw,d_lip_delta_i
34,36c36,46
<       double precision x0,y0,r012,rij12,facx0,
<      &  afacx0,bfacx0,abfacx0,Afac,BBfac,Afacsig,Bfacsig,sig_inv,
<      &  afacx0_lip,bfacx0_lip,abfacx0_lip
---
>       double precision vbldinvj,vbldinvi,dexpon,mult
>       double precision droff,irlamb2,esss,esssgrad,gamm2,cutoffsq
>       double precision scalar
> 
>       box=boxsize
>       halfbox=halfboxsize
>       mult=wsc
>       dexpon=expon
> 
>       cutoffsq=(r_cut_int-rlamb)*(r_cut_int-rlamb)
> 
48c58,64
<       lipid_i_ss=lipid_cache(nres+i).ss
---
>       lipid_i_ss=lipid_cache_ss(nres+i)
> 
>       droff=r_dropoff*irlamb
>       irlamb2=irlamb*6.0d0
>       c_i(1)=soa_c_x(nres+i)
>       c_i(2)=soa_c_y(nres+i)
>       c_i(3)=soa_c_z(nres+i)
55,61c71,77
< !$OMP&   xyzj,rij,irij,itypj,sig0ij,chi1,chi2,chi12,chip2,chip12,
< !$OMP&   alf2,alf12,sslipij,aa,bb,gamm,sss,sssgrad,
< !$OMP&   epsi,sigm,fac,e1,e2,sig,rij_shift,faclip,
< !$OMP&   om1,om2,om12,om1om2,chiom1,chiom2,chiom12,
< !$OMP&   chipom1,chipom2,chipom12,eps1,faceps1,faceps1_inv,
< !$OMP&   eps1_om12,facsig,sigsq,sigsq_om1,sigsq_om2,sigsq_om12,
< !$OMP&   facp,facp_inv,facp1,eps2rt,eps2rt_om1,eps2rt_om2,
---
> !$OMP&   j,xyzj,rij,irij,itypj,sig0ij,chi1,chi2,chi12,chip2,chip12,
> !$OMP&   esss,esssgrad,gamm,gamm2,alf2,alf12,sslipij,
> !$OMP&   fac,e1,e2,sig,rij_shift,faclip,om1,om2,om12,
> !$OMP&   chiom1,chiom2,chiom12,chipom1,chipom2,chipom12,reom1,reom2,
> !$OMP&   eps1,faceps1,faceps1_inv,gg_lip,eps23rt,erij,gg,
> !$OMP&   eps1_om12,sigsq,sigsq_om1,sigsq_om2,sigsq_om12,
> !$OMP&   facp,facp_inv,eps2rt,eps2rt_om1,eps2rt_om2,
63,66c79,80
< !$OMP&   gg_lip,eps23rt,erij,gg,j,idx,reom1,reom2,
< !$OMP&   x0,y0,r012,rij12,facx0,afacx0,bfacx0,abfacx0,
< !$OMP&   afacx0_lip,bfacx0_lip,abfacx0_lip,
< !$OMP&   Afac,BBfac,Afacsig,Bfacsig,sig_inv)
---
> !$OMP&   x0,r012m1,facx0,afacx0,bfacx0,abfacx0,
> !$OMP&   aal,bbl,aae,bbe,ABfacsig,vbldinvj,aux,rij2)
69,73d82
<         idx=EGB_MAP_IDX(seq)
<         xyzj(1)=chunk.x(idx)
<         xyzj(2)=chunk.y(idx)
<         xyzj(3)=chunk.z(idx)
<         rij=chunk.r(idx)
75,76c84,111
<         irij=dsqrt(rij)
<         rij=1.0d0/irij
---
> !       write(*,'(a,2i4,3f18.12)')'XI',i,j,c_i(:)
> 
> !       call wrapvec(c_tobox(:,nres+j)-c_tobox(:,nres+i),xyzj)
> !       call wrapvec2(c_tobox(:,nres+j)-c_tobox(:,nres+i),xyzj,
> !    &                halfbox,box)
>         call wrapvec((/
>      &    soa_c_x(nres+j)-c_i(1),
>      &    soa_c_y(nres+j)-c_i(2),
>      &    soa_c_z(nres+j)-c_i(3)
>      &    /),xyzj)
>         !rij=scalar(xyzj,xyzj)
> 
> #ifdef EGBLOG
>         write(*,'(a,2i4,3f18.12)')'EGBa',i,j,c_i(:)
>         write(*,'(a,2i4,3f18.12)')'EGBb',i,j,
>      &    soa_c_x(nres+j),soa_c_y(nres+j),soa_c_z(nres+j)
> #endif
> 
>         !irij=dsqrt(rij)
>         !rij=1.0d0/irij
>         rij2=scalar(xyzj,xyzj)
> #ifdef EGBLOG
>         write(*,'(a,2i4,1f18.12)')'EGBc',i,j,rij2
> #endif
> 
>         rij=real(1.0/sqrt(real(rij2,4)),8) ! low precision square root
>         rij=rij*(1.5d0-0.5d0*rij2*rij*rij) ! with single Newton step
>         irij=rij2*rij
80,82d114
< !       if(abs_itype(j).ne.iabs(itype(j)))then
< !         write(*,*)'FAIL',j,abs_itype(j),iabs(itype(j))
< !       endif
86a119,120
>         !sig0ij=sigma_j(itypj,itypi)
> #ifdef EGB_NSEQ
88d121
<         !chi1=chi(itypi,itypj)
91d123
<         chi12=chi1*chi2
93d124
<         chip12=chip1*chip2
95c126,141
<         alf12=0.5D0*(alf1+alf2)
---
> #else
>         sig0ij=egb_data2d(j,1,itypi)
>         chi1=egb_data2d(j,2,itypi)
>         chi2=egb_data2d(j,3,itypi)
>         chip2=egb_data1d(j,1)
>         alf2=egb_data1d(j,2)
> #endif
> 
>         !chi1=chi(itypi,itypj)
>         !chi1=chi_j(itypj,itypi)
>         !chi2=chi(itypj,itypi)
>         chi12=chi1*chi2
>         !chip2=chip(itypj)
>         chip12=chip1*chip2
>         !alf2=alp(itypj)
>         alf12=0.5d0*(alf1+alf2)
97c143
<         sslipij=0.5d0*(lipid_i_ss+lipid_cache(nres+j).ss)
---
>         sslipij=0.5d0*(lipid_i_ss+lipid_cache_ss(nres+j))
103,104c149,164
<         aa=aa_lipaq(itypj,itypi)*sslipij+aa_aq_j(itypj,itypi)
<         bb=bb_lipaq(itypj,itypi)*sslipij+bb_aq_j(itypj,itypi)
---
> #ifdef EGB_NSEQ
>         aal=aa_lipaq(itypj,itypi)
>         bbl=bb_lipaq(itypj,itypi)
> #else
>         aal=egb_data2d(j,4,itypi)
>         bbl=egb_data2d(j,5,itypi)
> #endif
>         !aa=aa_lipaq(itypj,itypi)*sslipij+aa_aq_j(itypj,itypi)
>         !bb=bb_lipaq(itypj,itypi)*sslipij+bb_aq_j(itypj,itypi)
> #ifdef EGB_NSEQ
>         aae=aal*sslipij+aa_aq_j(itypj,itypi)
>         bbe=bbl*sslipij+bb_aq_j(itypj,itypi)
> #else
>         aae=aal*sslipij+egb_data2d(j,6,itypi)
>         bbe=bbl*sslipij+egb_data2d(j,7,itypi)
> #endif
124,131c184,209
<         if(irij.lt.r_cut_int-rlamb) then
<           sss=1.0d0
<           sssgrad=0.0d0
<         else
<           gamm=(irij-(r_cut_int-rlamb))*irlamb
<           sss=1.0d0+gamm*gamm*(2*gamm-3.0d0)
<           sssgrad=gamm*(6*gamm-6.0d0)*irlamb
<         endif
---
>         !if(irij.lt.r_cut_int-rlamb) then
>         !  sss=1.0d0
>         !  sssgrad=0.0d0
>         !else
>         !  gamm=(irij-(r_cut_int-rlamb))*irlamb
>         !  sss=1.0d0+gamm*gamm*(2*gamm-3.0d0)
>         !  sssgrad=gamm*(6*gamm-6.0d0)*irlamb
>         !endif
> 
>         !gamm=max(0.0d0,irij*irlamb-droff)
>         !gamm=min(1.0d0,gamm)
>         !gamm2=gamm*gamm
>         !esss=1.0d0+gamm2*(2.0d0*gamm-3.0d0)
>         !esssgrad=(gamm2-gamm)*irlamb2
> 
>         !if (rij2.lt.cutoffsq) then
>         !  esss=1.0d0
>         !  esssgrad=0.0d0
>         !else
>         !  gamm=max(0.0d0,irij*irlamb-droff)
>         !  gamm=min(1.0d0,gamm)
>         !  gamm2=gamm*gamm
>         !  esss=1.0d0+gamm2*(2.0d0*gamm-3.0d0)
>         !  esssgrad=(gamm2-gamm)*irlamb2
>         !endif
> 
136,138c214,226
<         om1=scalar(dc_norm(:,nres+i),erij)
<         om2=scalar(dc_norm(:,nres+j),erij)
<         om12=scalar(dc_norm(:,nres+i),dc_norm(:,nres+j))
---
> !       om1=scalar(dc_norm(:,nres+i),erij)
> !       om2=scalar(dc_norm(:,nres+j),erij)
> !       om12=scalar(dc_norm(:,nres+i),dc_norm(:,nres+j))
>         om1=soa_dc_norm_x(nres+i)*erij(1)
>      &     +soa_dc_norm_y(nres+i)*erij(2)
>      &     +soa_dc_norm_z(nres+i)*erij(3)
>         om2=soa_dc_norm_x(nres+j)*erij(1)
>      &     +soa_dc_norm_y(nres+j)*erij(2)
>      &     +soa_dc_norm_z(nres+j)*erij(3)
>         om12=soa_dc_norm_x(nres+j)*soa_dc_norm_x(nres+i)
>      &      +soa_dc_norm_y(nres+j)*soa_dc_norm_y(nres+i)
>      &      +soa_dc_norm_z(nres+j)*soa_dc_norm_z(nres+i)
> 
142,143c230,234
<         faceps1_inv=1.0D0/faceps1
<         eps1=dsqrt(faceps1_inv)
---
>         !faceps1_inv=1.0D0/faceps1
>         !eps1=dsqrt(faceps1_inv)
>         eps1=real(1.0/sqrt(real(faceps1,4)),8)    ! low precision square root
>         eps1=eps1*(1.5d0-0.5d0*faceps1*eps1*eps1) ! with single Newton step
>         faceps1_inv=eps1*eps1
148c239
<         om1om2=om1*om2
---
>         !om1om2=om1*om2
151,152c242,243
<         facsig=om1*chiom1+om2*chiom2-2.0D0*om1om2*chiom12
<         sigsq=1.0D0-facsig*faceps1_inv
---
>         !facsig=om1*chiom1+om2*chiom2-2.0D0*om1om2*chiom12
>         !sigsq=1.0D0-facsig*faceps1_inv
156a248
>         sigsq=1.0D0-om1*sigsq_om1-om2*sigsq_om2
164c256
<         facp1=om1*chipom1+om2*chipom2-2.0D0*om1om2*chipom12
---
> !       facp1=om1*chipom1+om2*chipom2-2.0D0*om1om2*chipom12
166c258
<         eps2rt=1.0D0-facp1*facp_inv
---
> !       eps2rt=1.0D0-facp1*facp_inv
173a266,267
> 
>         eps2rt=1.0D0+om1*eps2rt_om1+om2*eps2rt_om2
179a274
> 
182,183c277,282
<         sigsq=1.0D0/sigsq
<         sig=sig0ij*dsqrt(sigsq)
---
>         !sigsq=1.0D0/sigsq
>         !sig=sig0ij*dsqrt(sigsq)
>         aux=real(1.0/sqrt(real(sigsq,4)),8) ! low precision square root
>         aux=aux*(1.5d0-0.5d0*sigsq*aux*aux) ! with single Newton step
>         sig=sig0ij*aux
>         sigsq=aux*aux
192a292
>         !x0=alpha_GB*sig0ij*(dsqrt(sigsq)-1.0)
198,230c298,324
<         if (rij_shift.le.x0) then
< c         sigder=0.0d0
<           EGB_POW(rij,fac)
<           rij12=fac*fac
< c         rij12=1.0d0
< !!        facx0=1.0d0/x0**expon
<           !facx0=1.0d0/x0
<           !EGB_POW(facx0,facx0)
<           EGB_POW(x0,facx0)
<           facx0=1.0d0/facx0
< 
< !!        facx02=facx0*facx0
< !!        r012=((1.0d0+alpha_GB)*(sig-sig0ij))**(2*expon)
<           r012=(1.0d0+alpha_GB)*(sig-sig0ij)
<           EGB_POW(r012,r012)
<           r012=r012*r012
< 
<           sig_inv=1.0d0/(sig-sig0ij)
< 
<           afacx0=aa*facx0*facx0
<           bfacx0=bb*facx0
<           abfacx0=afacx0+0.5d0*bfacx0
<           Afac=alpha_GB1*abfacx0
<           Afacsig=0.5d0*alpha_GB1*bfacx0*sig_inv
<           BBfac=Afac-(afacx0+bfacx0)
< c         BBfac=0.0d0
<           Bfacsig=(-alpha_GB1*(abfacx0+afacx0)+
<      &        (afacx0+afacx0+bfacx0))*sig_inv
< c         Bfacsig=0.0d0
<           Afac=Afac*r012
<           Afacsig=Afacsig*r012
< c         Afac=1.0d0
< c         Afacsig=0.0d0
---
>         rij_shift=max(rij_shift,x0)
>         e1=rij_shift
>         rij_shift=1.0d0/rij_shift
>         EGB_POW(rij_shift,facx0)
> 
>         afacx0=eps1*facx0*facx0*aae
>         bfacx0=eps1*facx0*bbe
>         abfacx0=afacx0+0.5d0*bfacx0
>         evdwij=afacx0+bfacx0
>         fac=-dexpon*eps23rt
> 
>         sigder=sigder*fac*rij_shift
>         fac=fac*2.0d0*abfacx0
> 
>         faclip=facx0*aal+bbl
> 
>         ABfacsig=abfacx0
> 
>         ! wystarczy e1.eq.x0 bo e1=max(rij_shift, x0) ???
>         if (e1.le.x0) then  !------------------------------------------
>           rij_shift=rij
>           e1=rij*(x0+sig-sig0ij)
>           !e1=rij*(alpha_GB+1.0d0)*(sig-sig0ij)
>           EGB_POW(e1,r012m1)
>           r012m1=r012m1*r012m1-1.0d0
>           e2=alpha_GB1*r012m1
>           ABfacsig=ABfacsig+0.5d0*bfacx0*r012m1*(alpha_GB+1.0d0)
233,313c327,332
< c         eps1=1.0d0
< c         eps2rt=1.0d0
< c         eps3rt=1.0d0
< !!        e1 = eps1*eps2rt*eps3rt*Afac*rij12
<           e1 = eps1*Afac*rij12
< !!        e2 = -eps1*eps2rt*eps3rt*BBfac
<           e2 = -eps1*BBfac
<           evdwij = e1+e2
< !         eps2der=evdwij*eps3rt
< !         eps3der=evdwij*eps2rt
<           eps2der=evdwij*eps3rt*4.0d0 !changes by K
<           eps3der=evdwij*eps2rt*2.0d0 !changes by K
< c         eps2der=0.0d0
< c         eps3der=0.0d0
< c         eps1_om12=0.0d0
<           evdwij=evdwij*eps23rt
< c         Afacsig=0.d0
< c         Bfacsig=0.0d0
< #ifdef DEBUG
<           if (lprn) then
<             write (iout,*) "aa",aa," bb",bb," sig0ij",sig0ij
<             sigm=dabs(aa/bb)**(1.0D0/6.0D0)
<             epsi=bb**2/aa
<             write (iout,'(2(a3,i3,2x),18(0pf9.5))')
<      &      restyp(itypi),i,restyp(itypj),j,
<      &      epsi,sigm,chi1,chi2,chip1,chip2,
<      &      eps1,eps2rt**2,eps3rt**2,sig,sig0ij,
<      &      eps1*eps2rt**2*eps3rt**2,om1,om2,om12,
<      &      1.0D0/rij,rij_shift,
<      &      evdwij
<           endif
<           if (energy_dec) write (iout,'(a,2i5,4f10.5,e15.5)')
<      &      'RE r sss evdw',i,j,1.0d0/rij,sss,sslipi,sslipj,evdwij
< #endif
<           eps1=eps1*eps23rt
< 
< C Calculate gradient components.
<           e1=e1*eps23rt
<           sigder=-expon*eps1*(Afacsig*rij12-Bfacsig)*sigder
<           fac=-2.0d0*expon*e1*rij!*rij
< c         print '(2i4,6f8.4)',i,j,sss,sssgrad*
< c     &     evdwij,fac,sigma(itypi,itypj),expon
< !         fac=fac+evdwij*sssgrad/sss*rij
< c         fac=0.0d0
< c         write (iout,*) "sigder",sigder," fac",fac," e1",e1,
< c     &       " e2",e2," sss",sss," sssgrad",sssgrad,"esp123",
< c     &       eps1*eps2rt**2*eps3rt**2
< 
< c         evdw=1.0D20
< cd        write (iout,'(2(a3,i3,2x),17(0pf7.3))')
< cd     &    restyp(itypi),i,restyp(itypj),j,
< cd     &    rij_shift,1.0D0/rij,sig,sig0ij,sigsq,1-dsqrt(sigsq) 
< c         return
<         afacx0_lip=aa_lipaq(itypi,itypj)*facx0*facx0
<         bfacx0_lip=bb_lipaq(itypi,itypj)*facx0
<         abfacx0_lip=afacx0_lip+0.5d0*bfacx0_lip
<         faclip=alpha_GB1*abfacx0_lip*(r012*rij12-1.0d0)
<      &        +facx0*(afacx0_lip+bfacx0_lip)
<         fac=fac*sss+evdwij*sssgrad
<         gg_lip=eps1*sss*0.5d0*faclip
<         else
< c---------------------------------------------------------------
<           rij_shift=1.0D0/rij_shift 
<           EGB_POW(rij_shift,fac)
< 
< C here to start with
<           faclip=fac
<           e1=fac*fac*aa
<           e2=fac*bb
<           !evdwij=eps1*eps2rt*eps3rt*(e1+e2)
<           !evdwij=eps1*eps23rt*(e1+e2)
<           evdwij=eps1*(e1+e2)
<           eps2der=evdwij*eps3rt*4.0d0
<           eps3der=evdwij*eps2rt*2.0d0
< C         write(63,'(2i3,2e10.3,2f10.5)') i,j,aa,bb, evdwij,
< C    &       ((sslipi+sslipj)/2.0d0+
< C    &       (2.0d0-sslipi-sslipj)/2.0d0)
< c         write (iout,*) "sigsq",sigsq," sig",sig," eps2rt",eps2rt,
< c    &       " eps3rt",eps3rt," eps1",eps1," e1",e1," e2",e2
<           !evdwij=evdwij*eps2rt*eps3rt
<           evdwij=evdwij*eps23rt
---
> c
>           evdwij=evdwij+abfacx0*e2
>           fac=fac*(e2+alpha_GB1)
>           faclip=(faclip-0.5d0*bbl)*e2+facx0*faclip
>           !faclip=(e2+facx0)*faclip-0.5d0*bbl*e2
>         endif  !-------------------------------------------------------
316,327c335,349
<           if (lprn) then
<             sigm=dabs(aa/bb)**(1.0D0/6.0D0)
<             epsi=bb**2/aa
<             write (iout,'(2(a3,i3,2x),17(0pf7.3))')
<      &       restyp(itypi),i,restyp(itypj),j,
<      &       epsi,sigm,chi1,chi2,chip1,chip2,
<      &       eps1,eps2rt**2,eps3rt**2,sig,sig0ij,
<      &       om1,om2,om12,1.0D0/rij,1.0D0/rij_shift,
<      &       evdwij
<           endif
<           if (energy_dec) write (iout,'(a,2i5,4f10.5,e15.5)') 
<      &      'r sss evdw',i,j,1.0d0/rij,sss,sslipi,sslipj,evdwij
---
> c          if (lprn) then
> c            write (iout,*) "aa",aa," bb",bb," sig0ij",sig0ij
> c            sigm=dabs(aa/bb)**(1.0D0/6.0D0)
> c            epsi=bb**2/aa
> c            write (iout,'(2(a3,i3,2x),18(0pf9.5))')
> c     &      restyp(itypi),i,restyp(itypj),j,
> c     &      epsi,sigm,chi1,chi2,chip1,chip2,
> c     &      eps1,eps2rt**2,eps3rt**2,sig,sig0ij,
> c     &      eps1*eps2rt**2*eps3rt**2,om1,om2,om12,
> c     &      1.0D0/rij,rij_shift,
> c     &      evdwij
> c          endif
>           if (energy_dec) write (iout,'(a,2i5,2f10.5,e15.5)')
>      &      'RE r sss evdw',i,j,1.0d0/rij,esss,evdwij
> c     &      'RE r sss evdw',i,j,1.0d0/rij,sss,sslipi,sslipj,evdwij
329d350
<           eps1=eps1*eps23rt
331,351c352
< C Calculate gradient components.
<           !e1=e1*eps1*eps23rt**2
<           e1=e1*eps1
<           fac=-expon*(e1+evdwij)*rij_shift
<           sigder=fac*sigder
<           !fac=rij*fac
< c         print '(2i4,6f8.4)',i,j,sss,sssgrad*
< c    &                      evdwij,fac,sigma(itypi,itypj),expon
< 
<           fac=fac*sss+evdwij*sssgrad
< 
< C Calculate the radial part of the gradient
< !         gg_lip=eps1*eps23rt
< !    &      *eps23rt*sss*0.5d0*(faclip*faclip*
< !    &       (aa_lip(itypi,itypj)-aa_aq(itypi,itypj))
< !    &      +faclip*(bb_lip(itypi,itypj)-bb_aq(itypi,itypj)))
< !    &      *eps23rt*sss*0.5d0*faclip
<           gg_lip=eps1
<      &      *sss*0.5d0*faclip
<      &      *(faclip*aa_lipaq(itypj,itypi)+bb_lipaq(itypj,itypi))
<         endif
---
>         sigder=sigder*(abfacx0+ABfacsig)
353,355c354,367
<         d_evdw=d_evdw+evdwij*sss
<         output(j).lip_delta=output(j).lip_delta+gg_lip
<         d_lip_delta_i=d_lip_delta_i+gg_lip
---
>         !gg_lip=eps1*eps23rt*esss*0.5d0*faclip*facx0
>         gg_lip=eps1*eps23rt*0.5d0*faclip*facx0
> 
>         eps2der=evdwij*eps3rt*4.0d0
>         eps3der=evdwij*eps2rt*2.0d0
>         evdwij=evdwij*eps23rt
>         !fac=fac*rij_shift*esss+evdwij*esssgrad
>         fac=fac*rij_shift
> 
>         !d_evdw=d_evdw+evdwij*esss
>         !output.lip_delta(j)=output.lip_delta(j)+gg_lip
>         !d_lip_delta_i=d_lip_delta_i+gg_lip
>         !write(*,'(a,2i6,3f18.12)')'G ',i,j,afacx0,bfacx0,esss
>         !write(*,'(a,2i6,3f18.12)')'G ',i,j,evdwij*esss
358c370
< c       call sc_grad_scale(sss)
---
> c       call sc_grad_scale(esss)
364d375
< !    &        -alf12*eps3der+sigder*sigsq_om12
366,368c377,406
<         eom1=eom1*sss
<         eom2=eom2*sss
<         eom12=eom12*sss
---
>         !if (rij2.lt.cutoffsq) then
>         !  !esss=1.0d0
>         !  !esssgrad=0.0d0
>         !else
>         if (rij2.gt.cutoffsq) then
>           gamm=max(0.0d0,irij*irlamb-droff)
>           gamm=min(1.0d0,gamm)
>           gamm2=gamm*gamm
>           esss=1.0d0+gamm2*(2.0d0*gamm-3.0d0)
>           esssgrad=(gamm2-gamm)*irlamb2
> 
>           fac=fac*esss+evdwij*esssgrad
>           gg_lip=gg_lip*esss
>           evdwij=evdwij*esss
>           eom1=eom1*esss
>           eom2=eom2*esss
>           eom12=eom12*esss
>         endif
>         !gg_lip=gg_lip*esss
>         !fac=fac*rij_shift*esss+evdwij*esssgrad
>         !d_evdw=d_evdw+evdwij*esss
>         d_evdw=d_evdw+evdwij
>         !output.lip_delta(j)=output.lip_delta(j)+gg_lip
>         gg_lip=wsc*gg_lip
>         output.dlipssgrad_nres(j)=output.dlipssgrad_nres(j)+gg_lip
>         d_lip_delta_i=d_lip_delta_i+gg_lip
> 
>         !eom1=eom1*esss
>         !eom2=eom2*esss
>         !eom12=eom12*esss
375a414
>         aux=(fac-rij*(om1+om2))
377c416
< !       rij=rij*sss
---
> !       rij=rij*esss
380,382c419,424
<         gg=erij*(fac-rij*(om1+om2))
< !    &     +rij*(eom1*dc_norm(:,nres+i)+eom2*dc_norm(:,nres+j))
<      &     +reom1*dc_norm(:,nres+i)+reom2*dc_norm(:,nres+j)
---
>         gg(1)=erij(1)*aux
>      &    +reom1*soa_dc_norm_x(nres+i)+reom2*soa_dc_norm_x(nres+j)
>         gg(2)=erij(2)*aux
>      &    +reom1*soa_dc_norm_y(nres+i)+reom2*soa_dc_norm_y(nres+j)
>         gg(3)=erij(3)*aux
>      &    +reom1*soa_dc_norm_z(nres+i)+reom2*soa_dc_norm_z(nres+j)
383a426
>         ! gvdwc
385c428,430
<         output(j).gvdwc=output(j).gvdwc+gg(:)
---
>         output.dgradbufcx(j)=output.dgradbufcx(j)+mult*gg(1)
>         output.dgradbufcy(j)=output.dgradbufcy(j)+mult*gg(2)
>         output.dgradbufcz(j)=output.dgradbufcz(j)+mult*gg(3)
387c432
< !       d_gvdwx_i=d_gvdwx_i+sss*(
---
> !       d_gvdwx_i=d_gvdwx_i+esss*(
391,394c436,441
<         d_gvdwx_i=d_gvdwx_i
<      &           +eom12*dc_norm(:,nres+j)
<      &           +eom1*erij(:)
< !    &           -(om12+om1)*dc_norm(:,nres+i)
---
>         d_gvdwx_i(1)=d_gvdwx_i(1)
>      &           +eom12*soa_dc_norm_x(nres+j)+eom1*erij(1)
>         d_gvdwx_i(2)=d_gvdwx_i(2)
>      &           +eom12*soa_dc_norm_y(nres+j)+eom1*erij(2)
>         d_gvdwx_i(3)=d_gvdwx_i(3)
>      &           +eom12*soa_dc_norm_z(nres+j)+eom1*erij(3)
396,403c443,486
< !       output(j).gvdwx=output(j).gvdwx+sss*(
< !    &     +eom12*(dc_norm(:,nres+i)-om12*dc_norm(:,nres+j))
< !    &     +eom2*(erij(:) - om2*dc_norm(:,nres+j))
< !    &      )
<         output(j).gvdwx=output(j).gvdwx
<      &                 +eom12*dc_norm(:,nres+i)
<      &                 +eom2*erij(:)
<      &                 -(om12+om2)*dc_norm(:,nres+j)
---
> 
>         ! gvdwx
> !      output(j).gvdwx=output(j).gvdwx+esss*(
> !   &     +eom12*(dc_norm(:,nres+i)-om12*dc_norm(:,nres+j))
> !   &     +eom2*(erij(:) - om2*dc_norm(:,nres+j))
> !   &      )
> !       vbldinvj=vbld_inv(nres+j)
> !       output.gvdwxx(j)=output.gvdwxx(j)+gg(1)
> !    &                 +vbldinvj*(
> !    &                 +eom12*soa_dc_norm_x(nres+i)
> !    &                 +eom2*erij(1)
> !    &                 -(om12+om2)*soa_dc_norm_x(nres+j)
> !    &                 )
> !       output.gvdwxy(j)=output.gvdwxy(j)+gg(2)
> !    &                 +vbldinvj*(
> !    &                 +eom12*soa_dc_norm_y(nres+i)
> !    &                 +eom2*erij(2)
> !    &                 -(om12+om2)*soa_dc_norm_y(nres+j)
> !    &                 )
> !       output.gvdwxz(j)=output.gvdwxz(j)+gg(3)
> !    &                 +vbldinvj*(
> !    &                 +eom12*soa_dc_norm_z(nres+i)
> !    &                 +eom2*erij(3)
> !    &                 -(om12+om2)*soa_dc_norm_z(nres+j)
> !    &                 )
>         vbldinvj=vbld_inv(nres+j)*wsc
>         output.dgradxx(j)=output.dgradxx(j)+wsc*gg(1)
>      &    +vbldinvj*(
>      &      +eom12*soa_dc_norm_x(nres+i)
>      &      +eom2*erij(1)
>      &      -(om12+om2)*soa_dc_norm_x(nres+j)
>      &    )
>         output.dgradxy(j)=output.dgradxy(j)+wsc*gg(2)
>      &    +vbldinvj*(
>      &      +eom12*soa_dc_norm_y(nres+i)
>      &      +eom2*erij(2)
>      &      -(om12+om2)*soa_dc_norm_y(nres+j)
>      &    )
>         output.dgradxz(j)=output.dgradxz(j)+wsc*gg(3)
>      &    +vbldinvj*(
>      &      +eom12*soa_dc_norm_z(nres+i)
>      &      +eom2*erij(3)
>      &      -(om12+om2)*soa_dc_norm_z(nres+j)
>      &    )
406a490
>       ! gvdwc
408,411c492,527
<       output(i).lip_delta=output(i).lip_delta+d_lip_delta_i
<       output(i).gvdwc=output(i).gvdwc+d_gvdwc_i
<       output(i).gvdwx=output(i).gvdwx+d_gvdwx_i
<      &               +d_gvdwx_dcnorm_i*dc_norm(:,nres+i)
---
>       !output.lip_delta(i)=output.lip_delta(i)+d_lip_delta_i
>       output.dlipssgrad_nres(i)=output.dlipssgrad_nres(i)+d_lip_delta_i
>       output.dgradbufcx(i)=output.dgradbufcx(i)+mult*d_gvdwc_i(1)
>       output.dgradbufcy(i)=output.dgradbufcy(i)+mult*d_gvdwc_i(2)
>       output.dgradbufcz(i)=output.dgradbufcz(i)+mult*d_gvdwc_i(3)
> 
>       ! gvdwx
> !     vbldinvi=vbld_inv(nres+i)
> !     output.gvdwxx(i)=output.gvdwxx(i)+d_gvdwc_i(1)
> !    &                 +vbldinvi*(
> !    &               +d_gvdwx_i(1)
> !    &               +d_gvdwx_dcnorm_i*soa_dc_norm_x(nres+i)
> !    &                 )
> !     output.gvdwxy(i)=output.gvdwxy(i)+d_gvdwc_i(2)
> !    &                 +vbldinvi*(
> !    &               +d_gvdwx_i(2)
> !    &               +d_gvdwx_dcnorm_i*soa_dc_norm_y(nres+i)
> !    &                 )
> !     output.gvdwxz(i)=output.gvdwxz(i)+d_gvdwc_i(3)
> !    &                 +vbldinvi*(
> !    &               +d_gvdwx_i(3)
> !    &               +d_gvdwx_dcnorm_i*soa_dc_norm_z(nres+i)
> !    &                 )
>       vbldinvi=vbld_inv(nres+i)*wsc
>       output.dgradxx(i)=output.dgradxx(i)+wsc*d_gvdwc_i(1)
>      &  +vbldinvi*(
>      &    d_gvdwx_i(1)+d_gvdwx_dcnorm_i*soa_dc_norm_x(nres+i)
>      &  )
>       output.dgradxy(i)=output.dgradxy(i)+wsc*d_gvdwc_i(2)
>      &  +vbldinvi*(
>      &    d_gvdwx_i(2)+d_gvdwx_dcnorm_i*soa_dc_norm_y(nres+i)
>      &  )
>       output.dgradxz(i)=output.dgradxz(i)+wsc*d_gvdwc_i(3)
>      &  +vbldinvi*(
>      &    d_gvdwx_i(3)+d_gvdwx_dcnorm_i*soa_dc_norm_z(nres+i)
>      &  )
eigen.f
elecont.f
e_modeller.F
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/e_modeller.F: No such file or directory
energy_p_new_barrier.F
0a1,2
> #include"timers"
> 
20a23
>       double precision timer_value
39a43,44
>       include 'COMMON.OMP'
>       include 'COMMON.GRID'
43,45d47
<       include 'COMMON.GRID'
<       include 'COMMON.OMP'
<       include 'COMMON.NMR'
50c52
<      & esaxs_constr,ehomology_constr,edfator,edfanei,edfabet
---
>      & esaxs_constr,ehomology_constr,edfator,edfanei,edfabet,ess
53c55
<       logical limit_threads,need_lists
---
>       logical need_lists
54a57
>       logical calc_eelec,calc_escp,calc_egb
55a59,60
>       integer i
> 
97a103
>           weights_(32)=1.0d0    ! ess
136c142
<       call zerograd
---
>       !call zerograd
138,139c144
<         call MPI_Bcast(itime_mat,1,MPI_INTEGER,king,FG_COMM,IERROR)
<         call MPI_Bcast(atimeave,1,MPI_INTEGER,king,FG_COMM,IERROR)
---
>         call MPI_Bcast(itime_mat,1,MPI_INT,king,FG_COMM,IERROR)
142c147
<       call zerograd
---
>       !call zerograd
147a153,172
> !#ifdef WITH_CUDA
> !      call cuda_etotal(energia)
> !
> !#else
>       call zerograd
>       !call omp_clear_buffers
> 
> #ifdef SPLITELE
>       calc_eelec = welec.gt.0d0.or.wvdwpp.gt.0d0.or.wel_loc.gt.0d0
>      &  .or. wturn3.gt.0d0.or.wturn4.gt.0d0 .or. wcorr.gt.0.0d0
>      &  .or. wcorr4.gt.0.0d0 .or. wcorr5.gt.0.d0
>      &  .or. wcorr6.gt.0.0d0 .or. wturn6.gt.0.0d0
> #else
>       calc_eelec = welec.gt.0d0.or.wel_loc.gt.0d0
>      &  .or. wturn3.gt.0d0.or.wturn4.gt.0d0 .or. wcorr.gt.0.0d0
>      &  .or. wcorr4.gt.0.0d0 .or. wcorr5.gt.0.d0
>      &  .or. wcorr6.gt.0.0d0 .or. wturn6.gt.0.0d0
> #endif
>       calc_escp = wscp.gt.0d0
> 
148a174,175
>       call make_soa_dc_norm
>       call calculate_lipid_layers
154,156c181,185
< #ifdef TIMING_ENE
< #ifdef MPI
<         time01=MPI_Wtime()
---
> 
>       START_TIME_01
> 
> #ifdef _OPENMP
>         max_threads=omp_get_max_threads()
158,159c187
<         time01=tcpu()
< #endif
---
>         max_threads=1
160a189,190
> 
>         !write(*,*)'hst grid recalc'
161a192,214
> !       do i=1,nres
> !         write(*,'(a,i4,3f18.12)')'C', i,c(:,i)
> !         write(*,'(a,i4,3f18.12)')'DC',i, dc(:,i)
> !         write(*,'(a,i4,3f18.12)')'Cn',i, c(:,i+nres)
> !         write(*,'(a,i4,3f18.12)')'DCn',i, dc(:,i+nres)
> !         write(*,'(a,i4,3f18.12)')'DCN',i, dc_norm(:,i)
> !         write(*,'(a,i4,3f18.12)')'DCNn',i, dc_norm(:,i+nres)
> !         write(*,'(a,i4,3f18.12)')'VBLD',i,
> !    &      vbld(i),vbld_inv(i),vbld_inv(i+nres)
> !       enddo
> 
>         ! escp list
>         scp_threads=max_threads
>         call make_split_inter_list(1,nres,iatscp_s,iatscp_e,
>      &    nscp_gr,iscpstart,iscpend,
>      &    cp(1,1),c_tobox(1,1),abs_itel,abs_itype,2,
>      &    newcontlistscpi,newcontlistscpj,
>      &    scpseplisti,scpseplistj,
>      &    gridcellsc,dgridrangep,griddatap,3,
>      &    scp_threads,scp_range,r_cut_int,r_buff_list)
> !       call dump_interactions('ISCP  ',scp_range,
> !    &    newcontlistscpi,newcontlistscpj,
> !    &    scpseplisti,scpseplistj,scp_threads)
163,168d215
<         call make_inter_list(iatscp_s,iatscp_e,nscp_gr,iscpstart,
<      &   iscpend,
<      &   cp(1,1),c_tobox(1,1),abs_itel,abs_itype,2,
<      &   g_listscp_start,g_listscp_end,newcontlistscpi,
<      &   newcontlistscpj,positive_sc,positive_p,
<      &   gridcellsc,dgridrangep,griddatap)
172c219,224
<         call make_inter_list(iatsc_s,iatsc_e,nint_gr,istart,iend,
---
> 
>         ! egb list
>         scsc_threads=max_threads
>         dynss_threads=max_threads
>         call make_split_inter_list_egb(1,nres,iatsc_s,iatsc_e,
>      &   nint_gr,istart,iend,
174,176c226,236
<      &   g_listscsc_start,g_listscsc_end,newcontlisti,newcontlistj,
<      &   positive_pn,positive_pn,
<      &   gridcellpn,dgridrangepn,griddatapn)
---
>      &   dyn_ss_mask,
>      &   newcontlisti,newcontlistj,scscseplisti,scscseplistj,
>      &   dynss_seplisti,dynss_seplistj,
>      &   gridcellpn,dgridrangepn,griddatapn,0,
>      &   scsc_threads,scsc_range,dynss_range,r_cut_int,r_buff_list)
> !       call dump_interactions('IEGB  ',scsc_range,
> !    &    newcontlisti,newcontlistj,
> !    &    scscseplisti,scscseplistj,scsc_threads)
> !       call dump_sep_interactions('IDYNSS',dynss_range,
> !    &    dynss_seplisti,dynss_seplistj,scsc_threads)
> 
180,184c240,252
<         call make_inter_list(iatel_s,iatel_e,nint_gr_el,ielstart,ielend,
<      &   cp(1,1),cp(1,1),abs_itel,abs_itel,3,
<      &   g_listpp_start,g_listpp_end,newcontlistppi,newcontlistppj,
<      &   positive_sc,positive_sc,
<      &   gridcellsc,dgridrangesc,griddatasc)
---
> 
>         ! eelec lists
>         pp_threads=max_threads
>         call make_split_inter_list(1,nct-1,iatel_s,iatel_e,
>      &    nint_gr_el,ielstart,ielend,
>      &    cp(1,1),cp(1,1),abs_itel,abs_itel,3,
>      &    newcontlistppi,newcontlistppj,
>      &    ppseplisti,ppseplistj,
>      &    gridcellsc,dgridrangesc,griddatasc,0,
>      &    pp_threads,pp_range,r_cut_int,r_buff_list)
> !       call dump_interactions('IELE  ',pp_range,
> !    &    newcontlistppi,newcontlistppj,
> !    &    ppseplisti,ppseplistj,pp_threads)
192,235c260
< #ifdef _OPENMP
<       max_threads=omp_get_max_threads()
< #else
<       max_threads=1
< #endif
<       scp_threads=max_threads
<       call split_list_for_threads(scp_from_ik,scp_to_ik,
<      &                            scp_first_blk,scp_last_blk,
<      &                            scp_first_j,scp_last_j,
<      &                            max_fg_threads,scp_threads,
<      &                            g_listscp_start,g_listscp_end,
<      &                            newcontlistscpi,10*maxres,
<      &                            newcontlistscpj,maxint_res*maxres,
<      &                            0,scp_limit_threads)
< 
<       scsc_threads=max_threads
<       call split_list_for_threads(scsc_from_ik,scsc_to_ik,
<      &                            scsc_first_blk,scsc_last_blk,
<      &                            scsc_first_j,scsc_last_j,
<      &                            max_fg_threads,scsc_threads,
<      &                            g_listscsc_start,g_listscsc_end,
<      &                            newcontlisti,10*maxres,
<      &                            newcontlistj,maxint_res*maxres,
<      &                            0,scsc_limit_threads)
< !    &                            2*max_threads,scsc_limit_threads)
< 
<       pp_threads=max_threads
<       call split_list_for_threads(pp_from_ik,pp_to_ik,
<      &                            pp_first_blk,pp_last_blk,
<      &                            pp_first_j,pp_last_j,
<      &                            max_fg_threads,pp_threads,
<      &                            g_listpp_start,g_listpp_end,
<      &                            newcontlistppi,10*maxres,
<      &                            newcontlistppj,maxint_res*maxres,
<      &                            0,pp_limit_threads)
< !    &                            4*max_threads,pp_limit_threads)
< 
< 
< #ifdef TIMING_ENE
< #ifdef MPI
<         time_list=time_list+MPI_Wtime()-time01
< #else
<         time_list=tcpu()-time01
< #endif
---
>         RECORD_TIME_01(time_list)
237d261
< #endif
241,247c265,266
< #ifdef TIMING
< #ifdef MPI
<       time00=MPI_Wtime()
< #else
<       time00=tcpu()
< #endif
< #endif
---
> 
>       START_TIME_00
266,268c285
<       call calculate_lipid_layers
<       !call update_lipaq
< 
---
> C important: egb must be calculated before eelec and escp
281,282c298,300
<   104 call egb(evdw)
< C      print *,"bylem w egb"
---
>   104 continue
>       call egb(evdw)
> c      print *,"bylem w egb"
331a350,357
>       START_TIME_01
>       ess=0.0d0
>       if (.not.dyn_ss) then
>         call ess_fixed(ess)
>       endif
>       call ess_dyn(ess)
>       !call ess_dyn(evdw)
>       RECORD_TIME_01(time_ess)
334,340c360
< #ifdef TIMING
< #ifdef MPI
<       time01=MPI_Wtime()
< #else
<       time01=tcpu()
< #endif
< #endif
---
>       START_TIME_01
342,350c362,364
< #ifdef TIMING
< #ifdef MPI
<       time_loc=time_loc+MPI_Wtime()-time01
<       time01=MPI_Wtime()
< #else
<       time_loc=time_loc+tcpu()-time01
<       time01=tcpu()
< #endif
< #endif
---
>       RECORD_TIME_01(time_loc)
> 
>       START_TIME_01
352,365c366,368
< #ifdef TIMING
< #ifdef MPI
<       time_vec=time_vec+MPI_Wtime()-time01
< #else
<       time_vec=time_vec+tcpu()-time01
< #endif
< #endif
< #ifdef TIMING_ENE
< #ifdef MPI
<       time01=MPI_Wtime()
< #else
<       time01=tcpu()
< #endif
< #endif
---
>       RECORD_TIME_01(time_vec)
> 
>       START_TIME_01
380,390c383
< #ifdef SPLITELE
<          if (welec.gt.0d0.or.wvdwpp.gt.0d0.or.wel_loc.gt.0d0.or.
<      &       wturn3.gt.0d0.or.wturn4.gt.0d0 .or. wcorr.gt.0.0d0
<      &       .or. wcorr4.gt.0.0d0 .or. wcorr5.gt.0.d0
<      &       .or. wcorr6.gt.0.0d0 .or. wturn6.gt.0.0d0 ) then
< #else
<          if (welec.gt.0d0.or.wel_loc.gt.0d0.or.
<      &       wturn3.gt.0d0.or.wturn4.gt.0d0 .or. wcorr.gt.0.0d0
<      &       .or. wcorr4.gt.0.0d0 .or. wcorr5.gt.0.d0
<      &       .or. wcorr6.gt.0.0d0 .or. wturn6.gt.0.0d0 ) then
< #endif
---
>          if (calc_eelec) then
400c393
<         write (iout,*) "Soft-spheer ELEC potential"
---
>         write (iout,*) "Soft-sphere ELEC potential"
404,410c397,398
< #ifdef TIMING_ENE
< #ifdef MPI
<       time_eelec=time_eelec+MPI_Wtime()-time01
< #else
<       time_eelec=time_eelec+tcpu()-time01
< #endif
< #endif
---
>       RECORD_TIME_01(time_eelec)
> 
419,425c407,408
< #ifdef TIMING_ENE
< #ifdef MPI
<       time01=MPI_Wtime()
< #else
<       time01=tcpu()
< #endif
< #endif
---
>       START_TIME_01
> 
427c410
<        if(wscp.gt.0d0) then
---
>        if(calc_escp) then
430,431c413,414
<         evdw2=0
<         evdw2_14=0
---
>         evdw2=0.0d0
>         evdw2_14=0.0d0
434a418
>         ! TODO: escp_soft_sphere is BROKEN
437,443c421
< #ifdef TIMING_ENE
< #ifdef MPI
<       time_escp=time_escp+MPI_Wtime()-time01
< #else
<       time_escp=time_escp+tcpu()-time01
< #endif
< #endif
---
>       RECORD_TIME_01(time_escp)
452a431
>       START_TIME_01
453a433
>       RECORD_TIME_01(time_edis)
454a435
> 
476a458
> 
495c477,479
<       if (ndih_constr.gt.0) call etor_constr(edihcnstr)
---
>       if (ndih_constr.gt.0) then
>         call etor_constr(edihcnstr)
>       endif
579,585c563,568
< #ifdef TIMING
< #ifdef MPI
<       time_enecalc=time_enecalc+MPI_Wtime()-time00
< #else
<       time_enecalc=time_enecalc+tcpu()-time00
< #endif
< #endif
---
>       START_TIME_01
>       call omp_sum_gradients
>       RECORD_TIME_01(time_omp_sum)
> 
>       RECORD_TIME_00(time_enecalc)
> 
587,593c570,571
< #ifdef TIMING
< #ifdef MPI
<       time00=MPI_Wtime()
< #else
<       time00=tcpu()
< #endif
< #endif
---
> 
>       START_TIME_00
637a616
>       energia(32)=ess
646,652c625,626
< #ifdef TIMING
< #ifdef MPI
<       time_sumene=time_sumene+MPI_Wtime()-time00
< #else
<       time_sumene=time_sumene+tcpu()-time00
< #endif
< #endif
---
>       RECORD_TIME_00(time_sumene)
> 
659a634,665
> #ifdef ENELOG
> #ifdef MPI
>       if (fg_rank.eq.0) then
> #endif
>       write(*,'(a,2f18.12)')'evdw        =',energia(1),evdw
>       write(*,'(a,2f18.12)')'evdw2       =',energia(2),evdw2
>       write(*,'(a,2f18.12)')'evdw2_14    =',energia(18),evdw2_14
>       write(*,'(a,2f18.12)')'ees         =',energia(3),ees
>       write(*,'(a,2f18.12)')'evdw1       =',energia(16),evdw1
>       write(*,'(a,2f18.12)')'eel_loc     =',energia(7),eel_loc
>       write(*,'(a,2f18.12)')'eello_turn3 =',energia(8),eello_turn3
>       write(*,'(a,2f18.12)')'eello_turn4 =',energia(9),eello_turn4
>       write(*,'(a,2f18.12)')'ebe         =',energia(11),ebe
>       write(*,'(a,2f18.12)')'escloc      =',energia(12),escloc
>       write(*,'(a,2f18.12)')'etors       =',energia(13),etors
>       write(*,'(a,2f18.12)')'estr        =',energia(17),estr
>       write(*,'(a,2f18.12)')'ecorr       =',energia(4),ecorr
>       write(*,'(a,2f18.12)')'ehomolconstr=',energia(27),ehomology_constr
>       write(*,'(a,2f18.12)')'eafmforce   =',energia(23),Eafmforce
>       write(*,'(a,2f18.12)')'ethetacnstr =',energia(24),ethetacnstr
>       write(*,'(a,2f18.12)')'edihcnstr   =',energia(19),edihcnstr
>       write(*,'(a,2f18.12)')'eliptran    =',energia(22),eliptran
>       write(*,'(a,2f18.12)')'ehpb        =',energia(15),ehpb
>       write(*,'(a,2f18.12)')'ess         =',energia(32),ess
>       write(*,'(a,2f18.12)')'Esaxs_constr=',energia(26),Esaxs_constr
>       write(*,'(a,2f18.12)')'ETOT        =',energia(0)
> #ifdef MPI
>       endif
> #endif
> #endif
> 
> !#endif ! #ifdef WITH_CUDA
694c700
<      & esaxs_constr,ehomology_constr,edfator,edfanei,edfabet
---
>      & esaxs_constr,ehomology_constr,edfator,edfanei,edfabet,ess
760a767
>       ess=energia(32)
770c777
<      & +wdfa_beta*edfabet
---
>      & +wdfa_beta*edfabet+ess
781c788
<      & +wdfa_beta*edfabet
---
>      & +wdfa_beta*edfabet+ess
806,1561d812
<       subroutine fmadd1(cnt,trg,a,src)
<       implicit none
<       integer i,cnt
<       double precision a
<       double precision trg(1:cnt)
<       double precision src(1:cnt)
<       if(a.ne.0.0d0) then
<         do i=1,cnt
<           trg(i)=trg(i)+a*src(i)
<         enddo
<       endif
<       return
<       end
< c-------------------------------------------------------------------------------
<       subroutine fmadd3(cnt,trg,a,src)
<       implicit none
<       include 'DIMENSIONS'
<       include 'COMMON.DERIV'
<       integer cnt
<       double precision a
<       double precision trg(3,1:cnt)
<       double precision src(3,1:cnt)
<       call fmadd1(cnt*3,trg(1,1),a,src(1,1))
<       return
<       end
< c-------------------------------------------------------------------------------
<       subroutine fmadd1pair(cnt,trg,a,src1,src2)
<       implicit none
<       integer i,cnt
<       double precision a
<       double precision trg(1:cnt)
<       double precision src1(1:cnt)
<       double precision src2(1:cnt)
<       if(a.ne.0.0d0) then
<         do i=1,cnt
<           trg(i)=trg(i)+a*(src1(i)+src2(i))
<         enddo
<       endif
<       return
<       end
< c-------------------------------------------------------------------------------
<       subroutine fmadd3pair(cnt,trg,a,src1,src2)
<       implicit none
<       include 'DIMENSIONS'
<       include 'COMMON.DERIV'
<       integer cnt
<       double precision a
<       double precision trg(3,1:cnt)
<       double precision src1(3,1:cnt)
<       double precision src2(3,1:cnt)
<       call fmadd1pair(cnt*3,trg(1,1),a,src1(1,1),src2(1,1))
<       return
<       end
< c-------------------------------------------------------------------------------
<       subroutine sum_gradient
< #ifdef _OPENMP
<       use omp_lib
< #endif
<       implicit none
<       include 'DIMENSIONS'
< #ifndef ISNAN
<       external proc_proc
< #ifdef WINPGI
< cMS$ATTRIBUTES C ::  proc_proc
< #endif
< #endif
< #ifdef MPI
<       include 'mpif.h'
<       integer ierror,ierr
< #else
<       double precision tcpu
< #endif
<       double precision time00,time01
<       double precision gradbufc(3,-1:maxres),gradbufx(3,-1:maxres),
<      &                 glocbuf(4*maxres),gradbufc_sum(3,-1:maxres),
<      &                 gloc_scbuf(3,-1:maxres)
<       include 'COMMON.SETUP'
<       include 'COMMON.IOUNITS'
<       include 'COMMON.FFIELD'
<       include 'COMMON.DERIV'
<       include 'COMMON.INTERACT'
<       include 'COMMON.SBRIDGE'
<       include 'COMMON.CHAIN'
<       include 'COMMON.VAR'
<       include 'COMMON.CONTROL'
<       include 'COMMON.TIME1'
<       include 'COMMON.MAXGRAD'
<       include 'COMMON.SCCOR'
< c      include 'COMMON.MD'
<       include 'COMMON.QRESTR'
<       !include 'COMMON.LOCAL'
<       integer i,j,k
<       integer range_lo,range_hi,from,from2,to,my_thread,threads_used
<       double precision scalar
<       double precision gvdwc_norm,gvdwc_scp_norm,gelc_norm,gvdwpp_norm,
<      &gradb_norm,ghpbc_norm,gradcorr_norm,gel_loc_norm,gcorr3_turn_norm,
<      &gcorr4_turn_norm,gradcorr5_norm,gradcorr6_norm,
<      &gcorr6_turn_norm,gsccorrc_norm,gscloc_norm,gvdwx_norm,
<      &gradx_scp_norm,ghpbx_norm,gradxorr_norm,gsccorrx_norm,
<      &gsclocx_norm
<       common /sumgradientcommon/ gradbufc,gradbufx,glocbuf,
<      &                           gradbufc_sum,gloc_scbuf
< #ifdef TIMING
< #ifdef MPI
<       time01=MPI_Wtime()
< #else
<       time01=tcpu()
< #endif
< #endif
<       gradbufc(:,nct+1:nres)=0.0d0
<       gradbufx(:,nct+1:nres)=0.0d0
<       gradbufc_sum(:,nct+1:nres)=0.0d0
<       gloc_scbuf(:,nct+1:nres)=0.0d0
< #ifdef DEBUG
<       write (iout,*) "sum_gradient gvdwc, gvdwx"
<       do i=1,nres
<         write (iout,'(i3,3f10.5,5x,3f10.5,5x,f10.5)')
<      &   i,(gvdwx(j,i),j=1,3),(gvdwc(j,i),j=1,3)
<       enddo
<       call flush(iout)
<       write (iout,*) "sum_gradient gelc, gelc_long"
<       do i=1,nres
<         write (iout,'(i3,3f10.5,5x,3f10.5,5x,f10.5)') 
<      &   i,(gelc(j,i),j=1,3),(gelc_long(j,i),j=1,3)
<       enddo
<       call flush(iout)
<       write (iout,*) "sum_gradient gel_loc, gel_loc_long"
<       do i=1,nres
<         write (iout,'(i3,3f10.5,5x,3f10.5,5x,f10.5)') 
<      &   i,(gel_loc(j,i),j=1,3),(gel_loc_long(j,i),j=1,3)
<       enddo
<       call flush(iout)
<       write (iout,*) "sum_gradient gvdwpp"
<       do i=1,nres
<         write (iout,'(i3,3f10.5,5x,3f10.5,5x,f10.5)') 
<      &   i,(gvdwpp(j,i),j=1,3)
<       enddo
<       call flush(iout)
< #endif
< #ifdef DEBUG
<       write (iout,*) "sum_gradient gsaxsc, gsaxsx"
<       do i=0,nres
<         write (iout,'(i3,3e15.5,5x,3e15.5)')
<      &        i,(gsaxsc(j,i),j=1,3),(gsaxsx(j,i),j=1,3)
<       enddo
<       call flush(iout)
< #endif
< 
<       !range_lo=0
<       !range_hi=nct
< #ifdef _OPENMP
<       threads_used=omp_get_max_threads()
< #else
<       threads_used=1
< #endif
< c      if (threads_used.eq.0)print *,"1 MyRank threads_used",threads_used
< 
< #ifdef MPI
< C FG slaves call the following matching MPI_Bcast in ERGASTULUM
<       if (nfgtasks.gt.1 .and. fg_rank.eq.0)
<      &  call MPI_Bcast(1,1,MPI_INTEGER,king,FG_COMM,IERROR)
< #endif
< C
< C 9/29/08 AL Transform parts of gradients in site coordinates to the gradient
< C            in virtual-bond-vector coordinates
< C
< #ifdef DEBUG
< c      write (iout,*) "gel_loc gel_loc_long and gel_loc_loc"
< c      do i=1,nres-1
< c        write (iout,'(i5,3f10.5,2x,3f10.5,2x,f10.5)')
< c     &   i,(gel_loc(j,i),j=1,3),(gel_loc_long(j,i),j=1,3),gel_loc_loc(i)
< c      enddo
< c      write (iout,*) "gel_loc_tur3 gel_loc_turn4"
< c      do i=1,nres-1
< c        write (iout,'(i5,3f10.5,2x,f10.5)')
< c     &  i,(gcorr4_turn(j,i),j=1,3),gel_loc_turn4(i)
< c      enddo
<       write (iout,*) "gradcorr5 gradcorr5_long gradcorr5_loc"
<       do i=1,nres
<         write (iout,'(i3,3f10.5,5x,3f10.5,5x,f10.5)')
<      &   i,(gradcorr5(j,i),j=1,3),(gradcorr5_long(j,i),j=1,3),
<      &   g_corr5_loc(i)
<       enddo
<       call flush(iout)
< #endif
< #ifdef DEBUG
<       write (iout,*) "gsaxsc"
<       do i=1,nres
<         write (iout,'(i3,3f10.5)') i,(wsaxs*gsaxsc(j,i),j=1,3)
<       enddo
<       call flush(iout)
< #endif
< 
< !$OMP PARALLEL NUM_THREADS(threads_used) DEFAULT(SHARED)
< !$OMP& PRIVATE(k,from,from2,to,my_thread)
< #ifdef _OPENMP
<       my_thread=omp_get_thread_num()+1
< #else
<       my_thread=1
< #endif
< #ifdef GRAD_NAN_CHECK
<       do i=1,nres
<       if (isnan(gvdwc(1,i))) write(*,*) MyRank," NaN in gvdwc",i
<       if (isnan(gvdwc_scpp(1,i)))write(*,*)MyRank," NaN in gvdwc_scpp",i
<       if (isnan(gelc_long(1,i))) write(*,*) MyRank," NaN in gelc_long",i
<       if (isnan(gvdwpp(1,i))) write(*,*) MyRank," NaN in gvdwpp",i
<       if (isnan(gradb(1,i))) write(*,*) MyRank," NaN in gradb",i
<       if (isnan(gel_loc_long(1,i))) write(*,*) 
<      & MyRank," NaN in gel_loc_long",i
<       if (isnan(gradcorr_long(1,i)))write(*,*)
<      & MyRank,"NaN in gradcorr_long",i
<       if (isnan(gradcorr_long(1,i)))write(*,*)
<      & MyRank," NaN in gradcorr_long",i
<       if (isnan(gradcorr5_long(1,i)))write(*,*)
<      & MyRank," NaN in gradcorr5_long",i
<       if (isnan(gradcorr6_long(1,i)))write(*,*)
<      & MyRank," NaN in gradcorr6_long",i
<       if (isnan(gcorr6_turn_long(1,i)))write(*,*)
<      & MyRank," NaN in gcorr6_turn_long",i
<       if (isnan(ghpbc(1,2))) write(*,*)MyRank," NaN in ghpbc",i
<       if (isnan(gliptranc(1,i)))write(*,*)
<      & MyRank," NaN in gliptranc",i
<       if (isnan(gradafm(1,i)))write(*,*) MyRank," NaN in gradafm",i
<       if (isnan(gg_tube(1,i)))write(*,*) MyRank," NaN in gg_tube",i
<       if (isnan(gsaxsc(1,i)))write(*,*) MyRank," NaN in gsaxsc",i
<       if (isnan(gdfad(1,i)))write(*,*) MyRank," NaN in gdfad",i
<       if (isnan(gdfat(1,i)))write(*,*) MyRank," NaN in gdfat",i
<       if (isnan(gdfan(1,i)))write(*,*) MyRank," NaN in gdfan",i
<       if (isnan(gdfab(1,i)))write(*,*) MyRank," NaN in gdfab",i
<       enddo
< #endif
<       !from=range_lo+((range_hi-range_lo)/threads_used)*(my_thread-1)
<       !to=range_lo+((range_hi-range_lo)/threads_used)*my_thread-1
<       !if(my_thread.eq.threads_used) to=range_hi
<       call split_work_for_threads(from,to,0,nct,my_thread,threads_used)
< c      if (to.eq.0 .or. to.gt.maxres)print *,"31 MyRank",MyRank," to",to
< 
<       k=to-from+1
< c      if (k.eq.0)print *,"31 MyRank",MyRank," k",k
< 
<       gradbufc(:,from:to)=0.0d0
<       call fmadd3(k,gradbufc(1,from),wsc,gvdwc(1,from))
<       call fmadd3pair(k,gradbufc(1,from),wscp,
<      &                gvdwc_scp(1,from),gvdwc_scpp(1,from))
<       call fmadd3(k,gradbufc(1,from),welec,gelc_long(1,from))
< #ifdef SPLITELE
<       call fmadd3(k,gradbufc(1,from),wvdwpp,gvdwpp(1,from))
< #else
<       call fmadd3(k,gradbufc(1,from),wbond,gradb(1,from))
< #endif
<       call fmadd3(k,gradbufc(1,from),wel_loc,gel_loc_long(1,from))
<       call fmadd3(k,gradbufc(1,from),wcorr,gradcorr_long(1,from))
<       call fmadd3(k,gradbufc(1,from),wcorr5,gradcorr5_long(1,from))
<       call fmadd3(k,gradbufc(1,from),wcorr6,gradcorr6_long(1,from))
<       call fmadd3(k,gradbufc(1,from),wturn6,gcorr6_turn_long(1,from))
<       call fmadd3(k,gradbufc(1,from),wstrain,ghpbc(1,from))
<       call fmadd3(k,gradbufc(1,from),wliptran,gliptranc(1,from))
<       call fmadd3(k,gradbufc(1,from),1.0d0,gradafm(1,from))
< #ifdef SHIELD
<       call fmadd3(k,gradbufc(1,from),welec,gshieldc(1,from))
<       call fmadd3(k,gradbufc(1,from),wcorr,gshieldc_ec(1,from))
< #ifdef SPLITELE
<       call fmadd3(k,gradbufc(1,from),wturn3,gshieldc_t3(1,from))
< #endif
<       call fmadd3(k,gradbufc(1,from),wturn4,gshieldc_t4(1,from))
<       call fmadd3(k,gradbufc(1,from),wel_loc,gshieldc_ll(1,from))
< #endif
<       call fmadd3(k,gradbufc(1,from),wtube,gg_tube(1,from))
<       call fmadd3(k,gradbufc(1,from),wsaxs,gsaxsc(1,from))
< c      from2=max(from2,1)
<       from2=max(from,1)
<       k=to-from2+1
<       call fmadd3(k,gradbufc(1,from2),wdfa_dist,gdfad(1,from2))
<       call fmadd3(k,gradbufc(1,from2),wdfa_tor,gdfat(1,from2))
<       call fmadd3(k,gradbufc(1,from2),wdfa_nei,gdfan(1,from2))
<       call fmadd3(k,gradbufc(1,from2),wdfa_beta,gdfab(1,from2))
< #ifdef GRAD_NAN_CHECK
<       do i=from2,to
<       if (isnan(gradbufc(1,i))) write(*,*)MyRank," NaNs in gradbufc-1",i
<       enddo
< #endif
<       gradbufc_sum(:,from:to)=gradbufc(:,from:to)
< !$OMP END PARALLEL
<       do i=nct+1,nres
<         gradbufc_sum(:,i)=gradbufc(:,i)
<         gradbufc(:,i)=0.0d0
<       enddo
< c      if (k.le.0 .or. to.le.0 .or. from2.eq.0) 
< c     &  print *,"314 MyRank",MyRank," k",k," from",from,
< c     & " from2",from2," to",to
< 
< #ifdef DEBUG
<       write (iout,*) "gradc from gradbufc"
<       do i=1,nres
<         write (iout,'(i3,3f10.5)') i,(gradc(j,i,icg),j=1,3)
<       enddo
<       call flush(iout)
< #endif
< !!#ifdef MPI
< !!      if (nfgtasks.gt.1) then
< !!        time00=MPI_Wtime()
< !!#ifdef DEBUG
< !!        write (iout,*) "gradbufc before allreduce"
< !!        do i=1,nres
< !!          write (iout,'(i3,3f10.5)') i,(gradbufc(j,i),j=1,3)
< !!        enddo
< !!        call flush(iout)
< !!#endif
< !!      do i=0,nres
< !!        do j=1,3
< !!          gradbufc_sum(j,i)=gradbufc(j,i)
< !!        enddo
< !!      enddo
< !!c      call MPI_AllReduce(gradbufc(1,1),gradbufc_sum(1,1),3*nres,
< !!c     &    MPI_DOUBLE_PRECISION,MPI_SUM,FG_COMM,IERR)
< !!c      time_reduce=time_reduce+MPI_Wtime()-time00
< !!#ifdef DEBUG
< !!c       write (iout,*) "gradbufc_sum after allreduce"
< !!c       do i=1,nres
< !!c         write (iout,'(i3,3f10.5)') i,(gradbufc_sum(j,i),j=1,3)
< !!c       enddo
< !!c       call flush(iout)
< !!#endif
< !!#ifdef TIMING
< !!c       time_allreduce=time_allreduce+MPI_Wtime()-time00
< !!#endif
< !!c      do i=nnt,nres
< !!      do i=0,nres
< !!        do k=1,3
< !!          gradbufc(k,i)=0.0d0
< !!        enddo
< !!      enddo
< !!c#ifdef DEBUG
< !!c       write (iout,*) "igrad_start",igrad_start," igrad_end",igrad_end
< !!c       write (iout,*) (i," jgrad_start",jgrad_start(i),
< !!c     &                  " jgrad_end  ",jgrad_end(i),
< !!c     &                  i=igrad_start,igrad_end)
< !!c#endif
< !!c
< !!c Obsolete and inefficient code; we can make the effort O(n) and, therefore,
< !!c do not parallelize this part.
< !!c
< !!c      do i=igrad_start,igrad_end
< !!c        do j=jgrad_start(i),jgrad_end(i)
< !!c          do k=1,3
< !!c            gradbufc(k,i)=gradbufc(k,i)+gradbufc_sum(k,j)
< !!c          enddo
< !!c        enddo
< !!c      enddo
< !!      do j=1,3
< !!        gradbufc(j,nres-1)=gradbufc_sum(j,nres)
< !!      enddo
< !!c      do i=nres-2,-1,-1
< !!      do i=nres-2,0,-1
< !!        do j=1,3
< !!          gradbufc(j,i)=gradbufc(j,i+1)+gradbufc_sum(j,i+1)
< !!        enddo
< !!      enddo
< !!      else
< !!#endif
< #ifdef DEBUG
<         write (iout,*) "gradbufc"
<         do i=0,nres
<           write (iout,'(i3,3f10.5)') i,(gradbufc(j,i),j=1,3)
<         enddo
<         call flush(iout)
< #endif
< c      do i=-1,nres
<       !do i=0,nres   !moved to previous OMP block
<       !  do j=1,3
<       !    gradbufc_sum(j,i)=gradbufc(j,i)
<       !    gradbufc(j,i)=0.0d0
<       !  enddo
<       !enddo
< c      if (k.le.0) print *,"410 MyRank",MyRank," k",k," from",from,
< c     & " from2",from2," to",to
<       gradbufc(:,nres)=0.0d0
<       gradbufc(:,nres-1)=gradbufc_sum(:,nres)
< c      do i=nres-2,-1,-1
<       do i=nres-2,0,-1
<         gradbufc(:,i)=gradbufc(:,i+1)+gradbufc_sum(:,i+1)
<       enddo
< c      if (k.le.0) print *,"41 MyRank",MyRank," k",k," from",from,
< c     & " from2",from2," to",to
< c      if (k.le.0)print *,"41 MyRank",MyRank," k",k
< #ifdef GRAD_NAN_CHECK
<       do i=1,2*nres
<         if (isnan(gradbufc(1,i))) then
<           write (*,*) MyRank," NaNs in gradbufc 0"
<           write (*,*) MyRank," gradbufc_sum"
<           print '(i5,3f10.5)',i,(gradbufc_sum(j,i),j=1,3)
<         endif
<       enddo
< #endif
< !       call suffix_sum3(gradbufc(1,0),gradbufc_sum(1,0),0,nres-2,
< !    &                   omp_get_max_threads())
< c      do i=nnt,nres-1
< c        do k=1,3
< c          gradbufc(k,i)=0.0d0
< c        enddo
< c        do j=i+1,nres
< c          do k=1,3
< c            gradbufc(k,i)=gradbufc(k,i)+gradbufc(k,j)
< c          enddo
< c        enddo
< c      enddo
< !!#ifdef MPI
< !!      endif
< !!#endif
< #ifdef DEBUG
<       write (iout,*) "gradbufc after summing"
<       do i=1,nres
<         write (iout,'(i3,3f10.5)') i,(gradbufc(j,i),j=1,3)
<       enddo
<       call flush(iout)
< #endif
<       gradbufc(:,nres)=0.0d0
<       gradc(:,-1,icg)=0.0d0
<       gradc(:,nct+1:nres+1,icg)=0.0d0
<       gradx(:,-1,icg)=0.0d0
<       gradx(:,nct+1:nres+1,icg)=0.0d0
< !$OMP PARALLEL NUM_THREADS(threads_used) DEFAULT(SHARED)
< !$OMP& PRIVATE(k,from,from2,to,my_thread)
< #ifdef _OPENMP
<       my_thread=omp_get_thread_num()+1
< #else
<       my_thread=1
< #endif
<       !from=range_lo+((range_hi-range_lo)/threads_used)*(my_thread-1)
<       !to=range_lo+((range_hi-range_lo)/threads_used)*my_thread-1
<       !if(my_thread.eq.threads_used) to=range_hi
<       call split_work_for_threads(from,to,0,nct,my_thread,threads_used)
<       k=to-from+1
< 
<       gradc(:,from:to,icg)=gradbufc(:,from:to)
<       gradbufc(:,from:to)=0.0d0
<       call fmadd3(k,gradc(1,from,icg),wang,glocangdc(1,from))
<       call fmadd3(k,gradc(1,from,icg),wtor,gloctordc(1,from))
<       call fmadd3(k,gradc(1,from,icg),welec,gelc(1,from))
<       call fmadd3(k,gradc(1,from,icg),wel_loc,gel_loc(1,from))
< !      call fmadd3(k,gradc(1,from,icg),0.5d0*wscp,gvdwc_scpp(1,from))
<       call fmadd3(k,gradbufc(1,from),wscp,gvdwc_scpp(1,from))
< !      call fmadd3(k,gradc(1,from,icg),0.5d0*welec,gelc_long(1,from))
<       call fmadd3(k,gradbufc(1,from),welec,gelc_long(1,from))
< #ifdef SPLITELE
< !      call fmadd3(k,gradc(1,from,icg),0.5d0*wvdwpp,gvdwpp(1,from))
<       call fmadd3(k,gradbufc(1,from),wvdwpp,gvdwpp(1,from))
< #endif
< !      call fmadd3(k,gradc(1,from,icg),0.5d0*wel_loc,
< !     &            gel_loc_long(1,from))
<       call fmadd3(k,gradbufc(1,from),wel_loc,gel_loc_long(1,from))
< #ifdef SPLITELE
< !      call fmadd3(k,gradc(1,from,icg),0.5d0*wcorr,
< !     &            gradcorr_long(1,from))
<       call fmadd3(k,gradbufc(1,from),wcorr,gradcorr_long(1,from))
< #else
< !      call fmadd3(k,gradc(1,from,icg),0.5d0*wcorr,gcorr_long(1,from))
<       call fmadd3(k,gradbufc(1,from),wcorr,gcorr_long(1,from))
< #endif
< !      call fmadd3(k,gradc(1,from,icg),0.5d0*wcorr5,
< !     &            gradcorr5_long(1,from))
<       call fmadd3(k,gradbufc(1,from),wcorr5,gradcorr5_long(1,from))
< !      call fmadd3(k,gradc(1,from,icg),0.5d0*wcorr6,
< !     &            gradcorr6_long(1,from))
<       call fmadd3(k,gradbufc(1,from),wcorr6,gradcorr6_long(1,from))
< !      call fmadd3(k,gradc(1,from,icg),0.5d0*wturn6,
< !     &            gcorr6_turn_long(1,from))
<       call fmadd3(k,gradbufc(1,from),wturn6,gcorr6_turn(1,from))
< 
<       call fmadd3(k,gradc(1,from,icg),0.5d0,gradbufc(1,from))
< 
<       call fmadd3(k,gradc(1,from,icg),wbond,gradb(1,from))
<       call fmadd3(k,gradc(1,from,icg),wcorr,gradcorr(1,from))
<       call fmadd3(k,gradc(1,from,icg),wturn3,gcorr3_turn(1,from))
<       call fmadd3(k,gradc(1,from,icg),wturn4,gcorr4_turn(1,from))
<       call fmadd3(k,gradc(1,from,icg),wcorr5,gradcorr5(1,from))
<       call fmadd3(k,gradc(1,from,icg),wcorr6,gradcorr6(1,from))
<       call fmadd3(k,gradc(1,from,icg),wturn6,gcorr6_turn(1,from))
<       call fmadd3(k,gradc(1,from,icg),wsccor,gsccorc(1,from))
<       call fmadd3(k,gradc(1,from,icg),wscloc,gscloc(1,from))
<       call fmadd3(k,gradc(1,from,icg),wstrain,ghpbdc(1,from))
<       call fmadd3(k,gradc(1,from,icg),wliptran,gliptranc(1,from))
<       call fmadd3(k,gradc(1,from,icg),1.0d0,gradafm(1,from))
< #ifdef SHIELD
<       call fmadd3(k,gradc(1,from,icg),welec,gshieldc(1,from))
<       call fmadd3(k,gradc(1,from,icg),welec,gshieldc_loc(1,from))
<       call fmadd3(k,gradc(1,from,icg),wcorr,gshieldc_ec(1,from))
<       call fmadd3(k,gradc(1,from,icg),wcorr,gshieldc_loc_ec(1,from))
<       call fmadd3(k,gradc(1,from,icg),wturn3,gshieldc_t3(1,from))
<       call fmadd3(k,gradc(1,from,icg),wturn3,gshieldc_loc_t3(1,from))
<       call fmadd3(k,gradc(1,from,icg),wturn4,gshieldc_t4(1,from))
<       call fmadd3(k,gradc(1,from,icg),wturn4,gshieldc_loc_t4(1,from))
<       call fmadd3(k,gradc(1,from,icg),wel_loc,gshieldc_ll(1,from))
<       call fmadd3(k,gradc(1,from,icg),wel_loc,gshieldc_loc_ll(1,from))
< #endif
<       call fmadd3(k,gradc(1,from,icg),wtube,gg_tube(1,from))
< 
<       gradx(:,from:to,icg)=0.0d0
<       call fmadd3(k,gradx(1,from,icg),wsc,gvdwx(1,from))
<       call fmadd3(k,gradx(1,from,icg),wscp,gradx_scp(1,from))
<       call fmadd3(k,gradx(1,from,icg),wbond,gradbx(1,from))
<       call fmadd3(k,gradx(1,from,icg),wstrain,ghpbx(1,from))
<       call fmadd3(k,gradx(1,from,icg),wcorr,gradxorr(1,from))
<       call fmadd3(k,gradx(1,from,icg),wsccor,gsccorx(1,from))
<       call fmadd3(k,gradx(1,from,icg),wscloc,gsclocx(1,from))
<       call fmadd3(k,gradx(1,from,icg),wliptran,gliptranx(1,from))
< #ifdef SHIELD
<       call fmadd3(k,gradx(1,from,icg),welec,gshieldx(1,from))
<       call fmadd3(k,gradx(1,from,icg),wcorr,gshieldx_ec(1,from))
<       call fmadd3(k,gradx(1,from,icg),wturn3,gshieldx_t3(1,from))
<       call fmadd3(k,gradx(1,from,icg),wturn4,gshieldx_t4(1,from))
<       call fmadd3(k,gradx(1,from,icg),wel_loc,gshieldx_ll(1,from))
< #endif
<       call fmadd3(k,gradx(1,from,icg),wtube,gg_tube_sc(1,from))
<       call fmadd3(k,gradx(1,from,icg),wsaxs,gsaxsx(1,from))
< !$OMP END PARALLEL
< 
<       if (constr_homology.gt.0) then
< !$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(j)
<         do i=1,nct
<           do j=1,3
<             gradc(j,i,icg)=gradc(j,i,icg)+duscdiff(j,i)
<             gradx(j,i,icg)=gradx(j,i,icg)+duscdiffx(j,i)
<           enddo
<         enddo
<       endif
< #ifdef DEBUG
<       write (iout,*) "gradc gradx gloc after adding"
<       write (iout,'(i5,3f10.5,5x,3f10.5,5x,f10.5)')
<      &   i,(gradc(j,0,icg),j=1,3),(gradx(j,0,icg),j=1,3)
<       do i=1,nres
<         write (iout,'(i5,3f10.5,5x,3f10.5,5x,f10.5)')
<      &   i,(gradc(j,i,icg),j=1,3),(gradx(j,i,icg),j=1,3),gloc(i,icg)
<       enddo
<       write (iout,*) "gloc before adding corr"
<       do i=1,4*nres
<         write (iout,*) i,gloc(i,icg)
<       enddo
< #endif
< c
< c      range_lo=1
< c      range_hi=nres-3
< c!$OMP PARALLEL NUM_THREADS(threads_used) DEFAULT(SHARED)
< c!$OMP& PRIVATE(from,to,my_thread)
< c AL 7/23/22 NMR anguular contribution
< c#ifdef _OPENMP
< c      my_thread=omp_get_thread_num()+1
< c#else
< c      my_thread=1
< c#endif
< c      from=range_lo+((range_hi-range_lo)/threads_used)*(my_thread-1)
< c      to=range_lo+((range_hi-range_lo)/threads_used)*my_thread-1
< c      if(my_thread.eq.threads_used) to=range_hi
< c
< c      call fmadd1(to-from+1,gloc(from,icg),wcorr,gcorr_loc(from))
< c      call fmadd1(to-from+1,gloc(from,icg),wcorr5,g_corr5_loc(from))
< c      call fmadd1(to-from+1,gloc(from,icg),wcorr6,g_corr6_loc(from))
< c      call fmadd1(to-from+1,gloc(from,icg),wturn4,gel_loc_turn4(from))
< c      call fmadd1(to-from+1,gloc(from,icg),wturn3,gel_loc_turn3(from))
< c      call fmadd1(to-from+1,gloc(from,icg),wturn6,gel_loc_turn6(from))
< c      call fmadd1(to-from+1,gloc(from,icg),wel_loc,gel_loc_loc(from))
< c      call fmadd1(to-from+1,gloc(from,icg),wstrain,ghpbdphi(from))
< c!$OMP END PARALLEL
< #ifdef GRAD_NAN_CHECK
<       if (isnan(ghpbdphi(4)))  write (*,*) MyRank," NaNs in ghpbdphi"
< #endif
<       do i=1,nres-3
<          gloc(i,icg)=gloc(i,icg)+wstrain*ghpbdphi(i)
<       enddo
< c
< #ifdef DEBUG
<       write (iout,*) "gloc after adding corr"
<       do i=1,4*nres
<         write (iout,*) i,gloc(i,icg)
<       enddo
< #endif
< #ifdef MPI
<       if (nfgtasks.gt.1) then
< !$OMP PARALLEL DO DEFAULT(SHARED)
<         do i=0,nres
<           gradbufc(:,i)=gradc(:,i,icg)
<           gradbufx(:,i)=gradx(:,i,icg)
<         enddo
< !$OMP PARALLEL DO DEFAULT(SHARED)
<         do i=1,4*nres
<           glocbuf(i)=gloc(i,icg)
<         enddo
< c#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
< c#undef DEBUG
< !$OMP PARALLEL DO DEFAULT(SHARED)
<         do i=1,nres
<           gloc_scbuf(:,i)=gloc_sc(:,i,icg)
<         enddo
< 
<         time00=MPI_Wtime()
<         call MPI_Barrier(FG_COMM,IERR)
<         time_barrier_g=time_barrier_g+MPI_Wtime()-time00
<         time00=MPI_Wtime()
<         call MPI_Reduce(gradbufc(1,0),gradc(1,0,icg),3*(nres+1),
<      &    MPI_DOUBLE_PRECISION,MPI_SUM,king,FG_COMM,IERR)
<         call MPI_Reduce(gradbufx(1,0),gradx(1,0,icg),3*(nres+1),
<      &    MPI_DOUBLE_PRECISION,MPI_SUM,king,FG_COMM,IERR)
<         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
< #ifdef DEBUG
<         write (iout,*) "gradc after reduce"
<         do i=0,nres
<           do j=1,3
<             write (iout,*) i,j,gradc(j,i,icg)
<           enddo
<         enddo
<         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
<         write (iout,*) "gloc after reduce"
<         do i=1,4*nres
<           write (iout,*) i,gloc(i,icg)
<         enddo
< #endif
<       endif
< #endif
<       if (gnorm_check) then
< c
< c Compute the maximum elements of the gradient
< c
<         gvdwc_max=0.0d0
<         gvdwc_scp_max=0.0d0
<         gelc_max=0.0d0
<         gvdwpp_max=0.0d0
<         gradb_max=0.0d0
<         ghpbc_max=0.0d0
<         gradcorr_max=0.0d0
<         gel_loc_max=0.0d0
<         gcorr3_turn_max=0.0d0
<         gcorr4_turn_max=0.0d0
<         gradcorr5_max=0.0d0
<         gradcorr6_max=0.0d0
<         gcorr6_turn_max=0.0d0
<         gsccorrc_max=0.0d0
<         gscloc_max=0.0d0
<         gvdwx_max=0.0d0
<         gradx_scp_max=0.0d0
<         ghpbx_max=0.0d0
<         gradxorr_max=0.0d0
<         gsccorrx_max=0.0d0
<         gsclocx_max=0.0d0
<         do i=1,nct
<           gvdwc_norm=dsqrt(scalar(gvdwc(1,i),gvdwc(1,i)))
<           if (gvdwc_norm.gt.gvdwc_max) gvdwc_max=gvdwc_norm
<           gvdwc_scp_norm=dsqrt(scalar(gvdwc_scp(1,i),gvdwc_scp(1,i)))
<           if (gvdwc_scp_norm.gt.gvdwc_scp_max)
<      &     gvdwc_scp_max=gvdwc_scp_norm
<           gelc_norm=dsqrt(scalar(gelc(1,i),gelc(1,i)))
<           if (gelc_norm.gt.gelc_max) gelc_max=gelc_norm
<           gvdwpp_norm=dsqrt(scalar(gvdwpp(1,i),gvdwpp(1,i)))
<           if (gvdwpp_norm.gt.gvdwpp_max) gvdwpp_max=gvdwpp_norm
<           gradb_norm=dsqrt(scalar(gradb(1,i),gradb(1,i)))
<           if (gradb_norm.gt.gradb_max) gradb_max=gradb_norm
<           ghpbc_norm=dsqrt(scalar(ghpbc(1,i),ghpbc(1,i)))
<           if (ghpbc_norm.gt.ghpbc_max) ghpbc_max=ghpbc_norm
<           gradcorr_norm=dsqrt(scalar(gradcorr(1,i),gradcorr(1,i)))
<           if (gradcorr_norm.gt.gradcorr_max) gradcorr_max=gradcorr_norm
<           gel_loc_norm=dsqrt(scalar(gel_loc(1,i),gel_loc(1,i)))
<           if (gel_loc_norm.gt.gel_loc_max) gel_loc_max=gel_loc_norm
<           gcorr3_turn_norm=dsqrt(scalar(gcorr3_turn(1,i),
<      &      gcorr3_turn(1,i)))
<           if (gcorr3_turn_norm.gt.gcorr3_turn_max)
<      &      gcorr3_turn_max=gcorr3_turn_norm
<           gcorr4_turn_norm=dsqrt(scalar(gcorr4_turn(1,i),
<      &      gcorr4_turn(1,i)))
<           if (gcorr4_turn_norm.gt.gcorr4_turn_max)
<      &      gcorr4_turn_max=gcorr4_turn_norm
<           gradcorr5_norm=dsqrt(scalar(gradcorr5(1,i),gradcorr5(1,i)))
<           if (gradcorr5_norm.gt.gradcorr5_max)
<      &      gradcorr5_max=gradcorr5_norm
<           gradcorr6_norm=dsqrt(scalar(gradcorr6(1,i),gradcorr6(1,i)))
<           if (gradcorr6_norm.gt.gradcorr6_max)
<      &           gradcorr6_max=gradcorr6_norm
<           gcorr6_turn_norm=dsqrt(scalar(gcorr6_turn(1,i),
<      &      gcorr6_turn(1,i)))
<           if (gcorr6_turn_norm.gt.gcorr6_turn_max)
<      &      gcorr6_turn_max=gcorr6_turn_norm
<           gsccorrc_norm=dsqrt(scalar(gsccorc(1,i),gsccorc(1,i)))
<           if (gsccorrc_norm.gt.gsccorrc_max) gsccorrc_max=gsccorrc_norm
<           gscloc_norm=dsqrt(scalar(gscloc(1,i),gscloc(1,i)))
<           if (gscloc_norm.gt.gscloc_max) gscloc_max=gscloc_norm
<           gvdwx_norm=dsqrt(scalar(gvdwx(1,i),gvdwx(1,i)))
<           if (gvdwx_norm.gt.gvdwx_max) gvdwx_max=gvdwx_norm
<           gradx_scp_norm=dsqrt(scalar(gradx_scp(1,i),gradx_scp(1,i)))
<           if (gradx_scp_norm.gt.gradx_scp_max)
<      &      gradx_scp_max=gradx_scp_norm
<           ghpbx_norm=dsqrt(scalar(ghpbx(1,i),ghpbx(1,i)))
<           if (ghpbx_norm.gt.ghpbx_max) ghpbx_max=ghpbx_norm
<           gradxorr_norm=dsqrt(scalar(gradxorr(1,i),gradxorr(1,i)))
<           if (gradxorr_norm.gt.gradxorr_max) gradxorr_max=gradxorr_norm
<           gsccorrx_norm=dsqrt(scalar(gsccorx(1,i),gsccorx(1,i)))
<           if (gsccorrx_norm.gt.gsccorrx_max) gsccorrx_max=gsccorrx_norm
<           gsclocx_norm=dsqrt(scalar(gsclocx(1,i),gsclocx(1,i)))
<           if (gsclocx_norm.gt.gsclocx_max) gsclocx_max=gsclocx_norm
<         enddo
<         if (gradout) then
< #if (defined AIX || defined CRAY)
<           open(istat,file=statname,position="append")
< #else
<           open(istat,file=statname,access="append")
< #endif
<           write (istat,'(1h#,21f10.2)') gvdwc_max,gvdwc_scp_max,
<      &      gelc_max,gvdwpp_max,gradb_max,ghpbc_max,
<      &      gradcorr_max,gel_loc_max,gcorr3_turn_max,gcorr4_turn_max,
<      &      gradcorr5_max,gradcorr6_max,gcorr6_turn_max,gsccorrc_max,
<      &      gscloc_max,gvdwx_max,gradx_scp_max,ghpbx_max,gradxorr_max,
<      &      gsccorrx_max,gsclocx_max
<           close(istat)
<           if (gvdwc_max.gt.1.0d4) then
<             write (iout,*) "gvdwc gvdwx gradb gradbx"
<             do i=nnt,nct
<               write(iout,'(i5,4(3f10.2,5x))') i,(gvdwc(j,i),gvdwx(j,i),
<      &          gradb(j,i),gradbx(j,i),j=1,3)
<             enddo
<             call pdbout(0.0d0,'cipiszcze',iout)
<             call flush(iout)
<           endif
<         endif
<       endif
< #ifdef DEBUG
<       write (iout,*) "gradc gradx gloc"
<       do i=1,nres
<         write (iout,'(i5,3f10.5,5x,3f10.5,5x,f10.5)')
<      &   i,(gradc(j,i,icg),j=1,3),(gradx(j,i,icg),j=1,3),gloc(i,icg)
<       enddo
< #endif
< #ifdef TIMING
< #ifdef MPI
<       time_sumgradient=time_sumgradient+MPI_Wtime()-time01
< #else
<       time_sumgradient=time_sumgradient+tcpu()-time01
< #endif
< #endif
<       return
<       end
< c-------------------------------------------------------------------------------
1648c899
<      & esaxs,ehomology_constr,edfator,edfanei,edfabet,etot
---
>      & esaxs,ehomology_constr,edfator,edfanei,edfabet,ess,etot
1687a939
>       ess = energia(32)
1695c947,950
<      &  ethetacnstr,ebr*nss,Uconst,wumb,eliptran,wliptran,Eafmforce,
---
>      &  ethetacnstr,
> !    &  ebr*nss,
>      &  ess,
>      &  Uconst,wumb,eliptran,wliptran,Eafmforce,
1738c993,996
<      &  ethetacnstr,ebr*nss,Uconst,wumb,eliptran,wliptran,Eafmforc,
---
>      &  ethetacnstr,
> !    &  ebr*nss,
>      &  ess,
>      &  Uconst,wumb,eliptran,wliptran,Eafmforc,
1807a1066,1072
> 
>       write(*,*)'ERROR:',
>      &  'ejl is broken by new list format'
>       write(iout,*)'ERROR:',
>      &  'ejl is broken by new list format'
>       call terminate
> 
1944a1210,1216
> 
>       write(*,*)'ERROR:',
>      &  'ejlk is broken by new list format'
>       write(iout,*)'ERROR:',
>      &  'ejlk is broken by new list format'
>       call terminate
> 
2067a1340,1346
> 
>       write(*,*)'ERROR:',
>      &  'ebp is broken by new list format'
>       write(iout,*)'ERROR:',
>      &  'ebp is broken by new list format'
>       call terminate
> 
2227a1507,1513
> 
>       write(*,*)'ERROR:',
>      &  'egbv is broken by new list format'
>       write(iout,*)'ERROR:',
>      &  'egbv is broken by new list format'
>       call terminate
> 
2519a1806,1812
> 
>       write(*,*)'ERROR:',
>      &  'e_softsphere is broken by new list format'
>       write(iout,*)'ERROR:',
>      &  'e_softsphere is broken by new list format'
>       call terminate
> 
2601a1895,1901
> 
>       write(*,*)'ERROR:',
>      &  'eelec_soft_sphere is broken by new list format'
>       write(iout,*)'ERROR:',
>      &  'eelec_soft_sphere is broken by new list format'
>       call terminate
> 
2687a1988,1991
> ! TODO: BROKEN
> ! (i,i-2), (i,i+2) pairs are removed from
> ! newcontlistppj, so these pairs must be handled separately;
> ! see subroutine escp for details
2705a2010,2016
> 
>       write(*,*)'ERROR:',
>      &  'escp_soft_sphere is broken by new list format'
>       write(iout,*)'ERROR:',
>      &  'escp_soft_sphere is broken by new list format'
>       call terminate
> 
2856a2168,2268
>       subroutine omp_ssbond_ene(i,j,eij,dgci,dgcj,dgxi,dgxj)
> C
> C The only difference between this and ssbond_ene is that this
> C subroutine does not update global gradients but stores results
> C in its parameters
> C
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.SBRIDGE'
>       include 'COMMON.CHAIN'
>       include 'COMMON.DERIV'
>       include 'COMMON.LOCAL'
>       include 'COMMON.INTERACT'
>       include 'COMMON.VAR'
>       include 'COMMON.IOUNITS'
>       integer i,j,k,itypi,itypj
>       double precision rij,rrij,ggk
>       double precision eij,ed,cosphi,pom1,pom2,dsci_inv,dscj_inv
>       double precision xi,yi,zi,xj,yj,zj,dxi,dyi,dzi,dxj,dyj,dzj
>       double precision om1,om2,om12,eom1,eom2,eom12
>       double precision deltad,deltat1,deltat2,deltat12
>       double precision erij(3),dcosom1(3),dcosom2(3),gg(3)
>       double precision dgci(3),dgcj(3),dgxi(3),dgxj(3)
>       itypi=iabs(itype(i))
>       xi=c(1,nres+i)
>       yi=c(2,nres+i)
>       zi=c(3,nres+i)
>       dxi=dc_norm(1,nres+i)
>       dyi=dc_norm(2,nres+i)
>       dzi=dc_norm(3,nres+i)
> c      dsci_inv=dsc_inv(itypi)
>       dsci_inv=vbld_inv(nres+i)
>       itypj=iabs(itype(j))
> c      dscj_inv=dsc_inv(itypj)
>       dscj_inv=vbld_inv(nres+j)
>       xj=c(1,nres+j)-xi
>       yj=c(2,nres+j)-yi
>       zj=c(3,nres+j)-zi
>       dxj=dc_norm(1,nres+j)
>       dyj=dc_norm(2,nres+j)
>       dzj=dc_norm(3,nres+j)
>       rrij=1.0D0/(xj*xj+yj*yj+zj*zj)
>       rij=dsqrt(rrij)
>       erij(1)=xj*rij
>       erij(2)=yj*rij
>       erij(3)=zj*rij
>       om1=dxi*erij(1)+dyi*erij(2)+dzi*erij(3)
>       om2=dxj*erij(1)+dyj*erij(2)+dzj*erij(3)
>       om12=dxi*dxj+dyi*dyj+dzi*dzj
>       do k=1,3
>         dcosom1(k)=rij*(dc_norm(k,nres+i)-om1*erij(k))
>         dcosom2(k)=rij*(dc_norm(k,nres+j)-om2*erij(k))
>       enddo
>       rij=1.0d0/rij
>       deltad=rij-d0cm
>       deltat1=1.0d0-om1
>       deltat2=1.0d0+om2
>       deltat12=om2-om1+2.0d0
>       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+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
>       ed=2*akcm*deltad+akct*deltat12
>       pom1=akct*deltad
>       pom2=v1ss+2*v2ss*cosphi+3*v3ss*cosphi*cosphi
>       eom1=-2*akth*deltat1-pom1-om2*pom2
>       eom2= 2*akth*deltat2+pom1-om1*pom2
>       eom12=pom2
>       do k=1,3
>         ggk=ed*erij(k)+eom1*dcosom1(k)+eom2*dcosom2(k)
> !       ghpbx(k,i)=ghpbx(k,i)-ggk
> !    &            +(eom12*(dc_norm(k,nres+j)-om12*dc_norm(k,nres+i))
> !    &            +eom1*(erij(k)-om1*dc_norm(k,nres+i)))*dsci_inv
>         dgxi(k)=dgxi(k)-ggk
>      &            +(eom12*(dc_norm(k,nres+j)-om12*dc_norm(k,nres+i))
>      &            +eom1*(erij(k)-om1*dc_norm(k,nres+i)))*dsci_inv
> !       ghpbx(k,j)=ghpbx(k,j)+ggk
> !    &            +(eom12*(dc_norm(k,nres+i)-om12*dc_norm(k,nres+j))
> !    &            +eom2*(erij(k)-om2*dc_norm(k,nres+j)))*dscj_inv
>         dgxj(k)=dgxj(k)+ggk
>      &            +(eom12*(dc_norm(k,nres+i)-om12*dc_norm(k,nres+j))
>      &            +eom2*(erij(k)-om2*dc_norm(k,nres+j)))*dscj_inv
> !       ghpbc(k,i)=ghpbc(k,i)-ggk
>         dgci(k)=dgci(k)-ggk
> !       ghpbc(k,j)=ghpbc(k,j)+ggk
>         dgcj(k)=dgcj(k)+ggk
>       enddo
> C
> C Calculate the components of the gradient in DC and X
> C
> cgrad      do k=i,j-1
> cgrad        do l=1,3
> cgrad          ghpbc(l,k)=ghpbc(l,k)+gg(l)
> cgrad        enddo
> cgrad      enddo
>       return
>       end
> C--------------------------------------------------------------------------
3847a3260
>       delta=0.02d0*pi
3871c3284,3285
< !$OMP&   sumene1y,sumene2y,sumene3y,sumene4y,sumene,sum_escloc
---
> !$OMP&   sumene1y,sumene2y,sumene3y,sumene4y,sumene,sum_escloc,
> !$OMP&   gradene
3888d3301
<       delta=0.02d0*pi
3910a3324,3327
> #ifdef ESCLOG
>           write(*,'(a,i4,3f18.12)')'escA',i,dc_norm(:,i-1)
>           write(*,'(a,i4,3f18.12)')'escB',i,dc_norm(:,i+nres)
> #endif
3918a3336,3340
> #ifdef ESCLOG
>           write(*,'(a,i4,3f18.12)')'escC',i,sumene,gradene,cossc1
>           write(*,'(a,i4,3f18.12)')'escD',i,vbld_inv(i)*
>      &      (dC_norm(:,i+nres)-dC_norm(:,i-1)*cossc1)*gradene
> #endif
3920a3343,3346
> #ifdef ESCLOG
>           write(*,'(a,i4,3f18.12)')'escE',i,vbld_inv(i+nres)*
>      &       (dC_norm(:,i-1)-dC_norm(:,i+nres)*cossc1)*gradene
> #endif
3927a3354,3357
> #ifdef ESCLOG
>           write(*,'(a,i4,3f18.12)')'escA',i,dc_norm(:,i)
>           write(*,'(a,i4,3f18.12)')'escB',i,dc_norm(:,i+nres)
> #endif
3932a3363,3365
> #ifdef ESCLOG
>           write(*,'(a,i4,3f18.12)')'escC',i,sumene,gradene,cossc
> #endif
3935a3369,3372
> #ifdef ESCLOG
>           write(*,'(a,i4,3f18.12)')'escD',i,vbld_inv(i+1)*
>      &      (dC_norm(:,i+nres)-dC_norm(:,i)*cossc)*gradene
> #endif
3937a3375,3378
> #ifdef ESCLOG
>           write(*,'(a,i4,3f18.12)')'escE',i,vbld_inv(i+nres)*
>      &       (dC_norm(:,i)-dC_norm(:,i+nres)*cossc)*gradene
> #endif
3951a3393,3399
> #ifdef ESCLOG
>         write(*,'(a,i4,3f18.12)')'escA1',i,dc_norm(:,i-1)
>         write(*,'(a,i4,3f18.12)')'escA2',i,dc_norm(:,i)
>         write(*,'(a,i4,2f18.12)')'escA3',i,cosfac2,sinfac2
>         write(*,'(a,i4,3f18.12)')'escB',i,dc_norm(:,i+nres)
> #endif
> 
4068a3517,3519
> #ifdef ESCLOG
>         write(*,'(a,i4,2f18.12)')'escC',i,sumene,cost2tab(i+1)
> #endif
4097a3549
> 
4156a3609
> 
4184c3637
<           dZZ_Ci(k)=            ! scalar breaks here
---
>           dZZ_Ci(k)=
4245,4256d3697
< !       write(*,'(i4,3f22.16)')i,de_dxx,de_dyy,de_dzz
< !       write(*,'(a,3f22.16)')'xc1 ',dxx_ci1
< !       write(*,'(a,3f22.16)')'yc1 ',dyy_ci1
< !       write(*,'(a,3f22.16)')'zc1 ',dzz_ci1
< !       write(*,'(a,3f22.16)')'tc1 ',dt_dCi1
< !       write(*,'(a,3f22.16)')'xci ',dxx_ci
< !       write(*,'(a,3f22.16)')'yci ',dyy_ci
< !       write(*,'(a,3f22.16)')'zci ',dzz_ci
< !       write(*,'(a,3f22.16)')'tci ',dt_dCi
< !       write(*,'(a,3f22.16)')'X   ',dxx_XYZ
< !       write(*,'(a,3f22.16)')'Y   ',dyy_XYZ
< !       write(*,'(a,3f22.16)')'Z   ',dzz_XYZ
4260a3702,3709
> 
> #ifdef ESCLOG
>         write(*,'(a,i4,3f18.12)')'escD',i,de_dxx*dxx_ci1
>      &             +de_dyy*dyy_ci1+de_dzz*dzz_ci1+de_dt*dt_dCi1
>         write(*,'(a,i4,3f18.12)')'escE',i,de_dxx*dxx_Ci
>      &             +de_dyy*dyy_Ci+de_dzz*dzz_Ci+de_dt*dt_dCi
> #endif
> 
4496,4503d3944
< c----------------------------------------------------------------------------
< c LICZENIE WIEZOW Z ROWNANIA ENERGII MODELLERA
<       subroutine e_modeller(ehomology_constr)
<       ehomology_constr=0.0d0
<       write (iout,*) "!!!!!UWAGA, JESTEM W DZIWNEJ PETLI, TEST!!!!!"
<       return
<       end
< C !!!!!!!! NIE CZYTANE !!!!!!!!!!!
4506,4509c3947,3950
<       subroutine etor_d(etors_d)
<       etors_d=0.0d0
<       return
<       end
---
> !     subroutine etor_d(etors_d)
> !     etors_d=0.0d0
> !     return
> !     end
4694a4136,4372
> c---------------------------------------------------------------------------------------------
>       subroutine etor_constr(edihcnstr)
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.VAR'
>       include 'COMMON.GEO'
>       include 'COMMON.LOCAL'
>       include 'COMMON.TORSION'
>       include 'COMMON.INTERACT'
>       include 'COMMON.DERIV'
>       include 'COMMON.CHAIN'
>       include 'COMMON.NAMES'
>       include 'COMMON.IOUNITS'
>       include 'COMMON.FFIELD'
>       include 'COMMON.TORCNSTR'
>       include 'COMMON.BOUNDS'
>       include 'COMMON.CONTROL'
>       double precision edihcnstr
>       double precision phii,gaudih_i,gauder_i,s,cos_i,dexpcos_i
>       double precision difi,fac
>       integer i,j,itori
>       double precision pinorm
>       !TODO: assumed that i != j implies
>       !      idih_constr(i) != idih_constr(j)
>       !      otherwise data races must be handled
> ! 6/20/98 - dihedral angle constraints
>       edihcnstr=0.0d0
> c      do i=1,ndih_constr
>       if (raw_psipred) then
> !$OMP PARALLEL DO DEFAULT(SHARED) REDUCTION(+:edihcnstr)
> !$OMP& PRIVATE(j,itori,phii,difi,gaudih_i,gauder_i,s,cos_i,dexpcos_i)
>         do i=idihconstr_start,idihconstr_end
>           itori=idih_constr(i)
>           phii=phi(itori)
>           gaudih_i=vpsipred(1,i)
>           gauder_i=0.0d0
>           do j=1,2
>             s = sdihed(j,i)
>             cos_i=(1.0d0-dcos(phii-phibound(j,i)))/s**2
>             dexpcos_i=dexp(-cos_i*cos_i)
>             gaudih_i=gaudih_i+vpsipred(j+1,i)*dexpcos_i
>             gauder_i=gauder_i-2*vpsipred(j+1,i)*dsin(phii-phibound(j,i))
>      &            *cos_i*dexpcos_i/s**2
>           enddo
>           edihcnstr=edihcnstr-wdihc*dlog(gaudih_i)
>           gloc(itori-3,icg)=gloc(itori-3,icg)-wdihc*gauder_i/gaudih_i
> #ifdef ENERGY_DEC
>           if (energy_dec)
>      &     write (iout,'(2i5,f8.3,f8.5,2(f8.5,2f8.3),f10.5)')
>      &     i,itori,phii*rad2deg,vpsipred(1,i),vpsipred(2,i),
>      &     phibound(1,i)*rad2deg,sdihed(1,i)*rad2deg,vpsipred(3,i),
>      &     phibound(2,i)*rad2deg,sdihed(2,i)*rad2deg,
>      &     -wdihc*dlog(gaudih_i)
> #endif
>         enddo
>       else ! .not.raw_psipred
> 
> !$OMP PARALLEL DO DEFAULT(SHARED) REDUCTION(+:edihcnstr)
> !$OMP& PRIVATE(itori,phii,difi,fac)
>         do i=idihconstr_start,idihconstr_end
>           itori=idih_constr(i)
>           phii=phi(itori)
>           difi=pinorm(phii-phi0(i))
>           if (abs(difi).gt.drange(i)) then
>             difi=difi-sign(drange(i),difi)
>             fac=ftors(i)*difi*difi*difi
>             edihcnstr=edihcnstr+0.25d0*fac*difi
>             gloc(itori-3,icg)=gloc(itori-3,icg)+fac
>           !else
>           !  difi=0.0
>           endif
>         enddo
>       endif !if(raw_psipred)
> 
>       return
>       end
> c-------------------------------------------------------------------------------------
>       subroutine etheta_constr(ethetacnstr)
> 
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.VAR'
>       include 'COMMON.GEO'
>       include 'COMMON.LOCAL'
>       include 'COMMON.TORSION'
>       include 'COMMON.INTERACT'
>       include 'COMMON.DERIV'
>       include 'COMMON.CHAIN'
>       include 'COMMON.NAMES'
>       include 'COMMON.IOUNITS'
>       include 'COMMON.FFIELD'
>       include 'COMMON.TORCNSTR'
>       include 'COMMON.CONTROL'
>       double precision ethetacnstr
>       double precision difi,thetiii,fac
>       integer i,itheta
>       double precision pinorm
>       ethetacnstr=0.0d0
> C      print *,ithetaconstr_start,ithetaconstr_end,"TU"
>       !TODO: assumed that i != j implies
>       !      itheta_constr(i) != itheta_constr(j)
>       !      otherwise data races must be handled
> !$OMP PARALLEL DO DEFAULT(SHARED) REDUCTION(+:ethetacnstr)
> !$OMP& PRIVATE(itheta,thetiii,difi,fac)
>       do i=ithetaconstr_start,ithetaconstr_end
>         itheta=itheta_constr(i)
>         thetiii=theta(itheta)
>         difi=pinorm(thetiii-theta_constr0(i))
> c        write (iout,*) difi*rad2deg,theta_drange(i)*rad2deg
> !        if (difi.gt.theta_drange(i)) then
> !          difi=difi-theta_drange(i)
> !          fac=for_thet_constr(i)*difi*difi*difi
> !c          write (iout,*) ">difi",theta_drange(i)*rad2deg,difi*rad2deg
> !          ethetacnstr=ethetacnstr+0.25d0*fac*difi
> !          gloc(itheta+nphi-2,icg)=gloc(itheta+nphi-2,icg)+fac
> !        else if (difi.lt.-theta_drange(i)) then
> !          difi=difi+theta_drange(i)
> !          fac=for_thet_constr(i)*difi*difi*difi
> !          ethetacnstr=ethetacnstr+0.25d0*fac*difi
> !          gloc(itheta+nphi-2,icg)=gloc(itheta+nphi-2,icg)+fac
>         if (abs(difi).gt.theta_drange(i)) then
>           difi=difi-sign(theta_drange(i),difi)
>           fac=for_thet_constr(i)*difi*difi*difi
>           ethetacnstr=ethetacnstr+0.25d0*fac*difi
>           gloc(itheta+nphi-2,icg)=gloc(itheta+nphi-2,icg)+fac
>         else
>           difi=0.0
> c          write (iout,*) "<>difi",difi*rad2deg
>         endif
> #ifdef ENERGY_DEC
>        if (energy_dec) then
>         write (iout,'(a6,2i5,4f8.3,2e14.5)') "ethetc",
>      &    i,itheta,rad2deg*thetiii,
>      &    rad2deg*theta_constr0(i),  rad2deg*theta_drange(i),
>      &    rad2deg*difi,0.25d0*for_thet_constr(i)*difi**4,
>      &    gloc(itheta+nphi-2,icg)
>         endif
> #endif
>       enddo
>       return
>       end
> c------------------------------------------------------------------------------
>       subroutine eback_sc_corr(esccor)
> c 7/21/2007 Correlations between the backbone-local and side-chain-local
> c        conformational states; temporarily implemented as differences
> c        between UNRES torsional potentials (dependent on three types of
> c        residues) and the torsional potentials dependent on all 20 types
> c        of residues computed from AM1  energy surfaces of terminally-blocked
> c        amino-acid residues.
>       implicit real*8 (a-h,o-z)
>       include 'DIMENSIONS'
>       include 'COMMON.VAR'
>       include 'COMMON.GEO'
>       include 'COMMON.LOCAL'
>       include 'COMMON.TORSION'
>       include 'COMMON.SCCOR'
>       include 'COMMON.INTERACT'
>       include 'COMMON.DERIV'
>       include 'COMMON.CHAIN'
>       include 'COMMON.NAMES'
>       include 'COMMON.IOUNITS'
>       include 'COMMON.FFIELD'
>       include 'COMMON.CONTROL'
>       logical lprn
> C Set lprn=.true. for debugging
>       lprn=.false.
> c      lprn=.true.
> c      write (iout,*) "EBACK_SC_COR",itau_start,itau_end
>       esccor=0.0D0
>       do i=itau_start,itau_end
>         if ((itype(i-2).eq.ntyp1).or.(itype(i-1).eq.ntyp1)) cycle
>         esccor_ii=0.0D0
>         isccori=isccortyp(itype(i-2))
>         isccori1=isccortyp(itype(i-1))
> 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
>         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,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--------------------------------------------------------------------------
>       double precision function scalar(u,v)
> #ifdef CRAY
> !DIR$ INLINEALWAYS scalar
> #endif
> #ifndef OSF
> cDEC$ ATTRIBUTES FORCEINLINE::scalar
> #endif
>       implicit none
>       double precision u(3),v(3)
> cd      double precision sc
> cd      integer i
> cd      sc=0.0d0
> cd      do i=1,3
> cd        sc=sc+u(i)*v(i)
> cd      enddo
> cd      scalar=sc
> 
>       scalar=u(1)*v(1)+u(2)*v(2)+u(3)*v(3)
>       return
>       end
4842c4520
<       implicit real*8 (a-h,o-z)
---
>       implicit none
4852d4529
<       include 'COMMON.CALC'
4855a4533,4538
>       include 'COMMON.LIPID'
>       integer i
>       double precision eliptran,eliptran1,eliptran2
>       double precision fracinbuf,sslip,ssgradlip,positi
>       double precision sscalelip
>       double precision sscagradlip
4866c4549,4567
<       eliptran=0.0
---
>       eliptran=0.0d0
>       eliptran1=0.0d0
>       eliptran2=0.0d0
> 
>       if (itype(ilip_start).ne.ntyp1) then
>         sslip=lipid_cache_ss_ns(ilip_start)
>         ssgradlip=lipid_cache_ssgrad_ns(ilip_start)
>         gliptranc(3,ilip_start-1)=gliptranc(3,ilip_start-1)
>      &    +ssgradlip*pepliptran*0.5d0
> 
>         sslip=lipid_cache_ss_ns(ilip_start+nres)
>         ssgradlip=lipid_cache_ssgrad_ns(ilip_start+nres)
>         gliptranc(3,ilip_start-1)=gliptranc(3,ilip_start-1)
>      &    +ssgradlip*liptranene(itype(ilip_start))
>       endif
> 
> !$OMP PARALLEL DO DEFAULT(SHARED)
> !$OMP& REDUCTION(+:eliptran1,eliptran2)
> !$OMP& PRIVATE(sslip,ssgradlip)
4871,4872c4572,4575
<         positi=(mod(((c(3,i)+c(3,i+1))/2.0d0),boxzsize))
<         if (positi.le.0.0) positi=positi+boxzsize
---
>         !positi=(mod(((c(3,i)+c(3,i+1))/2.0d0),boxzsize))
>         !if (positi.le.0.0) positi=positi+boxzsize
>         !call to_box_z(0.5d0*(c(3,i)+c(3,i+1),positi)
>         !positi=cp(3,i)
4876,4910c4579,4625
<        if ((positi.gt.bordlipbot)
<      &.and.(positi.lt.bordliptop)) then
< C the energy transfer exist
<         if (positi.lt.buflipbot) then
< C what fraction I am in
<          fracinbuf=1.0d0-
<      &        ((positi-bordlipbot)/lipbufthick)
< C lipbufthick is thickenes of lipid buffore
<          sslip=sscalelip(fracinbuf)
<          ssgradlip=-sscagradlip(fracinbuf)/lipbufthick
<          eliptran=eliptran+sslip*pepliptran
<          gliptranc(3,i)=gliptranc(3,i)+ssgradlip*pepliptran/2.0d0
<          gliptranc(3,i-1)=gliptranc(3,i-1)+ssgradlip*pepliptran/2.0d0
< C         gliptranc(3,i-2)=gliptranc(3,i)+ssgradlip*pepliptran
< 
< C        print *,"doing sccale for lower part"
< C         print *,i,sslip,fracinbuf,ssgradlip
<         elseif (positi.gt.bufliptop) then
<          fracinbuf=1.0d0-((bordliptop-positi)/lipbufthick)
<          sslip=sscalelip(fracinbuf)
<          ssgradlip=sscagradlip(fracinbuf)/lipbufthick
<          eliptran=eliptran+sslip*pepliptran
<          gliptranc(3,i)=gliptranc(3,i)+ssgradlip*pepliptran/2.0d0
<          gliptranc(3,i-1)=gliptranc(3,i-1)+ssgradlip*pepliptran/2.0d0
< C         gliptranc(3,i-2)=gliptranc(3,i)+ssgradlip*pepliptran
< C          print *, "doing sscalefor top part"
< C         print *,i,sslip,fracinbuf,ssgradlip
<         else
<          eliptran=eliptran+pepliptran
< C         print *,"I am in true lipid"
<         endif
< C       else
< C       eliptran=elpitran+0.0 ! I am in water
<        endif
<        enddo
---
>         sslip=lipid_cache_ss_ns(i)
>         ssgradlip=lipid_cache_ssgrad_ns(i)
>         !eliptran=eliptran+sslip*pepliptran
>         eliptran1=eliptran1+sslip
> !       gliptranc(3,i)=gliptranc(3,i)+ssgradlip*pepliptran*0.5d0
> !       gliptranc(3,i-1)=gliptranc(3,i-1)+ssgradlip*pepliptran*0.5d0
>         gliptranc(3,i)=gliptranc(3,i)
>      &    +(ssgradlip+lipid_cache_ssgrad_ns(i+1))*pepliptran*0.5d0
> 
> !        if ((positi.gt.bordlipbot)
> !     &      .and.(positi.lt.bordliptop)) then
> !C the energy transfer exist
> !          if (positi.lt.buflipbot) then
> !C what fraction I am in
> !            fracinbuf=1.0d0-
> !     &        ((positi-bordlipbot)/lipbufthick)
> !C lipbufthick is thickenes of lipid buffore
> !            sslip=sscalelip(fracinbuf)
> !            ssgradlip=-sscagradlip(fracinbuf)/lipbufthick
> !            eliptran=eliptran+sslip*pepliptran
> !            gliptranc(3,i)=gliptranc(3,i)+ssgradlip*pepliptran/2.0d0
> !            gliptranc(3,i-1)=gliptranc(3,i-1)+ssgradlip*pepliptran/2.0d0
> !C           gliptranc(3,i-2)=gliptranc(3,i)+ssgradlip*pepliptran
> !
> !C           print *,"doing sccale for lower part"
> !C           print *,i,sslip,fracinbuf,ssgradlip
> !          elseif (positi.gt.bufliptop) then
> !            fracinbuf=1.0d0-((bordliptop-positi)/lipbufthick)
> !            sslip=sscalelip(fracinbuf)
> !            ssgradlip=sscagradlip(fracinbuf)/lipbufthick
> !            eliptran=eliptran+sslip*pepliptran
> !            gliptranc(3,i)=gliptranc(3,i)+ssgradlip*pepliptran/2.0d0
> !            gliptranc(3,i-1)=gliptranc(3,i-1)+ssgradlip*pepliptran/2.0d0
> !C           gliptranc(3,i-2)=gliptranc(3,i)+ssgradlip*pepliptran
> !C           print *, "doing sscalefor top part"
> !C           print *,i,sslip,fracinbuf,ssgradlip
> !          else
> !            eliptran=eliptran+pepliptran
> !C           print *,"I am in true lipid"
> !          endif
> !C       else
> !C       eliptran=elpitran+0.0 ! I am in water
> !        endif
> !     enddo
> 
> !     eliptran=eliptran*pepliptran
> 
4916,4920c4631,4636
<        do i=ilip_start,ilip_end
<         if (itype(i).eq.ntyp1) cycle
<         positi=(mod(c(3,i+nres),boxzsize))
<         if (positi.le.0) positi=positi+boxzsize
< c        write(iout,*) "i",i," positi",positi,bordlipbot,buflipbot,
---
> !     do i=ilip_start,ilip_end
> !       if (itype(i).eq.ntyp1) cycle
>         !positi=(mod(c(3,i+nres),boxzsize))
>         !if (positi.le.0) positi=positi+boxzsize
>         !positi=c_tobox(3,i+nres)
> c       write(iout,*) "i",i," positi",positi,bordlipbot,buflipbot,
4926,4958c4642,4670
<        if ((positi.gt.bordlipbot)
<      & .and.(positi.lt.bordliptop)) then
< C the energy transfer exist
<         if (positi.lt.buflipbot) then
<          fracinbuf=1.0d0-
<      &     ((positi-bordlipbot)/lipbufthick)
< c         write (iout,*) "i",i,itype(i)," fracinbuf",fracinbuf
< c         write (iout,*) "i",i," liptranene",liptranene(itype(i))
< C lipbufthick is thickenes of lipid buffore
<          sslip=sscalelip(fracinbuf)
<          ssgradlip=-sscagradlip(fracinbuf)/lipbufthick
<          eliptran=eliptran+sslip*liptranene(itype(i))
<          gliptranx(3,i)=gliptranx(3,i)
<      &+ssgradlip*liptranene(itype(i))
<          gliptranc(3,i-1)= gliptranc(3,i-1)
<      &+ssgradlip*liptranene(itype(i))
< C         print *,"doing sccale for lower part"
<         elseif (positi.gt.bufliptop) then
<          fracinbuf=1.0d0-
<      &((bordliptop-positi)/lipbufthick)
<          sslip=sscalelip(fracinbuf)
<          ssgradlip=sscagradlip(fracinbuf)/lipbufthick
<          eliptran=eliptran+sslip*liptranene(itype(i))
<          gliptranx(3,i)=gliptranx(3,i)
<      &+ssgradlip*liptranene(itype(i))
<          gliptranc(3,i-1)= gliptranc(3,i-1)
<      &+ssgradlip*liptranene(itype(i))
< C          print *, "doing sscalefor top part",sslip,fracinbuf
<         else
<          eliptran=eliptran+liptranene(itype(i))
< C         print *,"I am in true lipid"
<         endif
<         endif ! if in lipid or buffor
---
>         sslip=lipid_cache_ss_ns(i+nres)
>         ssgradlip=lipid_cache_ssgrad_ns(i+nres)
>         eliptran2=eliptran2+sslip*liptranene(itype(i))
>         gliptranx(3,i)=gliptranx(3,i)+ssgradlip*liptranene(itype(i))
>         !gliptranc(3,i-1)=gliptranc(3,i-1)+ssgradlip*liptranene(itype(i))
>         gliptranc(3,i)=gliptranc(3,i)
>      &    +lipid_cache_ssgrad_ns(i+1+nres)*liptranene(itype(i+1))
> 
> !        if ((positi.gt.bordlipbot)
> !     &      .and.(positi.lt.bordliptop)) then
> !C the energy transfer exist
> !          if (positi.lt.buflipbot) then
> !            fracinbuf=1.0d0-((positi-bordlipbot)/lipbufthick)
> !c           write (iout,*) "i",i,itype(i)," fracinbuf",fracinbuf
> !c           write (iout,*) "i",i," liptranene",liptranene(itype(i))
> !C lipbufthick is thickenes of lipid buffore
> !            sslip=sscalelip(fracinbuf)
> !            ssgradlip=-sscagradlip(fracinbuf)/lipbufthick
> !            gliptranx(3,i)=gliptranx(3,i)
> !     &        +ssgradlip*liptranene(itype(i))
> !            gliptranc(3,i-1)= gliptranc(3,i-1)
> !     &        +ssgradlip*liptranene(itype(i))
> !C           print *, "doing sscalefor top part",sslip,fracinbuf
> !          else
> !            eliptran=eliptran+liptranene(itype(i))
> !C           print *,"I am in true lipid"
> !          endif
> !        endif ! if in lipid or buffor
> 
4961,4963c4673,4679
<        enddo
<        return
<        end
---
>       enddo
> 
>       eliptran=eliptran1*pepliptran+eliptran2
> 
>       return
>       end
> 
4980a4697
>       include 'COMMON.SETUP'
4983a4701,4707
> 
> #ifdef MPI
>       if (nfgtasks.gt.1 .and. fg_rank.ne.0) then
>         return
>       endif
> #endif
> 
4985,4986c4709,4710
<       diffafm(i)=c(i,afmend)-c(i,afmbeg)
<       dist=dist+diffafm(i)**2
---
>         diffafm(i)=c(i,afmend)-c(i,afmbeg)
>         dist=dist+diffafm(i)**2
4991,4992c4715,4716
<       gradafm(i,afmend-1)=-forceAFMconst*diffafm(i)/dist
<       gradafm(i,afmbeg-1)=forceAFMconst*diffafm(i)/dist
---
>         gradafm(i,afmend-1)=-forceAFMconst*diffafm(i)/dist
>         gradafm(i,afmbeg-1)=forceAFMconst*diffafm(i)/dist
5013a4738
>       include 'COMMON.SETUP'
5018a4744
> 
5020a4747,4753
> 
> #ifdef MPI
>       if (nfgtasks.gt.1 .and. fg_rank.ne.0) then
>         return
>       endif
> #endif
> 
5022,5023c4755,4756
<       diffafm(i)=c(i,afmend)-c(i,afmbeg)
<       dist=dist+diffafm(i)**2
---
>         diffafm(i)=c(i,afmend)-c(i,afmbeg)
>         dist=dist+diffafm(i)**2
5027c4760
<      & *(distafminit+totTafm*velAFMconst-dist)**2
---
>      &  *(distafminit+totTafm*velAFMconst-dist)**2
5030,5035c4763,4768
<       gradafm(i,afmend-1)=-forceAFMconst*
<      &(distafminit+totTafm*velAFMconst-dist)
<      &*diffafm(i)/dist
<       gradafm(i,afmbeg-1)=forceAFMconst*
<      &(distafminit+totTafm*velAFMconst-dist)
<      &*diffafm(i)/dist
---
>         gradafm(i,afmend-1)=-forceAFMconst*
>      &    (distafminit+totTafm*velAFMconst-dist)
>      &    *diffafm(i)/dist
>         gradafm(i,afmbeg-1)=forceAFMconst*
>      &    (distafminit+totTafm*velAFMconst-dist)
>      &    *diffafm(i)/dist
energy_p_new-sep_barrier.F
1716a1717,1718
>       include 'COMMON.OMP'
>       include 'COMMON.EELEC'
1721,1722c1723,1724
<       integer*1 doturn(maxres)
<       common /eelecscalecommon/doturn
---
>       !integer*1 doturn(maxres)
>       !common /eelecscalecommon/doturn
1731c1733
<       logical tail_agg
---
>       !logical tail_agg
1769c1771,1772
<         call set_matrices
---
>         !call set_matrices
>         call set_matrices_scale
1807,1808c1810,1811
<       tail_agg=(wturn3.gt.0.0d0)
<       doturn(1:nres)=0
---
>       !tail_agg=(wturn3.gt.0.0d0)
>       !doturn(1:nres)=0
1834c1837
<         if  (tail_agg) doturn(i)=or(doturn(i),1)
---
>         !if  (tail_agg) doturn(i)=or(doturn(i),1)
1862c1865
<         if (tail_agg) doturn(i)=or(doturn(i),2)
---
>         !if (tail_agg) doturn(i)=or(doturn(i),2)
1864c1867,1868
<       call calculate_turns(doturn,eello_turn3,eello_turn4)
---
>       !call calculate_turns(doturn,eello_turn3,eello_turn4)
>       call calculate_turns(eelecij_doturn,eello_turn3,eello_turn4)
2246c2250
<      &    *sss1*faclipij
---
>      &      *sss1*faclipij
2337a2342,2347
> !       write(*,'(a,2i5,3f18.12)')'AI',i,j,cp(:,i)
> !       write(*,'(a,2i5,3f18.12)')'AJ',i,j,cp(:,j)
> !       write(*,'(a,2i5,3f18.12)')'BI',i,j,mu(:,i)
> !       write(*,'(a,2i5,3f18.12)')'BJ',i,j,mu(:,j)
> !       write(*,'(a,2i5,3f18.12)')'C',i,j,fac,rij,faclipij
> !       write(*,'(a,2i5,3f18.12)')'E',i,j,eel_loc_ij,muier,mujer
2547d2556
< CD        print '(a)','Enter ESCP KURWA'
energy_split-sep.F
36a37
>       include 'COMMON.OMP'
56a58,59
>       call make_soa_dc_norm
>       call calculate_lipid_layers
71,73c74,81
<       call make_inter_list(iatel_s,iatel_e,nint_gr_el,ielstart,
<      &   ielend,
<      &   cp(1,1),cp(1,1),itel,itel,3,
---
> !     call make_inter_list(iatel_s,iatel_e,nint_gr_el,ielstart,
> !    &   ielend,
> !    &   cp(1,1),cp(1,1),itel,itel,3,
> !    &   g_listpp_start,g_listpp_end,newcontlistppi,newcontlistppj,
> !    &   positive_sc,positive_sc,
> !    &   gridcellsc,dgridrangesc,griddatasc)
>         call make_inter_list(iatel_s,iatel_e,nint_gr_el,ielstart,ielend,
>      &   cp(1,1),cp(1,1),abs_itel,abs_itel,3,
75,76c83
<      &   positive_sc,positive_sc,
<      &   gridcellsc,dgridrangesc,griddatasc)
---
>      &   gridcellsc,dgridrangesc,griddatasc,0)
96a104
>       use grid_arrays
131a140
>       include 'COMMON.GRID'
245a255,257
>       call make_soa_dc_norm
>       call calculate_lipid_layers
>       !call update_lipaq
379a392
>         ! TODO: escp_soft_sphere is BROKEN
410c423
<       do i=1,n_ene
---
>       do i=0,n_ene
443a457,466
> !     write(*,*)'evdw        =',energia(1),evdw
> !     write(*,*)'evdw2       =',energia(2),evdw2
> !     write(*,*)'evdw2_14    =',energia(18),evdw2_14
> !     write(*,*)'ees         =',energia(3),ees
> !     write(*,*)'evdw1       =',energia(16),evdw1
> !     write(*,*)'eel_loc     =',energia(7),eel_loc
> !     write(*,*)'eello_turn3 =',energia(8),eello_turn3
> !     write(*,*)'eello_turn4 =',energia(9),eello_turn4
> !     write(*,*)'ecorr       =',energia(4),ecorr
> !     write(*,*)'ETOT-LONG   =',energia(0)
482d504
<       include 'COMMON.NMR'
487c509
<      & esaxs_constr,ehomology_constr,edfator,edfanei,edfabet
---
>      & esaxs_constr,ehomology_constr,edfator,edfanei,edfabet,ess
524a547,549
>       call make_soa_dc_norm
>       call calculate_lipid_layers
>       !call update_lipaq
585d609
<         call MPI_Bcast(atimeave,1,MPI_INTEGER,king,FG_COMM,IERROR)
747a772,780
> 
> C
> C Calculate ess energy
> C
>       ess=0.0d0
>       if (.not.dyn_ss) then
>         call ess_fixed(ess)
>       endif
> 
912a946
>       energia(32)=ess
917a952,965
> !     write(*,*)'evdw        =',energia(1),evdw
> !     write(*,*)'evdw2       =',energia(2),evdw2
> !     write(*,*)'evdw2_14    =',energia(18),evdw2_14
> !     write(*,*)'ees         =',energia(3),ees
> !     write(*,*)'evdw1       =',energia(16),evdw1
> !     write(*,*)'eel_loc     =',energia(7),eel_loc
> !     write(*,*)'eello_turn3 =',energia(8),eello_turn3
> !     write(*,*)'eello_turn4 =',energia(9),eello_turn4
> !     write(*,*)'ebe         =',energia(11),ebe
> !     write(*,*)'escloc      =',energia(12),escloc
> !     write(*,*)'etors       =',energia(13),etors
> !     write(*,*)'estr        =',energia(17),estr
> !     write(*,*)'ecorr       =',energia(4),ecorr
> !     write(*,*)'ETOT-SHORT  =',energia(0)
entmcm.F
esaxs.F
1c1,2
<       subroutine e_saxs(Esaxs_constr)
---
>       subroutine e_saxs_old(Esaxs_constr)
>       !subroutine e_saxs(Esaxs_constr)
59a61,71
> 
> !     double precision tsaxsc(3,maxres)
> !     double precision ene_exasx2,tCnorm
> !     common /saxstempcommon/ tsaxsc
> 
>       common /saxscommon/ PgradC,PgradX,
> #ifdef MPI
>      &  PgradC_,PgradX_,
> #endif
>      &  inei,rnei,rneiprim,radtabprim,dnei_inv
> 
89c101
< c 7/11/2020 Adam: determine the neighbors 
---
> c 7/11/2020 Adam: determine the neighbors
104a117
> !         write(*,'(a,2i5,1f18.10)')'RI',i,j,fcont
117,118c130,131
<           xx=-xx  
<         endif 
---
>           xx=-xx
>         endif
126c139,143
<         enddo 
---
> !         write(*,'(a,2i5,5f18.12)')'RDP',i,inei(j,i),radtabprim(j,i),
> !    &      xprim,rneiprim(j),x,rnei(i)
> !         write(*,'(a,2i5,5f18.12)')'RDPd',i,inei(j,i),
> !    &      rnei_max_bound,rnei_min_bound,rnei_mid_bound
>         enddo
128c145
<         write (iout,'(a3,i6,f8.3,f8.3,f8.3)') 
---
>         write (iout,'(a3,i6,f8.3,f8.3,f8.3)')
191c208
<              if (energy_dec) write(iout,'(a4,3i5,8f10.4)') 
---
>              if (energy_dec) write(iout,'(a4,3i5,8f10.4)')
202a220,221
> 
> !            write(*,'(a,3i5,1f18.12)')'CAG',i,j,k,CACAgrad1
204c223
< c CA CA 
---
> c CA CA
218a238,241
> !              write(*,'(a,3i5,3f18.12,2i5)')'DI',i,inii,k,CACAgrad1,
> !    &           radtabprim(ii,i),dnei_inv(ii,i),i,j
> !              write(*,'(a,3i5,1f18.12)')'DVi',i,inii,k,aux
> 
229a253,255
> !              write(*,'(a,3i5,3f18.12,2i5)')'DJ',j,injj,k,CACAgrad1,
> !    &           radtabprim(jj,j),dnei_inv(jj,j),i,j
> !              write(*,'(a,3i5,1f18.12)')'DVj',i,inii,k,aux
244c270
<       if (nfgtasks.gt.1) then 
---
>       if (nfgtasks.gt.1) then
293,294c319,320
<         if (Pcalc(k).gt.0.0d0) 
<      &  Esaxs_constr = Esaxs_constr - Psaxs(k)*dlog(Pcalc(k)) 
---
>         if (Pcalc(k).gt.0.0d0)
>      &  Esaxs_constr = Esaxs_constr - Psaxs(k)*dlog(Pcalc(k))
304a331,334
> !     do k=1,nsaxs
> !       write(*,'(a,i4,1f18.12)')'PCALC',k,Pcalc(k)
> !     enddo
> 
312c342
<           auxX1=0.d0 
---
>           auxX1=0.d0
314c344
<             if (Pcalc(k).gt.0) 
---
>             if (Pcalc(k).gt.0)
338a369,371
> !     do i=nnt,nct
> !       write (*,'(a,i5,3f18.10)')'GSC',i,gsaxsc(:,i)
> !     enddo
347a381,1334
> !     tCnorm = Cnorm
> !     do i=nnt,nct
> !       tsaxsc(:,i)=gsaxsc(:,i)
> !     enddo
> !     call e_saxs2(ene_exasx2,Pcalc,rnei)
> !     call e_saxs2(ene_exasx2)
> !     write(*,*)'EDE',Esaxs_constr,ene_exasx2,Esaxs_constr-ene_exasx2
> !     write(*,*)'CNm',tCnorm,Cnorm,tCnorm-Cnorm
> !     Cnorm=tCnorm
> !     do i=nnt,nct
> !       write(*,'(a,i4,3f18.14)')'GSC',i,gsaxsc(:,i)-tsaxsc(:,i)
> !       gsaxsc(:,i)=tsaxsc(:,i)
> !     enddo
>       return
>       end
> c----------------------------------------------------------------------------
>       !subroutine e_saxs2(Esaxs_constr,opc,rnei)
>       subroutine e_saxs(Esaxs_constr)
>       !subroutine e_saxs2(Esaxs_constr)
>       use omp_lib
>       use omp_data
>       implicit none
>       include 'DIMENSIONS'
> #ifdef MPI
>       include "mpif.h"
>       include "COMMON.SETUP"
>       integer IERR
> #endif
>       include 'COMMON.SBRIDGE'
>       include 'COMMON.CHAIN'
>       include 'COMMON.GEO'
>       include 'COMMON.DERIV'
>       include 'COMMON.LOCAL'
>       include 'COMMON.INTERACT'
>       include 'COMMON.VAR'
>       include 'COMMON.IOUNITS'
> c      include 'COMMON.MD'
> #ifdef LANG0
> #ifdef FIVEDIAG
>       include 'COMMON.LANGEVIN.lang0.5diag'
> #else
>       include 'COMMON.LANGEVIN.lang0'
> #endif
> #else
>       include 'COMMON.LANGEVIN'
> #endif
>       include 'COMMON.CONTROL'
>       include 'COMMON.SAXS'
>       include 'COMMON.NAMES'
>       include 'COMMON.TIME1'
>       include 'COMMON.FFIELD'
>       include 'COMMON.OMP'
>       include 'COMMON.SAXSGRID'
> c
> !     double precision opc(maxSAXS)
> !     double precision rnei(maxres)
> 
>       double precision Esaxs_constr
> !     double precision lCnorm
> !     double precision Pcalc(maxSAXS)   !COMMON.SAXS
> !     double precision lPcalc(maxSAXS)   !COMMON.SAXS
> !     double precision rnei_mid_bound   !COMMON.SAXS
>       double precision thPcalc(maxSAXS,max_fg_threads)
>       double precision fac(maxSAXS)
> !     double precision log_dk(maxSAXS)
> !     double precision lgsaxsc(3,maxres)
>       integer i,j,k,jj
>       double precision rrr,diffd,dk,aux(3)
>       double precision sss2,ssgrad2
>       double precision dij,dij0,fcont,fprimcont
>       double precision dijCACA,sigma2CACA,expCACA
>       double precision CACAgrad,CACAgrad1
>       double precision radtab(maxres)
>       !double precision lrnei(maxres)
>       double precision cagrad1(maxres)
>       double precision xradprimtab(maxres)
>       double precision xrnei(maxres)
>       double precision x,xx,xprim,rnei_i,s,x2,lo,hi,x4
>       double precision radtabprimij,radtabprimji
>       double precision log_dijCACA,aux2,idij
>       double precision dgsc(3),sum_dsci(3)
>       double precision dgrad1,sum_dgrad1i
>       double precision restok_i
>       integer itypi,itypj
>       double precision time01,tcpu
>       double precision merge_buffer(maxres)
> 
>       double precision sscale2,sscalgrad2,dist,dist_sq
> 
>       common /saxs2common/ radtab,cagrad1,xrnei,xradprimtab,
>      &  merge_buffer!,lgsaxsc,lrnei
> 
>       integer threads_used,my_thread
>       integer from_blk,to_blk,from_j,to_j
>       integer from_ik,to_ik,first_blk,last_blk,first_j,last_j
>       integer ikont,jblock
> 
> #ifdef TIMING
> #ifdef MPI
>       time01=MPI_Wtime()
> #else
>       time01=tcpu()
> #endif
> #endif
> 
> c  SAXS restraint penalty function
>       rnei_mid_bound=(rnei_min_bound+rnei_max_bound)/2
> 
>       call calculate_saxs_interactions
>       threads_used=saxs_threads
> 
>       ! clear buffers
>       ! TODO: precalculate constants at intialization
>       Esaxs_constr=0.0d0
> !     do k=1,nsaxs
> !       log_dk(k)=dlog(distsaxs(k))
> !     enddo
>       !cagrad1(nnt:nct)=0.0d0
>       !xrnei(nnt:nct)=0
>       !lgsaxsc(:,nnt:nct)=0.0d0
> !!$OMP PARALLEL DO DEFAULT(SHARED)
>       !do i=nnt,nct
>       !  cagrad1(i)=0.0d0
>       !  xrnei(i)=0
>       !  gsaxsc(:,i)=0.0d0
>       !enddo
> 
> 
>       !----------------------------------------------------------------
>       ! first loop:
>       ! - calculate rnei, radtab data for sites
>       !----------------------------------------------------------------
> c 7/11/2020 Adam: determine the neighbors
> !     do i=nnt,nct
> !       xrnei(i)=0.0d0
> !       if (itype(i).eq.ntyp1) cycle
> !       do j=nnt,nct
> !         if (j.eq.i. or. itype(j).eq.ntyp1) cycle
> !         dij=dist(c(1,i),c(1,j))
> !         dij0=0.5d0*(restok(itype(i))+restok(itype(j)))+1.0d0
> !         call gcont(dij,dij0,1.0d0,0.1d0,fcont,fprimcont)
> !         xrnei(i)=xrnei(i)+fcont
> !       enddo
> !       x=2*(xrnei(i)-rnei_mid_bound)/(rnei_max_bound-rnei_min_bound)
> !       if (x.gt.0.0d0) then
> !         call Gbounds(x,xx,xprim,1.0d-2)
> !       else
> !         call Gbounds(-x,xx,xprim,1.0d-2)
> !         xx=-xx
> !       endif
> !       xprimtab(i)=xprim
> !       xradprimtab(i)=-(sigmamax-sigmamin)/
> !    &    (rnei_max_bound-rnei_min_bound)*xprim
> !       lrnei(i)=rnei_mid_bound+(rnei_max_bound-rnei_min_bound)*xx/2
> !       radtab(i)=restok(itype(i))/scal_rad
> !    &    +sigmamax-(sigmamax-sigmamin)*(xx+1.0d0)/2
> !     enddo
> 
> #ifdef ESAXSLOG
> !     do i=nnt,nct
> !       write(*,'(a,i4,3f18.12)')'ESXC0',i,c(:,i)
> !     enddo
> #endif
> 
> !$OMP PARALLEL IF(threads_used.gt.1)
> !$OMP& NUM_THREADS(threads_used) DEFAULT(SHARED)
> !$OMP& PRIVATE(
> !$OMP&   i,j,jj,ikont,jblock,from_j,to_j,
> !$OMP&   from_ik,to_ik,first_blk,last_blk,first_j,last_j,
> !$OMP&   my_thread,from_blk,to_blk,x,x2,x4,
> !$OMP&   rnei_i,dij,dij0,fcont,fprimcont,itypi,itypj,restok_i
> !$OMP& )
> #ifdef _OPENMP
>       my_thread=omp_get_thread_num()+1
> #else
>       my_thread=1
> #endif
> !     do my_thread=1,threads_used
> 
>       from_ik=saxs_range(my_thread).rng_start
>       to_ik=saxs_range(my_thread).rng_end
>       first_blk=saxs_range(my_thread).rng_first
>       last_blk=saxs_range(my_thread).rng_last
>       first_j=saxs_range(my_thread).rng_first_j
>       last_j=saxs_range(my_thread).rng_last_j
> 
>       do ikont=from_ik,to_ik
>         i=saxscontlisti(1,ikont)
>         from_blk=saxscontlisti(2,ikont-1)+1
>         to_blk=saxscontlisti(2,ikont)
> 
>         if(ikont.eq.from_ik) from_blk=first_blk
>         if(ikont.eq.to_ik) to_blk=last_blk
> 
>         rnei_i=0.0d0
>         itypi=itype(i)
>         !restok_i=restok(itypi)
>         restok_i=restok2(itypi)
> 
>         do jblock=from_blk,to_blk
>           from_j=saxscontlistj(1,jblock)
>           to_j=saxscontlistj(2,jblock)
>           if(jblock.eq.first_blk) from_j=first_j
>           if(jblock.eq.last_blk) to_j=last_j
> 
>           do j=from_j,to_j
>             itypj=itype(j)
>             if (j.eq.i. or. itypj.eq.ntyp1) cycle
>             !---
>             dij=dist(c(1,i),c(1,j))
>             !dij0=0.5d0*(restok_i+restok(itypj))+1.0d0
>             dij0=restok_i+restok2(itypj)
>             !call gcont(dij,dij0,1.0d0,0.1d0,fcont,fprimcont)
>             x=(dij-dij0)*10.0d0
>             if (x.lt.-1.0d0) then
>               fcont=1.0d0
>             else if (x.le.1.0d0) then
>               x2=x*x
>               !x4=x2*x2
>               !fcont=x*(-0.9375d0+0.6250d0*x2-0.1875d0*x4)+0.5d0
>               fcont=x*(-0.9375d0+(0.6250d0-0.1875d0*x2)*x2)+0.5d0
>             else
>               fcont=0.0D0
>             endif
>             rnei_i=rnei_i+fcont
> !            xrnei(j)=xrnei(j)+fcont
>             workspace(my_thread).item.saxs_data(j)=
>      &        workspace(my_thread).item.saxs_data(j)+fcont
>             !write(*,'(a,2i5,1f18.10)')'RI',i,j,fcont
> !         write(*,'(a,2i4,a,1f18.10,i4)')'RI',i,j,' 1 ',fcont,0
> !         write(*,'(a,2i4,a,3f18.10,i4)')'RI',i,j,' 2 ',c(:,i),0
> !         write(*,'(a,2i4,a,3f18.10,i4)')'RI',i,j,' 3 ',c(:,j),0
> !         write(*,'(a,2i4,a,3f18.10,3i4)')'RI',i,j,' 4 ',
> !    &      dij,restok(itype(i)),restok(itype(j)),itype(i),itype(j),0
>             !---
>           enddo
>         enddo  ! jblock
> !        xrnei(i)=xrnei(i)+rnei_i
>         workspace(my_thread).item.saxs_data(i)=
>      &    workspace(my_thread).item.saxs_data(i)+rnei_i
>       enddo  ! ikont
> 
>       ! interactions in separate pairs
>       from_ik=saxs_range(my_thread).sep_start
>       to_ik=saxs_range(my_thread).sep_end
>       first_j=saxs_range(my_thread).sep_first
>       last_j=saxs_range(my_thread).sep_last
> 
>       do i=from_ik,to_ik
>         from_j=saxsseplisti(i-1)+1
>         to_j=saxsseplisti(i)
> 
>         rnei_i=0.0d0
>         itypi=itype(i)
>         !restok_i=restok(itypi)
>         restok_i=restok2(itypi)
> 
>         if(i.eq.from_ik) from_j=first_j
>         if(i.eq.to_ik) to_j=last_j
>         if (from_j.gt.to_j) cycle
>         do jj=from_j,to_j
>           j=saxsseplistj(jj)
>           !---
>           itypj=itype(j)
>           if (j.eq.i. or. itypj.eq.ntyp1) cycle
>           dij=dist(c(1,i),c(1,j))
>           !dij0=0.5d0*(restok_i+restok(itypj))+1.0d0
>           dij0=restok_i+restok2(itypj)
>           !call gcont(dij,dij0,1.0d0,0.1d0,fcont,fprimcont)
>           !subroutine gcont(dij,dij0,1.0d0,0.1d0,fcont,fprimcont)
>           x=(dij-dij0)*10.0d0
>           if (x.lt.-1.0d0) then
>             fcont=1.0d0
>           else if (x.le.1.0d0) then
>             x2=x*x
>             !x4=x2*x2
>             !fcont=x*(-0.9375d0+0.6250d0*x2-0.1875d0*x4)+0.5d0
>             fcont=x*(-0.9375d0+(0.6250d0-0.1875d0*x2)*x2)+0.5d0
>           else
>             fcont=0.0d0
>           endif
>           rnei_i=rnei_i+fcont
> !          xrnei(j)=xrnei(j)+fcont
>           workspace(my_thread).item.saxs_data(j)=
>      &      workspace(my_thread).item.saxs_data(j)+fcont
>           !write(*,'(a,2i5,1f18.10)')'RI',i,j,fcont
>           !---
>         enddo
> 
> !        xrnei(i)=xrnei(i)+rnei_i
>         workspace(my_thread).item.saxs_data(i)=
>      &    workspace(my_thread).item.saxs_data(i)+rnei_i
>       enddo  ! i
> !     enddo !my_thread
> !$OMP END PARALLEL
> 
>       call omp_sum_saxs_data(xrnei,1,nct)
> !     do i=1,nct
> !       write(*,'(a,i4,1f18.12)')'XRNEI',i,xrnei(i)
> !     enddo
> 
>       !----------------------------------------------------------------
>       ! MPI merge of xnei array
>       !----------------------------------------------------------------
> #ifdef MPI
>       if (nfgtasks.gt.1) then
>         call MPI_AllReduce(xrnei(nnt),merge_buffer(nnt),nct-nnt+1,
>      &    MPI_DOUBLE_PRECISION,MPI_SUM,FG_COMM,IERR)
>         do i=nnt,nct
>           xrnei(i)=merge_buffer(i)
>         enddo
>       endif
> #endif
> 
> 
>       !----------------------------------------------------------------
>       ! finalize rnei and radtab computations
>       !----------------------------------------------------------------
> !$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(x,xx,xprim)
>       do i=nnt,nct
>         x=2*(xrnei(i)-rnei_mid_bound)/(rnei_max_bound-rnei_min_bound)
>         ! TODO: Gbounds(abs(x)) ?
>         ! TODO: optimize Gbounds
> !       write(*,'(a,1i4,5f18.12)')'ESX1a',i,xrnei(i),x,
> !    &    rnei_min_bound, rnei_mid_bound, rnei_max_bound
>         if (x.gt.0.0d0) then
>           call Gbounds(x,xx,xprim,1.0d-2)
>         else
>           call Gbounds(-x,xx,xprim,1.0d-2)
>           xx=-xx
>         endif
> !       write(*,'(a,2i4,4f18.12)')'ESX1fpre',i,itype(i),restok(itype(i))
> !    &    ,x,xx,xprim
>         xradprimtab(i)=-(sigmamax-sigmamin)/
>      &    (rnei_max_bound-rnei_min_bound)*xprim
> !       lrnei(i)=rnei_mid_bound+(rnei_max_bound-rnei_min_bound)*xx/2
> !       write(*,'(a,i5,f18.10,f18.14)')'RNEI',i,lrnei(i),
> !    &    lrnei(i)-rnei(i)
>         radtab(i)=restok(itype(i))/scal_rad
>      &    +sigmamax-(sigmamax-sigmamin)*(xx+1.0d0)/2
> #ifdef ESAXSLOG
>         write(*,'(a,i4,3f18.12)')'ESX1f',i,
>      &    xrnei(i),xradprimtab(i),radtab(i)
> #endif
>       enddo
> 
>       !----------------------------------------------------------------
>       ! second loop:
>       ! - calculate Pcalc
>       !----------------------------------------------------------------
> !!     do i=iatsc_s,iatsc_e
> !      do i=nnt,nct
> !       if (itype(i).eq.ntyp1) cycle
> !!      do iint=1,nint_gr(i)
> !!        do j=istart(i,iint),iend(i,iint)
> !       !do j=nnt,nct
> !       do j=i+1,nct
> !           if (itype(j).eq.ntyp1) cycle
> !           dijCACA=dist(c(1,i),c(1,j))
> !           sigma2CACA=4*dijCACA**2/(radtab(i)+radtab(j))**2
> !           log_dijCACA=dlog(dijCACA)
> !           IF (saxs_cutoff.eq.0.0d0) THEN
> !             do k=1,nsaxs
> !               dk = distsaxs(k)
> !               diffd = log_dijCACA-log_dk(k)
> !               expCACA = dexp(-0.5d0*sigma2CACA*diffd**2)
> !               lPcalc(k) = lPcalc(k)+expCACA
> !             enddo ! k
> !           ELSE
> !             rrr = saxs_cutoff*(restok(itype(i))+restok(itype(j)))/2
> !             do k=1,nsaxs
> !               dk = distsaxs(k)
> !               diffd = log_dijCACA-log_dk(k)
> !               sss2 = sscale2(dijCACA,rrr,dk,0.3d0)
> !               if (sss2.eq.0.0d0) cycle
> !               expCACA = dexp(-0.5d0*sigma2CACA*diffd**2)*sss2
> !               lPcalc(k) = lPcalc(k)+expCACA
> !             enddo ! k
> !           ENDIF
> !         enddo ! j
> !!      enddo ! iint
> !      enddo ! i
> 
> !$OMP PARALLEL IF(threads_used.gt.1)
> !$OMP& NUM_THREADS(threads_used) DEFAULT(SHARED)
> !$OMP& PRIVATE(
> !$OMP&   i,j,jj,ikont,jblock,from_j,to_j,
> !$OMP&   from_ik,to_ik,first_blk,last_blk,first_j,last_j,
> !$OMP&   my_thread,from_blk,to_blk,
> !$OMP&   dij
> !$OMP& )
> #ifdef _OPENMP
>       my_thread=omp_get_thread_num()+1
> #else
>       my_thread=1
> #endif
> !     do my_thread=1,threads_used
>       thPcalc(1:nsaxs,my_thread)=0.0d0
> 
>       from_ik=saxs_range(my_thread).rng_start
>       to_ik=saxs_range(my_thread).rng_end
>       first_blk=saxs_range(my_thread).rng_first
>       last_blk=saxs_range(my_thread).rng_last
>       first_j=saxs_range(my_thread).rng_first_j
>       last_j=saxs_range(my_thread).rng_last_j
> 
>       do ikont=from_ik,to_ik
>         i=saxscontlisti(1,ikont)
>         from_blk=saxscontlisti(2,ikont-1)+1
>         to_blk=saxscontlisti(2,ikont)
> 
>         if(ikont.eq.from_ik) from_blk=first_blk
>         if(ikont.eq.to_ik) to_blk=last_blk
> 
>         do jblock=from_blk,to_blk
>           from_j=saxscontlistj(1,jblock)
>           to_j=saxscontlistj(2,jblock)
>           if(jblock.eq.first_blk) from_j=first_j
>           if(jblock.eq.last_blk) to_j=last_j
> 
>           do j=from_j,to_j
>             if (j.eq.i. or. itype(j).eq.ntyp1) cycle
>             !---
>             dij=dist(c(1,i),c(1,j))
>             call add_expCACA_to_Pcalc(i,j,dij,radtab,
>      &        thPcalc(:,my_thread))
>             !---
>           enddo
>         enddo  ! jblock
>       enddo  ! ikont
> 
>       ! interactions in separate pairs
>       from_ik=saxs_range(my_thread).sep_start
>       to_ik=saxs_range(my_thread).sep_end
>       first_j=saxs_range(my_thread).sep_first
>       last_j=saxs_range(my_thread).sep_last
> 
>       do i=from_ik,to_ik
>         from_j=saxsseplisti(i-1)+1
>         to_j=saxsseplisti(i)
> 
>         if(i.eq.from_ik) from_j=first_j
>         if(i.eq.to_ik) to_j=last_j
>         if (from_j.gt.to_j) cycle
>         do jj=from_j,to_j
>           j=saxsseplistj(jj)
>           if (j.eq.i. or. itype(j).eq.ntyp1) cycle
>           !---
>           dij=dist(c(1,i),c(1,j))
>           call add_expCACA_to_Pcalc(i,j,dij,radtab,
>      &      thPcalc(:,my_thread))
>           !---
>         enddo
>       enddo  ! i
> !     enddo !my_thread
> !$OMP END PARALLEL
> 
>       do i=1,nsaxs
>         s=0.0d0
>         do my_thread=1,threads_used
>           s=s+thPcalc(i,my_thread)
>         enddo
> !       write(*,'(a,i4,3f18.12)')'PCALC',i,s,Pcalc(i),s-Pcalc(i)
>         Pcalc(i)=s
> #ifdef ESAXSLOG
>         write(*,'(a,i4,2f18.12)')'ESX2pc',i,Pcalc(i),Psaxs(i)
> #endif
>       enddo
> 
>       !----------------------------------------------------------------
>       ! MPI merge of Pcalc array
>       !----------------------------------------------------------------
> #ifdef MPI
>       if (nfgtasks.gt.1) then
>         call MPI_AllReduce(Pcalc(1),merge_buffer(1),nsaxs,
>      &    MPI_DOUBLE_PRECISION,MPI_SUM,FG_COMM,IERR)
>         do k=1,nsaxs
>           Pcalc(k)=merge_buffer(k)
>         enddo
>       endif
> #endif
> 
>       !----------------------------------------------------------------
>       ! calculate Esaxs_constr and Pcalc-based factors
>       !----------------------------------------------------------------
>       Cnorm = 0.0d0
>       do k=1,nsaxs
>         Cnorm = Cnorm + Pcalc(k)
>       enddo
> !     write(*,*)'esaxs2 Cnorm',lCnorm,Cnorm,lCnorm-Cnorm
> 
>       do k=1,nsaxs
>         if(Pcalc(k).gt.0) then
>           fac(k)=Psaxs(k)/Pcalc(k)-1.0d0/Cnorm
>         else
>           fac(k)=-1.0d0/Cnorm
>         endif
>       enddo
> #ifdef MPI
>       if (fg_rank.eq.king) then
> #endif
>       Esaxs_constr = dlog(Cnorm)-wsaxs0
>       do k=1,nsaxs
>         if (Pcalc(k).gt.0.0d0)
>      &    Esaxs_constr = Esaxs_constr - Psaxs(k)*dlog(Pcalc(k))
>       enddo
> #ifdef MPI
>       endif
> #endif
> #ifdef ESAXSLOG
>       write(*,'(a,2f18.12)')'ESX3a',Cnorm,Esaxs_constr
>       do k=1,nsaxs
>         write(*,'(a,i4,f18.12)')'ESX3b',k,fac(k)
>       enddo
> #endif
> 
>       !----------------------------------------------------------------
>       ! third loop:
>       ! - update gsaxsc by CACAgrad
>       ! - calculate CACAgrad1 sums for each site (uses fac)
>       ! TODO: CACAgrad=.../dijCACA and aux=CACAgrad../dijCACA -> one division by dijCACA squared
>       ! TODO: move multiplications outside k loop
>       ! TODO: move updates outside k loop
>       ! TODO: sscale2/ssgrad2 optimization
>       ! TODO: precalculated restok(itype(i))*saxs_cutoff * 0.5
>       ! TODO: division by sss2 is unnecessary
>       !----------------------------------------------------------------
> !      do i=nnt,nct
> !       if (itype(i).eq.ntyp1) cycle
> !       do j=i+1,nct
> !         if (itype(j).eq.ntyp1) cycle
> !         dijCACA=dist(c(1,i),c(1,j))
> !         sigma2CACA=4*dijCACA**2/(radtab(i)+radtab(j))**2
> !         log_dijCACA=dlog(dijCACA)
> !         IF (saxs_cutoff.eq.0.0d0) THEN
> !           do k=1,nsaxs
> !             dk = distsaxs(k)
> !             !diffd = dlog(dijCACA)-dlog(dk)
> !             diffd = log_dijCACA-log_dk(k)
> !             expCACA = dexp(-0.5d0*sigma2CACA*diffd**2)
> !             CACAgrad = sigma2CACA*diffd*(diffd+1.0d0)/dijCACA*expCACA
> !             CACAgrad1 = -sigma2CACA/(radtab(i)+radtab(j))
> !     &        *diffd*diffd*expCACA
> !
> !             cagrad1(i)=cagrad1(i)+CACAgrad1*fac(k)
> !             cagrad1(j)=cagrad1(j)+CACAgrad1*fac(k)
> !
> !             aux = CACAgrad*(C(:,j)-C(:,i))/dijCACA
> !             lgsaxsc(:,i)=lgsaxsc(:,i)-aux*fac(k)
> !             lgsaxsc(:,j)=lgsaxsc(:,j)+aux*fac(k)
> !
> !           enddo ! k
> !         ELSE
> !           rrr = saxs_cutoff*(restok(itype(i))+restok(itype(j)))/2
> !           do k=1,nsaxs
> !             dk = distsaxs(k)
> !             !diffd = dlog(dijCACA)-dlog(dk)
> !             diffd = log_dijCACA-log_dk(k)
> !             sss2 = sscale2(dijCACA,rrr,dk,0.3d0)
> !             if (sss2.eq.0.0d0) cycle
> !             ssgrad2 = sscalgrad2(dijCACA,rrr,dk,0.3d0)
> !             expCACA = dexp(-0.5d0*sigma2CACA*diffd**2)*sss2
> !             CACAgrad = sigma2CACA*diffd*(diffd+1.0d0)/dijCACA*expCACA
> !     &            -ssgrad2*expCACA/sss2
> !             CACAgrad1 = -sigma2CACA/(radtab(i)+radtab(j))
> !     &        *diffd*diffd*expCACA
> !
> !             cagrad1(i)=cagrad1(i)+CACAgrad1*fac(k)
> !             cagrad1(j)=cagrad1(j)+CACAgrad1*fac(k)
> !!            write(*,'(a,3i5,2f18.12)')'CAG',i,j,k,CACAgrad1,
> !!    &         CACAgrad1*fac(k)
> !
> !             aux = CACAgrad*(C(:,j)-C(:,i))/dijCACA
> !             lgsaxsc(:,i)=lgsaxsc(:,i)-aux*fac(k)
> !             lgsaxsc(:,j)=lgsaxsc(:,j)+aux*fac(k)
> !
> !           enddo ! k
> !         ENDIF
> !
> !       enddo ! j
> !      enddo ! i
> 
> !$OMP PARALLEL IF(threads_used.gt.1)
> !$OMP& NUM_THREADS(threads_used) DEFAULT(SHARED)
> !$OMP& PRIVATE(
> !$OMP&   i,j,jj,ikont,jblock,from_j,to_j,
> !$OMP&   from_ik,to_ik,first_blk,last_blk,first_j,last_j,
> !$OMP&   my_thread,from_blk,to_blk,
> !$OMP&   dij,dgrad1,dgsc,sum_dsci,sum_dgrad1i
> !$OMP& )
> #ifdef _OPENMP
>       my_thread=omp_get_thread_num()+1
> #else
>       my_thread=1
> #endif
>       !do my_thread=1,threads_used
> 
>       from_ik=saxs_range(my_thread).rng_start
>       to_ik=saxs_range(my_thread).rng_end
>       first_blk=saxs_range(my_thread).rng_first
>       last_blk=saxs_range(my_thread).rng_last
>       first_j=saxs_range(my_thread).rng_first_j
>       last_j=saxs_range(my_thread).rng_last_j
> 
>       do ikont=from_ik,to_ik
>         i=saxscontlisti(1,ikont)
>         from_blk=saxscontlisti(2,ikont-1)+1
>         to_blk=saxscontlisti(2,ikont)
> 
>         if(ikont.eq.from_ik) from_blk=first_blk
>         if(ikont.eq.to_ik) to_blk=last_blk
> 
>         sum_dsci=0.0d0
>         sum_dgrad1i=0.0d0
> 
>         do jblock=from_blk,to_blk
>           from_j=saxscontlistj(1,jblock)
>           to_j=saxscontlistj(2,jblock)
>           if(jblock.eq.first_blk) from_j=first_j
>           if(jblock.eq.last_blk) to_j=last_j
> 
>           do j=from_j,to_j
>             if (j.eq.i. or. itype(j).eq.ntyp1) cycle
>             !---
>             dij=dist(c(1,i),c(1,j))
>             call saxs_grad_first_step(i,j,dij,radtab,fac,
>      &             dgrad1,dgsc)
> #ifdef ESAXSLOG
>             write(*,'(a,2i4,2f18.12)')'ESX3d',i,j,dgrad1,dgrad1
>             write(*,'(a,2i4,3f18.12)')'ESX3e',i,j,-dgsc(:)
>             write(*,'(a,2i4,3f18.12)')'ESX3f',i,j,dgsc(:)
> #endif
>             !sum_dgrad1i=sum_dgrad1i+dgrad1i
>             sum_dgrad1i=sum_dgrad1i+dgrad1
>             !sum_dsci=sum_dsci+dsci(:)
>             sum_dsci=sum_dsci-dgsc(:)
> !            cagrad1(j)=cagrad1(j)+dgrad1
>             workspace(my_thread).item.saxs_data(j)=
>      &        workspace(my_thread).item.saxs_data(j)+dgrad1
> !            gsaxsc(1,j)=gsaxsc(1,j)+dgsc(1)
> !            gsaxsc(2,j)=gsaxsc(2,j)+dgsc(2)
> !            gsaxsc(3,j)=gsaxsc(3,j)+dgsc(3)
>             workspace(my_thread).item.dgradbufcx(j)=
>      &        workspace(my_thread).item.dgradbufcx(j)+dgsc(1)*wsaxs
>             workspace(my_thread).item.dgradbufcy(j)=
>      &        workspace(my_thread).item.dgradbufcy(j)+dgsc(2)*wsaxs
>             workspace(my_thread).item.dgradbufcz(j)=
>      &        workspace(my_thread).item.dgradbufcz(j)+dgsc(3)*wsaxs
>             !---
>           enddo
>         enddo  ! jblock
> 
> !        cagrad1(i)=cagrad1(i)+sum_dgrad1i
>         workspace(my_thread).item.saxs_data(i)=
>      &    workspace(my_thread).item.saxs_data(i)+sum_dgrad1i
> !        gsaxsc(1,i)=gsaxsc(1,i)+sum_dsci(1)
> !        gsaxsc(2,i)=gsaxsc(2,i)+sum_dsci(2)
> !        gsaxsc(3,i)=gsaxsc(3,i)+sum_dsci(3)
>         workspace(my_thread).item.dgradbufcx(i)=
>      &    workspace(my_thread).item.dgradbufcx(i)+sum_dsci(1)*wsaxs
>         workspace(my_thread).item.dgradbufcy(i)=
>      &    workspace(my_thread).item.dgradbufcy(i)+sum_dsci(2)*wsaxs
>         workspace(my_thread).item.dgradbufcz(i)=
>      &    workspace(my_thread).item.dgradbufcz(i)+sum_dsci(3)*wsaxs
>       enddo  ! ikont
> 
>       ! interactions in separate pairs
>       from_ik=saxs_range(my_thread).sep_start
>       to_ik=saxs_range(my_thread).sep_end
>       first_j=saxs_range(my_thread).sep_first
>       last_j=saxs_range(my_thread).sep_last
> 
>       do i=from_ik,to_ik
>         from_j=saxsseplisti(i-1)+1
>         to_j=saxsseplisti(i)
> 
>         sum_dsci=0.0d0
>         sum_dgrad1i=0.0d0
> 
>         if(i.eq.from_ik) from_j=first_j
>         if(i.eq.to_ik) to_j=last_j
>         if (from_j.gt.to_j) cycle
>         do jj=from_j,to_j
>           j=saxsseplistj(jj)
>           if (j.eq.i. or. itype(j).eq.ntyp1) cycle
>           !---
>           dij=dist(c(1,i),c(1,j))
>           call saxs_grad_first_step(i,j,dij,radtab,fac,
>      &           dgrad1,dgsc)
> #ifdef ESAXSLOG
>           write(*,'(a,2i4,2f18.12)')'ESX3d',i,j,dgrad1,dgrad1
>           write(*,'(a,2i4,3f18.12)')'ESX3e',i,j,-dgsc(:)
>           write(*,'(a,2i4,3f18.12)')'ESX3f',i,j,dgsc(:)
> #endif
>           !sum_dgrad1i=sum_dgrad1i+dgrad1i
>           sum_dgrad1i=sum_dgrad1i+dgrad1
>           !sum_dsci=sum_dsci+dsci(:)
>           sum_dsci=sum_dsci-dgsc(:)
> !          cagrad1(j)=cagrad1(j)+dgrad1
>           workspace(my_thread).item.saxs_data(j)=
>      &      workspace(my_thread).item.saxs_data(j)+dgrad1
> !          gsaxsc(1,j)=gsaxsc(1,j)+dgsc(1)
> !          gsaxsc(2,j)=gsaxsc(2,j)+dgsc(2)
> !          gsaxsc(3,j)=gsaxsc(3,j)+dgsc(3)
>           workspace(my_thread).item.dgradbufcx(j)=
>      &      workspace(my_thread).item.dgradbufcx(j)+dgsc(1)*wsaxs
>           workspace(my_thread).item.dgradbufcy(j)=
>      &      workspace(my_thread).item.dgradbufcy(j)+dgsc(2)*wsaxs
>           workspace(my_thread).item.dgradbufcz(j)=
>      &      workspace(my_thread).item.dgradbufcz(j)+dgsc(3)*wsaxs
>           !---
>         enddo
> 
> !        cagrad1(i)=cagrad1(i)+sum_dgrad1i
>         workspace(my_thread).item.saxs_data(i)=
>      &    workspace(my_thread).item.saxs_data(i)+sum_dgrad1i
> !        gsaxsc(1,i)=gsaxsc(1,i)+sum_dsci(1)
> !        gsaxsc(2,i)=gsaxsc(2,i)+sum_dsci(2)
> !        gsaxsc(3,i)=gsaxsc(3,i)+sum_dsci(3)
>         workspace(my_thread).item.dgradbufcx(i)=
>      &    workspace(my_thread).item.dgradbufcx(i)+sum_dsci(1)*wsaxs
>         workspace(my_thread).item.dgradbufcy(i)=
>      &    workspace(my_thread).item.dgradbufcy(i)+sum_dsci(2)*wsaxs
>         workspace(my_thread).item.dgradbufcz(i)=
>      &    workspace(my_thread).item.dgradbufcz(i)+sum_dsci(3)*wsaxs
>       enddo  ! i
> !     enddo !my_thread
> !$OMP END PARALLEL
> 
>       call omp_sum_saxs_data(cagrad1,1,nct)
> 
>       !----------------------------------------------------------------
>       ! MPI merge of cagrad1 array
>       !----------------------------------------------------------------
> #ifdef MPI
>       if (nfgtasks.gt.1) then
>         call MPI_AllReduce(cagrad1(nnt),merge_buffer(nnt),nct-nnt+1,
>      &    MPI_DOUBLE_PRECISION,MPI_SUM,FG_COMM,IERR)
>         do i=nnt,nct
>           cagrad1(i)=merge_buffer(i)
>         enddo
>       endif
> #endif
> 
> #ifdef ESAXSLOG
>       do i=nnt,nct
> 1       write(*,'(a,i4,1f18.12)')'ESX3y',i,cagrad1(i)
>         write(*,'(a,i4,3f18.12)')'ESX3z',i,gsaxsc(:,i)
>       enddo
> #endif
> 
>       !----------------------------------------------------------------
>       ! fourth loop:
>       ! - update gsaxsc by CACAgrad1
>       ! TODO: dij squared fast check
>       ! TODO: single multiplication by fprimcont
>       ! TODO: radtabprimij outside loop
>       ! TODO: merge lgsaxsc(:,i) updates
>       ! TODO: precalculated restok(itype(i)) * 0.5 + 0.5
>       ! TODO: (???) premultiply radtabprimij/ji by cagrad1(i)/cagrad1(j)
>       !----------------------------------------------------------------
> !     do i=nnt,nct
> !       if (itype(i).eq.ntyp1) cycle
> !       do j=i+1,nct
> !         if (itype(j).eq.ntyp1) cycle
> !         dij=dist(c(1,i),c(1,j))
> !         dij0=0.5d0*(restok(itype(i))+restok(itype(j)))+1.0d0
> !         call gcont(dij,dij0,1.0d0,0.1d0,fcont,fprimcont)
> !         if(fcont.gt.0.0d0) then
> !           radtabprimij=xradprimtab(i)*fprimcont
> !           radtabprimji=xradprimtab(j)*fprimcont
> !           aux=(cagrad1(i)*radtabprimij+cagrad1(j)*radtabprimji)
> !    &        *(C(:,j)-C(:,i))/dij
> !           lgsaxsc(:,i)=lgsaxsc(:,i)-aux
> !           lgsaxsc(:,j)=lgsaxsc(:,j)+aux
> !         endif
> !       enddo ! j
> !     enddo ! i
> 
> !$OMP PARALLEL IF(threads_used.gt.1)
> !$OMP& NUM_THREADS(threads_used) DEFAULT(SHARED)
> !$OMP& PRIVATE(
> !$OMP&   i,j,jj,ikont,jblock,from_j,to_j,
> !$OMP&   from_ik,to_ik,first_blk,last_blk,first_j,last_j,
> !$OMP&   my_thread,from_blk,to_blk,
> !$OMP&   dij,dij0,sum_dsci,fcont,fprimcont,aux,aux2,x2,x,lo,hi,idij
> !$OMP& )
> #ifdef _OPENMP
>       my_thread=omp_get_thread_num()+1
> #else
>       my_thread=1
> #endif
> !     do my_thread=1,threads_used
> 
>       from_ik=saxs_range(my_thread).rng_start
>       to_ik=saxs_range(my_thread).rng_end
>       first_blk=saxs_range(my_thread).rng_first
>       last_blk=saxs_range(my_thread).rng_last
>       first_j=saxs_range(my_thread).rng_first_j
>       last_j=saxs_range(my_thread).rng_last_j
> 
>       do ikont=from_ik,to_ik
>         i=saxscontlisti(1,ikont)
>         from_blk=saxscontlisti(2,ikont-1)+1
>         to_blk=saxscontlisti(2,ikont)
> 
>         if(ikont.eq.from_ik) from_blk=first_blk
>         if(ikont.eq.to_ik) to_blk=last_blk
> 
>         sum_dsci=0.0d0
> 
>         do jblock=from_blk,to_blk
>           from_j=saxscontlistj(1,jblock)
>           to_j=saxscontlistj(2,jblock)
>           if(jblock.eq.first_blk) from_j=first_j
>           if(jblock.eq.last_blk) to_j=last_j
> 
>           do j=from_j,to_j
>             if (j.eq.i. or. itype(j).eq.ntyp1) cycle
>             !---
>             !dij=dist(c(1,i),c(1,j))
>             aux2=dist_sq(c(1,i),c(1,j))
>             !dij0=0.5d0*(restok(itype(i))+restok(itype(j)))+1.0d0
>             dij0=restok2(itype(i))+restok2(itype(j))
>             !call gcont(dij,dij0,1.0d0,0.1d0,fcont,fprimcont)
>             ! |dist**2 - dij0**2 - delta^2| > 2 * delta * dij0
>             if(abs(aux2-dij0*dij0-0.01d0).ge.0.2d0*dij0) cycle
> 
>             dij=sqrt(aux2)
>             !idij=1.0d0/sqrt(aux2)
>             !x=(dij-dij0)*10.0d0
>             x=dij-dij0
>             !x=(aux2*idij-dij0)*10.0d0
>             x2=x*x
>             !fprimcont=-9.375D0+18.750D0*x2-9.375D0*x2*x2
>             fprimcont=-9.375D0+1875.0D0*x2-93750.0D0*x2*x2
>             aux2=cagrad1(i)*xradprimtab(i)+cagrad1(j)*xradprimtab(j)
>             aux2=aux2*fprimcont/dij
>             aux(1)=aux2*(C(1,j)-C(1,i))
>             aux(2)=aux2*(C(2,j)-C(2,i))
>             aux(3)=aux2*(C(3,j)-C(3,i))
>             sum_dsci(1)=sum_dsci(1)-aux(1)
>             sum_dsci(2)=sum_dsci(2)-aux(2)
>             sum_dsci(3)=sum_dsci(3)-aux(3)
> !            gsaxsc(1,j)=gsaxsc(1,j)+aux(1)
> !            gsaxsc(2,j)=gsaxsc(2,j)+aux(2)
> !            gsaxsc(3,j)=gsaxsc(3,j)+aux(3)
>             workspace(my_thread).item.dgradbufcx(j)=
>      &        workspace(my_thread).item.dgradbufcx(j)+aux(1)*wsaxs
>             workspace(my_thread).item.dgradbufcy(j)=
>      &        workspace(my_thread).item.dgradbufcy(j)+aux(2)*wsaxs
>             workspace(my_thread).item.dgradbufcz(j)=
>      &        workspace(my_thread).item.dgradbufcz(j)+aux(3)*wsaxs
>             !---
>           enddo
>         enddo  ! jblock
> 
> !        gsaxsc(1,i)=gsaxsc(1,i)+sum_dsci(1)
> !        gsaxsc(2,i)=gsaxsc(2,i)+sum_dsci(2)
> !        gsaxsc(3,i)=gsaxsc(3,i)+sum_dsci(3)
>         workspace(my_thread).item.dgradbufcx(i)=
>      &    workspace(my_thread).item.dgradbufcx(i)+sum_dsci(1)*wsaxs
>         workspace(my_thread).item.dgradbufcy(i)=
>      &    workspace(my_thread).item.dgradbufcy(i)+sum_dsci(2)*wsaxs
>         workspace(my_thread).item.dgradbufcz(i)=
>      &    workspace(my_thread).item.dgradbufcz(i)+sum_dsci(3)*wsaxs
>       enddo  ! ikont
> 
>       ! interactions in separate pairs
>       from_ik=saxs_range(my_thread).sep_start
>       to_ik=saxs_range(my_thread).sep_end
>       first_j=saxs_range(my_thread).sep_first
>       last_j=saxs_range(my_thread).sep_last
> 
>       do i=from_ik,to_ik
>         from_j=saxsseplisti(i-1)+1
>         to_j=saxsseplisti(i)
> 
>         sum_dsci=0.0d0
> 
>         if(i.eq.from_ik) from_j=first_j
>         if(i.eq.to_ik) to_j=last_j
>         if (from_j.gt.to_j) cycle
>         do jj=from_j,to_j
>           j=saxsseplistj(jj)
>           if (j.eq.i. or. itype(j).eq.ntyp1) cycle
>           !---
>           aux2=dist_sq(c(1,i),c(1,j))
>           !dij0=0.5d0*(restok(itype(i))+restok(itype(j)))+1.0d0
>           dij0=restok2(itype(i))+restok2(itype(j))
>           !call gcont(dij,dij0,1.0d0,0.1d0,fcont,fprimcont)
>           if(abs(aux2-dij0*dij0-0.01d0).ge.0.2d0*dij0) cycle
>           dij=sqrt(aux2)
>           !x=(dij-dij0)*10.0d0
>           x=dij-dij0
>           x2=x*x
>           !fprimcont=-9.375D0+18.750D0*x2-9.375D0*x2*x2
>           fprimcont=-9.375D0+1875.0D0*x2-93750.0D0*x2*x2
>           aux2=cagrad1(i)*xradprimtab(i)+cagrad1(j)*xradprimtab(j)
>           aux2=aux2*fprimcont/dij
>           aux(1)=aux2*(C(1,j)-C(1,i))
>           aux(2)=aux2*(C(2,j)-C(2,i))
>           aux(3)=aux2*(C(3,j)-C(3,i))
>           sum_dsci(1)=sum_dsci(1)-aux(1)
>           sum_dsci(2)=sum_dsci(2)-aux(2)
>           sum_dsci(3)=sum_dsci(3)-aux(3)
> !          gsaxsc(1,j)=gsaxsc(1,j)+aux(1)
> !          gsaxsc(2,j)=gsaxsc(2,j)+aux(2)
> !          gsaxsc(3,j)=gsaxsc(3,j)+aux(3)
>           workspace(my_thread).item.dgradbufcx(j)=
>      &      workspace(my_thread).item.dgradbufcx(j)+aux(1)*wsaxs
>           workspace(my_thread).item.dgradbufcy(j)=
>      &      workspace(my_thread).item.dgradbufcy(j)+aux(2)*wsaxs
>           workspace(my_thread).item.dgradbufcz(j)=
>      &      workspace(my_thread).item.dgradbufcz(j)+aux(3)*wsaxs
>           !---
>         enddo
> 
> !        gsaxsc(1,i)=gsaxsc(1,i)+sum_dsci(1)
> !        gsaxsc(2,i)=gsaxsc(2,i)+sum_dsci(2)
> !        gsaxsc(3,i)=gsaxsc(3,i)+sum_dsci(3)
>         workspace(my_thread).item.dgradbufcx(i)=
>      &    workspace(my_thread).item.dgradbufcx(i)+sum_dsci(1)*wsaxs
>         workspace(my_thread).item.dgradbufcy(i)=
>      &    workspace(my_thread).item.dgradbufcy(i)+sum_dsci(2)*wsaxs
>         workspace(my_thread).item.dgradbufcz(i)=
>      &    workspace(my_thread).item.dgradbufcz(i)+sum_dsci(3)*wsaxs
>       enddo  ! i
> !     enddo !my_thread
> !$OMP END PARALLEL
>       
> !     call omp_sum_saxs_energies
> 
> !#ifdef ESAXSLOG
> !     do i=nnt,nct
> !       write(*,'(a,i4,3f18.12)')'ESX4z',i,gsaxsc(:,i)
> !     enddo
> !#endif
> 
> !     do i=nnt,nct
> !       write (*,'(a,i5,3f18.10,4f19.14)')'GSC',i,lgsaxsc(:,i),
> !    &    maxval(lgsaxsc(:,i)-gsaxsC(:,i)),
> !    &    lgsaxsc(:,i)-gsaxsC(:,i)
> !     enddo
> !     do i=nnt,nct
> !       gsaxsC(:,i)=lgsaxsc(:,i)
> !     enddo
> #ifdef TIMING
> #ifdef MPI
>       time_SAXS=time_SAXS+MPI_Wtime()-time01
> #else
>       time_SAXS=time_SAXS+tcpu()
> #endif
> #endif
450c1437
<         logPtot = logPtot - dlog(PcalcC) 
---
>         logPtot = logPtot - dlog(PcalcC)
455c1442
<       if (nfgtasks.gt.1) then 
---
>       if (nfgtasks.gt.1) then
483a1471
> 
500a1489
> 
520a1510,2005
> C-----------------------------------------------------------------------
>       subroutine calculate_saxs_interactions
>       use omp_lib
>       use grid_arrays
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       include 'COMMON.INTERACT'
>       include 'COMMON.OMP'
>       include 'COMMON.GRID'
>       include 'COMMON.SAXSGRID'
>       double precision r_buff
>       integer max_threads,i,j
>       logical need_lists
> 
> #ifdef _OPENMP
>       max_threads=omp_get_max_threads()
> #else
>       max_threads=1
> #endif
> 
>       call mark_saxs_grid_iteration
>       call check_saxs_list_needed(need_lists)
>       if (need_lists) then
> #ifdef ESAXSLOG
>         write(*,*)'cpu SAXS list rebuid',saxs_single_grid
>         write(*,*)'cpu SAXS GS',saxsgridsize
>         write(*,*)'cpu SAXS CS',saxscellsize
> #endif
> 
>         ! build grid
>         if(.not.saxs_single_grid) then
>           call grid_tune_saxs_lists(r_buff,saxsgridsize,saxscellsize)
>           call calculate_nb_grid(c_tobox(1,1),abs_itype,nres,
>      &      gridcellsaxs,dgridrangesaxs,griddatasaxs,
>      &      saxsgridsize,saxscellsize)
>         else
>           call build_single_cell_grid(c_tobox(1,1),abs_itype,nres,
>      &      gridcellsaxs,dgridrangesaxs,griddatasaxs)
>         endif
>         saxs_first_time_grid=.false.
> 
> !       call dump_grid(nres,gridcellsaxs,dgridrangesaxs,
> !    &    griddatasaxs,saxsgridsize)
> 
>         ! build interactions
>         saxs_threads=max_threads
>         call make_split_inter_list(1,nres,iatsc_s,iatsc_e,
>      &    nint_gr,istart,iend,
>      &    c_tobox(1,1),c_tobox(1,1),abs_itype,abs_itype,4,
>      &    saxscontlisti,saxscontlistj,
>      &    saxsseplisti,saxsseplistj,
>      &    gridcellsaxs,dgridrangesaxs,griddatasaxs,0,
>      &    saxs_threads,saxs_range,
>      &    saxs_grid_cutoff,r_buff_saxs_list)
> 
> !       call dump_interactions('SAXS  ',saxs_range,
> !    &   saxscontlisti,saxscontlistj,
> !    &   saxsseplisti,saxsseplistj,saxs_threads)
>       endif
>       return
>       end subroutine
> 
> c-----------------------------------------------------------------------
>       subroutine check_saxs_list_needed(update_lists)
>       implicit none
> #ifdef MPI
>       include 'mpif.h'
> #endif
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       include 'COMMON.TIME1'
>       include 'COMMON.OMP'
>       include 'COMMON.GRID'
>       include 'COMMON.SAXSGRID'
>       logical update_lists
>       integer i
>       double precision maxd,d
>       double precision dist_sq
>       double precision time00
> 
> #ifdef MPI
>       time00=MPI_Wtime()
> #endif
> 
>       update_lists=.false.
>       if(saxs_first_time_grid) update_lists=.true.
> 
>       if(.not. update_lists) then
>         maxd=0
> !$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(d) REDUCTION(max:maxd)
>         do i=1,nres
>           call dist_sq2(saxs_base_c(:,i),c_tobox(:,i),d)
> #ifdef ESAXSLOG
>           !write(*,'(a,i4,7f16.8)')'SL',i,d,saxs_base_c(:,i),c_tobox(:,i)
> #endif
>           maxd=max(d,maxd)
>         enddo
> 
> #ifdef ESAXSLOG
>         !write(*,*)'cpu saxs maxd', maxd
> #endif
> 
>         if(maxd.gt.(0.25d0*r_buff_saxs_list*r_buff_saxs_list)) then
>           update_lists=.true.
>         endif
>       endif
> 
>       if(update_lists) then
> !$OMP PARALLEL DO DEFAULT(SHARED)
>         do i=1,nres
>           saxs_base_c(:,i)=c_tobox(:,i)
>         enddo
>       endif
> #ifdef MPI
>       time_saxs_list=time_saxs_list+MPI_Wtime()-time00
> #endif
> 
>       end
> 
> c-----------------------------------------------------------------------
>       subroutine calculate_saxs_cutoff
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.SAXS'
> #ifdef LANG0
>       include 'COMMON.LANGEVIN.lang0.5diag'
> #else
>       include 'COMMON.LANGEVIN'
> #endif
>       include 'COMMON.OMP'
>       include 'COMMON.NEWBOX'
>       include 'COMMON.SAXSGRID'
>       double precision maxrestok,maxdk
>       integer i
>       saxs_grid_cutoff=0.0d0
>       !if (nsaxs.gt.0 .and. saxs_mode.eq.0) then
>       if (nsaxs.gt.0) then
> !       do i=1,nsaxs
> !         write(*,*)'DS',i,distsaxs(i)
> !       enddo
> !       do i=1,ntyp
> !         write(*,*)'RT',i,restok(i)
> !       enddo
>         maxrestok=maxval(restok(1:ntyp))
>         maxdk=maxval(distsaxs(1:nsaxs))
> 
>         saxs_grid_cutoff=max(
>      &    saxs_cutoff*maxrestok+maxdk,
>      &    maxrestok+1.0d0+10.0d0)
> !       write(*,*)'A',maxrestok+1.0d0+10.0d0
> !       write(*,*)'B',saxs_cutoff*maxrestok+maxdk,
> !    &    saxs_cutoff,maxrestok,maxdk
> 
> !       write(*,*)'saxs_grid_cutoff',saxs_grid_cutoff
>         if(saxs_cutoff.eq.0.0d0) then
>           saxs_single_grid=.true.
>           saxs_grid_cutoff=boxsize(1)+boxsize(2)+boxsize(3)
>         endif
> #ifdef ESAXSLOG
>         write(*,*)'cpu saxs_grid_cutoff',saxs_grid_cutoff
> #endif
>       endif
>       end subroutine
> 
> c-----------------------------------------------------------------------
>       subroutine add_expCACA_to_Pcalc(i,j,dij,radtab,lPcalc)
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.INTERACT'
>       include 'COMMON.SAXS'
> #ifdef LANG0
>       include 'COMMON.LANGEVIN.lang0.5diag'
> #else
>       include 'COMMON.LANGEVIN'
> #endif
>       include 'COMMON.OMP'
>       double precision radtab(maxres)
>       double precision lPcalc(maxSAXS)
>       double precision dij,expCACA,sigma2CACA,log_dijCACA,diffd
>       double precision sss2,rrr,dk,iradtabij,rr,gamm
>       double precision sscale2
>       integer i,j,k
>       double precision lrlamb,lirlamb
>       parameter (lrlamb=0.3d0)
>       parameter (lirlamb=1.0d0/0.3d0)
> 
>       iradtabij=1.0d0/(radtab(i)+radtab(j))
>       !sigma2CACA=4*(dij*dij)/(radtab(i)+radtab(j))**2
>       !sigma2CACA=4*dij*dij*iradtabij*iradtabij
>       sigma2CACA=-2.0d0*dij*dij*iradtabij*iradtabij
>       log_dijCACA=dlog(dij)
>       if (saxs_cutoff.eq.0.0d0) then
>         do k=1,nsaxs
>           !dk=distsaxs(k)
>           diffd=log_dijCACA-log_distsaxs(k)
>           !expCACA=dexp(-0.5d0*sigma2CACA*diffd*diffd)
>           expCACA=dexp(sigma2CACA*diffd*diffd)
>           !write(*,'(a,3i4,1f18.12)')'ESX2b',i,j,k,expCACA
>           lPcalc(k)=lPcalc(k)+expCACA
>         enddo ! k
>       else
>         !rrr=saxs_cutoff*(restok(itype(i))+restok(itype(j)))*0.5
>         rrr=restok_co(itype(i))+restok_co(itype(j))
>         do k=1,nsaxs
>           dk=distsaxs(k)
>           diffd=log_dijCACA-log_distsaxs(k)
>           !sss2=sscale2(dij,rrr,dk,0.3d0)
>           !double precision function sscale2(dij,rrr,dk,lrlamb)
>           rr = dabs(dij-dk)
>           if(rr.gt.rrr) cycle
>           !if(rr.lt.rrr-lrlamb) then
>           !  sss2=1.0d0
>           !else if(rr.le.rrr.and.rr.ge.rrr-lrlamb) then
>           !else
>           sss2=1.0d0
>           if(rr.ge.rrr-lrlamb) then
>             !gamm=(rr-(rrr-lrlamb))/lrlamb
>             gamm=(rr-(rrr-lrlamb))*lirlamb
>             sss2=sss2+gamm*gamm*(2*gamm-3.0d0)
>           !else
>           !  sss2=0d0
>           endif
>           !if (sss2.eq.0.0d0) cycle
>           !expCACA=dexp(-0.5d0*sigma2CACA*diffd*diffd)*sss2
>           expCACA=dexp(sigma2CACA*diffd*diffd)*sss2
>           !write(*,'(a,3i4,1f18.12)')'ESX2',i,j,k,expCACA
>           lPcalc(k)=lPcalc(k)+expCACA
>         enddo ! k
>       endif
>       end subroutine
> 
> c-----------------------------------------------------------------------
>       subroutine saxs_grad_first_step(i,j,dij,radtab,fac,
>      &  dgrad1,dgsc)
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       include 'COMMON.INTERACT'
>       include 'COMMON.SAXS'
> #ifdef LANG0
>       include 'COMMON.LANGEVIN.lang0.5diag'
> #else
>       include 'COMMON.LANGEVIN'
> #endif
>       include 'COMMON.OMP'
>       integer i,j,k
>       double precision radtab(maxres)
>       double precision fac(maxSAXS)
>       double precision dgrad1,dgsc(3),saux,sgrad
>       double precision dij,expCACA,sigma2CACA,log_dijCACA,diffd
>       double precision iradtabij,saux2,mhalfsigma2CACA,aux2
>       double precision rr,gamm,gamm2
>       double precision sss2,ssgrad2,rrr,dk,CACAgrad,CACAgrad1
>       double precision sscale2,sscalgrad2
>       double precision lrlamb,lirlamb
>       parameter (lrlamb=0.3d0)
>       parameter (lirlamb=1.0d0/0.3d0)
> 
>       iradtabij=1.0d0/(radtab(i)+radtab(j))
>       sigma2CACA=4*dij*iradtabij*iradtabij
>       mhalfsigma2CACA=-0.5d0*dij*sigma2CACA
>       log_dijCACA=dlog(dij)
>       saux=0.0d0
>       sgrad=0.0d0
>       saux2=0.0d0
>       if (saxs_cutoff.eq.0.0d0) then
>         do k=1,nsaxs
>           diffd=log_dijCACA-log_distsaxs(k)
>           expCACA=dexp(mhalfsigma2CACA*diffd*diffd)*fac(k)
>           sgrad=sgrad-diffd*diffd*expCACA
>           saux=saux+diffd*(diffd+1.0d0)*expCACA
>         enddo ! k
>       else
>         !rrr = saxs_cutoff*(restok(itype(i))+restok(itype(j)))/2
>         rrr = restok_co(itype(i))+restok_co(itype(j))
>         do k=1,nsaxs
>           dk = distsaxs(k)
>           diffd = log_dijCACA-log_distsaxs(k)
>           rr = dabs(dij-dk)
>           if(rr.gt.rrr) cycle
>           if(rr.lt.rrr-lrlamb) then
>             gamm=0.0d0
>             sss2=1.0d0
>             ssgrad2=0.0d0
>           else
>             gamm=(rr-(rrr-lrlamb))*lirlamb
>             gamm2=gamm*gamm
>             !sss2=1.0d0+gamm*gamm*(2*gamm-3.0d0)
>             sss2=1.0d0+gamm2*(2*gamm-3.0d0)
>             !if (dij.ge.dk) ssgrad2=gamm*(6*gamm-6.0d0)*lirlamb
>             !else ssgrad2=-gamm*(6*gamm-6.0d0)*lirlamb
>             !ssgrad2=gamm*(6.0d0*gamm-6.0d0)*lirlamb*sign(1.0d0,dij-dk)
>             ssgrad2=(gamm2-gamm)*lirlamb*sign(6.0d0,dij-dk)
>           endif
>           expCACA=dexp(mhalfsigma2CACA*diffd*diffd)*fac(k)
>           saux2=saux2-expCACA*ssgrad2
>           aux2=diffd*expCACA*sss2
>           sgrad=sgrad-aux2*diffd
>           saux=saux+aux2*(diffd+1.0d0)
>         enddo ! k
>       endif
>       dgrad1=sgrad*dij*sigma2CACA*iradtabij
>       dgsc(:)=(sigma2CACA*saux+saux2)/dij*(C(:,j)-C(:,i))
>       end subroutine
> 
> C-----------------------------------------------------------------------
>       subroutine init_saxs
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.SAXS'
> #ifdef LANG0
>       include 'COMMON.LANGEVIN.lang0.5diag'
> #else
>       include 'COMMON.LANGEVIN'
> #endif
>       include 'COMMON.OMP'
>       include 'COMMON.SAXSGRID'
>       integer k
> 
>       saxs_grid_cutoff=0.0
>       saxs_single_grid=.false.
>       saxs_first_time_grid=.true.
>       saxs_grid_cells=1
>       saxsgridsize(1)=1
>       saxsgridsize(2)=1
>       saxsgridsize(3)=1
>       if(nsaxs.eq.0) return
> 
>       call calculate_saxs_cutoff
> 
>       do k=1,nsaxs
>         log_distsaxs(k)=dlog(distsaxs(k))
>       enddo
>       do k=1,ntyp
>         restok2(k)=0.5d0*restok(k)+0.5d0
>         restok_co(k)=0.5d0*restok(k)*saxs_cutoff
>       enddo
>       end subroutine
> 
> c-----------------------------------------------------------------------
>       subroutine omp_sum_saxs_energies
>       use omp_lib
>       use omp_data
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       include 'COMMON.DERIV'
>       include 'COMMON.FFIELD'
>       include 'COMMON.OMP'
>       double precision ggg(3)
>       integer i,j,from,to
>       integer threads_used,my_thread,delta,th_base,th_offset,rng
> 
>       threads_used=omp_get_max_threads()
> 
>       if(threads_used.le.numa_node_size) then
>         delta=1
>       else
>         delta=numa_node_size
> !$OMP PARALLEL NUM_THREADS(threads_used) DEFAULT(SHARED)
> !$OMP& PRIVATE(from,to,i,j,ggg,my_thread,th_base,th_offset,rng)
>         my_thread=omp_get_thread_num()
>         th_offset=mod(my_thread,delta)
>         th_base=my_thread-th_offset
>         rng=min(delta,threads_used-th_base)
> 
>         my_thread=my_thread+1
>         th_offset=th_offset+1
> 
>         call split_work_for_threads(from,to,0,nres,th_offset,rng)
> 
>         do j=from,to
>           ggg=0.0d0
>           do i=1,rng
>             ggg(1)=ggg(1)+workspace(th_base+i).item.dgradbufcx(j)
>             ggg(2)=ggg(2)+workspace(th_base+i).item.dgradbufcy(j)
>             ggg(3)=ggg(3)+workspace(th_base+i).item.dgradbufcz(j)
>           enddo
>           workspace(th_base+1).item.dgradbufcx(j)=ggg(1)
>           workspace(th_base+1).item.dgradbufcy(j)=ggg(2)
>           workspace(th_base+1).item.dgradbufcz(j)=ggg(3)
>         enddo
> !$OMP END PARALLEL
>       endif
> 
> !$OMP PARALLEL NUM_THREADS(threads_used) DEFAULT(SHARED)
> !$OMP& PRIVATE(from,to,i,j,ggg,my_thread)
>       my_thread=omp_get_thread_num()+1
> 
>       call split_work_for_threads(from,to,0,nres,my_thread,threads_used)
> 
>       do j=from,to
>         ggg=0.0d0
>         do i=1,threads_used,delta
>           ggg(1)=ggg(1)+workspace(i).item.dgradbufcx(j)
>           ggg(2)=ggg(2)+workspace(i).item.dgradbufcy(j)
>           ggg(3)=ggg(3)+workspace(i).item.dgradbufcz(j)
>         enddo
> #ifdef ESAXSLOG
>         write(*,'(a,i4,3f18.12)')'ESX4z',j,ggg(:)
> #endif
>         omp_dgradbufc(:,j)=omp_dgradbufc(:,j)+ggg*wsaxs
>       enddo
> !$OMP END PARALLEL
> 
> !$OMP PARALLEL NUM_THREADS(threads_used) DEFAULT(SHARED)
> !$OMP& PRIVATE(my_thread)
>       my_thread=omp_get_thread_num()+1
>       workspace(my_thread).item.dgradbufcx(-1:nres)=0.0d0
>       workspace(my_thread).item.dgradbufcy(-1:nres)=0.0d0
>       workspace(my_thread).item.dgradbufcz(-1:nres)=0.0d0
> !$OMP END PARALLEL
> 
>       esaxs_group_results=.true.
>       return
>       end
> 
> c-----------------------------------------------------------------------
>       subroutine omp_sum_saxs_data(res,low,high)
>       use omp_lib
>       use omp_data
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       include 'COMMON.DERIV'
>       include 'COMMON.FFIELD'
>       include 'COMMON.OMP'
>       double precision res(maxres)
>       double precision ggg
>       integer i,j,from,to,low,high
>       integer threads_used,my_thread,delta,th_base,th_offset,rng
> 
>       threads_used=omp_get_max_threads()
> 
>       if(threads_used.le.numa_node_size) then
>         delta=1
>       else
>         delta=numa_node_size
> !$OMP PARALLEL NUM_THREADS(threads_used) DEFAULT(SHARED)
> !$OMP& PRIVATE(from,to,i,j,ggg,my_thread,th_base,th_offset,rng)
>         my_thread=omp_get_thread_num()
>         th_offset=mod(my_thread,delta)
>         th_base=my_thread-th_offset
>         rng=min(delta,threads_used-th_base)
> 
>         my_thread=my_thread+1
>         th_offset=th_offset+1
> 
>         call split_work_for_threads(from,to,low,high,th_offset,rng)
> 
>         do j=from,to
>           ggg=0.0d0
>           do i=1,rng
>             ggg=ggg+workspace(th_base+i).item.saxs_data(j)
>           enddo
>           workspace(th_base+1).item.saxs_data(j)=ggg
>         enddo
> !$OMP END PARALLEL
>       endif
> 
> !$OMP PARALLEL NUM_THREADS(threads_used) DEFAULT(SHARED)
> !$OMP& PRIVATE(from,to,i,j,ggg,my_thread)
>       my_thread=omp_get_thread_num()+1
> 
>       call split_work_for_threads(from,to,low,high,
>      &   my_thread,threads_used)
> 
>       do j=from,to
>         ggg=0.0d0
>         do i=1,threads_used,delta
>           ggg=ggg+workspace(i).item.saxs_data(j)
>         enddo
>         res(j)=ggg
>       enddo
> !$OMP END PARALLEL
> 
> !$OMP PARALLEL NUM_THREADS(threads_used) DEFAULT(SHARED)
> !$OMP& PRIVATE(my_thread)
>       my_thread=omp_get_thread_num()+1
>       call clear_saxs_arrays(my_thread)
> !$OMP END PARALLEL
> 
>       return
>       end
> 
> C-----------------------------------------------------------------------
>       subroutine clear_saxs_arrays(my_thread)
>       use omp_data
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       integer my_thread
>       workspace(my_thread).item.saxs_data(-1:nres)=0.0d0
>       return
>       end
esc-diag.f
escp.F
3a4
>       include 'COMMON.DERIV'
4a6,7
>       include 'COMMON.FFIELD'
>       include 'COMMON.OMP'
6c9,10
<       if(expon2.eq.3)then
---
> 
>       if (expon2.eq.3) then
10a15,18
>       evdw2=evdw2/(wscp*expon)
>       evdw2_14=evdw2_14/(wscp*expon)
> 
>       escp_group_results=.true.
13,14c21,22
< C-------------------------------------------------------------------------------
<       subroutine clear_escp_values(num_threads)
---
> C-----------------------------------------------------------------------
>       subroutine clear_escp_values(threads_used)
16a25
>       include 'COMMON.OMP'
18,19c27,28
<       integer i,num_threads
<       do i=1,num_threads
---
>       integer i,threads_used
>       do i=1,threads_used
25,26c34,35
< C-------------------------------------------------------------------------------
<       subroutine sum_escp_values(evdw2,evdw2_14,num_threads)
---
> C-----------------------------------------------------------------------
>       subroutine sum_escp_values(evdw2,evdw2_14,threads_used)
28a38
>       include 'COMMON.OMP'
31c41
<       integer i,num_threads
---
>       integer i,threads_used
34c44
<       do i=1,num_threads
---
>       do i=1,threads_used
39a50
>       evdw2=evdw2+evdw2_14
42,57c53,55
< C-------------------------------------------------------------------------------
<       subroutine clear_escp_arrays(num_threads)
<       implicit none
<       include 'DIMENSIONS'
<       include 'COMMON.ESCP'
<       integer i,j,num_threads
<       do i=1,num_threads
<         do j=-1,maxres
<           escp_result(j,i).scp=0.0d0
<           escp_result(j,i).scpp=0.0d0
<         enddo
<       enddo
<       return
<       end
< C-------------------------------------------------------------------------------
<       subroutine sum_escp_arrays(num_threads,dexpon)
---
> 
> C-----------------------------------------------------------------------
>       subroutine init_escp
64c62
<       include 'COMMON.DERIV'
---
>       include 'COMMON.LOCAL'
65a64,65
>       include 'COMMON.FFIELD'
>       include 'COMMON.OMP'
67,69c67,70
<       integer i,j,num_threads,my_thread,from,to,threads_used
<       double precision ggg(3)
<       double precision dexpon
---
>       integer k,i,j,last,blk,plus_count,minus_count,itypi,itypj
>       integer threads_used,current_thread,work_left
>       integer plus_per_thread,plus_tails
>       integer minus_per_thread,minus_tails
77,80c78,121
<       do i=1,num_threads
<         do j=-1,0
<           escp_result(j,i).scp=0.0d0
<           escp_result(j,i).scpp=0.0d0
---
>       do i=1,threads_used
>         scp_range(i).rng_start=0
>         scp_range(i).rng_end=-1
>       enddo
> 
>       call build_pairs_lists_for_threads(threads_used,
>      &                                   1,nct,iatscp_s,iatscp_e,2,
>      &                                   scp_plus_count,
>      &                                   scp_plus_blocks,2*maxchain,
>      &                                   abs_itel,ntyp1,
>      &                                   abs_itype,ntyp1,
>      &                                   iscpstart,iscpend,nscp_gr)
> 
>       call build_pairs_lists_for_threads(threads_used,
>      &                                   1,nct,iatscp_s,iatscp_e,-2,
>      &                                   scp_minus_count,
>      &                                   scp_minus_blocks,2*maxchain,
>      &                                   abs_itel,ntyp1,
>      &                                   abs_itype,ntyp1,
>      &                                   iscpstart,iscpend,nscp_gr)
>  
> !     write(*,*)'ranges',1,nct,iatscp_s,iatscp_e
> !     write(*,*)'plus blks', scp_plus_count(1:threads_used)
> !     do i=1,threads_used
> !       do j=1,scp_plus_count(i)
> !         write(*,*)'  blk',i,
> !    &      scp_plus_blocks(2*j+0:2*j+1,i)
> !       enddo
> !     enddo
> 
> !     write(*,*)'minus blks', scp_minus_count(1:threads_used)
> !     do i=1,threads_used
> !       do j=1,scp_minus_count(i)
> !         write(*,*)'  blk',i,
> !    &      scp_minus_blocks(2*j+0:2*j+1,i)
> !       enddo
> !     enddo
> 
>       do i=1,2
>         do j=1,ntyp
>           escp_aad(j,i)=aad(j,i)*wscp*expon
>           escp_bad(j,i)=bad(j,i)*wscp*expon
>           escp14_aad(j,i)=aad(j,i)*wscp*expon*scal14
>           escp14_bad(j,i)=bad(j,i)*wscp*expon*scal14
84,101c125,134
< !$OMP PARALLEL NUM_THREADS(threads_used) DEFAULT(SHARED)
< !$OMP& PRIVATE(from,to,i,j,my_thread,ggg)
< #ifdef _OPENMP
<       my_thread=omp_get_thread_num()+1
< #else
<       my_thread=1
< #endif
< 
<       !from=1+((nct-1)/threads_used)*(my_thread-1)
<       !to=1+((nct-1)/threads_used)*my_thread-1
<       !if(my_thread.eq.threads_used) to=nct
<       call split_work_for_threads(from,to,1,nct,my_thread,threads_used)
< 
<       do j=from,to
<         ggg=0.0d0
<         do i=1,num_threads
<           ggg=ggg+escp_result(j,i).scp
<           escp_result(j,i).scp=0.0d0
---
>       do i=1,2
>         do j=1,nres
>           itypj=abs_itype(j)
>           if (itypj.eq.ntyp1) then
>             escp_row_aad(j,i)=0.0d0
>             escp_row_bad(j,i)=0.0d0
>           else
>             escp_row_aad(j,i)=aad(itypj,i)*wscp*expon
>             escp_row_bad(j,i)=bad(itypj,i)*wscp*expon
>           endif
103d135
<         gvdwc_scp(:,j)=ggg*dexpon
105,118d136
< 
<       do j=from,to
<         ggg=0.0d0
<         do i=1,num_threads
<           ggg=ggg+escp_result(j,i).scpp
<           escp_result(j,i).scpp=0.0d0
<         enddo
<         gvdwc_scpp(:,j)=ggg*dexpon
<       enddo
< 
<       do j=from,to
<         gradx_scp(:,j)=gradx_scp(:,j)*dexpon
<       enddo
< !$OMP END PARALLEL
121,142c139,143
< C-------------------------------------------------------------------------------
<       subroutine escp_queue_work(chunk,idx,j,xyzj,rij,aad,bad,full)
<       implicit none
<       include 'DIMENSIONS'
<       include 'COMMON.ESCP'
<       integer idx,j
<       double precision xyzj(3),rij,aad,bad
<       logical full
<       record /escp_work_chunk/ chunk
<       idx=idx+1
<       chunk.j(idx)=j
<       chunk.x(idx)=xyzj(1)
<       chunk.y(idx)=xyzj(2)
<       chunk.z(idx)=xyzj(3)
<       chunk.r(idx)=rij
<       chunk.aad(idx)=aad
<       chunk.bad(idx)=bad
<       full=idx.eq.escp_buffer_size
<       return
<       end
< C-----------------------------------------------------------------------------
<       subroutine escp_move_work(from,from_idx,to,to_idx)
---
> 
> C-----------------------------------------------------------------------
>       subroutine dump_escp_interactions
>       use omp_lib
>       use omp_data
143a145,147
> #ifdef MPI
>       include 'mpif.h'
> #endif
144a149,151
>       include 'COMMON.CHAIN'
>       include 'COMMON.INTERACT'
>       include 'COMMON.OMP'
146,156c153,203
<       integer from_idx,to_idx,idx
<       record /escp_work_chunk/ from,to
<       do idx=1,from_idx
<         to_idx=to_idx+1
<         to.j(to_idx)=from.j(idx)
<         to.x(to_idx)=from.x(idx)
<         to.y(to_idx)=from.y(idx)
<         to.z(to_idx)=from.z(idx)
<         to.r(to_idx)=from.r(idx)
<         to.aad(to_idx)=from.aad(idx)
<         to.bad(to_idx)=from.bad(idx)
---
>       integer i,j,k,iteli,ikont,jkont,jblock
>       integer threads_used,my_thread
>       integer from_blk,to_blk,from_j,to_j
>       integer from_ik,to_ik,first_blk,last_blk,first_j,last_j
> 
>       do my_thread=1,scp_threads
> 
>         do j=1,scp_plus_count(my_thread)
>           do k=scp_plus_blocks(j*2+0,my_thread),
>      &         scp_plus_blocks(j*2+1,my_thread)
>             write(*,*)'SCP+2',k,k+2
>           enddo
>         enddo
>         do j=1,scp_minus_count(my_thread)
>           do k=scp_minus_blocks(j*2+0,my_thread),
>      &         scp_minus_blocks(j*2+1,my_thread)
>             write(*,*)'SCP-2',k,k-2
>           enddo
>         enddo
> 
>         !from_ik=scp_from_ik(my_thread)
>         !to_ik=scp_to_ik(my_thread)
>         !first_blk=scp_first_blk(my_thread)
>         !last_blk=scp_last_blk(my_thread)
>         !first_j=scp_first_j(my_thread)
>         !last_j=scp_last_j(my_thread)
>         from_ik=scp_range(my_thread).rng_start
>         to_ik=scp_range(my_thread).rng_end
>         first_blk=scp_range(my_thread).rng_first
>         last_blk=scp_range(my_thread).rng_last
>         first_j=scp_range(my_thread).rng_first_j
>         last_j=scp_range(my_thread).rng_last_j
> 
>         do ikont=from_ik,to_ik
>           i=newcontlistscpi(1,ikont)
>           from_blk=newcontlistscpi(2,ikont-1)+1
>           to_blk=newcontlistscpi(2,ikont)
> 
>           if(ikont.eq.from_ik) from_blk=first_blk
>           if(ikont.eq.to_ik) to_blk=last_blk
> 
>           iteli=itel(i)
> 
>           do jblock=from_blk,to_blk
>             from_j=newcontlistscpj(1,jblock)
>             to_j=newcontlistscpj(2,jblock)
>             do j=from_j,to_j
>               write(*,*)'SCP  ',i,j
>             enddo
>           enddo ! jblock
>         enddo  ! ikont
158d204
<       from_idx=0
161,189c207,208
< C-------------------------------------------------------------------------------
< !     subroutine escp_add_result(i,output,accum,iaccum,delta2)
< !     implicit none
< !     include 'DIMENSIONS'
< !     include 'COMMON.ESCP'
< !     integer i
< !     record /escp_result_item/ output(-1:maxres)
< !     record /escp_accumulator/ accum
< !     double precision iaccum(3),delta2,delta2_14
< !     output(i).scpp=output(i).scpp-iaccum
< !     accum.evdw2=accum.evdw2+delta2
< !     return
< !     end
< C-------------------------------------------------------------------------------
< !     subroutine escp_add_result_14(i,output,accum,iaccum,
< !    &                              delta2,delta2_14)
< !     implicit none
< !     include 'DIMENSIONS'
< !     include 'COMMON.ESCP'
< !     integer i
< !     record /escp_result_item/ output(-1:maxres)
< !     record /escp_accumulator/ accum
< !     double precision iaccum(3),delta2,delta2_14
< !     output(i).scpp=output(i).scpp-iaccum
< !     accum.evdw2=accum.evdw2+delta2
< !     accum.evdw2_14=accum.evdw2_14+delta2_14
< !     return
< !     end
< C----------------------------------------------------------------------
---
> 
> C-----------------------------------------------------------------------
192c211
< #define ESCP_WORK_NSEQ       escp_3_do_work_nonseq
---
> #define ESCP_WORK_NSEQ       escp_3_do_work_nseq
194d212
< #define ESCP_WORK_DROPOFF    escp_3_do_work_dropoff
197d214
< #undef ESCP_WORK_DROPOFF
206c223
< #define ESCP_WORK_NSEQ       escp_n_do_work_nonseq
---
> #define ESCP_WORK_NSEQ       escp_n_do_work_nseq
208d224
< #define ESCP_WORK_DROPOFF    escp_n_do_work_dropoff
211d226
< #undef ESCP_WORK_DROPOFF
escp_t.F
1,2d0
< #include"simdstat"
< 
4d1
< #ifdef _OPENMP
6c3
< #endif
---
>       use omp_data
28d24
<       include 'COMMON.ESCP'
30,37c26,28
<       include 'COMMON.NEWBOX'
<       double precision xyzj(3),rij
<       double precision dexpon,r_dropoff_sq
<       double precision evdw2,evdw2_14,evdwij
<       double precision box(3),halfbox(3)
< 
<       record /escp_work_chunk/ seq_work,nseq_work,droff_work,e14_work
<       integer seq_idx,nseq_idx,droff_idx,e14_idx
---
>       include 'COMMON.ESCP'
>       double precision dexpon
>       double precision evdw2,evdw2_14
39,40c30
<       integer i,j,iteli,itypj,ikont,jkont,jblock,lastj
<       logical full,limited
---
>       integer i,j,iteli,ikont,jkont,jblock
46d35
<       double precision scalar
51c40
<       threads_used = scp_threads
---
>       threads_used=scp_threads
56,61c45,47
< !$OMP&   i,j,iteli,itypj,ikont,jkont,jblock,lastj,xyzj,rij,
< !$OMP&   seq_work,nseq_work,droff_work,e14_work,full,r_dropoff_sq,
< !$OMP&   seq_idx,nseq_idx,droff_idx,e14_idx,my_thread,
< !$OMP&   box,halfbox,
< !$OMP&   from_ik,to_ik,first_blk,last_blk,first_j,last_j,
< !$OMP&   from_blk,to_blk,from_j,to_j
---
> !$OMP&   i,j,iteli,ikont,jkont,jblock,
> !$OMP&   from_blk,to_blk,from_j,to_j,my_thread,
> !$OMP&   from_ik,to_ik,first_blk,last_blk,first_j,last_j
63d48
<       r_dropoff_sq=r_dropoff*r_dropoff
70,84c55,85
<       lastj=0
<       seq_idx=0
<       nseq_idx=0
<       droff_idx=0
<       e14_idx=0
< 
<       from_ik=scp_from_ik(my_thread)
<       to_ik=scp_to_ik(my_thread)
<       first_blk=scp_first_blk(my_thread)
<       last_blk=scp_last_blk(my_thread)
<       first_j=scp_first_j(my_thread)
<       last_j=scp_last_j(my_thread)
< 
<       box=boxsize
<       halfbox=halfboxsize
---
>       ! do the (i,i+2) pairs
>       do j=1,scp_plus_count(my_thread)
> !!DIR$ NOINLINE   ! uncomment when compiling with DEBUG_FLAGS=-g
>         call ESCP_WORK_14(2,
>      &    scp_plus_blocks(j*2+0,my_thread),
>      &    scp_plus_blocks(j*2+1,my_thread),
>      &    workspace(my_thread).item,
>      &    escp_accum(my_thread).evdw2_14,
>      &    escp_row_aad(:,1),escp_row_bad(:,1),
>      &    escp_row_aad,escp_row_bad)
>       enddo
>       ! do the (i,i-2) pairs
> !$OMP BARRIER
>       do j=1,scp_minus_count(my_thread)
> !!DIR$ NOINLINE   ! uncomment when compiling with DEBUG_FLAGS=-g
>         call ESCP_WORK_14(-2,
>      &    scp_minus_blocks(j*2+0,my_thread),
>      &    scp_minus_blocks(j*2+1,my_thread),
>      &    workspace(my_thread).item,
>      &    escp_accum(my_thread).evdw2_14,
>      &    escp_row_aad(:,1),escp_row_bad(:,1),
>      &    escp_row_aad,escp_row_bad)
>       enddo
> !$OMP BARRIER
> 
>       from_ik=scp_range(my_thread).rng_start
>       to_ik=scp_range(my_thread).rng_end
>       first_blk=scp_range(my_thread).rng_first
>       last_blk=scp_range(my_thread).rng_last
>       first_j=scp_range(my_thread).rng_first_j
>       last_j=scp_range(my_thread).rng_last_j
91,94c92,93
<         if(ikont.eq.from_ik) from_blk=scp_first_blk(my_thread)
<         if(ikont.eq.to_ik) to_blk=scp_last_blk(my_thread)
< 
<         lastj=-1
---
>         if(ikont.eq.from_ik) from_blk=first_blk
>         if(ikont.eq.to_ik) to_blk=last_blk
101,102c100,101
<           if((ikont.eq.from_ik).and.(jblock.eq.from_blk)) from_j=first_j
<           if((ikont.eq.to_ik).and.(jblock.eq.to_blk)) to_j=last_j
---
>           if(jblock.eq.first_blk) from_j=first_j
>           if(jblock.eq.last_blk) to_j=last_j
104,219c103,108
<           do j=from_j,to_j
< C Uncomment following three lines for SC-p interactions
< c       call wrapvec(c_tobox(:,nres+j)-cp(:,i), xyzj)
< C Uncomment following three lines for Ca-p interactions
< 
<             LOG_STAT(400,1)
< 
<             !call wrapvec(c_tobox(:,j)-cp(:,i),xyzj)
<             call wrapvec2(c_tobox(:,j)-cp(:,i),xyzj,halfbox,box)
<             rij=scalar(xyzj,xyzj)
<             if(rij.ge.r_cut_int_sq) cycle
< 
<             LOG_STAT(401,1)
< 
<             !itypj=iabs(itype(j))
<             itypj=abs_itype(j)
< 
<             if(iabs(j-i).le.2) then           ! _14 path
<               call escp_queue_work(e14_work,e14_idx,j,xyzj,rij,
<      &                             aad(itypj,iteli),bad(itypj,iteli),
<      &                             full)
<             else if(rij.gt.r_dropoff_sq) then ! path with dropoff
<               call escp_queue_work(droff_work,droff_idx,j,xyzj,rij,
<      &                             aad(itypj,iteli),bad(itypj,iteli),
<      &                             full)
<               if(full) then
<                 call ESCP_WORK_DROPOFF(droff_work,i,1,droff_idx,
<      &                                 escp_result(:,my_thread),
<      &                                 escp_accum(my_thread))
<                 LOG_STAT(411,droff_idx)
<                 droff_idx=0
<               endif
<             else                              ! no dropoff, no _14
< #ifdef ESCP_WITH_SEQ
<               if((j.eq.lastj+1).or.(seq_idx.eq.0)) then
<                 call escp_queue_work(seq_work,seq_idx,j,xyzj,rij,
<      &                               aad(itypj,iteli),bad(itypj,iteli),
<      &                               full)
<                 lastj=j
<                 if(full) then
< !DIR$ NOINLINE
<                   call ESCP_WORK_SEQ(seq_work,i,
<      &                               seq_work.j(1),seq_work.j(seq_idx),
<      &                               escp_result(:,my_thread),
<      &                               escp_accum(my_thread))
<                   LOG_STAT(402,seq_idx)
<                   seq_idx=0
<                   lastj=-1
<                 endif
<               else
<                 if((seq_idx.lt.simd_seq_cutoff) .and.
<      &             (nseq_idx+seq_idx.lt.escp_buffer_size)) then
<                   LOG_STAT(404,seq_idx)
<                   call escp_move_work(seq_work,seq_idx,
<      &                                nseq_work,nseq_idx)
<                 else
< !DIR$ NOINLINE
<                   call ESCP_WORK_SEQ(seq_work,i,
<      &                               seq_work.j(1),seq_work.j(seq_idx),
<      &                               escp_result(:,my_thread),
<      &                               escp_accum(my_thread))
<                   LOG_STAT(402,seq_idx)
<                   seq_idx=0
<                 endif
< #endif
<                 call escp_queue_work(nseq_work,nseq_idx,j,xyzj,rij,
<      &                               aad(itypj,iteli),bad(itypj,iteli),
<      &                               full)
<                 lastj=j
<                 if(full) then
<                   call ESCP_WORK_NSEQ(nseq_work,i,1,nseq_idx,
<      &                                escp_result(:,my_thread),
<      &                                escp_accum(my_thread))
<                   LOG_STAT(403,nseq_idx)
<                   nseq_idx=0
<                   lastj=-1
<                 endif
< #ifdef ESCP_WITH_SEQ
<               endif
< #endif
<             endif
<           enddo
<         enddo
< 
<         if(e14_idx.gt.0) then
<           call ESCP_WORK_14(e14_work,i,1,e14_idx,
<      &                      escp_result(:,my_thread),
<      &                      escp_accum(my_thread))
<           e14_idx=0
<           LOG_STAT(412,e14_idx)
<         endif
<         if(droff_idx.gt.0) then
<           call ESCP_WORK_DROPOFF(droff_work,i,1,droff_idx,
<      &                           escp_result(:,my_thread),
<      &                           escp_accum(my_thread))
<           droff_idx=0
<           LOG_STAT(411,droff_idx)
<         endif
< #ifdef ESCP_WITH_SEQ
<         if(seq_idx.gt.0) then
< !DIR$ NOINLINE
<           call ESCP_WORK_SEQ(seq_work,i,
<      &                       seq_work.j(1),seq_work.j(seq_idx),
<      &                       escp_result(:,my_thread),
<      &                       escp_accum(my_thread))
<           seq_idx=0
<           LOG_STAT(402,seq_idx)
<         endif
< #endif
<         if(nseq_idx.gt.0) then
<           call ESCP_WORK_NSEQ(nseq_work,i,1,nseq_idx,
<      &                        escp_result(:,my_thread),
<      &                        escp_accum(my_thread))
<           nseq_idx=0
<           LOG_STAT(403,nseq_idx)
<         endif
---
>           call ESCP_WORK_SEQ(i,from_j,to_j,
>      &      workspace(my_thread).item,
>      &      escp_accum(my_thread).evdw2,
>      &      escp_row_aad(:,iteli),escp_row_bad(:,iteli),
>      &      escp_row_aad,escp_row_bad)
>         enddo ! jblock
220a110,132
> 
>       from_ik=scp_range(my_thread).sep_start
>       to_ik=scp_range(my_thread).sep_end
>       first_j=scp_range(my_thread).sep_first
>       last_j=scp_range(my_thread).sep_last
> 
>       do i=from_ik,to_ik
>         from_j=scpseplisti(i-1)+1
>         to_j=scpseplisti(i)
> 
>         if(i.eq.from_ik) from_j=first_j
>         if(i.eq.to_ik) to_j=last_j
>         if (from_j.gt.to_j) cycle
> 
>         iteli=itel(i)
> 
> !DIR$ NOINLINE
>           call ESCP_WORK_NSEQ(i,from_j,to_j,
>      &      workspace(my_thread).item,
>      &      escp_accum(my_thread).evdw2,
>      &      escp_row_aad(:,iteli),escp_row_bad(:,iteli),
>      &      escp_row_aad,escp_row_bad)
>       enddo  ! i
224,225d135
<       dexpon=expon
<       call sum_escp_arrays(threads_used,dexpon)
241,251d150
< #ifdef ESCP_WITH_SEQ
< #define ESCP_WORK_NAME ESCP_WORK_SEQ
< #define ESCP_MAP_J(seq)   seq
< #define ESCP_MAP_IDX(seq) seq-chunk.j(1)+1
< #define ESCP_FIX_SUM_DELTA2
< #include"escp_work.F"
< #undef ESCP_FIX_SUM_DELTA2
< #undef ESCP_MAP_IDX(seq)
< #undef ESCP_MAP_J(seq)
< #undef ESCP_WORK_NAME
< #endif
253,255c152,157
< #define ESCP_WORK_NAME ESCP_WORK_NSEQ
< #define ESCP_MAP_J(seq)  chunk.j(seq)
< #define ESCP_MAP_IDX(seq) seq
---
> #define ESCP_WORK_NAME          ESCP_WORK_SEQ
> #define ESCP_MAP_I(seq)         i
> #define ESCP_MAP_J(seq)         seq
> #define ESCP_FIRST_VECTORX      cpi(1)
> #define ESCP_FIRST_VECTORY      cpi(2)
> #define ESCP_FIRST_VECTORZ      cpi(3)
257c159,161
< #undef ESCP_MAP_IDX(seq)
---
> #undef ESCP_FIRST_VECTORZ
> #undef ESCP_FIRST_VECTORY
> #undef ESCP_FIRST_VECTORX
258a163
> #undef ESCP_MAP_I(seq)
261,264c166,171
< #define ESCP_WORK_NAME ESCP_WORK_DROPOFF
< #define ESCP_WORK_WITH_DROPOFF
< #define ESCP_MAP_J(seq)  chunk.j(seq)
< #define ESCP_MAP_IDX(seq) seq
---
> #define ESCP_WORK_NAME          ESCP_WORK_NSEQ
> #define ESCP_MAP_I(seq)         i
> #define ESCP_MAP_J(seq)         scpseplistj(seq)
> #define ESCP_FIRST_VECTORX      cpi(1)
> #define ESCP_FIRST_VECTORY      cpi(2)
> #define ESCP_FIRST_VECTORZ      cpi(3)
266c173,175
< #undef ESCP_MAP_IDX(seq)
---
> #undef ESCP_FIRST_VECTORZ
> #undef ESCP_FIRST_VECTORY
> #undef ESCP_FIRST_VECTORX
268c177
< #undef ESCP_WORK_WITH_DROPOFF
---
> #undef ESCP_MAP_I(seq)
271d179
< #define ESCP_WORK_NAME ESCP_WORK_14
273,274c181,187
< #define ESCP_MAP_J(seq)  chunk.j(seq)
< #define ESCP_MAP_IDX(seq) seq
---
> #define ESCP_IMMEDIATE_UPDATE_I
> #define ESCP_WORK_NAME     ESCP_WORK_14
> #define ESCP_MAP_I(seq)    (seq)
> #define ESCP_MAP_J(seq)    (seq+i)
> #define ESCP_FIRST_VECTORX soa_cp_x(seq)
> #define ESCP_FIRST_VECTORY soa_cp_y(seq)
> #define ESCP_FIRST_VECTORZ soa_cp_z(seq)
276c189,191
< #undef ESCP_MAP_IDX(seq)
---
> #undef ESCP_FIRST_VECTORZ
> #undef ESCP_FIRST_VECTORY
> #undef ESCP_FIRST_VECTORX
277a193,194
> #undef ESCP_MAP_I(seq)
> #undef ESCP_IMMEDIATE_UPDATE_I
escp_work.F
1c1,3
<       subroutine ESCP_WORK_NAME(chunk,i,from,to,output,accum)
---
>       subroutine ESCP_WORK_NAME(i,from,to,output,
>      &  d_evdw2,row_aad,row_bad,rowa_aad,rowa_bad)
>       use omp_data
14c16
<       include 'COMMON.TIME1'
---
>       include 'COMMON.OMP'
16,19c18,21
<       integer from,to,i,j,seq,idx
<       record /escp_work_chunk/ chunk
<       record /escp_result_item/ output(-1:maxres)
<       record /escp_accumulator/ accum
---
>       integer from,to,i,j,seq,idx,itypj,iteli
>       type(workspace_type) output
>       double precision d_evdw2
>       double precision i_aad(ntyp),i_bad(ntyp)
20a23
>       double precision cpi(3)
22c25
<       double precision sqrtrij,sqrtrrij,gamm
---
>       double precision sqrtrij
23a27,37
>       double precision gamm,esss,esssgrad
>       double precision gamm2
>       double precision dexpon,droff,irlamb2,ad,bd
>       double precision laad,lbad
>       !double precision mult,srir
>       double precision srir
>       double precision row_aad(maxres),row_bad(maxres)
>       double precision rowa_aad(maxres,2),rowa_bad(maxres,2)
>       double precision scalar
> 
>       double precision cutoffsq
26c40
<       delta2_14=0.0d0
---
>       dexpon=expon
28,31c42,54
< #ifdef ESCP_WORK_WITH_14
< !DIR$ LOOP COUNT(2)
< #else
< !DIR$ LOOP COUNT(64)
---
>       cutoffsq=(r_cut_int-rlamb)*(r_cut_int-rlamb)
> 
> !      mult=wscp*dexpon
> !      laad=aad(1,1)*mult
> !      lbad=bad(1,1)*mult
> !#ifdef ESCP_WORK_WITH_14
> !      laad=scal14*laad
> !      lbad=scal14*lbad
> !#endif
> !      iteli=itel(i)
> 
> #ifndef ESCP_WORK_WITH_14
>       cpi=cp(:,i)
33,35c56,64
< !$OMP SIMD REDUCTION(+:iacc,delta2,delta2_14) PRIVATE(
< !$OMP&   j,idx,xyzj,ggg,rij,rrij,sqrtrij,sqrtrrij,
< !$OMP&   sss,sssgrad,gamm,fac,e1,e2,evdwij
---
>       droff=r_dropoff*irlamb
>       !irlamb2=irlamb*6.0d0/dexpon
>       !irlamb2=6.0d0/dexpon
>       irlamb2=idexpon6
> 
> !DIR$ LOOP COUNT(64)
> !$OMP SIMD REDUCTION(+:iacc,delta2) PRIVATE(
> !$OMP&   j,idx,xyzj,ggg,rij,rrij,sqrtrij,itypj,iteli,
> !$OMP&   esss,esssgrad,gamm,gamm2,fac,e1,e2,evdwij,ad,bd,srir
39,43d67
<         idx=ESCP_MAP_IDX(seq)
<         xyzj(1)=chunk.x(idx)
<         xyzj(2)=chunk.y(idx)
<         xyzj(3)=chunk.z(idx)
<         rij=chunk.r(idx)
45c69,75
<         rrij=1.0d0/rij
---
> !       call wrapvec(c_tobox(:,j)-cpi,xyzj)
>         call wrapvec((/
>      &    soa_c_x(j)-ESCP_FIRST_VECTORX,
>      &    soa_c_y(j)-ESCP_FIRST_VECTORY,
>      &    soa_c_z(j)-ESCP_FIRST_VECTORZ
>      &    /),xyzj)
>         rij=scalar(xyzj,xyzj)
47,53c77,84
< #ifdef ESCP_WORK_WITH_DROPOFF
<         sqrtrij=dsqrt(rij)
<         sqrtrrij=1.0d0/(sqrtrij*expon)
<         gamm=(sqrtrij-r_dropoff)*irlamb
<         sss=1.0d0+gamm*gamm*(2*gamm-3.0d0)
<         sssgrad=gamm*(6*gamm-6.0d0)*irlamb
<         sssgrad=sssgrad*sqrtrrij
---
> !       write(*,'(a,2i4,3f18.10)')'ESCP1',ESCP_MAP_I(seq),j,xyzj(:)
> #ifdef ESCP_WORK_WITH_14
>         !itypj=abs_itype(j)
>         iteli=itel(ESCP_MAP_I(seq))
>         !bd=escp14_bad(itypj,iteli)
>         !ad=escp14_aad(itypj,iteli)
>         bd=rowa_bad(j,iteli)*scal14
>         ad=rowa_aad(j,iteli)*scal14
55,57c86,89
<         sss=1.0d0
<         sssgrad=0.0d0
<         !sqrtrrij=0.0d0
---
> !       bd=escp_bad(itypj,iteli)
> !       ad=escp_aad(itypj,iteli)
>         bd=row_bad(j)
>         ad=row_aad(j)
59a92,95
> !       bd=lbad
> !       ad=laad
> 
>         rrij=1.0d0/rij
60a97,99
>         e2=fac*bd
>         e1=fac*fac*ad
>         evdwij=e1+e2
62,63c101,150
<         e2=fac*chunk.bad(idx)
<         e1=fac*fac*chunk.aad(idx)
---
>         fac=rrij*(evdwij+e1)
> 
>         !if (rij.lt.cutoffsq) then
>           !esss=1.0d0
>           !esssgrad=0.0d0
>           !rrij=1.0d0/rij
> 
>           !ESCP_POW(rrij,fac)
> 
>           !e2=fac*bd
>           !e1=fac*fac*ad
> 
>           !evdwij=e1+e2
>           !delta2=delta2+evdwij*esss
>           !delta2=delta2+evdwij
>           !fac=-mult*rrij*(evdwij+e1)
>           !fac=rrij*(evdwij+e1)
> 
>         !else
>         if (rij.gt.cutoffsq) then
>           sqrtrij=dsqrt(rij)
>           !rrij=1.0d0/rij
> 
>           srir=sqrtrij*irlamb
>           !gamm=max(0.0d0,sqrtrij*irlamb-droff)
>           gamm=max(0.0d0,srir-droff)
>           gamm=min(1.0d0,gamm)
>           gamm2=gamm*gamm
>           !esss=1.0d0+gamm*gamm*(2.0d0*gamm-3.0d0)
>           !esssgrad=gamm*(gamm-1.0d0)*irlamb2/sqrtrij
>           esss=1.0d0+gamm2*(2.0d0*gamm-3.0d0)
> !         esssgrad=(gamm2-gamm)*irlamb2/sqrtrij
> !         esssgrad=(gamm2-gamm)*irlamb2
> !         esssgrad=(gamm2-gamm)*irlamb2*sqrtrij
>           esssgrad=(gamm2-gamm)*srir*irlamb2
> 
>           !ESCP_POW(rrij,fac)
> 
>           !e2=fac*bd
>           !e1=fac*fac*ad
> 
>           !evdwij=e1+e2
>           !delta2=delta2+evdwij*esss
>           !fac=(evdwij*esssgrad-(evdwij+e1)*esss)*rrij*mult
>           !fac=((evdwij+e1)*esss-evdwij*esssgrad)*rrij
>           fac=fac*esss-evdwij*esssgrad*rrij
>           evdwij=evdwij*esss
>         endif
> 
>         delta2=delta2+evdwij
65,75d151
< #ifdef ESCP_WORK_WITH_14
<         !if (iabs(j-i) .le. 2) then
<         e1=scal14*e1
<         e2=scal14*e2
<         evdwij=e1+e2
<         delta2_14=delta2_14+evdwij*sss
<         !endif
< #else
<         evdwij=e1+e2
< #endif
<         delta2=delta2+evdwij*sss
78,80c154,155
<      &      'evdw2',i,j,1.0d0/dsqrt(rrij),sss,
<      &     evdwij,itel(i),-1,fac,chunk.aad(idx)
<      &     chunk.bad(idx)
---
>      &      'evdw2',i,j,1.0d0/dsqrt(rrij),esss,
>      &     evdwij,itel(i),-1,fac,ad,bd
85,87c160,163
<         fac=-(evdwij+e1)*rrij*sss
<         fac=fac+(evdwij)*sssgrad
<         !fac=fac+evdwij*sssgrad*sqrtrrij
---
>         !fac=evdwij*esssgrad-(evdwij+e1)*rrij*esss
>         !fac=(evdwij*esssgrad*sqrtrij-(evdwij+e1)*esss)*rrij
>         !fac=(evdwij*esssgrad-(evdwij+e1)*esss)*rrij
>         !fac=(evdwij*esssgrad-(evdwij+e1)*esss)*rrij*mult
88a165,175
> !       write(*,'(a,2i4,4f18.10)')'ESCP',ESCP_MAP_I(seq),j,
> !    &    evdwij/(wscp*expon),ggg(:)
> 
> #ifdef ESCP_IMMEDIATE_UPDATE_I
>         output.dgradbufc2x(ESCP_MAP_I(seq))=
>      &    output.dgradbufc2x(ESCP_MAP_I(seq))+ggg(1)
>         output.dgradbufc2y(ESCP_MAP_I(seq))=
>      &    output.dgradbufc2y(ESCP_MAP_I(seq))+ggg(2)
>         output.dgradbufc2z(ESCP_MAP_I(seq))=
>      &    output.dgradbufc2z(ESCP_MAP_I(seq))+ggg(3)
> #else
90c177,180
<         output(j).scp=output(j).scp+ggg
---
> #endif
>         output.dgradbufcx(j)=output.dgradbufcx(j)-ggg(1)
>         output.dgradbufcy(j)=output.dgradbufcy(j)-ggg(2)
>         output.dgradbufcz(j)=output.dgradbufcz(j)-ggg(3)
93,94c183,187
< #ifdef ESCP_WORK_WITH_14
<       accum.evdw2_14=accum.evdw2_14+delta2_14
---
> 
> #ifndef ESCP_IMMEDIATE_UPDATE_I
>       output.dgradbufc2x(i)=output.dgradbufc2x(i)+iacc(1)
>       output.dgradbufc2y(i)=output.dgradbufc2y(i)+iacc(2)
>       output.dgradbufc2z(i)=output.dgradbufc2z(i)+iacc(3)
96,97c189
<       output(i).scpp=output(i).scpp-iacc
<       accum.evdw2=accum.evdw2+delta2
---
>       d_evdw2=d_evdw2+delta2
100d191
< 
etheta_constr_aveave.F
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/etheta_constr_aveave.F: No such file or directory
etheta_constr_aveave-org-Aug-4-2025.F
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/etheta_constr_aveave-org-Aug-4-2025.F: No such file or directory
etheta_constr.F
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/etheta_constr.F: No such file or directory
etor_constr_aveave.F
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/etor_constr_aveave.F: No such file or directory
etor_constr_aveave-org-Aug-4-2025.F
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/etor_constr_aveave-org-Aug-4-2025.F: No such file or directory
etor_constr.F
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/etor_constr.F: No such file or directory
etor_kcc.F
42a43
> !     write(*,*)'etor_kcc'
86a88
> !       write(*,'(a,i4,4i4)')'R 1 ',i,itype(i-2),itype(i-1),itori,itori1
106a109,110
> !       write(*,'(a,i4,4f18.12)')'R 2 ',i,
> !    &    costhet1,costhet2,sinthet1,sinthet2
131c135
<         sint1t2n=1.0d0
---
>         sint1t2n=1.0d0          !TODO: unused?
136a141
> !       write(*,'(a,i4,1i4)')'R 3 ',i,nval
145a151
> !       write(*,'(a,i4,2f18.12)')'R 4 ',i,c1(nval),c2(nval)
149a156
> !       write(*,'(a,i4,2f18.12)')'R 5 ',i,cphsth1sth2,sphsth1sth2
158a166,173
> !       write(*,'(a,i4,3f18.12)')'R 61',i,dcgs1s2(:,1)
> !       write(*,'(a,i4,3f18.12)')'R 62',i,dcgs1s2(:,2)
> !       write(*,'(a,i4,3f18.12)')'R 63',i,dcgs1s2(:,3)
> !       write(*,'(a,i4,3f18.12)')'R 71',i,dsgs1s2(:,1)
> !       write(*,'(a,i4,3f18.12)')'R 72',i,dsgs1s2(:,2)
> !       write(*,'(a,i4,3f18.12)')'R 73',i,dsgs1s2(:,3)
> 
> !       write(*,'(a,i4,1i4)')'R 7a',i,nterm_kcc(itori,itori1)
163a179,181
> !       write(*,'(a,i4,7f18.12)')'R 8 ',i,cnphis1s2(:)
> !       write(*,'(a,i4,7f18.12)')'R 9 ',i,cnphis1s2cphi(:)
> !       write(*,'(a,i4,7f18.12)')'R A ',i,cnphis1s2t1t2(:)
168a187,189
> !       write(*,'(a,i4,7f18.12)')'R B ',i,snphis1s2(:)
> !       write(*,'(a,i4,7f18.12)')'R C ',i,snphis1s2cphi(:)
> !       write(*,'(a,i4,7f18.12)')'R D ',i,snphis1s2t1t2(:)
269a291,292
> !       write(*,'(a,i4,3f18.12)')'R Y ',i,glocit1,glocit2,sumaux
> !       write(*,'(a,i4,1f18.12)')'R Z ',i,etori
fdiag.f
fdisy.f
322,367d321
< 
< 
< C*****************************************************************
<       subroutine fdisys2(n,dm,du1,du2,dminv,rs,x)
<       implicit none
<       double precision dm(1:n),du1(1:n),du2(1:n),rs(1:n),x(1:n)
<       double precision dminv(1:n)
<       double precision dummy1,dummy2,dummy3
<       integer i,n
< C
< C  updating
< C
<       dummy3 = rs(1)
<       rs(1) = dummy3*dminv(1)
<       dummy2 = rs(2)-du1(1)*dummy3
<       rs(2) = dummy2*dminv(2)
<       do i=3,n,1
<          dummy1 = rs(i)-du1(i-1)*dummy2-du2(i-2)*dummy3
<          rs(i) = dummy1*dminv(i)
<          dummy3 = dummy2
<          dummy2 = dummy1
<          !dummy1 = dummy3
<       enddo
< 
< 
< C
< C  backsubstitution
< C
<       x(n) = rs(n)
<       x(n-1) = rs(n-1)-du1(n-1)*x(n)
<       do i=n-2,1,-1
<          x(i) = rs(i)-du1(i)*x(i+1)-du2(i)*x(i+2)
<       enddo
<       return
<       end
< 
< C*****************************************************************
<       subroutine fdisyp_precalc(n,dm,dminv)
<       implicit none
<       double precision dm(1:n),dminv(1:n)
<       integer i,n
<       do i=1,n
<         dminv(i)=1.0d0/dm(i)
<       enddo
<       return
<       end
fitsq.f
gauss.f
gen_rand_conf.F
913c913
<       common /overlapsccommon_all/ ioverlap_all
---
>       common /overlapsclistcommon/ ioverlap_all
922a923,924
>       include 'COMMON.OMP'
>       include 'COMMON.EGB'
924a927,928
>       integer from_blk,to_blk,from_j,to_j,my_thread
>       integer from_ik,to_ik,first_blk,last_blk,first_j,last_j
950c954
<       write (iout,*) "FG proecssor",fg_rank," g_listscsc_start",
---
>       write (iout,*) "FG processor",fg_rank," g_listscsc_start",
952c956
<       write (*,*) "FG proecssor",fg_rank," g_listscsc_start",
---
>       write (*,*) "FG processor",fg_rank," g_listscsc_start",
956c960,968
<       do ikont=g_listscsc_start,g_listscsc_end
---
> !     do ikont=g_listscsc_start,g_listscsc_end
>       do my_thread=1,scsc_threads
>       from_ik=scsc_range(my_thread).rng_start
>       to_ik=scsc_range(my_thread).rng_end
>       first_blk=scsc_range(my_thread).rng_first
>       last_blk=scsc_range(my_thread).rng_last
>       first_j=scsc_range(my_thread).rng_first_j
>       last_j=scsc_range(my_thread).rng_last_j
>       do ikont=from_ik,to_ik
962a975,981
> 
>         from_blk=newcontlisti(2,ikont-1)+1
>         to_blk=newcontlisti(2,ikont)
> 
>         if(ikont.eq.from_ik) from_blk=first_blk
>         if(ikont.eq.to_ik) to_blk=last_blk
> 
979,980c998,1008
<          do jblock=newcontlisti(2,ikont-1)+1,newcontlisti(2,ikont)
<          do j=newcontlistj(1,jblock),newcontlistj(2,jblock)
---
> !        do jblock=newcontlisti(2,ikont-1)+1,newcontlisti(2,ikont)
> !        do j=newcontlistj(1,jblock),newcontlistj(2,jblock)
>         do jblock=from_blk,to_blk
>           from_j=newcontlistj(1,jblock)
>           to_j=newcontlistj(2,jblock)
>           !if((ikont.eq.from_ik).and.(jblock.eq.from_blk)) from_j=first_j
>           !if((ikont.eq.to_ik).and.(jblock.eq.to_blk)) to_j=last_j
>           if(jblock.eq.first_blk) from_j=first_j
>           if(jblock.eq.last_blk) to_j=last_j
>           do j=from_j,to_j
> 
1068c1096,1097
<       enddo
---
>       enddo !ikont
>       enddo !my_thread
gen_rand_conf_mchain.F
geomout.F
157c157
<   30  FORMAT ('CONECT',8I5)
---
>   30  FORMAT ('CONECT',8I7)
397,398c397,398
<         call xdrfint(ixdrf, idssb(j)+nres, iret)
<         call xdrfint(ixdrf, jdssb(j)+nres, iret)
---
>         call xdrfint(ixdrf, idssb(j), iret)
>         call xdrfint(ixdrf, jdssb(j), iret)
480a481,485
> #ifdef MOMENTLOG
>       write(*,'(a,3f18.10)')'SOcm',cm
>       write(*,'(a,3f18.10)')'SOam',Lmom
>       write(*,'(a,3f18.10)')'SOvc',vcm
> #endif
489,490c494,495
<           write (line1,'(i10,f15.2,3f12.3,f7.2,2f6.3,4f12.3,f12.5,i5,
<      &         f12.3,$)')
---
>           write (line1,'(i10,f15.2,3f12.3,f7.2,2f6.3,4f12.3,i5,f12.5,
>      &         f12.3,i5,$)')
493,494c498,499
<      &       potEcomp(23),vcm_len,Lmom_len,me
<           format1="a157"
---
>      &       potEcomp(23),nss,vcm_len,Lmom_len,me
>           format1="a162"
497c502
<           write (line1,'(i10,f15.2,7f12.3,f12.5,f12.3,i5,$)')
---
>           write (line1,'(i10,f15.2,7f12.3,i5,f12.5,f12.3,i5,$)')
500,501c505,506
<      &          potEcomp(23),vcm_len,Lmom_len,me
<           format1="a138"
---
>      &          potEcomp(23),nss,vcm_len,Lmom_len,me
>           format1="a143"
512c517
<      &     f9.3,f12.5,f12.3,i5,$)')
---
>      &     f9.3,i5,f12.5,f12.3,i5,$)')
515,516c520,521
<      &         distance,potEcomp(23),vcm_len,Lmom_len,me
<           format1="a157"
---
>      &         distance,potEcomp(23),nss,vcm_len,Lmom_len,me
>           format1="a162"
519c524
<           write (line1,'(i10,f15.2,8f12.3,f12.5,f12.3,i5,$)')
---
>           write (line1,'(i10,f15.2,8f12.3,i5,f12.5,f12.3,i5,$)')
522,523c527,528
<      &          distance,potEcomp(23),vcm_len,Lmom_len,me
<           format1="a138"
---
>      &          distance,potEcomp(23),nss,vcm_len,Lmom_len,me
>           format1="a143"
528,529c533,534
<           write (line1,'(i10,f15.2,3f12.3,f7.2,4f6.3,3f12.3,f12.5f12.3,
<      &     i5,$)')
---
>           write (line1,'(i10,f15.2,3f12.3,f7.2,4f6.3,3f12.3,i5,
>      &     f12.5,f12.3,i5,$)')
531c536
<      &     rms,frac,frac_nn,co,amax,kinetic_T,t_bath,gyrate(),
---
>      &     rms,frac,frac_nn,co,amax,kinetic_T,t_bath,gyrate(),nss,
533c538
<           format1="a157"
---
>           format1="a162"
535c540
<           write (line1,'(i10,f15.2,7f12.3,f12.5,f12.3,i5,$)')
---
>           write (line1,'(i10,f15.2,7f12.3,i5,f12.5,f12.3,i5,$)')
537,538c542,543
<      &          amax,kinetic_T,t_bath,gyrate(),vcm_len,Lmom_len,me
<          format1="a138"
---
>      &          amax,kinetic_T,t_bath,gyrate(),nss,vcm_len,Lmom_len,me
>          format1="a143"
564c569,573
<           write (istat,format) "#"," ",
---
> c          if (dyn_ss) then
> c            write (istat,format) "#"," ",
> c     &      (ename(print_order(i)),i=1,nprint_ene),' NSS SSBONDS'
> c          else
>             write (istat,format) "#"," ",
565a575
> c          endif
570a581,587
> c          if (dyn_ss) then
> c            write (istat,'(i5,$)') nss
> c            do i=1,nss
> c              write (istat,'(i5,1h-,i5,$)') iss(idssb(i)),iss(jdssb(i))
> c            enddo
> c          endif
> c          write (istat,'(/)')
573a591,597
> c        if (dyn_ss) then
> c          write (istat,'(i5,$)') nss
> c          do i=1,nss
> c            write (istat,'(i5,1h-,i5,$)') iss(idssb(i)),iss(jdssb(i))
> c          enddo
> c        endif
> c        write (istat,'(/)')
gnmr1.f
1,47d0
< c-------------------------Amber penalty function-------------------------------------
<       double precision function gamber(y,y1,y2,y3,y4,k2,k3)
<       implicit none
<       double precision y
<       double precision y1,y2,y3,y4
<       double precision k2,k3
<   
<       ! Create penalty function gambernmr1
<         if(y.le.y1) then
<              gamber=2*k2*(y2-y1)*(y1-y) +k2*(y1-y2)**2
<         elseif ( y.ge.y1 .and. y.le.y2) then
<              gamber=k2*(y2-y)**2
<         elseif (y.ge.y2 .and. y.le.y3) then
<              gamber=0.0d0
<         elseif (y.ge.y3 .and. y.le.y4) then
<              gamber=k3*(y-y3)**2
<         elseif (y.ge.y4) then
<              gamber=2*k3*(y4-y3)*(y-y4) +k3*(y4-y3)**2
<         endif
< 
<       return
< 
<       end 
< c------------------------Derivative of Amber penalty function---------------------
<       double precision function gamberprim(y,y1,y2,y3,y4,k2,k3)
<       implicit none
<       double precision y
<       double precision y1,y2,y3,y4
<       double precision k2,k3
< 
<       ! Create derivative of the penalty function adgnmr1(y)
<         if(y.le.y1) then
<              gamberprim=-2*k2*(y2-y1)
<         elseif ( y.ge.y1 .and. y.le.y2) then
<              gamberprim=-2*k2*(y2-y)
<         elseif (y.ge.y2 .and. y.le.y3) then
<              gamberprim=0.0d0
<         elseif (y.ge.y3 .and. y.le.y4) then
<              gamberprim=2*k3*(y-y3)
<         elseif (y.ge.y4) then
<              gamberprim=2*k3*(y4-y3)
<         endif
< 
<       return
< 
<       end
< c--------------------------------------------------------------------------------
gradient_p.F
197a198
> #ifdef VIRTUAL
244c245
<       call sum_gradient
---
>       call sum_gradient_int
380a382,442
> #else
> c---------------------------------------------------------------------------
>       subroutine cartgrad
>       implicit none
>       include 'DIMENSIONS'
> #ifdef MPI
>       include 'mpif.h'
> #else
>       double precision tcpu
> #endif
>       include 'COMMON.CONTROL'
>       include 'COMMON.CHAIN'
>       include 'COMMON.DERIV'
>       include 'COMMON.VAR'
>       include 'COMMON.INTERACT'
>       include 'COMMON.FFIELD'
>       include 'COMMON.MD'
>       include 'COMMON.NMR'
>       include 'COMMON.TORCNSTR'
>       include 'COMMON.QRESTR'
>       include 'COMMON.IOUNITS'
>       include 'COMMON.TIME1'
>       integer i,j,kk
>       double precision time00,time01
> c
> c This subrouting calculates total Cartesian coordinate gradient.
> c The subroutine chainbuild_cart and energy MUST be called beforehand.
> c
> #ifdef TIMING
> #ifdef MPI
>       time00=MPI_Wtime()
> #else
>       time00=tcpu()
> #endif
> #endif
>       icg=1
> #ifdef DEBUG
>       write (iout,*) "Before sum_gradient"
>       do i=1,nres-1
>         write (iout,*) i," gradc  ",(gradc(j,i,icg),j=1,3)
>         write (iout,*) i," gradx  ",(gradx(j,i,icg),j=1,3)
>       enddo
>       write (iout,*) "gsaxsc, gsaxcx"
>       do i=1,nres-1
>         write (iout,*) i," gsaxsc  ",(gsaxsc(j,i),j=1,3)
>         write (iout,*) i," gsaxsx  ",(gsaxsx(j,i),j=1,3)
>       enddo
> #endif
>       call sum_gradient
> #ifdef TIMING
> #endif
> #ifdef DEBUG
>       write (iout,*) "After sum_gradient"
>       do i=1,nres-1
>         write (iout,*) i," gradc  ",(gradc(j,i,icg),j=1,3)
>         write (iout,*) i," gradx  ",(gradx(j,i,icg),j=1,3)
>       enddo
> #endif
>       return
>       end
> #endif
399a462,463
>       double precision gcarttemp(3,0:maxres)
>       common /gradtransformcommon/ gcarttemp
407a472
> !$OMP PARALLEL DO DEFAULT(SHARED)
409,412c474,478
<         do j=1,3
<           gcart(j,i)=-gcart(j,i)+gcart(j,i-1)-gxcart(j,i)
< !          gcart_new(j,i)=-gcart(j,i)+gcart(j,i-1)-gxcart(j,i)
<         enddo
---
>         gcarttemp(:,i)=-gcart(:,i)+gcart(:,i-1)-gxcart(:,i)
> !        do j=1,3
> !          gcart(j,i)=-gcart(j,i)+gcart(j,i-1)-gxcart(j,i)
> !!          gcart_new(j,i)=-gcart(j,i)+gcart(j,i-1)-gxcart(j,i)
> !        enddo
415a482,483
> #define CORRDUM
> #ifdef CORRDUM
416a485,486
>       gcart(:,1)=gcarttemp(:,1)
> !$OMP PARALLEL DO DEFAULT(SHARED)
417a488,492
>         !if (itype(i-1).eq.ntyp1 .and. itype(i).ne.ntyp1) then
>         !  gcart(:,i)=gcart(:,i)+gcart(:,i-1)
>         !else if (itype(i-1).ne.ntyp1 .and. itype(i).eq.ntyp1) then
>         !  gcart(:,i-1)=gcart(:,i-1)+gcart(:,i)
>         !endif
419,421c494,499
<           gcart(:,i)=gcart(:,i)+gcart(:,i-1)
<         else if (itype(i-1).ne.ntyp1 .and. itype(i).eq.ntyp1) then
<           gcart(:,i-1)=gcart(:,i-1)+gcart(:,i)
---
>           gcart(:,i)=gcarttemp(:,i)+gcarttemp(:,i-1)
>         else if (i.lt.nres .and.
>      &           itype(i).ne.ntyp1 .and. itype(i+1).eq.ntyp1) then
>           gcart(:,i)=gcarttemp(:,i)+gcarttemp(:,i+1)
>         else
>           gcart(:,i)=gcarttemp(:,i)
423a502,507
> #else
> !$OMP PARALLEL DO DEFAULT(SHARED)
>       do i=1,nres
>         gcart(:,i)=gcarttemp(:,i)
>       enddo
> #endif
480a565
>       include 'COMMON.OMP'
482c567,568
<       integer from,from2,to,cnt,my_thread,threads_used,range_lo,range_hi
---
>       integer from,from2,to,cnt,cnt2
>       integer my_thread,threads_used,range_lo,range_hi
498c584
< !$OMP& PRIVATE(my_thread,from,from2,to,cnt,i,j,kk,intertyp)
---
> !$OMP& PRIVATE(my_thread,from,from2,to,cnt,cnt2,i,j,kk,intertyp)
506a593,600
> 
>       call zero_array(3,cnt,omp_dgradbufc(1,from))
>       call zero_array(3,cnt,omp_dgradc(1,from))
>       call zero_array(3,cnt,omp_dgradx(1,from))
>       call zero_array(3,cnt*6,omp_dgloc(from*6))
> 
>       from2=max(from,1)
>       cnt2=to-from2+1
553c647,648
<       ghpbdphi(:nres)=0.0d0
---
>       !ghpbdphi(:nres)=0.0d0
>       call zero_array(1,cnt2,ghpbdphi(from2))
grid_arrays.F
5a6
>         integer, allocatable :: dgridrangesaxs(:)
grid.F
1,3d0
< #define SHIFT 9
< #define MASK  255
< 
13c10,91
<       include "COMMON.IOUNITS"
---
>       include 'COMMON.OMP'
>       include 'COMMON.GRID'
>       include 'COMMON.SAXSGRID'
>       integer res,row_length,saxs_row_length,max_row
>       integer ub_grid(3),ub_total           ! upper bound for grid size
>       integer ub_saxs_grid(3),ub_saxs_total
> 
>       call grid_size_from_range(ub_grid,r_cut_int)
>       ub_total=ub_grid(1)*ub_grid(2)*ub_grid(3)
> 
>       if(saxs_grid_cutoff.gt.0.0d0) then
>         call grid_size_from_range(ub_saxs_grid,saxs_grid_cutoff)
>         ub_saxs_total=ub_saxs_grid(1)*ub_saxs_grid(2)*ub_saxs_grid(3)
>       else
>         ub_saxs_grid(:)=1
>         ub_saxs_total=1
>       endif
> 
>       maxneggridsize=ub_grid
>       maxsaxsgridsize=ub_saxs_grid
> 
>       max_grid_cells=max(ub_total,ub_saxs_total)
> 
>       row_length=ub_total+16+(16-mod(ub_total,16))
>       saxs_row_length=ub_saxs_total+16+(16-mod(ub_saxs_total,16))
>       max_row=max(row_length,saxs_row_length)
> 
>       allocate(dgridptr(-1:max_row-2,max_fg_threads),stat=res)
>       call grid_check_allocation_status(res)
>       allocate(th_grid_count(-1:max_row-2,max_fg_threads),stat=res)
>       call grid_check_allocation_status(res)
>       allocate(dgridrangep(-2:row_length-3),stat=res)
>       call grid_check_allocation_status(res)
>       allocate(dgridrangepn(-2:row_length-3),stat=res)
>       call grid_check_allocation_status(res)
>       allocate(dgridrangesc(-2:row_length-3),stat=res)
>       call grid_check_allocation_status(res)
>       allocate(dgridrangesaxs(-2:saxs_row_length-3),stat=res)
>       call grid_check_allocation_status(res)
> 
>       r_buff_list=default_r_buff_list
>       r_buff_saxs_list=default_r_buff_list
> 
>       first_time_grid=.true.
>       call recalculate_grid_sizes(r_buff_list,.true.)
>       call recalculate_saxs_grid_sizes(r_buff_saxs_list,.true.)
> 
>       grid_opts%non_work_extra_time=0.0d0
>       saxs_grid_opts%non_work_extra_time=0.0d0
> 
>       grid_opts%enabled=.false.
>       saxs_grid_opts%enabled=.false.
>       end subroutine
> 
> C----------------------------------------------------------------------
>       subroutine grid_check_allocation_status(res)
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.IOUNITS'
>       integer res
>       if(res.ne.0) then
>         write (iout,*) "ERROR: cannot allocate enough memory for grids"
>         call terminate
>       endif
>       end subroutine
> 
> C----------------------------------------------------------------------
>       subroutine restart_grid_tuning(opts)
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.GRID'
>       type(grid_tuning_options) opts
>       opts%time_point=-1.0d0
>       opts%direction=1
>       opts%enabled=.true.
>       !write(*,*)'restart_grid_tuning'
>       end subroutine
> 
> C----------------------------------------------------------------------
>       subroutine mark_grid_tuning_iteration(opts)
>       implicit none
>       include 'DIMENSIONS'
15,17c93,95
<       double precision isqrt3
<       integer d,total_cells,row_length
<       integer res1,res2,res3,res4,res5
---
>       type(grid_tuning_options) opts
>       opts%iterations_in_sample=opts%iterations_in_sample+1
>       end subroutine
19c97,103
<       isqrt3=1.0d0/dsqrt(3.0d0)
---
> C----------------------------------------------------------------------
>       subroutine mark_grid_iteration
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.GRID'
>       call mark_grid_tuning_iteration(grid_opts)
>       end subroutine
21,23c105,141
<       negcellsize(1)=r_cut_int+r_buff_list
<       negcellsize(2)=r_cut_int+r_buff_list
<       negcellsize(3)=r_cut_int+r_buff_list
---
> C----------------------------------------------------------------------
>       subroutine mark_saxs_grid_iteration
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.GRID'
>       call mark_grid_tuning_iteration(saxs_grid_opts)
>       end subroutine
> 
> C----------------------------------------------------------------------
>       subroutine grid_tune(opts,tuned_buff)
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.SETUP'
>       include 'COMMON.GRID'
> #ifdef MPI
>       include "mpif.h"
>       integer ierr
> #endif
>       double precision tuned_buff,t,current_time,mpisum
>       type(grid_tuning_options) opts
>       double precision timer_value
> 
>       !write(*,*)'grid_tune',fg_rank,opts%enabled,opts%time_point
> 
>       if(.not.opts%enabled) return
> 
>       ! first call
>       if(opts%time_point.lt.0.0d0) then
>         opts%time_point=timer_value()
>         opts%last_time=0.0d0
>         opts%iterations_in_sample=0
>         opts%direction=1
>         opts%samples_left=grid_average_samples
>         opts%current_sum=0.0d0
>         !write(*,*)'first_call',fg_rank
>         return
>       endif
25,27c143,149
<       neggridsize(1)=ceiling(boxxsize/negcellsize(1))
<       neggridsize(2)=ceiling(boxysize/negcellsize(2))
<       neggridsize(3)=ceiling(boxzsize/negcellsize(3))
---
>       t=timer_value()
>       opts%current_sum=opts%current_sum+t-opts%time_point
>       opts%time_point=t
> 
>       opts%samples_left=opts%samples_left-1
>       !write(*,*)'samples_left',fg_rank,opts%samples_left
>       if(opts%samples_left.gt.0) return
29,31c151
<       neggridmerge(1)=negcellsize(1)*neggridsize(1).ne.boxxsize
<       neggridmerge(2)=negcellsize(2)*neggridsize(2).ne.boxysize
<       neggridmerge(3)=negcellsize(3)*neggridsize(3).ne.boxzsize
---
>       current_time=opts%current_sum-opts%non_work_extra_time
33,35c153,277
<       if(neggridmerge(1)) neggridsize(1)=neggridsize(1)-1
<       if(neggridmerge(2)) neggridsize(2)=neggridsize(2)-1
<       if(neggridmerge(3)) neggridsize(3)=neggridsize(3)-1
---
> #ifdef MPI
>       if (nfgtasks.gt.1) then
>         !write(*,*)'gathering',current_time,opts%iterations_in_sample
>         call MPI_reduce(current_time,mpisum,1,
>      &    MPI_DOUBLE_PRECISION,MPI_SUM,0,FG_COMM,IERR)
>         if(fg_rank.eq.0) then
>           current_time=mpisum
>           !write(*,*)'gathered',current_time
>         endif
>       endif
> #endif
> 
> #ifdef MPI
>       if (fg_rank.eq.0) then
> #endif
>       if(opts%iterations_in_sample.le.1) then
>         !current_time=opts%current_sum
>       else
>         !current_time=opts%current_sum/opts%iterations_in_sample
>         current_time=current_time/opts%iterations_in_sample
>       endif
> 
>       if(opts%last_time.eq.0.0d0) then
>         ! first sample gathered, change r_buff_list and see if improves
>       else if (current_time.lt.opts%last_time) then
>         ! got faster, keep going
>       else
>         ! got slower back up one step and reverse direction
>         opts%direction=-opts%direction
>       endif
>       tuned_buff=tuned_buff+opts%direction*grid_cutoff_step
>       tuned_buff=max(smallest_r_buff_list,tuned_buff)
>       !tuned_buff=0.5
> #ifdef MPI
>       endif
> #endif
> #ifdef MPI
>       if (nfgtasks.gt.1) then
>         !if(fg_rank.eq.0) write(*,*)'broadcasting',tuned_buff
>         call MPI_bcast(tuned_buff,1,
>      &    MPI_DOUBLE_PRECISION,0,FG_COMM,IERR)
>         !if(fg_rank.ne.0) write(*,*)'got broadcasted',tuned_buff
>       endif
> #endif
> #ifdef MPI
>       if (fg_rank.eq.0) then
> #endif
>       write(*,'(a,f6.2,a,f0.6,a,f0.6,a,i5,a,f0.6)')
>      &  'Tuning grid to ',tuned_buff,
>      &  ' curr. time: ',current_time/grid_average_samples,
>      &  ' + ',opts%non_work_extra_time,
>      &  ' (',opts%iterations_in_sample,
>      &  ' steps) prev. time: ',opts%last_time/grid_average_samples
> #ifdef MPI
>       else
>         write(*,'(a,f6.2)')'Tuning grid to ',tuned_buff
>       endif
> #endif
>       opts%last_time=current_time
>       opts%current_sum=0.0d0
>       opts%iterations_in_sample=0
>       opts%samples_left=grid_average_samples
>       opts%non_work_extra_time=0.0d0
>       end subroutine
> 
> C----------------------------------------------------------------------
>       subroutine grid_tune_lists(out_r_buff,gridsize,cellsize)
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       include 'COMMON.GRID'
>       !write(*,*)'grid_tune_lists'
>       double precision out_r_buff,cellsize(3)
>       integer gridsize(3)
>       call grid_tune(grid_opts,r_buff_list)
>       call recalculate_grid_sizes(r_buff_list,.false.)
>       out_r_buff=r_buff_list
>       gridsize(:)=neggridsize(:)
>       cellsize(:)=negcellsize(:)
>       end subroutine
> 
> C----------------------------------------------------------------------
>       subroutine grid_tune_saxs_lists(out_r_buff,gridsize,cellsize)
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       include 'COMMON.OMP'
>       include 'COMMON.GRID'
>       include 'COMMON.SAXSGRID'
>       !write(*,*)'grid_tune_saxs_lists'
>       double precision out_r_buff,cellsize(3)
>       integer gridsize(3)
>       call grid_tune(saxs_grid_opts,r_buff_saxs_list)
>       call recalculate_saxs_grid_sizes(r_buff_saxs_list,.false.)
>       out_r_buff=r_buff_saxs_list
>       gridsize(:)=saxsgridsize(:)
>       cellsize(:)=saxscellsize(:)
>       end subroutine
> 
> C----------------------------------------------------------------------
>       subroutine recalculate_grid_sizes(new_r_buff_list,first_time)
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       include 'COMMON.IOUNITS'
>       include 'COMMON.SPLITELE'
>       include 'COMMON.OMP'
>       include 'COMMON.GRID'
>       double precision new_r_buff_list
>       logical first_time
> 
>       call grid_size_from_range(neggridsize,r_cut_int+new_r_buff_list)
> 
>       if(nres.le.100) then
>         if(first_time) then
>           write(iout,'(a)')'Using single cell grid due to small NRES.'
>         endif
>         single_cell_grid=.true.
>       endif
> 
>       if(single_cell_grid) then
>         neggridsize(1)=1
>         neggridsize(2)=1
>         neggridsize(3)=1
>       endif
39a282,304
>       end subroutine
> 
>       subroutine recalculate_saxs_grid_sizes(new_r_buff_list,first_time)
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       include 'COMMON.IOUNITS'
>       include 'COMMON.SPLITELE'
>       include 'COMMON.OMP'
>       include 'COMMON.GRID'
>       include 'COMMON.SAXSGRID'
>       double precision new_r_buff_list
>       logical first_time
>       if(saxs_grid_cutoff.gt.0.0d0) then
>         call grid_size_from_range(saxsgridsize,
>      &    saxs_grid_cutoff+new_r_buff_list)
> 
>         if(nres.le.100) then
>           if(first_time) then
>             write(iout,'(a,a)')'Using single cell for SAXS grid ' //
>      &        'due to small NRES.'
>           saxs_single_grid=.true.
>         endif
41,60c306,326
<       poscellsize(1)=(r_cut_int+r_buff_list)*isqrt3
<       poscellsize(2)=(r_cut_int+r_buff_list)*isqrt3
<       poscellsize(3)=(r_cut_int+r_buff_list)*isqrt3
< 
<       posgridsize(1)=ceiling(boxxsize/poscellsize(1))
<       posgridsize(2)=ceiling(boxysize/poscellsize(2))
<       posgridsize(3)=ceiling(boxzsize/poscellsize(3))
< 
<       total_cells=neggridsize(1)*neggridsize(2)*neggridsize(3)
<       row_length=total_cells+(16-mod(total_cells,16))+16
<       max_grid_cells=total_cells
< 
<       allocate(dgridptr(-1:row_length-2,max_fg_threads),stat=res1)
<       allocate(dgridrangep(-2:row_length-3),stat=res2)
<       allocate(dgridrangepn(-2:row_length-3),stat=res3)
<       allocate(dgridrangesc(-2:row_length-3),stat=res4)
<       allocate(th_grid_count(-1:row_length-2,max_fg_threads),stat=res5)
<       if((res1.ne.0).or.(res2.ne.0).or.(res3.ne.0).or.
<      &   (res4.ne.0).or.(res5.ne.0)) then
<         write (iout,*) "ERROR: cannot allocate enough memory"
---
>         if(saxs_single_grid) then
>           saxsgridsize(1)=1
>           saxsgridsize(2)=1
>           saxsgridsize(3)=1
>         endif
>       else
>         saxsgridsize(1)=1
>         saxsgridsize(2)=1
>         saxsgridsize(3)=1
>       endif
> 
>       saxscellsize(1)=boxxsize/saxsgridsize(1)
>       saxscellsize(2)=boxysize/saxsgridsize(2)
>       saxscellsize(3)=boxzsize/saxsgridsize(3)
>       endif
>       end subroutine
> 
> C----------------------------------------------------------------------
>       subroutine grid_size_from_range(grid_size,rng)
>       use grid_arrays
>       implicit none
62c328
<         call MPI_Abort(MPI_COMM_WORLD)
---
>       include "mpif.h"
64,65c330,333
<         stop
<       endif
---
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       integer grid_size(3)
>       double precision rng,cellx,celly,cellz
66a335,349
>       cellx=rng
>       celly=rng
>       cellz=rng
> 
>       grid_size(1)=ceiling(boxxsize/cellx)
>       grid_size(2)=ceiling(boxxsize/celly)
>       grid_size(3)=ceiling(boxxsize/cellz)
> 
>       if(cellx*grid_size(1).ne.boxxsize) grid_size(1)=grid_size(1)-1
>       if(celly*grid_size(2).ne.boxysize) grid_size(2)=grid_size(2)-1
>       if(cellz*grid_size(3).ne.boxzsize) grid_size(3)=grid_size(3)-1
> 
>       if(grid_size(1).lt.4) grid_size(1)=1
>       if(grid_size(2).lt.4) grid_size(2)=1
>       if(grid_size(3).lt.4) grid_size(3)=1
71c354
<      &                     gridsize,invcellsize,gridmerge)
---
>      &                     gridsize,invcellsize)
90,92d372
<       !if(gridmerge(1).and.ix.ge.gridsize(1)-1) ix=gridsize(1)-1
<       !if(gridmerge(2).and.iy.ge.gridsize(2)-1) iy=gridsize(2)-1
<       !if(gridmerge(3).and.iz.ge.gridsize(3)-1) iz=gridsize(3)-1
93a374,376
>       !write(*,*)'gci',x,y,z
>       !write(*,*)'  1',ix,iy,iz
>       !write(*,'(a,3f12.4,i)')'GCI',x,y,z,cellid
101c384,385
<       neighbours=b'111111111111111111111111111'
---
>       ! own cell is not a neighbour
>       neighbours=b'111111111111101111111111111'
163d446
< !     subroutine calculate_nb_grid(input,ityp,n,
165c448,449
<      &                             gridcell,gridrange,griddata)
---
>      &                             gridcell,gridrange,griddata,
>      &                             gridsize,cellsize)
170a455
>       include 'COMMON.PENTADIAG'
172c457
<       integer i,j,n,ix,iy,iz,totalcells,cellid,dx,dy,dz,num_before
---
>       integer i,j,n,totalcells,cellid,dx,dy,dz,num_before
176d460
< !     integer ityp(maxres)
182,183c466,468
<       logical mrg(3)
<       integer mark
---
>       integer gridsize(3)
>       double precision cellsize(3)
>       integer mark,mycellid
191c476
<       gsize=neggridsize(1:3)
---
>       gsize=gridsize(1:3)
193,194c478
<       ics=1.0d0/negcellsize(1:3)
<       mrg=neggridmerge(1:3)
---
>       ics=1.0d0/cellsize(1:3)
196,197d479
<       !range_lo=1
<       !range_hi=n
208c490
< !$OMP&         fx,fy,fz,thr,mark,neighbours)
---
> !$OMP&         fx,fy,fz,thr,mark,neighbours,mycellid)
215,217d496
<       !from=range_lo+((range_hi-range_lo)/threads_used)*(my_thread-1)
<       !to=range_lo+((range_hi-range_lo)/threads_used)*my_thread-1
<       !if(my_thread.eq.threads_used) to=range_hi
230c509,510
<         call getcellid(input(:,i),0,0,0,gridcell(i),gsize,ics,mrg)
---
>         !write(*,*)'I',i
>         call getcellid(input(:,i),0,0,0,mycellid,gsize,ics)
231a512,514
>         gridcell(i)=mycellid
>         th_grid_count(mycellid,my_thread)=
>      &                       th_grid_count(mycellid,my_thread)+1
239,242c522,528
<               call getcellid(input(:,i),dx,dy,dz,cellid,gsize,ics,mrg)
<               !gridrange(2,cellid)=gridrange(2,cellid)+1
<               th_grid_count(cellid,my_thread)=
<      &                             th_grid_count(cellid,my_thread)+1
---
>               !write(*,'(a,i4,3f18.12)')'I',i,input(:,i)
>               call getcellid(input(:,i),dx,dy,dz,cellid,gsize,ics)
>               if (cellid.ne.mycellid) then
>                 !gridrange(2,cellid)=gridrange(2,cellid)+1
>                 th_grid_count(cellid,my_thread)=
>      &                               th_grid_count(cellid,my_thread)+1
>               endif
270a557,561
>       !if(dmp) then
>       !  do i=0,totalcells-1
>       !    write(*,'(a,i6,i)')'c:hst ',i,th_grid_count(i,threads_used)
>       !  enddo
>       !endif
280a572
>       enddo
281a574
>       do i=from,to
283c576,578
<         do j=2,threads_used
---
>       enddo
>       do j=2,threads_used
>         do i=from,to
291c586
< !$OMP&         fx,fy,fz,neighbours,mark)
---
> !$OMP&         fx,fy,fz,neighbours,mark,mycellid)
302a598
>         call getcellid(input(:,i),0,0,0,mycellid,gsize,ics)
303a600,602
>         j=dgridptr(mycellid,my_thread)
>         griddata(j)=i
>         dgridptr(mycellid,my_thread)=j+1
310,313c609,618
<               call getcellid(input(:,i),dx,dy,dz,cellid,gsize,ics,mrg)
<               j=dgridptr(cellid,my_thread)
<               griddata(j)=i
<               dgridptr(cellid,my_thread)=j+1
---
>               !write(*,*)'I',i
>               call getcellid(input(:,i),dx,dy,dz,cellid,gsize,ics)
>               if (cellid.ne.mycellid) then
>                 j=dgridptr(cellid,my_thread)
>                 griddata(j)=i
>                 dgridptr(cellid,my_thread)=j+1
> !               if(dmp)
> !    &            write(*,'(a,i6,a,i4,a,i3)')'H: c',cellid,' s ',i,' idx ',j
> !    &            write(*,'(a,i4,a,i6,a,i3)')'H: s',i,' c ',cellid,' i ',j
>               endif
323,404c628,739
< !      subroutine calculate_positive_grid(input,ityp,n,grid)
< !#ifdef _OPENMP
< !      use omp_lib
< !#endif
< !      implicit none
< !      include 'DIMENSIONS'
< !      include 'COMMON.CHAIN'
< !      include 'COMMON.GRID'
< !      integer i,j,n,ix,iy,iz,cellid,itypj
< !      integer range_lo,range_hi,from,to,my_thread,threads_used
< !      double precision x,y,z,invcellsize(3)
< !      integer grid(grid_length),ityp(maxres),gridsize(3)
< !      double precision input(3,n)
< !      integer lastcell,lastid,seqlen
< !      integer numseq,sumseq,mxseq
< !
< !      gridsize=posgridsize
< !
< !      invcellsize=1.0d0/poscellsize
< !
< !      range_lo=1
< !      range_hi=n
< !      threads_used=omp_get_max_threads()
< !!$OMP PARALLEL DEFAULT(SHARED)
< !!$OMP& PRIVATE(i,j,x,y,z,ix,iy,iz,itypj,cellid,lastcell,lastid,seqlen,
< !!$OMP&         my_thread)
< !      my_thread=omp_get_thread_num()+1
< !      from=range_lo+((range_hi-range_lo)/threads_used)*(my_thread-1)
< !      to=range_lo+((range_hi-range_lo)/threads_used)*my_thread-1
< !      if(my_thread.eq.threads_used) to=range_hi
< !
< !      lastcell=from
< !      lastid=-1
< !      seqlen=0
< !
< !      do i=from,to
< !        x=input(1,i)
< !        y=input(2,i)
< !        z=input(3,i)
< !        ix=idint(x*invcellsize(1))
< !        iy=idint(y*invcellsize(2))
< !        iz=idint(z*invcellsize(3))
< !        !cellid=ix+(iy+iz*gridsize(2))*gridsize(1)
< !        cellid=ix+ishft(iy,SHIFT)+ishft(iz,2*SHIFT)
< !!       write(*,*)'GS',x,y,z
< !!       write(*,*),'GI',x*invcellsize(1),y*invcellsize(2),
< !!    &                  z*invcellsize(3)
< !!       write(*,*),'GG',ix,iy,iz,cellid
< !
< !        itypj=iabs(ityp(i))
< !        if (itypj.eq.ntyp1 .or. itypj.eq.0) cellid=-1
< !
< !        if(cellid.eq.lastid) then
< !          seqlen=seqlen+1
< !        else
< !          do j=lastcell,i
< !            grid(j+j+1)=seqlen-(j-lastcell)
< !          enddo
< !          lastcell=i
< !!         sumseq=sumseq+seqlen
< !!         mxseq=max(mxseq,seqlen)
< !!         numseq=numseq+1
< !          seqlen=1
< !        endif
< !        grid(i*2)=cellid
< !        grid(i*2+1)=1
< !
< !        lastid=cellid
< !      enddo
< !!$OMP END PARALLEL
< !
< !!     if(seqlen.gt.0) then
< !!       sumseq=sumseq+seqlen
< !!       mxseq=max(mxseq,seqlen)
< !!       numseq=numseq+1
< !!     endif
< !!     write(*,*)'SEQ',numseq,dble(sumseq)/numseq,mxseq
< !!     do i=1,n
< !!       write(*,*)'D',i,grid(i*2),grid(i*2+1)
< !!     enddo
< !      end subroutine
< !
---
>       subroutine build_single_cell_grid(input,abs_ityp,n,
>      &                             gridcell,gridrange,griddata)
>       use omp_lib
>       use grid_arrays
>       implicit none
>       include "DIMENSIONS"
>       include 'COMMON.GRID'
> 
>       integer i,j,n,totalcells,num_before
>       integer itypj,s
>       double precision input(3,n)
>       integer abs_ityp(maxres)
>       integer gridcell(-1:maxres)
>       integer gridrange(-2:max_grid_cells)
>       integer griddata(27*maxres)
> 
>       integer my_thread,threads_used,range_lo,range_hi,from,to
>       integer th_from(max_fg_threads),th_to(max_fg_threads)
> 
> 
> #ifdef _OPENMP
>       threads_used=omp_get_max_threads()
> #else
>       threads_used=1
> #endif
> !$OMP PARALLEL NUM_THREADS(threads_used) DEFAULT(SHARED)
> !$OMP& PRIVATE(my_thread,from,to,i,j,itypj)
> #ifdef _OPENMP
>       my_thread=omp_get_thread_num()+1
> #else
>       my_thread=1
> #endif
> 
>       call split_work_for_threads(from,to,1,n,my_thread,threads_used)
>       th_from(my_thread)=from
>       th_to(my_thread)=to
>       th_grid_count(-1:0,my_thread)=0
> 
>       do i=from,to
>         !itypj=iabs(ityp(i))
>         itypj=abs_ityp(i)
>         if (itypj.eq.ntyp1 .or. itypj.eq.0) then
>           gridcell(i)=-1
>           cycle
>         endif
>         gridcell(i)=0
>         th_grid_count(0,my_thread)=th_grid_count(0,my_thread)+1
>       enddo
> !$OMP END PARALLEL
> 
>       s=0
>       do j=1,threads_used
>         s=s+th_grid_count(0,j)
>         th_grid_count(0,j)=s
>       enddo
> 
>       gridrange(-2)=0
>       gridrange(-1)=0
>       dgridptr(-1,:)=0
>       gridrange(0)=th_grid_count(0,threads_used)
> 
>       dgridptr(0,1)=th_grid_count(i-1,threads_used)+1
>       do j=2,threads_used
>         dgridptr(0,j)=dgridptr(0,1)+th_grid_count(0,j-1)
>       enddo
>       
> !$OMP PARALLEL NUM_THREADS(threads_used) DEFAULT(SHARED)
> !$OMP& PRIVATE(my_thread,from,to,i,j,itypj)
> #ifdef _OPENMP
>       my_thread=omp_get_thread_num()+1
> #else
>       my_thread=1
> #endif
>       from=th_from(my_thread)
>       to=th_to(my_thread)
>       do i=from,to
>         itypj=abs_ityp(i)
>         if (itypj.eq.ntyp1 .or. itypj.eq.0) cycle
>         j=dgridptr(0,my_thread)
>         griddata(j)=i
>         dgridptr(0,my_thread)=j+1
>       enddo
> !$OMP END PARALLEL
> 
>       end subroutine
> 
> C----------------------------------------------------------------------
>       subroutine dump_grid(n,gridcell,gridrange,griddata,gridsize)
>       use omp_lib
>       use grid_arrays
>       implicit none
>       include "DIMENSIONS"
>       include 'COMMON.GRID'
> 
>       integer i,j,n
>       integer itypj,s
>       integer gridcell(-1:maxres)
>       integer gridrange(-2:max_grid_cells)
>       integer griddata(27*maxres)
>       integer gridsize(3)
>       integer total_cells
>       total_cells=gridsize(1)*gridsize(2)*gridsize(3)
> 
>       do i=1,n
>         write(*,*)'gridcell',i,gridcell(i)
>       enddo
>       do i=0,total_cells-1
>         do j=gridrange(i-1)+1,gridrange(i)
>           write(*,*)'cell',i,j,griddata(j)
>         enddo
>       enddo
>       end
414a750
>       double precision r_buff
416,428c752,777
<       call calculate_nb_grid(cp(1,1),abs_itel,nres,
<      &                       gridcellsc,dgridrangesc,griddatasc)
<       call calculate_nb_grid(c_tobox(1,1),abs_itype,nres,
<      &                       gridcellp,dgridrangep,griddatap)
<       call calculate_nb_grid(c_tobox(1,nres+1),abs_itype,nres,
<      &                       gridcellpn,dgridrangepn,griddatapn)
< 
< !     call calculate_positive_grid(cp(1,1),itel,nres,
< !    &                             positive_sc)
< !     call calculate_positive_grid(c_tobox(1,1),itype,nres,
< !    &                             positive_p)
< !     call calculate_positive_grid(c_tobox(1,nres+1),itype,nres,
< !    &                             positive_pn)
---
>       if(.not.single_cell_grid) then
>         call grid_tune_lists(r_buff,neggridsize,negcellsize)
>         call calculate_nb_grid(cp(1,1),abs_itel,nres,
>      &                         gridcellsc,dgridrangesc,griddatasc,
>      &                         neggridsize,negcellsize)
>         call calculate_nb_grid(c_tobox(1,1),abs_itype,nres,
>      &                         gridcellp,dgridrangep,griddatap,
>      &                         neggridsize,negcellsize)
>         call calculate_nb_grid(c_tobox(1,nres+1),abs_itype,nres,
>      &                         gridcellpn,dgridrangepn,griddatapn,
>      &                         neggridsize,negcellsize)
>       else
>         call build_single_cell_grid(cp(1,1),abs_itel,nres,
>      &                         gridcellsc,dgridrangesc,griddatasc)
>         call build_single_cell_grid(c_tobox(1,1),abs_itype,nres,
>      &                         gridcellp,dgridrangep,griddatap)
>         call build_single_cell_grid(c_tobox(1,nres+1),abs_itype,nres,
>      &                         gridcellpn,dgridrangepn,griddatapn)
>       endif
>       first_time_grid=.false.
> !     call dump_grid(nres,gridcellsc,dgridrangesc,griddatasc,
> !    &  neggridsize)
> !     call dump_grid(nres,gridcellp,dgridrangep,griddatap,
> !    &  neggridsize)
> !     call dump_grid(nres,gridcellpn,dgridrangepn,griddatapn,
> !    &  neggridsize)
442,443d790
<       write (*,*)"  positive hits  ",posgridhits
<       write (*,*)"  positive jumps ",posgridjumps
indexx.f
inform.f
initialize_p.F
51c51
<       include 'COMMON.RESTARTED'
---
>       include 'COMMON.TIME1'
55a56
>       double precision time0,timer_value
161a163,164
> 
>       time0=timer_value()
168,170c171,173
< 	do j=1,3
< 	  c(j,i)=0.0D0
< 	  dc(j,i)=0.0D0
---
>       do j=1,3
>         c(j,i)=0.0D0
>         dc(j,i)=0.0D0
174,175c177,178
< 	do j=1,3
< 	  xloc(j,i)=0.0D0
---
>         do j=1,3
>           xloc(j,i)=0.0D0
179,181c182,184
< 	do j=1,ntyp
< 	  aa_aq(i,j)=0.0D0
< 	  bb_aq(i,j)=0.0D0
---
>         do j=1,ntyp
>           aa_aq(i,j)=0.0D0
>           bb_aq(i,j)=0.0D0
184,198c187,201
< 	  augm(i,j)=0.0D0
< 	  sigma(i,j)=0.0D0
< 	  r0(i,j)=0.0D0
< 	  chi(i,j)=0.0D0
<         enddo
< 	do j=1,2
< 	  bad(i,j)=0.0D0
<         enddo
< 	chip(i)=0.0D0
< 	alp(i)=0.0D0
< 	sigma0(i)=0.0D0
< 	sigii(i)=0.0D0
< 	rr0(i)=0.0D0
< 	a0thet(i)=0.0D0
< 	do j=1,2
---
>           augm(i,j)=0.0D0
>           sigma(i,j)=0.0D0
>           r0(i,j)=0.0D0
>           chi(i,j)=0.0D0
>         enddo
>         do j=1,2
>           bad(i,j)=0.0D0
>         enddo
>         chip(i)=0.0D0
>         alp(i)=0.0D0
>         sigma0(i)=0.0D0
>         sigii(i)=0.0D0
>         rr0(i)=0.0D0
>         a0thet(i)=0.0D0
>         do j=1,2
207c210
< 	  polthet(j,i)=0.0D0
---
>           polthet(j,i)=0.0D0
209,210c212,213
< 	do j=1,3
< 	  gthet(j,i)=0.0D0
---
>         do j=1,3
>           gthet(j,i)=0.0D0
212,218c215,221
< 	theta0(i)=0.0D0
< 	sig0(i)=0.0D0
< 	sigc0(i)=0.0D0
< 	do j=1,maxlob
< 	  bsc(j,i)=0.0D0
< 	  do k=1,3
< 	    censc(k,j,i)=0.0D0
---
>         theta0(i)=0.0D0
>         sig0(i)=0.0D0
>         sigc0(i)=0.0D0
>         do j=1,maxlob
>           bsc(j,i)=0.0D0
>           do k=1,3
>             censc(k,j,i)=0.0D0
221,222c224,225
< 	    do l=1,3
< 	      gaussc(l,k,j,i)=0.0D0
---
>             do l=1,3
>               gaussc(l,k,j,i)=0.0D0
225c228
< 	  nlob(i)=0
---
>           nlob(i)=0
265,266c268,269
< 	itype(i)=0
< 	itel(i)=0
---
>         itype(i)=0
>         itel(i)=0
273c276
< 	iss(i)=0
---
>         iss(i)=0
276c279
< 	dhpb(i)=0.0D0
---
>         dhpb(i)=0.0D0
279,280c282,283
< 	ihpb(i)=0
< 	jhpb(i)=0
---
>         ihpb(i)=0
>         jhpb(i)=0
285,286c288,289
<         b1(k,i)=0.0
<         b2(k,i)=0.0
---
> !       b1(k,i)=0.0
> !       b2(k,i)=0.0
307a311
>       time_memclear=timer_value()-time0
353d356
<       scal_force=1.0d0
385,386c388,389
< !         29       30       31 
<      3   "WDFAT","WDFAN","WDFAB"/
---
> !         29       30       31      32
>      3   "WDFAT","WDFAN","WDFAB","WSS  "/
418c421,422
<      1   "EDFABET"/
---
>      1   "EDFABET",
>      2   "ESS" /
423c427
<      & 24,15,26,27,28,29,30,31,22,23,25,20/
---
>      & 24,15,26,27,28,29,30,31,22,23,25,20,0/
427c431
<      & 24,15,26,27,28,29,30,31,22,23,25,20,0/
---
>      & 24,15,26,27,28,29,30,31,22,23,25,20,2*0/
431c435
<      & 24,15,26,27,28,29,30,31,22,23,25,20,0/
---
>      & 24,15,26,27,28,29,30,31,22,23,25,20,2*0/
435c439
<      & 24,15,26,27,28,29,30,31,22,23,25,20,2*0/
---
>      & 24,15,26,27,28,29,30,31,22,23,25,20,3*0/
441c445
<      & 24,15,26,27,22,23,25,20,4*0/
---
>      & 24,15,26,27,22,23,25,20,5*0/
445c449
<      & 24,15,26,27,22,23,25,20,5*0/
---
>      & 24,15,26,27,22,23,25,20,6*0/
449c453
<      & 24,15,26,27,22,23,25,20,5*0/
---
>      & 24,15,26,27,22,23,25,20,6*0/
453c457
<      & 24,15,26,27,22,23,25,20,6*0/
---
>      & 24,15,26,27,22,23,25,20,7*0/
473d476
<       include 'COMMON.NMR'
687c690
<         write (*,'(a)') 'Processor',fg_rank,' CG group',kolor,
---
>         write (*,*) 'Processor',fg_rank,' CG group',kolor,
808,834d810
<         if (atimeave_save.gt.0 .and. nfgtasks.gt.1) then
<           call MPI_AllGather(idihconstr_end,1,MPI_Integer,
<      &      idispls_dihconstr0,1,MPI_Integer,FG_COMM,IERROR)
<           icount_dihconstr0(0)=idispls_dihconstr0(0)
< c          write (iout,*) "idispls_dihconstr0",
< c     &     idispls_dihconstr0(0:nfgtasks-1)
<           do i=1,nfgtasks-1
<             icount_dihconstr0(i)=
<      &       idispls_dihconstr0(i)-idispls_dihconstr0(i-1)
<           enddo
<           do i=nfgtasks-1,1,-1
<             idispls_dihconstr0(i)=idispls_dihconstr0(i-1)
<           enddo
<           idispls_dihconstr0(0)=0
<           do i=0,nfgtasks-1
<             icount_dihconstr(i)=2*icount_dihconstr0(i)
<             idispls_dihconstr(i)=2*idispls_dihconstr0(i)
<           enddo
<           if (me.eq.king .or. .not.out1file) then
<           write (iout,*) "icount_dihconstr, idispls_dihconstr"
<           do i=0,nfgtasks-1
<             write (iout,'(5i6)') i,icount_dihconstr(i),
<      &       idispls_dihconstr(i),icount_dihconstr0(i),
<      &       idispls_dihconstr0(i)
<           enddo
<           endif
<         endif
842,868d817
<         if (atimeave_save.gt.0 .and. nfgtasks.gt.1) then
<           call MPI_AllGather(ithetaconstr_end,1,MPI_Integer,
<      &      idispls_thetaconstr0,1,MPI_Integer,FG_COMM,IERROR)
< c          write (iout,*) "idispls_thetaconstr0",
< c     &     idispls_thetaconstr0(0:nfgtasks-1)
<           icount_thetaconstr0(0)=idispls_thetaconstr0(0)
<           do i=1,nfgtasks-1
<             icount_thetaconstr0(i)=
<      &       idispls_thetaconstr0(i)-idispls_thetaconstr0(i-1)
<           enddo
<           do i=nfgtasks-1,1,-1
<             idispls_thetaconstr0(i)=idispls_thetaconstr0(i-1)
<           enddo
<           idispls_thetaconstr0(0)=0
<           do i=0,nfgtasks-1
<             icount_thetaconstr(i)=2*icount_thetaconstr0(i)
<             idispls_thetaconstr(i)=2*idispls_thetaconstr0(i)
<           enddo
<           if (me.eq.king .or. .not.out1file) then
<           write (iout,*) "icount_thetaconstr, idispls_thetaconstr"
<           do i=0,nfgtasks-1
<             write (iout,'(5i6)') i,icount_thetaconstr(i),
<      &       idispls_thetaconstr(i),icount_thetaconstr0(i),
<      &       idispls_thetaconstr0(i)
<           enddo
<           endif
<         endif
1802d1750
<       write (iout,*) "npeak",npeak," nhpb_peak",nhpb_peak
1856d1803
<       endif
1862d1808
<       if (me.eq.king .or. fg_rank.eq.0 .and. .not.out1file) then
1867,1891d1812
<         write (iout,*) "NMR_peak_partition: atimeave",atimeave_save
<       endif
<       if (atimeave_save.gt.0) then 
<         icount_nmraveave(0)=ipeak(1,peak_bound(2,0))
<         do i=1,nfgtasks-1
<           icount_nmraveave(i)=ipeak(2,peak_bound(2,i))
<      &      -ipeak(1,peak_bound(1,i))+1
<         enddo
<         idispls_nmraveave(0)=0
<         do i=1,nfgtasks-1
<           idispls_nmraveave(i)=idispls_nmraveave(i-1)
<      &      +icount_nmraveave(i-1)
<         enddo
<         do i=0,nfgtasks-1
<           icount_nmrave(i)=2*icount_nmraveave(i)
<           idispls_nmrave(i)=2*idispls_nmraveave(i)      
<         enddo
<         if (me.eq.king .or. .not.out1file) then
<         write (iout,*) "icount_nmraveave, idispls_nmraveave",
<      &   " icount_nmrave, idispls_nmrave"
<         do i=0,nfgtasks-1
<           write (iout,'(5i5)') i,icount_nmraveave(i),
<      &      idispls_nmraveave(i),icount_nmrave(i),idispls_nmrave(i)
<         enddo
<         endif
intcartderiv.F
34a35,37
> #ifdef FNLOG
>       write(*,*)'intcartderiv'
> #endif
65,66d67
<         !cost=dcos(theta(i))
<         !sint=sqrt(1-cost*cost)
98,99d98
<         !sint=dsin(theta(i))
<         !sint1=dsin(theta(i-1))
103,104d101
<         !cost=dcos(theta(i))
<         !cost1=dcos(theta(i-1))
194,195d190
<         !cost1=dcos(omicron(1,i))
<         !sint1=sqrt(1-cost1*cost1)
198,199d192
<         !cost2=dcos(omicron(2,i))
<         !sint2=sqrt(1-cost2*cost2)
250,251d242
<           !sint=dsin(theta(i))
<           !sint1=dsin(omicron(2,i-1))
254d244
<           !sing=dsin(tauangle(1,i))
256,257d245
<           !cost=dcos(theta(i))
<           !cost1=dcos(omicron(2,i-1))
260d247
<           !cosg=dcos(tauangle(1,i))
316,317d302
<             !sint=dsin(omicron(1,i))
<             !sint1=dsin(theta(i-1))
320d304
<             !sing=dsin(tauangle(2,i))
322,323d305
<             !cost=dcos(omicron(1,i))
<             !cost1=dcos(theta(i-1))
326d307
<             !cosg=dcos(tauangle(2,i))
383,384d363
<           !sint=dsin(omicron(1,i))
<           !sint1=dsin(omicron(2,i-1))
387d365
<           !sing=dsin(tauangle(3,i))
389,390d366
<           !cost=dcos(omicron(1,i))
<           !cost1=dcos(omicron(2,i-1))
393d368
<           !cosg=dcos(tauangle(3,i))
613c588
< 
---
> #ifdef VIRTUAL
775c750
< 
---
> #endif
intcor.f
int_from_cart.F
27a28
>       !write(*,*)'int_from_cart'
161a163
>         !write(*,'(a,i4,2f18.12)')'LP',i+1,costtab(i+1),sinttab(i+1)
int_to_cart.f
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/int_to_cart.f: No such file or directory
iounit.f
iperm.f
keys.f
kinetic_CASC.F
25a26,28
> #ifdef FNLOG
>       write(*,*)'kinetic_casc'
> #endif
43c46
<           v(j)=0.5d0*(d_t(j,i)+d_t(j,i+1))
---
>           v(j)=0.5d0*(vel(j,i)+vel(j,i+1))
44a48,50
> #ifdef KINETICLOG
>         write(*,'(a,i4,3f18.12)')'KCa',i,v(:)
> #endif
56,58c62,65
<           do j=1,3
<             v(j)=d_t(j,i)
<           enddo  
---
>           !do j=1,3
>           !  v(j)=vel(j,i)
>           !enddo  
>           v(:)=vel(:,i)
61,63c68,71
<           do j=1,3
<             v(j)=d_t(j,nres+i)
<           enddo
---
>           !do j=1,3
>           !  v(j)=vel(j,nres+i)
>           !enddo
>           v(:)=vel(:,nres+i)
66a75,77
> #ifdef KINETICLOG
>         write(*,'(a,i4,4f18.12)')'KCb',i,v(:),msc(iti)
> #endif
74c85
<            incr(j)=d_t(j,i+1)-d_t(j,i)
---
>            incr(j)=vel(j,i+1)-vel(j,i)
77a89,91
> #ifdef KINETICLOG
>          write(*,'(a,i4,3f18.12)')'KCc',i,incr(:)
> #endif
88c102
<              incr(j)=d_t(j,nres+i)-d_t(j,i)
---
>              incr(j)=vel(j,nres+i)-vel(j,i)
90a105,107
> #ifdef KINETICLOG
>            write(*,'(a,i4,3f18.12)')'KCd',i,incr(:)
> #endif
102a120
> !     write(*,'(a,5f18.12)')'KT',KE_total,KEt_p, KEt_sc, KEr_p, KEr_sc
kinetic_lesyng.F
92c92
<      &   +incr(3)*incr(3))
---
>      &     +incr(3)*incr(3))
lagrangian_lesyng.F
39a40
>        include 'COMMON.PENTADIAG'
63a65
> #ifdef VIRTUAL
64a67
> #endif
153c156,157
< C Conevert d_a to virtual-bon-vector basis
---
> #ifdef VIRTUAL
> C Convert d_a to virtual-bon-vector basis
203a208,214
> #endif
> #ifdef LAGRANGELOG
>       do i=0,nres
>         write(*,'(a,i4,3f18.12)')'LAda ',i,d_a(:,i)
>         write(*,'(a,i4,3f18.12)')'LAdan',i,d_a(:,i+nres)
>       enddo
> #endif
296a308,431
>        subroutine lagrangian_penta
>        implicit none
>        include 'DIMENSIONS'
> #ifdef MPI
>        include 'mpif.h'
> #else
>        double precision tcpu
> #endif
>        double precision time00
>        include 'COMMON.VAR'
>        include 'COMMON.CHAIN'
>        include 'COMMON.DERIV'
>        include 'COMMON.GEO'
>        include 'COMMON.LOCAL'
>        include 'COMMON.INTERACT'
>        include 'COMMON.MD'
>        include 'COMMON.LAGRANGE.5diag'
> #ifdef LANG0
>        include 'COMMON.LANGEVIN.lang0.5diag'
> #else
>        include 'COMMON.LANGEVIN'
> #endif
>        include 'COMMON.IOUNITS'
>        include 'COMMON.CONTROL'
>        include 'COMMON.MUCA'
>        include 'COMMON.TIME1'
>        include 'COMMON.PENTADIAG'
> 
>        integer i,j,ind
>        integer nind,innt,inct,ichain,n
> 
> #ifdef TIMING
> #ifdef MPI
>        time00=MPI_Wtime()
> #else
>        time00=tcpu()
> #endif
> #endif
> !#ifdef FIVEDIAG
> #ifdef VIRTUAL
>       call grad_transform
> #endif
> 
> !$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(ind)
>       do i=1,nres
>         ind=penta_c_to_matrix(i)
>         if(ind.lt.0) cycle
>         if (itype(i).eq.10)then
>           penta_solv_rs(ind,1:3)=-gcart(1:3,i)-gxcart(1:3,i)
>         else
>           penta_solv_rs(ind,1:3)=-gcart(1:3,i)
>           penta_solv_rs(ind+1,1:3)=-gxcart(1:3,i)
>         endif
>       enddo
> 
>       do j=1,3
>         call penta_solve(penta_size,
>      &    penta_solv_rs(:,j),penta_solv_result(:,j))
>       enddo
> 
>       d_a(:,0)=0.0d0
> !$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(ind)
>       do i=1,nres
>         ind=penta_c_to_matrix(i)
>         if(ind.lt.0) then
>           d_a(1:3,i)=0.0d0
>           d_a(1:3,i+nres)=0.0d0
>           cycle
>         endif
>         d_a(1:3,i)=penta_solv_result(ind,1:3)
>         if (itype(i).eq.10) then
>           d_a(1:3,i+nres)=0.0d0
>         else
>           d_a(1:3,i+nres)=penta_solv_result(ind+1,1:3)
>         endif
>       enddo
> 
> #ifdef VIRTUAL
> C Convert d_a to virtual-bon-vector basis
>       if (nnt.eq.1) then
>         d_a(:,0)=d_a(:,1)
>       endif
>       do i=1,nres
>         if (itype(i).eq.10 .or. itype(i).eq.ntyp1) then
>           do j=1,3
>             d_a(j,i)=d_a(j,i+1)-d_a(j,i)
>           enddo
>         else
>           do j=1,3
>             d_a(j,i+nres)=d_a(j,i+nres)-d_a(j,i)
>             d_a(j,i)=d_a(j,i+1)-d_a(j,i)
>           enddo
>         end if
>       enddo
> 
>       d_a(:,nres)=0.0d0
>       d_a(:,nct)=0.0d0
>       d_a(:,2*nres)=0.0d0
> c      write (iout,*) "Shifting accelerations"
>       if (nnt.gt.1) then
>         d_a(:,0)=d_a(:,1)
>         d_a(:,1)=0.0d0
>       endif
>       do ichain=2,nchain
>         d_a(:,chain_border1(1,ichain)-1)=d_a(:,chain_border1(1,ichain))
>         d_a(:,chain_border1(1,ichain))=0.0d0
>       enddo
> c      write (iout,*) "Adding accelerations"
>       do ichain=2,nchain
>         d_a(:,chain_border1(1,ichain)-1)=
>      &  d_a(:,chain_border1(1,ichain)-1)+d_a(:,chain_border(2,ichain-1))
>         d_a(:,chain_border(2,ichain-1))=0.0d0
>       enddo
> #endif
> #ifdef TIMING
> #ifdef MPI
>       time_lagrangian=time_lagrangian+MPI_Wtime()-time00
> #else
>       time_lagrangian=time_lagrangian+tcpu()-time00
> #endif
> #endif
>       return
>       end
> c------------------------------------------------------------------
331a467,470
> #ifdef WITH_CUDA
>       include 'COMMON.PENTADIAG'
>       logical uses_cuda
> #endif
463a603,630
> 
> #ifdef WITH_CUDA
>       if (uses_cuda()) then
>         penta_depth=0
>         penta_size=0
>         penta_c_to_matrix(0:nres)=-1
>         do ichain=1,nchain
>           n=dimen_chain(ichain)
>           innt=iposd_chain(ichain)
>           ind=innt-1
>           chain_border_ind(1,ichain)=ind+1
>           do i=chain_border(1,ichain),chain_border(2,ichain)
>             penta_c_to_matrix(i)=ind
>             if (itype(i).eq.10)then
>               ind=ind+1
>               ind_to_chain(ind)=ichain
>             else
>               ind=ind+2
>               ind_to_chain(ind-1)=ichain
>               ind_to_chain(ind)=ichain
>             endif
>           enddo
>           chain_border_ind(2,ichain)=ind
>           penta_size=max(penta_size,ind)
>         enddo
>       endif
> #endif
> 
487c654,659
< !       call fdisyp_precalc(n,dm(innt:inct-1),dminv(innt:inct-1))
---
> #ifdef WITH_CUDA
>         if (uses_cuda()) then
>           call penta_prepare_chain(n,innt,
>      &      DMorig(innt),DU1orig(innt),DU2orig(innt))
>         endif
> #endif
586c758
<       
---
> 
587a760,764
> #ifdef WITH_CUDA
>       if (uses_cuda()) then
>         call push_lagrangian_data_to_accelerator
>       endif
> #endif
1046a1224
> #ifdef VIRTUAL
1103a1282,1305
> #else
> #ifdef DEBUG
>       write (iout,*) "accel in fivediaginv_mult: 1"
>       do i=0,2*nres
>         write(iout,'(i5,3f10.5)') i,(accel(j,i),j=1,3)
>       enddo
> #endif
>       ind=0
>       do i=nnt,nct
>         do j=1,3
>           d_a_vec(ind+j)=accel(j,i)
>         enddo
>         ind=ind+3
>       enddo
>       do i=nnt,nct
>         if (itype(i).eq.ntyp1) cycle
>         if (itype(i).ne.10) then
>           do j=1,3
>             d_a_vec(ind+j)=accel(j,i+nres)
>           enddo
>           ind=ind+3
>         endif
>       enddo
> #endif
lbfgs.F
linmin.f
lipid.F
8,10c8,10
< !$OMP PARALLEL DO DEFAULT(NONE)
< !$OMP& PRIVATE(i)
< !$OMP& SHARED(nres,cp,lipid_cache)
---
>       double precision half_lipscale
>       half_lipscale=0.5d0*lipscale
> !$OMP PARALLEL DO DEFAULT(SHARED)
13c13,22
<      &                   lipid_cache(i).ss,lipid_cache(i).ssgrad)
---
>      &                   lipid_cache_ss(i),lipid_cache_ssgrad(i))
>         if (itype(i).ne.ntyp1) then
>           lipid_cache_ss_ns(i)=lipid_cache_ss(i)
>           lipid_cache_ssgrad_ns(i)=lipid_cache_ssgrad(i)
>         else
>           lipid_cache_ss_ns(i)=0.0d0
>           lipid_cache_ssgrad_ns(i)=0.0d0
>         endif
>         lipid_cache_ss(i)=lipid_cache_ss(i)*half_lipscale
>         lipid_cache_ssgrad(i)=lipid_cache_ssgrad(i)*half_lipscale
17,19c26
< !$OMP PARALLEL DO DEFAULT(NONE)
< !$OMP& PRIVATE(i)
< !$OMP& SHARED(nres,c_tobox,lipid_cache)
---
> !$OMP PARALLEL DO DEFAULT(SHARED)
24,25c31,39
<      &                   lipid_cache(nres+i).ss,
<      &                   lipid_cache(nres+i).ssgrad)
---
>      &                   lipid_cache_ss(nres+i),
>      &                   lipid_cache_ssgrad(nres+i))
>         if (itype(i).ne.ntyp1) then
>           lipid_cache_ss_ns(i+nres)=lipid_cache_ss(i+nres)
>           lipid_cache_ssgrad_ns(i+nres)=lipid_cache_ssgrad(i+nres)
>         else
>           lipid_cache_ss_ns(i+nres)=0.0d0
>           lipid_cache_ssgrad_ns(i+nres)=0.0d0
>         endif
list_merge.F
3c3
<       include "DIMENSIONS"
---
>       include 'DIMENSIONS'
7,12c7,12
<       include "COMMON.SETUP"
<       include "COMMON.CONTROL"
<       include "COMMON.CHAIN"
<       include "COMMON.INTERACT"
<       include "COMMON.SPLITELE"
<       include "COMMON.IOUNITS"
---
>       include 'COMMON.SETUP'
>       include 'COMMON.CONTROL'
>       include 'COMMON.CHAIN'
>       include 'COMMON.INTERACT'
>       include 'COMMON.SPLITELE'
>       include 'COMMON.IOUNITS'
local_move.f
loc_precalc.F
19a20
> !     write(*,*)'loc_prep'
23a25,26
> !     write(*,'(a,i4,2f18.12)')'LP',nres+1,
> !    &  costtab(nres+1),sinttab(nres+1)
29a33
>         !write(*,'(a,i4,2f18.12)')'LP',i+1,costtab(i+1),sinttab(i+1)
machpd.f
make_list_new.F
2,4c2,6
<      & c1,c2,ityp1,ityp2,itypint,g_list_start,g_list_end,newcontlisti,
<      & newcontlistj,
<      & positive_gridi,positive_gridj,gridcelli,gridrangej,griddataj)
---
>      &  c1,c2,ityp1,ityp2,itypint,
>      &  g_list_start,g_list_end,newcontlisti,newcontlistj,
>      &  gridcelli,gridrangej,griddataj,irange)
>       use omp_lib
>       use omp_data
18a21
>       integer irange
24,25c27
<       double precision dist_init,dist_temp,r_cut_all,
<      &                 r_cut_all2
---
>       double precision dist_init,r_cut_all
30,35c32,33
<       integer liststart(0:2*maxres+1)
<       integer ranges(2,maxres*maxint_gr)
<       integer*8 ntotint
<       integer i,j,k,l,itypi,itypj,subchap,xshift,yshift,zshift,iint,
<      &        ilist_sc,g_ilist_sc,ii,jj,irowi,irowj,g_rowj
<       integer cnt,rngcount,listbase,contbase
---
>       integer i,j,k,itypi,itypj,iint,ii,jj,irowi,irowj
>       integer cnt,rngcount,listbase
37,39d34
<       integer positive_gridi(grid_length)
<       integer positive_gridj(grid_length)
<       integer pgridi,pgridj,skipcount
43,46c38,39
<       integer r1lo,r1hi,r2lo,r2hi
<       logical are_near,skip
<       common /makelistcommon/ contlisti,contlistj,
<      &                        listi,listj,liststart
---
>       integer r1lo,r1hi,r2lo,r2hi,my_thread,iintlo,iinthi
>       common /makelistcommon/ contlisti,contlistj,listi,listj
59d51
< c     r_cut_all2=r_cut_all/dsqrt(2.0d0)
65,67c57,58
< !$OMP& PRIVATE(dist_init,xyzi,rij,itypi,itypj,ranges,rngcount,listbase,
< !$OMP&         jj,iint,j,pgridj,pgridi,skipcount,k,r1lo,r1hi,r2lo,r2hi,
< !$OMP&         are_near,skip,l)
---
> !$OMP& PRIVATE(dist_init,xyzi,rij,itypi,itypj,rngcount,listbase,
> !$OMP&         jj,iint,j,k,r1lo,r1hi,r2lo,r2hi,my_thread,iintlo,iinthi)
68a60
>         my_thread=omp_get_thread_num()+1
76c68,73
< !       pgridi=positive_gridi(i*2)
---
>           !if(itypint.eq.3)then
>           !  if (dist_init.gt.threshold) then
>           !    write(*,'(a,i4,3f18.12)')'Hp1:',i,c1(:,i)
>           !    write(*,'(a,i4,3f18.12)')'Hp2:',i,c2(:,i)
>           !  endif
>           !endif
88,91d84
< 
< #ifdef GRIDSTAT
<         !gridallcount=gridallcount+iend(i,iint)-istart(i,iint)+1
< #endif
94c87,90
<         do iint=gridrangej(k-1)+1,gridrangej(k)
---
>         iintlo=gridrangej(k-1)+1
>         iinthi=gridrangej(k)
>         !do iint=gridrangej(k-1)+1,gridrangej(k)
>         do iint=iintlo,iinthi
95a92
>           if((i-irange.lt.j) .and. (j.lt.i+irange)) cycle
106,115c103,118
<           itypj=iabs(ityp2(j))
<           if (itypj.eq.ntyp1 .or. itypj.eq.0) then
<             !TODO use skipcount
<             !j=j+1
< #ifdef GRIDSTAT
<             gridtypecuts=gridtypecuts+1
< #endif
<             cycle
<           endif
<           !pgridj=positive_gridj(j*2)
---
>           ! below is already filtered by grid
>           !itypj=iabs(ityp2(j))
>           !if (itypj.eq.ntyp1 .or. itypj.eq.0) then
>           !  cycle
>           !endif
> 
>           call wrapvec(c2(:,j)-xyzi,rij)
>           dist_init=sum(rij*rij)
> !         if(itypint.eq.2)then
> !           if (dist_init.gt.threshold) then
> !             write(*,'(a,2i4,a,f18.12)')'H:',i,j,' F ',dist_init
> !           else
> !             write(*,'(a,2i4,a,f18.12)')'H:',i,j,' T ',dist_init
> !           endif
> !         endif
>           if (dist_init.gt.threshold) cycle
117,142d119
<           !if (pgridi.eq.pgridj) then
< !          if (.false.) then
< !            skipcount=min(positive_gridj(j*2+1),iend(i,iint)-j+1)
< !            !write(*,*)'POS HIT',skipcount
< !#ifdef GRIDSTAT
< !            posgridhits=posgridhits+1
< !            posgridjumps=posgridjumps+skipcount-1
< !#endif
< !
< !          else
<             skipcount=1
<             call wrapvec(c2(:,j)-xyzi,rij)
<             dist_init=sum(rij*rij)
<             if (dist_init.gt.threshold) then
<               j=j+1
<               !write(*,*)'MISS SKIP'
< #ifdef GRIDSTAT
<               gridmiss=gridmiss+1
< #endif
<               cycle
<             endif
<             skipcount=1
< #ifdef GRIDSTAT
<             gridpass=gridpass+1
< #endif
< !          endif
146c123,124
<             ranges(1,rngcount)=j
---
>             !ranges(1,rngcount)=j
>             workspace(my_thread).item.list_ranges(rngcount*2-1)=j
159,161c137,139
<           j=j+skipcount
<           ranges(2,rngcount)=j-1
<           jj=j
---
>           !ranges(2,rngcount)=j-1
>           workspace(my_thread).item.list_ranges(rngcount*2-0)=j
>           jj=j+1
174c152,157
<           listj(:,listbase+j)=ranges(:,j)
---
>           !listj(:,listbase+j)=ranges(:,j)
>           !listj(:,listbase+j)=list_ranges(my_thread).array(j*2-1:j*2-0)
>           listj(1,listbase+j)=
>      &      workspace(my_thread).item.list_ranges(j*2-1)
>           listj(2,listbase+j)=
>      &      workspace(my_thread).item.list_ranges(j*2-0)
175a159,161
> !       call i_copy(listj(1,listbase+1),
> !    &              list_ranges(my_thread).array(1),
> !    &              2*rngcount)
178,187c164,188
<       irowi=0
<       contlisti(:,0)=0
<       liststart(0)=1
<       do i=iat_s,iat_e
<         cnt=listi(2,i-iat_s)
<         liststart(i-iat_s+1)=liststart(i-iat_s)+cnt
<         if(cnt.eq.0) cycle
<         irowi=irowi+1
<         contlisti(1,irowi)=i
<         contlisti(2,irowi)=cnt
---
>       call sort_inter_list(iat_s,iat_e,listi,listj,contlisti,contlistj,
>      &               irowi,irowj)
> 
>       call list_merge_tail(itypint,irowi,irowj,contlisti,contlistj,
>      &                     g_list_start,g_list_end,
>      &                     newcontlisti,newcontlistj)
> 
> !     if(itypint.eq.3)then
> !       write(*,*)'list',textint(itypint)
> !       do k=g_list_start,g_list_end
> !         i=newcontlisti(1,k)
> !         ii=newcontlisti(2,k-1)+1
> !         jj=newcontlisti(2,k)
> !         do j=ii,jj
> !           write(*,*)'blk',i,newcontlistj(1,j),newcontlistj(2,j)
> !         enddo
> !       enddo
> !     endif
>       return
>       end
>       subroutine i_copy(to,from,n)
>       implicit none
>       integer i,n,to(n),from(n)
>       do i=1,n
>         to(i)=from(i)
189c190,256
<       irowj=liststart(iat_e-iat_s+1)-1
---
>       return
>       end
> 
> C----------------------------------------------------------------------
>       subroutine make_inter_list_split(iat_s,iat_e,nint_gr,istart,iend,
>      &  c1,c2,ityp1,ityp2,itypint,
>      &  g_list_start,g_list_end,newcontlisti,newcontlistj,
>      &  g_list2_start,g_list2_end,newcontlist2i,newcontlist2j,
>      &  mask,
>      &  gridcelli,gridrangej,griddataj)
>       use omp_lib
>       use omp_data
>       implicit none
>       include "DIMENSIONS"
> #ifdef MPI
>       include 'mpif.h'
>       include "COMMON.SETUP"
> #endif
>       include "COMMON.CONTROL"
>       include "COMMON.CHAIN"
>       include "COMMON.SPLITELE"
>       include "COMMON.IOUNITS"
>       include "COMMON.GRID"
>       integer parallel_chunk_size
>       parameter (parallel_chunk_size=16)
>       integer iat_s,iat_e,nint_gr(maxres),istart(maxres,maxint_gr),
>      & iend(maxres,maxint_gr),ityp1(maxres),ityp2(maxres),itypint,
>      & g_list_start,g_list_end,newcontlisti(2,0:10*maxres),
>      & newcontlistj(2,maxint_res*maxres)
>       integer g_list2_start,g_list2_end,newcontlist2i(2,0:10*maxres),
>      & newcontlist2j(2,maxint_res*maxres)
>       logical mask(maxres) 
>       double precision c1(3,maxres),c2(3,maxres)
>       double precision dist_init,r_cut_all
>       double precision xyzi(3),rij(3)
>       double precision threshold
>       integer contlisti(2,0:10*maxres),contlistj(2,maxint_res*maxres)
>       integer jj,rngcount,irowi,irowj
>       integer listi(2,0:2*maxres),listj(2,maxint_res*maxres)
>       integer jj2,rngcount2,irowi2,irowj2
>       integer list2i(2,0:2*maxres),list2j(2,maxint_res*maxres)
>       integer i,j,k,itypi,itypj,iint
>       integer listbase
>       integer ierr
>       integer gridrangej(-2:max_grid_cells)
>       integer griddataj(27*maxres)
>       integer gridcelli(-1:maxres)
>       integer r1lo,r1hi,r2lo,r2hi,my_thread
>       common /makelistcommon/ contlisti,contlistj,
>      &                        listi,listj,list2i,list2j
>       character*4 textint(3) /'SCSC','SCp ','pp  '/
> 
> #ifdef DEBUG
>       write (iout,*) "make_inter_list_split ",textint(itypint),
>      &               " maxint_res",maxint_res
>       write (iout,*) "iat_s",iat_s," iat_e",iat_e
> #endif
>       g_list_start=0
>       g_list_end=0
>       g_list2_start=0
>       g_list2_end=0
> 
>       r_cut_all=r_cut_int+r_buff_list
>       threshold=r_cut_all*r_cut_all
>       irowi=0
>       irowj=0
>       irowj2=0
192c259,261
< !$OMP& PRIVATE(cnt,listbase,contbase,j)
---
> !$OMP& PRIVATE(dist_init,xyzi,rij,itypi,itypj,
> !$OMP&         rngcount,rngcount2,listbase,
> !$OMP&         jj,jj2,iint,j,k,r1lo,r1hi,r2lo,r2hi,my_thread)
194,199c263,344
<         cnt=listi(2,i-iat_s)
<         if(cnt.eq.0) cycle
<         listbase=listi(1,i-iat_s)-1
<         contbase=liststart(i-iat_s)-1
<         do j=1,cnt
<           contlistj(:,contbase+j)=listj(:,listbase+j)
---
>         my_thread=omp_get_thread_num()+1
>         listi(2,i-iat_s)=0
>         list2i(2,i-iat_s)=0
>         itypi=ityp1(i)
>         if (itypi.eq.ntyp1 .or. itypi.eq.0) cycle
>         xyzi=c1(:,i)
>         rngcount=0
>         rngcount2=0
>         jj=-1
>         jj2=-1
> 
>         r1lo=istart(i,1)
>         r1hi=iend(i,1)
>         if(nint_gr(i).gt.1) then
>           r2lo=istart(i,2)
>           r2hi=iend(i,2)
>         else
>           r2lo=r1lo
>           r2hi=r1hi
>         endif
> 
>         k=gridcelli(i)
>         if(k.lt.0) cycle
>         do iint=gridrangej(k-1)+1,gridrangej(k)
>           j=griddataj(iint)
>           if((j.lt.r1lo .or. j.gt.r1hi) .and.
>      &       (j.lt.r2lo .or. j.gt.r2hi)) cycle
>           !itypj=iabs(ityp2(j))
>           !if (itypj.eq.ntyp1 .or. itypj.eq.0) then
>           !  cycle
>           !endif
> 
>           call wrapvec(c2(:,j)-xyzi,rij)
>           dist_init=sum(rij*rij)
>           if (dist_init.gt.threshold) then
>             j=j+1
>             cycle
>           endif
> 
>           if (mask(i).and.mask(j)) then
>             if (j.ne.jj2) then
>               rngcount2=rngcount2+1
>               !ranges2(1,rngcount2)=j
>               workspace(my_thread).item.list_ranges2(rngcount2*2-1)=j
> #ifdef DEBUG
>               if (j.lt.jj2) then
>                 print *,"j index decreased in make_inter_list_",
>      &             textint(itypint)
> #ifdef MPI
>                 call MPI_Abort(MPI_COMM_WORLD,ierr)
> #else
>                 stop
> #endif
>               endif
> #endif
>             endif
>             j=j+1
>             !ranges2(2,rngcount2)=j-1
>             workspace(my_thread).item.list_ranges2(rngcount2*2-0)=j-1
>             jj2=j
>           else
>             if (j.ne.jj) then
>               rngcount=rngcount+1
>               !ranges(1,rngcount)=j
>               workspace(my_thread).item.list_ranges(rngcount*2-1)=j
> #ifdef DEBUG
>               if (j.lt.jj) then
>                 print *,"j index decreased in make_inter_list_",
>      &             textint(itypint)
> #ifdef MPI
>                 call MPI_Abort(MPI_COMM_WORLD,ierr)
> #else
>                 stop
> #endif
>               endif
> #endif
>             endif
>             j=j+1
>             !ranges(2,rngcount)=j-1
>             workspace(my_thread).item.list_ranges(rngcount*2-0)=j-1
>             jj=j
>           endif
201d345
<       enddo
203c347,642
<       call list_merge(irowi,irowj,contlisti,contlistj)
---
>         if (rngcount.gt.0) then
> !$OMP ATOMIC CAPTURE
>           listbase=irowj
>           irowj=irowj+rngcount
> !$OMP END ATOMIC
>           listi(1,i-iat_s)=listbase+1
>           listi(2,i-iat_s)=rngcount
>           do j=1,rngcount
>             !listj(:,listbase+j)=ranges(:,j)
>             listj(:,listbase+j)=
>      &        workspace(my_thread).item.list_ranges(j*2-1:j*2-0)
>           enddo
>         endif
> 
>         if (rngcount2.gt.0) then
> !$OMP ATOMIC CAPTURE
>           listbase=irowj2
>           irowj2=irowj2+rngcount2
> !$OMP END ATOMIC
>           list2i(1,i-iat_s)=listbase+1
>           list2i(2,i-iat_s)=rngcount2
>           do j=1,rngcount2
>             !list2j(:,listbase+j)=ranges2(:,j)
>             list2j(:,listbase+j)=
>      &        workspace(my_thread).item.list_ranges2(j*2-1:j*2-0)
>           enddo
>         endif
>       enddo   ! i
> 
>       call sort_inter_list(iat_s,iat_e,listi,listj,contlisti,contlistj,
>      &               irowi,irowj)
> 
>       call list_merge_tail(itypint,irowi,irowj,contlisti,contlistj,
>      &                     g_list_start,g_list_end,
>      &                     newcontlisti,newcontlistj)
> 
>       ! dyn_ss list
>       call sort_inter_list(iat_s,iat_e,list2i,list2j,
>      &                     contlisti,contlistj,irowi,irowj2)
> 
>       call list_merge_tail(itypint,irowi,irowj2,contlisti,contlistj,
>      &                     g_list2_start,g_list2_end,
>      &                     newcontlist2i,newcontlist2j)
> 
>       return
>       end
> 
> !C----------------------------------------------------------------------
> !      subroutine make_inter_list2(alo,ahi,iat_s,iat_e,
> !     &  nint_gr,istart,iend,
> !     &  c1,c2,ityp1,ityp2,itypint,
> !     &  g_list_start,g_list_end,newcontlisti,newcontlistj,
> !     &  seplisti,seplistj,
> !     &  gridcelli,gridrangej,griddataj,irange)
> !      use omp_lib
> !      use omp_data
> !      implicit none
> !      include "DIMENSIONS"
> !#ifdef MPI
> !      include 'mpif.h'
> !#endif
> !      include "COMMON.SETUP"
> !      include "COMMON.CONTROL"
> !      include "COMMON.CHAIN"
> !      include "COMMON.SPLITELE"
> !      include "COMMON.IOUNITS"
> !      include "COMMON.GRID"
> !      integer parallel_chunk_size
> !      parameter (parallel_chunk_size=16)
> !      integer iat_s,iat_e,itypint,g_list_start,g_list_end
> !      integer alo,ahi,irange
> !      integer nint_gr(maxres)
> !      integer istart(maxres,maxint_gr),iend(maxres,maxint_gr)
> !      integer ityp1(maxres),ityp2(maxres)
> !      integer newcontlisti(2,0:10*maxres)
> !      integer newcontlistj(2,maxint_res*maxres)
> !      integer seplisti(0:maxres),lseplisti(2,0:maxres)
> !      integer lseplisti2(0:maxres)
> !      integer seplistj(maxres*maxint_res),lseplistj(maxres*maxint_res)
> !      integer seplistidx,sepcount,sepbase,pa,pb
> !      double precision c1(3,maxres),c2(3,maxres)
> !      double precision dist_init,r_cut_all
> !      double precision xyzi(3),rij(3)
> !      double precision threshold
> !      integer contlisti(2,0:10*maxres)
> !      integer contlistj(2,maxint_res*maxres)
> !      integer listi(2,0:2*maxres)
> !      integer listj(2,maxint_res*maxres)
> !      integer gridrangej(-2:max_grid_cells)
> !      integer griddataj(27*maxres)
> !      integer gridcelli(-1:maxres)
> !      integer r1lo,r1hi,r2lo,r2hi,my_thread,iintlo,iinthi
> !      integer i,j,k,itypi,itypj,iint,ii,jj,irowi,irowj
> !      integer cnt,rngcount,listbase
> !      common /makelistcommon/ contlisti,contlistj,listi,listj,
> !     &                        lseplisti,lseplisti2,lseplistj
> !      logical lprn /.false./
> !      character*4 textint(3) /'SCSC','SCp ','pp  '/
> !
> !#ifdef DEBUG
> !      write (iout,*) "make_inter_list ",textint(itypint)," maxint_res",
> !     &  maxint_res
> !      write (iout,*) "iat_s",iat_s," iat_e",iat_e
> !#endif
> !      g_list_start=0
> !      g_list_end=0
> !      r_cut_all=r_cut_int+r_buff_list
> !      threshold=r_cut_all*r_cut_all
> !      irowi=0
> !      irowj=0
> !      ii=0
> !      seplistidx=0
> !
> !!$OMP PARALLEL DO DEFAULT(SHARED) SCHEDULE(DYNAMIC,parallel_chunk_size)
> !!$OMP& PRIVATE(dist_init,xyzi,rij,itypi,itypj,rngcount,listbase,
> !!$OMP&         jj,iint,j,k,r1lo,r1hi,r2lo,r2hi,my_thread,iintlo,iinthi,
> !!$OMP&         sepcount,pa,pb)
> !      do i=iat_s,iat_e
> !        my_thread=omp_get_thread_num()+1
> !        listi(2,i-iat_s)=0
> !        itypi=ityp1(i)
> !        if (itypi.eq.ntyp1 .or. itypi.eq.0) cycle
> !        xyzi=c1(:,i)
> !        rngcount=0
> !        sepcount=0
> !        jj=-1
> !
> !        r1lo=istart(i,1)
> !        r1hi=iend(i,1)
> !        if(nint_gr(i).gt.1) then
> !          r2lo=istart(i,2)
> !          r2hi=iend(i,2)
> !        else
> !          r2lo=r1lo
> !          r2hi=r1hi
> !        endif
> !
> !        k=gridcelli(i)
> !        if(k.lt.0) cycle
> !        iintlo=gridrangej(k-1)+1
> !        iinthi=gridrangej(k)
> !        do iint=iintlo,iinthi
> !          j=griddataj(iint)
> !          if((i-irange.lt.j) .and. (j.lt.i+irange)) cycle
> !          if((j.lt.r1lo .or. j.gt.r1hi) .and.
> !     &       (j.lt.r2lo .or. j.gt.r2hi)) cycle
> !          !itypj=iabs(ityp2(j))                      ! already filtered by grid
> !          !if (itypj.eq.ntyp1 .or. itypj.eq.0) cycle
> !
> !          call wrapvec(c2(:,j)-xyzi,rij)
> !          dist_init=sum(rij*rij)
> !          if (dist_init.gt.threshold) cycle
> !
> !          if (j.ne.jj) then             ! start of next group
> !#ifdef DEBUG
> !            if (j.lt.jj) then
> !              print *,"j index decreased in make_inter_list2_",
> !     &           textint(itypint)
> !              call terminate
> !            endif
> !#endif
> !            if(rngcount.gt.0) then
> !              pa=workspace(my_thread).item.list_ranges(rngcount*2-1)
> !              pb=workspace(my_thread).item.list_ranges(rngcount*2-0)
> !              if (pb-pa+1.le.sep_list_cutoff) then
> !                do k=pa,pb
> !                  sepcount=sepcount+1
> !                  workspace(my_thread).item.list_sep(sepcount)=k
> !                enddo
> !                rngcount=rngcount-1
> !              endif
> !            endif
> !            rngcount=rngcount+1
> !            workspace(my_thread).item.list_ranges(rngcount*2-1)=j
> !          endif
> !          workspace(my_thread).item.list_ranges(rngcount*2-0)=j
> !          jj=j+1
> !        enddo
> !
> !        ! check if last group is below sep threshold
> !        if (rngcount.gt.0) then 
> !          pa=workspace(my_thread).item.list_ranges(rngcount*2-1)
> !          pb=workspace(my_thread).item.list_ranges(rngcount*2-0)
> !          if (pb-pa+1.le.sep_list_cutoff) then
> !            do k=pa,pb
> !              sepcount=sepcount+1
> !              workspace(my_thread).item.list_sep(sepcount)=k
> !            enddo
> !            rngcount=rngcount-1
> !          endif
> !        endif
> !
> !        ! move groups to global buffer
> !        if (rngcount.gt.0) then
> !!$OMP ATOMIC CAPTURE
> !          listbase=irowj
> !          irowj=irowj+rngcount
> !!$OMP END ATOMIC
> !
> !          listi(1,i-iat_s)=listbase+1
> !          listi(2,i-iat_s)=rngcount
> !          do j=1,rngcount
> !            listj(1,listbase+j)=
> !     &        workspace(my_thread).item.list_ranges(j*2-1)
> !            listj(2,listbase+j)=
> !     &        workspace(my_thread).item.list_ranges(j*2-0)
> !          enddo
> !        endif
> !
> !        ! move individual interactions to global buffer
> !        if (sepcount.gt.0) then
> !!$OMP ATOMIC CAPTURE
> !          sepbase=seplistidx
> !          seplistidx=seplistidx+sepcount
> !!$OMP END ATOMIC
> !
> !          lseplisti(1,i)=sepbase+1
> !          lseplisti(2,i)=sepcount
> !          do j=1,sepcount
> !            seplistj(sepbase+j)=
> !     &        workspace(my_thread).item.list_sep(j)
> !          enddo
> !        endif
> !
> !      enddo   ! i
> !
> !      call sort_inter_list(iat_s,iat_e,listi,listj,contlisti,contlistj,
> !     &  irowi,irowj)
> !
> !      call sort_sep_inter_list(lseplisti,seplistj,lseplisti2,lseplistj,
> !     &  iat_s,iat_e,0,maxres,maxres*maxint_res)
> !
> !      call list_merge_tail(itypint,irowi,irowj,contlisti,contlistj,
> !     &  g_list_start,g_list_end,newcontlisti,newcontlistj)
> !
> !      call sep_list_merge_tail(itypint,lseplisti2,lseplistj,
> !     &  seplisti,seplistj,iat_s,iat_e,0,nres,maxres*maxint_res)
> !
> !      ! count interactions per site
> !      ! ranges_interactions=count interactions in ranges
> !      ! separate_interactions=count separate interactions
> !      ! total_iteractions=ranges_interactions+separate_interactions
> !      ! call split_work_for_threads(1,total_interactions,low,high,
> !      !                             fg_rank+1,nfgtasks)
> !
> !      if(itypint.eq.3)then
> !        write(*,*)fg_rank,'list',textint(itypint),iat_s,iat_e
> !        !do k=g_list_start,g_list_end
> !        do k=alo,ahi
> !          i=newcontlisti(1,k)
> !          ii=newcontlisti(2,k-1)+1
> !          jj=newcontlisti(2,k)
> !          do j=ii,jj
> !            write(*,*)fg_rank,'blk',i,
> !     &                newcontlistj(1,j),newcontlistj(2,j)
> !          enddo
> !        enddo
> !        write(*,*)fg_rank,'list',textint(itypint),'sep'
> !        !do i=iat_s,iat_e
> !        do i=alo,ahi
> !          do j=seplisti(i-1)+1,seplisti(i)
> !            write(*,*)fg_rank,'sep',i,seplistj(j),'idx',j
> !          enddo
> !        enddo
> !      endif
> !      return
> !      end
> 
> C----------------------------------------------------------------------
>       subroutine list_merge_tail(itypint,
>      &                          irowi,irowj,contlisti,contlistj,
>      &                          g_list_start,g_list_end,
>      &                          newcontlisti,newcontlistj)
>       implicit none
>       include "DIMENSIONS"
> #ifdef MPI
>       include 'mpif.h'
>       include "COMMON.SETUP"
> #endif
>       include "COMMON.CHAIN"
>       include "COMMON.CONTROL"
>       include "COMMON.IOUNITS"
>       integer itypint,irowi,irowj,i
>       integer g_list_start,g_list_end
>       integer contlisti(2,0:10*maxres),contlistj(2,maxint_res*maxres)
>       integer newcontlisti(2,0:10*maxres)
>       integer newcontlistj(2,maxint_res*maxres)
>       integer g_ilist_sc,g_rowj
>       integer*8 ntotint
>       integer ierr
>       character*4 textint(3) /'SCSC','SCp ','pp  '/
> 
>       !!call list_merge(irowi,irowj,contlisti,contlistj)
>       call fg_merge_list_of_pairs(itypint,irowi,irowj,
>      &  contlisti,contlistj)
> 
213c652
<       g_ilist_sc=irowi
---
> !     g_ilist_sc=irowi
222a662
> 
289a730,888
> 
> !C----------------------------------------------------------------------
> !      subroutine sep_list_merge_tail(itypint,
> !     &                               inlisti,inlistj,outlisti,outlistj,
> !     &                               iat_s,iat_e,ilo,ihi,maxj)
> !      implicit none
> !      include "DIMENSIONS"
> !#ifdef MPI
> !      include 'mpif.h'
> !      include "COMMON.SETUP"
> !#endif
> !      include "COMMON.CHAIN"
> !      include "COMMON.CONTROL"
> !      include "COMMON.IOUNITS"
> !
> !      integer itypint,iat_s,iat_e,ilo,ihi,maxj
> !      integer outlisti(ilo:ihi),inlisti(ilo:ihi)
> !      integer outlistj(maxj),inlistj(maxj)
> !      integer counts(0:max_fg_procs)
> !      integer offsets(0:max_fg_procs)
> !      integer offset,my_count,total_interactions
> !      integer i,ierror
> !
> !      inlisti(ilo:iat_s-1)=0
> !      inlisti(iat_e+1:ihi)=0
> !      my_count=sum(inlisti(iat_s:iat_e))
> !
> !      if(my_count.gt.maxj) then
> !        call too_many_interactions(itypint,my_count)
> !      endif
> !
> !#ifdef MPI
> !      if (nfgtasks.gt.1) then
> !        call MPI_Allgather(my_count,1,MPI_INTEGER, 
> !     &    counts,1,MPI_INTEGER,FG_COMM,ierror)
> !        offset=0
> !        do i=0,nfgtasks-1
> !          offsets(i)=offset
> !          offset=offset+counts(i)
> !        enddo
> !   
> !        total_interactions=offsets(nfgtasks-1)+counts(nfgtasks-1)
> !        if(total_interactions.gt.maxj) then
> !          call too_many_interactions(itypint,total_interactions)
> !        endif
> !   
> !        call MPI_Allreduce(inlisti,outlisti,ihi-ilo+1,MPI_INTEGER, 
> !     &    MPI_SUM,FG_COMM,ierror)
> !   
> !        call MPI_Allgatherv(inlistj,my_count,MPI_INTEGER,
> !     &    outlistj,counts,offsets,MPI_INTEGER,FG_COMM,ierror)
> !      else
> !        outlisti(ilo:ihi)=inlisti(ilo:ihi)
> !        outlistj(1:total_interactions)=inlistj(1:total_interactions)
> !      endif
> !#else
> !      outlisti(ilo:ihi)=inlisti(ilo:ihi)
> !      outlistj(1:my_count)=inlistj(1:my_count)
> !#endif
> !
> !      offset=0
> !      do i=ilo,ihi
> !        outlisti(i)=outlisti(i)+offset
> !        offset=outlisti(i)
> !      enddo
> !      return
> !      end
> 
> C----------------------------------------------------------------------
>       subroutine sort_inter_list(iat_s,iat_e,listi,listj,
>      &                           contlisti,contlistj,irowi,irowj)
>       implicit none
>       include 'DIMENSIONS'
> 
>       integer parallel_chunk_size
>       parameter (parallel_chunk_size=16)
> 
>       integer iat_s,iat_e,irowi,irowj,i,j,cnt,listbase,contbase
>       integer g_list_start,g_list_end
>       integer listi(2,0:2*maxres),listj(2,maxint_res*maxres)
>       integer contlisti(2,0:10*maxres),contlistj(2,maxint_res*maxres)
>       integer liststart(0:2*maxres+1)
>       common /makelistsrtcommon/ liststart
> 
>       irowi=0
>       contlisti(:,0)=0
>       liststart(0)=1
>       do i=iat_s,iat_e
>         cnt=listi(2,i-iat_s)
>         liststart(i-iat_s+1)=liststart(i-iat_s)+cnt
>         if(cnt.eq.0) cycle
>         irowi=irowi+1
>         contlisti(1,irowi)=i
>         contlisti(2,irowi)=cnt
>       enddo
>       irowj=liststart(iat_e-iat_s+1)-1
> 
> !$OMP PARALLEL DO DEFAULT(SHARED) SCHEDULE(DYNAMIC,parallel_chunk_size)
> !$OMP& PRIVATE(cnt,listbase,contbase,j)
>       do i=iat_s,iat_e
>         cnt=listi(2,i-iat_s)
>         if(cnt.eq.0) cycle
>         listbase=listi(1,i-iat_s)-1
>         contbase=liststart(i-iat_s)-1
>         do j=1,cnt
>           contlistj(:,contbase+j)=listj(:,listbase+j)
>         enddo
>       enddo
>       return
>       end
> 
> !C----------------------------------------------------------------------
> !! reorder lists at index ranges iat_s:iat_e
> !! iat_s,iat_e,ilo,ihi must fit in range [0, maxres]
> !! input:
> !!   inlisti(2,ilo:ihi) - array of pairs describing lists in inlistj:
> !!                        [start_index, num_items]
> !!   inlistj(jlen)      - items of lists
> !!   iat_s,iat_e        - range of list indices to sort
> !! output:
> !!   outlisti(ilo:ihi)  - end index of i-th list items of list i are
> !!                        at indices outlisti[i-1]+1:outlist[i]
> !!   outlistj(jlen)     - reordered lists' items
> !      subroutine sort_sep_inter_list(inlisti,inlistj,outlisti,outlistj,
> !     &                               iat_s,iat_e,ilo,ihi,jlen)
> !      implicit none
> !      include 'DIMENSIONS'
> !
> !      integer parallel_chunk_size
> !      parameter (parallel_chunk_size=16)
> !
> !      integer iat_s,iat_e,ilo,ihi,jlen
> !      integer outlisti(ilo:ihi),inlisti(2,ilo:ihi)
> !      integer outlistj(jlen),inlistj(jlen)
> !
> !      integer i,j,cnt,inbase,outbase
> !      integer liststart(0:maxres+1)
> !      common /makelistsrtcommon/ liststart
> !
> !      outlisti(iat_s-1)=0
> !      liststart(iat_s)=1
> !      do i=iat_s+1,iat_e
> !        liststart(i)=liststart(i-1)+inlisti(2,i-1)
> !      enddo
> !
> !!$OMP PARALLEL DO DEFAULT(SHARED) SCHEDULE(DYNAMIC,parallel_chunk_size)
> !!$OMP& PRIVATE(cnt,inbase,outbase,j)
> !      do i=iat_s,iat_e
> !        cnt=inlisti(2,i)
> !        outlisti(i)=cnt
> !        if(cnt.eq.0) cycle
> !        inbase=inlisti(1,i)-1
> !        outbase=liststart(i)-1
> !        do j=1,cnt
> !          outlistj(outbase+j)=inlistj(inbase+j)
> !        enddo
> !      enddo
> !      return
> !      end
make_list_RESPA_new.F
make_xx_list.F
map.F
66c66
<         call chainbuild
---
>         call chainbuild_extconf
math.f
matmult.f
mc.F
mcm.F
153c153
<       call chainbuild
---
>       call chainbuild_extconf
165c165
<         call chainbuild
---
>         call chainbuild_extconf
231c231
<           call chainbuild
---
>           call chainbuild_extconf
253c253
<             call chainbuild
---
>             call chainbuild_extconf
329c329
<                 call chainbuild
---
>                 call chainbuild_extconf
486c486
<       call chainbuild
---
>       call chainbuild_extconf
497c497
<       call chainbuild
---
>       call chainbuild_extconf
514c514
<       call chainbuild
---
>       call chainbuild_extconf
549c549
<           call chainbuild
---
>           call chainbuild_extconf
575c575
<            call chainbuild
---
>            call chainbuild_extconf
687c687
<              call chainbuild
---
>              call chainbuild_extconf
MD_A-MTS.F
40,42c40
<       include 'COMMON.NMR'
<       include 'COMMON.TORCNSTR'
<       include 'COMMON.RESTARTED'
---
>       include 'COMMON.GRID'
44,46c42,44
< #ifdef VOUT
<       double precision v_work(maxres6),v_transf(maxres6)
< #endif
---
> !#ifdef VOUT
> !      double precision v_work(maxres6),v_transf(maxres6)
> !#endif
56c54,55
<       double precision tt0
---
>       double precision tt0,tt1,td
>       double precision timer_value
58d56
<       include 'COMMON.AVNLOC'
63a62,65
> !     write(*,*)'MD',d_time,lang,n_timestep
> #ifdef FNLOG
>       write(*,*)'MD'
> #endif
80,84c82
< #ifdef MPI
<       tt0=MPI_Wtime()
< #else
<       tt0 = tcpu()
< #endif
---
>       tt0=timer_value()
89,93c87
< #ifdef MPI
<       t_MDsetup = MPI_Wtime()-tt0
< #else
<       t_MDsetup = tcpu()-tt0
< #endif
---
>       t_MDsetup = timer_value()-tt0
96,100c90
< #ifdef MPI
<       tt0 = MPI_Wtime()
< #else
<       tt0 = tcpu()
< #endif
---
>       tt0 = timer_value()
102c92
< #ifndef   LANG0
---
> #ifndef LANG0
133a124,125
>       t_langsetup=timer_value()-tt0
>       tt0=timer_value()
135,149c127,129
<       t_langsetup=MPI_Wtime()-tt0
<       tt0=MPI_Wtime()
< #else
<       t_langsetup=tcpu()-tt0
<       tt0=tcpu()
< #endif
<       atimeave=atimeave_save
<       write (iout,*) "atimeave",atimeave
<       if (atimeave.gt.0) then
<         if (npeak.gt.0) dnmr_xaveave(:nhpb_peak)=0.0d0
<         if (ntheta_constr.gt.0) theta_xaveave(:ntheta_constr)=0.0d0
<         if (ndih_constr.gt.0) then
<           cosphi_xaveave(:ndih_constr)=0.0d0
<           sinphi_xaveave(:ndih_constr)=0.0d0
<         endif
---
>       if (nfgtasks.gt.1) then
>         if (fg_rank.eq.0)
>      &    call MPI_Bcast(15,1,MPI_INT,king,FG_COMM,IERROR)
150a131,137
> #endif
>       call restart_grid_tuning(grid_opts)
>       call restart_grid_tuning(saxs_grid_opts)
> !-start of loop---------------------------------------------------------
> #ifdef MAX_ITER
>       n_timestep=MAX_ITER
> #endif
152c139,141
< c        write (iout,*) "itime",itime
---
> #ifdef TURNLOG
>         write(*,*)'MD iteration',itime,'(CPU)'
> #endif
197a187,201
> ! ADDED IN 44
> #ifndef VIRTUAL
>           !do i=1,2*nres
>           !  do j=1,3
>           !    vel_old(j,i)=vel(j,i)
>           !  enddo
>           !enddo
>           call memcpy3(vel_old,vel,2*nres)
>           !do i=nnt,nct-1
>           !  do j=1,3
>           !    velp_old(j,i)=velp(j,i)
>           !  enddo
>           !enddo
>           call memcpy3(velp_old(1,nnt),velp(1,nnt),(nct-1)-nnt+1)
> #endif
203c207,215
<              d_t(j,0)=d_t(j,0)-vcm(j)
---
>             d_t(j,0)=d_t(j,0)-vcm(j)
>           enddo
> ! ADDED IN 44
> #ifndef VIRTUAL
>           do i=1,2*nres
>             vel(:,i)=vel(:,i)-vcm(:)
>           enddo
>           do i=nnt,nct-1
>             velp(:,i)=velp(:,i)-vcm(:)
204a217,219
> #endif
> ! ADDED IN 44
> #ifdef VIRTUAL
205a221,223
> #else
>           call kinetic_CASC(EK)
> #endif
210,212c228,239
<             do j=1,3
<               d_t_old(j,i)=scalfac*d_t(j,i)
<             enddo
---
>             !do j=1,3
>             !  d_t_old(j,i)=scalfac*d_t(j,i)
>             !enddo
>             d_t_old(:,i)=scalfac*d_t(:,i)
>           enddo
> ! ADDED IN 44
> #ifndef VIRTUAL
>           do i=1,2*nres
>             vel_old(:,i)=scalfac*vel(:,i)
>           enddo
>           do i=nnt,nct-1
>             velp_old(:,i)=scalfac*velp(:,i)
213a241
> #endif
237,238c265,267
<          if (mod(itime,ntwe).eq.0) then
<            call statout(itime)
---
>           if (mod(itime,ntwe).eq.0) then
>             !write(*,*)'statout'
>             call statout(itime)
241,258d269
<          endif
< #ifdef VOUT
<         do j=1,3
<           v_work(j)=d_t(j,0)
<         enddo
<         ind=3
<         do i=nnt,nct-1
<           do j=1,3
<             ind=ind+1
<             v_work(ind)=d_t(j,i)
<           enddo
<         enddo
<         do i=nnt,nct
<           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)
<             enddo
260,271c271,272
<         enddo
< 
<         write (66,'(80f10.5)')
<      &    ((d_t(j,i),j=1,3),i=0,nres-1),((d_t(j,i+nres),j=1,3),i=1,nres)
<         do i=1,ind
<           v_transf(i)=0.0d0
<           do j=1,ind
<             v_transf(i)=v_transf(i)+gvec(j,i)*v_work(j)
<           enddo
<            v_transf(i)= v_transf(i)*dsqrt(geigen(i))
<         enddo
<         write (67,'(80f10.5)') (v_transf(i),i=1,ind)
---
> #ifdef VOUT
>           call vout
288,290c289,300
< c          write (iout,*) "Calling writerst"
< c          call flush(iout)
<           call writerst
---
>           !write(*,*)'irest_freq'
>           tt1=timer_value()
>           !open(irest2,file=rest2name,status='unknown')
>           !write(irest2,*) totT,EK,potE,totE,t_bath
>           !do i=0,2*nres-1
>           ! write (irest2,'(3e15.5)') (d_t(j,i),j=1,3)
>           !enddo
>           !do i=0,2*nres-1
>           ! write (irest2,'(3e15.5)') (dc(j,i),j=1,3)
>           !enddo
>           !close(irest2)
>           call write_restart_file
291a302,307
>           td=timer_value()-tt1
>           time_rst=time_rst+td
>           grid_opts%non_work_extra_time=
>      &      grid_opts%non_work_extra_time+td
>           saxs_grid_opts%non_work_extra_time=
>      &      saxs_grid_opts%non_work_extra_time+td
370,373c386,388
<       include 'COMMON.NMR'
<       include 'COMMON.AVNLOC'
<       include 'COMMON.RESTARTED'
<       integer ic
---
> 
>       include 'COMMON.OMP'
> 
387a403,405
> #ifdef FNLOG
>       write(*,*)'velverlet_step'
> #endif
404,409d421
<       if (atimeave.gt.0) then
< C Save average restraint information. So far for NFGTASKS.eq.1 only
<         if (nfgtasks.gt.1) 
<      &    call MPI_Bcast(15,1,MPI_INTEGER,king,FG_COMM,IERROR)
<         call save_nmrave_info
<       endif
444a457,458
> ! ADDED IN 44
> #ifdef VIRTUAL
445a460,463
> #else
>         call calculate_dC
>         call velocities
> #endif
448,467c466,470
<         if (large.and. mod(itime,ntwe).eq.0) then
<           write (iout,*) "Cartesian and internal coordinates: step 1"
<           call cartprint
<           call intout
<           write (iout,*) "dC"
<           do i=0,nres
<             write (iout,'(i3,3f10.5,3x,3f10.5)') i,(dc(j,i),j=1,3),
<      &      (dc(j,i+nres),j=1,3)
<           enddo
<           write (iout,*) "Accelerations"
<           do i=0,nres
<             write (iout,'(i3,3f10.5,3x,3f10.5)') i,(d_a(j,i),j=1,3),
<      &      (d_a(j,i+nres),j=1,3)
<           enddo
<           write (iout,*) "Velocities, step 1"
<           do i=0,nres
<             write (iout,'(i3,3f10.5,3x,3f10.5)') i,(d_t(j,i),j=1,3),
<      &      (d_t(j,i+nres),j=1,3)
<           enddo
<         endif
---
> c          write (iout,*) "VELVERLET ITIME",itime," NTWE",
> c     &      ntwe,mod(itime,ntwe)
>           if (large.and. mod(itime,ntwe).eq.0) then
>             call write_ntwe_step1
>           endif
480,507c483
<           write (iout,*) "NaNs in energy itime",itime
<           call enerprint(potEcomp)
<           write (iout,*) "DMfric",DMfric(:2*nres)
<           write (iout,*) "DU1fric",DU1fric(:2*nres)
<           write (iout,*) "DU2fric",DU2fric(:2*nres)
<           write (iout,*) "fric,stoch"
<           write (iout,*) fric_work(:6*nres)
<           write (iout,*) stoch_work(:6*nres)
<           write (iout,*) "gcart,gxcart"
<           do i=1,nres
<             write (iout,'(i5,3e15.5,5x,3e15.5)')i,(-gcart(j,i),j=1,3),
<      &      (-gxcart(j,i),j=1,3)
<           enddo
<           write(iout,*) "dc, dc_old"
<           do i=1,nres
<             write (iout,'(i5,3e15.5,5x,3e15.5)')i,(dc(j,i),j=1,3),
<      &      (dc_old(j,i),j=1,3)
<           enddo
<           write(iout,*) "d_t, d_t_old"
<           do i=1,nres
<             write (iout,'(i5,3e15.5,5x,3e15.5)')i,(d_t(j,i),j=1,3),
<      &      (d_t_old(j,i),j=1,3)
<           enddo
<           write(iout,*) "d_a, d_a_old"
<           do i=1,nres
<             write (iout,'(i5,3e15.5,5x,3e15.5)')i,(d_a(j,i),j=1,3),
<      &      (d_a_old(j,i),j=1,3)
<           enddo
---
>           call debug_write_when_nan_energy(itime)
520a497,499
> #ifdef ENELOG
>         write(*,'(a,3f18.8)')'EPOT=', potE,potEcomp(0),potEcomp(27)
> #endif
523a503
>         !call lagrangian_penta
525,540c505
<           write (iout,*) "NaN in accelerations itime",itime
<           write (iout,*) "gcart,gxcart"
<           do i=1,nres
<             write (iout,'(i5,3e15.5,5x,3e15.5)')i,(-gcart(j,i),j=1,3),
<      &      (-gxcart(j,i),j=1,3)
<           enddo
<           write(iout,*) "dc, dc_old"
<           do i=1,nres
<             write (iout,'(i5,3e15.5,5x,3e15.5)')i,(dc(j,i),j=1,3),
<      &      (dc_old(j,i),j=1,3)
<           enddo
<           write(iout,*) "d_a, d_a_old"
<           do i=1,nres
<             write (iout,'(i5,3e15.5,5x,3e15.5)')i,(d_a(j,i),j=1,3),
<      &      (d_a_old(j,i),j=1,3)
<           enddo
---
>           call debug_write_when_nan_d_a(itime)
550a516,519
> #ifdef ENELOG
>         write(*,'(a,2f18.8,i4)')'AMAX=',amax,epdrift,itime_scal
> #endif
>         !write(*,'(a,2f18.10,i4)')'CPU amax=',amax,epdrift,itime_scal
554,565d522
<           if (atimeave.gt.0) then
< c Restore the average distance information fro the previous step. So far for
< c NFGTASKS.eq.1 only
<             if (nfgtasks.gt.1) 
<      &        call MPI_Bcast(16,1,MPI_INTEGER,king,FG_COMM,IERROR)
<             call restore_nmrave_info
< c            do ic=1,npeak
< c              dnmr_xave(:,ic)=dnmr_xave_safe(:,ic)
< c            enddo
< c            nsteps = nsteps_save
< c            icalls = icalls_save
<           endif
583a541,542
> !         write(*,'(a,2i4,2f18.10)')'CPU scaling down',
> !    &      ifac_time,itime_scal,fac_time,d_time
652d610
<           if (PRINT_AMTS_MSG) then
656d613
<           endif
672a630,633
> ! ADDED IN 44
> #ifndef VIRTUAL
>           call velocities
> #endif
700a662,663
> ! ADDED IN 44
> #ifdef VIRTUAL
701a665,667
> #else
>       call kinetic_CASC(EK)
> #endif
702a669,671
> #ifdef ENELOG
>       write(*,*)'EK=', EK
> #endif
710a680
>       !write(*,'(a,4f18.10)')'EE',potEcomp(27),potE,EK,totE
711a682,684
> !     write(*,*)'cpu vb1', dimen3
> !     write(*,*)'cpu vb2', Rb, EK, kinetic_T
> !     write(*,*)'cpu vb3', d_time, tau_bath, t_bath
713,720c686,705
< !$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(j)
<       do i=0,2*nres
<         do j=1,3
<           dc_old(j,i)=dc(j,i)
<           d_t_old(j,i)=d_t(j,i)
<           d_a_old(j,i)=d_a(j,i)
<         enddo
<       enddo
---
> !!$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(j)
> !      do i=0,2*nres
> !        do j=1,3
> !          dc_old(j,i)=dc(j,i)
> !          d_t_old(j,i)=d_t(j,i)
> !          d_a_old(j,i)=d_a(j,i)
> !        enddo
> !      enddo
>       call memcpy3(dc_old(1,0),dc(1,0),2*nres+1)
>       call memcpy3(d_t_old(1,0),d_t(1,0),2*nres+1)
>       call memcpy3(d_a_old(1,0),d_a(1,0),2*nres+1)
> ! ADDED IN 44
> #ifndef VIRTUAL
>       !do i=1,2*nres
>       !  c_old(:,i)=c(:,i)
>       !  vel_old(:,i)=vel(:,i)
>       !enddo
>       call memcpy3(c_old(1,1),c(1,1),2*nres)
>       call memcpy3(vel_old(1,1),vel(1,1),2*nres)
> #endif
722,728c707,709
<       if (mod(itime,ntwe).eq.0 .and. large) then
<         write (iout,*) "Velocities, step 2"
<         do i=0,nres
<           write (iout,'(i3,3f10.5,3x,3f10.5)') i,(d_t(j,i),j=1,3),
<      &      (d_t(j,i+nres),j=1,3)
<         enddo
<       endif
---
>         if (mod(itime,ntwe).eq.0 .and. large) then
>           call write_ntwe_step2     
>         endif
772,773d752
<       include 'COMMON.NMR'
<       include 'COMMON.AVNLOC'
780,781c759,760
<      & d_a_old0(3,0:maxres2)
<       integer i,j,ic
---
>      & d_a_old0(3,0:maxres2),c_old0(3,maxres2),vel_old0(3,maxres2)
>       integer i,j
783c762
< c      logical PRINT_AMTS_MSG /.false./
---
>       logical PRINT_AMTS_MSG /.false./
794c773
<       common /cipiszcze/ itt
---
>       common /ipiszcze/ itt
798c777
<       common /respastepcommon/ dc_old0,d_t_old0
---
>       common /respastepcommon/ dc_old0,d_t_old0,c_old0,vel_old0
805,807c784,786
<         call pdbout(0.0d0,
<      &  "cipiszcze                                         ",iout)
<         call intout
---
> c        call pdbout(0.0d0,
> c     &  "ipiszcze                                         ",iout)
> c        call intout
812a792
> #ifdef VIRTUAL
817a798,804
> #else
>         write (iout,*) "Velocities, step 0"
>         do i=1,nres
>           write (iout,'(i3,3f10.5,3x,3f10.5)') i,(vel(j,i),j=1,3),
>      &      (vel(j,i+nres),j=1,3)
>         enddo
> #endif
823a811,813
> #ifndef VIRTUAL
>       call velocities
> #endif
825a816
> #ifdef VIRTUAL
830a822,828
> #else
>         write (iout,*) "Velocities, end"
>         do i=1,nres
>           write (iout,'(i3,3f10.5,3x,3f10.5)') i,(vel(j,i),j=1,3),
>      &      (vel(j,i+nres),j=1,3)
>         enddo
> #endif
864a863
> #ifdef VIRTUAL
879a879,895
> #else
>       do i=1,2*nres
>         do j=1,3
>           c_old(j,i)=c(j,i)
>           vel_old(j,i)=vel(j,i)
>           d_a_old(j,i)=d_a(j,i)
>         enddo
>       enddo
> c 6/30/08 A-MTS: attempt at increasing the split number
>       do i=1,2*nres
>         do j=1,3
>           c_old0(j,i)=c_old(j,i)
>           vel_old0(j,i)=vel_old(j,i)
>           d_a_old0(j,i)=d_a_old(j,i)
>         enddo
>       enddo
> #endif
886,897d901
<       if (atimeave.gt.0) then
< C Save average restraint information. So far for NFGTASKS.eq.1 only
<         if (nfgtasks.gt.1) 
<      &    call MPI_Bcast(15,1,MPI_INTEGER,king,FG_COMM,IERROR)
<         call save_nmrave_info
< c        do ic=link_start_peak,link_end_peak
< c          do j=1,2
< c            dnmr_xave_safe(j,ic)=dnmr_xave(j,ic)
< c          enddo
< c        enddo
< c        nsteps_save = nsteps
<       endif
936a941
> #ifdef VIRTUAL
937a943,946
> #else
>         call calculate_dC
>         call velocities
> #endif
944c953
<      &     "cipiszcze                                         ",iout)
---
>      &     "ipiszcze                                         ",iout)
946a956
> #ifdef VIRTUAL
951a962,968
> #else
>           write (iout,*) "Velocities, step 1"
>           do i=1,nres
>             write (iout,'(i3,3f10.5,3x,3f10.5)') i,(vel(j,i),j=1,3),
>      &        (vel(j,i+nres),j=1,3)
>           enddo
> #endif
1031,1041d1047
<             if (atimeave.gt.0) then
< c Restore the average distance information fro the previous step. So far for
< c NFGTASKS.eq.1 only
<               if (nfgtasks.gt.1) 
<      &          call MPI_Bcast(16,1,MPI_INTEGER,king,FG_COMM,IERROR)
<               call restore_nmrave_info
< c              do ic=link_start_peak,link_end_peak
< c                dnmr_xave(:,ic)=dnmr_xave_safe(:,ic)
< c              enddo
< c              nsteps = nsteps_save
<             endif
1042a1049
> #ifdef VIRTUAL
1049a1057,1065
> #else
>             do i=1,2*nres
>               do j=1,3
>                 c_old(j,i)=c_old0(j,i)
>                 vel_old(j,i)=vel_old0(j,i)
>                 d_a_old(j,i)=d_a_old0(j,i)
>               enddo
>             enddo
> #endif
1057d1072
<             if (PRINT_AMTS_MSG) then
1062d1076
<             endif
1084a1099,1101
> #ifndef VIRTUAL
>         call velocities
> #endif
1085a1103
> #ifdef VIRTUAL
1092a1111,1119
> #else
>         do i=1,2*nres
>           do j=1,3
>             c_old(j,i)=c(j,i)
>             vel_old(j,i)=vel(j,i)
>             d_a_old(j,i)=d_a(j,i)
>           enddo
>         enddo
> #endif
1150c1177
<      &  'cipiszcze                                         ',iout)
---
>      &  'ipiszcze                                         ',iout)
1156a1184
> #ifdef VIRTUAL
1161a1190,1196
> #else
>         write (iout,*) "Velocities, step 2"
>         do i=1,nres
>           write (iout,'(i3,3f10.5,3x,3f10.5)') i,(vel(j,i),j=1,3),
>      &      (vel(j,i+nres),j=1,3)
>         enddo
> #endif
1166a1202,1204
> #ifndef VIRTUAL
>       call velocities
> #endif
1181a1220
> #ifdef VIRTUAL
1182a1222,1224
> #else 
>       call kinetic_CASC(EK)
> #endif
1191a1234
> #ifdef VIRTUAL
1196a1240,1246
> #else
>         write (iout,*) "Velocities, end"
>         do i=1,nres
>           write (iout,'(i3,3f10.5,3x,3f10.5)') i,(vel(j,i),j=1,3),
>      &      (vel(j,i+nres),j=1,3)
>         enddo
> #endif
1222a1273
> #ifdef VIRTUAL
1238a1290,1309
> #else
>       do i=1,nres
>         do j=1,3
>           vel(j,i)=vel(j,i)+0.5d0*d_a(j,i)*d_time
>         enddo
>       enddo
>       do i=nnt,nct
>         if (itype(i).eq.ntyp1) cycle
>         inres=i+nres
>         if (itype(i).ne.10) then
>           do j=1,3
>             vel(j,inres)=vel(j,inres)+0.5d0*d_a(j,inres)*d_time
>           enddo
>         else
>           do j=1,3
>             vel(j,inres)=vel(j,i)
>           enddo
>         endif
>       enddo
> #endif
1261c1332
<       double precision adt,adt2
---
>       double precision adt(3),adt2(3)
1262a1334
> #ifdef VIRTUAL
1277,1278c1349,1351
< !$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(j,adt,adt2)
<       do i=nnt,nct-1
---
> !$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(j,adt,adt2,inres)
> !     do i=nnt,nct-1
>       do i=nnt,nct
1284,1293c1357,1368
<         do j=1,3
<           adt=d_a_old(j,i)*d_time
<           adt2=0.5d0*adt
<           dc(j,i)=dc_old(j,i)+(d_t_old(j,i)+adt2)*d_time
<           d_t_new(j,i)=d_t_old(j,i)+adt2
<           d_t(j,i)=d_t_old(j,i)+adt
<         enddo
<       enddo
< !$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(inres,j,adt,adt2)
<       do i=nnt,nct
---
>         if (i.lt.nct) then
>           do j=1,3
>             adt=d_a_old(j,i)*d_time
>             adt2=0.5d0*adt
>             dc(j,i)=dc_old(j,i)+(d_t_old(j,i)+adt2)*d_time
>             d_t_new(j,i)=d_t_old(j,i)+adt2
>             d_t(j,i)=d_t_old(j,i)+adt
>           enddo
>         endif
> !     enddo
> !!$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(inres,j,adt,adt2)
> !     do i=nnt,nct
1305a1381
> 
1312a1389,1457
> !--------------------------------------
> #else
> #ifdef FNLOG
>       write(*,*)'verlet1 cpu'
> #endif
> #ifdef DEBUG
>       write (iout,*) "VELVERLET1 START: C"
>       do i=1,nres
>         write (iout,'(i3,3f10.5,5x,3f10.5)') i,(c(j,i),j=1,3),
>      &   (c(j,i+nres),j=1,3)
>       enddo
> #endif
> !$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(j,adt,adt2,inres)
>       do i=1,nres
> C      SPYTAC ADAMA
> C       do i=0,nres
> #ifdef DEBUG
>         write (iout,*) "i",i," d_a_old",(d_a_old(j,i),j=1,3)
> #endif
>         adt(:)=d_a_old(:,i)*d_time
>         adt2=0.5d0*adt
>         c(:,i)=c_old(:,i)+(vel_old(:,i)+adt2)*d_time
>         vel_new(:,i)=vel_old(:,i)+adt2
>         vel(:,i)=vel_old(:,i)+adt
> #ifdef VERLETLOG
>         write(*,'(a,i4,3f18.10)')'V1da',i,d_a_old(:,i)
>         write(*,'(a,i4,3f18.10)')'V1vo',i,vel_old(:,i)
>         write(*,'(a,i4,3f18.10)')'V1co',i,c_old(:,i)
>         write(*,'(a,i4,3f18.10)')'V1c ',i,c(:,i)
>         write(*,'(a,i4,3f18.10)')'V1v ',i,vel(:,i)
>         write(*,'(a,i4,3f18.10)')'V1vn',i,vel_new(:,i)
> #endif
> !     enddo
> !!$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(inres,j,adt,adt2)
> !     do i=nnt,nct
> C        do i=0,nres
>         if (i.lt.nnt .or. nct.lt.i) cycle
>         if (itype(i).eq.ntyp1) cycle
>         inres=i+nres
>         if (itype(i).ne.10) then
>           adt(:)=d_a_old(:,inres)*d_time
>           adt2(:)=0.5d0*adt
>           c(:,inres)=c_old(:,inres)+(vel_old(:,inres)+adt2)*d_time
>           vel_new(:,inres)=vel_old(:,inres)+adt2
>           vel(:,inres)=vel_old(:,inres)+adt
>         else
>           !do j=1,3
>           !  c(j,inres)=c(j,i)
>           !enddo
>           c(:,inres)=c(:,i)
>         endif
> #ifdef VERLETLOG
>         write(*,'(a,i4,3f18.10)')'V1da',inres,d_a_old(:,inres)
>         write(*,'(a,i4,3f18.10)')'V1vo',inres,vel_old(:,inres)
>         write(*,'(a,i4,3f18.10)')'V1co',inres,c_old(:,inres)
>         write(*,'(a,i4,3f18.10)')'V1c ',inres,c(:,inres)
>         write(*,'(a,i4,3f18.10)')'V1v ',inres,vel(:,inres)
>         write(*,'(a,i4,3f18.10)')'V1vn',inres,vel_new(:,inres)
> #endif
>       enddo
> 
> #ifdef DEBUG
>       write (iout,*) "VELVERLET1 END: C"
>       do i=1,nres
>         write (iout,'(i3,3f10.5,5x,3f10.5)') i,(c(j,i),j=1,3),
>      &   (c(j,i+nres),j=1,3)
>       enddo
> #endif
> #endif
1335a1481,1484
> #ifdef FNLOG
>       write(*,*)'verlet2 cpu'
> #endif
> #ifdef VIRTUAL
1339,1344d1487
< !$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(j)
<       do i=nnt,nct-1
<         do j=1,3
<           d_t(j,i)=d_t_new(j,i)+0.5d0*d_a(j,i)*d_time
<         enddo
<       enddo
1345a1489
> !      do i=nnt,nct-1
1346a1491,1498
>         if (i.lt.nct) then
>           do j=1,3
>             d_t(j,i)=d_t_new(j,i)+0.5d0*d_a(j,i)*d_time
>           enddo
>         endif
> !      enddo
> !!$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(j,inres)
> !      do i=nnt,nct
1353a1506,1528
> #else
> !$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(j)
>       do i=1,nres
>         vel(:,i)=vel_new(:,i)+0.5d0*d_a(:,i)*d_time
> #ifdef VERLETLOG
>         write(*,'(a,i4,3f18.10)')'V2v',i,vel(:,i)
> #endif
>       enddo
> !TODO: merge loops
> !$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(j,inres)
>       do i=nnt,nct
>         if (itype(i).eq.ntyp1) cycle
>         inres=i+nres
>         if (itype(i).ne.10) then
>           vel(:,inres)=vel_new(:,inres)+0.5d0*d_a(:,inres)*d_time
>         else
>           vel(:,inres)=vel(:,i)
>         endif
> #ifdef VERLETLOG
>         write(*,'(a,i4,3f18.10)')'V2v',inres,vel(:,inres)
> #endif
>       enddo
> #endif
1474a1650,1653
> 
> #ifdef FNLOG
>       write(*,*)'sddir_verlet1 cpu'
> #endif
1478a1658
> #ifdef VIRTUAL
1509a1690,1769
> #else
>       ind=0
>       do i=nnt,nct
>         do j=1,3
>           adt=(d_a_old(j,i)+d_af_work(ind+j))*d_time
>           adt2=0.5d0*adt+sqrt13*d_as_work(ind+j)*d_time
>           c(j,i)=c_old(j,i)+(vel_old(j,i)+adt2)*d_time
>           vel_new(j,i)=vel_old(j,i)+0.5d0*adt
>           vel(j,i)=vel_old(j,i)+adt
>         enddo
> #ifdef VERLETLOG
>         write(*,'(a,i4,3f18.10)')'V1af',i,d_af_work(ind+1:ind+3)
>         write(*,'(a,i4,3f18.10)')'V1as',i,d_as_work(ind+1:ind+3)
>         write(*,'(a,i4,3f18.10)')'V1da',i,d_a_old(:,i)
>         write(*,'(a,i4,3f18.10)')'V1vo',i,vel_old(:,i)
>         write(*,'(a,i4,3f18.10)')'V1co',i,c_old(:,i)
>         write(*,'(a,i4,3f18.10)')'V1c ',i,c(:,i)
>         write(*,'(a,i4,3f18.10)')'V1v ',i,vel(:,i)
>         write(*,'(a,i4,3f18.10)')'V1vn',i,vel_new(:,i)
> #endif
>         ind=ind+3
>       enddo
> c      do j=1,3
> c        dc(j,0)=c(j,nnt)
> c      enddo
> c      if (nnt.gt.1) then
> c        do j=1,3
> c          dc(j,0)=dc(j,0)-dc(j,1)
> c        enddo
> c      endif
> c      do ichain=1,nchain
> c        i=chain_border1(1,ichain)
> c        if (itype(i).eq.ntyp1) then
> c          do j=1,3
> c            c(j,i)=c(j,i+1)-dc(j,i)
> c          enddo
> c        endif
> c        i=chain_border1(2,ichain)
> c        if (itype(i).eq.ntyp1) then
> c          do j=1,3
> c            c(j,i)=c(j,i-1)+dc(j,i)
> c          enddo
> c        endif
> c      enddo
>       do i=nnt,nct
>         if (itype(i).eq.ntyp1) cycle
>         inres=i+nres
>         if (itype(i).ne.10) then
>           do j=1,3
>             adt=(d_a_old(j,inres)+d_af_work(ind+j))*d_time
>             adt2=0.5d0*adt+sqrt13*d_as_work(ind+j)*d_time
>             c(j,inres)=c_old(j,inres)+(vel_old(j,inres)+adt2)*d_time
>             vel_new(j,inres)=vel_old(j,inres)+0.5d0*adt
>             vel(j,inres)=vel_old(j,inres)+adt
>           enddo
> #ifdef VERLETLOG
>           write(*,'(a,i4,3f18.10)')'V1af',inres,d_af_work(ind+1:ind+3)
>           write(*,'(a,i4,3f18.10)')'V1as',inres,d_as_work(ind+1:ind+3)
>           write(*,'(a,i4,3f18.10)')'V1da',inres,d_a_old(:,inres)
>           write(*,'(a,i4,3f18.10)')'V1vo',inres,vel_old(:,inres)
>           write(*,'(a,i4,3f18.10)')'V1co',inres,c_old(:,inres)
>           write(*,'(a,i4,3f18.10)')'V1c ',inres,c(:,inres)
>           write(*,'(a,i4,3f18.10)')'V1v ',inres,vel(:,inres)
>           write(*,'(a,i4,3f18.10)')'V1vn',inres,vel_new(:,inres)
> #endif
>           ind=ind+3
>         else
>           do j=1,3
>             c(j,inres)=c(j,i)
>             vel_new(j,inres)=vel_new(j,i)
>             vel(j,inres)=vel(j,i)
>           enddo
> #ifdef VERLETLOG
>           write(*,'(a,i4,3f18.10)')'V1c ',inres,c(:,inres)
>           write(*,'(a,i4,3f18.10)')'V1v ',inres,vel(:,inres)
>           write(*,'(a,i4,3f18.10)')'V1vn',inres,vel_new(:,inres)
> #endif
>         endif
>       enddo
> #endif
1548a1809,1811
> #ifdef FNLOG
>       write(*,*)'ssdir_verlet2 cpu'
> #endif
1563a1827
> #ifdef VIRTUAL
1586a1851,1891
> #else
>       ind=0
>       do i=nnt,nct
>         do j=1,3
>           vel(j,i)=vel_new(j,i)+(0.5d0*(d_a(j,i)+d_af_work(ind+j))
>      &     +sin60*d_as_work(ind+j)+cos60*d_as_work1(ind+j))*d_time
>         enddo
> #ifdef VERLETLOG
>         write(*,'(a,i4,3f18.10)')'V2af',i,d_af_work(ind+1:ind+3)
>         write(*,'(a,i4,3f18.10)')'V2as',i,d_as_work(ind+1:ind+3)
>         write(*,'(a,i4,3f18.10)')'V2da',i,d_a(:,i)
>         write(*,'(a,i4,3f18.10)')'V2vn',i,vel_new(:,i)
>         write(*,'(a,i4,3f18.10)')'V2as1',i,d_as_work1(ind+1:ind+3)
>         write(*,'(a,i4,4f18.10)')'V2v',i,vel(:,i),d_time
> #endif
>         ind=ind+3
>       enddo
>       do i=nnt,nct
>         if (itype(i).eq.ntyp1) cycle
>         inres=i+nres
>         if (itype(i).ne.10) then
>           do j=1,3
>             vel(j,inres)=vel_new(j,inres)+(0.5d0*(d_a(j,inres)
>      &       +d_af_work(ind+j))+sin60*d_as_work(ind+j)
>      &       +cos60*d_as_work1(ind+j))*d_time
>           enddo
> #ifdef VERLETLOG
>           write(*,'(a,i4,3f18.10)')'V2as1',inres,d_as_work1(ind+1:ind+3)
>           write(*,'(a,i4,3f18.10)')'V2v',inres,vel(:,inres)
> #endif
>           ind=ind+3
>         else
>           do j=1,3
>             vel(j,inres)=vel(j,i)
>           enddo
> #ifdef VERLETLOG
>           write(*,'(a,i4,3f18.10)')'V2v',inres,vel(:,inres)
> #endif
>         endif
>       enddo
> #endif
1618a1924,1925
> 
> #ifdef VIRTUAL
1621d1927
< 
1797a2104,2217
> ! #ifndef VIRTUAL =====================================================
> #else
>       th_data=0.0d0
> !!$OMP PARALLEL DEFAULT(SHARED) NUM_THREADS(threads_used)
> !!$OMP& PRIVATE(from,to,i,my_thread,ac,aco)
> !      my_thread=omp_get_thread_num()+1
> !      !from=range_lo+((range_hi-range_lo)/threads_used)*(my_thread-1)
> !      !to=range_lo+((range_hi-range_lo)/threads_used)*my_thread-1
> !      !if(my_thread.eq.threads_used) to=range_hi
> !      call split_work_for_threads(from,to,nnt,nct,
> !     &  my_thread,threads_used)
> !      th_from(my_thread)=from
> !      th_to(my_thread)=to
> !      ac=0.0d0
> !      aco=0.0d0
> !      do i=from,to
> !        !aco=aco+0.5d0*d_a_old(:,i)
> !        !ac=ac+0.5d0*d_a(:,i)
> !        aco=aco+d_a_old(:,i)
> !        ac=ac+d_a(:,i)
> !      enddo
> !      th_data(1,my_thread)=aco(1)
> !      th_data(2,my_thread)=aco(2)
> !      th_data(3,my_thread)=aco(3)
> !      th_data(4,my_thread)=ac(1)
> !      th_data(5,my_thread)=ac(2)
> !      th_data(6,my_thread)=ac(3)
> !!$OMP END PARALLEL
> !
> !      do i=1,threads_used
> !        th_data(1:6,i)=th_data(1:6,i)+th_data(1:6,i-1)
> !      enddo
> 
> !$OMP PARALLEL DEFAULT(SHARED) NUM_THREADS(threads_used)
> !$OMP& PRIVATE(from,to,i,j,accel_old,accel,dacc,tamax,my_thread)
>       my_thread=omp_get_thread_num()+1
> !     from=th_from(my_thread)
> !     to=th_to(my_thread)
> 
> !     accel_old(1)=th_data(1,my_thread-1)*0.5
> !     accel_old(2)=th_data(2,my_thread-1)*0.5
> !     accel_old(3)=th_data(3,my_thread-1)*0.5
> !     accel(1)=th_data(4,my_thread-1)*0.5
> !     accel(2)=th_data(5,my_thread-1)*0.5
> !     accel(3)=th_data(6,my_thread-1)*0.5
> 
>       tamax=0.0d0
> 
>       !do i=nnt,nct
> !     to=min(to,nct)
>       call split_work_for_threads(from,to,nnt,nct,
>      &  my_thread,threads_used)
>       do i=from,to
> c Backbone
> !       if (i.lt.nct) then
> c 7/3/08 changed to asymmetric difference
>           do j=1,3
> c           accel(j)=aux(j)+0.5d0*(d_a(j,i)-d_a_old(j,i))
> !           accel_old(j)=accel_old(j)+0.5d0*d_a_old(j,i)
> !           accel(j)=accel(j)+0.5d0*d_a(j,i)
> !           accel_old(j)=0.5d0*d_a_old(j,i)
> !           accel(j)=0.5d0*d_a(j,i)
> 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))
>             if (dabs(d_a(j,i)).gt.dabs(d_a_old(j,i))) then
>               dacc=dabs(d_a(j,i)-d_a_old(j,i))
> c             write (iout,*) i,dacc
>               !if (dacc.gt.amax) amax=dacc
>               tamax=dmax1(tamax,dacc)
>             endif
>           enddo
> !       endif
>       enddo
>       do i=from,to
>         if (itype(i).eq.10 .or. itype(i).eq.ntyp1) cycle
>         do j=1,3
>           if (dabs(d_a(j,i+nres)).gt.dabs(d_a_old(j,i+nres))) then
>             dacc=dabs(d_a(j,i+nres)-d_a_old(j,i+nres))
>             tamax=dmax1(tamax,dacc)
>           endif
>         enddo
>       enddo
>       th_data(7,my_thread)=tamax
> !$OMP END PARALLEL
> 
> !!$OMP PARALLEL DEFAULT(SHARED) NUM_THREADS(threads_used)
> !!$OMP& PRIVATE(from,to,i,j,accel_old,accel,dacc,tamax,my_thread)
> !      my_thread=omp_get_thread_num()+1
> !      from=th_from(my_thread)
> !      to=th_to(my_thread)
> !
> !      tamax=0.0d0
> !
> !      !do i=nnt,nct
> !      do i=from,to
> !        if (itype(i).eq.10 .or. itype(i).eq.ntyp1) cycle
> !        do j=1,3
> !          if (dabs(d_a(j,i+nres)).gt.dabs(d_a_old(j,i+nres))) then
> !            dacc=dabs(d_a(j,i+nres)-d_a_old(j,i+nres))
> !            tamax=dmax1(tamax,dacc)
> !          endif
> !        enddo
> !      enddo
> !      th_data(8,my_thread)=tamax
> !!$OMP END PARALLEL
> 
> 
>       amax=0.0d0
>       do i=1,threads_used
>         amax=dmax1(amax,th_data(7,i))
> !       amax=dmax1(amax,th_data(8,i))
>       enddo
> #endif
1805a2226
>       use omp_lib
1824c2245,2250
<       integer i,j
---
>       integer i,j,my_thread,threads_used
>       double precision th_data(8,max_fg_threads)
> 
>       threads_used=omp_get_max_threads()
>       th_data=0.0d0
> 
1826,1828c2252,2255
<       epdrift=0.0d0
< !$OMP PARALLEL DO DEFAULT(SHARED) REDUCTION(max:epdrift)
< !$OMP& PRIVATE(j,epdriftij)
---
>       !epdrift=0.0d0
> !!$OMP PARALLEL DO DEFAULT(SHARED) REDUCTION(max:epdrift)
> !!$OMP& PRIVATE(j,epdriftij,my_thread)
> !$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(j,epdriftij,my_thread)
1829a2257
>         my_thread=omp_get_thread_num()+1
1837c2265,2266
<             epdrift=max(epdrift,epdriftij)
---
>             !epdrift=max(epdrift,epdriftij)
>             th_data(1,my_thread)=max(th_data(1,my_thread),epdriftij)
1848c2277,2278
<             epdrift=max(epdrift,epdriftij)
---
>             !epdrift=max(epdrift,epdriftij)
>             th_data(1,my_thread)=max(th_data(1,my_thread),epdriftij)
1852a2283,2287
>       epdrift=0.0d0
>       do i=1,threads_used
>         epdrift=dmax1(amax,th_data(1,i))
>       enddo
> 
1889c2324
<       integer i,j ,inres
---
>       integer i,j,inres
1892,1894c2327,2334
< c      write(iout,*) "T_half", T_half
< c      write(iout,*) "EK", EK
< c      write(iout,*) "fact", fact
---
> !     write(*,*)'vb', tbf, lang
> !     write(*,*) "T_half", T_half
> !     write(*,*) "EK", EK
> !     write(*,*) "fact", fact
> !      write(iout,*) "T_half", T_half
> !      write(iout,*) "EK", EK
> !      write(iout,*) "fact", fact
> #ifdef VIRTUAL
1910a2351,2371
> #else
> !$OMP PARALLEL DO DEFAULT(SHARED)
>       do i=nnt,nct
>         vel(:,i)=fact*vel(:,i)
> #ifdef VERLETLOG
>         write(*,'(a,i4,3f18.12)')'VB ',i, vel(:,i)
> #endif
> !     enddo
> !     do i=nnt,nct
>         if (itype(i).eq.ntyp1) cycle
>         inres=i+nres
>         if (itype(i).ne.10) then
>           vel(:,inres)=fact*vel(:,inres)
>         else
>           vel(:,inres)=vel(:,i)
>         endif
> #ifdef VERLETLOG
>         write(*,'(a,i4,3f18.12)')'VBn',i, vel(:,inres)
> #endif
>       enddo
> #endif
1958,1959d2418
<       include 'COMMON.NMR'    
<       include 'COMMON.RESTARTED'
1976a2436
>       common /init_mdcommon/ varia
1984c2444,2446
<       include 'COMMON.AVNLOC'
---
> #ifdef FNLOG
>       write(*,*)'init_MD'
> #endif
1986d2447
<       call flush(iout)
2067d2527
<       restarted=.false.
2069d2528
<       call flush(iout)
2087c2546,2547
<         inquire(file=rest2name,exist=file_exist)
---
>         inquire(file=rsbname,exist=file_exist)
>         if(.not.file_exist) inquire(file=rest2name,exist=file_exist)
2090,2091d2549
<          restarted=.true.
<          atimeave=atimeave_save
2093,2095d2550
<            if(me.eq.king.or..not.out1file)
<      &      write(iout,*) "Initial state will be read from file ",
<      &      rest2name(:ilen(rest2name))
2096a2552,2571
> ! Zero CM velocity and net angular momentum for microcanonical runs
>            if (lang.eq.0 .and. .not.tbf) then
>            call place_coords_in_box
>            call inertia_tensor
>            call vcm_vel(vcm)
> !          write(*,'(a,3f18.10)')'vcm_vel=',vcm
> #ifdef VIRTUAL
>            do j=1,3
>              d_t(j,0)=d_t(j,0)-vcm(j)
>            enddo
> #else
>            do i=1,2*nres
>              vel(:,i)=vel(:,i)-vcm(:)
> !            write(*,'(a,i4,3f18.12)')'IM1',i,vel(:,i)
>            enddo
>            do i=nnt,nct-1
>              velp(:,i)=velp(:,i)-vcm(:)
>            enddo
> #endif
>            endif
2105a2581,2582
>           write(iout,*) "File ",rsbname(:ilen(rsbname)),
>      &       " does not exist"
2119a2597
> #ifdef VIRTUAL
2120a2599,2601
> #else
>       call kinetic_CASC(EK)
> #endif
2128,2129d2608
<       write (iout,*) "After generating random velocities"
<       call flush(iout)
2132a2612
> #ifdef VIRTUAL
2136a2617,2622
> #else
>        do i=1,nres
>          write (iout,'(i7,3f10.5,3x,3f10.5)') i,(vel(j,i),j=1,3),
>      &   (vel(j,i+nres),j=1,3)
>        enddo
> #endif
2139d2624
<       call flush(iout)
2149,2150d2633
<       write (iout,*) "restarted",restarted
<       call flush(iout)
2158,2159c2641
<             write (iout,*) "iranmin",iranmin
<             call flush(iout)
---
> #ifdef VIRTUAL
2160a2643,2645
> #else
>             call calculate_dC
> #endif
2240,2241c2725
<               if (me.eq.king .or. .not. out1file)
<      &         write (iout,*) "n_model_try",n_model_try
---
>               write (iout,*) "n_model_try",n_model_try
2313,2314c2797
<             if (me.eq.king.or..not.out1file)       
<      &         write (iout,*)
---
>               write (iout,*)
2323,2324c2806
<               if (me.eq.king.or..not.out1file)
<      &         write (iout,*) 'Calling OVERLAP_SC'
---
>               write (iout,*) 'Calling OVERLAP_SC'
2356a2839,2840
>             if(me.eq.king.or..not.out1file)
>      &       write(iout,*) 'LBFGS return code is ',status,' eval ',nfun
2359a2844,2845
>             if(me.eq.king.or..not.out1file)
>      &       write(iout,*) 'SUMSL return code is',iretcode,' eval ',nfun
2373a2860,2879
> #ifdef VIRTUAL
>         call chainbuild_cart
> #else
>         call calculate_dC
> #endif
> c        write (iout,*) "Coordinates before place_coords_in_box"
> c        do i=1,nres
> c          write (iout,'(i5,3f10.5,5x,3f10.5)') i,(c(j,i),j=1,3),
> c     &     (c(j,i+nres),j=1,3)
> c        enddo
>         call place_coords_in_box
> c        write (iout,*) "Coordinates after place_coords_in_box"
> c        do i=1,nres
> c          write (iout,'(i5,3f10.5,5x,3f10.5)') i,(c(j,i),j=1,3),
> c     &     (c(j,i+nres),j=1,3)
> c        enddo
> c        write (iout,*) "Peptide group coordinates"
> c        do i=1,nres-1
> c          write (iout,'(i5,3f10.5,5x,3f10.5)') i,(cp(j,i),j=1,3)
> c        enddo
2396a2903
> #ifdef VIRTUAL
2399a2907,2916
> #else
> !TODO: OMP?
>           do i=1,nres
>             if (itype(i).eq.ntyp1) cycle
>             vel(:,i)=vel(:,i)-vcm(:)
>             if (itype(i).eq.10) cycle
>             vel(:,i+nres)=vel(:,i+nres)-vcm(:)
>           enddo
>           call velocities
> #endif
2414c2931,2935
<       call chainbuild_cart
---
> #ifdef VIRTUAL
>         call chainbuild_cart
> #else
>         call calculate_dC
> #endif
2417,2418c2938
<       if (me.eq.king.or..not.out1file)
<      & write(iout,*) "Initial kinetic energy",EK," kinetic T",kinetic_T
---
>       write (iout,*) "Initial kinetic energy",EK," kinetic T",kinetic_T
2434,2435d2953
<       if(me.eq.king.or..not.out1file)
<      & write (iout,*) "before etotal restarted",restarted
2437,2472d2954
<       restarted=.false.
< c Diagnostics
< c      write(iout,*),"Energy components for initial structure"
< c      if (rest) call enerprint(potEcomp)
<       if (rest .and. atimeave_save.gt.0) then
<        if(me.eq.king.or..not.out1file) then
<         write (iout,'(a)') 
<      &   "Time-averaged energies computed and from restart info:"
<         write (iout,'(a,2f10.3)') "Edis    ",potEcomp(15),etimeave(1)
<         write (iout,'(a,2f10.3)') "Etor_r  ",potEcomp(19),etimeave(2)
<         write (iout,'(a,2f10.3)') "Etheta_r",potEcomp(24),etimeave(3)
<        endif
<       endif
< c End diagnostics
<       if (rest .and. atimeave_save.gt.0) then
<         if (dabs(potEcomp(15)-etimeave(1)).gt.1.0d-1
<      &    .or. dabs(potEcomp(19)-etimeave(2)).gt.1.0d-1
<      &    .or. dabs(potEcomp(24)-etimeave(3)).gt.1.0d-1) then
<          if(me.eq.king.or..not.out1file) then
<           write (iout,'(a)') 
<      &   "Warning, time-averaged energies different from restart info:"
<           write (iout,'(a,2f10.3)') "Edis    ",potEcomp(15),etimeave(1)
<           write (iout,'(a,2f10.3)') "Etor_r  ",potEcomp(19),etimeave(2)
<           write (iout,'(a,2f10.3)') "Etheta_r",potEcomp(24),etimeave(3)
<          endif
<         endif
< c         potEcomp(0)=potEcomp(0)-potEcomp(15)+etimeave(1)-
< c     &     potecomp(19)+etimeave(2)-potecomp(24)-etimeave(3)
< c         potEcomp(15)=etimeave(1)
< c         potEcomp(19)=etimeave(2)
< c         potEcomp(24)=etimeave(3)
<       endif
< c Diagnostics
< c      write(iout,*),"energy after assignment"
< c      if (rest) call enerprint(potEcomp)
< c End diagnostics
2546a3029,3038
> !TODO: OMP?
>       do i=1,2*nres
>         c_old(:,i)=c(:,i)
>       enddo
>       do i=1,2*nres
>         vel_old(:,i)=vel(:,i)
>       enddo
>       do i=nnt,nct-1
>         velp_old(:,i)=velp(:,i)
>       enddo
2614,3006d3105
< c-----------------------------------------------------------
<       subroutine random_vel
<       implicit none
<       include 'DIMENSIONS'
<       include 'COMMON.CONTROL'
<       include 'COMMON.VAR'
<       include 'COMMON.MD'
< #ifdef FIVEDIAG
<        include 'COMMON.LAGRANGE.5diag'
< #else
<        include 'COMMON.LAGRANGE'
< #endif
< #ifndef LANG0
<       include 'COMMON.LANGEVIN'
< #else
< #ifdef FIVEDIAG
<       include 'COMMON.LANGEVIN.lang0.5diag'
< #else
<       include 'COMMON.LANGEVIN.lang0'
< #endif
< #endif
<       include 'COMMON.CHAIN'
<       include 'COMMON.DERIV'
<       include 'COMMON.GEO'
<       include 'COMMON.LOCAL'
<       include 'COMMON.INTERACT'
<       include 'COMMON.IOUNITS'
<       include 'COMMON.NAMES'
<       include 'COMMON.TIME1'
<       double precision xv,sigv,lowb,highb,vec_afm(3),Ek1,Ek2,Ek3,aux
<       integer i,ii,j,k,l,ind
<       double precision anorm_distr
<       logical lprn /.false./
< #ifdef FIVEDIAG
<       integer ichain,n,innt,inct,ibeg,ierr
<       double precision work(8*maxres6)
<       integer iwork(maxres6)
<       double precision Ghalf(mmaxres2_chain),Geigen(maxres2_chain),
<      & Gvec(maxres2_chain,maxres2_chain)
<       common /przechowalnia/Ghalf,Geigen,Gvec
<       common /randomvelcommon/ work,iwork
< #ifdef DEBUG
<       double precision inertia(maxres2_chain,maxres2_chain)
< #endif
< c Generate random velocities from Gaussian distribution of mean 0 and std of KT/m
< c First generate velocities in the eigenspace of the G matrix
< c      write (iout,*) "Calling random_vel dimen dimen3",dimen,dimen3
< c      call flush(iout)
< #ifdef DEBUG
<       write (iout,*) "Random_vel, fivediag"
< #endif
<       d_t=0.0d0
<       Ek2=0.0d0
<       EK=0.0d0
<       Ek3=0.0d0
<       do ichain=1,nchain
<         ind=0
<         ghalf=0.0d0
<         n=dimen_chain(ichain)
<         innt=iposd_chain(ichain)
<         inct=innt+n-1
< #ifdef DEBUG
<         write (iout,*) "Chain",ichain," n",n," start",innt
<         do i=innt,inct
<           if (i.lt.inct-1) then
<             write (iout,'(2i3,3f10.5)') i,i-innt+1,DMorig(i),DU1orig(i),
<      &         DU2orig(i)
<           else if (i.eq.inct-1) then
<             write (iout,'(2i3,3f10.5)') i,i-innt+1,DMorig(i),DU1orig(i)
<           else
<             write (iout,'(2i3,3f10.5)') i,i-innt+1,DMorig(i)
<           endif
<         enddo
< #endif
<         ghalf(ind+1)=dmorig(innt)
<         ghalf(ind+2)=du1orig(innt)
<         ghalf(ind+3)=dmorig(innt+1)
<         ind=ind+3
<         do i=3,n
<           ind=ind+i-3
< c          write (iout,*) "i",i," ind",ind," indu2",innt+i-2,
< c     &       " indu1",innt+i-1," indm",innt+i
<           ghalf(ind+1)=du2orig(innt-1+i-2)
<           ghalf(ind+2)=du1orig(innt-1+i-1)
<           ghalf(ind+3)=dmorig(innt-1+i)
< c          write (iout,'(3(a,i2,1x))') "DU2",innt-1+i-2,
< c     &       "DU1",innt-1+i-1,"DM ",innt-1+i
<           ind=ind+3
<         enddo
< #ifdef DEBUG
<         ind=0
<         do i=1,n
<           do j=1,i
<             ind=ind+1
<             inertia(i,j)=ghalf(ind)
<             inertia(j,i)=ghalf(ind)
<           enddo
<         enddo
< #endif
< #ifdef DEBUG
<         write (iout,*) "Chain ",ichain," ind",ind," dim",n*(n+1)/2
<         write (iout,*) "Five-diagonal inertia matrix, lower triangle"
<         call matoutr(n,ghalf)
< #endif
<         call gldiag(maxres2_chain,n,n,Ghalf,work,Geigen,Gvec,ierr,iwork)
<         if (large) then
<           write (iout,'(//a,i3)')
<      &    "Eigenvectors and eigenvalues of the G matrix chain",ichain
<           call eigout(n,n,maxres2_chain,maxres2_chain,Gvec,Geigen)
<         endif
< #ifdef DIAGCHECK
< c check diagonalization
<         do i=1,n
<           do j=1,n
<             aux=0.0d0
<             do k=1,n
<               do l=1,n
<                 aux=aux+gvec(k,i)*gvec(l,j)*inertia(k,l)
<               enddo
<             enddo
<             if (i.eq.j) then
<               write (iout,*) i,j,aux,geigen(i)
<             else
<               write (iout,*) i,j,aux
<             endif
<           enddo
<         enddo
< #endif
<         xv=0.0d0
<         ii=0
<         do i=1,n
<           do k=1,3
<             ii=ii+1
<             sigv=dsqrt((Rb*t_bath)/geigen(i))
<             lowb=-5*sigv
<             highb=5*sigv
<             d_t_work_new(ii)=anorm_distr(xv,sigv,lowb,highb)
<             EK=EK+0.5d0*geigen(i)*d_t_work_new(ii)**2
< c            write (iout,*) "i",i," ii",ii," geigen",geigen(i),
< c     &      " d_t_work_new",d_t_work_new(ii)
<           enddo
<         enddo
<         do k=1,3
<           do i=1,n
<             ind=(i-1)*3+k
<             d_t_work(ind)=0.0d0
<             do j=1,n
<               d_t_work(ind)=d_t_work(ind)
<      &                +Gvec(i,j)*d_t_work_new((j-1)*3+k)
<             enddo
< c            write (iout,*) "i",i," ind",ind," d_t_work",d_t_work(ind)
< c            call flush(iout)
<           enddo
<         enddo
< #ifdef DEBUG
<         aux=0.0d0
<         do k=1,3
<           do i=1,n
<             do j=1,n
<             aux=aux+inertia(i,j)*d_t_work(3*(i-1)+k)*d_t_work(3*(j-1)+k)
<             enddo
<           enddo
<         enddo
<         Ek3=Ek3+aux/2
< #endif
< c Transfer to the d_t vector
<         innt=chain_border(1,ichain)
<         inct=chain_border(2,ichain)
<         ind=0
< c        write (iout,*) "ichain",ichain," innt",innt," inct",inct
<         do i=innt,inct
<           do j=1,3
<             ind=ind+1
<             d_t(j,i)=d_t_work(ind)
<           enddo
<           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)
<             enddo
<           endif
<         enddo
<       enddo
<       if (large) then
<         write (iout,*)
<         write (iout,*) "Random velocities in the Calpha,SC space"
<         do i=1,nres
<           write (iout,'(a3,1h(,i5,1h),3f10.5,3x,3f10.5)')
<      &    restyp(itype(i)),i,(d_t(j,i),j=1,3),(d_t(j,i+nres),j=1,3)
<         enddo
<       endif
<       call kinetic_CASC(Ek1)
< !
< ! Transform the velocities to virtual-bond space
< !
< #define WLOS
< #ifdef WLOS
<       if (nnt.eq.1) then
<         d_t(:,0)=d_t(:,1)
<       endif
<       do i=1,nres
<         if (itype(i).eq.10 .or. itype(i).eq.ntyp1) then
<           do j=1,3
<             d_t(j,i)=d_t(j,i+1)-d_t(j,i)
<           enddo
<         else
<           do j=1,3
<             d_t(j,i+nres)=d_t(j,i+nres)-d_t(j,i)
<             d_t(j,i)=d_t(j,i+1)-d_t(j,i)
<           enddo
<         end if
<       enddo
<       d_t(:,nres)=0.0d0
<       d_t(:,nct)=0.0d0
<       d_t(:,2*nres)=0.0d0
<       if (nnt.gt.1) then
<         d_t(:,0)=d_t(:,1)
<         d_t(:,1)=0.0d0
<       endif
< c      d_a(:,0)=d_a(:,1)
< c      d_a(:,1)=0.0d0
< c      write (iout,*) "Shifting accelerations"
<       do ichain=2,nchain
< c        write (iout,*) "ichain",chain_border1(1,ichain)-1,
< c     &     chain_border1(1,ichain)
<         d_t(:,chain_border1(1,ichain)-1)=d_t(:,chain_border1(1,ichain))
<         d_t(:,chain_border1(1,ichain))=0.0d0
<       enddo
< c      write (iout,*) "Adding accelerations"
<       do ichain=2,nchain
< c        write (iout,*) "chain",ichain,chain_border1(1,ichain)-1,
< c     &   chain_border(2,ichain-1)
<         d_t(:,chain_border1(1,ichain)-1)=
<      &  d_t(:,chain_border1(1,ichain)-1)+d_t(:,chain_border(2,ichain-1))
<         d_t(:,chain_border(2,ichain-1))=0.0d0
<       enddo
<       do ichain=2,nchain
<         write (iout,*) "chain",ichain,chain_border1(1,ichain)-1,
<      &   chain_border(2,ichain-1)
<         d_t(:,chain_border1(1,ichain)-1)=
<      &  d_t(:,chain_border1(1,ichain)-1)+d_t(:,chain_border(2,ichain-1))
<         d_t(:,chain_border(2,ichain-1))=0.0d0
<       enddo
< #ifdef DEBUG
<       call kinetic(EK)
<       write (iout,*) "Kinetic energy",Ek," kinetic temperature",
<      &  2.0d0/(dimen3*Rb)*EK
< c      call flush(iout)
< #endif
< #else
<       ibeg=0
< c      do j=1,3
< c        d_t(j,0)=d_t(j,nnt)
< c      enddo
<       do ichain=1,nchain
<       innt=chain_border(1,ichain)
<       inct=chain_border(2,ichain)
< c      write (iout,*) "ichain",ichain," innt",innt," inct",inct
< c      write (iout,*) "ibeg",ibeg
<       do j=1,3
<         d_t(j,ibeg)=d_t(j,innt)
<       enddo
<       ibeg=inct+1
<       do i=innt,inct
<         if (iabs(itype(i).eq.10)) then
< c          write (iout,*) "i",i,(d_t(j,i),j=1,3),(d_t(j,i+1),j=1,3)
<           do j=1,3
<             d_t(j,i)=d_t(j,i+1)-d_t(j,i)
<           enddo
<         else
<           do j=1,3
<             d_t(j,i+nres)=d_t(j,i+nres)-d_t(j,i)
<             d_t(j,i)=d_t(j,i+1)-d_t(j,i)
<           enddo
<         end if
<       enddo
<       enddo
< #endif
<       if (large) then
<         write (iout,*)
<         write (iout,*)
<      &    "Random velocities in the virtual-bond-vector space"
<         write (iout,'(3hORG,1h(,i5,1h),3f10.5)') 0,(d_t(j,0),j=1,3)
<         do i=1,nres
<           write (iout,'(a3,1h(,i5,1h),3f10.5,3x,3f10.5)')
<      &     restyp(itype(i)),i,(d_t(j,i),j=1,3),(d_t(j,i+nres),j=1,3)
<         enddo
<         write (iout,*)
<         write (iout,*) "Kinetic energy from inertia matrix eigenvalues",
<      &   Ek
<         write (iout,*)
<      &   "Kinetic temperatures from inertia matrix eigenvalues",
<      &   2*Ek/(3*dimen*Rb)
< #ifdef DEBUG
<         write (iout,*) "Kinetic energy from inertia matrix",Ek3
<         write (iout,*) "Kinetic temperatures from inertia",
<      &   2*Ek3/(3*dimen*Rb)
< #endif
<         write (iout,*) "Kinetic energy from velocities in CA-SC space",
<      &   Ek1
<         write (iout,*)
<      &   "Kinetic temperatures from velovities in CA-SC space",
<      &   2*Ek1/(3*dimen*Rb)
<         call kinetic(Ek1)
<         write (iout,*)
<      &   "Kinetic energy from virtual-bond-vector velocities",Ek1
<         write (iout,*)
<      &   "Kinetic temperature from virtual-bond-vector velocities ",
<      &   2*Ek1/(dimen3*Rb)
<       endif
< #else
<       xv=0.0d0
<       ii=0
<       do i=1,dimen
<         do k=1,3
<           ii=ii+1
<           sigv=dsqrt((Rb*t_bath)/geigen(i))
<           lowb=-5*sigv
<           highb=5*sigv
<           d_t_work_new(ii)=anorm_distr(xv,sigv,lowb,highb)
< 
< c          write (iout,*) "i",i," ii",ii," geigen",geigen(i),
< c     &      " d_t_work_new",d_t_work_new(ii)
<         enddo
<       enddo
< C       if (SELFGUIDE.gt.0) then
< C       distance=0.0
< C       do j=1,3
< C       vec_afm(j)=c(j,afmend)-c(j,afmbeg)
< C       distance=distance+vec_afm(j)**2
< C       enddo
< C       distance=dsqrt(distance)
< C       do j=1,3
< C         d_t_work_new(j+(afmbeg-1)*3)=-velAFMconst*vec_afm(j)/distance
< C         d_t_work_new(j+(afmend-1)*3)=velAFMconst*vec_afm(j)/distance
< C         write(iout,*) "myvel",d_t_work_new(j+(afmbeg-1)*3),
< C     &    d_t_work_new(j+(afmend-1)*3)
< C       enddo
< 
< C       endif
< 
< c diagnostics
< c      Ek1=0.0d0
< c      ii=0
< c      do i=1,dimen
< c        do k=1,3
< c          ii=ii+1
< c          Ek1=Ek1+0.5d0*geigen(i)*d_t_work_new(ii)**2
< c        enddo
< c      enddo
< c      write (iout,*) "Ek from eigenvectors",Ek1
< c end diagnostics
< c Transform velocities to UNRES coordinate space
<       do k=0,2
<         do i=1,dimen
<           ind=(i-1)*3+k+1
<           d_t_work(ind)=0.0d0
<           do j=1,dimen
<             d_t_work(ind)=d_t_work(ind)
<      &                      +Gvec(i,j)*d_t_work_new((j-1)*3+k+1)
<           enddo
< c          write (iout,*) "i",i," ind",ind," d_t_work",d_t_work(ind)
< c          call flush(iout)
<         enddo
<       enddo
< c Transfer to the d_t vector
<       do j=1,3
<         d_t(j,0)=d_t_work(j)
<       enddo
<       ind=3
<       do i=nnt,nct-1
<         do j=1,3
<           ind=ind+1
<           d_t(j,i)=d_t_work(ind)
<         enddo
<       enddo
<       do i=nnt,nct
<         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)
<           enddo
<         endif
<       enddo
< #ifdef DEBUG
<       call kinetic(EK)
<       write (iout,*) "Kinetic energy",Ek,EK1," kinetic temperature",
<      &  2.0d0/(dimen3*Rb)*EK,2.0d0/(dimen3*Rb)*EK1
< c      call flush(iout)
< #endif
< #endif
<       return
<       end
3643a3743,4015
> c-------------------------------------------------------------------------------
>       subroutine memcpy1(trg,src,cnt)
>       implicit none
>       integer cnt
>       double precision trg(1:cnt)
>       double precision src(1:cnt)
>       trg(1:cnt)=src(1:cnt)
>       return
>       end
> c-------------------------------------------------------------------------------
>       subroutine memcpy3(trg,src,cnt)
>       implicit none
>       integer cnt
>       double precision trg(3,1:cnt)
>       double precision src(3,1:cnt)
>       call memcpy1(trg(1,1),src(1,1),cnt*3)
>       return
>       end
> c-------------------------------------------------------------------------------
> #ifdef VOUT
>       subroutine vout
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       include 'COMMON.INTERACT'
> #ifdef FIVEDIAG
>       include 'COMMON.LANGEVIN.lang0.5diag'
> #else
>       include 'COMMON.LANGEVIN.lang0'
> #endif
> 
>       integer i,j,ind
>       double precision v_work(maxres6),v_transf(maxres6)
>       common /voitcommon/ v_work,v_transf
> 
>       do j=1,3
>         v_work(j)=d_t(j,0)
>       enddo
>       ind=3
>       do i=nnt,nct-1
>         do j=1,3
>           ind=ind+1
>           v_work(ind)=d_t(j,i)
>         enddo
>       enddo
>       do i=nnt,nct
>         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)
>           enddo
>         endif
>       enddo
> 
>       write (66,'(80f10.5)')
>      &  ((d_t(j,i),j=1,3),i=0,nres-1),((d_t(j,i+nres),j=1,3),i=1,nres)
>       do i=1,ind
>         v_transf(i)=0.0d0
>         do j=1,ind
>           v_transf(i)=v_transf(i)+gvec(j,i)*v_work(j)
>         enddo
>         v_transf(i)=v_transf(i)*dsqrt(geigen(i))
>       enddo
>       write (67,'(80f10.5)') (v_transf(i),i=1,ind)
>       end
> #endif
> c-------------------------------------------------------------------------------
>       subroutine write_ntwe_step1
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       include 'COMMON.MD'
>       include 'COMMON.IOUNITS'
> #ifdef FIVEDIAG
>       include 'COMMON.LAGRANGE.5diag'
> #else
>       include 'COMMON.LAGRANGE'
> #endif
>       integer i,j
> 
>       write (iout,*) "Cartesian and internal coordinates: step 1"
>       call cartprint
>       call intout
> #ifdef VIRTUAL
>       write (iout,*) "dC"
>       do i=0,nres
>         write (iout,'(i3,3f10.5,3x,3f10.5)') i,(dc(j,i),j=1,3),
>      &  (dc(j,i+nres),j=1,3)
>       enddo
>       write (iout,*) "Accelerations"
>       do i=0,nres
>         write (iout,'(i3,3f10.5,3x,3f10.5)') i,(d_a(j,i),j=1,3),
>      &  (d_a(j,i+nres),j=1,3)
>       enddo
>       write (iout,*) "Velocities, step 1"
>       do i=0,nres
>         write (iout,'(i3,3f10.5,3x,3f10.5)') i,(d_t(j,i),j=1,3),
>      &  (d_t(j,i+nres),j=1,3)
>       enddo
> #else
>       write (iout,*) "C"
>       do i=1,nres
>         write (iout,'(i3,3f10.5,3x,3f10.5)') i,(c(j,i),j=1,3),
>      &  (c(j,i+nres),j=1,3)
>       enddo
>       write (iout,*) "Accelerations"
>       do i=1,nres
>         write (iout,'(i3,3f10.5,3x,3f10.5)') i,(d_a(j,i),j=1,3),
>      &  (d_a(j,i+nres),j=1,3)
>       enddo
>       write (iout,*) "Velocities, step 1"
>       do i=1,nres
>         write (iout,'(i3,3f10.5,3x,3f10.5)') i,(vel(j,i),j=1,3),
>      &  (vel(j,i+nres),j=1,3)
>       enddo
> #endif
>       end
> c-------------------------------------------------------------------------------
>       subroutine write_ntwe_step2
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       include 'COMMON.MD'
>       include 'COMMON.IOUNITS'
> #ifdef FIVEDIAG
>       include 'COMMON.LAGRANGE.5diag'
> #else
>       include 'COMMON.LAGRANGE'
> #endif
>       integer i,j
> 
>       write (iout,*) "Velocities, step 2"
> 
> #ifdef VIRTUAL
>       do i=0,nres
>         write (iout,'(i3,3f10.5,3x,3f10.5)') i,(d_t(j,i),j=1,3),
>      &    (d_t(j,i+nres),j=1,3)
>       enddo
> #else
>       do i=1,nres
>         write (iout,'(i3,3f10.5,3x,3f10.5)') i,(vel(j,i),j=1,3),
>      &    (vel(j,i+nres),j=1,3)
>       enddo
> #endif
>       end
> c-------------------------------------------------------------------------------
>       subroutine debug_write_when_nan_energy(itime)
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       include 'COMMON.INTERACT'
>       include 'COMMON.MD'
>       include 'COMMON.IOUNITS'
> #ifdef FIVEDIAG
>       include 'COMMON.LAGRANGE.5diag'
> #else
>       include 'COMMON.LAGRANGE'
> #endif
> #ifndef LANG0
>       include 'COMMON.LANGEVIN'
> #else
> #ifdef FIVEDIAG
>       include 'COMMON.LANGEVIN.lang0.5diag'
> #else
>       include 'COMMON.LANGEVIN.lang0'
> #endif
> #endif
>       integer itime
>       integer i,j
> 
>       write (iout,*) "NaNs in energy itime",itime
>       call enerprint(potEcomp)
>       write (iout,*) "DMfric",DMfric(:2*nres)
>       write (iout,*) "DU1fric",DU1fric(:2*nres)
>       write (iout,*) "DU2fric",DU2fric(:2*nres)
>       write (iout,*) "fric,stoch"
>       write (iout,*) fric_work(:6*nres)
>       write (iout,*) stoch_work(:6*nres)
>       write (iout,*) "gcart,gxcart"
>       do i=1,nres
>         write (iout,'(i5,3e15.5,5x,3e15.5)')i,(-gcart(j,i),j=1,3),
>      &    (-gxcart(j,i),j=1,3)
>       enddo
> #ifdef VIRTUAL
>       write(iout,*) "dc, dc_old"
>       do i=1,nres
>         write (iout,'(i5,3e15.5,5x,3e15.5)')i,(dc(j,i),j=1,3),
>      &    (dc_old(j,i),j=1,3)
>       enddo
>       write(iout,*) "d_t, d_t_old"
>       do i=1,nres
>         write (iout,'(i5,3e15.5,5x,3e15.5)')i,(d_t(j,i),j=1,3),
>      &    (d_t_old(j,i),j=1,3)
>       enddo
>       write(iout,*) "d_a, d_a_old"
>       do i=1,nres
>         write (iout,'(i5,3e15.5,5x,3e15.5)')i,(d_a(j,i),j=1,3),
>      &    (d_a_old(j,i),j=1,3)
>       enddo
> #else
>       write(iout,*) "c, c_old"
>       do i=1,nres
>         write (iout,'(i5,3e15.5,5x,3e15.5)')i,(c(j,i),j=1,3),
>      &    (c_old(j,i),j=1,3)
>       enddo
>       write(iout,*) "vel, vel_old"
>       do i=1,nres
>         write (iout,'(i5,3e15.5,5x,3e15.5)')i,(vel(j,i),j=1,3),
>      &    (vel_old(j,i),j=1,3)
>       enddo
>       write(iout,*) "d_a, d_a_old"
>       do i=1,nres
>         write (iout,'(i5,3e15.5,5x,3e15.5)')i,(d_a(j,i),j=1,3),
>      &    (d_a_old(j,i),j=1,3)
>       enddo
> #endif
>       end
> c-------------------------------------------------------------------------------
>       subroutine debug_write_when_nan_d_a(itime)
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       include 'COMMON.INTERACT'
>       include 'COMMON.MD'
>       include 'COMMON.IOUNITS'
> #ifdef FIVEDIAG
>       include 'COMMON.LAGRANGE.5diag'
> #else
>       include 'COMMON.LAGRANGE'
> #endif
> #ifndef LANG0
>       include 'COMMON.LANGEVIN'
> #else
> #ifdef FIVEDIAG
>       include 'COMMON.LANGEVIN.lang0.5diag'
> #else
>       include 'COMMON.LANGEVIN.lang0'
> #endif
> #endif
>       integer itime
>       integer i,j
> 
>       write (iout,*) "NaN in accelerations itime",itime
>       write (iout,*) "gcart,gxcart"
>       do i=1,nres
>         write (iout,'(i5,3e15.5,5x,3e15.5)')i,(-gcart(j,i),j=1,3),
>      &    (-gxcart(j,i),j=1,3)
>       enddo
> ! ADDED IN 44
> #ifdef VIRTUAL
>       write(iout,*) "dc, dc_old"
>       do i=1,nres
>         write (iout,'(i5,3e15.5,5x,3e15.5)')i,(dc(j,i),j=1,3),
>      &    (dc_old(j,i),j=1,3)
>       enddo
>       write(iout,*) "d_a, d_a_old"
>       do i=1,nres
>         write (iout,'(i5,3e15.5,5x,3e15.5)')i,(d_a(j,i),j=1,3),
>      &    (d_a_old(j,i),j=1,3)
>       enddo
> #else
>       write(iout,*) "c, c_old"
>       do i=1,nres
>         write (iout,'(i5,3e15.5,5x,3e15.5)')i,(c(j,i),j=1,3),
>      &    (c_old(j,i),j=1,3)
>       enddo
>       write(iout,*) "d_a, d_a_old"
>       do i=1,nres
>         write (iout,'(i5,3e15.5,5x,3e15.5)')i,(d_a(j,i),j=1,3),
>      &    (d_a_old(j,i),j=1,3)
>       enddo
> #endif
>       end
minima.f
minimize_p.F
194d193
<       include 'COMMON.RESTARTED'
202a202,203
>       include 'COMMON.OMP'
>       include 'COMMON.GRID'
204c205
<       double precision edum(0:n_ene),time_order(0:20)
---
>       double precision edum(0:n_ene),time_order(0:15)
220,222d220
< c       write (iout,*)
< c     & 'Processor',fg_rank,' before receiving order'
< c       call flush(iout)
230,231c228,229
<         icall=icall+1
< c       write (iout,*)
---
>        icall=icall+1
> c      write (*,*)
233,234d230
< c       call flush(iout)
<     
238d233
<           restarted=.false.
245d239
<           restarted=.false.
252d245
<           restarted=.false.
260a254,255
>         else if (iorder.eq.13) then
>           call sum_gradient_int
312a308
> #ifdef VIRTUAL
313a310,314
> #else
>           call calculate_dC
> #endif
>         else if (iorder.eq.14) then
>           call chainbuild_extconf
326,338c327,329
<         else if (iorder.eq.13) then
<           call nmr_ave_restart_gather
<         else if (iorder.eq.14) then
<           call nmr_ave_restart_Bscatter
<           restarted=.true.
<         else if (iorder.eq.15) then
<           call save_nmrave_info
<         else if (iorder.eq.16) then
<           call restore_nmrave_info
<         else if (iorder.eq.17) then
<           write (iout,*) "Replica average order received"
<           call flush(iout)
<           call replica_average
---
>         else if (iorder.eq.15) then     ! ordered before MD loop
>           call restart_grid_tuning(grid_opts)
>           call restart_grid_tuning(saxs_grid_opts)
374c365
<       call sum_gradient
---
>       call sum_gradient_int
402c393
<       common /chuju/ jjj
---
>       common /korus/ jjj
422c413
<       call sum_gradient
---
>       call sum_gradient_int
439c430
<       common /chuju/ jjj
---
>       common /korus/ jjj
457c448
<       call sum_gradient
---
>       call sum_gradient_int
590a582
>       include 'COMMON.INTERACT'
658a651
> #ifdef VIRTUAL
675a669,693
> #else
> c      write (iout,*) "Array ialph"
> c      do i=1,nres
> c        write(iout,'(2i5,5x,2i5)') i,itype(i),ialph(i,1),ialph(i,2)
> c      enddo
>       k=0
>       do i=1,nres
>         if (itype(i).eq.ntyp1) cycle
>         do j=1,3
>           k=k+1
>           x(k)=c(j,i)
>         enddo
>       enddo
>       do i=2,nres-1
>         if (ialph(i,1).gt.0) then
>         do j=1,3
>           k=k+1
>           x(k)=c(j,i+nres)
>         enddo
>         endif
>       enddo
>       nvarx=k
> c      write (iout,*) "nvar",nvar
>       call calculate_dC
> #endif
699c717,723
< c      write (iout,*) "minim_dc Calling lbfgs"
---
> #ifdef DEBUG
>       write (iout,*) "checkgrad before LBFGS"
>       icheckgrad=1
>       aincr=1.0d-4
>       call exec_checkgrad
>       write (iout,*) "minim_dc Calling lbfgs"
> #endif
702c726,732
< c      write (iout,*) "minim_dc After lbfgs"
---
> #ifdef DEBUG
>       write (iout,*) "minim_dc After lbfgs"
>       write (iout,*) "checkgrad after LBFGS"
>       icheckgrad=1
>       aincr=1.0d-4
>       call exec_checkgrad
> #endif
716a747
> #ifdef VIRTUAL
732a764,790
> #else
>       k=0
>       do i=1,nres
>         if (itype(i).eq.ntyp1) cycle
>         do j=1,3
>           k=k+1
>           c(j,i)=x(k)
>         enddo
>       enddo
> c      write (iout,*) "k",k
>       do i=1,nres
> c        write (iout,*) "i",i," ialph",ialph(i,1)
>         if (ialph(i,1).gt.0) then
>         do j=1,3
>           k=k+1
> c          write (iout,*) "i,j,k",i,j,k
>           c(j,i+nres)=x(k)
>         enddo
> c        else 
> c        write (iout,*) "Gly"
> c        do j=1,3
> c          c(j,i+nres)=c(j,i)
> c        enddo
>         endif
>       enddo
>       call calculate_dC
> #endif
772a831
> #ifdef VIRTUAL
788a848,871
> #else
>       k=0
>       do i=1,nres
>         if (itype(i).eq.ntyp1) cycle
>         do j=1,3
>           k=k+1
>           c(j,i)=x(k)
>         enddo
>       enddo
>       do i=1,nres
>         if (ialph(i,1).gt.0) then
>         do j=1,3
>           k=k+1
>           c(j,i+nres)=x(k)
>         enddo
> c        else
> c        do j=1,3
> c          c(j,i+nres)=c(j,i)
> c        enddo
>         endif
>       enddo
> c      write (iout,*) "funcgrad_dc 1: k",k
>       call calculate_dC
> #endif
796a880
> #ifdef VIRTUAL
812c896,914
< 
---
> #else
>       k=0
>       do i=1,nres
>         if (itype(i).eq.ntyp1) cycle
>         do j=1,3
>           k=k+1
>           g(k)=gcart(j,i)
>         enddo
>       enddo
>       do i=2,nres-1
>         if (ialph(i,1).gt.0) then
>         do j=1,3
>           k=k+1
>           g(k)=gxcart(j,i)
>         enddo
>         endif
>       enddo
> c      write (iout,*) "funcgrad_dc 2: k",k
> #endif
837a940
> #ifdef VIRTUAL
854c957,974
< 
---
> #else
>       k=0
>       do i=1,nres
>         do j=1,3
>           k=k+1
>           c(j,i)=x(k)
>         enddo
>       enddo
>       do i=2,nres-1
>         if (ialph(i,1).gt.0) then
>         do j=1,3
>           k=k+1
>           c(j,i+nres)=x(k)
>         enddo
>         endif
>       enddo
>       call calculate_dC
> #endif
894a1015
> #ifdef VIRTUAL
910a1032,1049
> #else
>       k=0
>       do i=1,nres
>         do j=1,3
>           k=k+1
>           c(j,i)=x(k)
>         enddo
>       enddo
>       do i=2,nres-1
>         if (ialph(i,1).gt.0) then
>         do j=1,3
>           k=k+1
>           c(j,i+nres)=x(k)
>         enddo
>         endif
>       enddo
>       call calculate_dC
> #endif
916a1056
> #ifdef VIRTUAL
932c1072,1088
< 
---
> #else
>       k=0
>       do i=1,nres
>         do j=1,3
>           k=k+1
>           g(k)=gcart(j,i)
>         enddo
>       enddo
>       do i=2,nres-1
>         if (ialph(i,1).gt.0) then
>         do j=1,3
>           k=k+1
>           g(k)=gxcart(j,i)
>         enddo
>         endif
>       enddo
> #endif
minim_jlee.F
138c138
<           call chainbuild
---
>           call chainbuild_extconf
193c193
<           call chainbuild
---
>           call chainbuild_extconf
minim_mcmf.F
44c44
<       ichuj=0
---
>       ikorus=0
46c46
<       ichuj = ichuj + 1
---
>       ikorus = ikorus + 1
50c50
< c      print *, 'worker ',me,' received order ',n,ichuj
---
> c      print *, 'worker ',me,' received order ',n,ikorus
misc.f
moments.F
1a2
> #ifdef VIRTUAL
8,11d8
< #ifdef MPI
<       include 'mpif.h'
<       integer ierr
< #endif
186c183
<         do i=1,3    
---
>         do i=1,3
204c201
<      &    (d_t(j,i),j=1,3),(d_t(j,i+nres),j=1,3) 
---
>      &    (d_t(j,i),j=1,3),(d_t(j,i+nres),j=1,3)
221c218
<       do i=nnt,nct 
---
>       do i=nnt,nct
234c231
<      &    (d_t(j,i),j=1,3),(d_t(j,i+nres),j=1,3) 
---
>      &    (d_t(j,i),j=1,3),(d_t(j,i+nres),j=1,3)
405c402
< c   calculating the center of the mass of the protein	
---
> c   calculating the center of the mass of the protein
446a444
> ! end virtual bond vector section
447a446,930
> C--------------------------------------------------------------------------
> ! begin no virtual bond vector section
>       subroutine inertia_tensor
> c Calculating the intertia tensor for the entire protein in order to
> c remove the perpendicular components of velocity matrix which cause
> c the molecule to rotate.
>        implicit none
>        include 'DIMENSIONS'
> !#ifdef MPI
> !      include 'mpif.h'
> !      integer ierr
> !#endif
>        include 'COMMON.CONTROL'
>        include 'COMMON.VAR'
>        include 'COMMON.MD'
>        include 'COMMON.LAGRANGE.5diag'
>        include 'COMMON.CHAIN'
>        include 'COMMON.DERIV'
>        include 'COMMON.GEO'
>        include 'COMMON.LOCAL'
>        include 'COMMON.INTERACT'
>        include 'COMMON.IOUNITS'
>        include 'COMMON.NAMES'
> 
>       double precision Im(3,3),Imcp(3,3),cm(3),pr(3),M_SC,
>      & eigvec(3,3),Id(3,3),eigval(3),L(3),vp(3),vrot(3),
> !    & vpp(3,0:MAXRES),vs_p(3),pr1(3,3),
>      & vs_p(3),pr1(3,3),
>      & pr2(3,3),pp(3),incr(3),v(3),mag,mag2,ctemp(3)
>       common /gucio/ cm
> !     common /inertiaternsorcommon/ vpp
>       integer iti,inres,i,j,k,ichain
>       logical osob
> #ifdef DEBUG
>       write (iout,*) "Calling inertia_tensor dimen",dimen
> #endif
>       do i=1,3
>         do j=1,3
>           Im(i,j)=0.0d0
>           pr1(i,j)=0.0d0
>           pr2(i,j)=0.0d0
>         enddo
>         L(i)=0.0d0
>         cm(i)=0.0d0
>         vrot(i)=0.0d0
>       enddo
> c   calculating the center of the mass of the protein	
>       call calc_CM(cm)
> c      write (iout,*) "inertia: CM",cm
>       do i=nnt,nct-1
>         if (itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1) cycle
>         do j=1,3
> !          pr(j)=c(j,i)+0.5d0*dc(j,i)-cm(j)
> c          pr(j)=cp(j,i)-cm(j)
>           pr(j)=0.5d0*(c(j,i)+c(j,i+1))-cm(j)
>         enddo
>         Im(1,1)=Im(1,1)+mp*(pr(2)*pr(2)+pr(3)*pr(3))
>         Im(1,2)=Im(1,2)-mp*pr(1)*pr(2)
>         Im(1,3)=Im(1,3)-mp*pr(1)*pr(3)
>         Im(2,3)=Im(2,3)-mp*pr(2)*pr(3)
>         Im(2,2)=Im(2,2)+mp*(pr(3)*pr(3)+pr(1)*pr(1))
>         Im(3,3)=Im(3,3)+mp*(pr(1)*pr(1)+pr(2)*pr(2))
>       enddo
> 
>       do i=nnt,nct
>         iti=iabs(itype(i))
>         if (iti.eq.ntyp1) cycle
>         inres=i+nres
>         do j=1,3
>           pr(j)=c(j,inres)-cm(j)
>         enddo
>         Im(1,1)=Im(1,1)+msc(iabs(iti))*(pr(2)*pr(2)+pr(3)*pr(3))
>         Im(1,2)=Im(1,2)-msc(iabs(iti))*pr(1)*pr(2)
>         Im(1,3)=Im(1,3)-msc(iabs(iti))*pr(1)*pr(3)
>         Im(2,3)=Im(2,3)-msc(iabs(iti))*pr(2)*pr(3)
>         Im(2,2)=Im(2,2)+msc(iabs(iti))*(pr(3)*pr(3)+pr(1)*pr(1))
>         Im(3,3)=Im(3,3)+msc(iabs(iti))*(pr(1)*pr(1)+pr(2)*pr(2))
>       enddo
> 
>       do i=nnt,nct-1
>         if (itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1) cycle
>         Im(1,1)=Im(1,1)+Ip*(1-dc_norm(1,i)*dc_norm(1,i))*
>      &  vbld(i+1)*vbld(i+1)*0.25d0
>         Im(1,2)=Im(1,2)+Ip*(-dc_norm(1,i)*dc_norm(2,i))*
>      &  vbld(i+1)*vbld(i+1)*0.25d0
>         Im(1,3)=Im(1,3)+Ip*(-dc_norm(1,i)*dc_norm(3,i))*
>      &  vbld(i+1)*vbld(i+1)*0.25d0
>         Im(2,3)=Im(2,3)+Ip*(-dc_norm(2,i)*dc_norm(3,i))*
>      &  vbld(i+1)*vbld(i+1)*0.25d0
>         Im(2,2)=Im(2,2)+Ip*(1-dc_norm(2,i)*dc_norm(2,i))*
>      &  vbld(i+1)*vbld(i+1)*0.25d0
>         Im(3,3)=Im(3,3)+Ip*(1-dc_norm(3,i)*dc_norm(3,i))*
>      &  vbld(i+1)*vbld(i+1)*0.25d0
>       enddo
>       do i=nnt,nct
>         iti=iabs(itype(i))
>         if (iti.ne.10 .and. iti.ne.ntyp1) then
>           inres=i+nres
>           Im(1,1)=Im(1,1)+Isc(iti)*(1-dc_norm(1,inres)*
>      &    dc_norm(1,inres))*vbld(inres)*vbld(inres)
>           Im(1,2)=Im(1,2)-Isc(iti)*(dc_norm(1,inres)*
>      &    dc_norm(2,inres))*vbld(inres)*vbld(inres)
>           Im(1,3)=Im(1,3)-Isc(iti)*(dc_norm(1,inres)*
>      &    dc_norm(3,inres))*vbld(inres)*vbld(inres)
>           Im(2,3)=Im(2,3)-Isc(iti)*(dc_norm(2,inres)*
>      &    dc_norm(3,inres))*vbld(inres)*vbld(inres)
>           Im(2,2)=Im(2,2)+Isc(iti)*(1-dc_norm(2,inres)*
>      &    dc_norm(2,inres))*vbld(inres)*vbld(inres)
>           Im(3,3)=Im(3,3)+Isc(iti)*(1-dc_norm(3,inres)*
>      &    dc_norm(3,inres))*vbld(inres)*vbld(inres)
>         endif
>       enddo
>       call angmom(cm,L)
> #ifdef DEBUG
>       write(iout,*) "The angular momentum before adjustment:"
>       write(iout,*) (L(j),j=1,3)
> #endif
>       Im(2,1)=Im(1,2)
>       Im(3,1)=Im(1,3)
>       Im(3,2)=Im(2,3)
> #ifdef DEBUG
>       write (iout,*) "The moment of inertia tensor"
>       do i=1,3
>         write (iout,'(i5,3f10.5)') i,(Im(i,j),j=1,3)
>       enddo
> #endif
> c  Copy the Im matrix for the djacob subroutine
>       do i=1,3
>         do j=1,3
>           Imcp(i,j)=Im(i,j)
>           Id(i,j)=0.0d0
>         enddo
>       enddo
> 
>       if (dimen.gt.2) then
> c Try to compute the net angular velocity by solving the equations
> c system using the Banachiewicz method.
>         vrot=L
>         call banach(3,3,Imcp,vrot,osob)
> #ifdef DEBUG
>         write (iout,*) "osob",osob
>         write (iout,*) "vrot",vrot
> #endif
>       else
>         osob=.true.
>       endif
> 
>       if (osob) then
> C General inverse of the inertia matrix by diagonalization if the
> C inertia matrix is singular
> c Finding the eigenvectors and eignvalues of the inertia tensor
>         call djacob(3,3,10000,1.0d-15,Imcp,eigvec,eigval)
> #ifdef DEBUG
>         write (iout,*) "Eigenvalues & Eigenvectors"
>         write (iout,'(5x,3f10.5)') (eigval(i),i=1,3)
>         write (iout,*)
>         do i=1,3
>           write (iout,'(i5,3f10.5)') i,(eigvec(i,j),j=1,3)
>         enddo
> #endif
> c   Constructing the G-inverse of the inertia matrix
>         do i=1,3
>           if (dabs(eigval(i)).gt.1.0d-7) then
>             Id(i,i)=1.0d0/eigval(i)
>           else
>             Id(i,i)=0.0d0
>           endif
>         enddo
>         do i=1,3
>           do j=1,3
>             Imcp(i,j)=eigvec(j,i)
>           enddo
>         enddo
>         do i=1,3
>           do j=1,3
>             do k=1,3
>                pr1(i,j)=pr1(i,j)+Id(i,k)*Imcp(k,j)
>             enddo
>           enddo
>         enddo
>         do i=1,3
>           do j=1,3
>             do k=1,3
>               pr2(i,j)=pr2(i,j)+eigvec(i,k)*pr1(k,j)
>             enddo
>           enddo
>         enddo
> c  Calculating the total rotational velocity of the molecule
>         do i=1,3
>           do j=1,3
>             vrot(i)=vrot(i)+pr2(i,j)*L(j)
>           enddo
>         enddo
> #ifdef DEBUG
>         write (iout,*) "The inverse of the moment of inertia tensor"
>         do i=1,3
>           write (iout,'(i5,3f10.5)') i,(Pr2(i,j),j=1,3)
>         enddo
>         write (iout,*) "vrot",vrot
> #endif
>       endif
> c   Resetting the velocities
> #ifdef DEBUG
>       write (iout,*) "Velocities before adjustment"
>       do i=nnt,nct-1
>         write (iout,'(2i5,3f10.5,5x,3f10.5)') i,itype(i),
>      &    (vel(j,i),j=1,3),(vel(j,i+nres),j=1,3)
>       enddo
>       write (iout,*) "vrot",vrot
> #endif
>       do i=nnt,nct
>         if (itype(i).eq.ntyp1) cycle
>         ctemp(:) = c(:,i)-cm(:)
>         call vecpr(vrot(1),ctemp,vp)
> !        call vecpr(vrot(1),dc(1,i),vp)
>         do j=1,3
>           vel(j,i)=vel(j,i)-vp(j)
> !          d_t(j,i)=d_t(j,i)-vp(j)
>         enddo
>       enddo
> c      do ichain=2,nchain
> c        i=chain_border(2,ichain-1)
> c        j=chain_border1(1,ichain)
> c        d_t(:,i+1)=d_t(:,i+1)+d_t(:,i)+d_t(:,j)
> c        d_t(:,i)=0.0d0
> c        d_t(:,j)=0.0d0
> c      enddo ! ichain
>       do i=nnt,nct
>         if(itype(i).ne.10 .and. itype(i).ne.ntyp1) then
>           inres=i+nres
>           ctemp(:) = c(:,inres)-cm(:)
>           call vecpr(vrot(1),ctemp,vp)
> !          call vecpr(vrot(1),dc(1,inres),vp)
>           do j=1,3
>             vel(j,inres)=vel(j,inres)-vp(j)
> !            d_t(j,inres)=d_t(j,inres)-vp(j)
>           enddo
>         endif
>       enddo ! i
>       call velocities
> #ifdef DEBUG
>       write (iout,*) "Velocities after adjustment"
>       do i=nnt,nct-1
>         write (iout,'(2i5,3f10.5,5x,3f10.5)') i,itype(i),
>      &    (vel(j,i),j=1,3),(vel(j,i+nres),j=1,3)
>       enddo
> #endif
>       call angmom(cm,L)
> #ifdef DEBUG
>       write(iout,*) "The angular momentum after adjustment:"
>       write(iout,*) (L(j),j=1,3)
> c      call calc_CM(cm)
> c      write (iout,*) "Inertia 1 CM",cm
> #endif
>       return
>       end
> c----------------------------------------------------------------------------
>       subroutine angmom(cm,L)
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CONTROL'
>       include 'COMMON.VAR'
>       include 'COMMON.MD'
>       include 'COMMON.LAGRANGE.5diag'
> #ifdef LANG0
>       include 'COMMON.LANGEVIN.lang0.5diag'
> #else
>       include 'COMMON.LANGEVIN'
> #endif
>       include 'COMMON.CHAIN'
>       include 'COMMON.DERIV'
>       include 'COMMON.GEO'
>       include 'COMMON.LOCAL'
>       include 'COMMON.INTERACT'
>       include 'COMMON.IOUNITS'
>       include 'COMMON.NAMES'
>       double precision L(3),cm(3),pr(3),vp(3),vrot(3),
>      &  pp(3)
>       integer iti,inres,i,j
> c  Calculate the angular momentum
>       !write(*,*)'angmom'
>       !write(*,'(a,3f18.8)')'cm',cm(:)
>       do j=1,3
>          L(j)=0.0d0
>       enddo
> !      do j=1,3
> !         incr(j)=d_t(j,0)
> !      enddo
>       do i=nnt,nct-1
>         if (itype(i).ne.ntyp1 .and. itype(i+1).ne.ntyp1) then
>           do j=1,3
> c            pr(j)=cp(j,i)-cm(j)
>             pr(j)=0.5d0*(c(j,i)+c(j,i+1))-cm(j)
>           enddo
> !         write(*,'(a,i4,3f18.8)')'A',i,c(:,i)
> !         write(*,'(a,i4,3f18.8)')'B',i,pr(:)
> !         write(*,'(a,i4,3f18.8)')'C',i,velp(:,i)
> !          do j=1,3
> !            v(j)=incr(j)+0.5d0*d_t(j,i)
> !          enddo
> c        do j=1,3
> c          incr(j)=incr(j)+d_t(j,i)
> c        enddo
>           call vecpr(pr(1),velp(1,i),vp)
> !         write(*,'(a,i4,3f18.8)')'D',i,vp(:)
> !         write(*,'(a,i4,3f18.8)')'E',i,dc(:,i)
> !         write(*,'(a,i4,3f18.8)')'F',i,d_t(:,i)
>           do j=1,3
>             L(j)=L(j)+mp*vp(j)
>           enddo
>           do j=1,3
>             pr(j)=0.5d0*dc(j,i)
>             pp(j)=0.5d0*d_t(j,i)
>           enddo
>           call vecpr(pr(1),pp(1),vp)
>           do j=1,3
>             L(j)=L(j)+Ip*vp(j)
>           enddo
> !         write(*,'(a,i4,3f18.8)')'L1',i,L(:)
>         endif
> !        do j=1,3
> !          incr(j)=incr(j)+d_t(j,i)
> !        enddo
>       enddo
> !      do j=1,3
> !        incr(j)=d_t(j,0)
> !      enddo
>       do i=nnt,nct
>         iti=iabs(itype(i))
> 
>         if (iti.ne.ntyp1) then
> 
>         inres=i+nres
>         do j=1,3
>           pr(j)=c(j,inres)-cm(j)
>         enddo
> !        if (itype(i).ne.10) then
> !          do j=1,3
> !            v(j)=incr(j)+d_t(j,inres)
> !          enddo
> !        else
> !          do j=1,3
> !            v(j)=incr(j)
> !          enddo
> !        endif
> !       write(*,'(a,i4,3f18.8)')'G',i,c(:,inres)
> !       write(*,'(a,i4,3f18.8)')'H',i,dc(:,inres)
> !       write(*,'(a,i4,3f18.8)')'I',i,pr(:)
> !       write(*,'(a,i4,3f18.8)')'J',i,vel(:,inres)
> !       write(*,'(a,i4,3f18.8)')'K',i,d_t(:,inres)
>         call vecpr(pr(1),vel(1,inres),vp)
> !       write(*,'(a,i4,3f18.8)')'Kp',i,msc(iti),Isc(iti)
> !       write(*,'(a,i4,3f18.8)')'Kk',i,vp(:)
> c          write (iout,*) "i",i," iti",iti," pr",(pr(j),j=1,3),
> c      &     " v",(v(j),j=1,3)," vp",(vp(j),j=1,3)
>         do j=1,3
>           L(j)=L(j)+msc(iti)*vp(j)
>         enddo
> c          write (iout,*) "L",(l(j),j=1,3)
>         if (itype(i).ne.10) then
> !          do j=1,3
> !            v(j)=incr(j)+d_t(j,inres)
> !          enddo
>           call vecpr(dc(1,inres),d_t(1,inres),vp)
> !         write(*,'(a,i4,3f18.8)')'Kk2',i,vp(:)
>           do j=1,3
>             L(j)=L(j)+Isc(iti)*vp(j)
>           enddo
>         endif
> 
> !       write(*,'(a,i4,3f18.8)')'L2',i,L(:)
>         endif
> 
> !        do j=1,3
> !          incr(j)=incr(j)+d_t(j,i)
> !        enddo
>       enddo
>       return
>       end
> c------------------------------------------------------------------------------
>       subroutine vcm_vel(vcm)
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.VAR'
>       include 'COMMON.MD'
>       include 'COMMON.LAGRANGE.5diag'
>       include 'COMMON.CHAIN'
>       include 'COMMON.DERIV'
>       include 'COMMON.GEO'
>       include 'COMMON.LOCAL'
>       include 'COMMON.INTERACT'
>       include 'COMMON.IOUNITS'
>       double precision vcm(3),vv(3),summas,amas
>       integer i,j
>       do j=1,3
>         vcm(j)=0.0d0
> !        vv(j)=d_t(j,0)
>       enddo
>       summas=0.0d0
>       do i=nnt,nct-1
> c         if (i.lt.nct) then
> !        if (itype(i).ne.ntyp1.and. i.lt.nct. and. itype(i+1).ne.ntyp1)
>         if (itype(i).ne.ntyp1 .and. itype(i+1).ne.ntyp1) then
>           summas=summas+mp
>           do j=1,3
> !            vcm(j)=vcm(j)+mp*(vv(j)+0.5d0*d_t(j,i))
>             vcm(j)=vcm(j)+velp(j,i)
>           enddo
>         endif
>       enddo
>       vcm=mp*vcm
>       do i=nnt,nct
> c        write (iout,*) "summas",summas
>         amas=msc(iabs(itype(i)))
>         summas=summas+amas
> c        write (iout,*) "summas",summas
> c        write (iout,*) "i",i," vv",vv," vcm",vcm
>         if (itype(i).ne.10 .and. itype(i).ne.ntyp1) then
>           do j=1,3
> !            vcm(j)=vcm(j)+amas*(vv(j)+d_t(j,i+nres))
>             vcm(j)=vcm(j)+amas*vel(j,i+nres)
>           enddo
>         else
>           do j=1,3
>             vcm(j)=vcm(j)+amas*vel(j,i)
>           enddo
>         endif
>       enddo
> c       write (iout,*) "vcm",(vcm(j),j=1,3)," summas",summas
>       do j=1,3
>         vcm(j)=vcm(j)/summas
>       enddo
>       return
>       end
> c----------------------------------------------------------------------------
>       subroutine calc_CM(cm)
> c   calculating the center of the mass of the protein	
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CONTROL'
>       include 'COMMON.VAR'
>       include 'COMMON.MD'
>       include 'COMMON.LAGRANGE.5diag'
>       include 'COMMON.CHAIN'
>       include 'COMMON.DERIV'
>       include 'COMMON.GEO'
>       include 'COMMON.LOCAL'
>       include 'COMMON.INTERACT'
>       include 'COMMON.IOUNITS'
>       include 'COMMON.NAMES'
>       double precision cm(3)
>       double precision M_SC
>       integer iti,inres,i,j,k,ichain
>       cm=0.0d0
>       do i=nnt,nct-1
>         if (itype(i).eq.ntyp1 .or. itype(i+1).eq.ntyp1) cycle
> c        write (2,'(i5,3f10.5)') i,(cp(j,i),j=1,3)
>         do j=1,3
>           cm(j)=cm(j)+0.5d0*(c(j,i)+c(j,i+1))
>         enddo
>       enddo
>       do j=1,3
>         cm(j)=mp*cm(j)
>       enddo
>       M_SC=0.0d0
>       do i=nnt,nct
>          iti=iabs(itype(i))
>          if (iti.eq.ntyp1) cycle
>          M_SC=M_SC+msc(iti)
>          inres=i+nres
> c         write (2,'(i5,3f10.5)') i,(c(j,inres),j=1,3)
>          do j=1,3
>           cm(j)=cm(j)+msc(iti)*c(j,inres)
>          enddo
>       enddo
> c      write (iout,*) "calc_CM dimenp",dimenp," mp",mp," M_SC",M_SC
>       do j=1,3
>         cm(j)=cm(j)/(M_SC+dimenp*mp)
>       enddo
>       return
>       end
> ! end section with no virtual bond vectors
> #endif
> #else
> c begin old code (no pendadiagonal inertia matrix)
572c1055
< #endif        
---
> #endif
633c1116
<       do i=1,3    
---
>       do i=1,3
649c1132
<         call vecpr(vrot(1),dc(1,i),vp)  
---
>         call vecpr(vrot(1),dc(1,i),vp)
654c1137
<       do i=nnt,nct 
---
>       do i=nnt,nct
675c1158
<       end 
---
>       end
685c1168
<       include 'COMMON.LANGEVIN.lang0'
---
>        include 'COMMON.LANGEVIN.lang0'
849c1332
<         M_SC=0.0d0		   	 	
---
>         M_SC=0.0d0
851,852c1334,1335
<            iti=iabs(itype(i))	   	 
<       	   M_SC=M_SC+msc(iabs(iti))
---
>            iti=iabs(itype(i))
>            M_SC=M_SC+msc(iabs(iti))
855c1338
<             cm(j)=cm(j)+msc(iabs(iti))*c(j,inres)	    
---
>             cm(j)=cm(j)+msc(iabs(iti))*c(j,inres)
MP.F
9c9
<       logical lprn /.true./
---
>       logical lprn /.false./
51d50
<         AVE_COMM = CG_COMM
56d54
<         ave_rank=me
82d79
<         call MPI_Group_free(cg_group,ierr)
83a81
>         call MPI_Group_free(cg_group,ierr)
110,121d107
< #ifdef REPAVE
< C AL communicators for replica averaging
< c AVE_COMM : communicator
< c ave_size : #processes in a respective communicator
< c Note that we just need to revert kolor and key to get the result;
< c all processes of a given rank in FG_COMM form an AVE_COMM communicator
< c and their ranks are the sequential numbers of the FG_COMM groups
< c
<         call MPI_Comm_split(MPI_COMM_WORLD,key,kolor,AVE_COMM,ierr)
<         call MPI_Comm_size(AVE_COMM,navetasks,ierr)
<         call MPI_Comm_rank(AVE_COMM,ave_rank,ierr)
< #endif
125c111
<       write (*,'(8(a,i5))') "Processor",MyRank," out of",nprocs,
---
>       write (*,*) "Processor",MyRank," out of",nprocs,
128,129c114
<      & " rank in FG_COMM1",fg_rank1," size of FG_COMM1",nfgtasks1,
<      & " rank in AVE_COMM",ave_rank
---
>      & " rank in FG_COMM1",fg_rank1," size of FG_COMM1",nfgtasks1
131c116
<       write (*,'(8(a,i5))') "Processor",MyRank," out of",nprocs,
---
>       write (*,*) "Processor",MyRank," out of",nprocs,
133,134c118
<      & " rank in FG_COMM1",fg_rank1," size of FG_COMM1",nfgtasks1,
<      & " rank in AVE_COMM",ave_rank
---
>      & " rank in FG_COMM1",fg_rank1," size of FG_COMM1",nfgtasks1
537,539d520
< 
< 
< 
MP-orig.F
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/MP-orig.F: No such file or directory
MREMD.F
9d8
<       include 'COMMON.NMR'
71d69
<       itime_rep=1
102d99
<           remd_t_bath(k)=remd_t(i)
109d105
<       write (iout,*) "i2rep"
123,129c119
< C
< C AL 5/20/2025 Scatter batch temperatures.
< C
<       call mpi_scatter(remd_t_bath,1,mpi_double_precision,
<      &    t_bath,1,mpi_double_precision,king,CG_COMM,ierr)
<       call rescale_weights(t_bath)
<       write (iout,*) "My t_bath",t_bath
---
> 
169c159
<            if (file_exist) then
---
>            if(file_exist) then
172,178c162
<              if (atimeave_save.gt.0) then
<                write(iout,*),"res atimeave_save",atimeave_save
<                call read1restartave(i_index)
<              else
<                call read1restart(i_index)
<              endif 
<                     
---
>              call read1restart(i_index)
183c167,168
<            if (me.eq.king .or. .not.out1file) then
---
> 
>            if(me.eq.king.or..not.out1file) then
374,375d358
< c      write (iout,*) "After setup_MD_matrices"
< c      call flush(iout)
378,379d360
< c      write (iout,*) "After init_MD"
< c      call flush(iout)
383d363
<        call flush(iout)
450d429
<       itime_rep=itime
454,455d432
<       atimeave=atimeave_save
<       write (iout,*) "MREMD: atimeave",atimeave
458d434
<         itime_rep=itime
498c474
<      &     "Velocities reset to random values, time",totT       
---
>      &     "Velocities reset to random values, time",totT
503a480,491
> #ifndef VIRTUAL
>           do i=1,2*nres
>             do j=1,3
>               vel_old(j,i)=vel(j,i)
>             enddo
>           enddo
>           do i=nnt,nct-1
>             do j=1,3
>               velp_old(j,i)=velp(j,i)
>             enddo
>           enddo
> #endif
510a499,510
> #ifndef VIRTUAL
>           do i=1,2*nres
>             do j=1,3
>               vel_old(j,i)=vel(j,i)
>             enddo
>           enddo
>           do i=nnt,nct-1
>             do j=1,3
>               velp_old(j,i)=velp(j,i)
>             enddo
>           enddo
> #endif
514c514
< cd          write(iout,'(a,f20.2)') "Momenta zeroed out, time",totT     
---
> cd          write(iout,'(a,f20.2)') "Momenta zeroed out, time",totT
519a520,531
> #ifndef VIRTUAL
>           do i=1,2*nres
>             do j=1,3
>               vel_old(j,i)=scalfac*vel(j,i)
>             enddo
>           enddo
>           do i=nnt,nct-1
>             do j=1,3
>               velp_old(j,i)=scalfac*velp(j,i)
>             enddo
>           enddo
> #endif
541,553d552
< !
< ! AL 11/4/2025 Replica averaging restraints
<         if (ave_rep .and. mod(itime_rep,n_exch_restr).eq.0) then
<           call replica_weights(itime,remd_t_bath)
<           if (nfgtasks.gt.1) then
<             call MPI_Bcast(17,1,MPI_INTEGER,king,FG_COMM,IERR)
< #ifdef DEBUG
<             write (iout,*) "Replica average order bcast"
<             call flush(iout)
< #endif
<           endif
<           call replica_average
<         endif
580c579
<             Uconst_cache(:,i)=Uconst_cache(:,i+1)
---
>             Uconst_cache(i)=Uconst_cache(i+1)
608,615c607
<             if (atimeave.gt.0 .or.ave_rep) then
<               Uconst_cache(1,ntwx_cache)=potEcomp(15)
<               Uconst_cache(2,ntwx_cache)=potEcomp(19)
<               Uconst_cache(3,ntwx_cache)=potEcomp(24)
< c              write (iout,*) "atimeave",atimeave," Uconst_cache",
< c     &        Uconst_cache(1:3,ntwx_cache)
<             endif
<             Uconst_cache(0,ntwx_cache)=Uconst
---
>             Uconst_cache(ntwx_cache)=Uconst
676c668,669
<            do i=1,2*nres
---
> #ifdef VIRTUAL
>            do i=0,2*nres-1
679c672
<            do i=1,2*nres
---
>            do i=0,2*nres-1
681a675,682
> #else
>            do i=1,2*nres
>             write (irest2,'(3e15.5)') (vel(j,i),j=1,3)
>            enddo
>            do i=1,2*nres
>             write (irest2,'(3e15.5)') (c(j,i),j=1,3)
>            enddo
> #endif
890,898d890
<                           
<           if(atimeave_save.gt.0) then
<             if (me.eq.king .or. .not. out1file) then
<              write(iout,*),"res atimeave_save",atimeave_save
<             endif
<              call write1rstave(i_index)
<           else
<              call write1rst(i_index)
<           endif
899a892
>           if (restart1file) call write1rst(i_index)
1311d1303
< cAL-----------------------------------
1313,1316d1304
< c Replica average here
< c call replica_average
< c end average
< cAL-----------------------------------
1400,1409c1388,1390
<         if (me.eq.king .or. .not. out1file)
<      &    write(iout,*) 'writing restart at the end of run'
<         if (atimeave_save.gt.0) then
<           if (me.eq.king .or. .not. out1file) then
<             write(iout,*),"res atimeave_save",atimeave_save
<           endif
<           call write1rstave(i_index)
<         else
<           call write1rst(i_index)
<         endif
---
>           if (me.eq.king .or. .not. out1file)
>      &      write(iout,*) 'writing restart at the end of run'
>            call write1rst(i_index)
1441c1422,1627
< c------------------------------------------------------------------------------
---
> 
> c-----------------------------------------------------------------------
>       subroutine write1rst(i_index)
>       implicit none
>       include 'DIMENSIONS'
>       include 'mpif.h'
>       include 'COMMON.CONTROL'
>       include 'COMMON.MD'
> #ifdef FIVEDIAG
>        include 'COMMON.LAGRANGE.5diag'
> #else
>        include 'COMMON.LAGRANGE'
> #endif
>       include 'COMMON.QRESTR'
>       include 'COMMON.IOUNITS'
>       include 'COMMON.REMD'
>       include 'COMMON.SETUP'
>       include 'COMMON.CHAIN'
>       include 'COMMON.SBRIDGE'
>       include 'COMMON.INTERACT'
> 
>       real d_restart1(3,2*maxres*max_cg_procs),r_d(3,0:2*maxres-1),
>      &     d_restart2(3,2*maxres*max_cg_procs)
>       real t5_restart1(5)
>       integer iret,itmp
>       integer*2 i_index(max_rep,max_remd_m,max_set,max_mult_set)
>       common /przechowalnia/ d_restart1,d_restart2
>       integer i,j,il1,il,ixdrf
>       integer ierr
>       common /write1rstcommon/ r_d
> 
>        t5_restart1(1)=totT
>        t5_restart1(2)=EK
>        t5_restart1(3)=potE
>        t5_restart1(4)=t_bath
>        t5_restart1(5)=Uconst
> 
>        call mpi_gather(t5_restart1,5,mpi_real,
>      &      t_restart1,5,mpi_real,king,CG_COMM,ierr)
> 
> 
> #ifdef VIRTUAL
>        do i=0,2*nres-1
>          do j=1,3
>            r_d(j,i)=d_t(j,i)
>          enddo
>        enddo
>        call mpi_gather(r_d,3*2*nres,mpi_real,
>      &           d_restart1,3*2*nres,mpi_real,king,
>      &           CG_COMM,ierr)
> 
> 
>        do i=0,2*nres-1
>          do j=1,3
>            r_d(j,i)=dc(j,i)
>          enddo
>        enddo
> #else
>        do i=1,2*nres
>          do j=1,3
>            r_d(j,i)=vel(j,i)
>          enddo
>        enddo
>        call mpi_gather(r_d,3*2*nres,mpi_real,
>      &           d_restart1,3*2*nres,mpi_real,king,
>      &           CG_COMM,ierr)
> 
> 
>        do i=1,2*nres
>          do j=1,3
>            r_d(j,i)=c(j,i)
>          enddo
>        enddo
> #endif
>        call mpi_gather(r_d,3*2*nres,mpi_real,
>      &           d_restart2,3*2*nres,mpi_real,king,
>      &           CG_COMM,ierr)
> 
>        if(me.eq.king) then
> #ifdef AIX
>          call xdrfopen_(ixdrf,mremd_rst_name, "w", iret)
>          do i=0,nodes-1
>           call xdrfint_(ixdrf, i2rep(i), iret)
>          enddo
>          do i=1,remd_m(1)
>           call xdrfint_(ixdrf, ifirst(i), iret)
>          enddo
>          do il=1,nodes
>               do i=0,nupa(0,il)
>                call xdrfint_(ixdrf, nupa(i,il), iret)
>               enddo
> 
>               do i=0,ndowna(0,il)
>                call xdrfint_(ixdrf, ndowna(i,il), iret)
>               enddo
>          enddo
> 
>          do il=1,nodes
>            do j=1,4
>             call xdrffloat_(ixdrf, t_restart1(j,il), iret)
>            enddo
>          enddo
> 
>          do il=0,nodes-1
>            do i=1,2*nres
>             do j=1,3
>              call xdrffloat_(ixdrf, d_restart1(j,i+2*nres*il), iret)
>             enddo
>            enddo
>          enddo
>          do il=0,nodes-1
>            do i=1,2*nres
>             do j=1,3
>              call xdrffloat_(ixdrf, d_restart2(j,i+2*nres*il), iret)
>             enddo
>            enddo
>          enddo
> 
>          if(usampl) then
>            call xdrfint_(ixdrf, nset, iret)
>            do i=1,nset
>              call xdrfint_(ixdrf,mset(i), iret)
>            enddo
>            do i=0,nodes-1
>              call xdrfint_(ixdrf,i2set(i), iret)
>            enddo
>            do il=1,nset
>              do il1=1,mset(il)
>                do i=1,nrep
>                  do j=1,remd_m(i)
>                    itmp=i_index(i,j,il,il1)
>                    call xdrfint_(ixdrf,itmp, iret)
>                  enddo
>                enddo
>              enddo
>            enddo
> 
>          endif
>          call xdrfclose_(ixdrf, iret)
> #else
>          call xdrfopen(ixdrf,mremd_rst_name, "w", iret)
>          do i=0,nodes-1
>           call xdrfint(ixdrf, i2rep(i), iret)
>          enddo
>          do i=1,remd_m(1)
>           call xdrfint(ixdrf, ifirst(i), iret)
>          enddo
>          do il=1,nodes
>               do i=0,nupa(0,il)
>                call xdrfint(ixdrf, nupa(i,il), iret)
>               enddo
> 
>               do i=0,ndowna(0,il)
>                call xdrfint(ixdrf, ndowna(i,il), iret)
>               enddo
>          enddo
> 
>          do il=1,nodes
>            do j=1,4
>             call xdrffloat(ixdrf, t_restart1(j,il), iret)
>            enddo
>          enddo
> 
>          do il=0,nodes-1
>            do i=1,2*nres
>             do j=1,3
>              call xdrffloat(ixdrf, d_restart1(j,i+2*nres*il), iret)
>             enddo
>            enddo
>          enddo
>          do il=0,nodes-1
>            do i=1,2*nres
>             do j=1,3
>              call xdrffloat(ixdrf, d_restart2(j,i+2*nres*il), iret)
>             enddo
>            enddo
>          enddo
> 
> 
>              if(usampl) then
>               call xdrfint(ixdrf, nset, iret)
>               do i=1,nset
>                 call xdrfint(ixdrf,mset(i), iret)
>               enddo
>               do i=0,nodes-1
>                 call xdrfint(ixdrf,i2set(i), iret)
>               enddo
>               do il=1,nset
>                do il1=1,mset(il)
>                 do i=1,nrep
>                  do j=1,remd_m(i)
>                    itmp=i_index(i,j,il,il1)
>                    call xdrfint(ixdrf,itmp, iret)
>                  enddo
>                 enddo
>                enddo
>               enddo
> 
>              endif
>          call xdrfclose(ixdrf, iret)
> #endif
>        endif
>       return
>       end
> 
> 
1454d1639
<       include 'COMMON.NMR'
1457d1641
<       real r1_Urestart1(3)
1468a1653,1656
> C 12/26/2022 AL: Array to gather dynamic SSbond info
>       integer totnss(0:max_cg_procs-1),dynss_info(2,maxss),
>      &  totdynss_info(2,maxss*max_cg_procs),displ_ss(0:max_cg_procs-1),
>      &  count_ss(0:max_cg_procs-1),shifts
1470c1658
<      &         p_utheta,p_ugamma,p_uscdiff,r_c
---
>      &         p_utheta,p_ugamma,p_uscdiff,r_c,totnss,totdynss_info
1485,1490c1673,1678
<         t5_restart1(1)=totT_cache(ii)
<         t5_restart1(2)=EK_cache(ii)
<         t5_restart1(3)=potE_cache(ii)
<         t5_restart1(4)=t_bath_cache(ii)
<         t5_restart1(5)=Uconst_cache(0,ii)
<         call mpi_gather(t5_restart1,5,mpi_real,
---
>        t5_restart1(1)=totT_cache(ii)
>        t5_restart1(2)=EK_cache(ii)
>        t5_restart1(3)=potE_cache(ii)
>        t5_restart1(4)=t_bath_cache(ii)
>        t5_restart1(5)=Uconst_cache(ii)
>        call mpi_gather(t5_restart1,5,mpi_real,
1492,1498d1679
<         if (atimeave.gt.0 .or.ave_rep) then
<           r1_Urestart1(1)=Uconst_cache(1,ii)
<           r1_Urestart1(2)=Uconst_cache(2,ii)
<           r1_Urestart1(3)=Uconst_cache(3,ii)
<           call mpi_gather(r1_Urestart1,3,mpi_real,
<      &      r_Urestart1,3,mpi_real,king,CG_COMM,ierr)
<         endif
1500c1681
<         call mpi_gather(iset_cache(ii),1,mpi_integer,
---
>        call mpi_gather(iset_cache(ii),1,mpi_integer,
1503,1513c1684,1694
<         do i=1,nfrag
<           r_qfrag(i)=qfrag_cache(i,ii)
<         enddo
<         do i=1,npair
<           r_qpair(i)=qpair_cache(i,ii)
<         enddo
<         do i=1,nfrag_back
<           r_utheta(i)=utheta_cache(i,ii)
<           r_ugamma(i)=ugamma_cache(i,ii)
<           r_uscdiff(i)=uscdiff_cache(i,ii)
<         enddo
---
>           do i=1,nfrag
>            r_qfrag(i)=qfrag_cache(i,ii)
>           enddo
>           do i=1,npair
>            r_qpair(i)=qpair_cache(i,ii)
>           enddo
>           do i=1,nfrag_back
>            r_utheta(i)=utheta_cache(i,ii)
>            r_ugamma(i)=ugamma_cache(i,ii)
>            r_uscdiff(i)=uscdiff_cache(i,ii)
>           enddo
1530c1711,1725
< 
---
> c AL 12/26/2022: Gather ssbond info from nodes for a dynamic SSbond run
>         if (dyn_ss) then
>           do i=1,nss
>             dynss_info(1,i)=idssb(i)
>             dynss_info(2,i)=jdssb(i)
>           enddo
>           call mpi_allgather(nss,1,mpi_integer,totnss,1,mpi_integer,
>      &           CG_COMM,ierr)
>           do i=0,nodes-1
>             count_ss(i)=2*totnss(i)
>           enddo
>           displ_ss(0)=0
>           do i=1,nodes-1
>             displ_ss(i)=displ_ss(i-1)+2*totnss(i-1)
>           enddo
1531a1727,1744
>           do i=0,nodes-1
>             write (iout,*) "node",i," nss",totnss(i+1)," count_ss",
>      &        count_ss(i)," displ_ss",displ_ss(i)
>           enddo
> #endif
>           call mpi_gatherv(dynss_info,2*nss,mpi_integer,totdynss_info,
>      &     count_ss,displ_ss,mpi_integer,king,CG_COMM,ierr)
>         endif
> #ifdef DEBUG
>         if (dyn_ss) then
>           shifts=0
>           do i=0,nodes-1
>             write (iout,*) i," nss",totnss(i)," idssb,jdssb",
>      &       (totdynss_info(1,shifts+j),totdynss_info(2,shifts+j),
>      &        j=1,totnss(i))
>             shifts=shifts+totnss(i)
>           enddo
>         endif
1553a1767
>          shifts=0
1559,1568c1773,1786
<           call xdrfint_(ixdrf, nss, iret)
<           do j=1,nss
<            if (dyn_ss) then
<             call xdrfint(ixdrf, idssb(j)+nres, iret)
<             call xdrfint(ixdrf, jdssb(j)+nres, iret)
<            else
<             call xdrfint_(ixdrf, ihpb(j), iret)
<             call xdrfint_(ixdrf, jhpb(j), iret)
<            endif
<           enddo
---
>           if (dyn_ss) then
>             call xdrfint_(ixdrf, totnss(il-1), iret)
>             do j=totnss(il-1)
>               call xdrfint(ixdrf, totdynss_info(1,j+shifts), iret)
>               call xdrfint(ixdrf, totdynss_info(2,j+shifts), iret)
>             enddo
>             shifts=shifts+totnss(il-1)
>           else
>             call xdrfint_(ixdrf, nss, iret)
>             do j=1,nss
>               call xdrfint_(ixdrf, ihpb(j), iret)
>               call xdrfint_(ixdrf, jhpb(j), iret)
>             enddo
>           endif
1582,1586d1799
<           if (atimeave.gt.0 .or. ave_rep) then
<             call xdrffloat_(ixdrf, real(r_Urestart1(1,il)), iret)
<             call xdrffloat_(ixdrf, real(r_Urestart1(2,il)), iret)
<             call xdrffloat_(ixdrf, real(r_Urestart1(3,il)), iret)
<           endif
1601a1815
>          shifts=0
1607,1616c1821,1837
<           call xdrfint(ixdrf, nss, iret)
<           do j=1,nss
<            if (dyn_ss) then
<             call xdrfint(ixdrf, idssb(j)+nres, iret)
<             call xdrfint(ixdrf, jdssb(j)+nres, iret)
<            else
<             call xdrfint(ixdrf, ihpb(j), iret)
<             call xdrfint(ixdrf, jhpb(j), iret)
<            endif
<           enddo
---
>           if (dyn_ss) then
>             call xdrfint(ixdrf, totnss(il-1), iret)
>             write(2,*)"Writing dyn_ss trajectory",il," nss",totnss(il-1)
>             do j=1,totnss(il-1)
>               write (2,*) "idssb",totdynss_info(1,j+shifts),
>      &         " jdssb",totdynss_info(2,j+shifts)
>               call xdrfint(ixdrf, totdynss_info(1,j+shifts), iret)
>               call xdrfint(ixdrf, totdynss_info(2,j+shifts), iret)
>             enddo
>             shifts=shifts+totnss(il-1)
>           else
>             call xdrfint(ixdrf, nss, iret)
>             do j=1,nss
>               call xdrfint(ixdrf, ihpb(j), iret)
>               call xdrfint(ixdrf, jhpb(j), iret)
>             enddo
>           endif
1630,1634d1850
<           if (atimeave.gt.0 .or. ave_rep) then
<             call xdrffloat(ixdrf, real(r_Urestart1(1,il)), iret)
<             call xdrffloat(ixdrf, real(r_Urestart1(2,il)), iret)
<             call xdrffloat(ixdrf, real(r_Urestart1(3,il)), iret)
<           endif
1663c1879
<             Uconst_cache(:,i)=Uconst_cache(:,ii_write+i)
---
>             Uconst_cache(i)=Uconst_cache(ii_write+i)
1687c1903,2214
< c-------------------------------------------------------------------------------
---
> 
> 
>       subroutine read1restart(i_index)
>       implicit none
>       include 'DIMENSIONS'
>       include 'mpif.h'
>       include 'COMMON.CONTROL'
>       include 'COMMON.MD'
> #ifdef FIVEDIAG
>        include 'COMMON.LAGRANGE.5diag'
> #else
>        include 'COMMON.LAGRANGE'
> #endif
>       include 'COMMON.QRESTR'
>       include 'COMMON.IOUNITS'
>       include 'COMMON.REMD'
>       include 'COMMON.SETUP'
>       include 'COMMON.CHAIN'
>       include 'COMMON.SBRIDGE'
>       include 'COMMON.INTERACT'
>       real d_restart1(3,2*maxres*max_cg_procs),r_d(3,0:2*maxres-1),
>      &                 t5_restart1(5)
>       integer*2 i_index(max_rep,max_remd_m,max_set,max_mult_set)
>       common /przechowalnia/ d_restart1
>       integer i,j,il,il1,ixdrf,iret,itmp
>       integer ierr
>       common /read1restartcommon/ r_d
> c      write (*,*) "Processor",me," called read1restart"
> 
>          if(me.eq.king)then
>               open(irest2,file=mremd_rst_name,status='unknown')
>               read(irest2,*,err=334) i
>               write(iout,*) "Reading old rst in ASCI format"
>               close(irest2)
>                call read1restart_old
>                return
>  334          continue
> #ifdef AIX
>               call xdrfopen_(ixdrf,mremd_rst_name, "r", iret)
> 
>               do i=0,nodes-1
>                call xdrfint_(ixdrf, i2rep(i), iret)
>               enddo
>               do i=1,remd_m(1)
>                call xdrfint_(ixdrf, ifirst(i), iret)
>               enddo
>              do il=1,nodes
>               call xdrfint_(ixdrf, nupa(0,il), iret)
>               do i=1,nupa(0,il)
>                call xdrfint_(ixdrf, nupa(i,il), iret)
>               enddo
> 
>               call xdrfint_(ixdrf, ndowna(0,il), iret)
>               do i=1,ndowna(0,il)
>                call xdrfint_(ixdrf, ndowna(i,il), iret)
>               enddo
>              enddo
>              do il=1,nodes
>                do j=1,4
>                 call xdrffloat_(ixdrf, t_restart1(j,il), iret)
>                enddo
>              enddo
> #else
>               call xdrfopen(ixdrf,mremd_rst_name, "r", iret)
> 
>               do i=0,nodes-1
>                call xdrfint(ixdrf, i2rep(i), iret)
>               enddo
>               do i=1,remd_m(1)
>                call xdrfint(ixdrf, ifirst(i), iret)
>               enddo
>              do il=1,nodes
>               call xdrfint(ixdrf, nupa(0,il), iret)
>               do i=1,nupa(0,il)
>                call xdrfint(ixdrf, nupa(i,il), iret)
>               enddo
> 
>               call xdrfint(ixdrf, ndowna(0,il), iret)
>               do i=1,ndowna(0,il)
>                call xdrfint(ixdrf, ndowna(i,il), iret)
>               enddo
>              enddo
>              do il=1,nodes
>                do j=1,4
>                 call xdrffloat(ixdrf, t_restart1(j,il), iret)
>                enddo
>              enddo
> #endif
>          endif
>          call mpi_scatter(t_restart1,5,mpi_real,
>      &           t5_restart1,5,mpi_real,king,CG_COMM,ierr)
>          totT=t5_restart1(1)
>          EK=t5_restart1(2)
>          potE=t5_restart1(3)
>          t_bath=t5_restart1(4)
> 
>          if(me.eq.king)then
>               do il=0,nodes-1
>                do i=1,2*nres
> c                read(irest2,'(3e15.5)')
> c     &                (d_restart1(j,i+2*nres*il),j=1,3)
>             do j=1,3
> #ifdef AIX
>              call xdrffloat_(ixdrf, d_restart1(j,i+2*nres*il), iret)
> #else
>              call xdrffloat(ixdrf, d_restart1(j,i+2*nres*il), iret)
> #endif
>             enddo
>                enddo
>               enddo
>          endif
>          call mpi_scatter(d_restart1,3*2*nres,mpi_real,
>      &           r_d,3*2*nres,mpi_real,king,CG_COMM,ierr)
> 
> #ifdef VIRTUAL
>          do i=0,2*nres-1
>            do j=1,3
>             d_t(j,i)=r_d(j,i)
>            enddo
>          enddo
> #else
>          do i=1,2*nres
>            do j=1,3
>             vel(j,i)=r_d(j,i)
>            enddo
>          enddo
>          call velocities
> #endif
>          if(me.eq.king)then
>               do il=0,nodes-1
>                do i=1,2*nres
> c                read(irest2,'(3e15.5)')
> c     &                (d_restart1(j,i+2*nres*il),j=1,3)
>             do j=1,3
> #ifdef AIX
>              call xdrffloat_(ixdrf, d_restart1(j,i+2*nres*il), iret)
> #else
>              call xdrffloat(ixdrf, d_restart1(j,i+2*nres*il), iret)
> #endif
>             enddo
>                enddo
>               enddo
>          endif
>          call mpi_scatter(d_restart1,3*2*nres,mpi_real,
>      &           r_d,3*2*nres,mpi_real,king,CG_COMM,ierr)
> #ifdef VIRTUAL
>          do i=0,2*nres-1
>            do j=1,3
>             dc(j,i)=r_d(j,i)
>            enddo
>          enddo
> #else
>          do i=1,2*nres
>            do j=1,3
>             c(j,i)=r_d(j,i)
>            enddo
>          enddo
> #endif
> 
>            if(usampl) then
> #ifdef AIX
>              if(me.eq.king)then
>               call xdrfint_(ixdrf, nset, iret)
>               do i=1,nset
>                 call xdrfint_(ixdrf,mset(i), iret)
>               enddo
>               do i=0,nodes-1
>                 call xdrfint_(ixdrf,i2set(i), iret)
>               enddo
>               do il=1,nset
>                do il1=1,mset(il)
>                 do i=1,nrep
>                  do j=1,remd_m(i)
>                    call xdrfint_(ixdrf,itmp, iret)
>                    i_index(i,j,il,il1)=itmp
>                  enddo
>                 enddo
>                enddo
>               enddo
>              endif
> #else
>              if(me.eq.king)then
>               call xdrfint(ixdrf, nset, iret)
>               do i=1,nset
>                 call xdrfint(ixdrf,mset(i), iret)
>               enddo
>               do i=0,nodes-1
>                 call xdrfint(ixdrf,i2set(i), iret)
>               enddo
>               do il=1,nset
>                do il1=1,mset(il)
>                 do i=1,nrep
>                  do j=1,remd_m(i)
>                    call xdrfint(ixdrf,itmp, iret)
>                    i_index(i,j,il,il1)=itmp
>                  enddo
>                 enddo
>                enddo
>               enddo
>              endif
> #endif
> Corrected AL 8/19/2014: each processor needs whole iset array not only its
> c own element
> c              call mpi_scatter(i2set,1,mpi_integer,
> c     &           iset,1,mpi_integer,king,
> c     &           CG_COMM,ierr)
>               call mpi_bcast(i2set(0),nodes,mpi_integer,king,
>      &         CG_COMM,ierr)
>               iset=i2set(me)
> 
>            endif
> 
> 
>         if(me.eq.king) close(irest2)
>         return
>         end
> 
>       subroutine read1restart_old
>       implicit none
>       include 'DIMENSIONS'
>       include 'mpif.h'
>       include 'COMMON.MD'
> #ifdef FIVEDIAG
>        include 'COMMON.LAGRANGE.5diag'
> #else
>        include 'COMMON.LAGRANGE'
> #endif
>       include 'COMMON.IOUNITS'
>       include 'COMMON.REMD'
>       include 'COMMON.SETUP'
>       include 'COMMON.CHAIN'
>       include 'COMMON.SBRIDGE'
>       include 'COMMON.INTERACT'
>       real d_restart1(3,2*maxres*max_cg_procs),r_d(3,0:2*maxres-1),
>      &                 t5_restart1(5)
>       common /przechowalnia/ d_restart1
>       integer i,j,il,itmp
>       integer ierr
>       common /read1restartoldcommon/ r_d
> 
>          if(me.eq.king)then
>              open(irest2,file=mremd_rst_name,status='unknown')
>              read (irest2,*) (i2rep(i),i=0,nodes-1)
>              read (irest2,*) (ifirst(i),i=1,remd_m(1))
>              do il=1,nodes
>               read (irest2,*) nupa(0,il),(nupa(i,il),i=1,nupa(0,il))
>               read (irest2,*) ndowna(0,il),
>      &                    (ndowna(i,il),i=1,ndowna(0,il))
>              enddo
>              do il=1,nodes
>                read(irest2,*) (t_restart1(j,il),j=1,4)
>              enddo
>          endif
>          call mpi_scatter(t_restart1,5,mpi_real,
>      &           t5_restart1,5,mpi_real,king,CG_COMM,ierr)
>          totT=t5_restart1(1)
>          EK=t5_restart1(2)
>          potE=t5_restart1(3)
>          t_bath=t5_restart1(4)
> 
>          if(me.eq.king)then
>               do il=0,nodes-1
>                do i=1,2*nres
>                 read(irest2,'(3e15.5)')
>      &                (d_restart1(j,i+2*nres*il),j=1,3)
>                enddo
>               enddo
>          endif
>          call mpi_scatter(d_restart1,3*2*nres,mpi_real,
>      &           r_d,3*2*nres,mpi_real,king,CG_COMM,ierr)
> 
> #ifdef VIRTUAL
>          do i=0,2*nres-1
>            do j=1,3
>             d_t(j,i)=r_d(j,i)
>            enddo
>          enddo
> #else
>          do i=1,2*nres
>            do j=1,3
>             vel(j,i)=r_d(j,i)
>            enddo
>          enddo
>          call velocities
> #endif
>          if(me.eq.king)then
>               do il=0,nodes-1
>                do i=1,2*nres
>                 read(irest2,'(3e15.5)')
>      &                (d_restart1(j,i+2*nres*il),j=1,3)
>                enddo
>               enddo
>          endif
>          call mpi_scatter(d_restart1,3*2*nres,mpi_real,
>      &           r_d,3*2*nres,mpi_real,king,CG_COMM,ierr)
> #ifdef VIRTUAL
>          do i=0,2*nres-1
>            do j=1,3
>             dc(j,i)=r_d(j,i)
>            enddo
>          enddo
> #else
>          do i=1,2*nres
>            do j=1,3
>             c(j,i)=r_d(j,i)
>            enddo
>          enddo
> #endif
>         if(me.eq.king) close(irest2)
>         return
>         end
> c------------------------------------------
muca_md.F
mygauss.f
mytschebyshev.f
newconf.F
1283c1283
<         call chainbuild
---
>         call chainbuild_extconf
2167c2167
<       call chainbuild
---
>       call chainbuild_extconf
2264c2264
<         call chainbuild
---
>         call chainbuild_extconf
2330c2330
<         call chainbuild
---
>         call chainbuild_extconf
nmrave_info.F
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/nmrave_info.F: No such file or directory
nmr_hpos.F
3c3
< C 
---
> C
5c5
< C E. Lubecka, February 2020              
---
> C E. Lubecka, February 2020
23c23
< C 
---
> C
56a57,59
> #ifdef NMRLOG
>       write(*,*)'cpu nmr_hpos'
> #endif
156c159
< #else 
---
> #else
165c168
< c      enddo 
---
> c      enddo
189a193,231
> #ifdef NMRLOG
>           write(*,'(a,i4,i4,3f18.12)')'NMRcb11',innt,0,
>      &      dhpos_dc(:,1,1,innt,0)
>           write(*,'(a,i4,i4,3f18.12)')'NMRcb21',innt,0,
>      &      dhpos_dc(:,2,1,innt,0)
>           write(*,'(a,i4,i4,3f18.12)')'NMRcb31',innt,0,
>      &      dhpos_dc(:,3,1,innt,0)
>           write(*,'(a,i4,i4,3f18.12)')'NMRcb12',innt,0,
>      &      dhpos_dc(:,1,2,innt,0)
>           write(*,'(a,i4,i4,3f18.12)')'NMRcb22',innt,0,
>      &      dhpos_dc(:,2,2,innt,0)
>           write(*,'(a,i4,i4,3f18.12)')'NMRcb32',innt,0,
>      &      dhpos_dc(:,3,2,innt,0)
>           write(*,'(a,i4,i4,3f18.12)')'NMRcb13',innt,0,
>      &      dhpos_dc(:,1,3,innt,0)
>           write(*,'(a,i4,i4,3f18.12)')'NMRcb23',innt,0,
>      &      dhpos_dc(:,2,3,innt,0)
>           write(*,'(a,i4,i4,3f18.12)')'NMRcb33',innt,0,
>      &      dhpos_dc(:,3,3,innt,0)
> 
>           write(*,'(a,i4,i4,3f18.12)')'NMRce11',inct-1,0,
>      &      dhpos_dc(:,1,1,inct-1,0)
>           write(*,'(a,i4,i4,3f18.12)')'NMRce21',inct-1,0,
>      &      dhpos_dc(:,2,1,inct-1,0)
>           write(*,'(a,i4,i4,3f18.12)')'NMRce31',inct-1,0,
>      &      dhpos_dc(:,3,1,inct-1,0)
>           write(*,'(a,i4,i4,3f18.12)')'NMRce12',inct-1,0,
>      &      dhpos_dc(:,1,2,inct-1,0)
>           write(*,'(a,i4,i4,3f18.12)')'NMRce22',inct-1,0,
>      &      dhpos_dc(:,2,2,inct-1,0)
>           write(*,'(a,i4,i4,3f18.12)')'NMRce32',inct-1,0,
>      &      dhpos_dc(:,3,2,inct-1,0)
>           write(*,'(a,i4,i4,3f18.12)')'NMRce13',inct-1,0,
>      &      dhpos_dc(:,1,3,inct-1,0)
>           write(*,'(a,i4,i4,3f18.12)')'NMRce23',inct-1,0,
>      &      dhpos_dc(:,2,3,inct-1,0)
>           write(*,'(a,i4,i4,3f18.12)')'NMRce33',inct-1,0,
>      &      dhpos_dc(:,3,3,inct-1,0)
> #endif
190a233,241
> 
> #ifndef DEBUG
> !$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(
> !$OMP&   ntypProt,j,k,l,Calpha,Salpha,ha_tmp,dCalpha,dSalpha,
> !$OMP&   x_prime,y_prime,z_prime,uxder,uyder,uzder,
> !$OMP&   bet,Cbeta,Sbeta,dCbeta,dSbeta,sinb,dsinb,cosb,dcosb,aux,
> !$OMP&   qb_tmp,dhpos,pom1,pom2,pom3
> !$OMP& )
> #endif
200a252,274
> #ifdef NMRLOG
>           write(*,'(a,i4,3f18.12)')'NMR0a',i,x_prime(:)
>           write(*,'(a,i4,3f18.12)')'NMR0b',i,y_prime(:)
>           write(*,'(a,i4,3f18.12)')'NMR0c',i,z_prime(:)
>           write(*,'(a,i4,3f18.12)')'NMR0x11',i,uxder(:,1,1)
>           write(*,'(a,i4,3f18.12)')'NMR0x21',i,uxder(:,2,1)
>           write(*,'(a,i4,3f18.12)')'NMR0x31',i,uxder(:,3,1)
>           write(*,'(a,i4,3f18.12)')'NMR0x12',i,uxder(:,1,2)
>           write(*,'(a,i4,3f18.12)')'NMR0x22',i,uxder(:,2,2)
>           write(*,'(a,i4,3f18.12)')'NMR0x32',i,uxder(:,3,2)
>           write(*,'(a,i4,3f18.12)')'NMR0y11',i,uyder(:,1,1)
>           write(*,'(a,i4,3f18.12)')'NMR0y21',i,uyder(:,2,1)
>           write(*,'(a,i4,3f18.12)')'NMR0y31',i,uyder(:,3,1)
>           write(*,'(a,i4,3f18.12)')'NMR0y12',i,uyder(:,1,2)
>           write(*,'(a,i4,3f18.12)')'NMR0y22',i,uyder(:,2,2)
>           write(*,'(a,i4,3f18.12)')'NMR0y32',i,uyder(:,3,2)
>           write(*,'(a,i4,3f18.12)')'NMR0z11',i,uzder(:,1,1)
>           write(*,'(a,i4,3f18.12)')'NMR0z21',i,uzder(:,2,1)
>           write(*,'(a,i4,3f18.12)')'NMR0z31',i,uzder(:,3,1)
>           write(*,'(a,i4,3f18.12)')'NMR0z12',i,uzder(:,1,2)
>           write(*,'(a,i4,3f18.12)')'NMR0z22',i,uzder(:,2,2)
>           write(*,'(a,i4,3f18.12)')'NMR0z32',i,uzder(:,3,2)
> #endif
228c302
< #ifdef DEBUG
---
> #if defined(DEBUG) && defined (VIRTUAL)
290a365,368
> #ifdef NMRLOG
>           write(*,'(a,i4,i3,4f18.12)')'NMR1',i,itype(i),HAalphaGly,
>      &      dcos(HAalphaGly*deg2rad),dsin(HAalphaGly*deg2rad),HAbLenGly
> #endif
302a381,385
> #ifdef NMRLOG
>           write(*,'(a,i4,i3,4f18.12)')'NMR1',i,itype(i),HAalphaPro,
>      &      dcos(HAalphaPro*deg2rad),HAbLenPro,costtab(i+1)
>           write(*,'(a,i4,i3,3f18.12)')'NMR2',i,itype(i),haPro_parm(2:4)
> #endif
312a396,400
> #ifdef NMRLOG
>           write(*,'(a,i4,i3,4f18.12)')'NMR1',i,itype(i),HAalphaC,
>      &      dcos(HAalphaC*deg2rad),HAbLen,costtab(i+1)
>           write(*,'(a,i4,i3,3f18.12)')'NMR2',i,itype(i),ha_parm(2:4)
> #endif
314,318c402,412
< c H alpha positions        
<           do j=1,3
<             dhpos(j)=x_prime(j)*ha_tmp(1)
<      &        +y_prime(j)*ha_tmp(2)+z_prime(j)*ha_tmp(3)
<           enddo
---
> #ifdef NMRLOG
>           write(*,'(a,i4,i3,7f16.8)')'NMR3',i,itype(i),
>      &      Calpha,Salpha,ha_tmp(:),dCalpha,dSalpha
> #endif
> c H alpha positions
> !         do j=1,3
> !           dhpos(j)=x_prime(j)*ha_tmp(1)
> !    &        +y_prime(j)*ha_tmp(2)+z_prime(j)*ha_tmp(3)
> !         enddo
>           dhpos(:)=x_prime(:)*ha_tmp(1)
>      &      +y_prime(:)*ha_tmp(2)+z_prime(:)*ha_tmp(3)
328,329c422,423
<           do j=1,3
<             hpos(j,i,ntypProt)=c(j,i)+dhpos(j)
---
> !         do j=1,3
> !           hpos(j,i,ntypProt)=c(j,i)+dhpos(j)
331c425,430
<           enddo
---
> !         enddo
>           hpos(:,i,ntypProt)=c(:,i)+dhpos(:)
> #ifdef NMRLOG
>           write(*,'(a,i4,i4,3f18.12)')'NMR4',i,ntypProt,
>      &      hpos(:,i,ntypProt)
> #endif
335,339c434,441
<               do l=1,3
<                 dhpos_dc(l,k,j,i,ntypProt)=ha_tmp(1)*uxder(l,k,j)+
<      &          ha_tmp(2)*uyder(l,k,j)+ha_tmp(3)*uzder(l,k,j)+
<      &          (x_prime(l)*dCalpha-z_prime(l)*dSalpha)*dt_dCitab(k,j,i)
<               enddo
---
> !             do l=1,3
> !               dhpos_dc(l,k,j,i,ntypProt)=ha_tmp(1)*uxder(l,k,j)+
> !    &          ha_tmp(2)*uyder(l,k,j)+ha_tmp(3)*uzder(l,k,j)+
> !    &          (x_prime(l)*dCalpha-z_prime(l)*dSalpha)*dt_dCitab(k,j,i)
> !             enddo
>               dhpos_dc(:,k,j,i,ntypProt)=ha_tmp(1)*uxder(:,k,j)+
>      &          ha_tmp(2)*uyder(:,k,j)+ha_tmp(3)*uzder(:,k,j)+
>      &          (x_prime(:)*dCalpha-z_prime(:)*dSalpha)*dt_dCitab(k,j,i)
341a444,457
> #ifdef NMRLOG
>           write(*,'(a,i4,i4,3f18.12)')'NMR511',i,ntypProt,
>      &      dhpos_dc(:,1,1,i,ntypProt)
>           write(*,'(a,i4,i4,3f18.12)')'NMR521',i,ntypProt,
>      &      dhpos_dc(:,2,1,i,ntypProt)
>           write(*,'(a,i4,i4,3f18.12)')'NMR531',i,ntypProt,
>      &      dhpos_dc(:,3,1,i,ntypProt)
>           write(*,'(a,i4,i4,3f18.12)')'NMR512',i,ntypProt,
>      &      dhpos_dc(:,1,2,i,ntypProt)
>           write(*,'(a,i4,i4,3f18.12)')'NMR522',i,ntypProt,
>      &      dhpos_dc(:,2,2,i,ntypProt)
>           write(*,'(a,i4,i4,3f18.12)')'NMR532',i,ntypProt,
>      &      dhpos_dc(:,3,2,i,ntypProt)
> #endif
384a501,504
> #ifdef NMRLOG
>               write(*,'(a,i4,i4,7f16.8)')'NMR6a',i,ntypProt,
>      &          sinb,dsinb,cosb,dcosb,aux,HBbetaC,HBbLen
> #endif
388a509,512
> #ifdef NMRLOG
>           write(*,'(a,i4,i4,3f18.12)')'NMR6b',i,ntypProt,
>      &      qb_tmp(:)
> #endif
392,398c516,530
<             do j=1,3
<               dhpos(j)=x_prime(j)*qb_tmp(1)
<      &          +y_prime(j)*qb_tmp(2)+z_prime(j)*qb_tmp(3)
<             enddo
<             do j=1,3
<               hpos(j,i,ntypProt)=c(j,i)+dHpos(j)
<             enddo
---
> !           do j=1,3
> !             dhpos(j)=x_prime(j)*qb_tmp(1)
> !    &          +y_prime(j)*qb_tmp(2)+z_prime(j)*qb_tmp(3)
> !           enddo
>             dhpos(:)=x_prime(:)*qb_tmp(1)
>      &        +y_prime(:)*qb_tmp(2)+z_prime(:)*qb_tmp(3)
> !           do j=1,3
> !             hpos(j,i,ntypProt)=c(j,i)+dHpos(j)
> !           enddo
>             hpos(:,i,ntypProt)=c(:,i)+dHpos(:)
> #ifdef NMRLOG
>           write(*,'(a,i4,i4,3f18.12)')'NMR7',i,ntypProt,
>      &      hpos(:,i,ntypProt)
> #endif
> 
404,409c536,545
<                 do l=1,3
<                   dhpos_dc(l,k,j,i,ntypProt)=qb_tmp(1)*uxder(l,k,j)+
<      &              qb_tmp(2)*uyder(l,k,j)+qb_tmp(3)*uzder(l,k,j)+
<      &             (x_prime(l)*pom1+y_prime(l)*pom2+z_prime(l)*pom3)
<      &             *dt_dCitab(k,j,i)
<                 enddo
---
> !               do l=1,3
> !                 dhpos_dc(l,k,j,i,ntypProt)=qb_tmp(1)*uxder(l,k,j)+
> !    &              qb_tmp(2)*uyder(l,k,j)+qb_tmp(3)*uzder(l,k,j)+
> !    &             (x_prime(l)*pom1+y_prime(l)*pom2+z_prime(l)*pom3)
> !    &             *dt_dCitab(k,j,i)
> !               enddo
>                 dhpos_dc(:,k,j,i,ntypProt)=qb_tmp(1)*uxder(:,k,j)+
>      &            qb_tmp(2)*uyder(:,k,j)+qb_tmp(3)*uzder(:,k,j)+
>      &           (x_prime(:)*pom1+y_prime(:)*pom2+z_prime(:)*pom3)
>      &           *dt_dCitab(k,j,i)
411a548,561
> #ifdef NMRLOG
>           write(*,'(a,i4,i4,3f18.12)')'NMR811',i,ntypProt,
>      &      dhpos_dc(:,1,1,i,ntypProt)
>           write(*,'(a,i4,i4,3f18.12)')'NMR821',i,ntypProt,
>      &      dhpos_dc(:,2,1,i,ntypProt)
>           write(*,'(a,i4,i4,3f18.12)')'NMR831',i,ntypProt,
>      &      dhpos_dc(:,3,1,i,ntypProt)
>           write(*,'(a,i4,i4,3f18.12)')'NMR812',i,ntypProt,
>      &      dhpos_dc(:,1,2,i,ntypProt)
>           write(*,'(a,i4,i4,3f18.12)')'NMR822',i,ntypProt,
>      &      dhpos_dc(:,2,2,i,ntypProt)
>           write(*,'(a,i4,i4,3f18.12)')'NMR832',i,ntypProt,
>      &      dhpos_dc(:,3,2,i,ntypProt)
> #endif
442a593,597
> !$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(
> !$OMP&   k,l,ntypProt,hn1,hn2,fac,hn1theta,hn2theta,cosphi,sinphi,
> !$OMP&   y2,z2,Psum,dlPsum,PHN,pom1,pom2,
> !$OMP&   dPHNtheta1,dPHNtheta2,dPHNgamma,dCder
> !$OMP& )
459a615,618
> #ifdef NMRLOG
>           write(*,'(a,i4,i4,5f16.8)')'NMRa',i,ntypProt,
>      &      hn1(2:3),hn2(2:3),fac
> #endif
471a631,634
> #ifdef NMRLOG
>           write(*,'(a,i4,i4,5f16.8)')'NMRb',i,ntypProt,
>      &      hn1theta(2:3),hn2theta(2:3),fac
> #endif
485a649,652
> #ifdef NMRLOG
>           write(*,'(a,i4,i4,4f16.8)')'NMRc',i,ntypProt,
>      &      hn1(2:3),hn2(2:3)
> #endif
490,495c657,658
< #ifdef DEBUG
<           write (iout,*) "theta1",theta(i+1)*rad2deg,
<      &     " theta2",theta(i+2)*rad2deg," phi",phi(i+2)*rad2deg
<           write (iout,*) "hn1",hn1," hn2",hn2
<           write (iout,*) "y2",y2," z2",z2 
< #endif
---
> c          write (iout,*) "hn1",hn1," hn2",hn2
> c          write (iout,*) "y2",y2," z2",z2
498,502c661,665
< c          dlPsum=dsqrt(Psum(2)**2+Psum(3)**2)
< c AL 8/18/2023: Added a small number to the square of dipole-moment norm to ensumre stability
<           dlPsum=dsqrt(Psum(2)**2+Psum(3)**2+1.0d-3)
< #ifdef DEBUG
<           write (iout,*) "Psum",Psum(2),Psum(3),"dlPsum=",dlPsum
---
>           dlPsum=dsqrt(Psum(2)**2+Psum(3)**2)
> c          write (iout,*) "Psum",Psum(2),Psum(3),"dlPsum=",dlPsum
> #ifdef NMRLOG
>           write(*,'(a,i4,i4,7f16.8)')'NMRd',i,ntypProt,
>      &      cosphi,sinphi,y2,z2,Psum(2:3),dlPsum
503a667
> 
508,510d671
< #ifdef DEBUG
<           write (iout,*) "PHN",PHN
< #endif
517,520c678,680
<           pom1=-(Psum(2)*hn2theta(2)+Psum(3)*hn2theta(3))/dlPsum**2
< #ifdef DEBUG
<           write (iout,*) "pom1",pom1," pom2",pom2
<           write (iout,*) "hn2theta",hn2theta
---
> #ifdef NMRLOG
>           write(*,'(a,i4,i4,7f16.8)')'NMRe',i,ntypProt,
>      &      PHN(1:3),pom1,pom2,dPHNtheta1(2:3)
521a682
>           pom1=-(Psum(2)*hn2theta(2)+Psum(3)*hn2theta(3))/dlPsum**2
531a693,696
> #ifdef NMRLOG
>           write(*,'(a,i4,i4,6f16.8)')'NMRf',i,ntypProt,
>      &      dPHNgamma(2:3),pom1,pom2,dPHNtheta2(2:3)
> #endif
537a703,706
> #ifdef NMRLOG
>           write(*,'(a,i4,i4,3f18.12)')'NMRg',i,ntypProt,
>      &      hpos(:,i,ntypProt)
> #endif
538a708,714
>           write (iout,*) "ux",i,dc_norm(:,i)
>           write (iout,*) "uy",i,uy(:,2,i)
>           write (iout,*) "uz",i,uz(:,i)
>           write (iout,*) i," cosphi",cosphi," sinphi",sinphi,
>      &     " costtab",costtab(i+1),costtab(i+2),
>      &     " sinttab",sinttab(i+1),sinttab(i+2)
>           write(iout,'(4hPHN i5,3f10.5)') i,PHN
570,581c746,767
< #ifdef DEBUG
<           write (iout,*) "i",i," dhpos_dc1",
<      &      ((dhpos_dc(l,k,1,i,ntypProt),l=1,3),k=1,3)
<           write (iout,*) "i",i," dhpos_dc2",
<      &      ((dhpos_dc(l,k,2,i,ntypProt),l=1,3),k=1,3)
<           write (iout,*) "i",i," dhpos_dc3",
<      &      ((dhpos_dc(l,k,3,i,ntypProt),l=1,3),k=1,3)
<           write (iout,*) "i",i," dhpos_gamma",
<      &      (dhpos_gamma(k,i,ntypProt),k=1,3)
<           write (iout,*) "dPHNtheta1",dPHNtheta1
<           write (iout,*) "dPHNtheta2",dPHNtheta2
<           write (iout,*) "dPHNgamma",dPHNgamma
---
> #ifdef NMRLOG
>           write(*,'(a,i4,i4,3f18.12)')'NMRh11',i,ntypProt,
>      &      dhpos_dc(:,1,1,i,ntypProt)
>           write(*,'(a,i4,i4,3f18.12)')'NMRh21',i,ntypProt,
>      &      dhpos_dc(:,2,1,i,ntypProt)
>           write(*,'(a,i4,i4,3f18.12)')'NMRh31',i,ntypProt,
>      &      dhpos_dc(:,3,1,i,ntypProt)
>           write(*,'(a,i4,i4,3f18.12)')'NMRh12',i,ntypProt,
>      &      dhpos_dc(:,1,2,i,ntypProt)
>           write(*,'(a,i4,i4,3f18.12)')'NMRh22',i,ntypProt,
>      &      dhpos_dc(:,2,2,i,ntypProt)
>           write(*,'(a,i4,i4,3f18.12)')'NMRh32',i,ntypProt,
>      &      dhpos_dc(:,3,2,i,ntypProt)
>           write(*,'(a,i4,i4,3f18.12)')'NMRh13',i,ntypProt,
>      &      dhpos_dc(:,1,3,i,ntypProt)
>           write(*,'(a,i4,i4,3f18.12)')'NMRh23',i,ntypProt,
>      &      dhpos_dc(:,2,3,i,ntypProt)
>           write(*,'(a,i4,i4,3f18.12)')'NMRh33',i,ntypProt,
>      &      dhpos_dc(:,3,3,i,ntypProt)
> 
>           write(*,'(a,i4,i4,3f18.12)')'NMRi',i,ntypProt,
>      &      dhpos_gamma(:,i,ntypProt)
583d768
< #undef DEBUG
585a771
> 
593c779,781
< ! The positions and derivatives of other protons. 
---
> 
> ! The positions and derivatives of other protons.
> !$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(iti,j,k)
597,600c785,790
<         do j=3,nproton(iti)
<           do k=1,3
<             hpos(k,i,j)=C(k,i)+protpos(j,iti)*dC(k,i+nres)
<           enddo
---
>         k=nproton(iti)
>         do j=3,k
>           hpos(:,i,j)=C(:,i)+protpos(j,iti)*dC(:,i+nres)
> #ifdef NMRLOG
>           write(*,'(a,i4,i4,3f18.12)')'NMRp',i,j,hpos(:,i,j)
> #endif
603,604c793,819
<       if (lprn) then
<       write(iout,'(a)') "REMARK reconstructed proton positions"
---
> 
> !     if (lprn) then
> !     write(iout,'(a)') "REMARK reconstructed proton positions"
> !     iatom=0
> !     do i=nnt+1,nct
> !       iatom=iatom+1
> !       write (iout,'(4hATOM,i7,2x,a3,1x,a3,2x,i4,4x,3f8.3)')
> !    &        iatom,"CA",restyp(itype(i)),i-nnt+1,(c(j,i),j=1,3)
> !       iatom=iatom+1
> !       write (iout,'(4hATOM,i7,2x,a3,1x,a3,2x,i4,4x,3f8.3)')
> !    &      iatom,"H",restyp(itype(i)),i-nnt+1,(hpos(j,i-1,0),j=1,3)
> !       iatom=iatom+1
> !       write (iout,'(4hATOM,i7,2x,a3,1x,a3,2x,i4,4x,3f8.3)')
> !    &      iatom,"HA",restyp(itype(i)),i-nnt+1,(hpos(j,i,1),j=1,3)
> !       iatom=iatom+1
> !       write (iout,'(4hATOM,i7,2x,a3,1x,a3,2x,i4,4x,3f8.3)')
> !    &      iatom,"HB",restyp(itype(i)),i-nnt+1,(hpos(j,i,2),j=1,3)
> !     enddo
> !     endif
> #ifdef EDISLOG
>       do i=nnt,nct
>         iti=itype(i)
>         do j=0,nproton(iti)
>           write(*,'(a,2i4,3f18.10)')'HPOS',i,j,hpos(:,i,j)
>         enddo
>       enddo
>       write(*,'(a)') "REMARK reconstructed proton positions"
608c823
<         write (iout,'(4hATOM,i7,2x,a3,1x,a3,2x,i4,4x,3f8.3)')
---
>         write (*,'(4hATOM,i7,2x,a3,1x,a3,2x,i4,4x,3f8.3)')
611c826
<         write (iout,'(4hATOM,i7,2x,a3,1x,a3,2x,i4,4x,3f8.3)')
---
>         write (*,'(4hATOM,i7,2x,a3,1x,a3,2x,i4,4x,3f8.3)')
614c829
<         write (iout,'(4hATOM,i7,2x,a3,1x,a3,2x,i4,4x,3f8.3)')
---
>         write (*,'(4hATOM,i7,2x,a3,1x,a3,2x,i4,4x,3f8.3)')
617c832
<         write (iout,'(4hATOM,i7,2x,a3,1x,a3,2x,i4,4x,3f8.3)')
---
>         write (*,'(4hATOM,i7,2x,a3,1x,a3,2x,i4,4x,3f8.3)')
620c835
<       endif
---
> #endif
622a838
> 
639,641d854
<       double precision d0 /2.5d0/,delta /1.0d0/
< c      double precision d0 /3.0d0/,delta /4.0d0/
<       double precision d_red,gd_red
667,678d879
<       call red_dist(dd,d0,delta,d_red,gd_red)
< c      write (iout,*) i1,i2,"dd",dd," d_red",d_red," gd_red",gd_red
<       if (dd.le.d0) then
<         rvec = 0.0d0
<       else if (dd.lt.d0+delta) then
<         rvec = gd_red*rvec
<       endif
< c      rvec=rvec/dd
<       do j=1,3
<         rvec(j)=rvec(j)/dd
<       enddo
<       dd = d_red
717c918
<       enddo     
---
>       enddo
819,837d1019
<       return
<       end
< c------------------------------------------------------------------------------
<       subroutine red_dist(d,d0,delta,d_red,gd_red)
<       implicit none
<       double precision d,d0,delta,d_red,gd_red
<       double precision x,x2
<       if (d.le.d0) then
<         d_red=d0
<         gd_red=0.0d0
<       else if (d.lt.d0+delta) then
<         x = (d-d0)/delta
<         x2 = x*x
<         d_red = (-0.5d0*x2+1.5d0)*x2*delta+d0
<         gd_red = (-2.0d0*x2+3.0d0)*x
<       else
<         d_red=d
<         gd_red=1.0d0
<       endif
omp.F
5,6d4
<       use minima
<       use linmin
14d11
<       include "COMMON.INTERACT"
18,21c15,24
<       integer i
< #ifdef _OPENMP
<       write(*,*)'omp_get_max_threads =',omp_get_max_threads()
<       if (omp_get_max_threads().gt.max_fg_threads) then
---
>       integer i,ierr,innt,inct,ii,threads_used,my_thread
>       integer alloc_err,th_err,th_errs
>       integer stat
>       character*256 node_size_text
>       character*256 accelerator_text
> 
>       threads_used = omp_get_max_threads()
>       write(*,*)'omp_get_max_threads =',threads_used
> 
>       if (threads_used.gt.max_fg_threads) then
24c27
<         write (iout,*)"  ",omp_get_max_threads(), ">", max_fg_threads
---
>         write (iout,*)"  ",threads_used, ">", max_fg_threads
26c29,71
<         stop
---
>         call terminate
>       endif
> 
> #ifdef MPI
>       time00=MPI_Wtime()
> #endif
>       call getenv_loc('NODESIZE', node_size_text)
>       read(node_size_text,*,iostat=stat) numa_node_size
>       if (stat.ne.0 .or. numa_node_size.le.1) then
>         numa_node_size=threads_used
>       endif
>       write (iout,*)'Assuming NUMA node size',numa_node_size
> 
>       accelerator_mode=ACCELERATOR_DEFAULT
> 
>       call getenv_loc('ACCELERATOR', accelerator_text)
>       read(node_size_text,*,iostat=stat) numa_node_size
>       if (stat.ne.0) then
>         accelerator_mode=ACCELERATOR_NONE
>         write (iout,*)'Warning: could not understand ACCELERATOR',
>      &    'parameter; assuming default.'
>       else if (trim(accelerator_text).eq.'') then
>         accelerator_mode=ACCELERATOR_DEFAULT
>       else if (trim(accelerator_text).eq.'gpu'
>      &  .or. trim(accelerator_text).eq.'cuda') then
>         accelerator_mode=ACCELERATOR_CUDA
> #ifndef WITH_CUDA
>         write (iout,*)'ERROR: Not compiled with ACCELERATOR=CUDA.'
>         call terminate
> #endif
>       else if (trim(accelerator_text).eq.'cpu') then
>         accelerator_mode=ACCELERATOR_NONE
>       else
>         write (iout,*)'Unknown ACCELERATOR: '//trim(accelerator_text)
>         call terminate
>       endif
> 
>       if (accelerator_mode.eq.ACCELERATOR_DEFAULT) then
> #ifdef WITH_CUDA
>         accelerator_mode=ACCELERATOR_CUDA
> #else
>         accelerator_mode=ACCELERATOR_NONE
> #endif
27a73,99
> 
>       if (accelerator_mode.eq.ACCELERATOR_NONE) then
>         write (iout,*)'Using CPU for MD calculations.'
>       else if (accelerator_mode.eq.ACCELERATOR_CUDA) then
>         write (iout,*)'Using CUDA for MD calculations.'
>       else
>         accelerator_mode=ACCELERATOR_NONE
>         write (iout,*)'Using CPU for MD calculations.'
>       endif
> 
>       !call initialize_sss_maps
>       call ensure_modules_initialized
> 
>       call omp_allocate_arrays(threads_used)
> 
> #ifdef MPI
>       time_omp_init=MPI_Wtime()-time00
> #endif
>       return
>       end
> 
> C-----------------------------------------------------------------------
>       subroutine init_omp2
>       use omp_lib
>       implicit none
> #ifdef MPI
>       include 'mpif.h'
28a101,110
>       include "DIMENSIONS"
>       include "COMMON.CHAIN"
>       include "COMMON.IOUNITS"
>       include "COMMON.INTERACT"
>       include "COMMON.TIME1"
>       include "COMMON.OMP"
>       double precision time00
>       integer i,ierr,innt,inct,ii,threads_used,my_thread
>       integer alloc_err,th_err,th_errs
> 
32,36d113
<       call clear_eelec_values(max_fg_threads)
<       call clear_eelec_arrays(max_fg_threads)
<       call clear_escp_values(max_fg_threads)
<       call clear_escp_arrays(max_fg_threads)
<       call clear_egb_arrays(max_fg_threads)
38,41c115,138
<       do i=1,nres
<         abs_itype(i)=iabs(itype(i))
<         abs_itel(i)=iabs(itel(i))
<       enddo
---
>       threads_used = omp_get_max_threads()
> 
>       !call initialize_sss_maps
> 
>       !call omp_allocate_arrays(threads_used)
>       call omp_thread_clear_arrays(threads_used)
>       call precalculate_arrays
> 
>       !write(*,*)'clearing omp_dgradbufc in omp init'
>       omp_dgradbufc(:,-1:nres)=0.0d0
>       omp_dgradc(:,-1:nres)=0.0d0
>       omp_dgradx(:,-1:nres)=0.0d0
>       omp_dgloc(-6:6*nres)=0.0d0
> 
>       eelec_group_results=.false.
>       escp_group_results=.false.
>       egb_group_results=.false.
>       eturn_group_results=.false.
>       etor_group_results=.false.
>       esc_group_results=.false.
>       ebend_group_results=.false.
>       ebond_group_results=.false.
>       elip_group_results=.false.
>       esaxs_group_results=.false.
42a140,152
>       idexpon6=6.0d0/expon
> 
> #ifdef MPI
>       time_omp_init=time_omp_init+MPI_Wtime()-time00
> #endif
>       return
>       end
> 
> c-----------------------------------------------------------------------
>       subroutine ensure_modules_initialized
>       use minima
>       use linmin
>       implicit none
50a161,162
>       return
>       end
52,54c164,286
< #ifdef MPI
<       time_omp_init=MPI_Wtime()-time00
< #endif
---
> c-----------------------------------------------------------------------
>       subroutine precalculate_arrays
>       implicit none
>       include 'DIMENSIONS'
>       include "COMMON.CHAIN"
>       include "COMMON.INTERACT"
>       include "COMMON.TORSION"
>       include "COMMON.OMP"
>       integer i,ii,innt,inct
> 
>       iti_tab(0)=nloctyp
>       iti_tab(nres+1)=nloctyp
> !$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(ii,innt,inct)
>       do i=1,nres
>         abs_itype(i)=iabs(itype(i))
>         abs_itel(i)=iabs(itel(i))
> 
>         if (i.lt.nres) then
>           ii=ireschain(i)
>           if (ii.eq.0) then
>             innt=0
>             inct=0
>           else
>             innt=chain_border(1,ii)
>             inct=chain_border(2,ii)
>           endif
>           if (i.gt.innt .and. i.lt.inct) then
>             iti_tab(i)=itype2loc(itype(i))
>           else
>             iti_tab(i)=nloctyp
>           endif
>         endif
>       enddo
>       return
>       end
> 
> c-----------------------------------------------------------------------
>       subroutine omp_allocate_arrays(threads_used)
>       use omp_lib
>       use omp_data
>       implicit none
>       include 'DIMENSIONS'
>       !include 'COMMON.CHAIN'
>       integer threads_used,my_thread
>       integer alloc_err,th_sum,th_err,th_errs(max_fg_threads)
> 
>       alloc_err=0
> 
> !     allocate(list_ranges(threads_used),stat=alloc_err)
> !     write(*,*)'A=',alloc_err
> !     if(alloc_err.ne.0) call alloc_fail
> !     allocate(list_ranges2(threads_used),stat=alloc_err)
> !     write(*,*)'B=',alloc_err
> !     if(alloc_err.ne.0) call alloc_fail
>       allocate(workspace(threads_used),stat=alloc_err)
> !     write(*,*)'C=',alloc_err
>       if(alloc_err.ne.0) call alloc_fail
> 
> !$OMP PARALLEL NUM_THREADS(threads_used) DEFAULT(SHARED)
> !$OMP& PRIVATE(my_thread,th_err,th_sum)
>       my_thread=omp_get_thread_num()+1
>       th_sum=0
> 
>       allocate(workspace(my_thread).item,stat=th_err)
>       !write(*,*)'TC=',th_err
> !     write(*,'(a,i,z16,z8)')'TCloc',my_thread,
> !    &  loc(workspace(my_thread).item),
> !    &  sizeof(workspace(my_thread).item)
>       th_sum=max(th_sum,th_err)
> 
>       !write(*,*)'TA',my_thread,maxres,maxint_gr,2*maxres*maxint_gr
> !     allocate(list_ranges(my_thread).array(2*nres*maxint_gr),
> !    &         stat=th_err)
>       allocate(
>      &  workspace(my_thread).item.list_ranges(2*maxres*maxint_gr),
>      &  stat=th_err)
>       !write(*,*)'TA=',th_err
>       th_sum=max(th_sum,th_err)
> 
>       !write(*,*)'TB',my_thread,nres,maxint_gr,2*nres*maxint_gr
> !     allocate(list_ranges2(my_thread).array(2*nres*maxint_gr),
> !    &         stat=th_err)
>       allocate(
>      &  workspace(my_thread).item.list_ranges2(2*maxres*maxint_gr),
>      &  stat=th_err)
>       th_sum=max(th_sum,th_err)
>       !write(*,*)'TB=',th_err
> 
>       allocate(workspace(my_thread).item.list_sep(maxres),stat=th_err)
>       th_sum=max(th_sum,th_err)
> 
>       allocate(workspace(my_thread).item.list_sep2(maxres),stat=th_err)
>       th_sum=max(th_sum,th_err)
> 
>       th_errs(my_thread)=th_sum
> !     write(*,*)'TE=',th_sum
> !$OMP END PARALLEL
>       alloc_err=max(alloc_err,maxval(th_errs(1:threads_used)))
> 
>       !write(*,*)'AE=',alloc_err
>       if(alloc_err.ne.0) call alloc_fail
>       return
>       end
> 
> c-----------------------------------------------------------------------
>       subroutine omp_thread_clear_arrays(threads_used)
>       use omp_lib
>       implicit none
>       integer threads_used,my_thread
>       call clear_eelec_values(threads_used)
>       call clear_escp_values(threads_used)
>       call clear_egb_values(threads_used)
> 
> !$OMP PARALLEL NUM_THREADS(threads_used) DEFAULT(SHARED)
> !$OMP& PRIVATE(my_thread)
>       my_thread=omp_get_thread_num()+1
>       call omp_thread_clear_gradient_buffers(my_thread)
>       call clear_eelec_arrays(my_thread)
>       call clear_egb_arrays(my_thread)
>       call clear_saxs_arrays(my_thread)
> !$OMP END PARALLEL
> 
>       call clear_common_eelec_arrays(threads_used)
65,66c297
<      &                                  block_list,block_list_size,
<      &                                  interaction_limit,limited)
---
>      &                                  block_list,block_list_size)
81,82c312
<       integer thread,jstart,jend
<       integer interaction_limit
---
>       integer thread,sthread,scount,jstart,jend
86c316
<       logical limited,started
---
>       logical started,is_separate
87a318
>       integer a,b,c
98d328
<         !do jblock=i_list(2,ikont-1)+1,i_list(2,ikont)
106,113d335
<       limited=.false.
< 
<       if((interaction_limit.gt.0).and.
<      &   (num_interactions .lt. interaction_limit)) then
<         threads_used=1
<         limited=.true.
<       endif
< 
116a339,340
>       !write(*,*)'ITS',num_interactions,inters_per_thread,num_tails
> 
120a345,351
>       l_from_ik(1:max_threads)=0
>       l_to_ik(1:max_threads)=-1
>       l_first_blk(1:max_threads)=0
>       l_last_blk(1:max_threads)=-1
>       l_first_j(1:max_threads)=0
>       l_last_j(1:max_threads)=-1
> 
128a360,362
> 
>           !write(*,*)'BLK',i,block_start,block_end,block_size
> 
144,146c378,382
<               !if (thread.gt.0) then
<                 l_to_ik(thread)=ikont
<                 l_last_blk(thread)=jblock
---
>               l_to_ik(thread)=ikont
>               l_last_blk(thread)=jblock
>               if(block_size.eq.work_size) then   ! to account for shortened blocks
>                 l_last_j(thread)=block_end
>               else
148c384
<               !endif
---
>               endif
153a390
>               !write(*,*)'NEXT THREAD', thread
173a411,448
> 
> !     write(*,*)'SPLIT into',threads_used
> !     do i=1,threads_used
> !       write(*,*)' BLKS for', i
> !       write(*,*)' I',from_ik(i),to_ik(i)
> !       write(*,*)' F',first_blk(i),last_blk(i)
> !       write(*,*)' J',first_j(i),last_j(i)
> !     enddo
> 
> !     do thread=1,max_threads
> !       write(*,*)'THV',thread
> !       do i=1,plus_count(thread)
> !         write(*,*)'  V+',i,
> !    &              plus_block(i*2+0,thread),
> !    &              plus_block(i*2+1,thread)
> !       enddo
> !       do i=1,minus_count(thread)
> !         write(*,*)'  V-',i,
> !    &              minus_block(i*2+0,thread),
> !    &              minus_block(i*2+1,thread)
> !       enddo
> !     enddo
>       a=0
>       b=0
>       c=0
> !     if (separate_size.gt.1) then
> !       do thread=1,max_threads
> !         do i=1,plus_count(thread)
> !           b=b+plus_block(i*2+1,thread)-plus_block(i*2+0,thread)+1
> !         enddo
> !         do i=1,minus_count(thread)
> !           c=c+minus_block(i*2+1,thread)-minus_block(i*2+0,thread)+1
> !         enddo
> !       enddo
> !       write(*,*)'escp interactions',num_interactions,b,c
> !     endif
> !     write(*,*)'egb interactions',num_interactions
> 
178a454,463
> ! splits range of indexes [low, high] for threads_used threads
> ! thread numbered my_thread gets, in from, to variables, its
> ! part of work; when division is uneven, more work is assigned
> ! to threads with low indexes;
> ! eg.:
> ! split_work_for_threads(lo,hi,1,10,i,4)
> !   i=1 => lo=1,hi=3
> !   i=2 => lo=4,hi=6
> !   i=3 => lo=7,hi=8
> !   i=4 => lo=9,hi=10
182,183d466
<       !include"DIMENSIONS"
<       !include"COMMON.CHAIN"
235c518
<    
---
> 
295c578
<    
---
> 
299a583,584
>         ! TODO: may be better? (less writes)
>         !acc=acc+io(i)
316a602,604
>         ! TODO: may be better? (less writes)
>         !acc=acc+io(i)
>         !io(i)=acc
325a614
>       include 'COMMON.OMP'
334d622
<           sigma_j(j,i)=sigma(i,j)
337a626,636
>       do i=0,ntyp+1
>         do j=0,ntyp+1
>           sigma_j(j,i)=sigma(i,j)
>           if(sigma(i,j).ne.0.d0) then
>             inv_sigma(i,j)=1.0d0/sigma(i,j)
>           else
>             inv_sigma(i,j)=0.0d0
>           endif
>           !write(*,*)'SIGMA',i,j,sigma(i,j),inv_sigma(i,j)
>         enddo
>       enddo
369a669
>       include 'COMMON.GRID'
372c672
<       double precision maxd1,maxd2,maxd3,d
---
>       double precision maxd1,maxd2,maxd3,d,d1,d2,d3
379a680,681
>       call mark_grid_iteration
> 
383,384c685,688
<       if(itime_mat.eq.0) update_lists=.true.
<       
---
>       if(itime_mat.eq.0 .or. first_time_grid) then
>         update_lists=.true.
>       endif
> 
408c712,739
<    
---
> 
> !!$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(d1,d2,d3) REDUCTION(max:maxd1)
> !        do i=1,nres
> !          call dist_sq2(shift_base_c(:,i),c_tobox(:,i),d1)
> !          call dist_sq2(shift_base_c(:,nres+i),c_tobox(:,nres+i),d2)
> !          if(i.lt.nres) then
> !            call dist_sq2(shift_base_cp(:,i),cp(:,i),d3)
> !          else
> !            d3=0.0d0
> !          endif
> !          !write(*,'(a,i3,3f18.15)')'H',i,d1,d2,d3
> !          d1=max(d1,d2)
> !          d1=max(d1,d3)
> !          maxd1=max(maxd1,d1)
> !        enddo
> 
> !       do i=1,nres
> !          call dist_sq2(shift_base_c(:,i),c_tobox(:,i),d1)
> !          call dist_sq2(shift_base_c(:,nres+i),c_tobox(:,nres+i),d2)
> !          if(i.lt.nres) then
> !            call dist_sq2(shift_base_cp(:,i),cp(:,i),d3)
> !          else
> !            d3=0.0d0
> !          endif
> !         write(*,'(i3,a,4f16.10)')i,'C ',c_tobox(:,i),d1
> !         write(*,'(i3,a,4f16.10)')i,'CN',c_tobox(:,nres+i),d2
> !         write(*,'(i3,a,4f16.10)')i,'CP',cp(:,i),d3
> !       enddo
409a741
>         !write(*,'(a,f18.16)')'hst max_shift: ',maxd1
410a743
>           !write(*,'(a,f18.16)')'hst update'
435a769,1174
> 
> c-----------------------------------------------------------------------
>       subroutine omp_sum_gradients
>       use omp_lib
>       use omp_data
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       include 'COMMON.DERIV'
>       include 'COMMON.LIPID'
>       include 'COMMON.OMP'
>       double precision ggg(3),gggbufc(3),gggc(3),gggx(3),gggbufc2(3)
>       double precision g,glip
>       integer i,j,from,to
>       integer threads_used,my_thread,delta,th_base,th_offset,rng
> 
>       threads_used=omp_get_max_threads()
> 
>       if(threads_used.le.numa_node_size) then
>         delta=1
>       else
>         delta=numa_node_size
> !$OMP PARALLEL NUM_THREADS(threads_used) DEFAULT(SHARED)
> !$OMP& PRIVATE(from,to,i,j,g,glip,ggg,my_thread,th_base,th_offset,rng)
>         my_thread=omp_get_thread_num()
>         th_offset=mod(my_thread,delta)
>         th_base=my_thread-th_offset
>         rng=min(delta,threads_used-th_base)
> 
>         my_thread=my_thread+1
>         th_offset=th_offset+1
> 
>         call split_work_for_threads(from,to,0,nres,th_offset,rng)
> 
>         !write(*,*)'C',my_thread,th_base,th_offset,from,to
> 
>         do j=from,to
>           ggg=0.0d0
>           do i=1,rng
>             ggg(1)=ggg(1)+workspace(th_base+i).item.dgradbufcx(j)
>             ggg(2)=ggg(2)+workspace(th_base+i).item.dgradbufcy(j)
>             ggg(3)=ggg(3)+workspace(th_base+i).item.dgradbufcz(j)
>           enddo
>           workspace(th_base+1).item.dgradbufcx(j)=ggg(1)
>           workspace(th_base+1).item.dgradbufcy(j)=ggg(2)
>           workspace(th_base+1).item.dgradbufcz(j)=ggg(3)
> 
>           ggg=0.0d0
>           do i=1,rng
>             ggg(1)=ggg(1)+workspace(th_base+i).item.dgradcx(j)
>             ggg(2)=ggg(2)+workspace(th_base+i).item.dgradcy(j)
>             ggg(3)=ggg(3)+workspace(th_base+i).item.dgradcz(j)
>           enddo
>           workspace(th_base+1).item.dgradcx(j)=ggg(1)
>           workspace(th_base+1).item.dgradcy(j)=ggg(2)
>           workspace(th_base+1).item.dgradcz(j)=ggg(3)
> 
>           ggg=0.0d0
>           do i=1,rng
>             ggg(1)=ggg(1)+workspace(th_base+i).item.dgradxx(j)
>             ggg(2)=ggg(2)+workspace(th_base+i).item.dgradxy(j)
>             ggg(3)=ggg(3)+workspace(th_base+i).item.dgradxz(j)
>           enddo
>           workspace(th_base+1).item.dgradxx(j)=ggg(1)
>           workspace(th_base+1).item.dgradxy(j)=ggg(2)
>           workspace(th_base+1).item.dgradxz(j)=ggg(3)
> 
>           ggg=0.0d0
>           do i=1,rng
>             ggg(1)=ggg(1)+workspace(th_base+i).item.dgradbufc2x(j)
>             ggg(2)=ggg(2)+workspace(th_base+i).item.dgradbufc2y(j)
>             ggg(3)=ggg(3)+workspace(th_base+i).item.dgradbufc2z(j)
>           enddo
>           workspace(th_base+1).item.dgradbufc2x(j)=ggg(1)
>           workspace(th_base+1).item.dgradbufc2y(j)=ggg(2)
>           workspace(th_base+1).item.dgradbufc2z(j)=ggg(3)
> 
>           glip=0.0d0
>           do i=1,rng
>             glip=glip+workspace(th_base+i).item.dlipssgrad_nres(j)
>           enddo
>           workspace(th_base+1).item.dlipssgrad_nres(j)=glip
> 
>         enddo
> 
>         do j=from*6-5,to*6
>           g=0.0d0
>           do i=1,rng
>             g=g+workspace(th_base+i).item.dgloc(j)
>           enddo
>           workspace(th_base+1).item.dgloc(j)=g
>         enddo
> !$OMP END PARALLEL
>       endif
> 
> !$OMP PARALLEL NUM_THREADS(threads_used) DEFAULT(SHARED)
> !$OMP& PRIVATE(from,to,i,j,g,glip,gggbufc,gggc,gggx,gggbufc2,my_thread)
>       my_thread=omp_get_thread_num()+1
> 
>       call split_work_for_threads(from,to,0,nres,my_thread,threads_used)
> 
>       do j=from,to
>         gggbufc=0.0d0
>         do i=1,threads_used,delta
>           gggbufc(1)=gggbufc(1)+workspace(i).item.dgradbufcx(j)
>           gggbufc(2)=gggbufc(2)+workspace(i).item.dgradbufcy(j)
>           gggbufc(3)=gggbufc(3)+workspace(i).item.dgradbufcz(j)
>         enddo
> 
>         gggc=0.0d0
>         do i=1,threads_used,delta
>           gggc(1)=gggc(1)+workspace(i).item.dgradcx(j)
>           gggc(2)=gggc(2)+workspace(i).item.dgradcy(j)
>           gggc(3)=gggc(3)+workspace(i).item.dgradcz(j)
>         enddo
> 
>         gggbufc2=0.0d0
>         do i=1,threads_used,delta
>           gggbufc2(1)=gggbufc2(1)+workspace(i).item.dgradbufc2x(j)
>           gggbufc2(2)=gggbufc2(2)+workspace(i).item.dgradbufc2y(j)
>           gggbufc2(3)=gggbufc2(3)+workspace(i).item.dgradbufc2z(j)
>         enddo
> 
>         glip=0.0d0
>         do i=1,threads_used,delta
>           glip=glip+workspace(i).item.dlipssgrad_nres(j)
>         enddo
>         glip=glip*lipid_cache_ssgrad(nres+j)
> 
>         !write(*,'(a,i4,3f18.6)')'ebc',j,omp_dgradbufc(:,j)
>         !write(*,'(a,i4,3f18.12)')'egc',j,omp_dgradc(:,j)
> 
>         omp_dgradbufc(:,j)=omp_dgradbufc(:,j)+gggbufc+gggbufc2
>         omp_dgradbufc(3,j)=omp_dgradbufc(3,j)+glip
> 
>         omp_dgradc(:,j)=omp_dgradc(:,j)+gggc+gggbufc2*0.5d0
> 
>         gggx=0.0d0
>         do i=1,threads_used,delta
>           gggx(1)=gggx(1)+workspace(i).item.dgradxx(j)
>           gggx(2)=gggx(2)+workspace(i).item.dgradxy(j)
>           gggx(3)=gggx(3)+workspace(i).item.dgradxz(j)
>         enddo
>         omp_dgradx(:,j)=omp_dgradx(:,j)+gggx
>         omp_dgradx(3,j)=omp_dgradx(3,j)+glip
> 
>         !write(*,'(a,i4,3f18.12)')'EBC',j,omp_dgradbufc(:,j)
>         !write(*,'(a,i4,3f18.12)')'EGC',j,omp_dgradc(:,j)
>         !write(*,'(a,i4,3f18.12)')'EGX',j,omp_dgradx(:,j)
>       enddo
> 
>       do j=from*6-5,to*6
>         g=0.0d0
>         do i=1,threads_used,delta
>           g=g+workspace(i).item.dgloc(j)
>         enddo
>         omp_dgloc(j)=omp_dgloc(j)+g
>       enddo
> !$OMP END PARALLEL
> 
> !$OMP PARALLEL NUM_THREADS(threads_used) DEFAULT(SHARED)
> !$OMP& PRIVATE(my_thread)
>       my_thread=omp_get_thread_num()+1
>       call omp_thread_clear_gradient_buffers(my_thread)
> !$OMP END PARALLEL
> 
>       return
>       end
> 
> c-----------------------------------------------------------------------
>       subroutine omp_thread_clear_gradient_buffers(my_thread)
>       use omp_data
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       integer my_thread
> 
>       workspace(my_thread).item.dgradbufcx(-1:nres)=0.0d0
>       workspace(my_thread).item.dgradbufcy(-1:nres)=0.0d0
>       workspace(my_thread).item.dgradbufcz(-1:nres)=0.0d0
> 
>       workspace(my_thread).item.dgradcx(-1:nres)=0.0d0
>       workspace(my_thread).item.dgradcy(-1:nres)=0.0d0
>       workspace(my_thread).item.dgradcz(-1:nres)=0.0d0
> 
>       workspace(my_thread).item.dgradxx(-1:nres)=0.0d0
>       workspace(my_thread).item.dgradxy(-1:nres)=0.0d0
>       workspace(my_thread).item.dgradxz(-1:nres)=0.0d0
> 
>       workspace(my_thread).item.dgradbufc2x(-1:nres)=0.0d0
>       workspace(my_thread).item.dgradbufc2y(-1:nres)=0.0d0
>       workspace(my_thread).item.dgradbufc2z(-1:nres)=0.0d0
> 
>       workspace(my_thread).item.dlipssgrad_nres(-1:nres)=0.0d0
> 
>       workspace(my_thread).item.dgloc(0:nres*6)=0.0d0
>       return
>       end
> 
> c-----------------------------------------------------------------------
>       subroutine build_pairs_lists_for_threads(threads_used,
>      &                                         all_low,all_high,
>      &                                         low,high,delta,
>      &                                         block_counts,
>      &                                         blocks,blk_hi,
>      &                                         item_typei,filter_valuei,
>      &                                         item_typej,filter_valuej,
>      &                                         istart,iend,nint_gr)
>       implicit none
>       include 'DIMENSIONS'
> 
>       integer threads_used,all_low,all_high,low,high,delta,blk_hi
>       integer block_counts(threads_used)
>       integer blocks(0:blk_hi,threads_used)
>       integer item_typei(all_high),filter_valuei
>       integer item_typej(all_high),filter_valuej
>       integer nint_gr(maxres)
>       integer istart(maxres,maxint_gr),iend(maxres,maxint_gr)
>       integer from,to,item_count,typi,typj,i,j,blk,last
>       integer current_thread,item_per_thread,item_tails,work_left
>       integer r1lo,r1hi,r2lo,r2hi,my_thread,iintlo,iinthi
> 
>       from=max(low,all_low-delta)
>       to=min(high,all_high-delta)
> 
>       block_counts(1:threads_used)=0
> 
>       item_count=0
>       do i=from,to
> 
>         typi=item_typei(i)
>         if (typi.eq.filter_valuei .or. typi.eq.0) cycle
> 
>         j=i+delta
> 
>         r1lo=istart(i,1)
>         r1hi=iend(i,1)
>         if(nint_gr(i).gt.1) then
>           r2lo=istart(i,2)
>           r2hi=iend(i,2)
>         else
>           r2lo=r1lo
>           r2hi=r1hi
>         endif
>         !write(*,*)'C',i,j,r1lo,r1hi,r2lo,r2hi
>         if((j.lt.r1lo .or. j.gt.r1hi) .and.
>      &     (j.lt.r2lo .or. j.gt.r2hi)) cycle
> 
>         typj=item_typej(j)
>         if (typj.eq.filter_valuej .or. typj.eq.0) cycle
>         item_count=item_count+1
>       enddo
> 
>       item_per_thread=item_count/threads_used
>       item_tails=item_count-item_per_thread*threads_used
> 
>       blk=0
>       last=-1
>       current_thread=1
>       work_left=item_per_thread
>       if(current_thread.le.item_tails) work_left=work_left+1
> 
>       do i=from,to
>         typi=item_typei(i)
>         if (typi.eq.filter_valuei .or. typi.eq.0) cycle
> 
>         j=i+delta
> 
>         r1lo=istart(i,1)
>         r1hi=iend(i,1)
>         if(nint_gr(i).gt.1) then
>           r2lo=istart(i,2)
>           r2hi=iend(i,2)
>         else
>           r2lo=r1lo
>           r2hi=r1hi
>         endif
>         !write(*,*)'D',i,j,r1lo,r1hi,r2lo,r2hi
>         if((j.lt.r1lo .or. j.gt.r1hi) .and.
>      &     (j.lt.r2lo .or. j.gt.r2hi)) cycle
> 
>         typj=item_typej(j)
>         if (typj.eq.filter_valuej .or. typj.eq.0) cycle
> 
>         if(i.ne.last+1) then
>           blk=blk+1
>           blocks(blk*2+0,current_thread)=i
>         endif
>         blocks(blk*2+1,current_thread)=i
> 
>         last=i
>         work_left=work_left-1
>         if(work_left.le.0) then
>           block_counts(current_thread)=blk
>           current_thread=current_thread+1
>           work_left=item_per_thread
>           if(current_thread.le.item_tails) work_left=work_left+1
>           blk=0
>           last=-1
>         endif
>       enddo
>       end
> 
> c-----------------------------------------------------------------------
>       subroutine make_soa_dc_norm
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       include 'COMMON.OMP'
>       integer i
> 
>       soa_dc_norm_x(1)=dc_norm(1,1)
>       soa_dc_norm_y(1)=dc_norm(2,1)
>       soa_dc_norm_z(1)=dc_norm(3,1)
> c Calculate normalized sidechain-virtual-bond vectors
> !$OMP PARALLEL DO DEFAULT(SHARED)
>       do i=2,nres-1
>         soa_dc_norm_x(i)=dc_norm(1,i)
>         soa_dc_norm_y(i)=dc_norm(2,i)
>         soa_dc_norm_z(i)=dc_norm(3,i)
>         soa_dc_norm_x(i+nres)=dc_norm(1,i+nres)
>         soa_dc_norm_y(i+nres)=dc_norm(2,i+nres)
>         soa_dc_norm_z(i+nres)=dc_norm(3,i+nres)
>       enddo
>       return
>       end
> 
> c-----------------------------------------------------------------------
>       logical function uses_cuda()
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.OMP'
>       uses_cuda=(accelerator_mode.eq.ACCELERATOR_CUDA)
>       end function
> 
> c-----------------------------------------------------------------------
> !      subroutine initialize_sss_maps
> !      implicit none
> !      include 'DIMENSIONS'
> !      include 'COMMON.SPLITELE'
> !      include 'COMMON.INTERACT'
> !      include 'COMMON.OMP'
> !      integer i
> !      double precision rij,vsss,vsssgrad,sqrtrij,gamm
> !      double precision imap_size,dexpon,width,offset
> !
> !      dexpon=expon
> !      width=r_cut_int*r_cut_int-sss_map_offset
> !      sss_map_scale=dble(sss_map_size)/width
> !      offset=(r_cut_int-rlamb)*(r_cut_int-rlamb)
> !      sss_map_offset=offset*sss_map_scale
> !      r_sss_map_size=real(sss_map_size,4)
> !
> !      imap_size=width/sss_map_size
> !
> !      do i=0,sss_map_size
> !        !rij=r_cut_int+dble(rlamb)*i/sss_map_size
> !        rij=offset+i*imap_size
> !        !rij=r_cut_int+i*imap_size
> !        rij=dsqrt(rij)
> !        !sqrtrij=dsqrt(rij)
> !        !gamm=max(0.0d0,(sqrtrij-r_dropoff)*irlamb)
> !        !gamm=min(1.0d0,gamm)
> !        !esss=1.0d0+gamm*gamm*(2*gamm-3.0d0)
> !        !esssgrad=gamm*(6*gamm-6.0d0)*irlamb
> !        !!esssgrad=esssgrad*sqrtrrij
> !        !esssgrad=esssgrad/(sqrtrij*dexpon)
> !
> !        !if(rij.lt.r_cut_int-rlamb) then
> !        !  vsss=1.0d0
> !        !  vsssgrad=0.0d0
> !        !else
> !          gamm=(rij-(r_cut_int-rlamb))*irlamb
> !          gamm=max(0.0d0,min(gamm,1.0d0))
> !          vsss=1.0d0+gamm*gamm*(2.0d0*gamm-3.0d0)
> !          vsssgrad=gamm*(6.0d0*gamm-6.0d0)*irlamb
> !          vsssgrad=vsssgrad/(rij*dexpon)
> !        !endif
> !        !TODO: proper rounding
> !        sss_map(i)=vsss
> !        sssgrad_map(i)=vsssgrad
> !      enddo
> !      sss_map(sss_map_size+1)=sss_map(sss_map_size)
> !      sssgrad_map(sss_map_size+1)=sssgrad_map(sss_map_size)
> !      end
> !
> !      subroutine map_sss_escp(rij,res,resgrad)
> !      implicit none
> !      include 'DIMENSIONS'
> !      include 'COMMON.SPLITELE'
> !      include 'COMMON.OMP'
> !      double precision rij
> !      !real*4 t,x,xf,res,resgrad
> !      real*4 t,x,res,resgrad
> !      integer i
> !      !x=(rij-sss_map_offset)*sss_map_scale
> !      x=real(rij*sss_map_scale-sss_map_offset,4)
> !      x=max(0.0,min(x,r_sss_map_size))
> !      !xf=aint(x)
> !      i=int(x,4)
> !      t=x-i
> !      !t=x-xf
> !      !i=int(xf,4)
> !      res=sss_map(i)*(1.0-t)+sss_map(i+1)*t
> !      resgrad=sssgrad_map(i)*(1.0-t)+sssgrad_map(i+1)*t
> !      end
optsave_dum.f
orig_frame_chain.F
output.f
out_viol.F
39c39
<      & iviol,list_peak_viol(maxdim_nmr)
---
>      & iviol,list_peak_viol(maxdim)
parmread.F
75a76,85
> C To write to faster but not human readable restart file format:
> C note: RSB stores values with full precision instead of
> C       RTS's only 5 decimal digits
> C
> C setenv USE_RSB_OUTPUT YES
> C
> C To read RST file with the virtual-bond based content:
> C
> C setenv VIRTUAL_BOND_RST YES
> C
80a91,95
>       call getenv_loc("USE_RSB_OUTPUT",lancuch)
>       use_rsb_output=(ucase(lancuch).eq."YES".or.ucase(lancuch).eq."Y")
>       call getenv_loc("VIRTUAL_BOND_RST",lancuch)
>       virtual_bond_rst=
>      &  (ucase(lancuch).eq."YES".or.ucase(lancuch).eq."Y")
1871a1887
>       write (iout,*) "Xlink side bonds"
1873a1890
>         write (iout,*) res1,res2,namlink(i),ijunk,(rjunk,k=1,3*ijunk)
1877a1895
>         write (iout,*)ijunk,(rjunk,k=1,3*ijunk)
1882a1901
>       write (iout,*) "Xlink central bonds"
1885a1905,1906
>         write (iout,*) res1,res2,linknam,nbxlink(i),
>      &   (alink(k,i),aklink(k,i),dxlink(k,i),k=1,nbxlink(i))
1892a1914
>       write (iout,*) "Xlink angles"
1895a1918,1919
>         write (iout,*) res1,res2,linknam,ntxlink(1,i),
>      &   v0tlink(1,i),(axlink(k,1,i),bxlink(k,1,i),k=1,ntxlink(1,i))
1900a1925,1926
>         write (iout,*) ntxlink(2,i),
>      &   v0tlink(2,i),(axlink(k,2,i),bxlink(k,2,i),k=1,ntxlink(2,i))
1904a1931
>       write (iout,*) "Xlink torsional"
1907a1935,1936
>         write (iout,*) res1,res2,linknam,ngxlink(i),
>      &   v0glink(i),(vglink(k,i),k=1,ngxlink(i))
1918c1947
<      &   3('    a0  ','     k  ','    d0    '))") (k,k=1,nbxlink(i)) 
---
>      &   6('    a0  ','     k  ','    d0    '))")
1929d1957
<      &   (k,k=1,max0(ntxlink(1,i),ntxlink(2,i)) )
2305a2334,2340
> #define PAWEL
> #ifdef PAWEL
>       call reada(weightcard,"ATRISS",atriss,50000.0D0)
>       call reada(weightcard,"BTRISS",btriss,0.05D0)
>       call reada(weightcard,"CTRISS",ctriss,0.01D0)
>       call reada(weightcard,"DTRISS",dtriss,125000.0D0)
> #else
2309a2345
> #endif
permut.F
pinorm.f
place_coords_in_box.F
5a6
>       include 'COMMON.OMP'
10,18d10
<       do i=1,nres-1
<         call to_box2(c(:,i),c_tobox(:,i))
<         pos=0.5d0*(c(:,i)+c(:,i+1))
<         call to_box2(pos,cp(:,i))
<       enddo
< 
<       call to_box2(c(:,nres),c_tobox(:,nres))
< 
< !$OMP PARALLEL DO DEFAULT(SHARED)
19a12,24
>         if (i.lt.nres) then
>           call to_box2(c(:,i),c_tobox(:,i))
>           soa_c_x(i)=c_tobox(1,i)
>           soa_c_y(i)=c_tobox(2,i)
>           soa_c_z(i)=c_tobox(3,i)
>    
>           pos=0.5d0*(c(:,i)+c(:,i+1))
>           call to_box2(pos,cp(:,i))
>           soa_cp_x(i)=cp(1,i)
>           soa_cp_y(i)=cp(2,i)
>           soa_cp_z(i)=cp(3,i)
>         endif
> 
20a26,28
>         soa_c_x(nres+i)=c_tobox(1,nres+i)
>         soa_c_y(nres+i)=c_tobox(2,nres+i)
>         soa_c_z(nres+i)=c_tobox(3,nres+i)
21a30,37
> 
>       soa_cp_x(nres)=0.0d0
>       soa_cp_y(nres)=0.0d0
>       soa_cp_z(nres)=0.0d0
>       call to_box2(c(:,nres),c_tobox(:,nres))
>       soa_c_x(nres)=c_tobox(1,nres)
>       soa_c_y(nres)=c_tobox(2,nres)
>       soa_c_z(nres)=c_tobox(3,nres)
PMFprocess.F
printmat.f
prng_32.F
q_measure.F
randgens.f
ran.f
rattle.F
read_dist_constr.F
19,20c19
<       double precision fordepth_peak_,forcon_peak_,protdist_min,
<      &  linpeak_lo,linpeak_up
---
>       double precision fordepth_peak_,forcon_peak_,protdist_min
67,68d65
<         call reada(controlcard,'LINPEAK_LO',linpeak_lo,0.0d0)
<         call reada(controlcard,'LINPEAK_UP',linpeak_up,2.0d0)
74,75d70
<         write (iout,*) "LINPEAK_LO",linpeak_lo
<         write (iout,*) "LINPEAK_UP",linpeak_up
113c108
<             ddjk=dist(j,k)
---
>             ddjk=dist(c(1,j),c(1,k))
160c155
<             ddjk=dist(j,k)
---
>             ddjk=dist(c(1,j),c(1,k))
222,232c217
<           if (dhpb1_peak(nhpb_peak+1).lt.0.0d0) then
<             dhpb_peak(nhpb_peak+1)=-dhpb1_peak(nhpb_peak+1)
<             dhpb1_peak(nhpb_peak+1)=50.0d0
<             fordepth_peak(nhpb_peak+1)=fordepth_peak_
<           else
<             dhpb_peak(nhpb_peak+1)=protdist_min
<             fordepth_peak(nhpb_peak+1)=fordepth_peak_
<           endif
<           forcon_peak(nhpb_peak+1)=forcon_peak_
<           dhpb2_peak(nhpb_peak+1)=dhpb_peak(nhpb_peak+1)-linpeak_lo
<           dhpb3_peak(nhpb_peak+1)=dhpb1_peak(nhpb_peak+1)+linpeak_up
---
>           dhpb_peak(nhpb_peak+1)=protdist_min
233a219
>           forcon_peak(nhpb_peak+1)=forcon_peak_
234a221
>           fordepth_peak(nhpb_peak+1)=fordepth_peak_
456c443
<      &       dhpb(nhpb)=dist(ihpb(nhpb),jhpb(nhpb))
---
>      &       dhpb(nhpb)=dist(c(1,ihpb(nhpb)),c(1,jhpb(nhpb)))
503c490
<             dhpb(nhpb)=dist(i+nres*ii,j+nres*jj)
---
>             dhpb(nhpb)=dist(c(1,i+nres*ii),c(1,j+nres*jj))
readpdb-mult.F
437c437
< c      write (2,*) "UNRES_PDB",unres_pdb
---
>       write (2,*) "readpdb_template UNRES_PDB",unres_pdb," k",k
readrtns_CSA.F
92,103d91
<         if (atimeave_save.eq.1) then
<           write (iout,*) "Time-averaged restraints will be implemented"
<           write (iout,*) "TAU:",tau," UNRES MD units"
<           write (iout,*) "Silent averaging during",navint," time steps"
<           write (iout,*) "Forces scaled up until target scale factor",
<      &      " every ",it_interval," time steps in ",ndiv_tau," steps"
<           write (iout,*) "Modes of force calculation: distances",
<      &     ider_ave(1)," angles",ider_ave(2)," dihedrals",ider_ave(3)
<           write (iout,*) "Force scaling",.not.nonscal_ave_grad
<           write (iout,*) "Boundary on distance-force scaling",
<      &     dravdr_limit
<         endif
129d116
<       include 'COMMON.NMR'
192,212d178
< c---------------read parameters for time-averaged calulation----------------- 
<       call readi(controlcard,"TIME_AVE",atimeave,0)
<       atimeave_save=atimeave
<       atimeave=0
<       call readi(controlcard,"IDER_AVE_DIST",ider_ave(1),0)
<       call readi(controlcard,"IDER_AVE_DIHED",ider_ave(3),0)
<       call readi(controlcard,"IDER_AVE_ANG",ider_ave(2),0)
<       nonscal_ave_grad = index(controlcard,"NONSCAL_AVE_GRAD").gt.0
<       call readi(controlcard,"NAVINT",navint,1)
<       call readi(controlcard,"AVESCALE",it_interval,1)
< c      if (it_interval.lt.navint) it_interval=navint
<       call readi(controlcard,"NDIVTAU",ndiv_tau,1)
<       call reada(controlcard,"TAU_NMR",tau,100)
<       call reada(controlcard,"DRAVDR_LIMIT",dravdr_limit,1.0d1)
<       ave_rep = index(controlcard,"AVE_REP").gt.0
<       wham_rep = index(controlcard,"WHAM_REP").gt.  0
<       call readi(controlcard,"N_EXCH_RESTR",n_exch_restr,100)
<       call readi(controlcard,"MAXIT_WHAM",maxit_wham,5000)
<       write (iout,*) "AVE_REP ",ave_rep," N_EXCH_RESTR",N_EXCH_RESTR
<       write (iout,*) "WHAM_REP ",wham_rep," MAXIT_WHAM",MAXIT_WHAM
< c---------------end read parameter for time-averaged calculation--------------
253c219
<       mremd_dec=(index(controlcard,'MRM_DEC').gt.0)
---
>       mremd_dec=(index(controlcard,'MREMD_DEC').gt.0)
466d431
<       include 'COMMON.WHAM'
507,513c472,473
<          do i=1,nrep
<            beta_h(i)=1.0d0/(remd_t(i)*1.987D-3)
<          enddo
<          if (me.eq.king.or..not.out1file) then
<            write (iout,*)'tlist ',(remd_t(i),i=1,nrep)
<            write (iout,*)'beta_h',(beta_h(i),i=1,nrep)
<          endif
---
>          if(me.eq.king.or..not.out1file)
>      &    write (iout,*)'tlist',(remd_t(i),i=1,nrep)
528c488
<        endif
---
>          endif
576d535
<       d_time0 = d_time
582d540
<       PRINT_AMTS_MSG = index(controlcard,"PRINT_AMTS_MSG").gt.0
878a837
>         call save_contact_ref
1211a1171
>           call save_contact_ref
1222a1183
>       icont_shift=0
1225a1187
>         icont_shift=nstart_seq-nstart_sup
1298c1260
<             unres_pdb=.false.
---
> c            unres_pdb=.false.
1300a1263
> c            write (iout,*) "calling readpdb_template",nmodel_start
1301a1265
> c            write (iout,*) "after readpdb_template"
1587c1551
<         if (itype(iss(i)).ne.1) then
---
>         if (iabs(itype(iss(i))).ne.1) then
2480a2445,2446
>       rsbname=prefix(:ilen(prefix))//"_"//liczba(:ilen(liczba))
>      &  //'.rsb'
2507c2473
< c#define DEBUG
---
> !#define DEBUG
2516c2482
< c#undef DEBUG
---
> !#undef DEBUG
2630a2597,2685
>       subroutine readrst
>       implicit none
>       include 'DIMENSIONS'
>       include 'COMMON.CHAIN'
>       include 'COMMON.INTERACT'
>       include 'COMMON.IOUNITS'
>       include 'COMMON.CONTROL'
>       include 'COMMON.MD'
>       include 'COMMON.QRESTR'
>       include 'COMMON.SETUP'
> #ifdef FIVEDIAG
>        include 'COMMON.LAGRANGE.5diag'
> #else
>        include 'COMMON.LAGRANGE'
> #endif
>       integer i,j,iost
>       integer ilen
>       external ilen
> 
>       ! try reading file in RSB format
>       open(irest2,file=rsbname,status='OLD',iostat=iost,
>      &  form='unformatted')
>       if(iost.eq.0) then
>         if(me.eq.king.or..not.out1file)
>      &    write(iout,*) "Initial state will be read from file ",
>      &    rsbname(:ilen(rsbname))
>         read(irest2) totT,EK,potE,totE,t_bath
> #ifdef VIRTUAL
>         read(irest2) d_t(:,0:2*nres-1)
>         read(irest2) dc(:,0:2*nres-1)
> #else
>         read(irest2) vel(:,1:2*nres)
>         read(irest2) c(:,1:2*nres)
>         if (nnt.gt.1) dc(:,1)=c(:,2)-c(:,1)
>         call calculate_dc
>         call velocities
> #endif
>         if(usampl) then
>           read (irest2) iset
>         endif
> 
>       ! read file in RST format
>       else
>         if(me.eq.king.or..not.out1file)
>      &    write(iout,*) "Initial state will be read from file ",
>      &    rest2name(:ilen(rest2name))
>         open(irest2,file=rest2name,status='unknown')
>         read(irest2,*) totT,EK,potE,totE,t_bath
> #ifndef VIRTUAL
>         if(virtual_bond_rst) then
> #endif
>           do i=0,2*nres-1
>              read(irest2,'(3e15.5)') (d_t(j,i),j=1,3)
>           enddo
>           do i=0,2*nres-1
>              read(irest2,'(3e15.5)') (dc(j,i),j=1,3)
>           enddo
> #ifndef VIRTUAL
>           call chainbuild_cart
>           vel(:,nnt)=d_t(:,0)
>           do i=nnt+1,nres
>             vel(:,i)=vel(:,i)+d_t(j,i)
>           enddo
>           do i=nnt,nct
>             vel(:,i+nres)=vel(:,i+nres)+d_t(j,i)
>           enddo
> 
>         else
>           do i=1,2*nres
>              read(irest2,'(3e15.5)') (vel(j,i),j=1,3)
>           enddo
>           do i=1,2*nres
>              read(irest2,'(3e15.5)') (c(j,i),j=1,3)
>           enddo
>           if (nnt.gt.1) dc(:,1)=c(:,2)-c(:,1)
>           call calculate_dc
>           call velocities
>         endif
> #endif
>         if(usampl) then
>                read (irest2,*) iset
>         endif
>       endif
>       close(irest2)
>       totTafm=totT
> 
>       return
>       end
> c------------------------------------------------------------------------------
2829c2884
<       integer*1 ii_in_use(10*maxdim_cont)
---
>       integer*1 ii_in_use(55*maxdim)
2841,2842c2896,2897
<       common /readconstrhomologycommon/ ii_in_use,
<      &                                  rescore,rescore2,rescore3
---
>       common /readconstrhomologycommon/ rescore,rescore2,rescore3,
>      &                                  ii_in_use
3328a3384,3387
> #ifdef WITH_CUDA
>       include 'COMMON.CUDA'
>       logical uses_cuda
> #endif
3341a3401,3405
> #ifdef WITH_CUDA
>       if (uses_cuda()) then
>         cuda_seed=-seed
>       endif
> #endif
refsys.f
regularize.F
rescode.f
restart.F
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/restart.F: No such file or directory
rmdd.f
rmscalc.F
rmsd.F
10,12c10,17
<         call contact(.false.,ncont,icont,co)
<         frac=contact_fract(ncont,ncont_ref,icont,icont_ref)
<         frac_nn=contact_fract_nn(ncont,ncont_ref,icont,icont_ref)
---
> 
>         call contact_with_fract(.false.,ncont,icont,icont_ref_c,
>      &    icont_shift,co,frac,frac_nn)
> 
>         !call contact(.false.,ncont,icont,co)
>         !frac=contact_fract(ncont,ncont_ref,icont,icont_ref)
>         !frac_nn=contact_fract_nn(ncont,ncont_ref,icont,icont_ref)
> 
scales.f
sc_move.F
search.f
seq2chains.f
shift.F
simdstat.F
sort.f
ssMD.F
86,541d85
<       subroutine dyn_ssbond_ene(resi,resj,eij)
<       implicit none
<       include 'DIMENSIONS'
< #ifdef MPI
<       include 'mpif.h'
<       include 'COMMON.SETUP'
<       integer ierr
< #endif
<       include 'COMMON.SBRIDGE'
<       include 'COMMON.CHAIN'
<       include 'COMMON.DERIV'
<       include 'COMMON.LOCAL'
<       include 'COMMON.INTERACT'
<       include 'COMMON.VAR'
<       include 'COMMON.IOUNITS'
<       include 'COMMON.CALC'
<       include 'COMMON.NAMES'
<       include 'COMMON.SPLITELE'
< #ifndef CLUST
< #ifndef WHAM
<       include 'COMMON.MD'
< #endif
< #endif
< 
< c     External functions
<       double precision h_base
<       external h_base
< 
< c     Input arguments
<       integer resi,resj
< 
< c     Output arguments
<       double precision eij
< 
< c     Local variables
<       logical havebond
< c      integer itypi,itypj,k,l
<       double precision rrij,ssd,deltat1,deltat2,deltat12,cosphi
<       double precision sig0ij,ljd,sig,fac,e1,e2
<       double precision dcosom1(3),dcosom2(3),ed
<       double precision pom1,pom2
<       double precision ljA,ljB,ljXs
<       double precision d_ljB(1:3)
<       double precision ssA,ssB,ssC,ssXs
<       double precision ssxm,ljxm,ssm,ljm
<       double precision d_ssxm(1:3),d_ljxm(1:3),d_ssm(1:3),d_ljm(1:3)
<       double precision f1,f2,h1,h2,hd1,hd2
<       double precision omega,delta_inv,deltasq_inv,fac1,fac2
< c-------FIRST METHOD
<       double precision xm,d_xm(1:3)
<       double precision sslipi,sslipj,ssgradlipi,ssgradlipj
<       integer ici,icj,itypi,itypj
<       double precision boxshift,sscale,sscagrad
< c-------END FIRST METHOD
< c-------SECOND METHOD
< c$$$      double precision ss,d_ss(0:3),ljf,d_ljf(0:3)
< c-------END SECOND METHOD
< 
< c-------TESTING CODE
<       logical checkstop,transgrad
<       common /sschecks/ checkstop,transgrad
< 
<       integer icheck,nicheck,jcheck,njcheck
<       double precision echeck(-1:1),deps,ssx0,ljx0,xi,yi,zi
< c-------END TESTING CODE
< 
< 
<       i=resi
<       j=resj
<       ici=icys(i)
<       icj=icys(j)
< c      write (iout,*) "dyn_ssbond",resi,resj,ici,icj
< c      call flush(iout)
<       if (ici.eq.0 .or. icj.eq.0) then
< #ifdef MPI
<         write (*,'(a,i5,2a,a3,i5,5h and ,a3,i5)')
<      &  "Processor",me," attempt to create",
<      &  " a disulfide link between non-cysteine residues ",restyp(i),i,
<      &  restyp(j),j
<         call MPI_Abort(MPI_COMM_WORLD,ierr)
< #else
<         write (*,'(a,i5,2a,a3,i5,5h and ,a3,i5)')
<      &  "Attempt to create",
<      &  " a disulfide link between non-cysteine residues ",restyp(i),i,
<      &  restyp(j),j
<         stop
< #endif
<       endif
<       itypi=itype(i)
<       dxi=dc_norm(1,nres+i)
<       dyi=dc_norm(2,nres+i)
<       dzi=dc_norm(3,nres+i)
<       dsci_inv=vbld_inv(i+nres)
<       xi=c(1,nres+i)
<       yi=c(2,nres+i)
<       zi=c(3,nres+i)
<       call to_box(xi,yi,zi)
< c      write (iout,*) "After to_box i",xi,yi,zi
< c      call flush(iout)
< C define scaling factor for lipids
< 
< C        if (positi.le.0) positi=positi+boxzsize
< C        print *,i
< C first for peptide groups
< c for each residue check if it is in lipid or lipid water border area
<       call lipid_layer(xi,yi,zi,sslipi,ssgradlipi)
< c      write (iout,*) "After lipid_layer"
< c      call flush(iout)
<       itypj=itype(j)
<       xj=c(1,nres+j)
<       yj=c(2,nres+j)
<       zj=c(3,nres+j)
<       call to_box(xj,yj,zj)
< c      write (iout,*) "After to_box j",xj,yj,zj
< c      call flush(iout)
<       call lipid_layer(xj,yj,zj,sslipj,ssgradlipj)
< c      write (iout,*) "After lipid_layer"
< c      call flush(iout)
<       aa=aa_lip(itypi,itypj)*(sslipi+sslipj)/2.0d0
<      &  +aa_aq(itypi,itypj)*(2.0d0-sslipi+sslipj)/2.0d0
<       bb=bb_lip(itypi,itypj)*(sslipi+sslipj)/2.0d0
<      &  +bb_aq(itypi,itypj)*(2.0d0-sslipi+sslipj)/2.0d0
<       xj=boxshift(xj-xi,boxxsize)
<       yj=boxshift(yj-yi,boxysize)
<       zj=boxshift(zj-zi,boxzsize)
< c      write (iout,*) "After boxshift"
< c      call flush(iout)
<       dxj=dc_norm(1,nres+j)
<       dyj=dc_norm(2,nres+j)
<       dzj=dc_norm(3,nres+j)
<       dscj_inv=vbld_inv(j+nres)
< 
<       chi1=chi(itypi,itypj)
<       chi2=chi(itypj,itypi)
<       chi12=chi1*chi2
<       chip1=chip(itypi)
<       chip2=chip(itypj)
<       chip12=chip1*chip2
<       alf1=alp(itypi)
<       alf2=alp(itypj)
<       alf12=0.5D0*(alf1+alf2)
< 
<       rrij=1.0D0/(xj*xj+yj*yj+zj*zj)
<       rij=dsqrt(rrij)  ! sc_angular needs rij to really be the inverse
<             sss=sscale((1.0d0/rij)/sigma(itypi,itypj),r_cut_int)
<             sssgrad=sscagrad((1.0d0/rij)/sigma(itypi,itypj),r_cut_int)
< c     The following are set in sc_angular
< c      erij(1)=xj*rij
< c      erij(2)=yj*rij
< c      erij(3)=zj*rij
< c      om1=dxi*erij(1)+dyi*erij(2)+dzi*erij(3)
< c      om2=dxj*erij(1)+dyj*erij(2)+dzj*erij(3)
< c      om12=dxi*dxj+dyi*dyj+dzi*dzj
< c      write (iout,*) "Calling sc_angular"
< c      call flush(iout)
<       call sc_angular
< c      write (iout,*) "After sc_angular"
< c      call flush(iout)
<       rij=1.0D0/rij  ! Reset this so it makes sense
< 
<       sig0ij=sigma(itypi,itypj)
<       sig=sig0ij*dsqrt(1.0D0/sigsq)
< 
<       ljXs=sig-sig0ij
<       ljA=eps1*eps2rt**2*eps3rt**2
<       ljB=ljA*bb
<       ljA=ljA*aa
<       ljxm=ljXs+(-2.0D0*aa/bb)**(1.0D0/6.0D0)
< 
<       ssXs=d0cm
<       deltat1=1.0d0-om1
<       deltat2=1.0d0+om2
<       deltat12=om2-om1+2.0d0
<       cosphi=om12-om1*om2
<       ssA=akcm
<       ssB=akct*deltat12
<       ssC=ss_depth
<      &     +akth*(deltat1*deltat1+deltat2*deltat2)
<      &     +v1ss*cosphi+v2ss*cosphi*cosphi+v3ss*cosphi*cosphi*cosphi
<       ssxm=ssXs-0.5D0*ssB/ssA
< 
< c-------TESTING CODE
< c$$$c     Some extra output
< c$$$      ssm=ssC-0.25D0*ssB*ssB/ssA
< c$$$      ljm=-0.25D0*ljB*bb(itypi,itypj)/aa(itypi,itypj)
< c$$$      ssx0=ssB*ssB-4.0d0*ssA*ssC
< c$$$      if (ssx0.gt.0.0d0) then
< c$$$        ssx0=ssXs+0.5d0*(-ssB+sqrt(ssx0))/ssA
< c$$$      else
< c$$$        ssx0=ssxm
< c$$$      endif
< c$$$      ljx0=ljXs+(-aa(itypi,itypj)/bb(itypi,itypj))**(1.0D0/6.0D0)
< c$$$      write(iout,'(a,4f8.2,2f15.2,3f6.2)')"SSENERGIES ",
< c$$$     &     ssxm,ljxm,ssx0,ljx0,ssm,ljm,om1,om2,om12
< c$$$      return
< c-------END TESTING CODE
< 
< c-------TESTING CODE
< c     Stop and plot energy and derivative as a function of distance
< c      write (iout,*) "checkstop",checkstop
< c      call flush(iout)
<       if (checkstop) then
<         ssm=ssC-0.25D0*ssB*ssB/ssA
<         ljm=-0.25D0*ljB*bb/aa
<         if (ssm.lt.ljm .and.
<      &       dabs(rij-0.5d0*(ssxm+ljxm)).lt.0.35d0*(ljxm-ssxm)) then
<           nicheck=1000
<           njcheck=1
<           deps=0.5d-7
<         else
<           checkstop=.false.
<         endif
<       endif
<       if (.not.checkstop) then
<         nicheck=0
<         njcheck=-1
<       endif
< 
<       do icheck=0,nicheck
<       do jcheck=-1,njcheck
<       if (checkstop) rij=(ssxm-1.0d0)+
<      &       ((ljxm-ssxm+2.0d0)*icheck)/nicheck+jcheck*deps
< c-------END TESTING CODE
< 
<       if (rij.gt.ljxm) then
<         havebond=.false.
<         ljd=rij-ljXs
<         fac=(1.0D0/ljd)**expon
<         e1=fac*fac*aa
<         e2=fac*bb
<         eij=eps1*eps2rt*eps3rt*(e1+e2)
<         eps2der=eij*eps3rt
<         eps3der=eij*eps2rt
<         eij=eij*eps2rt*eps3rt*sss
< 
<         sigder=-sig/sigsq
<         e1=e1*eps1*eps2rt**2*eps3rt**2
<         ed=-expon*(e1+eij)/ljd
<         sigder=ed*sigder
<         ed=ed+eij/sss*sssgrad/sigma(itypi,itypj)*rij
<         eom1=eps2der*eps2rt_om1-2.0D0*alf1*eps3der+sigder*sigsq_om1
<         eom2=eps2der*eps2rt_om2+2.0D0*alf2*eps3der+sigder*sigsq_om2
<         eom12=eij*eps1_om12+eps2der*eps2rt_om12
<      &       -2.0D0*alf12*eps3der+sigder*sigsq_om12
<       else if (rij.lt.ssxm) then
<         havebond=.true.
<         ssd=rij-ssXs
<         eij=ssA*ssd*ssd+ssB*ssd+ssC
<         eij=eij*sss
<         ed=2*akcm*ssd+akct*deltat12
<         ed=ed+eij/sss*sssgrad/sigma(itypi,itypj)*rij
<         pom1=akct*ssd
<         pom2=v1ss+2*v2ss*cosphi+3*v3ss*cosphi*cosphi
<         eom1=-2*akth*deltat1-pom1-om2*pom2
<         eom2= 2*akth*deltat2+pom1-om1*pom2
<         eom12=pom2
<       else
<         omega=v1ss+2.0d0*v2ss*cosphi+3.0d0*v3ss*cosphi*cosphi
< 
<         d_ssxm(1)=0.5D0*akct/ssA
<         d_ssxm(2)=-d_ssxm(1)
<         d_ssxm(3)=0.0D0
< 
<         d_ljxm(1)=sig0ij/sqrt(sigsq**3)
<         d_ljxm(2)=d_ljxm(1)*sigsq_om2
<         d_ljxm(3)=d_ljxm(1)*sigsq_om12
<         d_ljxm(1)=d_ljxm(1)*sigsq_om1
< 
< c-------FIRST METHOD, DISCONTINUOUS SECOND DERIVATIVE
<         xm=0.5d0*(ssxm+ljxm)
<         do k=1,3
<           d_xm(k)=0.5d0*(d_ssxm(k)+d_ljxm(k))
<         enddo
<         if (rij.lt.xm) then
<           havebond=.true.
<           ssm=ssC-0.25D0*ssB*ssB/ssA
<           d_ssm(1)=0.5D0*akct*ssB/ssA
<           d_ssm(2)=2.0D0*akth*deltat2-om1*omega-d_ssm(1)
<           d_ssm(1)=-2.0D0*akth*deltat1-om2*omega+d_ssm(1)
<           d_ssm(3)=omega
<           f1=(rij-xm)/(ssxm-xm)
<           f2=(rij-ssxm)/(xm-ssxm)
<           h1=h_base(f1,hd1)
<           h2=h_base(f2,hd2)
<           eij=ssm*h1+Ht*h2
<           delta_inv=1.0d0/(xm-ssxm)
<           deltasq_inv=delta_inv*delta_inv
<           fac=ssm*hd1-Ht*hd2
<           fac1=deltasq_inv*fac*(xm-rij)
<           fac2=deltasq_inv*fac*(rij-ssxm)
<           ed=delta_inv*(Ht*hd2-ssm*hd1)
<           eij=eij*sss
<           ed=ed+eij/sss*sssgrad/sigma(itypi,itypj)*rij
<           eom1=fac1*d_ssxm(1)+fac2*d_xm(1)+h1*d_ssm(1)
<           eom2=fac1*d_ssxm(2)+fac2*d_xm(2)+h1*d_ssm(2)
<           eom12=fac1*d_ssxm(3)+fac2*d_xm(3)+h1*d_ssm(3)
<         else
<           havebond=.false.
<           ljm=-0.25D0*ljB*bb/aa
<           d_ljm(1)=-0.5D0*bb/aa*ljB
<           d_ljm(2)=d_ljm(1)*(0.5D0*eps2rt_om2/eps2rt+alf2/eps3rt)
<           d_ljm(3)=d_ljm(1)*(0.5D0*eps1_om12+0.5D0*eps2rt_om12/eps2rt-
<      +         alf12/eps3rt)
<           d_ljm(1)=d_ljm(1)*(0.5D0*eps2rt_om1/eps2rt-alf1/eps3rt)
<           f1=(rij-ljxm)/(xm-ljxm)
<           f2=(rij-xm)/(ljxm-xm)
<           h1=h_base(f1,hd1)
<           h2=h_base(f2,hd2)
<           eij=Ht*h1+ljm*h2
<           delta_inv=1.0d0/(ljxm-xm)
<           deltasq_inv=delta_inv*delta_inv
<           fac=Ht*hd1-ljm*hd2
<           fac1=deltasq_inv*fac*(ljxm-rij)
<           fac2=deltasq_inv*fac*(rij-xm)
<           ed=delta_inv*(ljm*hd2-Ht*hd1)
<           eij=eij*sss
<           ed=ed+eij/sss*sssgrad/sigma(itypi,itypj)*rij
<           eom1=fac1*d_xm(1)+fac2*d_ljxm(1)+h2*d_ljm(1)
<           eom2=fac1*d_xm(2)+fac2*d_ljxm(2)+h2*d_ljm(2)
<           eom12=fac1*d_xm(3)+fac2*d_ljxm(3)+h2*d_ljm(3)
<         endif
< c-------END FIRST METHOD, DISCONTINUOUS SECOND DERIVATIVE
< 
< c-------SECOND METHOD, CONTINUOUS SECOND DERIVATIVE
< c$$$        ssd=rij-ssXs
< c$$$        ljd=rij-ljXs
< c$$$        fac1=rij-ljxm
< c$$$        fac2=rij-ssxm
< c$$$
< c$$$        d_ljB(1)=ljB*(eps2rt_om1/eps2rt-2.0d0*alf1/eps3rt)
< c$$$        d_ljB(2)=ljB*(eps2rt_om2/eps2rt+2.0d0*alf2/eps3rt)
< c$$$        d_ljB(3)=ljB*(eps1_om12+eps2rt_om12/eps2rt-2.0d0*alf12/eps3rt)
< c$$$
< c$$$        ssm=ssC-0.25D0*ssB*ssB/ssA
< c$$$        d_ssm(1)=0.5D0*akct*ssB/ssA
< c$$$        d_ssm(2)=2.0D0*akth*deltat2-om1*omega-d_ssm(1)
< c$$$        d_ssm(1)=-2.0D0*akth*deltat1-om2*omega+d_ssm(1)
< c$$$        d_ssm(3)=omega
< c$$$
< c$$$        ljm=-0.25D0*bb(itypi,itypj)/aa(itypi,itypj)
< c$$$        do k=1,3
< c$$$          d_ljm(k)=ljm*d_ljB(k)
< c$$$        enddo
< c$$$        ljm=ljm*ljB
< c$$$
< c$$$        ss=ssA*ssd*ssd+ssB*ssd+ssC
< c$$$        d_ss(0)=2.0d0*ssA*ssd+ssB
< c$$$        d_ss(2)=akct*ssd
< c$$$        d_ss(1)=-d_ss(2)-2.0d0*akth*deltat1-om2*omega
< c$$$        d_ss(2)=d_ss(2)+2.0d0*akth*deltat2-om1*omega
< c$$$        d_ss(3)=omega
< c$$$
< c$$$        ljf=bb(itypi,itypj)/aa(itypi,itypj)
< c$$$        ljf=9.0d0*ljf*(-0.5d0*ljf)**(1.0d0/3.0d0)
< c$$$        d_ljf(0)=ljf*2.0d0*ljB*fac1
< c$$$        do k=1,3
< c$$$          d_ljf(k)=d_ljm(k)+ljf*(d_ljB(k)*fac1*fac1-
< c$$$     &         2.0d0*ljB*fac1*d_ljxm(k))
< c$$$        enddo
< c$$$        ljf=ljm+ljf*ljB*fac1*fac1
< c$$$
< c$$$        f1=(rij-ljxm)/(ssxm-ljxm)
< c$$$        f2=(rij-ssxm)/(ljxm-ssxm)
< c$$$        h1=h_base(f1,hd1)
< c$$$        h2=h_base(f2,hd2)
< c$$$        eij=ss*h1+ljf*h2
< c$$$        delta_inv=1.0d0/(ljxm-ssxm)
< c$$$        deltasq_inv=delta_inv*delta_inv
< c$$$        fac=ljf*hd2-ss*hd1
< c$$$        ed=d_ss(0)*h1+d_ljf(0)*h2+delta_inv*fac
< c$$$        eom1=d_ss(1)*h1+d_ljf(1)*h2+deltasq_inv*fac*
< c$$$     &       (fac1*d_ssxm(1)-fac2*(d_ljxm(1)))
< c$$$        eom2=d_ss(2)*h1+d_ljf(2)*h2+deltasq_inv*fac*
< c$$$     &       (fac1*d_ssxm(2)-fac2*(d_ljxm(2)))
< c$$$        eom12=d_ss(3)*h1+d_ljf(3)*h2+deltasq_inv*fac*
< c$$$     &       (fac1*d_ssxm(3)-fac2*(d_ljxm(3)))
< c$$$
< c$$$        havebond=.false.
< c$$$        if (ed.gt.0.0d0) havebond=.true.
< c-------END SECOND METHOD, CONTINUOUS SECOND DERIVATIVE
< 
<       endif
< 
< c      write (iout,*) "havebond",havebond
< c      call flush(iout)
<       if (havebond) then
< #ifndef CLUST
< #ifndef WHAM
< c        if (dyn_ssbond_ij(i,j).eq.1.0d300) then
< c          write(iout,'(a15,f12.2,f8.1,2i5)')
< c     &         "SSBOND_E_FORM",totT,t_bath,i,j
< c        endif
< #endif
< #endif
<         dyn_ssbond_ij(ici,icj)=eij
<       else if (.not.havebond .and. dyn_ssbond_ij(ici,icj).lt.1.0d300)
<      &then
<         dyn_ssbond_ij(ici,icj)=1.0d300
< #ifndef CLUST
< #ifndef WHAM
< c        write(iout,'(a15,f12.2,f8.1,2i5)')
< c     &       "SSBOND_E_BREAK",totT,t_bath,i,j
< #endif
< #endif
<       endif
< 
< c-------TESTING CODE
<       if (checkstop) then
<         if (jcheck.eq.0) write(iout,'(a,3f15.8,$)')
<      &       "CHECKSTOP",rij,eij,ed
<         echeck(jcheck)=eij
<       endif
<       enddo
<       if (checkstop) then
<         write(iout,'(f15.8)')(echeck(1)-echeck(-1))*0.5d0/deps
<       endif
<       enddo
<       if (checkstop) then
<         transgrad=.true.
<         checkstop=.false.
<       endif
< c-------END TESTING CODE
<             gg_lipi(3)=ssgradlipi*eij
<             gg_lipj(3)=ssgradlipj*eij
< 
<       do k=1,3
<         dcosom1(k)=(dc_norm(k,nres+i)-om1*erij(k))/rij
<         dcosom2(k)=(dc_norm(k,nres+j)-om2*erij(k))/rij
<       enddo
<       do k=1,3
<         gg(k)=ed*erij(k)+eom1*dcosom1(k)+eom2*dcosom2(k)
<       enddo
<       do k=1,3
<         gvdwx(k,i)=gvdwx(k,i)-gg(k)+gg_lipi(k)
<      &       +(eom12*(dc_norm(k,nres+j)-om12*dc_norm(k,nres+i))
<      &       +eom1*(erij(k)-om1*dc_norm(k,nres+i)))*dsci_inv
<         gvdwx(k,j)=gvdwx(k,j)+gg(k)+gg_lipj(k)
<      &       +(eom12*(dc_norm(k,nres+i)-om12*dc_norm(k,nres+j))
<      &       +eom2*(erij(k)-om2*dc_norm(k,nres+j)))*dscj_inv
<       enddo
< cgrad      do k=i,j-1
< cgrad        do l=1,3
< cgrad          gvdwc(l,k)=gvdwc(l,k)+gg(l)
< cgrad        enddo
< cgrad      enddo
< 
<       do l=1,3
<         gvdwc(l,i)=gvdwc(l,i)-gg(l)+gg_lipi(l)
<         gvdwc(l,j)=gvdwc(l,j)+gg(l)+gg_lipj(l)
<       enddo
< 
<       return
<       end
< 
< C-----------------------------------------------------------------------------
< 
583a128
>       implicit none
590a136
>       integer ierr
610a157
>       double precision buf_dyn_ssbond_ij(max_cyst,max_cyst)
612c159,183
<      &                         g_newihpb,g_newjhpb
---
>      &                         g_newihpb,g_newjhpb,
>      &                         buf_dyn_ssbond_ij
> #ifdef MPI
>       ! Reduce here dyn_ss_bond_ij instead of newihpb, newjhpb later
>       ! and calculate nss on king
>       ! because calculating allflag separately in different processes
>       ! leads to different results; eg. for pairs 1,2; 2,3; 3,4;
>       ! we have:
>       ! 1 process: add 1,2, exclude 2,3, add 3,4
>       !   result: 1,2; 3;4
>       ! 2 processes: 1,2 calucalted on process a, 2,3 and 3,4 calculated
>       ! on proceess b
>       !   process a: add 1,2
>       !   process b: add 2,3, exclude 3,4
>       !   result: 1,2; 2,3
>       if (nfgtasks.gt.1) then
>         call MPI_Reduce(dyn_ssbond_ij(1:ns,1:ns),
>      &    buf_dyn_ssbond_ij(1:ns,1:ns),ns*ns,
>      &    MPI_DOUBLE_PRECISION,MPI_MIN,king,FG_COMM,ierr)
>         if (fg_rank.eq.king) then
>           dyn_ssbond_ij(1:ns,1:ns)=buf_dyn_ssbond_ij(1:ns,1:ns)
>         endif
>       endif
>       if (fg_rank.eq.king) then
> #endif
661,662c232,261
< #ifdef MPI
<       if (nfgtasks.gt.1)then
---
> !#ifdef MPI
> !      if (nfgtasks.gt.1)then
> !
> !        call MPI_Reduce(newnss,g_newnss,1,
> !     &    MPI_INTEGER,MPI_SUM,king,FG_COMM,IERR)
> !        call MPI_Gather(newnss,1,MPI_INTEGER,
> !     &                  i_newnss,1,MPI_INTEGER,king,FG_COMM,IERR)
> !        displ(0)=0
> !        displ(1)=displ(1-1)
> !        !do i=1,nfgtasks-1,1
> !        do i=2,nfgtasks-1,1
> !          displ(i)=i_newnss(i-1)+displ(i-1)
> !        enddo
> !        call MPI_Gatherv(newihpb,newnss,MPI_INTEGER,
> !     &                   g_newihpb,i_newnss,displ,MPI_INTEGER,
> !     &                   king,FG_COMM,IERR)
> !        call MPI_Gatherv(newjhpb,newnss,MPI_INTEGER,
> !     &                   g_newjhpb,i_newnss,displ,MPI_INTEGER,
> !     &                   king,FG_COMM,IERR)
> !        if(fg_rank.eq.0) then
> !c         print *,'g_newnss',g_newnss
> !c         print *,'g_newihpb',(g_newihpb(i),i=1,g_newnss)
> !c         print *,'g_newjhpb',(g_newjhpb(i),i=1,g_newnss)
> !         newnss=g_newnss
> !         do i=1,newnss
> !          newihpb(i)=g_newihpb(i)
> !          newjhpb(i)=g_newjhpb(i)
> !         enddo
> !        endif
> !#endif
664,687c263,265
<         call MPI_Reduce(newnss,g_newnss,1,
<      &    MPI_INTEGER,MPI_SUM,king,FG_COMM,IERR)
<         call MPI_Gather(newnss,1,MPI_INTEGER,
<      &                  i_newnss,1,MPI_INTEGER,king,FG_COMM,IERR)
<         displ(0)=0
<         do i=1,nfgtasks-1,1
<           displ(i)=i_newnss(i-1)+displ(i-1)
<         enddo
<         call MPI_Gatherv(newihpb,newnss,MPI_INTEGER,
<      &                   g_newihpb,i_newnss,displ,MPI_INTEGER,
<      &                   king,FG_COMM,IERR)
<         call MPI_Gatherv(newjhpb,newnss,MPI_INTEGER,
<      &                   g_newjhpb,i_newnss,displ,MPI_INTEGER,
<      &                   king,FG_COMM,IERR)
<         if(fg_rank.eq.0) then
< c         print *,'g_newnss',g_newnss
< c         print *,'g_newihpb',(g_newihpb(i),i=1,g_newnss)
< c         print *,'g_newjhpb',(g_newjhpb(i),i=1,g_newnss)
<          newnss=g_newnss
<          do i=1,newnss
<           newihpb(i)=g_newihpb(i)
<           newjhpb(i)=g_newjhpb(i)
<          enddo
<         endif
---
> #ifdef MPI
>       else
>         newnss=0
1998,2144d1575
<       subroutine triple_ssbond_ene(resi,resj,resk,eij)
<       include 'DIMENSIONS'
<       include 'COMMON.SBRIDGE'
<       include 'COMMON.CHAIN'
<       include 'COMMON.DERIV'
<       include 'COMMON.LOCAL'
<       include 'COMMON.INTERACT'
<       include 'COMMON.VAR'
<       include 'COMMON.IOUNITS'
<       include 'COMMON.CALC'
< #ifndef CLUST
< #ifndef WHAM
<       include 'COMMON.MD'
< #endif
< #endif
< 
< c     External functions
<       double precision h_base
<       external h_base
< 
< c     Input arguments
<       integer resi,resj,resk
< 
< c     Output arguments
<       double precision eij,eij1,eij2,eij3
< 
< c     Local variables
<       logical havebond
< c      integer itypi,itypj,k,l
<       double precision rrij,ssd,deltat1,deltat2,deltat12,cosphi
<       double precision rrik,rrjk,rik,rjk,xi,xk,yi,yk,zi,zk,xij,yij,zij
<       double precision xik,yik,zik,xjk,yjk,zjk
<       double precision sig0ij,ljd,sig,fac,e1,e2
<       double precision dcosom1(3),dcosom2(3),ed
<       double precision pom1,pom2
<       double precision ljA,ljB,ljXs
<       double precision d_ljB(1:3)
<       double precision ssA,ssB,ssC,ssXs
<       double precision ssxm,ljxm,ssm,ljm
<       double precision d_ssxm(1:3),d_ljxm(1:3),d_ssm(1:3),d_ljm(1:3)
<       if (dtriss.eq.0) return
<       i=resi
<       j=resj
<       k=resk
< C      write(iout,*) resi,resj,resk
<       itypi=itype(i)
<       dxi=dc_norm(1,nres+i)
<       dyi=dc_norm(2,nres+i)
<       dzi=dc_norm(3,nres+i)
<       dsci_inv=vbld_inv(i+nres)
<       xi=c(1,nres+i)
<       yi=c(2,nres+i)
<       zi=c(3,nres+i)
< 
<       itypj=itype(j)
<       xj=c(1,nres+j)
<       yj=c(2,nres+j)
<       zj=c(3,nres+j)
< 
<       dxj=dc_norm(1,nres+j)
<       dyj=dc_norm(2,nres+j)
<       dzj=dc_norm(3,nres+j)
<       dscj_inv=vbld_inv(j+nres)
<       itypk=itype(k)
<       xk=c(1,nres+k)
<       yk=c(2,nres+k)
<       zk=c(3,nres+k)
< 
<       dxk=dc_norm(1,nres+k)
<       dyk=dc_norm(2,nres+k)
<       dzk=dc_norm(3,nres+k)
<       dscj_inv=vbld_inv(k+nres)
<       xij=xj-xi
<       xik=xk-xi
<       xjk=xk-xj
<       yij=yj-yi
<       yik=yk-yi
<       yjk=yk-yj
<       zij=zj-zi
<       zik=zk-zi
<       zjk=zk-zj
<       rrij=(xij*xij+yij*yij+zij*zij)
<       rij=dsqrt(rrij)  ! sc_angular needs rij to really be the inverse
<       rrik=(xik*xik+yik*yik+zik*zik)
<       rik=dsqrt(rrik)
<       rrjk=(xjk*xjk+yjk*yjk+zjk*zjk)
<       rjk=dsqrt(rrjk)
< C there are three combination of distances for each trisulfide bonds
< C The first case the ith atom is the center
< C Energy function is E=d/(a*(x-y)**2+b*(x+y)**2+c) where x is first
< C distance y is second distance the a,b,c,d are parameters derived for
< C this problem d parameter was set as a penalty currenlty set to 1.
<       eij1=dtriss/(atriss*(rij-rik)**2+btriss*(rij+rik)**2+ctriss)
< C second case jth atom is center
<       eij2=dtriss/(atriss*(rij-rjk)**2+btriss*(rij+rjk)**2+ctriss)
< C the third case kth atom is the center
<       eij3=dtriss/(atriss*(rik-rjk)**2+btriss*(rik+rjk)**2+ctriss)
< C      eij2=0.0
< C      eij3=0.0
< C      eij1=0.0
<       eij=eij1+eij2+eij3
< C      write(iout,*)i,j,k,eij
< C The energy penalty calculated now time for the gradient part
< C derivative over rij
<       fac=-eij1**2/dtriss*(2.0*atriss*(rij-rik)+2.0*btriss*(rij+rik))
<      &-eij2**2/dtriss*(2.0*atriss*(rij-rjk)+2.0*btriss*(rij+rjk))
<             gg(1)=xij*fac/rij
<             gg(2)=yij*fac/rij
<             gg(3)=zij*fac/rij
<       do m=1,3
<         gvdwx(m,i)=gvdwx(m,i)-gg(m)
<         gvdwx(m,j)=gvdwx(m,j)+gg(m)
<       enddo
<       do l=1,3
<         gvdwc(l,i)=gvdwc(l,i)-gg(l)
<         gvdwc(l,j)=gvdwc(l,j)+gg(l)
<       enddo
< C now derivative over rik
<       fac=-eij1**2/dtriss*(-2.0*atriss*(rij-rik)+2.0*btriss*(rij+rik))
<      &-eij3**2/dtriss*(2.0*atriss*(rik-rjk)+2.0*btriss*(rik+rjk))
<             gg(1)=xik*fac/rik
<             gg(2)=yik*fac/rik
<             gg(3)=zik*fac/rik
<       do m=1,3
<         gvdwx(m,i)=gvdwx(m,i)-gg(m)
<         gvdwx(m,k)=gvdwx(m,k)+gg(m)
<       enddo
<       do l=1,3
<         gvdwc(l,i)=gvdwc(l,i)-gg(l)
<         gvdwc(l,k)=gvdwc(l,k)+gg(l)
<       enddo
< C now derivative over rjk
<       fac=-eij2**2/dtriss*(-2.0*atriss*(rij-rjk)+2.0*btriss*(rij+rjk))-
<      &eij3**2/dtriss*(-2.0*atriss*(rik-rjk)+2.0*btriss*(rik+rjk))
<             gg(1)=xjk*fac/rjk
<             gg(2)=yjk*fac/rjk
<             gg(3)=zjk*fac/rjk
<       do m=1,3
<         gvdwx(m,j)=gvdwx(m,j)-gg(m)
<         gvdwx(m,k)=gvdwx(m,k)+gg(m)
<       enddo
<       do l=1,3
<         gvdwc(l,j)=gvdwc(l,j)-gg(l)
<         gvdwc(l,k)=gvdwc(l,k)+gg(l)
<       enddo
<       return
<       end
stochfric.F
50a51
> #ifdef VIRTUAL
52d52
<       d_t_work(:6*nres)=0.0d0
68a69
> #endif
87a89
> #ifdef VIRTUAL
88a91,93
> #else
>             vvec(ind+1)=vel(j,i)
> #endif
90a96
> #ifdef VIRTUAL
91a98,100
> #else
>               vvec(ind+1)=vel(j,i+nres)
> #endif
130a140
> #undef VIRTUAL
131a142
>       d_t_work(:6*nres)=0.0d0
567a579,581
> #ifdef WITH_CUDA
>       logical uses_cuda
> #endif
842a857,861
> #endif
> #ifdef WITH_CUDA
>       if (uses_cuda()) then
>         call push_DMfric_to_accelerator(DMfric,DU1fric,DU2fric,2*nres)
>       endif
sumsld.f
surfatom.f
test.F
27c27
<       call chainbuild
---
>       call chainbuild_extconf
48c48
<        call chainbuild
---
>        call chainbuild_extconf
61c61
<        call chainbuild
---
>        call chainbuild_extconf
91c91
<         call chainbuild
---
>         call chainbuild_extconf
149c149
<       call chainbuild
---
>       call chainbuild_extconf
189c189
<        call chainbuild
---
>        call chainbuild_extconf
212c212
<       call chainbuild
---
>       call chainbuild_extconf
228c228
<       call chainbuild
---
>       call chainbuild_extconf
252c252
<       call chainbuild
---
>       call chainbuild_extconf
911c911
<        call chainbuild
---
>        call chainbuild_extconf
1003c1003
<        call chainbuild
---
>        call chainbuild_extconf
1135c1135
<        call chainbuild
---
>        call chainbuild_extconf
1203c1203
<        call chainbuild
---
>        call chainbuild_extconf
1287c1287
<       call chainbuild
---
>       call chainbuild_extconf
1320c1320
<       call chainbuild
---
>       call chainbuild_extconf
1351c1351
<        call chainbuild
---
>        call chainbuild_extconf
1386a1387
> #ifdef VIRTUAL
1387a1389,1391
> #else
>       call calculate_dC
> #endif
thread.F
timing.F
71a72,74
>       time_omp_sum=0.0d0
>       time_cuda_turn=0.0d0
>       time_dynss=0.0d0
73a77,78
>       time_emodeller=0.0d0
>       time_saxs_list=0.0d0
76a82,88
>       time_contact=0.0d0
>       time_ess=0.0d0
>       time_reset_moment=0.0d0
>       time_statout=0.0d0
>       time_rst=0.0d0
>       time_cuda_init=0.0d0
> 
292a305,316
>       double precision function timer_value()
>       implicit none
> #ifdef MPI
>       include "mpif.h"
>       timer_value=MPI_Wtime()
> #else
>       double precision tcpu
>       timer_value=tcpu()
> #endif
>       return
>       end
> C---------------------------------------------------------------------------
356a381,388
>          write (*,*) "Processor",fg_rank,myrank," init",
>      &     time_initialization
>          write (*,*) "Processor",fg_rank,myrank,"   memclear",
>      &     time_memclear
> #ifdef WITH_CUDA
>          write (*,*) "Processor",fg_rank,myrank,"   cuda init",
>      &     time_cuda_init
> #endif
375a408
>            write (*,*) "Processor",fg_rank,myrank,"  dynss",time_dynss
382a416,417
>            write (*,*) "Processor",fg_rank,myrank," cuda turn",
>      &      time_cuda_turn
411a447,448
>          write (*,*) "Processor",fg_rank,myrank," SAXSlist",
>      &     time_saxs_list
412a450,452
>          if(time_emodeller.gt.0.0d0)
>      &     write (*,*) "Processor",fg_rank,myrank," emodeller",
>      &       time_emodeller
425a466,479
>          write (*,*) "Processor",fg_rank,myrank," ompsumgrad",
>      &     time_omp_sum
>          if(time_contact.gt.0.0d0)
>      &     write (*,*) "Processor",fg_rank,myrank," contacts",
>      &       time_contact
>          if(time_reset_moment.gt.0.0d0)
>      &     write (*,*) "Processor",fg_rank,myrank," reset_mom",
>      &       time_reset_moment
>          if(time_statout.gt.0.0d0)
>      &     write (*,*) "Processor",fg_rank,myrank," statout",
>      &       time_statout
>          if(time_rst.gt.0.0d0)
>      &     write (*,*) "Processor",fg_rank,myrank," rst",
>      &       time_rst
426a481,485
>          write (*,*) "init",time_initialization
>          write (*,*) "  memclear",time_memclear
> #ifdef WITH_CUDA
>          write (*,*) "  cuda init",time_cuda_init
> #endif
427a487,490
>          write (*,*) "  evdw",time_evdw
>          write (*,*) "    dynss",time_dynss
>          write (*,*) "  eelec",time_eelec
>          write (*,*) "  escp",time_escp
442a506
>          write (*,*) "SAXSlist",time_saxs_list
443a508,517
>          if(time_emodeller.gt.0.0d0)
>      &     write (*,*) "emodeller",time_emodeller
>          if(time_contact.gt.0.0d0)
>      &     write (*,*) "contacts",time_contact
>          if(time_reset_moment.gt.0.0d0)
>      &     write (*,*) "reset_mom",time_reset_moment
>          if(time_statout.gt.0.0d0)
>      &     write (*,*) "statout",time_statout
>          if(time_rst.gt.0.0d0)
>      &     write (*,*) "rst",time_rst
together.F
unres.F
35a36
>       include 'COMMON.OMP'
44a46
>       include 'COMMON.PENTADIAG'
47a50,52
> #ifdef WITH_CUDA
>       include 'COMMON.CUDA'
> #endif
68a74,76
> #ifdef TIMING
>       integer*8 time_init_start, time_init_end, time_init_rate
> #endif
71a80
>       logical uses_cuda
78a88,92
> #ifdef TIMING
>       ! use language-provided timer because nothing else is ready
>       call system_clock(time_init_start, time_init_rate)
> #endif
> 
87a102,104
> 
>       call init_omp
> 
98c115,116
<       call init_omp
---
>       call init_omp2
>       call init_saxs    ! must be before init_grids
100a119,133
>       call init_egb
>       call init_escp
> #ifdef WITH_CUDA
>       if (uses_cuda()) then
>         call init_accelerator
>       endif
> #endif
> 
> #ifdef TIMING
>       call system_clock(time_init_end)
>       time_initialization=
>      &  real(time_init_end-time_init_start,8)/time_init_rate
> #else
>       time_initialization=0.0
> #endif
129a163
> #ifdef VIRTUAL
130a165,167
> #else
>       if (indpdb.eq.0 .and. .not.read_cart) call calculate_dC
> #endif
175a213,217
> #ifdef WITH_CUDA
>       if (uses_cuda()) then
>         call finish_accelerator
>       endif
> #endif
184a227,231
> #ifdef WITH_CUDA
>       if (uses_cuda()) then
>         call finish_accelerator
>       endif
> #endif
195d241
<       double precision delta
203c249
<       include 'COMMON.FFIELD'
---
>       logical uses_cuda
207a254
> #ifdef VIRTUAL
208a256,258
> #else
>       call calculate_dC
> #endif
212a263,269
> #ifdef WITH_CUDA
>       if (uses_cuda()) then
>         call cuda_MD
>       else
>         call MD
>       endif
> #else
214,240d270
< #define CHECK_GRAD_AVE
< #ifdef CHECK_GRAD_AVE
<       write(iout,*),"Check ave gradient"
<       icheckgrad=1
<       aincr=1.0d-4
< #define ZERO_WEIGHTS
< #ifdef ZERO_WEIGHTS
<       wsc=0.0d0
<       wscp=0.0d0
<       welec=0.0d0
<       wcorr=0.0d0
<       wcorr5=0.0d0
<       wcorr6=0.0d0
<       wel_loc=0.0d0
<       wturn3=0.0d0
<       wturn4=0.0d0
<       wturn6=0.0d0
<       wang=0.0d0
<       wscloc=0.0d0
<       wtor=0.0d0
<       wtor_d=0.0d0
<       wvdwpp=0.0d0
<       wbond=0.0d0
< #endif
< #undef ZERO_WEIGHTS
<       call exec_checkgrad
<       write(iout,*)"End check ave gradient"
256d285
<       include 'COMMON.FFIELD'
259a289
> #ifdef VIRTUAL
260a291,293
> #else
>       call calculate_dC
> #endif
271,297d303
< #ifdef CHECK_MREMD_GRAD
<       write(iout,*),"Check MREMD ave gradient"
<       icheckgrad=1
<       aincr=1.0d-4
< #define ZERO_WEIGHTS
< #ifdef ZERO_WEIGHTS
<       wsc=0.0d0
<       wscp=0.0d0
<       welec=0.0d0
<       wcorr=0.0d0
<       wcorr5=0.0d0
<       wcorr6=0.0d0
<       wel_loc=0.0d0
<       wturn3=0.0d0
<       wturn4=0.0d0
<       wturn6=0.0d0
<       wang=0.0d0
<       wscloc=0.0d0
<       wtor=0.0d0
<       wtor_d=0.0d0
<       wvdwpp=0.0d0
<       wbond=0.0d0
< #endif
< #undef ZERO_WEIGHTS
<       call exec_checkgrad
<       write(iout,*)"End check MREMD ave gradient"
< #endif
348a355
> #ifdef VIRTUAL
349a357,359
> #else
>       if (indpdb.eq.0 .and. .not.read_cart) call calculate_dC
> #endif
355a366
> #ifdef VIRTUAL
356a368,370
> #else
>       call calculate_dC
> #endif
371a386
> #ifdef VIRTUAL
372a388,390
> #else
>         call calculate_dC
> #endif
690a709
> #ifdef VIRTUAL
691a711,713
> #else
>             call calculate_dC
> #endif
735a758
> #ifdef VIRTUAL
736a760,762
> #else
>             call calculate_dC
> #endif
770a797
> #ifdef VIRTUAL
771a799,801
> #else
>           call calculate_dC
> #endif
805a836
> #ifdef VIRTUAL
806a838,840
> #else
>             call calculate_dC
> #endif
840a875
> #ifdef VIRTUAL
841a877,879
> #else
>         call calculate_dC
> #endif
901a940
> #ifdef VIRTUAL
902a942,944
> #else
>             call calculate_dC
> #endif
957a1000
> #ifdef VIRTUAL
958a1002,1004
> #else
>       if (indpdb.eq.0) call calculate_dC
> #endif
1008a1055
> #ifdef VIRTUAL
1010a1058,1060
> #else
>       call calculate_dC
> #endif
1023,1024d1072
<           
<       write(iout,*),"before checking gradient icheckgrad",icheckgrad
1026,1029c1074
<   10  write(iout,*),"before call check_ecartint"
<       call check_ecartint
< !  10  call check_ecartint
<       write(iout,*),"after check_ecartint"
---
>   10  call check_ecartint
1068a1114
> #ifdef VIRTUAL
1069a1116,1118
> #else
>       call calculate_dC
> #endif
vec_and_deriv.F
30c30
<         fac=1.0d0/dsqrt(1.0d0-costh*costh)
---
>         fac=1.0d0/dsqrt(1.0d0-costh*costh) !fac=sinttab(i+2) ???
67,78c67,88
<         do j=1,2
<           do k=1,3
<             do l=1,3
<               uygrad(l,k,j,2,i)=uyder(l,k,j)
<               uzgrad(l,k,j,i)=uzder(l,k,j)
<             enddo
<           enddo
<         enddo 
<         call unormderiv(uy(1,2,i),uyder(1,1,1),facy,uygrad(1,1,1,2,i))
<         call unormderiv(uy(1,2,i),uyder(1,1,2),facy,uygrad(1,1,2,2,i))
<         call unormderiv(uz(1,i),uzder(1,1,1),fac,uzgrad(1,1,1,i))
<         call unormderiv(uz(1,i),uzder(1,1,2),fac,uzgrad(1,1,2,i))
---
>         ! TODO: NIEPOTRZEBNE?
>         !do j=1,2
>         !  do k=1,3
>         !    do l=1,3
>         !      uygrad(l,k,j,2,i)=uyder(l,k,j)
>         !      uzgrad(l,k,j,i)=uzder(l,k,j)
>         !    enddo
>         !  enddo
>         !enddo 
> !       call unormderiv(uy(1,2,i),uyder(1,1,1),facy,uygrad(1,1,1,2,i))
> !       call unormderiv(uy(1,2,i),uyder(1,1,2),facy,uygrad(1,1,2,2,i))
>         !TODO: dla i==nres-1 vbld_inv(i+2) to OOB
>         call unormderiv(uy(1,2,i),uyder(1,1,1),
>      &    facy*vbld_inv(i+1),uygrad(1,1,1,2,i))
>         call unormderiv(uy(1,2,i),uyder(1,1,2),
>      &    facy*vbld_inv(i+2),uygrad(1,1,2,2,i))
> !       call unormderiv(uz(1,i),uzder(1,1,1),fac,uzgrad(1,1,1,i))
> !       call unormderiv(uz(1,i),uzder(1,1,2),fac,uzgrad(1,1,2,i))
>         call unormderiv(uz(1,i),uzder(1,1,1),
>      &    fac*vbld_inv(i+1),uzgrad(1,1,1,i))
>         call unormderiv(uz(1,i),uzder(1,1,2),
>      &    fac*vbld_inv(i+2),uzgrad(1,1,2,i))
104,112c114,125
<         do j=1,2
<           do k=1,3
<             do l=1,3
<               uygrad(l,k,j,1,i)=uyder(l,k,j)
<             enddo
<           enddo
<         enddo 
<         call unormderiv(uy(1,1,i),uyder(1,1,1),facy,uygrad(1,1,1,1,i))
<         call unormderiv(uy(1,1,i),uyder(1,1,2),facy,uygrad(1,1,2,1,i))
---
>         ! TODO: NIEPOTRZEBNE?
>         !do j=1,2
>         !  do k=1,3
>         !    do l=1,3
>         !      uygrad(l,k,j,1,i)=uyder(l,k,j)
>         !    enddo
>         !  enddo
>         !enddo 
>         call unormderiv(uy(1,1,i),uyder(1,1,1),
>      &    facy*vbld_inv(i),uygrad(1,1,1,1,i))
>         call unormderiv(uy(1,1,i),uyder(1,1,2),
>      &    facy*vbld_inv(i+1),uygrad(1,1,2,1,i))
116,126c129,139
< !$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(vbld_inv_temp,j,k,l)
<       do i=1,nres-1
<         vbld_inv_temp(1)=vbld_inv(i)
<         vbld_inv_temp(2)=vbld_inv(i+1)
<         do j=1,2
<           do k=1,3
<             do l=1,3
<               uygrad(l,k,j,1,i)=vbld_inv_temp(j)*uygrad(l,k,j,1,i)
<             enddo
<           enddo
<         enddo
---
> !!$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(vbld_inv_temp,j,k,l)
> !      do i=1,nres-1
> !       vbld_inv_temp(1)=vbld_inv(i)
> !       vbld_inv_temp(2)=vbld_inv(i+1)
> !       do j=1,2
> !         do k=1,3
> !           do l=1,3
> !             uygrad(l,k,j,1,i)=vbld_inv_temp(j)*uygrad(l,k,j,1,i)
> !           enddo
> !         enddo
> !       enddo
129,137c142,150
<         vbld_inv_temp(1)=vbld_inv(i+1)
<         vbld_inv_temp(2)=vbld_inv(i+2)
<         do j=1,2
<           do k=1,3
<             do l=1,3
<               uzgrad(l,k,j,i)=vbld_inv_temp(j)*uzgrad(l,k,j,i)
<             enddo
<           enddo
<         enddo
---
> !       vbld_inv_temp(1)=vbld_inv(i+1)
> !       vbld_inv_temp(2)=vbld_inv(i+2)
> !       do j=1,2
> !         do k=1,3
> !           do l=1,3
> !             uzgrad(l,k,j,i)=vbld_inv_temp(j)*uzgrad(l,k,j,i)
> !           enddo
> !         enddo
> !       enddo
142,149c155,162
<         do j=1,2
<           do k=1,3
<             do l=1,3
<               uygrad(l,k,j,2,i)=vbld_inv_temp(j)*uygrad(l,k,j,2,i)
<             enddo
<           enddo
<         enddo
<       enddo
---
> !       do j=1,2
> !         do k=1,3
> !           do l=1,3
> !             uygrad(l,k,j,2,i)=vbld_inv_temp(j)*uygrad(l,k,j,2,i)
> !           enddo
> !         enddo
> !       enddo
> !     enddo
wham_calc1.F
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/wham_calc1.F: No such file or directory
xlinks.F
164c164,395
<       double precision function mytschebyshev(m,n,x,y,yt)
---
>       subroutine Xlinkene2(i,j,iltyp,ir,eij,dgci,dgcj,dgxi,dgxj)
>       implicit none
> #ifdef MPI
>       include "mpif.h"
> #endif
>       include 'DIMENSIONS'
>       include 'COMMON.CONTROL'
>       include 'COMMON.XLINKS'
>       include 'COMMON.CHAIN'
>       include 'COMMON.DERIV'
>       include 'COMMON.LOCAL'
>       include 'COMMON.INTERACT'
>       include 'COMMON.VAR'
>       include 'COMMON.IOUNITS'
>       include 'COMMON.GEO'
>       double precision dgci(3),dgcj(3),dgxi(3),dgxj(3)
>       double precision erij(3),dcosom1(3),dcosom2(3),gg(3)
>       integer i,j,iltyp,k,l,nbi,ir
>       double precision eij
>       double precision dxi,dyi,dzi,dxj,dyj,dzj,xi,yi,zi,xj,yj,zj,rij,
>      & rrij,dsci_inv,dscj_inv,cosom1,sinom1,cosom2,sinom2,cosom12,
>      & cosphi,edij,dedij,et1ij,det1ij,det1t2ij,
>      & et2ij,det2ij,ephiij,dephiij,usum,usumder,uprod,uprod1,diff,
>      & cosaux,sinaux,ud(maxxlink),u(maxxlink),usumsqder,uprod2
>       double precision mytschebyshev
> #ifdef DEBUG
>       write (iout,*) "Xlinkene",i,j,iltyp
> #endif
> #ifdef EDISLOG
>       write(*,'(a,4i4)')'XEa',i,j,iltyp,ir
> #endif
>       dxi=dc_norm(1,nres+i)
>       dyi=dc_norm(2,nres+i)
>       dzi=dc_norm(3,nres+i)
>       dxj=dc_norm(1,nres+j)
>       dyj=dc_norm(2,nres+j)
>       dzj=dc_norm(3,nres+j)
> #ifdef EDISLOG
>       write(*,'(a,2i4,3f18.12)')'XEb',i,j,dxi,dyi,dzi
> #endif
> #ifdef EDISLOG
>       write(*,'(a,2i4,3f18.12)')'XEc',i,j,dxj,dyj,dzj
> #endif
>       xi=c(1,i)+dc(1,nres+i)*rxlink(1,iltyp)
>       yi=c(2,i)+dc(2,nres+i)*rxlink(1,iltyp)
>       zi=c(3,i)+dc(3,nres+i)*rxlink(1,iltyp)
> c      write (iout,*) xi,yi,zi
>       xj=c(1,j)+dc(1,nres+j)*rxlink(2,iltyp)-xi
>       yj=c(2,j)+dc(2,nres+j)*rxlink(2,iltyp)-yi
>       zj=c(3,j)+dc(3,nres+j)*rxlink(2,iltyp)-zi
> c      write (iout,*) xj+xi,yj+yi,zj+zi
> #ifdef EDISLOG
>       write(*,'(a,2i4,3f18.12)')'XEd1',i, j, xi, yi, zi
>       write(*,'(a,2i4,3f18.12)')'XEd2',i, j, xj, yj, zj
>       write(*,'(a,2i4,3f18.12)')'XEd3',i, j, dc(:,nres+i)
>       write(*,'(a,2i4,3f18.12)')'XEd4',i, j, dc(:,nres+j)
>       write(*,'(a,2i4,2f18.12)')'XEd5',i, j,
>      &   rxlink(1,iltyp),rxlink(2,iltyp)
> #endif
>       dsci_inv=vbld_inv(nres+i)!/rxlink(1,iltyp)
>       dscj_inv=vbld_inv(nres+j)!/rxlink(2,iltyp)
> #ifdef EDISLOG
>       write(*,'(a,2i4,2f18.12)')'XEe',i,j,dsci_inv,dscj_inv
> #endif
>       rrij=1.0D0/(xj*xj+yj*yj+zj*zj)
>       rij=dsqrt(rrij)
> #ifdef EDISLOG
>       write(*,'(a,2i4,2f18.12)')'XEf',i,j,rrij,rij
> #endif
>       erij(1)=xj*rij
>       erij(2)=yj*rij
>       erij(3)=zj*rij
> #ifdef EDISLOG
>       write(*,'(a,2i4,3f18.12)')'XEg',i,j,erij(:)
> #endif
>       cosom1=dxi*erij(1)+dyi*erij(2)+dzi*erij(3)
>       cosom2=dxj*erij(1)+dyj*erij(2)+dzj*erij(3)
>       sinom1=dsqrt(1.0d0-cosom1*cosom1)
>       sinom2=dsqrt(1.0d0-cosom2*cosom2)
>       cosom12=dxi*dxj+dyi*dyj+dzi*dzj
>       do k=1,3
>         dcosom1(k)=rij*(dc_norm(k,nres+i)-cosom1*erij(k))
>         dcosom2(k)=rij*(dc_norm(k,nres+j)-cosom2*erij(k))
>       enddo
> #ifdef EDISLOG
>       write(*,'(a,2i4,3f18.12)')'XEh1',i,j,dcosom1(:)
>       write(*,'(a,2i4,3f18.12)')'XEh2',i,j,dcosom2(:)
> #endif
>       rij=1.0d0/rij
>       nbi=nbxlink(iltyp)
> #ifdef EDISLOG
>       write(*,'(a,3i4,1f18.12)')'XEi',i,j,nbi,rij
> #endif
>       if (nbi.eq.1) then
>         diff=rij-dxlink(1,iltyp)
>         dedij=aklink(1,iltyp)*diff
>         edij=0.5d0*dedij*diff
>       else
>         do k=1,nbi
>           diff=rij-dxlink(k,iltyp)
>           ud(k)=aklink(k,iltyp)*diff
>           u(k)=alink(k,iltyp)+0.5d0*ud(k)*diff
>         enddo
>         uprod=u(1)
>         do k=2,nbi
>           uprod=uprod*u(k)
>         enddo
>         usum=0.0d0
>         usumsqder=0.0d0
>         do k=1,nbi
>           uprod1=1.0d0
>           uprod2=1.0d0
>           do l=1,nbi
>             if (l.ne.k) then
>               uprod1=uprod1*u(l)
>               uprod2=uprod2*u(l)*u(l)
>             endif
>           enddo
>           usum=usum+uprod1
>           usumsqder=usumsqder+ud(k)*uprod2
>         enddo
>         edij=uprod/usum
>         dedij=usumsqder/(usum*usum)
>       endif
>       et1ij=v0tlink(1,iltyp)
> #ifdef EDISLOG
>       write(*,'(a,2i4,3f18.12)')'XEj',i,j,edij,dedij,et1ij
> #endif
> c      write (iout,*) "V0t1",v0tlink(1,iltyp)
>       det1ij=0.0d0
>       cosaux=1.0d0
>       sinaux=1.0d0
>       do k=1,ntxlink(1,iltyp)
>         det1ij=det1ij-k*axlink(k,1,iltyp)*cosaux
>      &               -k*bxlink(k,1,iltyp)*sinaux*cosom1/sinom1
>         cosaux=-cosaux*cosom1
>         sinaux=sinaux*sinom1
>         et1ij=et1ij+axlink(k,1,iltyp)*cosaux+bxlink(k,1,iltyp)*sinaux
> #ifdef EDISLOG
>         write(*,'(a,3i4,4f18.12)')'XEk',i,j,k,
>      &    axlink(k,1,iltyp),bxlink(k,1,iltyp),det1ij,et1ij
> #endif
>       enddo
>       et2ij=v0tlink(2,iltyp)
> c      write (iout,*) "V0t2",v0tlink(2,iltyp)
>       det2ij=0.0d0
>       cosaux=1.0d0
>       sinaux=1.0d0
>       do k=1,ntxlink(2,iltyp)
>         det2ij=det2ij+k*axlink(k,2,iltyp)*cosaux
>      &               -k*bxlink(k,2,iltyp)*sinaux*cosom2/sinom2
>         cosaux=cosaux*cosom2
>         sinaux=sinaux*sinom2
>         et2ij=et2ij+axlink(k,2,iltyp)*cosaux+bxlink(k,2,iltyp)*sinaux
> #ifdef EDISLOG
>         write(*,'(a,3i4,4f18.12)')'XEl',i,j,k,
>      &    axlink(k,2,iltyp),bxlink(k,2,iltyp),det2ij,et2ij
> #endif
>       enddo
>       sinaux=sinom1*sinom2
>       cosphi=cosom12-cosom1*cosom2
>       ephiij=v0glink(iltyp)+
>      &   mytschebyshev(1,ngxlink(iltyp),vglink(1,iltyp),cosphi,sinaux)
>       call mygradtschebyshev(1,ngxlink(iltyp),vglink(1,iltyp),
>      &   cosphi,sinaux,dephiij,det1t2ij)
> #ifdef EDISLOG
>         write(*,'(a,3i4,3f18.12)')'XEm',i,j,ngxlink(iltyp),
>      &    ephiij,dephiij,det1t2ij
> #endif
> c      edij=0.0d0
> c      dedij=0.0d0
> c      et1ij=0.0d0
> c      et2ij=0.0d0
> c      ephiij=0.0d0
> c      det1ij=0.0d0
> c      det2ij=0.0d0
> c      dephiij=0.0d0
> c      det1t2ij=0.0d0
>       eij=edij+et1ij+et2ij+ephiij
> #ifdef DEBUG
>       write (iout,*) "rij",rij," cosom1",cosom1," cosom2",cosom2,
>      &" sinom1",sinom1," sinom2",sinom2,
>      &" theta1",180-rad2deg*dacos(cosom1),
>      &" theta2",rad2deg*dacos(cosom2)," cosom12",cosom12,
>      &" cosphi",cosphi,cosphi/(sinom1*sinom2),
>      &" gamma",rad2deg*dacos(cosphi/(sinom1*sinom2))
>       write (iout,*) "edij",edij," et1ij",et1ij," et2ij",et2ij,
>      &  " ephiij",ephiij
> #endif
>       if (energy_dec) write(iout,'(a6,2i5,f8.3,3f8.2,5f10.3,i5)') 
>      & "edisX",i,j,rij,180-rad2deg*dacos(cosom1),rad2deg*dacos(cosom2),
>      & rad2deg*dacos(cosphi/(sinom1*sinom2)),edij,et1ij,et2ij,ephiij,
>      & eij,ir
> c      write (iout,*) "ded",dedij
>       det1ij=det1ij-det1t2ij*sinom2*cosom1/sinom1-dephiij*cosom2
>       det2ij=det2ij-det1t2ij*sinom1*cosom2/sinom2-dephiij*cosom1
> #ifdef EDISLOG
>       write(*,'(a,2i4,3f18.12)')'XEn',i,j,eij,det1ij,det2ij
> #endif
>       do k=1,3
>         gg(k)=dedij*erij(k)+det1ij*dcosom1(k)+det2ij*dcosom2(k)
> !       ghpbx(k,i)=ghpbx(k,i)-ggk*rxlink(1,iltyp)
> !    &           +(dephiij*(dc_norm(k,nres+j)-cosom12*dc_norm(k,nres+i))
> !    &     +det1ij*(erij(k)-cosom1*dc_norm(k,nres+i)))
> !    &       *dsci_inv
>         dgxi(k)=dgxi(k)-gg(k)*rxlink(1,iltyp)
>      &           +(dephiij*(dc_norm(k,nres+j)-cosom12*dc_norm(k,nres+i))
>      &     +det1ij*(erij(k)-cosom1*dc_norm(k,nres+i)))
>      &       *dsci_inv
> 
> !       ghpbx(k,j)=ghpbx(k,j)+ggk*rxlink(2,iltyp)
> !    &           +(dephiij*(dc_norm(k,nres+i)-cosom12*dc_norm(k,nres+j))
> !    &     +det2ij*(erij(k)-cosom2*dc_norm(k,nres+j)))
> !    &       *dscj_inv
>         dgxj(k)=dgxj(k)+gg(k)*rxlink(2,iltyp)
>      &           +(dephiij*(dc_norm(k,nres+i)-cosom12*dc_norm(k,nres+j))
>      &     +det2ij*(erij(k)-cosom2*dc_norm(k,nres+j)))
>      &       *dscj_inv
>         !ghpbc(k,i)=ghpbc(k,i)-ggk
>         !ghpbc(k,j)=ghpbc(k,j)+ggk
>         dgci(k)=dgci(k)-gg(k)
>         dgcj(k)=dgcj(k)+gg(k)
>       enddo
> #ifdef EDISLOG
>       write(*,'(a,2i4,3f18.12)')'XEo',i,j,gg(:)
>       write(*,'(a,2i4,3f18.12)')'XEp',i,j,dgxi(:)
>       write(*,'(a,2i4,3f18.12)')'XEq',i,j,dgxj(:)
> #endif
>       return
>       end
> c------------------------------------------------------------------------------
>       double precision function mytschebyshev_prev(m,n,x,y,yt)
180a412,457
>       enddo
> c      print *,(yy(i),i=1,n)
>       mytschebyshev_prev=aux
>       return
>       end
> c------------------------------------------------------------------------------
>       double precision function mytschebyshev(m,n,x,y,yt)
>       implicit none
>       include "DIMENSIONS"
>       integer i,m,n
>       double precision x(n),y,yt,aux!,yy(0:maxvar)
>       double precision yy,yy1,yyp,yypp,t,yt2
> c Tschebyshev polynomial in y multiplied by sin(t1)sin(t2) (yt). 
> c Note that the first term is omitted
> c m=0: the constant term is included
> c m=1: the constant term is not included
>       !yy(0)=1.0d0
>       !yy(1)=y
>       !yy1=y
>       ! m == 0 -> out of bounds access to x(0)
> 
>       !yypp=1.0d0
>       !yyp=y
>       yyp=1.0d0
>       yy=y
>       yy1=2*y
>       yt2=yt*yt
> 
>       aux=0.0d0
>       !if(m.le.1) aux=aux+x(1)*yy1
>       if(m.le.1) aux=aux+x(1)*y
> 
>       !do i=2,n
>       do i=2,m-1
>         !yy(i)=2*yy1*yy(i-1)-yy(i-2)*yt*yt
>         yypp=yyp
>         yyp=yy
>         yy=yy1*yyp-yypp*yt2
>       enddo
>       do i=2,n
>         !yy(i)=2*yy1*yy(i-1)-yy(i-2)*yt*yt
>         yypp=yyp
>         yyp=yy
>         yy=yy1*yyp-yypp*yt2
>         !aux=aux+x(i)*yy(i)
>         aux=aux+x(i)*yy
DIMENSIONS
21,25c21
<       parameter (max_fg_threads=24)
< C Minimum number of items in a single chunk to process sequentially.
< C Smaller chunks will be merged into a non-sequential chunk,
<       integer simd_seq_cutoff
<       parameter (simd_seq_cutoff=3)
---
>       parameter (max_fg_threads=512)
26a23,24
> C  Comment out the "parameter (maxres=235000)" line and uncomment
> C  the "parameter (maxres=63500)" to test on a system with 16GB of RAM.
28,34c26,64
< !      parameter (maxres=155000)
< !      parameter (maxres=1770000)
<       parameter (maxres=2000)
< C !!!
<       double precision r_buff_list
< !      parameter (r_buff_list=5.0)
<       parameter (r_buff_list=0.5)
---
>       parameter (maxres=235000)
> !      parameter (maxres=63500)
> C Width of the additional space around Verlet buffer. Increasing this
> C parameter reduces number of interactions lists rebuils but increases
> C the work wasted on calculating interactions that are outside the cutoff
> C distance.
>       double precision default_r_buff_list
> !      parameter (default_r_buff_list=5.0d0)
> !      parameter (default_r_buff_list=0.5d0)
> !      parameter (default_r_buff_list=4.0d0)
>       parameter (default_r_buff_list=2.0d0)
> C Lower limit for r_buff_list value.
>       double precision smallest_r_buff_list
>       parameter (smallest_r_buff_list=0.20d0)
> C Change of r_buff_list in a single grid tining step.
>       double precision grid_cutoff_step
> !      parameter (grid_cutoff_step=0.1d0)
>       parameter (grid_cutoff_step=0.2d0)
> !      parameter (grid_cutoff_step=0.5d0)
> C Number of iterations to average over for determining if change of
> C r_buff_list made a run-time improvement.
>       integer grid_average_samples
>       parameter (grid_average_samples=20)
> C Maximum number of continuous interactions sheduled to "separate" path.
> C Eg. for sep_list_cutoff=3, (1,2), (1,3), (1,4) will be stored and
> C processed as 3 separate interactions while (1,2), (1,3), (1,4), (1,5)
> C will be stored and procesed as an interaction range (1,2-5).
> C This should be set based on the width of FP64 SIMD unit in the target
> C CPU. Eg. an AVX2 CPU has a width of 4 FP64 so sep_list_cutoff
> C should be set to 3. Ranges shorter than the SIMD width would waste
> C lanes. Separate path is a bit slower, but do not waste C lanes, eg.
> C interactions (1,2), (1,3), (1,7), (1,9) will be computed in parallel
> C on a "separate" path with a 4-wide SIMD, while the same interactions
> C scheduled to a "continuous path" would require 3 iterations:
> C [(1,2), (1,3)], [(1,7)], [(1,9)].
>       integer sep_list_cutoff
>       parameter (sep_list_cutoff=3)
> !     parameter (sep_list_cutoff=0)     ! only ranges lists
> !     parameter (sep_list_cutoff=300)   ! only separates lists
66,68d95
< C Max. number of NMR distance restraints
<       integer maxdim_nmr
<       parameter (maxdim_nmr=10000)
84c111
<       parameter (maxxlink=20)
---
>       parameter (maxxlink=25)
87c114
<       parameter (maxint_res=250)
---
>       parameter (maxint_res=300)
153c180
<       parameter (n_ene=31,n_ene2=2*n_ene)
---
>       parameter (n_ene=32,n_ene2=2*n_ene)
198c225
<       parameter (max_remd_m=64)
---
>       parameter (max_remd_m=4)
214c241
<       parameter(max_template=50)
---
>       parameter(max_template=10)
DIMENSIONS.PMF
COMMON.APOTHEKE
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/COMMON.APOTHEKE: No such file or directory
COMMON.AVNLOC
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/COMMON.AVNLOC: No such file or directory
COMMON.BANK
COMMON.BOUNDS
COMMON.CACHE
COMMON.CALC
COMMON.CHAIN
6,7c6,7
<       double precision c,cp,dc,dc_old,d_c_work,xloc,xrot,dc_norm,t,r,
<      & prod,rt,dc_work,cref,crefjlee,velAFMconst,
---
>       double precision c,cp,dc,dc_old,c_old,d_c_work,xloc,xrot,
>      & dc_norm,t,r,prod,rt,dc_work,cref,crefjlee,velAFMconst,
11,12c11,12
<      & xloc(3,maxres),xrot(3,maxres),dc_norm(3,0:maxres2),
<      & dc_work(MAXRES6),
---
>      & c_old(3,maxres2),xloc(3,maxres),xrot(3,maxres),
>      & dc_norm(3,0:maxres2),dc_work(MAXRES6),
COMMON.CONTACTS
1a2,3
>       double precision icont_ref_c
>       integer icont_shift
3c5,7
<      &                  icont_ref(2,maxcont)
---
>      &                  icont_ref(2,maxcont),
>      &                  icont_ref_c(3,maxres2+2),
>      &                  icont_shift
COMMON.CONTMAT
COMMON.CONTROL
15c15,16
<      & out_template_coord,out_template_restr,usampl,loc_qlike,adaptive
---
>      & out_template_coord,out_template_restr,usampl,loc_qlike,adaptive,
>      & use_rsb_output,virtual_bond_rst
27c28,29
<      & read_homol_frag,out_template_coord,out_template_restr
---
>      & read_homol_frag,out_template_coord,out_template_restr,
>      & use_rsb_output,virtual_bond_rst
COMMON.CORRMAT
COMMON.CSA
COMMON.DBASE
COMMON.DERIV
COMMON.DFA
COMMON.DISTFIT
COMMON.EELEC
1,3d0
<       integer eelec_buffer_size
<       parameter (eelec_buffer_size=112)
< 
10,38c7
< 	double precision pad(4)
<       end structure
< !     record /eelec_thread_data/ eelec_th_data(max_fg_threads)
< 
< !     integer eelecij_work_size
< !     parameter (eelecij_work_size=maxres)
< !     structure /eelecij_workitem/
< !       integer i
< !       integer j
< !     end structure
< !     record /eelecij_workitem/ eelecij_work_turn3(eelecij_work_size)
< !     record /eelecij_workitem/ eelecij_work_turn4(eelecij_work_size)
<       integer eelecij_work_turn3(maxres)
<       integer eelecij_work_turn4(maxres)
<       integer eelecij_work_size_turn3
<       integer eelecij_work_size_turn4
<       integer*1 eelecij_doturn(maxres)
< 
<       structure /eelecij_work_chunk/
<         integer j(eelec_buffer_size)
<         double precision x(eelec_buffer_size)
<         double precision y(eelec_buffer_size)
<         double precision z(eelec_buffer_size)
<         double precision r(eelec_buffer_size)
< 	double precision lipid_j_ss(eelec_buffer_size)
< 	double precision lipid_j_ssgrad(eelec_buffer_size)
<         double precision muuyz_jx(eelec_buffer_size)
<         double precision muuyz_jy(eelec_buffer_size)
<         double precision muuyz_jz(eelec_buffer_size)
---
>         double precision pad(4)
49,68d17
<       structure /eelecij_array_delta/
<         double precision gvdwppx(-1:maxres)
<         double precision gvdwppy(-1:maxres)
<         double precision gvdwppz(-1:maxres)
<         double precision gelc_longx(-1:maxres)
<         double precision gelc_longy(-1:maxres)
<         double precision gelc_longz(-1:maxres)
<         double precision gelcx(-1:maxres)
<         double precision gelcy(-1:maxres)
<         double precision gelcz(-1:maxres)
<         double precision gel_loc_longx(-1:maxres)
<         double precision gel_loc_longy(-1:maxres)
<         double precision gel_loc_longz(-1:maxres)
<         double precision gel_locx(-2:maxres+1)
<         double precision gel_locy(-2:maxres+1)
<         double precision gel_locz(-2:maxres+1)
<       end structure
<       record /eelecij_array_delta/
<      &         eelecij_array(max_fg_threads)
< 
71d19
< !       double precision gel_loc_turn3
73,74d20
< !       double precision gel_loc_turn4
< !       double precision gloc
79,84d24
<       integer pp_from_ik(max_fg_threads)
<       integer pp_to_ik(max_fg_threads)
<       integer pp_first_blk(max_fg_threads)
<       integer pp_last_blk(max_fg_threads)
<       integer pp_first_j(max_fg_threads)
<       integer pp_last_j(max_fg_threads)
86d25
<       integer pp_limit_threads
88,94c27,37
<       common /eelecijcommons1/
<      &                        eelecij_value,eelecij_array,
<      &                        eelecij_turn,
<      &                        pp_from_ik,pp_to_ik,
<      &                        pp_first_blk,pp_last_blk,
<      &                        pp_first_j,pp_last_j,
<      &                        pp_threads
---
>       type(thread_interaction_range) pp_range(max_fg_threads)
> 
>       common /eelecijcommons1/ eelecij_value,eelecij_turn,
>      &  pp_range,pp_threads
> 
>       integer eelecij_work_turn3(maxres)
>       integer eelecij_work_turn4(maxres)
>       integer eelecij_work_size_turn3
>       integer eelecij_work_size_turn4
>       integer*1 eelecij_doturn(maxres)
> 
96,100c39,67
<      &                        eelecij_work_turn3,
<      &                        eelecij_work_turn4,
<      &                        eelecij_doturn,
<      &                        eelecij_work_size_turn3,
<      &                        eelecij_work_size_turn4
---
>      &  eelecij_work_turn3,eelecij_work_turn4,
>      &  eelecij_work_size_turn3,eelecij_work_size_turn4,
>      &  eelecij_doturn
> 
>       integer eelecij_blocks_2(maxres*2)
>       integer eelecij_blocks_3(maxres*2)
>       integer num_eelecij_blocks_2
>       integer num_eelecij_blocks_3
>       integer first_block_2(max_fg_threads)
>       integer last_block_2(max_fg_threads)
>       integer first_block_3(max_fg_threads)
>       integer last_block_3(max_fg_threads)
>       common /eelecijcommons3/
>      &                        eelecij_blocks_2,
>      &                        eelecij_blocks_3,
>      &                        num_eelecij_blocks_2,
>      &                        num_eelecij_blocks_3
>       common /eelecijcommons4/
>      &                        first_block_2,last_block_2,
>      &                        first_block_3,last_block_3
> 
>       integer ppseplisti(0:maxres)
>       integer ppseplistj(maxres*maxint_res)
>       common /eelecijcommons5/ ppseplisti,ppseplistj
> 
>       double precision soa_mu_x(maxres)
>       double precision soa_mu_y(maxres)
>       double precision soa_mu_z(maxres)
>       common /eelecijcommons6/ soa_mu_x,soa_mu_y,soa_mu_z
COMMON.EELEC.SHIELD
95d94
<       integer pp_limit_threads
COMMON.EGB
1,19d0
<       integer egb_buffer_size
<       parameter (egb_buffer_size=112)
< 
<       structure /egb_work_chunk/
<         integer j(egb_buffer_size)
<         double precision x(egb_buffer_size)
< 	double precision y(egb_buffer_size)
<         double precision z(egb_buffer_size)
< 	double precision r(egb_buffer_size)
<       end structure
< 
<       structure /egb_result_item/
<         double precision gvdwc(3)
<         double precision gvdwx(3)
<         double precision lip_delta
< 	double precision pad
<       end structure
<       record /egb_result_item/ egb_result(maxres,max_fg_threads)
< 
31,36c12,17
<       integer scsc_from_ik(max_fg_threads)
<       integer scsc_to_ik(max_fg_threads)
<       integer scsc_first_blk(max_fg_threads)
<       integer scsc_last_blk(max_fg_threads)
<       integer scsc_first_j(max_fg_threads)
<       integer scsc_last_j(max_fg_threads)
---
>       !integer scsc_from_ik(max_fg_threads)
>       !integer scsc_to_ik(max_fg_threads)
>       !integer scsc_first_blk(max_fg_threads)
>       !integer scsc_last_blk(max_fg_threads)
>       !integer scsc_first_j(max_fg_threads)
>       !integer scsc_last_j(max_fg_threads)
38c19,35
<       integer scsc_limit_threads
---
> 
>       type(thread_interaction_range) scsc_range(max_fg_threads)
> 
>       !integer dynss_from_ik(max_fg_threads)
>       !integer dynss_to_ik(max_fg_threads)
>       !integer dynss_first_blk(max_fg_threads)
>       !integer dynss_last_blk(max_fg_threads)
>       !integer dynss_first_j(max_fg_threads)
>       !integer dynss_last_j(max_fg_threads)
>       integer dynss_threads
> 
>       type(thread_interaction_range) dynss_range(max_fg_threads)
> 
>       integer scscseplisti(0:maxres)
>       integer scscseplistj(maxres*maxint_res)
>       integer dynss_seplisti(0:maxres)
>       integer dynss_seplistj(maxres*maxint_res)
44c41
<       double precision sigma_j(ntyp,ntyp)
---
>       double precision sigma_j(0:ntyp1,0:ntyp1)
46,47c43,46
<       common /egbderivedparams/ aa_aq_j,bb_aq_j,aa_lipaq,bb_lipaq,
<      &                          sigma_j, chi_j
---
>       double precision inv_sigma(0:ntyp1,0:ntyp1)
> 
>       double precision egb_data2d(maxres,7,ntyp)
>       double precision egb_data1d(maxres,2)
49,53c48,57
<       common /egbcommons/ egb_result,egb_accum,
<      &                    scsc_from_ik,scsc_to_ik,
<      &                    scsc_first_blk,scsc_last_blk,
<      &                    scsc_first_j,scsc_last_j,
<      &			  scsc_threads
---
>       common /egbderivedparams/
>      &  aa_aq_j,bb_aq_j,aa_lipaq,bb_lipaq,sigma_j,chi_j,
>      &  inv_sigma,egb_data2d,egb_data1d
> 
>       common /egbcommons/ egb_accum,
>      &                    scsc_range,dynss_range,
>      &                    scscseplisti,scscseplistj,
>      &                    dynss_seplisti,dynss_seplistj,
>      &			  scsc_threads,
>      &			  dynss_threads
COMMON.ESCP
1,12c1
<       integer escp_buffer_size
<       parameter (escp_buffer_size=224)
< 
<       structure /escp_result_item/
<         double precision scp(3)
< 	double precision pad1
<         double precision scpp(3)
< 	double precision pad2
<       end structure
<       record /escp_result_item/ escp_result(-1:maxres,max_fg_threads)
< 
<       structure /escp_accumulator/
---
>       type escp_accumulator
15,34c4,7
< 	double precision pad(6)
<       end structure
<       record /escp_accumulator/ escp_accum(max_fg_threads)
< 
<       structure /escp_work_chunk/
<         integer j(escp_buffer_size)
<         double precision x(escp_buffer_size)
< 	double precision y(escp_buffer_size)
<         double precision z(escp_buffer_size)
< 	double precision r(escp_buffer_size)
<         double precision aad(escp_buffer_size)
<         double precision bad(escp_buffer_size)
<       end structure
< 
<       integer scp_from_ik(max_fg_threads)
<       integer scp_to_ik(max_fg_threads)
<       integer scp_first_blk(max_fg_threads)
<       integer scp_last_blk(max_fg_threads)
<       integer scp_first_j(max_fg_threads)
<       integer scp_last_j(max_fg_threads)
---
>         double precision pad2(6)
>       end type escp_accumulator
>       type(escp_accumulator) escp_accum(max_fg_threads)
> 
36d8
<       integer scp_limit_threads
38,42c10,33
<       common /escp_common/ escp_result,escp_accum,
<      &                     scp_from_ik,scp_to_ik,
<      &                     scp_first_blk,scp_last_blk,
<      &                     scp_first_j,scp_last_j,
<      &			   scp_threads
---
>       type(thread_interaction_range) scp_range(max_fg_threads)
> 
>       integer scpseplisti(0:maxres)
>       integer scpseplistj(maxres*maxint_res)
> 
>       integer scp_plus_blocks(0:maxchain*2,max_fg_threads)
>       integer scp_minus_blocks(0:maxchain*2,max_fg_threads)
>       integer scp_plus_count(max_fg_threads)
>       integer scp_minus_count(max_fg_threads)
> 
>       ! aad and bad premultiplied by wscp*expon
>       double precision escp_aad(ntyp,2),escp_bad(ntyp,2)
>       ! aad and bad premultiplied by wscp*expon*scal14
>       double precision escp14_aad(ntyp,2),escp14_bad(ntyp,2)
> 
>       double precision escp_row_aad(maxres,2),escp_row_bad(maxres,2)
> 
>       common /escp_common/ escp_accum,
>      &  escp_aad,escp_bad,escp14_aad,escp14_bad,
>      &  escp_row_aad,escp_row_bad,
>      &  scp_plus_blocks,scp_minus_blocks,
>      &  scp_plus_count,scp_minus_count,
>      &  scpseplisti,scpseplistj,
>      &  scp_range,scp_threads
COMMON.FFIELD
COMMON.FRAG
COMMON.GEO
COMMON.GRID
4,7d3
<       integer positive_sc(grid_length)
<       integer positive_p(grid_length)
<       integer positive_pn(grid_length)
< 
18,19d13
<       double precision poscellsize(3)
<       double precision gridpad2
22,24d15
<       integer posgridsize(3)
<       integer gridpad4
<       integer*8 posgridhits,posgridjumps
28,29c19,25
<       logical neggridmerge(3)
<       common /grid0/ positive_sc,positive_p,positive_pn
---
> 
>       integer maxneggridsize(3)
>       integer maxsaxsgridsize(3)
> 
>       logical single_cell_grid
>       logical first_time_grid
> 
31,39c27,50
<      &              poscellsize,gridpad2,
<      &              neggridsize,gridpad3,
<      &              posgridsize,gridpad4,
<      &              posgridhits,posgridjumps,
<      &              neggridhits,
<      &              griddatap,griddatapn,griddatasc,
<      &              gridcellp,gridcellpn,gridcellsc,
<      &              gridallcount,gridmiss,gridpass,gridtypecuts,
<      &              max_grid_cells,neggridmerge
---
>      &  neggridsize,gridpad3,
>      &  maxneggridsize,maxsaxsgridsize,
>      &  neggridhits,
>      &  griddatap,griddatapn,griddatasc,
>      &  gridcellp,gridcellpn,gridcellsc,
>      &  gridallcount,gridmiss,gridpass,gridtypecuts,
>      &  max_grid_cells,
>      &  single_cell_grid
> 
>       type grid_tuning_options
>         double precision last_time
>         double precision time_point
>         double precision current_sum
>         double precision non_work_extra_time
>         integer direction
>         integer iterations_in_sample
>         integer samples_left
> 	logical enabled
>       end type grid_tuning_options
> 
>       double precision r_buff_list,r_buff_saxs_list
>       type(grid_tuning_options) grid_opts,saxs_grid_opts
>       common /autogrid/ r_buff_list,r_buff_saxs_list,
>      &  grid_opts,saxs_grid_opts
COMMON.HAIRPIN
COMMON.HEADER
COMMON.HOMOLOGY
COMMON.INFO
COMMON.INTERACT
COMMON.IOUNITS
23c23
<      &        mremd_rst_name,curdir,pref_orig
---
>      &        mremd_rst_name,curdir,pref_orig,rsbname
27c27,28
<      &       cartname,tmpdir,mremd_rst_name,curdir,pref_orig,liczba
---
>      &       cartname,tmpdir,mremd_rst_name,curdir,pref_orig,liczba,
>      &       rsbname
COMMON.LAGRANGE
COMMON.LAGRANGE.5diag
3,4c3,5
<      & d_t_new(3,0:MAXRES2),vtot(maxres2),
<      & d_af(3,maxres2),d_as(3,maxres2),
---
>      & d_t_new(3,0:MAXRES2),vel(3,maxres2),velp(3,maxres2),
>      & vtot(maxres2),vel_old(3,maxres2),vel_new(3,maxres2),
>      & velp_old(3,maxres2),d_af(3,maxres2),d_as(3,maxres2),
9,10c10
<      & DU1orig(MAXRES2),DU2orig(MAXRES2),
<      & dminv(MAXRES2)
---
>      & DU1orig(MAXRES2),DU2orig(MAXRES2)
13c13,14
<       common /lagrange/ d_t,d_t_old,d_t_new,d_t_work,d_t_work_new,d_a,
---
>       common /lagrange/ vel,velp,vel_old,velp_old,vel_new,d_t,d_t_old,
>      & d_t_new,d_t_work,d_t_work_new,d_a,
17,18c18
<      & iposd_chain(maxchain),
<      & dminv
---
>      & iposd_chain(maxchain)
COMMON.LANGEVIN
COMMON.LANGEVIN.lang0
COMMON.LANGEVIN.lang0.5diag
COMMON.LBFGS
COMMON.LIPCALC
COMMON.LIPID
1,7c1,7
<       structure /lipid_data/
<         double precision ss
<         double precision ssgrad
<       end structure
<       record /lipid_data/ lipid_cache(maxres*2)
< 
<       common /lipidcommons/ lipid_cache
---
>       double precision lipid_cache_ss(maxres*2)
>       double precision lipid_cache_ssgrad(maxres*2)
>       double precision lipid_cache_ss_ns(maxres*2)
>       double precision lipid_cache_ssgrad_ns(maxres*2)
>       common /lipidcommons/
>      &  lipid_cache_ss,lipid_cache_ssgrad,
>      &  lipid_cache_ss_ns,lipid_cache_ssgrad_ns
COMMON.LOCAL
COMMON.LOCEL
COMMON.LOCMOVE
COMMON.MAP
COMMON.MAXGRAD
COMMON.MCE
COMMON.MCM
COMMON.MD
6,7c6
<      & maxtime_split,itime_mat,imatupdate,irest_freq,
<      & itime_rep
---
>      & maxtime_split,itime_mat,imatupdate,irest_freq
9c8
<      & rattle,mdpdb,RESPA,preminim,PRINT_AMTS_MSG
---
>      & rattle,mdpdb,RESPA,preminim
13d11
<      & itime_rep,
16c14
<      & rattle,RESPA,PRINT_AMTS_MSG
---
>      & rattle,RESPA
COMMON.MINIM
COMMON.MUCA
COMMON.NAMES
COMMON.NEWBOX
1,4c1,7
<       double precision boxsize,boxpad1,iboxsize,boxpad2
<       double precision halfboxsize,boxpad3
<       common /newbox/ boxsize(3),boxpad1,iboxsize(3),boxpad2,
<      &                halfboxsize(3),boxpad3
---
>       double precision boxsize(3),boxpad1
>       double precision iboxsize(3),boxpad2
>       double precision halfboxsize(3),boxpad3
>       double precision mhalfboxsize(3),boxpad4
>       common /newbox/ boxsize,boxpad1,iboxsize,boxpad2,
>      &                halfboxsize,boxpad3,
>      &                mhalfboxsize,boxpad4
COMMON.NMR
6,7c6,7
<       double precision dhpb_peak,dhpb1_peak,dhpb2_peak,dhpb3_peak,
<      & forcon_peak,fordepth_peak,scal_peak,slope_peak
---
>       double precision dhpb_peak,dhpb1_peak,forcon_peak,fordepth_peak,
>      & scal_peak,slope_peak
9,27c9,13
<       double precision tau,dnmr_xave,dnmr_xave0,dnmr_repave,
<      & dnmr_xaveave,dravdr_limit
<       integer atimeave,atimeave_save,navint,
<      & ndiv_tau,it_interval,ider_ave
<       logical ave_rep,wham_rep
<       integer n_exch_restr,maxit_wham
<       logical nonscal_ave_grad
<       common /NMRpeaks/ dhpb_peak(maxdim_nmr),dhpb1_peak(maxdim_nmr),
<      & dhpb2_peak(maxdim_nmr),dhpb3_peak(maxdim_nmr),
<      & forcon_peak(maxdim_nmr),fordepth_peak(maxdim_nmr),
<      & dnmr_xave(2,maxdim_nmr),dnmr_xave0(maxdim_nmr),
<      & dnmr_xaveave(maxdim_nmr),dnmr_repave(maxdim_nmr),TAU,
<      & scal_peak,slope_peak,dravdr_limit,nonscal_ave_grad,
<      & atimeave,atimeave_save,navint,ndiv_tau,it_interval,ider_ave(3),
<      & ave_rep,wham_rep,n_exch_restr,maxit_wham,
<      & proton1(maxdim_nmr),proton2(maxdim_nmr),num_peak(maxdim_nmr),
<      & ihpb_peak(2,maxdim_nmr),jhpb_peak(2,maxdim_nmr),
<      & ibecarb_peak(maxdim_nmr),
<      & irestr_type_peak(maxdim_nmr),ipeak(2,maxdim_nmr),npeak,nhpb_peak
---
>       common /NMRpeaks/ dhpb_peak(maxdim),dhpb1_peak(maxdim),
>      & forcon_peak(maxdim),fordepth_peak(maxdim),scal_peak,slope_peak,
>      & proton1(maxdim),proton2(maxdim),num_peak(maxdim),
>      & ihpb_peak(2,maxdim),jhpb_peak(2,maxdim),ibecarb_peak(maxdim),
>      & irestr_type_peak(maxdim),ipeak(2,maxdim),npeak,nhpb_peak
35,40c21,22
<       integer link_start_peak,link_end_peak,icount_nmraveave,
<      & idispls_nmraveave,icount_nmrave,idispls_nmrave
<       common /links_nmr_split/ link_start_peak,link_end_peak,
<      & icount_nmraveave(0:max_fg_procs),
<      & idispls_nmraveave(0:max_fg_procs),
<      & icount_nmrave(0:max_fg_procs),idispls_nmrave(0:max_fg_procs)
---
>       integer link_start_peak,link_end_peak
>       common /links_nmr_split/ link_start_peak,link_end_peak
COMMON.NMR-orig-Aug-3-2025
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/COMMON.NMR-orig-Aug-3-2025: No such file or directory
COMMON.OMP
0a1,8
>       type thread_interaction_range
>         integer rng_start,rng_first,rng_first_j
>         integer rng_end,rng_last,rng_last_j
>         integer sep_start,sep_first
>         integer sep_end,sep_last
>         integer pad(6)
>       end type thread_interaction_range
> 
3a12,28
>       double precision soa_c_x(maxres*2+16)
>       double precision soa_c_y(maxres*2+16)
>       double precision soa_c_z(maxres*2+16)
> 
>       double precision soa_cp_x(maxres)
>       double precision soa_cp_y(maxres)
>       double precision soa_cp_z(maxres)
> 
>       double precision soa_dc_norm_x(0:maxres*2+16)
>       double precision soa_dc_norm_y(0:maxres*2+16)
>       double precision soa_dc_norm_z(0:maxres*2+16)
> 
>       double precision omp_dgradbufc(3,-1:maxres)
>       double precision omp_dgradc(3,-1:maxres)
>       double precision omp_dgradx(3,-1:maxres)
>       double precision omp_dgloc(-6:maxvar)
> 
5a31,87
>       integer iti_tab(0:maxres+1)
> 
>       logical eelec_group_results
>       logical escp_group_results
>       logical egb_group_results
>       logical eturn_group_results
>       logical etor_group_results
>       logical esc_group_results
>       logical ebend_group_results
>       logical ebond_group_results
>       logical elip_group_results
>       logical esaxs_group_results
> 
>       common /ompflags/ eelec_group_results,
>      &                  escp_group_results,
>      &                  egb_group_results,
>      &                  eturn_group_results,
>      &                  etor_group_results,
>      &                  esc_group_results,
>      &                  ebend_group_results,
>      &                  ebond_group_results,
>      &                  elip_group_results,
>      &                  esaxs_group_results
> 
>       double precision idexpon6
> 
>       integer numa_node_size
>       integer ACCELERATOR_DEFAULT
>       integer ACCELERATOR_NONE
>       integer ACCELERATOR_CUDA
>       parameter (ACCELERATOR_DEFAULT=0)
>       parameter (ACCELERATOR_NONE=1)
>       parameter (ACCELERATOR_CUDA=2)
>       integer accelerator_mode
> 
>       common /ompsoa/ soa_c_x,soa_c_y,soa_c_z,
>      &                soa_cp_x,soa_cp_y,soa_cp_z,
>      &                soa_dc_norm_x,soa_dc_norm_y,soa_dc_norm_z
>       common /ompgrad/ omp_dgradbufc,omp_dgradc,omp_dgradx,omp_dgloc
>       common /ompconsts/ idexpon6,numa_node_size,
>      &                   accelerator_mode
> 
>       double precision restok2(ntyp+1)
>       double precision restok_co(ntyp+1)
>       common /ompparams/ restok2,restok_co
> 
> !     double precision escp_aad(0:maxres,2)
> !     double precision escp_bad(0:maxres,2)
> 
> !     integer sss_map_size
> !     parameter (sss_map_size=65536)
> 
> !     real*4 sss_map(0:sss_map_size+3)
> !     real*4 sssgrad_map(0:sss_map_size+3)
> !     double precision sss_map_offset
> !     double precision sss_map_scale
> !     real*4 r_sss_map_size
8c90,93
<      &                   abs_itype,abs_itel
---
>      &                   abs_itype,abs_itel,iti_tab
> !     common /ompcommonescp/ escp_aad,escp_bad
> !     common /ompsssmap/ sss_map,sssgrad_map,
> !    &                   sss_map_offset,sss_map_scale,r_sss_map_size
COMMON.PMF
COMMON.QRESTR
COMMON.REMD
4d3
<       double precision rep_weight(max_rep,max_rep),myrep_weight
10c9
<       real t_restart1(5,max_cg_procs),r_Urestart1(3,max_cg_procs)
---
>       real t_restart1(5,max_cg_procs)
12d10
<       common /repavecommon/ rep_weight,myrep_weight
17c15
<      &                    r_Urestart1,iset_restart1
---
>      &                    iset_restart1
26c24
<      &                    Uconst_cache(0:3,max_cache_traj),
---
>      &                    Uconst_cache(max_cache_traj),
COMMON.RESTARTED
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/COMMON.RESTARTED: No such file or directory
COMMON.SAXS
5c5,6
<      & rnei_min_bound,rnei_max_bound,rnei_mid_bound,sigmamin,sigmamax
---
>      & rnei_min_bound,rnei_max_bound,rnei_mid_bound,sigmamin,sigmamax,
>      & log_distsaxs(maxsaxs)
8c9
<      & sigmamin,sigmamax,nsaxs,saxs_mode
---
>      & sigmamin,sigmamax,nsaxs,saxs_mode,log_distsaxs
COMMON.SBRIDGE
COMMON.SCALESAVE
COMMON.SCCOR
COMMON.SCROT
COMMON.SETUP
4d3
<      & navetasks,ave_rank,
6,7c5
<      & FG_COMM1,CONT_FROM_COMM,CONT_TO_COMM,AVE_COMM,
<      & lentyp(0:maxprocs-1),
---
>      & FG_COMM1,CONT_FROM_COMM,CONT_TO_COMM,lentyp(0:maxprocs-1),
11,12c9,10
<       common/setup/me,MyRank,cg_rank,fg_rank,fg_rank1,ave_rank,nodes,
<      & Nprocs,nfgtasks,nfgtasks1,navetasks,
---
>       common/setup/me,MyRank,cg_rank,fg_rank,fg_rank1,nodes,Nprocs,
>      & nfgtasks,nfgtasks1,
14c12
<      & FG_COMM1,CONT_FROM_COMM,CONT_TO_COMM,AVE_COMM,lentyp
---
>      & FG_COMM1,CONT_FROM_COMM,CONT_TO_COMM,lentyp
COMMON.SHIELD
COMMON.SIMDSTAT
COMMON.SPLITELE
COMMON.THREAD
COMMON.TIME1
22,23c22,26
<      & time_omp_init,time_check_rebuild_lists,
<      & time_nmr_hpos,time_nmr_hpos_0,time_nmr_hpos_hn,time_edis
---
>      & time_omp_init,time_check_rebuild_lists,time_omp_sum,
>      & time_cuda_turn,time_initialization,time_dynss,
>      & time_nmr_hpos,time_nmr_hpos_0,time_nmr_hpos_hn,time_edis,
>      & time_emodeller,time_saxs_list,time_contact,time_memclear,
>      & time_ess,time_reset_moment,time_statout,time_rst,time_cuda_init
37,38c40,44
<      & time_omp_init,time_check_rebuild_lists,
<      & time_nmr_hpos,time_nmr_hpos_0,time_nmr_hpos_hn,time_edis
---
>      & time_omp_init,time_check_rebuild_lists,time_omp_sum,
>      & time_cuda_turn,time_initialization,time_dynss,
>      & time_nmr_hpos,time_nmr_hpos_0,time_nmr_hpos_hn,time_edis,
>      & time_emodeller,time_saxs_list,time_contact,time_memclear,
>      & time_ess,time_reset_moment,time_statout,time_rst,time_cuda_init
COMMON.TORCNSTR
6,9c6
<      & ithetaconstr_end,icount_dihconstr,idispls_dihconstr,
<      & icount_dihconstr0,idispls_dihconstr0,
<      & icount_thetaconstr,idispls_thetaconstr,
<      & icount_thetaconstr0,idispls_thetaconstr0
---
>      & ithetaconstr_end
17,22c14,15
<      & ndih_constr,idih_constr,iconstr_dih,
<      & ndih_nconstr,idih_nconstr,idihconstr_start,idihconstr_end,
<      & icount_dihconstr(0:max_fg_procs-1),
<      & idispls_dihconstr(0:max_fg_procs-1),
<      & icount_dihconstr0(0:max_fg_procs-1),
<      & idispls_dihconstr0(0:max_fg_procs-1),
---
>      &  ndih_constr,idih_constr,iconstr_dih,
>      &  ndih_nconstr,idih_nconstr,idihconstr_start,idihconstr_end,
24,38c17
<      & ithetaconstr_end,
<      & icount_thetaconstr(0:max_fg_procs-1),
<      & idispls_thetaconstr(0:max_fg_procs-1),
<      & icount_thetaconstr0(0:max_fg_procs-1),
<      & idispls_thetaconstr0(0:max_fg_procs-1),
<      & raw_psipred
<       double precision theta_xave(2,maxres),theta_xave0(maxres),
<      & cosphi_xave(2,maxres),cosphi_xave0(maxres),sinphi_xave(2,maxres),
<      & sinphi_xave0(maxres),cosphi_repave(maxres),sinphi_repave(maxres),
<      & theta_xaveave(maxres),theta_repave(maxres),
<      & cosphi_xaveave(maxres),sinphi_xaveave(maxres)
<       common /ang_timeave/ theta_xave,theta_xave0,theta_xaveave,
<      & theta_repave,cosphi_xave,cosphi_xave0,cosphi_xaveave,
<      & sinphi_xave,sinphi_xave0,sinphi_xaveave,cosphi_repave,
<      & sinphi_repave
---
>      & ithetaconstr_end,raw_psipred
COMMON.TORCNSTR-org-Aug-4-2025
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/COMMON.TORCNSTR-org-Aug-4-2025: No such file or directory
COMMON.TORSION
38,39c38,39
<       double precision b1,b2,cc,dd,ee,ctilde,dtilde,b2tilde,b1tilde,
<      & b,bnew1,bnew2,ccold,ddold,ccnew,ddnew,eenew,e0new,gtb1,gtb2,
---
>       double precision cc,dd,ee,ctilde,dtilde,b2tilde,b1tilde,
>      & b,bnew1,bnew2,ccold,ddold,ccnew,ddnew,eenew,e0new,
44c44
<       common/fourier/ b1(2,maxres),b2(2,maxres),b(13,-ntyp:ntyp),
---
>       common/fourier/ b(13,-ntyp:ntyp),
58c58
<      &    gtb1(2,maxres),gtb2(2,maxres),gtCC(2,2,maxres),
---
>      &    gtCC(2,2,maxres),
COMMON.VAR
14c14
<      &          costtab(maxres), sinttab(maxres), cost2tab(maxres),
---
>      &          costtab(maxres+1), sinttab(maxres), cost2tab(maxres),
COMMON.VECTORS
COMMON.WHAM
diff: /users/project1/pt01225/adaml/Eurohpc-CPC/unres/source/COMMON.WHAM: No such file or directory
COMMON.XLINKS
