/*
matrixEx.cu
u = Av (Ex)
*/

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

__host__ __device__ __forceinline__
static void matrixEx_(
	int i, int j, int k,
	int pre, int ifreq,
	cuDoubleComplex *a, const cuDoubleComplex *v, cuDoubleComplex *u, cuDoubleComplex *b,
	const double *kdyn, const double *kdzn,
	const double *kdxc,
	const double *kryn, const double *krzn,
	const double *kryp, const double *krzp,
	const double *krym, const double *krzm,
	const double *krxc,
	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 ypbc = q->Ypbc;
	int zpbc = q->Zpbc;
	int lpml = q->Lpml;
	int m0 = ifreq * q->NMaterial;
	int64_t n0 = nEX(q, i, j, k);

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

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

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

				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 zc = cuCdiv(vc, make_cuDoubleComplex(fzpml[(2 * k + 0) + (2 * lpml)], kdzn[kb]));
				cuDoubleComplex zp = cuCdiv(vc, make_cuDoubleComplex(fzpml[(2 * k + 1) + (2 * lpml)], kdzn[kb]));
				cuDoubleComplex zm = cuCdiv(vc, make_cuDoubleComplex(fzpml[(2 * k - 1) + (2 * lpml)], kdzn[kb]));
				cuDoubleComplex xp = cuCdiv(vc, make_cuDoubleComplex(fxpml[(2 * i + 1) + (2 * lpml)], kdxc[ib]));

				c[ 0] = make_cuDoubleComplex(1, 0);
				c[ 1] = cuCneg(cuCmul(yc, yp));
				c[ 2] = cuCneg(cuCmul(yc, ym));
				c[ 3] = cuCneg(cuCmul(zc, zp));
				c[ 4] = cuCneg(cuCmul(zc, zm));
				c[ 5] =        cuCmul(yc, xp);
				c[ 6] = cuCneg(c[ 5]);
				c[ 7] =        c[ 6];
				c[ 8] =        c[ 5];
				c[ 9] =        cuCmul(zc, xp);
				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[mEX(q, i,     j,     k    ) + m0].cepsr);
			cuDoubleComplex c1 = cuCcast(material[mHZ(q, i,     j,     k    ) + m0].cnur);
			cuDoubleComplex c2 = cuCcast(material[mHZ(q, i,     j - 1, k    ) + m0].cnur);
			cuDoubleComplex c3 = cuCcast(material[mHY(q, i,     j,     k    ) + m0].cnur);
			cuDoubleComplex c4 = cuCcast(material[mHY(q, i,     j,     k - 1) + m0].cnur);

			c[ 0] = cuCneg(c0);
			c[ 1] = cuCrmul(- kryp[j],           c1);
			c[ 2] = cuCrmul(- krym[j],           c2);
			c[ 3] = cuCrmul(- krzp[k],           c3);
			c[ 4] = cuCrmul(- krzm[k],           c4);
			c[ 5] = cuCrmul(+ krxc[i] * kryn[j], c1);
			c[ 6] = cuCneg(c[ 5]);
			c[ 7] = cuCrmul(- krxc[i] * kryn[j], c2);
			c[ 8] = cuCneg(c[ 7]);
			c[ 9] = cuCrmul(+ krxc[i] * krzn[k], c3);
			c[10] = cuCneg(c[ 9]);
			c[11] = cuCrmul(- krxc[i] * krzn[k], 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 matrixEx_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 *kdyn, const double *kdzn,
	const double *kdxc,
	const double *kryn, const double *krzn,
	const double *kryp, const double *krzp,
	const double *krym, const double *krzm,
	const double *krxc,
	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)) {
		matrixEx_(
			i, j, k,
			pre, ifreq,
			a, v, u, b,
			kdyn, kdzn,
			kdxc,
			kryn, krzn,
			kryp, krzp,
			krym, krzm,
			krxc,
			material, me, mh,
			fxpml, fypml, fzpml,
			&d_Param);
	}
}

static void matrixEx_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 *kdyn, const double *kdzn,
	const double *kdxc,
	const double *kryn, const double *krzn,
	const double *kryp, const double *krzp,
	const double *krym, const double *krzm,
	const double *krxc,
	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++) {
		matrixEx_(
			i, j, k,
			pre, ifreq,
			a, v, u, b,
			kdyn, kdzn,
			kdxc,
			kryn, krzn,
			kryp, krzp,
			krym, krzm,
			krxc,
			material, me, mh,
			fxpml, fypml, fzpml,
			&h_Param);
	}
	}
	}
}

void matrixEx(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(k1Ex - k0Ex + 1, block.x),
			CEIL(j1Ex - j0Ex + 1, block.y),
			CEIL(i1Ex - i0Ex + 1, block.z));
		matrixEx_gpu<<<grid, block>>>(
			i0Ex, i1Ex, j0Ex, j1Ex, k0Ex, k1Ex,
			pre, ifreq,
			a, v, u, b,
			d_KDYn, d_KDZn,
			d_KDXc,
			d_KRYn, d_KRZn,
			d_KRYp, d_KRZp,
			d_KRYm, d_KRZm,
			d_KRXc,
			d_Material, d_mE, d_mH,
			d_Fxpml, d_Fypml, d_Fzpml);
		if (UM) cudaDeviceSynchronize();
	}
	else {
		matrixEx_cpu(
			i0Ex, i1Ex, j0Ex, j1Ex, k0Ex, k1Ex,
			pre, ifreq,
			a, v, u, b,
			KDYn, KDZn,
			KDXc,
			KRYn, KRZn,
			KRYp, KRZp,
			KRYm, KRZm,
			KRXc,
			Material, mE, mH,
			Fxpml, Fypml, Fzpml);
	}
}
