#include "peg_pic.h"


/****************** Injection of secondary paricle *******************/
/** "ipr" and "isp" are the primary and secondary particle types,   **/
/** respectively. "itp" is the injection type, "E" is the energy    **/
/** of the primary particle, "vt" is the thermal velocity of the    **/
/** secondary part. and "k" denotes LHS (=0) and RHS (k=1) walls.   **/
/** Function criates sec. particle with given velocity distribution **/
/** and with energy smaller than E_primary - E_binding **/

void secinj(int ipr, int isp, int itp, float *E, float svt, int k) {
	int   i;
	float Epr, Esec, Erel;

	Epr = *E;
	i = np[isp];
	np[isp]++;

	secdist(&vx[isp][i], &vy[isp][i], &vz[isp][i], itp);

	Esec = Ep(isp, i);
	Erel = Escale[isp]/Escale[ipr];
	Epr -= (Esec*svt*svt*Erel);

	if (Epr >0.) {
		*E = Epr;
		vx[isp][i] *= svt;
		vy[isp][i] *= svt;
		vz[isp][i] *= svt;
	} else  {
		/* if E_primary < E_sec., E_sec = Random*E_primary */
		Epr = (*E)*frand();
		*E -= Epr;
		Esec = sqrt(Epr/Esec/Erel);
		vx[isp][i] *= Esec;
		vy[isp][i] *= Esec;
		vz[isp][i] *= Esec;
	}

	if (!k) {
		x[isp][i] = frand()*vx[isp][i];
	} else {
		vx[isp][i] = -vx[isp][i];
		x[isp][i] = xnc + frand()*vx[isp][i];
	}
}
/* END of secinj() */

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