/**
  * Computes and plots correlation functions
  *
  * @param tauMax is the maximum time for correlation functions
  */
 public void computeCorrelation(int tauMax) {
   plotFrame.clearData();
   double energyAccumulator = 0, magnetizationAccumulator = 0;
   double energySquaredAccumulator = 0, magnetizationSquaredAccumulator = 0;
   for (int t = 0; t < numberOfPoints; t++) {
     energyAccumulator += energy[t];
     magnetizationAccumulator += magnetization[t];
     energySquaredAccumulator += energy[t] * energy[t];
     magnetizationSquaredAccumulator += magnetization[t] * magnetization[t];
   }
   double averageEnergySquared = Math.pow(energyAccumulator / numberOfPoints, 2);
   double averageMagnetizationSquared = Math.pow(magnetizationAccumulator / numberOfPoints, 2);
   // compute normalization factors
   double normE = (energySquaredAccumulator / numberOfPoints) - averageEnergySquared;
   double normM = (magnetizationSquaredAccumulator / numberOfPoints) - averageMagnetizationSquared;
   for (int tau = 1; tau <= tauMax; tau++) {
     double c_MAccumulator = 0;
     double c_EAccumulator = 0;
     int counter = 0;
     for (int t = 0; t < numberOfPoints - tau; t++) {
       c_MAccumulator += magnetization[t] * magnetization[t + tau];
       c_EAccumulator += energy[t] * energy[t + tau];
       counter++;
     }
     // correlation function defined so that c(0) = 1 and c(infinity) -> 0
     plotFrame.append(0, tau, ((c_MAccumulator / counter) - averageMagnetizationSquared) / normM);
     plotFrame.append(1, tau, ((c_EAccumulator / counter) - averageEnergySquared) / normE);
   }
   plotFrame.setVisible(true);
 }
 public void readXMLData() {
   energy = new double[0];
   magnetization = new double[0];
   numberOfPoints = 0;
   String filename = "ising_data.xml";
   JFileChooser chooser = OSPFrame.getChooser();
   int result = chooser.showOpenDialog(null);
   if (result == JFileChooser.APPROVE_OPTION) {
     filename = chooser.getSelectedFile().getAbsolutePath();
   } else {
     return;
   }
   XMLControlElement xmlControl = new XMLControlElement(filename);
   if (xmlControl.failedToRead()) {
     control.println("failed to read: " + filename);
   } else {
     // gets the datasets in the xml file
     Iterator it = xmlControl.getObjects(Dataset.class, false).iterator();
     while (it.hasNext()) {
       Dataset dataset = (Dataset) it.next();
       if (dataset.getName().equals("magnetization")) {
         magnetization = dataset.getYPoints();
       }
       if (dataset.getName().equals("energy")) {
         energy = dataset.getYPoints();
       }
     }
     numberOfPoints = magnetization.length;
     control.println("Reading: " + filename);
     control.println("Number of points = " + numberOfPoints);
   }
   calculate();
   plotFrame.repaint();
 }