Usage memo on the Open Source Physics Library (OSP)

Convenient java library called OSP is provided from www.opensourcephysics.org.

In this lecture (and the textbook), we use the library as a tool for writing graphical interfaces such as plotting graphics and doing visualizations.

Plotting numerical data (Simple Examples in chap.2)

Let's try to run PlotFrameApp and read the source codes shown below.

"PlotFrameApp.java" is an example to plot (x,y) data.

package org.opensourcephysics.sip.ch02;
import org.opensourcephysics.frames.PlotFrame;

public class PlotFrameApp {

  /**
   * Starts the Java application.
   * @param args  command line parameters
   */
  public static void main(String[] args) {
    PlotFrame frame = new PlotFrame("x", "sin(x)/x", "Plot example");
    for(int i = -100;i<=100;i++) {
      double x = i*0.2;
      frame.append(0, x, Math.sin(x)/x);
    }
    frame.setVisible(true);
    frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
  }
}

To create a new frame and set initial conditions and show the results in it.

Let's try to run CalculationApp and read the source codes.

"CalculationApp.java" is an example where we can set parameters and view the result in a frame (window). That also includes "start" and "reset" buttons.

package org.opensourcephysics.sip.ch02;
// gets needed classes, asterisk * means get all classes in controls subdirectory

import org.opensourcephysics.controls.*;

public class CalculationApp extends AbstractCalculation {

  /**
   * Does a calculation.
   */
  public void calculate() { // Does a calculation
    control.println("Calculation button pressed.");
    double x = control.getDouble("x value"); // String must match argument of setValue
    control.println("x*x = "+(x*x));
    control.println("random = "+Math.random());
  }

  /**
   * Resets the program to its initial state.
   */
  public void reset() {
    control.setValue("x value", 10.0); // describes parameter and sets its value
  }

  /**
   * Starts the Java application.
   * @param args  command line parameters
   */
  public static void main(String[] args) { // Create a calculation control structure using this class
    CalculationControl.createApp(new CalculationApp());
  }
}

Using OSP to improve "FallingParticle": "FallingParticleCalcApp.java"

We try to apply the control class to the falling particle simulation.

package org.opensourcephysics.sip.ch02;
import org.opensourcephysics.controls.*;

public class FallingParticleCalcApp extends AbstractCalculation { // beginning of class definition

  /**
   * Calculates the time it takes a ball to fall to the ground and displays the variables.
   */
  public void calculate() {
    // gets initial conditions
    double y0 = control.getDouble("Initial y");
    double v0 = control.getDouble("Initial v");
    // sets initial conditions
    Particle ball = new FallingParticle(y0, v0);
    // reads parameters and sets dt
    ball.dt = control.getDouble("dt");
    while(ball.y>0) {
      ball.step();
    }
    control.println("final time = "+ball.t);
    control.println("y = "+ball.y+" v = "+ball.v); // displays numerical results
    control.println("analytic y = "+ball.analyticPosition()); // displays analytic position
    control.println("analytic v = "+ball.analyticVelocity()); // displays analytic velocity
  }

  /**
   * Resets the program to its initial state.
   */
  public void reset() {
    control.setValue("Initial y", 10); // sets default input values
    control.setValue("Initial v", 0);
    control.setValue("dt", 0.01);
  }

  /**
   * Starts the Java application.
   * @param args  command line parameters
   */
  public static void main(String[] args) { // creates a calculation control structure using this class
    CalculationControl.createApp(new FallingParticleCalcApp());
  }
} // end of class definition

Animation and Simulation

Simulation for balls bouncing in a box is consited by 2 files:

BouncingBall.java

package org.opensourcephysics.sip.ch02;
import org.opensourcephysics.display.Circle;

public class BouncingBall extends Circle { // Circle is a class that can draw itself
  final static double g = 9.8; // constant
  final static double WALL = 10;
  private double x, y, vx, vy; // initial position and velocity

  /**
   * Constructs a BouncingBall with the given initial conditions.
   *
   * @param x double
   * @param vx double
   * @param y double
   * @param vy double
   */
  public BouncingBall(double x, double vx, double y, double vy) { // constructor
    this.x = x;   // sets instance value equal to passed value
    this.vx = vx; // sets instance value equal to passed value
    this.y = y;
    this.vy = vy;
    setXY(x, y); // sets the position using setXY in Circle superclass
  }

  /**
   * Steps (advances) the time.
   * @param dt double
   */
  public void step(double dt) {
    x = x+vx*dt; // Euler algorithm for numerical solution
    y = y+vy*dt;
    vy = vy-g*dt;
    if(x>WALL) {
      vx = -Math.abs(vx); // bounce off right wall
    } else if(x<-WALL) {
      vx = Math.abs(vx);  // bounce off left wall
    }
    if(y<0) {
      vy = Math.abs(vy); // bounce off floor
    }
    setXY(x, y);
  }
}

BouncingBallApp.java

package org.opensourcephysics.sip.ch02;
import org.opensourcephysics.controls.*;
import org.opensourcephysics.frames.*;

public class BouncingBallApp extends AbstractSimulation {
  // declares and instantiates a window to draw balls
  DisplayFrame frame = new DisplayFrame("x", "y", "Bouncing Balls");
  BouncingBall[] ball; // declares an array of BouncingBall objects
  double time, dt;

  /**
   * Initializes the simulation by creating the BouncingBall objects and adding them
   * to the frame.
   */
  public void initialize() {
    // sets boundaries of window in world coordinates
    frame.setPreferredMinMax(-10.0, 10.0, 0, 10);
    time = 0;
    frame.clearDrawables(); // removes old particles
    int n = control.getInt("number of balls");
    int v = control.getInt("speed");
    ball = new BouncingBall[n]; // instantiates array of n BouncingBall objects
    for(int i = 0;i < n;i++) {
      double theta = Math.PI*Math.random(); // random angle
      // instantiates the ith BouncingBall object
      ball[i] = new BouncingBall(0, v*Math.cos(theta), 0, v*Math.sin(theta));
      frame.addDrawable(ball[i]);           // // adds ball to frame so that it will be displayed
    }
    // decimalFormat instantiated in superclass and used to format numbers conveniently
    frame.setMessage("t = "+decimalFormat.format(time)); // appears in lower right hand corner
  }

  /**
   * Does a simulation step by stepping (advancing) each ball.
   */
  public void doStep() { // invoked every 1/10 second by timer in AbstractSimulation superclass
    for(int i = 0;i < ball.length;i++) {
      ball[i].step(dt);
    }
    time += dt;
    frame.setMessage("t="+decimalFormat.format(time));
  }

  /**
   * Checks the time step parameter whenever the start or step button is pressed.
   */
  public void startRunning() { // invoked when start or step button is pressed
    dt = control.getDouble("dt");
  } // gets time step

  /**
   * Resets the simulation parameters to their intial state.
   */
  public void reset() { // invoked when reset button is pressed
    control.setAdjustableValue("dt", 0.1); // allows dt to be changed after initializaton
    control.setValue("number of balls", 40);
    control.setValue("speed", 10);
  }

  /**
   * Starts the Java application.
   * @param args  command line parameters
   */
  public static void main(String[] args) { // sets up animation control structure using this class
    SimulationControl.createApp(new BouncingBallApp());
  }
}

Making javadoc

Then, you can see the documents of osp in "doc -> lib -> resources".

Note that the real directory of documents (html files) is located at "osp/bin/org/opensourcephysics/resources/tools/html".