SimpleGravity.java

The next interface that needs to be implemented is the PhysicalAlgorithm. The main purpose of this class is to provide the guts of the simulation. The equations of motion for our ball are going to be numerically integrated using the Eurler algorithm. The complete source code will show the following classes are necessary for compilation.

import java.util.Iterator;
import feynman.framework.system.PhysicalAlgorithm;
import feynman.framework.system.PhysicalSystem;
import feynman.framework.simulation.Simulation;
The Iterator and PhysicalSystem are used as they were in the Setup class. In addition the PhysicalAlgorithm is included so that the SimpleGravity class can implement it. The Simulation class is included and why it is needed will be discussed later. The class definition states it implements PhysicalAlgorithm and we also see that the Ball and Iterator type local instance variables that were used in the Setup class.
public class SimpleGravity implements PhysicalAlgorithm {
	
	// ------------------------------------- instance variables.
	private Iterator i;
	private Ball b;
As required by the contract of PhysicalAlgorithm, the SimpleGravity class provides an implementation for the calculate method as shown below:
public void calculate(Simulation sim, PhysicalSystem ps) {

	// Retrieve the reference to the particle from the physical system.
	i = ps.iterator();
	b = (Ball) i.next();

	// Store the previous velocity calculation.
	b.setVyold(b.getVy());

	// Euler algorithm.
	b.setAy(-9.8);
	b.setVy(b.getVy() + b.getAy() * sim.getInterval());
	b.setY(b.getY() + b.getVy() * sim.getInterval());

	...
}
Here we see that the calculate has a Simulation as an argument. The method retrieves a Ball from the collection for use. The previous value of the velocity is stored for reference when the setVyold() method is invoked. In this case a nested invocation is performed because the argument is also an invocation of the getVy() method of the Ball. This method is done to implement the Euler algorithm for numerically integrating the equations of motions for the Ball. The Euler algorithm for the Ball is

Equations implementing the Euler algorithm

From the source code you can see the nested invocation method is used to set the n+1 values of the position, velocity and acceleration using the get methods to retrieve the n values. We also see the first reference to methods accessible in the Simulation bean. The getInterval() method retrieves the value of the interval represented by dt in the equations above. The interval is important because its value has an effect on the overall accuracy of the simulation.

After the equations of motion are integrated and the new values of the position, velocity, and acceleration are updated for our Ball bean, the next step is to determine if the Ball has reached its maximum height. The methodology used to determine this is based on the fact that the sign of the y component of the velocity will change when the Ball has stopped ascending and is now descending.

	// Look for a change in sign in the velocity to catch the max height.
	if ((b.getVy() * b.getVyold()) < 0) b.setMaxheight(b.getY());
The if statement catches this and then assigns the maximum height value to the Ball bean based on the current y value.

The last step is an important one. The Simulation bean has a property called isRunning that is a boolean value of either true or false. The simulation initial starts with a value of true since it is actually running. If at any point and time the simulation needs to stopped or is determined to be completed based on a physical condition being met when there are still more iterations to be done, then the setRunning() method can be invoked to turn off the simulation.

	// Stop the simulation after the ball hits the ground.
	if(b.getY() < 0.0) sim.setRunning(false);
This is important because the Controller class queries this property using the isRunning() method. If this method returns a false value, then the Controller exits is iteration loop and then invokes the report() method of the FinalReport interface.

Previous Section | Next Section