Particles Confined in a Box

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!

Usually what one wants in these cases is to use periodic boundary conditions, and then you wrap the coordinates everytime particle crosses the boundaries. An example of a function to do that is here.

If you want the box to effectively have walls, that you need to implement either a force or an instantaneous change in the velocity of the particle as soon as it reaches the boundary.

1 Like

Is there a link for your presentation at FortCon?

It is at the conference youtube channel: FortranCon2021: Invited Pres.: A computational chemist perspective on Fortran vs. Julia programming - YouTube (I’m not particularly proud of it… didn´t manage the time very well).

I would like to change the velocity of the particles as they reach the boundary, the way i showed seems to work when the time step is small enough but some of the particles still can pass the boundary. Vi uns arquivos em portugues no seu git então caso prefira em portugues também hehe.

If you cannot tolerate any imprecision there, I think you need to compute analytically the instant of in which the particle hits the wall, and adjust the propagated position with that information (that analytical computation is possible at each time step, assuming constant acceleration within the time step).

1 Like

Sorry for a may be irrelevant comment, usually periodic or reflective boundary conditions are used.
In stead of using if then else, perhaps can use things like

x=mod(x,size_xxx)

Or things like that you know. The x on the right hand side can be any value, x on the left hand side will be confined within size_xxx. Things like that.
I apologize first if that does not help.

2 Likes