/*
*               TO CALCULATE CAPACITOR DISCHARGE
*/

#include"lib351.h"

void main(){

	//____DATA_DESCRIPTION___
	bool goout = false;
	while (!goout){

    	ofstream dataOutStr;

  		int nCols = 70;
  		int wPage = 80;
   		int hPage = 101;
		int nTSteps, m1, m2;
		float tau, deltaT;
		float q1, q2, time, deltaQ;

		//____DATA_INPUT___
		cout <<"Please, supply values :"<< endl;
		cout <<"time constant, tau ="<< endl;
    		cin  >>tau;
		cout <<"\ntime step, deltaT ="<< endl;
    		cin  >>deltaT;
  		cout <<"\nnumber of time steps, nTSteps ="<< endl;
    		cin  >>nTSteps;

    	outFileMaker("m350p1c.out", &dataOutStr);
		dataOutStr <<'#'<<"  CAPACITOR DISCHARGE CALCULATION" << endl<< endl;
		dataOutStr<<"deltaT = "<<deltaT<<";   nTSteps = "<<nTSteps<<";  tau = "<<tau<<endl<< endl;
		dataOutStr<<"Time   Exact Approx"<<endl;
		dataOutStr<<"         Q     Q  "<<endl;
		dataOutStr<<"         |     +  "<<endl;

		//________
		float *tArray, *q1Array, *q2Array;
		tArray = new float [nTSteps];
		q1Array = new float [nTSteps];
   		q2Array = new float [nTSteps];

		char tic;

		for(int i = 0; i <= nTSteps; i++){
			if (i == 0) {
				time = 0;
				q1 = 1;
				q2 = 1;
			}
			else {
				time = i * deltaT;
				deltaQ = -q2 * deltaT/tau;
				q2 += deltaQ;
				q1 = exp(-time/tau);
			}
				
			m1 = int (nCols * q1 + .5);
			m2 = int (nCols * q2 + .5);

			dataOutStr <<setiosflags(ios::fixed)<<setprecision(3)<<setw(6)<<time<<' '<<q1<<' '<<q2<<' ';
			for(int j = 0; j <= nCols; j++){
				tic = ' ';
				if (i == 0) {tic = '.';}
				if (i == nTSteps) {tic = '.';}
				if (j == 0) {tic = '.';}
				if (j == nCols){tic = '.';}
				if (j == m1){tic = '|';}
				if (j == m2){tic = '+';}
				dataOutStr <<tic;
			}
			dataOutStr <<endl;
		}

		goout = query();
	}
};

