Lets say i have a bunch of particles moving and i want to make they stay inside de box x:[0,1], y:[0,1], z:[0,1]. Does someone know how to make a efficient way to do that? The method i tried is this:
do i=1, 3 !passing by the 3 components
do j=1, Nm !passing by all the particles
if((r(j,i)>=1d0).or.(r(j,i)<=0)) then
CALL random_number(RF1)
CALL random_number(RF2)
theta = 2*pi*RF2
vt = sqrt(-log(RF1)) !some random tangential velocity
if(i==1) then
!for the x component the normal velocity is vx and the tangentiasls are vy and vz
v(j, i) = -vt !changing the signal for the normal velocity
v(j, 2) = vt*cos(theta)
v(j, 3) = vt*sin(theta)
else if(i==2) then
!for the y component the normal velocity is vy and the tangentiasls are vx and vz
v(j, i) = -vt !changing the signal for the normal velocity
v(j, 1) = vt*cos(theta)
v(j, 3) = vt*sin(theta)
else
!for the z component the normal velocity is vz and the tangentiasls are vy and vx
v(j, i) = -vt !changing the signal for the normal velocity
v(j, 1) = vt*cos(theta)
v(j, 2) = vt*sin(theta)
endif
endif
enddo
enddo
This ins’t working because the particles are, for some reason i haven’t time to check yet, staying at x,y,z~15-17. If someone know a proper way to do it, it would help me a lot!