Simulate the motion of N colliding particles according to the laws of elastic collision using event-driven simulation. Such simulations are widely used in molecular dynamics (MD) to understand and predict properties of physical systems at the particle level. This includes the motion of molecules in a gas, the dynamics of chemical reactions, atomic diffusion, sphere packing, the stability of the rings around Saturn, the phase transitions of cerium and cesium, one-dimensional self-gravitating systems, and front propagation. The same techniques apply to other domains that involve physical modeling of particle systems including computer graphics, computer games, and robotics.
Hard sphere model. The hard sphere model (billiard ball model) is an idealized model of the motion of atoms or molecules in a container. In this assignment, we will focus on the two-dimensional version called the hard disc model. The salient properties of this model are listed below.
Simulation. There are two natural approaches to simulating the system of particles.
This event-driven approach results in a more robust, accurate, and efficient simulation than the time-driven one.
Collision prediction. In this section we present the formulas that specify if and when a particle will collide with the boundary or with another particle, assuming all particles travel in straight-line trajectories.
Since the coordinates are between 0 and 1, a particle comes into contact with a horizontal wall at time t + Δt if the quantity ry + Δt vy equals either σ or (1 - σ). Solving for Δt yields:
An analogous equation predicts the time of collision with a vertical wall.
Let (rxi' , ryi' ) and (rxj' , ryj' ) denote the positions of particles i and j at the moment of contact, say t + Δt. When the particles collide, their centers are separated by a distance of σ = σi + σj. In other words:
σ2 = (rxi' - rxj' )2 + (ryi' - ryj' )2During the time prior to the collision, the particles move in straight-line trajectories. Thus,
rxi' = rxi + Δt vxi, ryi' = ryi + Δt vyiSubstituting these four equations into the previous one, solving the resulting quadratic equation for Δt, selecting the physically relevant root, and simplifying, we obtain an expression for Δt in terms of the known positions, velocities, and radii.
rxj' = rxj + Δt vxj, ryj' = ryj + Δt vyj
If either Δv ⋅ Δr ≥ 0 or d < 0, then the quadratic equation has no solution for Δt > 0; otherwise we are guaranteed that Δt ≥ 0.
Collision resolution. In this section we present the physical formulas that specify the behavior of a particle after an elastic collision with a reflecting boundary or with another particle. For simplicity, we ignore multi-particle collisions. There are three equations governing the elastic collision between a pair of hard discs: (i) conservation of linear momentum, (ii) conservation of kinetic energy, and (iii) upon collision, the normal force acts perpendicular to the surface at the collision point. Physics-ly inclined students are encouraged to derive the equations from first principles; the rest of you may keep reading.
and where mi and mj are the masses of particles i and j, and σ, Δx, Δy and Δ v ⋅ Δr are defined as above. Once we know the impulse, we can apply Newton's second law (in momentum form) to compute the velocities immediately after the collision.
vxi' = vxi + Jx / mi, vxj' = vxj - Jx / mj
vyi' = vyi + Jy / mi, vyj' = vyj - Jy / mj
Data format. Use the following data format. The first line contains the number of particles N. Each of the remaining N lines consists of 6 real numbers (position, velocity, mass, and radius) followed by three integers (red, green, and blue values of color). You may assume that all of the position coordinates are between 0 and 1, and the color values are between 0 and 255. Also, you may assume that none of the particles intersect each other or the walls.
N rx ry vx vy mass radius r g b rx ry vx vy mass radius r g b rx ry vx vy mass radius r g b rx ry vx vy mass radius r g b
Particle collision simulation in Java.
There are a number of ways to design a particle collision simulation program
using the physics formulas above.
We will describe one such approach, but you are free to substitute
your own ideas instead.
Our approach involves the following
data types: Particle, CollisionSystem,
and Event.
Particle.java: Particle data type.
Create a data type to represent particles moving in the unit box.
Include private instance variables for the position (in the x and y
directions), velocity (in the x and y directions), mass, and radius.
It should also have the following instance methods
public Particle(...):
constructor.
public double collidesX():
return the duration of time until the invoking particle collides
with a vertical wall, assuming it follows a straight-line trajectory.
If the particle never collides with a vertical wall, return
+infinity = DOUBLE_MAX or NaN???)
public double collidesY():
return the duration of time until the invoking particle collides
with a horizontal wall, assuming it follows a straight-line trajectory.
If the particle never collides with a horizontal wall, return
a negative number.
public double collides(Particle b):
return the duration of time until the invoking particle collides
with particle b, assuming both follow straight-line trajectories.
If the two particles never collide, return a negative value.
public void bounceX(): update the invoking particle
to simulate it bouncing off a vertical wall.
public void bounceY(): update the invoking particle
to simulate it bouncing off a horizontal wall.
public void bounce(Particle b): update both particles
to simulate them bouncing off each other.
public int getCollisionCount(): return the total
number of collisions involving this particle.
WallCollision: a collision
with a vertical VerticalCollision or
a horizontal HorizontalCollision wall,
BinaryCollision: a collision between two particles,
and RedrawEvent: a redraw event.
The Event class is repeated here:
import java.util.*;
public abstract class Event implements Comparable<Event>
{
protected double myTime; // time that event is scheduled to occur
protected List<Particle> myParticles; // particles involved in this event
// create a new event to occur at time t
public Event(double t)
{
myTime = t;
myParticles = new ArrayList<Particle>();
}
// accessor methods
public double time() {
return myTime;
}
public List<Particle> particles() {
return myParticles;
}
/**
* Compare the time associated with this event and that.
*
* @return a positive number (if this event occurs after that), a negative number
* (before), or zero (equal) accordingly.
*/
public int compareTo(Event that)
{
// TODO: complete compareTo
return 0;
}
/**
* has any collision occurred between when event was created and now?
*
* @return true iff no collisions have occurred with the particle(s)
* involved in this Event
*/
public abstract boolean isValid();
/**
* Process this event
*/
public abstract void process();
}
In order to implement isValid, the
event data type should store the collision counts of
the involved particles at the time the event was created.
The event corresponds to a physical collision if the
current collision counts of the particles are the
same as when the event was created.
CollisionSystem.java: Particle collision system.
The main program containing the event-driven simulation.
Follow the event-driven simulation loop described above, but also
consider collisions with the four walls and redraw events.
Data sets.
Event,
BinaryCollision, WallCollision, HorizontalCollision, &
RedrawEvent.
predict and main loop in simulate.