Actual source code: da.c

petsc-3.11.4 2019-09-28
Report Typos and Errors
  1:  #include <petsc/private/dmdaimpl.h>

  3: /*@
  4:   DMDASetSizes - Sets the number of grid points in the three dimensional directions

  6:   Logically Collective on DMDA

  8:   Input Parameters:
  9: + da - the DMDA
 10: . M - the global X size
 11: . N - the global Y size
 12: - P - the global Z size

 14:   Level: intermediate

 16:   Developer Notes:
 17:   Since the dimension may not yet have been set the code cannot error check for non-positive Y and Z number of grid points

 19: .seealso: PetscSplitOwnership()
 20: @*/
 21: PetscErrorCode  DMDASetSizes(DM da, PetscInt M, PetscInt N, PetscInt P)
 22: {
 23:   DM_DA *dd = (DM_DA*)da->data;

 30:   if (da->setupcalled) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_WRONGSTATE,"This function must be called before DMSetUp()");
 31:   if (M < 1) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_SIZ,"Number of grid points in X direction must be positive");
 32:   if (N < 0) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_SIZ,"Number of grid points in Y direction must be positive");
 33:   if (P < 0) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_SIZ,"Number of grid points in Z direction must be positive");

 35:   dd->M = M;
 36:   dd->N = N;
 37:   dd->P = P;
 38:   return(0);
 39: }

 41: /*@
 42:   DMDASetNumProcs - Sets the number of processes in each dimension

 44:   Logically Collective on DMDA

 46:   Input Parameters:
 47: + da - the DMDA
 48: . m - the number of X procs (or PETSC_DECIDE)
 49: . n - the number of Y procs (or PETSC_DECIDE)
 50: - p - the number of Z procs (or PETSC_DECIDE)

 52:   Level: intermediate

 54: .seealso: DMDASetSizes(), PetscSplitOwnership()
 55: @*/
 56: PetscErrorCode  DMDASetNumProcs(DM da, PetscInt m, PetscInt n, PetscInt p)
 57: {
 58:   DM_DA          *dd = (DM_DA*)da->data;

 66:   if (da->setupcalled) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_WRONGSTATE,"This function must be called before DMSetUp()");
 67:   dd->m = m;
 68:   dd->n = n;
 69:   dd->p = p;
 70:   if (da->dim == 2) {
 71:     PetscMPIInt size;
 72:     MPI_Comm_size(PetscObjectComm((PetscObject)da),&size);
 73:     if ((dd->m > 0) && (dd->n < 0)) {
 74:       dd->n = size/dd->m;
 75:       if (dd->n*dd->m != size) SETERRQ2(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_OUTOFRANGE,"%D processes in X direction not divisible into comm size %d",m,size);
 76:     }
 77:     if ((dd->n > 0) && (dd->m < 0)) {
 78:       dd->m = size/dd->n;
 79:       if (dd->n*dd->m != size) SETERRQ2(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_OUTOFRANGE,"%D processes in Y direction not divisible into comm size %d",n,size);
 80:     }
 81:   }
 82:   return(0);
 83: }

 85: /*@
 86:   DMDASetBoundaryType - Sets the type of ghost nodes on domain boundaries.

 88:   Not collective

 90:   Input Parameter:
 91: + da    - The DMDA
 92: - bx,by,bz - One of DM_BOUNDARY_NONE, DM_BOUNDARY_GHOSTED, DM_BOUNDARY_PERIODIC

 94:   Level: intermediate

 96: .keywords:  distributed array, periodicity
 97: .seealso: DMDACreate(), DMDestroy(), DMDA, DMBoundaryType
 98: @*/
 99: PetscErrorCode  DMDASetBoundaryType(DM da,DMBoundaryType bx,DMBoundaryType by,DMBoundaryType bz)
100: {
101:   DM_DA *dd = (DM_DA*)da->data;

108:   if (da->setupcalled) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_WRONGSTATE,"This function must be called before DMSetUp()");
109:   dd->bx = bx;
110:   dd->by = by;
111:   dd->bz = bz;
112:   return(0);
113: }

115: /*@
116:   DMDASetDof - Sets the number of degrees of freedom per vertex

118:   Not collective

120:   Input Parameters:
121: + da  - The DMDA
122: - dof - Number of degrees of freedom

124:   Level: intermediate

126: .keywords:  distributed array, degrees of freedom
127: .seealso: DMDAGetDof(), DMDACreate(), DMDestroy(), DMDA
128: @*/
129: PetscErrorCode  DMDASetDof(DM da, PetscInt dof)
130: {
131:   DM_DA *dd = (DM_DA*)da->data;

136:   if (da->setupcalled) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_WRONGSTATE,"This function must be called before DMSetUp()");
137:   dd->w  = dof;
138:   da->bs = dof;
139:   return(0);
140: }

142: /*@
143:   DMDAGetDof - Gets the number of degrees of freedom per vertex

145:   Not collective

147:   Input Parameter:
148: . da  - The DMDA

150:   Output Parameter:
151: . dof - Number of degrees of freedom

153:   Level: intermediate

155: .keywords:  distributed array, degrees of freedom
156: .seealso: DMDASetDof(), DMDACreate(), DMDestroy(), DMDA
157: @*/
158: PetscErrorCode DMDAGetDof(DM da, PetscInt *dof)
159: {
160:   DM_DA *dd = (DM_DA *) da->data;

165:   *dof = dd->w;
166:   return(0);
167: }

169: /*@
170:   DMDAGetOverlap - Gets the size of the per-processor overlap.

172:   Not collective

174:   Input Parameters:
175: . da  - The DMDA

177:   Output Parameters:
178: + x   - Overlap in the x direction
179: . y   - Overlap in the y direction
180: - z   - Overlap in the z direction

182:   Level: intermediate

184: .keywords:  distributed array, overlap, domain decomposition
185: .seealso: DMDACreateDomainDecomposition(), DMDASetOverlap(), DMDA
186: @*/
187: PetscErrorCode  DMDAGetOverlap(DM da,PetscInt *x,PetscInt *y,PetscInt *z)
188: {
189:   DM_DA *dd = (DM_DA*)da->data;

193:   if (x) *x = dd->xol;
194:   if (y) *y = dd->yol;
195:   if (z) *z = dd->zol;
196:   return(0);
197: }

199: /*@
200:   DMDASetOverlap - Sets the size of the per-processor overlap.

202:   Not collective

204:   Input Parameters:
205: + da  - The DMDA
206: . x   - Overlap in the x direction
207: . y   - Overlap in the y direction
208: - z   - Overlap in the z direction

210:   Level: intermediate

212: .keywords:  distributed array, overlap, domain decomposition
213: .seealso: DMDACreateDomainDecomposition(), DMDAGetOverlap(), DMDA
214: @*/
215: PetscErrorCode  DMDASetOverlap(DM da,PetscInt x,PetscInt y,PetscInt z)
216: {
217:   DM_DA *dd = (DM_DA*)da->data;

224:   dd->xol = x;
225:   dd->yol = y;
226:   dd->zol = z;
227:   return(0);
228: }


231: /*@
232:   DMDAGetNumLocalSubDomains - Gets the number of local subdomains created upon decomposition.

234:   Not collective

236:   Input Parameters:
237: . da  - The DMDA

239:   Output Parameters:
240: + Nsub   - Number of local subdomains created upon decomposition

242:   Level: intermediate

244: .keywords:  distributed array, domain decomposition
245: .seealso: DMDACreateDomainDecomposition(), DMDASetNumLocalSubDomains(), DMDA
246: @*/
247: PetscErrorCode  DMDAGetNumLocalSubDomains(DM da,PetscInt *Nsub)
248: {
249:   DM_DA *dd = (DM_DA*)da->data;

253:   if (Nsub) *Nsub = dd->Nsub;
254:   return(0);
255: }

257: /*@
258:   DMDASetNumLocalSubDomains - Sets the number of local subdomains created upon decomposition.

260:   Not collective

262:   Input Parameters:
263: + da  - The DMDA
264: - Nsub - The number of local subdomains requested

266:   Level: intermediate

268: .keywords:  distributed array, domain decomposition
269: .seealso: DMDACreateDomainDecomposition(), DMDAGetNumLocalSubDomains(), DMDA
270: @*/
271: PetscErrorCode  DMDASetNumLocalSubDomains(DM da,PetscInt Nsub)
272: {
273:   DM_DA *dd = (DM_DA*)da->data;

278:   dd->Nsub = Nsub;
279:   return(0);
280: }

282: /*@
283:   DMDASetOffset - Sets the index offset of the DA.

285:   Collective on DA

287:   Input Parameter:
288: + da  - The DMDA
289: . xo  - The offset in the x direction
290: . yo  - The offset in the y direction
291: - zo  - The offset in the z direction

293:   Level: intermediate

295:   Notes:
296:     This is used primarily to overlap a computation on a local DA with that on a global DA without
297:   changing boundary conditions or subdomain features that depend upon the global offsets.

299: .keywords:  distributed array, degrees of freedom
300: .seealso: DMDAGetOffset(), DMDAVecGetArray()
301: @*/
302: PetscErrorCode  DMDASetOffset(DM da, PetscInt xo, PetscInt yo, PetscInt zo, PetscInt Mo, PetscInt No, PetscInt Po)
303: {
305:   DM_DA          *dd = (DM_DA*)da->data;

315:   dd->xo = xo;
316:   dd->yo = yo;
317:   dd->zo = zo;
318:   dd->Mo = Mo;
319:   dd->No = No;
320:   dd->Po = Po;

322:   if (da->coordinateDM) {
323:     DMDASetOffset(da->coordinateDM,xo,yo,zo,Mo,No,Po);
324:   }
325:   return(0);
326: }

328: /*@
329:   DMDAGetOffset - Gets the index offset of the DA.

331:   Not collective

333:   Input Parameter:
334: . da  - The DMDA

336:   Output Parameters:
337: + xo  - The offset in the x direction
338: . yo  - The offset in the y direction
339: . zo  - The offset in the z direction
340: . Mo  - The global size in the x direction
341: . No  - The global size in the y direction
342: - Po  - The global size in the z direction

344:   Level: intermediate

346: .keywords:  distributed array, degrees of freedom
347: .seealso: DMDASetOffset(), DMDAVecGetArray()
348: @*/
349: PetscErrorCode  DMDAGetOffset(DM da,PetscInt *xo,PetscInt *yo,PetscInt *zo,PetscInt *Mo,PetscInt *No,PetscInt *Po)
350: {
351:   DM_DA *dd = (DM_DA*)da->data;

355:   if (xo) *xo = dd->xo;
356:   if (yo) *yo = dd->yo;
357:   if (zo) *zo = dd->zo;
358:   if (Mo) *Mo = dd->Mo;
359:   if (No) *No = dd->No;
360:   if (Po) *Po = dd->Po;
361:   return(0);
362: }

364: /*@
365:   DMDAGetNonOverlappingRegion - Gets the indices of the nonoverlapping region of a subdomain DM.

367:   Not collective

369:   Input Parameter:
370: . da  - The DMDA

372:   Output Parameters:
373: + xs  - The start of the region in x
374: . ys  - The start of the region in y
375: . zs  - The start of the region in z
376: . xs  - The size of the region in x
377: . ys  - The size of the region in y
378: . zs  - The size of the region in z

380:   Level: intermediate

382: .keywords:  distributed array, degrees of freedom
383: .seealso: DMDAGetOffset(), DMDAVecGetArray()
384: @*/
385: PetscErrorCode  DMDAGetNonOverlappingRegion(DM da, PetscInt *xs, PetscInt *ys, PetscInt *zs, PetscInt *xm, PetscInt *ym, PetscInt *zm)
386: {
387:   DM_DA          *dd = (DM_DA*)da->data;

391:   if (xs) *xs = dd->nonxs;
392:   if (ys) *ys = dd->nonys;
393:   if (zs) *zs = dd->nonzs;
394:   if (xm) *xm = dd->nonxm;
395:   if (ym) *ym = dd->nonym;
396:   if (zm) *zm = dd->nonzm;
397:   return(0);
398: }


401: /*@
402:   DMDASetNonOverlappingRegion - Sets the indices of the nonoverlapping region of a subdomain DM.

404:   Collective on DA

406:   Input Parameter:
407: + da  - The DMDA
408: . xs  - The start of the region in x
409: . ys  - The start of the region in y
410: . zs  - The start of the region in z
411: . xs  - The size of the region in x
412: . ys  - The size of the region in y
413: . zs  - The size of the region in z

415:   Level: intermediate

417: .keywords:  distributed array, degrees of freedom
418: .seealso: DMDAGetOffset(), DMDAVecGetArray()
419: @*/
420: PetscErrorCode  DMDASetNonOverlappingRegion(DM da, PetscInt xs, PetscInt ys, PetscInt zs, PetscInt xm, PetscInt ym, PetscInt zm)
421: {
422:   DM_DA          *dd = (DM_DA*)da->data;

432:   dd->nonxs = xs;
433:   dd->nonys = ys;
434:   dd->nonzs = zs;
435:   dd->nonxm = xm;
436:   dd->nonym = ym;
437:   dd->nonzm = zm;

439:   return(0);
440: }

442: /*@
443:   DMDASetStencilType - Sets the type of the communication stencil

445:   Logically Collective on DMDA

447:   Input Parameter:
448: + da    - The DMDA
449: - stype - The stencil type, use either DMDA_STENCIL_BOX or DMDA_STENCIL_STAR.

451:   Level: intermediate

453: .keywords:  distributed array, stencil
454: .seealso: DMDACreate(), DMDestroy(), DMDA
455: @*/
456: PetscErrorCode  DMDASetStencilType(DM da, DMDAStencilType stype)
457: {
458:   DM_DA *dd = (DM_DA*)da->data;

463:   if (da->setupcalled) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_WRONGSTATE,"This function must be called before DMSetUp()");
464:   dd->stencil_type = stype;
465:   return(0);
466: }

468: /*@
469:   DMDAGetStencilType - Gets the type of the communication stencil

471:   Not collective

473:   Input Parameter:
474: . da    - The DMDA

476:   Output Parameter:
477: . stype - The stencil type, use either DMDA_STENCIL_BOX or DMDA_STENCIL_STAR.

479:   Level: intermediate

481: .keywords:  distributed array, stencil
482: .seealso: DMDACreate(), DMDestroy(), DMDA
483: @*/
484: PetscErrorCode DMDAGetStencilType(DM da, DMDAStencilType *stype)
485: {
486:   DM_DA *dd = (DM_DA*)da->data;

491:   *stype = dd->stencil_type;
492:   return(0);
493: }

495: /*@
496:   DMDASetStencilWidth - Sets the width of the communication stencil

498:   Logically Collective on DMDA

500:   Input Parameter:
501: + da    - The DMDA
502: - width - The stencil width

504:   Level: intermediate

506: .keywords:  distributed array, stencil
507: .seealso: DMDACreate(), DMDestroy(), DMDA
508: @*/
509: PetscErrorCode  DMDASetStencilWidth(DM da, PetscInt width)
510: {
511:   DM_DA *dd = (DM_DA*)da->data;

516:   if (da->setupcalled) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_WRONGSTATE,"This function must be called before DMSetUp()");
517:   dd->s = width;
518:   return(0);
519: }

521: /*@
522:   DMDAGetStencilWidth - Gets the width of the communication stencil

524:   Not collective

526:   Input Parameter:
527: . da    - The DMDA

529:   Output Parameter:
530: . width - The stencil width

532:   Level: intermediate

534: .keywords:  distributed array, stencil
535: .seealso: DMDACreate(), DMDestroy(), DMDA
536: @*/
537: PetscErrorCode DMDAGetStencilWidth(DM da, PetscInt *width)
538: {
539:   DM_DA *dd = (DM_DA *) da->data;

544:   *width = dd->s;
545:   return(0);
546: }

548: static PetscErrorCode DMDACheckOwnershipRanges_Private(DM da,PetscInt M,PetscInt m,const PetscInt lx[])
549: {
550:   PetscInt i,sum;

553:   if (M < 0) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_WRONGSTATE,"Global dimension not set");
554:   for (i=sum=0; i<m; i++) sum += lx[i];
555:   if (sum != M) SETERRQ2(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_INCOMP,"Ownership ranges sum to %D but global dimension is %D",sum,M);
556:   return(0);
557: }

559: /*@
560:   DMDASetOwnershipRanges - Sets the number of nodes in each direction on each process

562:   Logically Collective on DMDA

564:   Input Parameter:
565: + da - The DMDA
566: . lx - array containing number of nodes in the X direction on each process, or NULL. If non-null, must be of length da->m
567: . ly - array containing number of nodes in the Y direction on each process, or NULL. If non-null, must be of length da->n
568: - lz - array containing number of nodes in the Z direction on each process, or NULL. If non-null, must be of length da->p.

570:   Level: intermediate

572:   Note: these numbers are NOT multiplied by the number of dof per node.

574: .keywords:  distributed array
575: .seealso: DMDACreate(), DMDestroy(), DMDA
576: @*/
577: PetscErrorCode  DMDASetOwnershipRanges(DM da, const PetscInt lx[], const PetscInt ly[], const PetscInt lz[])
578: {
580:   DM_DA          *dd = (DM_DA*)da->data;

584:   if (da->setupcalled) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_WRONGSTATE,"This function must be called before DMSetUp()");
585:   if (lx) {
586:     if (dd->m < 0) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_WRONGSTATE,"Cannot set ownership ranges before setting number of procs");
587:     DMDACheckOwnershipRanges_Private(da,dd->M,dd->m,lx);
588:     if (!dd->lx) {
589:       PetscMalloc1(dd->m, &dd->lx);
590:     }
591:     PetscMemcpy(dd->lx, lx, dd->m*sizeof(PetscInt));
592:   }
593:   if (ly) {
594:     if (dd->n < 0) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_WRONGSTATE,"Cannot set ownership ranges before setting number of procs");
595:     DMDACheckOwnershipRanges_Private(da,dd->N,dd->n,ly);
596:     if (!dd->ly) {
597:       PetscMalloc1(dd->n, &dd->ly);
598:     }
599:     PetscMemcpy(dd->ly, ly, dd->n*sizeof(PetscInt));
600:   }
601:   if (lz) {
602:     if (dd->p < 0) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_WRONGSTATE,"Cannot set ownership ranges before setting number of procs");
603:     DMDACheckOwnershipRanges_Private(da,dd->P,dd->p,lz);
604:     if (!dd->lz) {
605:       PetscMalloc1(dd->p, &dd->lz);
606:     }
607:     PetscMemcpy(dd->lz, lz, dd->p*sizeof(PetscInt));
608:   }
609:   return(0);
610: }

612: /*@
613:        DMDASetInterpolationType - Sets the type of interpolation that will be
614:           returned by DMCreateInterpolation()

616:    Logically Collective on DMDA

618:    Input Parameter:
619: +  da - initial distributed array
620: .  ctype - DMDA_Q1 and DMDA_Q0 are currently the only supported forms

622:    Level: intermediate

624:    Notes:
625:     you should call this on the coarser of the two DMDAs you pass to DMCreateInterpolation()

627: .keywords:  distributed array, interpolation

629: .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType
630: @*/
631: PetscErrorCode  DMDASetInterpolationType(DM da,DMDAInterpolationType ctype)
632: {
633:   DM_DA *dd = (DM_DA*)da->data;

638:   dd->interptype = ctype;
639:   return(0);
640: }

642: /*@
643:        DMDAGetInterpolationType - Gets the type of interpolation that will be
644:           used by DMCreateInterpolation()

646:    Not Collective

648:    Input Parameter:
649: .  da - distributed array

651:    Output Parameter:
652: .  ctype - interpolation type (DMDA_Q1 and DMDA_Q0 are currently the only supported forms)

654:    Level: intermediate

656: .keywords:  distributed array, interpolation

658: .seealso: DMDA, DMDAInterpolationType, DMDASetInterpolationType(), DMCreateInterpolation()
659: @*/
660: PetscErrorCode  DMDAGetInterpolationType(DM da,DMDAInterpolationType *ctype)
661: {
662:   DM_DA *dd = (DM_DA*)da->data;

667:   *ctype = dd->interptype;
668:   return(0);
669: }

671: /*@C
672:       DMDAGetNeighbors - Gets an array containing the MPI rank of all the current
673:         processes neighbors.

675:     Not Collective

677:    Input Parameter:
678: .     da - the DMDA object

680:    Output Parameters:
681: .     ranks - the neighbors ranks, stored with the x index increasing most rapidly.
682:               this process itself is in the list

684:    Notes:
685:     In 2d the array is of length 9, in 3d of length 27
686:           Not supported in 1d
687:           Do not free the array, it is freed when the DMDA is destroyed.

689:    Fortran Notes:
690:     In fortran you must pass in an array of the appropriate length.

692:    Level: intermediate

694: @*/
695: PetscErrorCode  DMDAGetNeighbors(DM da,const PetscMPIInt *ranks[])
696: {
697:   DM_DA *dd = (DM_DA*)da->data;

701:   *ranks = dd->neighbors;
702:   return(0);
703: }

705: /*@C
706:       DMDAGetOwnershipRanges - Gets the ranges of indices in the x, y and z direction that are owned by each process

708:     Not Collective

710:    Input Parameter:
711: .     da - the DMDA object

713:    Output Parameter:
714: +     lx - ownership along x direction (optional)
715: .     ly - ownership along y direction (optional)
716: -     lz - ownership along z direction (optional)

718:    Level: intermediate

720:     Note: these correspond to the optional final arguments passed to DMDACreate(), DMDACreate2d(), DMDACreate3d()

722:     In Fortran one must pass in arrays lx, ly, and lz that are long enough to hold the values; the sixth, seventh and
723:     eighth arguments from DMDAGetInfo()

725:      In C you should not free these arrays, nor change the values in them. They will only have valid values while the
726:     DMDA they came from still exists (has not been destroyed).

728:     These numbers are NOT multiplied by the number of dof per node.

730: .seealso: DMDAGetCorners(), DMDAGetGhostCorners(), DMDACreate(), DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), VecGetOwnershipRanges()
731: @*/
732: PetscErrorCode  DMDAGetOwnershipRanges(DM da,const PetscInt *lx[],const PetscInt *ly[],const PetscInt *lz[])
733: {
734:   DM_DA *dd = (DM_DA*)da->data;

738:   if (lx) *lx = dd->lx;
739:   if (ly) *ly = dd->ly;
740:   if (lz) *lz = dd->lz;
741:   return(0);
742: }

744: /*@
745:      DMDASetRefinementFactor - Set the ratios that the DMDA grid is refined

747:     Logically Collective on DMDA

749:   Input Parameters:
750: +    da - the DMDA object
751: .    refine_x - ratio of fine grid to coarse in x direction (2 by default)
752: .    refine_y - ratio of fine grid to coarse in y direction (2 by default)
753: -    refine_z - ratio of fine grid to coarse in z direction (2 by default)

755:   Options Database:
756: +  -da_refine_x - refinement ratio in x direction
757: .  -da_refine_y - refinement ratio in y direction
758: -  -da_refine_z - refinement ratio in z direction

760:   Level: intermediate

762:     Notes:
763:     Pass PETSC_IGNORE to leave a value unchanged

765: .seealso: DMRefine(), DMDAGetRefinementFactor()
766: @*/
767: PetscErrorCode  DMDASetRefinementFactor(DM da, PetscInt refine_x, PetscInt refine_y,PetscInt refine_z)
768: {
769:   DM_DA *dd = (DM_DA*)da->data;


777:   if (refine_x > 0) dd->refine_x = refine_x;
778:   if (refine_y > 0) dd->refine_y = refine_y;
779:   if (refine_z > 0) dd->refine_z = refine_z;
780:   return(0);
781: }

783: /*@C
784:      DMDAGetRefinementFactor - Gets the ratios that the DMDA grid is refined

786:     Not Collective

788:   Input Parameter:
789: .    da - the DMDA object

791:   Output Parameters:
792: +    refine_x - ratio of fine grid to coarse in x direction (2 by default)
793: .    refine_y - ratio of fine grid to coarse in y direction (2 by default)
794: -    refine_z - ratio of fine grid to coarse in z direction (2 by default)

796:   Level: intermediate

798:     Notes:
799:     Pass NULL for values you do not need

801: .seealso: DMRefine(), DMDASetRefinementFactor()
802: @*/
803: PetscErrorCode  DMDAGetRefinementFactor(DM da, PetscInt *refine_x, PetscInt *refine_y,PetscInt *refine_z)
804: {
805:   DM_DA *dd = (DM_DA*)da->data;

809:   if (refine_x) *refine_x = dd->refine_x;
810:   if (refine_y) *refine_y = dd->refine_y;
811:   if (refine_z) *refine_z = dd->refine_z;
812:   return(0);
813: }

815: /*@C
816:      DMDASetGetMatrix - Sets the routine used by the DMDA to allocate a matrix.

818:     Logically Collective on DMDA

820:   Input Parameters:
821: +    da - the DMDA object
822: -    f - the function that allocates the matrix for that specific DMDA

824:   Level: developer

826:    Notes:
827:     See DMDASetBlockFills() that provides a simple way to provide the nonzero structure for
828:        the diagonal and off-diagonal blocks of the matrix

830:    Not supported from Fortran

832: .seealso: DMCreateMatrix(), DMDASetBlockFills()
833: @*/
834: PetscErrorCode  DMDASetGetMatrix(DM da,PetscErrorCode (*f)(DM, Mat*))
835: {
838:   da->ops->creatematrix = f;
839:   return(0);
840: }

842: /*
843:   Creates "balanced" ownership ranges after refinement, constrained by the need for the
844:   fine grid boundaries to fall within one stencil width of the coarse partition.

846:   Uses a greedy algorithm to handle non-ideal layouts, could probably do something better.
847: */
848: static PetscErrorCode DMDARefineOwnershipRanges(DM da,PetscBool periodic,PetscInt stencil_width,PetscInt ratio,PetscInt m,const PetscInt lc[],PetscInt lf[])
849: {
850:   PetscInt       i,totalc = 0,remaining,startc = 0,startf = 0;

854:   if (ratio < 1) SETERRQ1(PetscObjectComm((PetscObject)da),PETSC_ERR_USER,"Requested refinement ratio %D must be at least 1",ratio);
855:   if (ratio == 1) {
856:     PetscMemcpy(lf,lc,m*sizeof(lc[0]));
857:     return(0);
858:   }
859:   for (i=0; i<m; i++) totalc += lc[i];
860:   remaining = (!periodic) + ratio * (totalc - (!periodic));
861:   for (i=0; i<m; i++) {
862:     PetscInt want = remaining/(m-i) + !!(remaining%(m-i));
863:     if (i == m-1) lf[i] = want;
864:     else {
865:       const PetscInt nextc = startc + lc[i];
866:       /* Move the first fine node of the next subdomain to the right until the coarse node on its left is within one
867:        * coarse stencil width of the first coarse node in the next subdomain. */
868:       while ((startf+want)/ratio < nextc - stencil_width) want++;
869:       /* Move the last fine node in the current subdomain to the left until the coarse node on its right is within one
870:        * coarse stencil width of the last coarse node in the current subdomain. */
871:       while ((startf+want-1+ratio-1)/ratio > nextc-1+stencil_width) want--;
872:       /* Make sure all constraints are satisfied */
873:       if (want < 0 || want > remaining || ((startf+want)/ratio < nextc - stencil_width)
874:           || ((startf+want-1+ratio-1)/ratio > nextc-1+stencil_width)) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_SIZ,"Could not find a compatible refined ownership range");
875:     }
876:     lf[i]      = want;
877:     startc    += lc[i];
878:     startf    += lf[i];
879:     remaining -= lf[i];
880:   }
881:   return(0);
882: }

884: /*
885:   Creates "balanced" ownership ranges after coarsening, constrained by the need for the
886:   fine grid boundaries to fall within one stencil width of the coarse partition.

888:   Uses a greedy algorithm to handle non-ideal layouts, could probably do something better.
889: */
890: static PetscErrorCode DMDACoarsenOwnershipRanges(DM da,PetscBool periodic,PetscInt stencil_width,PetscInt ratio,PetscInt m,const PetscInt lf[],PetscInt lc[])
891: {
892:   PetscInt       i,totalf,remaining,startc,startf;

896:   if (ratio < 1) SETERRQ1(PetscObjectComm((PetscObject)da),PETSC_ERR_USER,"Requested refinement ratio %D must be at least 1",ratio);
897:   if (ratio == 1) {
898:     PetscMemcpy(lc,lf,m*sizeof(lf[0]));
899:     return(0);
900:   }
901:   for (i=0,totalf=0; i<m; i++) totalf += lf[i];
902:   remaining = (!periodic) + (totalf - (!periodic)) / ratio;
903:   for (i=0,startc=0,startf=0; i<m; i++) {
904:     PetscInt want = remaining/(m-i) + !!(remaining%(m-i));
905:     if (i == m-1) lc[i] = want;
906:     else {
907:       const PetscInt nextf = startf+lf[i];
908:       /* Slide first coarse node of next subdomain to the left until the coarse node to the left of the first fine
909:        * node is within one stencil width. */
910:       while (nextf/ratio < startc+want-stencil_width) want--;
911:       /* Slide the last coarse node of the current subdomain to the right until the coarse node to the right of the last
912:        * fine node is within one stencil width. */
913:       while ((nextf-1+ratio-1)/ratio > startc+want-1+stencil_width) want++;
914:       if (want < 0 || want > remaining
915:           || (nextf/ratio < startc+want-stencil_width) || ((nextf-1+ratio-1)/ratio > startc+want-1+stencil_width)) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_SIZ,"Could not find a compatible coarsened ownership range");
916:     }
917:     lc[i]      = want;
918:     startc    += lc[i];
919:     startf    += lf[i];
920:     remaining -= lc[i];
921:   }
922:   return(0);
923: }

925: PetscErrorCode  DMRefine_DA(DM da,MPI_Comm comm,DM *daref)
926: {
928:   PetscInt       M,N,P,i,dim;
929:   DM             da2;
930:   DM_DA          *dd = (DM_DA*)da->data,*dd2;


936:   DMGetDimension(da, &dim);
937:   if (dd->bx == DM_BOUNDARY_PERIODIC || dd->interptype == DMDA_Q0) {
938:     M = dd->refine_x*dd->M;
939:   } else {
940:     M = 1 + dd->refine_x*(dd->M - 1);
941:   }
942:   if (dd->by == DM_BOUNDARY_PERIODIC || dd->interptype == DMDA_Q0) {
943:     if (dim > 1) {
944:       N = dd->refine_y*dd->N;
945:     } else {
946:       N = 1;
947:     }
948:   } else {
949:     N = 1 + dd->refine_y*(dd->N - 1);
950:   }
951:   if (dd->bz == DM_BOUNDARY_PERIODIC || dd->interptype == DMDA_Q0) {
952:     if (dim > 2) {
953:       P = dd->refine_z*dd->P;
954:     } else {
955:       P = 1;
956:     }
957:   } else {
958:     P = 1 + dd->refine_z*(dd->P - 1);
959:   }
960:   DMDACreate(PetscObjectComm((PetscObject)da),&da2);
961:   DMSetOptionsPrefix(da2,((PetscObject)da)->prefix);
962:   DMSetDimension(da2,dim);
963:   DMDASetSizes(da2,M,N,P);
964:   DMDASetNumProcs(da2,dd->m,dd->n,dd->p);
965:   DMDASetBoundaryType(da2,dd->bx,dd->by,dd->bz);
966:   DMDASetDof(da2,dd->w);
967:   DMDASetStencilType(da2,dd->stencil_type);
968:   DMDASetStencilWidth(da2,dd->s);
969:   if (dim == 3) {
970:     PetscInt *lx,*ly,*lz;
971:     PetscMalloc3(dd->m,&lx,dd->n,&ly,dd->p,&lz);
972:     DMDARefineOwnershipRanges(da,(PetscBool)(dd->bx == DM_BOUNDARY_PERIODIC || dd->interptype == DMDA_Q0),dd->s,dd->refine_x,dd->m,dd->lx,lx);
973:     DMDARefineOwnershipRanges(da,(PetscBool)(dd->by == DM_BOUNDARY_PERIODIC || dd->interptype == DMDA_Q0),dd->s,dd->refine_y,dd->n,dd->ly,ly);
974:     DMDARefineOwnershipRanges(da,(PetscBool)(dd->bz == DM_BOUNDARY_PERIODIC || dd->interptype == DMDA_Q0),dd->s,dd->refine_z,dd->p,dd->lz,lz);
975:     DMDASetOwnershipRanges(da2,lx,ly,lz);
976:     PetscFree3(lx,ly,lz);
977:   } else if (dim == 2) {
978:     PetscInt *lx,*ly;
979:     PetscMalloc2(dd->m,&lx,dd->n,&ly);
980:     DMDARefineOwnershipRanges(da,(PetscBool)(dd->bx == DM_BOUNDARY_PERIODIC || dd->interptype == DMDA_Q0),dd->s,dd->refine_x,dd->m,dd->lx,lx);
981:     DMDARefineOwnershipRanges(da,(PetscBool)(dd->by == DM_BOUNDARY_PERIODIC || dd->interptype == DMDA_Q0),dd->s,dd->refine_y,dd->n,dd->ly,ly);
982:     DMDASetOwnershipRanges(da2,lx,ly,NULL);
983:     PetscFree2(lx,ly);
984:   } else if (dim == 1) {
985:     PetscInt *lx;
986:     PetscMalloc1(dd->m,&lx);
987:     DMDARefineOwnershipRanges(da,(PetscBool)(dd->bx == DM_BOUNDARY_PERIODIC || dd->interptype == DMDA_Q0),dd->s,dd->refine_x,dd->m,dd->lx,lx);
988:     DMDASetOwnershipRanges(da2,lx,NULL,NULL);
989:     PetscFree(lx);
990:   }
991:   dd2 = (DM_DA*)da2->data;

993:   /* allow overloaded (user replaced) operations to be inherited by refinement clones */
994:   da2->ops->creatematrix = da->ops->creatematrix;
995:   /* da2->ops->createinterpolation = da->ops->createinterpolation; this causes problem with SNESVI */
996:   da2->ops->getcoloring = da->ops->getcoloring;
997:   dd2->interptype       = dd->interptype;

999:   /* copy fill information if given */
1000:   if (dd->dfill) {
1001:     PetscMalloc1(dd->dfill[dd->w]+dd->w+1,&dd2->dfill);
1002:     PetscMemcpy(dd2->dfill,dd->dfill,(dd->dfill[dd->w]+dd->w+1)*sizeof(PetscInt));
1003:   }
1004:   if (dd->ofill) {
1005:     PetscMalloc1(dd->ofill[dd->w]+dd->w+1,&dd2->ofill);
1006:     PetscMemcpy(dd2->ofill,dd->ofill,(dd->ofill[dd->w]+dd->w+1)*sizeof(PetscInt));
1007:   }
1008:   /* copy the refine information */
1009:   dd2->coarsen_x = dd2->refine_x = dd->refine_x;
1010:   dd2->coarsen_y = dd2->refine_y = dd->refine_y;
1011:   dd2->coarsen_z = dd2->refine_z = dd->refine_z;

1013:   if (dd->refine_z_hier) {
1014:     if (da->levelup - da->leveldown + 1 > -1 && da->levelup - da->leveldown + 1 < dd->refine_z_hier_n) {
1015:       dd2->refine_z = dd->refine_z_hier[da->levelup - da->leveldown + 1];
1016:     }
1017:     if (da->levelup - da->leveldown > -1 && da->levelup - da->leveldown < dd->refine_z_hier_n) {
1018:       dd2->coarsen_z = dd->refine_z_hier[da->levelup - da->leveldown];
1019:     }
1020:     dd2->refine_z_hier_n = dd->refine_z_hier_n;
1021:     PetscMalloc1(dd2->refine_z_hier_n,&dd2->refine_z_hier);
1022:     PetscMemcpy(dd2->refine_z_hier,dd->refine_z_hier,dd2->refine_z_hier_n*sizeof(PetscInt));
1023:   }
1024:   if (dd->refine_y_hier) {
1025:     if (da->levelup - da->leveldown + 1 > -1 && da->levelup - da->leveldown + 1 < dd->refine_y_hier_n) {
1026:       dd2->refine_y = dd->refine_y_hier[da->levelup - da->leveldown + 1];
1027:     }
1028:     if (da->levelup - da->leveldown > -1 && da->levelup - da->leveldown < dd->refine_y_hier_n) {
1029:       dd2->coarsen_y = dd->refine_y_hier[da->levelup - da->leveldown];
1030:     }
1031:     dd2->refine_y_hier_n = dd->refine_y_hier_n;
1032:     PetscMalloc1(dd2->refine_y_hier_n,&dd2->refine_y_hier);
1033:     PetscMemcpy(dd2->refine_y_hier,dd->refine_y_hier,dd2->refine_y_hier_n*sizeof(PetscInt));
1034:   }
1035:   if (dd->refine_x_hier) {
1036:     if (da->levelup - da->leveldown + 1 > -1 && da->levelup - da->leveldown + 1 < dd->refine_x_hier_n) {
1037:       dd2->refine_x = dd->refine_x_hier[da->levelup - da->leveldown + 1];
1038:     }
1039:     if (da->levelup - da->leveldown > -1 && da->levelup - da->leveldown < dd->refine_x_hier_n) {
1040:       dd2->coarsen_x = dd->refine_x_hier[da->levelup - da->leveldown];
1041:     }
1042:     dd2->refine_x_hier_n = dd->refine_x_hier_n;
1043:     PetscMalloc1(dd2->refine_x_hier_n,&dd2->refine_x_hier);
1044:     PetscMemcpy(dd2->refine_x_hier,dd->refine_x_hier,dd2->refine_x_hier_n*sizeof(PetscInt));
1045:   }


1048:   /* copy vector type information */
1049:   DMSetVecType(da2,da->vectype);

1051:   dd2->lf = dd->lf;
1052:   dd2->lj = dd->lj;

1054:   da2->leveldown = da->leveldown;
1055:   da2->levelup   = da->levelup + 1;

1057:   DMSetUp(da2);

1059:   /* interpolate coordinates if they are set on the coarse grid */
1060:   if (da->coordinates) {
1061:     DM  cdaf,cdac;
1062:     Vec coordsc,coordsf;
1063:     Mat II;

1065:     DMGetCoordinateDM(da,&cdac);
1066:     DMGetCoordinates(da,&coordsc);
1067:     DMGetCoordinateDM(da2,&cdaf);
1068:     /* force creation of the coordinate vector */
1069:     DMDASetUniformCoordinates(da2,0.0,1.0,0.0,1.0,0.0,1.0);
1070:     DMGetCoordinates(da2,&coordsf);
1071:     DMCreateInterpolation(cdac,cdaf,&II,NULL);
1072:     MatInterpolate(II,coordsc,coordsf);
1073:     MatDestroy(&II);
1074:   }

1076:   for (i=0; i<da->bs; i++) {
1077:     const char *fieldname;
1078:     DMDAGetFieldName(da,i,&fieldname);
1079:     DMDASetFieldName(da2,i,fieldname);
1080:   }

1082:   *daref = da2;
1083:   return(0);
1084: }


1087: PetscErrorCode  DMCoarsen_DA(DM da, MPI_Comm comm,DM *daref)
1088: {
1090:   PetscInt       M,N,P,i,dim;
1091:   DM             da2;
1092:   DM_DA          *dd = (DM_DA*)da->data,*dd2;


1098:   DMGetDimension(da, &dim);
1099:   if (dd->bx == DM_BOUNDARY_PERIODIC || dd->interptype == DMDA_Q0) {
1100:     M = dd->M / dd->coarsen_x;
1101:   } else {
1102:     M = 1 + (dd->M - 1) / dd->coarsen_x;
1103:   }
1104:   if (dd->by == DM_BOUNDARY_PERIODIC || dd->interptype == DMDA_Q0) {
1105:     if (dim > 1) {
1106:       N = dd->N / dd->coarsen_y;
1107:     } else {
1108:       N = 1;
1109:     }
1110:   } else {
1111:     N = 1 + (dd->N - 1) / dd->coarsen_y;
1112:   }
1113:   if (dd->bz == DM_BOUNDARY_PERIODIC || dd->interptype == DMDA_Q0) {
1114:     if (dim > 2) {
1115:       P = dd->P / dd->coarsen_z;
1116:     } else {
1117:       P = 1;
1118:     }
1119:   } else {
1120:     P = 1 + (dd->P - 1) / dd->coarsen_z;
1121:   }
1122:   DMDACreate(PetscObjectComm((PetscObject)da),&da2);
1123:   DMSetOptionsPrefix(da2,((PetscObject)da)->prefix);
1124:   DMSetDimension(da2,dim);
1125:   DMDASetSizes(da2,M,N,P);
1126:   DMDASetNumProcs(da2,dd->m,dd->n,dd->p);
1127:   DMDASetBoundaryType(da2,dd->bx,dd->by,dd->bz);
1128:   DMDASetDof(da2,dd->w);
1129:   DMDASetStencilType(da2,dd->stencil_type);
1130:   DMDASetStencilWidth(da2,dd->s);
1131:   if (dim == 3) {
1132:     PetscInt *lx,*ly,*lz;
1133:     PetscMalloc3(dd->m,&lx,dd->n,&ly,dd->p,&lz);
1134:     DMDACoarsenOwnershipRanges(da,(PetscBool)(dd->bx == DM_BOUNDARY_PERIODIC || dd->interptype == DMDA_Q0),dd->s,dd->coarsen_x,dd->m,dd->lx,lx);
1135:     DMDACoarsenOwnershipRanges(da,(PetscBool)(dd->by == DM_BOUNDARY_PERIODIC || dd->interptype == DMDA_Q0),dd->s,dd->coarsen_y,dd->n,dd->ly,ly);
1136:     DMDACoarsenOwnershipRanges(da,(PetscBool)(dd->bz == DM_BOUNDARY_PERIODIC || dd->interptype == DMDA_Q0),dd->s,dd->coarsen_z,dd->p,dd->lz,lz);
1137:     DMDASetOwnershipRanges(da2,lx,ly,lz);
1138:     PetscFree3(lx,ly,lz);
1139:   } else if (dim == 2) {
1140:     PetscInt *lx,*ly;
1141:     PetscMalloc2(dd->m,&lx,dd->n,&ly);
1142:     DMDACoarsenOwnershipRanges(da,(PetscBool)(dd->bx == DM_BOUNDARY_PERIODIC || dd->interptype == DMDA_Q0),dd->s,dd->coarsen_x,dd->m,dd->lx,lx);
1143:     DMDACoarsenOwnershipRanges(da,(PetscBool)(dd->by == DM_BOUNDARY_PERIODIC || dd->interptype == DMDA_Q0),dd->s,dd->coarsen_y,dd->n,dd->ly,ly);
1144:     DMDASetOwnershipRanges(da2,lx,ly,NULL);
1145:     PetscFree2(lx,ly);
1146:   } else if (dim == 1) {
1147:     PetscInt *lx;
1148:     PetscMalloc1(dd->m,&lx);
1149:     DMDACoarsenOwnershipRanges(da,(PetscBool)(dd->bx == DM_BOUNDARY_PERIODIC || dd->interptype == DMDA_Q0),dd->s,dd->coarsen_x,dd->m,dd->lx,lx);
1150:     DMDASetOwnershipRanges(da2,lx,NULL,NULL);
1151:     PetscFree(lx);
1152:   }
1153:   dd2 = (DM_DA*)da2->data;

1155:   /* allow overloaded (user replaced) operations to be inherited by refinement clones; why are only some inherited and not all? */
1156:   /* da2->ops->createinterpolation = da->ops->createinterpolation; copying this one causes trouble for DMSetVI */
1157:   da2->ops->creatematrix = da->ops->creatematrix;
1158:   da2->ops->getcoloring  = da->ops->getcoloring;
1159:   dd2->interptype        = dd->interptype;

1161:   /* copy fill information if given */
1162:   if (dd->dfill) {
1163:     PetscMalloc1(dd->dfill[dd->w]+dd->w+1,&dd2->dfill);
1164:     PetscMemcpy(dd2->dfill,dd->dfill,(dd->dfill[dd->w]+dd->w+1)*sizeof(PetscInt));
1165:   }
1166:   if (dd->ofill) {
1167:     PetscMalloc1(dd->ofill[dd->w]+dd->w+1,&dd2->ofill);
1168:     PetscMemcpy(dd2->ofill,dd->ofill,(dd->ofill[dd->w]+dd->w+1)*sizeof(PetscInt));
1169:   }
1170:   /* copy the refine information */
1171:   dd2->coarsen_x = dd2->refine_x = dd->coarsen_x;
1172:   dd2->coarsen_y = dd2->refine_y = dd->coarsen_y;
1173:   dd2->coarsen_z = dd2->refine_z = dd->coarsen_z;

1175:   if (dd->refine_z_hier) {
1176:     if (da->levelup - da->leveldown -1 > -1 && da->levelup - da->leveldown - 1< dd->refine_z_hier_n) {
1177:       dd2->refine_z = dd->refine_z_hier[da->levelup - da->leveldown - 1];
1178:     }
1179:     if (da->levelup - da->leveldown - 2 > -1 && da->levelup - da->leveldown - 2 < dd->refine_z_hier_n) {
1180:       dd2->coarsen_z = dd->refine_z_hier[da->levelup - da->leveldown - 2];
1181:     }
1182:     dd2->refine_z_hier_n = dd->refine_z_hier_n;
1183:     PetscMalloc1(dd2->refine_z_hier_n,&dd2->refine_z_hier);
1184:     PetscMemcpy(dd2->refine_z_hier,dd->refine_z_hier,dd2->refine_z_hier_n*sizeof(PetscInt));
1185:   }
1186:   if (dd->refine_y_hier) {
1187:     if (da->levelup - da->leveldown - 1 > -1 && da->levelup - da->leveldown - 1< dd->refine_y_hier_n) {
1188:       dd2->refine_y = dd->refine_y_hier[da->levelup - da->leveldown - 1];
1189:     }
1190:     if (da->levelup - da->leveldown - 2 > -1 && da->levelup - da->leveldown - 2 < dd->refine_y_hier_n) {
1191:       dd2->coarsen_y = dd->refine_y_hier[da->levelup - da->leveldown - 2];
1192:     }
1193:     dd2->refine_y_hier_n = dd->refine_y_hier_n;
1194:     PetscMalloc1(dd2->refine_y_hier_n,&dd2->refine_y_hier);
1195:     PetscMemcpy(dd2->refine_y_hier,dd->refine_y_hier,dd2->refine_y_hier_n*sizeof(PetscInt));
1196:   }
1197:   if (dd->refine_x_hier) {
1198:     if (da->levelup - da->leveldown - 1 > -1 && da->levelup - da->leveldown - 1 < dd->refine_x_hier_n) {
1199:       dd2->refine_x = dd->refine_x_hier[da->levelup - da->leveldown - 1];
1200:     }
1201:     if (da->levelup - da->leveldown - 2 > -1 && da->levelup - da->leveldown - 2 < dd->refine_x_hier_n) {
1202:       dd2->coarsen_x = dd->refine_x_hier[da->levelup - da->leveldown - 2];
1203:     }
1204:     dd2->refine_x_hier_n = dd->refine_x_hier_n;
1205:     PetscMalloc1(dd2->refine_x_hier_n,&dd2->refine_x_hier);
1206:     PetscMemcpy(dd2->refine_x_hier,dd->refine_x_hier,dd2->refine_x_hier_n*sizeof(PetscInt));
1207:   }

1209:   /* copy vector type information */
1210:   DMSetVecType(da2,da->vectype);

1212:   dd2->lf = dd->lf;
1213:   dd2->lj = dd->lj;

1215:   da2->leveldown = da->leveldown + 1;
1216:   da2->levelup   = da->levelup;

1218:   DMSetUp(da2);

1220:   /* inject coordinates if they are set on the fine grid */
1221:   if (da->coordinates) {
1222:     DM         cdaf,cdac;
1223:     Vec        coordsc,coordsf;
1224:     Mat        inject;
1225:     VecScatter vscat;

1227:     DMGetCoordinateDM(da,&cdaf);
1228:     DMGetCoordinates(da,&coordsf);
1229:     DMGetCoordinateDM(da2,&cdac);
1230:     /* force creation of the coordinate vector */
1231:     DMDASetUniformCoordinates(da2,0.0,1.0,0.0,1.0,0.0,1.0);
1232:     DMGetCoordinates(da2,&coordsc);

1234:     DMCreateInjection(cdac,cdaf,&inject);
1235:     MatScatterGetVecScatter(inject,&vscat);
1236:     VecScatterBegin(vscat,coordsf,coordsc,INSERT_VALUES,SCATTER_FORWARD);
1237:     VecScatterEnd(vscat,coordsf,coordsc,INSERT_VALUES,SCATTER_FORWARD);
1238:     MatDestroy(&inject);
1239:   }

1241:   for (i=0; i<da->bs; i++) {
1242:     const char *fieldname;
1243:     DMDAGetFieldName(da,i,&fieldname);
1244:     DMDASetFieldName(da2,i,fieldname);
1245:   }

1247:   *daref = da2;
1248:   return(0);
1249: }

1251: PetscErrorCode  DMRefineHierarchy_DA(DM da,PetscInt nlevels,DM daf[])
1252: {
1254:   PetscInt       i,n,*refx,*refy,*refz;

1258:   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
1259:   if (nlevels == 0) return(0);

1262:   /* Get refinement factors, defaults taken from the coarse DMDA */
1263:   PetscMalloc3(nlevels,&refx,nlevels,&refy,nlevels,&refz);
1264:   for (i=0; i<nlevels; i++) {
1265:     DMDAGetRefinementFactor(da,&refx[i],&refy[i],&refz[i]);
1266:   }
1267:   n    = nlevels;
1268:   PetscOptionsGetIntArray(((PetscObject)da)->options,((PetscObject)da)->prefix,"-da_refine_hierarchy_x",refx,&n,NULL);
1269:   n    = nlevels;
1270:   PetscOptionsGetIntArray(((PetscObject)da)->options,((PetscObject)da)->prefix,"-da_refine_hierarchy_y",refy,&n,NULL);
1271:   n    = nlevels;
1272:   PetscOptionsGetIntArray(((PetscObject)da)->options,((PetscObject)da)->prefix,"-da_refine_hierarchy_z",refz,&n,NULL);

1274:   DMDASetRefinementFactor(da,refx[0],refy[0],refz[0]);
1275:   DMRefine(da,PetscObjectComm((PetscObject)da),&daf[0]);
1276:   for (i=1; i<nlevels; i++) {
1277:     DMDASetRefinementFactor(daf[i-1],refx[i],refy[i],refz[i]);
1278:     DMRefine(daf[i-1],PetscObjectComm((PetscObject)da),&daf[i]);
1279:   }
1280:   PetscFree3(refx,refy,refz);
1281:   return(0);
1282: }

1284: PetscErrorCode  DMCoarsenHierarchy_DA(DM da,PetscInt nlevels,DM dac[])
1285: {
1287:   PetscInt       i;

1291:   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
1292:   if (nlevels == 0) return(0);
1294:   DMCoarsen(da,PetscObjectComm((PetscObject)da),&dac[0]);
1295:   for (i=1; i<nlevels; i++) {
1296:     DMCoarsen(dac[i-1],PetscObjectComm((PetscObject)da),&dac[i]);
1297:   }
1298:   return(0);
1299: }

1301:  #include <petscgll.h>

1303: PetscErrorCode DMDASetGLLCoordinates_1d(DM dm,PetscGLL *gll)
1304: {
1306:   PetscInt       i,j,n = gll->n,xs,xn,q;
1307:   PetscScalar    *xx;
1308:   PetscReal      h;
1309:   Vec            x;
1310:   DM_DA          *da = (DM_DA*)dm->data;

1313:   if (da->bx != DM_BOUNDARY_PERIODIC) {
1314:     DMDAGetInfo(dm,NULL,&q,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
1315:     q    = (q-1)/(n-1);  /* number of spectral elements */
1316:     h    = 2.0/q;
1317:     DMDAGetCorners(dm,&xs,NULL,NULL,&xn,NULL,NULL);
1318:     xs   = xs/(n-1);
1319:     xn   = xn/(n-1);
1320:     DMDASetUniformCoordinates(dm,-1.,1.,0.,0.,0.,0.);
1321:     DMGetCoordinates(dm,&x);
1322:     DMDAVecGetArray(dm,x,&xx);

1324:     /* loop over local spectral elements */
1325:     for (j=xs; j<xs+xn; j++) {
1326:       /*
1327:        Except for the first process, each process starts on the second GLL point of the first element on that process
1328:        */
1329:       for (i= (j == xs && xs > 0)? 1 : 0; i<n; i++) {
1330:         xx[j*(n-1) + i] = -1.0 + h*j + h*(gll->nodes[i]+1.0)/2.;
1331:       }
1332:     }
1333:     DMDAVecRestoreArray(dm,x,&xx);
1334:   } else SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_SUP,"Not yet implemented for periodic");
1335:   return(0);
1336: }

1338: /*@

1340:      DMDASetGLLCoordinates - Sets the global coordinates from -1 to 1 to the GLL points of as many GLL elements that fit the number of grid points

1342:    Collective on DM

1344:    Input Parameters:
1345: +   da - the DMDA object
1346: -   gll - the GLL object

1348:    Notes:
1349:     the parallel decomposition of grid points must correspond to the degree of the GLL. That is, the number of grid points
1350:           on each process much be divisible by the number of GLL elements needed per process. This depends on whether the DM is
1351:           periodic or not.

1353:    Level: advanced

1355: .seealso:   DMDACreate(), PetscGLLCreate(), DMGetCoordinates()
1356: @*/
1357: PetscErrorCode DMDASetGLLCoordinates(DM da,PetscGLL *gll)
1358: {

1362:   if (da->dim == 1) {
1363:     DMDASetGLLCoordinates_1d(da,gll);
1364:   } else SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_SUP,"Not yet implemented for 2 or 3d");
1365:   return(0);
1366: }

1368: PETSC_INTERN PetscErrorCode DMGetCompatibility_DA(DM da1,DM dm2,PetscBool *compatible,PetscBool *set)
1369: {
1371:   DM_DA          *dd1 = (DM_DA*)da1->data,*dd2;
1372:   DM             da2;
1373:   DMType         dmtype2;
1374:   PetscBool      isda,compatibleLocal;
1375:   PetscInt       i;

1378:   if (!da1->setupcalled) SETERRQ(PetscObjectComm((PetscObject)da1),PETSC_ERR_ARG_WRONGSTATE,"DMSetUp() must be called on first DM before DMGetCompatibility()");
1379:   DMGetType(dm2,&dmtype2);
1380:   PetscStrcmp(dmtype2,DMDA,&isda);
1381:   if (isda) {
1382:     da2 = dm2;
1383:     dd2 = (DM_DA*)da2->data;
1384:     if (!da2->setupcalled) SETERRQ(PetscObjectComm((PetscObject)da2),PETSC_ERR_ARG_WRONGSTATE,"DMSetUp() must be called on second DM before DMGetCompatibility()");
1385:     compatibleLocal = (PetscBool)(da1->dim == da2->dim);
1386:     if (compatibleLocal) compatibleLocal = (PetscBool)(compatibleLocal && (dd1->s == dd2->s)); /* Stencil width */
1387:     /*                                                                           Global size              ranks               Boundary type */
1388:     if (compatibleLocal)                 compatibleLocal = (PetscBool)(compatibleLocal && (dd1->M == dd2->M) && (dd1->m == dd2->m) && (dd1->bx == dd2->bx));
1389:     if (compatibleLocal && da1->dim > 1) compatibleLocal = (PetscBool)(compatibleLocal && (dd1->N == dd2->N) && (dd1->n == dd2->n) && (dd1->by == dd2->by));
1390:     if (compatibleLocal && da1->dim > 2) compatibleLocal = (PetscBool)(compatibleLocal && (dd1->P == dd2->P) && (dd1->p == dd2->p) && (dd1->bz == dd2->bz));
1391:     if (compatibleLocal) {
1392:       for (i=0; i<dd1->m; ++i) {
1393:         compatibleLocal = (PetscBool)(compatibleLocal && (dd1->lx[i] == dd2->lx[i]));           /* Local size     */
1394:       }
1395:     }
1396:     if (compatibleLocal && da1->dim > 1) {
1397:       for (i=0; i<dd1->n; ++i) {
1398:         compatibleLocal = (PetscBool)(compatibleLocal && (dd1->ly[i] == dd2->ly[i]));
1399:       }
1400:     }
1401:     if (compatibleLocal && da1->dim > 2) {
1402:       for (i=0; i<dd1->p; ++i) {
1403:         compatibleLocal = (PetscBool)(compatibleLocal && (dd1->lz[i] == dd2->lz[i]));
1404:       }
1405:     }
1406:     *compatible = compatibleLocal;
1407:     *set = PETSC_TRUE;
1408:   } else {
1409:     /* Decline to determine compatibility with other DM types */
1410:     *set = PETSC_FALSE;
1411:   }
1412:   return(0);
1413: }