#include "peg_pic.h"


/********** Calculate nfft1 time steps averaged values *********/
/*** These are time histories of different values      *********/
/*** All quantities, except the potential, are accumulated in **/
/*** other routines. Thus, no accumulation (summation) over ****/
/*** nfft1 time steps is done here ***/

void avt1() {
	int isp;

	if (!tstep) {
		/* initialization */
		for(isp=0; isp<nsp; isp++) {
			F_temp[isp]= FLHS[isp]=0.;
			Eflhs_temp[isp]= Eflhs[isp]=0.;
			F_tempRHS[isp]= FRHS[isp]=0.;
			Efrhs_temp[isp]= Efrhs[isp]=0.;
			Tlhs[isp] = Tlhs_temp[isp];
			Trhs[isp] = Trhs_temp[isp];
			Tlhs_temp[isp]=0.;
			Trhs_temp[isp]=0.;
		}

		for(isp=0; isp<Nnum; isp++) {
			Halhs_temp[isp]=Halhs[isp] =0.;
			Harhs_temp[isp]=Harhs[isp] =0.;
		}

		phi_av0 =phi[potpos[0]];
		phi_av05 =phi[potpos[1]];
		phit1 =0.;;
		phit2 =0.;
	} else if (nfft1==1) {
		for(isp=0; isp<nsp; isp++) {
			FLHS[isp]=F_temp[isp]*Fscale[isp];
			Eflhs[isp]=Eflhs_temp[isp]*Qwscale[isp];
			FRHS[isp]=F_tempRHS[isp]*Fscale[isp];
			Efrhs[isp]=Efrhs_temp[isp]*Qwscale[isp];
			Tlhs[isp] = Tlhs_temp[isp];
			Trhs[isp] = Trhs_temp[isp];
		}

		for(isp=0; isp<Nnum; isp++) {
			Halhs[isp] = Halhs_temp[isp];
			Harhs[isp] = Harhs_temp[isp];
		}

		phi_av0 =phi[potpos[0]];
		phi_av05 =phi[potpos[1]];
	} else if (!(tstep%nfft1)) {
		for(isp=0; isp<nsp; isp++) {
			FLHS[isp]=F_temp[isp]*Fscale[isp];
			Eflhs[isp]=Eflhs_temp[isp]*Qwscale[isp];
			FRHS[isp]=F_tempRHS[isp]*Fscale[isp];
			Efrhs[isp]=Efrhs_temp[isp]*Qwscale[isp];
			Tlhs[isp] = Tlhs_temp[isp]/nfft1;
			Trhs[isp] = Trhs_temp[isp]/nfft1;
			F_temp[isp]=0.;
			Eflhs_temp[isp]=0.;
			F_tempRHS[isp]=0.;
			Efrhs_temp[isp]=0.;
			Tlhs_temp[isp]=0.;
			Trhs_temp[isp]=0.;
		}

		for(isp=0; isp<Nnum; isp++) {
			Halhs[isp] = Halhs_temp[isp]/nfft1;
			Harhs[isp] = Harhs_temp[isp]/nfft1;
			Halhs_temp[isp] =0.;
			Harhs_temp[isp] =0.;
		}

		phi_av0 =phit1/nfft1;
		phi_av05 =phit2/nfft1;
		phit1=0.;
		phit2=0.;
	} else {
		phit1 +=phi[potpos[0]];
		phit2 +=phi[potpos[1]];
	}

}   /* END of avt1() */

/*************************************************************************/
/* Calculate nfft1 time steps averaged values (faster version "slow"=0) */
/* In the present stage practically same as avt1()!!!                   */

void avt2() {
	int isp;

	if (!tstep) {
		/* initialization */
		for(isp=0; isp<nsp; isp++) {
			F_temp[isp]=FLHS[isp]=0.;
			F_tempRHS[isp]=FRHS[isp]=0.;

			if (Ef_flag) {     
				Eflhs_temp[isp]=Eflhs[isp]=0.;
				Efrhs_temp[isp]=Efrhs[isp]=0.;
				Tlhs[isp] = Tlhs_temp[isp];
				Trhs[isp] = Trhs_temp[isp];
				Tlhs_temp[isp]=0.;
				Trhs_temp[isp]=0.;
			}
		}

		if (Ef_flag) {
			for(isp=0; isp<Nnum; isp++) {
				Halhs_temp[isp]=Halhs[isp] =0.;
				Harhs_temp[isp]=Harhs[isp] =0.;
			}
		}

		phi_av0 =phi[potpos[0]];
		phi_av05 =phi[potpos[1]];
		phit1 =0.;;
		phit2 =0.;
	} else if (!(tstep%nfft1)) {
		for(isp=0; isp<nsp; isp++) {
			FLHS[isp]=F_temp[isp]*Fscale[isp];
			FRHS[isp]=F_tempRHS[isp]*Fscale[isp];
			F_temp[isp]=0;
			F_tempRHS[isp]=0;

			if (Ef_flag) {
				Eflhs[isp]=Eflhs_temp[isp]*Qwscale[isp];
				Efrhs[isp]=Efrhs_temp[isp]*Qwscale[isp];
				Tlhs[isp] = Tlhs_temp[isp]/nfft1;
				Trhs[isp] = Trhs_temp[isp]/nfft1;
				Eflhs_temp[isp]=0;
				Efrhs_temp[isp]=0;
				Tlhs_temp[isp]=0.;
				Trhs_temp[isp]=0.;
			}
		}

		if (Ef_flag) {
			for(isp=0; isp<Nnum; isp++) {
				Halhs[isp] = Halhs_temp[isp]/nfft1;
				Harhs[isp] = Harhs_temp[isp]/nfft1;
				Halhs_temp[isp] =0.;
				Harhs_temp[isp] =0.;
			}
		}

		phi_av0 =phit1/nfft1;
		phi_av05 =phit2/nfft1;
		phit1=0.;
		phit2=0.;
		/* end of if(!(tstep... */
	} else {
		phit1 +=phi[potpos[0]];
		phit2 +=phi[potpos[1]];
	}
}   /* END of avt2() */

/***************************************************************/

