0
votes

I am working on a project to simulate a hard sphere model of a gas. (Similar to the ideal gas model.)

I have written my entire project, and it is working. To give you an idea of what I have done, there is a loop which does the following: (Pseudo code)

Get_Next_Collision(); // Figure out when the next collision will occur
Step_Time_Forwards(); // Step to time of collision
Process_Collision(); // Process collision between 2 particles
(Repeat)

For a large number of particles (say N particles), O(N*N) checks must be made to figure out when the next collision occurs. It is clearly inefficient to follow the above procedure, because in the vast majority of cases, collisions between pairs of particles are unaffected by the processing of a collision elsewhere. Therefore it is desirable to have some form of priority queue which stores the next event for each particle. (Actually, since a collision involves 2 particles, only half that number of events will be stored, because if A collides with B then B also collides with A, and at exactly the same time.)

I am finding it difficult to write such an event/collision priority queue.

I would like to know if there are any Molecular Dynamics simulators which have been written and which I can go and look at the source code in order to understand how I might implement such a priority queue.

Having done a google search, it is clear to me that there are many MD programs which have been written, however many of them are either vastly too complex or not suitable.

This may be because they have huge functionality, including the ability to produce visualizations or ability to compute the simulation for particles which have interacting forces acting between them, etc.

Some simulators are not suitable because they do calculations for a different model, ie: something other than the energy conserving, hard sphere model with elastic collisions. For example, particles interacting with potentials or non-spherical particles.

I have tried looking at the source code for LAMMPS, but it's vast and I struggle to make any sense of it.

I hope that is enough information about what I am trying to do. If not I can probably add some more info.

2
You're asking for an open sourced molecular dynamics program which does exactly what you're trying to do, nothing more and nothing less, that you can look at for inspiration to write up your own program? - user1717828
Learn some algorithm stuff, don't waste your time looking such basic algorithm in other people's code. - usr1234567
Not necessarily, the key points here are: If such a thing exists, can I look at the source code, does it implement some sort of a priority queue, and is it simple enough (say a few tens of files, a few tens of thousands of lines of code) that I can learn from it? - FreelanceConsultant
Do you make use of locality in your collision detection code? You can also use reality bubbles aka. observable universes in physics. - Boyko Perfanov
If you want to keep your model and just solve the event queue problem you are facing now, search for discreet event simulators, instead of simulators for molecular dynamics. This is a common simulation technique that is not bound to your domain (molecular dynamics). - André Sassi

2 Answers

0
votes

A basic version of a locality-aware system could look like this:

  1. Divide the universe into a cubic grid (where each cube has side A, and volume A^3), where each cube is sufficiently large, but sufficiently smaller than the total volume of the system. Each grid cube is further divided into 4 sub-cubes whose particles it can theoretically give to its neighboring cubes (and lend for calculations).
  2. Each grid cube registers particles that are contained within it and is aware of its neighboring grid cubes' contained particles.
  3. Define a particle's observable universe to have a radius of (grid dimension/2). Define timestep=(griddim/2) / max_speed. This postulates that particles from a maximum of four, adjacent grid cubes can theoretically interact in that time period.
  4. For every particle in every grid cube, run your traditional collision detection algorithm (with mini_timestep < timestep, where each particle is checked for possible collisions with other particles in its observable universe. Store the collisions into any structure sorted by time, even just an array, sorted by the time of collision.
  5. The first collision that happens within a mini_timestep resets your universe(and universe clock) to (last_time + time_to_collide), where time_to_collide < mini_timestep. I suppose that does not differ from your current algorithm. Important note: particles' absolute coordinates are updated, but which grid cube and sub-cube they belong to are not updated.
  6. Repeat step 5 until the large timestep has passed. Update the ownership of particles by each grid square.

The advantage of this system is that for each time window, we have (assuming uniform distribution of particles) O(universe_particles * grid_size) instead of O(universe_particles * universe_size) checks for collision. In good conditions (depending on universe size, speed and density of particles), you could improve the computation efficiency by orders of magnitude.

0
votes

I didn't understand how the 'priority queue' approach would work, but I have an alternative approach that may help you. It is what I think @Boyko Perfanov meant with 'make use of locality'.

You can sort the particles into 'buckets', such that you don't have to check each particle against each other ( O(n²) ). This uses the fact that particles can only collide if they are already quite close to each other. Create buckets that represent a small area/volume, and fill in all particles that are currently in the area/volume of the bucket ( O(n) worst case ). Then check all particles inside a bucket against the other particles in the bucket ( O(m*(n/m)²) average case, m = number of buckets ). The buckets need to be overlapping for this to work, or else you could also check the particles from neighboring buckets.

Update: If the particles can travel for a longer distance than the bucket size, an obvious 'solution' is to decrease the time-step. However this will increase the running time of the algorithm again, and it works only if there is a maximum speed.

Another solution applicable even when there is no maximum speed, would be to create an additional 'high velocity' bucket. Since the velocity distribution is usually a gaussian curve, not many particles would have to be placed into that bucket, so the 'bucket approach' would still be more efficient than O(n²).