Actual source code: dagetelem.c


  2: #include <petsc/private/dmdaimpl.h>

  4: static PetscErrorCode DMDAGetElements_1D(DM dm, PetscInt *nel, PetscInt *nen, const PetscInt *e[])
  5: {
  6:   DM_DA   *da = (DM_DA *)dm->data;
  7:   PetscInt i, xs, xe, Xs, Xe;
  8:   PetscInt cnt = 0;

 10:   if (!da->e) {
 11:     PetscInt corners[2];

 14:     DMDAGetCorners(dm, &xs, NULL, NULL, &xe, NULL, NULL);
 15:     DMDAGetGhostCorners(dm, &Xs, NULL, NULL, &Xe, NULL, NULL);
 16:     xe += xs;
 17:     Xe += Xs;
 18:     if (xs != Xs) xs -= 1;
 19:     da->ne = 1 * (xe - xs - 1);
 20:     PetscMalloc1(1 + 2 * da->ne, &da->e);
 21:     for (i = xs; i < xe - 1; i++) {
 22:       da->e[cnt++] = (i - Xs);
 23:       da->e[cnt++] = (i - Xs + 1);
 24:     }
 25:     da->nen = 2;

 27:     corners[0] = (xs - Xs);
 28:     corners[1] = (xe - 1 - Xs);
 29:     ISCreateGeneral(PETSC_COMM_SELF, 2, corners, PETSC_COPY_VALUES, &da->ecorners);
 30:   }
 31:   *nel = da->ne;
 32:   *nen = da->nen;
 33:   *e   = da->e;
 34:   return 0;
 35: }

 37: static PetscErrorCode DMDAGetElements_2D(DM dm, PetscInt *nel, PetscInt *nen, const PetscInt *e[])
 38: {
 39:   DM_DA   *da = (DM_DA *)dm->data;
 40:   PetscInt i, xs, xe, Xs, Xe;
 41:   PetscInt j, ys, ye, Ys, Ye;
 42:   PetscInt cnt = 0, cell[4], ns = 2;
 43:   PetscInt c, split[] = {0, 1, 3, 2, 3, 1};

 45:   if (!da->e) {
 46:     PetscInt corners[4], nn = 0;


 50:     switch (da->elementtype) {
 51:     case DMDA_ELEMENT_Q1:
 52:       da->nen = 4;
 53:       break;
 54:     case DMDA_ELEMENT_P1:
 55:       da->nen = 3;
 56:       break;
 57:     default:
 58:       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unknown element type %d", da->elementtype);
 59:     }
 60:     nn = da->nen;

 62:     if (da->elementtype == DMDA_ELEMENT_P1) ns = 2;
 63:     if (da->elementtype == DMDA_ELEMENT_Q1) ns = 1;
 64:     DMDAGetCorners(dm, &xs, &ys, NULL, &xe, &ye, NULL);
 65:     DMDAGetGhostCorners(dm, &Xs, &Ys, NULL, &Xe, &Ye, NULL);
 66:     xe += xs;
 67:     Xe += Xs;
 68:     if (xs != Xs) xs -= 1;
 69:     ye += ys;
 70:     Ye += Ys;
 71:     if (ys != Ys) ys -= 1;
 72:     da->ne = ns * (xe - xs - 1) * (ye - ys - 1);
 73:     PetscMalloc1(1 + nn * da->ne, &da->e);
 74:     for (j = ys; j < ye - 1; j++) {
 75:       for (i = xs; i < xe - 1; i++) {
 76:         cell[0] = (i - Xs) + (j - Ys) * (Xe - Xs);
 77:         cell[1] = (i - Xs + 1) + (j - Ys) * (Xe - Xs);
 78:         cell[2] = (i - Xs + 1) + (j - Ys + 1) * (Xe - Xs);
 79:         cell[3] = (i - Xs) + (j - Ys + 1) * (Xe - Xs);
 80:         if (da->elementtype == DMDA_ELEMENT_P1) {
 81:           for (c = 0; c < ns * nn; c++) da->e[cnt++] = cell[split[c]];
 82:         }
 83:         if (da->elementtype == DMDA_ELEMENT_Q1) {
 84:           for (c = 0; c < ns * nn; c++) da->e[cnt++] = cell[c];
 85:         }
 86:       }
 87:     }

 89:     corners[0] = (xs - Xs) + (ys - Ys) * (Xe - Xs);
 90:     corners[1] = (xe - 1 - Xs) + (ys - Ys) * (Xe - Xs);
 91:     corners[2] = (xs - Xs) + (ye - 1 - Ys) * (Xe - Xs);
 92:     corners[3] = (xe - 1 - Xs) + (ye - 1 - Ys) * (Xe - Xs);
 93:     ISCreateGeneral(PETSC_COMM_SELF, 4, corners, PETSC_COPY_VALUES, &da->ecorners);
 94:   }
 95:   *nel = da->ne;
 96:   *nen = da->nen;
 97:   *e   = da->e;
 98:   return 0;
 99: }

101: static PetscErrorCode DMDAGetElements_3D(DM dm, PetscInt *nel, PetscInt *nen, const PetscInt *e[])
102: {
103:   DM_DA   *da = (DM_DA *)dm->data;
104:   PetscInt i, xs, xe, Xs, Xe;
105:   PetscInt j, ys, ye, Ys, Ye;
106:   PetscInt k, zs, ze, Zs, Ze;
107:   PetscInt cnt = 0, cell[8], ns = 6;
108:   PetscInt c, split[] = {0, 1, 3, 7, 0, 1, 7, 4, 1, 2, 3, 7, 1, 2, 7, 6, 1, 4, 5, 7, 1, 5, 6, 7};

110:   if (!da->e) {
111:     PetscInt corners[8], nn = 0;


115:     switch (da->elementtype) {
116:     case DMDA_ELEMENT_Q1:
117:       da->nen = 8;
118:       break;
119:     case DMDA_ELEMENT_P1:
120:       da->nen = 4;
121:       break;
122:     default:
123:       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unknown element type %d", da->elementtype);
124:     }
125:     nn = da->nen;

127:     if (da->elementtype == DMDA_ELEMENT_P1) ns = 6;
128:     if (da->elementtype == DMDA_ELEMENT_Q1) ns = 1;
129:     DMDAGetCorners(dm, &xs, &ys, &zs, &xe, &ye, &ze);
130:     DMDAGetGhostCorners(dm, &Xs, &Ys, &Zs, &Xe, &Ye, &Ze);
131:     xe += xs;
132:     Xe += Xs;
133:     if (xs != Xs) xs -= 1;
134:     ye += ys;
135:     Ye += Ys;
136:     if (ys != Ys) ys -= 1;
137:     ze += zs;
138:     Ze += Zs;
139:     if (zs != Zs) zs -= 1;
140:     da->ne = ns * (xe - xs - 1) * (ye - ys - 1) * (ze - zs - 1);
141:     PetscMalloc1(1 + nn * da->ne, &da->e);
142:     for (k = zs; k < ze - 1; k++) {
143:       for (j = ys; j < ye - 1; j++) {
144:         for (i = xs; i < xe - 1; i++) {
145:           cell[0] = (i - Xs) + (j - Ys) * (Xe - Xs) + (k - Zs) * (Xe - Xs) * (Ye - Ys);
146:           cell[1] = (i + 1 - Xs) + (j - Ys) * (Xe - Xs) + (k - Zs) * (Xe - Xs) * (Ye - Ys);
147:           cell[2] = (i + 1 - Xs) + (j + 1 - Ys) * (Xe - Xs) + (k - Zs) * (Xe - Xs) * (Ye - Ys);
148:           cell[3] = (i - Xs) + (j + 1 - Ys) * (Xe - Xs) + (k - Zs) * (Xe - Xs) * (Ye - Ys);
149:           cell[4] = (i - Xs) + (j - Ys) * (Xe - Xs) + (k + 1 - Zs) * (Xe - Xs) * (Ye - Ys);
150:           cell[5] = (i + 1 - Xs) + (j - Ys) * (Xe - Xs) + (k + 1 - Zs) * (Xe - Xs) * (Ye - Ys);
151:           cell[6] = (i + 1 - Xs) + (j + 1 - Ys) * (Xe - Xs) + (k + 1 - Zs) * (Xe - Xs) * (Ye - Ys);
152:           cell[7] = (i - Xs) + (j + 1 - Ys) * (Xe - Xs) + (k + 1 - Zs) * (Xe - Xs) * (Ye - Ys);
153:           if (da->elementtype == DMDA_ELEMENT_P1) {
154:             for (c = 0; c < ns * nn; c++) da->e[cnt++] = cell[split[c]];
155:           }
156:           if (da->elementtype == DMDA_ELEMENT_Q1) {
157:             for (c = 0; c < ns * nn; c++) da->e[cnt++] = cell[c];
158:           }
159:         }
160:       }
161:     }

163:     corners[0] = (xs - Xs) + (ys - Ys) * (Xe - Xs) + (zs - Zs) * (Xe - Xs) * (Ye - Ys);
164:     corners[1] = (xe - 1 - Xs) + (ys - Ys) * (Xe - Xs) + (zs - Zs) * (Xe - Xs) * (Ye - Ys);
165:     corners[2] = (xs - Xs) + (ye - 1 - Ys) * (Xe - Xs) + (zs - Zs) * (Xe - Xs) * (Ye - Ys);
166:     corners[3] = (xe - 1 - Xs) + (ye - 1 - Ys) * (Xe - Xs) + (zs - Zs) * (Xe - Xs) * (Ye - Ys);
167:     corners[4] = (xs - Xs) + (ys - Ys) * (Xe - Xs) + (ze - 1 - Zs) * (Xe - Xs) * (Ye - Ys);
168:     corners[5] = (xe - 1 - Xs) + (ys - Ys) * (Xe - Xs) + (ze - 1 - Zs) * (Xe - Xs) * (Ye - Ys);
169:     corners[6] = (xs - Xs) + (ye - 1 - Ys) * (Xe - Xs) + (ze - 1 - Zs) * (Xe - Xs) * (Ye - Ys);
170:     corners[7] = (xe - 1 - Xs) + (ye - 1 - Ys) * (Xe - Xs) + (ze - 1 - Zs) * (Xe - Xs) * (Ye - Ys);
171:     ISCreateGeneral(PETSC_COMM_SELF, 8, corners, PETSC_COPY_VALUES, &da->ecorners);
172:   }
173:   *nel = da->ne;
174:   *nen = da->nen;
175:   *e   = da->e;
176:   return 0;
177: }

179: /*@
180:    DMDAGetElementsCorners - Returns the global (x,y,z) indices of the lower left
181:    corner of the non-overlapping decomposition identified by DMDAGetElements()

183:     Not Collective

185:    Input Parameter:
186: .     da - the DM object

188:    Output Parameters:
189: +     gx - the x index
190: .     gy - the y index
191: -     gz - the z index

193:    Level: intermediate

195:    Notes:

197: .seealso: `DMDAElementType`, `DMDASetElementType()`, `DMDAGetElements()`
198: @*/
199: PetscErrorCode DMDAGetElementsCorners(DM da, PetscInt *gx, PetscInt *gy, PetscInt *gz)
200: {
201:   PetscInt  xs, Xs;
202:   PetscInt  ys, Ys;
203:   PetscInt  zs, Zs;
204:   PetscBool isda;

210:   PetscObjectTypeCompare((PetscObject)da, DMDA, &isda);
212:   DMDAGetCorners(da, &xs, &ys, &zs, NULL, NULL, NULL);
213:   DMDAGetGhostCorners(da, &Xs, &Ys, &Zs, NULL, NULL, NULL);
214:   if (xs != Xs) xs -= 1;
215:   if (ys != Ys) ys -= 1;
216:   if (zs != Zs) zs -= 1;
217:   if (gx) *gx = xs;
218:   if (gy) *gy = ys;
219:   if (gz) *gz = zs;
220:   return 0;
221: }

223: /*@
224:       DMDAGetElementsSizes - Gets the local number of elements per direction for the non-overlapping decomposition identified by DMDAGetElements()

226:     Not Collective

228:    Input Parameter:
229: .     da - the DM object

231:    Output Parameters:
232: +     mx - number of local elements in x-direction
233: .     my - number of local elements in y-direction
234: -     mz - number of local elements in z-direction

236:    Level: intermediate

238:    Notes:
239:     It returns the same number of elements, irrespective of the DMDAElementType

241: .seealso: `DMDAElementType`, `DMDASetElementType()`, `DMDAGetElements`
242: @*/
243: PetscErrorCode DMDAGetElementsSizes(DM da, PetscInt *mx, PetscInt *my, PetscInt *mz)
244: {
245:   PetscInt  xs, xe, Xs;
246:   PetscInt  ys, ye, Ys;
247:   PetscInt  zs, ze, Zs;
248:   PetscInt  dim;
249:   PetscBool isda;

255:   PetscObjectTypeCompare((PetscObject)da, DMDA, &isda);
257:   DMDAGetCorners(da, &xs, &ys, &zs, &xe, &ye, &ze);
258:   DMDAGetGhostCorners(da, &Xs, &Ys, &Zs, NULL, NULL, NULL);
259:   xe += xs;
260:   if (xs != Xs) xs -= 1;
261:   ye += ys;
262:   if (ys != Ys) ys -= 1;
263:   ze += zs;
264:   if (zs != Zs) zs -= 1;
265:   if (mx) *mx = 0;
266:   if (my) *my = 0;
267:   if (mz) *mz = 0;
268:   DMGetDimension(da, &dim);
269:   switch (dim) {
270:   case 3:
271:     if (mz) *mz = ze - zs - 1; /* fall through */
272:   case 2:
273:     if (my) *my = ye - ys - 1; /* fall through */
274:   case 1:
275:     if (mx) *mx = xe - xs - 1;
276:     break;
277:   }
278:   return 0;
279: }

281: /*@
282:       DMDASetElementType - Sets the element type to be returned by DMDAGetElements()

284:     Not Collective

286:    Input Parameter:
287: .     da - the DMDA object

289:    Output Parameters:
290: .     etype - the element type, currently either DMDA_ELEMENT_P1 or DMDA_ELEMENT_Q1

292:    Level: intermediate

294: .seealso: `DMDAElementType`, `DMDAGetElementType()`, `DMDAGetElements()`, `DMDARestoreElements()`
295: @*/
296: PetscErrorCode DMDASetElementType(DM da, DMDAElementType etype)
297: {
298:   DM_DA    *dd = (DM_DA *)da->data;
299:   PetscBool isda;

303:   PetscObjectTypeCompare((PetscObject)da, DMDA, &isda);
304:   if (!isda) return 0;
305:   if (dd->elementtype != etype) {
306:     PetscFree(dd->e);
307:     ISDestroy(&dd->ecorners);

309:     dd->elementtype = etype;
310:     dd->ne          = 0;
311:     dd->nen         = 0;
312:     dd->e           = NULL;
313:   }
314:   return 0;
315: }

317: /*@
318:       DMDAGetElementType - Gets the element type to be returned by DMDAGetElements()

320:     Not Collective

322:    Input Parameter:
323: .     da - the DMDA object

325:    Output Parameters:
326: .     etype - the element type, currently either DMDA_ELEMENT_P1 or DMDA_ELEMENT_Q1

328:    Level: intermediate

330: .seealso: `DMDAElementType`, `DMDASetElementType()`, `DMDAGetElements()`, `DMDARestoreElements()`
331: @*/
332: PetscErrorCode DMDAGetElementType(DM da, DMDAElementType *etype)
333: {
334:   DM_DA    *dd = (DM_DA *)da->data;
335:   PetscBool isda;

339:   PetscObjectTypeCompare((PetscObject)da, DMDA, &isda);
341:   *etype = dd->elementtype;
342:   return 0;
343: }

345: /*@C
346:       DMDAGetElements - Gets an array containing the indices (in local coordinates)
347:                  of all the local elements

349:     Not Collective

351:    Input Parameter:
352: .     dm - the DM object

354:    Output Parameters:
355: +     nel - number of local elements
356: .     nen - number of element nodes
357: -     e - the local indices of the elements' vertices

359:    Level: intermediate

361:    Notes:
362:      Call DMDARestoreElements() once you have finished accessing the elements.

364:      Each process uniquely owns a subset of the elements. That is no element is owned by two or more processes.

366:      If on each process you integrate over its owned elements and use ADD_VALUES in Vec/MatSetValuesLocal() then you'll obtain the correct result.

368:      Not supported in Fortran

370: .seealso: `DMDAElementType`, `DMDASetElementType()`, `VecSetValuesLocal()`, `MatSetValuesLocal()`, `DMGlobalToLocalBegin()`, `DMLocalToGlobalBegin()`
371: @*/
372: PetscErrorCode DMDAGetElements(DM dm, PetscInt *nel, PetscInt *nen, const PetscInt *e[])
373: {
374:   PetscInt  dim;
375:   DM_DA    *dd = (DM_DA *)dm->data;
376:   PetscBool isda;

382:   PetscObjectTypeCompare((PetscObject)dm, DMDA, &isda);
385:   DMGetDimension(dm, &dim);
386:   if (dd->e) {
387:     *nel = dd->ne;
388:     *nen = dd->nen;
389:     *e   = dd->e;
390:     return 0;
391:   }
392:   if (dim == -1) {
393:     *nel = 0;
394:     *nen = 0;
395:     *e   = NULL;
396:   } else if (dim == 1) {
397:     DMDAGetElements_1D(dm, nel, nen, e);
398:   } else if (dim == 2) {
399:     DMDAGetElements_2D(dm, nel, nen, e);
400:   } else if (dim == 3) {
401:     DMDAGetElements_3D(dm, nel, nen, e);
402:   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "DMDA dimension not 1, 2, or 3, it is %" PetscInt_FMT, dim);
403:   return 0;
404: }

406: /*@
407:       DMDAGetSubdomainCornersIS - Gets an index set containing the corner indices (in local coordinates)
408:                                  of the non-overlapping decomposition identified by DMDAGetElements

410:     Not Collective

412:    Input Parameter:
413: .     dm - the DM object

415:    Output Parameters:
416: .     is - the index set

418:    Level: intermediate

420:    Notes:
421:     Call DMDARestoreSubdomainCornersIS() once you have finished accessing the index set.

423: .seealso: `DMDAElementType`, `DMDASetElementType()`, `DMDAGetElements()`, `DMDARestoreElementsCornersIS()`
424: @*/
425: PetscErrorCode DMDAGetSubdomainCornersIS(DM dm, IS *is)
426: {
427:   DM_DA    *dd = (DM_DA *)dm->data;
428:   PetscBool isda;

432:   PetscObjectTypeCompare((PetscObject)dm, DMDA, &isda);
435:   if (!dd->ecorners) { /* compute elements if not yet done */
436:     const PetscInt *e;
437:     PetscInt        nel, nen;

439:     DMDAGetElements(dm, &nel, &nen, &e);
440:     DMDARestoreElements(dm, &nel, &nen, &e);
441:   }
442:   *is = dd->ecorners;
443:   return 0;
444: }

446: /*@C
447:       DMDARestoreElements - Restores the array obtained with DMDAGetElements()

449:     Not Collective

451:    Input Parameters:
452: +     dm - the DM object
453: .     nel - number of local elements
454: .     nen - number of element nodes
455: -     e - the local indices of the elements' vertices

457:    Level: intermediate

459:    Note: You should not access these values after you have called this routine.

461:          This restore signals the DMDA object that you no longer need access to the array information.

463:          Not supported in Fortran

465: .seealso: `DMDAElementType`, `DMDASetElementType()`, `DMDAGetElements()`
466: @*/
467: PetscErrorCode DMDARestoreElements(DM dm, PetscInt *nel, PetscInt *nen, const PetscInt *e[])
468: {
473:   *nel = 0;
474:   *nen = -1;
475:   *e   = NULL;
476:   return 0;
477: }

479: /*@
480:       DMDARestoreSubdomainCornersIS - Restores the IS obtained with DMDAGetSubdomainCornersIS()

482:     Not Collective

484:    Input Parameters:
485: +     dm - the DM object
486: -     is - the index set

488:    Level: intermediate

490:    Note:

492: .seealso: `DMDAElementType`, `DMDASetElementType()`, `DMDAGetSubdomainCornersIS()`
493: @*/
494: PetscErrorCode DMDARestoreSubdomainCornersIS(DM dm, IS *is)
495: {
498:   *is = NULL;
499:   return 0;
500: }