/*
matrixEz.cu
u = Av (Ez)
*/

#include "oth.h"
#include "oth_cuda.h"
#include "cuda_complex.h"

__host__ __device__ __forceinline__
static void matrixEz_(
	int i, int j, int k,
	int pre, int ifreq,
	cuDoubleComplex *a, const cuDoubleComplex *v, cuDoubleComplex *u, cuDoubleComplex *b,
	const double *kdxn, const double *kdyn,
	const double *kdzc,
	const double *krxn, const double *kryn,
	const double *krxp, const double *kryp,
	const double *krxm, const double *krym,
	const double *krzc,
	const material_t *material, const int *me, const int *mh,
	const double *fxpml, const double *fypml, const double *fzpml,
	param_t *q)
{
	int nx = q->Nx;
	int ny = q->Ny;
	int nz = q->Nz;
	int xpbc = q->Xpbc;
	int ypbc = q->Ypbc;
	int lpml = q->Lpml;
	int m0 = ifreq * q->NMaterial;
	int64_t n0 = nEZ(q, i, j, k);

	int64_t p[NNZ];
	p[ 0] = nEZ(q, i,     j,     k    );
	p[ 1] = nEZ(q, i + 1, j,     k    );
	p[ 2] = nEZ(q, i - 1, j,     k    );
	p[ 3] = nEZ(q, i,     j + 1, k    );
	p[ 4] = nEZ(q, i,     j - 1, k    );
	p[ 5] = nEX(q, i,     j,     k + 1);
	p[ 6] = nEX(q, i,     j,     k    );
	p[ 7] = nEX(q, i - 1, j,     k + 1);
	p[ 8] = nEX(q, i - 1, j,     k    );
	p[ 9] = nEY(q, i,     j,     k + 1);
	p[10] = nEY(q, i,     j,     k    );
	p[11] = nEY(q, i,     j - 1, k + 1);
	p[12] = nEY(q, i,     j - 1, k    );

	cuDoubleComplex c[NNZ];
	memset(c, 0, NNZ * sizeof(cuDoubleComplex));

	if (mEZ(q, i, j, k) == PEC) {
		c[0] = make_cuDoubleComplex(1, 0);
	}
	else {
		if ((q->Abc == MUR) && (
			(!xpbc && ((i == 0) || (i == nx))) ||
			(!ypbc && ((j == 0) || (j == ny))))) {
			// Mur
			cuDoubleComplex cv = cuCcast(material[mEZ(q, i, j, k) + m0].cv);
			cuDoubleComplex cel = make_cuDoubleComplex(0, 0);
			int mur = 0;
			if      (!xpbc && (i ==  0)) {
				cel = cuCrmul(kdxn[ 0], cv);
				mur = 1;
			}
			else if (!xpbc && (i == nx)) {
				cel = cuCrmul(kdxn[nx], cv);
				mur = 2;
			}
			else if (!ypbc && (j ==  0)) {
				cel = cuCrmul(kdyn[ 0], cv);
				mur = 3;
			}
			else if (!ypbc && (j == ny)) {
				cel = cuCrmul(kdyn[ny], cv);
				mur = 4;
			}
			c[0] = make_cuDoubleComplex(1, 0);
			c[mur] = cuCrmul(-exp(cel.y), cuCexp(-cel.x));
		}
		else if ((q->Abc == PML) &&
			((k < 0) || (k >= nz) || (i <= 0) || (i >= nx) || (j <= 0) || (j >= ny))) {
			// PML
			int ib = MINMAX(0, nx,     i);
			int jb = MINMAX(0, ny,     j);
			int kb = MINMAX(0, nz - 1, k);
			if (mEZ(q, ib, jb, kb) != PEC) {
				d_complex_t cv = material[mEZ(q, ib, jb, kb) + m0].cv;
				cuDoubleComplex vc = cuCinv(make_cuDoubleComplex(cv.r, cv.i));

				cuDoubleComplex xc = cuCdiv(vc, make_cuDoubleComplex(fxpml[(2 * i + 0) + (2 * lpml)], kdxn[ib]));
				cuDoubleComplex xp = cuCdiv(vc, make_cuDoubleComplex(fxpml[(2 * i + 1) + (2 * lpml)], kdxn[ib]));
				cuDoubleComplex xm = cuCdiv(vc, make_cuDoubleComplex(fxpml[(2 * i - 1) + (2 * lpml)], kdxn[ib]));
				cuDoubleComplex yc = cuCdiv(vc, make_cuDoubleComplex(fypml[(2 * j + 0) + (2 * lpml)], kdyn[jb]));
				cuDoubleComplex yp = cuCdiv(vc, make_cuDoubleComplex(fypml[(2 * j + 1) + (2 * lpml)], kdyn[jb]));
				cuDoubleComplex ym = cuCdiv(vc, make_cuDoubleComplex(fypml[(2 * j - 1) + (2 * lpml)], kdyn[jb]));
				cuDoubleComplex zp = cuCdiv(vc, make_cuDoubleComplex(fzpml[(2 * k + 1) + (2 * lpml)], kdzc[kb]));

				c[ 0] = make_cuDoubleComplex(1, 0);
				c[ 1] = cuCneg(cuCmul(xc, xp));
				c[ 2] = cuCneg(cuCmul(xc, xm));
				c[ 3] = cuCneg(cuCmul(yc, yp));
				c[ 4] = cuCneg(cuCmul(yc, ym));
				c[ 5] =        cuCmul(xc, zp);
				c[ 6] = cuCneg(c[ 5]);
				c[ 7] =        c[ 6];
				c[ 8] =        c[ 5];
				c[ 9] =        cuCmul(yc, zp);
				c[10] = cuCneg(c[ 9]);
				c[11] =        c[10];
				c[12] =        c[ 9];
				c[ 0] = cuCsub(c[ 0], cuCadd4(c[ 1], c[ 2], c[ 3], c[ 4]));
			}
		}
		else {
			cuDoubleComplex c0 = cuCcast(material[mEZ(q, i,     j,     k    ) + m0].cepsr);
			cuDoubleComplex c1 = cuCcast(material[mHY(q, i,     j,     k    ) + m0].cnur);
			cuDoubleComplex c2 = cuCcast(material[mHY(q, i - 1, j,     k    ) + m0].cnur);
			cuDoubleComplex c3 = cuCcast(material[mHX(q, i,     j,     k    ) + m0].cnur);
			cuDoubleComplex c4 = cuCcast(material[mHX(q, i,     j - 1, k    ) + m0].cnur);

			c[ 0] = cuCneg(c0);
			c[ 1] = cuCrmul(- krxp[i],           c1);
			c[ 2] = cuCrmul(- krxm[i],           c2);
			c[ 3] = cuCrmul(- kryp[j],           c3);
			c[ 4] = cuCrmul(- krym[j],           c4);
			c[ 5] = cuCrmul(+ krzc[k] * krxn[i], c1);
			c[ 6] = cuCneg(c[ 5]);
			c[ 7] = cuCrmul(- krzc[k] * krxn[i], c2);
			c[ 8] = cuCneg(c[ 7]);
			c[ 9] = cuCrmul(+ krzc[k] * kryn[j], c3);
			c[10] = cuCneg(c[ 9]);
			c[11] = cuCrmul(- krzc[k] * kryn[j], c4);
			c[12] = cuCneg(c[11]);
			c[ 0] = cuCsub(c[ 0], cuCadd4(c[ 1], c[ 2], c[ 3], c[ 4]));
		}
	}

	cuDoubleComplex sf = q->SCALING ? cuCinv(c[0]) : make_cuDoubleComplex(1, 0);
	if (pre) {
		// setup matrix
		if (q->MATRIX) {
			for (int m = 0; m < NNZ; m++) {
				a[(n0 * NNZ) + m] = cuCmul(sf, c[m]);
			}
		}
		// scaling RHS
		b[n0] = cuCmul(sf, b[n0]);
	}
	else {
		// matrix * vector
		cuDoubleComplex sum = make_cuDoubleComplex(0, 0);
		for (int m = 0; m < NNZ; m++) {
			sum = cuCadd(sum, cuCmul(c[m], v[p[m]]));
		}
		u[n0] = cuCmul(sf, sum);
	}
}

__global__
static void matrixEz_gpu(
	int i0, int i1, int j0, int j1, int k0, int k1,
	int pre, int ifreq,
	cuDoubleComplex *a, const cuDoubleComplex *v, cuDoubleComplex *u, cuDoubleComplex *b,
	const double *kdxn, const double *kdyn,
	const double *kdzc,
	const double *krxn, const double *kryn,
	const double *krxp, const double *kryp,
	const double *krxm, const double *krym,
	const double *krzc,
	const material_t *material, const int *me, const int *mh,
	const double *fxpml, const double *fypml, const double *fzpml)
{
	int i = i0 + threadIdx.z + (blockIdx.z * blockDim.z);
	int j = j0 + threadIdx.y + (blockIdx.y * blockDim.y);
	int k = k0 + threadIdx.x + (blockIdx.x * blockDim.x);
	if ((i <= i1) &&
	    (j <= j1) &&
	    (k <= k1)) {
		matrixEz_(
			i, j, k,
			pre, ifreq,
			a, v, u, b,
			kdxn, kdyn,
			kdzc,
			krxn, kryn,
			krxp, kryp,
			krxm, krym,
			krzc,
			material, me, mh,
			fxpml, fypml, fzpml,
			&d_Param);
	}
}

static void matrixEz_cpu(
	int i0, int i1, int j0, int j1, int k0, int k1,
	int pre, int ifreq,
	cuDoubleComplex *a, const cuDoubleComplex *v, cuDoubleComplex *u, cuDoubleComplex *b,
	const double *kdxn, const double *kdyn,
	const double *kdzc,
	const double *krxn, const double *kryn,
	const double *krxp, const double *kryp,
	const double *krxm, const double *krym,
	const double *krzc,
	const material_t *material, const int *me, const int *mh,
	const double *fxpml, const double *fypml, const double *fzpml)
{
	for (int i = i0; i <= i1; i++) {
	for (int j = j0; j <= j1; j++) {
	for (int k = k0; k <= k1; k++) {
		matrixEz_(
			i, j, k,
			pre, ifreq,
			a, v, u, b,
			kdxn, kdyn,
			kdzc,
			krxn, kryn,
			krxp, kryp,
			krxm, krym,
			krzc,
			material, me, mh,
			fxpml, fypml, fzpml,
			&h_Param);
	}
	}
	}
}

void matrixEz(int pre, int ifreq, cuDoubleComplex *a, const cuDoubleComplex *v, cuDoubleComplex *u, cuDoubleComplex *b)
{
	if (GPU) {
		cudaMemcpyToSymbol(d_Param, &h_Param, sizeof(param_t));
		dim3 block(BLOCK_X, BLOCK_Y, 1);
		dim3 grid(
			CEIL(k1Ez - k0Ez + 1, block.x),
			CEIL(j1Ez - j0Ez + 1, block.y),
			CEIL(i1Ez - i0Ez + 1, block.z));
		matrixEz_gpu<<<grid, block>>>(
			i0Ez, i1Ez, j0Ez, j1Ez, k0Ez, k1Ez,
			pre, ifreq,
			a, v, u, b,
			d_KDXn, d_KDYn,
			d_KDZc,
			d_KRXn, d_KRYn,
			d_KRXp, d_KRYp,
			d_KRXm, d_KRYm,
			d_KRZc,
			d_Material, d_mE, d_mH,
			d_Fxpml, d_Fypml, d_Fzpml);
		if (UM) cudaDeviceSynchronize();
	}
	else {
		matrixEz_cpu(
			i0Ez, i1Ez, j0Ez, j1Ez, k0Ez, k1Ez,
			pre, ifreq,
			a, v, u, b,
			KDXn, KDYn,
			KDZc,
			KRXn, KRYn,
			KRXp, KRYp,
			KRXm, KRYm,
			KRZc,
			Material, mE, mH,
			Fxpml, Fypml, Fzpml);
	}
}
