module TPS2d_active_def_enhanced
implicit none
	integer, parameter :: potential_id = 1				! See function U1d or U2d for the relative potential
!~ 	real*8, parameter :: dt	= 0.001d0					! time step
!~ 	real*8, parameter :: dt	= 1.d0
	real*8, parameter :: dt	= 0.1d0
	integer, parameter :: Nt = 500000					! number of time steps (Nt*dt = duration of the trajectories)
	integer, parameter :: Nsaved1 = 60					! number of saved trajectories
	integer, parameter :: Nsaved2 = 1000000000
	character(len=100) :: file_id
	character(len=150) :: file_name1
	character(len=150) :: file_name2
	character(len=150) :: file_name3
	character(len=150) :: file_name4
	character(len=150) :: file_name5
	character(len=150) :: file_name6

!~ 	real*8, parameter :: D = 0.001d0					! translational diffusion coefficient
!~ 	real*8, parameter :: D = 0.00001d0
	real*8, parameter :: D = 0.001d0
!~ 	real*8, parameter :: D = 0.1d0
!~ 	real*8, parameter :: Dr = 0.1642d0						! rotational diffusion coefficient
!~ 	real*8, parameter :: Dr = 0.0069d0
!~ 	real*8, parameter :: v = 2.7719d0						! active speed
!~ 	real*8, parameter :: Dr = 1.d0
!~ 	real*8, parameter :: Dr = 0.5d0
!~ 	real*8, parameter :: v = 2.d0
!~ 	real*8, parameter :: v = 0.005657d0
!~ 	real*8, parameter :: v = 0.0189d0
	real*8, parameter :: v = 0.01d0						! final
	real*8, parameter :: Dr = 0.05d0
!~ 	real*8, parameter :: v = 0.014124d0					! averages_beg_end
!~ 	real*8, parameter :: Dr = 0.306790d0
	

	real*8, parameter :: beta = 1.d0					! inverse temperature
	real*8, parameter :: zeta = 1.d0/(D*dt)				! friction / dt
	real*8, parameter :: sigma = dsqrt(2*D*dt)			! translational noise variance
	real*8, parameter :: sigmar = dsqrt(2*Dr*dt)		! rotational noise variance
	real*8, parameter :: x0 = 1.d0						! position of energy minima are in +- x0
	real*8, parameter :: y0 = 0.d0
	real*8, parameter :: kx = 6.5d0						! stiffness of the potential
	real*8, parameter :: ky = 20.d0
	real*8, parameter :: Ubasins = 2.d0					! definition of the basins
	real*8, parameter :: pi = 3.1415926535897932384626433832795028841971693993751d0
	real*8, parameter :: rr = 0.1d0						!radius reactant region
	real*8, parameter :: xtloc = -0.8d0					!x coordinate of left bound of target region
	real*8, parameter :: rc = 0.1d0						!radius of regions in which the committor is computed
	real*8, parameter :: xc1 = -0.5d0					!x position of the center of region 1 in which the committor is computed
	real*8, parameter :: yc1 = 0.d0						!y position of the center of region 1 in which the committor is computed
	real*8, parameter :: xc2 = 0.d0						!x position of the center of region 2 in which the committor is computed
	real*8, parameter :: yc2 = 0.5d0					!y position of the center of region 2 in which the committor is computed
	real*8, parameter :: xc3 = 0.5d0					!x position of the center of region 3 in which the committor is computed
	real*8, parameter :: yc3 = 0.d0						!y position of the center of region 3 in which the committor is computed
!~ 	real*8, parameter :: rr = 49.d0						!radius reactant region
!~ 	real*8, parameter :: xtloc = -392.d0					!x coordinate of left bound of target region
!~ 	real*8, parameter :: rc = 49.d0						!radius of regions in which the committor is computed
!~ 	real*8, parameter :: xc1 = -245.d0					!x position of the center of region 1 in which the committor is computed
!~ 	real*8, parameter :: yc1 = 0.d0						!y position of the center of region 1 in which the committor is computed
!~ 	real*8, parameter :: xc2 = 0.d0						!x position of the center of region 2 in which the committor is computed
!~ 	real*8, parameter :: yc2 = 245.d0					!y position of the center of region 2 in which the committor is computed
!~ 	real*8, parameter :: xc3 = 245.d0					!x position of the center of region 3 in which the committor is computed
!~ 	real*8, parameter :: yc3 = 0.d0						!y position of the center of region 3 in which the committor is computed
	
	integer, parameter :: Nintervals = 8				!number of angular intervals for the committor
	integer, parameter :: radius = 1					!radius of the petri dish
	real, parameter :: over = 0.01						!hard radius particle
	integer, parameter :: e_lj = 1						!depth of lennard-jones well
!~ 	integer, parameter :: Nintervals = 8				!number of angular intervals for the committor
!~ 	integer, parameter :: radius = 490.d0					!radius of the petri dish
!~ 	real, parameter :: over = 10.d0						!hard radius particle
!~ 	integer, parameter :: e_lj = 1						!depth of lennard-jones well
	
	real*8, parameter :: factor1 = beta**2*D**2*dt**2
	real*8, parameter :: factor2 = 2*beta*D*dt

	real*8, parameter :: grid_dx = 0.02d0				! Delta grid x
	real*8, parameter :: grid_dy = 0.02d0				! Delta grid y
	real*8, parameter :: grid_dtheta = 2*pi/8			! Delta grid theta
	real*8, parameter :: grid_x_left = -3.d0			! grid margin x left
	real*8, parameter :: grid_x_right = 3.d0			! grid margin x right
	real*8, parameter :: grid_y_left = -3.d0			! grid margin y left
	real*8, parameter :: grid_y_right = 3.d0			! grid margin y right
	integer, parameter :: n_grid_x_left = Nint(grid_x_left/grid_dx)			! number of grid points in x direction left
	integer, parameter :: n_grid_x_right = Nint(grid_x_right/grid_dx)		! number of grid points in x direction left
	integer, parameter :: n_grid_y_left = Nint(grid_y_left/grid_dy)			! number of grid points in y direction left
	integer, parameter :: n_grid_y_right = Nint(grid_y_right/grid_dy)		! number of grid points in y direction left
	integer, parameter :: n_grid_theta = Nint((2*pi)/grid_dtheta)			! number of grid points for the angle

	! this part is to find the distribution in the reactant basin
	real*8, parameter :: gridrho_dx = 0.01d0				! Delta gridrho x
	real*8, parameter :: gridrho_dy = 0.01d0				! Delta gridrho y
	real*8, parameter :: gridrho_dtheta = 2*pi/64			! Delta gridrho theta
	real*8, parameter :: gridrho_x_left = -1.3d0			! gridrho margin x left
	real*8, parameter :: gridrho_x_right = -0.6d0			! gridrho margin x right
	real*8, parameter :: gridrho_y_left = -0.5d0			! gridrho margin y left
	real*8, parameter :: gridrho_y_right = 0.5d0			! gridrho margin y right
	integer, parameter :: n_gridrho_x_left = Nint(gridrho_x_left/gridrho_dx)		! number of gridrho points in x direction left
	integer, parameter :: n_gridrho_x_right = Nint(gridrho_x_right/gridrho_dx)		! number of gridrho points in x direction left
	integer, parameter :: n_gridrho_y_left = Nint(gridrho_y_left/gridrho_dy)		! number of gridrho points in y direction left
	integer, parameter :: n_gridrho_y_right = Nint(gridrho_y_right/gridrho_dy)		! number of gridrho points in y direction left
	integer, parameter :: n_gridrho_theta = Nint((2*pi)/gridrho_dtheta)				! number of gridrho points for the angle


	!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	!!!!!!!!!!!!!!!!   GLOBAL QUANTITES !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!~ 	real*8 :: v						! active velocity
!~ 	real*8 :: Dr					! rotational diffusion coefficient
	real*8 :: r(2,0:Nt)
	real*8 :: csi(2,0:Nt)
	real*8 :: theta(0:Nt)
	real*8 :: rnew(2,0:Nt)
	real*8 :: thetanew(0:Nt)
	real*8 :: csinew(2,0:Nt)
	integer :: idum,iduv
	real*8 :: path(2,0:Nt)
!~ 	real*8 :: rsaved(0:Nsaved,2,0:Nt)
	integer :: traiet
	integer :: nshootingmoves,naccshootingmoves,nrejectedshootingmoves
	integer :: nshiftingmoves,naccshiftingmoves,nrejectedshiftingmoves
	integer :: ntooshort
	integer :: i1def,i2def
	real*8 :: committor(n_grid_x_left:n_grid_x_right,n_grid_y_left:n_grid_y_right)
	integer :: n_committor(n_grid_x_left:n_grid_x_right,n_grid_y_left:n_grid_y_right)
	real*8 :: reactiveprobdensity(n_grid_x_left:n_grid_x_right,n_grid_y_left:n_grid_y_right)
	real*8 :: reactivep(n_grid_x_left:n_grid_x_right,n_grid_y_left:n_grid_y_right)
	real*8 :: reactiveprobdensityX(n_grid_x_left:n_grid_x_right)
	real*8 :: reactiveprobdensityY(n_grid_y_left:n_grid_y_right)
	real*8 :: avgthetaX(2,n_grid_x_left:n_grid_x_right)
	real*8 :: avgthetaY(2,n_grid_y_left:n_grid_y_right)
	real*8 :: thetadistributionX(3,n_grid_theta)
	real*8 :: thetadistributionY(3,n_grid_theta)
	real*8 :: thetadistributionP(12,n_grid_theta)
	real*8 :: thetadistributionX2(3,n_grid_theta)
	real*8 :: thetadistributionY2(3,n_grid_theta)
	real*8 :: thetadistributionP2(12,n_grid_theta)
	real*8 :: trajectorytype(4)
	
	
	real*8 :: reactiveJ(n_grid_x_left:n_grid_x_right,n_grid_y_left:n_grid_y_right,2)
	integer :: n_reactiveJ(n_grid_x_left:n_grid_x_right,n_grid_y_left:n_grid_y_right)
	
	real*8 :: rho (n_gridrho_x_left:n_gridrho_x_right,n_gridrho_y_left:n_gridrho_y_right,n_gridrho_theta)
	real*8 :: nin (n_gridrho_x_left:n_gridrho_x_right,n_gridrho_y_left:n_gridrho_y_right)
	real*8 :: ntot (n_gridrho_x_left:n_gridrho_x_right,n_gridrho_y_left:n_gridrho_y_right)
	real*8 :: comm (1:Nintervals)
	real*8 :: succ (1:Nintervals)
	real*8 :: q1 (0:7)
	real*8 :: q2 (0:7)
	real*8 :: q3 (0:7)
	real*8 :: trajang1 (0:7)
	real*8 :: trajang2 (0:7)
	real*8 :: trajang3 (0:7)
	real*8 :: nr1 (0:3)
	real*8 :: nr2 (0:3)
	real*8 :: nr3 (0:3)
	real*8 :: nt1 (0:3)
	real*8 :: nt2 (0:3)
	real*8 :: nt3 (0:3)
	real*8 :: std1 (0:3)
	real*8 :: std2 (0:3)
	real*8 :: std3 (0:3)
	real*8 :: e1 (0:3)
	real*8 :: e2 (0:3)
	real*8 :: e3 (0:3)
	real*8 :: e_r1 (0:3)
	real*8 :: e_r2 (0:3)
	real*8 :: e_r3 (0:3)
	real*8 :: q1f (0:3)
	real*8 :: q2f (0:3)
	real*8 :: q3f (0:3)
	real*8 :: trajangf1 (0:3)
	real*8 :: trajangf2 (0:3)
	real*8 :: trajangf3 (0:3)
	real*8 :: succ1 (0:3)
	real*8 :: succ2 (0:3)
	real*8 :: succ3 (0:3)

	save

!~ 	real*8 :: r(2,0:Nt)
!~ 	real*8 :: csi(2,0:Nt)
!~ 	real*8 :: theta(0:Nt)
!~ 	real*8 :: rnew(2,0:Nt)
!~ 	real*8 :: thetanew(0:Nt)
!~ 	real*8 :: csinew(2,0:Nt)
!~ 	integer :: idum,iduv
!~ 	real*8 :: path(2,0:Nt)
!~ 	real*8 :: rsaved(0:Nsaved,2,0:Nt)
!~ 	integer :: traiet
!~ 	integer :: nshootingmoves,naccshootingmoves,nrejectedshootingmoves
!~ 	integer :: nshiftingmoves,naccshiftingmoves,nrejectedshiftingmoves
!~ 	integer :: ntooshort
!~ 	integer :: i1def,i2def
!~ 	real*8 :: committor(n_grid_x_left:n_grid_x_right,n_grid_y_left:n_grid_y_right)
!~ 	integer :: n_committor(n_grid_x_left:n_grid_x_right,n_grid_y_left:n_grid_y_right)
!~ 	real*8 :: reactiveprobdensity(n_grid_x_left:n_grid_x_right,n_grid_y_left:n_grid_y_right)
!~ 	real*8 :: reactivep(n_grid_x_left:n_grid_x_right,n_grid_y_left:n_grid_y_right)
!~ 	real*8 :: reactiveprobdensityX(n_grid_x_left:n_grid_x_right)
!~ 	real*8 :: reactiveprobdensityY(n_grid_y_left:n_grid_y_right)
!~ 	real*8 :: avgthetaX(2,n_grid_x_left:n_grid_x_right)
!~ 	real*8 :: avgthetaY(2,n_grid_y_left:n_grid_y_right)
!~ 	real*8 :: thetadistributionX(3,n_grid_theta)
!~ 	real*8 :: thetadistributionY(3,n_grid_theta)
!~ 	real*8 :: thetadistributionP(12,n_grid_theta)
!~ 	real*8 :: thetadistributionX2(3,n_grid_theta)
!~ 	real*8 :: thetadistributionY2(3,n_grid_theta)
!~ 	real*8 :: thetadistributionP2(12,n_grid_theta)
!~ 	real*8 :: trajectorytype(4)
	
	
!~ 	real*8 :: reactiveJ(n_grid_x_left:n_grid_x_right,n_grid_y_left:n_grid_y_right,2)
!~ 	integer :: n_reactiveJ(n_grid_x_left:n_grid_x_right,n_grid_y_left:n_grid_y_right)
	
!~ 	real*8 :: rho (n_gridrho_x_left:n_gridrho_x_right,n_gridrho_y_left:n_gridrho_y_right,n_gridrho_theta)
!~ 	real*8 :: nin (n_gridrho_x_left:n_gridrho_x_right,n_gridrho_y_left:n_gridrho_y_right)
!~ 	real*8 :: ntot (n_gridrho_x_left:n_gridrho_x_right,n_gridrho_y_left:n_gridrho_y_right)
!~ 	real*8 :: comm (1:Nintervals)
!~ 	real*8 :: succ (1:Nintervals)
!~ 	real*8 :: q1 (0:7)
!~ 	real*8 :: q2 (0:7)
!~ 	real*8 :: q3 (0:7)
!~ 	real*8 :: trajang1 (0:7)
!~ 	real*8 :: trajang2 (0:7)
!~ 	real*8 :: trajang3 (0:7)
!~ 	real*8 :: nr1 (0:3)
!~ 	real*8 :: nr2 (0:3)
!~ 	real*8 :: nr3 (0:3)
!~ 	real*8 :: nt1 (0:3)
!~ 	real*8 :: nt2 (0:3)
!~ 	real*8 :: nt3 (0:3)
!~ 	real*8 :: std1 (0:3)
!~ 	real*8 :: std2 (0:3)
!~ 	real*8 :: std3 (0:3)
!~ 	real*8 :: e1 (0:3)
!~ 	real*8 :: e2 (0:3)
!~ 	real*8 :: e3 (0:3)
!~ 	real*8 :: e_r1 (0:3)
!~ 	real*8 :: e_r2 (0:3)
!~ 	real*8 :: e_r3 (0:3)
!~ 	real*8 :: q1f (0:3)
!~ 	real*8 :: q2f (0:3)
!~ 	real*8 :: q3f (0:3)
!~ 	real*8 :: trajangf1 (0:3)
!~ 	real*8 :: trajangf2 (0:3)
!~ 	real*8 :: trajangf3 (0:3)
!~ 	real*8 :: succ1 (0:3)
!~ 	real*8 :: succ2 (0:3)
!~ 	real*8 :: succ3 (0:3)

!~ 	save



contains
	!!!!!!!!!!!!!!!!!!!!!!  2d functions  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	function U2d (x,y)
	implicit none
	real*8 x,y,U2d
	if (potential_id.eq.1) then
		U2d = 0.5*ky*y**2 + kx*(x*x-x0*x0)**2
	else if (potential_id.eq.2) then
		U2d = kx * ((x**2-1.d0)**2+(x-y)**2)
	endif
	end function U2d

	function fx2d (x,y)
	implicit none
	real*8 x,y,fx2d
	if (potential_id.eq.1) then
		fx2d = - 4 * kx * x * (x*x - x0*x0)
	else if (potential_id.eq.2) then
		fx2d = - kx * (4*x*(x*x-1)+2*(x-y))
	endif
	end function fx2d

	function fy2d (x,y)
	implicit none
	real*8 x,y,fy2d
	if (potential_id.eq.1) then
		fy2d = - ky * y
	else if (potential_id.eq.2) then
		fy2d = kx * 2 * (x-y)
	endif
	end function fy2d
	
	function fwx2d (x,y)
	implicit none
	real*8 x,y,fwx2d,dr,dist
	dr = dsqrt(x**2+y**2)
	dist = radius-dr
!~ 	print*, radius, over
	if (dist .gt. over) then
		fwx2d = 0.d0
	else
!~ 		fwx2d = 4*e_lj*(12*((over**12)/(dist**13))-6*((over**6)/(dist**7)))*(-x/dr)
!~ 		fwx2d = - 100.d0*x
!~ 		fwx2d = - 100.d0*x
		fwx2d = - 200.d0*x*(x**2+y**2)
!~ 		print*, dist, fwx2d, over
	endif
	end function fwx2d

	function fwy2d (x,y)
	implicit none
	real*8 x,y,fwy2d,dr,dist
	dr = dsqrt(x**2+y**2)
	dist = radius-dr
	if (dist .gt. over) then
		fwy2d = 0.d0
	else
!~ 		fwy2d = 4*e_lj*(12*((over**12)/(dist**13))-6*((over**6)/(dist**7)))*(-y/dr)
!~ 		fwy2d = - 100.d0*y
!~ 		fwy2d = - 100.d0*y
		fwy2d = - 200.d0*y*(x**2+y**2)
	endif
	end function fwy2d

	function rho2d (x,y)
	implicit none
	real*8 x,y,rho2d
	rho2d = dexp(-beta*U2d(x,y))
	return
	end function rho2d

	function hR2d (x,y)
	implicit none
	real*8 x,y
	integer hR2d
	if (U2d(x,y)<Ubasins .and. x<0.d0) then
		hR2d = 1
	else
		hR2d = 0
	endif
	end function hR2d

	function hP2d (x,y)
	implicit none
	real*8 x,y
	integer hP2d
	if (U2d(x,y)<Ubasins .and. x>0.d0) then
		hP2d = 1
	else
		hP2d = 0
	endif
	end function hP2d

	subroutine define_initial_trajectory2d ()
	implicit none
	real*8 dx,dy
	real*8 dtheta,ra
	integer i,i1,i2,j
	if (potential_id.eq.1) then
		dx = 2*x0/Nt
		do i=0,Nt
            theta(i) = 0.d0
			r(1,i) = -x0 + i*dx
			r(2,i) = 0.d0
		enddo

	else if (potential_id.eq.2) then
		dx = dsqrt(2*D*dt)
		dy = dsqrt(2*D*dt)
		r(1,0) = -1.d0
		r(2,0) = -1.d0
		theta(0) = pi/4
		do i=1,Nt
			if (hP2d(r(1,i-1),r(2,i-1)).ne.1) then
				r(1,i) = r(1,i-1)+dx
				r(2,i) = r(2,i-1)+dy
				theta(i) = pi/4
			else
				r(1,i) = r(1,i-1)
				r(2,i) = r(2,i-1)
				theta(i) = pi/4
			endif
		enddo
	endif
	traiet = 0
	nshootingmoves=0
	naccshootingmoves=0
	nshiftingmoves=0
	naccshiftingmoves=0
	nrejectedshootingmoves=0
	nrejectedshiftingmoves=0
	ntooshort = 0
	!rsaved(traiet,:,:) = r(:,:)
	committor(:,:)=0.d0
	n_committor(:,:)=0
	reactiveprobdensity(:,:)=0.d0
	reactiveprobdensityX(:)=0.d0
	reactiveprobdensityY(:)=0.d0
	avgthetaX(:,:)=0.d0
	avgthetaY(:,:)=0.d0
	thetadistributionX(:,:)=0.d0
	thetadistributionY(:,:)=0.d0
	thetadistributionP(:,:)=0.d0
	thetadistributionX2(:,:)=0.d0
	thetadistributionY2(:,:)=0.d0
	thetadistributionP2(:,:)=0.d0
	trajectorytype(:) = 0.d0
	
	reactiveJ(:,:,:)=0.d0
	n_reactiveJ(:,:)=0

	call compute_reactive_segment2d (i1,i2)
	!print*, i1,i2
	
	rnew(:,:)=r(:,:)
	thetanew(:)=theta(:)
	j=Nt/2 - (i1+i2)/2
	do i = i1-1,i2+1
		r(:,i+j) = rnew(:,i)
		theta(i+j) = thetanew(i)
	enddo
	i1def = i1+j
	i2def = i2+j
	do i = i1def-2,0,-1
		r(:,i)=r(:,i+1)
		theta(i)=theta(i+1)
	enddo
	
	do i = 0,Nt-1
		csi(1,:) = (r(1,i+1)-r(1,i) - v*dcos(theta(i))*dt - fx2d(r(1,i),r(2,i))/zeta)/sigma
		csi(2,:) = (r(2,i+1)-r(2,i) - v*dsin(theta(i))*dt - fy2d(r(1,i),r(2,i))/zeta)/sigma
	enddo
	csinew(:,:) = csi(:,:)

	!print*, i1def,i2def
	end subroutine define_initial_trajectory2d

	subroutine compute_reactive_segment2d (i1,i2)
	implicit none
	integer i1,i2,i
	i1=-1
	i2=-1
	do i=Nt-1,0,-1
		if (hR2d(r(1,i),r(2,i)) .eq. 1 .and. hR2d(r(1,i+1),r(2,i+1)) .eq. 0 .and. hP2d(r(1,i+1),r(2,i+1)) .eq. 0) then
			i1=i+1
			exit
		endif
	enddo
	do i=1,Nt
		if (hP2d(r(1,i),r(2,i)) .eq. 1 .and. hR2d(r(1,i-1),r(2,i-1)) .eq. 0 .and. hP2d(r(1,i-1),r(2,i-1)) .eq. 0) then
			i2=i-1
			exit
		endif
	enddo
	if (i2 .lt. i1) then
		do i=i2-1,0,-1
			if (hR2d(r(1,i),r(2,i)) .eq. 1 .and. hR2d(r(1,i+1),r(2,i+1)) .eq. 0 .and. hP2d(r(1,i+1),r(2,i+1)) .eq. 0) then
				i1=i+1
				exit
			endif
		enddo
	endif
	end subroutine compute_reactive_segment2d

	subroutine update_committor2d (it,lp)
	implicit none
	integer it,lp
	integer i,j
	i = Nint(r(1,it)/grid_dx)
	j = Nint(r(2,it)/grid_dy)
	if (i.ge.n_grid_x_left .and. i.le.n_grid_x_right .and. j.ge.n_grid_y_left .and. j.le.n_grid_y_right) then
		if (lp.eq.1) then
			n_committor(i,j) = n_committor(i,j)+1
			committor(i,j) = committor(i,j)+1.d0
		else
			n_committor(i,j) = n_committor(i,j)+1
		endif
	endif
	end subroutine update_committor2d
	
	subroutine recognize_trajectorytype (typ)
	implicit none
	integer typ
	
	integer k
	real*8 A1,A2,A3,A4
	integer l(3),ini(3),fin(3),l3,l4
	integer nsegm,k1,k2,k3,k4,k5,k6
	
	A1 = -0.5d0
	A2 = 0.5d0
	A3 = 1.26
	A4 = -1.26
	
	l(:)=0
	nsegm=1
	l3=0
	l4=0
	typ = 4
	
	do k=i1def,i2def
		if (nsegm.eq.1) then
			if (l(nsegm).eq.0) then
				if (r(1,k).gt.A1) then
					l(nsegm)=1
					ini(nsegm)=k
				else
					l(nsegm)=0
					ini(nsegm)=0
				endif
			endif
			
			if (l(nsegm).eq.1 .and. r(1,k).gt.A2) then
				fin(nsegm)=k
				nsegm = nsegm+1
			endif
		endif
		
		if (nsegm.eq.2 .and. r(1,k).gt.A3) l3=1
		
		if (nsegm.eq.2 .and. l3.eq.1 ) then
			if (l(nsegm).eq.0) then
				if (r(1,k).lt.A2) then
					l(nsegm)=1
					ini(nsegm)=k
				else
					l(nsegm)=0
					ini(nsegm)=0
				endif
			endif
			
			if (l(nsegm).eq.1 .and. r(1,k).lt.A1) then
				fin(nsegm)=k
				nsegm = nsegm+1
			endif
		endif
		
		if (nsegm.eq.3 .and. r(1,k).lt.A4) l4=1
		
		if (nsegm.eq.3 .and. l4.eq.1) then
			if (l(nsegm).eq.0) then
				if (r(1,k).gt.A1) then
					l(nsegm)=1
					ini(nsegm)=k
				else
					l(nsegm)=0
					ini(nsegm)=0
				endif
			endif
			
			if (l(nsegm).eq.1 .and. r(1,k).gt.A2) then
				fin(nsegm)=k
				nsegm = nsegm+1
			endif
		endif
	enddo
	nsegm=nsegm-1
	
	if (nsegm.eq.1) then
		k1=ini(1)
		k2=fin(1)
		if (r(2,k1).gt.0.d0 .and. r(2,k2).gt.0.d0 .or. r(2,k1).lt.0.d0 .and. r(2,k2).lt.0.d0) then
			typ=1
		else
			typ=2
		endif
	else if (nsegm.eq.3) then
		k1=ini(1)
		k2=fin(1)
		k3=ini(2)
		k4=fin(2)
		k5=ini(3)
		k6=fin(3)
		if (r(2,k1).gt.0.d0 .and. r(2,k2).lt.0.d0 .and. r(2,k3).gt.0.d0 .and. r(2,k4).lt.0.d0 &
		 .and. r(2,k5).gt.0.d0 .and. r(2,k6).lt.0.d0 .or. &
		    r(2,k1).lt.0.d0 .and. r(2,k2).gt.0.d0 .and. r(2,k3).lt.0.d0 .and. r(2,k4).gt.0.d0 &
		 .and. r(2,k5).lt.0.d0 .and. r(2,k6).gt.0.d0) then
			typ=3
		else
			typ=4
		endif
	endif
	end subroutine recognize_trajectorytype
	
	subroutine update_reactivep (n)
	implicit none
	integer i,j,k,n
!~ 	print*, 1
	do k = 0,n-1
		i = Nint(path(1,k)/grid_dx)
		j = Nint(path(2,k)/grid_dy)
		if (i.ge.n_grid_x_left .and. i.le.n_grid_x_right .and. j.ge.n_grid_y_left .and. j.le.n_grid_y_right) then
			reactivep(i,j) = reactivep(i,j) + 1.d0
!~ 			print*, reactivep(i,j)
		endif
	enddo
	end subroutine update_reactivep
	
	subroutine final_reactivep ()
	implicit none
	integer i,j
	real*8 somma
	somma=0.d0
	do i=n_grid_x_left,n_grid_x_right
		do j=n_grid_y_left,n_grid_y_right
			somma=somma+reactivep(i,j)
		enddo
	enddo
	print*, somma
	reactivep(:,:)=reactivep(:,:)/(somma*grid_dx*grid_dy)
	end subroutine final_reactivep
	
	subroutine update_reactive_probdensitycurrent2d ()
	implicit none
	integer it,lp
	integer i,j,l,k,typ
	real*8 vx,vy,mm,angle,length
	length = (i2def-i1def)*1.d0
	do k = i1def,i2def
	
		vx=(r(1,k+1)-r(1,k))/dt
		vy=(r(2,k+1)-r(2,k))/dt
		i = Nint(r(1,k)/grid_dx)
		j = Nint(r(2,k)/grid_dy)
		mm=mod(theta(k),2*pi)
		if (mm.lt.0) mm=mm+2*pi
		l = int(mm/grid_dtheta)+1
		
		if (i.ge.n_grid_x_left .and. i.le.n_grid_x_right .and. j.ge.n_grid_y_left .and. j.le.n_grid_y_right) then
			reactiveprobdensity(i,j) = reactiveprobdensity(i,j) + 1.d0
			reactiveprobdensityX(i) = reactiveprobdensityX(i) + 1.d0
			reactiveprobdensityY(j) = reactiveprobdensityY(j) + 1.d0
			angle = mm
			if (angle.gt.pi) angle = -(2*pi-angle)
			avgthetaX(1,i) = avgthetaX(1,i) + angle
			avgthetaY(1,j) = avgthetaY(1,j) + angle
			avgthetaX(2,i) = avgthetaX(2,i) + angle**2
			avgthetaY(2,j) = avgthetaY(2,j) + angle**2

			if (r(1,k).ge.-1.04 .and. r(1,k).le.-0.96) then
				thetadistributionX(1,l)=thetadistributionX(1,l)+1.d0
				thetadistributionX2(1,l)=thetadistributionX2(1,l)+1.d0/length
			endif
			if (r(1,k).ge.-0.04 .and. r(1,k).le.0.04) then
				thetadistributionX(2,l)=thetadistributionX(2,l)+1.d0
				thetadistributionX2(2,l)=thetadistributionX2(2,l)+1.d0/length
			endif
			if (r(1,k).ge.0.96 .and. r(1,k).le.1.04) then
				thetadistributionX(3,l)=thetadistributionX(3,l)+1.d0
				thetadistributionX2(3,l)=thetadistributionX2(3,l)+1.d0/length
			endif
			
			if (r(2,k).ge.-0.76 .and. r(2,k).le.-0.68) then
				thetadistributionY(1,l)=thetadistributionY(1,l)+1.d0
				thetadistributionY2(1,l)=thetadistributionY2(1,l)+1.d0/length
			endif
			if (r(2,k).ge.-0.04 .and. r(2,k).le.0.04) then
				thetadistributionY(2,l)=thetadistributionY(2,l)+1.d0
				thetadistributionY2(2,l)=thetadistributionY2(2,l)+1.d0/length
			endif
			if (r(2,k).ge.0.68 .and. r(2,k).le.0.76) then
				thetadistributionY(3,l)=thetadistributionY(3,l)+1.d0
				thetadistributionY2(3,l)=thetadistributionY2(3,l)+1.d0/length
			endif
			
			if (r(1,k).ge.-1.04 .and. r(1,k).le.-0.96 .and. r(2,k).ge.0.68 .and. r(2,k).le.0.76) then
				thetadistributionP(1,l)=thetadistributionP(1,l)+1.d0
				thetadistributionP2(1,l)=thetadistributionP2(1,l)+1.d0/length
			endif
			if (r(1,k).ge.-0.04 .and. r(1,k).le.0.04 .and. r(2,k).ge.0.68 .and. r(2,k).le.0.76) then
				thetadistributionP(2,l)=thetadistributionP(2,l)+1.d0
				thetadistributionP2(2,l)=thetadistributionP2(2,l)+1.d0/length
			endif
			if (r(1,k).ge.0.96 .and. r(1,k).le.1.04 .and. r(2,k).ge.0.68 .and. r(2,k).le.0.76) then
				thetadistributionP(3,l)=thetadistributionP(3,l)+1.d0
				thetadistributionP2(3,l)=thetadistributionP2(3,l)+1.d0/length
			endif
			if (r(1,k).ge.-0.04 .and. r(1,k).le.0.04 .and. r(2,k).ge.-0.04 .and. r(2,k).le.0.04) then
				thetadistributionP(4,l)=thetadistributionP(4,l)+1.d0
				thetadistributionP2(4,l)=thetadistributionP2(4,l)+1.d0/length
			endif
			
			if (r(1,k).ge.-1.14 .and. r(1,k).le.-1.06 .and. r(2,k).ge.0.68 .and. r(2,k).le.0.76) then
				thetadistributionP(5,l)=thetadistributionP(5,l)+1.d0
				thetadistributionP2(5,l)=thetadistributionP2(5,l)+1.d0/length
			endif
			if (r(1,k).ge.-0.44 .and. r(1,k).le.-0.36 .and. r(2,k).ge.0.68 .and. r(2,k).le.0.76) then
				thetadistributionP(6,l)=thetadistributionP(6,l)+1.d0
				thetadistributionP2(6,l)=thetadistributionP2(6,l)+1.d0/length
			endif
			if (r(1,k).ge.0.36 .and. r(1,k).le.0.44 .and. r(2,k).ge.0.68 .and. r(2,k).le.0.76) then
				thetadistributionP(7,l)=thetadistributionP(7,l)+1.d0
				thetadistributionP2(7,l)=thetadistributionP2(7,l)+1.d0/length
			endif
			if (r(1,k).ge.1.06 .and. r(1,k).le.1.14 .and. r(2,k).ge.0.68 .and. r(2,k).le.0.76) then
				thetadistributionP(8,l)=thetadistributionP(8,l)+1.d0
				thetadistributionP2(8,l)=thetadistributionP2(8,l)+1.d0/length
			endif
			
			if (r(1,k).ge.-1.36 .and. r(1,k).le.-1.28 .and. r(2,k).ge.-0.04 .and. r(2,k).le.0.04) then
				thetadistributionP(9,l)=thetadistributionP(9,l)+1.d0
				thetadistributionP2(9,l)=thetadistributionP2(9,l)+1.d0/length
			endif
			if (r(1,k).ge.-0.44 .and. r(1,k).le.-0.36 .and. r(2,k).ge.-0.04 .and. r(2,k).le.0.04) then
				thetadistributionP(10,l)=thetadistributionP(10,l)+1.d0
				thetadistributionP2(10,l)=thetadistributionP2(10,l)+1.d0/length
			endif
			if (r(1,k).ge.0.36 .and. r(1,k).le.0.44 .and. r(2,k).ge.-0.04 .and. r(2,k).le.0.04) then
				thetadistributionP(11,l)=thetadistributionP(11,l)+1.d0
				thetadistributionP2(11,l)=thetadistributionP2(11,l)+1.d0/length
			endif
			if (r(1,k).ge.1.28 .and. r(1,k).le.1.36 .and. r(2,k).ge.-0.04 .and. r(2,k).le.0.04) then
				thetadistributionP(12,l)=thetadistributionP(12,l)+1.d0
				thetadistributionP2(12,l)=thetadistributionP2(12,l)+1.d0/length
			endif
			
			n_reactiveJ(i,j) = n_reactiveJ(i,j)+1
			reactiveJ(i,j,1) = reactiveJ(i,j,1)+vx
			reactiveJ(i,j,2) = reactiveJ(i,j,2)+vy
		endif
	enddo
	
	call recognize_trajectorytype (typ)
	trajectorytype(typ) = trajectorytype(typ)+1.d0
	
	end subroutine update_reactive_probdensitycurrent2d

	subroutine compute_final_averages2d ()
	implicit none
	integer i,j,k,l
	real*8 somma
	committor(:,:) = committor(:,:)/n_committor(:,:)
	reactiveJ(:,:,1) = reactiveJ(:,:,1)/n_reactiveJ(:,:)
	reactiveJ(:,:,2) = reactiveJ(:,:,2)/n_reactiveJ(:,:)
	
	somma=0.d0
	do i=n_grid_x_left,n_grid_x_right
		do j=n_grid_y_left,n_grid_y_right
			somma=somma+reactiveprobdensity(i,j)
		enddo
	enddo
	reactiveprobdensity(:,:)=reactiveprobdensity(:,:)/(somma*grid_dx*grid_dy)
	
	somma=0.d0
	do i=n_grid_x_left,n_grid_x_right
		somma=somma+reactiveprobdensityX(i)
		avgthetaX(1,i)=avgthetaX(1,i)/reactiveprobdensityX(i)
		avgthetaX(2,i)=dsqrt(avgthetaX(2,i)/reactiveprobdensityX(i) - avgthetaX(1,i)**2)
	enddo
	reactiveprobdensityX(:)=reactiveprobdensityX(:)/(somma*grid_dx)
	
	somma=0.d0
	do j=n_grid_y_left,n_grid_y_right
		somma=somma+reactiveprobdensityY(j)
		avgthetaY(1,j)=avgthetaY(1,j)/reactiveprobdensityY(j)
		avgthetaY(2,j)=dsqrt(avgthetaY(2,j)/reactiveprobdensityY(j) - avgthetaY(1,j)**2)
	enddo
	reactiveprobdensityY(:)=reactiveprobdensityY(:)/(somma*grid_dy)
	
	do k=1,3
		somma=0.d0
		do l=1,n_grid_theta
			somma=somma+thetadistributionX(k,l)
		enddo
		thetadistributionX(k,:)=thetadistributionX(k,:)/(somma*grid_dtheta)
	enddo
	
	do k=1,3
		somma=0.d0
		do l=1,n_grid_theta
			somma=somma+thetadistributionX2(k,l)
		enddo
		thetadistributionX2(k,:)=thetadistributionX2(k,:)/(somma*grid_dtheta)
	enddo
	
	do k=1,3
		somma=0.d0
		do l=1,n_grid_theta
			somma=somma+thetadistributionY(k,l)
		enddo
		thetadistributionY(k,:)=thetadistributionY(k,:)/(somma*grid_dtheta)
	enddo
	
	do k=1,3
		somma=0.d0
		do l=1,n_grid_theta
			somma=somma+thetadistributionY2(k,l)
		enddo
		thetadistributionY2(k,:)=thetadistributionY2(k,:)/(somma*grid_dtheta)
	enddo
	
	do k=1,12
		somma=0.d0
		do l=1,n_grid_theta
			somma=somma+thetadistributionP(k,l)
		enddo
		thetadistributionP(k,:)=thetadistributionP(k,:)/(somma*grid_dtheta)
	enddo
	
	do k=1,12
		somma=0.d0
		do l=1,n_grid_theta
			somma=somma+thetadistributionP2(k,l)
		enddo
		thetadistributionP2(k,:)=thetadistributionP2(k,:)/(somma*grid_dtheta)
	enddo
	
	somma=0.d0
	do k=1,4
		somma = somma + trajectorytype(k)
	enddo
	trajectorytype(:) = trajectorytype(:)/somma
	
	
	end subroutine compute_final_averages2d

	subroutine print_committor2d ()
	implicit none
	integer i,j
	open(unit=10,file='Committor_active_pe5.dat')
	write(10,*) '# ',grid_x_left,grid_x_right,grid_dx
	write(10,*) '# ',grid_y_left,grid_y_right,grid_dy
	do i=n_grid_x_left,n_grid_x_right
		do j=n_grid_y_left,n_grid_y_right
			write(10,'(F6.3,1X,F6.3,2X,F8.5,1X,I9)') i*grid_dx,j*grid_dx,committor(i,j),n_committor(i,j)
		enddo
	enddo
	close(10)
	end subroutine print_committor2d

	subroutine print_reactiveprobabilitydensity2d ()
	implicit none
	integer i,j,l
	
	open(unit=10,file='ReactiveProbabilityDensity_active_pe5.dat')
	write(10,*) '# ',grid_x_left,grid_x_right,grid_dx
	write(10,*) '# ',grid_y_left,grid_y_right,grid_dy
	do i=n_grid_x_left,n_grid_x_right
		do j=n_grid_y_left,n_grid_y_right
			write(10,'(F6.3,1X,F6.3,2X,F8.5)') i*grid_dx,j*grid_dx,reactiveprobdensity(i,j)
		enddo
	enddo
	close(10)
	
	open(unit=10,file='ReactiveProbabilityDensityX_active_pe5.dat')
	write(10,*) '# ',grid_x_left,grid_x_right,grid_dx
	do i=n_grid_x_left,n_grid_x_right
		write(10,'(F6.3,2X,F8.5,2X,F8.5,1X,F8.5)') i*grid_dx,reactiveprobdensityX(i),avgthetaX(:,i)
	enddo
	close(10)
	
	open(unit=10,file='ReactiveProbabilityDensityY_active_pe5.dat')
	write(10,*) '# ',grid_y_left,grid_y_right,grid_dy
	do j=n_grid_y_left,n_grid_y_right
		write(10,'(F6.3,2X,F8.5,2X,F8.5,1X,F8.5)') j*grid_dx,reactiveprobdensityY(j),avgthetaY(:,j)
	enddo
	close(10)
	
	open(unit=10,file='ThetaDistributionX_active_pe5.dat')
	write(10,*) '# theta distribution X'
	do l=1,n_grid_theta
		write(10,'(F6.3,3(2X,F8.5))') l*grid_dtheta,thetadistributionX(:,l)
	enddo
	close(10)
	open(unit=10,file='ThetaDistributionX2_active_pe5.dat')
	write(10,*) '# theta distribution X'
	do l=1,n_grid_theta
		write(10,'(F6.3,3(2X,F8.5))') l*grid_dtheta,thetadistributionX2(:,l)
	enddo
	close(10)
	
	open(unit=10,file='ThetaDistributionY_active_pe5.dat')
	write(10,*) '# theta distribution Y'
	do l=1,n_grid_theta
		write(10,'(F6.3,3(2X,F8.5))') l*grid_dtheta,thetadistributionY(:,l)
	enddo
	close(10)
	open(unit=10,file='ThetaDistributionY2_active_pe5.dat')
	write(10,*) '# theta distribution Y'
	do l=1,n_grid_theta
		write(10,'(F6.3,3(2X,F8.5))') l*grid_dtheta,thetadistributionY2(:,l)
	enddo
	close(10)
	
	open(unit=10,file='ThetaDistributionP_active_pe5.dat')
	write(10,*) '# theta distribution Points'
	do l=1,n_grid_theta
		write(10,'(F6.3,12(2X,F8.5))') l*grid_dtheta,thetadistributionP(:,l)
	enddo
	close(10)
	open(unit=10,file='ThetaDistributionP2_active_pe5.dat')
	write(10,*) '# theta distribution Points'
	do l=1,n_grid_theta
		write(10,'(F6.3,12(2X,F8.5))') l*grid_dtheta,thetadistributionP2(:,l)
	enddo
	close(10)
	
	open(unit=10,file='ReactivePathModes_pe5.dat')
	write(10,*) '# theta distribution Points'
	do l=1,4
		write(10,'(I2,2X,F8.5)') l,trajectorytype(l)
	enddo
	close(10)
	
	end subroutine print_reactiveprobabilitydensity2d

	subroutine print_reactivecurrent2d ()
	implicit none
	integer i,j
	open(unit=10,file='ReactiveCurrent_active_pe5.dat')
	write(10,*) '# ',grid_x_left,grid_x_right,grid_dx
	write(10,*) '# ',grid_y_left,grid_y_right,grid_dy
	do i=n_grid_x_left,n_grid_x_right
		do j=n_grid_y_left,n_grid_y_right
			if (n_reactiveJ(i,j).gt.0) then
			 write(10,'(F6.3,1X,F6.3,2X,F9.4,1X,F9.4,2X,I9,2X,F8.5)') &
			   i*grid_dx,j*grid_dx,reactiveJ(i,j,1),reactiveJ(i,j,2),n_reactiveJ(i,j),reactiveprobdensity(i,j)
			else
			 write(10,'(F6.3,1X,F6.3,2X,F9.4,1X,F9.4,2X,I9,2X,F8.5)') i*grid_dx,j*grid_dx,0.d0,0.d0,n_reactiveJ(i,j),0.d0
			endif
		enddo
	enddo
	close(10)
	end subroutine print_reactivecurrent2d

	subroutine print_trajectory_def ()
	implicit none
	integer i
	open(unit=10,file='trajdef.dat')
	do i=i1def-1,i2def+1
		write(10,*) r(1,i),r(2,i),mod(theta(i),2*pi)
	enddo
	close(10)
	end subroutine print_trajectory_def
	
	subroutine print_trajectory_prov (i1,i2)
	implicit none
	integer i1,i2
	integer i
	open(unit=10,file='trajprov.dat')
	do i=i1-1,i2+1
		write(10,*) rnew(1,i),rnew(2,i),mod(thetanew(i),2*pi)
	enddo
	close(10)
	end subroutine print_trajectory_prov
	
	!!!!!!!!!!!!!!!!!!!!  other usefull staff  !!!!!!!!!!!!!!!!!!!!!!!!!
	FUNCTION ranm()
	implicit none
	integer, parameter :: K4B=selected_int_kind(9)
	REAL*8 :: ranm
	!	Minimal” random number generator of Park and Miller combined with a Marsaglia shift
	!	sequence. Returns a uniform random deviate between 0.0 and 1.0 (exclusive of the endpoint
	!	values). This fully portable, scalar generator has the “traditional” (not Fortran 90) calling
	!	sequence with a random deviate as the returned function value: call with idum
	!	a negative integer to initialize; thereafter, do not alter idum except to reinitialize.
	!	The period of this generator is about 3.1 x 10^18
	INTEGER(K4B), PARAMETER :: IA=16807,IM=2147483647,IQ=127773,IR=2836
	REAL*8, SAVE :: am
	INTEGER(K4B), SAVE :: ix=-1,iy=-1,k
	if (idum <= 0 .or. iy < 0) then  !	Initialize.
		am=nearest(1.0,-1.0)/IM
		iy=ior(ieor(888889999,abs(idum)),1)
		ix=ieor(777755555,abs(idum))
		idum=abs(idum)+1
	endif
	ix=ieor(ix,ishft(ix,13))
	ix=ieor(ix,ishft(ix,-17))
	ix=ieor(ix,ishft(ix,5))
	k=iy/IQ
	iy=IA*(iy-k*IQ)-IR*k
	if (iy < 0) iy=iy+IM
	ranm=am*ior(iand(IM,ieor(ix,iy)),1)
	END FUNCTION ranm

	FUNCTION ranv()
	implicit none
	integer, parameter :: K4B=selected_int_kind(9)
	REAL*8 :: ranv
	!	Minimal” random number generator of Park and Miller combined with a Marsaglia shift
	!	sequence. Returns a uniform random deviate between 0.0 and 1.0 (exclusive of the endpoint
	!	values). This fully portable, scalar generator has the “traditional” (not Fortran 90) calling
	!	sequence with a random deviate as the returned function value: call with idum
	!	a negative integer to initialize; thereafter, do not alter idum except to reinitialize.
	!	The period of this generator is about 3.1 x 10^18
	INTEGER(K4B), PARAMETER :: IA=16807,IM=2147483647,IQ=127773,IR=2836
	REAL*8, SAVE :: am
	INTEGER(K4B), SAVE :: ix=-1,iy=-1,k
	if (iduv <= 0 .or. iy < 0) then  !	Initialize.
		am=nearest(1.0,-1.0)/IM
		iy=ior(ieor(888889999,abs(iduv)),1)
		ix=ieor(777755555,abs(iduv))
		iduv=abs(iduv)+1
	endif
	ix=ieor(ix,ishft(ix,13))
	ix=ieor(ix,ishft(ix,-17))
	ix=ieor(ix,ishft(ix,5))
	k=iy/IQ
	iy=IA*(iy-k*IQ)-IR*k
	if (iy < 0) iy=iy+IM
	ranv=am*ior(iand(IM,ieor(ix,iy)),1)
	END FUNCTION ranv

	FUNCTION ranintm(imin,imax)
	INTEGER imin,imax,iscelto,ranintm
	REAL*8 ran0,delta
	ran0 = ranm()
	delta=imax - imin + 1.0
	iscelto= int(ran0*delta)
	ranintm = imin + iscelto
	if (ranintm.lt.imin) ranintm=imin
	if (ranintm.gt.imax) ranintm=imax
	return
	END FUNCTION ranintm

	Subroutine GaussianVariable (DW,DW2)
	implicit none
	real*8 Dw,DW2
	real*8 XTm,XTv,pi
	parameter (pi=3.141592653589793238d0)
	XTm=ranm()
	XTv=ranv()
	DW=dsqrt(-2.d0*dlog(XTm)) * dcos(2*pi*XTv)
	DW2=dsqrt(-2.d0*dlog(XTm)) * dsin(2*pi*XTv)
	end Subroutine GaussianVariable

end module TPS2d_active_def_enhanced
