#include <math.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>


#define BIT1_VERSION "1.0.5"

#ifndef PI
#define PI		3.141592653589793
#endif
#define TWOPI		6.28318530717959
#define EPS0		8.8542e-12		/* (F/m)  */

#define MAXD            1005      /* max. number of particles per grid */
#define MAXSRF          3         /* max. number secodaries per primaries */
#define VVMAX           2097152   /* max. number of samples for the injection,*/

/* initial and source velocity distributions*/
#define TVMAX		4194304   /* same as above, NOT USED! */
#define NEMAX           4000      /* Number of energy bins */
/* For charged-neutral particle collisions*/

#ifndef max
#define max(x, y)       (((x) > (y)) ? (x) : (y))
#endif

#ifndef min
#define min(x, y)       (((x) < (y)) ? (x) : (y))
#endif

#ifndef DBL_MIN
#define DBL_MIN 	1E-200
#endif

#ifndef True
#define True            1
#endif

#ifndef False
#define False           0
#endif

#if defined(GLOBALORIGIN)
#define GLOBAL
#else
#define GLOBAL extern
#endif

/* struct for values (points) in potential graph */
struct pt {
	int x;		/* cell number */
	double y;	/* value of potential in current cell */
};


/* extra vars; added by J.K. */
int start_step_for_dump;
double *totalLen, *kLen; 
struct pt *phi_six;

GLOBAL int  nsp, nc, ng, nstrt, ecolsp, tstep, dshort1,
ecollisional, hist_hi, thist_hi, freq_hi, interval,
nfft, dcramped, reflux, nsmoothing, T_diag, nlast,
ccc, slow, field, cc_step, efi, efiFL, qtp, stopfl,
sourcD, cappa, datfile, potpos[2], inj_max, icollns, mflag,
dmpstep, Ef_flag, dwindow, last_step, datstep, MAXLEN, tas1,
origdmp, origres, HISTMAX, tas, nfft1, Ey_flag, Nnum, mvstep,
sourcD1, timed, NDP, Nex,
*np, *nbin, *nbin_fvx, *inject, *nstep, *dinj, *i_t,
*n_neut_L, *Hyd, *Hydex, *Hydion, *count_max, *Nsec, *icolsp,
*Index, *wt, *ht_sp, *tsrc, *Nexel,
**srf, **c_omt, **secsp, **sectype, **HH, **icollisional,
***injtype, ***Ldav;


GLOBAL float xnc, nc2p, length, area, rhoback, backj, dde, epsilon, bmag,
extl, extc, w0, dcbias, acbias, extq, extq_1, extq_2, extq_3,
exti, sigma, oldsigma, dx, dt, vxscale, vscale, dttx, se, x_E,
aa, lsq_n, n_av, cc_coef, ht, ht1, E_y, cs, sn, dn, extr,
df, ramp, theta0, jp, b0, risetime, LL_01, pp_01, m_0, m_1,
Src, L_s, nc2, nc3, tasr, nc05, dshort, L_s1, nc21, nc31, nc051,
tasr1, phi_av0, phi_av05, phit1, phit2, psi, poth[2],
Src1,
*rho, *e, *phi, *x_array, *jwall, *t_array, *qm, *q, *m,
*vpart, *vperpt, *vperp0, *tkv, *tx, *tz, *sx, *sz, *emin, *de,
*vpar0, *dtheta, *Escale, *xs_mid, *xf_mid, 
*Src_con, *Stemp, *heat, *F_temp, *F_tempRHS, *FLHS, *FRHS,
*Eflhs_temp, *Efrhs_temp, *Eflhs, *Efrhs, *xs_fvx, *xf_fvx,
*de_fvx, *Qscale, *vx_min, *Ey, *E1y, *fr_E, *pp, *LL,
*Z, *miu, *vspart, *vsperpt, *vspar0, *vsperp0, *phi_fft,
*f_array, *ese_hist, *cur_hist, *com_cur_hist, *cur_fft,
*mphi_fft, *Local_t_array, *Qwscale, *Tlhs, *Trhs, *Tlhs_temp,
*e_ave, *e_ave_show, *phi_ave, *phi_ave_show, *Teq, *Fscale,
*dlabda, *selsmax, *ext0, *elsengy0, *elsengy1, *Trhs_temp,
*elsengy2, *sextmax, *extengy1, *extengy2, *Tscale,
*sionmax, *ionengy0, *ionengy1, *ionengy2, *Nneut0, *Tneut0,
*Halhs, *Harhs, *Halhs_temp, *Harhs_temp, *mn, *Stemp1,
*Vxef, *Vyef, *Vzef, *vspart1, *vsperpt1, *vspar01, *vsperp01,
*Src_con1, *Stemp1, *heat1, *hfsrc, 
*gridref_l, *gridref_r, *gridref_r0, *gridref_rsramp, *gridref_l0, *gridref_lsramp,
**Halhs_hist, **Harhs_hist, **parvels, **sctang, **sctane,
**xd, **vx, **vy, **vz, **srho, **kes_hist, **fe,
**av_e, **enter, **vnpart, **vnperpt, **vnpar0, **vnperp0,
**np_hist, **jwall_hist, **phi_hist, **parvell, **parvelr,
**com_phi_hist, **Nneut, **Tneut, **extengy0,
**ftheta, **e_array, **th_array, 
**srho_ave, **srho_ave_show, **extrate, **ionrate, **Vx, **Vx_ave, 
**Tx, **Vx_ave_show, **Tx_ave, **Tx_ave_show, **F_hist, 
**Vy, **Vz, **feR, **T_dif, **fvx_array, **chrgxrate,
**T_dif_ave, **T_dif_ave_show, **Tx_hist, **T_dif_hist,
**Vxt, **Vpar, **Vpar_ave, **Vpar_ave_show, **F_histRHS, 
**Efrhs_hist, **Eflhs_hist, **Qh, **Qtot, **Qh_ave, **Qtot_ave, 
**Qh_ave_show, **Qtot_ave_show, 
**Tlhs_hist, **Trhs_hist, **rmmcc, **nimiu,
**Tneutshow, **achrgx, **bchrgx, **ascat, **bscat,
**sctang, **gridref_l_hist, **gridref_r_hist, 
***seec, ***Tseec, ***E0, ***Ew, ***f_vpar, ***f_vpar_ave,
***f_vpar_show, ***fe_mid, ***fe_mid_show, ***fe_mid_ave;


GLOBAL double t, **x;  


GLOBAL char  src, dargv[80], dargv1a[80], dargv1[80], dargv2[80], dargv3[80], dargv4[80], dargv5[80], dargv6[80], dargv7[80], dargv8[80];

GLOBAL float alpha_Six, alpha1_Six;

GLOBAL FILE  *InputDeck;


GLOBAL void circuit1(),	circuit2(), circuit3(), circuit4(), (*circuitptr)(),
bincc(), mcc(), history(), adjust(), movebe(), moveb(), fields(),
InitWindows(int, char *[]), neutinp(), move1(), move0(), moveb0(),
(*move_p)(), accum(), mccpar(),
maxwelli(float *, float *, float *, float,float,float,float,int,float,float),
maxwell(float *,float *,float *,float,float,float,float,int,float,float,int),
newvel(float, float, float *, float *, float *, int, int, float),
fmaxwell(float *, float *, float *, float),
vmaxwell0(float *, float *, float *, int),
vmaxwell0(float *, float *, float *, int),
surfphys(int, float, float, int), 
tcrossec(float *, char *, int, float),
dec1(char *, int *, int, int, int, int, int), 
Escat(float *, float *, int),
display_title(), innam(int, char *[]), rgen(char *[]),
chker(), param(), alsp(), species(int *, float *[2], int),
specpar(int, float *[2]), srfpar(int), ccpar(int), load(int []),
start(int, char *[]), dfa(), incirq(), memfree(),
walld(int, float, float, int), avq(int, int), 
One_2_One(float *, int, int), movf(char *, int, int),
One_2_One_d(float *, int, int), avt1(), avt2(), avt(int *), newhist(),
chist(float *), freqanalysis(), prtest(float *, int),
rdist(float, float, int, float *, int, float), 
secdist(float *, float *, float *, int),
secinj(int, int, int, float *, float, int),
gamma_ee(float *, float, float, float),
gamma_ie(float *, float, float, float);


GLOBAL float frand(), bc1(), bc2(), bc3(), bc4(), (*bcptr)(), Ep(int, int),
efsigma(float,float), (*fcrs)(float), sigma1(int, float),
sigma2(int, float, int), sigma3(int, float), 
sigma4(int, int, float), sigma5(int, int, float);


/**************** Functions for atomic data *********************/

GLOBAL float forig_el(float, int), forig_ex(float, int), feh_i(float), 
forig_i(float, int), feh_ecr(float);   


void Dump( char *filename);
void DumpCoords( char *fnbase, char *fnbaseAll, int count) ;
void cridatfile(int datfile, char *datfilename, char *datfilenameAll, int currNum);
void psrc1();
void psrc();
void heatup(int iisp, int j, int k) ;
void heatup1(int iisp, int j, int k);
void vmaxwell1(float *vx, float *vy, float *vz, int iisp);
void four1(float [], int, int);           
void realft(float [], int, int); 
void Restore( char *filename);
double revers(unsigned int num);

void freespace();

void usage(int tools, int argc, char *argv[], int toolsHelp);


#ifdef WITH_MT_RANDOM_GENERATOR	

void init_genrand(unsigned long s);
void init_by_array(unsigned long init_key[], int key_length);
unsigned long genrand_int32(void);
long genrand_int31(void);
double genrand_real1(void);
double genrand_real2(void);
double genrand_real3(void);
double genrand_res53(void);

#endif



#undef GLOBAL


