#include "peg_pic.h"

/* Parametrisation: calculation of general parameters used in the simulation */

void param() {
	int i, k;

	xnc= nc;
	ng = nc+1;
	dx = length/nc;
	dttx = dt*dt/dx;
	vscale = dx/dt;
	freq_hi= nfft/2;
	
	if (nfft) {
		df= 1/(nfft*dt);
	}
	
	dshort = fabs(dshort);
	
	if (!dshort) {
		dshort = 0;
	}
	
	theta0 *= TWOPI/360.0;
	psi *= TWOPI/360.0;
	cs = cos(psi);
	sn = sin(psi);
	w0 *= TWOPI;
	epsilon *= EPS0;
	se = dx*dx/epsilon;
	extq_1 = extq_2 = extq_3 = extq;
	interval = 1;
	//if (!slow && !stopfl)  NDP= 0;   
	
	for (i=0; i<NDP; i++) {
		xs_mid[i] /= dx;
		xf_mid[i] /= dx;
	}

	if (ccc) {
		lsq_n = 0.5*log(n_av);
	}
	
	bmag = fabs(bmag);
	
	if (nfft1==0) {
		nfft1= 1;     /* No averaging of "LHS and RHS" parameters */
	}

	for (i=0; i<2; i++) {
		poth[i] /= dx;
		potpos[i] = poth[i];
	}

	if ((bmag>0.) && (fabs(E_y)>0.) && (field)) {
		Ey_flag = 1;     /* for Ey */
	} else {
		Ey_flag = 0;
	}

	/** Deciding which mover to use **/

	if (Ey_flag) {
		x_E /=dx;
		move_p = movebe;
	} else if (bmag>0.) {
		if (field) {
			move_p = moveb;
		} else {
			move_p = moveb0;
		}
	} else if (field) {
		move_p = move1;
	} else {
		move_p = move0;
	}

	/** The first source parameters **/
	Src = fabs(Src);
	nc05 = fabs(nc05);

	if (nc05<1.e-20) {
		// if nc05 (center of source area) is "almost" 0, then it is middle of the system
		nc05 =0.5*nc;
	} else {
		nc05 /= dx;
	}

	if (sourcD) {
		nc2 = nc05 - L_s/dx;
		nc3 = nc05 + L_s/dx;

		if ((nc2<0.) || (nc3>nc)) {
			puts("START: Check the position of the middle, or the size of the first source!");
			exit(1);
		}
		
		if (L_s<dx) {
			printf(" L_s<dx\n");
			exit(1);
		}

		if (sourcD > 0) {
			Src *= 2.*dt*L_s*area/nc2p;  /* dn = dt*Src(input)*2L_s*S */
		} else {
			Src *= 8.*dt*L_s*area/nc2p/TWOPI;
		}
		
		L_s = 2.*L_s/dx;

/*		printf("src= %e\n", Src); */
	/** end Source parameters **/
		
	}else {
		ht = 0;                 /* No source - no heating */
	}

	/** The second source parameters **/
	Src1 = fabs(Src1);
	nc051 = fabs(nc051);
	
	if (nc051<1.e-20) {
		nc051 =0.5*nc;
	} else {
		nc051 /= dx;
	}


	if (sourcD1) {
		nc21 = nc051 - L_s1/dx;
		nc31 = nc051 + L_s1/dx;

		if ((nc21<0.) || (nc31>nc)) {
			puts("START: Check the position of the middle, or the size of the second source!");
			exit(1);
		} if (L_s1<dx) {
			printf(" L_s1<dx\n");
			exit(1);
		}

		if (sourcD1 > 0) {
			Src1 *= 2.*dt*L_s1*area/nc2p;  /* dn = dt*Src(input)*2L_s*S */
		} else {
			Src1 *= 8.*dt*L_s1*area/nc2p/TWOPI;
		}
		
		L_s1 = 2.*L_s1/dx;

		/** end Source parameters **/
	} else {
		ht1 = 0;                 /* No source - no heating */
	}

	/** ch.-netr. particle collision parameters **/
	if (Nnum) {
		icollns = 0;     /* initialization of the ion-neutral collision flag */
		
		for (i=0; i<Nnum; i++) {
			for (k=0; k<Nnum; k++) {
				/* flags for ion-neutral collisions */
				if ((HH[i][k])||(achrgx[i][k]>0.0)||(bchrgx[i][k]>0.0)||(ascat[i][k]>0.0)||(bscat[i][k]>0.0)) {
					icollisional[i][k] = 1;
					icollns =1;
				} else {
					icollisional[i][k] = 0;
				}
			}

			if (n_neut_L[i]==2) {
				if (dlabda[i]>0.)  {
					dlabda[i] = dx/dlabda[i];
				} else {
					printf(" labda = 0\n");
					exit(1);
				}
			}

			if (n_neut_L[i]==3) {
				dlabda[i] /= dx;
			}

		} /* end of for (i=1; <=Nnum; i++) loop */
	/* end of if (Nnum) loop */
	} else {
		ecollisional= 0;
		efi=0;                          /* no effective ionization */
	}

	/** end of ch.-neutr. particle collision parameters **/

	if (slow || stopfl) {
		Ef_flag=0;
		datfile=1;                       /* full diagnostics for ASCII file */
	} else {
		if (Ef_flag) {
			datfile=2;
		} else {
			datfile=3;
		}
	}

	if (slow) {
		stopfl=0;			      /* all profiles are calculated anyway */
	}

	if (stopfl) {
		/* fast run with calculation of profiles in steady state */
		nlast= 0;                       /* initialization of particle number */
		mflag= 0;			      /* no movie */
		//  dwindow= 0;   		      /* no wimdow diagnostics */
		dn /= 100;		      /* dn was in percents */
	}

	if (!datstep) {
		/* if we put datstep=0, then ... */
		datstep= nfft;
		
		if (!datstep) {
			datstep=1000;
		}
	}

	if (!slow && !stopfl) {
		/* if slow=0, no diagnostics */
		nfft = 0;
		T_diag = 0;
	}

	if ((slow || stopfl)&&(T_diag)) qtp= 1; /* All profiles */
	else if (slow || stopfl)        qtp= 2; /* All profiles except T_par-T_perp*/
	else if (mflag)       qtp= 3;  /* V_par, T and Q_total profiles for movies */
	else if (efi)         qtp= 4;  /* T profile for effective ionization */
	else                  qtp= 0;  /* no profiles */


	if(dcramped) {
		if(ramp*dcbias < 0.0) {
			fprintf(stderr, "\nWARNING: Ramp and dcbias have opposite signs.");
			fprintf(stderr, "\nThe external source will be discontineous!\n\n");
		}
		
		if(ramp) {
			risetime= fabs(dcbias)/fabs(ramp);
		} else {
			risetime= PI/w0;
		}
	}

} /* End of PARAM */

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

