View

advertisement
Easy Java Simulations step-by-step series of examples
Ideal Gas
Description
This simulation models the microscopic behavior of an ideal gas. Molecules can move
in uniform motion in any direction with equal probability and collide with the container
walls in an elastic way, although they don't interact among them.
The upper container wall can be moved to different heights. We visualize the
relationship between the volume of the container and the pressure the gas exerts on the
walls (and hope to get Boyle’s Law).
Model
Variables
We organize our variables is three pages, one for the container variables, one for the
molecules, and a final one for global variables.
The minimum and maximum x and y variables give us the container boundaries. The
wallX and wallY variables indicate the central point of the (draggable) upper wall.
Coupled Oscillators
page 1 of 8
Easy Java Simulations step-by-step series of examples




n indicates the number of molecules
mass and radius give the physical characteristics of each individual molecule.
speed indicates the common speed of the molecules.
The arrays x, y, vx, and vy contain the molecules positions and velocities.

t is the time (which is not actually used in the model) and dt its increment in
each iteration (evolution step)
average indicates how many samples to consider when measuring the average
pressure, counter is used to know when to measure.
pressure will hold the pressure exerted by the molecules on the container walls,
accPressure is the pressure in the period sampled.


Initialization
The code in the initialization page sets random initial conditions. The counter is set to a
negative integer in order to allow the system to run for a while before actually taking
measurements. Note how we turn the random number generated by Math.random (in
the range of the [0,1) interval) into a value in the right range.
Coupled Oscillators
page 2 of 8
Easy Java Simulations step-by-step series of examples
Evolution
Since the motion is uniform, we use plain pages of Java code to write our algorithm. A
more advanced version of this simulation could use the editor for differential equations
to solve for the motion, together with pages of events to compute the collisions with the
walls. Again, this is not needed in our case due to the simplicity of uniform motion.
The second page computes the collisions of the molecules with the walls and, at the
same time, computes the total pressure exerted by the molecules on the container walls.
The code (two lengthy to fit in a picture) reads:
double total = 0.0;
for (int i=0; i<n; i++) {
if (x[i]>=xmax-radius) { // Right wall
x[i]=2.0*(xmax-radius)-x[i];
vx[i]=-vx[i];
total += 2.0*Math.abs(vx[i]);
}
else if (x[i]<=xmin+radius) { // Left wall
x[i]=2.0*(xmin+radius)-x[i];
vx[i]=-vx[i];
total += 2.0*Math.abs(vx[i]);
}
if (y[i]>=wallY-radius) { // Upper wall
y[i]=2.0*(wallY-radius)-y[i]; vy[i]=-vy[i];
total += 2.0*Math.abs(vy[i]);
}
Coupled Oscillators
page 3 of 8
Easy Java Simulations step-by-step series of examples
else if (y[i]<=ymin+radius) { // Lower wall
y[i]=2.0*(ymin+radius)-y[i]; vy[i]=-vy[i];
total += 2.0*Math.abs(vy[i]);
}
}
double area = 2.0*(xmax-xmin)+2.0*(wallY-ymin);
pressure = total*mass/(area*dt);
Finally, the third page of the evolution computes the pressure average (when the counter
reaches the right value) and plots the new pressure versus volume point. It also pauses
the simulation so that the user can select a new position for the upper wall and restart
the simulation.
Constraints
No constraints are required.
Custom code
No custom code required. See however the Action properties of some view elements.
View
The view will have two windows. A left frame will show the molecules in the container
and the typical control buttons. A right dialog will show the plot of pressure versus
volume. See the complete Tree of Elements on the figure below.
For the left frame we start with the standard compound element which hosts a drawing
panel (pointed to by an arrow in the figure), rename the default particle included in it to
container, and add a ParticleSet element (also pointed to by an arrow in the figure) and
two more particles. The particles will be used to display the container, the upper wall
and a holder to move it.
Coupled Oscillators
page 4 of 8
Easy Java Simulations step-by-step series of examples
The following figures illustrate the most relevant property panel of the view elements in
this first window:
Coupled Oscillators
page 5 of 8
Easy Java Simulations step-by-step series of examples
The code of the On Drag action property for the holder element reads:
Coupled Oscillators
page 6 of 8
Easy Java Simulations step-by-step series of examples
wallX = (xmax+xmin)/2.0; // Keep the wall centered
wallY = Math.max(wallY,ymin+4*radius); // Don't go that low
for (int i=0; i<n; i++) if (y[i]>wallY) y[i] = wallY; // brush molecules
For the right window we use a Dialog view element with border layout, which has a
single child PlotingPanel in its Center position. The plotting panel has a Trace element
which plots the pressure versus volume points.
The properties of these elements are:
Notice the trace element has noo input defined. Points are added to the plot using the
element instance method:
_view.pressureVsVolume.addPoint(volume,accPressure);
The points in the plot are not connected by lines, but appear as isolated red rectangles of
4x4 pixels.
Coupled Oscillators
page 7 of 8
Easy Java Simulations step-by-step series of examples
Running the simulation
A sample run of this simulation with different positions of the upper wall produces the
plot displayed in the figure below.
Author
Francisco Esquembre
Universidad de Murcia, Spain
July 2007
Coupled Oscillators
page 8 of 8
Download