//CoData.macroVersion(6.100);
/**
 * This program makes a .dt file (lorenz.dt) with the 
 * X, Y, and Z values from Lorenz's simulated Weather Model, 
 * first published in the article: Deterministic Nonperiodic
 * Flow, Journal of the Atmospheric Sciences, 20 (1963) pp.
 * 130-141.
 * Copyright 1999-2002 CoHort Software.
 */
public class Lorenz extends com.cohort.CoData {

  void checkRunError() {
    if (length(getRunError())>0) error(getRunError());
  }

  void error(String s) {
    System.out.println("Error: "+s);
    exit(1);
  }

  public void run() {
    System.out.println("Creating lorenz.dt...");
    double dt=0.0001;
    int every=50, nPoints=2000;
    int i, j;
    double x=2, y=2, z=2;  //initial values not to important
    double dx, dy, dz;
    double time=currentTimeMillis();

    //reset the data file (not necessary, but be safe)
    runDataReset();
    checkRunError();

    //set up the rows and columns
    runDataInsertColumns(1, "fff", "X,Y,Z");
    checkRunError();
    runDataInsertRows(1, nPoints);
    checkRunError();

    //generate the data points
    //-1000 to 0 gives it time to find the attractor
    for (i=-1000; i<=nPoints; i+=1) {
      for (j=1; j<=every; j+=1) {
        dx=10*(y-x);
        dy=-x*z+28*x-y;
        dz=x*y-(8/3.0)*z;
        x+=dx*dt;
        y+=dy*dt;
        z+=dz*dt;
      }
      if (i>=1) {
        setDataDouble(1, i, x);
        setDataDouble(2, i, y);
        setDataDouble(3, i, z);
      }
    }
    
    //save the file
    runDataSaveAs(5, "", "lorenz.dt", 
      1, getDataNColumns(), 1, getDataNRows(), newline());
    checkRunError();
    System.out.println("Time = "+(currentTimeMillis()-time)+" ms.");    
  } 

  public static void main(String args[]) {
    Lorenz instance=new Lorenz();
    instance.run();
  }
  
}
