xref: /petsc/src/dm/impls/da/dalocal.c (revision a4294c51a225a5f9c7fbddc7b93e89a60c9417ca)
147c6ae99SBarry Smith 
247c6ae99SBarry Smith /*
347c6ae99SBarry Smith   Code for manipulating distributed regular arrays in parallel.
447c6ae99SBarry Smith */
547c6ae99SBarry Smith 
64035e84dSBarry Smith #include <petsc-private/dmdaimpl.h>    /*I   "petscdmda.h"   I*/
7b2fff234SMatthew G. Knepley #include <petscbt.h>
80c312b8eSJed Brown #include <petscsf.h>
947c6ae99SBarry Smith 
1047c6ae99SBarry Smith /*
11e3c5b3baSBarry Smith    This allows the DMDA vectors to properly tell MATLAB their dimensions
1247c6ae99SBarry Smith */
1347c6ae99SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
14c6db04a5SJed Brown #include <engine.h>   /* MATLAB include file */
15c6db04a5SJed Brown #include <mex.h>      /* MATLAB include file */
1647c6ae99SBarry Smith #undef __FUNCT__
1747c6ae99SBarry Smith #define __FUNCT__ "VecMatlabEnginePut_DA2d"
181e6b0712SBarry Smith static PetscErrorCode  VecMatlabEnginePut_DA2d(PetscObject obj,void *mengine)
1947c6ae99SBarry Smith {
2047c6ae99SBarry Smith   PetscErrorCode ierr;
2147c6ae99SBarry Smith   PetscInt       n,m;
2247c6ae99SBarry Smith   Vec            vec = (Vec)obj;
2347c6ae99SBarry Smith   PetscScalar    *array;
2447c6ae99SBarry Smith   mxArray        *mat;
259a42bb27SBarry Smith   DM             da;
2647c6ae99SBarry Smith 
2747c6ae99SBarry Smith   PetscFunctionBegin;
28c688c046SMatthew G Knepley   ierr = VecGetDM(vec, &da);CHKERRQ(ierr);
29ce94432eSBarry Smith   if (!da) SETERRQ(PetscObjectComm((PetscObject)vec),PETSC_ERR_ARG_WRONGSTATE,"Vector not associated with a DMDA");
30aa219208SBarry Smith   ierr = DMDAGetGhostCorners(da,0,0,0,&m,&n,0);CHKERRQ(ierr);
3147c6ae99SBarry Smith 
3247c6ae99SBarry Smith   ierr = VecGetArray(vec,&array);CHKERRQ(ierr);
3347c6ae99SBarry Smith #if !defined(PETSC_USE_COMPLEX)
3447c6ae99SBarry Smith   mat = mxCreateDoubleMatrix(m,n,mxREAL);
3547c6ae99SBarry Smith #else
3647c6ae99SBarry Smith   mat = mxCreateDoubleMatrix(m,n,mxCOMPLEX);
3747c6ae99SBarry Smith #endif
3847c6ae99SBarry Smith   ierr = PetscMemcpy(mxGetPr(mat),array,n*m*sizeof(PetscScalar));CHKERRQ(ierr);
3947c6ae99SBarry Smith   ierr = PetscObjectName(obj);CHKERRQ(ierr);
4047c6ae99SBarry Smith   engPutVariable((Engine*)mengine,obj->name,mat);
4147c6ae99SBarry Smith 
4247c6ae99SBarry Smith   ierr = VecRestoreArray(vec,&array);CHKERRQ(ierr);
4347c6ae99SBarry Smith   PetscFunctionReturn(0);
4447c6ae99SBarry Smith }
4547c6ae99SBarry Smith #endif
4647c6ae99SBarry Smith 
4747c6ae99SBarry Smith 
4847c6ae99SBarry Smith #undef __FUNCT__
49564755cdSBarry Smith #define __FUNCT__ "DMCreateLocalVector_DA"
507087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector_DA(DM da,Vec *g)
5147c6ae99SBarry Smith {
5247c6ae99SBarry Smith   PetscErrorCode ierr;
5347c6ae99SBarry Smith   DM_DA          *dd = (DM_DA*)da->data;
5447c6ae99SBarry Smith 
5547c6ae99SBarry Smith   PetscFunctionBegin;
5647c6ae99SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
5747c6ae99SBarry Smith   PetscValidPointer(g,2);
5811689aebSJed Brown   if (da->defaultSection) {
5911689aebSJed Brown     ierr = DMCreateLocalVector_Section_Private(da,g);CHKERRQ(ierr);
6011689aebSJed Brown   } else {
6147c6ae99SBarry Smith     ierr = VecCreate(PETSC_COMM_SELF,g);CHKERRQ(ierr);
6247c6ae99SBarry Smith     ierr = VecSetSizes(*g,dd->nlocal,PETSC_DETERMINE);CHKERRQ(ierr);
6347c6ae99SBarry Smith     ierr = VecSetBlockSize(*g,dd->w);CHKERRQ(ierr);
64401ddaa8SBarry Smith     ierr = VecSetType(*g,da->vectype);CHKERRQ(ierr);
65c688c046SMatthew G Knepley     ierr = VecSetDM(*g, da);CHKERRQ(ierr);
6647c6ae99SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
6747c6ae99SBarry Smith     if (dd->w == 1  && dd->dim == 2) {
68bdf89e91SBarry Smith       ierr = PetscObjectComposeFunction((PetscObject)*g,"PetscMatlabEnginePut_C",VecMatlabEnginePut_DA2d);CHKERRQ(ierr);
6947c6ae99SBarry Smith     }
7047c6ae99SBarry Smith #endif
7111689aebSJed Brown   }
7247c6ae99SBarry Smith   PetscFunctionReturn(0);
7347c6ae99SBarry Smith }
7447c6ae99SBarry Smith 
75a66d4d66SMatthew G Knepley #undef __FUNCT__
7657459e9aSMatthew G Knepley #define __FUNCT__ "DMDAGetNumCells"
77e42e3c58SMatthew G. Knepley PetscErrorCode DMDAGetNumCells(DM dm, PetscInt *numCellsX, PetscInt *numCellsY, PetscInt *numCellsZ, PetscInt *numCells)
7857459e9aSMatthew G Knepley {
7957459e9aSMatthew G Knepley   DM_DA          *da = (DM_DA*) dm->data;
8057459e9aSMatthew G Knepley   const PetscInt dim = da->dim;
8157459e9aSMatthew G Knepley   const PetscInt mx  = (da->Xe - da->Xs)/da->w, my = da->Ye - da->Ys, mz = da->Ze - da->Zs;
8257459e9aSMatthew G Knepley   const PetscInt nC  = (mx)*(dim > 1 ? (my)*(dim > 2 ? (mz) : 1) : 1);
8357459e9aSMatthew G Knepley 
8457459e9aSMatthew G Knepley   PetscFunctionBegin;
85e42e3c58SMatthew G. Knepley   if (numCellsX) {
86e42e3c58SMatthew G. Knepley     PetscValidIntPointer(numCellsX,2);
87e42e3c58SMatthew G. Knepley     *numCellsX = mx;
88e42e3c58SMatthew G. Knepley   }
89e42e3c58SMatthew G. Knepley   if (numCellsY) {
90e42e3c58SMatthew G. Knepley     PetscValidIntPointer(numCellsX,3);
91e42e3c58SMatthew G. Knepley     *numCellsY = my;
92e42e3c58SMatthew G. Knepley   }
93e42e3c58SMatthew G. Knepley   if (numCellsZ) {
94e42e3c58SMatthew G. Knepley     PetscValidIntPointer(numCellsX,4);
95e42e3c58SMatthew G. Knepley     *numCellsZ = mz;
96e42e3c58SMatthew G. Knepley   }
9757459e9aSMatthew G Knepley   if (numCells) {
98e42e3c58SMatthew G. Knepley     PetscValidIntPointer(numCells,5);
9957459e9aSMatthew G Knepley     *numCells = nC;
10057459e9aSMatthew G Knepley   }
10157459e9aSMatthew G Knepley   PetscFunctionReturn(0);
10257459e9aSMatthew G Knepley }
10357459e9aSMatthew G Knepley 
10457459e9aSMatthew G Knepley #undef __FUNCT__
10557459e9aSMatthew G Knepley #define __FUNCT__ "DMDAGetNumVertices"
10657459e9aSMatthew G Knepley PetscErrorCode DMDAGetNumVertices(DM dm, PetscInt *numVerticesX, PetscInt *numVerticesY, PetscInt *numVerticesZ, PetscInt *numVertices)
10757459e9aSMatthew G Knepley {
10857459e9aSMatthew G Knepley   DM_DA          *da = (DM_DA*) dm->data;
10957459e9aSMatthew G Knepley   const PetscInt dim = da->dim;
11057459e9aSMatthew G Knepley   const PetscInt mx  = (da->Xe - da->Xs)/da->w, my = da->Ye - da->Ys, mz = da->Ze - da->Zs;
11157459e9aSMatthew G Knepley   const PetscInt nVx = mx+1;
11257459e9aSMatthew G Knepley   const PetscInt nVy = dim > 1 ? (my+1) : 1;
11357459e9aSMatthew G Knepley   const PetscInt nVz = dim > 2 ? (mz+1) : 1;
11457459e9aSMatthew G Knepley   const PetscInt nV  = nVx*nVy*nVz;
11557459e9aSMatthew G Knepley 
11657459e9aSMatthew G Knepley   PetscFunctionBegin;
11757459e9aSMatthew G Knepley   if (numVerticesX) {
11857459e9aSMatthew G Knepley     PetscValidIntPointer(numVerticesX,2);
11957459e9aSMatthew G Knepley     *numVerticesX = nVx;
12057459e9aSMatthew G Knepley   }
12157459e9aSMatthew G Knepley   if (numVerticesY) {
12257459e9aSMatthew G Knepley     PetscValidIntPointer(numVerticesY,3);
12357459e9aSMatthew G Knepley     *numVerticesY = nVy;
12457459e9aSMatthew G Knepley   }
12557459e9aSMatthew G Knepley   if (numVerticesZ) {
12657459e9aSMatthew G Knepley     PetscValidIntPointer(numVerticesZ,4);
12757459e9aSMatthew G Knepley     *numVerticesZ = nVz;
12857459e9aSMatthew G Knepley   }
12957459e9aSMatthew G Knepley   if (numVertices) {
13057459e9aSMatthew G Knepley     PetscValidIntPointer(numVertices,5);
13157459e9aSMatthew G Knepley     *numVertices = nV;
13257459e9aSMatthew G Knepley   }
13357459e9aSMatthew G Knepley   PetscFunctionReturn(0);
13457459e9aSMatthew G Knepley }
13557459e9aSMatthew G Knepley 
13657459e9aSMatthew G Knepley #undef __FUNCT__
13757459e9aSMatthew G Knepley #define __FUNCT__ "DMDAGetNumFaces"
13857459e9aSMatthew G Knepley PetscErrorCode DMDAGetNumFaces(DM dm, PetscInt *numXFacesX, PetscInt *numXFaces, PetscInt *numYFacesY, PetscInt *numYFaces, PetscInt *numZFacesZ, PetscInt *numZFaces)
13957459e9aSMatthew G Knepley {
14057459e9aSMatthew G Knepley   DM_DA          *da = (DM_DA*) dm->data;
14157459e9aSMatthew G Knepley   const PetscInt dim = da->dim;
14257459e9aSMatthew G Knepley   const PetscInt mx  = (da->Xe - da->Xs)/da->w, my = da->Ye - da->Ys, mz = da->Ze - da->Zs;
14357459e9aSMatthew G Knepley   const PetscInt nxF = (dim > 1 ? (my)*(dim > 2 ? (mz) : 1) : 1);
14457459e9aSMatthew G Knepley   const PetscInt nXF = (mx+1)*nxF;
14557459e9aSMatthew G Knepley   const PetscInt nyF = mx*(dim > 2 ? mz : 1);
14657459e9aSMatthew G Knepley   const PetscInt nYF = dim > 1 ? (my+1)*nyF : 0;
14757459e9aSMatthew G Knepley   const PetscInt nzF = mx*(dim > 1 ? my : 0);
14857459e9aSMatthew G Knepley   const PetscInt nZF = dim > 2 ? (mz+1)*nzF : 0;
14957459e9aSMatthew G Knepley 
15057459e9aSMatthew G Knepley   PetscFunctionBegin;
15157459e9aSMatthew G Knepley   if (numXFacesX) {
15257459e9aSMatthew G Knepley     PetscValidIntPointer(numXFacesX,2);
15357459e9aSMatthew G Knepley     *numXFacesX = nxF;
15457459e9aSMatthew G Knepley   }
15557459e9aSMatthew G Knepley   if (numXFaces) {
15657459e9aSMatthew G Knepley     PetscValidIntPointer(numXFaces,3);
15757459e9aSMatthew G Knepley     *numXFaces = nXF;
15857459e9aSMatthew G Knepley   }
15957459e9aSMatthew G Knepley   if (numYFacesY) {
16057459e9aSMatthew G Knepley     PetscValidIntPointer(numYFacesY,4);
16157459e9aSMatthew G Knepley     *numYFacesY = nyF;
16257459e9aSMatthew G Knepley   }
16357459e9aSMatthew G Knepley   if (numYFaces) {
16457459e9aSMatthew G Knepley     PetscValidIntPointer(numYFaces,5);
16557459e9aSMatthew G Knepley     *numYFaces = nYF;
16657459e9aSMatthew G Knepley   }
16757459e9aSMatthew G Knepley   if (numZFacesZ) {
16857459e9aSMatthew G Knepley     PetscValidIntPointer(numZFacesZ,6);
16957459e9aSMatthew G Knepley     *numZFacesZ = nzF;
17057459e9aSMatthew G Knepley   }
17157459e9aSMatthew G Knepley   if (numZFaces) {
17257459e9aSMatthew G Knepley     PetscValidIntPointer(numZFaces,7);
17357459e9aSMatthew G Knepley     *numZFaces = nZF;
17457459e9aSMatthew G Knepley   }
17557459e9aSMatthew G Knepley   PetscFunctionReturn(0);
17657459e9aSMatthew G Knepley }
17757459e9aSMatthew G Knepley 
17857459e9aSMatthew G Knepley #undef __FUNCT__
17957459e9aSMatthew G Knepley #define __FUNCT__ "DMDAGetHeightStratum"
18057459e9aSMatthew G Knepley PetscErrorCode DMDAGetHeightStratum(DM dm, PetscInt height, PetscInt *pStart, PetscInt *pEnd)
18157459e9aSMatthew G Knepley {
18257459e9aSMatthew G Knepley   DM_DA          *da = (DM_DA*) dm->data;
18357459e9aSMatthew G Knepley   const PetscInt dim = da->dim;
18457459e9aSMatthew G Knepley   PetscInt       nC, nV, nXF, nYF, nZF;
18557459e9aSMatthew G Knepley   PetscErrorCode ierr;
18657459e9aSMatthew G Knepley 
18757459e9aSMatthew G Knepley   PetscFunctionBegin;
1888865f1eaSKarl Rupp   if (pStart) PetscValidIntPointer(pStart,3);
1898865f1eaSKarl Rupp   if (pEnd)   PetscValidIntPointer(pEnd,4);
190e42e3c58SMatthew G. Knepley   ierr = DMDAGetNumCells(dm, NULL, NULL, NULL, &nC);CHKERRQ(ierr);
1910298fd71SBarry Smith   ierr = DMDAGetNumVertices(dm, NULL, NULL, NULL, &nV);CHKERRQ(ierr);
1920298fd71SBarry Smith   ierr = DMDAGetNumFaces(dm, NULL, &nXF, NULL, &nYF, NULL, &nZF);CHKERRQ(ierr);
19357459e9aSMatthew G Knepley   if (height == 0) {
19457459e9aSMatthew G Knepley     /* Cells */
1958865f1eaSKarl Rupp     if (pStart) *pStart = 0;
1968865f1eaSKarl Rupp     if (pEnd)   *pEnd   = nC;
19757459e9aSMatthew G Knepley   } else if (height == 1) {
19857459e9aSMatthew G Knepley     /* Faces */
1998865f1eaSKarl Rupp     if (pStart) *pStart = nC+nV;
2008865f1eaSKarl Rupp     if (pEnd)   *pEnd   = nC+nV+nXF+nYF+nZF;
20157459e9aSMatthew G Knepley   } else if (height == dim) {
20257459e9aSMatthew G Knepley     /* Vertices */
2038865f1eaSKarl Rupp     if (pStart) *pStart = nC;
2048865f1eaSKarl Rupp     if (pEnd)   *pEnd   = nC+nV;
20557459e9aSMatthew G Knepley   } else if (height < 0) {
20657459e9aSMatthew G Knepley     /* All points */
2078865f1eaSKarl Rupp     if (pStart) *pStart = 0;
2088865f1eaSKarl Rupp     if (pEnd)   *pEnd   = nC+nV+nXF+nYF+nZF;
20982f516ccSBarry Smith   } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No points of height %d in the DA", height);
21057459e9aSMatthew G Knepley   PetscFunctionReturn(0);
21157459e9aSMatthew G Knepley }
21257459e9aSMatthew G Knepley 
21357459e9aSMatthew G Knepley #undef __FUNCT__
214d0892670SMatthew G. Knepley #define __FUNCT__ "DMDAGetDepthStratum"
215d0892670SMatthew G. Knepley PetscErrorCode DMDAGetDepthStratum(DM dm, PetscInt depth, PetscInt *pStart, PetscInt *pEnd)
216d0892670SMatthew G. Knepley {
217d0892670SMatthew G. Knepley   DM_DA         *da  = (DM_DA*) dm->data;
218d0892670SMatthew G. Knepley   const PetscInt dim = da->dim;
219d0892670SMatthew G. Knepley   PetscInt       nC, nV, nXF, nYF, nZF;
220d0892670SMatthew G. Knepley   PetscErrorCode ierr;
221d0892670SMatthew G. Knepley 
222d0892670SMatthew G. Knepley   PetscFunctionBegin;
223d0892670SMatthew G. Knepley   if (pStart) PetscValidIntPointer(pStart,3);
224d0892670SMatthew G. Knepley   if (pEnd)   PetscValidIntPointer(pEnd,4);
225d0892670SMatthew G. Knepley   ierr = DMDAGetNumCells(dm, NULL, NULL, NULL, &nC);CHKERRQ(ierr);
226d0892670SMatthew G. Knepley   ierr = DMDAGetNumVertices(dm, NULL, NULL, NULL, &nV);CHKERRQ(ierr);
227d0892670SMatthew G. Knepley   ierr = DMDAGetNumFaces(dm, NULL, &nXF, NULL, &nYF, NULL, &nZF);CHKERRQ(ierr);
228d0892670SMatthew G. Knepley   if (depth == dim) {
229d0892670SMatthew G. Knepley     /* Cells */
230d0892670SMatthew G. Knepley     if (pStart) *pStart = 0;
231d0892670SMatthew G. Knepley     if (pEnd)   *pEnd   = nC;
232d0892670SMatthew G. Knepley   } else if (depth == dim-1) {
233d0892670SMatthew G. Knepley     /* Faces */
234d0892670SMatthew G. Knepley     if (pStart) *pStart = nC+nV;
235d0892670SMatthew G. Knepley     if (pEnd)   *pEnd   = nC+nV+nXF+nYF+nZF;
236d0892670SMatthew G. Knepley   } else if (depth == 0) {
237d0892670SMatthew G. Knepley     /* Vertices */
238d0892670SMatthew G. Knepley     if (pStart) *pStart = nC;
239d0892670SMatthew G. Knepley     if (pEnd)   *pEnd   = nC+nV;
240d0892670SMatthew G. Knepley   } else if (depth < 0) {
241d0892670SMatthew G. Knepley     /* All points */
242d0892670SMatthew G. Knepley     if (pStart) *pStart = 0;
243d0892670SMatthew G. Knepley     if (pEnd)   *pEnd   = nC+nV+nXF+nYF+nZF;
244d0892670SMatthew G. Knepley   } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No points of depth %d in the DA", depth);
245d0892670SMatthew G. Knepley   PetscFunctionReturn(0);
246d0892670SMatthew G. Knepley }
247d0892670SMatthew G. Knepley 
248d0892670SMatthew G. Knepley #undef __FUNCT__
249d0892670SMatthew G. Knepley #define __FUNCT__ "DMDAGetConeSize"
250d0892670SMatthew G. Knepley PetscErrorCode DMDAGetConeSize(DM dm, PetscInt p, PetscInt *coneSize)
251d0892670SMatthew G. Knepley {
252d0892670SMatthew G. Knepley   DM_DA         *da  = (DM_DA*) dm->data;
253d0892670SMatthew G. Knepley   const PetscInt dim = da->dim;
254d0892670SMatthew G. Knepley   PetscInt       nC, nV, nXF, nYF, nZF;
255d0892670SMatthew G. Knepley   PetscErrorCode ierr;
256d0892670SMatthew G. Knepley 
257d0892670SMatthew G. Knepley   PetscFunctionBegin;
258d0892670SMatthew G. Knepley   *coneSize = 0;
259d0892670SMatthew G. Knepley   ierr = DMDAGetNumCells(dm, NULL, NULL, NULL, &nC);CHKERRQ(ierr);
260d0892670SMatthew G. Knepley   ierr = DMDAGetNumVertices(dm, NULL, NULL, NULL, &nV);CHKERRQ(ierr);
261d0892670SMatthew G. Knepley   ierr = DMDAGetNumFaces(dm, NULL, &nXF, NULL, &nYF, NULL, &nZF);CHKERRQ(ierr);
262d0892670SMatthew G. Knepley   switch (dim) {
263d0892670SMatthew G. Knepley   case 2:
264d0892670SMatthew G. Knepley     if (p >= 0) {
265d0892670SMatthew G. Knepley       if (p < nC) {
266d0892670SMatthew G. Knepley         *coneSize = 4;
267d0892670SMatthew G. Knepley       } else if (p < nC+nV) {
268d0892670SMatthew G. Knepley         *coneSize = 0;
269d0892670SMatthew G. Knepley       } else if (p < nC+nV+nXF+nYF+nZF) {
270d0892670SMatthew G. Knepley         *coneSize = 2;
271d0892670SMatthew G. Knepley       } else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d should be in [0, %d)", p, nC+nV+nXF+nYF+nZF);
272d0892670SMatthew G. Knepley     } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative point %d is invalid", p);
273d0892670SMatthew G. Knepley     break;
274d0892670SMatthew G. Knepley   case 3:
275d0892670SMatthew G. Knepley     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Too lazy to do 3D");
276d0892670SMatthew G. Knepley     break;
277d0892670SMatthew G. Knepley   }
278d0892670SMatthew G. Knepley   PetscFunctionReturn(0);
279d0892670SMatthew G. Knepley }
280d0892670SMatthew G. Knepley 
281d0892670SMatthew G. Knepley #undef __FUNCT__
282d0892670SMatthew G. Knepley #define __FUNCT__ "DMDAGetCone"
283d0892670SMatthew G. Knepley PetscErrorCode DMDAGetCone(DM dm, PetscInt p, PetscInt *cone[])
284d0892670SMatthew G. Knepley {
285d0892670SMatthew G. Knepley   DM_DA         *da  = (DM_DA*) dm->data;
286d0892670SMatthew G. Knepley   const PetscInt dim = da->dim;
287d0892670SMatthew G. Knepley   PetscInt       nCx, nCy, nCz, nC, nVx, nVy, nVz, nV, nxF, nyF, nzF, nXF, nYF, nZF;
288d0892670SMatthew G. Knepley   PetscErrorCode ierr;
289d0892670SMatthew G. Knepley 
290d0892670SMatthew G. Knepley   PetscFunctionBegin;
291d0892670SMatthew G. Knepley   if (!cone) {ierr = DMGetWorkArray(dm, 6, PETSC_INT, cone);CHKERRQ(ierr);}
292d0892670SMatthew G. Knepley   ierr = DMDAGetNumCells(dm, &nCx, &nCy, &nCz, &nC);CHKERRQ(ierr);
293d0892670SMatthew G. Knepley   ierr = DMDAGetNumVertices(dm, &nVx, &nVy, &nVz, &nV);CHKERRQ(ierr);
294d0892670SMatthew G. Knepley   ierr = DMDAGetNumFaces(dm, &nxF, &nXF, &nyF, &nYF, &nzF, &nZF);CHKERRQ(ierr);
295d0892670SMatthew G. Knepley   switch (dim) {
296d0892670SMatthew G. Knepley   case 2:
297d0892670SMatthew G. Knepley     if (p >= 0) {
298d0892670SMatthew G. Knepley       if (p < nC) {
299d0892670SMatthew G. Knepley         const PetscInt cy = p / nCx;
300d0892670SMatthew G. Knepley         const PetscInt cx = p % nCx;
301d0892670SMatthew G. Knepley 
302d0892670SMatthew G. Knepley         (*cone)[0] = cy*nxF + cx + nC+nV;
303d0892670SMatthew G. Knepley         (*cone)[1] = cx*nyF + cy + nyF + nC+nV+nXF;
304d0892670SMatthew G. Knepley         (*cone)[2] = cy*nxF + cx + nxF + nC+nV;
305d0892670SMatthew G. Knepley         (*cone)[3] = cx*nyF + cy + nC+nV+nXF;
306d0892670SMatthew G. Knepley         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Too lazy to do cell cones");
307d0892670SMatthew G. Knepley       } else if (p < nC+nV) {
308d0892670SMatthew G. Knepley       } else if (p < nC+nV+nXF) {
309d0892670SMatthew G. Knepley         const PetscInt fy = (p - nC+nV) / nxF;
310d0892670SMatthew G. Knepley         const PetscInt fx = (p - nC+nV) % nxF;
311d0892670SMatthew G. Knepley 
312d0892670SMatthew G. Knepley         (*cone)[0] = fy*nVx + fx + nC;
313d0892670SMatthew G. Knepley         (*cone)[1] = fy*nVx + fx + 1 + nC;
314d0892670SMatthew G. Knepley       } else if (p < nC+nV+nXF+nYF) {
315d0892670SMatthew G. Knepley         const PetscInt fx = (p - nC+nV+nXF) / nyF;
316d0892670SMatthew G. Knepley         const PetscInt fy = (p - nC+nV+nXF) % nyF;
317d0892670SMatthew G. Knepley 
318d0892670SMatthew G. Knepley         (*cone)[0] = fy*nVx + fx + nC;
319d0892670SMatthew G. Knepley         (*cone)[1] = fy*nVx + fx + nVx + nC;
320d0892670SMatthew G. Knepley       } else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d should be in [0, %d)", p, nC+nV+nXF+nYF+nZF);
321d0892670SMatthew G. Knepley     } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative point %d is invalid", p);
322d0892670SMatthew G. Knepley     break;
323d0892670SMatthew G. Knepley   case 3:
324d0892670SMatthew G. Knepley     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Too lazy to do 3D");
325d0892670SMatthew G. Knepley     break;
326d0892670SMatthew G. Knepley   }
327d0892670SMatthew G. Knepley   PetscFunctionReturn(0);
328d0892670SMatthew G. Knepley }
329d0892670SMatthew G. Knepley 
330d0892670SMatthew G. Knepley #undef __FUNCT__
331d0892670SMatthew G. Knepley #define __FUNCT__ "DMDARestoreCone"
332d0892670SMatthew G. Knepley PetscErrorCode DMDARestoreCone(DM dm, PetscInt p, PetscInt *cone[])
333d0892670SMatthew G. Knepley {
334d0892670SMatthew G. Knepley   PetscErrorCode ierr;
335d0892670SMatthew G. Knepley 
336d0892670SMatthew G. Knepley   PetscFunctionBegin;
337d0892670SMatthew G. Knepley   ierr = DMGetWorkArray(dm, 6, PETSC_INT, cone);CHKERRQ(ierr);
338d0892670SMatthew G. Knepley   PetscFunctionReturn(0);
339d0892670SMatthew G. Knepley }
340d0892670SMatthew G. Knepley 
341d0892670SMatthew G. Knepley #undef __FUNCT__
342a66d4d66SMatthew G Knepley #define __FUNCT__ "DMDACreateSection"
343a66d4d66SMatthew G Knepley /*@C
344a66d4d66SMatthew G Knepley   DMDACreateSection - Create a PetscSection inside the DMDA that describes data layout. This allows multiple fields with
345a66d4d66SMatthew G Knepley   different numbers of dofs on vertices, cells, and faces in each direction.
346a66d4d66SMatthew G Knepley 
347a66d4d66SMatthew G Knepley   Input Parameters:
348a66d4d66SMatthew G Knepley + dm- The DMDA
349a66d4d66SMatthew G Knepley . numFields - The number of fields
350*a4294c51SMatthew G. Knepley . numComp - The number of components in each field
351*a4294c51SMatthew G. Knepley . numDof - The number of dofs per dimension for each field
3520298fd71SBarry Smith . numFaceDof - The number of dofs per face for each field and direction, or NULL
353a66d4d66SMatthew G Knepley 
354a66d4d66SMatthew G Knepley   Level: developer
355a66d4d66SMatthew G Knepley 
356a66d4d66SMatthew G Knepley   Note:
357a66d4d66SMatthew G Knepley   The default DMDA numbering is as follows:
358a66d4d66SMatthew G Knepley 
359a66d4d66SMatthew G Knepley     - Cells:    [0,             nC)
360a66d4d66SMatthew G Knepley     - Vertices: [nC,            nC+nV)
36188ed4aceSMatthew G Knepley     - X-Faces:  [nC+nV,         nC+nV+nXF)         normal is +- x-dir
36288ed4aceSMatthew G Knepley     - Y-Faces:  [nC+nV+nXF,     nC+nV+nXF+nYF)     normal is +- y-dir
36388ed4aceSMatthew G Knepley     - Z-Faces:  [nC+nV+nXF+nYF, nC+nV+nXF+nYF+nZF) normal is +- z-dir
364a66d4d66SMatthew G Knepley 
365a66d4d66SMatthew G Knepley   We interpret the default DMDA partition as a cell partition, and the data assignment as a cell assignment.
366a66d4d66SMatthew G Knepley @*/
367*a4294c51SMatthew G. Knepley PetscErrorCode DMDACreateSection(DM dm, PetscInt numComp[], PetscInt numDof[], PetscInt numFaceDof[], PetscSection *s)
368a66d4d66SMatthew G Knepley {
369a66d4d66SMatthew G Knepley   DM_DA            *da  = (DM_DA*) dm->data;
370a4b60ecfSMatthew G. Knepley   PetscSection      section;
37188ed4aceSMatthew G Knepley   const PetscInt    dim = da->dim;
37280800b1aSMatthew G Knepley   PetscInt          numFields, numVertexTotDof = 0, numCellTotDof = 0, numFaceTotDof[3] = {0, 0, 0};
373b2fff234SMatthew G. Knepley   PetscBT           isLeaf;
37488ed4aceSMatthew G Knepley   PetscSF           sf;
375feafbc5cSMatthew G Knepley   PetscMPIInt       rank;
37688ed4aceSMatthew G Knepley   const PetscMPIInt *neighbors;
37788ed4aceSMatthew G Knepley   PetscInt          *localPoints;
37888ed4aceSMatthew G Knepley   PetscSFNode       *remotePoints;
379f5eeb527SMatthew G Knepley   PetscInt          nleaves = 0,  nleavesCheck = 0, nL = 0;
38057459e9aSMatthew G Knepley   PetscInt          nC, nVx, nVy, nVz, nV, nxF, nXF, nyF, nYF, nzF, nZF;
38157459e9aSMatthew G Knepley   PetscInt          pStart, pEnd, cStart, cEnd, vStart, vEnd, fStart, fEnd, xfStart, xfEnd, yfStart, yfEnd, zfStart, zfEnd;
38288ed4aceSMatthew G Knepley   PetscInt          f, v, c, xf, yf, zf, xn, yn, zn;
383a66d4d66SMatthew G Knepley   PetscErrorCode    ierr;
384a66d4d66SMatthew G Knepley 
385a66d4d66SMatthew G Knepley   PetscFunctionBegin;
386a66d4d66SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
387e42e3c58SMatthew G. Knepley   PetscValidPointer(s, 4);
38882f516ccSBarry Smith   ierr    = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
389e42e3c58SMatthew G. Knepley   ierr    = DMDAGetNumCells(dm, NULL, NULL, NULL, &nC);CHKERRQ(ierr);
39057459e9aSMatthew G Knepley   ierr    = DMDAGetNumVertices(dm, &nVx, &nVy, &nVz, &nV);CHKERRQ(ierr);
39157459e9aSMatthew G Knepley   ierr    = DMDAGetNumFaces(dm, &nxF, &nXF, &nyF, &nYF, &nzF, &nZF);CHKERRQ(ierr);
39257459e9aSMatthew G Knepley   ierr    = DMDAGetHeightStratum(dm, -1,  &pStart, &pEnd);CHKERRQ(ierr);
39357459e9aSMatthew G Knepley   ierr    = DMDAGetHeightStratum(dm, 0,   &cStart, &cEnd);CHKERRQ(ierr);
39457459e9aSMatthew G Knepley   ierr    = DMDAGetHeightStratum(dm, 1,   &fStart, &fEnd);CHKERRQ(ierr);
39557459e9aSMatthew G Knepley   ierr    = DMDAGetHeightStratum(dm, dim, &vStart, &vEnd);CHKERRQ(ierr);
39657459e9aSMatthew G Knepley   xfStart = vEnd;  xfEnd = xfStart+nXF;
39757459e9aSMatthew G Knepley   yfStart = xfEnd; yfEnd = yfStart+nYF;
39857459e9aSMatthew G Knepley   zfStart = yfEnd; zfEnd = zfStart+nZF;
39982f516ccSBarry Smith   if (zfEnd != fEnd) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid face end %d, should be %d", zfEnd, fEnd);
40088ed4aceSMatthew G Knepley   /* Create local section */
40180800b1aSMatthew G Knepley   ierr = DMDAGetInfo(dm, 0,0,0,0,0,0,0, &numFields, 0,0,0,0,0);CHKERRQ(ierr);
402a66d4d66SMatthew G Knepley   for (f = 0; f < numFields; ++f) {
403*a4294c51SMatthew G. Knepley     numVertexTotDof  += numDof[f*(dim+1)+0];
404*a4294c51SMatthew G. Knepley     numCellTotDof    += numDof[f*(dim+1)+dim];
405*a4294c51SMatthew G. Knepley     numFaceTotDof[0] += dim > 0 ? (numFaceDof ? numFaceDof[f*dim+0] : numDof[f*(dim+1)+dim-1]) : 0;
406*a4294c51SMatthew G. Knepley     numFaceTotDof[1] += dim > 1 ? (numFaceDof ? numFaceDof[f*dim+1] : numDof[f*(dim+1)+dim-1]) : 0;
407*a4294c51SMatthew G. Knepley     numFaceTotDof[2] += dim > 2 ? (numFaceDof ? numFaceDof[f*dim+2] : numDof[f*(dim+1)+dim-1]) : 0;
408a66d4d66SMatthew G Knepley   }
409a4b60ecfSMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
410*a4294c51SMatthew G. Knepley   if (numFields > 0) {
411a4b60ecfSMatthew G. Knepley     ierr = PetscSectionSetNumFields(section, numFields);CHKERRQ(ierr);
412a66d4d66SMatthew G Knepley     for (f = 0; f < numFields; ++f) {
41380800b1aSMatthew G Knepley       const char *name;
41480800b1aSMatthew G Knepley 
41580800b1aSMatthew G Knepley       ierr = DMDAGetFieldName(dm, f, &name);CHKERRQ(ierr);
416*a4294c51SMatthew G. Knepley       ierr = PetscSectionSetFieldName(section, f, name ? name : "Field");CHKERRQ(ierr);
41780800b1aSMatthew G Knepley       if (numComp) {
418a4b60ecfSMatthew G. Knepley         ierr = PetscSectionSetFieldComponents(section, f, numComp[f]);CHKERRQ(ierr);
419a66d4d66SMatthew G Knepley       }
420a66d4d66SMatthew G Knepley     }
421a66d4d66SMatthew G Knepley   }
422a4b60ecfSMatthew G. Knepley   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
423a66d4d66SMatthew G Knepley   for (v = vStart; v < vEnd; ++v) {
424a66d4d66SMatthew G Knepley     for (f = 0; f < numFields; ++f) {
425*a4294c51SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(section, v, f, numDof[f*(dim+1)+0]);CHKERRQ(ierr);
426a66d4d66SMatthew G Knepley     }
427a4b60ecfSMatthew G. Knepley     ierr = PetscSectionSetDof(section, v, numVertexTotDof);CHKERRQ(ierr);
428a66d4d66SMatthew G Knepley   }
429a66d4d66SMatthew G Knepley   for (xf = xfStart; xf < xfEnd; ++xf) {
430a66d4d66SMatthew G Knepley     for (f = 0; f < numFields; ++f) {
431*a4294c51SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(section, xf, f, numFaceDof ? numFaceDof[f*dim+0] : numDof[f*(dim+1)+dim-1]);CHKERRQ(ierr);
432a66d4d66SMatthew G Knepley     }
433a4b60ecfSMatthew G. Knepley     ierr = PetscSectionSetDof(section, xf, numFaceTotDof[0]);CHKERRQ(ierr);
434a66d4d66SMatthew G Knepley   }
435a66d4d66SMatthew G Knepley   for (yf = yfStart; yf < yfEnd; ++yf) {
436a66d4d66SMatthew G Knepley     for (f = 0; f < numFields; ++f) {
437*a4294c51SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(section, yf, f, numFaceDof ? numFaceDof[f*dim+1] : numDof[f*(dim+1)+dim-1]);CHKERRQ(ierr);
438a66d4d66SMatthew G Knepley     }
439a4b60ecfSMatthew G. Knepley     ierr = PetscSectionSetDof(section, yf, numFaceTotDof[1]);CHKERRQ(ierr);
440a66d4d66SMatthew G Knepley   }
441a66d4d66SMatthew G Knepley   for (zf = zfStart; zf < zfEnd; ++zf) {
442a66d4d66SMatthew G Knepley     for (f = 0; f < numFields; ++f) {
443*a4294c51SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(section, zf, f, numFaceDof ? numFaceDof[f*dim+2] : numDof[f*(dim+1)+dim-1]);CHKERRQ(ierr);
444a66d4d66SMatthew G Knepley     }
445a4b60ecfSMatthew G. Knepley     ierr = PetscSectionSetDof(section, zf, numFaceTotDof[2]);CHKERRQ(ierr);
446a66d4d66SMatthew G Knepley   }
447a66d4d66SMatthew G Knepley   for (c = cStart; c < cEnd; ++c) {
448a66d4d66SMatthew G Knepley     for (f = 0; f < numFields; ++f) {
449*a4294c51SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(section, c, f, numDof[f*(dim+1)+dim]);CHKERRQ(ierr);
450a66d4d66SMatthew G Knepley     }
451a4b60ecfSMatthew G. Knepley     ierr = PetscSectionSetDof(section, c, numCellTotDof);CHKERRQ(ierr);
452a66d4d66SMatthew G Knepley   }
453a4b60ecfSMatthew G. Knepley   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
45488ed4aceSMatthew G Knepley   /* Create mesh point SF */
455b2fff234SMatthew G. Knepley   ierr = PetscBTCreate(pEnd-pStart, &isLeaf);CHKERRQ(ierr);
45688ed4aceSMatthew G Knepley   ierr = DMDAGetNeighbors(dm, &neighbors);CHKERRQ(ierr);
45788ed4aceSMatthew G Knepley   for (zn = 0; zn < (dim > 2 ? 3 : 1); ++zn) {
45888ed4aceSMatthew G Knepley     for (yn = 0; yn < (dim > 1 ? 3 : 1); ++yn) {
45988ed4aceSMatthew G Knepley       for (xn = 0; xn < 3; ++xn) {
4607128ae9fSMatthew G Knepley         const PetscInt xp       = xn-1, yp = dim > 1 ? yn-1 : 0, zp = dim > 2 ? zn-1 : 0;
46188ed4aceSMatthew G Knepley         const PetscInt neighbor = neighbors[(zn*3+yn)*3+xn];
462b2fff234SMatthew G. Knepley         PetscInt       xv, yv, zv;
46388ed4aceSMatthew G Knepley 
4643814d604SMatthew G Knepley         if (neighbor >= 0 && neighbor < rank) {
465b2fff234SMatthew G. Knepley           if (xp < 0) { /* left */
466b2fff234SMatthew G. Knepley             if (yp < 0) { /* bottom */
467b2fff234SMatthew G. Knepley               if (zp < 0) { /* back */
468b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = (      0*nVy +     0)*nVx +     0 + nC; /* left bottom back vertex */
469b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
470b2fff234SMatthew G. Knepley               } else if (zp > 0) { /* front */
471b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = ((nVz-1)*nVy +     0)*nVx +     0 + nC; /* left bottom front vertex */
472b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
473b2fff234SMatthew G. Knepley               } else {
474b2fff234SMatthew G. Knepley                 for (zv = 0; zv < nVz; ++zv) {
475b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (zv*nVy +     0)*nVx +     0 + nC; /* left bottom vertices */
476b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
477b2fff234SMatthew G. Knepley                 }
478b2fff234SMatthew G. Knepley               }
479b2fff234SMatthew G. Knepley             } else if (yp > 0) { /* top */
480b2fff234SMatthew G. Knepley               if (zp < 0) { /* back */
481b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = (      0*nVy + nVy-1)*nVx +     0 + nC; /* left top back vertex */
482b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
483b2fff234SMatthew G. Knepley               } else if (zp > 0) { /* front */
484b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = ((nVz-1)*nVy + nVy-1)*nVx +     0 + nC; /* left top front vertex */
485b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
486b2fff234SMatthew G. Knepley               } else {
487b2fff234SMatthew G. Knepley                 for (zv = 0; zv < nVz; ++zv) {
488b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (zv*nVy + nVy-1)*nVx +     0 + nC; /* left top vertices */
489b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
490b2fff234SMatthew G. Knepley                 }
491b2fff234SMatthew G. Knepley               }
492b2fff234SMatthew G. Knepley             } else {
493b2fff234SMatthew G. Knepley               if (zp < 0) { /* back */
494b2fff234SMatthew G. Knepley                 for (yv = 0; yv < nVy; ++yv) {
495b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (      0*nVy + yv)*nVx +     0 + nC; /* left back vertices */
496b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
497b2fff234SMatthew G. Knepley                 }
498b2fff234SMatthew G. Knepley               } else if (zp > 0) { /* front */
499b2fff234SMatthew G. Knepley                 for (yv = 0; yv < nVy; ++yv) {
500b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = ((nVz-1)*nVy + yv)*nVx +     0 + nC; /* left front vertices */
501b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
502b2fff234SMatthew G. Knepley                 }
503b2fff234SMatthew G. Knepley               } else {
504b2fff234SMatthew G. Knepley                 for (zv = 0; zv < nVz; ++zv) {
505b2fff234SMatthew G. Knepley                   for (yv = 0; yv < nVy; ++yv) {
506b2fff234SMatthew G. Knepley                     const PetscInt localVertex  = (zv*nVy + yv)*nVx +     0 + nC; /* left vertices */
507b2fff234SMatthew G. Knepley                     if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
508b2fff234SMatthew G. Knepley                   }
509b2fff234SMatthew G. Knepley                 }
510b2fff234SMatthew G. Knepley #if 0
511b2fff234SMatthew G. Knepley                 for (xf = 0; xf < nxF; ++xf) {
512b2fff234SMatthew G. Knepley                   /* THIS IS WRONG */
513b2fff234SMatthew G. Knepley                   const PetscInt localFace  = 0 + nC+nV; /* left faces */
514b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localFace)) ++nleaves;
515b2fff234SMatthew G. Knepley                 }
516b2fff234SMatthew G. Knepley #endif
517b2fff234SMatthew G. Knepley               }
518b2fff234SMatthew G. Knepley             }
519b2fff234SMatthew G. Knepley           } else if (xp > 0) { /* right */
520b2fff234SMatthew G. Knepley             if (yp < 0) { /* bottom */
521b2fff234SMatthew G. Knepley               if (zp < 0) { /* back */
522b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = (      0*nVy +     0)*nVx + nVx-1 + nC; /* right bottom back vertex */
523b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
524b2fff234SMatthew G. Knepley               } else if (zp > 0) { /* front */
525b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = ((nVz-1)*nVy +     0)*nVx + nVx-1 + nC; /* right bottom front vertex */
526b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
527b2fff234SMatthew G. Knepley               } else {
528b2fff234SMatthew G. Knepley                 for (zv = 0; zv < nVz; ++zv) {
529b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (zv*nVy +     0)*nVx + nVx-1 + nC; /* right bottom vertices */
530b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
531b2fff234SMatthew G. Knepley                 }
532b2fff234SMatthew G. Knepley               }
533b2fff234SMatthew G. Knepley             } else if (yp > 0) { /* top */
534b2fff234SMatthew G. Knepley               if (zp < 0) { /* back */
535b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = (      0*nVy + nVy-1)*nVx + nVx-1 + nC; /* right top back vertex */
536b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
537b2fff234SMatthew G. Knepley               } else if (zp > 0) { /* front */
538b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = ((nVz-1)*nVy + nVy-1)*nVx + nVx-1 + nC; /* right top front vertex */
539b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
540b2fff234SMatthew G. Knepley               } else {
541b2fff234SMatthew G. Knepley                 for (zv = 0; zv < nVz; ++zv) {
542b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (zv*nVy + nVy-1)*nVx + nVx-1 + nC; /* right top vertices */
543b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
544b2fff234SMatthew G. Knepley                 }
545b2fff234SMatthew G. Knepley               }
546b2fff234SMatthew G. Knepley             } else {
547b2fff234SMatthew G. Knepley               if (zp < 0) { /* back */
548b2fff234SMatthew G. Knepley                 for (yv = 0; yv < nVy; ++yv) {
549b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (      0*nVy + yv)*nVx + nVx-1 + nC; /* right back vertices */
550b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
551b2fff234SMatthew G. Knepley                 }
552b2fff234SMatthew G. Knepley               } else if (zp > 0) { /* front */
553b2fff234SMatthew G. Knepley                 for (yv = 0; yv < nVy; ++yv) {
554b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = ((nVz-1)*nVy + yv)*nVx + nVx-1 + nC; /* right front vertices */
555b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
556b2fff234SMatthew G. Knepley                 }
557b2fff234SMatthew G. Knepley               } else {
558b2fff234SMatthew G. Knepley                 for (zv = 0; zv < nVz; ++zv) {
559b2fff234SMatthew G. Knepley                   for (yv = 0; yv < nVy; ++yv) {
560b2fff234SMatthew G. Knepley                     const PetscInt localVertex  = (zv*nVy + yv)*nVx + nVx-1 + nC; /* right vertices */
561b2fff234SMatthew G. Knepley                     if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
562b2fff234SMatthew G. Knepley                   }
563b2fff234SMatthew G. Knepley                 }
564b2fff234SMatthew G. Knepley #if 0
565b2fff234SMatthew G. Knepley                 for (xf = 0; xf < nxF; ++xf) {
566b2fff234SMatthew G. Knepley                   /* THIS IS WRONG */
567b2fff234SMatthew G. Knepley                   const PetscInt localFace  = 0 + nC+nV; /* right faces */
568b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localFace)) ++nleaves;
569b2fff234SMatthew G. Knepley                 }
570b2fff234SMatthew G. Knepley #endif
571b2fff234SMatthew G. Knepley               }
572b2fff234SMatthew G. Knepley             }
573b2fff234SMatthew G. Knepley           } else {
574b2fff234SMatthew G. Knepley             if (yp < 0) { /* bottom */
575b2fff234SMatthew G. Knepley               if (zp < 0) { /* back */
576b2fff234SMatthew G. Knepley                 for (xv = 0; xv < nVx; ++xv) {
577b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (      0*nVy +     0)*nVx + xv + nC; /* bottom back vertices */
578b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
579b2fff234SMatthew G. Knepley                 }
580b2fff234SMatthew G. Knepley               } else if (zp > 0) { /* front */
581b2fff234SMatthew G. Knepley                 for (xv = 0; xv < nVx; ++xv) {
582b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = ((nVz-1)*nVy +     0)*nVx + xv + nC; /* bottom front vertices */
583b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
584b2fff234SMatthew G. Knepley                 }
585b2fff234SMatthew G. Knepley               } else {
586b2fff234SMatthew G. Knepley                 for (zv = 0; zv < nVz; ++zv) {
587b2fff234SMatthew G. Knepley                   for (xv = 0; xv < nVx; ++xv) {
588b2fff234SMatthew G. Knepley                     const PetscInt localVertex  = (zv*nVy +     0)*nVx + xv + nC; /* bottom vertices */
589b2fff234SMatthew G. Knepley                     if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
590b2fff234SMatthew G. Knepley                   }
591b2fff234SMatthew G. Knepley                 }
592b2fff234SMatthew G. Knepley #if 0
593b2fff234SMatthew G. Knepley                 for (yf = 0; yf < nyF; ++yf) {
594b2fff234SMatthew G. Knepley                   /* THIS IS WRONG */
595b2fff234SMatthew G. Knepley                   const PetscInt localFace  = 0 + nC+nV; /* bottom faces */
596b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVFace)) ++nleaves;
597b2fff234SMatthew G. Knepley                 }
598b2fff234SMatthew G. Knepley #endif
599b2fff234SMatthew G. Knepley               }
600b2fff234SMatthew G. Knepley             } else if (yp > 0) { /* top */
601b2fff234SMatthew G. Knepley               if (zp < 0) { /* back */
602b2fff234SMatthew G. Knepley                 for (xv = 0; xv < nVx; ++xv) {
603b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (      0*nVy + nVy-1)*nVx + xv + nC; /* top back vertices */
604b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
605b2fff234SMatthew G. Knepley                 }
606b2fff234SMatthew G. Knepley               } else if (zp > 0) { /* front */
607b2fff234SMatthew G. Knepley                 for (xv = 0; xv < nVx; ++xv) {
608b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = ((nVz-1)*nVy + nVy-1)*nVx + xv + nC; /* top front vertices */
609b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
610b2fff234SMatthew G. Knepley                 }
611b2fff234SMatthew G. Knepley               } else {
612b2fff234SMatthew G. Knepley                 for (zv = 0; zv < nVz; ++zv) {
613b2fff234SMatthew G. Knepley                   for (xv = 0; xv < nVx; ++xv) {
614b2fff234SMatthew G. Knepley                     const PetscInt localVertex  = (zv*nVy + nVy-1)*nVx + xv + nC; /* top vertices */
615b2fff234SMatthew G. Knepley                     if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
616b2fff234SMatthew G. Knepley                   }
617b2fff234SMatthew G. Knepley                 }
618b2fff234SMatthew G. Knepley #if 0
619b2fff234SMatthew G. Knepley                 for (yf = 0; yf < nyF; ++yf) {
620b2fff234SMatthew G. Knepley                   /* THIS IS WRONG */
621b2fff234SMatthew G. Knepley                   const PetscInt localFace  = 0 + nC+nV; /* top faces */
622b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVFace)) ++nleaves;
623b2fff234SMatthew G. Knepley                 }
624b2fff234SMatthew G. Knepley #endif
625b2fff234SMatthew G. Knepley               }
626b2fff234SMatthew G. Knepley             } else {
627b2fff234SMatthew G. Knepley               if (zp < 0) { /* back */
628b2fff234SMatthew G. Knepley                 for (yv = 0; yv < nVy; ++yv) {
629b2fff234SMatthew G. Knepley                   for (xv = 0; xv < nVx; ++xv) {
630b2fff234SMatthew G. Knepley                     const PetscInt localVertex  = (      0*nVy + yv)*nVx + xv + nC; /* back vertices */
631b2fff234SMatthew G. Knepley                     if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
632b2fff234SMatthew G. Knepley                   }
633b2fff234SMatthew G. Knepley                 }
634b2fff234SMatthew G. Knepley #if 0
635b2fff234SMatthew G. Knepley                 for (zf = 0; zf < nzF; ++zf) {
636b2fff234SMatthew G. Knepley                   /* THIS IS WRONG */
637b2fff234SMatthew G. Knepley                   const PetscInt localFace  = 0 + nC+nV; /* back faces */
638b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localFace)) ++nleaves;
639b2fff234SMatthew G. Knepley                 }
640b2fff234SMatthew G. Knepley #endif
641b2fff234SMatthew G. Knepley               } else if (zp > 0) { /* front */
642b2fff234SMatthew G. Knepley                 for (yv = 0; yv < nVy; ++yv) {
643b2fff234SMatthew G. Knepley                   for (xv = 0; xv < nVx; ++xv) {
644b2fff234SMatthew G. Knepley                     const PetscInt localVertex  = ((nVz-1)*nVy + yv)*nVx + xv + nC; /* front vertices */
645b2fff234SMatthew G. Knepley                     if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
646b2fff234SMatthew G. Knepley                   }
647b2fff234SMatthew G. Knepley                 }
648b2fff234SMatthew G. Knepley #if 0
649b2fff234SMatthew G. Knepley                 for (zf = 0; zf < nzF; ++zf) {
650b2fff234SMatthew G. Knepley                   /* THIS IS WRONG */
651b2fff234SMatthew G. Knepley                   const PetscInt localFace  = 0 + nC+nV; /* front faces */
652b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localFace)) ++nleaves;
653b2fff234SMatthew G. Knepley                 }
654b2fff234SMatthew G. Knepley #endif
655b2fff234SMatthew G. Knepley               } else {
656b2fff234SMatthew G. Knepley                 /* Nothing is shared from the interior */
65788ed4aceSMatthew G Knepley               }
65888ed4aceSMatthew G Knepley             }
65988ed4aceSMatthew G Knepley           }
66088ed4aceSMatthew G Knepley         }
66188ed4aceSMatthew G Knepley       }
662b2fff234SMatthew G. Knepley     }
663b2fff234SMatthew G. Knepley   }
664b2fff234SMatthew G. Knepley   ierr = PetscBTMemzero(pEnd-pStart, isLeaf);CHKERRQ(ierr);
66588ed4aceSMatthew G Knepley   ierr = PetscMalloc2(nleaves,PetscInt,&localPoints,nleaves,PetscSFNode,&remotePoints);CHKERRQ(ierr);
66688ed4aceSMatthew G Knepley   for (zn = 0; zn < (dim > 2 ? 3 : 1); ++zn) {
66788ed4aceSMatthew G Knepley     for (yn = 0; yn < (dim > 1 ? 3 : 1); ++yn) {
66888ed4aceSMatthew G Knepley       for (xn = 0; xn < 3; ++xn) {
6697128ae9fSMatthew G Knepley         const PetscInt xp       = xn-1, yp = dim > 1 ? yn-1 : 0, zp = dim > 2 ? zn-1 : 0;
67088ed4aceSMatthew G Knepley         const PetscInt neighbor = neighbors[(zn*3+yn)*3+xn];
671f5eeb527SMatthew G Knepley         PetscInt       xv, yv, zv;
67288ed4aceSMatthew G Knepley 
6733814d604SMatthew G Knepley         if (neighbor >= 0 && neighbor < rank) {
67488ed4aceSMatthew G Knepley           if (xp < 0) { /* left */
67588ed4aceSMatthew G Knepley             if (yp < 0) { /* bottom */
67688ed4aceSMatthew G Knepley               if (zp < 0) { /* back */
677b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = (      0*nVy +     0)*nVx +     0 + nC; /* left bottom back vertex */
678f5eeb527SMatthew G Knepley                 const PetscInt remoteVertex = ((nVz-1)*nVy + nVy-1)*nVx + nVx-1 + nC; /* TODO: Correct this for neighbor sizes */
679f5eeb527SMatthew G Knepley 
680b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) {
681f5eeb527SMatthew G Knepley                   localPoints[nL]        = localVertex;
682f5eeb527SMatthew G Knepley                   remotePoints[nL].rank  = neighbor;
683f5eeb527SMatthew G Knepley                   remotePoints[nL].index = remoteVertex;
684f5eeb527SMatthew G Knepley                   ++nL;
685b2fff234SMatthew G. Knepley                 }
68688ed4aceSMatthew G Knepley               } else if (zp > 0) { /* front */
687b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = ((nVz-1)*nVy +     0)*nVx +     0 + nC; /* left bottom front vertex */
688f5eeb527SMatthew G Knepley                 const PetscInt remoteVertex = (      0*nVy + nVy-1)*nVx + nVx-1 + nC; /* TODO: Correct this for neighbor sizes */
689f5eeb527SMatthew G Knepley 
690b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) {
691f5eeb527SMatthew G Knepley                   localPoints[nL]        = localVertex;
692f5eeb527SMatthew G Knepley                   remotePoints[nL].rank  = neighbor;
693f5eeb527SMatthew G Knepley                   remotePoints[nL].index = remoteVertex;
694f5eeb527SMatthew G Knepley                   ++nL;
695b2fff234SMatthew G. Knepley                 }
69688ed4aceSMatthew G Knepley               } else {
697b2fff234SMatthew G. Knepley                 for (zv = 0; zv < nVz; ++zv) {
698b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (zv*nVy +     0)*nVx +     0 + nC; /* left bottom vertices */
699f5eeb527SMatthew G Knepley                   const PetscInt remoteVertex = (zv*nVy + nVy-1)*nVx + nVx-1 + nC; /* TODO: Correct this for neighbor sizes */
700f5eeb527SMatthew G Knepley 
701b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) {
702f5eeb527SMatthew G Knepley                     localPoints[nL]        = localVertex;
703f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
704f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteVertex;
705b2fff234SMatthew G. Knepley                     ++nL;
706b2fff234SMatthew G. Knepley                   }
707f5eeb527SMatthew G Knepley                 }
70888ed4aceSMatthew G Knepley               }
70988ed4aceSMatthew G Knepley             } else if (yp > 0) { /* top */
71088ed4aceSMatthew G Knepley               if (zp < 0) { /* back */
711b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = (      0*nVy + nVy-1)*nVx +     0 + nC; /* left top back vertex */
712f5eeb527SMatthew G Knepley                 const PetscInt remoteVertex = ((nVz-1)*nVy +     0)*nVx + nVx-1 + nC; /* TODO: Correct this for neighbor sizes */
713f5eeb527SMatthew G Knepley 
714b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) {
715f5eeb527SMatthew G Knepley                   localPoints[nL]        = localVertex;
716f5eeb527SMatthew G Knepley                   remotePoints[nL].rank  = neighbor;
717f5eeb527SMatthew G Knepley                   remotePoints[nL].index = remoteVertex;
718f5eeb527SMatthew G Knepley                   ++nL;
719b2fff234SMatthew G. Knepley                 }
72088ed4aceSMatthew G Knepley               } else if (zp > 0) { /* front */
721b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = ((nVz-1)*nVy + nVy-1)*nVx +     0 + nC; /* left top front vertex */
722f5eeb527SMatthew G Knepley                 const PetscInt remoteVertex = (      0*nVy +     0)*nVx + nVx-1 + nC; /* TODO: Correct this for neighbor sizes */
723f5eeb527SMatthew G Knepley 
724b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) {
725f5eeb527SMatthew G Knepley                   localPoints[nL]        = localVertex;
726f5eeb527SMatthew G Knepley                   remotePoints[nL].rank  = neighbor;
727f5eeb527SMatthew G Knepley                   remotePoints[nL].index = remoteVertex;
728f5eeb527SMatthew G Knepley                   ++nL;
729b2fff234SMatthew G. Knepley                 }
73088ed4aceSMatthew G Knepley               } else {
731b2fff234SMatthew G. Knepley                 for (zv = 0; zv < nVz; ++zv) {
732b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (zv*nVy + nVy-1)*nVx +     0 + nC; /* left top vertices */
733f5eeb527SMatthew G Knepley                   const PetscInt remoteVertex = (zv*nVy +     0)*nVx + nVx-1 + nC; /* TODO: Correct this for neighbor sizes */
734f5eeb527SMatthew G Knepley 
735b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) {
736f5eeb527SMatthew G Knepley                     localPoints[nL]        = localVertex;
737f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
738f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteVertex;
739b2fff234SMatthew G. Knepley                     ++nL;
740b2fff234SMatthew G. Knepley                   }
741f5eeb527SMatthew G Knepley                 }
74288ed4aceSMatthew G Knepley               }
74388ed4aceSMatthew G Knepley             } else {
74488ed4aceSMatthew G Knepley               if (zp < 0) { /* back */
745b2fff234SMatthew G. Knepley                 for (yv = 0; yv < nVy; ++yv) {
746b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (      0*nVy + yv)*nVx +     0 + nC; /* left back vertices */
747f5eeb527SMatthew G Knepley                   const PetscInt remoteVertex = ((nVz-1)*nVy + yv)*nVx + nVx-1 + nC; /* TODO: Correct this for neighbor sizes */
748f5eeb527SMatthew G Knepley 
749b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) {
750f5eeb527SMatthew G Knepley                     localPoints[nL]        = localVertex;
751f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
752f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteVertex;
753b2fff234SMatthew G. Knepley                     ++nL;
754b2fff234SMatthew G. Knepley                   }
755f5eeb527SMatthew G Knepley                 }
75688ed4aceSMatthew G Knepley               } else if (zp > 0) { /* front */
757b2fff234SMatthew G. Knepley                 for (yv = 0; yv < nVy; ++yv) {
758b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = ((nVz-1)*nVy + yv)*nVx +     0 + nC; /* left front vertices */
759f5eeb527SMatthew G Knepley                   const PetscInt remoteVertex = (      0*nVy + yv)*nVx + nVx-1 + nC; /* TODO: Correct this for neighbor sizes */
760f5eeb527SMatthew G Knepley 
761b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) {
762f5eeb527SMatthew G Knepley                     localPoints[nL]        = localVertex;
763f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
764f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteVertex;
765b2fff234SMatthew G. Knepley                     ++nL;
766b2fff234SMatthew G. Knepley                   }
767f5eeb527SMatthew G Knepley                 }
76888ed4aceSMatthew G Knepley               } else {
769f5eeb527SMatthew G Knepley                 for (zv = 0; zv < nVz; ++zv) {
770b2fff234SMatthew G. Knepley                   for (yv = 0; yv < nVy; ++yv) {
771b2fff234SMatthew G. Knepley                     const PetscInt localVertex  = (zv*nVy + yv)*nVx +     0 + nC; /* left vertices */
772f5eeb527SMatthew G Knepley                     const PetscInt remoteVertex = (zv*nVy + yv)*nVx + nVx-1 + nC; /* TODO: Correct this for neighbor sizes */
773f5eeb527SMatthew G Knepley 
774b2fff234SMatthew G. Knepley                     if (!PetscBTLookupSet(isLeaf, localVertex)) {
775f5eeb527SMatthew G Knepley                       localPoints[nL]        = localVertex;
776f5eeb527SMatthew G Knepley                       remotePoints[nL].rank  = neighbor;
777f5eeb527SMatthew G Knepley                       remotePoints[nL].index = remoteVertex;
778b2fff234SMatthew G. Knepley                       ++nL;
779f5eeb527SMatthew G Knepley                     }
780f5eeb527SMatthew G Knepley                   }
781b2fff234SMatthew G. Knepley                 }
782b2fff234SMatthew G. Knepley #if 0
783b2fff234SMatthew G. Knepley                 for (xf = 0; xf < nxF; ++xf) {
784f5eeb527SMatthew G Knepley                   /* THIS IS WRONG */
785b2fff234SMatthew G. Knepley                   const PetscInt localFace  = 0 + nC+nV; /* left faces */
786f5eeb527SMatthew G Knepley                   const PetscInt remoteFace = 0 + nC+nV;
787f5eeb527SMatthew G Knepley 
788b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localFace)) {
789f5eeb527SMatthew G Knepley                     localPoints[nL]        = localFace;
790f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
791f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteFace;
792f5eeb527SMatthew G Knepley                   }
79388ed4aceSMatthew G Knepley                 }
794b2fff234SMatthew G. Knepley #endif
795b2fff234SMatthew G. Knepley               }
79688ed4aceSMatthew G Knepley             }
79788ed4aceSMatthew G Knepley           } else if (xp > 0) { /* right */
79888ed4aceSMatthew G Knepley             if (yp < 0) { /* bottom */
79988ed4aceSMatthew G Knepley               if (zp < 0) { /* back */
800b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = (      0*nVy +     0)*nVx + nVx-1 + nC; /* right bottom back vertex */
801f5eeb527SMatthew G Knepley                 const PetscInt remoteVertex = ((nVz-1)*nVy + nVy-1)*nVx +     0 + nC; /* TODO: Correct this for neighbor sizes */
802f5eeb527SMatthew G Knepley 
803b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) {
804f5eeb527SMatthew G Knepley                   localPoints[nL]        = localVertex;
805f5eeb527SMatthew G Knepley                   remotePoints[nL].rank  = neighbor;
806f5eeb527SMatthew G Knepley                   remotePoints[nL].index = remoteVertex;
807f5eeb527SMatthew G Knepley                   ++nL;
808b2fff234SMatthew G. Knepley                 }
80988ed4aceSMatthew G Knepley               } else if (zp > 0) { /* front */
810b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = ((nVz-1)*nVy +     0)*nVx + nVx-1 + nC; /* right bottom front vertex */
811f5eeb527SMatthew G Knepley                 const PetscInt remoteVertex = (      0*nVy + nVy-1)*nVx +     0 + nC; /* TODO: Correct this for neighbor sizes */
812f5eeb527SMatthew G Knepley 
813b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) {
814f5eeb527SMatthew G Knepley                   localPoints[nL]        = localVertex;
815f5eeb527SMatthew G Knepley                   remotePoints[nL].rank  = neighbor;
816f5eeb527SMatthew G Knepley                   remotePoints[nL].index = remoteVertex;
817f5eeb527SMatthew G Knepley                   ++nL;
818b2fff234SMatthew G. Knepley                 }
81988ed4aceSMatthew G Knepley               } else {
820b2fff234SMatthew G. Knepley                 nleavesCheck += nVz;
821b2fff234SMatthew G. Knepley                 for (zv = 0; zv < nVz; ++zv) {
822b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (zv*nVy +     0)*nVx + nVx-1 + nC; /* right bottom vertices */
823f5eeb527SMatthew G Knepley                   const PetscInt remoteVertex = (zv*nVy + nVy-1)*nVx +     0 + nC; /* TODO: Correct this for neighbor sizes */
824f5eeb527SMatthew G Knepley 
825b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) {
826f5eeb527SMatthew G Knepley                     localPoints[nL]        = localVertex;
827f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
828f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteVertex;
829b2fff234SMatthew G. Knepley                     ++nL;
830b2fff234SMatthew G. Knepley                   }
831f5eeb527SMatthew G Knepley                 }
83288ed4aceSMatthew G Knepley               }
83388ed4aceSMatthew G Knepley             } else if (yp > 0) { /* top */
83488ed4aceSMatthew G Knepley               if (zp < 0) { /* back */
835b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = (      0*nVy + nVy-1)*nVx + nVx-1 + nC; /* right top back vertex */
836f5eeb527SMatthew G Knepley                 const PetscInt remoteVertex = ((nVz-1)*nVy +     0)*nVx +     0 + nC; /* TODO: Correct this for neighbor sizes */
837f5eeb527SMatthew G Knepley 
838b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) {
839f5eeb527SMatthew G Knepley                   localPoints[nL]        = localVertex;
840f5eeb527SMatthew G Knepley                   remotePoints[nL].rank  = neighbor;
841f5eeb527SMatthew G Knepley                   remotePoints[nL].index = remoteVertex;
842f5eeb527SMatthew G Knepley                   ++nL;
843b2fff234SMatthew G. Knepley                 }
84488ed4aceSMatthew G Knepley               } else if (zp > 0) { /* front */
845b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = ((nVz-1)*nVy + nVy-1)*nVx + nVx-1 + nC; /* right top front vertex */
846f5eeb527SMatthew G Knepley                 const PetscInt remoteVertex = (      0*nVy +     0)*nVx +     0 + nC; /* TODO: Correct this for neighbor sizes */
847f5eeb527SMatthew G Knepley 
848b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) {
849f5eeb527SMatthew G Knepley                   localPoints[nL]        = localVertex;
850f5eeb527SMatthew G Knepley                   remotePoints[nL].rank  = neighbor;
851f5eeb527SMatthew G Knepley                   remotePoints[nL].index = remoteVertex;
852f5eeb527SMatthew G Knepley                   ++nL;
853b2fff234SMatthew G. Knepley                 }
85488ed4aceSMatthew G Knepley               } else {
855b2fff234SMatthew G. Knepley                 for (zv = 0; zv < nVz; ++zv) {
856b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (zv*nVy + nVy-1)*nVx + nVx-1 + nC; /* right top vertices */
857f5eeb527SMatthew G Knepley                   const PetscInt remoteVertex = (zv*nVy +     0)*nVx +     0 + nC; /* TODO: Correct this for neighbor sizes */
858f5eeb527SMatthew G Knepley 
859b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) {
860f5eeb527SMatthew G Knepley                     localPoints[nL]        = localVertex;
861f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
862f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteVertex;
863b2fff234SMatthew G. Knepley                     ++nL;
864b2fff234SMatthew G. Knepley                   }
865f5eeb527SMatthew G Knepley                 }
86688ed4aceSMatthew G Knepley               }
86788ed4aceSMatthew G Knepley             } else {
86888ed4aceSMatthew G Knepley               if (zp < 0) { /* back */
869b2fff234SMatthew G. Knepley                 for (yv = 0; yv < nVy; ++yv) {
870b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (      0*nVy + yv)*nVx + nVx-1 + nC; /* right back vertices */
871f5eeb527SMatthew G Knepley                   const PetscInt remoteVertex = ((nVz-1)*nVy + yv)*nVx +     0 + nC; /* TODO: Correct this for neighbor sizes */
872f5eeb527SMatthew G Knepley 
873b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) {
874f5eeb527SMatthew G Knepley                     localPoints[nL]        = localVertex;
875f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
876f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteVertex;
877b2fff234SMatthew G. Knepley                     ++nL;
878b2fff234SMatthew G. Knepley                   }
879f5eeb527SMatthew G Knepley                 }
88088ed4aceSMatthew G Knepley               } else if (zp > 0) { /* front */
881b2fff234SMatthew G. Knepley                 for (yv = 0; yv < nVy; ++yv) {
882b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = ((nVz-1)*nVy + yv)*nVx + nVx-1 + nC; /* right front vertices */
883f5eeb527SMatthew G Knepley                   const PetscInt remoteVertex = (      0*nVy + yv)*nVx +     0 + nC; /* TODO: Correct this for neighbor sizes */
884f5eeb527SMatthew G Knepley 
885b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) {
886f5eeb527SMatthew G Knepley                     localPoints[nL]        = localVertex;
887f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
888f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteVertex;
889b2fff234SMatthew G. Knepley                     ++nL;
890b2fff234SMatthew G. Knepley                   }
891f5eeb527SMatthew G Knepley                 }
89288ed4aceSMatthew G Knepley               } else {
893f5eeb527SMatthew G Knepley                 for (zv = 0; zv < nVz; ++zv) {
894b2fff234SMatthew G. Knepley                   for (yv = 0; yv < nVy; ++yv) {
895b2fff234SMatthew G. Knepley                     const PetscInt localVertex  = (zv*nVy + yv)*nVx + nVx-1 + nC; /* right vertices */
896f5eeb527SMatthew G Knepley                     const PetscInt remoteVertex = (zv*nVy + yv)*nVx + 0     + nC; /* TODO: Correct this for neighbor sizes */
897f5eeb527SMatthew G Knepley 
898b2fff234SMatthew G. Knepley                     if (!PetscBTLookupSet(isLeaf, localVertex)) {
899f5eeb527SMatthew G Knepley                       localPoints[nL]        = localVertex;
900f5eeb527SMatthew G Knepley                       remotePoints[nL].rank  = neighbor;
901f5eeb527SMatthew G Knepley                       remotePoints[nL].index = remoteVertex;
902b2fff234SMatthew G. Knepley                       ++nL;
903f5eeb527SMatthew G Knepley                     }
904f5eeb527SMatthew G Knepley                   }
905b2fff234SMatthew G. Knepley                 }
906b2fff234SMatthew G. Knepley #if 0
907b2fff234SMatthew G. Knepley                 for (xf = 0; xf < nxF; ++xf) {
908f5eeb527SMatthew G Knepley                   /* THIS IS WRONG */
909b2fff234SMatthew G. Knepley                   const PetscInt localFace  = 0 + nC+nV; /* right faces */
910f5eeb527SMatthew G Knepley                   const PetscInt remoteFace = 0 + nC+nV;
911f5eeb527SMatthew G Knepley 
912b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localFace)) {
913f5eeb527SMatthew G Knepley                     localPoints[nL]        = localFace;
914f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
915f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteFace;
916b2fff234SMatthew G. Knepley                     ++nL;
917f5eeb527SMatthew G Knepley                   }
91888ed4aceSMatthew G Knepley                 }
919b2fff234SMatthew G. Knepley #endif
920b2fff234SMatthew G. Knepley               }
92188ed4aceSMatthew G Knepley             }
92288ed4aceSMatthew G Knepley           } else {
92388ed4aceSMatthew G Knepley             if (yp < 0) { /* bottom */
92488ed4aceSMatthew G Knepley               if (zp < 0) { /* back */
925b2fff234SMatthew G. Knepley                 for (xv = 0; xv < nVx; ++xv) {
926b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (      0*nVy +     0)*nVx + xv + nC; /* bottom back vertices */
927f5eeb527SMatthew G Knepley                   const PetscInt remoteVertex = ((nVz-1)*nVy + nVy-1)*nVx + xv + nC; /* TODO: Correct this for neighbor sizes */
928f5eeb527SMatthew G Knepley 
929b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) {
930f5eeb527SMatthew G Knepley                     localPoints[nL]        = localVertex;
931f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
932f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteVertex;
933b2fff234SMatthew G. Knepley                     ++nL;
934b2fff234SMatthew G. Knepley                   }
935f5eeb527SMatthew G Knepley                 }
93688ed4aceSMatthew G Knepley               } else if (zp > 0) { /* front */
937b2fff234SMatthew G. Knepley                 for (xv = 0; xv < nVx; ++xv) {
938b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = ((nVz-1)*nVy +     0)*nVx + xv + nC; /* bottom front vertices */
939f5eeb527SMatthew G Knepley                   const PetscInt remoteVertex = (      0*nVy + nVy-1)*nVx + xv + nC; /* TODO: Correct this for neighbor sizes */
940f5eeb527SMatthew G Knepley 
941b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) {
942f5eeb527SMatthew G Knepley                     localPoints[nL]        = localVertex;
943f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
944f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteVertex;
945b2fff234SMatthew G. Knepley                     ++nL;
946b2fff234SMatthew G. Knepley                   }
947f5eeb527SMatthew G Knepley                 }
94888ed4aceSMatthew G Knepley               } else {
949f5eeb527SMatthew G Knepley                 for (zv = 0; zv < nVz; ++zv) {
950b2fff234SMatthew G. Knepley                   for (xv = 0; xv < nVx; ++xv) {
951b2fff234SMatthew G. Knepley                     const PetscInt localVertex  = (zv*nVy +     0)*nVx + xv + nC; /* bottom vertices */
952f5eeb527SMatthew G Knepley                     const PetscInt remoteVertex = (zv*nVy + nVy-1)*nVx + xv + nC; /* TODO: Correct this for neighbor sizes */
953f5eeb527SMatthew G Knepley 
954b2fff234SMatthew G. Knepley                     if (!PetscBTLookupSet(isLeaf, localVertex)) {
955f5eeb527SMatthew G Knepley                       localPoints[nL]        = localVertex;
956f5eeb527SMatthew G Knepley                       remotePoints[nL].rank  = neighbor;
957f5eeb527SMatthew G Knepley                       remotePoints[nL].index = remoteVertex;
958b2fff234SMatthew G. Knepley                       ++nL;
959f5eeb527SMatthew G Knepley                     }
960f5eeb527SMatthew G Knepley                   }
961b2fff234SMatthew G. Knepley                 }
962b2fff234SMatthew G. Knepley #if 0
963b2fff234SMatthew G. Knepley                 for (yf = 0; yf < nyF; ++yf) {
964f5eeb527SMatthew G Knepley                   /* THIS IS WRONG */
965b2fff234SMatthew G. Knepley                   const PetscInt localFace  = 0 + nC+nV; /* bottom faces */
966f5eeb527SMatthew G Knepley                   const PetscInt remoteFace = 0 + nC+nV;
967f5eeb527SMatthew G Knepley 
968b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localFace)) {
969f5eeb527SMatthew G Knepley                     localPoints[nL]        = localFace;
970f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
971f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteFace;
972b2fff234SMatthew G. Knepley                     ++nL;
973f5eeb527SMatthew G Knepley                   }
97488ed4aceSMatthew G Knepley                 }
975b2fff234SMatthew G. Knepley #endif
976b2fff234SMatthew G. Knepley               }
97788ed4aceSMatthew G Knepley             } else if (yp > 0) { /* top */
97888ed4aceSMatthew G Knepley               if (zp < 0) { /* back */
979b2fff234SMatthew G. Knepley                 for (xv = 0; xv < nVx; ++xv) {
980b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (      0*nVy + nVy-1)*nVx + xv + nC; /* top back vertices */
981f5eeb527SMatthew G Knepley                   const PetscInt remoteVertex = ((nVz-1)*nVy +     0)*nVx + xv + nC; /* TODO: Correct this for neighbor sizes */
982f5eeb527SMatthew G Knepley 
983b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) {
984f5eeb527SMatthew G Knepley                     localPoints[nL]        = localVertex;
985f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
986f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteVertex;
987b2fff234SMatthew G. Knepley                     ++nL;
988b2fff234SMatthew G. Knepley                   }
989f5eeb527SMatthew G Knepley                 }
99088ed4aceSMatthew G Knepley               } else if (zp > 0) { /* front */
991b2fff234SMatthew G. Knepley                 for (xv = 0; xv < nVx; ++xv) {
992b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = ((nVz-1)*nVy + nVy-1)*nVx + xv + nC; /* top front vertices */
993f5eeb527SMatthew G Knepley                   const PetscInt remoteVertex = (      0*nVy +     0)*nVx + xv + nC; /* TODO: Correct this for neighbor sizes */
994f5eeb527SMatthew G Knepley 
995b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) {
996f5eeb527SMatthew G Knepley                     localPoints[nL]        = localVertex;
997f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
998f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteVertex;
999b2fff234SMatthew G. Knepley                     ++nL;
1000b2fff234SMatthew G. Knepley                   }
1001f5eeb527SMatthew G Knepley                 }
100288ed4aceSMatthew G Knepley               } else {
1003f5eeb527SMatthew G Knepley                 for (zv = 0; zv < nVz; ++zv) {
1004b2fff234SMatthew G. Knepley                   for (xv = 0; xv < nVx; ++xv) {
1005b2fff234SMatthew G. Knepley                     const PetscInt localVertex  = (zv*nVy + nVy-1)*nVx + xv + nC; /* top vertices */
1006f5eeb527SMatthew G Knepley                     const PetscInt remoteVertex = (zv*nVy +     0)*nVx + xv + nC; /* TODO: Correct this for neighbor sizes */
1007f5eeb527SMatthew G Knepley 
1008b2fff234SMatthew G. Knepley                     if (!PetscBTLookupSet(isLeaf, localVertex)) {
1009f5eeb527SMatthew G Knepley                       localPoints[nL]        = localVertex;
1010f5eeb527SMatthew G Knepley                       remotePoints[nL].rank  = neighbor;
1011f5eeb527SMatthew G Knepley                       remotePoints[nL].index = remoteVertex;
1012b2fff234SMatthew G. Knepley                       ++nL;
1013f5eeb527SMatthew G Knepley                     }
1014f5eeb527SMatthew G Knepley                   }
1015b2fff234SMatthew G. Knepley                 }
1016b2fff234SMatthew G. Knepley #if 0
1017b2fff234SMatthew G. Knepley                 for (yf = 0; yf < nyF; ++yf) {
1018f5eeb527SMatthew G Knepley                   /* THIS IS WRONG */
1019b2fff234SMatthew G. Knepley                   const PetscInt localFace  = 0 + nC+nV; /* top faces */
1020f5eeb527SMatthew G Knepley                   const PetscInt remoteFace = 0 + nC+nV;
1021f5eeb527SMatthew G Knepley 
1022b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localFace)) {
1023f5eeb527SMatthew G Knepley                     localPoints[nL]        = localFace;
1024f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
1025f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteFace;
1026b2fff234SMatthew G. Knepley                     ++nL;
1027f5eeb527SMatthew G Knepley                   }
102888ed4aceSMatthew G Knepley                 }
1029b2fff234SMatthew G. Knepley #endif
1030b2fff234SMatthew G. Knepley               }
103188ed4aceSMatthew G Knepley             } else {
103288ed4aceSMatthew G Knepley               if (zp < 0) { /* back */
1033f5eeb527SMatthew G Knepley                 for (yv = 0; yv < nVy; ++yv) {
1034b2fff234SMatthew G. Knepley                   for (xv = 0; xv < nVx; ++xv) {
1035b2fff234SMatthew G. Knepley                     const PetscInt localVertex  = (      0*nVy + yv)*nVx + xv + nC; /* back vertices */
1036f5eeb527SMatthew G Knepley                     const PetscInt remoteVertex = ((nVz-1)*nVy + yv)*nVx + xv + nC; /* TODO: Correct this for neighbor sizes */
1037f5eeb527SMatthew G Knepley 
1038b2fff234SMatthew G. Knepley                     if (!PetscBTLookupSet(isLeaf, localVertex)) {
1039f5eeb527SMatthew G Knepley                       localPoints[nL]        = localVertex;
1040f5eeb527SMatthew G Knepley                       remotePoints[nL].rank  = neighbor;
1041f5eeb527SMatthew G Knepley                       remotePoints[nL].index = remoteVertex;
1042b2fff234SMatthew G. Knepley                       ++nL;
1043f5eeb527SMatthew G Knepley                     }
1044f5eeb527SMatthew G Knepley                   }
1045b2fff234SMatthew G. Knepley                 }
1046b2fff234SMatthew G. Knepley #if 0
1047b2fff234SMatthew G. Knepley                 for (zf = 0; zf < nzF; ++zf) {
1048f5eeb527SMatthew G Knepley                   /* THIS IS WRONG */
1049b2fff234SMatthew G. Knepley                   const PetscInt localFace  = 0 + nC+nV; /* back faces */
1050f5eeb527SMatthew G Knepley                   const PetscInt remoteFace = 0 + nC+nV;
1051f5eeb527SMatthew G Knepley 
1052b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localFace)) {
1053f5eeb527SMatthew G Knepley                     localPoints[nL]        = localFace;
1054f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
1055f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteFace;
1056b2fff234SMatthew G. Knepley                     ++nL;
1057f5eeb527SMatthew G Knepley                   }
1058b2fff234SMatthew G. Knepley                 }
1059b2fff234SMatthew G. Knepley #endif
106088ed4aceSMatthew G Knepley               } else if (zp > 0) { /* front */
1061f5eeb527SMatthew G Knepley                 for (yv = 0; yv < nVy; ++yv) {
1062b2fff234SMatthew G. Knepley                   for (xv = 0; xv < nVx; ++xv) {
1063b2fff234SMatthew G. Knepley                     const PetscInt localVertex  = ((nVz-1)*nVy + yv)*nVx + xv + nC; /* front vertices */
1064f5eeb527SMatthew G Knepley                     const PetscInt remoteVertex = (      0*nVy + yv)*nVx + xv + nC; /* TODO: Correct this for neighbor sizes */
1065f5eeb527SMatthew G Knepley 
1066b2fff234SMatthew G. Knepley                     if (!PetscBTLookupSet(isLeaf, localVertex)) {
1067f5eeb527SMatthew G Knepley                       localPoints[nL]        = localVertex;
1068f5eeb527SMatthew G Knepley                       remotePoints[nL].rank  = neighbor;
1069f5eeb527SMatthew G Knepley                       remotePoints[nL].index = remoteVertex;
1070b2fff234SMatthew G. Knepley                       ++nL;
1071f5eeb527SMatthew G Knepley                     }
1072f5eeb527SMatthew G Knepley                   }
1073b2fff234SMatthew G. Knepley                 }
1074b2fff234SMatthew G. Knepley #if 0
1075b2fff234SMatthew G. Knepley                 for (zf = 0; zf < nzF; ++zf) {
1076f5eeb527SMatthew G Knepley                   /* THIS IS WRONG */
1077b2fff234SMatthew G. Knepley                   const PetscInt localFace  = 0 + nC+nV; /* front faces */
1078f5eeb527SMatthew G Knepley                   const PetscInt remoteFace = 0 + nC+nV;
1079f5eeb527SMatthew G Knepley 
1080b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localFace)) {
1081f5eeb527SMatthew G Knepley                     localPoints[nL]        = localFace;
1082f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
1083f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteFace;
1084b2fff234SMatthew G. Knepley                     ++nL;
1085f5eeb527SMatthew G Knepley                   }
1086b2fff234SMatthew G. Knepley                 }
1087b2fff234SMatthew G. Knepley #endif
108888ed4aceSMatthew G Knepley               } else {
108988ed4aceSMatthew G Knepley                 /* Nothing is shared from the interior */
109088ed4aceSMatthew G Knepley               }
109188ed4aceSMatthew G Knepley             }
109288ed4aceSMatthew G Knepley           }
109388ed4aceSMatthew G Knepley         }
109488ed4aceSMatthew G Knepley       }
109588ed4aceSMatthew G Knepley     }
109688ed4aceSMatthew G Knepley   }
1097b2fff234SMatthew G. Knepley   ierr = PetscBTDestroy(&isLeaf);CHKERRQ(ierr);
1098b2fff234SMatthew G. Knepley   /* Remove duplication in leaf determination */
1099b2fff234SMatthew G. Knepley   if (nleaves != nL) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "The number of leaves %d did not match the number of remote leaves %d", nleaves, nleavesCheck);
110082f516ccSBarry Smith   ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm), &sf);CHKERRQ(ierr);
110188ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(sf, pEnd, nleaves, localPoints, PETSC_OWN_POINTER, remotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
1102a4b60ecfSMatthew G. Knepley   ierr = DMSetPointSF(dm, sf);CHKERRQ(ierr);
110388ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1104*a4294c51SMatthew G. Knepley   *s = section;
1105*a4294c51SMatthew G. Knepley   PetscFunctionReturn(0);
1106*a4294c51SMatthew G. Knepley }
1107*a4294c51SMatthew G. Knepley 
1108d0892670SMatthew G. Knepley #undef __FUNCT__
1109d0892670SMatthew G. Knepley #define __FUNCT__ "DMDASetVertexCoordinates"
1110d0892670SMatthew G. Knepley PetscErrorCode DMDASetVertexCoordinates(DM dm, PetscReal xl, PetscReal xu, PetscReal yl, PetscReal yu, PetscReal zl, PetscReal zu)
1111d0892670SMatthew G. Knepley {
1112d0892670SMatthew G. Knepley   DM_DA         *da = (DM_DA *) dm->data;
1113d0892670SMatthew G. Knepley   Vec            coordinates;
1114d0892670SMatthew G. Knepley   PetscSection   section;
1115d0892670SMatthew G. Knepley   PetscScalar   *coords;
1116d0892670SMatthew G. Knepley   PetscReal      h[3];
1117d0892670SMatthew G. Knepley   PetscInt       dim, size, M, N, P, nVx, nVy, nVz, nV, vStart, vEnd, v, i, j, k;
1118d0892670SMatthew G. Knepley   PetscErrorCode ierr;
1119d0892670SMatthew G. Knepley 
1120d0892670SMatthew G. Knepley   PetscFunctionBegin;
1121d0892670SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1122d0892670SMatthew G. Knepley   ierr = DMDAGetInfo(dm, &dim, &M, &N, &P, 0,0,0,0,0,0,0,0,0);CHKERRQ(ierr);
1123d0892670SMatthew G. Knepley   h[0] = (xu - xl)/M;
1124d0892670SMatthew G. Knepley   h[1] = (yu - yl)/N;
1125d0892670SMatthew G. Knepley   h[2] = (zu - zl)/P;
1126d0892670SMatthew G. Knepley   ierr = DMDAGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1127d0892670SMatthew G. Knepley   ierr = DMDAGetNumVertices(dm, &nVx, &nVy, &nVz, &nV);CHKERRQ(ierr);
1128d0892670SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &section);CHKERRQ(ierr);
1129d0892670SMatthew G. Knepley   ierr = PetscSectionSetNumFields(section, 1);CHKERRQ(ierr);
1130d0892670SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(section, 0, dim);CHKERRQ(ierr);
1131d0892670SMatthew G. Knepley   ierr = PetscSectionSetChart(section, vStart, vEnd);CHKERRQ(ierr);
1132d0892670SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
1133d0892670SMatthew G. Knepley     ierr = PetscSectionSetDof(section, v, dim);CHKERRQ(ierr);
1134d0892670SMatthew G. Knepley   }
1135d0892670SMatthew G. Knepley   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
1136d0892670SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
1137d0892670SMatthew G. Knepley   ierr = VecCreateSeq(PETSC_COMM_SELF, size, &coordinates);CHKERRQ(ierr);
1138d0892670SMatthew G. Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
1139d0892670SMatthew G. Knepley   for (k = 0; k < nVz; ++k) {
1140d0892670SMatthew G. Knepley     PetscInt ind[3] = {0, 0, k + da->zs}, d, off;
1141d0892670SMatthew G. Knepley 
1142d0892670SMatthew G. Knepley     for (j = 0; j < nVy; ++j) {
1143d0892670SMatthew G. Knepley       ind[1] = j + da->ys;
1144d0892670SMatthew G. Knepley       for (i = 0; i < nVx; ++i) {
1145d0892670SMatthew G. Knepley         const PetscInt vertex = (k*nVy + j)*nVx + i + vStart;
1146d0892670SMatthew G. Knepley 
1147d0892670SMatthew G. Knepley         ierr = PetscSectionGetOffset(section, vertex, &off);CHKERRQ(ierr);
1148d0892670SMatthew G. Knepley         ind[0] = i + da->xs;
1149d0892670SMatthew G. Knepley         for (d = 0; d < dim; ++d) {
1150d0892670SMatthew G. Knepley           coords[off+d] = h[d]*ind[d];
1151d0892670SMatthew G. Knepley         }
1152d0892670SMatthew G. Knepley       }
1153d0892670SMatthew G. Knepley     }
1154d0892670SMatthew G. Knepley   }
1155d0892670SMatthew G. Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
1156d0892670SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, section);CHKERRQ(ierr);
1157d0892670SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
1158a4b60ecfSMatthew G. Knepley   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
1159d0892670SMatthew G. Knepley   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
1160d0892670SMatthew G. Knepley   PetscFunctionReturn(0);
1161d0892670SMatthew G. Knepley }
1162a66d4d66SMatthew G Knepley   PetscFunctionReturn(0);
1163a66d4d66SMatthew G Knepley }
1164a66d4d66SMatthew G Knepley 
116547c6ae99SBarry Smith /* ------------------------------------------------------------------- */
116647c6ae99SBarry Smith 
116747c6ae99SBarry Smith #undef __FUNCT__
1168aa219208SBarry Smith #define __FUNCT__ "DMDAGetArray"
116947c6ae99SBarry Smith /*@C
1170aa219208SBarry Smith      DMDAGetArray - Gets a work array for a DMDA
117147c6ae99SBarry Smith 
117247c6ae99SBarry Smith     Input Parameter:
117347c6ae99SBarry Smith +    da - information about my local patch
117447c6ae99SBarry Smith -    ghosted - do you want arrays for the ghosted or nonghosted patch
117547c6ae99SBarry Smith 
117647c6ae99SBarry Smith     Output Parameters:
117747c6ae99SBarry Smith .    vptr - array data structured
117847c6ae99SBarry Smith 
117947c6ae99SBarry Smith     Note:  The vector values are NOT initialized and may have garbage in them, so you may need
118047c6ae99SBarry Smith            to zero them.
118147c6ae99SBarry Smith 
118247c6ae99SBarry Smith   Level: advanced
118347c6ae99SBarry Smith 
1184bcaeba4dSBarry Smith .seealso: DMDARestoreArray()
118547c6ae99SBarry Smith 
118647c6ae99SBarry Smith @*/
11877087cfbeSBarry Smith PetscErrorCode  DMDAGetArray(DM da,PetscBool ghosted,void *vptr)
118847c6ae99SBarry Smith {
118947c6ae99SBarry Smith   PetscErrorCode ierr;
119047c6ae99SBarry Smith   PetscInt       j,i,xs,ys,xm,ym,zs,zm;
119147c6ae99SBarry Smith   char           *iarray_start;
119247c6ae99SBarry Smith   void           **iptr = (void**)vptr;
119347c6ae99SBarry Smith   DM_DA          *dd    = (DM_DA*)da->data;
119447c6ae99SBarry Smith 
119547c6ae99SBarry Smith   PetscFunctionBegin;
119647c6ae99SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
119747c6ae99SBarry Smith   if (ghosted) {
1198aa219208SBarry Smith     for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) {
119947c6ae99SBarry Smith       if (dd->arrayghostedin[i]) {
120047c6ae99SBarry Smith         *iptr                 = dd->arrayghostedin[i];
120147c6ae99SBarry Smith         iarray_start          = (char*)dd->startghostedin[i];
12020298fd71SBarry Smith         dd->arrayghostedin[i] = NULL;
12030298fd71SBarry Smith         dd->startghostedin[i] = NULL;
120447c6ae99SBarry Smith 
120547c6ae99SBarry Smith         goto done;
120647c6ae99SBarry Smith       }
120747c6ae99SBarry Smith     }
120847c6ae99SBarry Smith     xs = dd->Xs;
120947c6ae99SBarry Smith     ys = dd->Ys;
121047c6ae99SBarry Smith     zs = dd->Zs;
121147c6ae99SBarry Smith     xm = dd->Xe-dd->Xs;
121247c6ae99SBarry Smith     ym = dd->Ye-dd->Ys;
121347c6ae99SBarry Smith     zm = dd->Ze-dd->Zs;
121447c6ae99SBarry Smith   } else {
1215aa219208SBarry Smith     for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) {
121647c6ae99SBarry Smith       if (dd->arrayin[i]) {
121747c6ae99SBarry Smith         *iptr          = dd->arrayin[i];
121847c6ae99SBarry Smith         iarray_start   = (char*)dd->startin[i];
12190298fd71SBarry Smith         dd->arrayin[i] = NULL;
12200298fd71SBarry Smith         dd->startin[i] = NULL;
122147c6ae99SBarry Smith 
122247c6ae99SBarry Smith         goto done;
122347c6ae99SBarry Smith       }
122447c6ae99SBarry Smith     }
122547c6ae99SBarry Smith     xs = dd->xs;
122647c6ae99SBarry Smith     ys = dd->ys;
122747c6ae99SBarry Smith     zs = dd->zs;
122847c6ae99SBarry Smith     xm = dd->xe-dd->xs;
122947c6ae99SBarry Smith     ym = dd->ye-dd->ys;
123047c6ae99SBarry Smith     zm = dd->ze-dd->zs;
123147c6ae99SBarry Smith   }
123247c6ae99SBarry Smith 
123347c6ae99SBarry Smith   switch (dd->dim) {
123447c6ae99SBarry Smith   case 1: {
123547c6ae99SBarry Smith     void *ptr;
123647c6ae99SBarry Smith 
123747c6ae99SBarry Smith     ierr = PetscMalloc(xm*sizeof(PetscScalar),&iarray_start);CHKERRQ(ierr);
123847c6ae99SBarry Smith 
123947c6ae99SBarry Smith     ptr   = (void*)(iarray_start - xs*sizeof(PetscScalar));
124047c6ae99SBarry Smith     *iptr = (void*)ptr;
12418865f1eaSKarl Rupp     break;
12428865f1eaSKarl Rupp   }
124347c6ae99SBarry Smith   case 2: {
124447c6ae99SBarry Smith     void **ptr;
124547c6ae99SBarry Smith 
124647c6ae99SBarry Smith     ierr = PetscMalloc((ym+1)*sizeof(void*)+xm*ym*sizeof(PetscScalar),&iarray_start);CHKERRQ(ierr);
124747c6ae99SBarry Smith 
124847c6ae99SBarry Smith     ptr = (void**)(iarray_start + xm*ym*sizeof(PetscScalar) - ys*sizeof(void*));
12498865f1eaSKarl Rupp     for (j=ys; j<ys+ym; j++) ptr[j] = iarray_start + sizeof(PetscScalar)*(xm*(j-ys) - xs);
125047c6ae99SBarry Smith     *iptr = (void*)ptr;
12518865f1eaSKarl Rupp     break;
12528865f1eaSKarl Rupp   }
125347c6ae99SBarry Smith   case 3: {
125447c6ae99SBarry Smith     void ***ptr,**bptr;
125547c6ae99SBarry Smith 
125647c6ae99SBarry Smith     ierr = PetscMalloc((zm+1)*sizeof(void**)+(ym*zm+1)*sizeof(void*)+xm*ym*zm*sizeof(PetscScalar),&iarray_start);CHKERRQ(ierr);
125747c6ae99SBarry Smith 
125847c6ae99SBarry Smith     ptr  = (void***)(iarray_start + xm*ym*zm*sizeof(PetscScalar) - zs*sizeof(void*));
125947c6ae99SBarry Smith     bptr = (void**)(iarray_start + xm*ym*zm*sizeof(PetscScalar) + zm*sizeof(void**));
12608865f1eaSKarl Rupp     for (i=zs; i<zs+zm; i++) ptr[i] = bptr + ((i-zs)*ym - ys);
126147c6ae99SBarry Smith     for (i=zs; i<zs+zm; i++) {
126247c6ae99SBarry Smith       for (j=ys; j<ys+ym; j++) {
126347c6ae99SBarry Smith         ptr[i][j] = iarray_start + sizeof(PetscScalar)*(xm*ym*(i-zs) + xm*(j-ys) - xs);
126447c6ae99SBarry Smith       }
126547c6ae99SBarry Smith     }
126647c6ae99SBarry Smith     *iptr = (void*)ptr;
12678865f1eaSKarl Rupp     break;
12688865f1eaSKarl Rupp   }
126947c6ae99SBarry Smith   default:
1270ce94432eSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject)da),PETSC_ERR_SUP,"Dimension %D not supported",dd->dim);
127147c6ae99SBarry Smith   }
127247c6ae99SBarry Smith 
127347c6ae99SBarry Smith done:
127447c6ae99SBarry Smith   /* add arrays to the checked out list */
127547c6ae99SBarry Smith   if (ghosted) {
1276aa219208SBarry Smith     for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) {
127747c6ae99SBarry Smith       if (!dd->arrayghostedout[i]) {
127847c6ae99SBarry Smith         dd->arrayghostedout[i] = *iptr;
127947c6ae99SBarry Smith         dd->startghostedout[i] = iarray_start;
128047c6ae99SBarry Smith         break;
128147c6ae99SBarry Smith       }
128247c6ae99SBarry Smith     }
128347c6ae99SBarry Smith   } else {
1284aa219208SBarry Smith     for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) {
128547c6ae99SBarry Smith       if (!dd->arrayout[i]) {
128647c6ae99SBarry Smith         dd->arrayout[i] = *iptr;
128747c6ae99SBarry Smith         dd->startout[i] = iarray_start;
128847c6ae99SBarry Smith         break;
128947c6ae99SBarry Smith       }
129047c6ae99SBarry Smith     }
129147c6ae99SBarry Smith   }
129247c6ae99SBarry Smith   PetscFunctionReturn(0);
129347c6ae99SBarry Smith }
129447c6ae99SBarry Smith 
129547c6ae99SBarry Smith #undef __FUNCT__
1296aa219208SBarry Smith #define __FUNCT__ "DMDARestoreArray"
129747c6ae99SBarry Smith /*@C
1298aa219208SBarry Smith      DMDARestoreArray - Restores an array of derivative types for a DMDA
129947c6ae99SBarry Smith 
130047c6ae99SBarry Smith     Input Parameter:
130147c6ae99SBarry Smith +    da - information about my local patch
130247c6ae99SBarry Smith .    ghosted - do you want arrays for the ghosted or nonghosted patch
130347c6ae99SBarry Smith -    vptr - array data structured to be passed to ad_FormFunctionLocal()
130447c6ae99SBarry Smith 
130547c6ae99SBarry Smith      Level: advanced
130647c6ae99SBarry Smith 
1307bcaeba4dSBarry Smith .seealso: DMDAGetArray()
130847c6ae99SBarry Smith 
130947c6ae99SBarry Smith @*/
13107087cfbeSBarry Smith PetscErrorCode  DMDARestoreArray(DM da,PetscBool ghosted,void *vptr)
131147c6ae99SBarry Smith {
131247c6ae99SBarry Smith   PetscInt i;
131347c6ae99SBarry Smith   void     **iptr = (void**)vptr,*iarray_start = 0;
131447c6ae99SBarry Smith   DM_DA    *dd    = (DM_DA*)da->data;
131547c6ae99SBarry Smith 
131647c6ae99SBarry Smith   PetscFunctionBegin;
131747c6ae99SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
131847c6ae99SBarry Smith   if (ghosted) {
1319aa219208SBarry Smith     for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) {
132047c6ae99SBarry Smith       if (dd->arrayghostedout[i] == *iptr) {
132147c6ae99SBarry Smith         iarray_start           = dd->startghostedout[i];
13220298fd71SBarry Smith         dd->arrayghostedout[i] = NULL;
13230298fd71SBarry Smith         dd->startghostedout[i] = NULL;
132447c6ae99SBarry Smith         break;
132547c6ae99SBarry Smith       }
132647c6ae99SBarry Smith     }
1327aa219208SBarry Smith     for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) {
132847c6ae99SBarry Smith       if (!dd->arrayghostedin[i]) {
132947c6ae99SBarry Smith         dd->arrayghostedin[i] = *iptr;
133047c6ae99SBarry Smith         dd->startghostedin[i] = iarray_start;
133147c6ae99SBarry Smith         break;
133247c6ae99SBarry Smith       }
133347c6ae99SBarry Smith     }
133447c6ae99SBarry Smith   } else {
1335aa219208SBarry Smith     for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) {
133647c6ae99SBarry Smith       if (dd->arrayout[i] == *iptr) {
133747c6ae99SBarry Smith         iarray_start    = dd->startout[i];
13380298fd71SBarry Smith         dd->arrayout[i] = NULL;
13390298fd71SBarry Smith         dd->startout[i] = NULL;
134047c6ae99SBarry Smith         break;
134147c6ae99SBarry Smith       }
134247c6ae99SBarry Smith     }
1343aa219208SBarry Smith     for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) {
134447c6ae99SBarry Smith       if (!dd->arrayin[i]) {
134547c6ae99SBarry Smith         dd->arrayin[i] = *iptr;
134647c6ae99SBarry Smith         dd->startin[i] = iarray_start;
134747c6ae99SBarry Smith         break;
134847c6ae99SBarry Smith       }
134947c6ae99SBarry Smith     }
135047c6ae99SBarry Smith   }
135147c6ae99SBarry Smith   PetscFunctionReturn(0);
135247c6ae99SBarry Smith }
135347c6ae99SBarry Smith 
1354