back up next contents bibliography JBONE applet: programming

Resources:   applet   --   execution   --   programing   --   variable index   --   source code

The numerical schemes submitted from the java window are automatically inserted in the JBONE source code (e.g. solution 2.01) and compiled on the server before you can download them for execution locally in your browser. This webpage reviews a limited number of commands for those who are not familiar with the java programming language, but nevertheless who would like to modify the applets. More details concerning the source code can be found in the program tree, the name index and the program listing. For a complete tutorial in java programming, consult the free course from Sun Microsystems.

Algorithm for time evolution.
The JBONE = Java Bed for ONE dimension applet is a wrapper to perform a stepwise evolution of functions, i.e. solving time-dependent differential equations using an algorithm of the form

  1. At time=0 use an initial condition (e.g. Gaussian, Cosine, Box) to initialize various arrays f0[i],f[i],g[i] with sampled on a mesh x[i], i=0.. x.length-1.
  2. Plot f (black curve), f0 (grey), and g (blue, not always visible).
  3. Use the java code predefined in the applet or submitted from the java window to calculate the new values fp obtained after a small time step using the present f and sometimes the past values fm.
  4. Shift the time levels by updating the variables time=time+timeStep and the solution arrays fm=f; f=fp.
  5. Goto 2 until finished.
Only the third step can be modified by the user in the java window; for example, a loop
   double scale = runData.getParamValue(rundata.UserDoubleNm);
   for (int i=0; i<=n; i++) {
     fp[i]=scale*f[i];
   }
executes an artificial evolution, where the next value fp is obtained from a simple scaling of the present value f, where the scaling factor corresponds to the TAG parameter UserDouble defined in the applet. When programming, remember that you have to force your browser to reload the applet after each modification to prevent it from using an older version that is often stored in the browser cache.

JBONE variables.
The list of run parameters is a java object called runData is used to defines local variables (double = 16 digits real, int = 9 digits signed integer) using statements of the form
  advection vel double vel =runData.getParamValue(rundata.VelocityName);
  diffusion coeff double diff =runData.getParamValue(rundata.DiffusionName);
  dispersion coeff double disp =runData.getParamValue(rundata.DispersionName);
  time step double step =runData.getParamValue(rundata.TimeStepName)();
  number mesh pts int n =runData.getParamValueInt(rundata.MeshPointsName);
  number particles int N =runData.getParamValueInt(rundata.WalkersName);
  implicit time double theta=runData.getParamValue(rundata.TimeThetaName);
  tunable quadrat. double tune =runData.getParamValue(rundata.TuneQuadName);
  IC amplitude double ampl =runData.getParamValueInt(rundata.ICAmplitudeName);
  IC position double pos =runData.getParamValueInt(rundata.ICPositionName);
  IC width double width=runData.getParamValueInt(rundata.ICWidthName);
  IC wavelength double wavel=runData.getParamValueInt(rundata.ICWavelengthName);
  total run time double rTime=runData.getParamValue(rundata.RunTimeName);
  lower value axis double x0 =runData.getParamValue(rundata.MeshLeftName);
  length of axis double len =runData.getParamValue(rundata.MeshLengthName);
  user defined int int myInt=runData.getParamValueInt(rundata.PhysDataCaseName);
  user defined dbl double myDbl=runData.getParamValue(rundata.PhysDataValueName);

The evolution is then computed with the help of predefined arrays containing the solution (an object called solution)
  time in the simulation double time;
  last index of solution int n = x.length-1;
  mesh points, intervals double[] x,dx;
  solution functions double[] f0,f,g;
  old, present future double[] fm,f,fp;
  derivatives double[] dfm,df,dfp;
  current realization double[] currentState;

Note that in java as well as in C and C++, the index of arrays starts with zero (x[0]) and finishes with an index lower by one element less than its size (x[x.length-1]).

Debugging.
Having corrected all the compiler errors does unfortunately not mean that your scheme immediately behaves the way you want... You may have to temporarily inactivate an error by commenting it out with the /* java comment delimiters */

   /* This mistake divides by zero but has been commented out
   double error = fp[n]/0;
   */
   System.out.println(fp[i]);
or monitor the value of a quantity, here the values of the array fp, by printing it to the java console of your browser. From the values that are printed after a single step, it is often possible to track down the mistakes.

Common errors.
Avoid the most common difficulties when you start programming

  • Every new variable that is not explicitly listed in the global index of variables has to be declared; memory must be allocated for arrays and objects using the java command new:
      int i = 3;                // Declare i as an integer
      double[] c;               // Declare c[] array 16 digits nbrs
      c = new double[i];        // Memory for c[0], c[1], c[2]
      BandMatrix  A;            // Declare A as a BandMatrix object
      A = new BandMatrix(3,10); // Memory for 3 bands with 10 doubles
    
  • Accessing the element c[0] before the memory has been attributed with a new statement leads to the infamous java.lang.NullPointerException error, while the element c[3] throws java.lang.ArrayIndexOutOfBoundsException:3, because the array is accessed outside its valid range 0,1,2.
  • In java, the assigning equal sign is denoted by as single = whereas the comparing equal sign by a double ==
      int a = 42;
      if (a == 17) System.out.println("a is equal to 17");
      if (a != 17) System.out.println("a is not equal to 17");
    
    will print the text "a is not equal to 17" to the java console.



      
back up next contents bibliography Copyright © Lifelong-learners at 13:19:47, February 11th, 2012