changes in wham and cluser + cutoff corr
[unres4.git] / source / wham / conform_compar.F90
1       module conform_compar
2 !-----------------------------------------------------------------------------
3       use names
4       use io_units
5       use geometry_data, only:nres
6       use math, only:pinorm
7       use geometry, only:dist
8       use regularize_, only:fitsq
9 !
10       use wham_data
11 #ifndef CLUSTER
12       use w_compar_data
13 #endif
14 #ifdef MPI
15       use MPI_data
16 !      include "COMMON.MPI"
17 #endif
18       implicit none
19 !-----------------------------------------------------------------------------
20 !
21 !
22 !-----------------------------------------------------------------------------
23       contains
24 #ifndef CLUSTER
25 !-----------------------------------------------------------------------------
26 ! conf_compar.F
27 !-----------------------------------------------------------------------------
28       subroutine conf_compar(jcon,lprn,print_class)
29 !      implicit real*8 (a-h,o-z)
30       use energy_data, only:icont,ncont,nnt,nct,maxcont!,&
31 !                      nsccont_frag_ref,isccont_frag_ref
32 #ifdef MPI
33       include "mpif.h"
34 #endif
35 !      include 'DIMENSIONS'
36 !      include 'DIMENSIONS.ZSCOPT'
37 !      include 'DIMENSIONS.COMPAR'
38 !      include 'DIMENSIONS.FREE'
39 !      include 'COMMON.CONTROL'
40 !      include 'COMMON.IOUNITS'
41 !      include 'COMMON.COMPAR'
42 !      include 'COMMON.CHAIN' 
43 !      include 'COMMON.INTERACT'
44 !      include 'COMMON.VAR'
45 !      include 'COMMON.PEPTCONT'
46 !      include 'COMMON.CONTACTS1'
47 !      include 'COMMON.HEADER'
48 !      include 'COMMON.FREE'
49 !      include 'COMMON.ENERGIES'
50 !#ifdef MPI
51 !      include 'COMMON.MPI'
52 !#endif
53 !      integer ilen
54 !      external ilen
55       logical :: lprn,print_class
56       integer :: ncont_frag(mmaxfrag),&
57        icont_frag(2,maxcont,mmaxfrag),ncontsc,&
58        icontsc(1,maxcont),nsccont_frag(mmaxfrag),&
59        isccont_frag(2,maxcont,mmaxfrag)
60       integer :: isecstr(nres)
61       integer :: itemp(maxfrag)
62       character(len=4) :: liczba
63       real(kind=8) :: Epot,rms
64       integer :: jcon,i,j,ind,ncnat,nsec_match,ishift,ishif1,ishif2,&
65                  nc_match,ncon_match,iclass_rms,ishifft_rms,ishiff,ishif
66       integer :: k,kk,iclass_con,iscor,ik,ishifft_con,idig,iex,im
67 !      print *,"Enter conf_compar",jcon
68       call angnorm12(rmsang)
69 ! Level 1: check secondary and supersecondary structure
70       call elecont(lprn,ncont,icont,nnt,nct)
71       if (lprn) then
72         write (iout,*) "elecont finished"
73         call flush(iout)
74       endif
75       call secondary2(lprn,.false.,ncont,icont,isecstr)
76       if (lprn) then
77         write (iout,*) "secondary2 finished"
78         call flush(iout)
79       endif
80       call contact(lprn,ncontsc,icontsc,nnt,nct)
81       if (lprn) then
82          write(iout,*) "Assigning electrostatic contacts"
83          call flush(iout)
84       endif
85       call contacts_between_fragments(lprn,3,ncont,icont,ncont_frag,&
86          icont_frag)
87       if (lprn) then
88         write(iout,*) "Assigning sidechain contacts"
89         call flush(iout)
90       endif
91       call contacts_between_fragments(lprn,3,ncontsc,icontsc,&
92          nsccont_frag,isccont_frag)
93       if (lprn) then
94         write(iout,*) "--> After contacts_between_fragments"
95         call flush(iout)
96       endif
97       do i=1,nlevel
98         do j=1,isnfrag(nlevel+1)
99           iclass(j,i)=0
100         enddo
101       enddo
102       do j=1,nfrag(1)
103         ind = icant(j,j)
104         if (lprn) then
105           write (iout,'(80(1h=))') 
106           write (iout,*) "Level",1," fragment",j
107           write (iout,'(80(1h=))') 
108         endif
109         call flush(iout)
110         rmsfrag(j,1)=rmscalc(0,1,j,jcon,lprn)
111 ! Compare electrostatic contacts in the current conf with that in the native
112 ! structure.
113         if (lprn) write (iout,*) &
114           "Comparing electrostatic contact map and local structure" 
115         call flush(iout)
116         ncnat=ncont_frag_ref(ind)
117 !        write (iout,*) "before match_contact:",nc_fragm(j,1),
118 !     &   nc_req_setf(j,1)
119 !        call flush(iout)
120         call match_secondary(j,isecstr,nsec_match,lprn)
121         if (lprn) write (iout,*) "Fragment",j," nsec_match",&
122           nsec_match," length",len_frag(j,1)," min_len",&
123           frac_sec*len_frag(j,1)
124         if (nsec_match.lt.frac_sec*len_frag(j,1)) then
125           iclass(j,1)=0
126           if (lprn) write (iout,*) "Fragment",j,&
127             " has incorrect secondary structure"
128         else
129           iclass(j,1)=1
130           if (lprn) write (iout,*) "Fragment",j,&
131             " has correct secondary structure"
132         endif
133         if (ielecont(j,1).gt.0) then
134           call match_contact(ishif1,ishif2,nc_match,ncon_match,&
135             ncont_frag_ref(ind),icont_frag_ref(1,1,ind),&
136             ncont_frag(ind),icont_frag(1,1,ind),&
137             j,n_shift(1,j,1),n_shift(2,j,1),nc_fragm(j,1),&
138             nc_req_setf(j,1),istruct(j),.true.,lprn)
139         else if (isccont(j,1).gt.0) then
140           call match_contact(ishif1,ishif2,nc_match,ncon_match,&
141             nsccont_frag_ref(ind),isccont_frag_ref(1,1,ind),&
142             nsccont_frag(ind),isccont_frag(1,1,ind),&
143             j,n_shift(1,j,1),n_shift(2,j,1),nc_fragm(j,1),&
144             nc_req_setf(j,1),istruct(j),.true.,lprn)
145         else if (iloc(j).gt.0) then
146 !          write (iout,*) "n_shif",n_shift(1,j,1),n_shift(2,j,1)
147           call match_contact(ishif1,ishif2,nc_match,ncon_match,&
148             0,icont_frag_ref(1,1,ind),&
149             ncont_frag(ind),icont_frag(1,1,ind),&
150             j,n_shift(1,j,1),n_shift(2,j,1),nc_fragm(j,1),&
151             0,istruct(j),.true.,lprn)
152 !          write (iout,*) "n_shif",n_shift(1,j,1),n_shift(2,j,1)
153         else
154           ishif=0
155           nc_match=1
156         endif
157         if (lprn) write (iout,*) "ishif1",ishif1," ishif2",ishif2
158         ishif=ishif1
159         qfrag(j,1)=qwolynes(1,j)
160         if (iabs(ishif2).gt.iabs(ishif1)) ishif=ishif2
161         if (lprn) write (iout,*) "ishift",ishif," nc_match",nc_match
162 !        write (iout,*) "j",j," ishif",ishif," rms",rmsfrag(j,1)
163         if (irms(j,1).gt.0) then
164           if (rmsfrag(j,1).le.rmscutfrag(1,j,1)) then
165             iclass_rms=2
166             ishifft_rms=0
167           else
168             ishiff=0
169             rms=1.0d2
170             iclass_rms=0
171             do while (rms.gt.rmscutfrag(1,j,1) .and. &
172                ishiff.lt.n_shift(1,j,1))
173               ishiff=ishiff+1
174               rms=rmscalc(-ishiff,1,j,jcon,lprn)
175 !              write(iout,*)"jcon,i,j,ishiff",jcon,i,j,-ishiff,
176 !     &          " rms",rms," rmscut",rmscutfrag(1,j,1)
177               if (lprn) write (iout,*) "rms",rmsfrag(j,1) 
178               if (rms.gt.rmscutfrag(1,j,1)) then
179                 rms=rmscalc(ishiff,1,j,jcon,lprn)
180 !                write (iout,*) "jcon,1,j,ishiff",jcon,1,j,ishiff,
181 !     &           " rms",rms
182               endif
183               if (lprn) write (iout,*) "rms",rmsfrag(j,1) 
184             enddo
185 !            write (iout,*) "After loop: rms",rms,
186 !     &        " rmscut",rmscutfrag(1,j,1)
187 !            write (iout,*) "iclass_rms",iclass_rms
188             if (rms.le.rmscutfrag(1,j,1)) then
189               ishifft_rms=ishiff
190               rmsfrag(j,1)=rms
191               iclass_rms=1
192             endif
193 !            write (iout,*) "iclass_rms",iclass_rms
194           endif
195 !          write (iout,*) "ishif",ishif
196           if (iabs(ishifft_rms).gt.iabs(ishif)) ishif=ishifft_rms
197         else
198           iclass_rms=1
199         endif
200 !        write (iout,*) "ishif",ishif," iclass",iclass(j,1),
201 !     &    " iclass_rms",iclass_rms
202         if (nc_match.gt.0 .and. iclass_rms.gt.0) then
203           if (ishif.eq.0) then
204             iclass(j,1)=iclass(j,1)+6
205           else
206             iclass(j,1)=iclass(j,1)+2
207           endif
208         endif
209         ncont_nat(1,j,1)=nc_match
210         ncont_nat(2,j,1)=ncon_match
211         ishifft(j,1)=ishif
212 !        write (iout,*) "iclass",iclass(j,1)
213       enddo
214 ! Next levels: Check arrangements of elementary fragments.
215       do i=2,nlevel
216         do j=1,nfrag(i)
217         if (i .eq. 2) ind = icant(ipiece(1,j,i),ipiece(2,j,i))
218         if (lprn) then
219             write (iout,'(80(1h=))') 
220             write (iout,*) "Level",i," fragment",j
221             write (iout,'(80(1h=))') 
222         endif
223 ! If an elementary fragment doesn't exist, don't check higher hierarchy levels.
224         do k=1,npiece(j,i)
225           ik=ipiece(k,j,i)
226           if (iclass(ik,1).eq.0) then
227             iclass(j,i)=0
228             goto 12
229           endif
230         enddo
231         if (i.eq.2 .and. ielecont(j,i).gt.0) then
232           iclass_con=0
233           ishifft_con=0
234           if (lprn) write (iout,*) &
235            "Comparing electrostatic contact map: fragments",&
236             ipiece(1,j,i),ipiece(2,j,i)," ind",ind
237           call match_contact(ishif1,ishif2,nc_match,ncon_match,&
238            ncont_frag_ref(ind),icont_frag_ref(1,1,ind),&
239            ncont_frag(ind),icont_frag(1,1,ind),&
240            j,n_shift(1,j,i),n_shift(2,j,i),nc_fragm(j,i),&
241            nc_req_setf(j,i),2,.false.,lprn)
242           ishif=ishif1
243           if (iabs(ishif2).gt.iabs(ishif1)) ishif=ishif2
244           if (nc_match.gt.0) then
245             if (ishif.eq.0) then
246               iclass_con=2
247             else
248               iclass_con=1
249             endif
250           endif
251           ncont_nat(1,j,i)=nc_match
252           ncont_nat(2,j,i)=ncon_match
253           ishifft_con=ishif
254         else if (i.eq.2 .and. isccont(j,i).gt.0) then
255           iclass_con=0
256           ishifft_con=0
257           if (lprn) write (iout,*) &
258            "Comparing sidechain contact map: fragments",&
259            ipiece(1,j,i),ipiece(2,j,i)," ind",ind
260           call match_contact(ishif1,ishif2,nc_match,ncon_match,&
261            nsccont_frag_ref(ind),isccont_frag_ref(1,1,ind),&
262            nsccont_frag(ind),isccont_frag(1,1,ind),&
263            j,n_shift(1,j,i),n_shift(2,j,i),nc_fragm(j,i),&
264            nc_req_setf(j,i),2,.false.,lprn)
265           ishif=ishif1
266           if (iabs(ishif2).gt.iabs(ishif1)) ishif=ishif2
267           if (nc_match.gt.0) then
268             if (ishif.eq.0) then
269               iclass_con=2
270             else
271               iclass_con=1
272             endif
273           endif
274           ncont_nat(1,j,i)=nc_match
275           ncont_nat(2,j,i)=ncon_match
276           ishifft_con=ishif
277         else if (i.eq.2) then
278           iclass_con=2
279           ishifft_con=0
280         endif
281         if (i.eq.2) qfrag(j,2)=qwolynes(2,j)
282         if (lprn) write (iout,*) &
283           "Comparing rms: fragments",&
284            (ipiece(k,j,i),k=1,npiece(j,i))
285         rmsfrag(j,i)=rmscalc(0,i,j,jcon,lprn)
286         if (irms(j,i).gt.0) then
287           iclass_rms=0
288           ishifft_rms=0
289           if (lprn) write (iout,*) "rms",rmsfrag(j,i)
290 !          write (iout,*) "i",i," j",j," rmsfrag",rmsfrag(j,i),
291 !     &     " rmscutfrag",rmscutfrag(1,j,i)
292           if (rmsfrag(j,i).le.rmscutfrag(1,j,i)) then
293             iclass_rms=2
294             ishifft_rms=0
295           else
296             ishif=0
297             rms=1.0d2
298             do while (rms.gt.rmscutfrag(1,j,i) .and. &
299                ishif.lt.n_shift(1,j,i))
300               ishif=ishif+1
301               rms=rmscalc(-ishif,i,j,jcon,lprn)
302 !              print *,"jcon,i,j,ishif",jcon,i,j,-ishif," rms",rms
303               if (lprn) write (iout,*) "rms",rmsfrag(j,i) 
304               if (rms.gt.rmscutfrag(1,j,i)) then
305                 rms=rmscalc(ishif,i,j,jcon,lprn)
306 !                print *,"jcon,i,j,ishif",jcon,i,j,ishif," rms",rms
307               endif
308               if (lprn) write (iout,*) "rms",rms
309             enddo
310             if (rms.le.rmscutfrag(1,j,i)) then
311               ishifft_rms=ishif
312               rmsfrag(j,i)=rms
313               iclass_rms=1
314             endif
315           endif
316         endif
317         if (irms(j,i).eq.0 .and. ielecont(j,i).eq.0 .and. &
318           isccont(j,i).eq.0 ) then
319           write (iout,*) "Error: no measure of comparison specified:",&
320             " level",i," part",j
321           stop
322         endif
323         if (lprn) &
324         write (iout,*) "iclass_con",iclass_con," iclass_rms",iclass_rms
325         if (i.eq.2) then
326           iclass(j,i) = min0(iclass_con,iclass_rms)
327           if (iabs(ishifft_rms).gt.iabs(ishifft_con)) then
328             ishifft(j,i)=ishifft_rms
329           else
330             ishifft(j,i)=ishifft_con
331           endif
332         else if (i.gt.2) then
333           iclass(j,i) = iclass_rms
334           ishifft(j,i)= ishifft_rms
335         endif
336    12   continue
337         enddo
338       enddo
339       rms_nat=rmsnat(jcon)
340       qnat=qwolynes(0,0)
341 ! Compute the structural class
342       iscor=0
343       IF (.NOT. BINARY) THEN
344       do i=1,nlevel
345         IF (I.EQ.1) THEN
346         do j=1,nfrag(i)
347           itemp(j)=iclass(j,i)
348         enddo
349         do kk=-1,1
350           do j=1,nfrag(i)
351             idig = 2*isnfrag(nlevel+1)-2*isnfrag(i)-kk*nfrag(i)-j
352             iex = 2**idig
353             im=mod(itemp(j),2)
354             itemp(j)=itemp(j)/2
355 !            write (iout,*) "i",i," j",j," idig",idig," iex",iex,
356 !     &        " iclass",iclass(j,i)," im",im
357             iscor=iscor+im*iex
358           enddo
359         enddo
360         ELSE
361         do j=1,nfrag(i)
362           idig = 2*isnfrag(nlevel+1)-2*isnfrag(i)-j
363           iex = 2**idig
364           if (iclass(j,i).gt.0) then
365             im=1
366           else
367             im=0
368           endif
369 !          write (iout,*) "i",i," j",j," idig",idig," iex",iex,
370 !     &      " iclass",iclass(j,i)," im",im
371           iscor=iscor+im*iex
372         enddo
373         do j=1,nfrag(i)
374           idig = 2*isnfrag(nlevel+1)-2*isnfrag(i)-nfrag(i)-j
375           iex = 2**idig
376           if (iclass(j,i).gt.1) then
377             im=1
378           else
379             im=0
380           endif
381 !          write (iout,*) "i",i," j",j," idig",idig," iex",iex,
382 !     &      " iclass",iclass(j,i)," im",im
383           iscor=iscor+im*iex
384         enddo
385         ENDIF
386       enddo
387       iscore=iscor
388       ENDIF
389       if (print_class) then
390 #ifdef MPI
391           write(istat,'(i6,$)') jcon+indstart(me)-1
392           write (istat,'(f10.2,$)') (potE(jcon,k),k=1,nParmSet),&
393            -entfac(jcon)
394 #else
395           write(istat,'(i6,$)') jcon
396           write (istat,'(f10.2,$)') (potE(jcon,k),k=1,nParmSet),&
397             -entfac(jcon)
398 #endif
399           write (istat,'(f8.3,2f6.3,$)') &
400             rms_nat,qnat,rmsang/(nres-3)
401           do j=1,nlevel
402             write(istat,'(1x,$,20(i3,$))') &
403               (ncont_nat(1,k,j),k=1,nfrag(j))
404             if (j.lt.3) then
405               write(istat,'(1x,$,20(f5.1,f5.2$))') &
406                 (rmsfrag(k,j),qfrag(k,j),k=1,nfrag(j))
407             else
408               write(istat,'(1x,$,20(f5.1$))') &
409                 (rmsfrag(k,j),k=1,nfrag(j))
410             endif
411             write(istat,'(1x,$,20(i1,$))') &
412               (iclass(k,j),k=1,nfrag(j))
413           enddo
414           if (binary) then
415             write (istat,'("  ",$)')
416             do j=1,nlevel
417               write (istat,'(100(i1,$))')(iclass(k,j),&
418                  k=1,nfrag(j))
419               if (j.lt.nlevel) write(iout,'(".",$)')
420             enddo
421             write (istat,*)
422           else
423             write (istat,'(i10)') iscore
424           endif
425       endif
426       RETURN
427       END subroutine conf_compar
428 !-----------------------------------------------------------------------------
429 ! angnorm.f
430 !-----------------------------------------------------------------------------
431       subroutine add_angpair(ici,icj,nang_pair,iang_pair)
432
433 !      implicit real*8 (a-h,o-z)
434 !      include 'DIMENSIONS'
435 !      include 'COMMON.IOUNITS'
436 !      include 'COMMON.CHAIN'
437       integer :: ici,icj,nang_pair,iang_pair(2,nres)
438       integer :: i,ian1,ian2
439 !      write (iout,*) "add_angpair: ici",ici," icj",icj,
440 !     &  " nang_pair",nang_pair
441       ian1=ici+2
442       if (ian1.lt.4 .or. ian1.gt.nres) return
443       ian2=icj+2
444 !      write (iout,*) "ian1",ian1," ian2",ian2
445       if (ian2.lt.4 .or. ian2.gt.nres) return
446       do i=1,nang_pair
447         if (ian1.eq.iang_pair(1,i) .and. ian2.eq.iang_pair(2,i)) return
448       enddo
449       nang_pair=nang_pair+1
450       iang_pair(1,nang_pair)=ian1
451       iang_pair(2,nang_pair)=ian2
452       return
453       end subroutine add_angpair
454 !-------------------------------------------------------------------------
455       subroutine angnorm(jfrag,ishif1,ishif2,diffang_max,angn,fract,lprn)
456
457       use geometry_data, only:nstart_sup,nend_sup,phi,theta,&
458                               rad2deg,dwapi
459 !      implicit real*8 (a-h,o-z)
460 !      include 'DIMENSIONS'
461 !      include 'DIMENSIONS.ZSCOPT'
462 !      include 'DIMENSIONS.COMPAR'
463 !      include 'COMMON.IOUNITS'
464 !      include 'COMMON.VAR'
465 !      include 'COMMON.COMPAR'
466 !      include 'COMMON.CHAIN'
467 !      include 'COMMON.GEO'
468       real(kind=8) :: pinorm,deltang
469       logical :: lprn
470       integer :: jfrag,ishif1,ishif2,nn,npart,nn4,nne
471       real(kind=8) :: diffang_max,angn,fract,ff
472       integer :: i,j,nbeg,nend,ll,longest
473       if (lprn) write (iout,'(80(1h*))')
474       angn=0.0d0
475       nn = 0
476       fract = 1.0d0
477       npart = npiece(jfrag,1)
478       nn4 = nstart_sup+3
479       nne = min0(nend_sup,nres)
480       if (lprn) write (iout,*) "nn4",nn4," nne",nne
481       do i=1,npart
482         nbeg = ifrag(1,i,jfrag) + 3 - ishif1
483         if (nbeg.lt.nn4) nbeg=nn4
484         nend = ifrag(2,i,jfrag) + 1 - ishif2
485         if (nend.gt.nne) nend=nne
486         if (nend.ge.nbeg) then
487         nn = nn + nend - nbeg + 1
488         if (lprn) write (iout,*) "i=",i," nbeg",nbeg," nend",nend,&
489           " nn",nn," ishift1",ishif1," ishift2",ishif2
490         if (lprn) write (iout,*) "angles"
491         longest=0
492         ll = 0
493         do j=nbeg,nend
494 !          deltang = pinorm(phi(j)-phi_ref(j+ishif1))
495           deltang=spherang(phi_ref(j+ishif1),theta_ref(j-1+ishif1),&
496             theta_ref(j+ishif1),phi(j),theta(j-1),theta(j))
497           if (dabs(deltang).gt.diffang_max) then
498             if (ll.gt.longest) longest = ll
499             ll = 0
500           else
501             ll=ll+1
502           endif
503           if (ll.gt.longest) longest = ll
504           if (lprn) write (iout,'(i5,3f10.5)')j,rad2deg*phi(j),&
505            rad2deg*phi_ref(j+ishif1),rad2deg*deltang
506           angn=angn+dabs(deltang)
507         enddo
508         longest=longest+3
509         ff = dfloat(longest)/dfloat(nend - nbeg + 4)
510         if (lprn) write (iout,*)"segment",i," longest fragment within",&
511           diffang_max*rad2deg,":",longest," fraction",ff
512         if (ff.lt.fract) fract = ff
513         endif
514       enddo
515       if (nn.gt.0) then
516         angn = angn/nn
517       else
518         angn = dwapi
519       endif
520       if (lprn) write (iout,*) "nn",nn," norm",rad2deg*angn,&
521         " fract",fract
522       return
523       end subroutine angnorm
524 !-------------------------------------------------------------------------
525       subroutine angnorm2(jfrag,ishif1,ishif2,ncont,icont,lprn,&
526         diffang_max,anorm,fract)
527
528       use geometry_data, only:nstart_sup,nend_sup,phi,theta,&
529                               rad2deg
530 !      implicit real*8 (a-h,o-z)
531 !      include 'DIMENSIONS'
532 !      include 'DIMENSIONS.ZSCOPT'
533 !      include 'DIMENSIONS.COMPAR'
534 !      include 'COMMON.IOUNITS'
535 !      include 'COMMON.VAR'
536 !      include 'COMMON.COMPAR'
537 !      include 'COMMON.CHAIN'
538 !      include 'COMMON.GEO'
539       integer :: ncont,icont(2,ncont),longest
540       real(kind=8) :: anorm,diffang_max,fract
541       integer :: npiece_c,ifrag_c(2,maxpiece),ishift_c(maxpiece)
542       real(kind=8) :: pinorm
543       logical :: lprn
544       integer :: jfrag,ishif1,ishif2
545       integer :: nn,nn4,nne,npart,i,j,jstart,jend,ic1,ic2,idi,iic
546       integer :: nbeg,nend,ll
547       real(kind=8) :: angn,ishifc,deltang,ff
548
549       if (lprn) write (iout,'(80(1h*))')
550 !
551 ! Determine the segments for which angles will be compared
552 !
553       nn4 = nstart_sup+3
554       nne = min0(nend_sup,nres)
555       if (lprn) write (iout,*) "nn4",nn4," nne",nne
556       npart=npiece(jfrag,1)
557       npiece_c=0
558       do i=1,npart
559 !        write (iout,*) "i",i," ifrag",ifrag(1,i,jfrag),ifrag(2,i,jfrag)
560         if (icont(1,ncont).lt.ifrag(1,i,jfrag) .or. &
561           icont(1,1).gt.ifrag(2,i,jfrag)) goto 11
562         jstart=1
563         do while (jstart.lt.ncont .and. &
564          icont(1,jstart).lt.ifrag(1,i,jfrag))
565 !          write (iout,*) "jstart",jstart," icont",icont(1,jstart),
566 !     &     " ifrag",ifrag(1,i,jfrag)
567           jstart=jstart+1
568         enddo
569 !        write (iout,*) "jstart",jstart," icont",icont(1,jstart),
570 !     &   " ifrag",ifrag(1,i,jfrag)
571         if (icont(1,jstart).lt.ifrag(1,i,jfrag)) goto 11
572         npiece_c=npiece_c+1
573         ic1=icont(1,jstart)
574         ifrag_c(1,npiece_c)=icont(1,jstart)
575         jend=ncont
576         do while (jend.gt.1 .and. icont(1,jend).gt.ifrag(2,i,jfrag))
577 !          write (iout,*) "jend",jend," icont",icont(1,jend),
578 !     &     " ifrag",ifrag(2,i,jfrag)
579           jend=jend-1
580         enddo
581 !        write (iout,*) "jend",jend," icont",icont(1,jend),
582 !     &   " ifrag",ifrag(2,i,jfrag)
583         ic2=icont(1,jend)
584         ifrag_c(2,npiece_c)=icont(1,jend)+1
585         ishift_c(npiece_c)=ishif1
586 !        write (iout,*) "1: i",i," jstart:",jstart," jend",jend,
587 !     &    " ic1",ic1," ic2",ic2,
588 !     &    " ifrag",ifrag(1,i,jfrag),ifrag(2,i,jfrag)
589    11   continue
590         if (ncont.eq.1 .or. icont(2,ncont).gt.icont(2,1)) then
591           idi=1
592         else
593           idi=-1
594         endif
595 !        write (iout,*) "idi",idi
596         if (idi.eq.1) then
597           if (icont(2,1).gt.ifrag(2,i,jfrag) .or. &
598             icont(2,ncont).lt.ifrag(1,i,jfrag)) goto 12
599           jstart=1
600           do while (jstart.lt.ncont .and. &
601            icont(2,jstart).lt.ifrag(1,i,jfrag))
602 !           write (iout,*) "jstart",jstart," icont",icont(2,jstart),
603 !     &     " ifrag",ifrag(1,i,jfrag)
604             jstart=jstart+1
605           enddo
606 !          write (iout,*) "jstart",jstart," icont",icont(2,jstart),
607 !     &     " ifrag",ifrag(1,i,jfrag)
608           if (icont(2,jstart).lt.ifrag(1,i,jfrag)) goto 12
609           npiece_c=npiece_c+1
610           ic1=icont(2,jstart)
611           ifrag_c(2,npiece_c)=icont(2,jstart)+1
612           jend=ncont
613           do while (jend.gt.1 .and. icont(2,jend).gt.ifrag(2,i,jfrag))
614 !            write (iout,*) "jend",jend," icont",icont(2,jend),
615 !     &     " ifrag",ifrag(2,i,jfrag)
616             jend=jend-1
617           enddo
618 !          write (iout,*) "jend",jend," icont",icont(2,jend),
619 !     &     " ifrag",ifrag(2,i,jfrag)
620         else if (idi.eq.-1) then
621           if (icont(2,ncont).gt.ifrag(2,i,jfrag) .or. &
622               icont(2,1).lt.ifrag(1,i,jfrag)) goto 12
623           jstart=ncont
624           do while (jstart.gt.ncont .and. &
625            icont(2,jstart).lt.ifrag(1,i,jfrag))
626 !           write (iout,*) "jstart",jstart," icont",icont(2,jstart),
627 !     &     " ifrag",ifrag(1,i,jfrag)
628             jstart=jstart-1
629           enddo
630 !          write (iout,*) "jstart",jstart," icont",icont(2,jstart),
631 !     &     " ifrag",ifrag(1,i,jfrag)
632           if (icont(2,jstart).lt.ifrag(1,i,jfrag)) goto 12
633           npiece_c=npiece_c+1
634           ic1=icont(2,jstart)
635           ifrag_c(2,npiece_c)=icont(2,jstart)+1
636           jend=1
637           do while (jend.lt.ncont .and. &
638              icont(2,jend).gt.ifrag(2,i,jfrag))
639 !             write (iout,*) "jend",jend," icont",icont(2,jend),
640 !     &         " ifrag",ifrag(2,i,jfrag)
641             jend=jend+1
642           enddo
643 !          write (iout,*) "jend",jend," icont",icont(2,jend),
644 !     &     " ifrag",ifrag(2,i,jfrag)
645         endif
646         ic2=icont(2,jend)
647         if (ic2.lt.ic1) then
648           iic = ic1
649           ic1 = ic2
650           ic2 = iic
651         endif
652 !        write (iout,*) "2: i",i," ic1",ic1," ic2",ic2,
653 !     &    " jstart:",jstart," jend",jend,
654 !     &    " ifrag",ifrag(1,i,jfrag),ifrag(2,i,jfrag)
655         ifrag_c(1,npiece_c)=ic1
656         ifrag_c(2,npiece_c)=ic2+1
657         ishift_c(npiece_c)=ishif2
658    12   continue
659       enddo
660       if (lprn) then
661         write (iout,*) "Before merge: npiece_c",npiece_c
662         do i=1,npiece_c
663           write (iout,*) ifrag_c(1,i),ifrag_c(2,i),ishift_c(i)
664         enddo
665       endif
666 !
667 ! Merge overlapping segments (e.g., avoid splitting helices)
668 !
669       i=1
670       do while (i .lt. npiece_c)
671         if (ishift_c(i).eq.ishift_c(i+1) .and. &
672            ifrag_c(2,i).gt.ifrag_c(1,i+1)) then
673            ifrag_c(2,i)=ifrag_c(2,i+1)
674            do j=i+1,npiece_c
675              ishift_c(j)=ishift_c(j+1)
676              ifrag_c(1,j)=ifrag_c(1,j+1)
677              ifrag_c(2,j)=ifrag_c(2,j+1)
678            enddo
679            npiece_c=npiece_c-1
680         else
681           i=i+1
682         endif
683       enddo
684       if (lprn) then
685         write (iout,*) "After merge: npiece_c",npiece_c
686         do i=1,npiece_c
687           write (iout,*) ifrag_c(1,i),ifrag_c(2,i),ishift_c(i)
688         enddo
689       endif
690 !
691 ! Compare angles
692 !
693       angn=0.0d0
694       anorm=0
695       nn = 0
696       fract = 1.0d0
697       npart = npiece_c
698       do i=1,npart
699         ishifc=ishift_c(i)
700         nbeg = ifrag_c(1,i) + 3 - ishifc
701         if (nbeg.lt.nn4) nbeg=nn4
702         nend = ifrag_c(2,i)  - ishifc + 1
703         if (nend.gt.nne) nend=nne
704         if (nend.ge.nbeg) then
705         nn = nn + nend - nbeg + 1
706         if (lprn) write (iout,*) "i=",i," nbeg",nbeg," nend",nend,&
707           " nn",nn," ishifc",ishifc
708         if (lprn) write (iout,*) "angles"
709         longest=0
710         ll = 0
711         do j=nbeg,nend
712 !          deltang = pinorm(phi(j)-phi_ref(j+ishifc))
713           deltang=spherang(phi_ref(j+ishifc),theta_ref(j-1+ishifc),&
714             theta_ref(j+ishifc),phi(j),theta(j-1),theta(j))
715           if (dabs(deltang).gt.diffang_max) then
716             if (ll.gt.longest) longest = ll
717             ll = 0
718           else
719             ll=ll+1
720           endif
721           if (ll.gt.longest) longest = ll
722           if (lprn) write (iout,'(i5,3f10.5)')j,rad2deg*phi(j),&
723            rad2deg*phi_ref(j+ishifc),rad2deg*deltang
724           angn=angn+dabs(deltang)
725         enddo
726         longest=longest+3
727         ff = dfloat(longest)/dfloat(nend - nbeg + 4)
728         if (lprn) write (iout,*)"segment",i," longest fragment within",&
729           diffang_max*rad2deg,":",longest," fraction",ff
730         if (ff.lt.fract) fract = ff
731         endif
732       enddo
733       if (nn.gt.0) anorm = angn/nn
734       if (lprn) write (iout,*) "nn",nn," norm",anorm," fract:",fract
735       return
736       end subroutine angnorm2
737 !-------------------------------------------------------------------------
738       real(kind=8) function angnorm1(nang_pair,iang_pair,lprn)
739
740       use geometry_data, only:phi,theta,rad2deg
741 !      implicit real*8 (a-h,o-z)
742 !      include 'DIMENSIONS'
743 !      include 'DIMENSIONS.ZSCOPT'
744 !      include 'DIMENSIONS.COMPAR'
745 !      include 'COMMON.IOUNITS'
746 !      include 'COMMON.VAR'
747 !      include 'COMMON.COMPAR'
748 !      include 'COMMON.CHAIN'
749 !      include 'COMMON.GEO'
750       logical :: lprn
751       integer :: nang_pair,iang_pair(2,nres)
752       real(kind=8) :: pinorm
753       integer :: j,ia1,ia2
754       real(kind=8) :: angn,deltang
755       angn=0.0d0
756       if (lprn) write (iout,'(80(1h*))')
757       if (lprn) write (iout,*) "nang_pair",nang_pair
758       if (lprn) write (iout,*) "angles"
759       do j=1,nang_pair
760         ia1 = iang_pair(1,j)
761         ia2 = iang_pair(2,j)
762 !        deltang = pinorm(phi(ia1)-phi_ref(ia2))
763          deltang=spherang(phi_ref(ia2),theta_ref(ia2-1),&
764             theta_ref(ia2),phi(ia2),theta(ia2-1),theta(ia2))
765         if (lprn) write (iout,'(3i5,3f10.5)')j,ia1,ia2,rad2deg*phi(ia1),&
766          rad2deg*phi_ref(ia2),rad2deg*deltang
767         angn=angn+dabs(deltang)
768       enddo
769       if (lprn) &
770       write (iout,*)"nang_pair",nang_pair," angn",rad2deg*angn/nang_pair
771       angnorm1 = angn/nang_pair
772       return
773       end function angnorm1
774 !------------------------------------------------------------------------------
775       subroutine angnorm12(diff)
776
777       use geometry_data, only:phi,theta,nstart_sup,nend_sup
778 !      implicit real*8 (a-h,o-z)
779 !      include 'DIMENSIONS'
780 !      include 'DIMENSIONS.ZSCOPT'
781 !      include 'DIMENSIONS.COMPAR'
782 !      include 'COMMON.IOUNITS'
783 !      include 'COMMON.VAR'
784 !      include 'COMMON.COMPAR'
785 !      include 'COMMON.CHAIN'
786 !      include 'COMMON.GEO'
787       real(kind=8) :: pinorm,diff
788       integer :: nn4,nne,j
789       diff=0.0d0
790       nn4 = nstart_sup+3
791       nne = min0(nend_sup,nres)
792 !      do j=nn4-1,nne
793 !        diff = diff+rad2deg*dabs(pinorm(theta(j)-theta_ref(j)))
794 !      enddo
795       do j=nn4,nne 
796 !        diff = diff+rad2deg*dabs(pinorm(phi(j)-phi_ref(j)))
797          diff=diff+spherang(phi_ref(j),theta_ref(j-1),&
798             theta_ref(j),phi(j),theta(j-1),theta(j))
799       enddo
800       return
801       end subroutine angnorm12
802 !--------------------------------------------------------------------------------
803       real(kind=8) function spherang(gam1,theta11,theta12,&
804          gam2,theta21,theta22)
805 !      implicit none
806       use geometry, only:arcos
807       real(kind=8) :: gam1,theta11,theta12,gam2,theta21,theta22,&
808         x1,x2,xmed,f1,f2,fmed
809       real(kind=8) :: tolx=1.0d-4, tolf=1.0d-4
810       real(kind=8) :: sumcos
811 !el      real(kind=8) :: pinorm,sumangp !arcos,
812       integer :: it,maxit=100
813 ! Calculate the difference of the angles of two superposed 4-redidue fragments
814 !
815 !       O      P
816 !        \    /
817 !     O'--C--C       
818 !             \
819 !              P'
820 !
821 ! The fragment O'-C-C-P' is rotated by angle fi about the C-C axis
822 ! to achieve the minimum difference between the O'-C-O and P-C-P angles;
823 ! the sum of these angles is the difference returned by the function.
824 !
825 ! 4/28/04 AL
826 ! If thetas match, take the difference of gamma and exit.
827       if (dabs(theta11-theta12).lt.tolx &
828        .and. dabs(theta21-theta22).lt.tolx) then
829         spherang=dabs(pinorm(gam2-gam1))
830         return
831       endif
832 ! If the gammas are the same, take the difference of thetas and exit.
833       x1=0.0d0
834       x2=0.5d0*pinorm(gam2-gam1)
835       if (dabs(x2) .lt. tolx) then
836         spherang=dabs(theta11-theta21)+dabs(theta12-theta22)
837         return
838       else if (x2.lt.0.0d0) then
839         x1=x2
840         x2=0.0d0
841       endif 
842 ! Else apply regula falsi method to compute optimum overlap of the terminal Calphas
843       f1=sumangp(gam1,theta11,theta12,gam2,theta21,theta22,x1)
844       f2=sumangp(gam1,theta11,theta12,gam2,theta21,theta22,x2)
845       do it=1,maxit
846         xmed=x1-f1*(x2-x1)/(f2-f1)
847         fmed=sumangp(gam1,theta11,theta12,gam2,theta21,theta22,xmed)
848 !        write (*,*) 'it',it,' xmed ',xmed,' fmed ',fmed
849         if ( (dabs(xmed-x1).lt.tolx .or. dabs(x2-xmed).lt.tolx) &
850              .and. dabs(fmed).lt.tolf ) then
851           x1=xmed
852           f1=fmed
853           goto 10
854         else if ( fmed*f1.lt.0.0d0 ) then
855           x2=xmed
856           f2=fmed
857         else
858           x1=xmed
859           f1=fmed
860         endif
861       enddo
862    10 continue
863       spherang=arcos(dcos(theta11)*dcos(theta12) &
864        +dsin(theta11)*dsin(theta12)*dcos(x1))+ &
865        arcos(dcos(theta21)*dcos(theta22)+ &
866        dsin(theta21)*dsin(theta22)*dcos(gam2-gam1+x1))
867       return
868       end function spherang
869 !--------------------------------------------------------------------------------
870       real(kind=8) function sumangp(gam1,theta11,theta12,gam2,&
871        theta21,theta22,fi)
872 !      implicit none
873       real(kind=8) :: gam1,theta11,theta12,gam2,theta21,theta22,fi,&
874        cost11,cost12,cost21,cost22,sint11,sint12,sint21,sint22,cosd1,&
875        cosd2
876 ! derivarive of the sum of the difference of the angles of a 4-residue fragment.
877 !      real(kind=8) :: arcos
878       cost11=dcos(theta11)
879       cost12=dcos(theta12)
880       cost21=dcos(theta21)
881       cost22=dcos(theta22)
882       sint11=dsin(theta11)
883       sint12=dsin(theta12)
884       sint21=dsin(theta21)
885       sint22=dsin(theta22)
886       cosd1=cost11*cost12+sint11*sint12*dcos(fi)
887       cosd2=cost21*cost22+sint21*sint22*dcos(gam2-gam1+fi)
888       sumangp=sint11*sint12*dsin(fi)/dsqrt(1.0d0-cosd1*cosd1) &
889        +sint21*sint22*dsin(gam2-gam1+fi)/dsqrt(1.0d0-cosd2*cosd2)
890       return
891       end function sumangp
892 !-----------------------------------------------------------------------------
893 ! contact.f
894 !-----------------------------------------------------------------------------
895       subroutine contact(lprint,ncont,icont,ist,ien)
896
897       use calc_data
898       use geometry_data, only:c,dc,dc_norm
899       use energy_data, only:itype,maxcont,molnum
900 !      implicit none
901 !      include 'DIMENSIONS'
902 !      include 'DIMENSIONS.ZSCOPT'
903 !      include 'COMMON.CONTROL'
904 !      include 'COMMON.IOUNITS'
905 !      include 'COMMON.CHAIN'
906 !      include 'COMMON.INTERACT'
907 !      include 'COMMON.FFIELD'
908 !      include 'COMMON.NAMES'
909 !      include 'COMMON.CALC'
910 !      include 'COMMON.CONTPAR'
911 !      include 'COMMON.LOCAL'
912       integer :: ist,ien,kkk,iti,itj,itypi,itypj,i1,i2,it1,it2,mnum,mnum2
913       real(kind=8) :: csc !el,dist
914       real(kind=8),dimension(maxcont) :: cscore,omt1,omt2,omt12,&
915           ddsc,ddla,ddlb
916       integer :: ncont,mhum
917       integer,dimension(2,maxcont) :: icont
918       real(kind=8) :: u,v,a(3),b(3),dla,dlb
919       logical :: lprint
920 !el-------
921       dla=0.0d0
922       dlb=0.0d0
923 !el------
924       ncont=0
925       kkk=3
926       if (lprint) then
927       do i=1,nres
928         mnum=molnum(i)
929         write (iout,110) restyp(itype(i,mnum),mnum),i,c(1,i),c(2,i),&
930           c(3,i),dc(1,nres+i),dc(2,nres+i),dc(3,nres+i),&
931           dc_norm(1,nres+i),dc_norm(2,nres+i),dc_norm(3,nres+i)
932       enddo
933       endif
934   110 format (a,'(',i3,')',9f8.3)
935       do i=ist,ien-kkk
936         mnum=molnum(i)
937         iti=iabs(itype(i,mnum))
938         if (iti.le.0 .or. iti.gt.ntyp_molec(mnum)) cycle
939         do j=i+kkk,ien
940           mnum2=molnum(j)
941           itj=iabs(itype(j,mnum2))
942           if (itj.le.0 .or. itj.gt.ntyp_molec(mnum2)) cycle
943           itypi=iti
944           itypj=itj
945           xj = c(1,nres+j)-c(1,nres+i)    
946           yj = c(2,nres+j)-c(2,nres+i)    
947           zj = c(3,nres+j)-c(3,nres+i)    
948           dxi = dc_norm(1,nres+i)
949           dyi = dc_norm(2,nres+i)
950           dzi = dc_norm(3,nres+i)
951           dxj = dc_norm(1,nres+j)
952           dyj = dc_norm(2,nres+j)
953           dzj = dc_norm(3,nres+j)
954           do k=1,3
955             a(k)=dc(k,nres+i)
956             b(k)=dc(k,nres+j)
957           enddo
958 !          write (iout,*) (a(k),k=1,3),(b(k),k=1,3)
959           if (icomparfunc.eq.1) then
960             call contfunc(csc,iti,itj)
961           else if (icomparfunc.eq.2) then
962             call scdist(csc,iti,itj)
963           else if (icomparfunc.eq.3 .or. icomparfunc.eq.5) then
964             csc = dist(nres+i,nres+j)
965           else if (icomparfunc.eq.4) then
966             call odlodc(c(1,i),c(1,j),a,b,u,v,dla,dlb,csc)
967           else
968             write (*,*) "Error - Unknown sidechain contact function"
969             write (iout,*) "Error - Unknown sidechain contact function"
970           endif
971           if (csc.lt.sc_cutoff(iti,itj)) then
972 !            write(iout,*) "i",i," j",j," dla",dla,dsc(iti),
973 !     &      " dlb",dlb,dsc(itj)," csc",csc,sc_cutoff(iti,itj),
974 !     &      dxi,dyi,dzi,dxi**2+dyi**2+dzi**2,
975 !     &      dxj,dyj,dzj,dxj**2+dyj**2+dzj**2,om1,om2,om12,
976 !     &      xj,yj,zj
977 !            write(iout,*)'egb',itypi,itypj,chi1,chi2,chip1,chip2,
978 !     &       sig0ij,rij,rrij,om1,om2,om12,chiom1,chiom2,chiom12,
979 !     &       chipom1,chipom2,chipom12,sig,eps2rt,rij_shift,e2,evdw,
980 !     &       csc
981             ncont=ncont+1
982             cscore(ncont)=csc
983             icont(1,ncont)=i
984             icont(2,ncont)=j
985             omt1(ncont)=om1
986             omt2(ncont)=om2
987             omt12(ncont)=om12
988             ddsc(ncont)=1.0d0/rij
989             ddla(ncont)=dla
990             ddlb(ncont)=dlb
991           endif
992         enddo
993       enddo
994       if (lprint) then
995         write (iout,'(a)') 'Contact map:'
996         do i=1,ncont
997           mnum=molnum(i)
998           i1=icont(1,i)
999           i2=icont(2,i)
1000           it1=itype(i1,1)
1001           it2=itype(i2,1)
1002           if (mnum.eq.0) mnum=1
1003           print *,"CONTACT",i1,i2,mnum,it1,it2
1004           write (iout,'(i3,2x,a,i4,2x,a,i4,5f8.3,3f10.5)') &
1005            i,restyp(it1,mnum),i1,restyp(it2,mnum),i2,cscore(i),&
1006            sc_cutoff(iabs(it1),iabs(it2)),ddsc(i),ddla(i),ddlb(i),&
1007            omt1(i),omt2(i),omt12(i)
1008         enddo
1009       endif
1010       return
1011       end subroutine contact
1012 #else
1013 !----------------------------------------------------------------------------
1014       subroutine contact(lprint,ncont,icont)
1015
1016       use energy_data, only: nnt,nct,itype,ipot,maxcont,sigma,sigmaii,molnum
1017 !      include 'DIMENSIONS'
1018 !      include 'COMMON.IOUNITS'
1019 !      include 'COMMON.CHAIN'
1020 !      include 'COMMON.INTERACT'
1021 !      include 'COMMON.FFIELD'
1022 !      include 'COMMON.NAMES'
1023       real(kind=8) :: facont=1.569D0  ! facont = (2/(1-sqrt(1-1/4)))**(1/6)
1024       integer :: ncont,icont(2,maxcont)
1025       logical :: lprint
1026       integer :: kkk,i,j,i1,i2,it1,it2,iti,itj
1027       real(kind=8) :: rcomp
1028       integer :: mnum,mnum2
1029       ncont=0
1030       kkk=3
1031 !     print *,'nnt=',nnt,' nct=',nct
1032       do i=nnt+kkk,nct
1033         mnum=molnum(i)
1034         iti=iabs(itype(i,1))
1035         do j=nnt,i-kkk
1036           mnum2=molnum(j)
1037           itj=iabs(itype(j,1))
1038           if (ipot.ne.4) then
1039 !           rcomp=sigmaii(iti,itj)+1.0D0
1040             rcomp=facont*sigmaii(iti,itj)
1041           else
1042 !           rcomp=sigma(iti,itj)+1.0D0
1043             rcomp=facont*sigma(iti,itj)
1044           endif
1045 !         rcomp=6.5D0
1046 !         print *,'rcomp=',rcomp,' dist=',dist(nres+i,nres+j)
1047           if (dist(nres+i,nres+j).lt.rcomp) then
1048             ncont=ncont+1
1049             icont(1,ncont)=i
1050             icont(2,ncont)=j
1051           endif
1052         enddo
1053       enddo
1054       if (lprint) then
1055         write (iout,'(a)') 'Contact map:'
1056         do i=1,ncont
1057            mnum=molnum(i)
1058           i1=icont(1,i)
1059           i2=icont(2,i)
1060           it1=itype(i1,1)
1061           it2=itype(i2,1)
1062           write(iout,*) "test",i1,i2,it1,it2
1063           write (iout,'(i3,2x,a,i4,2x,a,i4)') &
1064            i,restyp(it1,mnum),i1,restyp(it2,mnum),i2
1065         enddo
1066       endif
1067       return
1068       end subroutine contact
1069 #endif
1070 !----------------------------------------------------------------------------
1071       real(kind=8) function contact_fract(ncont,ncont_ref,&
1072                                            icont,icont_ref)
1073
1074       use energy_data, only:maxcont
1075 !      implicit none
1076 !      include 'DIMENSIONS'
1077 !      include 'COMMON.IOUNITS'
1078       integer :: i,j,nmatch
1079       integer :: ncont,ncont_ref
1080       integer,dimension(2,maxcont) :: icont,icont_ref
1081       nmatch=0
1082 !     print *,'ncont=',ncont,' ncont_ref=',ncont_ref 
1083 !     write (iout,'(20i4)') (icont_ref(1,i),i=1,ncont_ref)
1084 !     write (iout,'(20i4)') (icont_ref(2,i),i=1,ncont_ref)
1085 !     write (iout,'(20i4)') (icont(1,i),i=1,ncont)
1086 !     write (iout,'(20i4)') (icont(2,i),i=1,ncont)
1087       do i=1,ncont
1088         do j=1,ncont_ref
1089           if (icont(1,i).eq.icont_ref(1,j) .and. &
1090               icont(2,i).eq.icont_ref(2,j)) nmatch=nmatch+1
1091         enddo
1092       enddo
1093 !     print *,' nmatch=',nmatch
1094 !     contact_fract=dfloat(nmatch)/dfloat(max0(ncont,ncont_ref))
1095       contact_fract=dfloat(nmatch)/dfloat(ncont_ref)
1096       return
1097       end function contact_fract
1098 #ifndef CLUSTER
1099 !------------------------------------------------------------------------------
1100       subroutine pept_cont(lprint,ncont,icont)
1101
1102       use geometry_data, only:c
1103       use energy_data, only:maxcont,nnt,nct,itype,molnum
1104 !      implicit none
1105 !      include 'DIMENSIONS'
1106 !      include 'DIMENSIONS.ZSCOPT'
1107 !      include 'COMMON.IOUNITS'
1108 !      include 'COMMON.CHAIN'
1109 !      include 'COMMON.INTERACT'
1110 !      include 'COMMON.FFIELD'
1111 !      include 'COMMON.NAMES'
1112       integer :: ncont,icont(2,maxcont)
1113       integer :: i,j,k,kkk,i1,i2,it1,it2,mnum
1114       logical :: lprint
1115 !el      real(kind=8) :: dist
1116       real(kind=8) :: rcomp=5.5d0
1117       ncont=0
1118       kkk=0
1119       print *,'Entering pept_cont: nnt=',nnt,' nct=',nct
1120       do i=nnt,nct-3
1121         do k=1,3
1122           c(k,2*nres+1)=0.5d0*(c(k,i)+c(k,i+1))
1123         enddo
1124         do j=i+2,nct-1
1125           do k=1,3
1126             c(k,2*nres+2)=0.5d0*(c(k,j)+c(k,j+1))
1127           enddo
1128           if (dist(2*nres+1,2*nres+2).lt.rcomp) then
1129             ncont=ncont+1
1130             icont(1,ncont)=i
1131             icont(2,ncont)=j
1132           endif
1133         enddo
1134       enddo
1135       if (lprint) then
1136         write (iout,'(a)') 'PP contact map:'
1137         do i=1,ncont
1138           mnum=molnum(i)
1139           i1=icont(1,i)
1140           i2=icont(2,i)
1141           it1=itype(i1,mnum)
1142           it2=itype(i2,mnum)
1143           write (iout,'(i3,2x,a,i4,2x,a,i4)') &
1144            i,restyp(it1,mnum),i1,restyp(it2,mnum),i2
1145         enddo
1146       endif
1147       return
1148       end subroutine pept_cont
1149 !-----------------------------------------------------------------------------
1150 ! cont_frag.f
1151 !-----------------------------------------------------------------------------
1152       subroutine contacts_between_fragments(lprint,is,ncont,icont,&
1153          ncont_interfrag,icont_interfrag)
1154
1155       use energy_data, only:itype,maxcont,molnum
1156 !      include 'DIMENSIONS'
1157 !      include 'DIMENSIONS.ZSCOPT'
1158 !      include 'DIMENSIONS.COMPAR'
1159 !      include 'COMMON.INTERACT'
1160 !      include 'COMMON.COMPAR'
1161 !      include 'COMMON.IOUNITS'
1162 !      include 'COMMON.CHAIN'
1163 !      include 'COMMON.NAMES'
1164       integer :: icont(2,maxcont),ncont_interfrag(mmaxfrag),&
1165         icont_interfrag(2,maxcont,mmaxfrag)
1166       logical :: OK1,OK2,lprint
1167       integer :: is,ncont,i,j,ind,nc,k,ic1,ic2,l,i1,i2,it1,it2,mnum
1168 ! Determine the contacts that occur within a fragment and between fragments.
1169       do i=1,nfrag(1)
1170         do j=1,i
1171           ind = icant(i,j)
1172           nc=0
1173 !          write (iout,*) "i",i,(ifrag(1,k,i),ifrag(2,k,i)
1174 !     &      ,k=1,npiece(i,1))
1175 !          write (iout,*) "j",j,(ifrag(1,k,j),ifrag(2,k,j)
1176 !     &      ,k=1,npiece(j,1))
1177 !          write (iout,*) "ncont",ncont
1178           do k=1,ncont
1179             ic1=icont(1,k)
1180             ic2=icont(2,k)
1181             OK1=.false.
1182             l=0
1183             do while (.not.OK1 .and. l.lt.npiece(j,1)) 
1184               l=l+1
1185               OK1=ic1.ge.ifrag(1,l,j)-is .and. &
1186                ic1.le.ifrag(2,l,j)+is
1187             enddo
1188             OK2=.false.
1189             l=0
1190             do while (.not.OK2 .and. l.lt.npiece(i,1)) 
1191               l=l+1
1192               OK2=ic2.ge.ifrag(1,l,i)-is .and. &
1193                ic2.le.ifrag(2,l,i)+is
1194             enddo 
1195 !            write(iout,*) "k",k," ic1",ic1," ic2",ic2," OK1",OK1,
1196 !     &        " OK2",OK2
1197             if (OK1.and.OK2) then
1198               nc=nc+1
1199               icont_interfrag(1,nc,ind)=ic1 
1200               icont_interfrag(2,nc,ind)=ic2 
1201 !              write (iout,*) "nc",nc," ic1",ic1," ic2",ic2
1202             endif
1203           enddo
1204           ncont_interfrag(ind)=nc
1205 !          do k=1,ncont_interfrag(ind)
1206 !              i1=icont_interfrag(1,k,ind)
1207 !              i2=icont_interfrag(2,k,ind)
1208 !              it1=itype(i1)
1209 !              it2=itype(i2)
1210 !              write (iout,'(i3,2x,a,i4,2x,a,i4)')
1211 !     &          i,restyp(it1),i1,restyp(it2),i2
1212 !          enddo
1213         enddo
1214       enddo
1215       if (lprint) then
1216         write (iout,*) "Contacts within fragments:"
1217         do i=1,nfrag(1)
1218           write (iout,*) "Fragment",i," (",(ifrag(1,k,i),&
1219            ifrag(2,k,i),k=1,npiece(i,1)),")"
1220           ind=icant(i,i)
1221           do k=1,ncont_interfrag(ind)
1222             i1=icont_interfrag(1,k,ind)
1223             i2=icont_interfrag(2,k,ind)
1224             mnum=molnum(i1)
1225             it1=itype(i1,mnum)
1226             it2=itype(i2,molnum(i2))
1227             write (iout,'(i3,2x,a,i4,2x,a,i4)') &
1228               i,restyp(it1,mnum),i1,restyp(it2,molnum(i2)),i2
1229           enddo
1230         enddo
1231         write (iout,*)
1232         write (iout,*) "Contacts between fragments:"
1233         do i=1,nfrag(1)
1234         do j=1,i-1
1235           ind = icant(i,j)
1236           write (iout,*) "Fragments",i," (",(ifrag(1,k,i),&
1237            ifrag(2,k,i),k=1,npiece(i,1)),") and",j," (",&
1238            (ifrag(1,k,j),ifrag(2,k,j),k=1,npiece(j,1)),")"
1239           write (iout,*) "Number of contacts",&
1240            ncont_interfrag(ind)
1241           ind=icant(i,j)
1242           do k=1,ncont_interfrag(ind)
1243             i1=icont_interfrag(1,k,ind)
1244             i2=icont_interfrag(2,k,ind)
1245             mnum=molnum(i1)
1246             it1=itype(i1,mnum)
1247             it2=itype(i2,molnum(i2))
1248             write (iout,'(i3,2x,a,i4,2x,a,i4)') &
1249               i,restyp(it1,mnum),i1,restyp(it2,molnum(i2)),i2
1250           enddo
1251         enddo
1252         enddo
1253       endif
1254       return
1255       end subroutine contacts_between_fragments
1256 !-----------------------------------------------------------------------------
1257 ! contfunc.f 
1258 !-----------------------------------------------------------------------------
1259       subroutine contfunc(cscore,itypi,itypj)
1260 !
1261 ! This subroutine calculates the contact function based on
1262 ! the Gay-Berne potential of interaction.
1263 !
1264       use calc_data
1265 !      implicit real*8 (a-h,o-z)
1266 !      include 'DIMENSIONS'
1267 !      include 'COMMON.CONTPAR'
1268 !      include 'COMMON.CALC'
1269       integer :: expon=6
1270       integer :: itypi,itypj
1271       real(kind=8) :: cscore,sig0ij,rrij,sig,rij_shift,evdw,e2
1272 !
1273       sig0ij=sig_comp(itypi,itypj)
1274       chi1=chi_comp(itypi,itypj)
1275       chi2=chi_comp(itypj,itypi)
1276       chi12=chi1*chi2
1277       chip1=chip_comp(itypi,itypj)
1278       chip2=chip_comp(itypj,itypi)
1279       chip12=chip1*chip2
1280       rrij=1.0D0/(xj*xj+yj*yj+zj*zj)
1281       rij=dsqrt(rrij)
1282 ! Calculate angle-dependent terms of the contact function
1283       erij(1)=xj*rij
1284       erij(2)=yj*rij
1285       erij(3)=zj*rij
1286       om1=dxi*erij(1)+dyi*erij(2)+dzi*erij(3)
1287       om2=dxj*erij(1)+dyj*erij(2)+dzj*erij(3)
1288       om12=dxi*dxj+dyi*dyj+dzi*dzj
1289       chiom12=chi12*om12
1290 !      print *,'egb',itypi,itypj,chi1,chi2,chip1,chip2,
1291 !     &  sig0ij,
1292 !     &  rij,rrij,om1,om2,om12
1293 ! Calculate eps1(om12)
1294       faceps1=1.0D0-om12*chiom12
1295       faceps1_inv=1.0D0/faceps1
1296       eps1=dsqrt(faceps1_inv)
1297 ! Following variable is eps1*deps1/dom12
1298       eps1_om12=faceps1_inv*chiom12
1299 ! Calculate sigma(om1,om2,om12)
1300       om1om2=om1*om2
1301       chiom1=chi1*om1
1302       chiom2=chi2*om2
1303       facsig=om1*chiom1+om2*chiom2-2.0D0*om1om2*chiom12
1304       sigsq=1.0D0-facsig*faceps1_inv
1305 ! Calculate eps2 and its derivatives in om1, om2, and om12.
1306       chipom1=chip1*om1
1307       chipom2=chip2*om2
1308       chipom12=chip12*om12
1309       facp=1.0D0-om12*chipom12
1310       facp_inv=1.0D0/facp
1311       facp1=om1*chipom1+om2*chipom2-2.0D0*om1om2*chipom12
1312 ! Following variable is the square root of eps2
1313       eps2rt=1.0D0-facp1*facp_inv
1314       sigsq=1.0D0/sigsq
1315       sig=sig0ij*dsqrt(sigsq)
1316       rij_shift=1.0D0/rij-sig+sig0ij
1317       if (rij_shift.le.0.0D0) then
1318         evdw=1.0D1
1319         cscore = -dlog(evdw+1.0d-6)  
1320         return
1321       endif
1322       rij_shift=1.0D0/rij_shift 
1323       e2=(rij_shift*sig0ij)**expon
1324       evdw=dabs(eps1*eps2rt**2*e2)
1325       if (evdw.gt.1.0d1) evdw = 1.0d1
1326       cscore = -dlog(evdw+1.0d-6) 
1327       return
1328       end subroutine contfunc
1329 !------------------------------------------------------------------------------
1330       subroutine scdist(cscore,itypi,itypj)
1331 !
1332 ! This subroutine calculates the contact distance
1333 !
1334       use calc_data
1335 !      implicit real*8 (a-h,o-z)
1336 !      include 'DIMENSIONS'
1337 !      include 'COMMON.CONTPAR'
1338 !      include 'COMMON.CALC'
1339       integer :: itypi,itypj
1340       real(kind=8) :: cscore,rrij
1341
1342       chi1=chi_comp(itypi,itypj)
1343       chi2=chi_comp(itypj,itypi)
1344       chi12=chi1*chi2
1345       rrij=xj*xj+yj*yj+zj*zj
1346       rij=dsqrt(rrij)
1347 ! Calculate angle-dependent terms of the contact function
1348       erij(1)=xj/rij
1349       erij(2)=yj/rij
1350       erij(3)=zj/rij
1351       om1=dxi*erij(1)+dyi*erij(2)+dzi*erij(3)
1352       om2=dxj*erij(1)+dyj*erij(2)+dzj*erij(3)
1353       om12=dxi*dxj+dyi*dyj+dzi*dzj
1354       chiom12=chi12*om12
1355       om1om2=om1*om2
1356       chiom1=chi1*om1
1357       chiom2=chi2*om2
1358       cscore=dsqrt(rrij+chi1**2+chi2**2+2*rij*(chiom2-chiom1)-2*chiom12)
1359       return
1360       end subroutine scdist
1361 !------------------------------------------------------------------------------
1362 ! elecont.f
1363 !------------------------------------------------------------------------------
1364       subroutine elecont(lprint,ncont,icont,ist,ien)
1365
1366       use geometry_data, only:c
1367       use energy_data, only:maxcont,rpp,epp,itype,itel,vblinv,vblinv2,molnum
1368 !      implicit none
1369 !      include 'DIMENSIONS'
1370 !      include 'DIMENSIONS.ZSCOPT'
1371 !      include 'DIMENSIONS.COMPAR'
1372 !      include 'COMMON.IOUNITS'
1373 !      include 'COMMON.CHAIN'
1374 !      include 'COMMON.INTERACT'
1375 !      include 'COMMON.FFIELD'
1376 !      include 'COMMON.NAMES'
1377 !      include 'COMMON.LOCAL'
1378       logical :: lprint
1379       integer :: i,j,k,ist,ien,iteli,itelj,ind,i1,i2,it1,it2,ic1,ic2
1380       real(kind=8) :: rri,xi,yi,zi,dxi,dyi,dzi,xmedi,ymedi,zmedi,&
1381         xj,yj,zj,dxj,dyj,dzj,aaa,bbb,ael6i,ael3i,rrmij,rmij,r3ij,r6ij,&
1382         vrmij,cosa,cosb,cosg,fac,ev1,ev2,fac3,fac4,evdwij,el1,el2,&
1383         eesij,ees,evdw,ene
1384       real(kind=8),dimension(2,2) :: elpp6c=reshape((/-0.2379d0,&
1385                        -0.2056d0,-0.2056d0,-0.0610d0/),shape(elpp6c))
1386       real(kind=8),dimension(2,2) :: elpp3c=reshape((/ 0.0503d0,&
1387                         0.0000d0, 0.0000d0, 0.0692d0/),shape(elpp3c))
1388       real(kind=8),dimension(2,2) :: ael6c,ael3c,appc,bppc
1389       real(kind=8) :: elcutoff=-0.3d0
1390       real(kind=8) :: elecutoff_14=-0.5d0
1391       integer :: ncont,icont(2,maxcont),mnum
1392       real(kind=8) :: econt(maxcont)
1393 !
1394 ! Load the constants of peptide bond - peptide bond interactions.
1395 ! Type 1 - ordinary peptide bond, type 2 - alkylated peptide bond (e.g.
1396 ! proline) - determined by averaging ECEPP energy.      
1397 !
1398 ! as of 7/06/91.
1399 !
1400 !      data epp    / 0.3045d0, 0.3649d0, 0.3649d0, 0.5743d0/
1401 !      data rpp    / 4.5088d0, 4.5395d0, 4.5395d0, 4.4846d0/
1402 !el      data (elpp6c)   /-0.2379d0,-0.2056d0,-0.2056d0,-0.0610d0/
1403 !el      data (elpp3c)   / 0.0503d0, 0.0000d0, 0.0000d0, 0.0692d0/
1404 !el      data (elcutoff) /-0.3d0/
1405 !el      data (elecutoff_14) /-0.5d0/
1406       ees=0.0d0
1407       evdw=0.0d0
1408       if (lprint) write (iout,'(a)') &
1409         "Constants of electrostatic interaction energy expression."
1410       do i=1,2
1411         do j=1,2
1412         rri=rpp(i,j)**6
1413         appc(i,j)=epp(i,j)*rri*rri 
1414         bppc(i,j)=-2.0*epp(i,j)*rri
1415         ael6c(i,j)=elpp6c(i,j)*4.2**6
1416         ael3c(i,j)=elpp3c(i,j)*4.2**3
1417         if (lprint) &
1418         write (iout,'(2i2,4e15.4)') i,j,appc(i,j),bppc(i,j),ael6c(i,j),&
1419                                      ael3c(i,j)
1420         enddo
1421       enddo
1422       ncont=0
1423       do 1 i=ist,ien-2
1424         xi=c(1,i)
1425         yi=c(2,i)
1426         zi=c(3,i)
1427         dxi=c(1,i+1)-c(1,i)
1428         dyi=c(2,i+1)-c(2,i)
1429         dzi=c(3,i+1)-c(3,i)
1430         xmedi=xi+0.5*dxi
1431         ymedi=yi+0.5*dyi
1432         zmedi=zi+0.5*dzi
1433         do 4 j=i+2,ien-1
1434           ind=ind+1
1435           iteli=itel(i)
1436           itelj=itel(j)
1437           if (j.eq.i+2 .and. itelj.eq.2) iteli=2
1438           if (iteli.eq.2 .and. itelj.eq.2 &
1439             .or.iteli.eq.0 .or.itelj.eq.0) goto 4
1440           aaa=appc(iteli,itelj)
1441           bbb=bppc(iteli,itelj)
1442           ael6i=ael6c(iteli,itelj)
1443           ael3i=ael3c(iteli,itelj) 
1444           dxj=c(1,j+1)-c(1,j)
1445           dyj=c(2,j+1)-c(2,j)
1446           dzj=c(3,j+1)-c(3,j)
1447           xj=c(1,j)+0.5*dxj-xmedi
1448           yj=c(2,j)+0.5*dyj-ymedi
1449           zj=c(3,j)+0.5*dzj-zmedi
1450           rrmij=1.0/(xj*xj+yj*yj+zj*zj)
1451           rmij=sqrt(rrmij)
1452           r3ij=rrmij*rmij
1453           r6ij=r3ij*r3ij  
1454           vrmij=vblinv*rmij
1455           cosa=(dxi*dxj+dyi*dyj+dzi*dzj)*vblinv2      
1456           cosb=(xj*dxi+yj*dyi+zj*dzi)*vrmij
1457           cosg=(xj*dxj+yj*dyj+zj*dzj)*vrmij
1458           fac=cosa-3.0*cosb*cosg
1459           ev1=aaa*r6ij*r6ij
1460           ev2=bbb*r6ij
1461           fac3=ael6i*r6ij
1462           fac4=ael3i*r3ij
1463           evdwij=ev1+ev2
1464           el1=fac3*(4.0+fac*fac-3.0*(cosb*cosb+cosg*cosg))
1465           el2=fac4*fac       
1466           eesij=el1+el2
1467           if (j.gt.i+2 .and. eesij.le.elcutoff .or. &
1468               j.eq.i+2 .and. eesij.le.elecutoff_14) then
1469              ncont=ncont+1
1470              icont(1,ncont)=i
1471              icont(2,ncont)=j
1472              econt(ncont)=eesij
1473           endif
1474           ees=ees+eesij
1475           evdw=evdw+evdwij
1476     4   continue
1477     1 continue
1478       if (lprint) then
1479         write (iout,*) 'Total average electrostatic energy: ',ees
1480         write (iout,*) 'VDW energy between peptide-group centers: ',evdw
1481         write (iout,*)
1482         write (iout,*) 'Electrostatic contacts before pruning: '
1483         do i=1,ncont
1484           i1=icont(1,i)
1485           i2=icont(2,i)
1486           it1=itype(i1,molnum(i1))
1487           it2=itype(i2,molnum(i1))
1488           write (iout,'(i3,2x,a,i4,2x,a,i4,f10.5)') &
1489            i,restyp(it1,molnum(i1)),i1,restyp(it2,molnum(i1)),i2,econt(i)
1490         enddo
1491       endif
1492 ! For given residues keep only the contacts with the greatest energy.
1493       i=0
1494       do while (i.lt.ncont)
1495         i=i+1
1496         ene=econt(i)
1497         ic1=icont(1,i)
1498         ic2=icont(2,i)
1499         j=i
1500         do while (j.lt.ncont)
1501           j=j+1
1502           if (ic1.eq.icont(1,j).and.iabs(icont(2,j)-ic2).le.2 .or. &
1503               ic2.eq.icont(2,j).and.iabs(icont(1,j)-ic1).le.2) then
1504 !            write (iout,*) "i",i," j",j," ic1",ic1," ic2",ic2,
1505 !     &       " jc1",icont(1,j)," jc2",icont(2,j)," ncont",ncont
1506             if (econt(j).lt.ene .and. icont(2,j).ne.icont(1,j)+2) then
1507               if (ic1.eq.icont(1,j)) then
1508                 do k=1,ncont
1509                   if (k.ne.i .and. k.ne.j .and. icont(2,k).eq.icont(2,j)&
1510                      .and. iabs(icont(1,k)-ic1).le.2 .and. &
1511                      econt(k).lt.econt(j) ) goto 21 
1512                 enddo
1513               else if (ic2.eq.icont(2,j) ) then
1514                 do k=1,ncont
1515                   if (k.ne.i .and. k.ne.j .and. icont(1,k).eq.icont(1,j)&
1516                      .and. iabs(icont(2,k)-ic2).le.2 .and. &
1517                      econt(k).lt.econt(j) ) goto 21 
1518                 enddo
1519               endif
1520 ! Remove ith contact
1521               do k=i+1,ncont
1522                 icont(1,k-1)=icont(1,k)
1523                 icont(2,k-1)=icont(2,k)
1524                 econt(k-1)=econt(k) 
1525               enddo
1526               i=i-1
1527               ncont=ncont-1
1528 !              write (iout,*) "ncont",ncont
1529 !              do k=1,ncont
1530 !                write (iout,*) icont(1,k),icont(2,k)
1531 !              enddo
1532               goto 20
1533             else if (econt(j).gt.ene .and. ic2.ne.ic1+2) &
1534             then
1535               if (ic1.eq.icont(1,j)) then
1536                 do k=1,ncont
1537                   if (k.ne.i .and. k.ne.j .and. icont(2,k).eq.ic2 &
1538                      .and. iabs(icont(1,k)-icont(1,j)).le.2 .and. &
1539                      econt(k).lt.econt(i) ) goto 21 
1540                 enddo
1541               else if (ic2.eq.icont(2,j) ) then
1542                 do k=1,ncont
1543                   if (k.ne.i .and. k.ne.j .and. icont(1,k).eq.ic1 &
1544                      .and. iabs(icont(2,k)-icont(2,j)).le.2 .and. &
1545                      econt(k).lt.econt(i) ) goto 21 
1546                 enddo
1547               endif
1548 ! Remove jth contact
1549               do k=j+1,ncont
1550                 icont(1,k-1)=icont(1,k)
1551                 icont(2,k-1)=icont(2,k)
1552                 econt(k-1)=econt(k) 
1553               enddo
1554               ncont=ncont-1
1555 !              write (iout,*) "ncont",ncont
1556 !              do k=1,ncont
1557 !                write (iout,*) icont(1,k),icont(2,k)
1558 !              enddo
1559               j=j-1
1560             endif   
1561           endif
1562    21     continue
1563         enddo
1564    20   continue
1565       enddo
1566       if (lprint) then
1567         write (iout,*)
1568         write (iout,*) 'Electrostatic contacts after pruning: '
1569         do i=1,ncont
1570           mnum=molnum(i)
1571           i1=icont(1,i)
1572           i2=icont(2,i)
1573             mnum=molnum(i1)
1574             it1=itype(i1,mnum)
1575             it2=itype(i2,molnum(i2))
1576           write (iout,'(i3,2x,a,i4,2x,a,i4,f10.5)') &
1577            i,restyp(it1,mnum),i1,restyp(it2,molnum(i2)),i2,econt(i)
1578         enddo
1579       endif
1580       return
1581       end subroutine elecont
1582 !------------------------------------------------------------------------------
1583 ! match_contact.f
1584 !------------------------------------------------------------------------------
1585       subroutine match_contact(ishif1,ishif2,nc_match,nc_match1_max,&
1586          ncont_ref,icont_ref,ncont,icont,jfrag,n_shif1,n_shif2,&
1587          nc_frac,nc_req_set,istr,llocal,lprn)
1588
1589       use energy_data, only:maxcont
1590 !      implicit real*8 (a-h,o-z)
1591 !      include 'DIMENSIONS'
1592 !      include 'COMMON.IOUNITS'
1593       integer :: ncont_ref,ncont,ishift,ishif2,nc_match
1594       integer,dimension(2,maxcont) :: icont_ref,icont !(2,maxcont)
1595       real(kind=8) :: nc_frac
1596       logical :: llocal,lprn
1597       integer :: ishif1,nc_match1_max,jfrag,n_shif1,n_shif2,&
1598                  nc_req_set,istr,nc_match_max
1599       integer :: i,nc_req,nc_match1,is,js
1600       nc_match_max=0
1601       do i=1,ncont_ref
1602         nc_match_max=nc_match_max+ &
1603          min0(icont_ref(2,i)-icont_ref(1,i)-1,3)
1604       enddo
1605       if (istr.eq.3) then
1606         nc_req=0
1607       else if (nc_req_set.eq.0) then
1608         nc_req=nc_match_max*nc_frac
1609       else
1610         nc_req = dmin1(nc_match_max*nc_frac+0.5d0,&
1611           dfloat(nc_req_set)+1.0d-7)
1612       endif
1613 !      write (iout,*) "match_contact: nc_req:",nc_req
1614 !      write (iout,*) "nc_match_max",nc_match_max
1615 !      write (iout,*) "jfrag",jfrag," n_shif1",n_shif1,
1616 !     &   " n_shif2",n_shif2
1617 ! Match current contact map against reference contact map; exit, if at least
1618 ! half of the contacts match
1619       call ncont_match(nc_match,nc_match1,0,0,ncont_ref,icont_ref,&
1620           ncont,icont,jfrag,llocal,lprn)
1621       nc_match1_max=nc_match1
1622       if (lprn .and. nc_match.gt.0) write (iout,*) &
1623         "Shift:",0,0," nc_match1",nc_match1,&
1624         " nc_match=",nc_match," req'd",nc_req
1625       if (nc_req.gt.0 .and. nc_match.ge.nc_req .or. &
1626           nc_req.eq.0 .and. nc_match.eq.1) then
1627          ishif1=0
1628          ishif2=0
1629          return
1630       endif
1631 ! If sufficient matches are not found, try to shift contact maps up to three
1632 ! positions.
1633       if (n_shif1.gt.0) then
1634       do is=1,n_shif1
1635 ! The following four tries help to find shifted beta-sheet patterns
1636 ! Shift "left" strand backward
1637         call ncont_match(nc_match,nc_match1,-is,0,ncont_ref,&
1638           icont_ref,ncont,icont,jfrag,llocal,lprn)
1639         if (nc_match1.gt.nc_match1_max) nc_match1_max=nc_match1
1640         if (lprn .and. nc_match.gt.0) write (iout,*) & 
1641           "Shift:",-is,0," nc_match1",nc_match1,&
1642           " nc_match=",nc_match," req'd",nc_req
1643         if (nc_req.gt.0 .and. nc_match.ge.nc_req .or. &
1644            nc_req.eq.0 .and. nc_match.eq.1) then
1645           ishif1=-is
1646           ishif2=0
1647           return
1648         endif
1649 ! Shift "left" strand forward
1650         call ncont_match(nc_match,nc_match1,is,0,ncont_ref,&
1651             icont_ref,ncont,icont,jfrag,llocal,lprn)
1652         if (nc_match1.gt.nc_match1_max) nc_match1_max=nc_match1
1653         if (lprn .and. nc_match.gt.0) write (iout,*) &
1654          "Shift:",is,0," nc_match1",nc_match1,&
1655          " nc_match=",nc_match," req'd",nc_req
1656         if (nc_req.gt.0 .and. nc_match.ge.nc_req .or. &
1657            nc_req.eq.0 .and. nc_match.eq.1) then
1658           ishif1=is
1659           ishif2=0
1660           return
1661         endif
1662       enddo
1663       if (nc_req.eq.0) return
1664 ! Shift "right" strand backward
1665       do is=1,n_shif1
1666         call ncont_match(nc_match,nc_match1,0,-is,ncont_ref,&
1667            icont_ref,ncont,icont,jfrag,llocal,lprn)
1668         if (nc_match1.gt.nc_match1_max) nc_match1_max=nc_match1
1669         if (lprn .and. nc_match.gt.0) write (iout,*) &
1670           "Shift:",0,-is," nc_match1",nc_match1,&
1671           " nc_match=",nc_match," req'd",nc_req
1672         if (nc_match.ge.nc_req) then
1673           ishif1=0
1674           ishif2=-is
1675           return
1676         endif
1677 ! Shift "right" strand upward
1678         call ncont_match(nc_match,nc_match1,0,is,ncont_ref,&
1679           icont_ref,ncont,icont,jfrag,llocal,lprn)
1680         if (nc_match1.gt.nc_match1_max) nc_match1_max=nc_match1
1681         if (lprn .and. nc_match.gt.0) write (iout,*) &
1682           "Shift:",0,is," nc_match1",nc_match1,&
1683           " nc_match=",nc_match," req'd",nc_req
1684         if (nc_match.ge.nc_req) then
1685           ishif1=0
1686           ishif2=is
1687           return
1688         endif
1689       enddo ! is
1690 ! Now try to shift both residues in contacts.
1691       do is=1,n_shif1
1692         do js=1,is
1693           if (js.ne.is) then
1694             call ncont_match(nc_match,nc_match1,-is,-js,ncont_ref,&
1695               icont_ref,ncont,icont,jfrag,llocal,lprn)
1696             if (nc_match1.gt.nc_match1_max) nc_match1_max=nc_match1
1697             if (lprn .and. nc_match.gt.0) write (iout,*) &
1698                "Shift:",-is,-js," nc_match1",nc_match1,&
1699                " nc_match=",nc_match," req'd",nc_req
1700             if (nc_match.ge.nc_req) then
1701               ishif1=-is
1702               ishif2=-js
1703               return
1704             endif
1705             call ncont_match(nc_match,nc_match1,is,js,ncont_ref,&
1706               icont_ref,ncont,icont,jfrag,llocal,lprn)
1707             if (nc_match1.gt.nc_match1_max) nc_match1_max=nc_match1
1708             if (lprn .and. nc_match.gt.0) write (iout,*) &
1709               "Shift:",is,js," nc_match1",nc_match1,&
1710               " nc_match=",nc_match," req'd",nc_req
1711             if (nc_match.ge.nc_req) then
1712               ishif1=is
1713               ishif2=js
1714               return
1715             endif
1716 !
1717             call ncont_match(nc_match,nc_match1,-js,-is,ncont_ref,&
1718               icont_ref,ncont,icont,jfrag,llocal,lprn)
1719             if (nc_match1.gt.nc_match1_max) nc_match1_max=nc_match1
1720             if (lprn .and. nc_match.gt.0) write (iout,*) &
1721               "Shift:",-js,-is," nc_match1",nc_match1,&
1722               " nc_match=",nc_match," req'd",nc_req
1723             if (nc_match.ge.nc_req) then
1724               ishif1=-js
1725               ishif2=-is
1726               return
1727             endif
1728 !
1729             call ncont_match(nc_match,nc_match1,js,is,ncont_ref,&
1730               icont_ref,ncont,icont,jfrag,llocal,lprn)
1731             if (nc_match1.gt.nc_match1_max) nc_match1_max=nc_match1
1732             if (lprn .and. nc_match.gt.0) write (iout,*) &
1733               "Shift:",js,is," nc_match1",nc_match1,&
1734               " nc_match=",nc_match," req'd",nc_req
1735             if (nc_match.ge.nc_req) then
1736               ishif1=js
1737               ishif2=is
1738               return
1739             endif
1740           endif
1741 !
1742           if (is+js.le.n_shif1) then
1743           call ncont_match(nc_match,nc_match1,-is,js,ncont_ref,&
1744             icont_ref,ncont,icont,jfrag,llocal,lprn)
1745           if (nc_match1.gt.nc_match1_max) nc_match1_max=nc_match1
1746           if (lprn .and. nc_match.gt.0) write (iout,*) &
1747            "Shift:",-is,js," nc_match1",nc_match1,&
1748            " nc_match=",nc_match," req'd",nc_req
1749           if (nc_match.ge.nc_req) then
1750             ishif1=-is
1751             ishif2=js
1752             return
1753           endif
1754 !
1755           call ncont_match(nc_match,nc_match1,js,-is,ncont_ref,&
1756             icont_ref,ncont,icont,jfrag,llocal,lprn)
1757           if (nc_match1.gt.nc_match1_max) nc_match1_max=nc_match1
1758           if (lprn .and. nc_match.gt.0) write (iout,*) &
1759            "Shift:",js,-is," nc_match1",nc_match1,&
1760            " nc_match=",nc_match," req'd",nc_req
1761           if (nc_match.ge.nc_req) then
1762             ishif1=js
1763             ishif2=-is
1764             return
1765           endif
1766           endif
1767 !
1768         enddo !js
1769       enddo !is
1770       endif
1771
1772       if (n_shif2.gt.0) then
1773       do is=1,n_shif2
1774         call ncont_match(nc_match,nc_match1,-is,-is,ncont_ref,&
1775           icont_ref,ncont,icont,jfrag,llocal,lprn)
1776         if (nc_match1.gt.nc_match1_max) nc_match1_max=nc_match1
1777         if (lprn .and. nc_match.gt.0) write (iout,*) &
1778            "Shift:",-is,-is," nc_match1",nc_match1,&
1779            " nc_match=",nc_match," req'd",nc_req
1780         if (nc_match.ge.nc_req) then
1781           ishif1=-is
1782           ishif2=-is
1783           return
1784         endif
1785         call ncont_match(nc_match,nc_match1,is,is,ncont_ref,&
1786           icont_ref,ncont,icont,jfrag,llocal,lprn)
1787         if (nc_match1.gt.nc_match1_max) nc_match1_max=nc_match1
1788         if (lprn .and. nc_match.gt.0) write (iout,*) &
1789           "Shift:",is,is," nc_match1",nc_match1,&
1790           " nc_match=",nc_match," req'd",nc_req
1791         if (nc_match.ge.nc_req) then
1792           ishif1=is
1793           ishif2=is
1794           return
1795         endif
1796       enddo
1797       endif
1798 ! If this point is reached, the contact maps are different. 
1799       nc_match=0
1800       ishif1=0
1801       ishif2=0
1802       return
1803       end subroutine match_contact
1804 !-------------------------------------------------------------------------
1805       subroutine ncont_match(nc_match,nc_match1,ishif1,ishif2,&
1806          ncont_ref,icont_ref,ncont,icont,jfrag,llocal,lprn)
1807
1808       use energy_data, only:nnt,nct,maxcont
1809 !      implicit real*8 (a-h,o-z)
1810 !      include 'DIMENSIONS'
1811 !      include 'DIMENSIONS.ZSCOPT'
1812 !      include 'DIMENSIONS.COMPAR'
1813 !      include 'COMMON.IOUNITS'
1814 !      include 'COMMON.INTERACT'
1815 !      include 'COMMON.GEO'
1816 !      include 'COMMON.COMPAR'
1817       logical :: llocal,lprn
1818       integer ncont_ref,ncont,ishift,ishif2,nang_pair
1819       integer,dimension(2,maxcont) :: icont_ref,icont,icont_match !(2,maxcont)
1820       integer,dimension(2,nres) :: iang_pair !(2,maxres)
1821       integer :: nc_match,nc_match1,ishif1,jfrag
1822       integer :: i,j,ic1,ic2
1823       real(kind=8) :: diffang,fract,rad2deg
1824
1825 ! Compare the contact map against the reference contact map; they're stored
1826 ! in ICONT and ICONT_REF, respectively. The current contact map can be shifted.
1827       if (lprn) write (iout,'(80(1h*))')
1828       nc_match=0
1829       nc_match1=0
1830 ! Check the local structure by comparing dihedral angles.
1831 !      write (iout,*) "ncont_match: ncont_ref",ncont_ref," llocal",llocal
1832       if (llocal .and. ncont_ref.eq.0) then
1833 ! If there are no contacts just compare the dihedral angles and exit.
1834         call angnorm(jfrag,ishif1,ishif2,ang_cut1(jfrag),diffang,fract,&
1835           lprn)
1836         if (lprn) write (iout,*) "diffang:",diffang*rad2deg,&
1837          " ang_cut:",ang_cut(jfrag)*rad2deg," fract",fract
1838         if (diffang.le.ang_cut(jfrag) .and. fract.ge.frac_min(jfrag)) &
1839         then
1840           nc_match=1
1841         else
1842           nc_match=0
1843         endif
1844         return
1845       endif
1846       nang_pair=0
1847       do i=1,ncont
1848         ic1=icont(1,i)+ishif1
1849         ic2=icont(2,i)+ishif2
1850 !        write (iout,*) "i",i," ic1",ic1," ic2",ic2
1851         if (ic1.lt.nnt .or. ic2.gt.nct) goto 10
1852         do j=1,ncont_ref
1853           if (ic1.eq.icont_ref(1,j).and.ic2.eq.icont_ref(2,j)) then
1854             nc_match=nc_match+min0(icont_ref(2,j)-icont_ref(1,j)-1,3)
1855             nc_match1=nc_match1+1
1856             icont_match(1,nc_match1)=ic1
1857             icont_match(2,nc_match1)=ic2
1858 !            call add_angpair(icont(1,i),icont_ref(1,j),
1859 !     &         nang_pair,iang_pair)
1860 !            call add_angpair(icont(2,i),icont_ref(2,j),
1861 !     &         nang_pair,iang_pair) 
1862             if (lprn) write (iout,*) "Contacts:",icont(1,i),icont(2,i),&
1863              " match",icont_ref(1,j),icont_ref(2,j),&
1864              " shifts",ishif1,ishif2
1865             goto 10
1866           endif
1867         enddo 
1868    10   continue
1869       enddo
1870       if (lprn) then
1871         write (iout,*) "nc_match",nc_match," nc_match1",nc_match1
1872         write (iout,*) "icont_match"
1873         do i=1,nc_match1
1874           write (iout,*) icont_match(1,i),icont_match(2,i)
1875         enddo
1876       endif
1877       if (llocal .and. nc_match.gt.0) then
1878         call angnorm2(jfrag,ishif1,ishif2,nc_match1,icont_match,lprn,&
1879           ang_cut1(jfrag),diffang,fract)
1880         if (lprn) write (iout,*) "diffang:",diffang*rad2deg,&
1881          " ang_cut:",ang_cut(jfrag)*rad2deg,&
1882          " ang_cut1",ang_cut1(jfrag)*rad2deg
1883         if (diffang.gt.ang_cut(jfrag) &
1884           .or. fract.lt.frac_min(jfrag)) nc_match=0
1885       endif
1886 !      if (nc_match.gt.0) then
1887 !        diffang = angnorm1(nang_pair,iang_pair,lprn)
1888 !        if (diffang.gt.ang_cut(jfrag)) nc_match=0
1889 !      endif
1890       if (lprn) write (iout,*) "ishif1",ishif1," ishif2",ishif2,&
1891          " diffang",rad2deg*diffang," nc_match",nc_match
1892       return
1893       end subroutine ncont_match
1894 !------------------------------------------------------------------------------
1895       subroutine match_secondary(jfrag,isecstr,nsec_match,lprn)
1896 ! This subroutine compares the secondary structure (isecstr) of fragment jfrag 
1897 ! conformation considered to that of the reference conformation.
1898 ! Returns the number of equivalent residues (nsec_match).
1899 !      implicit real*8 (a-h,o-z)
1900 !      include 'DIMENSIONS'
1901 !      include 'DIMENSIONS.ZSCOPT'
1902 !      include 'DIMENSIONS.COMPAR'
1903 !      include 'COMMON.IOUNITS'
1904 !      include 'COMMON.CHAIN'
1905 !      include 'COMMON.PEPTCONT'
1906 !      include 'COMMON.COMPAR'
1907       logical :: lprn
1908       integer :: isecstr(nres)
1909       integer :: jfrag,nsec_match,npart,i,j
1910       npart = npiece(jfrag,1)
1911       nsec_match=0
1912       if (lprn) then
1913         write (iout,*) "match_secondary jfrag",jfrag," ifrag",&
1914               (ifrag(1,i,jfrag),ifrag(2,i,jfrag),i=1,npart)
1915         write (iout,'(80i1)') (isec_ref(j),j=1,nres)
1916         write (iout,'(80i1)') (isecstr(j),j=1,nres)
1917       endif
1918       do i=1,npart
1919         do j=ifrag(1,i,jfrag),ifrag(2,i,jfrag)
1920 ! The residue has equivalent conformational state to that of the reference
1921 ! structure, if:
1922 !  a) the conformational states are equal or
1923 !  b) the reference state is a coil and that of the conformation considered 
1924 !     is a strand or
1925 !  c) the conformational state of the conformation considered is a strand
1926 !     and that of the reference conformation is a coil.
1927 ! 10/28/02 - case (b) deleted.
1928           if (isecstr(j).eq.isec_ref(j) .or. &
1929 !     &        isecstr(j).eq.0 .and. isec_ref(j).eq.1 .or.
1930               isec_ref(j).eq.0 .and. isecstr(j).eq.1) &
1931             nsec_match=nsec_match+1 
1932         enddo
1933       enddo
1934       return
1935       end subroutine match_secondary
1936 !------------------------------------------------------------------------------
1937 ! odlodc.f
1938 !------------------------------------------------------------------------------
1939       subroutine odlodc(r1,r2,a,b,uu,vv,aa,bb,dd)
1940
1941       use energy_data, only:ncont_ref,icont_ref!,nsccont_frag_ref,&
1942 !                            isccont_frag_ref
1943 !      implicit real*8 (a-h,o-z)
1944       real(kind=8),dimension(3) :: r1,r2,a,b,x,y
1945       real(kind=8) :: uu,vv,aa,bb,dd
1946       real(kind=8) :: ab,ar,br,det,dd1,dd2,dd3,dd4,dd5
1947 !el      odl(u,v) = (r1(1)-r2(1))**2+(r1(2)-r2(2))**2+(r1(3)-r2(3))**2 &
1948 !el       + 2*ar*u - 2*br*v - 2*ab*u*v + aa*u**2 + bb*v**2
1949 !      print *,"r1",(r1(i),i=1,3)
1950 !      print *,"r2",(r2(i),i=1,3)
1951 !      print *,"a",(a(i),i=1,3)
1952 !      print *,"b",(b(i),i=1,3)
1953       aa = a(1)**2+a(2)**2+a(3)**2
1954       bb = b(1)**2+b(2)**2+b(3)**2
1955       ab = a(1)*b(1)+a(2)*b(2)+a(3)*b(3) 
1956       ar = a(1)*(r1(1)-r2(1))+a(2)*(r1(2)-r2(2))+a(3)*(r1(3)-r2(3))
1957       br = b(1)*(r1(1)-r2(1))+b(2)*(r1(2)-r2(2))+b(3)*(r1(3)-r2(3))
1958       det = aa*bb-ab**2
1959 !      print *,'aa',aa,' bb',bb,' ab',ab,' ar',ar,' br',br,' det',det
1960       uu = (-ar*bb+br*ab)/det
1961       vv = (br*aa-ar*ab)/det
1962 !      print *,u,v
1963       uu=dmin1(uu,1.0d0)
1964       uu=dmax1(uu,0.0d0)
1965       vv=dmin1(vv,1.0d0)
1966       vv=dmax1(vv,0.0d0)
1967 !el      dd1 = odl(uu,vv)
1968       dd1 = odl(uu,vv,r1,r2,ar,br,ab,aa,bb)
1969 !el      dd2 = odl(0.0d0,0.0d0)
1970       dd2 = odl(0.0d0,0.0d0,r1,r2,ar,br,ab,aa,bb)
1971 !el      dd3 = odl(0.0d0,1.0d0)
1972       dd3 = odl(0.0d0,1.0d0,r1,r2,ar,br,ab,aa,bb)
1973 !el      dd4 = odl(1.0d0,0.0d0)
1974       dd4 = odl(1.0d0,0.0d0,r1,r2,ar,br,ab,aa,bb)
1975 !el      dd5 = odl(1.0d0,1.0d0)
1976       dd5 = odl(1.0d0,1.0d0,r1,r2,ar,br,ab,aa,bb)
1977       dd = dsqrt(dmin1(dd1,dd2,dd3,dd4,dd5))
1978       if (dd.eq.dd2) then
1979         uu=0.0d0
1980         vv=0.0d0
1981       else if (dd.eq.dd3) then
1982         uu=0.0d0
1983         vv=1.0d0
1984       else if (dd.eq.dd4) then
1985         uu=1.0d0
1986         vv=0.0d0
1987       else if (dd.eq.dd5) then
1988         uu=1.0d0
1989         vv=1.0d0
1990       endif 
1991 ! Control check
1992 !      do i=1,3
1993 !        x(i)=r1(i)+u*a(i)
1994 !        y(i)=r2(i)+v*b(i)
1995 !      enddo
1996 !      dd1 = (x(1)-y(1))**2+(x(2)-y(2))**2+(x(3)-y(3))**2
1997 !      dd1 = dsqrt(dd1)
1998       aa = dsqrt(aa)
1999       bb = dsqrt(bb)
2000 !      write (8,*) uu,vv,dd,dd1
2001 !      print *,dd,dd1
2002       return
2003       end subroutine odlodc
2004 !------------------------------------------------------------------------------
2005       real(kind=8) function odl(u,v,r1,r2,ar,br,ab,aa,bb)
2006
2007       real(kind=8),dimension(3) :: r1,r2
2008       real(kind=8) :: aa,bb,u,v,ar,br,ab
2009
2010       odl = (r1(1)-r2(1))**2+(r1(2)-r2(2))**2+(r1(3)-r2(3))**2 &
2011        + 2*ar*u - 2*br*v - 2*ab*u*v + aa*u**2 + bb*v**2
2012
2013       end function odl
2014 !------------------------------------------------------------------------------
2015 ! proc_cont.f
2016 !------------------------------------------------------------------------------
2017       subroutine proc_cont
2018
2019       use geometry_data, only:rad2deg
2020       use energy_data, only:ncont_ref,icont_ref!,nsccont_frag_ref,&
2021 !                            isccont_frag_ref
2022 !      implicit real*8 (a-h,o-z)
2023 !      include 'DIMENSIONS'
2024 !      include 'DIMENSIONS.ZSCOPT'
2025 !      include 'DIMENSIONS.COMPAR'
2026 !      include 'COMMON.IOUNITS'
2027 !      include 'COMMON.TIME1'
2028 !      include 'COMMON.SBRIDGE'
2029 !      include 'COMMON.CONTROL'
2030 !      include 'COMMON.COMPAR'
2031 !      include 'COMMON.CHAIN'
2032 !      include 'COMMON.HEADER'
2033 !      include 'COMMON.CONTACTS1'
2034 !      include 'COMMON.PEPTCONT'
2035 !      include 'COMMON.GEO'
2036       integer :: i,j,k,ind,len_cut,ndigit,length_frag
2037
2038       write (iout,*) "proc_cont: nlevel",nlevel
2039       if (nlevel.lt.0) then
2040         write (iout,*) "call define_fragments"
2041         call define_fragments
2042       else
2043         write (iout,*) "call secondary2"
2044         call secondary2(.true.,.false.,ncont_pept_ref,icont_pept_ref,&
2045            isec_ref)
2046       endif
2047       write (iout,'(80(1h=))')
2048       write (iout,*) "Electrostatic contacts"
2049       call contacts_between_fragments(.true.,0,ncont_pept_ref,&
2050        icont_pept_ref,ncont_frag_ref(1),icont_frag_ref(1,1,1))
2051       write (iout,'(80(1h=))')
2052       write (iout,*) "Side chain contacts"
2053       call contacts_between_fragments(.true.,0,ncont_ref,&
2054        icont_ref,nsccont_frag_ref(1),isccont_frag_ref(1,1,1))
2055       if (nlevel.lt.0) then
2056         do i=1,nfrag(1)
2057           ind=icant(i,i)
2058           len_cut=1000
2059           if (istruct(i).le.1) then
2060             len_cut=max0(len_frag(i,1)*4/5,3)
2061           else if (istruct(i).eq.2 .or. istruct(i).eq.4) then
2062             len_cut=max0(len_frag(i,1)*2/5,3)
2063           endif
2064           write (iout,*) "i",i," istruct",istruct(i)," ncont_frag",&
2065             ncont_frag_ref(ind)," len_cut",len_cut,&
2066             " icont_single",icont_single," iloc_single",iloc_single
2067           iloc(i)=iloc_single
2068           if (iloc(i).gt.0) write (iout,*) &
2069            "Local structure used to compare structure of fragment",i,&
2070            " to native."
2071           if (istruct(i).ne.3 .and. istruct(i).ne.0 &
2072               .and. icont_single.gt.0 .and. &
2073               ncont_frag_ref(ind).ge.len_cut) then
2074             write (iout,*) "Electrostatic contacts used to compare",&
2075              " structure of fragment",i," to native."
2076             ielecont(i,1)=1
2077             isccont(i,1)=0
2078           else if (icont_single.gt.0 .and. nsccont_frag_ref(ind) &
2079             .ge.len_cut) then
2080             write (iout,*) "Side chain contacts used to compare",&
2081              " structure of fragment",i," to native."
2082             isccont(i,1)=1
2083             ielecont(i,1)=0
2084           else
2085             write (iout,*) "Contacts not used to compare",&
2086              " structure of fragment",i," to native."
2087             ielecont(i,1)=0
2088             isccont(i,1)=0
2089             nc_req_setf(i,1)=0
2090           endif
2091           if (irms_single.gt.0 .or. isccont(i,1).eq.0 &
2092                .and. ielecont(i,1).eq.0) then
2093             write (iout,*) "RMSD used to compare",&
2094              " structure of fragment",i," to native."
2095             irms(i,1)=1
2096           else
2097             write (iout,*) "RMSD not used to compare",&
2098              " structure of fragment",i," to native."
2099             irms(i,1)=0
2100           endif
2101         enddo
2102       endif
2103       if (nlevel.lt.-1) then
2104         call define_pairs
2105         nlevel = -nlevel
2106         if (nlevel.gt.3) nlevel=3
2107         if (nlevel.eq.3) then
2108           nfrag(3)=1
2109           npiece(1,3)=nfrag(1)
2110           do i=1,nfrag(1)
2111             ipiece(i,1,3)=i
2112           enddo
2113           ielecont(1,3)=0
2114           isccont(1,3)=0
2115           irms(1,3)=1
2116           n_shift(1,1,3)=0
2117           n_shift(2,1,3)=0
2118         endif 
2119       else if (nlevel.eq.-1) then
2120         nlevel=1
2121       endif
2122       isnfrag(1)=0
2123       do i=1,nlevel
2124         isnfrag(i+1)=isnfrag(i)+nfrag(i)
2125       enddo
2126       ndigit=3*nfrag(1)
2127       do i=2,nlevel
2128         ndigit=ndigit+2*nfrag(i)
2129       enddo
2130       write (iout,*) "ndigit",ndigit
2131       if (.not.binary .and. ndigit.gt.30) then
2132         write (iout,*) "Highest class too large; switching to",&
2133           " binary representation."
2134         binary=.true.
2135       endif
2136       write (iout,*) "isnfrag",(isnfrag(i),i=1,nlevel+1)
2137       write(iout,*) "rmscut_base_up",rmscut_base_up,&
2138        " rmscut_base_low",rmscut_base_low," rmsup_lim",rmsup_lim
2139       do i=1,nlevel
2140         do j=1,nfrag(i)
2141           length_frag = 0
2142           if (i.eq.1) then
2143             do k=1,npiece(j,i)
2144               length_frag=length_frag+ifrag(2,k,j)-ifrag(1,k,j)+1
2145             enddo
2146           else
2147             do k=1,npiece(j,i)
2148               length_frag=length_frag+len_frag(ipiece(k,j,i),1)
2149             enddo
2150           endif
2151           len_frag(j,i)=length_frag
2152           rmscutfrag(1,j,i)=rmscut_base_up*length_frag
2153           rmscutfrag(2,j,i)=rmscut_base_low*length_frag 
2154           if (rmscutfrag(1,j,i).lt.rmsup_lim) &
2155             rmscutfrag(1,j,i)=rmsup_lim
2156           if (rmscutfrag(1,j,i).gt.rmsupup_lim) & 
2157             rmscutfrag(1,j,i)=rmsupup_lim
2158         enddo
2159       enddo
2160       write (iout,*) "Level",1," number of fragments:",nfrag(1)
2161       do j=1,nfrag(1)
2162         write (iout,*) npiece(j,1),(ifrag(1,k,j),ifrag(2,k,j),&
2163           k=1,npiece(j,1)),len_frag(j,1),rmscutfrag(1,j,1),&
2164           rmscutfrag(2,j,1),n_shift(1,j,1),n_shift(2,j,1),&
2165           ang_cut(j)*rad2deg,ang_cut1(j)*rad2deg,frac_min(j),&
2166           nc_fragm(j,1),nc_req_setf(j,1),istruct(j)
2167       enddo
2168       do i=2,nlevel
2169         write (iout,*) "Level",i," number of fragments:",nfrag(i)
2170         do j=1,nfrag(i)
2171           write (iout,*) npiece(j,i),(ipiece(k,j,i),&
2172             k=1,npiece(j,i)),len_frag(j,i),rmscutfrag(1,j,i),&
2173             rmscutfrag(2,j,i),n_shift(1,j,i),n_shift(2,j,i),&
2174             nc_fragm(j,i),nc_req_setf(j,i) 
2175         enddo
2176       enddo
2177       return
2178       end subroutine proc_cont
2179 !------------------------------------------------------------------------------
2180 ! define_pairs.f
2181 !------------------------------------------------------------------------------
2182       subroutine define_pairs
2183
2184 !      use energy_data, only:nsccont_frag_ref
2185 !      implicit real*8 (a-h,o-z)
2186 !      include 'DIMENSIONS'
2187 !      include 'DIMENSIONS.ZSCOPT'
2188 !      include 'DIMENSIONS.COMPAR'
2189 !      include 'COMMON.IOUNITS'
2190 !      include 'COMMON.TIME1'
2191 !      include 'COMMON.SBRIDGE'
2192 !      include 'COMMON.CONTROL'
2193 !      include 'COMMON.COMPAR'
2194 !      include 'COMMON.FRAG'
2195 !      include 'COMMON.CHAIN'
2196 !      include 'COMMON.HEADER'
2197 !      include 'COMMON.GEO'
2198 !      include 'COMMON.CONTACTS1'
2199 !      include 'COMMON.PEPTCONT'
2200       integer :: j,k,i,length_frag,ind,ll1,ll2,len_cut
2201
2202       do j=1,nfrag(1)
2203         length_frag = 0
2204         do k=1,npiece(j,1)
2205           length_frag=length_frag+ifrag(2,k,j)-ifrag(1,k,j)+1
2206         enddo
2207         len_frag(j,1)=length_frag
2208         write (iout,*) "Fragment",j," length",len_frag(j,1)
2209       enddo
2210       nfrag(2)=0
2211       do i=1,nfrag(1)
2212         do j=i+1,nfrag(1)
2213           ind = icant(i,j)
2214           if (istruct(i).le.1 .or. istruct(j).le.1) then
2215             if (istruct(i).le.1) then
2216               ll1=len_frag(i,1)
2217             else
2218               ll1=len_frag(i,1)/2
2219             endif
2220             if (istruct(j).le.1) then
2221               ll2=len_frag(j,1)
2222             else
2223               ll2=len_frag(j,1)/2
2224             endif
2225             len_cut=max0(min0(ll1*2/3,ll2*4/5),3)
2226           else
2227             if (istruct(i).eq.2 .or. istruct(i).eq.4) then
2228               ll1=len_frag(i,1)/2
2229             else
2230               ll1=len_frag(i,1) 
2231             endif
2232             if (istruct(j).eq.2 .or. istruct(j).eq.4) then
2233               ll2=len_frag(j,1)/2
2234             else
2235               ll2=len_frag(j,1) 
2236             endif
2237             len_cut=max0(min0(ll1*4/5,ll2)*4/5,3)
2238           endif
2239           write (iout,*) "Fragments",i,j," structure",istruct(i),&
2240              istruct(j)," # contacts",&
2241              ncont_frag_ref(ind),nsccont_frag_ref(ind),&
2242              " lengths",len_frag(i,1),len_frag(j,1),&
2243              " ll1",ll1," ll2",ll2," len_cut",len_cut
2244           if ((istruct(i).eq.1 .or. istruct(j).eq.1) .and. &
2245             nsccont_frag_ref(ind).ge.len_cut ) then
2246             if (istruct(i).eq.1 .and. istruct(j).eq.1) then
2247               write (iout,*) "Adding pair of helices",i,j,&
2248               " based on SC contacts"
2249             else
2250               write (iout,*) "Adding helix+strand/sheet pair",i,j,&
2251               " based on SC contacts"
2252             endif
2253             nfrag(2)=nfrag(2)+1
2254             if (icont_pair.gt.0) then
2255               write (iout,*)  "# SC contacts will be used",&
2256               " in comparison."
2257               isccont(nfrag(2),2)=1
2258             endif
2259             if (irms_pair.gt.0) then
2260               write (iout,*)  "Fragment RMSD will be used",&
2261               " in comparison."
2262               irms(nfrag(2),2)=1
2263             endif
2264             npiece(nfrag(2),2)=2
2265             ipiece(1,nfrag(2),2)=i
2266             ipiece(2,nfrag(2),2)=j
2267             ielecont(nfrag(2),2)=0
2268             n_shift(1,nfrag(2),2)=nshift_pair
2269             n_shift(2,nfrag(2),2)=nshift_pair
2270             nc_fragm(nfrag(2),2)=ncfrac_pair
2271             nc_req_setf(nfrag(2),2)=ncreq_pair
2272           else if ((istruct(i).ge.2 .and. istruct(i).le.4) &
2273              .and. (istruct(j).ge.2 .and. istruct(i).le.4) &
2274              .and. ncont_frag_ref(ind).ge.len_cut ) then
2275             nfrag(2)=nfrag(2)+1
2276             write (iout,*) "Adding pair strands/sheets",i,j,&
2277               " based on pp contacts"
2278             if (icont_pair.gt.0) then
2279               write (iout,*) "# pp contacts will be used",&
2280               " in comparison."
2281               ielecont(nfrag(2),2)=1
2282             endif
2283             if (irms_pair.gt.0) then
2284               write (iout,*)  "Fragment RMSD will be used",&
2285               " in comparison."
2286               irms(nfrag(2),2)=1
2287             endif
2288             npiece(nfrag(2),2)=2
2289             ipiece(1,nfrag(2),2)=i
2290             ipiece(2,nfrag(2),2)=j
2291             ielecont(nfrag(2),2)=1
2292             isccont(nfrag(2),2)=0
2293             n_shift(1,nfrag(2),2)=nshift_pair
2294             n_shift(2,nfrag(2),2)=nshift_pair
2295             nc_fragm(nfrag(2),2)=ncfrac_bet
2296             nc_req_setf(nfrag(2),2)=ncreq_bet
2297           endif
2298         enddo
2299       enddo
2300       write (iout,*) "Pairs found"
2301       do i=1,nfrag(2)
2302         write (iout,*) ipiece(1,i,2),ipiece(2,i,2)
2303       enddo
2304       return
2305       end subroutine define_pairs
2306 !------------------------------------------------------------------------------
2307 ! icant.f
2308 !------------------------------------------------------------------------------
2309       INTEGER FUNCTION ICANT(I,J)
2310       integer :: i,j
2311       IF (I.GE.J) THEN
2312         ICANT=(I*(I-1))/2+J
2313       ELSE
2314         ICANT=(J*(J-1))/2+I
2315       ENDIF
2316       RETURN
2317       END FUNCTION ICANT
2318 !------------------------------------------------------------------------------
2319 ! mysort.f
2320 !------------------------------------------------------------------------------
2321       subroutine imysort(n, m, mm, x, y, z, z1, z2, z3, z4, z5, z6)
2322 !      implicit none
2323       integer :: n,m,mm
2324       integer :: x(m,mm,n),y(n),z(n),z1(2,n),z6(n),xmin,xtemp
2325       real(kind=8) :: z2(n),z3(n),z4(n),z5(n)
2326       real(kind=8) :: xxtemp
2327       integer :: i,j,k,imax
2328       do i=1,n
2329         xmin=x(1,1,i)
2330         imax=i
2331         do j=i+1,n
2332           if (x(1,1,j).lt.xmin) then
2333             imax=j
2334             xmin=x(1,1,j)
2335           endif
2336         enddo
2337         xxtemp=z2(imax)
2338         z2(imax)=z2(i)
2339         z2(i)=xxtemp 
2340         xxtemp=z3(imax)
2341         z3(imax)=z3(i)
2342         z3(i)=xxtemp 
2343         xxtemp=z4(imax)
2344         z4(imax)=z4(i)
2345         z4(i)=xxtemp 
2346         xxtemp=z5(imax)
2347         z5(imax)=z5(i)
2348         z5(i)=xxtemp 
2349         xtemp=y(imax)
2350         y(imax)=y(i)
2351         y(i)=xtemp
2352         xtemp=z(imax)
2353         z(imax)=z(i)
2354         z(i)=xtemp
2355         xtemp=z6(imax)
2356         z6(imax)=z6(i)
2357         z6(i)=xtemp
2358         do j=1,2
2359           xtemp=z1(j,imax)
2360           z1(j,imax)=z1(j,i)
2361           z1(j,i)=xtemp
2362         enddo
2363         do j=1,m
2364           do k=1,mm
2365             xtemp=x(j,k,imax) 
2366             x(j,k,imax)=x(j,k,i)
2367             x(j,k,i)=xtemp
2368           enddo
2369         enddo
2370       enddo
2371       return
2372       end subroutine imysort
2373 !------------------------------------------------------------------------------
2374 ! qwolynes.f
2375 !-------------------------------------------------------------------------------
2376       real(kind=8) function qwolynes(ilevel,jfrag)
2377
2378       use geometry_data, only:cref,nperm
2379       use control_data, only:symetr
2380       use energy_data, only:nnt,nct,itype,molnum
2381 !      implicit none
2382 !      include 'DIMENSIONS'
2383 !      include 'DIMENSIONS.ZSCOPT'
2384 !      include 'DIMENSIONS.COMPAR'
2385 !      include 'COMMON.IOUNITS'
2386 !      include 'COMMON.COMPAR'
2387 !      include 'COMMON.CHAIN' 
2388 !      include 'COMMON.INTERACT'
2389 !      include 'COMMON.CONTROL'
2390       integer :: ilevel,jfrag,kkk
2391       integer :: i,j,jl,k,l,il,kl,nl,np,ip,kp
2392       integer :: nsep=3
2393       real(kind=8),dimension(:),allocatable :: tempus !(maxperm)
2394       real(kind=8) :: maxiQ !dist,
2395       real(kind=8) :: qq,qqij,qqijCM,dij,d0ij,dijCM,d0ijCM
2396       logical :: lprn=.false.
2397       real(kind=8) :: x !el sigm
2398 !el      sigm(x)=0.25d0*x
2399       nperm=1
2400       maxiQ=0
2401       do i=1,symetr
2402       nperm=i*nperm
2403       enddo
2404 !      write (iout,*) "QWolyes: " jfrag",jfrag,
2405 !     &  " ilevel",ilevel
2406       allocate(tempus(nperm))
2407       do kkk=1,nperm
2408       qq = 0.0d0
2409       if (ilevel.eq.0) then
2410         if (lprn) write (iout,*) "Q computed for whole molecule"
2411         nl=0
2412         do il=nnt+nsep,nct
2413           do jl=nnt,il-nsep
2414             dij=0.0d0
2415             dijCM=0.0d0
2416             d0ij=0.0d0
2417             d0ijCM=0.0d0
2418             qqij=0.0d0
2419             qqijCM=0.0d0
2420             nl=nl+1
2421             d0ij=dsqrt((cref(1,jl,kkk)-cref(1,il,kkk))**2+ &
2422                        (cref(2,jl,kkk)-cref(2,il,kkk))**2+ &
2423                        (cref(3,jl,kkk)-cref(3,il,kkk))**2)
2424             dij=dist(il,jl)
2425             qqij = dexp(-0.5d0*((dij-d0ij)/(sigm(d0ij)))**2)
2426             if (itype(il,molnum(il)).ne.10 .or. itype(jl,molnum(jl)).ne.10) then
2427               nl=nl+1
2428               d0ijCM=dsqrt( &
2429                      (cref(1,jl+nres,kkk)-cref(1,il+nres,kkk))**2+ &
2430                      (cref(2,jl+nres,kkk)-cref(2,il+nres,kkk))**2+ &
2431                      (cref(3,jl+nres,kkk)-cref(3,il+nres,kkk))**2)
2432               dijCM=dist(il+nres,jl+nres)
2433               qqijCM = dexp(-0.5d0*((dijCM-d0ijCM)/(sigm(d0ijCM)))**2)
2434             endif
2435             qq = qq+qqij+qqijCM
2436             if (lprn) then
2437               write (iout,*) "il",il," jl",jl,&
2438                " itype",itype(il,molnum(il)),itype(jl,molnum(jl))
2439               write (iout,*)"d0ij",d0ij," dij",dij," d0ijCM",d0ijCM,&
2440                " dijCM",dijCM," qqij",qqij," qqijCM",qqijCM
2441             endif
2442           enddo
2443         enddo
2444         qq = qq/nl
2445         if (lprn) write (iout,*) "nl",nl," qq",qq
2446       else if (ilevel.eq.1) then
2447         if (lprn) write (iout,*) "Level",ilevel," fragment",jfrag
2448         nl=0
2449 !        write (iout,*) "nlist_frag",nlist_frag(jfrag)
2450         do i=2,nlist_frag(jfrag)
2451           do j=1,i-1
2452             il=list_frag(i,jfrag)
2453             jl=list_frag(j,jfrag)
2454             if (iabs(il-jl).gt.nsep) then
2455               dij=0.0d0
2456               dijCM=0.0d0
2457               d0ij=0.0d0
2458               d0ijCM=0.0d0
2459               qqij=0.0d0
2460               qqijCM=0.0d0
2461               nl=nl+1
2462               d0ij=dsqrt((cref(1,jl,kkk)-cref(1,il,kkk))**2+ &
2463                          (cref(2,jl,kkk)-cref(2,il,kkk))**2+ &
2464                          (cref(3,jl,kkk)-cref(3,il,kkk))**2)
2465               dij=dist(il,jl)
2466               qqij = dexp(-0.5d0*((dij-d0ij)/(sigm(d0ij)))**2)
2467               if (itype(il,molnum(il)).ne.10 .or. itype(jl,molnum(jl)).ne.10) then
2468                 nl=nl+1
2469                 d0ijCM=dsqrt( &
2470                        (cref(1,jl+nres,kkk)-cref(1,il+nres,kkk))**2+ &
2471                        (cref(2,jl+nres,kkk)-cref(2,il+nres,kkk))**2+ &
2472                        (cref(3,jl+nres,kkk)-cref(3,il+nres,kkk))**2)
2473                 dijCM=dist(il+nres,jl+nres)
2474                qqijCM = dexp(-0.5d0*((dijCM-d0ijCM)/(sigm(d0ijCM)))**2)
2475               endif
2476               qq = qq+qqij+qqijCM
2477               if (lprn) then
2478                 write (iout,*) "i",i," j",j," il",il," jl",jl,&
2479                  " itype",itype(il,molnum(il)),itype(jl,molnum(jl))
2480                 write (iout,*)"d0ij",d0ij," dij",dij," d0ijCM",d0ijCM,&
2481                  " dijCM",dijCM," qqij",qqij," qqijCM",qqijCM
2482               endif
2483             endif
2484           enddo
2485         enddo
2486         qq = qq/nl
2487         if (lprn) write (iout,*) "nl",nl," qq",qq
2488       else if (ilevel.eq.2) then
2489         np=npiece(jfrag,ilevel)
2490         nl=0
2491         do i=2,np
2492           ip=ipiece(i,jfrag,ilevel)
2493           do j=1,nlist_frag(ip) 
2494             il=list_frag(j,ip)
2495             do k=1,i-1 
2496               kp=ipiece(k,jfrag,ilevel)
2497               do l=1,nlist_frag(kp)
2498                 kl=list_frag(l,kp)
2499                 if (iabs(kl-il).gt.nsep) then 
2500                   nl=nl+1
2501                   dij=0.0d0
2502                   dijCM=0.0d0
2503                   d0ij=0.0d0
2504                   d0ijCM=0.0d0
2505                   qqij=0.0d0
2506                   qqijCM=0.0d0
2507                   d0ij=dsqrt((cref(1,kl,kkk)-cref(1,il,kkk))**2+ &
2508                              (cref(2,kl,kkk)-cref(2,il,kkk))**2+ &
2509                              (cref(3,kl,kkk)-cref(3,il,kkk))**2)
2510                   dij=dist(il,kl)
2511                   qqij = dexp(-0.5d0*((dij-d0ij)/(sigm(d0ij)))**2)
2512                   if (itype(il,molnum(il)).ne.10 .or. itype(kl,molnum(kl)).ne.10) then
2513                     nl=nl+1
2514                     d0ijCM=dsqrt( &
2515                        (cref(1,kl+nres,kkk)-cref(1,il+nres,kkk))**2+ &
2516                        (cref(2,kl+nres,kkk)-cref(2,il+nres,kkk))**2+ &
2517                        (cref(3,kl+nres,kkk)-cref(3,il+nres,kkk))**2)
2518                     dijCM=dist(il+nres,kl+nres)
2519                     qqijCM = dexp(-0.5d0*((dijCM-d0ijCM)/ &
2520                       (sigm(d0ijCM)))**2)
2521                   endif
2522                   qq = qq+qqij+qqijCM
2523                   if (lprn) then
2524                     write (iout,*) "i",i," j",j," k",k," l",l," il",il,&
2525                       " kl",kl," itype",itype(il,molnum(il)), &
2526                          itype(kl,molnum(kl))
2527                     write (iout,*) " d0ij",d0ij," dij",dij," d0ijCM",&
2528                     d0ijCM," dijCM",dijCM," qqij",qqij," qqijCM",qqijCM
2529                   endif
2530                 endif
2531               enddo  ! l
2532             enddo    ! k
2533           enddo      ! j
2534         enddo        ! i
2535         qq = qq/nl
2536         if (lprn) write (iout,*) "nl",nl," qq",qq
2537       else
2538         write (iout,*)"Error: Q can be computed only for level 1 and 2."
2539       endif
2540       tempus(kkk)=qq
2541       enddo
2542       do kkk=1,nperm
2543        if (maxiQ.le.tempus(kkk)) maxiQ=tempus(kkk)
2544       enddo
2545       qwolynes=1.0d0-maxiQ
2546       deallocate(tempus)
2547       return
2548       end function qwolynes
2549 !-------------------------------------------------------------------------------
2550       real(kind=8) function sigm(x)
2551       real(kind=8) :: x
2552       sigm=0.25d0*x
2553       return
2554       end function sigm
2555 !-------------------------------------------------------------------------------
2556       subroutine fragment_list
2557 !      implicit none
2558 !      include 'DIMENSIONS'
2559 !      include 'DIMENSIONS.ZSCOPT'
2560 !      include 'DIMENSIONS.COMPAR'
2561 !      include 'COMMON.IOUNITS'
2562 !      include 'COMMON.COMPAR'
2563       logical :: lprn=.true.
2564       integer :: i,ilevel,j,k,jfrag
2565       do jfrag=1,nfrag(1)
2566         nlist_frag(jfrag)=0
2567         do i=1,npiece(jfrag,1)
2568           if (lprn) write (iout,*) "jfrag=",jfrag,&
2569             "i=",i," fragment",ifrag(1,i,jfrag),&
2570             ifrag(2,i,jfrag)
2571           do j=ifrag(1,i,jfrag),ifrag(2,i,jfrag)
2572             do k=1,nlist_frag(jfrag)
2573               if (list_frag(k,jfrag).eq.j) goto 10
2574             enddo
2575             nlist_frag(jfrag)=nlist_frag(jfrag)+1
2576             list_frag(nlist_frag(jfrag),jfrag)=j
2577           enddo
2578   10      continue
2579         enddo
2580       enddo
2581       write (iout,*) "Fragment list"
2582       do j=1,nfrag(1)
2583         write (iout,*)"Fragment",j," list",(list_frag(k,j),&
2584          k=1,nlist_frag(j))
2585       enddo
2586       return
2587       end subroutine fragment_list
2588 !-------------------------------------------------------------------------------
2589       real(kind=8) function rmscalc(ishif,i,j,jcon,lprn)
2590
2591       use w_comm_local
2592       use control_data, only:symetr
2593       use geometry_data, only:nperm
2594 !      implicit real*8 (a-h,o-z)
2595 !      include 'DIMENSIONS'
2596 !      include 'DIMENSIONS.ZSCOPT'
2597 !      include 'DIMENSIONS.COMPAR'
2598 !      include 'COMMON.IOUNITS'
2599 !      include 'COMMON.COMPAR'
2600 !      include 'COMMON.CHAIN' 
2601 !      include 'COMMON.INTERACT'
2602 !      include 'COMMON.VAR'
2603 !      include 'COMMON.CONTROL'
2604       real(kind=8) :: przes(3),obrot(3,3)
2605 !el      real(kind=8) :: creff(3,nres*2),cc(3,nres*2)
2606 !el      logical :: iadded(nres)
2607 !el      integer :: inumber(2,nres)
2608 !el      common /ccc/ creff,cc,iadded,inumber
2609       logical :: lprn
2610       logical :: non_conv
2611       integer :: ishif,i,j,jcon,idup,kkk,l,k,kk
2612       real(kind=8) :: rminrms,rms
2613       if (lprn) then
2614         write (iout,*) "i",i," j",j," jcont",jcon," ishif",ishif
2615         write (iout,*) "npiece",npiece(j,i)
2616         call flush(iout)
2617       endif
2618 !      write (iout,*) "symetr",symetr
2619 !      call flush(iout)
2620       nperm=1
2621       do idup=1,symetr
2622       nperm=nperm*idup
2623       enddo
2624 !      write (iout,*) "nperm",nperm
2625 !      call flush(iout)
2626       do kkk=1,nperm
2627       idup=0
2628       do l=1,nres
2629         iadded(l)=.false.
2630       enddo
2631 !      write (iout,*) "kkk",kkk
2632 !      call flush(iout)
2633       do k=1,npiece(j,i)
2634         if (i.eq.1) then
2635           if (lprn) then
2636             write (iout,*) "Level 1: j=",j,"k=",k," adding fragment",&
2637                ifrag(1,k,j),ifrag(2,k,j)
2638             call flush(iout)
2639           endif
2640           call cprep(ifrag(1,k,j),ifrag(2,k,j),ishif,idup,kkk)
2641 !          write (iout,*) "Exit cprep"
2642 !          call flush(iout)
2643 !          write (iout,*) "ii=",ii
2644         else
2645           kk = ipiece(k,j,i)
2646 !          write (iout,*) "kk",kk," npiece",npiece(kk,1)
2647           do l=1,npiece(kk,1)
2648             if (lprn) then
2649               write (iout,*) "Level",i,": j=",j,"k=",k," kk=",kk,&
2650                 " l=",l," adding fragment",&
2651                 ifrag(1,l,kk),ifrag(2,l,kk)
2652               call flush(iout)
2653             endif
2654             call cprep(ifrag(1,l,kk),ifrag(2,l,kk),ishif,idup,kkk)
2655 !            write (iout,*) "After cprep"
2656 !            call flush(iout)
2657           enddo 
2658         endif
2659       enddo
2660       enddo
2661       if (lprn) then
2662         write (iout,*) "tuszukaj"
2663         do kkk=1,nperm
2664           do k=1,idup
2665             write(iout,'(5i4,2(3f10.5,5x))') i,j,k,inumber(1,k),&
2666               inumber(2,k),(creff(l,k),l=1,3),(cc(l,k),l=1,3)
2667           enddo
2668         enddo
2669         call flush(iout)
2670       endif
2671       rminrms=1.0d10
2672       do kkk=1,nperm
2673       call fitsq(rms,cc(1,1),creff(1,1),idup,przes,obrot,non_conv)
2674       if (non_conv) then
2675         print *,'Error: FITSQ non-convergent, jcon',jcon,i
2676         rms = 1.0d10
2677       else if (rms.lt.-1.0d-6) then 
2678         print *,'Error: rms^2 = ',rms,jcon,i
2679         rms = 1.0d10
2680       else if (rms.ge.1.0d-6 .and. rms.lt.0) then
2681         rms = 0.0d0
2682       endif
2683 !      write (iout,*) "rmsmin", rminrms, "rms", rms
2684       if (rms.le.rminrms) rminrms=rms
2685       enddo
2686       rmscalc = dsqrt(rminrms)
2687 !      write (iout, *) "analysys", rmscalc,anatemp
2688       return
2689       end function rmscalc
2690 !-------------------------------------------------------------------------
2691       subroutine cprep(if1,if2,ishif,idup,kwa)
2692
2693       use w_comm_local
2694       use control_data, only:symetr
2695       use geometry_data, only:nperm,cref,c
2696 !      implicit real*8 (a-h,o-z)
2697 !      include 'DIMENSIONS'
2698 !      include 'DIMENSIONS.ZSCOPT'
2699 !      include 'DIMENSIONS.COMPAR'
2700 !      include 'COMMON.CONTROL'
2701 !      include 'COMMON.IOUNITS'
2702 !      include 'COMMON.COMPAR'
2703 !      include 'COMMON.CHAIN' 
2704 !      include 'COMMON.INTERACT'
2705 !      include 'COMMON.VAR'
2706       real(kind=8) :: przes(3),obrot(3,3)
2707 !el      real(kind=8) :: creff(3,nres*2),cc(3,nres*2)
2708 !el      logical :: iadded(nres)
2709 !el      integer :: inumber(2,nres)
2710       integer :: iistrart,kwa,blar
2711 !el      common /ccc/ creff,cc,iadded,inumber
2712       integer :: if1,if2,ishif,idup,kkk,l,m
2713 !      write (iout,*) "Calling cprep symetr",symetr," kwa",kwa
2714       nperm=1
2715       do blar=1,symetr
2716       nperm=nperm*blar
2717       enddo
2718 !      write (iout,*) "nperm",nperm
2719       kkk=kwa
2720 !      ii=0
2721       do l=if1,if2
2722 !        write (iout,*) "l",l," iadded",iadded(l)
2723 !        call flush(iout)
2724         if (l+ishif.gt.1 .and. l+ishif.le.nres .and. .not.iadded(l)) &
2725         then
2726           idup=idup+1
2727           iadded(l)=.true.
2728           inumber(1,idup)=l
2729           inumber(2,idup)=l+ishif
2730           do m=1,3
2731             creff(m,idup)=cref(m,l,kkk)
2732             cc(m,idup)=c(m,l+ishif)
2733           enddo
2734         endif
2735       enddo
2736       return
2737       end subroutine cprep
2738 !-------------------------------------------------------------------------
2739       real(kind=8) function rmsnat(jcon)
2740
2741       use control_data, only:symetr
2742       use geometry_data, only:nperm,cref,c
2743       use energy_data, only:itype,molnum
2744 !      implicit real*8 (a-h,o-z)
2745 !      include 'DIMENSIONS'
2746 !      include 'DIMENSIONS.ZSCOPT'
2747 !      include 'DIMENSIONS.COMPAR'
2748 !      include 'COMMON.IOUNITS'
2749 !      include 'COMMON.COMPAR'
2750 !      include 'COMMON.CHAIN' 
2751 !      include 'COMMON.INTERACT'
2752 !      include 'COMMON.VAR'
2753 !      include 'COMMON.CONTROL'
2754       real(kind=8) :: przes(3),obrot(3,3),cc(3,2*nres),ccref(3,2*nres)
2755       logical :: non_conv
2756       integer :: ishif,i,j,resprzesun,jcon,kkk,nnsup
2757       real(kind=8) :: rminrms,rmsminsing,rms
2758       rminrms=10.0d10
2759       rmsminsing=10d10
2760       nperm=1
2761       do i=1,symetr
2762        nperm=nperm*i
2763       enddo
2764       do kkk=1,nperm
2765        nnsup=0
2766        do i=1,nres
2767         if (itype(i,molnum(i)).ne.ntyp1_molec(molnum(i))) then
2768           nnsup=nnsup+1
2769           do j=1,3
2770             cc(j,nnsup)=c(j,i)
2771             ccref(j,nnsup)=cref(j,i,kkk)
2772           enddo
2773         endif
2774        enddo
2775        call fitsq(rms,cc(1,1),ccref(1,1),nnsup,przes,obrot,non_conv)
2776        if (non_conv) then
2777         print *,'Error: FITSQ non-convergent, jcon',jcon,i
2778         rms=1.0d10
2779        else if (rms.lt.-1.0d-6) then 
2780         print *,'Error: rms^2 = ',rms,jcon,i
2781         rms = 1.0d10
2782        else if (rms.ge.1.0d-6 .and. rms.lt.0) then
2783         rms=0.0d0
2784        endif
2785        if (rms.le.rminrms) rminrms=rms
2786 !       write (iout,*) "kkk",kkk," rmsnat",rms , rminrms
2787       enddo
2788       rmsnat = dsqrt(rminrms)
2789 !      write (iout,*)  "analysys",rmsnat, anatemp
2790 !      liczenie rmsdla pojedynczego lancucha
2791       return
2792       end function rmsnat
2793 !-------------------------------------------------------------------------------
2794       subroutine define_fragments
2795
2796       use geometry_data, only:rad2deg
2797       use energy_data, only:itype,molnum
2798       use compare_data, only:nhfrag,nbfrag,bfrag,hfrag
2799 !      implicit real*8 (a-h,o-z)
2800 !      include 'DIMENSIONS'
2801 !      include 'DIMENSIONS.ZSCOPT'
2802 !      include 'DIMENSIONS.COMPAR'
2803 !      include 'COMMON.IOUNITS'
2804 !      include 'COMMON.TIME1'
2805 !      include 'COMMON.FRAG'
2806 !      include 'COMMON.SBRIDGE'
2807 !      include 'COMMON.CONTROL'
2808 !      include 'COMMON.COMPAR'
2809 !      include 'COMMON.CHAIN'
2810 !      include 'COMMON.HEADER'
2811 !      include 'COMMON.GEO'
2812 !      include 'COMMON.CONTACTS'
2813 !      include 'COMMON.PEPTCONT'
2814 !      include 'COMMON.INTERACT'
2815 !      include 'COMMON.NAMES'
2816       integer :: nstrand,istrand(2,nres/2)
2817       integer :: nhairp,ihairp(2,nres/5),mnum
2818       character(len=16) :: strstr(4)=reshape((/'helix','hairpin',&
2819                           'strand','strand pair'/),shape(strstr))
2820       integer :: j,i,ii,i1,i2,i3,i4,it1,it2,it3,it4
2821
2822       write (iout,*) 'NC_FRAC_HEL',ncfrac_hel,' NC_REQ_HEL',ncreq_hel,&
2823                      'NC_FRAC_BET',ncfrac_bet,' NC_REQ_BET',ncreq_bet,&
2824                  'NC_FRAC_PAIR',ncfrac_pair,' NC_REQ_PAIR',ncreq_pair,&
2825         ' RMS_PAIR',irms_pair,' SPLIT_BET',isplit_bet
2826       write (iout,*) 'NSHIFT_HEL',nshift_hel,' NSHIFT_BET',nshift_bet,&
2827         ' NSHIFT_STRAND',nshift_strand,' NSHIFT_PAIR',nshift_pair
2828       write (iout,*) 'ANGCUT_HEL',angcut_hel*rad2deg,&
2829         ' MAXANG_HEL',angcut1_hel*rad2deg
2830       write (iout,*) 'ANGCUT_BET',angcut_bet*rad2deg,&
2831                      ' MAXANG_BET',angcut1_bet*rad2deg
2832       write (iout,*) 'ANGCUT_STRAND',angcut_strand*rad2deg,&
2833                      ' MAXANG_STRAND',angcut1_strand*rad2deg
2834       write (iout,*) 'FRAC_MIN',frac_min_set
2835 ! Find secondary structure elements (helices and beta-sheets)
2836       call secondary2(.true.,.false.,ncont_pept_ref,icont_pept_ref,&
2837          isec_ref)
2838 ! Define primary fragments. First include the helices.
2839       nhairp=0
2840       nstrand=0
2841 ! Merge helices
2842 ! AL 12/23/03 - to avoid splitting helices into very small fragments
2843       if (merge_helices) then
2844       write (iout,*) "Before merging helices: nhfrag",nhfrag
2845       do i=1,nhfrag
2846         write (2,*) hfrag(1,i),hfrag(2,i)
2847       enddo
2848       i=1
2849       do while (i.lt.nhfrag)
2850         if (hfrag(1,i+1)-hfrag(2,i).le.1) then
2851           nhfrag=nhfrag-1
2852           hfrag(2,i)=hfrag(2,i+1)
2853           do j=i+1,nhfrag
2854             hfrag(1,j)=hfrag(1,j+1)
2855             hfrag(2,j)=hfrag(2,j+1)
2856           enddo
2857         endif 
2858         i=i+1
2859       enddo
2860       write (iout,*) "After merging helices: nhfrag",nhfrag
2861       do i=1,nhfrag
2862         write (2,*) hfrag(1,i),hfrag(2,i)
2863       enddo
2864       endif
2865       nfrag(1)=nhfrag
2866       do i=1,nhfrag
2867         npiece(i,1)=1
2868         ifrag(1,1,i)=hfrag(1,i) 
2869         ifrag(2,1,i)=hfrag(2,i) 
2870         n_shift(1,i,1)=0
2871         n_shift(2,i,1)=nshift_hel
2872         ang_cut(i)=angcut_hel
2873         ang_cut1(i)=angcut1_hel
2874         frac_min(i)=frac_min_set
2875         nc_fragm(i,1)=ncfrac_hel
2876         nc_req_setf(i,1)=ncreq_hel
2877         istruct(i)=1
2878       enddo
2879       write (iout,*) "isplit_bet",isplit_bet
2880       if (isplit_bet.gt.1) then
2881 ! Split beta-sheets into strands and store strands as primary fragments.
2882         call split_beta(nbfrag,bfrag,nstrand,istrand,nhairp,ihairp)
2883         do i=1,nstrand
2884           ii=i+nfrag(1)
2885           npiece(ii,1)=1
2886           ifrag(1,1,ii)=istrand(1,i)
2887           ifrag(2,1,ii)=istrand(2,i)
2888           n_shift(1,ii,1)=nshift_strand
2889           n_shift(2,ii,1)=nshift_strand
2890           ang_cut(ii)=angcut_strand
2891           ang_cut1(ii)=angcut1_strand
2892           frac_min(ii)=frac_min_set
2893           nc_fragm(ii,1)=0
2894           nc_req_setf(ii,1)=0
2895           istruct(ii)=3
2896         enddo
2897         nfrag(1)=nfrag(1)+nstrand
2898       else if (isplit_bet.eq.1) then
2899 ! Split only far beta-sheets; does not split hairpins.
2900         call find_and_remove_hairpins(nbfrag,bfrag,nhairp,ihairp)
2901         call split_beta(nbfrag,bfrag,nstrand,istrand,nhairp,ihairp)
2902         do i=1,nhairp
2903           ii=i+nfrag(1)
2904           npiece(ii,1)=1
2905           ifrag(1,1,ii)=ihairp(1,i) 
2906           ifrag(2,1,ii)=ihairp(2,i) 
2907           n_shift(1,ii,1)=nshift_bet
2908           n_shift(2,ii,1)=nshift_bet
2909           ang_cut(ii)=angcut_bet
2910           ang_cut1(ii)=angcut1_bet
2911           frac_min(ii)=frac_min_set
2912           nc_fragm(ii,1)=ncfrac_bet
2913           nc_req_setf(ii,1)=ncreq_bet
2914           istruct(ii)=2
2915         enddo
2916         nfrag(1)=nfrag(1)+nhairp
2917         do i=1,nstrand
2918           ii=i+nfrag(1)
2919           npiece(ii,1)=1
2920           ifrag(1,1,ii)=istrand(1,i)
2921           ifrag(2,1,ii)=istrand(2,i)
2922           n_shift(1,ii,1)=nshift_strand
2923           n_shift(2,ii,1)=nshift_strand
2924           ang_cut(ii)=angcut_strand
2925           ang_cut1(ii)=angcut1_strand
2926           frac_min(ii)=frac_min_set
2927           nc_fragm(ii,1)=0
2928           nc_req_setf(ii,1)=0
2929           istruct(ii)=3
2930         enddo
2931         nfrag(1)=nfrag(1)+nstrand
2932       else
2933 ! Do not split beta-sheets; each pair of strands is a primary element.
2934         call find_and_remove_hairpins(nbfrag,bfrag,nhairp,ihairp)
2935         do i=1,nhairp
2936           ii=i+nfrag(1)
2937           npiece(ii,1)=1
2938           ifrag(1,1,ii)=ihairp(1,i) 
2939           ifrag(2,1,ii)=ihairp(2,i) 
2940           n_shift(1,ii,1)=nshift_bet
2941           n_shift(2,ii,1)=nshift_bet
2942           ang_cut(ii)=angcut_bet
2943           ang_cut1(ii)=angcut1_bet
2944           frac_min(ii)=frac_min_set
2945           nc_fragm(ii,1)=ncfrac_bet
2946           nc_req_setf(ii,1)=ncreq_bet
2947           istruct(ii)=2
2948         enddo
2949         nfrag(1)=nfrag(1)+nhairp
2950         do i=1,nbfrag
2951           ii=i+nfrag(1)
2952           npiece(ii,1)=2
2953           ifrag(1,1,ii)=bfrag(1,i) 
2954           ifrag(2,1,ii)=bfrag(2,i) 
2955           if (bfrag(3,i).lt.bfrag(4,i)) then
2956             ifrag(1,2,ii)=bfrag(3,i)
2957             ifrag(2,2,ii)=bfrag(4,i)
2958           else
2959             ifrag(1,2,ii)=bfrag(4,i)
2960             ifrag(2,2,ii)=bfrag(3,i)
2961           endif
2962           n_shift(1,ii,1)=nshift_bet
2963           n_shift(2,ii,1)=nshift_bet
2964           ang_cut(ii)=angcut_bet
2965           ang_cut1(ii)=angcut1_bet
2966           frac_min(ii)=frac_min_set
2967           nc_fragm(ii,1)=ncfrac_bet
2968           nc_req_setf(ii,1)=ncreq_bet
2969           istruct(ii)=4
2970         enddo
2971         nfrag(1)=nfrag(1)+nbfrag
2972       endif
2973       write (iout,*) "The following primary fragments were found:"
2974       write (iout,*) "Helices:",nhfrag
2975       do i=1,nhfrag
2976         mnum=molnum(i)
2977         i1=ifrag(1,1,i)
2978         i2=ifrag(2,1,i)
2979         it1=itype(i1,mnum)
2980         it2=itype(i2,mnum)
2981         write (iout,'(i3,2x,a,i4,2x,a,i4)') &
2982              i,restyp(it1,mnum),i1,restyp(it2,mnum),i2
2983       enddo
2984       write (iout,*) "Hairpins:",nhairp
2985       do i=nhfrag+1,nhfrag+nhairp
2986         i1=ifrag(1,1,i)
2987         i2=ifrag(2,1,i)
2988         it1=itype(i1,molnum(i1))
2989         it2=itype(i2,molnum(i2))
2990         write (iout,'(i3,2x,a,i4,2x,a,i4,2x)') &
2991              i,restyp(it1,molnum(i1)),i1,restyp(it2,molnum(i2)),i2
2992       enddo
2993       write (iout,*) "Far strand pairs:",nbfrag
2994       do i=nhfrag+nhairp+1,nhfrag+nhairp+nbfrag
2995         i1=ifrag(1,1,i)
2996         i2=ifrag(2,1,i)
2997         it1=itype(i1,molnum(i1))
2998         it2=itype(i2,molnum(i1))
2999         i3=ifrag(1,2,i)
3000         i4=ifrag(2,2,i)
3001         it3=itype(i3,molnum(i3))
3002         it4=itype(i4,molnum(i4))
3003         write (iout,'(i3,2x,a,i4,2x,a,i4," and ",a,i4,2x,a,i4)') &
3004              i,restyp(it1,molnum(i1)),i1,restyp(it2,molnum(i2)),i2,&
3005                restyp(it3,molnum(i3)),i3,restyp(it4,molnum(i4)),i4
3006       enddo
3007       write (iout,*) "Strands:",nstrand
3008       do i=nhfrag+nhairp+nbfrag+1,nfrag(1)
3009         mnum=molnum(i)
3010         i1=ifrag(1,1,i)
3011         i2=ifrag(2,1,i)
3012         it1=itype(i1,mnum)
3013         it2=itype(i2,mnum)
3014         write (iout,'(i3,2x,a,i4,2x,a,i4)') &
3015              i,restyp(it1,mnum),i1,restyp(it2,mnum),i2
3016       enddo
3017       call imysort(nfrag(1),2,maxpiece,ifrag(1,1,1),npiece(1,1),&
3018         istruct(1),n_shift(1,1,1),ang_cut(1),ang_cut1(1),frac_min(1),&
3019         nc_fragm(1,1),nc_req_setf(1,1))
3020       write (iout,*) "Fragments after sorting:"
3021       do i=1,nfrag(1)
3022         mnum=molnum(i)
3023         i1=ifrag(1,1,i)
3024         i2=ifrag(2,1,i)
3025         it1=itype(i1,mnum)
3026         it2=itype(i2,mnum)
3027         write (iout,'(i3,2x,a,i4,2x,a,i4,$)') &
3028              i,restyp(it1,molnum(it1)),i1,restyp(it2,molnum(it2)),i2
3029         if (npiece(i,1).eq.1) then
3030           write (iout,'(2x,a)') strstr(istruct(i))
3031         else
3032           i1=ifrag(1,2,i)
3033           i2=ifrag(2,2,i)
3034           it1=itype(i1,mnum)
3035           it2=itype(i2,mnum)
3036           write (iout,'(2x,a,i4,2x,a,i4,2x,a)') &
3037              restyp(it1,molnum(it1)),i1,restyp(it2,molnum(it2)),i2,strstr(istruct(i))
3038         endif
3039       enddo
3040       return
3041       end subroutine define_fragments
3042 !------------------------------------------------------------------------------
3043       subroutine find_and_remove_hairpins(nbfrag,bfrag,nhairp,ihairp)
3044 !      implicit real*8 (a-h,o-z)
3045 !      include 'DIMENSIONS'
3046 !      include 'DIMENSIONS.ZSCOPT'
3047 !      include 'DIMENSIONS.COMPAR'
3048 !      include 'COMMON.IOUNITS'
3049       integer :: nbfrag,bfrag(4,nres/3)
3050       integer :: nhairp,ihairp(2,nres/5)
3051       integer :: i,j,k 
3052       write (iout,*) "Entered find_and_remove_hairpins"
3053       write (iout,*) "nbfrag",nbfrag
3054       do i=1,nbfrag
3055         write (iout,*) i,(bfrag(k,i),k=1,4)
3056       enddo
3057       nhairp=0
3058       i=1
3059       do while (i.le.nbfrag)
3060         write (iout,*) "check hairpin:",i,(bfrag(j,i),j=1,4)
3061         if (bfrag(3,i).gt.bfrag(4,i) .and. bfrag(4,i)-bfrag(2,i).lt.5) &
3062         then
3063           write (iout,*) "Found hairpin:",i,bfrag(1,i),bfrag(3,i)
3064           nhairp=nhairp+1
3065           ihairp(1,nhairp)=bfrag(1,i)
3066           ihairp(2,nhairp)=bfrag(3,i) 
3067           nbfrag=nbfrag-1
3068           do j=i,nbfrag
3069             do k=1,4
3070               bfrag(k,j)=bfrag(k,j+1)
3071             enddo
3072           enddo
3073         else
3074           i=i+1
3075         endif
3076       enddo
3077       write (iout,*) "After finding hairpins:"
3078       write (iout,*) "nhairp",nhairp
3079       do i=1,nhairp
3080         write (iout,*) i,ihairp(1,i),ihairp(2,i)
3081       enddo
3082       write (iout,*) "nbfrag",nbfrag
3083       do i=1,nbfrag
3084         write (iout,*) i,(bfrag(k,i),k=1,4)
3085       enddo
3086       return
3087       end subroutine find_and_remove_hairpins
3088 !------------------------------------------------------------------------------
3089       subroutine split_beta(nbfrag,bfrag,nstrand,istrand,nhairp,ihairp)
3090 !      implicit real*8 (a-h,o-z)
3091 !      include 'DIMENSIONS'
3092 !      include 'DIMENSIONS.ZSCOPT'
3093 !      include 'DIMENSIONS.COMPAR'
3094 !      include 'COMMON.IOUNITS'
3095       integer :: nbfrag,bfrag(4,nres/3)
3096       integer :: nstrand,istrand(2,nres/2)
3097       integer :: nhairp,ihairp(2,nres/5) 
3098       logical :: found
3099       integer :: i,k
3100       write (iout,*) "Entered split_beta"
3101       write (iout,*) "nbfrag",nbfrag
3102       do i=1,nbfrag
3103         write (iout,*) i,(bfrag(k,i),k=1,4)
3104       enddo
3105       nstrand=0
3106       do i=1,nbfrag
3107         write (iout,*) "calling add_strand:",i,bfrag(1,i),bfrag(2,i)
3108         call add_strand(nstrand,istrand,nhairp,ihairp,&
3109            bfrag(1,i),bfrag(2,i),found)
3110         if (bfrag(3,i).lt.bfrag(4,i)) then
3111           write (iout,*) "calling add_strand:",i,bfrag(3,i),bfrag(4,i)
3112           call add_strand(nstrand,istrand,nhairp,ihairp,&
3113            bfrag(3,i),bfrag(4,i),found)
3114         else
3115           write (iout,*) "calling add_strand:",i,bfrag(4,i),bfrag(3,i)
3116           call add_strand(nstrand,istrand,nhairp,ihairp,&
3117             bfrag(4,i),bfrag(3,i),found)
3118         endif
3119       enddo
3120       nbfrag=0
3121       write (iout,*) "Strands found:",nstrand
3122       do i=1,nstrand
3123         write (iout,*) i,istrand(1,i),istrand(2,i)
3124       enddo
3125       return
3126       end subroutine split_beta
3127 !------------------------------------------------------------------------------
3128       subroutine add_strand(nstrand,istrand,nhairp,ihairp,is1,is2,found)
3129 !      implicit real*8 (a-h,o-z)
3130 !      include 'DIMENSIONS'
3131 !      include 'DIMENSIONS.ZSCOPT'
3132 !      include 'DIMENSIONS.COMPAR'
3133 !      include 'COMMON.IOUNITS'
3134       integer :: nstrand,istrand(2,nres/2)
3135       integer :: nhairp,ihairp(2,nres/5) 
3136       logical :: found
3137       integer :: is1,is2,j,idelt
3138       found=.false.
3139       do j=1,nhairp
3140         idelt=(ihairp(2,j)-ihairp(1,j))/6
3141         if (is1.lt.ihairp(2,j)-idelt.and.is2.gt.ihairp(1,j)+idelt) then
3142           write (iout,*) "strand",is1,is2," is part of hairpin",&
3143             ihairp(1,j),ihairp(2,j)
3144           return
3145         endif
3146       enddo
3147       do j=1,nstrand
3148         idelt=(istrand(2,j)-istrand(1,j))/3
3149         if (is1.lt.istrand(2,j)-idelt.and.is2.gt.istrand(1,j)+idelt) &
3150         then
3151 ! The strand already exists in the array; update its ends if necessary.
3152           write (iout,*) "strand",is1,is2," found at position",j,&
3153            ":",istrand(1,j),istrand(2,j)
3154           istrand(1,j)=min0(istrand(1,j),is1)
3155           istrand(2,j)=max0(istrand(2,j),is2)
3156           return   
3157         endif
3158       enddo
3159 ! The strand has not been found; add it to the array.
3160       write (iout,*) "strand",is1,is2," added to the array."
3161       found=.true.
3162       nstrand=nstrand+1
3163       istrand(1,nstrand)=is1
3164       istrand(2,nstrand)=is2
3165       return
3166       end subroutine add_strand
3167 !------------------------------------------------------------------------------
3168       subroutine secondary2(lprint,lprint_sec,ncont,icont,isecstr)
3169
3170       use geometry_data, only:anatemp,rad2deg,phi,nstart_sup,nend_sup
3171       use energy_data, only:itype,maxcont,molnum
3172       use compare_data, only:bfrag,hfrag,nbfrag,nhfrag
3173       use compare, only:freeres
3174 !      implicit real*8 (a-h,o-z)
3175 !      include 'DIMENSIONS'
3176 !      include 'DIMENSIONS.ZSCOPT'
3177 !      include 'COMMON.IOUNITS'
3178 !      include 'COMMON.FRAG'
3179 !      include 'COMMON.VAR'
3180 !      include 'COMMON.GEO'
3181 !      include 'COMMON.CHAIN'
3182 !      include 'COMMON.NAMES'
3183 !      include 'COMMON.INTERACT'
3184       integer :: ncont,icont(2,maxcont),isec(nres,4),nsec(nres+1),&
3185         isecstr(nres+1)
3186       logical :: lprint,lprint_sec,not_done !el,freeres
3187       integer :: i,j,ii1,jj1,i1,j1,ij,k,ien,ist
3188       integer :: nstrand,nbeta,nhelix,iii1,jjj1,mnum
3189       real(kind=8) :: p1,p2
3190 !rel      external freeres
3191       character(len=1) :: csec(0:2)=reshape((/'-','E','H'/),shape(csec))
3192       if (lprint) then
3193         write (iout,*) "entered secondary2",ncont
3194         write (iout,*) "nstart_sup",nstart_sup," nend_sup",nend_sup
3195         do i=1,ncont
3196           write (iout,*) icont(1,i),icont(2,i)
3197         enddo
3198       endif
3199       do i=1,nres
3200         isecstr(i)=0
3201       enddo
3202       nbfrag=0
3203       nhfrag=0
3204       do i=1,nres
3205         isec(i,1)=0
3206         isec(i,2)=0
3207         nsec(i)=0
3208       enddo
3209
3210 ! finding parallel beta
3211 !d      write (iout,*) '------- looking for parallel beta -----------'
3212       nbeta=0
3213       nstrand=0
3214       do i=1,ncont
3215         i1=icont(1,i)
3216         j1=icont(2,i)
3217         if (i1.ge.nstart_sup .and. i1.le.nend_sup &
3218            .and. j1.gt.nstart_sup .and. j1.le.nend_sup) then
3219 !d        write (iout,*) "parallel",i1,j1
3220         if(j1-i1.gt.5 .and. freeres(i1,j1,nsec,isec)) then
3221           ii1=i1
3222           jj1=j1
3223 !d          write (iout,*) i1,j1
3224           not_done=.true.
3225           do while (not_done)
3226            i1=i1+1
3227            j1=j1+1
3228             do j=1,ncont
3229               if (i1.eq.icont(1,j) .and. j1.eq.icont(2,j) .and. &
3230                    freeres(i1,j1,nsec,isec)) goto 5
3231             enddo
3232             not_done=.false.
3233   5         continue
3234 !d            write (iout,*) i1,j1,not_done
3235           enddo
3236           j1=j1-1
3237           i1=i1-1
3238           if (i1-ii1.gt.1) then
3239             ii1=max0(ii1-1,1)
3240             jj1=max0(jj1-1,1)
3241             nbeta=nbeta+1
3242             if(lprint)write(iout,'(a,i3,4i4)')'parallel beta',&
3243                      nbeta,ii1,i1,jj1,j1
3244
3245             nbfrag=nbfrag+1
3246             bfrag(1,nbfrag)=ii1+1
3247             bfrag(2,nbfrag)=i1+1
3248             bfrag(3,nbfrag)=jj1+1
3249             bfrag(4,nbfrag)=min0(j1+1,nres) 
3250
3251             do ij=ii1,i1
3252              nsec(ij)=nsec(ij)+1
3253              isec(ij,nsec(ij))=nbeta
3254             enddo
3255             do ij=jj1,j1
3256              nsec(ij)=nsec(ij)+1
3257              isec(ij,nsec(ij))=nbeta
3258             enddo
3259
3260            if(lprint_sec) then 
3261             nstrand=nstrand+1
3262             if (nbeta.le.9) then
3263               write(12,'(a18,i1,a9,i3,a2,i3,a1)') &
3264                 "DefPropRes 'strand",nstrand,&
3265                 "' 'num = ",ii1-1,"..",i1-1,"'"
3266             else
3267               write(12,'(a18,i2,a9,i3,a2,i3,a1)') &
3268                 "DefPropRes 'strand",nstrand,&
3269                 "' 'num = ",ii1-1,"..",i1-1,"'"
3270             endif
3271             nstrand=nstrand+1
3272             if (nbeta.le.9) then
3273               write(12,'(a18,i1,a9,i3,a2,i3,a1)') &
3274                 "DefPropRes 'strand",nstrand,&
3275                 "' 'num = ",jj1-1,"..",j1-1,"'"
3276             else
3277               write(12,'(a18,i2,a9,i3,a2,i3,a1)') &
3278                 "DefPropRes 'strand",nstrand,&
3279                 "' 'num = ",jj1-1,"..",j1-1,"'"
3280             endif
3281               write(12,'(a8,4i4)') &
3282                 "SetNeigh",ii1-1,i1-1,jj1-1,j1-1
3283            endif
3284           endif
3285         endif
3286         endif ! i1.ge.nstart_sup .and. i1.le.nend_sup .and. i2.gt.nstart_sup .and. i2.le.nend_sup
3287       enddo
3288
3289 ! finding antiparallel beta
3290 !d      write (iout,*) '--------- looking for antiparallel beta ---------'
3291
3292       do i=1,ncont
3293         i1=icont(1,i)
3294         j1=icont(2,i)
3295         if (freeres(i1,j1,nsec,isec)) then
3296           ii1=i1
3297           jj1=j1
3298 !d          write (iout,*) i1,j1
3299
3300           not_done=.true.
3301           do while (not_done)
3302            i1=i1+1
3303            j1=j1-1
3304             do j=1,ncont
3305               if (i1.eq.icont(1,j).and.j1.eq.icont(2,j) .and. &
3306                    freeres(i1,j1,nsec,isec)) goto 6
3307             enddo
3308             not_done=.false.
3309   6         continue
3310 !d            write (iout,*) i1,j1,not_done
3311           enddo
3312           i1=i1-1
3313           j1=j1+1
3314           if (i1-ii1.gt.1) then
3315
3316             nbfrag=nbfrag+1
3317             bfrag(1,nbfrag)=ii1
3318             bfrag(2,nbfrag)=min0(i1+1,nres)
3319             bfrag(3,nbfrag)=min0(jj1+1,nres)
3320             bfrag(4,nbfrag)=j1
3321
3322             nbeta=nbeta+1
3323             iii1=max0(ii1-1,1)
3324             do ij=iii1,i1
3325              nsec(ij)=nsec(ij)+1
3326              if (nsec(ij).le.2) then
3327               isec(ij,nsec(ij))=nbeta
3328              endif
3329             enddo
3330             jjj1=max0(j1-1,1)  
3331             do ij=jjj1,jj1
3332              nsec(ij)=nsec(ij)+1
3333              if (nsec(ij).le.2) then
3334               isec(ij,nsec(ij))=nbeta
3335              endif
3336             enddo
3337
3338
3339            if (lprint_sec) then
3340             write (iout,'(a,i3,4i4)')'antiparallel beta',&
3341                          nbeta,ii1-1,i1,jj1,j1-1
3342             nstrand=nstrand+1
3343             if (nstrand.le.9) then
3344               write(12,'(a18,i1,a9,i3,a2,i3,a1)') &
3345                 "DefPropRes 'strand",nstrand,&
3346                 "' 'num = ",ii1-2,"..",i1-1,"'"
3347             else
3348               write(12,'(a18,i2,a9,i3,a2,i3,a1)') &
3349                 "DefPropRes 'strand",nstrand,&
3350                 "' 'num = ",ii1-2,"..",i1-1,"'"
3351             endif
3352             nstrand=nstrand+1
3353             if (nstrand.le.9) then
3354               write(12,'(a18,i1,a9,i3,a2,i3,a1)') &
3355                 "DefPropRes 'strand",nstrand,&
3356                 "' 'num = ",j1-2,"..",jj1-1,"'"
3357             else
3358               write(12,'(a18,i2,a9,i3,a2,i3,a1)') &
3359                 "DefPropRes 'strand",nstrand,&
3360                 "' 'num = ",j1-2,"..",jj1-1,"'"
3361             endif
3362               write(12,'(a8,4i4)') &
3363                 "SetNeigh",ii1-2,i1-1,jj1-1,j1-2
3364            endif
3365           endif
3366         endif
3367       enddo
3368
3369 !d      write (iout,*) "After beta:",nbfrag
3370 !d      do i=1,nbfrag
3371 !d        write (iout,*) (bfrag(j,i),j=1,4)
3372 !d      enddo
3373
3374       if (nstrand.gt.0.and.lprint_sec) then
3375         write(12,'(a27,$)') "DefPropRes 'sheet' 'strand1"
3376         do i=2,nstrand
3377          if (i.le.9) then
3378           write(12,'(a9,i1,$)') " | strand",i
3379          else
3380           write(12,'(a9,i2,$)') " | strand",i
3381          endif
3382         enddo
3383         write(12,'(a1)') "'"
3384       endif
3385
3386        
3387 ! finding alpha or 310 helix
3388
3389       nhelix=0
3390       do i=1,ncont
3391         i1=icont(1,i)
3392         j1=icont(2,i)
3393         p1=phi(i1+2)*rad2deg
3394         p2=0.0
3395         if (j1+2.le.nres) p2=phi(j1+2)*rad2deg
3396
3397
3398         if (j1.eq.i1+3 .and. &
3399              ((p1.ge.10.and.p1.le.80).or.i1.le.2).and. &
3400              ((p2.ge.10.and.p2.le.80).or.j1.le.2.or.j1.ge.nres-3) )then
3401 !d          if (j1.eq.i1+3) write (iout,*) "found 1-4 ",i1,j1,p1,p2
3402 !o          if (j1.eq.i1+4) write (iout,*) "found 1-5 ",i1,j1,p1,p2
3403           ii1=i1
3404           jj1=j1
3405           if (nsec(ii1).eq.0) then 
3406             not_done=.true.
3407           else
3408             not_done=.false.
3409           endif
3410           do while (not_done)
3411             i1=i1+1
3412             j1=j1+1
3413             do j=1,ncont
3414               if (i1.eq.icont(1,j) .and. j1.eq.icont(2,j)) goto 10
3415             enddo
3416             not_done=.false.
3417   10        continue
3418             p1=phi(i1+2)*rad2deg
3419             p2=phi(j1+2)*rad2deg
3420             if (p1.lt.10.or.p1.gt.80.or.p2.lt.10.or.p2.gt.80) &
3421                                     not_done=.false.
3422
3423 !d           write (iout,*) i1,j1,not_done,p1,p2
3424           enddo
3425           j1=j1+1
3426           if (j1-ii1.gt.4) then
3427             nhelix=nhelix+1
3428 !d            write (iout,*)'helix',nhelix,ii1,j1
3429
3430             nhfrag=nhfrag+1
3431             hfrag(1,nhfrag)=ii1
3432             hfrag(2,nhfrag)=j1
3433
3434             do ij=ii1,j1
3435              nsec(ij)=-1
3436             enddo
3437            if (lprint_sec) then
3438             write (iout,'(a,i3,2i4)') "Helix",nhelix,ii1-1,j1-1
3439             if (nhelix.le.9) then
3440               write(12,'(a17,i1,a9,i3,a2,i3,a1)') &
3441                 "DefPropRes 'helix",nhelix,&
3442                 "' 'num = ",ii1-1,"..",j1-2,"'"
3443             else
3444               write(12,'(a17,i2,a9,i3,a2,i3,a1)') &
3445                 "DefPropRes 'helix",nhelix,&
3446                 "' 'num = ",ii1-1,"..",j1-2,"'"
3447             endif
3448            endif
3449           endif
3450         endif
3451       enddo
3452        
3453       if (nhelix.gt.0.and.lprint_sec) then
3454         write(12,'(a26,$)') "DefPropRes 'helix' 'helix1"
3455         do i=2,nhelix
3456          if (nhelix.le.9) then
3457           write(12,'(a8,i1,$)') " | helix",i
3458          else
3459           write(12,'(a8,i2,$)') " | helix",i
3460          endif
3461         enddo
3462         write(12,'(a1)') "'"
3463       endif
3464
3465       if (lprint_sec) then
3466        write(12,'(a37)') "DefPropRes 'coil' '! (helix | sheet)'"
3467        write(12,'(a20)') "XMacStand ribbon.mac"
3468       endif
3469         
3470       if (lprint) then
3471
3472         write(iout,*) 'UNRES seq:',anatemp
3473         do j=1,nbfrag
3474          write(iout,*) 'beta ',(bfrag(i,j),i=1,4)
3475         enddo
3476   
3477         do j=1,nhfrag
3478          write(iout,*) 'helix ',(hfrag(i,j),i=1,2),anatemp
3479         enddo
3480
3481       endif   
3482   
3483       do j=1,nbfrag
3484         do k=min0(bfrag(1,j),bfrag(2,j)),max0(bfrag(1,j),bfrag(2,j)) 
3485           isecstr(k)=1
3486         enddo
3487         do k=min0(bfrag(3,j),bfrag(4,j)),max0(bfrag(3,j),bfrag(4,j)) 
3488           isecstr(k)=1
3489         enddo
3490       enddo
3491       do j=1,nhfrag
3492         do k=hfrag(1,j),hfrag(2,j)
3493           isecstr(k)=2
3494         enddo
3495       enddo
3496       if (lprint) then
3497         write (iout,*)
3498         write (iout,*) "Secondary structure"
3499         do i=1,nres,80
3500           mnum=molnum(i)
3501           ist=i
3502           ien=min0(i+79,nres)
3503           write (iout,*)
3504           write (iout,'(8(7x,i3))') (k,k=ist+9,ien,10)
3505           write (iout,'(80a1)') (onelet(itype(k,mnum)),k=ist,ien) 
3506           write (iout,'(80a1)') (csec(isecstr(k)),k=ist,ien) 
3507         enddo 
3508         write (iout,*)
3509       endif
3510       return
3511       end subroutine secondary2
3512 !-------------------------------------------------
3513 !      logical function freeres(i,j,nsec,isec)
3514 !      include 'DIMENSIONS'
3515 !      integer :: isec(nres,4),nsec(nres)
3516 !      integer :: i,j,k,l
3517 !      freeres=.false.
3518 !
3519 !      if (nsec(i).gt.1.or.nsec(j).gt.1) return
3520 !      do k=1,nsec(i)
3521 !        do l=1,nsec(j)
3522 !          if (isec(i,k).eq.isec(j,l)) return
3523 !        enddo
3524 !      enddo
3525 !      freeres=.true.
3526 !      return
3527 !      end function freeres
3528 !-------------------------------------------------
3529        subroutine alloc_compar_arrays(nfrg,nlev)
3530
3531        use energy_data, only:maxcont
3532        use w_comm_local
3533        integer :: nfrg,nlev
3534
3535 !write(iout,*) "in alloc conpar arrays: nlevel=", nlevel," nfrag(1)=",nfrag(1)
3536 !------------------------
3537 ! commom.contacts
3538 !      common /contacts/
3539       allocate(nsccont_frag_ref(mmaxfrag)) !(mmaxfrag) !wham
3540       allocate(isccont_frag_ref(2,maxcont,mmaxfrag)) !(2,maxcont,mmaxfrag) !wham
3541 !------------------------
3542 ! COMMON.COMPAR
3543 !      common /compar/
3544       allocate(rmsfrag(nfrg,nlev+1),nc_fragm(nfrg,nlev+1)) !(maxfrag,maxlevel)
3545       allocate(qfrag(nfrg,2)) !(maxfrag,2)
3546       allocate(rmscutfrag(2,nfrg,nlev+1)) !(2,maxfrag,maxlevel)
3547       allocate(ang_cut(nfrg),ang_cut1(nfrg),frac_min(nfrg)) !(maxfrag)
3548       allocate(nc_req_setf(nfrg,nlev+1),npiece(nfrg,nlev+1),&
3549         ielecont(nfrg,nlev+1),isccont(nfrg,nlev+1),irms(nfrg,nlev+1),&
3550         ishifft(nfrg,nlev+1),len_frag(nfrg,nlev+1)) !(maxfrag,maxlevel)
3551       allocate(ncont_nat(2,nfrg,nlev+1))
3552       allocate(n_shift(2,nfrg,nlev+1)) !(2,maxfrag,maxlevel)
3553 !      allocate(nfrag(nlev)) !(maxlevel)
3554       allocate(isnfrag(nlev+2)) !(maxlevel+1)
3555       allocate(ifrag(2,maxpiece,nfrg)) !(2,maxpiece,maxfrag)
3556       allocate(ipiece(maxpiece,nfrg,2:nlev+1)) !(maxpiece,maxfrag,2:maxlevel)
3557       allocate(istruct(nfrg),iloc(nfrg),nlist_frag(nfrg)) !(maxfrag)
3558       allocate(iclass(nlev*nfrg,nlev+1)) !(maxlevel*maxfrag,maxlevel)
3559       allocate(list_frag(nres,nfrg)) !(maxres,maxfrag)
3560 !------------------------
3561 ! COMMON.PEPTCONT
3562 !      common /peptcont/
3563 !      integer,dimension(:,:),allocatable :: icont_pept_ref !(2,maxcont)
3564       allocate(ncont_frag_ref(mmaxfrag)) !(mmaxfrag)
3565       allocate(icont_frag_ref(2,maxcont,mmaxfrag)) !(2,maxcont,mmaxfrag)
3566 !      integer,dimension(:),allocatable :: isec_ref !(maxres)
3567 !------------------------
3568 !      module w_comm_local
3569 !      common /ccc/
3570       allocate(creff(3,2*nres),cc(3,2*nres)) !(3,nres*2)
3571       allocate(iadded(nres)) !(nres)
3572       allocate(inumber(2,nres)) !(2,nres)
3573
3574
3575 !-------------------------------------------------------------------------------
3576       end subroutine alloc_compar_arrays
3577 #endif
3578 !-------------------------------------------------------------------------------
3579       end module conform_compar 
3580