// pcwaveguide3dfdtd.cpp : main project file.

/*This file was copied from waveguide3dfdtd on 11/17/07 and was modified to allow
to show the effects of a photonic crystal as the guiding walls in 2D*/

#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <complex.h>
#include <assert.h>
#include <limits.h>

#define IE 150  
#define JE 200
#define KE 100 //propagation direction
#define ia 7
#define ja 7
#define ka 7
#define	epsz 8.85e-12   //permittivity of free space
#define	pi 3.14159		
#define	eps 12.9		//relative permittivity of GaAs
#define	cond 0.000000   //loss term for dielectric material... assumed 0 for GaAs
#define	c 3.0e8			//speed of light in free space

void createPML(float npml, float*gi1, float*gi2, float*gi3, float*gj1, float*gj2, float*gj3, float*gk1, float*gk2, float*gk3, float*fi1, float*fi2, float*fi3, float*fj1, float*fj2, float*fj3, float*fk1, float*fk2, float*fk3);
void defineDielectric(float dt, int height, int width, float ***gax, float ***gbx, float ***gay, float ***gby, float ***gaz, float ***gbz, char *guide_type);
void calcDx(int ib, int ixh, float *** dx, float ***hy, float ***hz, float*** idxl, float ***idxh, float curl_h, float *gi1, float *gj3, float *gk3, float *gj2, float *gk2);
void calcDy(int jb, int jyh, float *** dy, float ***hx, float ***hz, float*** idyl, float ***idyh, float curl_h, float *gj1, float *gi3, float *gk3, float *gi2, float *gk2);
void calcDz(int kb, int kzh, float *** dz, float ***hx, float ***hy, float*** idzl, float ***idzh, float curl_h, float *gk1, float *gi3, float *gj3, float *gi2, float *gj2);
void inputSource(float ***dz, int ic, int jc, int kc, int T, float height, float width, float dt, float *parameters);
void calcEx(float ***ex, float ***dx, float ***gax, float ***gbx, float ***ix);
void calcEy(float ***ey, float ***dy, float ***gay, float ***gby, float ***iy);
void calcEz(float ***ez, float ***dz, float ***gaz, float ***gbz, float ***iz);
void calcHx(float ***hx, float***ey, float***ez, float***ihxl, float***ihxh, float curl_e, float *fj3, float *fk3, float*fj2, float *fk2, float *fi1, int ixh, int ib);
void calcHy(float ***hy, float***ex, float***ez, float***ihyl, float***ihyh, float curl_e, float *fi3, float *fk3, float*fi2, float *fk2, float *fj1, int jyh, int jb);
void calcHz(float ***hz, float***ex, float***ey, float***ihzl, float***ihzh, float curl_e, float *fi3, float *fj3, float*fi2, float *fj2, float *fk1, int kzh, int kb);
void printFile(float T, int Tcalc, float ***ez, char *file_name, char * s1, char *s2, char *my_dest, float width, float height, int constSpot);
void printIndFile(int row_max, int colmn_max, float *** var, char * file_name, int dimConst, int constSpot);
void createCircle(double zc, double yc, float height, double radius, double dt, float *** p_ga, float *** p_gb);


int main( int argc, char *argv[] )//int main(array<System::String ^> ^args)
{

	//BEGIN Declare all of the variables
	int l,n,i,j,k,ic,jc,kc,nsteps;
	float lambda, ddx, dt, epsilon, sigma, eaf, npml, T;  //wavelength, spacial step size, time step size, permittivity, loss, time step 
	float height, width;  //waveguide parameters (physical dimension in # of spacial steps)
	float curl_e;
	int ib, jb, kb;
	int Tcalc = 0; 
	FILE *fpinput;  //variable for input file containing parameters
	char stringin[30];  //used to store text from input file
	char guide_type[15];	//stores which type of waveguide... rectangular/photonic crystal/cavity
	char input_type[30];	//stores which type of input...  Gaussian wave packet or Gaussian in space
	float t0, spread, pulse;  //parameters for Gaussian in time 
	float pulseHeight, pulseWidth, pulseFreq, position;   //more parameters for Gaussians
	float divisions;  //number of spacial steps in 1 wavelength
	FILE *fp;		//variable for output file containing E and H field data


	/*Declare all variables that need allocation  These are 3D arrays!
	gax, gay, gaz, dx, dy, dz, ex, ey, ez, hx, hy, hz, ix, iy, iz, gbx, gby, gbz, ihx, ihy,...*/
	
	//Electric Flux Terms
	float ***dx;
	dx = (float ***) malloc(IE * sizeof(float **));
	assert(dx != NULL);
	for (i = 0; i < IE; i++)
	{
		dx[i] = (float **) malloc(JE * sizeof(float *));
		assert(dx != NULL);
		for(j = 0; j < JE; j++)
			dx[i][j] = (float *) malloc(KE * sizeof(float));
	}

	float ***dy;
	dy = (float ***) malloc(IE * sizeof(float **));
	assert(dy != NULL);
	for (i = 0; i < IE; i++)
	{
		dy[i] = (float **) malloc(JE * sizeof(float *));
		assert(dy != NULL);
		for(j = 0; j < JE; j++)
			dy[i][j] = (float *) malloc(KE * sizeof(float));
	}

	float ***dz;
	dz = (float ***) malloc(IE * sizeof(float **));
	assert(dz != NULL);
	for (i = 0; i < IE; i++)
	{
		dz[i] = (float **) malloc(JE * sizeof(float *));
		assert(dz != NULL);
		for(j = 0; j < JE; j++)
			dz[i][j] = (float *) malloc(KE * sizeof(float));
	}

	//Electric Field Terms
	float ***ex;
	ex = (float ***) malloc(IE * sizeof(float **));
	assert(ex != NULL);
	for (i = 0; i < IE; i++)
	{
		ex[i] = (float **) malloc(JE * sizeof(float *));
		assert(ex != NULL);
		for(j = 0; j < JE; j++)
			ex[i][j] = (float *) malloc(KE * sizeof(float));
	}

	float ***ey;
	ey = (float ***) malloc(IE * sizeof(float **));
	assert(ey != NULL);
	for (i = 0; i < IE; i++)
	{
		ey[i] = (float **) malloc(JE * sizeof(float *));
		assert(ey != NULL);
		for(j = 0; j < JE; j++)
			ey[i][j] = (float *) malloc(KE * sizeof(float));
	}

	float ***ez;
	ez = (float ***) malloc(IE * sizeof(float **));
	assert(ez != NULL);
	for (i = 0; i < IE; i++)
	{
		ez[i] = (float **) malloc(JE * sizeof(float *));
		assert(ez != NULL);
		for(j = 0; j < JE; j++)
			ez[i][j] = (float *) malloc(KE * sizeof(float));
	}

	//Magnetic Field Terms
	float ***hx;
	hx = (float ***) malloc(IE * sizeof(float **));
	assert(hx != NULL);
	for (i = 0; i < IE; i++)
	{
		hx[i] = (float **) malloc(JE * sizeof(float *));
		assert(hx != NULL);
		for(j = 0; j < JE; j++)
			hx[i][j] = (float *) malloc(KE * sizeof(float));
	}

	float ***hy;
	hy = (float ***) malloc(IE * sizeof(float **));
	assert(hy != NULL);
	for (i = 0; i < IE; i++)
	{
		hy[i] = (float **) malloc(JE * sizeof(float *));
		assert(hy != NULL);
		for(j = 0; j < JE; j++)
			hy[i][j] = (float *) malloc(KE * sizeof(float));
	}

	float ***hz;
	hz = (float ***) malloc(IE * sizeof(float **));
	assert(hz != NULL);
	for (i = 0; i < IE; i++)
	{
		hz[i] = (float **) malloc(JE * sizeof(float *));
		assert(hz != NULL);
		for(j = 0; j < JE; j++)
			hz[i][j] = (float *) malloc(KE * sizeof(float));
	}

	//Intermediate Values for calculating the electric field from electric flux
	float ***ix;
	ix = (float ***) malloc(IE * sizeof(float **));
	assert(ix != NULL);
	for (i = 0; i < IE; i++)
	{
		ix[i] = (float **) malloc(JE * sizeof(float *));
		assert(ix != NULL);
		for(j = 0; j < JE; j++)
			ix[i][j] = (float *) malloc(KE * sizeof(float));
	}

	float ***iy;
	iy = (float ***) malloc(IE * sizeof(float **));
	assert(iy != NULL);
	for (i = 0; i < IE; i++)
	{
		iy[i] = (float **) malloc(JE * sizeof(float *));
		assert(iy != NULL);
		for(j = 0; j < JE; j++)
			iy[i][j] = (float *) malloc(KE * sizeof(float));
	}

	float ***iz;
	iz = (float ***) malloc(IE * sizeof(float **));
	assert(iz != NULL);
	for (i = 0; i < IE; i++)
	{
		iz[i] = (float **) malloc(JE * sizeof(float *));
		assert(iz != NULL);
		for(j = 0; j < JE; j++)
			iz[i][j] = (float *) malloc(KE * sizeof(float));
	}

	//For dielectric
	float ***gax;
	gax = (float ***) malloc(IE * sizeof(float **));
	assert(gax != NULL);
	for (i = 0; i < IE; i++)
	{
		gax[i] = (float **) malloc(JE * sizeof(float *));
		assert(gax != NULL);
		for(j = 0; j < JE; j++)
			gax[i][j] = (float *) malloc(KE * sizeof(float));
	}

	float ***gay;
	gay = (float ***) malloc(IE * sizeof(float **));
	assert(gay != NULL);
	for (i = 0; i < IE; i++)
	{
		gay[i] = (float **) malloc(JE * sizeof(float *));
		assert(gay != NULL);
		for(j = 0; j < JE; j++)
			gay[i][j] = (float *) malloc(KE * sizeof(float));
	}

	float ***gaz;
	gaz = (float ***) malloc(IE * sizeof(float **));
	assert(gaz != NULL);
	for (i = 0; i < IE; i++)
	{
		gaz[i] = (float **) malloc(JE * sizeof(float *));
		assert(gaz != NULL);
		for(j = 0; j < JE; j++)
			gaz[i][j] = (float *) malloc(KE * sizeof(float));
	}

	float ***gbx;
	gbx = (float ***) malloc(IE * sizeof(float **));
	assert(gbx != NULL);
	for (i = 0; i < IE; i++)
	{
		gbx[i] = (float **) malloc(JE * sizeof(float *));
		assert(gbx != NULL);
		for(j = 0; j < JE; j++)
			gbx[i][j] = (float *) malloc(KE * sizeof(float));
	}

	float ***gby;
	gby = (float ***) malloc(IE * sizeof(float **));
	assert(gby != NULL);
	for (i = 0; i < IE; i++)
	{
		gby[i] = (float **) malloc(JE * sizeof(float *));
		assert(gby != NULL);
		for(j = 0; j < JE; j++)
			gby[i][j] = (float *) malloc(KE * sizeof(float));
	}

	float ***gbz;
	gbz = (float ***) malloc(IE * sizeof(float **));
	assert(gbz != NULL);
	for (i = 0; i < IE; i++)
	{
		gbz[i] = (float **) malloc(JE * sizeof(float *));
		assert(gbz != NULL);
		for(j = 0; j < JE; j++)
			gbz[i][j] = (float *) malloc(KE * sizeof(float));
	}

	//id and ih variables are used for the PML
	//idxl and idxh
	float ***idxl;
	idxl = (float ***) malloc(ia * sizeof(float **));
	assert(idxl != NULL);
	for (i = 0; i < ia; i++)
	{
		idxl[i] = (float **) malloc(JE * sizeof(float *));
		assert(idxl != NULL);
		for(j = 0; j < JE; j++)
			idxl[i][j] = (float *) malloc(KE * sizeof(float));
	}

	float ***idxh;
	idxh = (float ***) malloc(ia * sizeof(float **));
	assert(idxh != NULL);
	for ( i = 0; i < ia; i++)
	{
		idxh[i] = (float **) malloc(JE * sizeof(float *));
		assert(idxh != NULL);
		for( j = 0; j < JE; j++)
			idxh[i][j] = (float *) malloc(KE * sizeof(float));
	}

	//ihxl and ihxh
	float ***ihxl;
	ihxl = (float ***) malloc(ia * sizeof(float **));
	assert(ihxl != NULL);
	for ( i = 0; i < ia; i++)
	{
		ihxl[i] = (float **) malloc(JE * sizeof(float *));
		assert(ihxl != NULL);
		for( j = 0; j < JE; j++)
			ihxl[i][j] = (float *) malloc(KE * sizeof(float));
	}

	float ***ihxh;
	ihxh = (float ***) malloc(ia * sizeof(float **));
	assert(ihxh != NULL);
	for ( i = 0; i < ia; i++)
	{
		ihxh[i] = (float **) malloc(JE * sizeof(float *));
		assert(ihxh != NULL);
		for( j = 0; j < JE; j++)
			ihxh[i][j] = (float *) malloc(KE * sizeof(float));
	}

	//idyl and idyh
	float ***idyl;
	idyl = (float ***) malloc(IE * sizeof(float **));
	assert(idyl != NULL);
	for ( i = 0; i < IE; i++)
	{
		idyl[i] = (float **) malloc(ja * sizeof(float *));
		assert(idyl != NULL);
		for( j = 0; j < ja; j++)
			idyl[i][j] = (float *) malloc(KE * sizeof(float));
	}

	float ***idyh;
	idyh = (float ***) malloc(IE * sizeof(float **));
	assert(idyh != NULL);
	for ( i = 0; i < IE; i++)
	{
		idyh[i] = (float **) malloc(ja * sizeof(float *));
		assert(idyh != NULL);
		for( j = 0; j < ja; j++)
			idyh[i][j] = (float *) malloc(KE * sizeof(float));
	}

	//ihyl and ihyh
	float ***ihyl;
	ihyl = (float ***) malloc(IE * sizeof(float **));
	assert(ihyl != NULL);
	for ( i = 0; i < IE; i++)
	{
		ihyl[i] = (float **) malloc(ja * sizeof(float *));
		assert(ihyl != NULL);
		for( j = 0; j < ja; j++)
			ihyl[i][j] = (float *) malloc(KE * sizeof(float));
	}

	float ***ihyh;
	ihyh = (float ***) malloc(IE * sizeof(float **));
	assert(ihyh != NULL);
	for ( i = 0; i < IE; i++)
	{
		ihyh[i] = (float **) malloc(ja * sizeof(float *));
		assert(ihyh != NULL);
		for( j = 0; j < ja; j++)
			ihyh[i][j] = (float *) malloc(KE * sizeof(float));
	}

	//idzl and idzh
	float ***idzl;
	idzl = (float ***) malloc(IE * sizeof(float **));
	assert(idzl != NULL);
	for ( i = 0; i < IE; i++)
	{
		idzl[i] = (float **) malloc(JE * sizeof(float *));
		assert(idzl != NULL);
		for( j = 0; j < JE; j++)
			idzl[i][j] = (float *) malloc(ka * sizeof(float));
	}

		float ***idzh;
	idzh = (float ***) malloc(IE * sizeof(float **));
	assert(idzh != NULL);
	for ( i = 0; i < IE; i++)
	{
		idzh[i] = (float **) malloc(JE * sizeof(float *));
		assert(idzh != NULL);
		for( j = 0; j < JE; j++)
			idzh[i][j] = (float *) malloc(ka * sizeof(float));
	}

	//ihzl and ihzh
	float ***ihzl;
	ihzl = (float ***) malloc(IE * sizeof(float **));
	assert(ihzl != NULL);
	for ( i = 0; i < IE; i++)
	{
		ihzl[i] = (float **) malloc(JE * sizeof(float *));
		assert(ihzl != NULL);
		for( j = 0; j < JE; j++)
			ihzl[i][j] = (float *) malloc(ka * sizeof(float));
	}

	float ***ihzh;
	ihzh = (float ***) malloc(IE * sizeof(float **));
	assert(ihzh != NULL);
	for ( i = 0; i < IE; i++)
	{
		ihzh[i] = (float **) malloc(JE * sizeof(float *));
		assert(ihzh != NULL);
		for( j = 0; j < JE; j++)
			ihzh[i][j] = (float *) malloc(ka * sizeof(float));
	}

	//1D variables used to create the PML
	float *gi1 = (float *)calloc(IE , sizeof(float));
	float *gi2 = (float *)calloc(IE , sizeof(float));
	float *gi3 = (float *)calloc(IE , sizeof(float));
	float *gj1 = (float *)calloc(JE , sizeof(float));
	float *gj2 = (float *)calloc(JE , sizeof(float));
	float *gj3 = (float *)calloc(JE , sizeof(float));
	float *gk1 = (float *)calloc(KE , sizeof(float));
	float *gk2 = (float *)calloc(KE , sizeof(float));
	float *gk3 = (float *)calloc(KE , sizeof(float));
	float *fi1 = (float *)calloc(IE , sizeof(float));
	float *fi2 = (float *)calloc(IE , sizeof(float));
	float *fi3 = (float *)calloc(IE , sizeof(float));
	float *fj1 = (float *)calloc(JE , sizeof(float));
	float *fj2 = (float *)calloc(JE , sizeof(float));
	float *fj3 = (float *)calloc(JE , sizeof(float));
	float *fk1 = (float *)calloc(KE , sizeof(float));
	float *fk2 = (float *)calloc(KE , sizeof(float));
	float *fk3 = (float *)calloc(KE , sizeof(float));

	//single value variables used for PML
	int ixh = 0; 
	int jyh = 0;
	int kzh = 0;		
	float curl_h = 0.0; 
	float curl_d = 0.0;		
	/*END Declare the Variables*/


	/*BEGIN Initialize the Variables*/
	/*Input from file*/
	/*Input various parameter data from file.  Format is chosen to be as in the 
	input_format_Doc.txt file.  The wavelength, divisions, guide type, and input
	type are read from the file.  The file must be named input.txt */
	float parameters[]= {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
	fpinput = fopen("input.txt", "r");
	if(fpinput==NULL) 
	{
		printf("Error: can't open file.\n");
		return 1;
	}
	else    //File format must be as specified in input_format_Doc.txt
	{
		printf("File opened successfully.\n");
		fscanf(fpinput, "%s %e", &stringin, &lambda);
		fscanf(fpinput, "%s %f", &stringin, &divisions);
		fscanf(fpinput, "%s %f", &stringin, &height);
		fscanf(fpinput, "%s %f", &stringin, &width);
		fscanf(fpinput, "%s %s", &stringin, &guide_type);
		fscanf(fpinput, "%s %s", &stringin, &input_type);
		if(strcmp(input_type, "GaussianWavePacket") ==0)
		{
			fscanf(fpinput, "%s %f", &stringin, &spread);
			fscanf(fpinput, "%s %f", &stringin, &t0);
			fscanf(fpinput, "%s %f", &stringin, &pulseHeight);
			fscanf(fpinput, "%s %f", &stringin, &pulseWidth);
			fscanf(fpinput, "%s %f", &stringin, &pulseFreq);
			fscanf(fpinput, "%s %f", &stringin, &position);
			parameters[0] = spread;
			parameters[1] = t0;
			parameters[2] = pulseHeight;
			parameters[3] = pulseWidth;
			parameters[4] = pulseFreq; 			
			parameters[5] = position;
		}
		else if(strcmp(input_type, "GaussianSine") ==0)
		{
			fscanf(fpinput, "%s %f", &stringin, &pulseHeight);
			fscanf(fpinput, "%s %f", &stringin, &pulseWidth);
			fscanf(fpinput, "%s %E", &stringin, &pulseFreq);
			fscanf(fpinput, "%s %f", &stringin, &position);
			parameters[2] = pulseHeight;
			parameters[3] = pulseWidth; 
			parameters[4] = pulseFreq; 
			parameters[5] = position;
		}
		else
		{
			printf("Sorry input type not found... exiting the program.\n");
			return 1;
		}
	}
	fclose(fpinput);

	ddx = lambda/divisions;  //171.4284;  //85.7142;  //171.4284; //85.7142;  //42.8571;		//ddx max value for stability is lambda/20;
	dt = ddx/6e8;			//also a stability condition

	printf("lambda = %2.9e  dx = %2.9e  dt = %2.19e  \n", lambda, ddx, dt);
	printf("height = %f  width = %f  guide type is %s \n", height, width, guide_type);
	printf("Spread %f, t0 %f, h %f, w %f, f %e, p %f\n", spread, t0, pulseHeight, pulseWidth, pulseFreq, position);
	/*End of Input from file*/

	ib = IE-ia;
	jb = JE-ja;
	kb = KE-ka;
	ic = IE/2;
	jc = JE/2;
	kc = KE/2;

	printf("ic = %3d   jc = %3d  kc = %3d \n", ic, jc, kc);

	/*Initialize the arrays*/
	for(k=0; k<KE; k++)
	{
		for(j=0; j<JE; j++)
		{
			for(i=0; i<IE; i++)
			{
				ex[i][j][k] = 0.0;
				ey[i][j][k] = 0.0;
				ez[i][j][k] = 0.0;
				dx[i][j][k] = 0.0;
				dy[i][j][k] = 0.0;
				dz[i][j][k] = 0.0;
				hx[i][j][k] = 0.0;
				hy[i][j][k] = 0.0;
				hz[i][j][k] = 0.0;
				ix[i][j][k] = 0.0;
				iy[i][j][k] = 0.0;
				iz[i][j][k] = 0.0;
				gax[i][j][k] = 1.0;
				gay[i][j][k] = 1.0;
				gaz[i][j][k] = 1.0;
				gbx[i][j][k] = cond*dt/epsz;
				gby[i][j][k] = cond*dt/epsz;
				gbz[i][j][k] = cond*dt/epsz;
			}
		}
	}

	/*Initialize the boundary parameters*/
	for(i=0; i<ia; i++)
	{
		for(j=0; j<JE; j++)
		{
			for(k=0; k<KE; k++)
			{
				idxl[i][j][k] = 0.0;
				idxh[i][j][k] = 0.0;
				ihxl[i][j][k] = 0.0;
				ihxh[i][j][k] = 0.0;
			}
		}
	}

	for(i=0; i<IE; i++)
	{
		for(j=0; j<ja; j++)
		{
			for(k=0; k<KE; k++)
			{
				idyl[i][j][k] = 0.0;
				idyh[i][j][k] = 0.0;
				ihyl[i][j][k] = 0.0;
				ihyh[i][j][k] = 0.0;
			}
		}
	}

	for(i=0; i<IE; i++)
	{
		for(j=0; j<JE; j++)
		{
			for(k=0; k<ka; k++)
			{
				idzl[i][j][k] = 0.0;
				idzh[i][j][k] = 0.0;
				ihzl[i][j][k] = 0.0;
				ihzh[i][j][k] = 0.0;
			}
		}
	}
	//Boundary Conditions/PML variables are initialized in the function createPML
	/*END Initialize the Variables*/

	
	/*BEGIN Dielectric Boundary*/
	defineDielectric(dt, (int)height, (int)width, gax, gbx, gay, gby, gaz, gbz, guide_type);
	/*END Dielectric Boundary*/


	/*BEGIN Create PML*/
	printf("npml -->  ");
	scanf("%f", &npml);
	printf("%f \n", npml);

	createPML(npml, gi1, gi2, gi3, gj1, gj2, gj3, gk1, gk2, gk3, fi1, fi2, fi3, fj1, fj2, fj3, fk1, fk2, fk3);	
	/*END Create PML*/


	T = 0;
	nsteps = 1;

	/*BEGIN the main part of the program, this will run until the command screen is exited*/
	while(nsteps > 0)
	{
		/*Although the program will continue running until exited by the user, the user will
		specify nsteps which determines the number of time steps to be calculated before waiting
		for user input.  At that point the user can enter an additional time step to continue 
		taking data.  If one enters 30 and then when prompted again enters 30 again, the program 
		will end it's calculations at T=60.  Data is taken every 20 time steps.  The maximum amount
		of time the program will run is 14,000 time steps (ensure that program won't continue 
		indefinitely due to user not quiting the program*/
		printf("nsteps --> ");	//user defines how long to run the program
		scanf("%d", &nsteps);
		printf("%d \n", nsteps);

		/* Start of the Main FDTD loop*/
		for(n=1; n<=nsteps; n++)
		{ 
			T = T+1;
			printf("%3f   ",T);

			/*Calculate the Dx field*/
			calcDx(ib, ixh, dx, hy, hz, idxl, idxh, curl_h, gi1, gj3, gk3, gj2, gk2);
			
			/*Calculate the Dy field*/
			calcDy(jb, jyh, dy, hx, hz, idyl, idyh, curl_h, gj1, gi3, gk3, gi2, gk2);

			/*Calculate the Dz field*/
			calcDz(kb, kzh, dz, hx, hy, idzl, idzh, curl_h, gk1, gi3, gj3, gi2, gj2);

			/*Source*/
			inputSource(dx, ic, jc, kc, (int)T, height, width, dt, parameters);

			/*Calculate the E field from D field*/
			calcEx(ex, dx, gax, gbx, ix);
			calcEy(ey, dy, gay, gby, iy);
			calcEz(ez, dz, gaz, gbz, iz);

			/*Calculate the Hx field*/
			calcHx(hx, ey, ez, ihxl, ihxh, curl_e, fj3, fk3, fj2, fk2, fi1, ixh, ib);

			/*Calculate the Hy field*/
			calcHy(hy, ex, ez, ihyl, ihyh, curl_e, fi3, fk3, fi2, fk2, fj1, jyh, jb);

			/*Calculate the Hz field*/
			calcHz(hz, ex, ey, ihzl, ihzh, curl_e, fi3, fj3, fi2, fj2, fk1, kzh, kb);

			/*End of the data calculation in the main FDTD loop*/
		    
		
			/*BEGIN Declaring the Variables for File*/
			char *file_name;
			file_name = malloc(30); 			//Allow for program to store data at multiple points
			
			char *s1 = "1111";					//use this to create a movie later
			char *s2 = "2222";
			char *my_dest[30];
			int save_data;
			char ending[30] = ".txt";
			char file_is[30] = "Hyi";

			s1 = file_is;
			s2 = ending;

			save_data = 20;
			Tcalc = (int) T;

			int spoti = 1;

			if(Tcalc % save_data == 0)
			{
				printFile(T, Tcalc, hy, file_name, s1, s2, my_dest, width, height, spoti);
			}

			char *file_name2;
			file_name2 = malloc(30); 			//Allow for program to store data at multiple points
			char *s3 = "1111";
			char file_is_second[30] = "Hyj";
			s3 = file_is_second;

			int save_data1 = 20;
			int Tcalc1 = (int) T;

			int spotj = 1;

			if(Tcalc1 % save_data1 == 0)
			{
				printFile(T, Tcalc1, hy, file_name2, s3, s2, my_dest, width, height, spotj);
			}

			if(T == 14000)  //Used to force quit if program left running forever
			{
				return 0;
			}
		}
		/*END of main FDTD Loop*/

		//Print last variable data to standard output
		printf("Ezj \n");
		for(k = 1; k<KE; k++)
		{
			printf("%2d  ", k);
			for(i=1;i<IE-1; i++)
			{
				printf("%6.3f", ez[i][jc][k]);
			}
			printf(" \n");
		}

		printf("Dz \n");
		for(k = 1; k<KE; k++)
		{
			printf("%2d  ", k);
			for(i=1;i<IE-1; i++)
			{
				printf("%6.3f", dz[i][jc][k]);
			}
			printf(" \n");
		}
	}

	return 0;
}

/*This function is used to create a perfectly matched layer around the problem space. It uses
the Berninger Method.  This is necessary to limit problem space size by creating an artificial
boundary that eliminates reflections back into the problem space. (inifinite problem space can 
be simulated in this way)*/
void createPML(float npml, float*gi1, float*gi2, float*gi3, float*gj1, float*gj2, float*gj3, float*gk1, float*gk2, float*gk3, float*fi1, float*fi2, float*fi3, float*fj1, float*fj2, float*fj3, float*fk1, float*fk2, float*fk3)
{
	int i, j, k;
	float xn, xxn, xnum, xd; 
	int n_pml = (int) npml;

	/*Boundary Conditions*/
	for(i=0; i<IE; i++)
	{
		gi1[i] = 0.0;
		fi1[i] = 0.0;
		gi2[i] = 1.0;
		fi2[i] = 1.0;
		gi3[i] = 1.0;
		fi3[i] = 1.0;
	}

	for(j=0; j<JE; j++)
	{
		gj1[j] = 0.0;
		fj1[j] = 0.0;
		gj2[j] = 1.0;
		fj2[j] = 1.0;
		gj3[j] = 1.0;
		fj3[j] = 1.0;
	}

	for(k=0; k<KE; k++)
	{
		gk1[k] = 0.0;
		fk1[k] = 0.0;
		gk2[k] = 1.0;
		fk2[k] = 1.0;
		gk3[k] = 1.0;
		fk3[k] = 1.0;
	}

	//set the arrays to the necessary PML values
	//i (x) direction
	for(i=0; i<n_pml; i++)
	{
		xxn = (npml-i)/npml;
		xn = .33*powf(xxn,3.0);
		printf("%d xxn = %8.4f  xn = %8.4f  npml = %f \n", i, xxn, xn, npml);
		fi1[i] = xn;
		fi1[IE-i-1] = xn;
		gi2[i] = 1./(1.+xn);
		gi2[IE-i-1] = 1./(1.+xn);
		gi3[i] = (1.-xn)/(1.+xn);
		gi3[IE-i-1] = (1.-xn)/(1.+xn);
		xxn = (npml-i-.5)/npml;
		xn = .33*powf(xxn,3.0);
		gi1[i] = xn;
		gi1[IE-i-1] = xn;
		fi2[i] = 1./(1.+xn);
		fi2[IE-i-1] = 1./(1.+xn);
		fi3[i] = (1.-xn)/(1.+xn);
		fi3[IE-i-1] = (1.-xn)/(1.+xn);
	}
	printf("fi & gi \n");
	for(i=0; i<IE; i++)
	{
		printf("%2d  %6.4f %6.4f %6.4f  \n", i, fi1[i], gi2[i], gi3[i]);
		printf("%2d  %6.4f %6.4f %6.4f  \n", i, gi1[i], fi2[i], fi3[i]);
	}

	//j (y) direction
	for(j=0; j<n_pml; j++)
	{
		xxn = (npml-j)/npml;
		xn = .33*powf(xxn,3.0);
		printf("%d xxn = %8.4f  xn = %8.4f  \n", j, xxn, xn);
		fj1[j] = xn;
		fj1[JE-j-1] = xn;
		gj2[j] = 1./(1.+xn);
		gj2[JE-j-1] = 1./(1.+xn);
		gj3[j] = (1.-xn)/(1.+xn);
		gj3[JE-j-1] = (1.-xn)/(1.+xn);
		xxn = (npml-j-.5)/npml;
		xn = .33*powf(xxn,3.0);
		gj1[j] = xn;
		gj1[JE-j-1] = xn;
		fj2[j] = 1./(1.+xn);
		fj2[JE-j-1] = 1./(1.+xn);
		fj3[j] = (1.-xn)/(1.+xn);
		fj3[JE-j-1] = (1.-xn)/(1.+xn);
	}
	printf("fj & gj \n");
	for(j=0; j<JE; j++)
	{
		printf("%2d  %6.4f %6.4f %6.4f  \n", j, fj1[j], gj2[j], gj3[j]);
		printf("%2d  %6.4f %6.4f %6.4f  \n", j, gj1[j], fj2[j], fj3[j]);
	}
	
	//k (z) direction
	for(k=0; k<n_pml; k++)
	{
		xxn = (npml-k)/npml;
		xn = .33*powf(xxn,3.0);
		printf("%d xxn = %8.4f  xn = %8.4f  \n", k, xxn, xn);
		fk1[k] = xn;
		fk1[KE-k-1] = xn;
		gk2[k] = 1./(1.+xn);
		gk2[KE-k-1] = 1./(1.+xn);
		gk3[k] = (1.-xn)/(1.+xn);
		gk3[KE-k-1] = (1.-xn)/(1.+xn);
		xxn = (npml-k-.5)/npml;
		xn = .33*powf(xxn,3.0);
		gk1[k] = xn;
		gk1[KE-k-1] = xn;
		fk2[k] = 1./(1.+xn);
		fk2[KE-k-1] = 1./(1.+xn);
		fk3[k] = (1.-xn)/(1.+xn);
		fk3[KE-k-1] = (1.-xn)/(1.+xn);
	}
	printf("fk & gk \n");
	for(k=0; k<KE; k++)
	{
		printf("%2d  %6.4f %6.4f %6.4f  \n", k, fk1[k], gk2[k], gk3[k]);
		printf("%2d  %6.4f %6.4f %6.4f  \n", k, gk1[k], fk2[k], fk3[k]);
	}	
}

/*This function is used to create a dielectric variation in the problem space.  The posibilities are
rectangular waveguides and photonic crystal waveguides and cavitities.  The parameters for the photonic
crystal variation is hard coded into the program and should be correct to cause a bandgap with midband
frequency in the infared range of approximately 1.3 or 1.5 um.*/
void defineDielectric(float dt, int height, int width, float ***gax, float ***gbx, float ***gay, float ***gby, float ***gaz, float ***gbz, char *guide_type)
{
	//These are redefinied so they don't have to be passed into the function
	int i, j, k;
	int ib = IE-ia;
	int jb = JE-ja;
	int kb = KE-ka;
	int ic = IE/2;
	int jc = JE/2;
	int kc = KE/2;

	FILE *fp;

	float i_low = ic - height;//44;
	float i_high = ic + height;//44;
	float j_low;
	float j_high;

	float sigma = 0.000000;
	double cavity = 0;  //1 for true and 0 for false initialized to 0 and changed if user chose cavity as guide_type
	int circles = 1;    //1 if there is to be a photonic crystal in the problem space and 0 if not

	//Determine which type of dielectric pattern to use based on what was chosen by the user in the input.txt file
	if(strcmp(guide_type, "rectangular")==0)
	{
		j_low = jc - width; 
		j_high = jc + width; 
	}
	if(strcmp(guide_type, "pc")==0)
	{
		j_low = 0; 
		j_high = JE-1;
		circles = 1;
	}
	if(strcmp(guide_type, "cavity")==0)
	{
		j_low = 0; 
		j_high = JE-1;  
		cavity = 1;
		circles = 1;
	}

	/*Calculate/Define gax, gbx*/
	for(i = (int)i_low; i<(int)i_high+1; i++)
	{
		for(j = (int)j_low; j<(int)j_high+1; j++)
		{
			for(k = 0; k<KE; k++)
			{
				gax[i][j][k] = 1.0/(eps+(sigma*dt/epsz));		//accounts for lossy dielectric material
				gbx[i][j][k] = sigma*dt/epsz;
			}
		}
	}

	//printIndFile(IE, JE, gax, "gax_z.txt", kc, 3);
	//printIndFile(IE, KE, gax, "gax_y.txt", jc, 2);
	//printIndFile(JE, KE, gax, "gax_x.txt", ic, 1);
	
	/*Calculate/Define gay, gby*/
	for(i = (int)i_low; i<(int)i_high+1; i++)
	{
		for(j = (int)j_low; j<(int)j_high+1; j++)
		{
			for(k = 0; k<KE; k++)
			{
				gay[i][j][k] = 1.0/(eps+(sigma*dt/epsz));
				gby[i][j][k] = sigma*dt/epsz;
			}
		}
	}

	//printIndFile(IE, JE, gay, "gay_z.txt", kc, 3);
	//printIndFile(IE, KE, gay, "gay_y.txt", jc, 2);
	//printIndFile(JE, KE, gay, "gay_x.txt", ic, 1);

	/*Calculate/Define gaz, gbz*/
	for(i = (int)i_low; i<(int)i_high+1; i++)
	{
		for(j = (int)j_low; j<(int)j_high+1; j++)
		{
			for(k = 0; k<KE; k++)
			{
				gaz[i][j][k] = 1.0/(eps+(sigma*dt/epsz));
				gbz[i][j][k] = sigma*dt/epsz;			
			}
		}
	}

	//printIndFile(IE, JE, gaz, "gaz_z.txt", kc, 3);
	//printIndFile(IE, KE, gaz, "gaz_y.txt", jc, 2);
	//printIndFile(JE, KE, gaz, "gaz_x.txt", ic, 1);


	/*BEGIN creating PC pattern if chosen by user*/
	//Create the circles in the middle section.  Centered in the space and that depends on if the 
	//number of circles is odd or even.
	double zpos, ypos_plus, ypos_minus, ypos;
	float radius = 8.0;  //hole radius
	float lattice_const = 20.0;  //period of hole repetition
	double circle_space = lattice_const; //center to center spacing of circles or circle_space = circle diameter + spacing
	double seperation = (double)(lattice_const-2*radius);  //distance between the circle edges
	int num_circle = round(((double)KE-seperation)/circle_space);  //(space available-space on the ends of the row)/space for 1 circle
	printf("circle_space is %f \n", circle_space);
	printf("num_circle is %u \n", num_circle);
	printf("%g \n", seperation);

	float row_spacing = 17.0; //calculated by hand using geometry for perfect triangular crystal lattice
	printf("row spacing = %f \n", row_spacing);


	if(circles==1)  //if there is a PC in the problem space
	{
		for(k = 0; k < 6; ++k)   //allow for 6 rows
		{
			ypos_plus = JE/2+round(row_spacing*(k));
			ypos_minus = JE/2-round(row_spacing*(k));
			ypos = JE/2;

			if(cavity == 1 && k==0)			//PC cavity structure
			{
				if(num_circle%2 ==0)		//Number of circles in the row is even
				{
					for(i = 1; i < (num_circle/2); i++)
					{
						zpos = KE/2 + seperation/2 + radius + circle_space*i;
						createCircle(zpos, ypos, 2.0*height, radius, dt, gax, gbx);
						createCircle(zpos, ypos, 2.0*height, radius, dt, gay, gby);
						createCircle(zpos, ypos, 2.0*height, radius, dt, gaz, gbz);
						zpos = KE/2 - (seperation/2 + radius + circle_space*i);
						createCircle(zpos, ypos, 2.0*height, radius, dt, gax, gbx);
						createCircle(zpos, ypos, 2.0*height, radius, dt, gay, gby);
						createCircle(zpos, ypos, 2.0*height, radius, dt, gaz, gbz);
					}
				}
				else						//Number of circles in the row is odd
				{
					for(i = 1; i<(num_circle/2)+1; i++)
					{
						zpos = KE/2 + circle_space*i;
						zpos = round(zpos);
						createCircle(zpos, ypos, 2.0*height, radius, dt, gax, gbx);
						createCircle(zpos, ypos, 2.0*height, radius, dt, gay, gby);
						createCircle(zpos, ypos, 2.0*height, radius, dt, gaz, gbz);
						zpos = KE/2 - circle_space*i;
						zpos = round(zpos);
						createCircle(zpos, ypos, 2.0*height, radius, dt, gax, gbx);
						createCircle(zpos, ypos, 2.0*height, radius, dt, gay, gby);
						createCircle(zpos, ypos, 2.0*height, radius, dt, gaz, gbz);
					}
				}
			}
			else							//PC waveguide structure
			{
				if(num_circle%2 ==0 && k>0)		//Number of circles in the row is even
				{
					printf("even \n");
					for(i = 0; i < (num_circle/2); i++)
					{
						zpos = KE/2 + seperation/2 + radius + circle_space*i;
						createCircle(zpos, ypos_plus, 2.0*height, radius, dt, gax, gbx);
						createCircle(zpos, ypos_plus, 2.0*height, radius, dt, gay, gby);
						createCircle(zpos, ypos_plus, 2.0*height, radius, dt, gaz, gbz);
						zpos = KE/2 -(seperation/2 + radius + circle_space*i);
						createCircle(zpos, ypos_plus, 2.0*height, radius, dt, gax, gbx);
						createCircle(zpos, ypos_plus, 2.0*height, radius, dt, gay, gby);
						createCircle(zpos, ypos_plus, 2.0*height, radius, dt, gaz, gbz);

						zpos = KE/2 + seperation/2 + radius + circle_space*i;
						createCircle(zpos, ypos_minus, 2.0*height, radius, dt, gax, gbx);
						createCircle(zpos, ypos_minus, 2.0*height, radius, dt, gay, gby);
						createCircle(zpos, ypos_minus, 2.0*height, radius, dt, gaz, gbz);
						zpos = KE/2 -(seperation/2 + radius + circle_space*i);
						createCircle(zpos, ypos_minus, 2.0*height, radius, dt, gax, gbx);
						createCircle(zpos, ypos_minus, 2.0*height, radius, dt, gay, gby);
						createCircle(zpos, ypos_minus, 2.0*height, radius, dt, gaz, gbz);
					}
				}
				else 					//Number of circles in the row is odd
				{
					if(k > 0)
					{
						printf("odd \n");
						for(i = 0; i<(num_circle/2)+1; i++)
						{
							zpos = KE/2 + circle_space*i;
							zpos = round(zpos);
							createCircle(zpos, ypos_plus, 2.0*height, radius, dt, gax, gbx);
							createCircle(zpos, ypos_plus, 2.0*height, radius, dt, gay, gby);
							createCircle(zpos, ypos_plus, 2.0*height, radius, dt, gaz, gbz);
							zpos = KE/2 - circle_space*i;
							zpos = round(zpos);
							createCircle(zpos, ypos_plus, 2.0*height, radius, dt, gax, gbx);
							createCircle(zpos, ypos_plus, 2.0*height, radius, dt, gay, gby);
							createCircle(zpos, ypos_plus, 2.0*height, radius, dt, gaz, gbz);

							zpos = KE/2 + circle_space*i;
							zpos = round(zpos);
							createCircle(zpos, ypos_minus, 2.0*height, radius, dt, gax, gbx);
							createCircle(zpos, ypos_minus, 2.0*height, radius, dt, gay, gby);
							createCircle(zpos, ypos_minus, 2.0*height, radius, dt, gaz, gbz);
							zpos = KE/2 - circle_space*i;
							zpos = round(zpos);
							createCircle(zpos, ypos_minus, 2.0*height, radius, dt, gax, gbx);
							createCircle(zpos, ypos_minus, 2.0*height, radius, dt, gay, gby);
							createCircle(zpos, ypos_minus, 2.0*height, radius, dt, gaz, gbz);
						}
					}
				}
			
			}
		num_circle = num_circle+1;		//increment so that even row will be followed by odd and vice versa
		}								//this will create a triangular lattice PC
	}

	printIndFile(IE, JE, gax, "gax_z.txt", kc, 3);
	printIndFile(IE, KE, gax, "gax_y.txt", jc, 2);
	printIndFile(JE, KE, gax, "gax_x.txt", ic, 1);
}

//This function calculates the electric flux in x from the magnetic fields
void calcDx(int ib, int ixh, float *** dx, float ***hy, float ***hz, float*** idxl, float ***idxh, float curl_h, float *gi1, float *gj3, float *gk3, float *gj2, float *gk2)
{
	int i, j, k;

	for(i=1; i<ia; i++)
	{
		for(j=1; j<JE; j++)
		{
			for(k=1; k<KE; k++)
			{
				curl_h = (hz[i][j][k] - hz[i][j-1][k]-hy[i][j][k]+hy[i][j][k-1]);
				idxl[i][j][k] = idxl[i][j][k] + curl_h;
				dx[i][j][k] = gj3[j]*gk3[k]*dx[i][j][k] +gj2[j]*gk2[k]*.5*(curl_h+ gi1[i]*idxl[i][j][k]);
			}
		}
	}

	for(i=ia; i<=ib; i++)
	{
		for(j=1; j<JE; j++)
		{
			for(k=1; k<KE; k++)
			{
				curl_h = (hz[i][j][k] - hz[i][j-1][k]-hy[i][j][k]+hy[i][j][k-1]);
				dx[i][j][k] = gj3[j]*gk3[k]*dx[i][j][k] +gj2[j]*gk2[k]*.5*curl_h;
			}
		}
	}

	for(i=ib+1; i<IE; i++)
	{
		ixh = i-ib-1;
		for(j=1; j<JE; j++)
		{
			for(k=1; k<KE; k++)
			{
				curl_h = (hz[i][j][k] - hz[i][j-1][k]-hy[i][j][k]+hy[i][j][k-1]);
				idxh[ixh][j][k] = idxh[ixh][j][k] + curl_h;
				dx[i][j][k] = gj3[j]*gk3[k]*dx[i][j][k] +gj2[j]*gk2[k]*.5*(curl_h+ gi1[i]*idxh[ixh][j][k]);
			}
		}
	}

}
//This function calculates the electric flux in y from the magnetic fields
void calcDy(int jb, int jyh, float *** dy, float ***hx, float ***hz, float*** idyl, float ***idyh, float curl_h, float *gj1, float *gi3, float *gk3, float *gi2, float *gk2)
{
	int i, j, k;
	for(i=1; i<IE; i++)
	{
		for(j=1; j<ja; j++)
		{
			for(k=1; k<KE; k++)
			{
				curl_h = (hx[i][j][k] - hx[i][j][k-1]-hz[i][j][k]+hz[i-1][j][k]);
				idyl[i][j][k] = idyl[i][j][k] + curl_h;
				dy[i][j][k] = gi3[i]*gk3[k]*dy[i][j][k] +gi2[i]*gk2[k]*.5*(curl_h+ gj1[j]*idyl[i][j][k]);
			}
		}
	}

	for(i=1; i<IE; i++)
	{
		for(j=ja; j<=jb; j++)
		{
			for(k=1; k<KE; k++)
			{
				curl_h = (hx[i][j][k] - hx[i][j][k-1]-hz[i][j][k]+hz[i-1][j][k]);
				dy[i][j][k] = gi3[i]*gk3[k]*dy[i][j][k] +gi2[i]*gk2[k]*.5*curl_h;
			}
		}
	}

	for(i=1; i<IE; i++)
	{
		for(j=jb+1; j<JE; j++)
		{
			jyh = j-jb-1;
			for(k=1; k<KE; k++)
			{
				curl_h = (hx[i][j][k] - hx[i][j][k-1]-hz[i][j][k]+hz[i-1][j][k]);
				idyh[i][jyh][k] = idyh[i][jyh][k] + curl_h;
				dy[i][j][k] = gi3[i]*gk3[k]*dy[i][j][k] +gi2[i]*gk2[k]*.5*(curl_h+ gj1[j]*idyh[i][jyh][k]);
			}
		}
	}
}

//This function calculates the electric flux in z from the magnetic fields
void calcDz(int kb, int kzh, float *** dz, float ***hx, float ***hy, float*** idzl, float ***idzh, float curl_h, float *gk1, float *gi3, float *gj3, float *gi2, float *gj2)
{
	int i, j, k;
	int kc = KE/2; 
	for(i=1; i<IE; i++)
	{
		for(j=1; j<JE; j++)
		{
			for(k=1; k<ka; k++)
			{
				curl_h = (hy[i][j][k] - hy[i-1][j][k]-hx[i][j][k]+hx[i][j-1][k]);
				idzl[i][j][k] = idzl[i][j][k] + curl_h;
				dz[i][j][k] = gi3[i]*gj3[j]*dz[i][j][k] +gi2[i]*gj2[j]*.5*(curl_h+ gk1[k]*idzl[i][j][k]);
			}
		}
	}

	for(i=1; i<IE; i++)
	{
		for(j=1; j<JE; j++)
		{
			for(k=ka; k<=kb; k++)
			{
				curl_h = (hy[i][j][k] - hy[i-1][j][k]-hx[i][j][k]+hx[i][j-1][k]);
				dz[i][j][k] = gi3[i]*gj3[j]*dz[i][j][k] +gi2[i]*gj2[j]*.5*curl_h;
			}
		}
	}

	for(i=1; i<IE; i++)
	{
		for(j=1; j<JE; j++)
		{
			for(k=kb+1; k<KE; k++)
			{
				kzh = k-kb-1;
				curl_h = (hy[i][j][k] - hy[i-1][j][k]-hx[i][j][k]+hx[i][j-1][k]);
				idzh[i][j][kzh] = idzh[i][j][kzh] + curl_h;
				dz[i][j][k] = gi3[i]*gj3[j]*dz[i][j][k] +gi2[i]*gj2[j]*.5*(curl_h+ gk1[k]*idzh[i][j][kzh]);
			}
		}
	}
}
//This function is used to create an input into the problem space.  It relies on user input from file input.txt
void inputSource(float ***dz, int ic, int jc, int kc, int T, float height, float width, float dt, float *parameters)
{
	int i, j, k;
	float t0;// =  943 original working value
	float spread; // = 500 original working value
	FILE *fp;

	//For Gaussian in xz
	//Since propagation direction is z should be pulse in xz plane and dz=pulse;
	float pulseHeight; // = .5 original working value
	float pulseWidth;  // = 2 original working value
	float pulseFreq;   // = 2.0000E14 original working value
	int position;

	spread = parameters[0];
	t0 = parameters[1];
	pulseHeight = parameters[2];
	pulseWidth = parameters[3];
	pulseFreq = parameters[4];
	position = (int) parameters[5];
	printf("Spread %f, t0 %f, h %f, w %f, f %e, p %u\n", spread, t0, pulseHeight, pulseWidth, pulseFreq, position);


	//Create the input.  This will be either a Gaussian pulse in space with a Gaussian component in time, or
	//a Gaussian pulse in space with a sinusoid in time.  This will allow for propagation
	if(spread ==0 & t0==0)  //no Gaussian in time
	{
		for(i=0; i<IE; i++)
		{
			for(j=0; j<JE; j++)
			{
				//gaussian in space gaussian and sinusoidal in time w/ freq corresponding to 1.5um
				dz[i][j][position] = exp(-.5*(pow((ic-i)/pulseHeight, 2)))*exp(-.5*(pow((jc-j)/pulseWidth, 2)))*sin(2*pi*pulseFreq*T*dt);
			}
		}
	}
	else					//Gaussian in time
	{
		for(i=0; i<IE; i++)
		{
			for(j=0; j<JE; j++)
			{
				/*Used for most of the simulations... gaussian in space gaussian and gaussian/sinusoidal in 
				time w/ freq corresponding to 1.5um*/
				dz[i][j][position] = exp(-.5*(pow((ic-i)/pulseHeight, 2)))*exp(-.5*(pow((jc-j)/pulseWidth, 2)))*sin(2*pi*pulseFreq*T*dt)*exp(-.5*(pow((t0-T)/spread,2)));
			}
		}
	}

	//Pulse will have the largest magnitude when T=t0 so save the input (Dz) at that point to a file called Dz.txt
	if(T==t0)
	{
		printIndFile(IE, JE, dz, "Dz.txt", kc, 3);
	}
}

//This function calculates the electric field in x from the electric flux in x
void calcEx(float ***ex, float *** dx, float ***gax, float ***gbx, float ***ix)
{
	int i, j, k;
	for(k=0; k<KE; k++)
	{
		for(j=0; j<JE; j++)
		{
			for(i=0; i<IE; i++)
			{
//				ex[i][j][k] = gax[i][j][k]*(dx[i][j][k]);  This could be used for a loss-less dielectric
				ex[i][j][k] = gax[i][j][k]*(dx[i][j][k]-ix[i][j][k]);
				ix[i][j][k] = ix[i][j][k]+gbx[i][j][k]*ex[i][j][k];
			}
		}
	}
}

//This function calculates the electric field in y from the electric flux in y
void calcEy(float ***ey, float ***dy, float ***gay, float ***gby, float ***iy)
{
	int i, j, k;
	for(k=0; k<KE; k++)
	{
		for(j=0; j<JE; j++)
		{
			for(i=0; i<IE; i++)
			{
				ey[i][j][k] = gay[i][j][k]*dy[i][j][k]; 
				iy[i][j][k] = iy[i][j][k]+gby[i][j][k]*ey[i][j][k];
			}
		}
	}
}

//This function calculates the electric field in z from the electric flux in z
void calcEz(float ***ez, float ***dz, float ***gaz, float ***gbz, float ***iz)
{
	int i, j, k;
	for(k=0; k<KE; k++)
	{
		for(j=0; j<JE; j++)
		{
			for(i=0; i<IE; i++)
			{
				ez[i][j][k] = gaz[i][j][k]*dz[i][j][k]; 
				iz[i][j][k] = iz[i][j][k]+gbz[i][j][k]*ez[i][j][k];
			}
		}
	}
}

//This function calculates the magnetic field in x from the electric fields
void calcHx(float ***hx, float***ey, float***ez, float***ihxl, float***ihxh, float curl_e, float *fj3, float *fk3, float*fj2, float *fk2, float *fi1, int ixh, int ib)
{
	int i, j, k;
	for(i=0; i<ia; i++)
	{
		for(j=0; j<JE-1; j++)
		{
			for(k=0; k<KE-1; k++)
			{
				curl_e = (ey[i][j][k+1] - ey[i][j][k]-ez[i][j+1][k]+ez[i][j][k]);
				ihxl[i][j][k] = ihxl[i][j][k] + curl_e;
				hx[i][j][k] = fj3[j]*fk3[k]*hx[i][j][k] + fj2[j]*fk2[k]*.5*(curl_e+ fi1[i]*ihxl[i][j][k]);
			}
		}
	}

	for(i=ia; i<=ib; i++)
	{
		for(j=0; j<JE-1; j++)
		{
			for(k=0; k<KE-1; k++)
			{
				curl_e = (ey[i][j][k+1] - ey[i][j][k]-ez[i][j+1][k]+ez[i][j][k]);
				hx[i][j][k] = fj3[j]*fk3[k]*hx[i][j][k] + fj2[j]*fk2[k]*.5*curl_e;
			}
		}
	}

	for(i=ib+1; i<IE; i++)
	{
		ixh = i-ib-1;
		for(j=0; j<JE-1; j++)
		{
			for(k=0; k<KE-1; k++)
			{
				curl_e = (ey[i][j][k+1] - ey[i][j][k]-ez[i][j+1][k]+ez[i][j][k]);
				ihxh[ixh][j][k] = ihxh[ixh][j][k] + curl_e;
				hx[i][j][k] = fj3[j]*fk3[k]*hx[i][j][k] + fj2[j]*fk2[k]*.5*(curl_e+ fi1[i]*ihxh[ixh][j][k]);
			}
		}
	}
}

//This function calculates the magnetic field in y from the electric fields
void calcHy(float ***hy, float***ex, float***ez, float***ihyl, float***ihyh, float curl_e, float *fi3, float *fk3, float*fi2, float *fk2, float *fj1, int jyh, int jb)
{
	int i, j, k;	
	for(i=0; i<IE-1; i++)
	{
		for(j=0; j<ja; j++)
		{
			for(k=0; k<KE-1; k++)
			{
				curl_e = (ez[i+1][j][k] - ez[i][j][k]-ex[i][j][k+1]+ex[i][j][k]);
				ihyl[i][j][k] = ihyl[i][j][k] + curl_e;
				hy[i][j][k] = fi3[i]*fk3[k]*hy[i][j][k] + fi2[i]*fk2[k]*.5*(curl_e+ fj1[j]*ihyl[i][j][k]);
			}
		}
	}

	for(i=0; i<IE-1; i++)
	{
		for(j=ja; j<=jb; j++)
		{
			for(k=0; k<KE-1; k++)
			{
				curl_e = (ez[i+1][j][k] - ez[i][j][k]-ex[i][j][k+1]+ex[i][j][k]);
				hy[i][j][k] = fi3[i]*fk3[k]*hy[i][j][k] + fi2[i]*fk2[k]*.5*curl_e;
			}
		}
	}

	for(i=0; i<IE-1; i++)
	{
		for(j=jb+1; j<JE; j++)
		{
			jyh = j-jb-1;
			for(k=0; k<KE-1; k++)
			{
				curl_e = (ez[i+1][j][k] - ez[i][j][k]-ex[i][j][k+1]+ex[i][j][k]);
				ihyh[i][jyh][k] = ihyh[i][jyh][k] + curl_e;
				hy[i][j][k] = fi3[i]*fk3[k]*hy[i][j][k] + fi2[i]*fk2[k]*.5*(curl_e+ fj1[j]*ihyh[i][jyh][k]);
			}
		}
	}

}

//This function calculates the magnetic field in z from the electric fields
void calcHz(float ***hz, float***ex, float***ey, float***ihzl, float***ihzh, float curl_e, float *fi3, float *fj3, float*fi2, float *fj2, float *fk1, int kzh, int kb)
{
	int i, j, k;
	int kc = KE/2;

	for(i=0; i<IE-1; i++)
	{
		for(j=0; j<JE-1; j++)
		{
			for(k=0; k<ka; k++)
			{
				curl_e = (ex[i][j+1][k] - ex[i][j][k]-ey[i+1][j][k]+ey[i][j][k]);
				ihzl[i][j][k] = ihzl[i][j][k] + curl_e;
				hz[i][j][k] = fi3[i]*fj3[j]*hz[i][j][k] + fi2[i]*fj2[j]*.5*(curl_e+ fk1[k]*ihzl[i][j][k]);
			}
		}
	}

	for(i=1; i<IE-1; i++)
	{
		for(j=1; j<JE-1; j++)
		{
			for(k=ka; k<=kb; k++)
			{
				curl_e = (ex[i][j+1][k] - ex[i][j][k]-ey[i+1][j][k]+ey[i][j][k]);
				hz[i][j][k] = fi3[i]*fj3[j]*hz[i][j][k] + fi2[i]*fj2[j]*.5*curl_e;
			}
		}
	}

	for(i=1; i<IE-1; i++)
	{
		for(j=1; j<JE-1; j++)
		{
			for(k=kb+1; k<KE; k++)
			{
				kzh = k-kb-1;
				curl_e = (ex[i][j+1][k] - ex[i][j][k]-ey[i+1][j][k]+ey[i][j][k]);
				ihzh[i][j][kzh] = ihzh[i][j][kzh] + curl_e;
				hz[i][j][k] = fi3[i]*fj3[j]*hz[i][j][k] + fi2[i]*fj2[j]*.5*(curl_e+ fk1[k]*ihzh[i][j][kzh]);
			}
		}
	}
}

/*This function is used to setup the variables to print the fields to text files.  Actual file writing is done
in printIndFile*/
void printFile(float T, int Tcalc, float ***ez, char *file_name, char * s1, char *s2, char *my_dest, float width, float height, int constSpot)
{
	int i, j, k;
	int jc = JE/2;
	int ic = IE/2;
	int kc = KE/2;
	FILE *fp;
	int kc_value;

	file_name = s1;

	sprintf(my_dest, "%d", (int)T);
  
	s1 = strcat(file_name, my_dest);

	printf("s1 is %s \n", s1);
	printf("s2 is %s \n", s2);
	file_name = strcat(file_name, s2);
	printf("file_name is %s  \n", file_name);

	/*Determine which slice to take... either x, y or z (determined by constSpot)*/
	if(constSpot==1)	//x slice in middle
	{
		printIndFile(JE, KE, ez, file_name, ic, constSpot);   
	}

	if(constSpot==2)	//y slice in middle
	{
		printIndFile(IE, KE, ez, file_name, jc, constSpot);   
	}

	double interval;
	if(constSpot==3)	//z slice in middle
	{ 
		printIndFile(IE, JE, ez, file_name, kc, constSpot);   
	}

	//customized data slices
	if(constSpot==4)	//z slice at z=1065
	{ 
		printIndFile(IE, JE, ez, file_name, 1065, 3);   
	}

	kc_value = 0;
	if(constSpot==5)	//z slice at various z values used to track the propagation of a specific wave
	{ 
		if(Tcalc%14 ==0)
		{
			interval = Tcalc/7;  //because wave takes 7 times steps to travel 1 cell
			kc_value = kc_value + 2; //interval;
			printIndFile(IE, JE, ez, file_name, kc_value, 3);
		}
	}
}

//This function is used to print data to a specific file as specified by file_name.
void printIndFile(int row_max, int colmn_max, float *** var, char * file_name, int dimConst, int constSpot)
{
	FILE *fp;

	int i, j;

	fp = fopen(file_name, "w");
	for(i=0; i<row_max; i++)
	{
		for(j=0; j<colmn_max; j++)
		{
			if(constSpot == 1)
			{
				fprintf(fp, "%8.4f  ", var[dimConst][i][j]);
			}
			if(constSpot == 2)
			{
				fprintf(fp, "%8.4f  ", var[i][dimConst][j]);
			}
			if(constSpot == 3)
			{
				fprintf(fp, "%8.4f  ", var[i][j][dimConst]);
			}
		}
		fprintf(fp, "\n");
	}
	fclose(fp);
}

/*This function is used by createDielectric to create circles in the problem space.  The specific case here
is that of a dielectric slab with air holes in it.  The circles will have permittivity equal to the free space
permittivity.*/
void createCircle(double zc, double yc, float height, double radius, double dt, float *** p_ga, float *** p_gb)
{
	double zdist, ydist;
	double dist;
	double epsilon_air = 1.0;
	float i_low = IE/2 - height/2;
	float i_high = IE/2 + height/2;

	int i, j, k;

	for(k=0; k<KE; k++)
	{
		for(j = 0; j<JE; j++)
		{
			for(i = (int)i_low; i<(int)i_high+1; i++)
			{
				zdist = (double)(zc -k);
				ydist = (double)(yc -j);
				dist = sqrt(pow(zdist,2)+pow(ydist, 2));
				if(dist <= radius)
				{
					p_ga[i][j][k] = 1.0; //1./(epsilon_air + (sigma*dt/epsz));  //1.0
					p_gb[i][j][k] = 0.0; //sigma * dt/epsz; //0.0
				}
			}
		}
	}
}
