unres_package_Oct_2016 from emilial
[unres4.git] / source / cluster / cluster.f90
1       program cluster
2 !
3 ! Program to cluster united-residue MCM results.
4 !
5       use clust_data
6       use probability
7       use tracking
8       use hc_
9       use io_clust
10 !#define CLUSTER
11       use io_units
12       use io_base, only: permut
13       use geometry_data, only: nres,theta,phi,alph,omeg,&
14                          c,cref
15       use energy_data, only: nnt,nct
16       use control_data, only: symetr,outpdb,outmol2,titel,&
17                           iopt,print_dist,MaxProcs
18       use control, only: tcpu,initialize
19
20       use wham_data, only: punch_dist
21       use io_wham, only: parmread
22       use work_part 
23 !      include 'DIMENSIONS'
24 !      include 'sizesclu.dat'
25 #ifdef MPI
26       use mpi_data
27       implicit none
28       include "mpif.h"
29       integer :: IERROR,ERRCODE !STATUS(MPI_STATUS_SIZE)
30 #else
31       implicit none
32 !      include "COMMON.MPI"
33 #endif
34 !      include 'COMMON.TIME1'
35 !      include 'COMMON.INTERACT'
36 !      include 'COMMON.NAMES'
37 !      include 'COMMON.GEO'
38 !      include 'COMMON.HEADER'
39 !      include 'COMMON.CONTROL'
40 !      include 'COMMON.CHAIN'
41 !      include 'COMMON.VAR'
42 !      include 'COMMON.CLUSTER'
43 !      include 'COMMON.IOUNITS'
44 !      include 'COMMON.FREE'
45
46       logical,dimension(:),allocatable :: printang !(max_cut)
47       integer,dimension(:),allocatable :: printpdb !(max_cut)
48       integer,dimension(:),allocatable :: printmol2 !(max_cut)
49       character(len=240) lineh
50       REAL(kind=4),dimension(:),allocatable :: CRIT,MEMBR !(maxconf)
51       REAL(kind=4),dimension(:),allocatable :: CRITVAL !(maxconf-1)
52       INTEGER,dimension(:),allocatable :: IA,IB !(maxconf)
53       INTEGER,dimension(:,:),allocatable :: ICLASS !(maxconf,maxconf-1)
54       INTEGER,dimension(:),allocatable :: HVALS !(maxconf-1)
55       INTEGER,dimension(:),allocatable :: IORDER,HEIGHT !(maxconf-1)
56       integer,dimension(:),allocatable :: nn !(maxconf)
57       integer :: ndis
58       real(kind=4),dimension(:),allocatable :: DISNN !(maxconf)
59       LOGICAL,dimension(:),allocatable :: FLAG !(maxconf)
60       integer :: i,j,k,l,m,n,len,lev,idum,ii,ind,jj,icut,ncon,&
61         it,ncon_work,ind1,kkk
62       real(kind=8) :: t1,t2,difconf
63       
64       real(kind=8),dimension(:),allocatable :: varia !(maxvar)
65       real(kind=8),dimension(:),allocatable :: list_conf_ !(maxvar)
66       real(kind=8) :: hrtime,mintime,sectime
67       logical :: eof
68       external :: difconf
69 !el
70       real(kind=4),dimension(:),allocatable :: diss_ !(maxdist)
71       integer,dimension(:),allocatable :: scount_ !(maxdist)
72 #ifdef MPI
73       call MPI_Init( IERROR )
74       call MPI_Comm_rank( MPI_COMM_WORLD, me, IERROR )
75       call MPI_Comm_size( MPI_COMM_WORLD, nprocs, IERROR )
76       Master = 0
77       if (ierror.gt.0) then
78         write(iout,*) "SEVERE ERROR - Can't initialize MPI."
79         call mpi_finalize(ierror)
80         stop
81       endif
82       if (nprocs.gt.MaxProcs+1) then
83         write (2,*) "Error - too many processors",&
84          nprocs,MaxProcs+1
85         write (2,*) "Increase MaxProcs and recompile"
86         call MPI_Finalize(IERROR)
87         stop
88       endif
89 #endif
90 !elwrite(iout,*) "before parmread"
91       allocate(printang(max_cut))
92       allocate(printpdb(max_cut))
93       allocate(printmol2(max_cut))
94       call initialize
95 !elwrite(iout,*) "before parmread"
96       call openunits
97 !elwrite(iout,*) "before parmread"
98       call parmread
99       call read_control
100 !elwrite(iout,*) "after read control"
101       call molread
102 !      if (refstr) call read_ref_structure(*30)
103       do i=1,nres
104         phi(i)=0.0D0
105         theta(i)=0.0D0
106         alph(i)=0.0D0
107         omeg(i)=0.0D0
108       enddo
109 !      write (iout,*) "Before permut"
110 !       write (iout,*) "symetr", symetr
111 !      call flush(iout)
112       call permut(symetr)
113 !      write (iout,*) "after permut"
114 !      call flush(iout)
115       print *,'MAIN: nnt=',nnt,' nct=',nct
116
117       DO I=1,NCUT
118         PRINTANG(I)=.FALSE.
119         PRINTPDB(I)=0
120         printmol2(i)=0
121         IF (RCUTOFF(I).LT.0.0) THEN
122           RCUTOFF(I)=ABS(RCUTOFF(I))
123           PRINTANG(I)=.TRUE.
124           PRINTPDB(I)=outpdb
125           printmol2(i)=outmol2
126         ENDIF
127       ENDDO
128       write (iout,*) 'Number of cutoffs:',NCUT
129       write (iout,*) 'Cutoff values:'
130       DO ICUT=1,NCUT
131         WRITE(IOUT,'(8HRCUTOFF(,I2,2H)=,F8.1,2i2)')ICUT,RCUTOFF(ICUT),&
132           printpdb(icut),printmol2(icut)
133       ENDDO
134       DO I=1,NRES-3  
135         MULT(I)=1
136       ENDDO
137       allocate(list_conf(maxconf))
138       do i=1,maxconf
139         list_conf(i)=i
140       enddo
141       call read_coords(ncon,*20)
142
143       allocate(list_conf_(maxconf))
144       do i=1,maxconf
145         list_conf_(i)=list_conf(i)
146       enddo
147       deallocate(list_conf) 
148       allocate(list_conf(ncon))
149       do i=1,ncon
150         list_conf(i)=list_conf_(i)
151       enddo
152       deallocate(list_conf_) 
153
154 !el      call alloc_clust_arrays(ncon)
155  
156       write (iout,*) 'from read_coords: ncon',ncon
157       
158       write (iout,*) "nT",nT
159       do iT=1,nT
160       write (iout,*) "iT",iT
161 #ifdef MPI
162       call work_partition(.true.,ncon)
163 #endif
164 !elwrite(iout,*)"after work partition, ncon_work=", ncon_work,ncon
165
166       call probabl(iT,ncon_work,ncon,*20)
167
168 !elwrite(iout,*)"after probabl, ncon_work=", ncon_work,ncon
169
170       if (ncon_work.lt.2) then
171         write (iout,*) "Too few conformations; clustering skipped"
172         exit
173       endif
174 #ifdef MPI
175       ndis=ncon_work*(ncon_work-1)/2
176       call work_partition(.true.,ndis)
177 #endif
178 !el      call alloc_clust_arrays(ncon_work)
179       allocate(ICC(ncon_work))
180       allocate(DISS(maxdist))
181
182       DO I=1,NCON_work
183         ICC(I)=I
184       ENDDO
185       WRITE (iout,'(A80)') TITEL
186       t1=tcpu()
187 !
188 ! CALCULATE DISTANCES
189 !
190       call daread_ccoords(1,ncon_work)
191       ind1=0
192       DO I=1,NCON_work-1
193         if (mod(i,100).eq.0) print *,'Calculating RMS i=',i
194         do k=1,2*nres
195           do l=1,3
196             c(l,k)=allcart(l,k,i)
197           enddo 
198         enddo
199         kkk=1
200         do k=1,nres
201           do l=1,3
202             cref(l,k,kkk)=c(l,k)
203           enddo
204         enddo
205         DO J=I+1,NCON_work
206           IND=IOFFSET(NCON_work,I,J)
207 #ifdef MPI
208           if (ind.ge.indstart(me) .and. ind.le.indend(me)) then
209 #endif
210           ind1=ind1+1
211           DISS(IND1)=DIFCONF(I,J)
212 !          write (iout,'(2i4,i10,f10.5)') i,j,ind,DISS(IND)
213 #ifdef MPI
214           endif
215 #endif
216         ENDDO
217       ENDDO
218       t2=tcpu()
219       WRITE (iout,'(/a,1pe14.5,a/)') &
220        'Time for distance calculation:',T2-T1,' sec.'
221       t1=tcpu()
222       PRINT '(a)','End of distance computation'
223 !el---------------
224       allocate(diss_(maxdist))
225       allocate(scount_(0:nprocs))
226       
227       do i=1,maxdist
228         diss_(i)=diss(i)
229       enddo
230       do i=0,nprocs
231         scount_(i)=scount(i)
232       enddo
233 !el-----------
234 #ifdef MPI
235       call MPI_Gatherv(diss_(1),scount_(me),MPI_REAL,diss(1),&
236            scount(0),idispl(0),MPI_REAL,Master,MPI_COMM_WORLD, IERROR)
237       if (me.eq.master) then
238 #endif
239       deallocate(diss_)
240       deallocate(scount_)
241  
242       open(80,file='/tmp/distance',form='unformatted')
243       do i=1,ndis
244         write(80) diss(i)
245       enddo
246       if (punch_dist) then
247         do i=1,ncon_work-1
248           do j=i+1,ncon_work
249             IND=IOFFSET(NCON,I,J)
250             write (jrms,'(2i5,2f10.5)') i,j,diss(IND),&
251               energy(j)-energy(i)
252           enddo
253         enddo
254       endif
255 !
256 ! Print out the RMS deviation matrix.
257 !
258       if (print_dist) CALL DISTOUT(NCON_work)
259 !
260 !  call hierarchical clustering HC from F. Murtagh
261 !
262       N=NCON_work
263       LEN = (N*(N-1))/2
264       write(iout,*) "-------------------------------------------"
265       write(iout,*) "HIERARCHICAL CLUSTERING using"
266       if (iopt.eq.1) then
267         write(iout,*) "WARD'S MINIMUM VARIANCE METHOD"
268       elseif (iopt.eq.2) then
269         write(iout,*) "SINGLE LINK METHOD"
270       elseif (iopt.eq.3) then
271         write(iout,*) "COMPLETE LINK METHOD"
272       elseif (iopt.eq.4) then
273         write(iout,*) "AVERAGE LINK (OR GROUP AVERAGE) METHOD"
274       elseif (iopt.eq.5) then
275         write(iout,*) "MCQUITTY'S METHOD"
276       elseif (iopt.eq.6) then
277         write(iout,*) "MEDIAN (GOWER'S) METHOD"
278       elseif (iopt.eq.7) then
279         write(iout,*) "CENTROID METHOD"
280       else
281         write(iout,*) "IOPT=",iopt," IS INVALID, use 1-7"
282         write(*,*) "IOPT=",iopt," IS INVALID, use 1-7"
283         stop
284       endif
285       write(iout,*)
286       write(iout,*) "hc.f by F. Murtagh, ESA/ESO/STECF, Garching"
287       write(iout,*) "February 1986"
288       write(iout,*) "References:"
289       write(iout,*) "1. Multidimensional clustering algorithms"
290       write(iout,*) "   Fionn Murtagh"
291       write(iout,*) "   Vienna : Physica-Verlag, 1985."
292       write(iout,*) "2. Multivariate data analysis"
293       write(iout,*) "   Fionn Murtagh and Andre Heck"
294       write(iout,*) "   Kluwer Academic Publishers, 1987"
295       write(iout,*) "-------------------------------------------"
296       write(iout,*)
297
298 #ifdef DEBUG
299       write (iout,*) "The TOTFREE array"
300       do i=1,ncon_work
301         write (iout,'(2i5,f10.5)') i,list_conf(i),totfree(i)
302       enddo
303 #endif
304       allocate(CRIT(N),MEMBR(N)) !(maxconf)
305       allocate(CRITVAL(N-1)) !(maxconf-1)
306       allocate(IA(N),IB(N))
307       allocate(ICLASS(N,N-1)) !(maxconf,maxconf-1)
308       allocate(HVALS(N-1)) !(maxconf-1)
309       allocate(IORDER(N-1),HEIGHT(N-1)) !(maxconf-1)
310       allocate(nn(N)) !(maxconf)
311       allocate(DISNN(N)) !(maxconf)
312       allocate(FLAG(N)) !(maxconf)
313  
314       call flush(iout)
315       CALL HC(N,M,LEN,IOPT,IA,IB,CRIT,MEMBR,NN,DISNN,FLAG,DISS)
316       LEV = N-1
317       write (iout,*) "n",n," ncon_work",ncon_work," lev",lev
318       if (lev.lt.2) then
319         write (iout,*) "Too few conformations to cluster."
320         goto 192
321       endif
322       CALL HCASS(N,IA,IB,CRIT,LEV,ICLASS,HVALS,IORDER,CRITVAL,HEIGHT)
323 !      CALL HCDEN(LEV,IORDER,HEIGHT,CRITVAL)
324
325       allocate(licz(maxgr))
326       allocate(iass(maxgr))
327       allocate(nconf(maxgr,maxingr))
328       allocate(totfree_gr(maxgr))
329
330       do i=1,maxgr
331         licz(i)=0
332       enddo
333       icut=1
334       i=1
335       NGR=i+1
336       do j=1,n
337         licz(iclass(j,i))=licz(iclass(j,i))+1
338         nconf(iclass(j,i),licz(iclass(j,i)))=j
339 !        write (iout,*) i,j,iclass(j,i),licz(iclass(j,i)),
340 !     &    nconf(iclass(j,i),licz(iclass(j,i)))
341       enddo        
342       do i=1,lev-1
343
344          idum=lev-i
345          DO L=1,LEV
346             IF (HEIGHT(L).EQ.IDUM) GOTO 190
347          ENDDO
348  190     IDUM=L
349          write(IOUT,*) "i+1",i+1," idum",idum," critval",CRITVAL(IDUM),&
350           " icut",icut," cutoff",rcutoff(icut)
351          IF (CRITVAL(IDUM).LT. RCUTOFF(ICUT)) THEN
352           WRITE (iout,'(/a,f10.5)') 'AT CUTOFF:',rcutoff(icut)
353           write (iout,'(a,f8.2)') 'Maximum distance found:',&
354                     CRITVAL(IDUM)
355           CALL SRTCLUST(ICUT,ncon_work,iT)
356           CALL TRACK(ICUT)
357           CALL WRTCLUST(ncon_work,ICUT,PRINTANG,PRINTPDB,PRINTMOL2,iT)
358           icut=icut+1
359           if (icut.gt.ncut) goto 191
360          ENDIF
361          NGR=i+1
362          do l=1,maxgr
363           licz(l)=0
364          enddo
365          do j=1,n
366          enddo
367          do j=1,n
368           licz(iclass(j,i))=licz(iclass(j,i))+1
369           nconf(iclass(j,i),licz(iclass(j,i)))=j
370 !d        write (iout,*) i,j,iclass(j,i),licz(iclass(j,i)),&
371 !d         nconf(iclass(j,i),licz(iclass(j,i)))
372 !d          print *,j,iclass(j,i),
373 !d     &     licz(iclass(j,i)),nconf(iclass(j,i),licz(iclass(j,i)))
374          enddo
375       enddo
376  191  continue
377 !
378       if (plot_tree) then
379         CALL WRITRACK
380         CALL PLOTREE
381       endif
382 !
383       t2=tcpu()
384       WRITE (iout,'(/a,1pe14.5,a/)') &
385        'Total time for clustering:',T2-T1,' sec.'
386 #ifdef MPI
387       endif
388 #endif
389  192  continue
390       enddo
391 !
392       close(icbase,status="delete")
393 #ifdef MPI
394 !el      call MPI_Finalize(MPI_COMM_WORLD,IERROR)
395       call MPI_Finalize(IERROR)
396 #endif
397       stop '********** Program terminated normally.'
398    20 write (iout,*) "Error reading coordinates"
399 #ifdef MPI
400 !el      call MPI_Finalize(MPI_COMM_WORLD,IERROR)
401       call MPI_Finalize(IERROR)
402 #endif
403       stop
404    30 write (iout,*) "Error reading reference structure"
405 #ifdef MPI
406 !el      call MPI_Finalize(MPI_COMM_WORLD,IERROR)
407       call MPI_Finalize(IERROR)
408 #endif
409       stop
410       end program cluster
411 !---------------------------------------------------------------------------
412 !
413 !---------------------------------------------------------------------------
414       real(kind=8) function difconf(icon,jcon)
415
416       use clust_data
417
418       use io_units, only: iout
419       use io_base, only: permut
420       use geometry_data, only: nres,c,cref,tabperm
421       use energy_data, only: nct,nnt
422       use control_data, only: symetr,lside,nend,nstart
423       use regularize_, only: fitsq
424       implicit none
425 !      include 'DIMENSIONS'
426 !      include 'sizesclu.dat'
427 !      include 'COMMON.CONTROL'
428 !      include 'COMMON.CLUSTER'
429 !      include 'COMMON.CHAIN' 
430 !      include 'COMMON.INTERACT'
431 !      include 'COMMON.VAR'
432 !      include 'COMMON.IOUNITS'
433       logical :: non_conv
434       real(kind=8) :: przes(3),obrot(3,3)
435       real(kind=8) :: xx(3,2*nres),yy(3,2*nres) !(3,maxres2)
436       integer :: i,ii,j,icon,jcon,kkk,nperm,chalen,zzz
437       integer :: iaperm,ibezperm,run
438       real(kind=8) :: rms,rmsmina
439 !      write (iout,*) "tu dochodze"
440       rmsmina=10d10
441       nperm=1
442       do i=1,symetr
443       nperm=i*nperm
444       enddo
445 !      write (iout,*) "nperm",nperm
446       call permut(symetr)
447 !      write (iout,*) "tabperm", tabperm(1,1)
448       do kkk=1,nperm
449       if (lside) then
450         ii=0
451         chalen=int((nend-nstart+2)/symetr)
452         do run=1,symetr
453          do i=nstart,(nstart+chalen-1)
454           zzz=tabperm(kkk,run)
455 !          write (iout,*) "tutaj",zzz
456           ii=ii+1
457           iaperm=(zzz-1)*chalen+i
458           ibezperm=(run-1)*chalen+i
459           do j=1,3
460             xx(j,ii)=allcart(j,iaperm,jcon)
461             yy(j,ii)=cref(j,ibezperm,kkk)
462           enddo
463          enddo
464         enddo
465         do run=1,symetr
466          do i=nstart,(nstart+chalen-1)
467           zzz=tabperm(kkk,run)
468           ii=ii+1
469           iaperm=(zzz-1)*chalen+i
470           ibezperm=(run-1)*chalen+i
471 !          if (itype(i).ne.10) then
472             ii=ii+1
473             do j=1,3 
474               xx(j,ii)=allcart(j,iaperm+nres,jcon)
475               yy(j,ii)=cref(j,ibezperm+nres,kkk)
476             enddo
477            enddo
478 !          endif
479         enddo
480         call fitsq(rms,xx(1,1),yy(1,1),ii,przes,obrot,non_conv)
481       else
482         chalen=int((nct-nnt+2)/symetr)
483         do run=1,symetr
484          do i=nnt,(nnt+chalen-1)
485           zzz=tabperm(kkk,run)
486 !           write (iout,*) "tu szukaj", zzz,run,kkk
487           iaperm=(zzz-1)*chalen+i
488           ibezperm=(run-1)*chalen+i
489 !        do i=nnt,nct
490           do j=1,3
491             c(j,i)=allcart(j,iaperm,jcon)
492           enddo
493          enddo
494         enddo
495         call fitsq(rms,c(1,nstart),cref(1,nstart,kkk),nend-nstart+1,&
496              przes,&
497              obrot,non_conv)
498       endif
499       if (rms.lt.0.0) then
500         print *,'error, rms^2 = ',rms,icon,jcon
501         stop
502       endif
503       if (non_conv) print *,non_conv,icon,jcon
504       if (rmsmina.gt.rms) rmsmina=rms
505       enddo
506       difconf=dsqrt(rmsmina)
507       return
508       end function difconf
509 !------------------------------------------------------------------------------
510       subroutine distout(ncon)
511
512       use clust_data
513       use hc_, only:ioffset
514       use io_units, only: iout
515       implicit none
516 !      include 'DIMENSIONS'
517 !      include 'sizesclu.dat'
518       integer :: ncon
519       integer,parameter :: ncol=10
520 !      include 'COMMON.IOUNITS'
521 !      include 'COMMON.CLUSTER'
522       integer :: i,j,k,jlim,jlim1,nlim,ind
523       real(kind=4) :: b(ncol)
524
525       write (iout,'(a)') 'The distance matrix'
526       do 1 i=1,ncon,ncol
527       nlim=min0(i+ncol-1,ncon)
528       write (iout,1000) (k,k=i,nlim)
529       write (iout,'(8h--------,10a)') ('-------',k=i,nlim)
530  1000 format (/8x,10(i4,3x))
531  1020 format (/1x,80(1h-)/)
532       do 2 j=i,ncon
533       jlim=min0(j,nlim)
534       if (jlim.eq.j) then
535         b(jlim-i+1)=0.0d0
536         jlim1=jlim-1
537       else
538         jlim1=jlim
539       endif
540       do 3 k=i,jlim1
541        if (j.lt.k) then 
542           IND=IOFFSET(NCON,j,k)
543        else
544           IND=IOFFSET(NCON,k,j)
545        endif
546     3  b(k-i+1)=diss(IND)
547       write (iout,1010) j,(b(k),k=1,jlim-i+1)
548     2 continue
549     1 continue
550  1010 format (i5,3x,10(f6.2,1x))
551       return
552       end subroutine distout
553 !------------------------------------------------------------------------------
554 ! srtclust.f
555 !------------------------------------------------------------------------------
556       SUBROUTINE SRTCLUST(ICUT,NCON,IB)
557
558       use clust_data
559       use io_units, only: iout
560 !      implicit real*8 (a-h,o-z)
561 !     include 'DIMENSIONS'
562 !     include 'sizesclu.dat'
563 !     include 'COMMON.CLUSTER'
564 !     include 'COMMON.FREE'
565 !     include 'COMMON.IOUNITS'
566       implicit none
567       real(kind=8),dimension(:),allocatable :: prob !(maxgr)
568       real(kind=8) :: emin,ene,en1,sumprob
569       integer :: igr,i,ii,li1,li2,ligr,ico,jco,ind1,ind2
570       integer :: jgr,li,nco,ib,ncon,icut
571 !
572 ! Compute free energies of clusters
573 !
574       allocate(prob(maxgr))
575
576       do igr=1,ngr
577       emin=totfree(nconf(igr,1))
578       totfree_gr(igr)=1.0d0
579       do i=2,licz(igr)
580         ii=nconf(igr,i)
581         totfree_gr(igr)=totfree_gr(igr)+dexp(-totfree(ii)+emin)
582       enddo
583 !      write (iout,*) "igr",igr," totfree",emin,
584 !     &    " totfree_gr",totfree_gr(igr)
585       totfree_gr(igr)=emin-dlog(totfree_gr(igr))
586 !      write (iout,*) igr," efree",totfree_gr(igr)/beta_h(ib)
587       enddo
588 !
589 !  SORT CONFORMATIONS IN GROUPS ACC. TO ENERGY
590 !
591       DO 16 IGR=1,NGR
592       LIGR=LICZ(IGR)
593       DO 17 ICO=1,LIGR-1
594       IND1=NCONF(IGR,ICO)
595       ENE=totfree(IND1)
596       DO 18 JCO=ICO+1,LIGR
597       IND2=NCONF(IGR,JCO)
598       EN1=totfree(IND2)
599       IF (EN1.LT.ENE) THEN
600         NCONF(IGR,ICO)=IND2
601         NCONF(IGR,JCO)=IND1
602         IND1=IND2
603         ENE=EN1
604       ENDIF
605    18 CONTINUE
606    17 CONTINUE
607    16 CONTINUE
608 !
609 !  SORT GROUPS
610 !
611       DO 71 IGR=1,NGR
612       ENE=totfree_gr(IGR)
613       DO 72 JGR=IGR+1,NGR
614       EN1=totfree_gr(JGR)
615       IF (EN1.LT.ENE) THEN
616         LI1=LICZ(IGR)
617         LI2=LICZ(JGR)
618         LI=MAX0(LI1,LI2)
619         DO 73 I=1,LI   
620         NCO=NCONF(IGR,I)
621         NCONF(IGR,I)=NCONF(JGR,I)
622         NCONF(JGR,I)=NCO
623    73   CONTINUE
624         totfree_gr(igr)=en1
625         totfree_gr(jgr)=ene
626         ENE=EN1
627         LICZ(IGR)=LI2
628         LICZ(JGR)=LI1
629       ENDIF
630    72 CONTINUE
631    71 CONTINUE
632       write (iout,'("Free energies and probabilities of clusters at",f6.1," K")')&
633            1.0d0/(1.987d-3*beta_h(ib)) !'
634       prob(1)=1.0d0
635       sumprob=1.0d0
636       do i=2,ngr
637         prob(i)=dexp(-(totfree_gr(i)-totfree_gr(1)))
638         sumprob=sumprob+prob(i)
639       enddo
640       do i=1,ngr
641         prob(i)=prob(i)/sumprob
642       enddo
643       sumprob=0.0d0
644       write (iout,'("clust   efree    prob sumprob")')
645       do i=1,ngr
646         sumprob=sumprob+prob(i)
647         write (iout,'(i5,f8.1,2f8.5)') i,totfree_gr(i)/beta_h(ib),&
648           prob(i),sumprob
649       enddo
650       DO 81 IGR=1,NGR
651       LI=LICZ(IGR)
652       DO 82 I=1,LI 
653    82 IASS(NCONF(IGR,I))=IGR
654    81 CONTINUE
655       if (lgrp) then
656         do i=1,ncon
657           iass_tot(i,icut)=iass(i)
658 !          write (iout,*) icut,i,iass(i),iass_tot(i,icut)
659         enddo
660       endif
661       RETURN
662       END SUBROUTINE SRTCLUST
663 !------------------------------------------------------------------------------
664 !------------------------------------------------------------------------------