module radiuswritemodule
contains
subroutine radiuswrite(rx,ry,rz,radius,v)

	implicit none
    integer, dimension(:), intent(inout) :: v    !global parameters !v(1) is valance electron, v(6) is core electron
    double precision, dimension(:), intent(in) :: rx,ry,rz
    double precision, intent(out) :: radius
    integer p1,p2,e1,e2,n1,n2,m,i,j
    integer :: temp(1)
    double precision comx,comy,comz,mp,mne,d1,d2,d3,d4,d5,d6,r1,rn
    double precision :: i6(6),ibs(6)
    double precision :: i5(5),i4(4)


mp=1836.1526    !proton/electron mass ratio
mne=1838.684    !neutron/electron mass ratio

p1=7
p2=12
e1=1
e2=6
n1=13
n2=18


!Center of Mass
comx=(rx(7)*mp+rx(8)*mp+rx(9)*mp+rx(10)*mp+rx(11)*mp+rx(12)*mp+rx(13)*mne+rx(14)*mne+rx(15)*mne+rx(16)*mne+rx(17)*mne+rx(18)*mne)/&
        (6.0*mp+6.0*mne)
comy=(ry(7)*mp+ry(8)*mp+ry(9)*mp+ry(10)*mp+ry(11)*mp+ry(12)*mp+ry(13)*mne+ry(14)*mne+ry(15)*mne+ry(16)*mne+ry(17)*mne+ry(18)*mne)/&
        (6.0*mp+6.0*mne)
comz=(rz(7)*mp+rz(8)*mp+rz(9)*mp+rz(10)*mp+rz(11)*mp+rz(12)*mp+rz(13)*mne+rz(14)*mne+rz(15)*mne+rz(16)*mne+rz(17)*mne+rz(18)*mne)/&
        (6.0*mp+6.0*mne)

!distance between nucleus and each electron
d1=sqrt((comx-rx(1))**2+(comy-ry(1))**2+(comz-rz(1))**2)  !d1 for electron 1
d2=sqrt((comx-rx(2))**2+(comy-ry(2))**2+(comz-rz(2))**2)  !d2 for electron 2
d3=sqrt((comx-rx(3))**2+(comy-ry(3))**2+(comz-rz(3))**2)  !d3 for electron 3
d4=sqrt((comx-rx(4))**2+(comy-ry(4))**2+(comz-rz(4))**2)  !d4 for electron 4
d5=sqrt((comx-rx(5))**2+(comy-ry(5))**2+(comz-rz(5))**2)  !d5 for electron 5
d6=sqrt((comx-rx(6))**2+(comy-ry(6))**2+(comz-rz(6))**2)  !d6 for electron 6


!if(d1>d2) then
!    radius=d1
!else
!    radius=d2
!end if

i6=(/ d1,d2,d3,d4,d5,d6 /)

radius=max(d1,d2,d3,d4,d5,d6)

!   write(8,*) "position is ", maxloc(i6)
temp = maxloc(i6)   !maxloc returns an integer with array size 1 (nonsense of course)
                    ! so we are creating "temp(1)" of array size and taking value from it
v(1) = temp(1)


ibs=i6  !before sorting array storing

! write(10,*) "Before sorting  ",i6

!!!!! sorting i6 in increasing order !!!!!!!

do i=1,5
    do j=i+1,6
        
        if (i6(i)>i6(j)) then
            rn=i6(i)
            i6(i)=i6(j)
            i6(j)=rn
        end if


    end do
end do

write(16,*) "Radius of electrons from core to valence:",i6

!!!!! End of sorting  !!!!!!!!!!


do i=1,6
        
    if (ibs(i)==i6(1)) then
        v(6)=i
    end if

    if (ibs(i)==i6(2)) then
        v(5)=i
    end if

    if (ibs(i)==i6(3)) then
        v(4)=i
    end if

    if (ibs(i)==i6(4)) then
        v(3)=i
    end if

    if (ibs(i)==i6(5)) then
        v(2)=i
    end if

    if (ibs(i)==i6(6)) then
        v(1)=i
    end if

end do

















!radius=max(d1,d2,d3,d4,d5,d6)

!if (radius==d1) then
!    val1=1
!    m=max(d2,d3,d4,d5,d6)
!end if

!if (radius==d2) then
!    val1=2
!    m=max(d1,d3,d4,d5,d6)
!end if

!if (radius==d3) then
!    val1=3
!    m=max(d1,d2,d4,d5,d6)
!end if

!if (radius==d4) then
!    val1=4
!    m=max(d1,d2,d3,d5,d6)
!end if

!if (radius==d5) then
!    val1=5
!    m=max(d1,d2,d3,d4,d6)
!end if


!if (radius==d6) then
!    val1=6
!    m=max(d1,d2,d3,d4,d5)
!end if










end subroutine radiuswrite
end module radiuswritemodule
	  
