#include "peg_pic.h"

/** reading input, initialization, space allocation **/

void start(int iargc, char *pargv[]) {
	float *fj0[2];
	int i, isp, *initnp;
	extern double revers();
	char datext[]=".dat", dmpext[]=".dmp", coordsext[]="-ph_space.dat";

	innam(iargc, pargv);        /* initialize parameters */
	rgen(pargv);                /* read general input parameters */
	chker();                    /* checking general parameters */
	param();                    /* parameterization: dimen.less parameters, ... */
	alsp();                     /* allocate space main parameters */
	
	printf("nsp = %d\n", nsp);

	/** creating dmp, txt and dat file names ***/
	strcpy(dargv, pargv[1]);
	strcpy(dargv1, pargv[1]);
	strcpy(dargv1a, pargv[1]);
	strcpy(dargv2, pargv[1]);
	strcpy(dargv4, pargv[1]);
	strcpy(dargv5, pargv[1]);
	strcpy(dargv6, pargv[1]);
	strcpy(dargv7, pargv[1]);
	strcpy(dargv8, pargv[1]);		/* for writing current time step */
	
	if( origdmp == 3 ) {
		/* if selected by input file, write detailed dat files and keep history of dat and dmp files */
		/* add format for displaying numbers inside file names */
		strcat(dargv, "-%06d");
		strcat(dargv1, "-%06d-%s");
		strcat(dargv1a, "-%06d-all");
		strcat(dargv5, "-%06d");
		strcat(dargv6, "-sp%02d-%06d");
		strcat(dargv7, "-sp%02d-all");
	} else if( origdmp == 5 ) {
		/* if selected by input file, write detailed dat files and keep history of dat and dmp files */
		/* add format for displaying numbers inside file names */
		strcat(dargv1, "-%06d-%s");
	}
		
	/* add also extensions */
	strcat(dargv, dmpext);
	strcat(dargv1, datext);
	strcat(dargv1a, datext);
	strcat(dargv4, ".src");
	strcat(dargv5, ".pot");
	strcat(dargv6, coordsext);
	strcat(dargv7, coordsext);
	strcat(dargv8, "-current-timestep");

	
	initnp= (int *)malloc(nsp*sizeof(int));

	for (i=0; i<2; ++i) {
		fj0[i]= (float *)malloc(nsp*sizeof(float));
	}

	for (isp=0; isp<nsp; ++isp) {
		/* Set up the parameters for each "species" */
		species(initnp, fj0, isp);  /* read input and save parameters */
		specpar(isp, fj0);          /* calculate and normalize parameters */
	}

	fclose(InputDeck);

	if (Nnum) {
		neutinp();          /* input for neutrals */
	}
	
	vxscale *= 5;

	if (slow || stopfl) {
		dfa();               /* allocate space and initialize the dist. functions */
	}
	
	incirq();                  /* initializing of circuit parameters */

	Index = (int *)malloc(VVMAX*sizeof(int));;
	
	for (i=0; i<VVMAX; i++) {
		/* "random" number between 0 and 2097152  */
		Index[i] = VVMAX*revers(i);
	}

	if(iargc > 2 && !access(pargv[2], F_OK)) {
		/* start from a dump file if given */                                                                                
		Restore(pargv[2]);
	} else {
		load(initnp);         /* otherwise load with the initial distributions */                                                                                
	}
	
	memfree();               /* free memory if necessary */

}   /* End of START */
