Actual source code: dacreate.c

petsc-3.5.0 2014-06-30
Report Typos and Errors
  2: #include <petsc-private/dmdaimpl.h>    /*I   "petscdmda.h"   I*/

  6: PetscErrorCode  DMSetFromOptions_DA(DM da)
  7: {
  9:   DM_DA          *dd         = (DM_DA*)da->data;
 10:   PetscInt       refine      = 0,maxnlevels = 100,refx[100],refy[100],refz[100],n,i;
 11:   PetscBool      negativeMNP = PETSC_FALSE,bM = PETSC_FALSE,bN = PETSC_FALSE, bP = PETSC_FALSE,flg;


 16:   if (dd->M < 0) {
 17:     dd->M       = -dd->M;
 18:     bM          = PETSC_TRUE;
 19:     negativeMNP = PETSC_TRUE;
 20:   }
 21:   if (dd->dim > 1 && dd->N < 0) {
 22:     dd->N       = -dd->N;
 23:     bN          = PETSC_TRUE;
 24:     negativeMNP = PETSC_TRUE;
 25:   }
 26:   if (dd->dim > 2 && dd->P < 0) {
 27:     dd->P       = -dd->P;
 28:     bP          = PETSC_TRUE;
 29:     negativeMNP = PETSC_TRUE;
 30:   }

 32:   PetscOptionsHead("DMDA Options");
 33:   if (bM) {PetscOptionsInt("-da_grid_x","Number of grid points in x direction","DMDASetSizes",dd->M,&dd->M,NULL);}
 34:   if (bN) {PetscOptionsInt("-da_grid_y","Number of grid points in y direction","DMDASetSizes",dd->N,&dd->N,NULL);}
 35:   if (bP) {PetscOptionsInt("-da_grid_z","Number of grid points in z direction","DMDASetSizes",dd->P,&dd->P,NULL);}

 37:   PetscOptionsInt("-da_overlap","Decomposition overlap in all directions","DMDASetOverlap",dd->xol,&dd->xol,&flg);
 38:   if (flg) {DMDASetOverlap(da,dd->xol,dd->xol,dd->xol);}
 39:   PetscOptionsInt("-da_overlap_x","Decomposition overlap in x direction","DMDASetOverlap",dd->xol,&dd->xol,NULL);
 40:   if (dd->dim > 1) {PetscOptionsInt("-da_overlap_y","Decomposition overlap in y direction","DMDASetOverlap",dd->yol,&dd->yol,NULL);}
 41:   if (dd->dim > 2) {PetscOptionsInt("-da_overlap_z","Decomposition overlap in z direction","DMDASetOverlap",dd->zol,&dd->zol,NULL);}

 43:   PetscOptionsInt("-da_local_subdomains","","DMDASetNumLocalSubdomains",dd->Nsub,&dd->Nsub,&flg);
 44:   if (flg) {DMDASetNumLocalSubDomains(da,dd->Nsub);}

 46:   /* Handle DMDA parallel distibution */
 47:   PetscOptionsInt("-da_processors_x","Number of processors in x direction","DMDASetNumProcs",dd->m,&dd->m,NULL);
 48:   if (dd->dim > 1) {PetscOptionsInt("-da_processors_y","Number of processors in y direction","DMDASetNumProcs",dd->n,&dd->n,NULL);}
 49:   if (dd->dim > 2) {PetscOptionsInt("-da_processors_z","Number of processors in z direction","DMDASetNumProcs",dd->p,&dd->p,NULL);}
 50:   /* Handle DMDA refinement */
 51:   PetscOptionsInt("-da_refine_x","Refinement ratio in x direction","DMDASetRefinementFactor",dd->refine_x,&dd->refine_x,NULL);
 52:   if (dd->dim > 1) {PetscOptionsInt("-da_refine_y","Refinement ratio in y direction","DMDASetRefinementFactor",dd->refine_y,&dd->refine_y,NULL);}
 53:   if (dd->dim > 2) {PetscOptionsInt("-da_refine_z","Refinement ratio in z direction","DMDASetRefinementFactor",dd->refine_z,&dd->refine_z,NULL);}
 54:   dd->coarsen_x = dd->refine_x; dd->coarsen_y = dd->refine_y; dd->coarsen_z = dd->refine_z;

 56:   /* Get refinement factors, defaults taken from the coarse DMDA */
 57:   DMDAGetRefinementFactor(da,&refx[0],&refy[0],&refz[0]);
 58:   for (i=1; i<maxnlevels; i++) {
 59:     refx[i] = refx[0];
 60:     refy[i] = refy[0];
 61:     refz[i] = refz[0];
 62:   }
 63:   n    = maxnlevels;
 64:   PetscOptionsGetIntArray(((PetscObject)da)->prefix,"-da_refine_hierarchy_x",refx,&n,NULL);
 65:   if (da->levelup - da->leveldown >= 0) dd->refine_x = refx[da->levelup - da->leveldown];
 66:   if (da->levelup - da->leveldown >= 1) dd->coarsen_x = refx[da->levelup - da->leveldown - 1];
 67:   if (dd->dim > 1) {
 68:     n    = maxnlevels;
 69:     PetscOptionsGetIntArray(((PetscObject)da)->prefix,"-da_refine_hierarchy_y",refy,&n,NULL);
 70:     if (da->levelup - da->leveldown >= 0) dd->refine_y = refy[da->levelup - da->leveldown];
 71:     if (da->levelup - da->leveldown >= 1) dd->coarsen_y = refy[da->levelup - da->leveldown - 1];
 72:   }
 73:   if (dd->dim > 2) {
 74:     n    = maxnlevels;
 75:     PetscOptionsGetIntArray(((PetscObject)da)->prefix,"-da_refine_hierarchy_z",refz,&n,NULL);
 76:     if (da->levelup - da->leveldown >= 0) dd->refine_z = refz[da->levelup - da->leveldown];
 77:     if (da->levelup - da->leveldown >= 1) dd->coarsen_z = refz[da->levelup - da->leveldown - 1];
 78:   }

 80:   if (negativeMNP) {PetscOptionsInt("-da_refine","Uniformly refine DA one or more times","None",refine,&refine,NULL);}
 81:   PetscOptionsTail();

 83:   while (refine--) {
 84:     if (dd->bx == DM_BOUNDARY_PERIODIC || dd->interptype == DMDA_Q0) {
 85:       dd->M = dd->refine_x*dd->M;
 86:     } else {
 87:       dd->M = 1 + dd->refine_x*(dd->M - 1);
 88:     }
 89:     if (dd->by == DM_BOUNDARY_PERIODIC || dd->interptype == DMDA_Q0) {
 90:       dd->N = dd->refine_y*dd->N;
 91:     } else {
 92:       dd->N = 1 + dd->refine_y*(dd->N - 1);
 93:     }
 94:     if (dd->bz == DM_BOUNDARY_PERIODIC || dd->interptype == DMDA_Q0) {
 95:       dd->P = dd->refine_z*dd->P;
 96:     } else {
 97:       dd->P = 1 + dd->refine_z*(dd->P - 1);
 98:     }
 99:     da->levelup++;
100:     if (da->levelup - da->leveldown >= 0) {
101:       dd->refine_x = refx[da->levelup - da->leveldown];
102:       dd->refine_y = refy[da->levelup - da->leveldown];
103:       dd->refine_z = refz[da->levelup - da->leveldown];
104:     }
105:     if (da->levelup - da->leveldown >= 1) {
106:       dd->coarsen_x = refx[da->levelup - da->leveldown - 1];
107:       dd->coarsen_y = refy[da->levelup - da->leveldown - 1];
108:       dd->coarsen_z = refz[da->levelup - da->leveldown - 1];
109:     }
110:   }
111:   return(0);
112: }

114: extern PetscErrorCode  DMCreateGlobalVector_DA(DM,Vec*);
115: extern PetscErrorCode  DMCreateLocalVector_DA(DM,Vec*);
116: extern PetscErrorCode  DMGlobalToLocalBegin_DA(DM,Vec,InsertMode,Vec);
117: extern PetscErrorCode  DMGlobalToLocalEnd_DA(DM,Vec,InsertMode,Vec);
118: extern PetscErrorCode  DMLocalToGlobalBegin_DA(DM,Vec,InsertMode,Vec);
119: extern PetscErrorCode  DMLocalToGlobalEnd_DA(DM,Vec,InsertMode,Vec);
120: extern PetscErrorCode  DMLocalToLocalBegin_DA(DM,Vec,InsertMode,Vec);
121: extern PetscErrorCode  DMLocalToLocalEnd_DA(DM,Vec,InsertMode,Vec);
122: extern PetscErrorCode  DMCreateInterpolation_DA(DM,DM,Mat*,Vec*);
123: extern PetscErrorCode  DMCreateColoring_DA(DM,ISColoringType,ISColoring*);
124: extern PetscErrorCode  DMCreateMatrix_DA(DM,Mat*);
125: extern PetscErrorCode  DMCreateCoordinateDM_DA(DM,DM*);
126: extern PetscErrorCode  DMRefine_DA(DM,MPI_Comm,DM*);
127: extern PetscErrorCode  DMCoarsen_DA(DM,MPI_Comm,DM*);
128: extern PetscErrorCode  DMRefineHierarchy_DA(DM,PetscInt,DM[]);
129: extern PetscErrorCode  DMCoarsenHierarchy_DA(DM,PetscInt,DM[]);
130: extern PetscErrorCode  DMCreateInjection_DA(DM,DM,VecScatter*);
131: extern PetscErrorCode  DMCreateAggregates_DA(DM,DM,Mat*);
132: extern PetscErrorCode  DMView_DA(DM,PetscViewer);
133: extern PetscErrorCode  DMSetUp_DA(DM);
134: extern PetscErrorCode  DMDestroy_DA(DM);
135: extern PetscErrorCode  DMCreateDomainDecomposition_DA(DM,PetscInt*,char***,IS**,IS**,DM**);
136: extern PetscErrorCode  DMCreateDomainDecompositionScatters_DA(DM,PetscInt,DM*,VecScatter**,VecScatter**,VecScatter**);

140: PetscErrorCode DMLoad_DA(DM da,PetscViewer viewer)
141: {
142:   PetscErrorCode   ierr;
143:   PetscInt         dim,m,n,p,dof,swidth;
144:   DMDAStencilType  stencil;
145:   DMBoundaryType   bx,by,bz;
146:   PetscBool        coors;
147:   DM               dac;
148:   Vec              c;

151:   PetscViewerBinaryRead(viewer,&dim,1,PETSC_INT);
152:   PetscViewerBinaryRead(viewer,&m,1,PETSC_INT);
153:   PetscViewerBinaryRead(viewer,&n,1,PETSC_INT);
154:   PetscViewerBinaryRead(viewer,&p,1,PETSC_INT);
155:   PetscViewerBinaryRead(viewer,&dof,1,PETSC_INT);
156:   PetscViewerBinaryRead(viewer,&swidth,1,PETSC_INT);
157:   PetscViewerBinaryRead(viewer,&bx,1,PETSC_ENUM);
158:   PetscViewerBinaryRead(viewer,&by,1,PETSC_ENUM);
159:   PetscViewerBinaryRead(viewer,&bz,1,PETSC_ENUM);
160:   PetscViewerBinaryRead(viewer,&stencil,1,PETSC_ENUM);

162:   DMDASetDim(da, dim);
163:   DMDASetSizes(da, m,n,p);
164:   DMDASetBoundaryType(da, bx, by, bz);
165:   DMDASetDof(da, dof);
166:   DMDASetStencilType(da, stencil);
167:   DMDASetStencilWidth(da, swidth);
168:   DMSetUp(da);
169:   PetscViewerBinaryRead(viewer,&coors,1,PETSC_ENUM);
170:   if (coors) {
171:     DMGetCoordinateDM(da,&dac);
172:     DMCreateGlobalVector(dac,&c);
173:     VecLoad(c,viewer);
174:     DMSetCoordinates(da,c);
175:     VecDestroy(&c);
176:   }
177:   return(0);
178: }

182: PetscErrorCode DMCreateSubDM_DA(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
183: {
184:   DM_DA         *da = (DM_DA*) dm->data;
185:   PetscSection   section;

189:   if (subdm) {
190:     PetscSF sf;
191:     Vec     coords;
192:     void   *ctx;
193:     /* Cannot use DMClone since the dof stuff is mixed in. Ugh
194:     DMClone(dm, subdm); */
195:     DMCreate(PetscObjectComm((PetscObject)dm), subdm);
196:     DMGetPointSF(dm, &sf);
197:     DMSetPointSF(*subdm, sf);
198:     DMGetApplicationContext(dm, &ctx);
199:     DMSetApplicationContext(*subdm, ctx);
200:     DMGetCoordinatesLocal(dm, &coords);
201:     if (coords) {
202:       DMSetCoordinatesLocal(*subdm, coords);
203:     } else {
204:       DMGetCoordinates(dm, &coords);
205:       if (coords) {DMSetCoordinates(*subdm, coords);}
206:     }

208:     DMSetType(*subdm, DMDA);
209:     DMDASetDim(*subdm, da->dim);
210:     DMDASetSizes(*subdm, da->M, da->N, da->P);
211:     DMDASetNumProcs(*subdm, da->m, da->n, da->p);
212:     DMDASetBoundaryType(*subdm, da->bx, da->by, da->bz);
213:     DMDASetDof(*subdm, numFields);
214:     DMDASetStencilType(*subdm, da->stencil_type);
215:     DMDASetStencilWidth(*subdm, da->s);
216:     DMDASetOwnershipRanges(*subdm, da->lx, da->ly, da->lz);
217:   }
218:   DMGetDefaultSection(dm, &section);
219:   if (section) {
220:     DMCreateSubDM_Section_Private(dm, numFields, fields, is, subdm);
221:   } else {
222:     if (is) {
223:       PetscInt *indices, cnt = 0, dof = da->w, i, j;

225:       PetscMalloc1(da->Nlocal*numFields/dof, &indices);
226:       for (i = da->base/dof; i < (da->base+da->Nlocal)/dof; ++i) {
227:         for (j = 0; j < numFields; ++j) {
228:           indices[cnt++] = dof*i + fields[j];
229:         }
230:       }
231:       if (cnt != da->Nlocal*numFields/dof) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Count %d does not equal expected value %d", cnt, da->Nlocal*numFields/dof);
232:       ISCreateGeneral(PetscObjectComm((PetscObject) dm), cnt, indices, PETSC_OWN_POINTER, is);
233:     }
234:   }
235:   return(0);
236: }

240: PetscErrorCode DMCreateFieldDecomposition_DA(DM dm, PetscInt *len,char ***namelist, IS **islist, DM **dmlist)
241: {
242:   PetscInt       i;
244:   DM_DA          *dd = (DM_DA*)dm->data;
245:   PetscInt       dof = dd->w;

248:   if (len) *len = dof;
249:   if (islist) {
250:     Vec      v;
251:     PetscInt rstart,n;

253:     DMGetGlobalVector(dm,&v);
254:     VecGetOwnershipRange(v,&rstart,NULL);
255:     VecGetLocalSize(v,&n);
256:     DMRestoreGlobalVector(dm,&v);
257:     PetscMalloc1(dof,islist);
258:     for (i=0; i<dof; i++) {
259:       ISCreateStride(PetscObjectComm((PetscObject)dm),n/dof,rstart+i,dof,&(*islist)[i]);
260:     }
261:   }
262:   if (namelist) {
263:     PetscMalloc1(dof, namelist);
264:     if (dd->fieldname) {
265:       for (i=0; i<dof; i++) {
266:         PetscStrallocpy(dd->fieldname[i],&(*namelist)[i]);
267:       }
268:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Currently DMDA must have fieldnames");
269:   }
270:   if (dmlist) {
271:     DM da;

273:     DMDACreate(PetscObjectComm((PetscObject)dm), &da);
274:     DMDASetDim(da, dd->dim);
275:     DMDASetSizes(da, dd->M, dd->N, dd->P);
276:     DMDASetNumProcs(da, dd->m, dd->n, dd->p);
277:     DMDASetBoundaryType(da, dd->bx, dd->by, dd->bz);
278:     DMDASetDof(da, 1);
279:     DMDASetStencilType(da, dd->stencil_type);
280:     DMDASetStencilWidth(da, dd->s);
281:     DMSetUp(da);
282:     PetscMalloc1(dof,dmlist);
283:     for (i=0; i<dof-1; i++) {PetscObjectReference((PetscObject)da);}
284:     for (i=0; i<dof; i++) (*dmlist)[i] = da;
285:   }
286:   return(0);
287: }

291: PetscErrorCode DMClone_DA(DM dm, DM *newdm)
292: {
293:   DM_DA         *da = (DM_DA *) dm->data;

297:   DMSetType(*newdm, DMDA);
298:   DMDASetDim(*newdm, da->dim);
299:   DMDASetSizes(*newdm, da->M, da->N, da->P);
300:   DMDASetNumProcs(*newdm, da->m, da->n, da->p);
301:   DMDASetBoundaryType(*newdm, da->bx, da->by, da->bz);
302:   DMDASetDof(*newdm, da->w);
303:   DMDASetStencilType(*newdm, da->stencil_type);
304:   DMDASetStencilWidth(*newdm, da->s);
305:   DMDASetOwnershipRanges(*newdm, da->lx, da->ly, da->lz);
306:   DMSetUp(*newdm);
307:   return(0);
308: }

310: /*MC
311:    DMDA = "da" - A DM object that is used to manage data for a structured grid in 1, 2, or 3 dimensions.
312:          In the global representation of the vector each process stores a non-overlapping rectangular (or slab in 3d) portion of the grid points.
313:          In the local representation these rectangular regions (slabs) are extended in all directions by a stencil width.

315:          The vectors can be thought of as either cell centered or vertex centered on the mesh. But some variables cannot be cell centered and others
316:          vertex centered.

318:   Level: intermediate

320: .seealso: DMType, DMCOMPOSITE, DMDACreate(), DMCreate(), DMSetType()
321: M*/


326: PETSC_EXTERN PetscErrorCode DMCreate_DA(DM da)
327: {
329:   DM_DA          *dd;

333:   PetscNewLog(da,&dd);
334:   da->data = dd;

336:   dd->dim        = -1;
337:   dd->interptype = DMDA_Q1;
338:   dd->refine_x   = 2;
339:   dd->refine_y   = 2;
340:   dd->refine_z   = 2;
341:   dd->coarsen_x  = 2;
342:   dd->coarsen_y  = 2;
343:   dd->coarsen_z  = 2;
344:   dd->fieldname  = NULL;
345:   dd->nlocal     = -1;
346:   dd->Nlocal     = -1;
347:   dd->M          = -1;
348:   dd->N          = -1;
349:   dd->P          = -1;
350:   dd->m          = -1;
351:   dd->n          = -1;
352:   dd->p          = -1;
353:   dd->w          = -1;
354:   dd->s          = -1;

356:   dd->xs = -1; dd->xe = -1; dd->ys = -1; dd->ye = -1; dd->zs = -1; dd->ze = -1;
357:   dd->Xs = -1; dd->Xe = -1; dd->Ys = -1; dd->Ye = -1; dd->Zs = -1; dd->Ze = -1;

359:   dd->Nsub            = 1;
360:   dd->xol             = 0;
361:   dd->yol             = 0;
362:   dd->zol             = 0;
363:   dd->xo              = 0;
364:   dd->yo              = 0;
365:   dd->zo              = 0;
366:   dd->Mo              = -1;
367:   dd->No              = -1;
368:   dd->Po              = -1;

370:   dd->gtol         = NULL;
371:   dd->ltol         = NULL;
372:   dd->ao           = NULL;
373:   dd->base         = -1;
374:   dd->bx           = DM_BOUNDARY_NONE;
375:   dd->by           = DM_BOUNDARY_NONE;
376:   dd->bz           = DM_BOUNDARY_NONE;
377:   dd->stencil_type = DMDA_STENCIL_BOX;
378:   dd->interptype   = DMDA_Q1;
379:   dd->lx           = NULL;
380:   dd->ly           = NULL;
381:   dd->lz           = NULL;

383:   dd->elementtype = DMDA_ELEMENT_Q1;

385:   da->ops->globaltolocalbegin          = DMGlobalToLocalBegin_DA;
386:   da->ops->globaltolocalend            = DMGlobalToLocalEnd_DA;
387:   da->ops->localtoglobalbegin          = DMLocalToGlobalBegin_DA;
388:   da->ops->localtoglobalend            = DMLocalToGlobalEnd_DA;
389:   da->ops->localtolocalbegin           = DMLocalToLocalBegin_DA;
390:   da->ops->localtolocalend             = DMLocalToLocalEnd_DA;
391:   da->ops->createglobalvector          = DMCreateGlobalVector_DA;
392:   da->ops->createlocalvector           = DMCreateLocalVector_DA;
393:   da->ops->createinterpolation         = DMCreateInterpolation_DA;
394:   da->ops->getcoloring                 = DMCreateColoring_DA;
395:   da->ops->creatematrix                = DMCreateMatrix_DA;
396:   da->ops->refine                      = DMRefine_DA;
397:   da->ops->coarsen                     = DMCoarsen_DA;
398:   da->ops->refinehierarchy             = DMRefineHierarchy_DA;
399:   da->ops->coarsenhierarchy            = DMCoarsenHierarchy_DA;
400:   da->ops->getinjection                = DMCreateInjection_DA;
401:   da->ops->getaggregates               = DMCreateAggregates_DA;
402:   da->ops->destroy                     = DMDestroy_DA;
403:   da->ops->view                        = 0;
404:   da->ops->setfromoptions              = DMSetFromOptions_DA;
405:   da->ops->setup                       = DMSetUp_DA;
406:   da->ops->clone                       = DMClone_DA;
407:   da->ops->load                        = DMLoad_DA;
408:   da->ops->createcoordinatedm          = DMCreateCoordinateDM_DA;
409:   da->ops->createsubdm                 = DMCreateSubDM_DA;
410:   da->ops->createfielddecomposition    = DMCreateFieldDecomposition_DA;
411:   da->ops->createdomaindecomposition   = DMCreateDomainDecomposition_DA;
412:   da->ops->createddscatters            = DMCreateDomainDecompositionScatters_DA;
413:   return(0);
414: }

418: /*@
419:   DMDACreate - Creates a DMDA object.

421:   Collective on MPI_Comm

423:   Input Parameter:
424: . comm - The communicator for the DMDA object

426:   Output Parameter:
427: . da  - The DMDA object

429:   Level: advanced

431:   Developers Note: Since there exists DMDACreate1/2/3d() should this routine even exist?

433: .keywords: DMDA, create
434: .seealso:  DMDASetSizes(), DMDADuplicate(),  DMDACreate1d(), DMDACreate2d(), DMDACreate3d()
435: @*/
436: PetscErrorCode  DMDACreate(MPI_Comm comm, DM *da)
437: {

442:   DMCreate(comm,da);
443:   DMSetType(*da,DMDA);
444:   return(0);
445: }