#include "peg_pic.h"


/************ Checking of general input parameters ******************/

void chker() {
	int i;

	for (i=0; i<NDP; i++) {
		if (xs_mid[i] > xf_mid[i]) {
			puts("START: Check x_start and x_finish for particle distribution functions!\n");
			exit(1);
		}
		
		if (i) {
			if (xf_mid[i-1] > xs_mid[i]) {
				puts("START: Check x_start and x_finish for particle distribution functions!\n");
				exit(1);
			}
		}
	}

	if (Nnum>3) {
		puts("START: Too many neutrals (Nnum>3)!\n");
		exit(1);
	}

	if (Nnum) {
		/* 
   if (((Nnum==nsp) && (ecollisional)) || (Nnum>nsp))
  {
    puts("START: Number of neutrals (Nnum) larger than number of ions (nsp)\n");    exit(1);
  }
		 */

		for (i=0; i<Nnum; i++) {
			if (icolsp[i]==ecollisional) {
				puts("START: The electrons cannot be neutrals, check icol_X!\n");
				exit(1);
			}

			if ((icolsp[i]<=0)||(icolsp[i]>nsp)) {
				puts("START: Check collided species, icol_X!\n");
				exit(1);
			}

			/*
    for (j=i+1; j<Nnum; j++)
     if (icolsp[i] == icolsp[j])
   {
    puts("START:  Two of collisional ion species are same, check icol_X!\n");
    exit(1);
   }
			 */


			if ((n_neut_L[i]==1 || n_neut_L[i]==2) && (!sourcD) && (!sourcD1)) {
				printf(" no source - no linear or exponential neutral density profiles! \n"); 
				exit(1);
			}

			if ((n_neut_L[i]==3) && (!dshort1) && (2*dlabda[i]>length)) {
				printf(" Check the size of the system in the neutrals (%d+1) file!\n", i);
				exit(1);
			}
		} /* end of for (i=1; i<=Nnum; i++) loop */
	} /* end of if (Nnum) */

	if (extl < 0. || extr < 0. || extc < 0.) {
		puts("START: dont be cute with R,L,C < 0\n");
		exit(1);
	}

	if (!dt) {
		puts("START: dt == 0!!\n");
		exit(1);
	}

	if (ccc > nsp) {
		ccc = nsp;
	}

	if (ccc && (!cc_step)) {
		puts("START: cc_step=0.\n");
		exit(1);
	}

	if ((ccc) && (!n_av)) {
		puts("START: n_av = 0\n");
		exit(1);
	}

	/* Check to see if nfft is an integer power of 2 */
	if(nfft) {
		for(i=0; i<= 22; i++) {
			if(nfft == (1<<i)) {
				i=0;
				break;
			}
		}
		
		if(i) {
			puts("START: nfft is not an integer power of 2");
			exit(1);
		}
	} else {
		puts("START: nfft can not be 0!\n");
		exit(1);
	}

	// why? HISTMAX= nfft;
	// HISTMAX= 1024;

	/* Check to see if HISTMAX is an integer power of 2 */
	if(HISTMAX) {
		for(i=0; i<= 22; i++) {
			if(HISTMAX == (1<<i)) {
				i=0;
				break;
			}
		}
		
		if(i) {
			puts("START: Max-hist. is not an integer power of 2");
			exit(1);
		}
	} else {
		HISTMAX= 2;
	}

	if (reflux) {                       /* No external cirquit */
		extc = 0.;
	}

	if ((sourcD) && (tas)) {
		tasr *=0.01;

		if ((tas) && (tasr>0.99)) {
			puts("START: Too high temperature gradient!");
			exit(1);
		}

		tasr *=2.;                         /* 2 comes from L_s->2xL_s */
	}

	if ((sourcD) && ((nc2<0.)||(nc3>xnc))) {
		puts("START: Check position and size of the first source!");
		exit(1);
	}

	if ((sourcD1) && (tas1)) {
		tasr1 *=0.01;

		if ((tas1) && (tasr1>0.99))	{
			puts("START: Too high temperature gradient!");
			exit(1);
		}

		tasr1 *=2.;                   /* 2 comes from L_s1->2xL_s1 */
	}

	if ((sourcD1) && ((nc21<nc2)||(nc31>nc3))) {
		puts("START: Check position and size of the second source!");
		exit(1);
	}

	for (i=0; i<2; i++) {
		if (poth[i]<0 || poth[i]>=length) {
			puts("START: Check position of potential and temperature history diagnostics");
			exit(1);
		}
	}

} /* End of CHKER */

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