#include "peg_pic.h"


/** Allocate space and initialize distribution functions **/

void dfa() {
	int isp, i, s;

	fe     = (float  **)malloc(nsp*sizeof(float *));
	feR    = (float  **)malloc(nsp*sizeof(float *));
	e_array= (float  **)malloc(nsp*sizeof(float *));
	ftheta  = (float **)malloc(nsp*sizeof(float *));
	th_array= (float **)malloc(nsp*sizeof(float *));
	fe_mid     = (float ***)malloc(nsp*sizeof(float **));
	fe_mid_ave = (float ***)malloc(nsp*sizeof(float **));
	fe_mid_show= (float ***)malloc(nsp*sizeof(float **));
	fvx_array= (float **)malloc(nsp*sizeof(float *));
	f_vpar   = (float ***)malloc(nsp*sizeof(float **));
	f_vpar_ave = (float ***)malloc(nsp*sizeof(float **));
	f_vpar_show= (float ***)malloc(nsp*sizeof(float **));

	for(isp=0; isp<nsp; isp++) {
		fe[isp]      = (float *)malloc(nbin[isp]*sizeof(float));
		feR[isp]     = (float *)malloc(nbin[isp]*sizeof(float));
		ftheta[isp]  = (float *)malloc(nbin[isp]*sizeof(float));
		th_array[isp]= (float *)malloc(nbin[isp]*sizeof(float));
		e_array[isp] = (float *)malloc(nbin[isp]*sizeof(float));
		fvx_array[isp]= (float *)malloc(nbin_fvx[isp]*sizeof(float));

		if (NDP) {
			fe_mid[isp]     = (float **)malloc(NDP*sizeof(float *));
			fe_mid_ave[isp] = (float **)malloc(NDP*sizeof(float *));
			fe_mid_show[isp]= (float **)malloc(NDP*sizeof(float *));
			f_vpar[isp]     = (float **)malloc(NDP*sizeof(float *));
			f_vpar_ave[isp] = (float **)malloc(NDP*sizeof(float *));
			f_vpar_show[isp]= (float **)malloc(NDP*sizeof(float *));
		}
	}

	for(isp=0; isp<nsp; isp++) {
		for (i=0; i<NDP; i++) {
			fe_mid[isp][i]     = (float *)malloc(nbin[isp]*sizeof(float));
			fe_mid_ave[isp][i] = (float *)malloc(nbin[isp]*sizeof(float));
			fe_mid_show[isp][i]= (float *)malloc(nbin[isp]*sizeof(float));
			f_vpar[isp][i]     = (float *)malloc(nbin_fvx[isp]*sizeof(float));
			f_vpar_ave[isp][i] = (float *)malloc(nbin_fvx[isp]*sizeof(float));
			f_vpar_show[isp][i] = (float *)malloc(nbin_fvx[isp]*sizeof(float));
		}
	}

	if (NDP && nsp && (!f_vpar_show[nsp-1][NDP-1])) {
		puts("START: f(vpar) malloc failed");
		exit(1);
	}

	for(isp=0; isp<nsp; isp++) {
		for(i=0; i<nbin[isp]; i++) {
			fe[isp][i]      = 0.0;
			feR[isp][i]     = 0.0;
			ftheta[isp][i]  = 0.0;
			th_array[isp][i]= i*dtheta[isp];
		}

		for (s=0; s<NDP; s++) {
			for(i=0; i<nbin[isp]; i++) {
				fe_mid[isp][s][i] = fe_mid_ave[isp][s][i] = fe_mid_show[isp][s][i] = 0.0;
				
				if (s==NDP-1) {
					e_array[isp][i] = emin[isp] +i*de[isp];
				}
			}

			for(i=0; i<nbin_fvx[isp]; i++) {
				f_vpar[isp][s][i] = f_vpar_ave[isp][s][i] = f_vpar_show[isp][s][i] =0.0;
				
				if (s==NDP-1){
					fvx_array[isp][i]= vx_min[isp] + (i+.5)*de_fvx[isp];
				}
			}
		}
	}  // end of the for(isp=0; isp<nsp; isp++) loop
} /* End of DFA */

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