xref: /petsc/src/dm/impls/da/dageometry.c (revision 9c3cf19f96b150d3072ff3db14cab4776f10ce04)
1af0996ceSBarry Smith #include <petsc/private/dmdaimpl.h>     /*I  "petscdmda.h"   I*/
257459e9aSMatthew G Knepley 
3d7a12f93SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode FillClosureArray_Static(DM dm, PetscSection section, PetscInt nP, const PetscInt points[], PetscScalar *vArray, PetscInt *csize, PetscScalar **array)
457459e9aSMatthew G Knepley {
557459e9aSMatthew G Knepley   PetscScalar    *a;
60a3ada39SMatthew G. Knepley   PetscInt       pStart, pEnd, size = 0, dof, off, d, k, i;
757459e9aSMatthew G Knepley   PetscErrorCode ierr;
857459e9aSMatthew G Knepley 
957459e9aSMatthew G Knepley   PetscFunctionBegin;
100a3ada39SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1157459e9aSMatthew G Knepley   for (i = 0; i < nP; ++i) {
120a3ada39SMatthew G. Knepley     const PetscInt p = points[i];
130a3ada39SMatthew G. Knepley 
140a3ada39SMatthew G. Knepley     if ((p < pStart) || (p >= pEnd)) continue;
150a3ada39SMatthew G. Knepley     ierr  = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1657459e9aSMatthew G Knepley     size += dof;
1757459e9aSMatthew G Knepley   }
180a3ada39SMatthew G. Knepley   if (csize) *csize = size;
190a3ada39SMatthew G. Knepley   if (array) {
20aa1993deSMatthew G Knepley     ierr = DMGetWorkArray(dm, size, PETSC_SCALAR, &a);CHKERRQ(ierr);
2157459e9aSMatthew G Knepley     for (i = 0, k = 0; i < nP; ++i) {
220a3ada39SMatthew G. Knepley       const PetscInt p = points[i];
230a3ada39SMatthew G. Knepley 
240a3ada39SMatthew G. Knepley       if ((p < pStart) || (p >= pEnd)) continue;
250a3ada39SMatthew G. Knepley       ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
260a3ada39SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, p, &off);CHKERRQ(ierr);
2757459e9aSMatthew G Knepley 
288865f1eaSKarl Rupp       for (d = 0; d < dof; ++d, ++k) a[k] = vArray[off+d];
2957459e9aSMatthew G Knepley     }
3057459e9aSMatthew G Knepley     *array = a;
310a3ada39SMatthew G. Knepley   }
3257459e9aSMatthew G Knepley   PetscFunctionReturn(0);
3357459e9aSMatthew G Knepley }
3457459e9aSMatthew G Knepley 
3557459e9aSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode FillClosureVec_Private(DM dm, PetscSection section, PetscInt nP, const PetscInt points[], PetscScalar *vArray, const PetscScalar *array, InsertMode mode)
3657459e9aSMatthew G Knepley {
3757459e9aSMatthew G Knepley   PetscInt       dof, off, d, k, i;
3857459e9aSMatthew G Knepley   PetscErrorCode ierr;
3957459e9aSMatthew G Knepley 
4057459e9aSMatthew G Knepley   PetscFunctionBegin;
4157459e9aSMatthew G Knepley   if ((mode == INSERT_VALUES) || (mode == INSERT_ALL_VALUES)) {
4257459e9aSMatthew G Knepley     for (i = 0, k = 0; i < nP; ++i) {
4357459e9aSMatthew G Knepley       ierr = PetscSectionGetDof(section, points[i], &dof);CHKERRQ(ierr);
4457459e9aSMatthew G Knepley       ierr = PetscSectionGetOffset(section, points[i], &off);CHKERRQ(ierr);
4557459e9aSMatthew G Knepley 
468865f1eaSKarl Rupp       for (d = 0; d < dof; ++d, ++k) vArray[off+d] = array[k];
4757459e9aSMatthew G Knepley     }
4857459e9aSMatthew G Knepley   } else {
4957459e9aSMatthew G Knepley     for (i = 0, k = 0; i < nP; ++i) {
5057459e9aSMatthew G Knepley       ierr = PetscSectionGetDof(section, points[i], &dof);CHKERRQ(ierr);
5157459e9aSMatthew G Knepley       ierr = PetscSectionGetOffset(section, points[i], &off);CHKERRQ(ierr);
5257459e9aSMatthew G Knepley 
538865f1eaSKarl Rupp       for (d = 0; d < dof; ++d, ++k) vArray[off+d] += array[k];
5457459e9aSMatthew G Knepley     }
5557459e9aSMatthew G Knepley   }
5657459e9aSMatthew G Knepley   PetscFunctionReturn(0);
5757459e9aSMatthew G Knepley }
5857459e9aSMatthew G Knepley 
59aa1993deSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode GetPointArray_Private(DM dm,PetscInt n,PetscInt *points,PetscInt *rn,const PetscInt **rpoints)
60aa1993deSMatthew G Knepley {
61aa1993deSMatthew G Knepley   PetscErrorCode ierr;
62aa1993deSMatthew G Knepley   PetscInt       *work;
63aa1993deSMatthew G Knepley 
64aa1993deSMatthew G Knepley   PetscFunctionBegin;
65aa1993deSMatthew G Knepley   if (rn) *rn = n;
66aa1993deSMatthew G Knepley   if (rpoints) {
67aa1993deSMatthew G Knepley     ierr     = DMGetWorkArray(dm,n,PETSC_INT,&work);CHKERRQ(ierr);
68aa1993deSMatthew G Knepley     ierr     = PetscMemcpy(work,points,n*sizeof(PetscInt));CHKERRQ(ierr);
69aa1993deSMatthew G Knepley     *rpoints = work;
70aa1993deSMatthew G Knepley   }
71aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
72aa1993deSMatthew G Knepley }
73aa1993deSMatthew G Knepley 
74aa1993deSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode RestorePointArray_Private(DM dm,PetscInt *rn,const PetscInt **rpoints)
75aa1993deSMatthew G Knepley {
76aa1993deSMatthew G Knepley   PetscErrorCode ierr;
77aa1993deSMatthew G Knepley 
78aa1993deSMatthew G Knepley   PetscFunctionBegin;
79aa1993deSMatthew G Knepley   if (rn) *rn = 0;
808865f1eaSKarl Rupp   if (rpoints) {
812a808120SBarry Smith     /* note the second argument to DMRestoreWorkArray() is always ignored */
822a808120SBarry Smith     ierr = DMRestoreWorkArray(dm,0, PETSC_INT, (void*) rpoints);CHKERRQ(ierr);
838865f1eaSKarl Rupp   }
84aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
85aa1993deSMatthew G Knepley }
86aa1993deSMatthew G Knepley 
876693a731SMatthew G. Knepley PetscErrorCode DMDAGetTransitiveClosure(DM dm, PetscInt p, PetscBool useClosure, PetscInt *closureSize, PetscInt **closure)
886693a731SMatthew G. Knepley {
89c73cfb54SMatthew G. Knepley   PetscInt       dim = dm->dim;
906693a731SMatthew G. Knepley   PetscInt       nVx, nVy, nVz, nxF, nXF, nyF, nYF, nzF, nZF;
916693a731SMatthew G. Knepley   PetscInt       pStart, pEnd, cStart, cEnd, vStart, vEnd, fStart, fEnd, xfStart, xfEnd, yfStart;
926693a731SMatthew G. Knepley   PetscErrorCode ierr;
936693a731SMatthew G. Knepley 
946693a731SMatthew G. Knepley   PetscFunctionBegin;
956693a731SMatthew G. Knepley   if (!useClosure) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Star operation is not yet supported");
966693a731SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
972a808120SBarry Smith   PetscValidIntPointer(closureSize,4);
986693a731SMatthew G. Knepley   ierr = DMDAGetHeightStratum(dm, -1,  &pStart, &pEnd);CHKERRQ(ierr);
996693a731SMatthew G. Knepley   ierr = DMDAGetHeightStratum(dm,  0,  &cStart, &cEnd);CHKERRQ(ierr);
1006693a731SMatthew G. Knepley   ierr = DMDAGetHeightStratum(dm,  1,  &fStart, &fEnd);CHKERRQ(ierr);
1016693a731SMatthew G. Knepley   ierr = DMDAGetHeightStratum(dm, dim, &vStart, &vEnd);CHKERRQ(ierr);
1026693a731SMatthew G. Knepley   ierr = DMDAGetNumVertices(dm, &nVx, &nVy, &nVz, NULL);CHKERRQ(ierr);
1036693a731SMatthew G. Knepley   ierr = DMDAGetNumFaces(dm, &nxF, &nXF, &nyF, &nYF, &nzF, &nZF);CHKERRQ(ierr);
1046693a731SMatthew G. Knepley   xfStart = fStart; xfEnd = xfStart+nXF;
1056693a731SMatthew G. Knepley   yfStart = xfEnd;
1066693a731SMatthew G. Knepley   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid point %d should be in [%d, %d)", p, pStart, pEnd);
1076693a731SMatthew G. Knepley   if ((p >= cStart) || (p < cEnd)) {
1086693a731SMatthew G. Knepley     /* Cell */
1096693a731SMatthew G. Knepley     if (dim == 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Not implemented");
1106693a731SMatthew G. Knepley     else if (dim == 2) {
1116693a731SMatthew G. Knepley       /* 4 faces, 4 vertices
1126693a731SMatthew G. Knepley          Bottom-left vertex follows same order as cells
1136693a731SMatthew G. Knepley          Bottom y-face same order as cells
1146693a731SMatthew G. Knepley          Left x-face follows same order as cells
1156693a731SMatthew G. Knepley          We number the quad:
1166693a731SMatthew G. Knepley 
1176693a731SMatthew G. Knepley            8--3--7
1186693a731SMatthew G. Knepley            |     |
1196693a731SMatthew G. Knepley            4  0  2
1206693a731SMatthew G. Knepley            |     |
1216693a731SMatthew G. Knepley            5--1--6
1226693a731SMatthew G. Knepley       */
1236693a731SMatthew G. Knepley       PetscInt c  = p - cStart, cx = c % (nVx-1), cy = c / (nVx-1);
1246693a731SMatthew G. Knepley       PetscInt v  = cy*nVx + cx +  vStart;
1256693a731SMatthew G. Knepley       PetscInt xf = cy*nxF + cx + xfStart;
1266693a731SMatthew G. Knepley       PetscInt yf = c + yfStart;
1276693a731SMatthew G. Knepley 
1282a808120SBarry Smith       *closureSize = 9;
1296693a731SMatthew G. Knepley       if (!(*closure)) {ierr = DMGetWorkArray(dm, *closureSize, PETSC_INT, closure);CHKERRQ(ierr);}
1306693a731SMatthew G. Knepley       (*closure)[0] = p; (*closure)[1] = yf; (*closure)[2] = xf+1; (*closure)[3] = yf+nyF; (*closure)[4] = xf+0; (*closure)[5] = v+0; (*closure)[6]= v+1; (*closure)[7] = v+nVx+1; (*closure)[8] = v+nVx+0;
1316693a731SMatthew G. Knepley     } else {
1326693a731SMatthew G. Knepley       /* 6 faces, 12 edges, 8 vertices
1336693a731SMatthew G. Knepley          Bottom-left vertex follows same order as cells
1346693a731SMatthew G. Knepley          Bottom y-face same order as cells
1356693a731SMatthew G. Knepley          Left x-face follows same order as cells
1366693a731SMatthew G. Knepley          We number the quad:
1376693a731SMatthew G. Knepley 
1386693a731SMatthew G. Knepley            8--3--7
1396693a731SMatthew G. Knepley            |     |
1406693a731SMatthew G. Knepley            4  0  2
1416693a731SMatthew G. Knepley            |     |
1426693a731SMatthew G. Knepley            5--1--6
1436693a731SMatthew G. Knepley       */
144be8e0784SMatthew G. Knepley #if 0
1456693a731SMatthew G. Knepley       PetscInt c  = p - cStart, cx = c % (nVx-1), cy = c / (nVx-1), cz = c / ((nVx-1)*(nVy-1));
1466693a731SMatthew G. Knepley       PetscInt v  = cy*nVx + cx +  vStart;
1476693a731SMatthew G. Knepley       PetscInt xf = cy*nxF + cx + xfStart;
1486693a731SMatthew G. Knepley       PetscInt yf = c + yfStart;
1496693a731SMatthew G. Knepley 
1502a808120SBarry Smith       *closureSize = 26;
1516693a731SMatthew G. Knepley       if (!(*closure)) {ierr = DMGetWorkArray(dm, *closureSize, PETSC_INT, closure);CHKERRQ(ierr);}
152be8e0784SMatthew G. Knepley #endif
153be8e0784SMatthew G. Knepley       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Not implemented");
1546693a731SMatthew G. Knepley     }
1556693a731SMatthew G. Knepley   } else if ((p >= vStart) || (p < vEnd)) {
1566693a731SMatthew G. Knepley     /* Vertex */
1572a808120SBarry Smith     *closureSize = 1;
1586693a731SMatthew G. Knepley     if (!(*closure)) {ierr = DMGetWorkArray(dm, *closureSize, PETSC_INT, closure);CHKERRQ(ierr);}
1596693a731SMatthew G. Knepley     (*closure)[0] = p;
1606693a731SMatthew G. Knepley   } else if ((p >= fStart) || (p < fStart + nXF)) {
1616693a731SMatthew G. Knepley     /* X Face */
1626693a731SMatthew G. Knepley     if (dim == 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "There are no faces in 1D");
1636693a731SMatthew G. Knepley     else if (dim == 2) {
1646693a731SMatthew G. Knepley       /* 2 vertices: The bottom vertex has the same numbering as the face */
1656693a731SMatthew G. Knepley       PetscInt f = p - xfStart;
1666693a731SMatthew G. Knepley 
1672a808120SBarry Smith       *closureSize = 3;
1686693a731SMatthew G. Knepley       if (!(*closure)) {ierr = DMGetWorkArray(dm, *closureSize, PETSC_INT, closure);CHKERRQ(ierr);}
1696693a731SMatthew G. Knepley       (*closure)[0] = p; (*closure)[1] = f; (*closure)[2] = f+nVx;
1706693a731SMatthew G. Knepley     } else if (dim == 3) {
1716693a731SMatthew G. Knepley       /* 4 vertices */
1726693a731SMatthew G. Knepley       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Not implemented");
1736693a731SMatthew G. Knepley     }
1746693a731SMatthew G. Knepley   } else if ((p >= fStart + nXF) || (p < fStart + nXF + nYF)) {
1756693a731SMatthew G. Knepley     /* Y Face */
1766693a731SMatthew G. Knepley     if (dim == 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "There are no faces in 1D");
1776693a731SMatthew G. Knepley     else if (dim == 2) {
1786693a731SMatthew G. Knepley       /* 2 vertices: The left vertex has the same numbering as the face */
1796693a731SMatthew G. Knepley       PetscInt f = p - yfStart;
1806693a731SMatthew G. Knepley 
1812a808120SBarry Smith       *closureSize = 3;
1826693a731SMatthew G. Knepley       if (!(*closure)) {ierr = DMGetWorkArray(dm, *closureSize, PETSC_INT, closure);CHKERRQ(ierr);}
1836693a731SMatthew G. Knepley       (*closure)[0] = p; (*closure)[1] = f; (*closure)[2]= f+1;
1846693a731SMatthew G. Knepley     } else if (dim == 3) {
1856693a731SMatthew G. Knepley       /* 4 vertices */
1866693a731SMatthew G. Knepley       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Not implemented");
1876693a731SMatthew G. Knepley     }
1886693a731SMatthew G. Knepley   } else {
1896693a731SMatthew G. Knepley     /* Z Face */
1906693a731SMatthew G. Knepley     if (dim == 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "There are no faces in 1D");
1916693a731SMatthew G. Knepley     else if (dim == 2) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "There are no z-faces in 2D");
1926693a731SMatthew G. Knepley     else if (dim == 3) {
1936693a731SMatthew G. Knepley       /* 4 vertices */
1946693a731SMatthew G. Knepley       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Not implemented");
1956693a731SMatthew G. Knepley     }
1966693a731SMatthew G. Knepley   }
1976693a731SMatthew G. Knepley   PetscFunctionReturn(0);
1986693a731SMatthew G. Knepley }
1996693a731SMatthew G. Knepley 
2006693a731SMatthew G. Knepley PetscErrorCode DMDARestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useClosure, PetscInt *closureSize, PetscInt **closure)
2016693a731SMatthew G. Knepley {
2026693a731SMatthew G. Knepley   PetscErrorCode ierr;
2036693a731SMatthew G. Knepley 
2046693a731SMatthew G. Knepley   PetscFunctionBegin;
2056693a731SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, closure);CHKERRQ(ierr);
2066693a731SMatthew G. Knepley   PetscFunctionReturn(0);
2076693a731SMatthew G. Knepley }
2086693a731SMatthew G. Knepley 
209aa1993deSMatthew G Knepley PetscErrorCode DMDAGetClosure(DM dm, PetscSection section, PetscInt p,PetscInt *n,const PetscInt **closure)
21057459e9aSMatthew G Knepley {
211c73cfb54SMatthew G. Knepley   PetscInt       dim = dm->dim;
212aa1993deSMatthew G Knepley   PetscInt       nVx, nVy, nxF, nXF, nyF, nYF, nzF, nZF;
213aa1993deSMatthew G Knepley   PetscInt       pStart, pEnd, cStart, cEnd, vStart, vEnd, fStart, fEnd, xfStart, xfEnd, yfStart;
214aa1993deSMatthew G Knepley   PetscErrorCode ierr;
215aa1993deSMatthew G Knepley 
216aa1993deSMatthew G Knepley   PetscFunctionBegin;
217aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
218aa1993deSMatthew G Knepley   if (n) PetscValidIntPointer(n,4);
219aa1993deSMatthew G Knepley   if (closure) PetscValidPointer(closure, 5);
220aa1993deSMatthew G Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
22182f516ccSBarry Smith   if (!section) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "This DM has not default PetscSection");
222aa1993deSMatthew G Knepley   ierr    = DMDAGetHeightStratum(dm, -1,  &pStart, &pEnd);CHKERRQ(ierr);
223aa1993deSMatthew G Knepley   ierr    = DMDAGetHeightStratum(dm, 0,   &cStart, &cEnd);CHKERRQ(ierr);
224aa1993deSMatthew G Knepley   ierr    = DMDAGetHeightStratum(dm, 1,   &fStart, &fEnd);CHKERRQ(ierr);
225aa1993deSMatthew G Knepley   ierr    = DMDAGetHeightStratum(dm, dim, &vStart, &vEnd);CHKERRQ(ierr);
2260298fd71SBarry Smith   ierr    = DMDAGetNumVertices(dm, &nVx, &nVy, NULL, NULL);CHKERRQ(ierr);
227aa1993deSMatthew G Knepley   ierr    = DMDAGetNumFaces(dm, &nxF, &nXF, &nyF, &nYF, &nzF, &nZF);CHKERRQ(ierr);
228aa1993deSMatthew G Knepley   xfStart = fStart; xfEnd = xfStart+nXF;
229aa1993deSMatthew G Knepley   yfStart = xfEnd;
23082f516ccSBarry Smith   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid point %d should be in [%d, %d)", p, pStart, pEnd);
231aa1993deSMatthew G Knepley   if ((p >= cStart) || (p < cEnd)) {
232aa1993deSMatthew G Knepley     /* Cell */
23382f516ccSBarry Smith     if (dim == 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Not implemented");
234201c01f8SBarry Smith     else if (dim == 2) {
235aa1993deSMatthew G Knepley       /* 4 faces, 4 vertices
236aa1993deSMatthew G Knepley          Bottom-left vertex follows same order as cells
237aa1993deSMatthew G Knepley          Bottom y-face same order as cells
238aa1993deSMatthew G Knepley          Left x-face follows same order as cells
239aa1993deSMatthew G Knepley          We number the quad:
240aa1993deSMatthew G Knepley 
241aa1993deSMatthew G Knepley            8--3--7
242aa1993deSMatthew G Knepley            |     |
243aa1993deSMatthew G Knepley            4  0  2
244aa1993deSMatthew G Knepley            |     |
245aa1993deSMatthew G Knepley            5--1--6
246aa1993deSMatthew G Knepley       */
247aa1993deSMatthew G Knepley       PetscInt c  = p - cStart, cx = c % (nVx-1), cy = c / (nVx-1);
248aa1993deSMatthew G Knepley       PetscInt v  = cy*nVx + cx +  vStart;
249aa1993deSMatthew G Knepley       PetscInt xf = cy*nxF + cx + xfStart;
250aa1993deSMatthew G Knepley       PetscInt yf = c + yfStart;
251da80777bSKarl Rupp       PetscInt points[9];
252da80777bSKarl Rupp 
253da80777bSKarl Rupp       /* Note: initializer list is not computable at compile time, hence we fill the array manually */
254da80777bSKarl Rupp       points[0] = p; points[1] = yf; points[2] = xf+1; points[3] = yf+nyF; points[4] = xf+0; points[5] = v+0; points[6]= v+1; points[7] = v+nVx+1; points[8] = v+nVx+0;
255aa1993deSMatthew G Knepley 
256aa1993deSMatthew G Knepley       ierr = GetPointArray_Private(dm,9,points,n,closure);CHKERRQ(ierr);
25782f516ccSBarry Smith     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Not implemented");
258aa1993deSMatthew G Knepley   } else if ((p >= vStart) || (p < vEnd)) {
259aa1993deSMatthew G Knepley     /* Vertex */
260aa1993deSMatthew G Knepley     ierr = GetPointArray_Private(dm,1,&p,n,closure);CHKERRQ(ierr);
261aa1993deSMatthew G Knepley   } else if ((p >= fStart) || (p < fStart + nXF)) {
262aa1993deSMatthew G Knepley     /* X Face */
26382f516ccSBarry Smith     if (dim == 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "There are no faces in 1D");
264201c01f8SBarry Smith     else if (dim == 2) {
265aa1993deSMatthew G Knepley       /* 2 vertices: The bottom vertex has the same numbering as the face */
266aa1993deSMatthew G Knepley       PetscInt f = p - xfStart;
267da80777bSKarl Rupp       PetscInt points[3];
268aa1993deSMatthew G Knepley 
269da80777bSKarl Rupp       points[0] = p; points[1] = f; points[2] = f+nVx;
27082f516ccSBarry Smith       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Broken");
271aa1993deSMatthew G Knepley       ierr = GetPointArray_Private(dm,3,points,n,closure);CHKERRQ(ierr);
272aa1993deSMatthew G Knepley     } else if (dim == 3) {
273aa1993deSMatthew G Knepley       /* 4 vertices */
27482f516ccSBarry Smith       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Not implemented");
275aa1993deSMatthew G Knepley     }
276aa1993deSMatthew G Knepley   } else if ((p >= fStart + nXF) || (p < fStart + nXF + nYF)) {
277aa1993deSMatthew G Knepley     /* Y Face */
27882f516ccSBarry Smith     if (dim == 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "There are no faces in 1D");
279201c01f8SBarry Smith     else if (dim == 2) {
280aa1993deSMatthew G Knepley       /* 2 vertices: The left vertex has the same numbering as the face */
281aa1993deSMatthew G Knepley       PetscInt f = p - yfStart;
282da80777bSKarl Rupp       PetscInt points[3];
283aa1993deSMatthew G Knepley 
284da80777bSKarl Rupp       points[0] = p; points[1] = f; points[2]= f+1;
28582f516ccSBarry Smith       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Broken");
286aa1993deSMatthew G Knepley       ierr = GetPointArray_Private(dm, 3, points, n, closure);CHKERRQ(ierr);
287aa1993deSMatthew G Knepley     } else if (dim == 3) {
288aa1993deSMatthew G Knepley       /* 4 vertices */
28982f516ccSBarry Smith       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Not implemented");
290aa1993deSMatthew G Knepley     }
291aa1993deSMatthew G Knepley   } else {
292aa1993deSMatthew G Knepley     /* Z Face */
29382f516ccSBarry Smith     if (dim == 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "There are no faces in 1D");
29482f516ccSBarry Smith     else if (dim == 2) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "There are no z-faces in 2D");
295201c01f8SBarry Smith     else if (dim == 3) {
296aa1993deSMatthew G Knepley       /* 4 vertices */
29782f516ccSBarry Smith       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Not implemented");
298aa1993deSMatthew G Knepley     }
299aa1993deSMatthew G Knepley   }
300aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
301aa1993deSMatthew G Knepley }
302aa1993deSMatthew G Knepley 
303e5c84f05SJed Brown /* Zeros n and closure. */
304aa1993deSMatthew G Knepley PetscErrorCode DMDARestoreClosure(DM dm, PetscSection section, PetscInt p,PetscInt *n,const PetscInt **closure)
305aa1993deSMatthew G Knepley {
306aa1993deSMatthew G Knepley   PetscErrorCode ierr;
307aa1993deSMatthew G Knepley 
308aa1993deSMatthew G Knepley   PetscFunctionBegin;
309aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
310e5c84f05SJed Brown   if (n) PetscValidIntPointer(n,4);
311e5c84f05SJed Brown   if (closure) PetscValidPointer(closure, 5);
312aa1993deSMatthew G Knepley   ierr = RestorePointArray_Private(dm,n,closure);CHKERRQ(ierr);
313aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
314aa1993deSMatthew G Knepley }
315aa1993deSMatthew G Knepley 
316d7a12f93SMatthew G. Knepley PetscErrorCode DMDAGetClosureScalar(DM dm, PetscSection section, PetscInt p, PetscScalar *vArray, PetscInt *csize, PetscScalar **values)
317aa1993deSMatthew G Knepley {
318c73cfb54SMatthew G. Knepley   PetscInt       dim = dm->dim;
31957459e9aSMatthew G Knepley   PetscInt       nVx, nVy, nxF, nXF, nyF, nYF, nzF, nZF;
32095babc02SJed Brown   PetscInt       pStart, pEnd, cStart, cEnd, vStart, vEnd, fStart, fEnd, xfStart, xfEnd, yfStart, yfEnd, zfStart;
32157459e9aSMatthew G Knepley   PetscErrorCode ierr;
32257459e9aSMatthew G Knepley 
32357459e9aSMatthew G Knepley   PetscFunctionBegin;
32457459e9aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
325aa1993deSMatthew G Knepley   PetscValidScalarPointer(vArray, 4);
3260a3ada39SMatthew G. Knepley   if (values) PetscValidPointer(values, 6);
32757459e9aSMatthew G Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
32882f516ccSBarry Smith   if (!section) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "This DM has not default PetscSection");
32957459e9aSMatthew G Knepley   ierr    = DMDAGetHeightStratum(dm, -1,  &pStart, &pEnd);CHKERRQ(ierr);
33057459e9aSMatthew G Knepley   ierr    = DMDAGetHeightStratum(dm, 0,   &cStart, &cEnd);CHKERRQ(ierr);
33157459e9aSMatthew G Knepley   ierr    = DMDAGetHeightStratum(dm, 1,   &fStart, &fEnd);CHKERRQ(ierr);
33257459e9aSMatthew G Knepley   ierr    = DMDAGetHeightStratum(dm, dim, &vStart, &vEnd);CHKERRQ(ierr);
3330298fd71SBarry Smith   ierr    = DMDAGetNumVertices(dm, &nVx, &nVy, NULL, NULL);CHKERRQ(ierr);
33457459e9aSMatthew G Knepley   ierr    = DMDAGetNumFaces(dm, &nxF, &nXF, &nyF, &nYF, &nzF, &nZF);CHKERRQ(ierr);
33557459e9aSMatthew G Knepley   xfStart = fStart; xfEnd = xfStart+nXF;
33657459e9aSMatthew G Knepley   yfStart = xfEnd;  yfEnd = yfStart+nYF;
33795babc02SJed Brown   zfStart = yfEnd;
33882f516ccSBarry Smith   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid point %d should be in [%d, %d)", p, pStart, pEnd);
33957459e9aSMatthew G Knepley   if ((p >= cStart) || (p < cEnd)) {
34057459e9aSMatthew G Knepley     /* Cell */
34182f516ccSBarry Smith     if (dim == 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Not implemented");
342201c01f8SBarry Smith     else if (dim == 2) {
34357459e9aSMatthew G Knepley       /* 4 faces, 4 vertices
34457459e9aSMatthew G Knepley          Bottom-left vertex follows same order as cells
34557459e9aSMatthew G Knepley          Bottom y-face same order as cells
34657459e9aSMatthew G Knepley          Left x-face follows same order as cells
34757459e9aSMatthew G Knepley          We number the quad:
34857459e9aSMatthew G Knepley 
34957459e9aSMatthew G Knepley            8--3--7
35057459e9aSMatthew G Knepley            |     |
35157459e9aSMatthew G Knepley            4  0  2
35257459e9aSMatthew G Knepley            |     |
35357459e9aSMatthew G Knepley            5--1--6
35457459e9aSMatthew G Knepley       */
35557459e9aSMatthew G Knepley       PetscInt c  = p - cStart, cx = c % (nVx-1), cy = c / (nVx-1);
35657459e9aSMatthew G Knepley       PetscInt v  = cy*nVx + cx +  vStart;
357312b75c6SMatthew G. Knepley       PetscInt xf = cx*nxF + cy + xfStart;
35857459e9aSMatthew G Knepley       PetscInt yf = c + yfStart;
359da80777bSKarl Rupp       PetscInt points[9];
36057459e9aSMatthew G Knepley 
361312b75c6SMatthew G. Knepley       points[0] = p;
362312b75c6SMatthew G. Knepley       points[1] = yf;  points[2] = xf+nxF; points[3] = yf+nyF;  points[4] = xf;
363312b75c6SMatthew G. Knepley       points[5] = v+0; points[6] = v+1;    points[7] = v+nVx+1; points[8] = v+nVx+0;
3640a3ada39SMatthew G. Knepley       ierr = FillClosureArray_Static(dm, section, 9, points, vArray, csize, values);CHKERRQ(ierr);
36557459e9aSMatthew G Knepley     } else {
36657459e9aSMatthew G Knepley       /* 6 faces, 8 vertices
36757459e9aSMatthew G Knepley          Bottom-left-back vertex follows same order as cells
36857459e9aSMatthew G Knepley          Back z-face follows same order as cells
36957459e9aSMatthew G Knepley          Bottom y-face follows same order as cells
37057459e9aSMatthew G Knepley          Left x-face follows same order as cells
37157459e9aSMatthew G Knepley 
37257459e9aSMatthew G Knepley               14-----13
37357459e9aSMatthew G Knepley               /|    /|
37457459e9aSMatthew G Knepley              / | 2 / |
37557459e9aSMatthew G Knepley             / 5|  /  |
37657459e9aSMatthew G Knepley           10-----9  4|
37757459e9aSMatthew G Knepley            |  11-|---12
37857459e9aSMatthew G Knepley            |6 /  |  /
37957459e9aSMatthew G Knepley            | /1 3| /
38057459e9aSMatthew G Knepley            |/    |/
38157459e9aSMatthew G Knepley            7-----8
38257459e9aSMatthew G Knepley       */
38357459e9aSMatthew G Knepley       PetscInt c = p - cStart;
384da80777bSKarl Rupp       PetscInt points[15];
385da80777bSKarl Rupp 
386da80777bSKarl Rupp       points[0]  = p; points[1] = c+zfStart; points[2] = c+zfStart+nzF; points[3] = c+yfStart; points[4] = c+xfStart+nxF; points[5] = c+yfStart+nyF; points[6] = c+xfStart;
387da80777bSKarl Rupp       points[7]  = c+vStart+0; points[8] = c+vStart+1; points[9] = c+vStart+nVx+1; points[10] = c+vStart+nVx+0; points[11] = c+vStart+nVx*nVy+0;
388da80777bSKarl Rupp       points[12] = c+vStart+nVx*nVy+1; points[13] = c+vStart+nVx*nVy+nVx+1; points[14] = c+vStart+nVx*nVy+nVx+0;
38957459e9aSMatthew G Knepley 
39082f516ccSBarry Smith       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Broken");
3910a3ada39SMatthew G. Knepley       ierr = FillClosureArray_Static(dm, section, 15, points, vArray, csize, values);CHKERRQ(ierr);
39257459e9aSMatthew G Knepley     }
39357459e9aSMatthew G Knepley   } else if ((p >= vStart) || (p < vEnd)) {
39457459e9aSMatthew G Knepley     /* Vertex */
3950a3ada39SMatthew G. Knepley     ierr = FillClosureArray_Static(dm, section, 1, &p, vArray, csize, values);CHKERRQ(ierr);
39657459e9aSMatthew G Knepley   } else if ((p >= fStart) || (p < fStart + nXF)) {
39757459e9aSMatthew G Knepley     /* X Face */
39882f516ccSBarry Smith     if (dim == 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "There are no faces in 1D");
399201c01f8SBarry Smith     else if (dim == 2) {
40057459e9aSMatthew G Knepley       /* 2 vertices: The bottom vertex has the same numbering as the face */
40157459e9aSMatthew G Knepley       PetscInt f = p - xfStart;
402da80777bSKarl Rupp       PetscInt points[3];
40357459e9aSMatthew G Knepley 
404da80777bSKarl Rupp       points[0] = p; points[1] = f; points[2] = f+nVx;
40582f516ccSBarry Smith       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Broken");
4060a3ada39SMatthew G. Knepley       ierr = FillClosureArray_Static(dm, section, 3, points, vArray, csize, values);CHKERRQ(ierr);
40782f516ccSBarry Smith     } else if (dim == 3) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Not implemented");
40857459e9aSMatthew G Knepley   } else if ((p >= fStart + nXF) || (p < fStart + nXF + nYF)) {
40957459e9aSMatthew G Knepley     /* Y Face */
41082f516ccSBarry Smith     if (dim == 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "There are no faces in 1D");
411201c01f8SBarry Smith     else if (dim == 2) {
41257459e9aSMatthew G Knepley       /* 2 vertices: The left vertex has the same numbering as the face */
41357459e9aSMatthew G Knepley       PetscInt f = p - yfStart;
414da80777bSKarl Rupp       PetscInt points[3];
41557459e9aSMatthew G Knepley 
416da80777bSKarl Rupp       points[0] = p; points[1] = f; points[2] = f+1;
41782f516ccSBarry Smith       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Broken");
4180a3ada39SMatthew G. Knepley       ierr = FillClosureArray_Static(dm, section, 3, points, vArray, csize, values);CHKERRQ(ierr);
41982f516ccSBarry Smith     } else if (dim == 3) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Not implemented");
42057459e9aSMatthew G Knepley   } else {
42157459e9aSMatthew G Knepley     /* Z Face */
42282f516ccSBarry Smith     if (dim == 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "There are no faces in 1D");
42382f516ccSBarry Smith     else if (dim == 2) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "There are no z-faces in 2D");
42482f516ccSBarry Smith     else if (dim == 3) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Not implemented");
42557459e9aSMatthew G Knepley   }
426aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
427aa1993deSMatthew G Knepley }
428aa1993deSMatthew G Knepley 
429d7a12f93SMatthew G. Knepley PetscErrorCode DMDAVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt p, PetscInt *csize, PetscScalar **values)
430aa1993deSMatthew G Knepley {
431aa1993deSMatthew G Knepley   PetscScalar    *vArray;
432aa1993deSMatthew G Knepley   PetscErrorCode ierr;
433aa1993deSMatthew G Knepley 
434aa1993deSMatthew G Knepley   PetscFunctionBegin;
435aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
436aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4370a3ada39SMatthew G. Knepley   if (values) PetscValidPointer(values, 6);
438aa1993deSMatthew G Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
4390a3ada39SMatthew G. Knepley   ierr = DMDAGetClosureScalar(dm, section, p, vArray, csize, values);CHKERRQ(ierr);
44057459e9aSMatthew G Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
44157459e9aSMatthew G Knepley   PetscFunctionReturn(0);
44257459e9aSMatthew G Knepley }
44357459e9aSMatthew G Knepley 
444d7a12f93SMatthew G. Knepley PetscErrorCode DMDARestoreClosureScalar(DM dm, PetscSection section, PetscInt p, PetscScalar *vArray, PetscInt *csize, PetscScalar **values)
445aa1993deSMatthew G Knepley {
446aa1993deSMatthew G Knepley   PetscErrorCode ierr;
447aa1993deSMatthew G Knepley 
448aa1993deSMatthew G Knepley   PetscFunctionBegin;
449aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4500a3ada39SMatthew G. Knepley   PetscValidPointer(values, 6);
4510a3ada39SMatthew G. Knepley   ierr  = DMRestoreWorkArray(dm, csize ? *csize : 0, PETSC_SCALAR, (void*) values);CHKERRQ(ierr);
452aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
453aa1993deSMatthew G Knepley }
454aa1993deSMatthew G Knepley 
455d7a12f93SMatthew G. Knepley PetscErrorCode DMDAVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt p, PetscInt *csize, PetscScalar **values)
456aa1993deSMatthew G Knepley {
457aa1993deSMatthew G Knepley   PetscErrorCode ierr;
458aa1993deSMatthew G Knepley 
459aa1993deSMatthew G Knepley   PetscFunctionBegin;
460aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
461aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
462aa1993deSMatthew G Knepley   PetscValidPointer(values, 5);
4630a3ada39SMatthew G. Knepley   ierr = DMDARestoreClosureScalar(dm, section, p, NULL, csize, values);CHKERRQ(ierr);
464aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
465aa1993deSMatthew G Knepley }
466aa1993deSMatthew G Knepley 
467aa1993deSMatthew G Knepley PetscErrorCode DMDASetClosureScalar(DM dm, PetscSection section, PetscInt p,PetscScalar *vArray, const PetscScalar *values, InsertMode mode)
46857459e9aSMatthew G Knepley {
469c73cfb54SMatthew G. Knepley   PetscInt       dim = dm->dim;
4706a7fb054SMatthew G. Knepley   PetscInt       nVx, nVy, nxF, nXF, nyF, nYF, nzF, nZF, nCx, nCy;
47195babc02SJed Brown   PetscInt       pStart, pEnd, cStart, cEnd, vStart, vEnd, fStart, fEnd, xfStart, xfEnd, yfStart, yfEnd, zfStart;
47257459e9aSMatthew G Knepley   PetscErrorCode ierr;
47357459e9aSMatthew G Knepley 
47457459e9aSMatthew G Knepley   PetscFunctionBegin;
47557459e9aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
476aa1993deSMatthew G Knepley   PetscValidScalarPointer(values, 4);
47757459e9aSMatthew G Knepley   PetscValidPointer(values, 5);
47857459e9aSMatthew G Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
47982f516ccSBarry Smith   if (!section) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "This DM has not default PetscSection");
48057459e9aSMatthew G Knepley   ierr    = DMDAGetHeightStratum(dm, -1,  &pStart, &pEnd);CHKERRQ(ierr);
48157459e9aSMatthew G Knepley   ierr    = DMDAGetHeightStratum(dm, 0,   &cStart, &cEnd);CHKERRQ(ierr);
48257459e9aSMatthew G Knepley   ierr    = DMDAGetHeightStratum(dm, 1,   &fStart, &fEnd);CHKERRQ(ierr);
48357459e9aSMatthew G Knepley   ierr    = DMDAGetHeightStratum(dm, dim, &vStart, &vEnd);CHKERRQ(ierr);
4846a7fb054SMatthew G. Knepley   ierr    = DMDAGetNumCells(dm, &nCx, &nCy, NULL, NULL);CHKERRQ(ierr);
4850298fd71SBarry Smith   ierr    = DMDAGetNumVertices(dm, &nVx, &nVy, NULL, NULL);CHKERRQ(ierr);
48657459e9aSMatthew G Knepley   ierr    = DMDAGetNumFaces(dm, &nxF, &nXF, &nyF, &nYF, &nzF, &nZF);CHKERRQ(ierr);
48757459e9aSMatthew G Knepley   xfStart = fStart; xfEnd = xfStart+nXF;
48857459e9aSMatthew G Knepley   yfStart = xfEnd;  yfEnd = yfStart+nYF;
48995babc02SJed Brown   zfStart = yfEnd;
49082f516ccSBarry Smith   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid point %d should be in [%d, %d)", p, pStart, pEnd);
49157459e9aSMatthew G Knepley   if ((p >= cStart) || (p < cEnd)) {
49257459e9aSMatthew G Knepley     /* Cell */
49382f516ccSBarry Smith     if (dim == 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Not implemented");
494201c01f8SBarry Smith     else if (dim == 2) {
49557459e9aSMatthew G Knepley       /* 4 faces, 4 vertices
49657459e9aSMatthew G Knepley          Bottom-left vertex follows same order as cells
49757459e9aSMatthew G Knepley          Bottom y-face same order as cells
49857459e9aSMatthew G Knepley          Left x-face follows same order as cells
49957459e9aSMatthew G Knepley          We number the quad:
50057459e9aSMatthew G Knepley 
50157459e9aSMatthew G Knepley            8--3--7
50257459e9aSMatthew G Knepley            |     |
50357459e9aSMatthew G Knepley            4  0  2
50457459e9aSMatthew G Knepley            |     |
50557459e9aSMatthew G Knepley            5--1--6
50657459e9aSMatthew G Knepley       */
507312b75c6SMatthew G. Knepley       PetscInt c = p - cStart, cx = c % (nVx-1), cy = c / (nVx-1);
508312b75c6SMatthew G. Knepley       PetscInt v  = cy*nVx + cx +  vStart;
509312b75c6SMatthew G. Knepley       PetscInt xf = cx*nxF + cy + xfStart;
510312b75c6SMatthew G. Knepley       PetscInt yf = c + yfStart;
511da80777bSKarl Rupp       PetscInt points[9];
51257459e9aSMatthew G Knepley 
5136a7fb054SMatthew G. Knepley       points[0] = p;
514312b75c6SMatthew G. Knepley       points[1] = yf;  points[2] = xf+nxF; points[3] = yf+nyF;  points[4] = xf;
515312b75c6SMatthew G. Knepley       points[5] = v+0; points[6] = v+1;    points[7] = v+nVx+1; points[8] = v+nVx+0;
51657459e9aSMatthew G Knepley       ierr      = FillClosureVec_Private(dm, section, 9, points, vArray, values, mode);CHKERRQ(ierr);
51757459e9aSMatthew G Knepley     } else {
51857459e9aSMatthew G Knepley       /* 6 faces, 8 vertices
51957459e9aSMatthew G Knepley          Bottom-left-back vertex follows same order as cells
52057459e9aSMatthew G Knepley          Back z-face follows same order as cells
52157459e9aSMatthew G Knepley          Bottom y-face follows same order as cells
52257459e9aSMatthew G Knepley          Left x-face follows same order as cells
52357459e9aSMatthew G Knepley 
52457459e9aSMatthew G Knepley               14-----13
52557459e9aSMatthew G Knepley               /|    /|
52657459e9aSMatthew G Knepley              / | 2 / |
52757459e9aSMatthew G Knepley             / 5|  /  |
52857459e9aSMatthew G Knepley           10-----9  4|
52957459e9aSMatthew G Knepley            |  11-|---12
53057459e9aSMatthew G Knepley            |6 /  |  /
53157459e9aSMatthew G Knepley            | /1 3| /
53257459e9aSMatthew G Knepley            |/    |/
53357459e9aSMatthew G Knepley            7-----8
53457459e9aSMatthew G Knepley       */
53557459e9aSMatthew G Knepley       PetscInt c = p - cStart;
536da80777bSKarl Rupp       PetscInt points[15];
53757459e9aSMatthew G Knepley 
538da80777bSKarl Rupp       points[0]  = p; points[1] = c+zfStart; points[2] = c+zfStart+nzF; points[3] = c+yfStart; points[4] = c+xfStart+nxF; points[5] = c+yfStart+nyF; points[6] = c+xfStart;
539da80777bSKarl Rupp       points[7]  = c+vStart+0; points[8] = c+vStart+1; points[9] = c+vStart+nVx+1; points[10] = c+vStart+nVx+0; points[11] = c+vStart+nVx*nVy+0; points[12] = c+vStart+nVx*nVy+1;
540da80777bSKarl Rupp       points[13] = c+vStart+nVx*nVy+nVx+1; points[14] = c+vStart+nVx*nVy+nVx+0;
54157459e9aSMatthew G Knepley       ierr       = FillClosureVec_Private(dm, section, 15, points, vArray, values, mode);CHKERRQ(ierr);
54257459e9aSMatthew G Knepley     }
54357459e9aSMatthew G Knepley   } else if ((p >= vStart) || (p < vEnd)) {
54457459e9aSMatthew G Knepley     /* Vertex */
54557459e9aSMatthew G Knepley     ierr = FillClosureVec_Private(dm, section, 1, &p, vArray, values, mode);CHKERRQ(ierr);
54657459e9aSMatthew G Knepley   } else if ((p >= fStart) || (p < fStart + nXF)) {
54757459e9aSMatthew G Knepley     /* X Face */
54882f516ccSBarry Smith     if (dim == 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "There are no faces in 1D");
549201c01f8SBarry Smith     else if (dim == 2) {
55057459e9aSMatthew G Knepley       /* 2 vertices: The bottom vertex has the same numbering as the face */
55157459e9aSMatthew G Knepley       PetscInt f = p - xfStart;
552da80777bSKarl Rupp       PetscInt points[3];
55357459e9aSMatthew G Knepley 
554da80777bSKarl Rupp       points[0] = p; points[1] = f; points[2] = f+nVx;
55557459e9aSMatthew G Knepley       ierr      = FillClosureVec_Private(dm, section, 3, points, vArray, values, mode);CHKERRQ(ierr);
55682f516ccSBarry Smith     } else if (dim == 3) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Not implemented");
55757459e9aSMatthew G Knepley   } else if ((p >= fStart + nXF) || (p < fStart + nXF + nYF)) {
55857459e9aSMatthew G Knepley     /* Y Face */
55982f516ccSBarry Smith     if (dim == 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "There are no faces in 1D");
560201c01f8SBarry Smith     else if (dim == 2) {
56157459e9aSMatthew G Knepley       /* 2 vertices: The left vertex has the same numbering as the face */
56257459e9aSMatthew G Knepley       PetscInt f = p - yfStart;
563da80777bSKarl Rupp       PetscInt points[3];
56457459e9aSMatthew G Knepley 
565da80777bSKarl Rupp       points[0] = p; points[1] = f; points[2] = f+1;
56657459e9aSMatthew G Knepley       ierr      = FillClosureVec_Private(dm, section, 3, points, vArray, values, mode);CHKERRQ(ierr);
56782f516ccSBarry Smith     } else if (dim == 3) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Not implemented");
56857459e9aSMatthew G Knepley   } else {
56957459e9aSMatthew G Knepley     /* Z Face */
57082f516ccSBarry Smith     if (dim == 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "There are no faces in 1D");
57182f516ccSBarry Smith     else if (dim == 2) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "There are no z-faces in 2D");
57282f516ccSBarry Smith     else if (dim == 3) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Not implemented");
57357459e9aSMatthew G Knepley   }
574aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
575aa1993deSMatthew G Knepley }
576aa1993deSMatthew G Knepley 
577aa1993deSMatthew G Knepley PetscErrorCode DMDAVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt p, const PetscScalar *values, InsertMode mode)
578aa1993deSMatthew G Knepley {
579aa1993deSMatthew G Knepley   PetscScalar    *vArray;
580aa1993deSMatthew G Knepley   PetscErrorCode ierr;
581aa1993deSMatthew G Knepley 
582aa1993deSMatthew G Knepley   PetscFunctionBegin;
583aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
584aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
585aa1993deSMatthew G Knepley   PetscValidPointer(values, 5);
586aa1993deSMatthew G Knepley   ierr = VecGetArray(v,&vArray);CHKERRQ(ierr);
587aa1993deSMatthew G Knepley   ierr = DMDASetClosureScalar(dm,section,p,vArray,values,mode);CHKERRQ(ierr);
58857459e9aSMatthew G Knepley   ierr = VecRestoreArray(v,&vArray);CHKERRQ(ierr);
58957459e9aSMatthew G Knepley   PetscFunctionReturn(0);
59057459e9aSMatthew G Knepley }
59157459e9aSMatthew G Knepley 
592ed4e918cSMatthew G Knepley /*@
593f0e3914cSSatish Balay   DMDAConvertToCell - Convert (i,j,k) to local cell number
594341c9bdaSMatthew G Knepley 
595ed4e918cSMatthew G Knepley   Not Collective
596ed4e918cSMatthew G Knepley 
597ed4e918cSMatthew G Knepley   Input Parameter:
598ed4e918cSMatthew G Knepley + da - the distributed array
599907376e6SBarry Smith - s - A MatStencil giving (i,j,k)
600ed4e918cSMatthew G Knepley 
601ed4e918cSMatthew G Knepley   Output Parameter:
602ed4e918cSMatthew G Knepley . cell - the local cell number
603ed4e918cSMatthew G Knepley 
604ed4e918cSMatthew G Knepley   Level: developer
605ed4e918cSMatthew G Knepley 
606ed4e918cSMatthew G Knepley .seealso: DMDAVecGetClosure()
607ed4e918cSMatthew G Knepley @*/
608ed4e918cSMatthew G Knepley PetscErrorCode DMDAConvertToCell(DM dm, MatStencil s, PetscInt *cell)
609341c9bdaSMatthew G Knepley {
610341c9bdaSMatthew G Knepley   DM_DA          *da = (DM_DA*) dm->data;
611c73cfb54SMatthew G. Knepley   const PetscInt dim = dm->dim;
6124e846693SMatthew G Knepley   const PetscInt mx  = (da->Xe - da->Xs)/da->w, my = da->Ye - da->Ys /*, mz = da->Ze - da->Zs*/;
613ed4e918cSMatthew G Knepley   const PetscInt il  = s.i - da->Xs/da->w, jl = dim > 1 ? s.j - da->Ys : 0, kl = dim > 2 ? s.k - da->Zs : 0;
614341c9bdaSMatthew G Knepley 
615341c9bdaSMatthew G Knepley   PetscFunctionBegin;
616ed4e918cSMatthew G Knepley   *cell = -1;
61782f516ccSBarry Smith   if ((s.i < da->Xs/da->w) || (s.i >= da->Xe/da->w))    SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Stencil i %d should be in [%d, %d)", s.i, da->Xs/da->w, da->Xe/da->w);
61882f516ccSBarry Smith   if ((dim > 1) && ((s.j < da->Ys) || (s.j >= da->Ye))) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Stencil j %d should be in [%d, %d)", s.j, da->Ys, da->Ye);
61982f516ccSBarry Smith   if ((dim > 2) && ((s.k < da->Zs) || (s.k >= da->Ze))) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Stencil k %d should be in [%d, %d)", s.k, da->Zs, da->Ze);
620ed4e918cSMatthew G Knepley   *cell = (kl*my + jl)*mx + il;
621ed4e918cSMatthew G Knepley   PetscFunctionReturn(0);
622341c9bdaSMatthew G Knepley }
623341c9bdaSMatthew G Knepley 
62415d2e57cSJed Brown PetscErrorCode DMDAComputeCellGeometry_2D(DM dm, const PetscScalar vertices[], const PetscReal refPoint[], PetscReal J[], PetscReal invJ[], PetscReal *detJ)
62557459e9aSMatthew G Knepley {
62657459e9aSMatthew G Knepley   const PetscScalar x0   = vertices[0];
62757459e9aSMatthew G Knepley   const PetscScalar y0   = vertices[1];
62857459e9aSMatthew G Knepley   const PetscScalar x1   = vertices[2];
62957459e9aSMatthew G Knepley   const PetscScalar y1   = vertices[3];
63057459e9aSMatthew G Knepley   const PetscScalar x2   = vertices[4];
63157459e9aSMatthew G Knepley   const PetscScalar y2   = vertices[5];
63257459e9aSMatthew G Knepley   const PetscScalar x3   = vertices[6];
63357459e9aSMatthew G Knepley   const PetscScalar y3   = vertices[7];
63457459e9aSMatthew G Knepley   const PetscScalar f_01 = x2 - x1 - x3 + x0;
63557459e9aSMatthew G Knepley   const PetscScalar g_01 = y2 - y1 - y3 + y0;
63657459e9aSMatthew G Knepley   const PetscScalar x    = refPoint[0];
63757459e9aSMatthew G Knepley   const PetscScalar y    = refPoint[1];
63857459e9aSMatthew G Knepley   PetscReal         invDet;
63957459e9aSMatthew G Knepley   PetscErrorCode    ierr;
64057459e9aSMatthew G Knepley 
64157459e9aSMatthew G Knepley   PetscFunctionBegin;
642e477d84eSMatthew G. Knepley #if 0
64315d2e57cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell (%g,%g)--(%g,%g)--(%g,%g)--(%g,%g)\n",
6444c06c320SMatthew G Knepley                      PetscRealPart(x0),PetscRealPart(y0),PetscRealPart(x1),PetscRealPart(y1),PetscRealPart(x2),PetscRealPart(y2),PetscRealPart(x3),PetscRealPart(y3));CHKERRQ(ierr);
64515d2e57cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Ref Point (%g,%g)\n", PetscRealPart(x), PetscRealPart(y));CHKERRQ(ierr);
64615d2e57cSJed Brown #endif
64715d2e57cSJed Brown   J[0]    = PetscRealPart(x1 - x0 + f_01*y) * 0.5; J[1] = PetscRealPart(x3 - x0 + f_01*x) * 0.5;
64815d2e57cSJed Brown   J[2]    = PetscRealPart(y1 - y0 + g_01*y) * 0.5; J[3] = PetscRealPart(y3 - y0 + g_01*x) * 0.5;
64957459e9aSMatthew G Knepley   *detJ   = J[0]*J[3] - J[1]*J[2];
65057459e9aSMatthew G Knepley   invDet  = 1.0/(*detJ);
651e477d84eSMatthew G. Knepley   if (invJ) {
65257459e9aSMatthew G Knepley     invJ[0] =  invDet*J[3]; invJ[1] = -invDet*J[1];
65357459e9aSMatthew G Knepley     invJ[2] = -invDet*J[2]; invJ[3] =  invDet*J[0];
654e477d84eSMatthew G. Knepley   }
65557459e9aSMatthew G Knepley   ierr    = PetscLogFlops(30);CHKERRQ(ierr);
65657459e9aSMatthew G Knepley   PetscFunctionReturn(0);
65757459e9aSMatthew G Knepley }
65857459e9aSMatthew G Knepley 
6598e0841e0SMatthew G. Knepley PetscErrorCode DMDAComputeCellGeometryFEM(DM dm, PetscInt cell, PetscQuadrature quad, PetscReal v0[], PetscReal J[], PetscReal invJ[], PetscReal detJ[])
66057459e9aSMatthew G Knepley {
66157459e9aSMatthew G Knepley   DM               cdm;
66257459e9aSMatthew G Knepley   Vec              coordinates;
66321454ff5SMatthew G. Knepley   const PetscReal *quadPoints;
664d7a12f93SMatthew G. Knepley   PetscScalar     *vertices = NULL;
665*9c3cf19fSMatthew G. Knepley   PetscInt         Nq, csize, dim, d, q;
66657459e9aSMatthew G Knepley   PetscErrorCode   ierr;
66757459e9aSMatthew G Knepley 
66857459e9aSMatthew G Knepley   PetscFunctionBegin;
66957459e9aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67057459e9aSMatthew G Knepley   ierr = DMDAGetInfo(dm, &dim, 0,0,0,0,0,0,0,0,0,0,0,0);CHKERRQ(ierr);
671e477d84eSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
6726636e97aSMatthew G Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
6730a3ada39SMatthew G. Knepley   ierr = DMDAVecGetClosure(cdm, NULL, coordinates, cell, &csize, &vertices);CHKERRQ(ierr);
6748865f1eaSKarl Rupp   for (d = 0; d < dim; ++d) v0[d] = PetscRealPart(vertices[d]);
67557459e9aSMatthew G Knepley   switch (dim) {
67657459e9aSMatthew G Knepley   case 2:
677*9c3cf19fSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, &quadPoints, NULL);CHKERRQ(ierr);
678*9c3cf19fSMatthew G. Knepley     for (q = 0; q < Nq; ++q) {
67921454ff5SMatthew G. Knepley       ierr = DMDAComputeCellGeometry_2D(dm, vertices, &quadPoints[q*dim], J, invJ, detJ);CHKERRQ(ierr);
68057459e9aSMatthew G Knepley     }
68157459e9aSMatthew G Knepley     break;
68257459e9aSMatthew G Knepley   default:
68382f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Dimension %d not supported", dim);
68457459e9aSMatthew G Knepley   }
6850a3ada39SMatthew G. Knepley   ierr = DMDAVecRestoreClosure(cdm, NULL, coordinates, cell, &csize, &vertices);CHKERRQ(ierr);
68657459e9aSMatthew G Knepley   PetscFunctionReturn(0);
68757459e9aSMatthew G Knepley }
688