
Subroutine Trans( Nlj, Nion, MaxMol, MaxSp, MaxLJ, MaxIon, Nmol, &
				  Xlj, Ylj, Zlj, TYPElj, Xion, Yion, Zion, TYPEion, &
				  NSTEPS, MaxSteps, STEPSTART, STEPLENGTH, NTRIALS, &
				  MaxBeads, METHOD, MaxInt, MaxReal, INTPARAM, &
				  REALPARAM, BEADTYPE, BONDSAPART, BoxSize, SPECIES, &
				  LENGTHlj, LENGTHion, STARTlj, STARTion, PROB_SP, &
				  NTemp, BETA, Nham, LNWSTATE, XLRCORR, ELRCORR,&
				  Nljgrs, EPS, SIG, CP, ALP, RMAX, Niongrs, CHARGE, &
				  Alpha, Kmax, Nkvec, KX, KY, KZ, CONST, &
				  EXPX, EXPY, EXPZ, SUMQEXPV, SUMQX, SUMQY, SUMQZ, &
				  ULJBOX, ULJLR, UREAL, UFOURIER, USURF, USELF_CH, &
				  USELF_MOL, ULJ_MOL, UION_MOL, UINTRA, SpID, Don, &
				  Success, f14_lj, f14_ion, Seed )

implicit none

! This subroutine attempts to transfer a molecule from one phase to another.

integer, parameter										:: NPhases = 2

! Nlj is the number of LJ beads in the simulation boxes.
! Nion is the number of ionic beads in the simulation boxes.

integer, dimension(NPhases), intent(inout)				:: Nlj
integer, dimension(NPhases), intent(inout)				:: Nion

! MaxMol is the maximum number of molecules per box.
! MaxSp is the maximum number of Species in the simulation.
! MaxLJ is the maximum number of LJ beads per box.
! MaxIon is the maximum number of ionic beads per box.

integer, intent(in)										:: MaxMol
integer, intent(in)										:: MaxSp
integer, intent(in)										:: MaxLJ
integer, intent(in)										:: MaxIon

! Nmol is the number of molecules in the simulation boxes.
! Nmol(0, iphase) ==> total number of molecules in that phase.
! Nmol(1:MaxSp, iphase) ==> number of molecules of that species in that phase.

integer, dimension(0:MaxSp, 0:NPhases), intent(inout)	:: Nmol

! Xlj, Ylj, and Zlj are the coordinates of the LJ beads.
! Xion, Yion, and Zion are the coordinates of the ionic beads.

real, dimension( MaxLJ, NPhases ), intent(inout)		:: Xlj, Ylj, Zlj
real, dimension( MaxIon, NPhases ), intent(inout)		:: Xion, Yion, Zion

! TYPElj contains the group identity of each LJ bead.
! TYPEion contains the group identity of each ionic bead.

integer, dimension( MaxLJ, NPhases ), intent(inout)		:: TYPElj
integer, dimension( MaxIon, NPhases ), intent(inout)	:: TYPEion

! NSTEPS is the number of configurational bias steps it takes to grow a molecule.
! MaxSteps is the maximum number of CB steps.

integer, dimension(MaxSp), intent(in)					:: NSTEPS
integer, intent(in)										:: MaxSteps

! STEPSTART is the start bead of each CB step.
! STEPLENGTH is the number of beads in each CB step.
! NTRIALS is the number of trial orientations/locations for each CB step.

integer, dimension(MaxSteps, MaxSp), intent(in)			:: STEPSTART
integer, dimension(MaxSteps, MaxSp), intent(in)			:: STEPLENGTH
integer, dimension(MaxSteps, MaxSp), intent(in)			:: NTRIALS

! MaxBeads in the maximum number of beads per molecule.
! METHOD is the method used to grow each bead.
! MaxInt is the maximum number of integer parameters.
! MaxReal is the maximum number of real	parameters.
! INTPARAM are the integer parameters needed by the method to grow each bead.
! REALPARAM are the real parameters needed by the method to grow each bead.
! BEADTYPE indicates whether the bead is 'LJ' or 'ION'.
! BONDSAPART gives the bondlengths any two LJ beads are away from each other.

integer, intent(in)										:: MaxBeads
character*10, dimension(MaxBeads, MaxSp), intent(in)	:: METHOD
integer, intent(in)										:: MaxInt
integer, intent(in)										:: MaxReal
integer, dimension(MaxInt, MaxBeads, MaxSp), intent(in)	:: INTPARAM
real, dimension(MaxReal, MaxBeads, MaxSp), intent(in)	:: REALPARAM
character*5, dimension(MaxBeads, MaxSp), intent(in)		:: BEADTYPE
integer, dimension(MaxBeads,MaxBeads,MaxSp), intent(in)	:: BONDSAPART

! BoxSize is the length of the simulation box.

real, dimension(NPhases), intent(in)					:: BoxSize

! SPECIES contains the species identity of each molecule.
! LENGTHlj contains the number of LJ beads in each molecule.
! LENGTHion	contains the number of ionic beads in each molecule.
! STARTlj contains the starting LJ bead number for each molecule.
! STARTion contains the starting ionic bead number for each molecule.

integer, dimension(MaxMol, NPhases), intent(inout)		:: SPECIES
integer, dimension(MaxMol, NPhases), intent(inout)		:: LENGTHlj, LENGTHion
integer, dimension(MaxMol, NPhases), intent(inout)		:: STARTlj, STARTion

! PROB_SP contains the accumulative probability of selecting a given species.

real, dimension(MaxSp), intent(in)						:: PROB_SP
! NTemp is the number of temperatures being used.

integer, intent(in)										:: NTemp

! BETA contains the reciprical temperature.

real, dimension(NTemp,1), intent(in)					:: BETA

! Nham is the number of hamiltonians being used.

integer, intent(in)										:: Nham

! LNWSTATE contains the weight of each temperature and hamiltonian.

real, dimension(NTemp, Nham), intent(inout)				:: LNWSTATE

! XLRCORR and ELRCORR contain parameters for the long range LJ energy.

real, dimension(605), intent(in)						:: XLRCORR, ELRCORR

! Nljgrs is the number of LJ groups in the system.
! EPS is a rank 3 array containing the eps_ij parameters for each hamiltonian.
! SIG is a rank 3 array containing the sigma_ij parameters for each hamiltonian.
! CP contains the C_ij parameters for each hamiltonian.
! ALP contains the alpha_ij parameters for each hamiltonian.
! RMAX contains the Rmax_ij parameters for each hamiltonian.
									
integer, intent(in)										:: Nljgrs
real, dimension(Nljgrs, Nljgrs, Nham), intent(in)		:: EPS, SIG, CP, ALP, RMAX

! Niongrs is the number of ionic groups in the system.
! CHARGE is a rank 2 array containing the charge of group i for each hamiltonian.
									
integer, intent(in)										:: Niongrs
real, dimension(Niongrs, Nham)							:: CHARGE

! Alpha is an Ewald sum parameter, Alpha = kappa * L, for kappa in A + T.

real, intent(in)										:: Alpha

! Kmax is an Ewald sum parameter.
! Nkvec is the number of k-vectors used in the Fourier sum.
! KX, KY, KZ contain the vector identity of the Nkvec vectors.
! CONST contains the constant part of the Fourier summation for a given Nkvec.

integer, intent(in)										:: Kmax
integer, intent(in)										:: Nkvec
integer, dimension(Nkvec), intent(in)					:: KX, KY, KZ
real, dimension(Nkvec), intent(in)						:: CONST

! EXPX contains the value of exp( i*kx*x ) for a given kx and ion.

complex, dimension(0:Kmax, MaxIon, NPhases), intent(inout) &
														:: EXPX
complex, dimension(-Kmax:Kmax, MaxIon, NPhases), intent(inout) &
														:: EXPY, EXPZ

! SUMQEXPV contains the summation of qi*exp(i*(kx*x + ky*y + kz*z)) 
! for a given k-vector and hamiltonian.

complex, dimension(Nkvec, Nham, NPhases), intent(inout) :: SUMQEXPV

! SUMQX is the summation of qi * xi for all ions in the box.

real, dimension(Nham,NPhases), intent(inout)			:: SUMQX, SUMQY, SUMQZ

! ULJ is the LJ energy of the system without the long range correction.
! UFOURIER is the coulombic fourier energy of the system.
! UREAL is the coulombic real energy of the system.
! USURF is the coulombic surface energy of the system.
! USELF_CH is the summation of the square of all the charges.
! USELF_MOL is the self energy of a given molecule.
! ULJ_MOL is the non bonded intramolecular LJ energy of a molecule.
! ULJ_MOL is the non bonded intramolecular ionic energy of a molecule.
! UINTRA contains the bending and torsional energy of each molecule.

real, dimension(Nham,NPhases), intent(inout)			:: ULJBOX, ULJLR
real, dimension(Nham,NPhases), intent(inout)			:: UREAL, UFOURIER
real, dimension(Nham,NPhases), intent(inout)			:: USURF, USELF_CH
real, dimension(MaxMol,Nham,NPhases), intent(inout)		:: USELF_MOL
real, dimension(MaxMol,Nham,NPhases), intent(inout)		:: ULJ_MOL
real, dimension(MaxMol,Nham,NPhases), intent(inout)		:: UION_MOL
real, dimension(MaxMol,NPhases), intent(inout)			:: UINTRA

! SpID indicates which type of species was used in the move.

integer, intent(out)									:: SpID

! Don is a flag to indicate whether box 1 or box 2 was the donor phase.
! Don = 1 for Box 1.
! Don = 2 for Box 2.

integer, intent(out)									:: Don

! Success is a logical indicating whether the move was successful or not.

logical, intent(out)									:: Success

! f14_lj and f14_ion are damping factors for the 1-4 intramolecular interactions

real, intent(in)										:: f14_lj
real, intent(in)										:: f14_ion

! Seed is the current random number generator seed value.

integer, intent(inout)									:: Seed

real, external											:: ran2

! Local Variables

integer										:: h, i, k, m
integer										:: Count
integer										:: Rec
integer										:: MolSpecies
integer										:: Mol
integer										:: lenlj, lenion
integer										:: stlj, stion
integer										:: endlj, endion
integer, dimension(MaxLJ)					:: TYPElj_new
integer, dimension(MaxIon)					:: TYPEion_new
integer, dimension(Nljgrs)					:: NGROUPS

integer, allocatable, dimension(:) 			:: TYPElj_grow
integer, allocatable, dimension(:) 			:: TYPEion_grow

logical										:: New

real										:: Random
real										:: PiRatio
real										:: Uintra_grow
real										:: CoulCombo
real										:: BigW_old, BigW_new
real, dimension(MaxLJ)						:: Xlj_new, Ylj_new, Zlj_new
real, dimension(MaxIon)						:: Xion_new, Yion_new, Zion_new
real, dimension(Nham)						:: ULJBOX_new, ULJLR_new
real, dimension(Nham)						:: UREAL_new, UFOURIER_new
real, dimension(Nham)						:: USURF_new, USELF_CH_new
real, dimension(Nham)						:: ULJBOX_grow, ULJLR_grow
real, dimension(Nham)						:: UREAL_grow, UFOURIER_grow
real, dimension(Nham)						:: USURF_grow, USELF_CH_grow
real, dimension(Nham)						:: USELF_MOL_grow
real, dimension(Nham)						:: ULJ_MOL_grow
real, dimension(Nham)						:: UION_MOL_grow
real, dimension(Nham)						:: dULJBOX
real, dimension(Nham)						:: dUREAL, dUFOURIER
real, dimension(Nham)						:: dUSURF, dUSELF_CH
real, dimension(Nham)						:: SUMQX_new, SUMQY_new, SUMQZ_new
real, dimension(Nham)						:: SUMQX_grow, SUMQY_grow, SUMQZ_grow
real, dimension(Nljgrs,Nljgrs,Nham)			:: EPS_TMP

real, allocatable, dimension(:) 			:: Xlj_grow, Ylj_grow, Zlj_grow
real, allocatable, dimension(:) 			:: Xion_grow, Yion_grow, Zion_grow

real, parameter								:: Pi = 3.14159265359
real, parameter								:: ec = 1.60217733e-19
real, parameter								:: eps0 = 8.854187817e-12
real, parameter								:: kB = 1.380658e-23
										
complex, dimension(Nkvec, Nham)				:: SUMQEXPV_new, SUMQEXPV_grow
complex, allocatable, dimension(:,:)		:: EXPX_grow, EXPY_grow, EXPZ_grow
complex, allocatable, dimension(:,:)		:: CZERO1, CZERO2


CoulCombo = ec * ec * 1.0e10 / ( 4.0 * Pi * eps0 * kB )

Success = .False.

Random = ran2(Seed)
SpID = 0
i = 1

do while ( SpID == 0 )

	if( Random < PROB_SP(i) ) SpID = i

	i = i + 1

end do

if( ran2(Seed) < 0.5 ) then

	Don = 1
	Rec = 2

else 

	Don = 2
	Rec = 1

end if

if( Nmol(SpID, Don) == 0 ) return

MolSpecies = int( Nmol( SpID, Don) * ran2(Seed) ) + 1

i = 0
Count = 0

do while ( Count < MolSpecies )
	
	i = i + 1
	
	if( SPECIES(i, Don) == SpID ) Count = Count + 1

end do

Mol = i

lenlj = LENGTHlj(Mol, Don)
stlj = STARTlj(Mol, Don)
endlj = stlj + lenlj - 1

lenion = LENGTHion(Mol, Don)
stion = STARTion(Mol, Don)
endion = stion + lenion - 1

if( stlj == 1 ) then

	if( Nmol(0,Don) == 1 ) then

		Xlj_new = 0.0
		Ylj_new = 0.0
		Zlj_new = 0.0

		TYPElj_new = 0
	
	else

		Xlj_new( 1:Nlj(Don)-lenlj ) = Xlj( endlj+1:Nlj(Don), Don )
		Ylj_new( 1:Nlj(Don)-lenlj ) = Ylj( endlj+1:Nlj(Don), Don )
		Zlj_new( 1:Nlj(Don)-lenlj ) = Zlj( endlj+1:Nlj(Don), Don )

		TYPElj_new( 1:Nlj(Don)-lenlj ) = TYPElj( endlj+1:Nlj(Don), Don )

	end if

else if( stlj + lenlj - 1 == Nlj(Don) ) then

	Xlj_new( 1:stlj-1 ) = Xlj( 1:stlj-1, Don )
	Ylj_new( 1:stlj-1 ) = Ylj( 1:stlj-1, Don )
	Zlj_new( 1:stlj-1 ) = Zlj( 1:stlj-1, Don )

	TYPElj_new( 1:stlj-1 ) = TYPElj( 1:stlj-1, Don )

else

	Xlj_new( 1:stlj-1 ) = Xlj( 1:stlj-1, Don )
	Xlj_new( stlj:Nlj(Don)-lenlj ) = Xlj( endlj+1:Nlj(Don), Don )
	Ylj_new( 1:stlj-1 ) = Ylj( 1:stlj-1, Don )
	Ylj_new( stlj:Nlj(Don)-lenlj ) = Ylj( endlj+1:Nlj(Don), Don )
	Zlj_new( 1:stlj-1 ) = Zlj( 1:stlj-1, Don )
	Zlj_new( stlj:Nlj(Don)-lenlj ) = Zlj( endlj+1:Nlj(Don), Don )

	TYPElj_new( 1:stlj-1 ) = TYPElj( 1:stlj-1, Don )
	TYPElj_new( stlj:Nlj(Don)-lenlj ) = TYPElj( endlj+1:Nlj(Don), Don )

end if

if( lenion > 0 ) then

	if( stion == 1 ) then

		if( Nmol(0,Don) == 1 ) then

			Xion_new = 0.0
			Yion_new = 0.0
			Zion_new = 0.0

			TYPEion_new = 0.0

		else

			Xion_new( 1:Nion(Don)-lenion ) = Xion( endion+1:Nion(Don), Don )
			Yion_new( 1:Nion(Don)-lenion ) = Yion( endion+1:Nion(Don), Don )
			Zion_new( 1:Nion(Don)-lenion ) = Zion( endion+1:Nion(Don), Don )

			TYPEion_new( 1:Nion(Don)-lenion ) = TYPEion( endion+1:Nion(Don), Don )

		end if

	else if( stion + lenion - 1 == Nion(Don) ) then

		Xion_new( 1:stion-1 ) = Xion( 1:stion-1, Don )
		Yion_new( 1:stion-1 ) = Yion( 1:stion-1, Don )
		Zion_new( 1:stion-1 ) = Zion( 1:stion-1, Don )

		TYPEion_new( 1:stion-1 ) = TYPEion( 1:stion-1, Don )

	else

		Xion_new( 1:stion-1 ) = Xion( 1:stion-1, Don )
		Xion_new( stion:Nion(Don)-lenion ) = Xion( endion+1:Nion(Don), Don )
		Yion_new( 1:stion-1 ) = Yion( 1:stion-1, Don )
		Yion_new( stion:Nion(Don)-lenion ) = Yion( endion+1:Nion(Don), Don )
		Zion_new( 1:stion-1 ) = Zion( 1:stion-1, Don )
		Zion_new( stion:Nion(Don)-lenion ) = Zion( endion+1:Nion(Don), Don )

		TYPEion_new( 1:stion-1 ) = TYPEion( 1:stion-1, Don )
		TYPEion_new( stion:Nion(Don)-lenion ) = TYPEion( endion+1:Nion(Don), Don )

	end if

end if

allocate( Xlj_grow(lenlj) )
allocate( Ylj_grow(lenlj) )
allocate( Zlj_grow(lenlj) )

allocate( TYPElj_grow(lenlj) )

Xlj_grow( 1:lenlj ) = Xlj( stlj:endlj, Don )
Ylj_grow( 1:lenlj ) = Ylj( stlj:endlj, Don )
Zlj_grow( 1:lenlj ) = Zlj( stlj:endlj, Don )

TYPElj_grow( 1:lenlj ) = TYPElj( stlj:endlj, Don )

call e6molecule( lenlj, Xlj_grow, Ylj_grow, Zlj_grow, &
				 TYPElj_grow, Nlj(Don)-lenlj, &
				 Xlj_new, Ylj_new, Zlj_new, TYPElj_new, &
				 Nham, Nljgrs, EPS, SIG, CP, ALP, RMAX, &
				 BoxSize(Don), dULJBOX )

ULJBOX_new(:) = ULJBOX(:, Don) - dULJBOX(:)

ULJBOX_grow = ULJBOX_new

NGROUPS = 0
	
do k = 1, Nlj(Don)-lenlj

	NGROUPS( TYPElj_new(k) ) = NGROUPS( TYPElj_new(k) ) + 1

end do

if( CP(1,1,1) > 0.0 ) then

	EPS_TMP = EPS * CP * ALP / ( ALP - 6.0 ) / 4.0

else

	EPS_TMP = EPS

end if

call lrcorr( Nljgrs, Nham, BoxSize(Don), NGROUPS, EPS_TMP, SIG, XLRCORR, &
			 ELRCORR, ULJLR_new )

ULJLR_grow = ULJLR_new

ULJ_MOL_grow = 0.0

Uintra_grow = 0.0

if( lenion > 0 ) then

	allocate( Xion_grow(lenion) )
	allocate( Yion_grow(lenion) )
	allocate( Zion_grow(lenion) )

	allocate( TYPEion_grow(lenion) )

	Xion_grow( 1:lenion ) = Xion( stion:endion, Don )
	Yion_grow( 1:lenion ) = Yion( stion:endion, Don )
	Zion_grow( 1:lenion ) = Zion( stion:endion, Don )

	TYPEion_grow( 1:lenion ) = TYPEion( stion:endion, Don )

	call RealMolecule( lenion, Xion_grow, Yion_grow, Zion_grow, &
					   TYPEion_grow, Nion(Don)-lenion, &
					   Xion_new, Yion_new, Zion_new, TYPEion_new, &
					   Nham, Niongrs, CHARGE, BoxSize(Don), Alpha, dUREAL )

	UREAL_new(:) = UREAL(:, Don) - dUREAL(:) * CoulCombo

	UREAL_grow = UREAL_new

	Xion_grow = -Xion_grow
	Yion_grow = -Yion_grow
	Zion_grow = -Zion_grow

	call Surf_Move( lenion, Xion_grow, Yion_grow, Zion_grow, &
					TYPEion_grow, Nham, Niongrs, CHARGE, &
					BoxSize(Don), SUMQX(:,Don), SUMQY(:,Don), SUMQZ(:,Don), &
					SUMQX_new, SUMQY_new, SUMQZ_new, dUSURF )

	Xion_grow = -Xion_grow
	Yion_grow = -Yion_grow
	Zion_grow = -Zion_grow

	SUMQX_grow = SUMQX_new
	SUMQY_grow = SUMQY_new
	SUMQZ_grow = SUMQZ_new


	USURF_new(:) = USURF(:, Don) + dUSURF(:) * CoulCombo

	USURF_grow = USURF_new

	allocate( EXPX_grow(0:Kmax, lenion ) )
	allocate( EXPY_grow(-Kmax:Kmax, lenion ) )
	allocate( EXPZ_grow(-Kmax:Kmax, lenion ) )

	allocate( CZERO1(0:Kmax,lenion) )
	allocate( CZERO2(-Kmax:Kmax,lenion) )

	CZERO1 = ( 0.0, 0.0 )
	CZERO2 = ( 0.0, 0.0 )

	CHARGE = -CHARGE

	call Fourier_Move( lenion, Xion_grow, Yion_grow, Zion_grow, &
					   TYPEion_grow, Nham, Niongrs, &
					   CHARGE, BoxSize(Don), Kmax, Nkvec, KX, KY, KZ, &
					   CONST, CZERO1, CZERO2, CZERO2, &
					   EXPX_grow, EXPY_grow, EXPZ_grow, &
					   SUMQEXPV(:,:,Don), SUMQEXPV_new, dUFOURIER )

	CHARGE = -CHARGE

	SUMQEXPV_grow = SUMQEXPV_new

	UFOURIER_new(:) = UFOURIER(:, Don) + dUFOURIER * CoulCombo

	UFOURIER_grow = UFOURIER_new

	deallocate( CZERO1 )
	deallocate( CZERO2 )

	dUSELF_CH = 0.0

	do h = 1, Nham
					
		do m = stion, endion
		
			dUSELF_CH(h) = dUSELF_CH(h) + CHARGE( TYPEion(m, Don), h ) * &
							CHARGE( TYPEion(m, Don), h )
				
		end do
					
		dUSELF_CH(h) = dUSELF_CH(h) * Alpha * CoulCombo / sqrt(Pi) / BoxSize(Don) 

		USELF_CH_new(h) = USELF_CH(h, Don) - dUSELF_CH(h)

		USELF_CH_grow(h) = USELF_CH_new(h)
	
	end do

	USELF_MOL_grow = 0.0
	UION_MOL_grow = 0.0

else

	UREAL_new = 0.0
	USURF_new = 0.0
	UFOURIER_new = 0.0
	USELF_CH_new = 0.0
	
end if

New = .False.

call Grow( NSTEPS(SpID), STEPSTART(:,SpID), STEPLENGTH(:,SpID), &
		   NTRIALS(:,SpID), lenlj, lenion, MaxBeads, METHOD(:,SpID), &
		   MaxInt, MaxReal, INTPARAM(:,:,SpID), REALPARAM(:,:,SpID), &
		   BEADTYPE(:,SpID), New, 1, Xlj_grow, Ylj_grow, Zlj_grow, &
		   TYPElj_grow, Xion_grow, Yion_grow, Zion_grow, &
		   TYPEion_grow, Nham, ULJBOX_grow, ULJLR_grow, &
		   ULJ_MOL_grow, UREAL_grow, USURF_grow, UFOURIER_grow, &
		   USELF_CH_grow, USELF_MOL_grow, UION_MOL_grow, Uintra_grow, &
		   Niongrs, CHARGE, Alpha, Kmax, Nkvec, KX, KY, KZ, CONST, &
		   SUMQX_grow, SUMQY_grow, SUMQZ_grow, EXPX_grow, EXPY_grow, &
		   EXPZ_grow, SUMQEXPV_grow, BigW_old, NTemp, BETA, LNWSTATE, &
		   Nlj(Don)-lenlj, Xlj_new, Ylj_new, Zlj_new, TYPElj_new, &
		   Nion(Don)-lenion, Xion_new, Yion_new, Zion_new, TYPEion_new, &
		   BoxSize(Don), Nljgrs, EPS, SIG, CP, ALP, RMAX, XLRCORR, ELRCORR, &
		   BONDSAPART(:,:,SpID), f14_lj, f14_ion, Seed )


Xlj_grow = 0.0
Ylj_grow = 0.0
Zlj_grow = 0.0

if( lenion > 0 ) then

	Xion_grow = 0.0
	Yion_grow = 0.0
	Zion_grow = 0.0

end if

ULJBOX_grow(:) = ULJBOX(:, Rec)
ULJLR_grow(:) = ULJLR(:, Rec)
ULJ_MOL_grow = 0.0
UREAL_grow(:) = UREAL(:, Rec)
USURF_grow(:) = USURF(:, Rec)
UFOURIER_grow(:) = UFOURIER(:, Rec)
USELF_CH_grow(:) = USELF_CH(:, Rec)
USELF_MOL_grow = 0.0
UION_MOL_grow = 0.0
Uintra_grow = 0.0

SUMQX_grow(:) = SUMQX(:, Rec)
SUMQY_grow(:) = SUMQY(:, Rec)
SUMQZ_grow(:) = SUMQZ(:, Rec)

SUMQEXPV_grow(:,:) = SUMQEXPV(:,:, Rec)

New = .True.

call Grow( NSTEPS(SpID), STEPSTART(:,SpID), STEPLENGTH(:,SpID), &
		   NTRIALS(:,SpID), lenlj, lenion, MaxBeads, METHOD(:,SpID), &
		   MaxInt, MaxReal, INTPARAM(:,:,SpID), REALPARAM(:,:,SpID), &
		   BEADTYPE(:,SpID), New, 1, Xlj_grow, Ylj_grow, Zlj_grow, &
		   TYPElj_grow, Xion_grow, Yion_grow, Zion_grow, &
		   TYPEion_grow, Nham, ULJBOX_grow, ULJLR_grow, &
		   ULJ_MOL_grow, UREAL_grow, USURF_grow, UFOURIER_grow, &
		   USELF_CH_grow, USELF_MOL_grow, UION_MOL_grow, Uintra_grow, &
		   Niongrs, CHARGE, Alpha, Kmax, Nkvec, KX, KY, KZ, CONST, &
		   SUMQX_grow, SUMQY_grow, SUMQZ_grow, EXPX_grow, EXPY_grow, &
		   EXPZ_grow, SUMQEXPV_grow, BigW_new, NTemp, BETA, LNWSTATE, &
		   Nlj(Rec), Xlj(:,Rec), Ylj(:,Rec), Zlj(:,Rec), TYPElj(:,Rec), &
		   Nion(Rec), Xion(:,Rec), Yion(:,Rec), Zion(:,Rec), TYPEion(:,Rec), &
		   BoxSize(Rec), Nljgrs, EPS, SIG, CP, ALP, RMAX, XLRCORR, ELRCORR, &
		   BONDSAPART(:,:,SpID), f14_lj, f14_ion, Seed )

PiRatio = ( BigW_new / BigW_old ) * ( real( Nmol(SpID,Don) ) / real( Nmol(SpID,Rec) + 1 ) ) * &
		  ( BoxSize(Rec) / BoxSize(Don) ) ** 3.0

if( ran2(Seed) < PiRatio ) then

	Success = .True.
	
	Xlj( Nlj(Rec)+1:Nlj(Rec)+lenlj, Rec ) = Xlj_grow( 1:lenlj )
	Ylj( Nlj(Rec)+1:Nlj(Rec)+lenlj, Rec ) = Ylj_grow( 1:lenlj )
	Zlj( Nlj(Rec)+1:Nlj(Rec)+lenlj, Rec ) = Zlj_grow( 1:lenlj )

	TYPElj( Nlj(Rec)+1:Nlj(Rec)+lenlj, Rec ) = TYPElj( stlj:endlj,Don )

	ULJBOX(:,Rec) = ULJBOX_grow(:)
	ULJLR(:,Rec) = ULJLR_grow(:)

	Xlj(:, Don) = Xlj_new(:)
	Ylj(:, Don) = Ylj_new(:)
	Zlj(:, Don) = Zlj_new(:)

	TYPElj(:,Don) = TYPElj_new(:)

	ULJBOX(:,Don) = ULJBOX_new(:)
	ULJLR(:,Don) = ULJLR_new(:)

	
	if( lenion > 0 ) then

		Xion( Nion(Rec)+1:Nion(Rec)+lenion, Rec ) = Xion_grow( 1:lenion )
		Yion( Nion(Rec)+1:Nion(Rec)+lenion, Rec ) = Yion_grow( 1:lenion )
		Zion( Nion(Rec)+1:Nion(Rec)+lenion, Rec ) = Zion_grow( 1:lenion )

		TYPEion( Nion(Rec)+1:Nion(Rec)+lenion, Rec ) = TYPEion( stion:endion,Don )

		UREAL(:,Rec) = UREAL_grow(:)
		USURF(:,Rec) = USURF_grow(:)
		UFOURIER(:,Rec) = UFOURIER_grow(:)
		USELF_CH(:,Rec) = USELF_CH_grow(:)

		SUMQX(:,Rec) = SUMQX_grow(:)
		SUMQY(:,Rec) = SUMQY_grow(:)
		SUMQZ(:,Rec) = SUMQZ_grow(:)

		SUMQEXPV(:,:,Rec) = SUMQEXPV_grow(:,:)

		Xion(:, Don) = Xion_new(:)
		Yion(:, Don) = Yion_new(:)
		Zion(:, Don) = Zion_new(:)

		TYPEion(:,Don) = TYPEion_new(:)

		UREAL(:,Don) = UREAL_new(:)
		USURF(:,Don) = USURF_new(:)
		UFOURIER(:,Don) = UFOURIER_new(:)
		USELF_CH(:,Don) = USELF_CH_new(:)

		SUMQX(:,Don) = SUMQX_new(:)
		SUMQY(:,Don) = SUMQY_new(:)
		SUMQZ(:,Don) = SUMQZ_new(:)

		SUMQEXPV(:,:,Don) = SUMQEXPV_new(:,:)

		if( stion == 1 ) then

			if( Nmol(0,Don) == 1 ) then

				EXPX(:,:,Don) = ( 0.0, 0.0 )
				EXPY(:,:,Don) = ( 0.0, 0.0 )
				EXPZ(:,:,Don) = ( 0.0, 0.0 )

			else

				EXPX( :, 1:Nion(Don)-lenion, Don ) = EXPX( :, endion+1:Nion(Don), Don )
				EXPY( :, 1:Nion(Don)-lenion, Don ) = EXPY( :, endion+1:Nion(Don), Don )
				EXPZ( :, 1:Nion(Don)-lenion, Don ) = EXPZ( :, endion+1:Nion(Don), Don )

			end if

		else if( stion + lenion - 1 == Nion(Don) ) then

			EXPX( :, stion:Nion(Don), Don ) = ( 0.0, 0.0 )
			EXPY( :, stion:Nion(Don), Don ) = ( 0.0, 0.0 )
			EXPZ( :, stion:Nion(Don), Don ) = ( 0.0, 0.0 )

		else

			EXPX( :, stion:Nion(Don)-lenion, Don ) = EXPX( :, endion+1:Nion(Don), Don )
			EXPY( :, stion:Nion(Don)-lenion, Don ) = EXPY( :, endion+1:Nion(Don), Don )
			EXPZ( :, stion:Nion(Don)-lenion, Don ) = EXPZ( :, endion+1:Nion(Don), Don )

		end if

		EXPX( :, Nion(Rec)+1:Nion(Rec)+lenion, Rec ) = EXPX_grow( :, 1:lenion )
		EXPY( :, Nion(Rec)+1:Nion(Rec)+lenion, Rec ) = EXPY_grow( :, 1:lenion )
		EXPZ( :, Nion(Rec)+1:Nion(Rec)+lenion, Rec ) = EXPZ_grow( :, 1:lenion )

	end if

	if( Mol == 1 ) then

		if( Nmol(0,Don) == 1 ) then

			SPECIES(:,Don) = 0

			LENGTHlj(:,Don) = 0
			LENGTHion(:,Don) = 0

			USELF_MOL(:,:,Don) = 0.0
			UION_MOL(:,:,Don) = 0.0

			ULJ_MOL(:,:,Don) = 0.0 

			UINTRA(:,Don) = 0.0

			STARTlj(:,Don) = 0
			STARTion(:,Don)  = 0

		else

			SPECIES( 1:Nmol(0,Don)-1, Don ) = SPECIES( 2:Nmol(0,Don), Don )

			LENGTHlj( 1:Nmol(0,Don)-1, Don ) = LENGTHlj( 2:Nmol(0,Don), Don )
			LENGTHion( 1:Nmol(0,Don)-1, Don ) = LENGTHion( 2:Nmol(0,Don), Don )

			USELF_MOL( 1:Nmol(0,Don)-1, :, Don ) = USELF_MOL( 2:Nmol(0,Don), :, Don )

			UION_MOL( 1:Nmol(0,Don)-1, :, Don ) = UION_MOL( 2:Nmol(0,Don), :, Don )

			ULJ_MOL( 1:Nmol(0,Don)-1, :, Don ) = ULJ_MOL( 2:Nmol(0,Don), :, Don )

			UINTRA( 1:Nmol(0,Don)-1, Don ) = UINTRA( 2:Nmol(0,Don), Don )
			
			STARTlj(1, Don) = 1
			STARTion(1, Don) = 1

			do k = 2, Nmol(0, Don) - 1

				STARTlj(k, Don) = STARTlj(k-1, Don)	+ LENGTHlj(k-1, Don)
				STARTion(k, Don) = STARTion(k-1, Don)	+ LENGTHion(k-1, Don)

			end do

		end if

	else if( Mol == Nmol(0,Don) ) then

		SPECIES( Mol, Don ) = 0

		LENGTHlj( Mol, Don ) = 0
		LENGTHion( Mol, Don ) = 0

		USELF_MOL( Mol, :, Don ) = 0.0

		UION_MOL( Mol, :, Don ) = 0.0

		ULJ_MOL( Mol, :, Don ) = 0.0

		UINTRA( Mol, Don ) = 0.0

		STARTlj( Mol, Don ) = 0
		STARTion( Mol, Don ) = 0

	else

		SPECIES( Mol:Nmol(0,Don)-1, Don ) = SPECIES( Mol+1:Nmol(0,Don), Don )

		LENGTHlj( Mol:Nmol(0,Don)-1, Don ) = LENGTHlj( Mol+1:Nmol(0,Don), Don )
		LENGTHion( Mol:Nmol(0,Don)-1, Don ) = LENGTHion( Mol+1:Nmol(0,Don), Don )

		USELF_MOL( Mol:Nmol(0,Don)-1, :, Don ) = USELF_MOL( Mol+1:Nmol(0,Don), :, Don )

		UION_MOL( Mol:Nmol(0,Don)-1, :, Don ) = UION_MOL( Mol+1:Nmol(0,Don), :, Don )

		ULJ_MOL( Mol:Nmol(0,Don)-1, :, Don ) = ULJ_MOL( Mol+1:Nmol(0,Don), :, Don )

		UINTRA( Mol:Nmol(0,Don)-1, Don ) = UINTRA( Mol+1:Nmol(0,Don), Don )

		do k = Mol, Nmol(0,Don) - 1

			STARTlj(k, Don) = STARTlj(k-1, Don)	+ LENGTHlj(k-1, Don)
			STARTion(k, Don) = STARTion(k-1, Don)	+ LENGTHion(k-1, Don)

		end do

	end if

	SPECIES( Nmol(0,Rec)+1, Rec ) = SpID

	LENGTHlj( Nmol(0,Rec)+1, Rec ) = lenlj
	LENGTHion( Nmol(0,Rec)+1, Rec ) = lenion

	if( Nmol(0,Rec) == 0 ) then

		STARTlj(1,Rec) = 1
		STARTion(1,Rec) = 1

	else


		STARTlj( Nmol(0,Rec)+1, Rec ) = STARTlj( Nmol(0,Rec), Rec ) + &
										LENGTHlj( Nmol(0,Rec), Rec )
		STARTion( Nmol(0,Rec)+1, Rec ) = STARTion( Nmol(0,Rec), Rec ) + &
										 LENGTHion( Nmol(0,Rec), Rec )

	end if

	USELF_MOL( Nmol(0,Rec)+1,:, Rec ) = USELF_MOL_grow(:)

	UION_MOL( Nmol(0,Rec)+1,:, Rec ) = UION_MOL_grow(:)

	ULJ_MOL( Nmol(0,Rec) + 1,:, Rec ) = ULJ_MOL_grow(:)

	UINTRA( Nmol(0,Rec) + 1, Rec ) = Uintra_grow

	
	Nmol(0, Don) = Nmol(0, Don) - 1
	Nmol(SpID, Don) = Nmol(SpID, Don) - 1
	
	Nmol(0, Rec) = Nmol(0, Rec) + 1
	Nmol(SpID, Rec) = Nmol(SpID, Rec) + 1
	
	Nlj(Don) = Nlj(Don) - lenlj
	Nlj(Rec) = Nlj(Rec) + lenlj

	Nion(Don) = Nion(Don) - lenion
	Nion(Rec) = Nion(Rec) + lenion

end if

deallocate( Xlj_grow )
deallocate( Ylj_grow )
deallocate( Zlj_grow )

deallocate( TYPElj_grow )

if( lenion > 0 ) then

	deallocate( Xion_grow )
	deallocate( Yion_grow )
	deallocate( Zion_grow )

	deallocate( TYPEion_grow )

	deallocate( EXPX_grow )
	deallocate( EXPY_grow )
	deallocate( EXPZ_grow )

end if

return

end subroutine Trans




