refactor solver (untested)

This commit is contained in:
jan 2016-08-03 14:51:25 -07:00
commit ff3951ba35
5 changed files with 194 additions and 124 deletions

View file

@ -0,0 +1,52 @@
/* Common code for E, H updates
*
* Template parameters:
* ctype string denoting type for storing complex field values
* shape list of 3 ints specifying shape of fields
*/
//Defines to clean up operation names
#define ctype {{ctype}}_t
#define zero {{ctype}}_new(0.0, 0.0)
#define add {{ctype}}_add
#define sub {{ctype}}_sub
#define mul {{ctype}}_mul
// Field sizes
const int sx = {shape[0]};
const int sy = {shape[1]};
const int sz = {shape[2]};
//Since we use i to index into Ex[], E[], ... rather than E[], do nothing if
// i is outside the bounds of Ex[].
if (i >= sx * sy * sz) {
PYOPENCL_ELWISE_CONTINUE;
}
// Given a linear index i and shape (sx, sy, sz), defines x, y, and z
// as the 3D indices of the current element (i).
// (ie, converts linear index [i] to field indices (x, y, z)
const int z = i / (sx * sy);
const int y = (i - z * sx * sy) / sx;
const int x = (i - y * sx - z * sx * sy);
// Calculate linear index offsets corresponding to offsets in 3D
// (ie, if E[i] <-> E(x, y, z), then E[i + diy] <-> E(x, y + 1, z)
const int dix = 1;
const int diy = sx;
const int diz = sx * sy;
//Pointer offsets into the components of a linearized vector-field
// (eg. Hx = H + XX, where H and Hx are pointers)
const int XX = 0;
const int YY = sx * sy * sz;
const int ZZ = sx * sy * sz * 2;
//Define pointers to vector components of each field (eg. Hx = H + XX)
__global ctype *Ex = E + XX;
__global ctype *Ey = E + YY;
__global ctype *Ez = E + ZZ;
__global ctype *Hx = H + XX;
__global ctype *Hy = H + YY;
__global ctype *Hz = H + ZZ;

View file

@ -1,17 +1,39 @@
/*
*
* H update equations
*
* Template parameters:
* mu False if (mu == 1) everywhere
* pmc False if no PMC anywhere
* common_cl Rendered code from common.cl
*
* Arguments:
* ctype *E E-field
* ctype *H H-field
* ctype *inv_mu 1/mu (at H-field locations)
* char *pmc Boolean mask denoting presence of PMC (at H-field locations)
* ctype *inv_dex 1/dx_e (complex cell widths for x direction at E locations)
* ctype *inv_dey 1/dy_e (complex cell widths for y direction at E locations)
* ctype *inv_dez 1/dz_e (complex cell widths for z direction at E locations)
*
*/
//Define sx, x, dix (and y, z versions of those)
{{dixyz_source}}
{{common_cl}}
//Define vectorized fields and pointers (eg. Hx = H + XX)
{{vec_source}}
__global ctype *inv_mu_x = inv_mu + XX;
__global ctype *inv_mu_y = inv_mu + YY;
__global ctype *inv_mu_z = inv_mu + ZZ;
__global ctype *pmc_x = pmc + XX;
__global ctype *pmc_y = pmc + YY;
__global ctype *pmc_z = pmc + ZZ;
// Wrap indices if necessary
/*
* Implement periodic boundary conditions
*
* ipx gives the index of the adjacent cell in the plus-x direction ([i]ndex [p]lus [x]).
* In the event that we start at x == (sx - 1), we actually want to wrap around and grab the cell
* where x == 0 instead, ie. ipx = i - (sx - 1) * dix .
*/
int ipx, ipy, ipz;
if ( x == sx - 1 ) {
ipx = i - (sx - 1) * dix;
@ -32,53 +54,56 @@ if ( z == sz - 1 ) {
}
//Update H components; set them to 0 if PMC is enabled there.
// Also divide by mu only if requested.
//Update H components; set them to 0 if PMC is enabled at that location.
//Mu division and PMC conditional are only included if {{mu}} and {{pmc}} are true
{% if pmc -%}
if (pmc[XX + i] != 0) {
Hx[i] = cdouble_new(0.0, 0.0);
if (pmc_x[i] != 0) {
Hx[i] = zero;
} else
{%- endif -%}
{
cdouble_t Dzy = cdouble_mul(cdouble_sub(Ez[ipy], Ez[i]), inv_dey[y]);
cdouble_t Dyz = cdouble_mul(cdouble_sub(Ey[ipz], Ey[i]), inv_dez[z]);
ctype Dzy = mul(sub(Ez[ipy], Ez[i]), inv_dey[y]);
ctype Dyz = mul(sub(Ey[ipz], Ey[i]), inv_dez[z]);
ctype x_curl = sub(Dzy, Dyz);
{%- if mu -%}
Hx[i] = cdouble_mul(inv_mu[XX + i], cdouble_sub(Dzy, Dyz));
Hx[i] = mul(inv_mu_x[i], x_curl);
{%- else -%}
Hx[i] = cdouble_sub(Dzy, Dyz);
Hx[i] = x_curl;
{%- endif %}
}
{% if pmc -%}
if (pmc[YY + i] != 0) {
Hy[i] = cdouble_new(0.0, 0.0);
if (pmc_y[i] != 0) {
Hy[i] = zero;
} else
{%- endif -%}
{
cdouble_t Dxz = cdouble_mul(cdouble_sub(Ex[ipz], Ex[i]), inv_dez[z]);
cdouble_t Dzx = cdouble_mul(cdouble_sub(Ez[ipx], Ez[i]), inv_dex[x]);
ctype Dxz = mul(sub(Ex[ipz], Ex[i]), inv_dez[z]);
ctype Dzx = mul(sub(Ez[ipx], Ez[i]), inv_dex[x]);
ctype y_curl = sub(Dxz, Dzx);
{%- if mu -%}
Hy[i] = cdouble_mul(inv_mu[YY + i], cdouble_sub(Dxz, Dzx));
Hy[i] = mul(inv_mu_y[i], y_curl);
{%- else -%}
Hy[i] = cdouble_sub(Dxz, Dzx);
Hy[i] = y_curl;
{%- endif %}
}
{% if pmc -%}
if (pmc[ZZ + i] != 0) {
Hz[i] = cdouble_new(0.0, 0.0);
if (pmc_z[i] != 0) {
Hz[i] = zero;
} else
{%- endif -%}
{
cdouble_t Dyx = cdouble_mul(cdouble_sub(Ey[ipx], Ey[i]), inv_dex[x]);
cdouble_t Dxy = cdouble_mul(cdouble_sub(Ex[ipy], Ex[i]), inv_dey[y]);
ctype Dyx = mul(sub(Ey[ipx], Ey[i]), inv_dex[x]);
ctype Dxy = mul(sub(Ex[ipy], Ex[i]), inv_dey[y]);
ctype z_curl = sub(Dyx, Dxy);
{%- if mu -%}
Hz[i] = cdouble_mul(inv_mu[ZZ + i], cdouble_sub(Dyx, Dxy));
Hz[i] = mul(inv_mu_z[i], z_curl);
{%- else -%}
Hz[i] = cdouble_sub(Dyx, Dxy);
Hz[i] = z_curl;
{%- endif %}
}

View file

@ -1,17 +1,45 @@
/*
*
* E update equations
*
* Template parameters:
* pec False if no PEC anywhere
* common_cl Rendered code from common.cl
*
* Arguments:
* ctype *E E-field
* ctype *H H-field
* ctype *oeps omega*epsilon (at E-field locations)
* ctype *Pl Entries of (diagonal) left preconditioner matrix
* char *pec Boolean mask denoting presence of PEC (at E-field locations)
* ctype *inv_dhx 1/dx_h (complex cell widths for x direction at H locations)
* ctype *inv_dhy 1/dy_h (complex cell widths for y direction at H locations)
* ctype *inv_dhz 1/dz_h (complex cell widths for z direction at H locations)
*
*/
//Define sx, x, dix (and y, z versions of those)
{{dixyz_source}}
//Define vectorized fields and pointers (eg. Hx = H + XX)
{{vec_source}}
{{common_cl}}
// Wrap indices if necessary
__global ctype *oeps_x = oeps + XX;
__global ctype *oeps_y = oeps + YY;
__global ctype *oeps_z = oeps + ZZ;
__global ctype *pec_x = pec + XX;
__global ctype *pec_y = pec + YY;
__global ctype *pec_z = pec + ZZ;
__global ctype *Pl_x = Pl + XX;
__global ctype *Pl_y = Pl + YY;
__global ctype *Pl_z = Pl + ZZ;
/*
* Implement periodic boundary conditions
*
* imx gives the index of the adjacent cell in the minus-x direction ([i]ndex [m]inus [x]).
* In the event that we start at x == 0, we actually want to wrap around and grab the cell
* where x == (sx - 1) instead, ie. imx = i + (sx - 1) * dix .
*/
int imx, imy, imz;
if ( x == 0 ) {
imx = i + (sx - 1) * dix;
@ -34,38 +62,38 @@ if ( z == 0 ) {
//Update E components; set them to 0 if PEC is enabled there.
{% if pec -%}
if (pec[XX + i] == 0)
if (pec_x[i] == 0)
{%- endif -%}
{
cdouble_t tEx = cdouble_mul(Ex[i], oeps[XX + i]);
cdouble_t Dzy = cdouble_mul(cdouble_sub(Hz[i], Hz[imy]), inv_dhy[y]);
cdouble_t Dyz = cdouble_mul(cdouble_sub(Hy[i], Hy[imz]), inv_dhz[z]);
tEx = cdouble_add(tEx, cdouble_sub(Dzy, Dyz));
Ex[i] = cdouble_mul(tEx, Pl[XX + i]);
ctype tEx = mul(Ex[i], oeps_x[i]);
ctype Dzy = mul(sub(Hz[i], Hz[imy]), inv_dhy[y]);
ctype Dyz = mul(sub(Hy[i], Hy[imz]), inv_dhz[z]);
tEx = add(tEx, sub(Dzy, Dyz));
Ex[i] = mul(tEx, Pl_x[i]);
}
{% if pec -%}
if (pec[YY + i] == 0)
if (pec_y[i] == 0)
{%- endif -%}
{
cdouble_t tEy = cdouble_mul(Ey[i], oeps[YY + i]);
cdouble_t Dxz = cdouble_mul(cdouble_sub(Hx[i], Hx[imz]), inv_dhz[z]);
cdouble_t Dzx = cdouble_mul(cdouble_sub(Hz[i], Hz[imx]), inv_dhx[x]);
tEy = cdouble_add(tEy, cdouble_sub(Dxz, Dzx));
Ey[i] = cdouble_mul(tEy, Pl[YY + i]);
ctype tEy = mul(Ey[i], oeps_y[i]);
ctype Dxz = mul(sub(Hx[i], Hx[imz]), inv_dhz[z]);
ctype Dzx = mul(sub(Hz[i], Hz[imx]), inv_dhx[x]);
tEy = add(tEy, sub(Dxz, Dzx));
Ey[i] = mul(tEy, Pl_y[i]);
}
{% if pec -%}
if (pec[ZZ + i] == 0)
if (pec_z[i] == 0)
{%- endif -%}
{
cdouble_t tEz = cdouble_mul(Ez[i], oeps[ZZ + i]);
cdouble_t Dyx = cdouble_mul(cdouble_sub(Hy[i], Hy[imx]), inv_dhx[x]);
cdouble_t Dxy = cdouble_mul(cdouble_sub(Hx[i], Hx[imy]), inv_dhy[y]);
tEz = cdouble_add(tEz, cdouble_sub(Dyx, Dxy));
Ez[i] = cdouble_mul(tEz, Pl[ZZ + i]);
ctype tEz = mul(Ez[i], oeps_z[i]);
ctype Dyx = mul(sub(Hy[i], Hy[imx]), inv_dhx[x]);
ctype Dxy = mul(sub(Hx[i], Hx[imy]), inv_dhy[y]);
tEz = add(tEz, sub(Dyx, Dxy));
Ez[i] = mul(tEz, Pl_z[i]);
}
/*
* End H update equations
* End E update equations
*/

View file

@ -1,9 +1,29 @@
/*
* Apply PEC and preconditioner.
*
* Template parameters:
* ctype name of complex type (eg. cdouble)
* pec false iff no PEC anyhwere
*
* Arguments:
* ctype *E (output) E-field
* ctype *Pr Entries of (diagonal) right preconditioner matrix
* ctype *p (input vector)
*
*/
//Defines to clean up operation names
#define ctype {{ctype}}_t
#define zero {{ctype}}_new(0.0, 0.0)
#define mul {{ctype}}_mul
{%- if pec -%}
if (pec[i] != 0) {
E[i] = cdouble_new(0.0, 0.0);
E[i] = zero;
} else
{%- endif -%}
{
E[i] = cdouble_mul(Pr[i], p[i]);
E[i] = mul(Pr[i], p[i]);
}