#include "peg_pic.h"


/*************    Diagnostics at the wall    ***********************/
/* Calculates particle and energy fluxes at the wall, and energy   */
/* and angular distributions of the absorbed particles.  ***********/
/* "iisp" - type of the particle species, "dum" - particle energy  */
/* "sigmd"= V/Vx, V and Vx are the total and the x-component of    */ 
/* particle velocity, k=0 - LHS wall, k=1 - RHS wall */

void walld(int iisp, float dum, float sigmd, int k) {
	int s;

	if (k==1) {
		/* RHS wall */
		F_tempRHS[iisp]  +=1;
		Efrhs_temp[iisp] += dum;

		dum = (dum - emin[iisp])/de[iisp];
		s = dum;

		if (s<nbin[iisp]-1 && dum>=0) {
			dum -= s;
			feR[iisp][s]  += (!s) ? 2*(1-dum) : 1-dum;
			feR[iisp][s+1]+= (s==nbin[iisp]-2) ? 2*dum : dum;
		}
	} else {
		/* LHS wall */
		F_temp[iisp]  +=1;
		Eflhs_temp[iisp] += dum;

		dum = (dum - emin[iisp])/de[iisp];
		s = dum;
		
		if (s<nbin[iisp]-1 && dum>=0) {
			dum -= s;
			fe[iisp][s]  += (!s) ? 2*(1-dum) : 1-dum;
			fe[iisp][s+1]+= (s==nbin[iisp]-2) ? 2*dum : dum;
		}

		dum = acos(1./sigmd)/dtheta[iisp];
		s = dum;
		dum -= s;
		ftheta[iisp][s]  += (!s) ? 2*(1-dum) : 1-dum;
		ftheta[iisp][s+1]+= (s==nbin[iisp]-2) ? 2*dum : dum;
	}

}  /* END of walld() */

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