Home : Course Map : Chapter 20 :
Simulation Demo
JavaTech
Course Map
Chapter 20

Introduction
Simulation
Server Demo
Client Demo
Demo Support

Exercises

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

The simulation is run by the server as directed by the client. The code listings are given for the following classes:

The directory path for the package for the Simulation, SimulationThread, and SimData classes goes as

javatech
   |
   |__ all20
                    |
                    |__ server
                          |
                          |__ impl

 

Simulation.java

package javatech.all20.server.impl;

//-----------------------------------------------------------------------
//    CLASS:        Simulation                    -
/************************************************************************
* This class does the real computational work of the simulation demo.
************************************************************************/
public class Simulation {
  final public static int ABNORMAL = -1;
  final public static int NORMAL = 0;
  final public static int FORCED_HALT = 1;

  private SimData fSimData;
  private float fAmp, fOmega, fSpringCons, fDeltaT, fMaxTime, fMass;
  private boolean fKillMe;

  //-------------------------------------------------------------------
  //    CONSTRCUTOR:        Simulation                -
  /********************************************************************
   * Constructs a Simulation object.
   *******************************************************************/
  public Simulation (SimData sd) {
    fSimData = sd;
  } // ctor


  //-------------------------------------------------------------------
  //    METHOD:        initialize                    -
  /********************************************************************
   * Initializes the simulation.
   *
   * @param indata    array of initial simulation data
   *******************************************************************/
  /** Initialize the simulation. **/
  public void initialize (float[] indata) {
    fAmp    = indata[0];
    fOmega   = indata[1];
    fSpringCons = indata[2];
    fDeltaT   = indata[3];
    fMaxTime  = indata[4];

    fMass = fSpringCons/(fOmega*fOmega);
  } // initialize


  //-------------------------------------------------------------------
  //    METHOD:        halt                        -
  /********************************************************************
   * Halts the simulation by setting a flag that will be periodically
   * checked by the running simulation code.
   *******************************************************************/
  public void halt () { fKillMe = true; }


  //-------------------------------------------------------------------
  //    METHOD:        startRunning                    -
  /********************************************************************
   * Start calculating the simulation results.
   *******************************************************************/
  public int startRunning () {
    fKillMe = false;
    int status = ABNORMAL; // leave abnormal until reset
    float time = 0.f;
    int iter = 0;
    float[] data = new float[SimData.OUTSIZE];
    float[] new_indata = new float[SimData.INSIZE];
    int num_data_stores = 0;

    // Keep running until the fKillMe flag becomes true.
    while (!fKillMe) {
      time = iter * fDeltaT;
      if (time <= fMaxTime) {
        // Simulate a long-running calculation by sleeping for 100 ms.
        try {
          Thread.sleep (100);
        }
        catch (InterruptedException ie) {/*ignore*/}

        // Populate the output data array
        // time
        data[0] = time;
        // Calculated values:
        // position = A * sin (omega*t)
        data[1] = fAmp * (float) Math.sin (fOmega * time);
        // velocity = omega*A * cos (omega*t)
        data[2] = fOmega * fAmp * (float) Math.cos (fOmega * time);
        // acceleration = -omega**2 * A * sin (omega*t) = -omega**2 * x
        data[3] = -fOmega*fOmega * data[1];
        // kinetic energy = 1/2 * m * v**2
        data[4] = .5f * fMass * data[2]*data[2];
        // potential energy = 1/2 * k * x**2
        data[5] = .5f * fSpringCons * data[1]*data[1];
        System.err.println ("storing " + data[1] + " " + data[2] + " " +
         
data[3] + " " + data[4] + " " + data[5] + " at " + data[0]);
        new_indata = fSimData.storeData (data);
        fAmp = new_indata[0];
        fOmega = new_indata[1];
        fSpringCons = new_indata[2];
        num_data_stores++;
        // If the control flag in new_indata[5] == 1, halt the simulation.
        if ((int)new_indata[5] == 1) halt();
      }

      else {
        System.err.println (
   "Simulation: time exceeds max, storing negative time and ending simulation");
        data[0] = -time;
        data[1] = -999.999f;
        fSimData.storeData (data);
        status = NORMAL;
        break;
      }
      iter ++;
    } // while !fKillMe

    // If exiting because of the fKillMe flag, set data[0] and data[1]
    // to special values indicating a forced termination.
    if (fKillMe) {
      data[0] = -100.f;
      data[1] = -100.f;
      System.err.println ("storing -100 -100 for FORCED_HALT");
      fSimData.storeData (data);
      status = FORCED_HALT;
    }

    System.err.println ("Total valid data stores = " + num_data_stores);
    return status;
  } // startRunning
} // Simulation

SimulationThread.java
package javatech.all20.server.impl;

//-----------------------------------------------------------------------
//    CLASS:        SimulationThread                -
/************************************************************************
* This is the thread that runs the simulation.
************************************************************************/
public class SimulationThread extends Thread {
  private Simulation fSimulation;

  //-------------------------------------------------------------------
  //    CONSTRCUTOR:        SimulationThread            -
  /********************************************************************
   * Constructs a SimulationThread object.
   *******************************************************************/
  public SimulationThread (SimData simData) {
    fSimulation = new Simulation (simData);
  } // ctor


  //-------------------------------------------------------------------
  //    METHOD:        halt                        -
  /********************************************************************
   * Halts the simulation.
   *******************************************************************/
  void halt () { fSimulation.halt(); }


  //-------------------------------------------------------------------
  //    METHOD:        initialize                    -
  /********************************************************************
   * Initializes the simulation.
   *******************************************************************/
  public void initialize (float[] initdata) {
    fSimulation.initialize (initdata);
  } // initialize


  //-------------------------------------------------------------------
  //    METHOD:        run                        -
  /********************************************************************
   * Starts the simulation running.
   *******************************************************************/
  public void run () {
    // Start the simulation running. The simulation will continue to run
    // until the end time is reached or it is forced to halt with the halt()
    // method or it quits abnormally. This return status is not presently
    // being used.
    int status = fSimulation.startRunning ();

    String stat = status == Simulation.NORMAL ? "NORMAL" :
             status == Simulation.FORCED_HALT ? "FORCED HALT":
             status == Simulation.ABNORMAL ? "ABNORMAL" :
           "REALLY REALLY ABNORMAL!";
    System.err.println ("SimulationThread: simulation finished with status " +
      status + " (" + stat + ")");
    fSimulation = null;
  } // run
} // SimulationThread
SimData.java
package javatech.all20.server.impl;

//-----------------------------------------------------------------------
//    CLASS:        SimData                        -
/************************************************************************
* Stores and retrieves simulation data arrays.
************************************************************************/
public class SimData {
  final public static int INSIZE = 6;
  final public static int OUTSIZE = 6;

  private float[] fInData;
  private float[] fOutData;

  //-------------------------------------------------------------------
  //    CONSTRCUTOR:        SimData                    -
  /********************************************************************
   * Constructs a SimData object.
   *******************************************************************/
  /** ctor **/
  public SimData () {
    fInData = new float [INSIZE];
    fOutData = new float [OUTSIZE];
    //fOutData[0] = -1.f;
  } // ctor


  //-------------------------------------------------------------------
  //    METHOD:        storeData                    -
  /********************************************************************
   * Stores an array of data and returns the current input data set.
   *
   * @param data    array of data to be stored
   *******************************************************************/
  public synchronized float[] storeData (float[] data) {
    System.arraycopy (data, 0, fOutData, 0, OUTSIZE);
    return fInData;
  } // storeData


  //-------------------------------------------------------------------
  //    METHOD:        getResults                    -
  /********************************************************************
   * Retrieves the data set most recently stored with storeData
   * while loading a new set of input data.
   *
   * @param indata    new array of input data
   *
   * @return array containing the current output data set
   *******************************************************************/
  public synchronized float[] getResults (float[] indata) {
    System.arraycopy (indata, 0, fInData, 0, INSIZE);
    return fOutData;
  } // getResults
} // SimData

 

 

Most recent update: Oct. 14, 2005

  
  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.