/*
*                   TO CALCULATE SERIES LRC CURRENT
*/

#include"lib351.h"

void main(){

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

    	ofstream dataOutStr;

		int imRows = 101;
		int imColumns = 73;
		int imColumnsZ = imColumns/2;
		int nTSteps, m;
		float r, c, l, v0, frequency, deltaT;
		float q, q1, time, current, current1, appliedVoltage, appliedVoltage1;
		float dTimeCurr, dTimeCurr1;
		char tic;

		//____DATA_INPUT____

		cout <<"Please supply values:"<< endl;
		cout <<"\nresistance, R ="<< endl;
			cin  >>r;
		cout <<"\ninductance, L ="<< endl;
			cin  >>l;
    	cout <<"\ncapacitance, C ="<< endl;
			cin  >>c;
   		cout <<"\ntime step, deltaT ="<< endl;
			cin  >>deltaT;
  	   	cout <<"\nnumber of time steps, nTSteps ="<< endl;
			cin  >>nTSteps;
  		cout <<"\namplitude of applied voltage, v0="<< endl;
			cin  >>v0;
		cout <<"\nfrequency of applied voltage, frequency ="<< endl;
			cin  >>frequency;

        outFileMaker("m351p1c.out", &dataOutStr);
		dataOutStr<<'#'<<"   SERIES LRC CURRENT CALCULATION"<<endl;
        dataOutStr<<"R = "<<r<<""<<endl;
        dataOutStr<<"L = "<<l<<""<<endl;
        dataOutStr<<"C = "<<c<<""<<endl;
        dataOutStr<<"deltaT = "<<deltaT<<""<<endl;
        dataOutStr<<"v0 = "<<v0<<""<<endl;
        dataOutStr<<"frequency = "<<frequency<<""<<endl<<endl;
        dataOutStr<<" Time  Current"<<endl;

		//___LOOP_ON_TIME____

		for(int i = 0; i <= nTSteps; i++){

			if (i == 0) {
				time = 0.0;
				q = 1.0;
				current = 0.0;
			}
			else {
				appliedVoltage = v0 * sin(frequency * time);
				dTimeCurr = (1./l)*(appliedVoltage - current * r - q/c);
				q1 = q + current * deltaT;
				current1 = current + dTimeCurr * deltaT;
				appliedVoltage1 = v0 * sin(frequency * (current + deltaT));
				dTimeCurr1 = (1./l)*(appliedVoltage1 - current1 * r - q1/c);
				q += (current + current1)*deltaT/2;
				current += (dTimeCurr + dTimeCurr1)*deltaT/2;
				time += deltaT;
			}
			m = int(imColumnsZ + imColumns * current + 0.5);
			dataOutStr <<setiosflags(ios::fixed)<<setw(7)<<setprecision(3)<<time<<' '<<setw(6)<<setprecision(3)<<current<<' ';
			for (int j = 0; j <= imColumns; j++) {
				tic = ' ';
				if (i == 0) {tic = '.';}
				if (i == nTSteps) {tic = '.';}
				if (j == 0) {tic = '.';}
				if (j == imColumns) {tic = '.';}
				if (j == imColumnsZ) {tic = '|';}
				if (j == m) {tic = '*';}
				dataOutStr <<tic;
			}
			dataOutStr <<endl;
		}
		goout = query();
	}
};


