# Wolfram Function Repository

Instant-use add-on functions for the Wolfram Language

Function Repository Resource:

Simulate identical unit-mass hard spheres moving in an N-dimensional box

Contributed by:
Matt Kafker and Christopher Wolfram

ResourceFunction["HardSphereSimulation"][ generates a simulation of the motion of a system of identical unit-mass hard spheres with simulation parameters specified by |

Input *assoc* must include the following:

"Positions" | initial particle positions |

"Velocities" | initial particle velocities |

"BoxSize" | side-length of simulation box |

"ParticleRadius" | radius of spheres |

"Steps" | number of simulation time steps |

"StepSize" | duration of a single time step |

"BoundaryCondition" | interaction between particles and container walls |

"Output" | simulation output |

The dimensions of vectors in "Positions" and "Velocities" determine the spatial dimension of the system (they can be any positive integer dimension).

The box is centered on the origin.

Possible named boundary conditions include:

"Reflecting" | particle velocities reflect upon contact with container walls |

"Periodic" | particles are translated to opposite side of box upon reaching container walls |

"Infinite" | particles do not alter motion upon reaching container walls |

Possible named outputs include:

"PositionsByTime" | list of all particle positions at every time step |

"VelocitiesByTime" | list of all particle velocities at every time step |

"SpeedsByTime" | list of all particle speeds at every time step |

"TrajectoriesByTime" | list of all particle positions and velocities at every time step |

"PositionsByParticle" | list of positions over time for each particle |

"VelocitiesByParticle" | list of velocities over time for each particle |

"SpeedsByParticle" | list of speeds over time for each particle |

"TrajectoriesByParticle" | list of positions and velocities over time for each particle |

"Collisions" | list of particle-particle collisions |

"All" | list containing “TrajectoriesByTime” output as the first entry, and “Collisions” as the second |

"Visualize" | Graphics object with a Manipulate slider for visualizing the time-evolution of the system in one, two, or three spatial dimensions |

"KineticEnergy" | list of total kinetic energy at every time step |

"Temperature" | list of temperature at every time step |

"CausalGraph" | Graph object with nodes representing collisions and directed edges indicating that one particle was contiguously involved in two collisions |

Multiple outputs can be provided as a List

Each collision takes the form {*particle*_{i},*particle*_{j},*time of collision*}.

The option "RandomNonOverlapping" may be given for *pos*, in which case a random configuration of non-overlapping particle positions is chosen within the box.

Simulate the motion of two hard disks over five time steps, and extract the trajectory data in various ways. Get the positions over time:

In[1]:= |

Out[1]= |

Get the velocities over time:

In[2]:= |

Out[2]= |

Get the speeds over time:

In[3]:= |

Out[3]= |

Get the trajectories over time:

In[4]:= |

Out[4]= |

Get the positions grouped by particle:

In[5]:= |

Out[5]= |

Get the velocities grouped by particle:

In[6]:= |

Out[6]= |

Get the speeds grouped by particle:

In[7]:= |

Out[7]= |

Get the trajectories grouped by particle:

In[8]:= |

Out[8]= |

Use trajectory data to visualize frames from a hard sphere simulation:

In[9]:= |

Out[9]= |

Use "Visualize" to generate an interactive visualization of many hard spheres moving in a box:

In[10]:= |

Out[10]= |

Use "RandomNonOverlapping" to generate random non-overlapping initial positions in the box:

In[11]:= |

Out[11]= |

Visualization works in 1D:

In[12]:= |

Out[12]= |

Visualization works in 3D as well:

In[13]:= |

Out[13]= |

Periodic boundary conditions identify opposite walls of the box with one another:

In[14]:= |

Out[14]= |

Infinite boundary conditions eliminate particle-wall interactions altogether:

In[15]:= |

Out[15]= |

Multiple outputs can be returned at once:

In[16]:= |

Out[16]= |

Particle speeds should obey Maxwell-Boltzmann distribution:

In[17]:= |

Out[17]= |

Extract a collision list from the simulation, where collisions take the form {particle_{1},particle_{2},time of collision}:

In[18]:= |

Out[18]= |

From particle-particle collisions, construct the causal graph:

In[19]:= |

Out[19]= |

Construct a causal graph for a disordered system:

In[20]:= |

Out[20]= |

As the nodes of the causal graph represent collisions between two particles and the edges represent individual particles, we expect all nodes to have indegree and outdegree 2, besides from the graph boundary (i.e., the beginning and end of the simulation):

In[21]:= |

Out[21]= |

Total kinetic energy of spheres should be conserved in time. Show that the error stays within machine precision:

In[22]:= |

Out[22]= |

Momentum should be conserved during collisions between particles:

In[23]:= |

Out[23]= |

Particle overlaps are resolved pairwise, so situations involving multiple overlapping spheres may lead to unexpected results (such as a disruption of symmetry):

In[24]:= |

Out[24]= |

The "RandomNonOverlapping" option is not guaranteed to find non-overlapping configurations at very high packing fractions:

In[25]:= |

Out[25]= |

Simulate a Newton's cradle:

In[26]:= |

Out[26]= |

Use HardSphereSimulation to simulate a simplified game of pool:

In[27]:= |

Out[30]= |

This work is licensed under a Creative Commons Attribution 4.0 International License