
subroutine StoreConf( Istore, FinalConf, Nsp, MaxSp, Nmol, NAMEsp, &
				      Conf, BoxSize, DXYZ, DROT, CDV, MaxMol, &
				      LENGTHlj, LENGTHion, SPECIES, MaxBeads, BEADTYPE, &
				      MaxLJ, MaxIon, Xlj, Ylj, Zlj, Xion, Yion, Zion, Seed )

! This subroutine stores the current conformation to FinalConf if Istore = 0
! If Istore = 1, 2, or 3 the configuration is stored in a temporary file.

implicit none

integer, parameter										:: Nphases = 2

integer, intent(inout)									:: Istore  

character*30, intent(in)							    :: FinalConf
	! If Istore==1,2 or 3 then the configuration is stored to a 
	! temporary file called 'tmp0'//'1,2, or 3'//'_'//TRIM(FinalConf).

integer, intent(in)										:: Nsp
integer, intent(in)										:: MaxSp

integer, dimension(0:MaxSp,0:Nphases), intent(in)		:: Nmol

character*15, dimension(MaxSp), intent(in)				:: NAMEsp

integer, intent(in)										:: Conf

real, dimension(Nphases), intent(in)					:: BoxSize

real, dimension(MaxSp,Nphases), intent(in)				:: DXYZ
real, dimension(MaxSp,Nphases), intent(in)				:: DROT
real, dimension(Nphases), intent(in)					:: CDV

integer, intent(in)										:: MaxMol

integer, dimension(MaxMol,Nphases), intent(in)			:: LENGTHlj
integer, dimension(MaxMol,Nphases), intent(in)			:: LENGTHion
integer, dimension(MaxMol,Nphases), intent(in)			:: SPECIES

integer, intent(in)										:: MaxBeads

character*5, dimension(MaxBeads,MaxSp), intent(in)		:: BEADTYPE

integer, intent(in)										:: MaxLJ
integer, intent(in)										:: MaxIon

real, dimension(MaxLJ,Nphases), intent(in)				:: Xlj, Ylj, Zlj
real, dimension(MaxIon,Nphases), intent(in)				:: Xion, Yion, Zion

integer, intent(in)										:: Seed


! Local variables.

integer					:: i, j, k
integer					:: lenlj, lenion, sp
integer					:: ljb, ionb

integer, parameter		:: unit = 15

character*6, parameter  :: name01 = 'tmp01_'
character*6, parameter  :: name02 = 'tmp02_'
character*6, parameter  :: name03 = 'tmp03_'


if ( Istore == 0 ) then

     open( unit, file = FinalConf )

else if ( 1 <= Istore   .AND.   Istore <= 3 ) then

     ! Istore == 1 or 2 cover the usual case of saving the current
     ! configuration in two files, so that we have protection against
     ! a system crash.
     
	 if( Istore == 1 ) then
       
	    Istore = 2	    
        open( unit, file = name01//TRIM(FinalConf) )
     
	 else if ( Istore == 2 ) then
     
	    Istore = 3
        open( unit, file = name02//TRIM(FinalConf) )
     
	 else if ( Istore == 3 ) then
     
	    Istore = 1
        open( unit, file = name03//TRIM(FinalConf) )
     
	 end if

end if

! Write name and molecules of each species.
do i = 1, Nsp
     write(unit,"(A, T45, 4(I5,2X) )") NAMEsp(i),    &
                 Nmol(i, 1:Nphases), Nmol(i,0)
enddo

! Write number of molecules per phase, BOXLENGTHs etc.
write(unit,*)
write(unit,*) Conf, Seed
write(unit,*) BoxSize( 1: Nphases )
write(unit,*)

do i = 1, Nphases

    write(unit, "(A, I5, A)") ' Iphase = ', i, &
           ' DXYZ(iphase), DROT(iphase), Constant DV(iphase) (A^3)'
    
	write(unit,"( 10(1X,G24.16) )") DXYZ(1:Nsp,i )
    
	write(unit,"( 10(1X,G24.16) )") DROT(1:Nsp,i )
	
	write(unit,"( 10(1X,G24.16) )") CDV( i )

end do

write(unit,*) 

! Write configuration.

do i = 1, Nphases

	ljb = 0 
	ionb = 0
     
	 do j = 1, Nmol(0,i)

		lenlj = LENGTHlj(j,i)
		lenion = LENGTHion(j,i)

		sp = SPECIES(j,i)

		do k = 1, lenlj + lenion

			if( BEADTYPE(k,sp) == 'LJ' ) then

				ljb = ljb + 1

				write(unit, "(3(1X,G24.16), 4(1X,I5))")	&
					Xlj(ljb,i), Ylj(ljb,i), Zlj(ljb,i), j, i, k, sp
					
			else if( BEADTYPE(k,sp) == 'ION' ) then

				ionb = ionb + 1

				write(unit, "(3(1X,G24.16), 4(1X,I5))")	&
					Xion(ionb,i), Yion(ionb,i), Zion(ionb,i), j, i, k, sp

			end if

		end do

	end do

end do

close( unit )
	 
return

end subroutine StoreConf




