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
ResourceFunction["HardSphereSimulation"][assoc] generates a simulation of the motion of a system of identical unit-mass hard spheres with simulation parameters specified by assoc. |
"Positions" | initial particle positions |
"Velocities" | initial particle velocities |
"BoxRadius" | half-side-length of simulation box |
"ParticleRadius" | radius of spheres |
"Steps" | number of simulation time steps |
"dt" | duration of a single time step |
"BoundaryCondition" | interaction between particles and container walls |
"Output" | simulation output |
"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 |
"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 |
"FinalFrame" | Graphics object depicting the state of the system at the final time step |
"CausalGraph" | Graph object with nodes representing collisions and directed edges indicating that one particle was contiguously involved in two collisions |
Find the position of two hard disks over five time steps:
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]= |
Return the positions grouped by particle:
In[5]:= |
Out[5]= |
Get the velocities by particle:
In[6]:= |
Out[6]= |
Get the speeds by particle:
In[7]:= |
Out[7]= |
Get the trajectories by particle:
In[8]:= |
Out[8]= |
Use HardSphereSimulation to visualize many hard spheres moving in a box:
In[9]:= |
Out[9]= |
Use "Random" to generate random initial positions in the box:
In[10]:= |
Out[10]= |
Visualization works in 1D and 3D as well:
In[11]:= |
Out[11]= |
In[12]:= |
Out[12]= |
Periodic boundary conditions identify opposite walls of the box with one another:
In[13]:= |
Out[13]= |
Infinite boundary conditions eliminate particle-wall interactions altogether:
In[14]:= |
Out[14]= |
Particle speeds should obey Maxwell-Boltzmann distribution:
In[15]:= |
Out[15]= |
Extract a collision list from the simulation, where collisions take the form {particle1,particle2,time of collision}:
In[16]:= |
Out[16]= |
From particle-particle collisions, construct the causal graph:
In[17]:= |
Out[17]= |
Construct a causal graph for a disordered system (ensuring initial positions do not overlap to avoid spurious collisions):
In[18]:= |
Out[18]= |
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[19]:= |
Out[19]= |
Total kinetic energy of spheres should be conserved in time. Show that the error stays within machine precision:
In[20]:= |
Out[20]= |
Momentum should be conserved during collisions between particles:
In[21]:= |
Out[21]= |
Particle overlaps are resolved pairwise, so situations involving multiple overlapping spheres may lead to unexpected results (such as a disruption of symmetry):
In[22]:= |
Out[22]= |
Simulate a Newton's cradle:
In[23]:= |
Out[23]= |
Use HardSphereSimulation to simulate a simplified game of pool:
In[24]:= |
Out[8]= |
This work is licensed under a Creative Commons Attribution 4.0 International License