
Subroutine Remove( Nlj, Nion, MaxMol, MaxSp, MaxEE, Nmol, &
				   Xlj, Ylj, Zlj, TYPElj, DAMPlj2, DAMPlj3, &
				   Xion, Yion, Zion, TYPEion, DAMPion, &
				   MaxSteps, STEPSTART, STEPLENGTH, NTRIALS, &
				   MaxBeads, METHOD, MaxInt, MaxReal, INTPARAM, &
				   REALPARAM, BEADTYPE, BONDSAPART, &
				   ERSTEPS, ERSTART, EREND, EESTEPS, EEW, &
				   BoxSize, SPECIES, LENGTHlj, LENGTHion, &
				   STARTlj, STARTion, &
				   Nham, BETA, ZETA, LNW, LNPSI, LnPi, 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, &
				   Success, tag_val, tag_mol, rmol, &
				   Nres, Nresmol, reslen, Xresmols, &
				   Yresmols, Zresmols, Uint_resm, &
				   Ulj_resm, Uion_resm, &
				   f14_lj, f14_ion, ER_HIST, Seed )

implicit none

! This subroutine regrows a flexible molecule from StartGrowthStep to the 
! last step and then accepts or rejects the move.

! Nlj is the number of LJ beads in the simulation boxes.
! Nion is the number of ionic beads in the simulation boxes.

integer, intent(inout)									:: Nlj
integer, intent(inout)									:: Nion

! MaxMol is the maximum number of molecules per box.
! MaxSp is the maximum number of Species in the simulation.

integer, intent(in)										:: MaxMol
integer, intent(in)										:: MaxSp
integer, intent(in)										:: MaxEE

! 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), 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(Nlj), intent(inout)						:: Xlj, Ylj, Zlj
real, dimension(Nion), 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(Nlj), intent(inout)					:: TYPElj
integer, dimension(Nion), intent(inout)					:: TYPEion

real, dimension(Nlj), intent(inout)						:: DAMPlj2, DAMPlj3
real, dimension(Nion), intent(inout)					:: DAMPion

! MaxSteps is the maximum number of CB steps.

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

integer, dimension(MaxSp), intent(in)					:: ERSTEPS
integer, dimension(MaxSteps,MaxSp), intent(in)			:: ERSTART
integer, dimension(MaxSteps,MaxSp), intent(in)			:: EREND

integer, dimension(MaxSp), intent(in)					:: EESTEPS
real, dimension(MaxEE, MaxSp), intent(in)				:: EEW

! BoxSize is the length of the simulation box.

real, 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(Nmol(0)), intent(inout)				:: SPECIES
integer, dimension(Nmol(0)), intent(inout)				:: LENGTHlj, LENGTHion
integer, dimension(Nmol(0)), intent(inout)				:: STARTlj, STARTion

! Nham is the number of hamiltonians being used.

integer, intent(in)										:: Nham

! beta contains the reciprocal temperature.

real, dimension(Nham), intent(in)						:: BETA

! zeta is a gcmc parameter = f(mu, T)
! zeta = q(T) * exp(beta * mu_B) from Frenkel + Smit

real, dimension(MaxSp, Nham), intent(in)				:: ZETA

! LNW contains the weight of each hamiltonian.

real, dimension(Nham), intent(inout)					:: LNW

! LNPSI contains the log(psi) of each hamiltonian.

real, dimension(Nham), intent(inout)					:: LNPSI

! LnPi contains the log(pi).

real, intent(inout)										:: LnPi

! 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 is a rank 3 array containing the C_ij parameters for each hamiltonian.
! ALP is a rank 3 array containing the alpha_ij parameters for each hamiltonian.
! RMAX is a rank 3 array containing 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, Nion), intent(inout)			:: EXPX
complex, dimension(-Kmax:Kmax, Nion), 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), intent(inout)			:: SUMQEXPV

! SUMQX is the summation of qi * xi for all ions in the box.

real, dimension(Nham), 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.
! UION_MOL is the non bonded intramolecular ionic energy of a molecule.
! UINTRA contains the bending and torsional energy of each molecule.

real, dimension(Nham), intent(inout)					:: ULJBOX, ULJLR
real, dimension(Nham), intent(inout)					:: UREAL, UFOURIER
real, dimension(Nham), intent(inout)					:: USURF, USELF_CH
real, dimension(MaxMol,Nham), intent(inout)				:: USELF_MOL
real, dimension(MaxMol,Nham), intent(inout)				:: ULJ_MOL
real, dimension(MaxMol,Nham), intent(inout)				:: UION_MOL
real, dimension(MaxMol), intent(inout)					:: UINTRA

! SpID indicates which type of species was used in the move.

integer, intent(out)									:: SpID

! Success is a logical indicating whether the move was successful or not.

logical, intent(out)									:: Success

integer													:: tag_val
integer													:: tag_mol

integer													:: rmol

integer, intent(in)										:: Nres, Nresmol

integer, dimension(Nres), intent(in)					:: reslen

real, dimension(Nresmol, MaxBeads, Nres), intent(in)	:: Xresmols, Yresmols, Zresmols
real, dimension(Nresmol, Nres), intent(in)				:: Uint_resm
real, dimension(Nresmol, Nham, Nres), intent(in)		:: Ulj_resm, Uion_resm

! f14_lj and f14_ion are damping factors for the 1-4 intramolecular interactions

real, intent(in)										:: f14_lj
real, intent(in)										:: f14_ion

integer, dimension(MaxSteps+1,MaxSp,2), intent(inout)	:: ER_HIST

! Seed is the current random number generator seed value.

integer, intent(inout)									:: Seed

real, external											:: ran2

! Local Variables

integer										:: h, i, j, k
integer										:: Count
integer										:: lenlj, lenion
integer										:: stlj, stion
integer										:: endlj, endion
integer, dimension(Nlj)						:: TYPElj_new
integer, dimension(Nion)					:: TYPEion_new
integer, dimension(Nljgrs)					:: NGROUPS

integer, allocatable, dimension(:) 			:: TYPElj_grow
integer, allocatable, dimension(:) 			:: TYPEion_grow

logical										:: New

real										:: LnPi_tmp, LnPi_old, Largest
real										:: Uintra_grow
real										:: CoulCombo
real, dimension(Nlj)						:: Xlj_new, Ylj_new, Zlj_new
real, dimension(Nion)						:: Xion_new, Yion_new, Zion_new
real, dimension(Nlj)						:: DAMPlj2_new, DAMPlj3_new
real, dimension(Nion)						:: DAMPion_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)						:: dUFOURIER
real, dimension(Nham)						:: dUSURF
real, dimension(Nham)						:: SUMQX_new, SUMQY_new, SUMQZ_new
real, dimension(Nham)						:: SUMQX_grow, SUMQY_grow, SUMQZ_grow
real, dimension(Nham)						:: TMP, dU
real, dimension(Nham)						:: BIGW_old
real, dimension(Nham)						:: ZETA_tmp
real, dimension(Nljgrs, Nljgrs, Nham)		:: EPS_CP

real, allocatable, dimension(:) 			:: Xlj_grow, Ylj_grow, Zlj_grow
real, allocatable, dimension(:) 			:: Xion_grow, Yion_grow, Zion_grow
real, allocatable, dimension(:) 			:: DAMPlj2_grow, DAMPlj3_grow
real, allocatable, dimension(:) 			:: DAMPion_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.

if( tag_mol == 0 ) then

	if( Nmol(SpID) == 0 ) then
	
		tag_val = 0
		return

	end if

	tag_mol = int( Nmol(SpID) * ran2(Seed) ) + 1

	i = 0
	Count = 0

	do while ( Count < tag_mol )
		
		i = i + 1
		
		if( SPECIES(i) == SpID ) Count = Count + 1

	end do

	tag_mol = i

else

	SpID = SPECIES(tag_mol)

end if

rmol = tag_mol

lenlj = LENGTHlj(tag_mol)
stlj = STARTlj(tag_mol)
endlj = stlj + lenlj - 1

lenion = LENGTHion(tag_mol)
stion = STARTion(tag_mol)
endion = stion + lenion - 1

if( stlj == 1 ) then

	if( Nmol(0) == 1 ) then

		Xlj_new = 0.0
		Ylj_new = 0.0
		Zlj_new = 0.0

		TYPElj_new = 0

		DAMPlj2_new = 0.0
		DAMPlj3_new = 0.0
	
	else

		Xlj_new( 1:Nlj-lenlj ) = Xlj( endlj+1:Nlj )
		Ylj_new( 1:Nlj-lenlj ) = Ylj( endlj+1:Nlj )
		Zlj_new( 1:Nlj-lenlj ) = Zlj( endlj+1:Nlj )

		TYPElj_new( 1:Nlj-lenlj ) = TYPElj( endlj+1:Nlj )

		DAMPlj2_new( 1:Nlj-lenlj ) = DAMPlj2( endlj+1:Nlj )
		DAMPlj3_new( 1:Nlj-lenlj ) = DAMPlj3( endlj+1:Nlj )

	end if

else if( stlj + lenlj - 1 == Nlj ) then

	Xlj_new( 1:stlj-1 ) = Xlj( 1:stlj-1 )
	Ylj_new( 1:stlj-1 ) = Ylj( 1:stlj-1 )
	Zlj_new( 1:stlj-1 ) = Zlj( 1:stlj-1 )

	TYPElj_new( 1:stlj-1 ) = TYPElj( 1:stlj-1 )

	DAMPlj2_new( 1:stlj-1 ) = DAMPlj2( 1:stlj-1 )
	DAMPlj3_new( 1:stlj-1 ) = DAMPlj3( 1:stlj-1 )

else

	Xlj_new( 1:stlj-1 ) = Xlj( 1:stlj-1 )
	Xlj_new( stlj:Nlj-lenlj ) = Xlj( endlj+1:Nlj )
	Ylj_new( 1:stlj-1 ) = Ylj( 1:stlj-1 )
	Ylj_new( stlj:Nlj-lenlj ) = Ylj( endlj+1:Nlj )
	Zlj_new( 1:stlj-1 ) = Zlj( 1:stlj-1 )
	Zlj_new( stlj:Nlj-lenlj ) = Zlj( endlj+1:Nlj )

	TYPElj_new( 1:stlj-1 ) = TYPElj( 1:stlj-1 )
	TYPElj_new( stlj:Nlj-lenlj ) = TYPElj( endlj+1:Nlj )

	DAMPlj2_new( 1:stlj-1 ) = DAMPlj2( 1:stlj-1 )
	DAMPlj2_new( stlj:Nlj-lenlj ) = DAMPlj2( endlj+1:Nlj )
	DAMPlj3_new( 1:stlj-1 ) = DAMPlj3( 1:stlj-1 )
	DAMPlj3_new( stlj:Nlj-lenlj ) = DAMPlj3( endlj+1:Nlj )

end if

if( lenion > 0 ) then

	if( stion == 1 ) then

		if( Nmol(0) == 1 ) then

			Xion_new = 0.0
			Yion_new = 0.0
			Zion_new = 0.0

			TYPEion_new = 0.0

			DAMPion_new = 0.0

		else

			Xion_new( 1:Nion-lenion ) = Xion( endion+1:Nion )
			Yion_new( 1:Nion-lenion ) = Yion( endion+1:Nion )
			Zion_new( 1:Nion-lenion ) = Zion( endion+1:Nion )

			TYPEion_new( 1:Nion-lenion ) = TYPEion( endion+1:Nion )

			DAMPion_new( 1:Nion-lenion ) = DAMPion( endion+1:Nion )

		end if

	else if( stion + lenion - 1 == Nion ) then

		Xion_new( 1:stion-1 ) = Xion( 1:stion-1 )
		Yion_new( 1:stion-1 ) = Yion( 1:stion-1 )
		Zion_new( 1:stion-1 ) = Zion( 1:stion-1 )

		TYPEion_new( 1:stion-1 ) = TYPEion( 1:stion-1 )

		DAMPion_new( 1:stion-1 ) = DAMPion( 1:stion-1 )

	else

		Xion_new( 1:stion-1 ) = Xion( 1:stion-1 )
		Xion_new( stion:Nion-lenion ) = Xion( endion+1:Nion )
		Yion_new( 1:stion-1 ) = Yion( 1:stion-1 )
		Yion_new( stion:Nion-lenion ) = Yion( endion+1:Nion )
		Zion_new( 1:stion-1 ) = Zion( 1:stion-1 )
		Zion_new( stion:Nion-lenion ) = Zion( endion+1:Nion )

		TYPEion_new( 1:stion-1 ) = TYPEion( 1:stion-1 )
		TYPEion_new( stion:Nion-lenion ) = TYPEion( endion+1:Nion )

		DAMPion_new( 1:stion-1 ) = DAMPion( 1:stion-1 )
		DAMPion_new( stion:Nion-lenion ) = DAMPion( endion+1:Nion )

	end if

end if

allocate( Xlj_grow(lenlj) )
allocate( Ylj_grow(lenlj) )
allocate( Zlj_grow(lenlj) )

allocate( TYPElj_grow(lenlj) )

allocate( DAMPlj2_grow(lenlj) )
allocate( DAMPlj3_grow(lenlj) )

Xlj_grow( 1:lenlj ) = Xlj( stlj:endlj )
Ylj_grow( 1:lenlj ) = Ylj( stlj:endlj )
Zlj_grow( 1:lenlj ) = Zlj( stlj:endlj )

TYPElj_grow( 1:lenlj ) = TYPElj( stlj:endlj )

DAMPlj2_grow( 1:lenlj ) = DAMPlj2( stlj:endlj )
DAMPlj3_grow( 1:lenlj ) = DAMPlj3( stlj:endlj )

ULJBOX_grow = 0.0

NGROUPS = 0
	
do k = 1, Nlj-lenlj

	NGROUPS( TYPElj_new(k) ) = NGROUPS( TYPElj_new(k) ) + 1

end do

do h = 1, Nham

	if( CP(1,1,h) > 0.0 ) then

		EPS_CP(:,:,h) = EPS(:,:,h) * CP(:,:,h) * ALP(:,:,h) / &
						( ALP(:,:,h) - 6.0 ) / 4.0

	else

		EPS_CP(:,:,h) = EPS(:,:,h) * ALP(:,:,h) / ( ALP(:,:,h) - 6.0 ) * &
						( ALP(:,:,h) / 6.0 ) ** ( 6.0 / ( ALP(:,:,h) - 6.0 ) ) / 4.0

	end if

end do

call lrcorr( Nljgrs, Nham, BoxSize, NGROUPS, EPS_CP, SIG, XLRCORR, &
			 ELRCORR, ULJLR_new )

ULJLR_grow = ULJLR_new

ULJ_MOL_grow = 0.0

UION_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) )

	allocate( DAMPion_grow(lenion) )

	Xion_grow( 1:lenion ) = Xion( stion:endion )
	Yion_grow( 1:lenion ) = Yion( stion:endion )
	Zion_grow( 1:lenion ) = Zion( stion:endion )

	TYPEion_grow( 1:lenion ) = TYPEion( stion:endion )

	DAMPion_grow( 1:lenion ) = DAMPion( stion:endion )

	UREAL_grow = 0.0

	Xion_grow = -Xion_grow
	Yion_grow = -Yion_grow
	Zion_grow = -Zion_grow

	call Surf_Move( lenion, Xion_grow, Yion_grow, Zion_grow, &
					TYPEion_grow, DAMPion_grow, &
					Nham, Niongrs, CHARGE, &
					BoxSize, SUMQX, SUMQY, SUMQZ, &
					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 + 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, DAMPion_grow, &
					   Nham, Niongrs, CHARGE, BoxSize, &
					   Kmax, Nkvec, KX, KY, KZ, &
					   CONST, CZERO1, CZERO2, CZERO2, &
					   EXPX_grow, EXPY_grow, EXPZ_grow, &
					   SUMQEXPV, SUMQEXPV_new, dUFOURIER )

	CHARGE = -CHARGE

	SUMQEXPV_grow = SUMQEXPV_new

	UFOURIER_new = UFOURIER + dUFOURIER * CoulCombo

	UFOURIER_grow = UFOURIER_new

	deallocate( CZERO1 )
	deallocate( CZERO2 )

	USELF_CH_grow = 0.0
	USELF_MOL_grow = 0.0

else

	UREAL_new = 0.0
	UREAL_grow = 0.0
	USURF_new = 0.0
	USURF_grow = 0.0
	UFOURIER_new = 0.0
	UFOURIER_grow = 0.0
	USELF_CH_new = 0.0
	USELF_CH_grow = 0.0
	USELF_MOL_grow = 0.0
	
end if

TMP = LNPSI

LnPi_old = LnPi

New = .false.

ZETA_tmp(:) = ZETA(SpID,:)

do i = 1, ERSTEPS(SpID)

	call Grow( EREND(i,SpID), STEPSTART(:,SpID), STEPLENGTH(:,SpID), &
			   NTRIALS(:,SpID), lenlj, lenion, MaxBeads, METHOD(:,SpID), &
			   MaxInt, MaxReal, INTPARAM(:,:,SpID), REALPARAM(:,:,SpID), &
			   BEADTYPE(:,SpID), New, ERSTART(i,SpID), &
			   Xlj_grow, Ylj_grow, Zlj_grow, &
			   TYPElj_grow, DAMPlj2_grow, DAMPlj3_grow, &
			   Xion_grow, Yion_grow, Zion_grow, &
			   TYPEion_grow, DAMPion_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, &
			   BETA, BIGW_old, LNW, &
			   Nlj-lenlj, Xlj_new, Ylj_new, Zlj_new, &
			   TYPElj_new, DAMPlj2_new, DAMPlj3_new, &
			   Nion-lenion, Xion_new, Yion_new, Zion_new, &
			   TYPEion_new, DAMPion_new, &
			   BoxSize, Nljgrs, EPS, SIG, CP, ALP, RMAX, &
			   XLRCORR, ELRCORR, &
			   Nres, Nresmol, reslen, Xresmols, &
			   Yresmols, Zresmols, Uint_resm, &
			   Ulj_resm, Uion_resm, &
			   BONDSAPART(:,:,SpID), f14_lj, f14_ion, Seed )


	do j = ERSTART(i,SpID), EREND(i,SpID)
	
		BigW_old = BigW_old / real( NTRIALS(j,SpID) )

	end do

	TMP = TMP - log( BIGW_old ) - &
			log( ZETA_tmp * ( BoxSize**3.0 ) / real( Nmol(SpID) ) ) / real( ERSTEPS(SpID) )

	Largest = maxval( LNW + TMP )

	LnPi_tmp = log( sum( exp( LNW + TMP - Largest ) ) ) + Largest

	LnPi_tmp = LnPi_tmp - ( EEW(1,SpID) - EEW(EESTEPS(SpID),SpID) ) / real( ERSTEPS(SpID) )

	if( ran2(Seed) < 1.0 - exp(LnPi_tmp - LnPi_old) ) exit

	LnPi_old = LnPi_tmp

	if( i == ERSTEPS(SpID) ) then

		Success = .true.

		ULJBOX_new = ULJBOX - ULJBOX_grow

		UREAL_new = UREAL - UREAL_grow
		USELF_CH_new = USELF_CH - USELF_CH_grow

		dU(:) =	ULJBOX_new(:) + ULJLR_new(:) + 0.0 + &
				UREAL_new(:) + USURF_new(:) + UFOURIER_new(:) - &
				USELF_CH_new(:) - 0.0 + 0.0 - &
				( ULJBOX(:) + ULJLR(:) + ULJ_MOL(tag_mol,:) + &
				  UREAL(:) + USURF(:) + UFOURIER(:) - &
				  USELF_CH(:) - USELF_MOL(tag_mol,:) + UION_MOL(tag_mol,:) )

		dU = -dU * BETA

		LNPSI = LNPSI + dU - log( ZETA_tmp * ( BoxSize**3.0 ) / &
								  real( Nmol(SpID) ) )

		Largest = maxval( LNW + LNPSI )

		LnPi = log( sum( exp( LNW + LNPSI - Largest ) ) ) + Largest

		Xlj = Xlj_new
		Ylj = Ylj_new
		Zlj = Zlj_new

		TYPElj = TYPElj_new

		DAMPlj2 = DAMPlj2_new
		DAMPlj3 = DAMPlj3_new

		ULJBOX = ULJBOX_new
		ULJLR = ULJLR_new

		if( lenion > 0 ) then

			Xion = Xion_new
			Yion = Yion_new
			Zion = Zion_new

			TYPEion = TYPEion_new

			DAMPion = DAMPion_new

			UREAL = UREAL_new
			USURF = USURF_new
			UFOURIER = UFOURIER_new
			USELF_CH = USELF_CH_new

			SUMQX = SUMQX_new
			SUMQY = SUMQY_new
			SUMQZ = SUMQZ_new

			SUMQEXPV = SUMQEXPV_new

			if( stion == 1 ) then

				if( Nmol(0) == 1 ) then

					EXPX = ( 0.0, 0.0 )
					EXPY = ( 0.0, 0.0 )
					EXPZ = ( 0.0, 0.0 )

				else

					EXPX( :, 1:Nion-lenion ) = EXPX( :, endion+1:Nion )
					EXPY( :, 1:Nion-lenion ) = EXPY( :, endion+1:Nion )
					EXPZ( :, 1:Nion-lenion ) = EXPZ( :, endion+1:Nion )

				end if

			else if( stion + lenion - 1 == Nion ) then

				EXPX( :, stion:Nion ) = ( 0.0, 0.0 )
				EXPY( :, stion:Nion ) = ( 0.0, 0.0 )
				EXPZ( :, stion:Nion ) = ( 0.0, 0.0 )

			else

				EXPX( :, stion:Nion-lenion ) = EXPX( :, endion+1:Nion )
				EXPY( :, stion:Nion-lenion ) = EXPY( :, endion+1:Nion )
				EXPZ( :, stion:Nion-lenion ) = EXPZ( :, endion+1:Nion )

			end if

		end if


		if( tag_mol == 1 ) then

			if( Nmol(0) == 1 ) then

				SPECIES = 0

				LENGTHlj = 0
				LENGTHion = 0

				USELF_MOL = 0.0

				ULJ_MOL = 0.0 

				UION_MOL = 0.0 

				UINTRA = 0.0

				STARTlj = 0
				STARTion  = 0

			else

				SPECIES( 1:Nmol(0)-1 ) = SPECIES( 2:Nmol(0) )

				LENGTHlj( 1:Nmol(0)-1 ) = LENGTHlj( 2:Nmol(0) )
				LENGTHion( 1:Nmol(0)-1 ) = LENGTHion( 2:Nmol(0) )

				USELF_MOL( 1:Nmol(0)-1, : ) = USELF_MOL( 2:Nmol(0), : )

				ULJ_MOL( 1:Nmol(0)-1, : ) = ULJ_MOL( 2:Nmol(0), : )

				UION_MOL( 1:Nmol(0)-1, : ) = UION_MOL( 2:Nmol(0), : )

				UINTRA( 1:Nmol(0)-1 ) = UINTRA( 2:Nmol(0) )

				STARTlj(1) = 1
				STARTion(1) = 1

				do k = 2, Nmol(0) - 1

					STARTlj(k) = STARTlj(k-1)	+ LENGTHlj(k-1)
					STARTion(k) = STARTion(k-1)	+ LENGTHion(k-1)

				end do

			end if

		else if( tag_mol == Nmol(0) ) then

			SPECIES( tag_mol ) = 0

			LENGTHlj( tag_mol ) = 0
			LENGTHion( tag_mol ) = 0

			USELF_MOL( tag_mol, : ) = 0.0

			ULJ_MOL( tag_mol, : ) = 0.0

			UION_MOL( tag_mol, : ) = 0.0

			UINTRA( tag_mol ) = 0.0

			STARTlj( tag_mol ) = 0
			STARTion( tag_mol ) = 0

		else

			SPECIES( tag_mol:Nmol(0)-1 ) = SPECIES( tag_mol+1:Nmol(0) )

			LENGTHlj( tag_mol:Nmol(0)-1 ) = LENGTHlj( tag_mol+1:Nmol(0) )
			LENGTHion( tag_mol:Nmol(0)-1 ) = LENGTHion( tag_mol+1:Nmol(0) )

			USELF_MOL( tag_mol:Nmol(0)-1, : ) = USELF_MOL( tag_mol+1:Nmol(0), : )

			ULJ_MOL( tag_mol:Nmol(0)-1, : ) = ULJ_MOL( tag_mol+1:Nmol(0), : )

			UION_MOL( tag_mol:Nmol(0)-1, : ) = UION_MOL( tag_mol+1:Nmol(0), : )

			UINTRA( tag_mol:Nmol(0)-1 ) = UINTRA( tag_mol+1:Nmol(0) )

			do k = tag_mol, Nmol(0) - 1

				STARTlj(k) = STARTlj(k-1)	+ LENGTHlj(k-1)
				STARTion(k) = STARTion(k-1)	+ LENGTHion(k-1)

			end do

		end if

		Nmol(0) = Nmol(0) - 1
		Nmol(SpID) = Nmol(SpID) - 1

		Nlj = Nlj - lenlj

		Nion = Nion - lenion

		tag_val = 0
		tag_mol = 0

	end if

end do

if( .NOT. success .AND. EESTEPS(SpID) == 1 ) then

	tag_val = 0
	tag_mol = 0

end if

ER_HIST(i, SpID, 2) = ER_HIST(i, SpID, 2) + 1

deallocate( Xlj_grow )
deallocate( Ylj_grow )
deallocate( Zlj_grow )

deallocate( TYPElj_grow )

deallocate( DAMPlj2_grow )
deallocate( DAMPlj3_grow )

if( lenion > 0 ) then

	deallocate( Xion_grow )
	deallocate( Yion_grow )
	deallocate( Zion_grow )

	deallocate( TYPEion_grow )

	deallocate( DAMPion_grow )

	deallocate( EXPX_grow )
	deallocate( EXPY_grow )
	deallocate( EXPZ_grow )

end if

return

end subroutine Remove
