xref: /petsc/src/dm/impls/da/dalocal.c (revision b698f38184bb2d8cca5de7f5c9c4b2ba4d75311d)
147c6ae99SBarry Smith 
247c6ae99SBarry Smith /*
347c6ae99SBarry Smith   Code for manipulating distributed regular arrays in parallel.
447c6ae99SBarry Smith */
547c6ae99SBarry Smith 
6af0996ceSBarry Smith #include <petsc/private/dmdaimpl.h>    /*I   "petscdmda.h"   I*/
7b2fff234SMatthew G. Knepley #include <petscbt.h>
80c312b8eSJed Brown #include <petscsf.h>
92764a2aaSMatthew G. Knepley #include <petscds.h>
109a800dd8SMatthew G. Knepley #include <petscfe.h>
1147c6ae99SBarry Smith 
1247c6ae99SBarry Smith /*
13e3c5b3baSBarry Smith    This allows the DMDA vectors to properly tell MATLAB their dimensions
1447c6ae99SBarry Smith */
1547c6ae99SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
16c6db04a5SJed Brown #include <engine.h>   /* MATLAB include file */
17c6db04a5SJed Brown #include <mex.h>      /* MATLAB include file */
1847c6ae99SBarry Smith #undef __FUNCT__
1947c6ae99SBarry Smith #define __FUNCT__ "VecMatlabEnginePut_DA2d"
201e6b0712SBarry Smith static PetscErrorCode  VecMatlabEnginePut_DA2d(PetscObject obj,void *mengine)
2147c6ae99SBarry Smith {
2247c6ae99SBarry Smith   PetscErrorCode ierr;
2347c6ae99SBarry Smith   PetscInt       n,m;
2447c6ae99SBarry Smith   Vec            vec = (Vec)obj;
2547c6ae99SBarry Smith   PetscScalar    *array;
2647c6ae99SBarry Smith   mxArray        *mat;
279a42bb27SBarry Smith   DM             da;
2847c6ae99SBarry Smith 
2947c6ae99SBarry Smith   PetscFunctionBegin;
30c688c046SMatthew G Knepley   ierr = VecGetDM(vec, &da);CHKERRQ(ierr);
31ce94432eSBarry Smith   if (!da) SETERRQ(PetscObjectComm((PetscObject)vec),PETSC_ERR_ARG_WRONGSTATE,"Vector not associated with a DMDA");
32aa219208SBarry Smith   ierr = DMDAGetGhostCorners(da,0,0,0,&m,&n,0);CHKERRQ(ierr);
3347c6ae99SBarry Smith 
3447c6ae99SBarry Smith   ierr = VecGetArray(vec,&array);CHKERRQ(ierr);
3547c6ae99SBarry Smith #if !defined(PETSC_USE_COMPLEX)
3647c6ae99SBarry Smith   mat = mxCreateDoubleMatrix(m,n,mxREAL);
3747c6ae99SBarry Smith #else
3847c6ae99SBarry Smith   mat = mxCreateDoubleMatrix(m,n,mxCOMPLEX);
3947c6ae99SBarry Smith #endif
4047c6ae99SBarry Smith   ierr = PetscMemcpy(mxGetPr(mat),array,n*m*sizeof(PetscScalar));CHKERRQ(ierr);
4147c6ae99SBarry Smith   ierr = PetscObjectName(obj);CHKERRQ(ierr);
4247c6ae99SBarry Smith   engPutVariable((Engine*)mengine,obj->name,mat);
4347c6ae99SBarry Smith 
4447c6ae99SBarry Smith   ierr = VecRestoreArray(vec,&array);CHKERRQ(ierr);
4547c6ae99SBarry Smith   PetscFunctionReturn(0);
4647c6ae99SBarry Smith }
4747c6ae99SBarry Smith #endif
4847c6ae99SBarry Smith 
4947c6ae99SBarry Smith 
5047c6ae99SBarry Smith #undef __FUNCT__
51564755cdSBarry Smith #define __FUNCT__ "DMCreateLocalVector_DA"
527087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector_DA(DM da,Vec *g)
5347c6ae99SBarry Smith {
5447c6ae99SBarry Smith   PetscErrorCode ierr;
5547c6ae99SBarry Smith   DM_DA          *dd = (DM_DA*)da->data;
5647c6ae99SBarry Smith 
5747c6ae99SBarry Smith   PetscFunctionBegin;
5847c6ae99SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
5947c6ae99SBarry Smith   PetscValidPointer(g,2);
6011689aebSJed Brown   if (da->defaultSection) {
6111689aebSJed Brown     ierr = DMCreateLocalVector_Section_Private(da,g);CHKERRQ(ierr);
6211689aebSJed Brown   } else {
6347c6ae99SBarry Smith     ierr = VecCreate(PETSC_COMM_SELF,g);CHKERRQ(ierr);
6447c6ae99SBarry Smith     ierr = VecSetSizes(*g,dd->nlocal,PETSC_DETERMINE);CHKERRQ(ierr);
6547c6ae99SBarry Smith     ierr = VecSetBlockSize(*g,dd->w);CHKERRQ(ierr);
66401ddaa8SBarry Smith     ierr = VecSetType(*g,da->vectype);CHKERRQ(ierr);
67c688c046SMatthew G Knepley     ierr = VecSetDM(*g, da);CHKERRQ(ierr);
6847c6ae99SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
697bc9226cSBarry Smith     if (dd->w == 1  && da->dim == 2) {
70bdf89e91SBarry Smith       ierr = PetscObjectComposeFunction((PetscObject)*g,"PetscMatlabEnginePut_C",VecMatlabEnginePut_DA2d);CHKERRQ(ierr);
7147c6ae99SBarry Smith     }
7247c6ae99SBarry Smith #endif
7311689aebSJed Brown   }
7447c6ae99SBarry Smith   PetscFunctionReturn(0);
7547c6ae99SBarry Smith }
7647c6ae99SBarry Smith 
77a66d4d66SMatthew G Knepley #undef __FUNCT__
7857459e9aSMatthew G Knepley #define __FUNCT__ "DMDAGetNumCells"
79939f6067SMatthew G. Knepley /*@
80939f6067SMatthew G. Knepley   DMDAGetNumCells - Get the number of cells in the local piece of the DMDA. This includes ghost cells.
81939f6067SMatthew G. Knepley 
82939f6067SMatthew G. Knepley   Input Parameter:
83939f6067SMatthew G. Knepley . dm - The DM object
84939f6067SMatthew G. Knepley 
85939f6067SMatthew G. Knepley   Output Parameters:
86939f6067SMatthew G. Knepley + numCellsX - The number of local cells in the x-direction
87939f6067SMatthew G. Knepley . numCellsY - The number of local cells in the y-direction
88939f6067SMatthew G. Knepley . numCellsZ - The number of local cells in the z-direction
89939f6067SMatthew G. Knepley - numCells - The number of local cells
90939f6067SMatthew G. Knepley 
91939f6067SMatthew G. Knepley   Level: developer
92939f6067SMatthew G. Knepley 
93939f6067SMatthew G. Knepley .seealso: DMDAGetCellPoint()
94939f6067SMatthew G. Knepley @*/
953582350cSMatthew G. Knepley PetscErrorCode DMDAGetNumCells(DM dm, PetscInt *numCellsX, PetscInt *numCellsY, PetscInt *numCellsZ, PetscInt *numCells)
9657459e9aSMatthew G Knepley {
9757459e9aSMatthew G Knepley   DM_DA         *da  = (DM_DA*) dm->data;
98c73cfb54SMatthew G. Knepley   const PetscInt dim = dm->dim;
9957459e9aSMatthew G Knepley   const PetscInt mx  = (da->Xe - da->Xs)/da->w, my = da->Ye - da->Ys, mz = da->Ze - da->Zs;
10057459e9aSMatthew G Knepley   const PetscInt nC  = (mx)*(dim > 1 ? (my)*(dim > 2 ? (mz) : 1) : 1);
10157459e9aSMatthew G Knepley 
10257459e9aSMatthew G Knepley   PetscFunctionBegin;
103d6401b93SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1043582350cSMatthew G. Knepley   if (numCellsX) {
1053582350cSMatthew G. Knepley     PetscValidIntPointer(numCellsX,2);
1063582350cSMatthew G. Knepley     *numCellsX = mx;
1073582350cSMatthew G. Knepley   }
1083582350cSMatthew G. Knepley   if (numCellsY) {
1093582350cSMatthew G. Knepley     PetscValidIntPointer(numCellsX,3);
1103582350cSMatthew G. Knepley     *numCellsY = my;
1113582350cSMatthew G. Knepley   }
1123582350cSMatthew G. Knepley   if (numCellsZ) {
1133582350cSMatthew G. Knepley     PetscValidIntPointer(numCellsX,4);
1143582350cSMatthew G. Knepley     *numCellsZ = mz;
1153582350cSMatthew G. Knepley   }
11657459e9aSMatthew G Knepley   if (numCells) {
1173582350cSMatthew G. Knepley     PetscValidIntPointer(numCells,5);
11857459e9aSMatthew G Knepley     *numCells = nC;
11957459e9aSMatthew G Knepley   }
12057459e9aSMatthew G Knepley   PetscFunctionReturn(0);
12157459e9aSMatthew G Knepley }
12257459e9aSMatthew G Knepley 
12357459e9aSMatthew G Knepley #undef __FUNCT__
124d6401b93SMatthew G. Knepley #define __FUNCT__ "DMDAGetCellPoint"
125d6401b93SMatthew G. Knepley /*@
126d6401b93SMatthew G. Knepley   DMDAGetCellPoint - Get the DM point corresponding to the tuple (i, j, k) in the DMDA
127d6401b93SMatthew G. Knepley 
128d6401b93SMatthew G. Knepley   Input Parameters:
129d6401b93SMatthew G. Knepley + dm - The DM object
130d6401b93SMatthew G. Knepley - i,j,k - The global indices for the cell
131d6401b93SMatthew G. Knepley 
132d6401b93SMatthew G. Knepley   Output Parameters:
133d6401b93SMatthew G. Knepley . point - The local DM point
134d6401b93SMatthew G. Knepley 
135d6401b93SMatthew G. Knepley   Level: developer
136d6401b93SMatthew G. Knepley 
137d6401b93SMatthew G. Knepley .seealso: DMDAGetNumCells()
138d6401b93SMatthew G. Knepley @*/
139d6401b93SMatthew G. Knepley PetscErrorCode DMDAGetCellPoint(DM dm, PetscInt i, PetscInt j, PetscInt k, PetscInt *point)
140d6401b93SMatthew G. Knepley {
141c73cfb54SMatthew G. Knepley   const PetscInt dim = dm->dim;
142d6401b93SMatthew G. Knepley   DMDALocalInfo  info;
143d6401b93SMatthew G. Knepley   PetscErrorCode ierr;
144d6401b93SMatthew G. Knepley 
145d6401b93SMatthew G. Knepley   PetscFunctionBegin;
146d6401b93SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
147d6401b93SMatthew G. Knepley   PetscValidIntPointer(point,5);
148d6401b93SMatthew G. Knepley   ierr = DMDAGetLocalInfo(dm, &info);CHKERRQ(ierr);
149d6401b93SMatthew G. Knepley   if (dim > 0) {if ((i < info.gxs) || (i >= info.gxs+info.gxm)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "X index %d not in [%d, %d)", i, info.gxs, info.gxs+info.gxm);}
150d6401b93SMatthew G. Knepley   if (dim > 1) {if ((i < info.gys) || (i >= info.gys+info.gym)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Y index %d not in [%d, %d)", i, info.gys, info.gys+info.gym);}
151d6401b93SMatthew G. Knepley   if (dim > 2) {if ((i < info.gzs) || (i >= info.gzs+info.gzm)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Z index %d not in [%d, %d)", i, info.gzs, info.gzs+info.gzm);}
152d6401b93SMatthew G. Knepley   *point = i + (dim > 1 ? (j + (dim > 2 ? k*info.gym : 0))*info.gxm : 0);
153d6401b93SMatthew G. Knepley   PetscFunctionReturn(0);
154d6401b93SMatthew G. Knepley }
155d6401b93SMatthew G. Knepley 
156d6401b93SMatthew G. Knepley #undef __FUNCT__
15757459e9aSMatthew G Knepley #define __FUNCT__ "DMDAGetNumVertices"
15857459e9aSMatthew G Knepley PetscErrorCode DMDAGetNumVertices(DM dm, PetscInt *numVerticesX, PetscInt *numVerticesY, PetscInt *numVerticesZ, PetscInt *numVertices)
15957459e9aSMatthew G Knepley {
16057459e9aSMatthew G Knepley   DM_DA          *da = (DM_DA*) dm->data;
161c73cfb54SMatthew G. Knepley   const PetscInt dim = dm->dim;
16257459e9aSMatthew G Knepley   const PetscInt mx  = (da->Xe - da->Xs)/da->w, my = da->Ye - da->Ys, mz = da->Ze - da->Zs;
16357459e9aSMatthew G Knepley   const PetscInt nVx = mx+1;
16457459e9aSMatthew G Knepley   const PetscInt nVy = dim > 1 ? (my+1) : 1;
16557459e9aSMatthew G Knepley   const PetscInt nVz = dim > 2 ? (mz+1) : 1;
16657459e9aSMatthew G Knepley   const PetscInt nV  = nVx*nVy*nVz;
16757459e9aSMatthew G Knepley 
16857459e9aSMatthew G Knepley   PetscFunctionBegin;
16957459e9aSMatthew G Knepley   if (numVerticesX) {
17057459e9aSMatthew G Knepley     PetscValidIntPointer(numVerticesX,2);
17157459e9aSMatthew G Knepley     *numVerticesX = nVx;
17257459e9aSMatthew G Knepley   }
17357459e9aSMatthew G Knepley   if (numVerticesY) {
17457459e9aSMatthew G Knepley     PetscValidIntPointer(numVerticesY,3);
17557459e9aSMatthew G Knepley     *numVerticesY = nVy;
17657459e9aSMatthew G Knepley   }
17757459e9aSMatthew G Knepley   if (numVerticesZ) {
17857459e9aSMatthew G Knepley     PetscValidIntPointer(numVerticesZ,4);
17957459e9aSMatthew G Knepley     *numVerticesZ = nVz;
18057459e9aSMatthew G Knepley   }
18157459e9aSMatthew G Knepley   if (numVertices) {
18257459e9aSMatthew G Knepley     PetscValidIntPointer(numVertices,5);
18357459e9aSMatthew G Knepley     *numVertices = nV;
18457459e9aSMatthew G Knepley   }
18557459e9aSMatthew G Knepley   PetscFunctionReturn(0);
18657459e9aSMatthew G Knepley }
18757459e9aSMatthew G Knepley 
18857459e9aSMatthew G Knepley #undef __FUNCT__
18957459e9aSMatthew G Knepley #define __FUNCT__ "DMDAGetNumFaces"
19057459e9aSMatthew G Knepley PetscErrorCode DMDAGetNumFaces(DM dm, PetscInt *numXFacesX, PetscInt *numXFaces, PetscInt *numYFacesY, PetscInt *numYFaces, PetscInt *numZFacesZ, PetscInt *numZFaces)
19157459e9aSMatthew G Knepley {
19257459e9aSMatthew G Knepley   DM_DA          *da = (DM_DA*) dm->data;
193c73cfb54SMatthew G. Knepley   const PetscInt dim = dm->dim;
19457459e9aSMatthew G Knepley   const PetscInt mx  = (da->Xe - da->Xs)/da->w, my = da->Ye - da->Ys, mz = da->Ze - da->Zs;
19557459e9aSMatthew G Knepley   const PetscInt nxF = (dim > 1 ? (my)*(dim > 2 ? (mz) : 1) : 1);
19657459e9aSMatthew G Knepley   const PetscInt nXF = (mx+1)*nxF;
19757459e9aSMatthew G Knepley   const PetscInt nyF = mx*(dim > 2 ? mz : 1);
19857459e9aSMatthew G Knepley   const PetscInt nYF = dim > 1 ? (my+1)*nyF : 0;
19957459e9aSMatthew G Knepley   const PetscInt nzF = mx*(dim > 1 ? my : 0);
20057459e9aSMatthew G Knepley   const PetscInt nZF = dim > 2 ? (mz+1)*nzF : 0;
20157459e9aSMatthew G Knepley 
20257459e9aSMatthew G Knepley   PetscFunctionBegin;
20357459e9aSMatthew G Knepley   if (numXFacesX) {
20457459e9aSMatthew G Knepley     PetscValidIntPointer(numXFacesX,2);
20557459e9aSMatthew G Knepley     *numXFacesX = nxF;
20657459e9aSMatthew G Knepley   }
20757459e9aSMatthew G Knepley   if (numXFaces) {
20857459e9aSMatthew G Knepley     PetscValidIntPointer(numXFaces,3);
20957459e9aSMatthew G Knepley     *numXFaces = nXF;
21057459e9aSMatthew G Knepley   }
21157459e9aSMatthew G Knepley   if (numYFacesY) {
21257459e9aSMatthew G Knepley     PetscValidIntPointer(numYFacesY,4);
21357459e9aSMatthew G Knepley     *numYFacesY = nyF;
21457459e9aSMatthew G Knepley   }
21557459e9aSMatthew G Knepley   if (numYFaces) {
21657459e9aSMatthew G Knepley     PetscValidIntPointer(numYFaces,5);
21757459e9aSMatthew G Knepley     *numYFaces = nYF;
21857459e9aSMatthew G Knepley   }
21957459e9aSMatthew G Knepley   if (numZFacesZ) {
22057459e9aSMatthew G Knepley     PetscValidIntPointer(numZFacesZ,6);
22157459e9aSMatthew G Knepley     *numZFacesZ = nzF;
22257459e9aSMatthew G Knepley   }
22357459e9aSMatthew G Knepley   if (numZFaces) {
22457459e9aSMatthew G Knepley     PetscValidIntPointer(numZFaces,7);
22557459e9aSMatthew G Knepley     *numZFaces = nZF;
22657459e9aSMatthew G Knepley   }
22757459e9aSMatthew G Knepley   PetscFunctionReturn(0);
22857459e9aSMatthew G Knepley }
22957459e9aSMatthew G Knepley 
23057459e9aSMatthew G Knepley #undef __FUNCT__
23157459e9aSMatthew G Knepley #define __FUNCT__ "DMDAGetHeightStratum"
23257459e9aSMatthew G Knepley PetscErrorCode DMDAGetHeightStratum(DM dm, PetscInt height, PetscInt *pStart, PetscInt *pEnd)
23357459e9aSMatthew G Knepley {
234c73cfb54SMatthew G. Knepley   const PetscInt dim = dm->dim;
23557459e9aSMatthew G Knepley   PetscInt       nC, nV, nXF, nYF, nZF;
23657459e9aSMatthew G Knepley   PetscErrorCode ierr;
23757459e9aSMatthew G Knepley 
23857459e9aSMatthew G Knepley   PetscFunctionBegin;
2398865f1eaSKarl Rupp   if (pStart) PetscValidIntPointer(pStart,3);
2408865f1eaSKarl Rupp   if (pEnd)   PetscValidIntPointer(pEnd,4);
2413582350cSMatthew G. Knepley   ierr = DMDAGetNumCells(dm, NULL, NULL, NULL, &nC);CHKERRQ(ierr);
2420298fd71SBarry Smith   ierr = DMDAGetNumVertices(dm, NULL, NULL, NULL, &nV);CHKERRQ(ierr);
2430298fd71SBarry Smith   ierr = DMDAGetNumFaces(dm, NULL, &nXF, NULL, &nYF, NULL, &nZF);CHKERRQ(ierr);
24457459e9aSMatthew G Knepley   if (height == 0) {
24557459e9aSMatthew G Knepley     /* Cells */
2468865f1eaSKarl Rupp     if (pStart) *pStart = 0;
2478865f1eaSKarl Rupp     if (pEnd)   *pEnd   = nC;
24857459e9aSMatthew G Knepley   } else if (height == 1) {
24957459e9aSMatthew G Knepley     /* Faces */
2508865f1eaSKarl Rupp     if (pStart) *pStart = nC+nV;
2518865f1eaSKarl Rupp     if (pEnd)   *pEnd   = nC+nV+nXF+nYF+nZF;
25257459e9aSMatthew G Knepley   } else if (height == dim) {
25357459e9aSMatthew G Knepley     /* Vertices */
2548865f1eaSKarl Rupp     if (pStart) *pStart = nC;
2558865f1eaSKarl Rupp     if (pEnd)   *pEnd   = nC+nV;
25657459e9aSMatthew G Knepley   } else if (height < 0) {
25757459e9aSMatthew G Knepley     /* All points */
2588865f1eaSKarl Rupp     if (pStart) *pStart = 0;
2598865f1eaSKarl Rupp     if (pEnd)   *pEnd   = nC+nV+nXF+nYF+nZF;
26082f516ccSBarry Smith   } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No points of height %d in the DA", height);
26157459e9aSMatthew G Knepley   PetscFunctionReturn(0);
26257459e9aSMatthew G Knepley }
26357459e9aSMatthew G Knepley 
26457459e9aSMatthew G Knepley #undef __FUNCT__
2653385ff7eSMatthew G. Knepley #define __FUNCT__ "DMDAGetDepthStratum"
2663385ff7eSMatthew G. Knepley PetscErrorCode DMDAGetDepthStratum(DM dm, PetscInt depth, PetscInt *pStart, PetscInt *pEnd)
2673385ff7eSMatthew G. Knepley {
268c73cfb54SMatthew G. Knepley   const PetscInt dim = dm->dim;
2693385ff7eSMatthew G. Knepley   PetscInt       nC, nV, nXF, nYF, nZF;
2703385ff7eSMatthew G. Knepley   PetscErrorCode ierr;
2713385ff7eSMatthew G. Knepley 
2723385ff7eSMatthew G. Knepley   PetscFunctionBegin;
2733385ff7eSMatthew G. Knepley   if (pStart) PetscValidIntPointer(pStart,3);
2743385ff7eSMatthew G. Knepley   if (pEnd)   PetscValidIntPointer(pEnd,4);
2753385ff7eSMatthew G. Knepley   ierr = DMDAGetNumCells(dm, NULL, NULL, NULL, &nC);CHKERRQ(ierr);
2763385ff7eSMatthew G. Knepley   ierr = DMDAGetNumVertices(dm, NULL, NULL, NULL, &nV);CHKERRQ(ierr);
2773385ff7eSMatthew G. Knepley   ierr = DMDAGetNumFaces(dm, NULL, &nXF, NULL, &nYF, NULL, &nZF);CHKERRQ(ierr);
2783385ff7eSMatthew G. Knepley   if (depth == dim) {
2793385ff7eSMatthew G. Knepley     /* Cells */
2803385ff7eSMatthew G. Knepley     if (pStart) *pStart = 0;
2813385ff7eSMatthew G. Knepley     if (pEnd)   *pEnd   = nC;
2823385ff7eSMatthew G. Knepley   } else if (depth == dim-1) {
2833385ff7eSMatthew G. Knepley     /* Faces */
2843385ff7eSMatthew G. Knepley     if (pStart) *pStart = nC+nV;
2853385ff7eSMatthew G. Knepley     if (pEnd)   *pEnd   = nC+nV+nXF+nYF+nZF;
2863385ff7eSMatthew G. Knepley   } else if (depth == 0) {
2873385ff7eSMatthew G. Knepley     /* Vertices */
2883385ff7eSMatthew G. Knepley     if (pStart) *pStart = nC;
2893385ff7eSMatthew G. Knepley     if (pEnd)   *pEnd   = nC+nV;
2903385ff7eSMatthew G. Knepley   } else if (depth < 0) {
2913385ff7eSMatthew G. Knepley     /* All points */
2923385ff7eSMatthew G. Knepley     if (pStart) *pStart = 0;
2933385ff7eSMatthew G. Knepley     if (pEnd)   *pEnd   = nC+nV+nXF+nYF+nZF;
2943385ff7eSMatthew G. Knepley   } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No points of depth %d in the DA", depth);
2953385ff7eSMatthew G. Knepley   PetscFunctionReturn(0);
2963385ff7eSMatthew G. Knepley }
2973385ff7eSMatthew G. Knepley 
2983385ff7eSMatthew G. Knepley #undef __FUNCT__
2993385ff7eSMatthew G. Knepley #define __FUNCT__ "DMDAGetConeSize"
3003385ff7eSMatthew G. Knepley PetscErrorCode DMDAGetConeSize(DM dm, PetscInt p, PetscInt *coneSize)
3013385ff7eSMatthew G. Knepley {
302c73cfb54SMatthew G. Knepley   const PetscInt dim = dm->dim;
3033385ff7eSMatthew G. Knepley   PetscInt       nC, nV, nXF, nYF, nZF;
3043385ff7eSMatthew G. Knepley   PetscErrorCode ierr;
3053385ff7eSMatthew G. Knepley 
3063385ff7eSMatthew G. Knepley   PetscFunctionBegin;
3073385ff7eSMatthew G. Knepley   *coneSize = 0;
3083385ff7eSMatthew G. Knepley   ierr = DMDAGetNumCells(dm, NULL, NULL, NULL, &nC);CHKERRQ(ierr);
3093385ff7eSMatthew G. Knepley   ierr = DMDAGetNumVertices(dm, NULL, NULL, NULL, &nV);CHKERRQ(ierr);
3103385ff7eSMatthew G. Knepley   ierr = DMDAGetNumFaces(dm, NULL, &nXF, NULL, &nYF, NULL, &nZF);CHKERRQ(ierr);
3113385ff7eSMatthew G. Knepley   switch (dim) {
3123385ff7eSMatthew G. Knepley   case 2:
3133385ff7eSMatthew G. Knepley     if (p >= 0) {
3143385ff7eSMatthew G. Knepley       if (p < nC) {
3153385ff7eSMatthew G. Knepley         *coneSize = 4;
3163385ff7eSMatthew G. Knepley       } else if (p < nC+nV) {
3173385ff7eSMatthew G. Knepley         *coneSize = 0;
3183385ff7eSMatthew G. Knepley       } else if (p < nC+nV+nXF+nYF+nZF) {
3193385ff7eSMatthew G. Knepley         *coneSize = 2;
3203385ff7eSMatthew G. Knepley       } else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d should be in [0, %d)", p, nC+nV+nXF+nYF+nZF);
3213385ff7eSMatthew G. Knepley     } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative point %d is invalid", p);
3223385ff7eSMatthew G. Knepley     break;
3233385ff7eSMatthew G. Knepley   case 3:
3243385ff7eSMatthew G. Knepley     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Too lazy to do 3D");
3253385ff7eSMatthew G. Knepley     break;
3263385ff7eSMatthew G. Knepley   }
3273385ff7eSMatthew G. Knepley   PetscFunctionReturn(0);
3283385ff7eSMatthew G. Knepley }
3293385ff7eSMatthew G. Knepley 
3303385ff7eSMatthew G. Knepley #undef __FUNCT__
3313385ff7eSMatthew G. Knepley #define __FUNCT__ "DMDAGetCone"
3323385ff7eSMatthew G. Knepley PetscErrorCode DMDAGetCone(DM dm, PetscInt p, PetscInt *cone[])
3333385ff7eSMatthew G. Knepley {
334c73cfb54SMatthew G. Knepley   const PetscInt dim = dm->dim;
3353385ff7eSMatthew G. Knepley   PetscInt       nCx, nCy, nCz, nC, nVx, nVy, nVz, nV, nxF, nyF, nzF, nXF, nYF, nZF;
3363385ff7eSMatthew G. Knepley   PetscErrorCode ierr;
3373385ff7eSMatthew G. Knepley 
3383385ff7eSMatthew G. Knepley   PetscFunctionBegin;
3393385ff7eSMatthew G. Knepley   if (!cone) {ierr = DMGetWorkArray(dm, 6, PETSC_INT, cone);CHKERRQ(ierr);}
3403385ff7eSMatthew G. Knepley   ierr = DMDAGetNumCells(dm, &nCx, &nCy, &nCz, &nC);CHKERRQ(ierr);
3413385ff7eSMatthew G. Knepley   ierr = DMDAGetNumVertices(dm, &nVx, &nVy, &nVz, &nV);CHKERRQ(ierr);
3423385ff7eSMatthew G. Knepley   ierr = DMDAGetNumFaces(dm, &nxF, &nXF, &nyF, &nYF, &nzF, &nZF);CHKERRQ(ierr);
3433385ff7eSMatthew G. Knepley   switch (dim) {
3443385ff7eSMatthew G. Knepley   case 2:
3453385ff7eSMatthew G. Knepley     if (p >= 0) {
3463385ff7eSMatthew G. Knepley       if (p < nC) {
3473385ff7eSMatthew G. Knepley         const PetscInt cy = p / nCx;
3483385ff7eSMatthew G. Knepley         const PetscInt cx = p % nCx;
3493385ff7eSMatthew G. Knepley 
3503385ff7eSMatthew G. Knepley         (*cone)[0] = cy*nxF + cx + nC+nV;
3513385ff7eSMatthew G. Knepley         (*cone)[1] = cx*nyF + cy + nyF + nC+nV+nXF;
3523385ff7eSMatthew G. Knepley         (*cone)[2] = cy*nxF + cx + nxF + nC+nV;
3533385ff7eSMatthew G. Knepley         (*cone)[3] = cx*nyF + cy + nC+nV+nXF;
3543385ff7eSMatthew G. Knepley         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Too lazy to do cell cones");
3553385ff7eSMatthew G. Knepley       } else if (p < nC+nV) {
3563385ff7eSMatthew G. Knepley       } else if (p < nC+nV+nXF) {
3573385ff7eSMatthew G. Knepley         const PetscInt fy = (p - nC+nV) / nxF;
3583385ff7eSMatthew G. Knepley         const PetscInt fx = (p - nC+nV) % nxF;
3593385ff7eSMatthew G. Knepley 
3603385ff7eSMatthew G. Knepley         (*cone)[0] = fy*nVx + fx + nC;
3613385ff7eSMatthew G. Knepley         (*cone)[1] = fy*nVx + fx + 1 + nC;
3623385ff7eSMatthew G. Knepley       } else if (p < nC+nV+nXF+nYF) {
3633385ff7eSMatthew G. Knepley         const PetscInt fx = (p - nC+nV+nXF) / nyF;
3643385ff7eSMatthew G. Knepley         const PetscInt fy = (p - nC+nV+nXF) % nyF;
3653385ff7eSMatthew G. Knepley 
3663385ff7eSMatthew G. Knepley         (*cone)[0] = fy*nVx + fx + nC;
3673385ff7eSMatthew G. Knepley         (*cone)[1] = fy*nVx + fx + nVx + nC;
3683385ff7eSMatthew G. Knepley       } else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d should be in [0, %d)", p, nC+nV+nXF+nYF+nZF);
3693385ff7eSMatthew G. Knepley     } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative point %d is invalid", p);
3703385ff7eSMatthew G. Knepley     break;
3713385ff7eSMatthew G. Knepley   case 3:
3723385ff7eSMatthew G. Knepley     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Too lazy to do 3D");
3733385ff7eSMatthew G. Knepley     break;
3743385ff7eSMatthew G. Knepley   }
3753385ff7eSMatthew G. Knepley   PetscFunctionReturn(0);
3763385ff7eSMatthew G. Knepley }
3773385ff7eSMatthew G. Knepley 
3783385ff7eSMatthew G. Knepley #undef __FUNCT__
3793385ff7eSMatthew G. Knepley #define __FUNCT__ "DMDARestoreCone"
3803385ff7eSMatthew G. Knepley PetscErrorCode DMDARestoreCone(DM dm, PetscInt p, PetscInt *cone[])
3813385ff7eSMatthew G. Knepley {
3823385ff7eSMatthew G. Knepley   PetscErrorCode ierr;
3833385ff7eSMatthew G. Knepley 
3843385ff7eSMatthew G. Knepley   PetscFunctionBegin;
3853385ff7eSMatthew G. Knepley   ierr = DMGetWorkArray(dm, 6, PETSC_INT, cone);CHKERRQ(ierr);
3863385ff7eSMatthew G. Knepley   PetscFunctionReturn(0);
3873385ff7eSMatthew G. Knepley }
3883385ff7eSMatthew G. Knepley 
3893385ff7eSMatthew G. Knepley #undef __FUNCT__
390a66d4d66SMatthew G Knepley #define __FUNCT__ "DMDACreateSection"
391a66d4d66SMatthew G Knepley /*@C
392a66d4d66SMatthew G Knepley   DMDACreateSection - Create a PetscSection inside the DMDA that describes data layout. This allows multiple fields with
393a66d4d66SMatthew G Knepley   different numbers of dofs on vertices, cells, and faces in each direction.
394a66d4d66SMatthew G Knepley 
395a66d4d66SMatthew G Knepley   Input Parameters:
396a66d4d66SMatthew G Knepley + dm- The DMDA
397a66d4d66SMatthew G Knepley . numFields - The number of fields
398affa55c7SMatthew G. Knepley . numComp - The number of components in each field
399affa55c7SMatthew G. Knepley . numDof - The number of dofs per dimension for each field
4000298fd71SBarry Smith . numFaceDof - The number of dofs per face for each field and direction, or NULL
401a66d4d66SMatthew G Knepley 
402a66d4d66SMatthew G Knepley   Level: developer
403a66d4d66SMatthew G Knepley 
404a66d4d66SMatthew G Knepley   Note:
405a66d4d66SMatthew G Knepley   The default DMDA numbering is as follows:
406a66d4d66SMatthew G Knepley 
407a66d4d66SMatthew G Knepley     - Cells:    [0,             nC)
408a66d4d66SMatthew G Knepley     - Vertices: [nC,            nC+nV)
40988ed4aceSMatthew G Knepley     - X-Faces:  [nC+nV,         nC+nV+nXF)         normal is +- x-dir
41088ed4aceSMatthew G Knepley     - Y-Faces:  [nC+nV+nXF,     nC+nV+nXF+nYF)     normal is +- y-dir
41188ed4aceSMatthew G Knepley     - Z-Faces:  [nC+nV+nXF+nYF, nC+nV+nXF+nYF+nZF) normal is +- z-dir
412a66d4d66SMatthew G Knepley 
413a66d4d66SMatthew G Knepley   We interpret the default DMDA partition as a cell partition, and the data assignment as a cell assignment.
414a66d4d66SMatthew G Knepley @*/
41544e1f9abSMatthew G. Knepley PetscErrorCode DMDACreateSection(DM dm, const PetscInt numComp[], const PetscInt numDof[], const PetscInt numFaceDof[], PetscSection *s)
416a66d4d66SMatthew G Knepley {
417a4b60ecfSMatthew G. Knepley   PetscSection      section;
418c73cfb54SMatthew G. Knepley   const PetscInt    dim = dm->dim;
41980800b1aSMatthew G Knepley   PetscInt          numFields, numVertexTotDof = 0, numCellTotDof = 0, numFaceTotDof[3] = {0, 0, 0};
420b2fff234SMatthew G. Knepley   PetscBT           isLeaf;
42188ed4aceSMatthew G Knepley   PetscSF           sf;
422feafbc5cSMatthew G Knepley   PetscMPIInt       rank;
42388ed4aceSMatthew G Knepley   const PetscMPIInt *neighbors;
42488ed4aceSMatthew G Knepley   PetscInt          *localPoints;
42588ed4aceSMatthew G Knepley   PetscSFNode       *remotePoints;
426f5eeb527SMatthew G Knepley   PetscInt          nleaves = 0,  nleavesCheck = 0, nL = 0;
42757459e9aSMatthew G Knepley   PetscInt          nC, nVx, nVy, nVz, nV, nxF, nXF, nyF, nYF, nzF, nZF;
42857459e9aSMatthew G Knepley   PetscInt          pStart, pEnd, cStart, cEnd, vStart, vEnd, fStart, fEnd, xfStart, xfEnd, yfStart, yfEnd, zfStart, zfEnd;
42988ed4aceSMatthew G Knepley   PetscInt          f, v, c, xf, yf, zf, xn, yn, zn;
430a66d4d66SMatthew G Knepley   PetscErrorCode    ierr;
431a66d4d66SMatthew G Knepley 
432a66d4d66SMatthew G Knepley   PetscFunctionBegin;
433a66d4d66SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4343582350cSMatthew G. Knepley   PetscValidPointer(s, 4);
43582f516ccSBarry Smith   ierr    = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
4363582350cSMatthew G. Knepley   ierr    = DMDAGetNumCells(dm, NULL, NULL, NULL, &nC);CHKERRQ(ierr);
43757459e9aSMatthew G Knepley   ierr    = DMDAGetNumVertices(dm, &nVx, &nVy, &nVz, &nV);CHKERRQ(ierr);
43857459e9aSMatthew G Knepley   ierr    = DMDAGetNumFaces(dm, &nxF, &nXF, &nyF, &nYF, &nzF, &nZF);CHKERRQ(ierr);
43957459e9aSMatthew G Knepley   ierr    = DMDAGetHeightStratum(dm, -1,  &pStart, &pEnd);CHKERRQ(ierr);
44057459e9aSMatthew G Knepley   ierr    = DMDAGetHeightStratum(dm, 0,   &cStart, &cEnd);CHKERRQ(ierr);
44157459e9aSMatthew G Knepley   ierr    = DMDAGetHeightStratum(dm, 1,   &fStart, &fEnd);CHKERRQ(ierr);
44257459e9aSMatthew G Knepley   ierr    = DMDAGetHeightStratum(dm, dim, &vStart, &vEnd);CHKERRQ(ierr);
44357459e9aSMatthew G Knepley   xfStart = vEnd;  xfEnd = xfStart+nXF;
44457459e9aSMatthew G Knepley   yfStart = xfEnd; yfEnd = yfStart+nYF;
44557459e9aSMatthew G Knepley   zfStart = yfEnd; zfEnd = zfStart+nZF;
44682f516ccSBarry Smith   if (zfEnd != fEnd) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid face end %d, should be %d", zfEnd, fEnd);
44788ed4aceSMatthew G Knepley   /* Create local section */
44880800b1aSMatthew G Knepley   ierr = DMDAGetInfo(dm, 0,0,0,0,0,0,0, &numFields, 0,0,0,0,0);CHKERRQ(ierr);
449a66d4d66SMatthew G Knepley   for (f = 0; f < numFields; ++f) {
450affa55c7SMatthew G. Knepley     numVertexTotDof  += numDof[f*(dim+1)+0];
451affa55c7SMatthew G. Knepley     numCellTotDof    += numDof[f*(dim+1)+dim];
452affa55c7SMatthew G. Knepley     numFaceTotDof[0] += dim > 0 ? (numFaceDof ? numFaceDof[f*dim+0] : numDof[f*(dim+1)+dim-1]) : 0;
453affa55c7SMatthew G. Knepley     numFaceTotDof[1] += dim > 1 ? (numFaceDof ? numFaceDof[f*dim+1] : numDof[f*(dim+1)+dim-1]) : 0;
454affa55c7SMatthew G. Knepley     numFaceTotDof[2] += dim > 2 ? (numFaceDof ? numFaceDof[f*dim+2] : numDof[f*(dim+1)+dim-1]) : 0;
455a66d4d66SMatthew G Knepley   }
456a4b60ecfSMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
457affa55c7SMatthew G. Knepley   if (numFields > 0) {
458a4b60ecfSMatthew G. Knepley     ierr = PetscSectionSetNumFields(section, numFields);CHKERRQ(ierr);
459a66d4d66SMatthew G Knepley     for (f = 0; f < numFields; ++f) {
46080800b1aSMatthew G Knepley       const char *name;
46180800b1aSMatthew G Knepley 
46280800b1aSMatthew G Knepley       ierr = DMDAGetFieldName(dm, f, &name);CHKERRQ(ierr);
463affa55c7SMatthew G. Knepley       ierr = PetscSectionSetFieldName(section, f, name ? name : "Field");CHKERRQ(ierr);
46480800b1aSMatthew G Knepley       if (numComp) {
465a4b60ecfSMatthew G. Knepley         ierr = PetscSectionSetFieldComponents(section, f, numComp[f]);CHKERRQ(ierr);
466a66d4d66SMatthew G Knepley       }
467a66d4d66SMatthew G Knepley     }
468a66d4d66SMatthew G Knepley   }
469a4b60ecfSMatthew G. Knepley   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
470a66d4d66SMatthew G Knepley   for (v = vStart; v < vEnd; ++v) {
471a66d4d66SMatthew G Knepley     for (f = 0; f < numFields; ++f) {
472affa55c7SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(section, v, f, numDof[f*(dim+1)+0]);CHKERRQ(ierr);
473a66d4d66SMatthew G Knepley     }
474a4b60ecfSMatthew G. Knepley     ierr = PetscSectionSetDof(section, v, numVertexTotDof);CHKERRQ(ierr);
475a66d4d66SMatthew G Knepley   }
476a66d4d66SMatthew G Knepley   for (xf = xfStart; xf < xfEnd; ++xf) {
477a66d4d66SMatthew G Knepley     for (f = 0; f < numFields; ++f) {
478affa55c7SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(section, xf, f, numFaceDof ? numFaceDof[f*dim+0] : numDof[f*(dim+1)+dim-1]);CHKERRQ(ierr);
479a66d4d66SMatthew G Knepley     }
480a4b60ecfSMatthew G. Knepley     ierr = PetscSectionSetDof(section, xf, numFaceTotDof[0]);CHKERRQ(ierr);
481a66d4d66SMatthew G Knepley   }
482a66d4d66SMatthew G Knepley   for (yf = yfStart; yf < yfEnd; ++yf) {
483a66d4d66SMatthew G Knepley     for (f = 0; f < numFields; ++f) {
484affa55c7SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(section, yf, f, numFaceDof ? numFaceDof[f*dim+1] : numDof[f*(dim+1)+dim-1]);CHKERRQ(ierr);
485a66d4d66SMatthew G Knepley     }
486a4b60ecfSMatthew G. Knepley     ierr = PetscSectionSetDof(section, yf, numFaceTotDof[1]);CHKERRQ(ierr);
487a66d4d66SMatthew G Knepley   }
488a66d4d66SMatthew G Knepley   for (zf = zfStart; zf < zfEnd; ++zf) {
489a66d4d66SMatthew G Knepley     for (f = 0; f < numFields; ++f) {
490affa55c7SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(section, zf, f, numFaceDof ? numFaceDof[f*dim+2] : numDof[f*(dim+1)+dim-1]);CHKERRQ(ierr);
491a66d4d66SMatthew G Knepley     }
492a4b60ecfSMatthew G. Knepley     ierr = PetscSectionSetDof(section, zf, numFaceTotDof[2]);CHKERRQ(ierr);
493a66d4d66SMatthew G Knepley   }
494a66d4d66SMatthew G Knepley   for (c = cStart; c < cEnd; ++c) {
495a66d4d66SMatthew G Knepley     for (f = 0; f < numFields; ++f) {
496affa55c7SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(section, c, f, numDof[f*(dim+1)+dim]);CHKERRQ(ierr);
497a66d4d66SMatthew G Knepley     }
498a4b60ecfSMatthew G. Knepley     ierr = PetscSectionSetDof(section, c, numCellTotDof);CHKERRQ(ierr);
499a66d4d66SMatthew G Knepley   }
500a4b60ecfSMatthew G. Knepley   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
50188ed4aceSMatthew G Knepley   /* Create mesh point SF */
502b2fff234SMatthew G. Knepley   ierr = PetscBTCreate(pEnd-pStart, &isLeaf);CHKERRQ(ierr);
50388ed4aceSMatthew G Knepley   ierr = DMDAGetNeighbors(dm, &neighbors);CHKERRQ(ierr);
50488ed4aceSMatthew G Knepley   for (zn = 0; zn < (dim > 2 ? 3 : 1); ++zn) {
50588ed4aceSMatthew G Knepley     for (yn = 0; yn < (dim > 1 ? 3 : 1); ++yn) {
50688ed4aceSMatthew G Knepley       for (xn = 0; xn < 3; ++xn) {
5077128ae9fSMatthew G Knepley         const PetscInt xp       = xn-1, yp = dim > 1 ? yn-1 : 0, zp = dim > 2 ? zn-1 : 0;
50888ed4aceSMatthew G Knepley         const PetscInt neighbor = neighbors[(zn*3+yn)*3+xn];
509b2fff234SMatthew G. Knepley         PetscInt       xv, yv, zv;
51088ed4aceSMatthew G Knepley 
5113814d604SMatthew G Knepley         if (neighbor >= 0 && neighbor < rank) {
512b2fff234SMatthew G. Knepley           if (xp < 0) { /* left */
513b2fff234SMatthew G. Knepley             if (yp < 0) { /* bottom */
514b2fff234SMatthew G. Knepley               if (zp < 0) { /* back */
515b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = (      0*nVy +     0)*nVx +     0 + nC; /* left bottom back vertex */
516b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
517b2fff234SMatthew G. Knepley               } else if (zp > 0) { /* front */
518b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = ((nVz-1)*nVy +     0)*nVx +     0 + nC; /* left bottom front vertex */
519b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
520b2fff234SMatthew G. Knepley               } else {
521b2fff234SMatthew G. Knepley                 for (zv = 0; zv < nVz; ++zv) {
522b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (zv*nVy +     0)*nVx +     0 + nC; /* left bottom vertices */
523b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
524b2fff234SMatthew G. Knepley                 }
525b2fff234SMatthew G. Knepley               }
526b2fff234SMatthew G. Knepley             } else if (yp > 0) { /* top */
527b2fff234SMatthew G. Knepley               if (zp < 0) { /* back */
528b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = (      0*nVy + nVy-1)*nVx +     0 + nC; /* left top back vertex */
529b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
530b2fff234SMatthew G. Knepley               } else if (zp > 0) { /* front */
531b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = ((nVz-1)*nVy + nVy-1)*nVx +     0 + nC; /* left top front vertex */
532b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
533b2fff234SMatthew G. Knepley               } else {
534b2fff234SMatthew G. Knepley                 for (zv = 0; zv < nVz; ++zv) {
535b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (zv*nVy + nVy-1)*nVx +     0 + nC; /* left top vertices */
536b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
537b2fff234SMatthew G. Knepley                 }
538b2fff234SMatthew G. Knepley               }
539b2fff234SMatthew G. Knepley             } else {
540b2fff234SMatthew G. Knepley               if (zp < 0) { /* back */
541b2fff234SMatthew G. Knepley                 for (yv = 0; yv < nVy; ++yv) {
542b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (      0*nVy + yv)*nVx +     0 + nC; /* left back vertices */
543b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
544b2fff234SMatthew G. Knepley                 }
545b2fff234SMatthew G. Knepley               } else if (zp > 0) { /* front */
546b2fff234SMatthew G. Knepley                 for (yv = 0; yv < nVy; ++yv) {
547b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = ((nVz-1)*nVy + yv)*nVx +     0 + nC; /* left front vertices */
548b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
549b2fff234SMatthew G. Knepley                 }
550b2fff234SMatthew G. Knepley               } else {
551b2fff234SMatthew G. Knepley                 for (zv = 0; zv < nVz; ++zv) {
552b2fff234SMatthew G. Knepley                   for (yv = 0; yv < nVy; ++yv) {
553b2fff234SMatthew G. Knepley                     const PetscInt localVertex  = (zv*nVy + yv)*nVx +     0 + nC; /* left vertices */
554b2fff234SMatthew G. Knepley                     if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
555b2fff234SMatthew G. Knepley                   }
556b2fff234SMatthew G. Knepley                 }
557b2fff234SMatthew G. Knepley #if 0
558b2fff234SMatthew G. Knepley                 for (xf = 0; xf < nxF; ++xf) {
559b2fff234SMatthew G. Knepley                   /* THIS IS WRONG */
560b2fff234SMatthew G. Knepley                   const PetscInt localFace  = 0 + nC+nV; /* left faces */
561b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localFace)) ++nleaves;
562b2fff234SMatthew G. Knepley                 }
563b2fff234SMatthew G. Knepley #endif
564b2fff234SMatthew G. Knepley               }
565b2fff234SMatthew G. Knepley             }
566b2fff234SMatthew G. Knepley           } else if (xp > 0) { /* right */
567b2fff234SMatthew G. Knepley             if (yp < 0) { /* bottom */
568b2fff234SMatthew G. Knepley               if (zp < 0) { /* back */
569b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = (      0*nVy +     0)*nVx + nVx-1 + nC; /* right bottom back vertex */
570b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
571b2fff234SMatthew G. Knepley               } else if (zp > 0) { /* front */
572b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = ((nVz-1)*nVy +     0)*nVx + nVx-1 + nC; /* right bottom front vertex */
573b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
574b2fff234SMatthew G. Knepley               } else {
575b2fff234SMatthew G. Knepley                 for (zv = 0; zv < nVz; ++zv) {
576b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (zv*nVy +     0)*nVx + nVx-1 + nC; /* right bottom vertices */
577b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
578b2fff234SMatthew G. Knepley                 }
579b2fff234SMatthew G. Knepley               }
580b2fff234SMatthew G. Knepley             } else if (yp > 0) { /* top */
581b2fff234SMatthew G. Knepley               if (zp < 0) { /* back */
582b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = (      0*nVy + nVy-1)*nVx + nVx-1 + nC; /* right top back vertex */
583b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
584b2fff234SMatthew G. Knepley               } else if (zp > 0) { /* front */
585b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = ((nVz-1)*nVy + nVy-1)*nVx + nVx-1 + nC; /* right top front vertex */
586b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
587b2fff234SMatthew G. Knepley               } else {
588b2fff234SMatthew G. Knepley                 for (zv = 0; zv < nVz; ++zv) {
589b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (zv*nVy + nVy-1)*nVx + nVx-1 + nC; /* right top vertices */
590b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
591b2fff234SMatthew G. Knepley                 }
592b2fff234SMatthew G. Knepley               }
593b2fff234SMatthew G. Knepley             } else {
594b2fff234SMatthew G. Knepley               if (zp < 0) { /* back */
595b2fff234SMatthew G. Knepley                 for (yv = 0; yv < nVy; ++yv) {
596b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (      0*nVy + yv)*nVx + nVx-1 + nC; /* right back vertices */
597b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
598b2fff234SMatthew G. Knepley                 }
599b2fff234SMatthew G. Knepley               } else if (zp > 0) { /* front */
600b2fff234SMatthew G. Knepley                 for (yv = 0; yv < nVy; ++yv) {
601b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = ((nVz-1)*nVy + yv)*nVx + nVx-1 + nC; /* right front vertices */
602b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
603b2fff234SMatthew G. Knepley                 }
604b2fff234SMatthew G. Knepley               } else {
605b2fff234SMatthew G. Knepley                 for (zv = 0; zv < nVz; ++zv) {
606b2fff234SMatthew G. Knepley                   for (yv = 0; yv < nVy; ++yv) {
607b2fff234SMatthew G. Knepley                     const PetscInt localVertex  = (zv*nVy + yv)*nVx + nVx-1 + nC; /* right vertices */
608b2fff234SMatthew G. Knepley                     if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
609b2fff234SMatthew G. Knepley                   }
610b2fff234SMatthew G. Knepley                 }
611b2fff234SMatthew G. Knepley #if 0
612b2fff234SMatthew G. Knepley                 for (xf = 0; xf < nxF; ++xf) {
613b2fff234SMatthew G. Knepley                   /* THIS IS WRONG */
614b2fff234SMatthew G. Knepley                   const PetscInt localFace  = 0 + nC+nV; /* right faces */
615b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localFace)) ++nleaves;
616b2fff234SMatthew G. Knepley                 }
617b2fff234SMatthew G. Knepley #endif
618b2fff234SMatthew G. Knepley               }
619b2fff234SMatthew G. Knepley             }
620b2fff234SMatthew G. Knepley           } else {
621b2fff234SMatthew G. Knepley             if (yp < 0) { /* bottom */
622b2fff234SMatthew G. Knepley               if (zp < 0) { /* back */
623b2fff234SMatthew G. Knepley                 for (xv = 0; xv < nVx; ++xv) {
624b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (      0*nVy +     0)*nVx + xv + nC; /* bottom back vertices */
625b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
626b2fff234SMatthew G. Knepley                 }
627b2fff234SMatthew G. Knepley               } else if (zp > 0) { /* front */
628b2fff234SMatthew G. Knepley                 for (xv = 0; xv < nVx; ++xv) {
629b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = ((nVz-1)*nVy +     0)*nVx + xv + nC; /* bottom front vertices */
630b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
631b2fff234SMatthew G. Knepley                 }
632b2fff234SMatthew G. Knepley               } else {
633b2fff234SMatthew G. Knepley                 for (zv = 0; zv < nVz; ++zv) {
634b2fff234SMatthew G. Knepley                   for (xv = 0; xv < nVx; ++xv) {
635b2fff234SMatthew G. Knepley                     const PetscInt localVertex  = (zv*nVy +     0)*nVx + xv + nC; /* bottom vertices */
636b2fff234SMatthew G. Knepley                     if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
637b2fff234SMatthew G. Knepley                   }
638b2fff234SMatthew G. Knepley                 }
639b2fff234SMatthew G. Knepley #if 0
640b2fff234SMatthew G. Knepley                 for (yf = 0; yf < nyF; ++yf) {
641b2fff234SMatthew G. Knepley                   /* THIS IS WRONG */
642b2fff234SMatthew G. Knepley                   const PetscInt localFace  = 0 + nC+nV; /* bottom faces */
643b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVFace)) ++nleaves;
644b2fff234SMatthew G. Knepley                 }
645b2fff234SMatthew G. Knepley #endif
646b2fff234SMatthew G. Knepley               }
647b2fff234SMatthew G. Knepley             } else if (yp > 0) { /* top */
648b2fff234SMatthew G. Knepley               if (zp < 0) { /* back */
649b2fff234SMatthew G. Knepley                 for (xv = 0; xv < nVx; ++xv) {
650b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (      0*nVy + nVy-1)*nVx + xv + nC; /* top back vertices */
651b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
652b2fff234SMatthew G. Knepley                 }
653b2fff234SMatthew G. Knepley               } else if (zp > 0) { /* front */
654b2fff234SMatthew G. Knepley                 for (xv = 0; xv < nVx; ++xv) {
655b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = ((nVz-1)*nVy + nVy-1)*nVx + xv + nC; /* top front vertices */
656b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
657b2fff234SMatthew G. Knepley                 }
658b2fff234SMatthew G. Knepley               } else {
659b2fff234SMatthew G. Knepley                 for (zv = 0; zv < nVz; ++zv) {
660b2fff234SMatthew G. Knepley                   for (xv = 0; xv < nVx; ++xv) {
661b2fff234SMatthew G. Knepley                     const PetscInt localVertex  = (zv*nVy + nVy-1)*nVx + xv + nC; /* top vertices */
662b2fff234SMatthew G. Knepley                     if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
663b2fff234SMatthew G. Knepley                   }
664b2fff234SMatthew G. Knepley                 }
665b2fff234SMatthew G. Knepley #if 0
666b2fff234SMatthew G. Knepley                 for (yf = 0; yf < nyF; ++yf) {
667b2fff234SMatthew G. Knepley                   /* THIS IS WRONG */
668b2fff234SMatthew G. Knepley                   const PetscInt localFace  = 0 + nC+nV; /* top faces */
669b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVFace)) ++nleaves;
670b2fff234SMatthew G. Knepley                 }
671b2fff234SMatthew G. Knepley #endif
672b2fff234SMatthew G. Knepley               }
673b2fff234SMatthew G. Knepley             } else {
674b2fff234SMatthew G. Knepley               if (zp < 0) { /* back */
675b2fff234SMatthew G. Knepley                 for (yv = 0; yv < nVy; ++yv) {
676b2fff234SMatthew G. Knepley                   for (xv = 0; xv < nVx; ++xv) {
677b2fff234SMatthew G. Knepley                     const PetscInt localVertex  = (      0*nVy + yv)*nVx + xv + nC; /* back vertices */
678b2fff234SMatthew G. Knepley                     if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
679b2fff234SMatthew G. Knepley                   }
680b2fff234SMatthew G. Knepley                 }
681b2fff234SMatthew G. Knepley #if 0
682b2fff234SMatthew G. Knepley                 for (zf = 0; zf < nzF; ++zf) {
683b2fff234SMatthew G. Knepley                   /* THIS IS WRONG */
684b2fff234SMatthew G. Knepley                   const PetscInt localFace  = 0 + nC+nV; /* back faces */
685b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localFace)) ++nleaves;
686b2fff234SMatthew G. Knepley                 }
687b2fff234SMatthew G. Knepley #endif
688b2fff234SMatthew G. Knepley               } else if (zp > 0) { /* front */
689b2fff234SMatthew G. Knepley                 for (yv = 0; yv < nVy; ++yv) {
690b2fff234SMatthew G. Knepley                   for (xv = 0; xv < nVx; ++xv) {
691b2fff234SMatthew G. Knepley                     const PetscInt localVertex  = ((nVz-1)*nVy + yv)*nVx + xv + nC; /* front vertices */
692b2fff234SMatthew G. Knepley                     if (!PetscBTLookupSet(isLeaf, localVertex)) ++nleaves;
693b2fff234SMatthew G. Knepley                   }
694b2fff234SMatthew G. Knepley                 }
695b2fff234SMatthew G. Knepley #if 0
696b2fff234SMatthew G. Knepley                 for (zf = 0; zf < nzF; ++zf) {
697b2fff234SMatthew G. Knepley                   /* THIS IS WRONG */
698b2fff234SMatthew G. Knepley                   const PetscInt localFace  = 0 + nC+nV; /* front faces */
699b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localFace)) ++nleaves;
700b2fff234SMatthew G. Knepley                 }
701b2fff234SMatthew G. Knepley #endif
702b2fff234SMatthew G. Knepley               } else {
703b2fff234SMatthew G. Knepley                 /* Nothing is shared from the interior */
70488ed4aceSMatthew G Knepley               }
70588ed4aceSMatthew G Knepley             }
70688ed4aceSMatthew G Knepley           }
70788ed4aceSMatthew G Knepley         }
70888ed4aceSMatthew G Knepley       }
709b2fff234SMatthew G. Knepley     }
710b2fff234SMatthew G. Knepley   }
711b2fff234SMatthew G. Knepley   ierr = PetscBTMemzero(pEnd-pStart, isLeaf);CHKERRQ(ierr);
7120d6c776aSBarry Smith   ierr = PetscMalloc1(nleaves,&localPoints);CHKERRQ(ierr);
7130d6c776aSBarry Smith   ierr = PetscMalloc1(nleaves,&remotePoints);CHKERRQ(ierr);
71488ed4aceSMatthew G Knepley   for (zn = 0; zn < (dim > 2 ? 3 : 1); ++zn) {
71588ed4aceSMatthew G Knepley     for (yn = 0; yn < (dim > 1 ? 3 : 1); ++yn) {
71688ed4aceSMatthew G Knepley       for (xn = 0; xn < 3; ++xn) {
7177128ae9fSMatthew G Knepley         const PetscInt xp       = xn-1, yp = dim > 1 ? yn-1 : 0, zp = dim > 2 ? zn-1 : 0;
71888ed4aceSMatthew G Knepley         const PetscInt neighbor = neighbors[(zn*3+yn)*3+xn];
719f5eeb527SMatthew G Knepley         PetscInt       xv, yv, zv;
72088ed4aceSMatthew G Knepley 
7213814d604SMatthew G Knepley         if (neighbor >= 0 && neighbor < rank) {
72288ed4aceSMatthew G Knepley           if (xp < 0) { /* left */
72388ed4aceSMatthew G Knepley             if (yp < 0) { /* bottom */
72488ed4aceSMatthew G Knepley               if (zp < 0) { /* back */
725b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = (      0*nVy +     0)*nVx +     0 + nC; /* left bottom back vertex */
726f5eeb527SMatthew G Knepley                 const PetscInt remoteVertex = ((nVz-1)*nVy + nVy-1)*nVx + nVx-1 + nC; /* TODO: Correct this for neighbor sizes */
727f5eeb527SMatthew G Knepley 
728b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) {
729f5eeb527SMatthew G Knepley                   localPoints[nL]        = localVertex;
730f5eeb527SMatthew G Knepley                   remotePoints[nL].rank  = neighbor;
731f5eeb527SMatthew G Knepley                   remotePoints[nL].index = remoteVertex;
732f5eeb527SMatthew G Knepley                   ++nL;
733b2fff234SMatthew G. Knepley                 }
73488ed4aceSMatthew G Knepley               } else if (zp > 0) { /* front */
735b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = ((nVz-1)*nVy +     0)*nVx +     0 + nC; /* left bottom front vertex */
736f5eeb527SMatthew G Knepley                 const PetscInt remoteVertex = (      0*nVy + nVy-1)*nVx + nVx-1 + nC; /* TODO: Correct this for neighbor sizes */
737f5eeb527SMatthew G Knepley 
738b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) {
739f5eeb527SMatthew G Knepley                   localPoints[nL]        = localVertex;
740f5eeb527SMatthew G Knepley                   remotePoints[nL].rank  = neighbor;
741f5eeb527SMatthew G Knepley                   remotePoints[nL].index = remoteVertex;
742f5eeb527SMatthew G Knepley                   ++nL;
743b2fff234SMatthew G. Knepley                 }
74488ed4aceSMatthew G Knepley               } else {
745b2fff234SMatthew G. Knepley                 for (zv = 0; zv < nVz; ++zv) {
746b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (zv*nVy +     0)*nVx +     0 + nC; /* left bottom vertices */
747f5eeb527SMatthew G Knepley                   const PetscInt remoteVertex = (zv*nVy + nVy-1)*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               }
75788ed4aceSMatthew G Knepley             } else if (yp > 0) { /* top */
75888ed4aceSMatthew G Knepley               if (zp < 0) { /* back */
759b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = (      0*nVy + nVy-1)*nVx +     0 + nC; /* left top back vertex */
760f5eeb527SMatthew G Knepley                 const PetscInt remoteVertex = ((nVz-1)*nVy +     0)*nVx + nVx-1 + nC; /* TODO: Correct this for neighbor sizes */
761f5eeb527SMatthew G Knepley 
762b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) {
763f5eeb527SMatthew G Knepley                   localPoints[nL]        = localVertex;
764f5eeb527SMatthew G Knepley                   remotePoints[nL].rank  = neighbor;
765f5eeb527SMatthew G Knepley                   remotePoints[nL].index = remoteVertex;
766f5eeb527SMatthew G Knepley                   ++nL;
767b2fff234SMatthew G. Knepley                 }
76888ed4aceSMatthew G Knepley               } else if (zp > 0) { /* front */
769b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = ((nVz-1)*nVy + nVy-1)*nVx +     0 + nC; /* left top front vertex */
770f5eeb527SMatthew G Knepley                 const PetscInt remoteVertex = (      0*nVy +     0)*nVx + nVx-1 + nC; /* TODO: Correct this for neighbor sizes */
771f5eeb527SMatthew G Knepley 
772b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) {
773f5eeb527SMatthew G Knepley                   localPoints[nL]        = localVertex;
774f5eeb527SMatthew G Knepley                   remotePoints[nL].rank  = neighbor;
775f5eeb527SMatthew G Knepley                   remotePoints[nL].index = remoteVertex;
776f5eeb527SMatthew G Knepley                   ++nL;
777b2fff234SMatthew G. Knepley                 }
77888ed4aceSMatthew G Knepley               } else {
779b2fff234SMatthew G. Knepley                 for (zv = 0; zv < nVz; ++zv) {
780b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (zv*nVy + nVy-1)*nVx +     0 + nC; /* left top vertices */
781f5eeb527SMatthew G Knepley                   const PetscInt remoteVertex = (zv*nVy +     0)*nVx + nVx-1 + nC; /* TODO: Correct this for neighbor sizes */
782f5eeb527SMatthew G Knepley 
783b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) {
784f5eeb527SMatthew G Knepley                     localPoints[nL]        = localVertex;
785f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
786f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteVertex;
787b2fff234SMatthew G. Knepley                     ++nL;
788b2fff234SMatthew G. Knepley                   }
789f5eeb527SMatthew G Knepley                 }
79088ed4aceSMatthew G Knepley               }
79188ed4aceSMatthew G Knepley             } else {
79288ed4aceSMatthew G Knepley               if (zp < 0) { /* back */
793b2fff234SMatthew G. Knepley                 for (yv = 0; yv < nVy; ++yv) {
794b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (      0*nVy + yv)*nVx +     0 + nC; /* left back vertices */
795f5eeb527SMatthew G Knepley                   const PetscInt remoteVertex = ((nVz-1)*nVy + yv)*nVx + nVx-1 + nC; /* TODO: Correct this for neighbor sizes */
796f5eeb527SMatthew G Knepley 
797b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) {
798f5eeb527SMatthew G Knepley                     localPoints[nL]        = localVertex;
799f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
800f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteVertex;
801b2fff234SMatthew G. Knepley                     ++nL;
802b2fff234SMatthew G. Knepley                   }
803f5eeb527SMatthew G Knepley                 }
80488ed4aceSMatthew G Knepley               } else if (zp > 0) { /* front */
805b2fff234SMatthew G. Knepley                 for (yv = 0; yv < nVy; ++yv) {
806b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = ((nVz-1)*nVy + yv)*nVx +     0 + nC; /* left front vertices */
807f5eeb527SMatthew G Knepley                   const PetscInt remoteVertex = (      0*nVy + yv)*nVx + nVx-1 + nC; /* TODO: Correct this for neighbor sizes */
808f5eeb527SMatthew G Knepley 
809b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) {
810f5eeb527SMatthew G Knepley                     localPoints[nL]        = localVertex;
811f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
812f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteVertex;
813b2fff234SMatthew G. Knepley                     ++nL;
814b2fff234SMatthew G. Knepley                   }
815f5eeb527SMatthew G Knepley                 }
81688ed4aceSMatthew G Knepley               } else {
817f5eeb527SMatthew G Knepley                 for (zv = 0; zv < nVz; ++zv) {
818b2fff234SMatthew G. Knepley                   for (yv = 0; yv < nVy; ++yv) {
819b2fff234SMatthew G. Knepley                     const PetscInt localVertex  = (zv*nVy + yv)*nVx +     0 + nC; /* left vertices */
820f5eeb527SMatthew G Knepley                     const PetscInt remoteVertex = (zv*nVy + yv)*nVx + nVx-1 + nC; /* TODO: Correct this for neighbor sizes */
821f5eeb527SMatthew G Knepley 
822b2fff234SMatthew G. Knepley                     if (!PetscBTLookupSet(isLeaf, localVertex)) {
823f5eeb527SMatthew G Knepley                       localPoints[nL]        = localVertex;
824f5eeb527SMatthew G Knepley                       remotePoints[nL].rank  = neighbor;
825f5eeb527SMatthew G Knepley                       remotePoints[nL].index = remoteVertex;
826b2fff234SMatthew G. Knepley                       ++nL;
827f5eeb527SMatthew G Knepley                     }
828f5eeb527SMatthew G Knepley                   }
829b2fff234SMatthew G. Knepley                 }
830b2fff234SMatthew G. Knepley #if 0
831b2fff234SMatthew G. Knepley                 for (xf = 0; xf < nxF; ++xf) {
832f5eeb527SMatthew G Knepley                   /* THIS IS WRONG */
833b2fff234SMatthew G. Knepley                   const PetscInt localFace  = 0 + nC+nV; /* left faces */
834f5eeb527SMatthew G Knepley                   const PetscInt remoteFace = 0 + nC+nV;
835f5eeb527SMatthew G Knepley 
836b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localFace)) {
837f5eeb527SMatthew G Knepley                     localPoints[nL]        = localFace;
838f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
839f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteFace;
840f5eeb527SMatthew G Knepley                   }
84188ed4aceSMatthew G Knepley                 }
842b2fff234SMatthew G. Knepley #endif
843b2fff234SMatthew G. Knepley               }
84488ed4aceSMatthew G Knepley             }
84588ed4aceSMatthew G Knepley           } else if (xp > 0) { /* right */
84688ed4aceSMatthew G Knepley             if (yp < 0) { /* bottom */
84788ed4aceSMatthew G Knepley               if (zp < 0) { /* back */
848b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = (      0*nVy +     0)*nVx + nVx-1 + nC; /* right bottom back vertex */
849f5eeb527SMatthew G Knepley                 const PetscInt remoteVertex = ((nVz-1)*nVy + nVy-1)*nVx +     0 + nC; /* TODO: Correct this for neighbor sizes */
850f5eeb527SMatthew G Knepley 
851b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) {
852f5eeb527SMatthew G Knepley                   localPoints[nL]        = localVertex;
853f5eeb527SMatthew G Knepley                   remotePoints[nL].rank  = neighbor;
854f5eeb527SMatthew G Knepley                   remotePoints[nL].index = remoteVertex;
855f5eeb527SMatthew G Knepley                   ++nL;
856b2fff234SMatthew G. Knepley                 }
85788ed4aceSMatthew G Knepley               } else if (zp > 0) { /* front */
858b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = ((nVz-1)*nVy +     0)*nVx + nVx-1 + nC; /* right bottom front vertex */
859f5eeb527SMatthew G Knepley                 const PetscInt remoteVertex = (      0*nVy + nVy-1)*nVx +     0 + nC; /* TODO: Correct this for neighbor sizes */
860f5eeb527SMatthew G Knepley 
861b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) {
862f5eeb527SMatthew G Knepley                   localPoints[nL]        = localVertex;
863f5eeb527SMatthew G Knepley                   remotePoints[nL].rank  = neighbor;
864f5eeb527SMatthew G Knepley                   remotePoints[nL].index = remoteVertex;
865f5eeb527SMatthew G Knepley                   ++nL;
866b2fff234SMatthew G. Knepley                 }
86788ed4aceSMatthew G Knepley               } else {
868b2fff234SMatthew G. Knepley                 nleavesCheck += nVz;
869b2fff234SMatthew G. Knepley                 for (zv = 0; zv < nVz; ++zv) {
870b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (zv*nVy +     0)*nVx + nVx-1 + nC; /* right bottom vertices */
871f5eeb527SMatthew G Knepley                   const PetscInt remoteVertex = (zv*nVy + nVy-1)*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               }
88188ed4aceSMatthew G Knepley             } else if (yp > 0) { /* top */
88288ed4aceSMatthew G Knepley               if (zp < 0) { /* back */
883b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = (      0*nVy + nVy-1)*nVx + nVx-1 + nC; /* right top back vertex */
884f5eeb527SMatthew G Knepley                 const PetscInt remoteVertex = ((nVz-1)*nVy +     0)*nVx +     0 + nC; /* TODO: Correct this for neighbor sizes */
885f5eeb527SMatthew G Knepley 
886b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) {
887f5eeb527SMatthew G Knepley                   localPoints[nL]        = localVertex;
888f5eeb527SMatthew G Knepley                   remotePoints[nL].rank  = neighbor;
889f5eeb527SMatthew G Knepley                   remotePoints[nL].index = remoteVertex;
890f5eeb527SMatthew G Knepley                   ++nL;
891b2fff234SMatthew G. Knepley                 }
89288ed4aceSMatthew G Knepley               } else if (zp > 0) { /* front */
893b2fff234SMatthew G. Knepley                 const PetscInt localVertex  = ((nVz-1)*nVy + nVy-1)*nVx + nVx-1 + nC; /* right top front vertex */
894f5eeb527SMatthew G Knepley                 const PetscInt remoteVertex = (      0*nVy +     0)*nVx +     0 + nC; /* TODO: Correct this for neighbor sizes */
895f5eeb527SMatthew G Knepley 
896b2fff234SMatthew G. Knepley                 if (!PetscBTLookupSet(isLeaf, localVertex)) {
897f5eeb527SMatthew G Knepley                   localPoints[nL]        = localVertex;
898f5eeb527SMatthew G Knepley                   remotePoints[nL].rank  = neighbor;
899f5eeb527SMatthew G Knepley                   remotePoints[nL].index = remoteVertex;
900f5eeb527SMatthew G Knepley                   ++nL;
901b2fff234SMatthew G. Knepley                 }
90288ed4aceSMatthew G Knepley               } else {
903b2fff234SMatthew G. Knepley                 for (zv = 0; zv < nVz; ++zv) {
904b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (zv*nVy + nVy-1)*nVx + nVx-1 + nC; /* right top vertices */
905f5eeb527SMatthew G Knepley                   const PetscInt remoteVertex = (zv*nVy +     0)*nVx +     0 + nC; /* TODO: Correct this for neighbor sizes */
906f5eeb527SMatthew G Knepley 
907b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) {
908f5eeb527SMatthew G Knepley                     localPoints[nL]        = localVertex;
909f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
910f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteVertex;
911b2fff234SMatthew G. Knepley                     ++nL;
912b2fff234SMatthew G. Knepley                   }
913f5eeb527SMatthew G Knepley                 }
91488ed4aceSMatthew G Knepley               }
91588ed4aceSMatthew G Knepley             } else {
91688ed4aceSMatthew G Knepley               if (zp < 0) { /* back */
917b2fff234SMatthew G. Knepley                 for (yv = 0; yv < nVy; ++yv) {
918b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (      0*nVy + yv)*nVx + nVx-1 + nC; /* right back vertices */
919f5eeb527SMatthew G Knepley                   const PetscInt remoteVertex = ((nVz-1)*nVy + yv)*nVx +     0 + nC; /* TODO: Correct this for neighbor sizes */
920f5eeb527SMatthew G Knepley 
921b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) {
922f5eeb527SMatthew G Knepley                     localPoints[nL]        = localVertex;
923f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
924f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteVertex;
925b2fff234SMatthew G. Knepley                     ++nL;
926b2fff234SMatthew G. Knepley                   }
927f5eeb527SMatthew G Knepley                 }
92888ed4aceSMatthew G Knepley               } else if (zp > 0) { /* front */
929b2fff234SMatthew G. Knepley                 for (yv = 0; yv < nVy; ++yv) {
930b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = ((nVz-1)*nVy + yv)*nVx + nVx-1 + nC; /* right front vertices */
931f5eeb527SMatthew G Knepley                   const PetscInt remoteVertex = (      0*nVy + yv)*nVx +     0 + nC; /* TODO: Correct this for neighbor sizes */
932f5eeb527SMatthew G Knepley 
933b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) {
934f5eeb527SMatthew G Knepley                     localPoints[nL]        = localVertex;
935f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
936f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteVertex;
937b2fff234SMatthew G. Knepley                     ++nL;
938b2fff234SMatthew G. Knepley                   }
939f5eeb527SMatthew G Knepley                 }
94088ed4aceSMatthew G Knepley               } else {
941f5eeb527SMatthew G Knepley                 for (zv = 0; zv < nVz; ++zv) {
942b2fff234SMatthew G. Knepley                   for (yv = 0; yv < nVy; ++yv) {
943b2fff234SMatthew G. Knepley                     const PetscInt localVertex  = (zv*nVy + yv)*nVx + nVx-1 + nC; /* right vertices */
944f5eeb527SMatthew G Knepley                     const PetscInt remoteVertex = (zv*nVy + yv)*nVx + 0     + nC; /* TODO: Correct this for neighbor sizes */
945f5eeb527SMatthew G Knepley 
946b2fff234SMatthew G. Knepley                     if (!PetscBTLookupSet(isLeaf, localVertex)) {
947f5eeb527SMatthew G Knepley                       localPoints[nL]        = localVertex;
948f5eeb527SMatthew G Knepley                       remotePoints[nL].rank  = neighbor;
949f5eeb527SMatthew G Knepley                       remotePoints[nL].index = remoteVertex;
950b2fff234SMatthew G. Knepley                       ++nL;
951f5eeb527SMatthew G Knepley                     }
952f5eeb527SMatthew G Knepley                   }
953b2fff234SMatthew G. Knepley                 }
954b2fff234SMatthew G. Knepley #if 0
955b2fff234SMatthew G. Knepley                 for (xf = 0; xf < nxF; ++xf) {
956f5eeb527SMatthew G Knepley                   /* THIS IS WRONG */
957b2fff234SMatthew G. Knepley                   const PetscInt localFace  = 0 + nC+nV; /* right faces */
958f5eeb527SMatthew G Knepley                   const PetscInt remoteFace = 0 + nC+nV;
959f5eeb527SMatthew G Knepley 
960b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localFace)) {
961f5eeb527SMatthew G Knepley                     localPoints[nL]        = localFace;
962f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
963f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteFace;
964b2fff234SMatthew G. Knepley                     ++nL;
965f5eeb527SMatthew G Knepley                   }
96688ed4aceSMatthew G Knepley                 }
967b2fff234SMatthew G. Knepley #endif
968b2fff234SMatthew G. Knepley               }
96988ed4aceSMatthew G Knepley             }
97088ed4aceSMatthew G Knepley           } else {
97188ed4aceSMatthew G Knepley             if (yp < 0) { /* bottom */
97288ed4aceSMatthew G Knepley               if (zp < 0) { /* back */
973b2fff234SMatthew G. Knepley                 for (xv = 0; xv < nVx; ++xv) {
974b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (      0*nVy +     0)*nVx + xv + nC; /* bottom back vertices */
975f5eeb527SMatthew G Knepley                   const PetscInt remoteVertex = ((nVz-1)*nVy + nVy-1)*nVx + xv + nC; /* TODO: Correct this for neighbor sizes */
976f5eeb527SMatthew G Knepley 
977b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) {
978f5eeb527SMatthew G Knepley                     localPoints[nL]        = localVertex;
979f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
980f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteVertex;
981b2fff234SMatthew G. Knepley                     ++nL;
982b2fff234SMatthew G. Knepley                   }
983f5eeb527SMatthew G Knepley                 }
98488ed4aceSMatthew G Knepley               } else if (zp > 0) { /* front */
985b2fff234SMatthew G. Knepley                 for (xv = 0; xv < nVx; ++xv) {
986b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = ((nVz-1)*nVy +     0)*nVx + xv + nC; /* bottom front vertices */
987f5eeb527SMatthew G Knepley                   const PetscInt remoteVertex = (      0*nVy + nVy-1)*nVx + xv + nC; /* TODO: Correct this for neighbor sizes */
988f5eeb527SMatthew G Knepley 
989b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) {
990f5eeb527SMatthew G Knepley                     localPoints[nL]        = localVertex;
991f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
992f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteVertex;
993b2fff234SMatthew G. Knepley                     ++nL;
994b2fff234SMatthew G. Knepley                   }
995f5eeb527SMatthew G Knepley                 }
99688ed4aceSMatthew G Knepley               } else {
997f5eeb527SMatthew G Knepley                 for (zv = 0; zv < nVz; ++zv) {
998b2fff234SMatthew G. Knepley                   for (xv = 0; xv < nVx; ++xv) {
999b2fff234SMatthew G. Knepley                     const PetscInt localVertex  = (zv*nVy +     0)*nVx + xv + nC; /* bottom vertices */
1000f5eeb527SMatthew G Knepley                     const PetscInt remoteVertex = (zv*nVy + nVy-1)*nVx + xv + nC; /* TODO: Correct this for neighbor sizes */
1001f5eeb527SMatthew G Knepley 
1002b2fff234SMatthew G. Knepley                     if (!PetscBTLookupSet(isLeaf, localVertex)) {
1003f5eeb527SMatthew G Knepley                       localPoints[nL]        = localVertex;
1004f5eeb527SMatthew G Knepley                       remotePoints[nL].rank  = neighbor;
1005f5eeb527SMatthew G Knepley                       remotePoints[nL].index = remoteVertex;
1006b2fff234SMatthew G. Knepley                       ++nL;
1007f5eeb527SMatthew G Knepley                     }
1008f5eeb527SMatthew G Knepley                   }
1009b2fff234SMatthew G. Knepley                 }
1010b2fff234SMatthew G. Knepley #if 0
1011b2fff234SMatthew G. Knepley                 for (yf = 0; yf < nyF; ++yf) {
1012f5eeb527SMatthew G Knepley                   /* THIS IS WRONG */
1013b2fff234SMatthew G. Knepley                   const PetscInt localFace  = 0 + nC+nV; /* bottom faces */
1014f5eeb527SMatthew G Knepley                   const PetscInt remoteFace = 0 + nC+nV;
1015f5eeb527SMatthew G Knepley 
1016b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localFace)) {
1017f5eeb527SMatthew G Knepley                     localPoints[nL]        = localFace;
1018f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
1019f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteFace;
1020b2fff234SMatthew G. Knepley                     ++nL;
1021f5eeb527SMatthew G Knepley                   }
102288ed4aceSMatthew G Knepley                 }
1023b2fff234SMatthew G. Knepley #endif
1024b2fff234SMatthew G. Knepley               }
102588ed4aceSMatthew G Knepley             } else if (yp > 0) { /* top */
102688ed4aceSMatthew G Knepley               if (zp < 0) { /* back */
1027b2fff234SMatthew G. Knepley                 for (xv = 0; xv < nVx; ++xv) {
1028b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = (      0*nVy + nVy-1)*nVx + xv + nC; /* top back vertices */
1029f5eeb527SMatthew G Knepley                   const PetscInt remoteVertex = ((nVz-1)*nVy +     0)*nVx + xv + nC; /* TODO: Correct this for neighbor sizes */
1030f5eeb527SMatthew G Knepley 
1031b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) {
1032f5eeb527SMatthew G Knepley                     localPoints[nL]        = localVertex;
1033f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
1034f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteVertex;
1035b2fff234SMatthew G. Knepley                     ++nL;
1036b2fff234SMatthew G. Knepley                   }
1037f5eeb527SMatthew G Knepley                 }
103888ed4aceSMatthew G Knepley               } else if (zp > 0) { /* front */
1039b2fff234SMatthew G. Knepley                 for (xv = 0; xv < nVx; ++xv) {
1040b2fff234SMatthew G. Knepley                   const PetscInt localVertex  = ((nVz-1)*nVy + nVy-1)*nVx + xv + nC; /* top front vertices */
1041f5eeb527SMatthew G Knepley                   const PetscInt remoteVertex = (      0*nVy +     0)*nVx + xv + nC; /* TODO: Correct this for neighbor sizes */
1042f5eeb527SMatthew G Knepley 
1043b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localVertex)) {
1044f5eeb527SMatthew G Knepley                     localPoints[nL]        = localVertex;
1045f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
1046f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteVertex;
1047b2fff234SMatthew G. Knepley                     ++nL;
1048b2fff234SMatthew G. Knepley                   }
1049f5eeb527SMatthew G Knepley                 }
105088ed4aceSMatthew G Knepley               } else {
1051f5eeb527SMatthew G Knepley                 for (zv = 0; zv < nVz; ++zv) {
1052b2fff234SMatthew G. Knepley                   for (xv = 0; xv < nVx; ++xv) {
1053b2fff234SMatthew G. Knepley                     const PetscInt localVertex  = (zv*nVy + nVy-1)*nVx + xv + nC; /* top vertices */
1054f5eeb527SMatthew G Knepley                     const PetscInt remoteVertex = (zv*nVy +     0)*nVx + xv + nC; /* TODO: Correct this for neighbor sizes */
1055f5eeb527SMatthew G Knepley 
1056b2fff234SMatthew G. Knepley                     if (!PetscBTLookupSet(isLeaf, localVertex)) {
1057f5eeb527SMatthew G Knepley                       localPoints[nL]        = localVertex;
1058f5eeb527SMatthew G Knepley                       remotePoints[nL].rank  = neighbor;
1059f5eeb527SMatthew G Knepley                       remotePoints[nL].index = remoteVertex;
1060b2fff234SMatthew G. Knepley                       ++nL;
1061f5eeb527SMatthew G Knepley                     }
1062f5eeb527SMatthew G Knepley                   }
1063b2fff234SMatthew G. Knepley                 }
1064b2fff234SMatthew G. Knepley #if 0
1065b2fff234SMatthew G. Knepley                 for (yf = 0; yf < nyF; ++yf) {
1066f5eeb527SMatthew G Knepley                   /* THIS IS WRONG */
1067b2fff234SMatthew G. Knepley                   const PetscInt localFace  = 0 + nC+nV; /* top faces */
1068f5eeb527SMatthew G Knepley                   const PetscInt remoteFace = 0 + nC+nV;
1069f5eeb527SMatthew G Knepley 
1070b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localFace)) {
1071f5eeb527SMatthew G Knepley                     localPoints[nL]        = localFace;
1072f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
1073f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteFace;
1074b2fff234SMatthew G. Knepley                     ++nL;
1075f5eeb527SMatthew G Knepley                   }
107688ed4aceSMatthew G Knepley                 }
1077b2fff234SMatthew G. Knepley #endif
1078b2fff234SMatthew G. Knepley               }
107988ed4aceSMatthew G Knepley             } else {
108088ed4aceSMatthew G Knepley               if (zp < 0) { /* back */
1081f5eeb527SMatthew G Knepley                 for (yv = 0; yv < nVy; ++yv) {
1082b2fff234SMatthew G. Knepley                   for (xv = 0; xv < nVx; ++xv) {
1083b2fff234SMatthew G. Knepley                     const PetscInt localVertex  = (      0*nVy + yv)*nVx + xv + nC; /* back vertices */
1084f5eeb527SMatthew G Knepley                     const PetscInt remoteVertex = ((nVz-1)*nVy + yv)*nVx + xv + nC; /* TODO: Correct this for neighbor sizes */
1085f5eeb527SMatthew G Knepley 
1086b2fff234SMatthew G. Knepley                     if (!PetscBTLookupSet(isLeaf, localVertex)) {
1087f5eeb527SMatthew G Knepley                       localPoints[nL]        = localVertex;
1088f5eeb527SMatthew G Knepley                       remotePoints[nL].rank  = neighbor;
1089f5eeb527SMatthew G Knepley                       remotePoints[nL].index = remoteVertex;
1090b2fff234SMatthew G. Knepley                       ++nL;
1091f5eeb527SMatthew G Knepley                     }
1092f5eeb527SMatthew G Knepley                   }
1093b2fff234SMatthew G. Knepley                 }
1094b2fff234SMatthew G. Knepley #if 0
1095b2fff234SMatthew G. Knepley                 for (zf = 0; zf < nzF; ++zf) {
1096f5eeb527SMatthew G Knepley                   /* THIS IS WRONG */
1097b2fff234SMatthew G. Knepley                   const PetscInt localFace  = 0 + nC+nV; /* back faces */
1098f5eeb527SMatthew G Knepley                   const PetscInt remoteFace = 0 + nC+nV;
1099f5eeb527SMatthew G Knepley 
1100b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localFace)) {
1101f5eeb527SMatthew G Knepley                     localPoints[nL]        = localFace;
1102f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
1103f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteFace;
1104b2fff234SMatthew G. Knepley                     ++nL;
1105f5eeb527SMatthew G Knepley                   }
1106b2fff234SMatthew G. Knepley                 }
1107b2fff234SMatthew G. Knepley #endif
110888ed4aceSMatthew G Knepley               } else if (zp > 0) { /* front */
1109f5eeb527SMatthew G Knepley                 for (yv = 0; yv < nVy; ++yv) {
1110b2fff234SMatthew G. Knepley                   for (xv = 0; xv < nVx; ++xv) {
1111b2fff234SMatthew G. Knepley                     const PetscInt localVertex  = ((nVz-1)*nVy + yv)*nVx + xv + nC; /* front vertices */
1112f5eeb527SMatthew G Knepley                     const PetscInt remoteVertex = (      0*nVy + yv)*nVx + xv + nC; /* TODO: Correct this for neighbor sizes */
1113f5eeb527SMatthew G Knepley 
1114b2fff234SMatthew G. Knepley                     if (!PetscBTLookupSet(isLeaf, localVertex)) {
1115f5eeb527SMatthew G Knepley                       localPoints[nL]        = localVertex;
1116f5eeb527SMatthew G Knepley                       remotePoints[nL].rank  = neighbor;
1117f5eeb527SMatthew G Knepley                       remotePoints[nL].index = remoteVertex;
1118b2fff234SMatthew G. Knepley                       ++nL;
1119f5eeb527SMatthew G Knepley                     }
1120f5eeb527SMatthew G Knepley                   }
1121b2fff234SMatthew G. Knepley                 }
1122b2fff234SMatthew G. Knepley #if 0
1123b2fff234SMatthew G. Knepley                 for (zf = 0; zf < nzF; ++zf) {
1124f5eeb527SMatthew G Knepley                   /* THIS IS WRONG */
1125b2fff234SMatthew G. Knepley                   const PetscInt localFace  = 0 + nC+nV; /* front faces */
1126f5eeb527SMatthew G Knepley                   const PetscInt remoteFace = 0 + nC+nV;
1127f5eeb527SMatthew G Knepley 
1128b2fff234SMatthew G. Knepley                   if (!PetscBTLookupSet(isLeaf, localFace)) {
1129f5eeb527SMatthew G Knepley                     localPoints[nL]        = localFace;
1130f5eeb527SMatthew G Knepley                     remotePoints[nL].rank  = neighbor;
1131f5eeb527SMatthew G Knepley                     remotePoints[nL].index = remoteFace;
1132b2fff234SMatthew G. Knepley                     ++nL;
1133f5eeb527SMatthew G Knepley                   }
1134b2fff234SMatthew G. Knepley                 }
1135b2fff234SMatthew G. Knepley #endif
113688ed4aceSMatthew G Knepley               } else {
113788ed4aceSMatthew G Knepley                 /* Nothing is shared from the interior */
113888ed4aceSMatthew G Knepley               }
113988ed4aceSMatthew G Knepley             }
114088ed4aceSMatthew G Knepley           }
114188ed4aceSMatthew G Knepley         }
114288ed4aceSMatthew G Knepley       }
114388ed4aceSMatthew G Knepley     }
114488ed4aceSMatthew G Knepley   }
1145b2fff234SMatthew G. Knepley   ierr = PetscBTDestroy(&isLeaf);CHKERRQ(ierr);
1146b2fff234SMatthew G. Knepley   /* Remove duplication in leaf determination */
1147b2fff234SMatthew 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);
114882f516ccSBarry Smith   ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm), &sf);CHKERRQ(ierr);
114988ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(sf, pEnd, nleaves, localPoints, PETSC_OWN_POINTER, remotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
1150a4b60ecfSMatthew G. Knepley   ierr = DMSetPointSF(dm, sf);CHKERRQ(ierr);
115188ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1152affa55c7SMatthew G. Knepley   *s = section;
1153affa55c7SMatthew G. Knepley   PetscFunctionReturn(0);
1154affa55c7SMatthew G. Knepley }
1155affa55c7SMatthew G. Knepley 
11563385ff7eSMatthew G. Knepley #undef __FUNCT__
11573385ff7eSMatthew G. Knepley #define __FUNCT__ "DMDASetVertexCoordinates"
11583385ff7eSMatthew G. Knepley PetscErrorCode DMDASetVertexCoordinates(DM dm, PetscReal xl, PetscReal xu, PetscReal yl, PetscReal yu, PetscReal zl, PetscReal zu)
11593385ff7eSMatthew G. Knepley {
11603385ff7eSMatthew G. Knepley   DM_DA         *da = (DM_DA *) dm->data;
11613385ff7eSMatthew G. Knepley   Vec            coordinates;
11623385ff7eSMatthew G. Knepley   PetscSection   section;
11633385ff7eSMatthew G. Knepley   PetscScalar   *coords;
11643385ff7eSMatthew G. Knepley   PetscReal      h[3];
11653385ff7eSMatthew G. Knepley   PetscInt       dim, size, M, N, P, nVx, nVy, nVz, nV, vStart, vEnd, v, i, j, k;
11663385ff7eSMatthew G. Knepley   PetscErrorCode ierr;
11673385ff7eSMatthew G. Knepley 
11683385ff7eSMatthew G. Knepley   PetscFunctionBegin;
11693385ff7eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11703385ff7eSMatthew G. Knepley   ierr = DMDAGetInfo(dm, &dim, &M, &N, &P, 0,0,0,0,0,0,0,0,0);CHKERRQ(ierr);
11713385ff7eSMatthew G. Knepley   h[0] = (xu - xl)/M;
11723385ff7eSMatthew G. Knepley   h[1] = (yu - yl)/N;
11733385ff7eSMatthew G. Knepley   h[2] = (zu - zl)/P;
11743385ff7eSMatthew G. Knepley   ierr = DMDAGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
11753385ff7eSMatthew G. Knepley   ierr = DMDAGetNumVertices(dm, &nVx, &nVy, &nVz, &nV);CHKERRQ(ierr);
11763385ff7eSMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &section);CHKERRQ(ierr);
11773385ff7eSMatthew G. Knepley   ierr = PetscSectionSetNumFields(section, 1);CHKERRQ(ierr);
11783385ff7eSMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(section, 0, dim);CHKERRQ(ierr);
11793385ff7eSMatthew G. Knepley   ierr = PetscSectionSetChart(section, vStart, vEnd);CHKERRQ(ierr);
11803385ff7eSMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
11813385ff7eSMatthew G. Knepley     ierr = PetscSectionSetDof(section, v, dim);CHKERRQ(ierr);
11823385ff7eSMatthew G. Knepley   }
11833385ff7eSMatthew G. Knepley   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
11843385ff7eSMatthew G. Knepley   ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
11853385ff7eSMatthew G. Knepley   ierr = VecCreateSeq(PETSC_COMM_SELF, size, &coordinates);CHKERRQ(ierr);
118624640c55SToby Isaac   ierr = PetscObjectSetName((PetscObject)coordinates,"coordinates");CHKERRQ(ierr);
11873385ff7eSMatthew G. Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
11883385ff7eSMatthew G. Knepley   for (k = 0; k < nVz; ++k) {
1189e4d69e08SBarry Smith     PetscInt ind[3], d, off;
11903385ff7eSMatthew G. Knepley 
1191e4d69e08SBarry Smith     ind[0] = 0;
1192e4d69e08SBarry Smith     ind[1] = 0;
1193e4d69e08SBarry Smith     ind[2] = k + da->zs;
11943385ff7eSMatthew G. Knepley     for (j = 0; j < nVy; ++j) {
11953385ff7eSMatthew G. Knepley       ind[1] = j + da->ys;
11963385ff7eSMatthew G. Knepley       for (i = 0; i < nVx; ++i) {
11973385ff7eSMatthew G. Knepley         const PetscInt vertex = (k*nVy + j)*nVx + i + vStart;
11983385ff7eSMatthew G. Knepley 
11993385ff7eSMatthew G. Knepley         ierr = PetscSectionGetOffset(section, vertex, &off);CHKERRQ(ierr);
12003385ff7eSMatthew G. Knepley         ind[0] = i + da->xs;
12013385ff7eSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
12023385ff7eSMatthew G. Knepley           coords[off+d] = h[d]*ind[d];
12033385ff7eSMatthew G. Knepley         }
12043385ff7eSMatthew G. Knepley       }
12053385ff7eSMatthew G. Knepley     }
12063385ff7eSMatthew G. Knepley   }
12073385ff7eSMatthew G. Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
120846e270d4SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, section);CHKERRQ(ierr);
12093385ff7eSMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
1210a4b60ecfSMatthew G. Knepley   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
12113385ff7eSMatthew G. Knepley   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
12123385ff7eSMatthew G. Knepley   PetscFunctionReturn(0);
12133385ff7eSMatthew G. Knepley }
12149a800dd8SMatthew G. Knepley 
12159a800dd8SMatthew G. Knepley #undef __FUNCT__
1216c970b36aSToby Isaac #define __FUNCT__ "DMProjectFunctionLocal_DA"
1217c970b36aSToby Isaac PetscErrorCode DMProjectFunctionLocal_DA(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
12189a800dd8SMatthew G. Knepley {
12192764a2aaSMatthew G. Knepley   PetscDS    prob;
1220b7a4d31fSMatthew G. Knepley   PetscFE         fe;
1221b7a4d31fSMatthew G. Knepley   PetscDualSpace  sp;
12229a800dd8SMatthew G. Knepley   PetscQuadrature q;
12239a800dd8SMatthew G. Knepley   PetscSection    section;
12249a800dd8SMatthew G. Knepley   PetscScalar    *values;
12257a1a1bd4SToby Isaac   PetscInt        numFields, numComp, numPoints, dim, dimEmbed, spDim, totDim, numValues, cStart, cEnd, f, c, v, d;
12269a800dd8SMatthew G. Knepley   PetscErrorCode  ierr;
12279a800dd8SMatthew G. Knepley 
12289a800dd8SMatthew G. Knepley   PetscFunctionBegin;
12292764a2aaSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
12302764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
12312764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(prob, 0, (PetscObject *) &fe);CHKERRQ(ierr);
1232b7a4d31fSMatthew G. Knepley   ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
12339a800dd8SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
12349a800dd8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
12359a800dd8SMatthew G. Knepley   ierr = DMDAGetInfo(dm, &dim,0,0,0,0,0,0,0,0,0,0,0,0);CHKERRQ(ierr);
12367a1a1bd4SToby Isaac   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
12379a800dd8SMatthew G. Knepley   ierr = DMDAGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
12389a800dd8SMatthew G. Knepley   ierr = DMDAVecGetClosure(dm, section, localX, cStart, &numValues, NULL);CHKERRQ(ierr);
12399a800dd8SMatthew G. Knepley   if (numValues != totDim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The section cell closure size %d != dual space dimension %d", numValues, totDim);
12409a800dd8SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
124121454ff5SMatthew G. Knepley   ierr = PetscQuadratureGetData(q, NULL, &numPoints, NULL, NULL);CHKERRQ(ierr);
12429a800dd8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1243de2092bfSMatthew G. Knepley     PetscFECellGeom geom;
12449a800dd8SMatthew G. Knepley 
1245de2092bfSMatthew G. Knepley     ierr          = DMDAComputeCellGeometryFEM(dm, c, q, geom.v0, geom.J, NULL, &geom.detJ);CHKERRQ(ierr);
12467a1a1bd4SToby Isaac     geom.dim      = dim;
12477a1a1bd4SToby Isaac     geom.dimEmbed = dimEmbed;
12489a800dd8SMatthew G. Knepley     for (f = 0, v = 0; f < numFields; ++f) {
1249c110b1eeSGeoffrey Irving       void * const ctx = ctxs ? ctxs[f] : NULL;
1250b7a4d31fSMatthew G. Knepley 
12512764a2aaSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
1252b7a4d31fSMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
1253b7a4d31fSMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &sp);CHKERRQ(ierr);
1254b7a4d31fSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(sp, &spDim);CHKERRQ(ierr);
12559a800dd8SMatthew G. Knepley       for (d = 0; d < spDim; ++d) {
12560163fd50SMatthew G. Knepley         ierr = PetscDualSpaceApply(sp, d, time, &geom, numComp, funcs[f], ctx, &values[v]);CHKERRQ(ierr);
12579a800dd8SMatthew G. Knepley         v += numComp;
12589a800dd8SMatthew G. Knepley       }
12599a800dd8SMatthew G. Knepley     }
12609a800dd8SMatthew G. Knepley     ierr = DMDAVecSetClosure(dm, section, localX, c, values, mode);CHKERRQ(ierr);
12619a800dd8SMatthew G. Knepley   }
12629a800dd8SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
12639a800dd8SMatthew G. Knepley   PetscFunctionReturn(0);
12649a800dd8SMatthew G. Knepley }
12659a800dd8SMatthew G. Knepley 
12669a800dd8SMatthew G. Knepley #undef __FUNCT__
1267c970b36aSToby Isaac #define __FUNCT__ "DMComputeL2Diff_DA"
1268c970b36aSToby Isaac PetscErrorCode DMComputeL2Diff_DA(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
12699a800dd8SMatthew G. Knepley {
12709a800dd8SMatthew G. Knepley   const PetscInt  debug = 0;
12712764a2aaSMatthew G. Knepley   PetscDS    prob;
1272b7a4d31fSMatthew G. Knepley   PetscFE         fe;
12739a800dd8SMatthew G. Knepley   PetscQuadrature quad;
1274b7a4d31fSMatthew G. Knepley   PetscSection    section;
12759a800dd8SMatthew G. Knepley   Vec             localX;
12769a800dd8SMatthew G. Knepley   PetscScalar    *funcVal;
12779a800dd8SMatthew G. Knepley   PetscReal      *coords, *v0, *J, *invJ, detJ;
12789a800dd8SMatthew G. Knepley   PetscReal       localDiff = 0.0;
1279b7a4d31fSMatthew G. Knepley   PetscInt        dim, numFields, Nc, cStart, cEnd, c, field, fieldOffset, comp;
12809a800dd8SMatthew G. Knepley   PetscErrorCode  ierr;
12819a800dd8SMatthew G. Knepley 
12829a800dd8SMatthew G. Knepley   PetscFunctionBegin;
12839a800dd8SMatthew G. Knepley   ierr = DMDAGetInfo(dm, &dim,0,0,0,0,0,0,0,0,0,0,0,0);CHKERRQ(ierr);
12842764a2aaSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
12852764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalComponents(prob, &Nc);CHKERRQ(ierr);
12862764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(prob, 0, (PetscObject *) &fe);CHKERRQ(ierr);
1287b7a4d31fSMatthew G. Knepley   ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
12889a800dd8SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
12899a800dd8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
12909a800dd8SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
12919a800dd8SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
12929a800dd8SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
12939a800dd8SMatthew G. Knepley   /* There are no BC values in DAs right now: ierr = DMDAProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */
1294b7a4d31fSMatthew G. Knepley   ierr = PetscMalloc5(Nc,&funcVal,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
12959a800dd8SMatthew G. Knepley   ierr = DMDAGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
12969a800dd8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
12979a800dd8SMatthew G. Knepley     PetscScalar *x = NULL;
12989a800dd8SMatthew G. Knepley     PetscReal    elemDiff = 0.0;
12999a800dd8SMatthew G. Knepley 
13008e0841e0SMatthew G. Knepley     ierr = DMDAComputeCellGeometryFEM(dm, c, quad, v0, J, invJ, &detJ);CHKERRQ(ierr);
13019a800dd8SMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
13029a800dd8SMatthew G. Knepley     ierr = DMDAVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
13039a800dd8SMatthew G. Knepley 
13049a800dd8SMatthew G. Knepley     for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) {
1305c110b1eeSGeoffrey Irving       void * const ctx = ctxs ? ctxs[field] : NULL;
130621454ff5SMatthew G. Knepley       const PetscReal *quadPoints, *quadWeights;
13079a800dd8SMatthew G. Knepley       PetscReal       *basis;
130821454ff5SMatthew G. Knepley       PetscInt         numQuadPoints, numBasisFuncs, numBasisComps, q, d, e, fc, f;
13099a800dd8SMatthew G. Knepley 
13102764a2aaSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr);
131121454ff5SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr);
1312b7a4d31fSMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &numBasisFuncs);CHKERRQ(ierr);
1313b7a4d31fSMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numBasisComps);CHKERRQ(ierr);
1314b7a4d31fSMatthew G. Knepley       ierr = PetscFEGetDefaultTabulation(fe, &basis, NULL, NULL);CHKERRQ(ierr);
13159a800dd8SMatthew G. Knepley       if (debug) {
13169a800dd8SMatthew G. Knepley         char title[1024];
13179a800dd8SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
13189a800dd8SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, numBasisFuncs*numBasisComps, &x[fieldOffset]);CHKERRQ(ierr);
13199a800dd8SMatthew G. Knepley       }
13209a800dd8SMatthew G. Knepley       for (q = 0; q < numQuadPoints; ++q) {
13219a800dd8SMatthew G. Knepley         for (d = 0; d < dim; d++) {
13229a800dd8SMatthew G. Knepley           coords[d] = v0[d];
13239a800dd8SMatthew G. Knepley           for (e = 0; e < dim; e++) {
13249a800dd8SMatthew G. Knepley             coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0);
13259a800dd8SMatthew G. Knepley           }
13269a800dd8SMatthew G. Knepley         }
1327a98e6df5SMatthew G. Knepley         ierr = (*funcs[field])(dim, time, coords, numFields, funcVal, ctx);CHKERRQ(ierr);
13289a800dd8SMatthew G. Knepley         for (fc = 0; fc < numBasisComps; ++fc) {
13299a800dd8SMatthew G. Knepley           PetscScalar interpolant = 0.0;
13309a800dd8SMatthew G. Knepley 
13319a800dd8SMatthew G. Knepley           for (f = 0; f < numBasisFuncs; ++f) {
13329a800dd8SMatthew G. Knepley             const PetscInt fidx = f*numBasisComps+fc;
13339a800dd8SMatthew G. Knepley             interpolant += x[fieldOffset+fidx]*basis[q*numBasisFuncs*numBasisComps+fidx];
13349a800dd8SMatthew G. Knepley           }
13359a800dd8SMatthew G. Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %d field %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ);CHKERRQ(ierr);}
13369a800dd8SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ;
13379a800dd8SMatthew G. Knepley         }
13389a800dd8SMatthew G. Knepley       }
13399a800dd8SMatthew G. Knepley       comp        += numBasisComps;
13409a800dd8SMatthew G. Knepley       fieldOffset += numBasisFuncs*numBasisComps;
13419a800dd8SMatthew G. Knepley     }
13429a800dd8SMatthew G. Knepley     ierr = DMDAVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
13439a800dd8SMatthew G. Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
13449a800dd8SMatthew G. Knepley     localDiff += elemDiff;
13459a800dd8SMatthew G. Knepley   }
13469a800dd8SMatthew G. Knepley   ierr  = PetscFree5(funcVal,coords,v0,J,invJ);CHKERRQ(ierr);
13479a800dd8SMatthew G. Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1348b2566f29SBarry Smith   ierr  = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
13499a800dd8SMatthew G. Knepley   *diff = PetscSqrtReal(*diff);
1350a66d4d66SMatthew G Knepley   PetscFunctionReturn(0);
1351a66d4d66SMatthew G Knepley }
1352a66d4d66SMatthew G Knepley 
135323d86601SMatthew G. Knepley #undef __FUNCT__
1354*b698f381SToby Isaac #define __FUNCT__ "DMComputeL2GradientDiff_DA"
1355*b698f381SToby Isaac PetscErrorCode DMComputeL2GradientDiff_DA(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, const PetscReal n[], PetscReal *diff)
135623d86601SMatthew G. Knepley {
135723d86601SMatthew G. Knepley   const PetscInt  debug = 0;
13582764a2aaSMatthew G. Knepley   PetscDS    prob;
1359b7a4d31fSMatthew G. Knepley   PetscFE         fe;
136023d86601SMatthew G. Knepley   PetscQuadrature quad;
1361b7a4d31fSMatthew G. Knepley   PetscSection    section;
136223d86601SMatthew G. Knepley   Vec             localX;
136323d86601SMatthew G. Knepley   PetscScalar    *funcVal, *interpolantVec;
136423d86601SMatthew G. Knepley   PetscReal      *coords, *realSpaceDer, *v0, *J, *invJ, detJ;
136523d86601SMatthew G. Knepley   PetscReal       localDiff = 0.0;
1366b7a4d31fSMatthew G. Knepley   PetscInt        dim, numFields, Nc, cStart, cEnd, c, field, fieldOffset, comp;
136723d86601SMatthew G. Knepley   PetscErrorCode  ierr;
136823d86601SMatthew G. Knepley 
136923d86601SMatthew G. Knepley   PetscFunctionBegin;
137023d86601SMatthew G. Knepley   ierr = DMDAGetInfo(dm, &dim,0,0,0,0,0,0,0,0,0,0,0,0);CHKERRQ(ierr);
13712764a2aaSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
13722764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalComponents(prob, &Nc);CHKERRQ(ierr);
13732764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(prob, 0, (PetscObject *) &fe);CHKERRQ(ierr);
1374b7a4d31fSMatthew G. Knepley   ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
137523d86601SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
137623d86601SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
137723d86601SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
137823d86601SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
137923d86601SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
138023d86601SMatthew G. Knepley   /* There are no BC values in DAs right now: ierr = DMDAProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */
1381b7a4d31fSMatthew G. Knepley   ierr = PetscMalloc7(Nc,&funcVal,dim,&coords,dim,&realSpaceDer,dim,&v0,dim*dim,&J,dim*dim,&invJ,dim,&interpolantVec);CHKERRQ(ierr);
138223d86601SMatthew G. Knepley   ierr = DMDAGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
138323d86601SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
138423d86601SMatthew G. Knepley     PetscScalar *x = NULL;
138523d86601SMatthew G. Knepley     PetscReal    elemDiff = 0.0;
138623d86601SMatthew G. Knepley 
13878e0841e0SMatthew G. Knepley     ierr = DMDAComputeCellGeometryFEM(dm, c, quad, v0, J, invJ, &detJ);CHKERRQ(ierr);
138823d86601SMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
138923d86601SMatthew G. Knepley     ierr = DMDAVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
139023d86601SMatthew G. Knepley 
139123d86601SMatthew G. Knepley     for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) {
139223d86601SMatthew G. Knepley       void * const ctx = ctxs ? ctxs[field] : NULL;
139323d86601SMatthew G. Knepley       const PetscReal *quadPoints, *quadWeights;
139423d86601SMatthew G. Knepley       PetscReal       *basisDer;
139523d86601SMatthew G. Knepley       PetscInt         Nq, Nb, Nc, q, d, e, fc, f, g;
139623d86601SMatthew G. Knepley 
13972764a2aaSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr);
139823d86601SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
1399b7a4d31fSMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
1400b7a4d31fSMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
1401b7a4d31fSMatthew G. Knepley       ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr);
140223d86601SMatthew G. Knepley       if (debug) {
140323d86601SMatthew G. Knepley         char title[1024];
140423d86601SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
140523d86601SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb*Nc, &x[fieldOffset]);CHKERRQ(ierr);
140623d86601SMatthew G. Knepley       }
140723d86601SMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
140823d86601SMatthew G. Knepley         for (d = 0; d < dim; d++) {
140923d86601SMatthew G. Knepley           coords[d] = v0[d];
141023d86601SMatthew G. Knepley           for (e = 0; e < dim; e++) {
141123d86601SMatthew G. Knepley             coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0);
141223d86601SMatthew G. Knepley           }
141323d86601SMatthew G. Knepley         }
1414a98e6df5SMatthew G. Knepley         ierr = (*funcs[field])(dim, time, coords, n, numFields, funcVal, ctx);CHKERRQ(ierr);
141523d86601SMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
141623d86601SMatthew G. Knepley           PetscScalar interpolant = 0.0;
141723d86601SMatthew G. Knepley 
141823d86601SMatthew G. Knepley           for (d = 0; d < dim; ++d) interpolantVec[d] = 0.0;
141923d86601SMatthew G. Knepley           for (f = 0; f < Nb; ++f) {
142023d86601SMatthew G. Knepley             const PetscInt fidx = f*Nc+fc;
142123d86601SMatthew G. Knepley 
142223d86601SMatthew G. Knepley             for (d = 0; d < dim; ++d) {
142323d86601SMatthew G. Knepley               realSpaceDer[d] = 0.0;
142423d86601SMatthew G. Knepley               for (g = 0; g < dim; ++g) {
142523d86601SMatthew G. Knepley                 realSpaceDer[d] += invJ[g*dim+d]*basisDer[(q*Nb*Nc+fidx)*dim+g];
142623d86601SMatthew G. Knepley               }
142723d86601SMatthew G. Knepley               interpolantVec[d] += x[fieldOffset+fidx]*realSpaceDer[d];
142823d86601SMatthew G. Knepley             }
142923d86601SMatthew G. Knepley           }
143023d86601SMatthew G. Knepley           for (d = 0; d < dim; ++d) interpolant += interpolantVec[d]*n[d];
143123d86601SMatthew G. Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %d fieldDer %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ);CHKERRQ(ierr);}
143223d86601SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ;
143323d86601SMatthew G. Knepley         }
143423d86601SMatthew G. Knepley       }
143523d86601SMatthew G. Knepley       comp        += Nc;
143623d86601SMatthew G. Knepley       fieldOffset += Nb*Nc;
143723d86601SMatthew G. Knepley     }
143823d86601SMatthew G. Knepley     ierr = DMDAVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
143923d86601SMatthew G. Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
144023d86601SMatthew G. Knepley     localDiff += elemDiff;
144123d86601SMatthew G. Knepley   }
144223d86601SMatthew G. Knepley   ierr  = PetscFree7(funcVal,coords,realSpaceDer,v0,J,invJ,interpolantVec);CHKERRQ(ierr);
144323d86601SMatthew G. Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1444b2566f29SBarry Smith   ierr  = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
144523d86601SMatthew G. Knepley   *diff = PetscSqrtReal(*diff);
144623d86601SMatthew G. Knepley   PetscFunctionReturn(0);
144723d86601SMatthew G. Knepley }
144823d86601SMatthew G. Knepley 
144947c6ae99SBarry Smith /* ------------------------------------------------------------------- */
145047c6ae99SBarry Smith 
145147c6ae99SBarry Smith #undef __FUNCT__
1452aa219208SBarry Smith #define __FUNCT__ "DMDAGetArray"
145347c6ae99SBarry Smith /*@C
1454aa219208SBarry Smith      DMDAGetArray - Gets a work array for a DMDA
145547c6ae99SBarry Smith 
145647c6ae99SBarry Smith     Input Parameter:
145747c6ae99SBarry Smith +    da - information about my local patch
145847c6ae99SBarry Smith -    ghosted - do you want arrays for the ghosted or nonghosted patch
145947c6ae99SBarry Smith 
146047c6ae99SBarry Smith     Output Parameters:
146147c6ae99SBarry Smith .    vptr - array data structured
146247c6ae99SBarry Smith 
146347c6ae99SBarry Smith     Note:  The vector values are NOT initialized and may have garbage in them, so you may need
146447c6ae99SBarry Smith            to zero them.
146547c6ae99SBarry Smith 
146647c6ae99SBarry Smith   Level: advanced
146747c6ae99SBarry Smith 
1468bcaeba4dSBarry Smith .seealso: DMDARestoreArray()
146947c6ae99SBarry Smith 
147047c6ae99SBarry Smith @*/
14717087cfbeSBarry Smith PetscErrorCode  DMDAGetArray(DM da,PetscBool ghosted,void *vptr)
147247c6ae99SBarry Smith {
147347c6ae99SBarry Smith   PetscErrorCode ierr;
147447c6ae99SBarry Smith   PetscInt       j,i,xs,ys,xm,ym,zs,zm;
147547c6ae99SBarry Smith   char           *iarray_start;
147647c6ae99SBarry Smith   void           **iptr = (void**)vptr;
147747c6ae99SBarry Smith   DM_DA          *dd    = (DM_DA*)da->data;
147847c6ae99SBarry Smith 
147947c6ae99SBarry Smith   PetscFunctionBegin;
148047c6ae99SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
148147c6ae99SBarry Smith   if (ghosted) {
1482aa219208SBarry Smith     for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) {
148347c6ae99SBarry Smith       if (dd->arrayghostedin[i]) {
148447c6ae99SBarry Smith         *iptr                 = dd->arrayghostedin[i];
148547c6ae99SBarry Smith         iarray_start          = (char*)dd->startghostedin[i];
14860298fd71SBarry Smith         dd->arrayghostedin[i] = NULL;
14870298fd71SBarry Smith         dd->startghostedin[i] = NULL;
148847c6ae99SBarry Smith 
148947c6ae99SBarry Smith         goto done;
149047c6ae99SBarry Smith       }
149147c6ae99SBarry Smith     }
149247c6ae99SBarry Smith     xs = dd->Xs;
149347c6ae99SBarry Smith     ys = dd->Ys;
149447c6ae99SBarry Smith     zs = dd->Zs;
149547c6ae99SBarry Smith     xm = dd->Xe-dd->Xs;
149647c6ae99SBarry Smith     ym = dd->Ye-dd->Ys;
149747c6ae99SBarry Smith     zm = dd->Ze-dd->Zs;
149847c6ae99SBarry Smith   } else {
1499aa219208SBarry Smith     for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) {
150047c6ae99SBarry Smith       if (dd->arrayin[i]) {
150147c6ae99SBarry Smith         *iptr          = dd->arrayin[i];
150247c6ae99SBarry Smith         iarray_start   = (char*)dd->startin[i];
15030298fd71SBarry Smith         dd->arrayin[i] = NULL;
15040298fd71SBarry Smith         dd->startin[i] = NULL;
150547c6ae99SBarry Smith 
150647c6ae99SBarry Smith         goto done;
150747c6ae99SBarry Smith       }
150847c6ae99SBarry Smith     }
150947c6ae99SBarry Smith     xs = dd->xs;
151047c6ae99SBarry Smith     ys = dd->ys;
151147c6ae99SBarry Smith     zs = dd->zs;
151247c6ae99SBarry Smith     xm = dd->xe-dd->xs;
151347c6ae99SBarry Smith     ym = dd->ye-dd->ys;
151447c6ae99SBarry Smith     zm = dd->ze-dd->zs;
151547c6ae99SBarry Smith   }
151647c6ae99SBarry Smith 
1517c73cfb54SMatthew G. Knepley   switch (da->dim) {
151847c6ae99SBarry Smith   case 1: {
151947c6ae99SBarry Smith     void *ptr;
152047c6ae99SBarry Smith 
1521901f1932SJed Brown     ierr = PetscMalloc(xm*sizeof(PetscScalar),&iarray_start);CHKERRQ(ierr);
152247c6ae99SBarry Smith 
152347c6ae99SBarry Smith     ptr   = (void*)(iarray_start - xs*sizeof(PetscScalar));
152447c6ae99SBarry Smith     *iptr = (void*)ptr;
15258865f1eaSKarl Rupp     break;
15268865f1eaSKarl Rupp   }
152747c6ae99SBarry Smith   case 2: {
152847c6ae99SBarry Smith     void **ptr;
152947c6ae99SBarry Smith 
1530901f1932SJed Brown     ierr = PetscMalloc((ym+1)*sizeof(void*)+xm*ym*sizeof(PetscScalar),&iarray_start);CHKERRQ(ierr);
153147c6ae99SBarry Smith 
153247c6ae99SBarry Smith     ptr = (void**)(iarray_start + xm*ym*sizeof(PetscScalar) - ys*sizeof(void*));
15338865f1eaSKarl Rupp     for (j=ys; j<ys+ym; j++) ptr[j] = iarray_start + sizeof(PetscScalar)*(xm*(j-ys) - xs);
153447c6ae99SBarry Smith     *iptr = (void*)ptr;
15358865f1eaSKarl Rupp     break;
15368865f1eaSKarl Rupp   }
153747c6ae99SBarry Smith   case 3: {
153847c6ae99SBarry Smith     void ***ptr,**bptr;
153947c6ae99SBarry Smith 
1540901f1932SJed Brown     ierr = PetscMalloc((zm+1)*sizeof(void**)+(ym*zm+1)*sizeof(void*)+xm*ym*zm*sizeof(PetscScalar),&iarray_start);CHKERRQ(ierr);
154147c6ae99SBarry Smith 
154247c6ae99SBarry Smith     ptr  = (void***)(iarray_start + xm*ym*zm*sizeof(PetscScalar) - zs*sizeof(void*));
154347c6ae99SBarry Smith     bptr = (void**)(iarray_start + xm*ym*zm*sizeof(PetscScalar) + zm*sizeof(void**));
15448865f1eaSKarl Rupp     for (i=zs; i<zs+zm; i++) ptr[i] = bptr + ((i-zs)*ym - ys);
154547c6ae99SBarry Smith     for (i=zs; i<zs+zm; i++) {
154647c6ae99SBarry Smith       for (j=ys; j<ys+ym; j++) {
154747c6ae99SBarry Smith         ptr[i][j] = iarray_start + sizeof(PetscScalar)*(xm*ym*(i-zs) + xm*(j-ys) - xs);
154847c6ae99SBarry Smith       }
154947c6ae99SBarry Smith     }
155047c6ae99SBarry Smith     *iptr = (void*)ptr;
15518865f1eaSKarl Rupp     break;
15528865f1eaSKarl Rupp   }
155347c6ae99SBarry Smith   default:
1554c73cfb54SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject)da),PETSC_ERR_SUP,"Dimension %D not supported",da->dim);
155547c6ae99SBarry Smith   }
155647c6ae99SBarry Smith 
155747c6ae99SBarry Smith done:
155847c6ae99SBarry Smith   /* add arrays to the checked out list */
155947c6ae99SBarry Smith   if (ghosted) {
1560aa219208SBarry Smith     for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) {
156147c6ae99SBarry Smith       if (!dd->arrayghostedout[i]) {
156247c6ae99SBarry Smith         dd->arrayghostedout[i] = *iptr;
156347c6ae99SBarry Smith         dd->startghostedout[i] = iarray_start;
156447c6ae99SBarry Smith         break;
156547c6ae99SBarry Smith       }
156647c6ae99SBarry Smith     }
156747c6ae99SBarry Smith   } else {
1568aa219208SBarry Smith     for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) {
156947c6ae99SBarry Smith       if (!dd->arrayout[i]) {
157047c6ae99SBarry Smith         dd->arrayout[i] = *iptr;
157147c6ae99SBarry Smith         dd->startout[i] = iarray_start;
157247c6ae99SBarry Smith         break;
157347c6ae99SBarry Smith       }
157447c6ae99SBarry Smith     }
157547c6ae99SBarry Smith   }
157647c6ae99SBarry Smith   PetscFunctionReturn(0);
157747c6ae99SBarry Smith }
157847c6ae99SBarry Smith 
157947c6ae99SBarry Smith #undef __FUNCT__
1580aa219208SBarry Smith #define __FUNCT__ "DMDARestoreArray"
158147c6ae99SBarry Smith /*@C
1582aa219208SBarry Smith      DMDARestoreArray - Restores an array of derivative types for a DMDA
158347c6ae99SBarry Smith 
158447c6ae99SBarry Smith     Input Parameter:
158547c6ae99SBarry Smith +    da - information about my local patch
158647c6ae99SBarry Smith .    ghosted - do you want arrays for the ghosted or nonghosted patch
158747c6ae99SBarry Smith -    vptr - array data structured to be passed to ad_FormFunctionLocal()
158847c6ae99SBarry Smith 
158947c6ae99SBarry Smith      Level: advanced
159047c6ae99SBarry Smith 
1591bcaeba4dSBarry Smith .seealso: DMDAGetArray()
159247c6ae99SBarry Smith 
159347c6ae99SBarry Smith @*/
15947087cfbeSBarry Smith PetscErrorCode  DMDARestoreArray(DM da,PetscBool ghosted,void *vptr)
159547c6ae99SBarry Smith {
159647c6ae99SBarry Smith   PetscInt i;
159747c6ae99SBarry Smith   void     **iptr = (void**)vptr,*iarray_start = 0;
159847c6ae99SBarry Smith   DM_DA    *dd    = (DM_DA*)da->data;
159947c6ae99SBarry Smith 
160047c6ae99SBarry Smith   PetscFunctionBegin;
160147c6ae99SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
160247c6ae99SBarry Smith   if (ghosted) {
1603aa219208SBarry Smith     for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) {
160447c6ae99SBarry Smith       if (dd->arrayghostedout[i] == *iptr) {
160547c6ae99SBarry Smith         iarray_start           = dd->startghostedout[i];
16060298fd71SBarry Smith         dd->arrayghostedout[i] = NULL;
16070298fd71SBarry Smith         dd->startghostedout[i] = NULL;
160847c6ae99SBarry Smith         break;
160947c6ae99SBarry Smith       }
161047c6ae99SBarry Smith     }
1611aa219208SBarry Smith     for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) {
161247c6ae99SBarry Smith       if (!dd->arrayghostedin[i]) {
161347c6ae99SBarry Smith         dd->arrayghostedin[i] = *iptr;
161447c6ae99SBarry Smith         dd->startghostedin[i] = iarray_start;
161547c6ae99SBarry Smith         break;
161647c6ae99SBarry Smith       }
161747c6ae99SBarry Smith     }
161847c6ae99SBarry Smith   } else {
1619aa219208SBarry Smith     for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) {
162047c6ae99SBarry Smith       if (dd->arrayout[i] == *iptr) {
162147c6ae99SBarry Smith         iarray_start    = dd->startout[i];
16220298fd71SBarry Smith         dd->arrayout[i] = NULL;
16230298fd71SBarry Smith         dd->startout[i] = NULL;
162447c6ae99SBarry Smith         break;
162547c6ae99SBarry Smith       }
162647c6ae99SBarry Smith     }
1627aa219208SBarry Smith     for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) {
162847c6ae99SBarry Smith       if (!dd->arrayin[i]) {
162947c6ae99SBarry Smith         dd->arrayin[i] = *iptr;
163047c6ae99SBarry Smith         dd->startin[i] = iarray_start;
163147c6ae99SBarry Smith         break;
163247c6ae99SBarry Smith       }
163347c6ae99SBarry Smith     }
163447c6ae99SBarry Smith   }
163547c6ae99SBarry Smith   PetscFunctionReturn(0);
163647c6ae99SBarry Smith }
163747c6ae99SBarry Smith 
1638