package clothSimulator;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
//import java.util.Timer;
//import java.util.TimerTask;

public class ClothSimExperiment1 {

	/** this experiment is meant to find an optimum Baum parameter.  We look at sevarl nxn sheets and for each one we go through many different
	 *Baumgarte parameters.  We look at the cumulative absolute value error for each Baum setting and record it into a file.
	 * 
	 */

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub


		//set the damping to twice the square root of the spring constant for baumgarte
		//set the cloth to nxn
		//run
		//get the g vector
		//fidn the maximum constraint value
		//record it
		
//		int stepper=0;
		//Timer theTimer= new Timer();
		//long numSecs=3000;
		theSimulator=new clothSimulation();
		integrateSteps=(int)Math.floor(8/theSimulator.h.getValue());
	   // TimerTask task ;
		arrayAsVector g;		
		for(sheetSize=10;sheetSize<12;sheetSize++){
			//set the sheet width and height to i and create a new system			
			theSimulator.system.sheetWidth.setValue(sheetSize);
			theSimulator.system.sheetHeight.setValue(sheetSize);		
			//create the sheet
			theSimulator.steprequest = false;
			theSimulator.play.setValue(false);
			try { Thread.sleep(50); } catch(Exception ex) { ex.printStackTrace(); }
			theSimulator.newSystem(2);
			//adjust baumgarte and record the error at each new baumgarte parameter
			c=125;
			k=0;
			for(int j=0; j< BaumSteps; j++){
				totalError=0;
				//set the Baum parameter (critical damping)
				c+=cStep;
				theSimulator.system.BGkd.setValue(c);
				k=c*c/4;
				theSimulator.system.BGks.setValue(k);
				//step the system repeatedly until integrateSteps frames have passed
				//as the system runs, add up the absolute value of all the constraints
			    //reset the system
				theSimulator.play.setValue(false);
				try {
					Thread.sleep(200);
				} catch (InterruptedException f) {
					// TODO Auto-generated catch block
					f.printStackTrace();
				}

 			    theSimulator.resetSystem();
 			    
				try {
					Thread.sleep(200);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
 			    
 			    
 			    theSimulator.play.setValue(true);
				//now start summing the errors and hopefully this timer thing is like a thread and will eventually stop the system
				//the following loop should run for 10 seconds or some other period of time, so get rid of dataPoints
				//also, we want to sum up all the error over all time and over all constraints
//				stepper=0;
				theSimulator.play.setValue(true);
				int currentFrame=-1;
				while(theSimulator.system.frame < integrateSteps) {
					if(theSimulator.system.frame!=currentFrame){
						g=theSimulator.system.constraintList.make_g_Vector();
						//find the absolute-value-norm ie the sum of the absolute value of all the entries
						totalError+=g.getAbsValNorm();
						currentFrame=theSimulator.system.frame;
					}
				}
				//now print to a file
				recordError(totalError);
				try {
					Thread.sleep(50);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		theSimulator.steprequest = false;
		theSimulator.play.setValue(false);
		System.out.println("Done getting data");
	}
	
	//this is the number of integration steps we take...it will be such that integrateSteps*timeStep=around 10 seconds
	private static int integrateSteps;
	private static int sheetSize;
	//the BaumParameters which we set to critical damping ie k=c*c/4
	private static double c;
	private static double k;
	private static double cStep=2;
	//BaumSteps is just maximum Baum value divided by an actual step in Baum Value
	private static int BaumSteps=175;
	private static double totalError;
	public static Boolean TenSecondsDone=false;
	private static OutputStreamWriter out;
	private static clothSimulation theSimulator;
	public static void recordError(double error){
		//open a file 
		File distanceError=new File("");
//		String fileName;
		if(theSimulator.get_applyBaumgarte()){
			distanceError = new File("Experiment1/BaumErr_"+sheetSize+".txt");
		}else if (theSimulator.get_applyPostStep()){
			distanceError = new File("PostStepError.txt");
		}
		//record the error		
		try{
			FileOutputStream dataOut =new FileOutputStream( distanceError, true ); // append
			out = new OutputStreamWriter(dataOut);
			out.write(""+c+", "+error+" \n");				
			out.close();
		}catch(Exception e){
			//do nothing
			System.out.println("...couldn't write to file..");
		}
	}
	
}
