       !****************************************************************************************************
       !              A Subroutine of Main, Generate the structures in the transition pathway              *
       !****************************************************************************************************
       
       Subroutine InterStru ( SITE, Nitr, Nstp, RMSDitv, rA, rB, dfmVA, dfmVB, dist, rn ) 
       
       Implicit None
       
! ---- Variables : --------------------------------------------------------------------------------- !
       
       Integer     SITE, Nitr
       
       Integer     Nstp
       
       Real        RMSDitv ! RMSD interval in the transition pathway (0:chose the smallest RMSD)

       Real        rA(1:SITE, 1:3)  ! Coordinates of initial stru. A
       Real        rB(1:SITE, 1:3)  ! Coordinates of initial stru. B 
       
       Real        dfmVA(1:Nitr, 1:SITE, 1:3)  ! Deformation vector of stru. A 
       Real        dfmVB(1:Nitr, 1:SITE, 1:3)  ! Deformation vector of stru. B

       Real        dist(0:Nitr, 1:2)  ! Distance between intermediate states
                                      ! dist(0,1) : between two initial structures
                                      ! dist(0,2) : between two gap after Nitr iterations
                                      ! dist(k,1) : between the final and inital, in the k^th iteration for stru. A
                                      ! dist(k,2) : between the final and inital, in the k^th iteration for stru. B
        
       Real        rn(1:Nstp, 1:SITE, 1:3)    ! The coordinates of the strucutre in the transition pathway
              
! ---- Variables : --------------------------------------------------------------------------------- !
       
       Real        riA(0:Nitr, 1:SITE, 1:3)   ! Intermediate states 
       Real        riB(0:Nitr, 1:SITE, 1:3)   ! Intermediate states
       
       Real        rnA(1:Nstp, 1:SITE, 1:3)   ! transition states
       Real        rnB(1:Nstp, 1:SITE, 1:3)   ! transition states
       Real        rnG(1:Nstp, 1:SITE, 1:3)   ! transition states
       
       Integer     nA     ! Number of steps from initial to k^th Intermediate states
       Integer     nB     ! Number of steps from initial to k^th Intermediate states
       Integer     nG     ! Number of steps in filling the gaps
       
       Real        dvgap(1:SITE, 1:3)  ! the Deformation vector for the Gap
        
! ---- Variables : --------------------------------------------------------------------------------- !
       
       Integer     n, i, j, k, ct, ni
       
! ==== Body of Subroutine InterStru ================================================================ !     

! -------------------------------------------------- Prepare intermediate states coordinates       
       Do n=1, SITE
           riA(0,n,1)=rA(n,1)
           riA(0,n,2)=rA(n,2)
           riA(0,n,3)=rA(n,3)
           
           riB(0,n,1)=rB(n,1)
           riB(0,n,2)=rB(n,2)
           riB(0,n,3)=rB(n,3)           
       Enddo

       Do k=1, Nitr
           
           Do n=1, SITE
                riA(k,n,1) = riA(k-1,n,1) + dfmVA(k,n,1)
                riA(k,n,2) = riA(k-1,n,2) + dfmVA(k,n,2)
                riA(k,n,3) = riA(k-1,n,3) + dfmVA(k,n,3)

                riB(k,n,1) = riB(k-1,n,1) + dfmVB(k,n,1)
                riB(k,n,2) = riB(k-1,n,2) + dfmVB(k,n,2)
                riB(k,n,3) = riB(k-1,n,3) + dfmVB(k,n,3)          
           Enddo 
       Enddo
! =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= 
       nA=0
       Do k=1, Nitr
            
            If( dist(k,1)>=RMSDitv ) Then
                ni = dist(k,1)/RMSDitv
            Else 
                ni = 1
            EndIf
            
            Do i=0, ni-1
                nA = nA+1                    
                Do n=1, SITE
                    rnA(nA,n,1) = riA(k-1,n,1) + dfmVA(k,n,1)*(i*1.0/(1.0*ni))
                    rnA(nA,n,2) = riA(k-1,n,2) + dfmVA(k,n,2)*(i*1.0/(1.0*ni))
                    rnA(nA,n,3) = riA(k-1,n,3) + dfmVA(k,n,3)*(i*1.0/(1.0*ni))
                Enddo ! n
            Enddo ! i
       
       Enddo ! k              
       
       ! A-B-A-B-A-B-A-B-A-B-A-B-A-B-A-B-A-B-A-B-A-B-A-B-A-B-A-B-A-B-A-B-A-B-A-B ! 
       
       nB=0
       Do k=1, Nitr
            
            If( dist(k,2)>=RMSDitv ) Then
                ni = dist(k,2)/RMSDitv
            Else 
                ni = 1
            EndIf
            
            Do i=0, ni-1
                nB = nB+1                    
                Do n=1, SITE
                    rnB(nB,n,1) = riB(k-1,n,1) + dfmVB(k,n,1)*(i*1.0/(1.0*ni))
                    rnB(nB,n,2) = riB(k-1,n,2) + dfmVB(k,n,2)*(i*1.0/(1.0*ni))
                    rnB(nB,n,3) = riB(k-1,n,3) + dfmVB(k,n,3)*(i*1.0/(1.0*ni))
                Enddo ! n
            Enddo ! i
       
       Enddo ! k              
       
       ! G+G+G+G+G+G+G+G+G+G+G+G+G+G+G+G+G+G+G+G+G+G+G+G+G+G+G+G+G+G+G+G+G+G+G+G ! 
       
       Do n=1, SITE
           dvgap(n,1) = riB(Nitr,n,1) - riA(Nitr,n,1)
           dvgap(n,2) = riB(Nitr,n,2) - riA(Nitr,n,2)
           dvgap(n,3) = riB(Nitr,n,3) - riA(Nitr,n,3)
       Enddo
       
       If( dist(0,2)>=RMSDitv ) then
            ni = dist(0,2)/RMSDitv 
       Else
            ni = 1
       EndIf
       
       nG=0
       Do i=0, ni
           nG = nG+1
           Do n=1, SITE
              rnG(nG,n,1) = riA(Nitr,n,1) + dvgap(n,1)*(i*1.0/(1.0*ni))
              rnG(nG,n,2) = riA(Nitr,n,2) + dvgap(n,2)*(i*1.0/(1.0*ni))
              rnG(nG,n,3) = riA(Nitr,n,3) + dvgap(n,3)*(i*1.0/(1.0*ni))
           Enddo
       Enddo ! i

! @*@ @*@ @*@ @*@ @*@ @*@ @*@ @*@ @*@ @*@ @*@ @*@ @*@ @*@ @*@ @*@ @*@ @*@ @*@ @*@ ! Move rnA, rnB, rnG to rn
       
       j=0
       Do i=1, nA
           j=j+1
           Do n=1, SITE
               rn(j,n,1)=rnA(i,n,1)
               rn(j,n,2)=rnA(i,n,2)
               rn(j,n,3)=rnA(i,n,3)
           Enddo ! n           
       Enddo
       
       Do i=1, nG
           j=j+1
           Do n=1, SITE
               rn(j,n,1)=rnG(i,n,1)
               rn(j,n,2)=rnG(i,n,2)
               rn(j,n,3)=rnG(i,n,3)
           Enddo ! n           
       Enddo ! i  

       Do i=nB, 1, -1
           j=j+1
           Do n=1, SITE
               rn(j,n,1)=rnB(i,n,1)
               rn(j,n,2)=rnB(i,n,2)
               rn(j,n,3)=rnB(i,n,3)
           Enddo ! n           
       Enddo
                
       If(j.NE.Nstp) Then
            Write(*,'(A)')'                             '
            Write(*,'(A,I6)') 'Error! The total steps in the transition pathway, [Nstp] :', Nstp  
            Write(*,'(A,I6)') '       The total steps in the transition pathway, [j]    :', j
            Stop
       EndIf
       
       Write(*,'(A)')'                             '
       Write(*,'(A)')      '|--------------------------------------------|'
!       Write(*,'(A,F)')    '| The RMSD interval is : ', RMSDitv
       Write(*,'(A,I3)')   '| Transition steps [Struc. A]: ', nA 
       Write(*,'(A,I3)')   '| Transition steps [Gap]     : ', nG 
       Write(*,'(A,I3)')   '| Transition steps [Struc. B]: ', nB 
       Write(*,'(A)')      '|--------------------------------------------|'
       Write(*,'(A)')'                             '
       
! =====E==N===D====================================================================================== !
       Return
       End            
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       