!     Last change:  RCD   5 May 2010    2:10 pm
!-----------------------------------------------------------------------------------------------------------
	PROGRAM ASA_molecular_set_Collective_Distances
!
    parameter(NatMX=151)			!Maximum atoms per Molecule
!
    Integer*4 MolMolMAX
    parameter(MOLMAX=70)  			!Maximum number of Molecules
    parameter(MolMolMAX=(MOLMAX*MOLMAX+MOLMAX)/2)
!
    CHARACTER (LEN=80):: GENERAL_TITLE
!-------------------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------------------
   	 double precision :: zero; PARAMETER(zero=0.0d0)
!
	integer*4 :: MoloM
    integer*4 :: MM,iii,II,IJ
    Double precision  DistE,D_Coll
	Double precision  Zim,Deig,Zum
	allocatable:: zim(:),Deig(:),Zum(:,:),DistE(:)
!
!----------------------------------------------------------------------------------------------------------------
    Character*70 :: ID_Label_Pre,ID_Label_Suf
    Character*70 :: QSMatrix_IN 
    Character*70 :: INPUT_QSM,OUTPUT_QSM
    Character*70 :: OUTPUT_EDIST
!----------------------------------------------------------------------------------------------------------------
!		Reads a label file, where it is found 
!			input/output file names
!
		open(304,File='Labelstaff_CollDIS.txt')
!
		Read(304,*)ID_Label_Pre
        Read(304,*)ID_Label_Suf
        Read(304,*)QSMatrix_IN
!
        close(304)
!
!       Reads the optimal similarity matrix and the Carbó Index matrix
!
		INPUT_QSM=TRIM(ID_Label_Pre)//TRIM(QSMatrix_IN)//TRIM(ID_Label_Suf)//'.txt'
 		Open(111,File=INPUT_QSM)
        READ(111,'(a80)')General_title;READ(111,*)Molom,MM
!
		ALLOCATE(Zim(MM),Deig(Molom),Zum(Molom,Molom),DistE(MM))
!
   	 	Read(111,*)(iii,Zim(iii),kkk=1,MM); CLOSE(111)
!
                MilMil=MM
!
!-------------------------------------------------------------------------------------------------------------------
!
!		Save the Optimal Overlap Matrix on file Z_OVERLAP.txt
!
		OUTPUT_QSM=TRIM(ID_Label_Pre)//'OUT_QSM'//TRIM(ID_Label_Suf)//'.txt'
!
        OPEN(34,FILE=OUTPUT_QSM)
        WRITE(34,'(A80)')GENERAL_TITLE
        write(34,*)Molom,milmil
!
        DO I=1,Molom
        II=(I*I-I)/2 
        Write(34,*)I
        do J=1,I; IJ=J+II ; Deig(J)=Zim(IJ); 
          Zum(J,I)=Zim(IJ); Zum(I,J)=Zum(J,I); end do
        write(34,*)(J,Deig(J),J=1,I)
        END DO
        Close(34)
!
        deallocate(Deig)
!
		OUTPUT_EDIST=TRIM(ID_Label_Pre)//'E_DIST'//TRIM(ID_Label_Suf)//'.txt'
        OPEN (666,FILE=OUTPUT_EDIST)
                WRITE(666,'(a80)')General_title
                WRITE(666,*)Molom
!                
				do I=1,Molom
                II=(I*I-I)/2
                  do J=1,I
                    IJ=J+II
                    DistE(IJ)=zero
                    if(I==J)cycle
                      DistE(IJ)=Zum(I,I)+Zum(J,J)-2*Zum(J,I)
                    end do
                    end do
!                    
                D_Coll=zero
				do I=1,Molom
                II=(I*I-I)/2
                do J=1,I
                  IJ=II+J
                  if(I==J)Cycle
                  D_Coll=D_Coll+DistE(IJ)
                  end do
                  end do
!
			Write(666,*)(IJ,DistE(IJ),IJ=1,MM)
        	Write(666,'(5x,"Collective Distance:  ",E15.8)')D_Coll
            write(666,'(5x,"SQRT(CollDistance) :  ",E15.8)')SQRT(D_Coll)
            MM=(Molom*Molom-Molom)/2
        	Write(666,'(5x,"Mean CollDistance  :  ",E15.8)')D_Coll/MM
!
        CLOSE(666)
!
        WRITE(*,*)' Molecular Quantum Similarity Suite'
		write(*,*)' Collective Distance Success!'
		pause        
!
!--------------------------------------------------------------------------------------------------------------------
	end                !program Collective Euclidian Distances
!...............................................................................................................
!..................................................................................................
