/*
setupIndex.c
*/

#include "oth.h"
#include "oth_prototype.h"

void setupIndex(void)
{
/*
	E : array index
	Ex : i=-L...Nx+L-1, j=-L...Ny+L,   k=-L...Nz+L
	Ey : i=-L...Nx+L,   j=-L...Ny+L-1, k=-L...Nz+L
	Ez : i=-L...Nx+L,   j=-L...Ny+L,   k=-L...Nz+L-1
*/
	const int lx = (Abc == PML) ? Lpml : 1;
	const int i0ex =    - lx;
	const int i0ey =    - lx;
	const int i0ez =    - lx;
	const int i1ex = Nx + lx - 1;
	const int i1ey = Nx + lx;
	const int i1ez = Nx + lx;
	//printf("i : %d %d %d %d %d %d\n", i0ex, i0ey, i0ez, i1ex, i1ey, i1ez); fflush(stdout);

	const int ly = (Abc == PML) ? Lpml : 1;
	const int j0ex =    - ly;
	const int j0ey =    - ly;
	const int j0ez =    - ly;
	const int j1ex = Ny + ly;
	const int j1ey = Ny + ly - 1;
	const int j1ez = Ny + ly;
	//printf("j : %d %d %d %d %d %d\n", j0ex, j0ey, j0ez, j1ex, j1ey, j1ez); fflush(stdout);

	const int lz = (Abc == PML) ? Lpml : 1;
	const int k0ex =    - lz;
	const int k0ey =    - lz;
	const int k0ez =    - lz;
	const int k1ex = Nz + lz;
	const int k1ey = Nz + lz;
	const int k1ez = Nz + lz - 1;
	//printf("k : %d %d %d %d %d %d\n", k0ex, k0ey, k0ez, k1ex, k1ey, k1ez); fflush(stdout);

	NE = arrayIndex(
		i0ex, i1ex, j0ex, j1ex, k0ex, k1ex,
		i0ey, i1ey, j0ey, j1ey, k0ey, k1ey,
		i0ez, i1ez, j0ez, j1ez, k0ez, k1ez);

	assert(NEY(i0ey, j0ey, k0ey)     == 0);
	assert(NEZ(i0ez, j0ez, k0ez)     == NEY(i0ey, j1ey, k1ey) + 1);
	assert(NEX(i0ex, j0ex, k0ex)     == NEZ(i0ez, j1ez, k1ez) + 1);
	assert(NEZ(i1ez, j1ez, k1ez) + 1 == NE);
	assert(NEY(i1ey, j1ey, k1ey) + 1 == NEZ(i1ez, j0ez, k0ez));
	assert(NEX(i1ex, j1ex, k1ex) + 1 == NEY(i1ey, j0ey, k0ey));

	// E : loop index

	i0Ex = i0ex + ((Abc == PML) ? 0 : 1);
	i0Ey = i0ey + 1;
	i0Ez = i0ez + 1;
	i1Ex = i1ex - ((Abc == PML) ? 0 : 1);
	i1Ey = i1ey - 1;
	i1Ez = i1ez - 1;
	//printf("E i : %d %d %d %d %d %d\n", i0Ex, i0Ey, i0Ez, i1Ex, i1Ey, i1Ez); fflush(stdout);

	j0Ex = j0ex + 1;
	j0Ey = j0ey + ((Abc == PML) ? 0 : 1);
	j0Ez = j0ez + 1;
	j1Ex = j1ex - 1;
	j1Ey = j1ey - ((Abc == PML) ? 0 : 1);
	j1Ez = j1ez - 1;
	//printf("E j : %d %d %d %d %d %d\n", j0Ex, j0Ey, j0Ez, j1Ex, j1Ey, j1Ez); fflush(stdout);

	k0Ex = k0ex + 1;
	k0Ey = k0ey + 1;
	k0Ez = k0ez + ((Abc == PML) ? 0 : 1);
	k1Ex = k1ex - 1;
	k1Ey = k1ey - 1;
	k1Ez = k1ez - ((Abc == PML) ? 0 : 1);
	//printf("E k : %d %d %d %d %d %d\n", k0Ex, k0Ey, k0Ez, k1Ex, k1Ey, k1Ez); fflush(stdout);

	// H array index = E array index

	NHXI = NEXI;
	NHXJ = NEXJ;
	NHXK = NEXK;
	NHX0 = NEX0;

	NHYI = NEYI;
	NHYJ = NEYJ;
	NHYK = NEYK;
	NHY0 = NEY0;

	NHZI = NEZI;
	NHZJ = NEZJ;
	NHZK = NEZK;
	NHZ0 = NEZ0;

	NH = NE;

	// H array bound = to be addressed

	i0Hx = 0;
	i0Hy = -1;
	i0Hz = -1;
	i1Hx = Nx;
	i1Hy = Nx;
	i1Hz = Nx;

	j0Hx = -1;
	j0Hy = 0;
	j0Hz = -1;
	j1Hx = Ny;
	j1Hy = Ny;
	j1Hz = Ny;

	k0Hx = -1;
	k0Hy = -1;
	k0Hz = 0;
	k1Hx = Nz;
	k1Hy = Nz;
	k1Hz = Nz;
/*
	printf("%zd %zd %zd\n", NHX(i0Hx, j0Hx, k0Hx), NHX(i1Hx, j1Hx, k1Hx), NH);
	printf("%zd %zd %zd\n", NHY(i0Hy, j0Hy, k0Hy), NHY(i1Hy, j1Hy, k1Hy), NH);
	printf("%zd %zd %zd\n", NHZ(i0Hz, j0Hz, k0Hz), NHZ(i1Hz, j1Hz, k1Hz), NH);
*/
	assert(NHX(i0Hx, j0Hx, k0Hx) >= 0 && NHX(i1Hx, j1Hx, k1Hx) < NH);
	assert(NHY(i0Hy, j0Hy, k0Hy) >= 0 && NHY(i1Hy, j1Hy, k1Hy) < NH);
	assert(NHZ(i0Hz, j0Hz, k0Hz) >= 0 && NHZ(i1Hz, j1Hz, k1Hz) < NH);
}
